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
__anon3d3b0eaa0302() 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 const char* libraryPath = "LIBRARYPATH";
261 bool needBreakPoint = true;
262 uint32_t instanceId = 10;
263 bool result = jsEnv->StartDebugger(libraryPath, needBreakPoint, instanceId);
264 EXPECT_EQ(result, false);
265 }
266
267 /**
268 * @tc.name: StopDebugger_0100
269 * @tc.desc: StopDebugger
270 * @tc.type: FUNC
271 * @tc.require: issue
272 */
273 HWTEST_F(JsEnvironmentTest, StopDebugger_0100, TestSize.Level0)
274 {
275 auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
276 ASSERT_NE(jsEnv, nullptr);
277
278 jsEnv->StopDebugger();
279 }
280
281 /**
282 * @tc.name: InitConsoleModule_0100
283 * @tc.desc: InitConsoleModule
284 * @tc.type: FUNC
285 * @tc.require: issue
286 */
287 HWTEST_F(JsEnvironmentTest, InitConsoleModule_0100, TestSize.Level0)
288 {
289 auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
290 ASSERT_NE(jsEnv, nullptr);
291
292 jsEnv->InitConsoleModule();
293
294 panda::RuntimeOption pandaOption;
295 auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
296 ASSERT_EQ(ret, true);
297
298 jsEnv->InitConsoleModule();
299 }
300
301 /**
302 * @tc.name: StartProfiler_0100
303 * @tc.desc: StartProfiler
304 * @tc.type: FUNC
305 */
306 HWTEST_F(JsEnvironmentTest, StartProfiler_0100, TestSize.Level1)
307 {
308 auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
309 ASSERT_NE(jsEnv, nullptr);
310
311 const char* libraryPath = "LIBRARYPATH";
312 jsEnv->StartProfiler(libraryPath, 0, JsEnvironment::PROFILERTYPE::PROFILERTYPE_CPU, 0);
313 ASSERT_EQ(jsEnv->GetVM(), nullptr);
314 }
315
316 /**
317 * @tc.name: StartProfiler_0200
318 * @tc.desc: StartProfiler
319 * @tc.type: FUNC
320 */
321 HWTEST_F(JsEnvironmentTest, StartProfiler_0200, TestSize.Level1)
322 {
323 auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>());
324 ASSERT_NE(jsEnv, nullptr);
325
326 panda::RuntimeOption pandaOption;
327 auto ret = jsEnv->Initialize(pandaOption, static_cast<void*>(this));
328 ASSERT_EQ(ret, true);
329
330 const char* libraryPath = "LIBRARYPATH";
331 jsEnv->StartProfiler(libraryPath, 0, JsEnvironment::PROFILERTYPE::PROFILERTYPE_HEAP, 0);
332 ASSERT_NE(jsEnv->GetVM(), nullptr);
333 }
334
335 /**
336 * @tc.name: PostSyncTask_0100
337 * @tc.desc: Js environment post sync task.
338 * @tc.type: FUNC
339 * @tc.require: issueI7C87T
340 */
341 HWTEST_F(JsEnvironmentTest, PostSyncTask_0100, TestSize.Level0)
342 {
343 auto runner = AppExecFwk::EventRunner::Create("TASK_RUNNER");
344 ASSERT_NE(runner, nullptr);
345 auto jsEnv = std::make_shared<JsEnvironment>(std::make_unique<AbilityRuntime::OHOSJsEnvironmentImpl>(runner));
346 ASSERT_NE(jsEnv, nullptr);
347 panda::RuntimeOption pandaOption;
348 ASSERT_EQ(jsEnv->Initialize(pandaOption, static_cast<void*>(this)), true);
349 ASSERT_EQ(jsEnv->InitLoop(), true);
350
351 std::string taskName = "syncTask001";
352 bool taskExecuted = false;
__anon3d3b0eaa0402() 353 auto task = [taskName, &taskExecuted]() {
354 JSENV_LOG_I("%{public}s called.", taskName.c_str());
355 taskExecuted = true;
356 };
357 jsEnv->PostSyncTask(task, taskName);
358 EXPECT_EQ(taskExecuted, true);
359 }
360 } // namespace JsEnv
361 } // namespace OHOS
362