1 /*
2 * Copyright (c) 2024 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 "hilog_tag_wrapper.h"
29 #include "mock_js_runtime.h"
30 #include "mock_jsnapi.h"
31
32 using namespace testing;
33 using namespace testing::ext;
34 using namespace testing::mt;
35
36 namespace OHOS {
37 namespace AbilityRuntime {
38 namespace {
39 const std::string TEST_BUNDLE_NAME = "com.ohos.contactsdataability";
40 const std::string TEST_MODULE_NAME = ".ContactsDataAbility";
41 const std::string TEST_ABILITY_NAME = "ContactsDataAbility";
42 const std::string TEST_CODE_PATH = "/data/storage/el1/bundle";
43 const std::string TEST_HAP_PATH =
44 "/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 Runtime::Options options_;
55 };
56
SetUpTestCase()57 void JsRuntimeTest::SetUpTestCase() {}
58
TearDownTestCase()59 void JsRuntimeTest::TearDownTestCase() {}
60
SetUp()61 void JsRuntimeTest::SetUp()
62 {
63 Runtime::Options newOptions;
64 options_ = newOptions;
65 options_.bundleName = TEST_BUNDLE_NAME;
66 options_.codePath = TEST_CODE_PATH;
67 options_.loadAce = false;
68 options_.isBundle = true;
69 options_.preload = false;
70 std::shared_ptr<AppExecFwk::EventRunner> eventRunner =
71 AppExecFwk::EventRunner::Create(TEST_ABILITY_NAME);
72 options_.eventRunner = eventRunner;
73 }
74
TearDown()75 void JsRuntimeTest::TearDown() {}
76
77 /**
78 * @tc.name: DebuggerConnectionHandler_0100
79 * @tc.desc: JsRuntime test for DebuggerConnectionHandler.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0100, TestSize.Level1)
83 {
84 AbilityRuntime::Runtime::Options options;
85 options.preload = true;
86 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
87 ASSERT_NE(jsRuntime, nullptr);
88 bool isDebugApp = true;
89 bool isStartWithDebug = false;
90 jsRuntime->jsEnv_->vm_ = nullptr;
91 jsRuntime->DebuggerConnectionHandler(isDebugApp, isStartWithDebug);
92 EXPECT_NE(jsRuntime, nullptr);
93 jsRuntime.reset();
94 }
95
96 /**
97 * @tc.name: DebuggerConnectionHandler_0200
98 * @tc.desc: JsRuntime test for DebuggerConnectionHandler.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(JsRuntimeTest, UpdatePkgContextInfoJson_0200, TestSize.Level1)
102 {
103 AbilityRuntime::Runtime::Options options;
104 options.preload = true;
105 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
106 jsRuntime->jsEnv_->vm_ = nullptr;
107 ASSERT_NE(jsRuntime, nullptr);
108 bool isDebugApp = true;
109 bool isStartWithDebug = false;
110 jsRuntime->DebuggerConnectionHandler(isDebugApp, isStartWithDebug);
111 EXPECT_NE(jsRuntime, nullptr);
112 jsRuntime.reset();
113 }
114
115 /**
116 * @tc.name: GetSafeData_0100
117 * @tc.desc: JsRuntime test for GetSafeData.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(JsRuntimeTest, GetSafeData_0100, TestSize.Level1)
121 {
122 TAG_LOGI(AAFwkTag::TEST, "GetSafeData_0100 start");
123 AbilityRuntime::Runtime::Options options;
124 options.preload = true;
125 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
126 ASSERT_NE(jsRuntime, nullptr);
127 std::string path = "";
128 std::string fileFullName = "";
129 jsRuntime->jsEnv_->vm_ = nullptr;
130 auto result = jsRuntime->GetSafeData(path, fileFullName);
131 EXPECT_EQ(result, nullptr);
132 jsRuntime.reset();
133 std::this_thread::sleep_for(std::chrono::milliseconds(200));
134 TAG_LOGI(AAFwkTag::TEST, "GetSafeData_0100 end");
135 }
136
137 /**
138 * @tc.name: DebuggerConnectionManager_0100
139 * @tc.desc: JsRuntime test for DebuggerConnectionManager.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0100, TestSize.Level1)
143 {
144 TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0100 start");
145 AbilityRuntime::Runtime::Options options;
146 options.preload = true;
147 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
148 ASSERT_NE(jsRuntime, nullptr);
149 bool isDebugApp = true;
150 bool isStartWithDebug = true;
151 AbilityRuntime::Runtime::DebugOption dOption;
152 dOption.perfCmd = "profile jsperf 100";
153 dOption.isStartWithDebug = false;
154 dOption.processName = "test";
155 dOption.isDebugApp = true;
156 dOption.isStartWithNative = false;
157 jsRuntime->jsEnv_->vm_ = nullptr;
158 jsRuntime->DebuggerConnectionManager(isDebugApp, isStartWithDebug, dOption);
159 EXPECT_NE(jsRuntime, nullptr);
160 jsRuntime.reset();
161 std::this_thread::sleep_for(std::chrono::milliseconds(200));
162 TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0100 end");
163 }
164
165 /**
166 * @tc.name: DebuggerConnectionManager_0200
167 * @tc.desc: JsRuntime test for DebuggerConnectionManager.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(JsRuntimeTest, DebuggerConnectionManager_0200, TestSize.Level1)
171 {
172 TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0200 start");
173 AbilityRuntime::Runtime::Options options;
174 options.preload = true;
175 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
176 ASSERT_NE(jsRuntime, nullptr);
177 bool isDebugApp = false;
178 bool isStartWithDebug = true;
179 AbilityRuntime::Runtime::DebugOption dOption;
180 dOption.perfCmd = "profile jsperf 100";
181 dOption.isStartWithDebug = false;
182 dOption.processName = "test";
183 dOption.isDebugApp = true;
184 dOption.isStartWithNative = false;
185 jsRuntime->jsEnv_->vm_ = nullptr;
186 jsRuntime->DebuggerConnectionManager(isDebugApp, isStartWithDebug, dOption);
187 EXPECT_NE(jsRuntime, nullptr);
188 jsRuntime.reset();
189 std::this_thread::sleep_for(std::chrono::milliseconds(200));
190 TAG_LOGI(AAFwkTag::TEST, "DebuggerConnectionManager_0200 end");
191 }
192
193 /**
194 * @tc.name: SetDebugOption_0100
195 * @tc.desc: JsRuntime test for SetDebugOption.
196 * @tc.type: FUNC
197 */
198 HWTEST_F(JsRuntimeTest, SetDebugOption_0100, TestSize.Level1)
199 {
200 TAG_LOGI(AAFwkTag::TEST, "SetDebugOption_0100 start");
201 AbilityRuntime::Runtime::Options options;
202 options.preload = true;
203 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
204 EXPECT_NE(jsRuntime, nullptr);
205 AbilityRuntime::Runtime::DebugOption dOption;
206 dOption.perfCmd = "profile test";
207 dOption.processName = "testProcess";
208 EXPECT_NE(jsRuntime->debugOption_.perfCmd, "profile test");
209 EXPECT_NE(jsRuntime->debugOption_.processName, "testProcess");
210 jsRuntime->SetDebugOption(dOption);
211 EXPECT_EQ(jsRuntime->debugOption_.perfCmd, "profile test");
212 EXPECT_EQ(jsRuntime->debugOption_.processName, "testProcess");
213 jsRuntime.reset();
214 std::this_thread::sleep_for(std::chrono::milliseconds(200));
215 TAG_LOGI(AAFwkTag::TEST, "SetDebugOption_0100 end");
216 }
217
218 /**
219 * @tc.name: StartLocalDebugMode_0100
220 * @tc.desc: JsRuntime test for StartLocalDebugMode with isDebugFromLocal=true.
221 * @tc.type: FUNC
222 */
223 HWTEST_F(JsRuntimeTest, StartLocalDebugMode_0100, TestSize.Level1)
224 {
225 TAG_LOGI(AAFwkTag::TEST, "StartLocalDebugMode_0100 start");
226 AbilityRuntime::Runtime::Options options;
227 options.preload = true;
228 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
229 EXPECT_NE(jsRuntime, nullptr);
230 jsRuntime->debugOption_.isDebugFromLocal=false;
231 jsRuntime->StartLocalDebugMode(true);
232 EXPECT_EQ(jsRuntime->debugOption_.isDebugFromLocal, true);
233 jsRuntime.reset();
234 std::this_thread::sleep_for(std::chrono::milliseconds(200));
235 TAG_LOGI(AAFwkTag::TEST, "StartLocalDebugMode_0100 end");
236 }
237
238 /**
239 * @tc.name: SetStopPreloadSoCallback_0100
240 * @tc.desc: JsRuntime test for SetStopPreloadSoCallback to verify GetEcmaVm is called.
241 * @tc.type: FUNC
242 */
243 HWTEST_F(JsRuntimeTest, SetStopPreloadSoCallback_0100, TestSize.Level1)
244 {
245 TAG_LOGI(AAFwkTag::TEST, "SetStopPreloadSoCallback_0100 start");
246 AbilityRuntime::Runtime::Options options;
247 options.preload = true;
248 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
249 int mark = 0;
__anon459d97350202() 250 auto callback = [&mark]() {
251 mark = 1;
252 TAG_LOGI(AAFwkTag::TEST, "Callback function called");
253 };
254 jsRuntime->SetStopPreloadSoCallback(callback);
255 jsRuntime.reset();
256 EXPECT_EQ(mark, 0);
257 std::this_thread::sleep_for(std::chrono::milliseconds(200));
258 TAG_LOGI(AAFwkTag::TEST, "SetStopPreloadSoCallback_0100 end");
259 }
260
261 /**
262 * @tc.name: StartProfiler_0100
263 * @tc.desc: JsRuntime test for StartProfiler.
264 * @tc.type: FUNC
265 */
266 HWTEST_F(JsRuntimeTest, StartProfiler_0100, TestSize.Level1)
267 {
268 TAG_LOGI(AAFwkTag::TEST, "StartProfiler_0100 start");
269 AbilityRuntime::Runtime::Options options;
270 options.preload = true;
271 auto jsRuntime = AbilityRuntime::JsRuntime::Create(options);
272 ASSERT_NE(jsRuntime, nullptr);
273 AbilityRuntime::Runtime::DebugOption dOption;
274 dOption.isDebugFromLocal = false;
275 dOption.isDeveloperMode = false;
276 dOption.isDebugApp = true;
277 dOption.isStartWithDebug = false;
278 dOption.processName = "testProcess";
279 dOption.perfCmd = "profile jsperf 100";
280 jsRuntime->instanceId_ = 999;
281 jsRuntime->StartProfiler(dOption);
282 EXPECT_EQ(jsRuntime->instanceId_, 999);
283 jsRuntime.reset();
284 std::this_thread::sleep_for(std::chrono::milliseconds(200));
285 TAG_LOGI(AAFwkTag::TEST, "StartProfiler_0100 end");
286 }
287
288
289 } // namespace AbilityRuntime
290 } // namespace OHOS