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 "mock_jsnapi.h"
30 #include "hilog_tag_wrapper.h"
31 #include "js_runtime_lite.h"
32
33 using namespace testing;
34 using namespace testing::ext;
35 using namespace testing::mt;
36
37 namespace OHOS {
38 namespace AbilityRuntime {
39 namespace {
40 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
41 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
42 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
43 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
44 const std::string TEST_HAP_PATH = "/system/app/com.ohos.contactsdataability/Contacts_DataAbility.hap";
45 const std::string TEST_LIB_PATH = "/data/storage/el1/bundle/lib/";
46 const std::string TEST_MODULE_PATH = "/data/storage/el1/bundle/curJsModulePath";
47 } // namespace
48 class JsRuntimeTest : public testing::Test {
49 public:
50 static void SetUpTestCase();
51 static void TearDownTestCase();
52 void SetUp() override;
53 void TearDown() override;
54
55 Runtime::Options options_;
56 };
57
SetUpTestCase()58 void JsRuntimeTest::SetUpTestCase()
59 {}
60
TearDownTestCase()61 void JsRuntimeTest::TearDownTestCase()
62 {}
63
SetUp()64 void JsRuntimeTest::SetUp()
65 {
66 Runtime::Options newOptions;
67 options_ = newOptions;
68 options_.bundleName = TEST_BUNDLE_NAME;
69 options_.codePath = TEST_CODE_PATH;
70 options_.loadAce = false;
71 options_.isBundle = true;
72 options_.preload = false;
73 std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
74 options_.eventRunner = eventRunner;
75 }
76
TearDown()77 void JsRuntimeTest::TearDown()
78 {}
79
80 /**
81 * @tc.name: JsperfProfilerCommandParse_100
82 * @tc.desc: JsRuntime test for JsperfProfilerCommandParse.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(JsRuntimeTest, JsperfProfilerCommandParse_100, TestSize.Level1)
86 {
87 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
88 std::string command = "";
89 constexpr int32_t defaultVal = 500;
90 constexpr int32_t emptyVal = 0;
91 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), emptyVal);
92 command = "jsperfabc";
93 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
94 command = "jsperf";
95 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
96 command = "jsperf ";
97 ASSERT_EQ(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
98 command = "jsperf 1000";
99 ASSERT_NE(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
100 command = " jsperf 1000";
101 ASSERT_NE(jsRuntime->JsperfProfilerCommandParse(command, defaultVal), defaultVal);
102 jsRuntime.reset();
103 std::this_thread::sleep_for(std::chrono::milliseconds(200));
104 }
105
106 /**
107 * @tc.name: JsRuntimeTest_0100
108 * @tc.desc: JsRuntime Test
109 * @tc.type: FUNC
110 * @tc.require: issueI581SE
111 */
112 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0100, TestSize.Level0)
113 {
114 options_.preload = true;
115 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
116 EXPECT_TRUE(jsRuntime != nullptr);
117
118 jsRuntime = nullptr;
119 options_.preload = false;
120 jsRuntime = JsRuntime::Create(options_);
121 EXPECT_TRUE(jsRuntime != nullptr);
122 jsRuntime.reset();
123 std::this_thread::sleep_for(std::chrono::milliseconds(200));
124 }
125
126 /**
127 * @tc.name: JsRuntimeTest_0200
128 * @tc.desc: JsRuntime Test
129 * @tc.type: FUNC
130 * @tc.require: issueI581RO
131 */
132 HWTEST_F(JsRuntimeTest, JsRuntimeTest_0200, TestSize.Level0)
133 {
134 std::string appLibPathKey = TEST_BUNDLE_NAME + TEST_MODULE_NAME;
135 std::string libPath = TEST_LIB_PATH;
136
137 AppLibPathMap appLibPaths {};
138 JsRuntime::SetAppLibPath(appLibPaths);
139
140 appLibPaths[appLibPathKey].emplace_back(libPath);
141 EXPECT_NE(appLibPaths.size(), 0);
142 JsRuntime::SetAppLibPath(appLibPaths);
143 }
144
145 /**
146 * @tc.name: JsRuntimeUtilsTest_0100
147 * @tc.desc: JsRuntimeUtils Test
148 * @tc.type: FUNC
149 * @tc.require: issueI581RO
150 */
151 HWTEST_F(JsRuntimeTest, JsRuntimeUtilsTest_0100, TestSize.Level0)
152 {
153 auto runtime = AbilityRuntime::Runtime::Create(options_);
154 auto env = (static_cast<AbilityRuntime::JsRuntime&>(*runtime)).GetNapiEnv();
155
156 napi_ref callbackRef = nullptr;
157 napi_create_reference(env, CreateJsUndefined(env), 1, &callbackRef);
158 std::unique_ptr<NapiAsyncTask> task = std::make_unique<NapiAsyncTask>(callbackRef, nullptr, nullptr);
159 task->ResolveWithNoError(env, CreateJsUndefined(env));
160 EXPECT_TRUE(task->callbackRef_ == nullptr);
161
162 napi_deferred nativeDeferred = nullptr;
163 napi_value result;
164 napi_create_promise(env, &nativeDeferred, &result);
165 task = std::make_unique<NapiAsyncTask>(nativeDeferred, nullptr, nullptr);
166 task->ResolveWithNoError(env, CreateJsUndefined(env));
167 EXPECT_TRUE(task->deferred_ == nullptr);
168
169 task->deferred_ = nullptr;
170 task->callbackRef_ = nullptr;
171 task->ResolveWithNoError(env, CreateJsUndefined(env));
172 EXPECT_TRUE(task->deferred_ == nullptr);
173 EXPECT_TRUE(task->callbackRef_ == nullptr);
174 runtime.reset();
175 std::this_thread::sleep_for(std::chrono::milliseconds(200));
176 }
177
178 /**
179 * @tc.name: JsRuntimeGetLanguageTest_0100
180 * @tc.desc: JsRuntime Test
181 * @tc.type: FUNC
182 */
183 HWTEST_F(JsRuntimeTest, JsRuntimeGetLanguageTest_0100, TestSize.Level0)
184 {
185 options_.preload = true;
186 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
187 EXPECT_TRUE(jsRuntime != nullptr);
188
189 JsRuntime::Language language = jsRuntime->GetLanguage();
190 EXPECT_TRUE(language == JsRuntime::Language::JS);
191 jsRuntime.reset();
192 std::this_thread::sleep_for(std::chrono::milliseconds(200));
193 }
194
195 /**
196 * @tc.name: JsRuntimeNotifyApplicationStateTest_0100
197 * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine is nullptr.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0100, TestSize.Level0)
201 {
202 TAG_LOGI(AAFwkTag::TEST, "NotifyApplicationState start");
203
204 std::unique_ptr<JsRuntime> jsRuntime = std::make_unique<MockJsRuntime>();
205 EXPECT_TRUE(jsRuntime != nullptr);
206
207 bool isBackground = false;
208 jsRuntime->NotifyApplicationState(isBackground);
209
210 TAG_LOGI(AAFwkTag::TEST, "NotifyApplicationState end");
211 std::this_thread::sleep_for(std::chrono::milliseconds(200));
212 }
213
214 /**
215 * @tc.name: JsRuntimeNotifyApplicationStateTest_0200
216 * @tc.desc: JsRuntime test for NotifyApplicationState when nativeEngine is not nullptr.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyApplicationStateTest_0200, TestSize.Level0)
220 {
221 TAG_LOGI(AAFwkTag::TEST, "NotifyApplicationState start");
222
223 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
224 EXPECT_TRUE(jsRuntime != nullptr);
225
226 bool isBackground = true;
227 jsRuntime->NotifyApplicationState(isBackground);
228
229 jsRuntime.reset();
230 std::this_thread::sleep_for(std::chrono::milliseconds(200));
231 TAG_LOGI(AAFwkTag::TEST, "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 TAG_LOGI(AAFwkTag::TEST, "DumpHeapSnapshot start");
242 MockJsRuntime mockJsRuntime;
243 bool isPrivate = false;
244 mockJsRuntime.DumpHeapSnapshot(isPrivate);
245 TAG_LOGI(AAFwkTag::TEST, "DumpHeapSnapshot end");
246 }
247
248 /**
249 * @tc.name: JsRuntimePreloadSystemModuleTest_0100
250 * @tc.desc: JsRuntime test for PreloadSystemModule.
251 * @tc.type: FUNC
252 */
253 HWTEST_F(JsRuntimeTest, JsRuntimePreloadSystemModuleTest_0100, TestSize.Level0)
254 {
255 TAG_LOGI(AAFwkTag::TEST, "PreloadSystemModule start");
256
257 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
258 EXPECT_TRUE(jsRuntime != nullptr);
259
260 std::string moduleName = "PreloadSystemModuleTest";
261 jsRuntime->PreloadSystemModule(moduleName);
262
263 jsRuntime.reset();
264 std::this_thread::sleep_for(std::chrono::milliseconds(200));
265 TAG_LOGI(AAFwkTag::TEST, "PreloadSystemModule end");
266 }
267
268 /**
269 * @tc.name: JsRuntimeRunSandboxScriptTest_0100
270 * @tc.desc: JsRuntime test for RunSandboxScript.
271 * @tc.type: FUNC
272 */
273 HWTEST_F(JsRuntimeTest, JsRuntimeRunSandboxScriptTest_0100, TestSize.Level0)
274 {
275 TAG_LOGI(AAFwkTag::TEST, "RunSandboxScript start");
276
277 auto jsRuntime = std::make_unique<JsRuntime>();
278 std::string path = "";
279 std::string hapPath = "";
280 jsRuntime->RunSandboxScript(path, hapPath);
281 EXPECT_TRUE(jsRuntime != nullptr);
282 jsRuntime.reset();
283 std::this_thread::sleep_for(std::chrono::milliseconds(200));
284 TAG_LOGI(AAFwkTag::TEST, "RunSandboxScript end");
285 }
286
287 /**
288 * @tc.name: JsRuntimeLoadSystemModuleByEngineTest_0100
289 * @tc.desc: JsRuntime test for LoadSystemModuleByEngine.
290 * @tc.type: FUNC
291 */
292 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleByEngineTest_0100, TestSize.Level0)
293 {
294 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModuleByEngine start");
295
296 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
297 auto env = (static_cast<AbilityRuntime::MockJsRuntime&>(*runtime)).GetNapiEnv();
298
299 std::string moduleName = "";
300 std::unique_ptr<NativeReference> ref = MockJsRuntime::LoadSystemModuleByEngine(env, moduleName, nullptr, 0);
301 EXPECT_EQ(ref, nullptr);
302
303 runtime.reset();
304 std::this_thread::sleep_for(std::chrono::milliseconds(200));
305 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModuleByEngine end");
306 }
307
308 /**
309 * @tc.name: JsRuntimeFinishPreloadTest_0100
310 * @tc.desc: JsRuntime test for FinishPreload.
311 * @tc.type: FUNC
312 */
313 HWTEST_F(JsRuntimeTest, JsRuntimeFinishPreloadTest_0100, TestSize.Level0)
314 {
315 TAG_LOGI(AAFwkTag::TEST, "FinishPreload start");
316
317 auto jsRuntime = std::make_unique<JsRuntime>();
318
319 jsRuntime->FinishPreload();
320 EXPECT_TRUE(jsRuntime != nullptr);
321
322 jsRuntime.reset();
323 std::this_thread::sleep_for(std::chrono::milliseconds(200));
324 TAG_LOGI(AAFwkTag::TEST, "FinishPreload end");
325 }
326
327 /**
328 * @tc.name: JsRuntimePostPreloadTest_0100
329 * @tc.desc: JsRuntime test for FinishPreload.
330 * @tc.type: FUNC
331 */
332 HWTEST_F(JsRuntimeTest, JsRuntimePostPreloadTest_0100, TestSize.Level0)
333 {
334 TAG_LOGI(AAFwkTag::TEST, "PostPreload start");
335
336 auto jsRuntime = std::make_unique<JsRuntime>();
337
338 jsRuntime->PostPreload(options_);
339 EXPECT_TRUE(jsRuntime != nullptr);
340
341 jsRuntime.reset();
342 std::this_thread::sleep_for(std::chrono::milliseconds(200));
343 TAG_LOGI(AAFwkTag::TEST, "PostPreload end");
344 }
345
346 /**
347 * @tc.name: JsRuntimeLoadAotFileTest_0100
348 * @tc.desc: JsRuntime test for LoadAotFile.
349 * @tc.type: FUNC
350 */
351 HWTEST_F(JsRuntimeTest, JsRuntimeLoadAotFileTest_0100, TestSize.Level0)
352 {
353 TAG_LOGI(AAFwkTag::TEST, "LoadAotFile start");
354
355 auto jsRuntime = std::make_unique<JsRuntime>();
356
357 jsRuntime->LoadAotFile(options_);
358 EXPECT_TRUE(jsRuntime != nullptr);
359
360 jsRuntime.reset();
361 std::this_thread::sleep_for(std::chrono::milliseconds(200));
362 TAG_LOGI(AAFwkTag::TEST, "LoadAotFile end");
363 }
364
365 /**
366 * @tc.name: JsRuntimeLoadModuleTest_0100
367 * @tc.desc: JsRuntime test for LoadModule.
368 * @tc.type: FUNC
369 */
370 HWTEST_F(JsRuntimeTest, JsRuntimeLoadModuleTest_0100, TestSize.Level0)
371 {
372 TAG_LOGI(AAFwkTag::TEST, "LoadModule start");
373
374 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options_);
375 EXPECT_TRUE(jsRuntime != nullptr);
376
377 std::string moduleName = TEST_MODULE_NAME;
378 std::string modulePath = TEST_MODULE_PATH;
379 std::string hapPath = TEST_HAP_PATH;
380 bool esmodule = true;
381 std::unique_ptr<NativeReference> ref = (static_cast<AbilityRuntime::JsRuntime&>(*jsRuntime)).LoadModule(moduleName,
382 modulePath, hapPath, esmodule);
383 EXPECT_EQ(ref, nullptr);
384 jsRuntime.reset();
385 std::this_thread::sleep_for(std::chrono::milliseconds(200));
386 TAG_LOGI(AAFwkTag::TEST, "LoadModule end");
387 }
388
389 /**
390 * @tc.name: JsRuntimeLoadSystemModuleTest_0100
391 * @tc.desc: JsRuntime test for LoadSystemModule (invoke the overwrite interface).
392 * @tc.type: FUNC
393 */
394 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModuleTest_0100, TestSize.Level0)
395 {
396 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModule start");
397
398 MockJsRuntime mockJsRuntime;
399 std::unique_ptr<NativeReference> ref = mockJsRuntime.LoadSystemModule("", nullptr, 0);
400 EXPECT_EQ(ref, nullptr);
401
402 std::this_thread::sleep_for(std::chrono::milliseconds(200));
403 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModule 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 TAG_LOGI(AAFwkTag::TEST, "SavePreloaded start");
414
415 auto runtime = AbilityRuntime::Runtime::Create(options_);
416 runtime->SavePreloaded(nullptr);
417 EXPECT_TRUE(runtime != nullptr);
418
419 runtime.reset();
420 std::this_thread::sleep_for(std::chrono::milliseconds(200));
421 TAG_LOGI(AAFwkTag::TEST, "SavePreloaded end");
422 }
423
424 /**
425 * @tc.name: RuntimeSetModuleLoadCheckerTest_0100
426 * @tc.desc: Runtime test for SetModuleLoadChecker.
427 * @tc.type: FUNC
428 */
429 HWTEST_F(JsRuntimeTest, RuntimeSetModuleLoadCheckerTest_0100, TestSize.Level0)
430 {
431 TAG_LOGI(AAFwkTag::TEST, "SetModuleLoadChecker start");
432
433 auto runtime = AbilityRuntime::Runtime::Create(options_);
434 runtime->SetModuleLoadChecker(nullptr);
435 EXPECT_TRUE(runtime != nullptr);
436
437 runtime.reset();
438 std::this_thread::sleep_for(std::chrono::milliseconds(200));
439 TAG_LOGI(AAFwkTag::TEST, "SetModuleLoadChecker end");
440 }
441
442 /**
443 * @tc.name: JsRuntimeSuspendVMTest_0100
444 * @tc.desc: JsRuntime test for SuspendVM.
445 * @tc.type: FUNC
446 */
447 HWTEST_F(JsRuntimeTest, JsRuntimeSuspendVMTest_0100, TestSize.Level0)
448 {
449 TAG_LOGI(AAFwkTag::TEST, "SuspendVM start");
450
451 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
452 auto result = runtime->SuspendVM(gettid());
453 EXPECT_EQ(result, false);
454
455 runtime.reset();
456 std::this_thread::sleep_for(std::chrono::milliseconds(200));
457 TAG_LOGI(AAFwkTag::TEST, "SuspendVM end");
458 }
459
460 /**
461 * @tc.name: JsRuntimeResumeVMTest_0100
462 * @tc.desc: JsRuntime test for ResumeVM.
463 * @tc.type: FUNC
464 */
465 HWTEST_F(JsRuntimeTest, JsRuntimeResumeVMTest_0100, TestSize.Level0)
466 {
467 TAG_LOGI(AAFwkTag::TEST, "ResumeVM start");
468
469 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
470 runtime->ResumeVM(gettid());
471 EXPECT_TRUE(runtime != nullptr);
472
473 runtime.reset();
474 std::this_thread::sleep_for(std::chrono::milliseconds(200));
475 TAG_LOGI(AAFwkTag::TEST, "ResumeVM end");
476 }
477
478 /**
479 * @tc.name: JsRuntimeSetDeviceDisconnectCallbackTest_0100
480 * @tc.desc: JsRuntime test for SetDeviceDisconnectCallback.
481 * @tc.type: FUNC
482 */
483 HWTEST_F(JsRuntimeTest, JsRuntimeSetDeviceDisconnectCallbackTest_0100, TestSize.Level0)
484 {
485 TAG_LOGI(AAFwkTag::TEST, "SetDeviceDisconnectCallback start");
486
487 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
__anon1bdc87840202() 488 std::function<bool()> task = [&]() {
489 return true;
490 };
491 runtime->SetDeviceDisconnectCallback(task);
492 EXPECT_TRUE(runtime != nullptr);
493
494 runtime.reset();
495 std::this_thread::sleep_for(std::chrono::milliseconds(200));
496 TAG_LOGI(AAFwkTag::TEST, "SetDeviceDisconnectCallback end");
497 }
498
499 /**
500 * @tc.name: JsRuntimeDetachCallbackFuncTest_0100
501 * @tc.desc: JsRuntime test for PostTask.
502 * @tc.type: FUNC
503 */
504 HWTEST_F(JsRuntimeTest, JsRuntimeDetachCallbackFuncTest_0100, TestSize.Level0)
505 {
506 TAG_LOGI(AAFwkTag::TEST, "DetachCallbackFunc start");
507
508 auto runtime = AbilityRuntime::JsRuntime::Create(options_);
509 auto env = (static_cast<AbilityRuntime::MockJsRuntime&>(*runtime)).GetNapiEnv();
510 int32_t value = 1;
511 int32_t number = 1;
512 auto result = AbilityRuntime::DetachCallbackFunc(env, &value, &number);
513 EXPECT_EQ(result, &value);
514
515 runtime.reset();
516 std::this_thread::sleep_for(std::chrono::milliseconds(200));
517 TAG_LOGI(AAFwkTag::TEST, "DetachCallbackFunc end");
518 }
519
520 /**
521 * @tc.name: JsRuntimeLoadSystemModulesTest_0100
522 * @tc.desc: JsRuntime test for LoadSystemModule.
523 * @tc.type: FUNC
524 */
525 HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModulesTest_0100, TestSize.Level0)
526 {
527 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModule start");
528
529 auto jsRuntime = std::make_unique<JsRuntime>();
530 EXPECT_TRUE(jsRuntime != nullptr);
531
532 std::string moduleName = "PreloadSystemModuleTest";
533 napi_value object = nullptr;
534 std::unique_ptr<NativeReference> ref = jsRuntime->LoadSystemModule(moduleName, &object, 0);
535 EXPECT_EQ(ref, nullptr);
536
537 jsRuntime.reset();
538 std::this_thread::sleep_for(std::chrono::milliseconds(200));
539 TAG_LOGI(AAFwkTag::TEST, "LoadSystemModule end");
540 }
541
542 /**
543 * @tc.name: JsRuntimeStartDebugModeTest_0100
544 * @tc.desc: JsRuntime test for StartDebugMode.
545 * @tc.type: FUNC
546 */
547 HWTEST_F(JsRuntimeTest, JsRuntimeStartDebugModeTest_0100, TestSize.Level0)
548 {
549 TAG_LOGI(AAFwkTag::TEST, "StartDebugMode start");
550
551 auto jsRuntime = std::make_unique<JsRuntime>();
552 AbilityRuntime::Runtime::DebugOption debugOption;
553 debugOption.isStartWithDebug = true;
554 debugOption.processName = "test";
555 debugOption.isDebugApp = true;
556 debugOption.isStartWithNative = false;
557 jsRuntime->StartDebugMode(debugOption);
558 EXPECT_TRUE(jsRuntime != nullptr);
559
560 jsRuntime.reset();
561 std::this_thread::sleep_for(std::chrono::milliseconds(200));
562 TAG_LOGI(AAFwkTag::TEST, "StartDebugMode end");
563 }
564
565 /**
566 * @tc.name: JsRuntimeStopDebugModeTest_0100
567 * @tc.desc: JsRuntime test for StopDebugMode.
568 * @tc.type: FUNC
569 */
570 HWTEST_F(JsRuntimeTest, JsRuntimeStopDebugModeTest_0100, TestSize.Level0)
571 {
572 TAG_LOGI(AAFwkTag::TEST, "StopDebugMode start");
573
574 auto jsRuntime = std::make_unique<JsRuntime>();
575
576 jsRuntime->StopDebugMode();
577 EXPECT_TRUE(jsRuntime != nullptr);
578
579 jsRuntime.reset();
580 std::this_thread::sleep_for(std::chrono::milliseconds(200));
581 TAG_LOGI(AAFwkTag::TEST, "StopDebugMode end");
582 }
583
584 /**
585 * @tc.name: JsRuntimeInitConsoleModuleTest_0100
586 * @tc.desc: JsRuntime test for InitConsoleModule.
587 * @tc.type: FUNC
588 */
589 HWTEST_F(JsRuntimeTest, JsRuntimeInitConsoleModuleTest_0100, TestSize.Level0)
590 {
591 TAG_LOGI(AAFwkTag::TEST, "InitConsoleModule start");
592
593 auto jsRuntime = std::make_unique<JsRuntime>();
594
595 jsRuntime->InitConsoleModule();
596 EXPECT_TRUE(jsRuntime != nullptr);
597
598 jsRuntime.reset();
599 std::this_thread::sleep_for(std::chrono::milliseconds(200));
600 TAG_LOGI(AAFwkTag::TEST, "InitConsoleModule end");
601 }
602
603 /**
604 * @tc.name: JsRuntimeLoadRepairPatchTest_0100
605 * @tc.desc: JsRuntime test for LoadRepairPatch.
606 * @tc.type: FUNC
607 */
608 HWTEST_F(JsRuntimeTest, JsRuntimeLoadRepairPatchTest_0100, TestSize.Level0)
609 {
610 TAG_LOGI(AAFwkTag::TEST, "LoadRepairPatch start");
611
612 auto jsRuntime = std::make_unique<JsRuntime>();
613 EXPECT_TRUE(jsRuntime != nullptr);
614
615 std::string hqfFile = "<hqfFile>";
616 std::string hapPath = "<hapPath>";
617 bool lrp = jsRuntime->LoadRepairPatch(hqfFile, hapPath);
618 EXPECT_EQ(lrp, false);
619
620 jsRuntime.reset();
621 std::this_thread::sleep_for(std::chrono::milliseconds(200));
622 TAG_LOGI(AAFwkTag::TEST, "LoadRepairPatch end");
623 }
624
625 /**
626 * @tc.name: JsRuntimeUnLoadRepairPatchTest_0100
627 * @tc.desc: JsRuntime test for UnLoadRepairPatch.
628 * @tc.type: FUNC
629 */
630 HWTEST_F(JsRuntimeTest, JsRuntimeUnLoadRepairPatchTest_0100, TestSize.Level0)
631 {
632 TAG_LOGI(AAFwkTag::TEST, "UnLoadRepairPatch start");
633
634 auto jsRuntime = std::make_unique<JsRuntime>();
635 EXPECT_TRUE(jsRuntime != nullptr);
636
637 std::string hqfFile = "<hqfFile>";
638 bool lrp = jsRuntime->UnLoadRepairPatch(hqfFile);
639 EXPECT_EQ(lrp, false);
640
641 jsRuntime.reset();
642 std::this_thread::sleep_for(std::chrono::milliseconds(200));
643 TAG_LOGI(AAFwkTag::TEST, "UnLoadRepairPatch end");
644 }
645
646 /**
647 * @tc.name: JsRuntimeNotifyHotReloadPageTest_0100
648 * @tc.desc: JsRuntime test for NotifyHotReloadPage.
649 * @tc.type: FUNC
650 */
651 HWTEST_F(JsRuntimeTest, JsRuntimeNotifyHotReloadPageTest_0100, TestSize.Level0)
652 {
653 TAG_LOGI(AAFwkTag::TEST, "NotifyHotReloadPage start");
654
655 auto jsRuntime = std::make_unique<JsRuntime>();
656 EXPECT_TRUE(jsRuntime != nullptr);
657
658 bool lrp = jsRuntime->NotifyHotReloadPage();
659 EXPECT_EQ(lrp, true);
660
661 jsRuntime.reset();
662 std::this_thread::sleep_for(std::chrono::milliseconds(200));
663 TAG_LOGI(AAFwkTag::TEST, "NotifyHotReloadPage end");
664 }
665
666 /**
667 * @tc.name: JsRuntimeUpdateModuleNameAndAssetPathTest_0100
668 * @tc.desc: JsRuntime test for UpdateModuleNameAndAssetPath.
669 * @tc.type: FUNC
670 */
671 HWTEST_F(JsRuntimeTest, JsRuntimeUpdateModuleNameAndAssetPathTest_0100, TestSize.Level0)
672 {
673 TAG_LOGI(AAFwkTag::TEST, "UpdateModuleNameAndAssetPath start");
674
675 auto jsRuntime = std::make_unique<JsRuntime>();
676 EXPECT_TRUE(jsRuntime != nullptr);
677
678 std::string moduleName = "moduleName";
679 jsRuntime->UpdateModuleNameAndAssetPath(moduleName);
680
681 jsRuntime.reset();
682 std::this_thread::sleep_for(std::chrono::milliseconds(200));
683 TAG_LOGI(AAFwkTag::TEST, "UpdateModuleNameAndAssetPath end");
684 }
685
686 /**
687 * @tc.name: JsRuntimeUpdateModuleNameAndAssetPathTest_0200
688 * @tc.desc: JsRuntime test for UpdateModuleNameAndAssetPath.
689 * @tc.type: FUNC
690 */
691 HWTEST_F(JsRuntimeTest, JsRuntimeUpdateModuleNameAndAssetPathTest_0200, TestSize.Level0)
692 {
693 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeUpdateModuleNameAndAssetPathTest_0200 start");
694
695 auto jsRuntime = std::make_unique<JsRuntime>();
696 EXPECT_TRUE(jsRuntime != nullptr);
697
698 jsRuntime->isBundle_ = false;
699 std::string moduleName = "moduleName";
700 jsRuntime->UpdateModuleNameAndAssetPath(moduleName);
701
702 jsRuntime.reset();
703 std::this_thread::sleep_for(std::chrono::milliseconds(200));
704 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeUpdateModuleNameAndAssetPathTest_0200 end");
705 }
706
707 /**
708 * @tc.name: JsRuntimeUpdateModuleNameAndAssetPathTest_0300
709 * @tc.desc: JsRuntime test for UpdateModuleNameAndAssetPath.
710 * @tc.type: FUNC
711 */
712 HWTEST_F(JsRuntimeTest, JsRuntimeUpdateModuleNameAndAssetPathTest_0300, TestSize.Level0)
713 {
714 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeUpdateModuleNameAndAssetPathTest_0300 start");
715
716 auto jsRuntime = std::make_unique<JsRuntime>();
717 EXPECT_TRUE(jsRuntime != nullptr);
718
719 jsRuntime->isBundle_ = false;
720 std::string moduleName = "";
721 jsRuntime->UpdateModuleNameAndAssetPath(moduleName);
722
723 jsRuntime.reset();
724 std::this_thread::sleep_for(std::chrono::milliseconds(200));
725 TAG_LOGI(AAFwkTag::TEST, "JsRuntimeUpdateModuleNameAndAssetPathTest_0300 end");
726 }
727
728 /**
729 * @tc.name: JsRuntimeInitialize_0100
730 * @tc.desc: Initialize js runtime in multi thread.
731 * @tc.type: FUNC
732 * @tc.require: issueI6KODF
733 */
734 HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0100, TestSize.Level0)
735 {
736 TAG_LOGI(AAFwkTag::TEST, "Running in multi-thread, using default thread number.");
737
738 AbilityRuntime::Runtime::Options options;
739 options.loadAce = false;
740 options.preload = true;
741 options.isStageModel = false;
742
743 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
744 ASSERT_NE(jsRuntime, nullptr);
745 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
746 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
747
748 jsRuntime.reset();
749 std::this_thread::sleep_for(std::chrono::milliseconds(200));
750 }
751
752 /**
753 * @tc.name: JsRuntimeInitialize_0200
754 * @tc.desc: preload js runtime.
755 * @tc.type: FUNC
756 * @tc.require: issueI6KODF
757 */
758 HWTEST_F(JsRuntimeTest, JsRuntimeInitialize_0200, TestSize.Level0)
759 {
760 AbilityRuntime::Runtime::Options options;
761 options.preload = true;
762
763 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
764 ASSERT_NE(jsRuntime, nullptr);
765 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
766 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
767 jsRuntime.reset();
768
769 options.preload = false;
770 jsRuntime = AbilityRuntime::JsRuntime::Create(options);
771 ASSERT_NE(jsRuntime, nullptr);
772 EXPECT_NE(jsRuntime->GetEcmaVm(), nullptr);
773 EXPECT_NE(jsRuntime->GetNativeEnginePointer(), nullptr);
774
775 jsRuntime.reset();
776 std::this_thread::sleep_for(std::chrono::milliseconds(200));
777 }
778
779 /**
780 * @tc.name: RegisterUncaughtExceptionHandler_0100
781 * @tc.desc: JsRuntime test for RegisterUncaughtExceptionHandler.
782 * @tc.type: FUNC
783 */
784 HWTEST_F(JsRuntimeTest, RegisterUncaughtExceptionHandler_0100, TestSize.Level0)
785 {
786 TAG_LOGI(AAFwkTag::TEST, "RegisterUncaughtExceptionHandler start");
787
788 AbilityRuntime::Runtime::Options options;
789 options.preload = false;
790 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
791
792 ASSERT_NE(jsRuntime, nullptr);
793 JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo;
794 jsRuntime->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
795 jsRuntime.reset();
796 std::this_thread::sleep_for(std::chrono::milliseconds(200));
797 TAG_LOGI(AAFwkTag::TEST, "RegisterUncaughtExceptionHandler end");
798 }
799
800 /**
801 * @tc.name: RegisterUncaughtExceptionHandler_0200
802 * @tc.desc: JsRuntime test for RegisterUncaughtExceptionHandler.
803 * @tc.type: FUNC
804 */
805 HWTEST_F(JsRuntimeTest, RegisterUncaughtExceptionHandler_0200, TestSize.Level0)
806 {
807 TAG_LOGI(AAFwkTag::TEST, "RegisterUncaughtExceptionHandler start");
808
809 AbilityRuntime::Runtime::Options options;
810 options.preload = true;
811 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
812
813 ASSERT_NE(jsRuntime, nullptr);
814 JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo;
815 jsRuntime->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
816 jsRuntime.reset();
817 std::this_thread::sleep_for(std::chrono::milliseconds(200));
818 TAG_LOGI(AAFwkTag::TEST, "RegisterUncaughtExceptionHandler end");
819 }
820
821 /**
822 * @tc.name: ReadSourceMapData_0100
823 * @tc.desc: JsRuntime test for ReadSourceMapData.
824 * @tc.type: FUNC
825 */
826 HWTEST_F(JsRuntimeTest, ReadSourceMapData_0100, TestSize.Level0)
827 {
828 TAG_LOGI(AAFwkTag::TEST, "ReadSourceMapData start");
829
830 AbilityRuntime::Runtime::Options options;
831 options.preload = true;
832 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
833
834 ASSERT_NE(jsRuntime, nullptr);
835
836 std::string hapPath = "";
837 std::string sourceMapPath = "";
838 std::string content = "";
839 auto result = jsRuntime->ReadSourceMapData(hapPath, sourceMapPath, content);
840 ASSERT_FALSE(result);
841 jsRuntime.reset();
842 std::this_thread::sleep_for(std::chrono::milliseconds(200));
843 TAG_LOGI(AAFwkTag::TEST, "ReadSourceMapData end");
844 }
845
846 /**
847 * @tc.name: StopDebugger_0100
848 * @tc.desc: JsRuntime test for StopDebugger.
849 * @tc.type: FUNC
850 */
851 HWTEST_F(JsRuntimeTest, StopDebugger_0100, TestSize.Level0)
852 {
853 TAG_LOGI(AAFwkTag::TEST, "StopDebugger start");
854
855 AbilityRuntime::Runtime::Options options;
856 options.preload = true;
857 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
858
859 ASSERT_NE(jsRuntime, nullptr);
860
861 jsRuntime->StopDebugger();
862 jsRuntime.reset();
863 std::this_thread::sleep_for(std::chrono::milliseconds(200));
864 TAG_LOGI(AAFwkTag::TEST, "StopDebugger end");
865 }
866
867 /**
868 * @tc.name: GetFileBuffer_0100
869 * @tc.desc: JsRuntime test for GetFileBuffer.
870 * @tc.type: FUNC
871 */
872 HWTEST_F(JsRuntimeTest, GetFileBuffer_0100, TestSize.Level0)
873 {
874 TAG_LOGI(AAFwkTag::TEST, "GetFileBuffer start");
875
876 AbilityRuntime::Runtime::Options options;
877 options.preload = true;
878 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
879
880 ASSERT_NE(jsRuntime, nullptr);
881
882 std::string filePath = "";
883 std::string fileFullName = "";
884 std::vector<uint8_t> buffer;
885 jsRuntime->GetFileBuffer(filePath, fileFullName, buffer);
886 jsRuntime.reset();
887 std::this_thread::sleep_for(std::chrono::milliseconds(200));
888 TAG_LOGI(AAFwkTag::TEST, "GetFileBuffer end");
889 }
890
891 /**
892 * @tc.name: GetFileBuffer_0200
893 * @tc.desc: JsRuntime test for GetFileBuffer.
894 * @tc.type: FUNC
895 */
896 HWTEST_F(JsRuntimeTest, GetFileBuffer_0200, TestSize.Level0)
897 {
898 TAG_LOGI(AAFwkTag::TEST, "GetFileBuffer start");
899
900 AbilityRuntime::Runtime::Options options;
901 options.preload = true;
902 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
903
904 ASSERT_NE(jsRuntime, nullptr);
905
906 std::string filePath = "";
907 std::string fileFullName = "";
908 std::vector<uint8_t> buffer;
909 jsRuntime->GetFileBuffer(filePath, fileFullName, buffer, false);
910 jsRuntime.reset();
911 std::this_thread::sleep_for(std::chrono::milliseconds(200));
912 TAG_LOGI(AAFwkTag::TEST, "GetFileBuffer end");
913 }
914
915 /**
916 * @tc.name: JsRuntimeRunScriptTest_0100
917 * @tc.desc: JsRuntime test for RunScript.
918 * @tc.type: FUNC
919 */
920 HWTEST_F(JsRuntimeTest, JsRuntimeRunScriptTest_0100, TestSize.Level0)
921 {
922 TAG_LOGI(AAFwkTag::TEST, "RunScript start");
923
924 AbilityRuntime::Runtime::Options options;
925 options.preload = false;
926 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
927
928 std::string srcPath = TEST_MODULE_PATH;
929 std::string hapPath = TEST_HAP_PATH;
930 jsRuntime->RunScript(srcPath, hapPath);
931 ASSERT_NE(jsRuntime, nullptr);
932
933 jsRuntime.reset();
934 std::this_thread::sleep_for(std::chrono::milliseconds(200));
935 TAG_LOGI(AAFwkTag::TEST, "RunScript end");
936 }
937
938 /**
939 * @tc.name: JsRuntimeLoadScriptTest_0100
940 * @tc.desc: JsRuntime test for LoadScript.
941 * @tc.type: FUNC
942 */
943 HWTEST_F(JsRuntimeTest, JsRuntimeLoadScriptTest_0100, TestSize.Level0)
944 {
945 AbilityRuntime::Runtime::Options options;
946 options.preload = false;
947 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
948
949 std::string path = "/system/etc/strip.native.min.abc";
950 jsRuntime->LoadScript(path);
951 ASSERT_NE(jsRuntime, nullptr);
952 jsRuntime.reset();
953 std::this_thread::sleep_for(std::chrono::milliseconds(200));
954 }
955
956 /**
957 * @tc.name: JsRuntimeStopDebuggerTest_0100
958 * @tc.desc: JsRuntime test for StopDebugger.
959 * @tc.type: FUNC
960 */
961 HWTEST_F(JsRuntimeTest, JsRuntimeStopDebuggerTest_0100, TestSize.Level0)
962 {
963 AbilityRuntime::Runtime::Options options;
964 options.preload = false;
965 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
966 ASSERT_NE(jsRuntime, nullptr);
967
968 jsRuntime->StopDebugger();
969 jsRuntime.reset();
970 std::this_thread::sleep_for(std::chrono::milliseconds(200));
971 }
972
973 /**
974 * @tc.name: PostSyncTask_0100
975 * @tc.desc: Js runtime post sync task.
976 * @tc.type: FUNC
977 * @tc.require: issueI7C87T
978 */
979 HWTEST_F(JsRuntimeTest, PostSyncTask_0100, TestSize.Level0)
980 {
981 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
982 ASSERT_NE(jsRuntime, nullptr);
983
984 std::string taskName = "syncTask001";
985 bool taskExecuted = false;
__anon1bdc87840302() 986 auto task = [taskName, &taskExecuted]() {
987 TAG_LOGI(AAFwkTag::TEST, "%{public}s called.", taskName.c_str());
988 taskExecuted = true;
989 };
990 jsRuntime->PostSyncTask(task, taskName);
991 EXPECT_EQ(taskExecuted, true);
992 jsRuntime.reset();
993 std::this_thread::sleep_for(std::chrono::milliseconds(200));
994 }
995
996 /**
997 * @tc.name: ReInitJsEnvImpl_0100
998 * @tc.desc: Js runtime reinit js env impl.
999 * @tc.type: FUNC
1000 * @tc.require: issueI7C87T
1001 */
1002 HWTEST_F(JsRuntimeTest, ReInitJsEnvImpl_0100, TestSize.Level1)
1003 {
1004 auto jsRuntime = std::make_unique<JsRuntime>();
1005 EXPECT_TRUE(jsRuntime != nullptr);
1006
1007 // called when jsEnv is invalid.
1008 jsRuntime->ReInitJsEnvImpl(options_);
1009
1010 auto ret = jsRuntime->CreateJsEnv(options_);
1011 EXPECT_EQ(ret, true);
1012 jsRuntime->ReInitJsEnvImpl(options_);
1013 jsRuntime.reset();
1014 std::this_thread::sleep_for(std::chrono::milliseconds(200));
1015 }
1016
1017 /**
1018 * @tc.name: JsRuntimeStartProfilerTest_0100
1019 * @tc.desc: JsRuntime test for StartProfiler.
1020 * @tc.type: FUNC
1021 */
1022 HWTEST_F(JsRuntimeTest, JsRuntimeStartProfilerTest_0100, TestSize.Level1)
1023 {
1024 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
1025
1026 bool needBreakPoint = false;
1027 uint32_t instanceId = 1;
1028 jsRuntime->StartDebugger(needBreakPoint, instanceId);
1029
1030 AbilityRuntime::Runtime::DebugOption debugOption;
1031 debugOption.perfCmd = "profile jsperf 100";
1032 debugOption.isStartWithDebug = false;
1033 debugOption.processName = "test";
1034 debugOption.isDebugApp = true;
1035 debugOption.isStartWithNative = false;
1036 jsRuntime->StartProfiler(debugOption);
1037 ASSERT_NE(jsRuntime, nullptr);
1038 jsRuntime.reset();
1039 }
1040
1041 /**
1042 * @tc.name: PostTask_0100
1043 * @tc.desc: Js runtime post task.
1044 * @tc.type: FUNC
1045 * @tc.require: issueI7C87T
1046 */
1047 HWTEST_F(JsRuntimeTest, PostTask_0100, TestSize.Level0)
1048 {
1049 TAG_LOGI(AAFwkTag::TEST, "PostTask_0100 start");
1050 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
1051 ASSERT_NE(jsRuntime, nullptr);
1052
1053 std::string taskName = "postTask001";
1054 bool taskExecuted = false;
__anon1bdc87840402() 1055 auto task = [taskName, &taskExecuted]() {
1056 TAG_LOGI(AAFwkTag::TEST, "%{public}s called.", taskName.c_str());
1057 taskExecuted = true;
1058 };
1059 int64_t delayTime = 10;
1060 jsRuntime->PostTask(task, taskName, delayTime);
1061 EXPECT_NE(taskExecuted, true);
1062 TAG_LOGI(AAFwkTag::TEST, "PostTask_0100 end");
1063 }
1064
1065 /**
1066 * @tc.name: RemoveTask_0100
1067 * @tc.desc: Js runtime remove task.
1068 * @tc.type: FUNC
1069 * @tc.require: issueI7C87T
1070 */
1071 HWTEST_F(JsRuntimeTest, RemoveTask_0100, TestSize.Level0)
1072 {
1073 TAG_LOGI(AAFwkTag::TEST, "RemoveTask_0100 start");
1074 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options_);
1075 ASSERT_NE(jsRuntime, nullptr);
1076
1077 std::string taskName = "removeTask001";
1078 bool taskExecuted = false;
__anon1bdc87840502() 1079 auto task = [taskName, &taskExecuted]() {
1080 TAG_LOGI(AAFwkTag::TEST, "%{public}s called.", taskName.c_str());
1081 taskExecuted = true;
1082 };
1083 int64_t delayTime = 10;
1084 jsRuntime->PostTask(task, taskName, delayTime);
1085 jsRuntime->RemoveTask(taskName);
1086 EXPECT_NE(taskExecuted, true);
1087 TAG_LOGI(AAFwkTag::TEST, "RemoveTask_0100 end");
1088 }
1089
1090 /**
1091 * @tc.name: StartDebugger_0100
1092 * @tc.desc: JsRuntime test for StartDebugger.
1093 * @tc.type: FUNC
1094 */
1095 HWTEST_F(JsRuntimeTest, StartDebugger_0100, TestSize.Level0)
1096 {
1097 TAG_LOGI(AAFwkTag::TEST, "StartDebugger_0100 start");
1098
1099 AbilityRuntime::Runtime::Options options;
1100 options.preload = true;
1101 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1102
1103 ASSERT_NE(jsRuntime, nullptr);
1104
1105 bool needBreakPoint = false;
1106 uint32_t instanceId = 1;
1107
1108 jsRuntime->StartDebugger(needBreakPoint, instanceId);
1109 // debug mode is global option, maybe has started by other testcase, not check here.
1110 TAG_LOGI(AAFwkTag::TEST, "StartDebugger_0100 end");
1111 }
1112
1113 /**
1114 * @tc.name: ReloadFormComponent_0100
1115 * @tc.desc: JsRuntime test for ReloadFormComponent.
1116 * @tc.type: FUNC
1117 */
1118 HWTEST_F(JsRuntimeTest, ReloadFormComponent_0100, TestSize.Level0)
1119 {
1120 TAG_LOGI(AAFwkTag::TEST, "ReloadFormComponent_0100 start");
1121
1122 AbilityRuntime::Runtime::Options options;
1123 options.preload = true;
1124 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1125
1126 ASSERT_NE(jsRuntime, nullptr);
1127
1128 jsRuntime->ReloadFormComponent();
1129 jsRuntime.reset();
1130 TAG_LOGI(AAFwkTag::TEST, "ReloadFormComponent_0100 end");
1131 }
1132
1133 /**
1134 * @tc.name: SetRequestAotCallback_0100
1135 * @tc.desc: JsRuntime test for SetRequestAotCallback.
1136 * @tc.type: FUNC
1137 * @tc.require: issueI82L1A
1138 */
1139 HWTEST_F(JsRuntimeTest, SetRequestAotCallback_0100, TestSize.Level0)
1140 {
1141 TAG_LOGI(AAFwkTag::TEST, "start");
1142
1143 AbilityRuntime::Runtime::Options options;
1144 options.preload = true;
1145 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1146 ASSERT_NE(jsRuntime, nullptr);
1147
1148 jsRuntime->SetRequestAotCallback();
1149 auto ret = panda::MockJSNApi::GetInstance()->RequestAot("bundleName", "moduleName", 0);
1150 EXPECT_NE(ret, -1);
1151 jsRuntime.reset();
1152 TAG_LOGI(AAFwkTag::TEST, "finish");
1153 }
1154
1155 /**
1156 * @tc.name: DestroyHeapProfiler_0100
1157 * @tc.desc: JsRuntime test for DestroyHeapProfiler.
1158 * @tc.type: FUNC
1159 */
1160 HWTEST_F(JsRuntimeTest, DestroyHeapProfiler_0100, TestSize.Level0)
1161 {
1162 TAG_LOGI(AAFwkTag::TEST, "DestroyHeapProfiler_0100 start");
1163
1164 AbilityRuntime::Runtime::Options options;
1165 options.preload = true;
1166 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1167
1168 jsRuntime->DestroyHeapProfiler();
1169 ASSERT_NE(jsRuntime, nullptr);
1170 jsRuntime.reset();
1171 TAG_LOGI(AAFwkTag::TEST, "DestroyHeapProfiler_0100 end");
1172 }
1173
1174 /**
1175 * @tc.name: ForceFullGC_0100
1176 * @tc.desc: JsRuntime test for ForceFullGC.
1177 * @tc.type: FUNC
1178 */
1179 HWTEST_F(JsRuntimeTest, ForceFullGC_0100, TestSize.Level0)
1180 {
1181 TAG_LOGI(AAFwkTag::TEST, "ForceFullGC_0100 start");
1182
1183 AbilityRuntime::Runtime::Options options;
1184 options.preload = true;
1185 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1186
1187 jsRuntime->ForceFullGC();
1188 ASSERT_NE(jsRuntime, nullptr);
1189 jsRuntime.reset();
1190 TAG_LOGI(AAFwkTag::TEST, "ForceFullGC_0100 end");
1191 }
1192
1193 /**
1194 * @tc.name: InitLoop_0100
1195 * @tc.desc: JsRuntime test for InitLoop.
1196 * @tc.type: FUNC
1197 */
1198 HWTEST_F(JsRuntimeTest, InitLoop_0100, TestSize.Level0)
1199 {
1200 TAG_LOGI(AAFwkTag::TEST, "InitLoop_0100 start");
1201
1202 AbilityRuntime::Runtime::Options options;
1203 options.preload = true;
1204 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1205
1206 auto result = jsRuntime->InitLoop();
1207 ASSERT_EQ(result, true);
1208 jsRuntime.reset();
1209 TAG_LOGI(AAFwkTag::TEST, "InitLoop_0100 end");
1210 }
1211
1212 /**
1213 * @tc.name: InitSourceMap_0100
1214 * @tc.desc: JsRuntime test for InitSourceMap.
1215 * @tc.type: FUNC
1216 */
1217 HWTEST_F(JsRuntimeTest, InitSourceMap_0100, TestSize.Level0)
1218 {
1219 TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0100 start");
1220
1221 AbilityRuntime::Runtime::Options options;
1222 options.preload = true;
1223 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1224 auto operatorObj = std::make_shared<JsEnv::SourceMapOperator>("", true, true);
1225 jsRuntime->InitSourceMap(operatorObj);
1226 ASSERT_NE(jsRuntime, nullptr);
1227 jsRuntime.reset();
1228 TAG_LOGI(AAFwkTag::TEST, "InitSourceMap_0100 end");
1229 }
1230
1231 /**
1232 * @tc.name: Deinitialize_0100
1233 * @tc.desc: JsRuntime test for Deinitialize.
1234 * @tc.type: FUNC
1235 */
1236 HWTEST_F(JsRuntimeTest, Deinitialize_0100, TestSize.Level0)
1237 {
1238 TAG_LOGI(AAFwkTag::TEST, "Deinitialize_0100 start");
1239
1240 AbilityRuntime::Runtime::Options options;
1241 options.preload = true;
1242 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1243
1244 jsRuntime->Deinitialize();
1245 ASSERT_NE(jsRuntime, nullptr);
1246 jsRuntime.reset();
1247 TAG_LOGI(AAFwkTag::TEST, "Deinitialize_0100 end");
1248 }
1249
1250 /**
1251 * @tc.name: GetPkgContextInfoListMap_0100
1252 * @tc.desc: JsRuntime test for GetPkgContextInfoListMap.
1253 * @tc.type: FUNC
1254 */
1255 HWTEST_F(JsRuntimeTest, GetPkgContextInfoListMap_0100, TestSize.Level0)
1256 {
1257 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 start");
1258
1259 std::map<std::string, std::string> modulePkgContentMap;
1260 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
1261 "library","version":"1.0.0","entryPath":"","isSO":false}})";
1262 modulePkgContentMap["entry"] = pkgContentJsonString;
1263
1264 AbilityRuntime::Runtime::Options options;
1265 options.preload = true;
1266 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1267 std::map<std::string, std::vector<std::vector<std::string>>> ret;
1268 std::map<std::string, std::string> pkgAliasMap;
1269 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
1270 std::string expectString = "library:packageName:library:bundleName:";
1271 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
1272 auto it = ret.find("entry");
1273 ASSERT_EQ(it, ret.end());
1274 std::string pkgRetString;
1275 for (const auto& vec : it->second) {
1276 for (const auto& str : vec) {
1277 pkgRetString += str + ":";
1278 }
1279 }
1280 ASSERT_EQ(pkgRetString, "");
1281 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0100 end");
1282 }
1283
1284 /**
1285 * @tc.name: GetPkgContextInfoListMap_0200
1286 * @tc.desc: JsRuntime test for GetPkgContextInfoListMap.
1287 * @tc.type: FUNC
1288 */
1289 HWTEST_F(JsRuntimeTest, GetPkgContextInfoListMap_0200, TestSize.Level0)
1290 {
1291 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 start");
1292
1293 std::map<std::string, std::string> modulePkgContentMap;
1294 std::string pkgContentJsonString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
1295 "library","version":"1.0.0","entryPath":"","isSO":false}})";
1296 modulePkgContentMap["entry"] = pkgContentJsonString;
1297
1298 std::string libraryString = R"({"library":{"packageName":"library","bundleName":"com.xxx.xxxx","moduleName":
1299 "library","version":"1.0.0","entryPath":"","isSO":false}})";
1300 modulePkgContentMap["library"] = libraryString;
1301
1302 AbilityRuntime::Runtime::Options options;
1303 options.preload = true;
1304 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
1305 std::map<std::string, std::vector<std::vector<std::string>>> ret;
1306 std::map<std::string, std::string> pkgAliasMap;
1307 JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(modulePkgContentMap, ret, pkgAliasMap);
1308 std::string expectString = "library:packageName:library:bundleName:";
1309 expectString += "com.xxx.xxxx:moduleName:library:version:1.0.0:entryPath::isSO:false:";
1310 auto it = ret.find("entry");
1311 ASSERT_EQ(it, ret.end());
1312 auto libraryIt = ret.find("library");
1313 ASSERT_EQ(libraryIt, ret.end());
1314 std::string pkgRetString;
1315 for (const auto& vec : it->second) {
1316 for (const auto& str : vec) {
1317 pkgRetString += str + ":";
1318 }
1319 }
1320 ASSERT_EQ(pkgRetString, "");
1321 TAG_LOGI(AAFwkTag::TEST, "GetPkgContextInfoListMap_0200 end");
1322 }
1323
1324 /**
1325 * @tc.name: CreateJsEnv_0100
1326 * @tc.desc: JsRuntime test for CreateJsEnv.
1327 * @tc.type: FUNC
1328 * @tc.require: issueI9CHSB
1329 */
1330 HWTEST_F(JsRuntimeTest, CreateJsEnv_0100, TestSize.Level1)
1331 {
1332 auto jsRuntime = std::make_unique<JsRuntime>();
1333 auto ret = jsRuntime->CreateJsEnv(options_);
1334 EXPECT_EQ(ret, true);
1335 }
1336
1337 /**
1338 * @tc.name: DumpCpuProfile_0100
1339 * @tc.desc: JsRuntime test for DumpCpuProfile.
1340 * @tc.type: FUNC
1341 */
1342 HWTEST_F(JsRuntimeTest, DumpCpuProfile_0100, TestSize.Level1)
1343 {
1344 auto jsRuntime = std::make_unique<JsRuntime>();
1345 bool isPrivate = true;
1346 jsRuntime->DumpCpuProfile();
1347 EXPECT_TRUE(jsRuntime != nullptr);
1348 }
1349
1350 /**
1351 * @tc.name: DumpHeapSnapshot_0100
1352 * @tc.desc: JsRuntime test for DumpHeapSnapshot.
1353 * @tc.type: FUNC
1354 */
1355 HWTEST_F(JsRuntimeTest, DumpHeapSnapshot_0100, TestSize.Level1)
1356 {
1357 auto jsRuntime = std::make_unique<JsRuntime>();
1358 bool isPrivate = true;
1359 jsRuntime->DumpHeapSnapshot(isPrivate);
1360 EXPECT_TRUE(jsRuntime != nullptr);
1361 }
1362
1363 /**
1364 * @tc.name: DumpHeapSnapshot_0200
1365 * @tc.desc: JsRuntime test for DumpHeapSnapshot.
1366 * @tc.type: FUNC
1367 */
1368 HWTEST_F(JsRuntimeTest, DumpHeapSnapshot_0200, TestSize.Level1)
1369 {
1370 auto jsRuntime = std::make_unique<JsRuntime>();
1371 uint32_t tid = 1;
1372 bool isFullGC = true;
1373 jsRuntime->DumpHeapSnapshot(tid, isFullGC);
1374 EXPECT_TRUE(jsRuntime != nullptr);
1375 }
1376
1377 /**
1378 * @tc.name: AllowCrossThreadExecution_0200
1379 * @tc.desc: JsRuntime test for AllowCrossThreadExecution.
1380 * @tc.type: FUNC
1381 */
1382 HWTEST_F(JsRuntimeTest, AllowCrossThreadExecution_0200, TestSize.Level1)
1383 {
1384 auto jsRuntime = std::make_unique<JsRuntime>();
1385 jsRuntime->AllowCrossThreadExecution();
1386 EXPECT_TRUE(jsRuntime != nullptr);
1387 }
1388
1389 /**
1390 * @tc.name: GetHeapPrepare_0200
1391 * @tc.desc: JsRuntime test for GetHeapPrepare.
1392 * @tc.type: FUNC
1393 */
1394 HWTEST_F(JsRuntimeTest, GetHeapPrepare_0200, TestSize.Level1)
1395 {
1396 auto jsRuntime = std::make_unique<JsRuntime>();
1397 jsRuntime->GetHeapPrepare();
1398 EXPECT_TRUE(jsRuntime != nullptr);
1399 }
1400
1401 /**
1402 * @tc.name: RegisterQuickFixQueryFunc_0200
1403 * @tc.desc: JsRuntime test for RegisterQuickFixQueryFunc.
1404 * @tc.type: FUNC
1405 */
1406 HWTEST_F(JsRuntimeTest, RegisterQuickFixQueryFunc_0200, TestSize.Level1)
1407 {
1408 auto jsRuntime = std::make_unique<JsRuntime>();
1409 std::map<std::string, std::string> moduleAndPath;
1410 jsRuntime->RegisterQuickFixQueryFunc(moduleAndPath);
1411 EXPECT_TRUE(jsRuntime != nullptr);
1412 }
1413 } // namespace AbilityRuntime
1414 } // namespace OHOS
1415