• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "js_runtime.h"
21 #include "js_runtime_utils.h"
22 #include "js_worker.h"
23 #undef private
24 #undef protected
25 
26 #include "event_runner.h"
27 #include "mock_js_runtime.h"
28 #include "hilog_wrapper.h"
29 
30 using namespace testing;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 namespace {
36 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
37 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
38 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
39 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
40 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
41 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
42 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curJsModulePath";
43 }  // namespace
44 class JsRuntimeTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp() override;
49     void TearDown() override;
50 
51     Runtime::Options options_;
52 };
53 
SetUpTestCase()54 void JsRuntimeTest::SetUpTestCase()
55 {}
56 
TearDownTestCase()57 void JsRuntimeTest::TearDownTestCase()
58 {}
59 
SetUp()60 void JsRuntimeTest::SetUp()
61 {
62     options_.bundleName = TEST_BUNDLE_NAME;
63     options_.codePath = TEST_CODE_PATH;
64     options_.hapPath = TEST_HAP_PATH;
65     options_.loadAce = true;
66     options_.isBundle = true;
67     options_.preload = false;
68     std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
69     options_.eventRunner = eventRunner;
70 }
71 
TearDown()72 void JsRuntimeTest::TearDown()
73 {}
74 
75 /**
76  * @tc.name: JsRuntimeTest_0100
77  * @tc.desc: JsRuntime Test
78  * @tc.type: FUNC
79  * @tc.require: issueI581SE
80  */
81 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0100, TestSize.Level0)
82 {
83     options_.preload = true;
84     std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
85     EXPECT_TRUE(jsRuntime != nullptr);
86 
87     jsRuntime = nullptr;
88     options_.preload = false;
89     jsRuntime = JsRuntime::Create(options_);
90     EXPECT_TRUE(jsRuntime != nullptr);
91 }
92 
93 /**
94  * @tc.name: JsRuntimeTest_0200
95  * @tc.desc: JsRuntime Test
96  * @tc.type: FUNC
97  * @tc.require: issueI581RO
98  */
99 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0200, TestSize.Level0)
100 {
101     std::string appLibPathKey = TEST_BUNDLE_NAME + TEST_MODULE_NAME;
102     std::string libPath = TEST_LIB_PATH;
103     options_.appLibPaths[appLibPathKey].emplace_back(libPath);
104     std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
105     EXPECT_TRUE(jsRuntime != nullptr);
106 }
107 
108 /**
109  * @tc.name: JsRuntimeUtilsTest_0100
110  * @tc.desc: JsRuntimeUtils Test
111  * @tc.type: FUNC
112  * @tc.require: issueI581RO
113  */
114 HWTEST_F(JsRuntimeTest, JsRuntimeUtilsTest_0100, TestSize.Level0)
115 {
116     auto runtime = AbilityRuntime::Runtime::Create(options_);
117     auto& jsEngine = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNativeEngine();
118 
119     NativeReference* callbackRef = jsEngine.CreateReference(jsEngine.CreateUndefined(), 1);
120     std::unique_ptr<AsyncTask> task = std::make_unique<AsyncTask>(callbackRef, nullptr, nullptr);
121     task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
122     EXPECT_TRUE(task->callbackRef_ == nullptr);
123 
124     NativeDeferred* nativeDeferred = nullptr;
125     jsEngine.CreatePromise(&nativeDeferred);
126     task = std::make_unique<AsyncTask>(nativeDeferred, nullptr, nullptr);
127     task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
128     EXPECT_TRUE(task->deferred_ == nullptr);
129 
130     task->deferred_ = nullptr;
131     task->callbackRef_ = nullptr;
132     task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
133     EXPECT_TRUE(task->deferred_ == nullptr);
134     EXPECT_TRUE(task->callbackRef_ == nullptr);
135 }
136 
137 /**
138  * @tc.name: JsWorkerTest_0100
139  * @tc.desc: JsWorker Test
140  * @tc.type: FUNC
141  * @tc.require: issueI581RO
142  */
143 HWTEST_F(JsRuntimeTest, JsWorkerTest_0100, TestSize.Level0)
144 {
145     auto runtime = AbilityRuntime::Runtime::Create(options_);
146     auto& jsEngine = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNativeEngine();
147 
148     std::vector<uint8_t> content;
149     std::string str = "test";
150     InitWorkerModule(jsEngine, "", true, true);
151     jsEngine.CallGetAssetFunc("", content, str);
152     EXPECT_TRUE(content.empty());
153 
154     jsEngine.CallGetAssetFunc("test", content, str);
155     EXPECT_TRUE(content.empty());
156 
157     jsEngine.CallGetAssetFunc("test.test", content, str);
158     EXPECT_TRUE(content.empty());
159 
160     InitWorkerModule(jsEngine, "", false, false);
161     jsEngine.CallGetAssetFunc("", content, str);
162     EXPECT_TRUE(content.empty());
163 
164     jsEngine.CallGetAssetFunc("test", content, str);
165     EXPECT_TRUE(content.empty());
166 
167     jsEngine.CallGetAssetFunc("test.test", content, str);
168     EXPECT_TRUE(content.empty());
169 
170     InitWorkerModule(jsEngine, TEST_CODE_PATH, true, true);
171     jsEngine.CallGetAssetFunc("", content, str);
172     EXPECT_TRUE(content.empty());
173 
174     jsEngine.CallGetAssetFunc("test", content, str);
175     EXPECT_TRUE(content.empty());
176 
177     jsEngine.CallGetAssetFunc("test.test", content, str);
178     EXPECT_TRUE(content.empty());
179 
180     InitWorkerModule(jsEngine, TEST_CODE_PATH, false, false);
181     jsEngine.CallGetAssetFunc("", content, str);
182     EXPECT_TRUE(content.empty());
183 
184     jsEngine.CallGetAssetFunc("test", content, str);
185     EXPECT_TRUE(content.empty());
186 
187     jsEngine.CallGetAssetFunc("test.test", content, str);
188     EXPECT_TRUE(content.empty());
189 }
190 
191 /**
192  * @tc.name: JsRuntimeGetLanguageTest_0100
193  * @tc.desc: JsRuntime Test
194  * @tc.type: FUNC
195  */
196 HWTEST_F(JsRuntimeTest, JsRuntimeGetLanguageTest_0100, TestSize.Level0)
197 {
198     options_.preload = true;
199     std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
200     EXPECT_TRUE(jsRuntime != nullptr);
201 
202     JsRuntime::Language language = jsRuntime->GetLanguage();
203     EXPECT_TRUE(language == JsRuntime::Language::JS);
204 }
205 
206 /**
207  * @tc.name: JsRuntimeBuildJsStackInfoListTest_0100
208  * @tc.desc: JsRuntime test for BuildJsStackInfoList.
209  * @tc.type: FUNC
210  */
211 HWTEST_F(JsRuntimeTest, JsRuntimeBuildJsStackInfoListTest_0100, TestSize.Level0)
212 {
213     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
214     EXPECT_TRUE(jsRuntime != nullptr);
215 
216     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
217 
218     std::vector<JsFrames> frames;
219     bool ret = jsRuntime->BuildJsStackInfoList(gettid(), frames);
220     EXPECT_TRUE(ret);
221 }
222 
223 /**
224  * @tc.name: JsRuntimeNotifyApplicationStateTest_0100
225  * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine_ is nullptr.
226  * @tc.type: FUNC
227  */
228 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0100, TestSize.Level0)
229 {
230     HILOG_INFO("NotifyApplicationState start");
231 
232     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
233     EXPECT_TRUE(jsRuntime != nullptr);
234 
235     jsRuntime->nativeEngine_ = nullptr;
236 
237     bool isBackground = false;
238     jsRuntime->NotifyApplicationState(isBackground);
239 
240     HILOG_INFO("NotifyApplicationState end");
241 }
242 
243 /**
244  * @tc.name: JsRuntimeNotifyApplicationStateTest_0200
245  * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine_ is not nullptr.
246  * @tc.type: FUNC
247  */
248 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0200, TestSize.Level0)
249 {
250     HILOG_INFO("NotifyApplicationState start");
251 
252     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
253     EXPECT_TRUE(jsRuntime != nullptr);
254 
255     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
256 
257     bool isBackground = true;
258     jsRuntime->NotifyApplicationState(isBackground);
259 
260     HILOG_INFO("NotifyApplicationState end");
261 }
262 
263 /**
264  * @tc.name: JsRuntimeDumpHeapSnapshotTest_0100
265  * @tc.desc: JsRuntime test for DumpHeapSnapshot.
266  * @tc.type: FUNC
267  */
268 HWTEST_F(JsRuntimeTest, JsRuntimeDumpHeapSnapshotTest_0100, TestSize.Level0)
269 {
270     HILOG_INFO("DumpHeapSnapshot start");
271 
272     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
273     EXPECT_TRUE(jsRuntime != nullptr);
274 
275     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
276 
277     bool isPrivate = true;
278     jsRuntime->DumpHeapSnapshot(isPrivate);
279 
280     HILOG_INFO("DumpHeapSnapshot end");
281 }
282 
283 /**
284  * @tc.name: JsRuntimePreloadSystemModuleTest_0100
285  * @tc.desc: JsRuntime test for PreloadSystemModule.
286  * @tc.type: FUNC
287  */
288 HWTEST_F(JsRuntimeTest, JsRuntimePreloadSystemModuleTest_0100, TestSize.Level0)
289 {
290     HILOG_INFO("PreloadSystemModule start");
291 
292     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
293     EXPECT_TRUE(jsRuntime != nullptr);
294 
295     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
296 
297     std::string moduleName = "PreloadSystemModuleTest";
298     jsRuntime->PreloadSystemModule(moduleName);
299 
300     HILOG_INFO("PreloadSystemModule end");
301 }
302 
303 /**
304  * @tc.name: JsRuntimeRunSandboxScriptTest_0100
305  * @tc.desc: JsRuntime test for RunSandboxScript.
306  * @tc.type: FUNC
307  */
308 HWTEST_F(JsRuntimeTest, JsRuntimeRunSandboxScriptTest_0100, TestSize.Level0)
309 {
310     HILOG_INFO("RunSandboxScript start");
311 
312     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
313     EXPECT_TRUE(jsRuntime != nullptr);
314 
315     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
316 
317     std::string path = "";
318     std::string hapPath = "";
319     bool ret = jsRuntime->RunSandboxScript(path, hapPath);
320     EXPECT_TRUE(ret);
321 
322     HILOG_INFO("RunSandboxScript end");
323 }
324 
325 /**
326  * @tc.name: JsRuntimeLoadSystemModuleByEngineTest_0100
327  * @tc.desc: JsRuntime test for LoadSystemModuleByEngine.
328  * @tc.type: FUNC
329  */
330 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleByEngineTest_0100, TestSize.Level0)
331 {
332     HILOG_INFO("LoadSystemModuleByEngine start");
333 
334     auto runtime = AbilityRuntime::JsRuntime::Create(options_);
335     auto &jsEngine = (static_cast<AbilityRuntime::MockJsRuntime &>(*runtime)).GetNativeEngine();
336 
337     std::string moduleName = "";
338     std::unique_ptr<NativeReference> ref = MockJsRuntime::LoadSystemModuleByEngine(&jsEngine, moduleName, nullptr, 0);
339     EXPECT_NE(ref, nullptr);
340 
341     HILOG_INFO("LoadSystemModuleByEngine end");
342 }
343 
344 /**
345  * @tc.name: JsRuntimeLoadModuleTest_0100
346  * @tc.desc: JsRuntime test for LoadModule.
347  * @tc.type: FUNC
348  */
349 HWTEST_F(JsRuntimeTest, JsRuntimeLoadModuleTest_0100, TestSize.Level0)
350 {
351     HILOG_INFO("LoadModule start");
352 
353     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
354     EXPECT_TRUE(jsRuntime != nullptr);
355 
356     jsRuntime->nativeEngine_ = std::make_unique<MockJsNativeEngine>();
357 
358     std::string moduleName = TEST_MODULE_NAME;
359     std::string modulePath = TEST_MODULE_PATH;
360     std::string hapPath = TEST_HAP_PATH;
361     bool esmodule = true;
362     std::unique_ptr<NativeReference> ref = jsRuntime->LoadModule(moduleName, modulePath, hapPath, esmodule);
363     EXPECT_EQ(ref, nullptr);
364 
365     HILOG_INFO("LoadModule end");
366 }
367 
368 /**
369  * @tc.name: JsRuntimeLoadSystemModuleTest_0100
370  * @tc.desc: JsRuntime test for LoadSystemModule (invoke the overwrite interface).
371  * @tc.type: FUNC
372  */
373 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleTest_0100, TestSize.Level0)
374 {
375     HILOG_INFO("LoadSystemModule start");
376 
377     MockJsRuntime mockJsRuntime;
378     std::unique_ptr<NativeReference> ref = mockJsRuntime.LoadSystemModule("", nullptr, 0);
379     EXPECT_EQ(ref, nullptr);
380 
381     HILOG_INFO("LoadSystemModule end");
382 }
383 
384 /**
385  * @tc.name: JsRuntimePostTaskTest_0100
386  * @tc.desc: JsRuntime test for PostTask.
387  * @tc.type: FUNC
388  */
389 HWTEST_F(JsRuntimeTest, JsRuntimePostTaskTest_0100, TestSize.Level0)
390 {
391     HILOG_INFO("PostTask start");
392 
393     std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
394     EXPECT_TRUE(jsRuntime != nullptr);
395 
396     jsRuntime->eventHandler_ = nullptr;
397 
__anonbe9007cc0202() 398     auto task = []() { GTEST_LOG_(INFO) << "JsRuntimePostTaskTest_0100 task called"; };
399     std::string name = "";
400     int64_t delayTime = 0;
401     jsRuntime->PostTask(task, name, delayTime);
402 
403     HILOG_INFO("PostTask end");
404 }
405 
406 /**
407  * @tc.name: RuntimeSavePreloadedTest_0100
408  * @tc.desc: Runtime test for SavePreloaded.
409  * @tc.type: FUNC
410  */
411 HWTEST_F(JsRuntimeTest, RuntimeSavePreloadedTest_0100, TestSize.Level0)
412 {
413     HILOG_INFO("SavePreloaded start");
414 
415     Runtime::SavePreloaded(nullptr);
416 
417     HILOG_INFO("SavePreloaded end");
418 }
419 
420 /**
421  * @tc.name: JsRuntimeDetachCallbackFuncTest_0100
422  * @tc.desc: JsRuntime test for PostTask.
423  * @tc.type: FUNC
424  */
425 HWTEST_F(JsRuntimeTest, JsRuntimeDetachCallbackFuncTest_0100, TestSize.Level0)
426 {
427     HILOG_INFO("DetachCallbackFunc start");
428     auto runtime = AbilityRuntime::JsRuntime::Create(options_);
429     auto& jsEngine = (static_cast<AbilityRuntime::MockJsRuntime&>(*runtime)).GetNativeEngine();
430     int32_t value = 1;
431     int32_t number = 1;
432     auto result = AbilityRuntime::DetachCallbackFunc(&jsEngine, &value, &number);
433     EXPECT_EQ(result, &value);
434 
435     HILOG_INFO("DetachCallbackFunc end");
436 }
437 }  // namespace AbilityRuntime
438 }  // namespace OHOS
439