1 /*
2  * Copyright (c) 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 "js_environment.h"
17 
18 #include <gtest/gtest.h>
19 #include <gtest/hwext/gtest-multithread.h>
20 #include <cstdarg>
21 #include <string>
22 
23 #include "ecmascript/napi/include/jsnapi.h"
24 #include "ohos_js_env_logger.h"
25 #include "ohos_js_environment_impl.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace testing::mt;
30 
31 namespace {
32 bool callbackModuleFlag;
33 }
34 
35 namespace OHOS {
36 namespace JsEnv {
37 class JsEnvironmentTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp() override;
42     void TearDown() override;
43 };
44 
SetUpTestCase()45 void JsEnvironmentTest::SetUpTestCase()
46 {
47     AbilityRuntime::OHOSJsEnvLogger::RegisterJsEnvLogger();
48 }
49 
TearDownTestCase()50 void JsEnvironmentTest::TearDownTestCase()
51 {}
52 
SetUp()53 void JsEnvironmentTest::SetUp()
54 {}
55 
TearDown()56 void JsEnvironmentTest::TearDown()
57 {}
58 
59 namespace {
CallBackModuleFunc()60 void CallBackModuleFunc()
61 {
62     callbackModuleFlag = true;
63 }
64 }
65 
66 /**
67  * @tc.name: JsEnvInitialize_0100
68  * @tc.desc: Initialize js environment.
69  * @tc.type: FUNC
70  * @tc.require: issueI6KODF
71  */
72 HWTEST_F(JsEnvironmentTest, JsEnvInitialize_0100, TestSize.Level0)
73 {
74     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
75     ASSERT_NE(jsEnv, nullptr);
76     ASSERT_EQ(jsEnv->GetVM(), nullptr);
77     ASSERT_EQ(jsEnv->GetNativeEngine(), nullptr);
78 
79     panda::RuntimeOption pandaOption;
80     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
81     ASSERT_EQ(ret, true);
82 
83     auto vm = jsEnv->GetVM();
84     EXPECT_NE(vm, nullptr);
85 
86     auto nativeEngine = jsEnv->GetNativeEngine();
87     EXPECT_NE(nativeEngine, nullptr);
88 }
89 
90 /**
91  * @tc.name: JsEnvInitialize_0200
92  * @tc.desc: Initialize js environment in multi thread.
93  * @tc.type: FUNC
94  * @tc.require: issueI6KODF
95  */
96 HWTEST_F(JsEnvironmentTest, JsEnvInitialize_0200, TestSize.Level0)
97 {
98     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
99     ASSERT_NE(jsEnv, nullptr);
100 
101     panda::RuntimeOption pandaOption;
102     ASSERT_EQ(jsEnv->Initialize(pandaOption, nullptr), true);
103     EXPECT_NE(jsEnv->GetVM(), nullptr);
104     EXPECT_NE(jsEnv->GetNativeEngine(), nullptr);
105 }
106 
107 /**
108  * @tc.name: LoadScript_0100
109  * @tc.desc: load script with invalid engine.
110  * @tc.type: FUNC
111  * @tc.require: issueI6KODF
112  */
113 HWTEST_F(JsEnvironmentTest, LoadScript_0100, TestSize.Level0)
114 {
115     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
116     ASSERT_NE(jsEnv, nullptr);
117 
118     EXPECT_EQ(jsEnv->LoadScript(""), false);
119 }
120 
121 /**
122  * @tc.name: LoadScript_0200
123  * @tc.desc: load script with specify path.
124  * @tc.type: FUNC
125  * @tc.require: issueI6KODF
126  */
127 HWTEST_F(JsEnvironmentTest, LoadScript_0200, TestSize.Level0)
128 {
129     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
130     ASSERT_NE(jsEnv, nullptr);
131 
132     panda::RuntimeOption pandaOption;
133     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
134     ASSERT_EQ(ret, true);
135 
136     EXPECT_EQ(jsEnv->LoadScript("/system/etc/strip.native.min.abc"), true);
137 }
138 
139 /**
140  * @tc.name: JsEnvInitTimerModule_0100
141  * @tc.desc: Initialize timer module.
142  * @tc.type: FUNC
143  * @tc.require: issueI6Z5M5
144  */
145 HWTEST_F(JsEnvironmentTest, JsEnvInitTimerModule_0100, TestSize.Level0)
146 {
147     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
148     ASSERT_NE(jsEnv, nullptr);
149 
150     // Init timer module when native engine is invalid.
151     jsEnv->InitTimerModule();
152 
153     panda::RuntimeOption pandaOption;
154     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
155     ASSERT_EQ(ret, true);
156 
157     // Init timer module when native engine has created.
158     jsEnv->InitTimerModule();
159 }
160 
161 /**
162  * @tc.name: PostTask_0100
163  * @tc.desc: PostTask
164  * @tc.type: FUNC
165  * @tc.require: issue
166  */
167 HWTEST_F(JsEnvironmentTest, PostTask_0100, TestSize.Level0)
168 {
169     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
170     ASSERT_NE(jsEnv, nullptr);
171 
172     // Init timer module when native engine is invalid.
173     std::function<void()> task = CallBackModuleFunc;
174     std::string name = "NAME";
175     int64_t delayTime = 10;
176     jsEnv->PostTask(task, name, delayTime);
177 }
178 
179 /**
180  * @tc.name: RemoveTask_0100
181  * @tc.desc: RemoveTask
182  * @tc.type: FUNC
183  * @tc.require: issue
184  */
185 HWTEST_F(JsEnvironmentTest, RemoveTask_0100, TestSize.Level0)
186 {
187     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
188     ASSERT_NE(jsEnv, nullptr);
189 
190     std::string name = "NAME";
191     jsEnv->RemoveTask(name);
192 }
193 
194 /**
195  * @tc.name: InitSyscapModule_0100
196  * @tc.desc: InitSyscapModule
197  * @tc.type: FUNC
198  * @tc.require: issue
199  */
200 HWTEST_F(JsEnvironmentTest, InitSyscapModule_0100, TestSize.Level0)
201 {
202     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
203     ASSERT_NE(jsEnv, nullptr);
204 
205     jsEnv->InitSyscapModule();
206 }
207 
208 /**
209  * @tc.name: RegisterUncaughtExceptionHandler_0100
210  * @tc.desc: RegisterUncaughtExceptionHandler
211  * @tc.type: FUNC
212  * @tc.require: issue
213  */
214 HWTEST_F(JsEnvironmentTest, RegisterUncaughtExceptionHandler_0100, TestSize.Level0)
215 {
216     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
217     ASSERT_NE(jsEnv, nullptr);
218 
219     JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo;
220     jsEnv->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
221 }
222 
223 /**
224  * @tc.name: StartDebugger_0100
225  * @tc.desc: StartDebugger
226  * @tc.type: FUNC
227  * @tc.require: issue
228  */
229 HWTEST_F(JsEnvironmentTest, StartDebugger_0100, TestSize.Level0)
230 {
231     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
232     ASSERT_NE(jsEnv, nullptr);
233 
234     std::string option = "ark:1234@Debugger";
235     uint32_t socketFd = 10;
236     bool isDebugApp = true;
237     bool result = jsEnv->StartDebugger(option, socketFd, isDebugApp);
238     ASSERT_EQ(result, false);
239 }
240 
241 /**
242  * @tc.name: StartDebugger_0200
243  * @tc.desc: StartDebugger
244  * @tc.type: FUNC
245  * @tc.require: issue
246  */
247 HWTEST_F(JsEnvironmentTest, StartDebugger_0200, TestSize.Level0)
248 {
249     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
250     ASSERT_NE(jsEnv, nullptr);
251     panda::RuntimeOption pandaOption;
252     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
253     ASSERT_EQ(ret, true);
254 
255     std::string option = "ark:1234@Debugger";
256     uint32_t socketFd = 10;
257     bool isDebugApp = true;
258     bool result = jsEnv->StartDebugger(option, socketFd, isDebugApp);
259     ASSERT_EQ(result, false);
260 }
261 
262 /**
263  * @tc.name: StopDebugger_0100
264  * @tc.desc: StopDebugger
265  * @tc.type: FUNC
266  * @tc.require: issue
267  */
268 HWTEST_F(JsEnvironmentTest, StopDebugger_0100, TestSize.Level0)
269 {
270     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
271     ASSERT_NE(jsEnv, nullptr);
272 
273     jsEnv->StopDebugger();
274 }
275 
276 /**
277  * @tc.name: StopDebugger_0200
278  * @tc.desc: StopDebugger
279  * @tc.type: FUNC
280  * @tc.require: issue
281  */
282 HWTEST_F(JsEnvironmentTest, StopDebugger_0200, TestSize.Level0)
283 {
284     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
285 
286     panda::RuntimeOption pandaOption;
287     jsEnv->Initialize(pandaOption, static_cast<void*>(this));
288     jsEnv->StopDebugger();
289     ASSERT_NE(jsEnv, nullptr);
290 }
291 
292 /**
293  * @tc.name: StopDebugger_0300
294  * @tc.desc: StopDebugger
295  * @tc.type: FUNC
296  * @tc.require: issue
297  */
298 HWTEST_F(JsEnvironmentTest, StopDebugger_0300, TestSize.Level0)
299 {
300     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
301 
302     panda::RuntimeOption pandaOption;
303     jsEnv->Initialize(pandaOption, static_cast<void*>(this));
304     std::string option = "ark:1234@Debugger";
305     jsEnv->StopDebugger(option);
306     ASSERT_NE(jsEnv, nullptr);
307 }
308 
309 /**
310  * @tc.name: InitConsoleModule_0100
311  * @tc.desc: InitConsoleModule
312  * @tc.type: FUNC
313  * @tc.require: issue
314  */
315 HWTEST_F(JsEnvironmentTest, InitConsoleModule_0100, TestSize.Level0)
316 {
317     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
318     ASSERT_NE(jsEnv, nullptr);
319 
320     jsEnv->InitConsoleModule();
321 
322     panda::RuntimeOption pandaOption;
323     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
324     ASSERT_EQ(ret, true);
325 
326     jsEnv->InitConsoleModule();
327 }
328 
329 /**
330  * @tc.name: StartProfiler_0100
331  * @tc.desc: StartProfiler
332  * @tc.type: FUNC
333  */
334 HWTEST_F(JsEnvironmentTest, StartProfiler_0100, TestSize.Level1)
335 {
336     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
337     ASSERT_NE(jsEnv, nullptr);
338 
339     const char* libraryPath = "LIBRARYPATH";
340     jsEnv->StartProfiler(libraryPath, 0, JsEnvironment::PROFILERTYPE::PROFILERTYPE_CPU, 0, 0, true);
341     ASSERT_EQ(jsEnv->GetVM(), nullptr);
342 }
343 
344 /**
345  * @tc.name: StartProfiler_0200
346  * @tc.desc: StartProfiler
347  * @tc.type: FUNC
348  */
349 HWTEST_F(JsEnvironmentTest, StartProfiler_0200, TestSize.Level1)
350 {
351     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
352     ASSERT_NE(jsEnv, nullptr);
353 
354     panda::RuntimeOption pandaOption;
355     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
356     ASSERT_EQ(ret, true);
357 
358     const char* libraryPath = "LIBRARYPATH";
359     jsEnv->StartProfiler(libraryPath, 0, JsEnvironment::PROFILERTYPE::PROFILERTYPE_HEAP, 0, 0, true);
360     ASSERT_NE(jsEnv->GetVM(), nullptr);
361 }
362 
363 /**
364  * @tc.name: PostSyncTask_0100
365  * @tc.desc: Js environment post sync task.
366  * @tc.type: FUNC
367  * @tc.require: issueI7C87T
368  */
369 HWTEST_F(JsEnvironmentTest, PostSyncTask_0100, TestSize.Level0)
370 {
371     auto runner = AppExecFwk::EventRunner::Create("TASK_RUNNER");
372     ASSERT_NE(runner, nullptr);
373     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>(runner));
374     ASSERT_NE(jsEnv, nullptr);
375     panda::RuntimeOption pandaOption;
376     ASSERT_EQ(jsEnv->Initialize(pandaOption, static_cast<void*>(this)), true);
377     ASSERT_EQ(jsEnv->InitLoop(), true);
378 
379     std::string taskName = "syncTask001";
380     bool taskExecuted = false;
__anonac4465820302() 381     auto task = [taskName, &taskExecuted]() {
382         taskExecuted = true;
383     };
384     jsEnv->PostSyncTask(task, taskName);
385     EXPECT_EQ(taskExecuted, true);
386 }
387 
388 /**
389  * @tc.name: SetRequestAotCallback_0100
390  * @tc.desc: Js environment SetRequestAotCallback.
391  * @tc.type: FUNC
392  * @tc.require: issueI82L1A
393  */
394 HWTEST_F(JsEnvironmentTest, SetRequestAotCallback_0100, TestSize.Level0)
395 {
396     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
397     ASSERT_NE(jsEnv, nullptr);
398 
__anonac4465820402(const std::string& bundleName, const std::string& moduleName, int32_t triggerMode) 399     auto callback = [](const std::string& bundleName, const std::string& moduleName, int32_t triggerMode) -> int32_t {
400         return 0;
401     };
402     jsEnv->SetRequestAotCallback(callback);
403 }
404 
405 /**
406  * @tc.name: ParseHdcRegisterOption_0100
407  * @tc.desc: Js environment ParseHdcRegisterOption.
408  * @tc.type: FUNC
409  */
410 HWTEST_F(JsEnvironmentTest, ParseHdcRegisterOption_0100, TestSize.Level0)
411 {
412     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
413     ASSERT_NE(jsEnv, nullptr);
414     std::string option1 = "";
415     int result1 = jsEnv->ParseHdcRegisterOption(option1);
416     ASSERT_EQ(result1, -1);
417     std::string option2 = "@";
418     int result2 = jsEnv->ParseHdcRegisterOption(option2);
419     ASSERT_EQ(result2, -1);
420     std::string option3 = ":";
421     int result3 = jsEnv->ParseHdcRegisterOption(option3);
422     ASSERT_EQ(result3, -1);
423     std::string option4 = "ark:123@Debugger";
424     int result4 = jsEnv->ParseHdcRegisterOption(option4);
425     ASSERT_EQ(result4, 123);
426     std::string option5 = "ark:123@456@Debugger";
427     int result5 = jsEnv->ParseHdcRegisterOption(option5);
428     ASSERT_EQ(result5, 456);
429 }
430 
431 /**
432  * @tc.name: SetDeviceDisconnectCallback_0100
433  * @tc.desc: Js environment SetDeviceDisconnectCallback.
434  * @tc.type: FUNC
435  * @tc.require: issueI82L1A
436  */
437 HWTEST_F(JsEnvironmentTest, SetDeviceDisconnectCallback_0100, TestSize.Level0)
438 {
439     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
440     ASSERT_NE(jsEnv, nullptr);
441     panda::RuntimeOption pandaOption;
442     auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
443     ASSERT_EQ(ret, true);
444 
445     bool taskExecuted = false;
__anonac4465820502() 446     auto task = [&taskExecuted]() {
447         return true;
448     };
449     jsEnv->SetDeviceDisconnectCallback(task);
450     ASSERT_EQ(taskExecuted, false);
451 }
452 
453 /**
454  * @tc.name: DestroyHeapProfiler_0100
455  * @tc.desc: Js environment DestroyHeapProfiler.
456  * @tc.type: FUNC
457  */
458 HWTEST_F(JsEnvironmentTest, DestroyHeapProfiler_0100, TestSize.Level0)
459 {
460     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
461     jsEnv->DestroyHeapProfiler();
462     ASSERT_NE(jsEnv, nullptr);
463 }
464 
465 /**
466  * @tc.name: NotifyDebugMode_0100
467  * @tc.desc: Js environment NotifyDebugMode.
468  * @tc.type: FUNC
469  */
470 HWTEST_F(JsEnvironmentTest, NotifyDebugMode_0100, TestSize.Level0)
471 {
472     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
473     int tid = 1;
474     char* libraryPath;
475     uint32_t instanceId = 1;
476     bool debug = true;
477     bool debugMode = true;
478     jsEnv->NotifyDebugMode(tid, libraryPath, instanceId, debug, debugMode);
479     ASSERT_NE(jsEnv, nullptr);
480 }
481 
482 /**
483  * @tc.name: GetDebuggerPostTask_0100
484  * @tc.desc: Js environment GetDebuggerPostTask.
485  * @tc.type: FUNC
486  */
487 HWTEST_F(JsEnvironmentTest, GetDebuggerPostTask_0100, TestSize.Level0)
488 {
489     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
490     jsEnv->GetDebuggerPostTask();
491     ASSERT_NE(jsEnv, nullptr);
492 }
493 
494 /**
495  * @tc.name: GetDebuggerPostTask_0200
496  * @tc.desc: Js environment GetDebuggerPostTask.
497  * @tc.type: FUNC
498  */
499 HWTEST_F(JsEnvironmentTest, GetDebuggerPostTask_0200, TestSize.Level0)
500 {
501     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
502     auto poster = jsEnv->GetDebuggerPostTask();
503     ASSERT_NE(jsEnv, nullptr);
__anonac4465820602() 504     poster([]() {
505         std::string temp;
506     });
507 }
508 
509 /**
510  * @tc.name: GetHeapPrepare_0100
511  * @tc.desc: Js environment GetHeapPrepare.
512  * @tc.type: FUNC
513  */
514 HWTEST_F(JsEnvironmentTest, GetHeapPrepare_0100, TestSize.Level0)
515 {
516     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
517     jsEnv->GetHeapPrepare();
518     ASSERT_NE(jsEnv, nullptr);
519 }
520 
521 /**
522  * @tc.name: GetHeapPrepare_0200
523  * @tc.desc: Js environment GetHeapPrepare.
524  * @tc.type: FUNC
525  */
526 HWTEST_F(JsEnvironmentTest, GetHeapPrepare_0200, TestSize.Level0)
527 {
528     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
529     panda::RuntimeOption pandaOption;
530     jsEnv->Initialize(pandaOption, static_cast<void*>(this));
531     jsEnv->GetHeapPrepare();
532     ASSERT_NE(jsEnv, nullptr);
533 }
534 
535 /**
536  * @tc.name: GetSourceMapOperator_0100
537  * @tc.desc: Js environment GetSourceMapOperator.
538  * @tc.type: FUNC
539  */
540 HWTEST_F(JsEnvironmentTest, GetSourceMapOperator_0100, TestSize.Level0)
541 {
542     auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
543     jsEnv->GetSourceMapOperator();
544     ASSERT_NE(jsEnv, nullptr);
545 }
546 } // namespace JsEnv
547 } // namespace OHOS
548