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