1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #define private public
19 #define protected public
20 #include "hilog_tag_wrapper.h"
21 #include "js_runtime.h"
22 #include "js_environment.h"
23 #include "js_runtime_lite.h"
24 #undef private
25 #undef protected
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace AbilityRuntime {
32
33 class JsRuntimeLiteTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39 };
40
SetUpTestCase()41 void JsRuntimeLiteTest::SetUpTestCase() {}
42
TearDownTestCase()43 void JsRuntimeLiteTest::TearDownTestCase() {}
44
SetUp()45 void JsRuntimeLiteTest::SetUp() {}
46
TearDown()47 void JsRuntimeLiteTest::TearDown() {}
48
49 /**
50 * @tc.name: JsRuntimeLiteTest_001
51 * @tc.desc: basic function test.
52 * @tc.type: FUNC
53 */
54 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_001, TestSize.Level1)
55 {
56 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_001 start");
57 Options options;
58 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
59 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
60 EXPECT_EQ(err, napi_status::napi_ok);
61
62 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()));
63 EXPECT_EQ(err, napi_status::napi_ok);
64 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_001 end");
65 }
66
67 /**
68 * @tc.name: JsRuntimeLiteTest_002
69 * @tc.desc: basic function test.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_002, TestSize.Level1)
73 {
74 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_002 start");
75 Options options;
76 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
77 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
78 EXPECT_EQ(err, napi_status::napi_ok);
79
80 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv2 = nullptr;
81 err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv2);
82 EXPECT_EQ(err, napi_status::napi_create_ark_runtime_only_one_env_per_thread);
83
84 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()));
85 EXPECT_EQ(err, napi_status::napi_ok);
86
87 err = JsRuntimeLite::GetInstance().RemoveJsEnv(reinterpret_cast<napi_env>(jsEnv2->GetNativeEngine()));
88 EXPECT_EQ(err, napi_status::napi_destroy_ark_runtime_env_not_exist);
89 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_002 end");
90 }
91
92 /**
93 * @tc.name: JsRuntimeLiteTest_003
94 * @tc.desc: basic function test.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(JsRuntimeLiteTest, JsRuntimeLiteTest_003, TestSize.Level1)
98 {
99 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_003 start");
100 Options options;
101 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
102 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
103 EXPECT_EQ(err, napi_status::napi_ok);
104
105 napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine());
106 err = JsRuntimeLite::GetInstance().Init(options, env);
107 EXPECT_EQ(err, napi_status::napi_ok);
108
109 err = JsRuntimeLite::GetInstance().RemoveJsEnv(env);
110 EXPECT_EQ(err, napi_status::napi_ok);
111 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeLiteTest_003 end");
112 }
113
114 /**
115 * @tc.name: GetChildOptions_0100
116 * @tc.desc: JsRuntime test for GetChildOptions.
117 * @tc.type: FUNC
118 */
119 HWTEST_F(JsRuntimeLiteTest, GetChildOptions_0100, TestSize.Level1)
120 {
121 auto child = JsRuntimeLite::GetInstance().GetChildOptions();
122 EXPECT_TRUE(child == nullptr);
123 }
124
125 /**
126 * @tc.name: GetPkgContextInfoListMap_0100
127 * @tc.desc: JsRuntimeLiteTest test for GetPkgContextInfoListMap.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(JsRuntimeLiteTest, GetPkgContextInfoListMap_0100, TestSize.Level2)
131 {
132 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 start");
133
134 std::map<std::string, std::string> modulePkgContentMap;
135 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
136 "library","version":"1.0.0","entryPath":"","isSO":false}})";
137 modulePkgContentMap["entry"] = pkgContentJsonString;
138
139 AbilityRuntime::Runtime::Options options;
140 options.preload = true;
141 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
142 std::map<std::string, std::vector<std::vector<std::string>>> ret;
143 std::map<std::string, std::string> pkgAliasMap;
144 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
145 std::string expectString = "library:packageName:library:bundleName:";
146 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
147 auto it = ret.find("entry");
148 ASSERT_EQ(it, ret.end());
149 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 end");
150 }
151
152 /**
153 * @tc.name: GetPkgContextInfoListMap_0200
154 * @tc.desc: JsRuntimeLiteTest test for GetPkgContextInfoListMap.
155 * @tc.type: FUNC
156 */
157 HWTEST_F(JsRuntimeLiteTest, GetPkgContextInfoListMap_0200, TestSize.Level2)
158 {
159 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 start");
160
161 std::map<std::string, std::string> modulePkgContentMap;
162 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
163 "library","version":"1.0.0","entryPath":"","isSO":false}})";
164 modulePkgContentMap["entry"] = pkgContentJsonString;
165
166 std::string libraryString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
167 "library","version":"1.0.0","entryPath":"","isSO":false}})";
168 modulePkgContentMap["library"] = libraryString;
169
170 AbilityRuntime::Runtime::Options options;
171 options.preload = true;
172 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
173 std::map<std::string, std::vector<std::vector<std::string>>> ret;
174 std::map<std::string, std::string> pkgAliasMap;
175 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
176 std::string expectString = "library:packageName:library:bundleName:";
177 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
178 auto it = ret.find("entry");
179 ASSERT_EQ(it, ret.end());
180 auto libraryIt = ret.find("library");
181 ASSERT_EQ(libraryIt, ret.end());
182 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 end");
183 }
184
185 /**
186 * @tc.name: Init_0100
187 * @tc.desc: JsRuntimeLiteTest test for Init.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(JsRuntimeLiteTest, Init_0100, TestSize.Level1)
191 {
192 napi_env env = {};
193 Options options;
194 JsRuntimeLite::GetInstance().envMap_.clear();
195
196 auto ret = JsRuntimeLite::GetInstance().Init(options, env);
197 EXPECT_EQ(ret, napi_status::napi_generic_failure);
198 }
199
200 /**
201 * @tc.name: Init_0200
202 * @tc.desc: JsRuntimeLiteTest test for Init.
203 * @tc.type: FUNC
204 */
205 HWTEST_F(JsRuntimeLiteTest, Init_0200, TestSize.Level1)
206 {
207 napi_env env = {};
208 Options options;
209 auto jsEnv = std::make_shared<JsEnv::JsEnvironment>();
210 jsEnv->vm_ = nullptr;
211 JsRuntimeLite::GetInstance().envMap_.emplace(env, jsEnv);
212
213 auto ret = JsRuntimeLite::GetInstance().Init(options, env);
214 EXPECT_EQ(ret, napi_status::napi_generic_failure);
215 }
216
217 /**
218 * @tc.name: AddEnv_0100
219 * @tc.desc: JsRuntimeLiteTest test for AddEnv.
220 * @tc.type: FUNC
221 */
222 HWTEST_F(JsRuntimeLiteTest, AddEnv_0100, TestSize.Level1)
223 {
224 napi_env env = {};
225 auto jsEnv = std::make_shared<JsEnv::JsEnvironment>();
226 JsRuntimeLite::GetInstance().threadIds_.clear();
227 JsRuntimeLite::GetInstance().envMap_.clear();
228 JsRuntimeLite::GetInstance().envMap_.emplace(env, jsEnv);
229
230 auto ret = JsRuntimeLite::GetInstance().AddEnv(env, jsEnv);
231 EXPECT_EQ(ret, napi_status::napi_generic_failure);
232 }
233
234 /**
235 * @tc.name: GetEcmaVm_0100
236 * @tc.desc: JsRuntimeLiteTest test for GetEcmaVm.
237 * @tc.type: FUNC
238 */
239 HWTEST_F(JsRuntimeLiteTest, GetEcmaVm_0100, TestSize.Level1)
240 {
241 auto ret = JsRuntimeLite::GetInstance().GetEcmaVm(nullptr);
242 EXPECT_EQ(ret, nullptr);
243 }
244
245 /**
246 * @tc.name: GetJsEnv_0100
247 * @tc.desc: JsRuntimeLiteTest test for GetJsEnv.
248 * @tc.type: FUNC
249 */
250 HWTEST_F(JsRuntimeLiteTest, GetJsEnv_0100, TestSize.Level1)
251 {
252 napi_env env = {};
253 JsRuntimeLite::GetInstance().envMap_.clear();
254
255 auto ret = JsRuntimeLite::GetInstance().GetJsEnv(env);
256 EXPECT_EQ(ret, nullptr);
257 }
258
259 /**
260 * @tc.name: InitLoop_0100
261 * @tc.desc: JsRuntimeLiteTest test for InitLoop.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(JsRuntimeLiteTest, InitLoop_0100, TestSize.Level1)
265 {
266 auto ret = JsRuntimeLite::GetInstance().InitLoop(nullptr);
267 EXPECT_EQ(ret, false);
268 }
269
270 /**
271 * @tc.name: ParsePkgContextInfoJsonString_0100
272 * @tc.desc: JsRuntimeLiteTest test for ParsePkgContextInfoJsonString.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(JsRuntimeLiteTest, ParsePkgContextInfoJsonString_0100, TestSize.Level1)
276 {
277 nlohmann::json itemObject;
278 itemObject["key"] = "value";
279 std::string key = "key";
280 std::vector<std::string> items = {};
281 JsRuntimeLite::GetInstance().ParsePkgContextInfoJsonString(itemObject, key, items);
282 auto rBeginIt = items.rbegin();
283 EXPECT_EQ(*rBeginIt, "value");
284 }
285
286 /**
287 * @tc.name: ParsePkgContextInfoJsonString_0200
288 * @tc.desc: JsRuntimeLiteTest test for ParsePkgContextInfoJsonString.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(JsRuntimeLiteTest, ParsePkgContextInfoJsonString_0200, TestSize.Level1)
292 {
293 nlohmann::json itemObject;
294 itemObject["key"] = "value";
295 std::string key = "FakeKey";
296 std::vector<std::string> items = {};
297 JsRuntimeLite::GetInstance().ParsePkgContextInfoJsonString(itemObject, key, items);
298 auto rBeginIt = items.rbegin();
299 EXPECT_EQ(*rBeginIt, "");
300 }
301 } // namespace AbilityRuntime
302 } // namespace OHOS