1 /*
2 * Copyright (c) 2022-2023 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 #include <gtest/hwext/gtest-multithread.h>
18
19 #define private public
20 #define protected public
21 #include "js_environment.h"
22 #include "js_runtime.h"
23 #include "js_runtime_utils.h"
24 #include "js_worker.h"
25 #undef private
26 #undef protected
27 #include "event_runner.h"
28 #include "mock_js_runtime.h"
29 #include "hilog_wrapper.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33 using namespace testing::mt;
34
35 namespace OHOS {
36 namespace AbilityRuntime {
37 namespace {
38 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
39 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
40 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
41 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
42 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
43 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
44 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curJsModulePath";
45 } // namespace
46 class JsRuntimeTest : public testing::Test {
47 public:
48 static void SetUpTestCase();
49 static void TearDownTestCase();
50 void SetUp() override;
51 void TearDown() override;
52
53 Runtime::Options options_;
54 };
55
SetUpTestCase()56 void JsRuntimeTest::SetUpTestCase()
57 {}
58
TearDownTestCase()59 void JsRuntimeTest::TearDownTestCase()
60 {}
61
SetUp()62 void JsRuntimeTest::SetUp()
63 {
64 options_.bundleName = TEST_BUNDLE_NAME;
65 options_.codePath = TEST_CODE_PATH;
66 options_.loadAce = false;
67 options_.isBundle = true;
68 options_.preload = false;
69 std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
70 options_.eventRunner = eventRunner;
71 }
72
TearDown()73 void JsRuntimeTest::TearDown()
74 {}
75
76 /**
77 * @tc.name: JsperfProfilerCommandParse_100
78 * @tc.desc: JsRuntime test for JsperfProfilerCommandParse.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(JsRuntimeTest, JsperfProfilerCommandParse_100, TestSize.Level1)
82 {
83 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
84 std::string command = "";
85 constexpr int32_t defaultVal = 500;
86 constexpr int32_t emptyVal = 0;
87 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), emptyVal);
88 command = "jsperfabc";
89 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
90 command = "jsperf";
91 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
92 command = "jsperf ";
93 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
94 command = "jsperf 1000";
95 ASSERT_NE(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
96 command = " jsperf 1000";
97 ASSERT_NE(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
98 }
99
100 /**
101 * @tc.name: JsRuntimeTest_0100
102 * @tc.desc: JsRuntime Test
103 * @tc.type: FUNC
104 * @tc.require: issueI581SE
105 */
106 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0100, TestSize.Level0)
107 {
108 options_.preload = true;
109 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
110 EXPECT_TRUE(jsRuntime != nullptr);
111
112 jsRuntime = nullptr;
113 options_.preload = false;
114 jsRuntime = JsRuntime::Create(options_);
115 EXPECT_TRUE(jsRuntime != nullptr);
116 }
117
118 /**
119 * @tc.name: JsRuntimeTest_0200
120 * @tc.desc: JsRuntime Test
121 * @tc.type: FUNC
122 * @tc.require: issueI581RO
123 */
124 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0200, TestSize.Level0)
125 {
126 std::string appLibPathKey = TEST_BUNDLE_NAME + TEST_MODULE_NAME;
127 std::string libPath = TEST_LIB_PATH;
128
129 AppLibPathMap appLibPaths {};
130 JsRuntime::SetAppLibPath(appLibPaths);
131
132 appLibPaths[appLibPathKey].emplace_back(libPath);
133 EXPECT_NE(appLibPaths.size(), 0);
134 JsRuntime::SetAppLibPath(appLibPaths);
135 }
136
137 /**
138 * @tc.name: JsRuntimeUtilsTest_0100
139 * @tc.desc: JsRuntimeUtils Test
140 * @tc.type: FUNC
141 * @tc.require: issueI581RO
142 */
143 HWTEST_F(JsRuntimeTest, JsRuntimeUtilsTest_0100, TestSize.Level0)
144 {
145 auto runtime = AbilityRuntime::Runtime::Create(options_);
146 auto& jsEngine = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNativeEngine();
147
148 NativeReference* callbackRef = jsEngine.CreateReference(jsEngine.CreateUndefined(), 1);
149 std::unique_ptr<AsyncTask> task = std::make_unique<AsyncTask>(callbackRef, nullptr, nullptr);
150 task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
151 EXPECT_TRUE(task->callbackRef_ == nullptr);
152
153 NativeDeferred* nativeDeferred = nullptr;
154 jsEngine.CreatePromise(&nativeDeferred);
155 task = std::make_unique<AsyncTask>(nativeDeferred, nullptr, nullptr);
156 task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
157 EXPECT_TRUE(task->deferred_ == nullptr);
158
159 task->deferred_ = nullptr;
160 task->callbackRef_ = nullptr;
161 task->ResolveWithNoError(jsEngine, jsEngine.CreateUndefined());
162 EXPECT_TRUE(task->deferred_ == nullptr);
163 EXPECT_TRUE(task->callbackRef_ == nullptr);
164 }
165
166 /**
167 * @tc.name: JsRuntimeGetLanguageTest_0100
168 * @tc.desc: JsRuntime Test
169 * @tc.type: FUNC
170 */
171 HWTEST_F(JsRuntimeTest, JsRuntimeGetLanguageTest_0100, TestSize.Level0)
172 {
173 options_.preload = true;
174 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
175 EXPECT_TRUE(jsRuntime != nullptr);
176
177 JsRuntime::Language language = jsRuntime->GetLanguage();
178 EXPECT_TRUE(language == JsRuntime::Language::JS);
179 }
180
181 /**
182 * @tc.name: JsRuntimeBuildJsStackInfoListTest_0100
183 * @tc.desc: JsRuntime test for BuildJsStackInfoList.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(JsRuntimeTest, JsRuntimeBuildJsStackInfoListTest_0100, TestSize.Level0)
187 {
188 HILOG_INFO("Test BuildJsStackInfoList start");
189 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
190 EXPECT_TRUE(jsRuntime != nullptr);
191
192 std::vector<JsFrames> frames;
193 bool ret = jsRuntime->BuildJsStackInfoList(gettid(), frames);
194 EXPECT_FALSE(ret);
195 HILOG_INFO("Test BuildJsStackInfoList end");
196 }
197
198 /**
199 * @tc.name: JsRuntimeNotifyApplicationStateTest_0100
200 * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine is nullptr.
201 * @tc.type: FUNC
202 */
203 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0100, TestSize.Level0)
204 {
205 HILOG_INFO("NotifyApplicationState start");
206
207 std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
208 EXPECT_TRUE(jsRuntime != nullptr);
209
210 bool isBackground = false;
211 jsRuntime->NotifyApplicationState(isBackground);
212
213 HILOG_INFO("NotifyApplicationState end");
214 }
215
216 /**
217 * @tc.name: JsRuntimeNotifyApplicationStateTest_0200
218 * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine is not nullptr.
219 * @tc.type: FUNC
220 */
221 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0200, TestSize.Level0)
222 {
223 HILOG_INFO("NotifyApplicationState start");
224
225 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
226 EXPECT_TRUE(jsRuntime != nullptr);
227
228 bool isBackground = true;
229 jsRuntime->NotifyApplicationState(isBackground);
230
231 HILOG_INFO("NotifyApplicationState end");
232 }
233
234 /**
235 * @tc.name: JsRuntimeDumpHeapSnapshotTest_0100
236 * @tc.desc: JsRuntime test for DumpHeapSnapshot.
237 * @tc.type: FUNC
238 */
239 HWTEST_F(JsRuntimeTest, JsRuntimeDumpHeapSnapshotTest_0100, TestSize.Level0)
240 {
241 HILOG_INFO("DumpHeapSnapshot start");
242
243 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
244 EXPECT_TRUE(jsRuntime != nullptr);
245
246 bool isPrivate = true;
247 jsRuntime->DumpHeapSnapshot(isPrivate);
248
249 HILOG_INFO("DumpHeapSnapshot end");
250 }
251
252 /**
253 * @tc.name: JsRuntimePreloadSystemModuleTest_0100
254 * @tc.desc: JsRuntime test for PreloadSystemModule.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(JsRuntimeTest, JsRuntimePreloadSystemModuleTest_0100, TestSize.Level0)
258 {
259 HILOG_INFO("PreloadSystemModule start");
260
261 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
262 EXPECT_TRUE(jsRuntime != nullptr);
263
264 std::string moduleName = "PreloadSystemModuleTest";
265 jsRuntime->PreloadSystemModule(moduleName);
266
267 HILOG_INFO("PreloadSystemModule end");
268 }
269
270 /**
271 * @tc.name: JsRuntimeRunSandboxScriptTest_0100
272 * @tc.desc: JsRuntime test for RunSandboxScript.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(JsRuntimeTest, JsRuntimeRunSandboxScriptTest_0100, TestSize.Level0)
276 {
277 HILOG_INFO("RunSandboxScript start");
278
279 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
280 EXPECT_TRUE(jsRuntime != nullptr);
281
282 std::string path = "";
283 std::string hapPath = "";
284 bool ret = (static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).RunSandboxScript(path, hapPath);
285 EXPECT_FALSE(ret);
286
287 HILOG_INFO("RunSandboxScript end");
288 }
289
290 /**
291 * @tc.name: JsRuntimeLoadSystemModuleByEngineTest_0100
292 * @tc.desc: JsRuntime test for LoadSystemModuleByEngine.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleByEngineTest_0100, TestSize.Level0)
296 {
297 HILOG_INFO("LoadSystemModuleByEngine start");
298
299 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
300 auto& jsEngine = (static_cast<AbilityRuntime::MockJsRuntime&>(*runtime)).GetNativeEngine();
301
302 std::string moduleName = "";
303 std::unique_ptr<NativeReference> ref = MockJsRuntime::LoadSystemModuleByEngine(&jsEngine, moduleName, nullptr, 0);
304 EXPECT_NE(ref, nullptr);
305
306 HILOG_INFO("LoadSystemModuleByEngine end");
307 }
308
309 /**
310 * @tc.name: JsRuntimeLoadModuleTest_0100
311 * @tc.desc: JsRuntime test for LoadModule.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(JsRuntimeTest, JsRuntimeLoadModuleTest_0100, TestSize.Level0)
315 {
316 HILOG_INFO("LoadModule start");
317
318 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
319 EXPECT_TRUE(jsRuntime != nullptr);
320
321 std::string moduleName = TEST_MODULE_NAME;
322 std::string modulePath = TEST_MODULE_PATH;
323 std::string hapPath = TEST_HAP_PATH;
324 bool esmodule = true;
325 std::unique_ptr<NativeReference> ref = (static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).LoadModule(moduleName,
326 modulePath, hapPath, esmodule);
327 EXPECT_EQ(ref, nullptr);
328
329 HILOG_INFO("LoadModule end");
330 }
331
332 /**
333 * @tc.name: JsRuntimeLoadSystemModuleTest_0100
334 * @tc.desc: JsRuntime test for LoadSystemModule (invoke the overwrite interface).
335 * @tc.type: FUNC
336 */
337 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleTest_0100, TestSize.Level0)
338 {
339 HILOG_INFO("LoadSystemModule start");
340
341 MockJsRuntime mockJsRuntime;
342 std::unique_ptr<NativeReference> ref = mockJsRuntime.LoadSystemModule("", nullptr, 0);
343 EXPECT_EQ(ref, nullptr);
344
345 HILOG_INFO("LoadSystemModule end");
346 }
347
348 /**
349 * @tc.name: RuntimeSavePreloadedTest_0100
350 * @tc.desc: Runtime test for SavePreloaded.
351 * @tc.type: FUNC
352 */
353 HWTEST_F(JsRuntimeTest, RuntimeSavePreloadedTest_0100, TestSize.Level0)
354 {
355 HILOG_INFO("SavePreloaded start");
356
357 Runtime::SavePreloaded(nullptr);
358
359 HILOG_INFO("SavePreloaded end");
360 }
361
362 /**
363 * @tc.name: JsRuntimeDetachCallbackFuncTest_0100
364 * @tc.desc: JsRuntime test for PostTask.
365 * @tc.type: FUNC
366 */
367 HWTEST_F(JsRuntimeTest, JsRuntimeDetachCallbackFuncTest_0100, TestSize.Level0)
368 {
369 HILOG_INFO("DetachCallbackFunc start");
370
371 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
372 auto& jsEngine = (static_cast<AbilityRuntime::MockJsRuntime&>(*runtime)).GetNativeEngine();
373 int32_t value = 1;
374 int32_t number = 1;
375 auto result = AbilityRuntime::DetachCallbackFunc(&jsEngine, &value, &number);
376 EXPECT_EQ(result, &value);
377
378 HILOG_INFO("DetachCallbackFunc end");
379 }
380
381 /**
382 * @tc.name: JsRuntimeLoadSystemModulesTest_0100
383 * @tc.desc: JsRuntime test for LoadSystemModule.
384 * @tc.type: FUNC
385 */
386 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModulesTest_0100, TestSize.Level0)
387 {
388 HILOG_INFO("LoadSystemModule start");
389
390 auto jsRuntime = std::make_unique<JsRuntime>();
391 EXPECT_TRUE(jsRuntime != nullptr);
392
393 std::string moduleName = "PreloadSystemModuleTest";
394 std::unique_ptr<NativeReference> ref = jsRuntime->LoadSystemModule(moduleName, nullptr, 0);
395 EXPECT_EQ(ref, nullptr);
396
397 HILOG_INFO("LoadSystemModule end");
398 }
399
400 /**
401 * @tc.name: JsRuntimeStartDebugModeTest_0100
402 * @tc.desc: JsRuntime test for StartDebugMode.
403 * @tc.type: FUNC
404 */
405 HWTEST_F(JsRuntimeTest, JsRuntimeStartDebugModeTest_0100, TestSize.Level0)
406 {
407 HILOG_INFO("StartDebugMode start");
408
409 auto jsRuntime = std::make_unique<JsRuntime>();
410 EXPECT_TRUE(jsRuntime != nullptr);
411
412 bool needBreakPoint = true;
413 jsRuntime->StartDebugMode(needBreakPoint);
414 jsRuntime->StopDebugMode();
415
416 HILOG_INFO("StartDebugMode end");
417 }
418
419 /**
420 * @tc.name: JsRuntimeLoadRepairPatchTest_0100
421 * @tc.desc: JsRuntime test for LoadRepairPatch.
422 * @tc.type: FUNC
423 */
424 HWTEST_F(JsRuntimeTest, JsRuntimeLoadRepairPatchTest_0100, TestSize.Level0)
425 {
426 HILOG_INFO("LoadRepairPatch start");
427
428 auto jsRuntime = std::make_unique<JsRuntime>();
429 EXPECT_TRUE(jsRuntime != nullptr);
430
431 std::string hqfFile = "<hqfFile>";
432 std::string hapPath = "<hapPath>";
433 bool lrp = jsRuntime->LoadRepairPatch(hqfFile, hapPath);
434 EXPECT_EQ(lrp, false);
435
436 HILOG_INFO("LoadRepairPatch end");
437 }
438
439 /**
440 * @tc.name: JsRuntimeUnLoadRepairPatchTest_0100
441 * @tc.desc: JsRuntime test for UnLoadRepairPatch.
442 * @tc.type: FUNC
443 */
444 HWTEST_F(JsRuntimeTest, JsRuntimeUnLoadRepairPatchTest_0100, TestSize.Level0)
445 {
446 HILOG_INFO("UnLoadRepairPatch start");
447
448 auto jsRuntime = std::make_unique<JsRuntime>();
449 EXPECT_TRUE(jsRuntime != nullptr);
450
451 std::string hqfFile = "<hqfFile>";
452 bool lrp = jsRuntime->UnLoadRepairPatch(hqfFile);
453 EXPECT_EQ(lrp, false);
454
455 HILOG_INFO("UnLoadRepairPatch end");
456 }
457
458 /**
459 * @tc.name: JsRuntimeNotifyHotReloadPageTest_0100
460 * @tc.desc: JsRuntime test for NotifyHotReloadPage.
461 * @tc.type: FUNC
462 */
463 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyHotReloadPageTest_0100, TestSize.Level0)
464 {
465 HILOG_INFO("NotifyHotReloadPage start");
466
467 auto jsRuntime = std::make_unique<JsRuntime>();
468 EXPECT_TRUE(jsRuntime != nullptr);
469
470 bool lrp = jsRuntime->NotifyHotReloadPage();
471 EXPECT_EQ(lrp, true);
472
473 HILOG_INFO("NotifyHotReloadPage end");
474 }
475
476 /**
477 * @tc.name: JsRuntimeUpdateModuleNameAndAssetPathTest_0100
478 * @tc.desc: JsRuntime test for UpdateModuleNameAndAssetPath.
479 * @tc.type: FUNC
480 */
481 HWTEST_F(JsRuntimeTest, JsRuntimeUpdateModuleNameAndAssetPathTest_0100, TestSize.Level0)
482 {
483 HILOG_INFO("UpdateModuleNameAndAssetPath start");
484
485 auto jsRuntime = std::make_unique<JsRuntime>();
486 EXPECT_TRUE(jsRuntime != nullptr);
487
488 std::string moduleName = "moduleName";
489 jsRuntime->UpdateModuleNameAndAssetPath(moduleName);
490
491 HILOG_INFO("UpdateModuleNameAndAssetPath end");
492 }
493
494 /**
495 * @tc.name: JsRuntimeInitialize_0100
496 * @tc.desc: Initialize js runtime in multi thread.
497 * @tc.type: FUNC
498 * @tc.require: issueI6KODF
499 */
500 HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0100, TestSize.Level0)
501 {
502 HILOG_INFO("Running in multi-thread, using default thread number.");
503
__anon3a03788b0202() 504 auto task = []() {
505 HILOG_INFO("Running in thread %{public}d", gettid());
506 AbilityRuntime::Runtime::Options options;
507 options.loadAce = false;
508 options.preload = false;
509 options.isStageModel = false;
510
511 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
512 ASSERT_NE(jsRuntime, nullptr);
513 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
514 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
515 };
516
517 GTEST_RUN_TASK(task);
518 }
519
520 /**
521 * @tc.name: JsRuntimeInitialize_0200
522 * @tc.desc: preload js runtime.
523 * @tc.type: FUNC
524 * @tc.require: issueI6KODF
525 */
526 HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0200, TestSize.Level0)
527 {
528 AbilityRuntime::Runtime::Options options;
529 options.preload = true;
530
531 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
532 ASSERT_NE(jsRuntime, nullptr);
533 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
534 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
535
536 options.preload = false;
537 jsRuntime = AbilityRuntime::JsRuntime::Create(options);
538 ASSERT_NE(jsRuntime, nullptr);
539 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
540 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
541 }
542
543 /**
544 * @tc.name: RegisterQuickFixQueryFunc_0100
545 * @tc.desc: JsRuntime test for RegisterQuickFixQueryFunc.
546 * @tc.type: FUNC
547 */
548 HWTEST_F(JsRuntimeTest, RegisterQuickFixQueryFunc_0100, TestSize.Level0)
549 {
550 HILOG_INFO("RegisterQuickFixQueryFunc start");
551
552 auto jsRuntime = std::make_unique<JsRuntime>();
553 EXPECT_TRUE(jsRuntime != nullptr);
554 std::string moudel = "<moudelName>";
555 std::string hqfFile = "<hqfFile>";
556 std::map<std::string, std::string> moduleAndPath;
557 moduleAndPath.insert(std::make_pair(moudel, hqfFile));
558 jsRuntime->RegisterQuickFixQueryFunc(moduleAndPath);
559
560 HILOG_INFO("RegisterQuickFixQueryFunc end");
561 }
562
563 /**
564 * @tc.name: RegisterUncaughtExceptionHandler_0100
565 * @tc.desc: JsRuntime test for RegisterUncaughtExceptionHandler.
566 * @tc.type: FUNC
567 */
568 HWTEST_F(JsRuntimeTest, RegisterUncaughtExceptionHandler_0100, TestSize.Level0)
569 {
570 HILOG_INFO("RegisterUncaughtExceptionHandler start");
571
572 AbilityRuntime::Runtime::Options options;
573 options.preload = false;
574 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
575
576 ASSERT_NE(jsRuntime, nullptr);
577 JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo;
578 jsRuntime->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
579 HILOG_INFO("RegisterUncaughtExceptionHandler end");
580 }
581
582 /**
583 * @tc.name: RegisterUncaughtExceptionHandler_0200
584 * @tc.desc: JsRuntime test for RegisterUncaughtExceptionHandler.
585 * @tc.type: FUNC
586 */
587 HWTEST_F(JsRuntimeTest, RegisterUncaughtExceptionHandler_0200, TestSize.Level0)
588 {
589 HILOG_INFO("RegisterUncaughtExceptionHandler start");
590
591 AbilityRuntime::Runtime::Options options;
592 options.preload = true;
593 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
594
595 ASSERT_NE(jsRuntime, nullptr);
596 JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo;
597 jsRuntime->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
598 HILOG_INFO("RegisterUncaughtExceptionHandler end");
599 }
600
601 /**
602 * @tc.name: ReadSourceMapData_0100
603 * @tc.desc: JsRuntime test for ReadSourceMapData.
604 * @tc.type: FUNC
605 */
606 HWTEST_F(JsRuntimeTest, ReadSourceMapData_0100, TestSize.Level0)
607 {
608 HILOG_INFO("ReadSourceMapData start");
609
610 AbilityRuntime::Runtime::Options options;
611 options.preload = true;
612 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
613
614 ASSERT_NE(jsRuntime, nullptr);
615
616 std::string hapPath = "";
617 std::string sourceMapPath = "";
618 std::string content = "";
619 jsRuntime->ReadSourceMapData(hapPath, sourceMapPath, content);
620 HILOG_INFO("ReadSourceMapData end");
621 }
622
623 /**
624 * @tc.name: StopDebugger_0100
625 * @tc.desc: JsRuntime test for StopDebugger.
626 * @tc.type: FUNC
627 */
628 HWTEST_F(JsRuntimeTest, StopDebugger_0100, TestSize.Level0)
629 {
630 HILOG_INFO("StopDebugger start");
631
632 AbilityRuntime::Runtime::Options options;
633 options.preload = true;
634 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
635
636 ASSERT_NE(jsRuntime, nullptr);
637
638 jsRuntime->StopDebugger();
639 HILOG_INFO("StopDebugger end");
640 }
641
642 /**
643 * @tc.name: GetFileBuffer_0200
644 * @tc.desc: JsRuntime test for GetFileBuffer.
645 * @tc.type: FUNC
646 */
647 HWTEST_F(JsRuntimeTest, GetFileBuffer_0200, TestSize.Level0)
648 {
649 HILOG_INFO("GetFileBuffer start");
650
651 AbilityRuntime::Runtime::Options options;
652 options.preload = true;
653 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
654
655 ASSERT_NE(jsRuntime, nullptr);
656
657 std::string filePath = "";
658 std::string fileFullName = "";
659 std::vector<uint8_t> buffer;
660 jsRuntime->GetFileBuffer(filePath, fileFullName, buffer);
661 HILOG_INFO("GetFileBuffer end");
662 }
663
664 /**
665 * @tc.name: JsRuntimeRunScriptTest_0100
666 * @tc.desc: JsRuntime test for RunScript.
667 * @tc.type: FUNC
668 */
669 HWTEST_F(JsRuntimeTest, JsRuntimeRunScriptTest_0100, TestSize.Level0)
670 {
671 HILOG_INFO("RunScript start");
672
673 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
674 EXPECT_TRUE(jsRuntime != nullptr);
675
676 std::string srcPath = "";
677 std::string hapPath = "";
678 bool useCommonChunk = true;
679 bool ret = (static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).RunScript(srcPath, hapPath, useCommonChunk);
680 EXPECT_FALSE(ret);
681
682 HILOG_INFO("RunScript end");
683 }
684
685 /**
686 * @tc.name: JsRuntimeLoadScriptTest_0100
687 * @tc.desc: JsRuntime test for LoadScript.
688 * @tc.type: FUNC
689 */
690 HWTEST_F(JsRuntimeTest, JsRuntimeLoadScriptTest_0100, TestSize.Level0)
691 {
692 AbilityRuntime::Runtime::Options options;
693 options.preload = false;
694 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
695 ASSERT_NE(jsRuntime, nullptr);
696
697 std::string path = "";
698 std::vector<uint8_t>* buffer = nullptr;
699 bool isBundle = true;
700 jsRuntime->LoadScript(path, buffer, isBundle);
701 }
702
703 /**
704 * @tc.name: JsRuntimeLoadScriptTest_0200
705 * @tc.desc: JsRuntime test for LoadScript.
706 * @tc.type: FUNC
707 */
708 HWTEST_F(JsRuntimeTest, JsRuntimeLoadScriptTest_0200, TestSize.Level0)
709 {
710 AbilityRuntime::Runtime::Options options;
711 options.preload = false;
712 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
713 ASSERT_NE(jsRuntime, nullptr);
714
715 std::string path = "";
716 uint8_t *buffer = nullptr;
717 size_t len = 1;
718 bool isBundle = true;
719 jsRuntime->LoadScript(path, buffer, len, isBundle);
720 }
721
722 /**
723 * @tc.name: JsRuntimeStopDebuggerTest_0100
724 * @tc.desc: JsRuntime test for StopDebugger.
725 * @tc.type: FUNC
726 */
727 HWTEST_F(JsRuntimeTest, JsRuntimeStopDebuggerTest_0100, TestSize.Level0)
728 {
729 AbilityRuntime::Runtime::Options options;
730 options.preload = false;
731 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
732 ASSERT_NE(jsRuntime, nullptr);
733
734 jsRuntime->StopDebugger();
735 }
736
737 /**
738 * @tc.name: PostSyncTask_0100
739 * @tc.desc: Js runtime post sync task.
740 * @tc.type: FUNC
741 * @tc.require: issueI7C87T
742 */
743 HWTEST_F(JsRuntimeTest, PostSyncTask_0100, TestSize.Level0)
744 {
745 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
746 ASSERT_NE(jsRuntime, nullptr);
747
748 std::string taskName = "syncTask001";
749 bool taskExecuted = false;
__anon3a03788b0302() 750 auto task = [taskName, &taskExecuted]() {
751 HILOG_INFO("%{public}s called.", taskName.c_str());
752 taskExecuted = true;
753 };
754 jsRuntime->PostSyncTask(task, taskName);
755 EXPECT_EQ(taskExecuted, true);
756 }
757
758 /**
759 * @tc.name: PostSyncTask_0200
760 * @tc.desc: Js runtime post sync task in preload scene.
761 * @tc.type: FUNC
762 * @tc.require: issueI7C87T
763 */
764 HWTEST_F(JsRuntimeTest, PostSyncTask_0200, TestSize.Level1)
765 {
766 options_.preload = true;
767 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
768 EXPECT_TRUE(jsRuntime != nullptr);
769
770 Runtime::SavePreloaded(std::move(jsRuntime));
771
772 options_.preload = false;
773 auto newJsRuntime = JsRuntime::Create(options_);
774 EXPECT_TRUE(newJsRuntime != nullptr);
775
776 std::string taskName = "syncTask002";
777 bool taskExecuted = false;
__anon3a03788b0402() 778 auto task = [taskName, &taskExecuted]() {
779 HILOG_INFO("%{public}s called.", taskName.c_str());
780 taskExecuted = true;
781 };
782 newJsRuntime->PostSyncTask(task, taskName);
783 EXPECT_EQ(taskExecuted, true);
784 }
785
786 /**
787 * @tc.name: ReInitJsEnvImpl_0100
788 * @tc.desc: Js runtime reinit js env impl.
789 * @tc.type: FUNC
790 * @tc.require: issueI7C87T
791 */
792 HWTEST_F(JsRuntimeTest, ReInitJsEnvImpl_0100, TestSize.Level1)
793 {
794 auto jsRuntime = std::make_unique<JsRuntime>();
795 EXPECT_TRUE(jsRuntime != nullptr);
796
797 // called when jsEnv is invalid.
798 jsRuntime->ReInitJsEnvImpl(options_);
799
800 auto ret = jsRuntime->CreateJsEnv(options_);
801 EXPECT_EQ(ret, true);
802 jsRuntime->ReInitJsEnvImpl(options_);
803 }
804
805 /**
806 * @tc.name: JsRuntimeStartProfilerTest_0100
807 * @tc.desc: JsRuntime test for StartProfiler.
808 * @tc.type: FUNC
809 */
810 HWTEST_F(JsRuntimeTest, JsRuntimeStartProfilerTest_0100, TestSize.Level1)
811 {
812 AbilityRuntime::Runtime::Options options;
813 options.preload = false;
814 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
815
816 bool needBreakPoint = false;
817 uint32_t instanceId = 1;
818 jsRuntime->StartDebugger(needBreakPoint, instanceId);
819
820 std::string perfCmd = "profile jsperf 100";
821 jsRuntime->StartProfiler(perfCmd);
822 ASSERT_NE(jsRuntime, nullptr);
823 }
824
825 /**
826 * @tc.name: PostTask_0100
827 * @tc.desc: Js runtime post task.
828 * @tc.type: FUNC
829 * @tc.require: issueI7C87T
830 */
831 HWTEST_F(JsRuntimeTest, PostTask_0100, TestSize.Level0)
832 {
833 HILOG_INFO("PostTask_0100 start");
834 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
835 ASSERT_NE(jsRuntime, nullptr);
836
837 std::string taskName = "postTask001";
838 bool taskExecuted = false;
__anon3a03788b0502() 839 auto task = [taskName, &taskExecuted]() {
840 HILOG_INFO("%{public}s called.", taskName.c_str());
841 taskExecuted = true;
842 };
843 int64_t delayTime = 10;
844 jsRuntime->PostTask(task, taskName, delayTime);
845 EXPECT_NE(taskExecuted, true);
846 HILOG_INFO("PostTask_0100 end");
847 }
848
849 /**
850 * @tc.name: RemoveTask_0100
851 * @tc.desc: Js runtime remove task.
852 * @tc.type: FUNC
853 * @tc.require: issueI7C87T
854 */
855 HWTEST_F(JsRuntimeTest, RemoveTask_0100, TestSize.Level0)
856 {
857 HILOG_INFO("RemoveTask_0100 start");
858 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
859 ASSERT_NE(jsRuntime, nullptr);
860
861 std::string taskName = "removeTask001";
862 bool taskExecuted = false;
__anon3a03788b0602() 863 auto task = [taskName, &taskExecuted]() {
864 HILOG_INFO("%{public}s called.", taskName.c_str());
865 taskExecuted = true;
866 };
867 int64_t delayTime = 10;
868 jsRuntime->PostTask(task, taskName, delayTime);
869 jsRuntime->RemoveTask(taskName);
870 EXPECT_NE(taskExecuted, true);
871 HILOG_INFO("RemoveTask_0100 end");
872 }
873
874 /**
875 * @tc.name: StartDebugger_0100
876 * @tc.desc: JsRuntime test for StartDebugger.
877 * @tc.type: FUNC
878 */
879 HWTEST_F(JsRuntimeTest, StartDebugger_0100, TestSize.Level0)
880 {
881 HILOG_INFO("StartDebugger_0100 start");
882
883 AbilityRuntime::Runtime::Options options;
884 options.preload = true;
885 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
886
887 ASSERT_NE(jsRuntime, nullptr);
888
889 bool needBreakPoint = false;
890 uint32_t instanceId = 1;
891
892 jsRuntime->StartDebugger(needBreakPoint, instanceId);
893 // debug mode is global option, maybe has started by other testcase, not check here.
894 HILOG_INFO("StartDebugger_0100 end");
895 }
896
897 /**
898 * @tc.name: DoCleanWorkAfterStageCleaned_0100
899 * @tc.desc: JsRuntime test for DoCleanWorkAfterStageCleaned.
900 * @tc.type: FUNC
901 */
902 HWTEST_F(JsRuntimeTest, DoCleanWorkAfterStageCleaned_0100, TestSize.Level0)
903 {
904 HILOG_INFO("DoCleanWorkAfterStageCleaned_0100 start");
905
906 AbilityRuntime::Runtime::Options options;
907 options.preload = true;
908 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
909
910 ASSERT_NE(jsRuntime, nullptr);
911
912 jsRuntime->DoCleanWorkAfterStageCleaned();
913 HILOG_INFO("DoCleanWorkAfterStageCleaned_0100 end");
914 }
915
916 /**
917 * @tc.name: ReloadFormComponent_0100
918 * @tc.desc: JsRuntime test for ReloadFormComponent.
919 * @tc.type: FUNC
920 */
921 HWTEST_F(JsRuntimeTest, ReloadFormComponent_0100, TestSize.Level0)
922 {
923 HILOG_INFO("ReloadFormComponent_0100 start");
924
925 AbilityRuntime::Runtime::Options options;
926 options.preload = true;
927 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
928
929 ASSERT_NE(jsRuntime, nullptr);
930
931 jsRuntime->ReloadFormComponent();
932 HILOG_INFO("ReloadFormComponent_0100 end");
933 }
934 } // namespace AbilityRuntime
935 } // namespace OHOS
936