• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
16 #include <sys/stat.h>
17 #include "gtest/gtest.h"
18 #include "window.h"
19 #define private public
20 #define protected public
21 #include "MockGlobalResult.h"
22 #include "JsAppImpl.h"
23 #include "CommandLineInterface.h"
24 #include "CommandParser.h"
25 #include "VirtualScreenImpl.h"
26 #include "window_model.h"
27 #include "window_display.h"
28 #include "FileSystem.h"
29 #include "ace_preview_helper.h"
30 #include "window_model.h"
31 
32 namespace {
33     class JsAppImplTest : public ::testing::Test {
34     public:
JsAppImplTest()35         JsAppImplTest() {}
~JsAppImplTest()36         ~JsAppImplTest() {}
37     protected:
WriteFile(std::string testFile,std::string fileContent)38         static void WriteFile(std::string testFile, std::string fileContent)
39         {
40             std::ofstream file(testFile, std::ios::out | std::ios::in | std::ios_base::trunc);
41             if (file.is_open()) {
42                 file << fileContent;
43                 file.close();
44             } else {
45                 printf("Error open file!\n");
46             }
47         }
48 
SetUpTestCase()49         static void SetUpTestCase()
50         {
51             CommandLineInterface::GetInstance().Init("pipeName");
52             char buffer[FILENAME_MAX];
53             if (getcwd(buffer, FILENAME_MAX) != nullptr) {
54                 testDir = std::string(buffer);
55             } else {
56                 printf("error: getcwd failed\n");
57             }
58         }
59 
60         static std::string testDir;
61     };
62     std::string JsAppImplTest::testDir = "";
63 
64     // 测试拷贝构造函数是否被删除
TEST_F(JsAppImplTest,CopyConstructorDeletedTest)65     TEST_F(JsAppImplTest, CopyConstructorDeletedTest)
66     {
67         EXPECT_TRUE(std::is_copy_constructible<JsApp>::value == false);
68     }
69 
70     // 测试赋值运算符是否被删除
TEST_F(JsAppImplTest,AssignmentOperatorDeletedTest)71     TEST_F(JsAppImplTest, AssignmentOperatorDeletedTest)
72     {
73         EXPECT_TRUE(std::is_copy_assignable<JsApp>::value == false);
74     }
75 
TEST_F(JsAppImplTest,StartTest)76     TEST_F(JsAppImplTest, StartTest)
77     {
78         JsAppImpl::GetInstance().isStop = false;
79         std::thread thread1([]() {
80             JsAppImpl::GetInstance().Start();
81         });
82         thread1.detach();
83         std::this_thread::sleep_for(std::chrono::milliseconds(10));
84         JsAppImpl::GetInstance().isStop = true;
85         std::this_thread::sleep_for(std::chrono::milliseconds(10));
86         EXPECT_TRUE(JsAppImpl::GetInstance().isFinished);
87     }
88 
TEST_F(JsAppImplTest,RestartTest)89     TEST_F(JsAppImplTest, RestartTest)
90     {
91         JsAppImpl::GetInstance().ability =
92             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
93         JsAppImpl::GetInstance().Restart();
94         bool eq = JsAppImpl::GetInstance().ability == nullptr;
95         EXPECT_TRUE(eq);
96     }
97 
TEST_F(JsAppImplTest,InterruptTest)98     TEST_F(JsAppImplTest, InterruptTest)
99     {
100         JsAppImpl::GetInstance().isStop = false;
101         JsAppImpl::GetInstance().Interrupt();
102         EXPECT_TRUE(JsAppImpl::GetInstance().isStop);
103     }
104 
TEST_F(JsAppImplTest,GetJSONTreeTest)105     TEST_F(JsAppImplTest, GetJSONTreeTest)
106     {
107         EXPECT_EQ(JsAppImpl::GetInstance().GetJSONTree(), "jsontree");
108     }
109 
TEST_F(JsAppImplTest,GetDefaultJSONTreeTest)110     TEST_F(JsAppImplTest, GetDefaultJSONTreeTest)
111     {
112         EXPECT_EQ(JsAppImpl::GetInstance().GetDefaultJSONTree(), "defaultjsontree");
113     }
114 
TEST_F(JsAppImplTest,OrientationChangedTest)115     TEST_F(JsAppImplTest, OrientationChangedTest)
116     {
117         JsAppImpl::GetInstance().ability =
118             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
119         g_surfaceChanged = false;
120         JsAppImpl::GetInstance().OrientationChanged("portrait");
121         EXPECT_TRUE(g_surfaceChanged);
122         EXPECT_EQ(JsAppImpl::GetInstance().orientation, "portrait");
123         g_surfaceChanged = false;
124         JsAppImpl::GetInstance().OrientationChanged("landscape");
125         EXPECT_TRUE(g_surfaceChanged);
126         EXPECT_EQ(JsAppImpl::GetInstance().orientation, "landscape");
127     }
128 
TEST_F(JsAppImplTest,ResolutionChangedTest)129     TEST_F(JsAppImplTest, ResolutionChangedTest)
130     {
131         JsAppImpl::GetInstance().isStop = false;
132         std::thread thread1([]() {
133             JsAppImpl::GetInstance().Start();
134         });
135         thread1.detach();
136         std::this_thread::sleep_for(std::chrono::milliseconds(10));
137         int32_t originWidth = 222;
138         int32_t originHeight = 333;
139         int32_t width = 222;
140         int32_t height = 333;
141         int32_t screenDensity = 360;
142         std::string reason = "resize";
143         ResolutionParam param(originWidth, originHeight, width, height);
144         g_surfaceChanged = false;
145         JsAppImpl::GetInstance().ResolutionChanged(param, screenDensity, reason);
146         EXPECT_TRUE(g_surfaceChanged);
147         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, 222);
148         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, 333);
149     }
150 
TEST_F(JsAppImplTest,ConvertResizeReasonTest)151     TEST_F(JsAppImplTest, ConvertResizeReasonTest)
152     {
153         EXPECT_EQ(JsAppImpl::GetInstance().ConvertResizeReason("undefined"),
154             OHOS::Ace::WindowSizeChangeReason::UNDEFINED);
155         EXPECT_EQ(JsAppImpl::GetInstance().ConvertResizeReason("rotation"),
156             OHOS::Ace::WindowSizeChangeReason::ROTATION);
157         EXPECT_EQ(JsAppImpl::GetInstance().ConvertResizeReason("resize"),
158             OHOS::Ace::WindowSizeChangeReason::RESIZE);
159     }
160 
TEST_F(JsAppImplTest,SetResolutionParamsTest)161     TEST_F(JsAppImplTest, SetResolutionParamsTest)
162     {
163         CommandParser::GetInstance().deviceType = "phone";
164         JsAppImpl::GetInstance().InitCommandInfo();
165         int32_t originWidth = 111;
166         int32_t originHeight = 222;
167         int32_t width = 111;
168         int32_t height = 222;
169         int32_t screenDensity = 480;
170         JsAppImpl::GetInstance().SetResolutionParams(originWidth, originHeight, width, height, screenDensity);
171         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, 111);
172         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, 222);
173         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.density, 3);
174         EXPECT_EQ(JsAppImpl::GetInstance().orientation, "portrait");
175         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.orientation,
176             OHOS::Ace::DeviceOrientation::PORTRAIT);
177 
178         originWidth = 222;
179         originHeight = 111;
180         width = 222;
181         height = 111;
182         screenDensity = 320;
183         JsAppImpl::GetInstance().SetResolutionParams(originWidth, originHeight, width, height, screenDensity);
184         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, 222);
185         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, 111);
186         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.density, 2);
187         EXPECT_EQ(JsAppImpl::GetInstance().orientation, "landscape");
188         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.orientation,
189             OHOS::Ace::DeviceOrientation::LANDSCAPE);
190     }
191 
TEST_F(JsAppImplTest,SetArgsColorModeTest)192     TEST_F(JsAppImplTest, SetArgsColorModeTest)
193     {
194         JsAppImpl::GetInstance().SetArgsColorMode("light");
195         EXPECT_EQ("light", JsAppImpl::GetInstance().colorMode);
196     }
197 
TEST_F(JsAppImplTest,SetArgsAceVersionTest)198     TEST_F(JsAppImplTest, SetArgsAceVersionTest)
199     {
200         JsAppImpl::GetInstance().SetArgsAceVersion("ACE_2_0");
201         EXPECT_EQ("ACE_2_0", JsAppImpl::GetInstance().aceVersion);
202     }
203 
TEST_F(JsAppImplTest,SetDeviceOrentationTest)204     TEST_F(JsAppImplTest, SetDeviceOrentationTest)
205     {
206         JsAppImpl::GetInstance().SetDeviceOrentation("landscape");
207         EXPECT_EQ("landscape", JsAppImpl::GetInstance().orientation);
208     }
209 
TEST_F(JsAppImplTest,GetOrientationTest)210     TEST_F(JsAppImplTest, GetOrientationTest)
211     {
212         JsAppImpl::GetInstance().orientation = "portrait";
213         EXPECT_EQ(JsAppImpl::GetInstance().GetOrientation(), "portrait");
214     }
215 
TEST_F(JsAppImplTest,GetColorModeTest)216     TEST_F(JsAppImplTest, GetColorModeTest)
217     {
218         JsAppImpl::GetInstance().colorMode = "dark";
219         EXPECT_EQ(JsAppImpl::GetInstance().GetColorMode(), "dark");
220     }
221 
TEST_F(JsAppImplTest,ColorModeChangedTest)222     TEST_F(JsAppImplTest, ColorModeChangedTest)
223     {
224         JsAppImpl::GetInstance().ability =
225             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
226         // light
227         g_onConfigurationChanged = false;
228         JsAppImpl::GetInstance().ColorModeChanged("light");
229         EXPECT_TRUE(g_onConfigurationChanged);
230         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.colorMode,
231             OHOS::Ace::ColorMode::LIGHT);
232         // dark
233         g_onConfigurationChanged = false;
234         JsAppImpl::GetInstance().ColorModeChanged("dark");
235         EXPECT_TRUE(g_onConfigurationChanged);
236         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.colorMode,
237             OHOS::Ace::ColorMode::DARK);
238     }
239 
TEST_F(JsAppImplTest,ReloadRuntimePageTest)240     TEST_F(JsAppImplTest, ReloadRuntimePageTest)
241     {
242         JsAppImpl::GetInstance().ability =
243             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
244         g_replacePage = false;
245         JsAppImpl::GetInstance().ReloadRuntimePage("pages/Index");
246         EXPECT_TRUE(g_replacePage);
247     }
248 
TEST_F(JsAppImplTest,SetScreenDensityTest)249     TEST_F(JsAppImplTest, SetScreenDensityTest)
250     {
251         JsAppImpl::GetInstance().SetScreenDensity("360");
252         EXPECT_EQ(JsAppImpl::GetInstance().screenDensity, "360");
253     }
254 
TEST_F(JsAppImplTest,SetConfigChangesTest)255     TEST_F(JsAppImplTest, SetConfigChangesTest)
256     {
257         JsAppImpl::GetInstance().SetConfigChanges("aaa");
258         EXPECT_EQ(JsAppImpl::GetInstance().configChanges, "aaa");
259     }
260 
TEST_F(JsAppImplTest,MemoryRefreshTest)261     TEST_F(JsAppImplTest, MemoryRefreshTest)
262     {
263         // ability is nullptr
264         JsAppImpl::GetInstance().ability = nullptr;
265         g_operateComponent = false;
266         JsAppImpl::GetInstance().MemoryRefresh("aaa");
267         EXPECT_FALSE(g_operateComponent);
268         // ability is not nullptr
269         JsAppImpl::GetInstance().ability =
270             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
271         g_operateComponent = false;
272         JsAppImpl::GetInstance().MemoryRefresh("aaa");
273         EXPECT_TRUE(g_operateComponent);
274     }
275 
TEST_F(JsAppImplTest,LoadDocumentTest)276     TEST_F(JsAppImplTest, LoadDocumentTest)
277     {
278         JsAppImpl::GetInstance().ability =
279             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
280         Json2::Value val = JsonReader::CreateNull();
281         g_loadAceDocument = false;
282         JsAppImpl::GetInstance().LoadDocument("aaa", "bbb", val);
283         EXPECT_TRUE(g_loadAceDocument);
284     }
285 
TEST_F(JsAppImplTest,FoldStatusChangedTest)286     TEST_F(JsAppImplTest, FoldStatusChangedTest)
287     {
288         JsAppImpl::GetInstance().isStop = false;
289         std::thread thread1([]() {
290             JsAppImpl::GetInstance().Start();
291         });
292         thread1.detach();
293         std::this_thread::sleep_for(std::chrono::milliseconds(10));
294         int width = 200;
295         int height = 300;
296         g_execStatusChangedCallback = false;
297         JsAppImpl::GetInstance().FoldStatusChanged("fold", width, height);
298         EXPECT_EQ(VirtualScreenImpl::GetInstance().foldStatus, "fold");
299         EXPECT_EQ(OHOS::Previewer::PreviewerDisplay::GetInstance().foldStatus_, OHOS::Rosen::FoldStatus::FOLDED);
300         EXPECT_TRUE(g_execStatusChangedCallback);
301         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, width);
302         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, height);
303 
304         JsAppImpl::GetInstance().FoldStatusChanged("unfold", width, height);
305         EXPECT_EQ(VirtualScreenImpl::GetInstance().foldStatus, "unfold");
306         EXPECT_EQ(OHOS::Previewer::PreviewerDisplay::GetInstance().foldStatus_, OHOS::Rosen::FoldStatus::EXPAND);
307 
308         JsAppImpl::GetInstance().FoldStatusChanged("half_fold", width, height);
309         EXPECT_EQ(VirtualScreenImpl::GetInstance().foldStatus, "half_fold");
310         EXPECT_EQ(OHOS::Previewer::PreviewerDisplay::GetInstance().foldStatus_, OHOS::Rosen::FoldStatus::HALF_FOLD);
311 
312         width = 210;
313         height = 310;
314         g_execStatusChangedCallback = false;
315         JsAppImpl::GetInstance().FoldStatusChanged("unknown", width, height);
316         EXPECT_EQ(VirtualScreenImpl::GetInstance().foldStatus, "unknown");
317         EXPECT_EQ(OHOS::Previewer::PreviewerDisplay::GetInstance().foldStatus_, OHOS::Rosen::FoldStatus::UNKNOWN);
318         EXPECT_TRUE(g_execStatusChangedCallback);
319         EXPECT_NE(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, width);
320         EXPECT_NE(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, height);
321     }
322 
TEST_F(JsAppImplTest,DispatchBackPressedEventTest)323     TEST_F(JsAppImplTest, DispatchBackPressedEventTest)
324     {
325         g_onBackPressed = false;
326         JsAppImpl::GetInstance().DispatchBackPressedEvent();
327         EXPECT_TRUE(g_onBackPressed);
328     }
329 
TEST_F(JsAppImplTest,DispatchKeyEventTest)330     TEST_F(JsAppImplTest, DispatchKeyEventTest)
331     {
332         g_onInputEvent = false;
333         JsAppImpl::GetInstance().DispatchKeyEvent(nullptr);
334         EXPECT_TRUE(g_onInputEvent);
335     }
336 
TEST_F(JsAppImplTest,DispatchPointerEventTest)337     TEST_F(JsAppImplTest, DispatchPointerEventTest)
338     {
339         g_onInputEvent = false;
340         JsAppImpl::GetInstance().DispatchPointerEvent(nullptr);
341         EXPECT_TRUE(g_onInputEvent);
342     }
343 
TEST_F(JsAppImplTest,DispatchAxisEventTest)344     TEST_F(JsAppImplTest, DispatchAxisEventTest)
345     {
346         g_onInputEvent = false;
347         JsAppImpl::GetInstance().DispatchAxisEvent(nullptr);
348         EXPECT_TRUE(g_onInputEvent);
349     }
350 
TEST_F(JsAppImplTest,DispatchInputMethodEventTest)351     TEST_F(JsAppImplTest, DispatchInputMethodEventTest)
352     {
353         g_onInputMethodEvent = false;
354         int code = 12;
355         JsAppImpl::GetInstance().DispatchInputMethodEvent(code);
356         EXPECT_TRUE(g_onInputMethodEvent);
357     }
358 
TEST_F(JsAppImplTest,InitGlfwEnvTest)359     TEST_F(JsAppImplTest, InitGlfwEnvTest)
360     {
361         g_glfwInit = false;
362         g_createGlfwWindow = false;
363         JsAppImpl::GetInstance().InitGlfwEnv();
364         EXPECT_TRUE(g_glfwInit);
365         EXPECT_TRUE(g_createGlfwWindow);
366     }
367 
TEST_F(JsAppImplTest,SetJsAppArgsTest)368     TEST_F(JsAppImplTest, SetJsAppArgsTest)
369     {
370         CommandParser::GetInstance().isComponentMode = true;
371         JsAppImpl::GetInstance().InitCommandInfo();
372         OHOS::Ace::Platform::AceRunArgs args;
373         JsAppImpl::GetInstance().SetJsAppArgs(args);
374         EXPECT_EQ(args.windowTitle, "Ace");
375         EXPECT_TRUE(args.isComponentMode);
376     }
377 
TEST_F(JsAppImplTest,RunJsAppTest)378     TEST_F(JsAppImplTest, RunJsAppTest)
379     {
380         JsAppImpl::GetInstance().isDebug = false;
381         JsAppImpl::GetInstance().debugServerPort = 0;
382         g_setContentInfoCallback = false;
383         g_createSurfaceNode = false;
384         g_setWindow = false;
385         g_initEnv = false;
386         JsAppImpl::GetInstance().RunJsApp();
387         auto ptr = OHOS::Ace::Platform::AcePreviewHelper::GetInstance()->GetCallbackOfHspBufferTracker();
388         EXPECT_TRUE(ptr != nullptr);
389         uint8_t* data = new uint8_t[1];
390         size_t size = 0;
391         std::string msg;
392         bool ret = ptr("aa", &data, &size, msg);
393         EXPECT_FALSE(ret);
394         EXPECT_TRUE(g_setContentInfoCallback);
395         EXPECT_TRUE(g_createSurfaceNode);
396         EXPECT_TRUE(g_setWindow);
397         EXPECT_TRUE(g_initEnv);
398         delete[] data;
399 
400         JsAppImpl::GetInstance().isDebug = true;
401         JsAppImpl::GetInstance().debugServerPort = 8888;
402         g_setHostResolveBufferTracker = false;
403         g_setContentInfoCallback = false;
404         g_createSurfaceNode = false;
405         JsAppImpl::GetInstance().RunJsApp();
406         EXPECT_FALSE(g_setHostResolveBufferTracker);
407         EXPECT_FALSE(g_setContentInfoCallback);
408         EXPECT_FALSE(g_createSurfaceNode);
409     }
410 
411     // JsApp start
TEST_F(JsAppImplTest,ResolutionParamTest)412     TEST_F(JsAppImplTest, ResolutionParamTest)
413     {
414         int width = 100;
415         ResolutionParam param(width, width, width, width);
416         EXPECT_EQ(param.orignalWidth, width);
417         EXPECT_EQ(param.orignalHeight, width);
418         EXPECT_EQ(param.compressionWidth, width);
419         EXPECT_EQ(param.compressionHeight, width);
420     }
421 
TEST_F(JsAppImplTest,StopTest)422     TEST_F(JsAppImplTest, StopTest)
423     {
424         JsAppImpl::GetInstance().isStop = false;
425         JsAppImpl::GetInstance().isFinished = false;
426         std::thread thread1([]() {
427             JsAppImpl::GetInstance().Stop();
428         });
429         thread1.detach();
430         std::this_thread::sleep_for(std::chrono::milliseconds(10));
431         JsAppImpl::GetInstance().isFinished = true;
432         std::this_thread::sleep_for(std::chrono::milliseconds(20));
433         EXPECT_TRUE(JsAppImpl::GetInstance().isStop);
434     }
435 
TEST_F(JsAppImplTest,SetJsAppPathTest)436     TEST_F(JsAppImplTest, SetJsAppPathTest)
437     {
438         JsAppImpl::GetInstance().SetJsAppPath("pages/Index");
439         EXPECT_EQ(JsAppImpl::GetInstance().jsAppPath, "pages/Index");
440     }
441 
TEST_F(JsAppImplTest,SetUrlPathTest)442     TEST_F(JsAppImplTest, SetUrlPathTest)
443     {
444         JsAppImpl::GetInstance().SetUrlPath("pages/Index");
445         EXPECT_EQ(JsAppImpl::GetInstance().urlPath, "pages/Index");
446     }
447 
TEST_F(JsAppImplTest,SetPipeNameTest)448     TEST_F(JsAppImplTest, SetPipeNameTest)
449     {
450         JsAppImpl::GetInstance().SetPipeName("phone_1");
451         EXPECT_EQ(JsAppImpl::GetInstance().pipeName, "phone_1");
452     }
453 
TEST_F(JsAppImplTest,SetPipePortTest)454     TEST_F(JsAppImplTest, SetPipePortTest)
455     {
456         JsAppImpl::GetInstance().SetPipePort("5000");
457         EXPECT_EQ(JsAppImpl::GetInstance().pipePort, "5000");
458     }
459 
TEST_F(JsAppImplTest,SetBundleNameTest)460     TEST_F(JsAppImplTest, SetBundleNameTest)
461     {
462         JsAppImpl::GetInstance().SetBundleName("aaa");
463         EXPECT_EQ(JsAppImpl::GetInstance().bundleName, "aaa");
464         EXPECT_EQ(FileSystem::bundleName, "aaa");
465         size_t pos = FileSystem::fileSystemPath.find("aaa");
466         EXPECT_TRUE(pos != std::string::npos);
467     }
468 
TEST_F(JsAppImplTest,SetRunningTest)469     TEST_F(JsAppImplTest, SetRunningTest)
470     {
471         JsAppImpl::GetInstance().SetRunning(true);
472         EXPECT_TRUE(JsAppImpl::GetInstance().isRunning);
473         JsAppImpl::GetInstance().SetRunning(false);
474         EXPECT_FALSE(JsAppImpl::GetInstance().isRunning);
475     }
476 
TEST_F(JsAppImplTest,GetRunningTest)477     TEST_F(JsAppImplTest, GetRunningTest)
478     {
479         JsAppImpl::GetInstance().isRunning = true;
480         EXPECT_TRUE(JsAppImpl::GetInstance().GetRunning());
481         JsAppImpl::GetInstance().isRunning = false;
482         EXPECT_FALSE(JsAppImpl::GetInstance().GetRunning());
483     }
484 
TEST_F(JsAppImplTest,SetIsDebugTest)485     TEST_F(JsAppImplTest, SetIsDebugTest)
486     {
487         JsAppImpl::GetInstance().SetIsDebug(true);
488         EXPECT_TRUE(JsAppImpl::GetInstance().isDebug);
489         JsAppImpl::GetInstance().SetIsDebug(false);
490         EXPECT_FALSE(JsAppImpl::GetInstance().isDebug);
491     }
492 
TEST_F(JsAppImplTest,SetDebugServerPortTest)493     TEST_F(JsAppImplTest, SetDebugServerPortTest)
494     {
495         int port = 5000;
496         JsAppImpl::GetInstance().SetDebugServerPort(port);
497         EXPECT_EQ(JsAppImpl::GetInstance().debugServerPort, port);
498     }
499 
TEST_F(JsAppImplTest,SetJSHeapSizeTest)500     TEST_F(JsAppImplTest, SetJSHeapSizeTest)
501     {
502         int size = 5000;
503         JsAppImpl::GetInstance().SetJSHeapSize(size);
504         EXPECT_EQ(JsAppImpl::GetInstance().jsHeapSize, size);
505     }
506 
TEST_F(JsAppImplTest,IsLiteDeviceTest)507     TEST_F(JsAppImplTest, IsLiteDeviceTest)
508     {
509         EXPECT_TRUE(JsApp::IsLiteDevice("liteWearable"));
510         EXPECT_FALSE(JsApp::IsLiteDevice("phone"));
511     }
512     // JsApp end
513 
TEST_F(JsAppImplTest,SetPkgContextInfoTest)514     TEST_F(JsAppImplTest, SetPkgContextInfoTest)
515     {
516         CommandParser::GetInstance().appResourcePath = testDir;
517         CommandParser::GetInstance().loaderJsonPath = testDir + "/loader.json";
518         JsAppImpl::GetInstance().InitCommandInfo();
519         const std::string moduleJsonPath = testDir + FileSystem::GetSeparator() + "module.json";
520         const std::string pkgContextInfoJsonPath = testDir + FileSystem::GetSeparator() + "pkgContextInfo.json";
521         // 直接调用
522         JsAppImpl::GetInstance().SetPkgContextInfo();
523         EXPECT_TRUE(JsAppImpl::GetInstance().aceRunArgs.packageNameList.empty());
524         // 创建module.json,json不写全1
525         std::string moduleJsonContent = R"({"aaa":"bbb"})";
526         WriteFile(moduleJsonPath, moduleJsonContent);
527         JsAppImpl::GetInstance().SetPkgContextInfo();
528         EXPECT_TRUE(JsAppImpl::GetInstance().aceRunArgs.packageNameList.empty());
529         // 创建module.json,json不写全2
530         moduleJsonContent = R"({"module":{"name":333}})";
531         WriteFile(moduleJsonPath, moduleJsonContent);
532         JsAppImpl::GetInstance().SetPkgContextInfo();
533         EXPECT_TRUE(JsAppImpl::GetInstance().aceRunArgs.packageNameList.empty());
534         // 创建module.json,json写全
535         moduleJsonContent = R"({"module":{"name":"entry","packageName":"entry"}})";
536         WriteFile(moduleJsonPath, moduleJsonContent);
537         JsAppImpl::GetInstance().SetPkgContextInfo();
538         EXPECT_FALSE(JsAppImpl::GetInstance().aceRunArgs.packageNameList.empty());
539         EXPECT_TRUE(JsAppImpl::GetInstance().aceRunArgs.pkgContextInfoJsonStringMap.empty());
540         // 创建pkgContextInfo.json
541         std::string pkgContextInfoJsonContent = R"({"entry":{"packageName":"entry"}})";
542         WriteFile(pkgContextInfoJsonPath, pkgContextInfoJsonContent);
543         JsAppImpl::GetInstance().SetPkgContextInfo();
544         EXPECT_FALSE(JsAppImpl::GetInstance().aceRunArgs.packageNameList.empty());
545         EXPECT_FALSE(JsAppImpl::GetInstance().aceRunArgs.pkgContextInfoJsonStringMap.empty());
546         if (std::remove(moduleJsonPath.c_str()) != 0) {
547             printf("Error deleting module.json file!\n");
548         }
549         if (std::remove(pkgContextInfoJsonPath.c_str()) != 0) {
550             printf("Error deleting pkgContextInfo.json file!\n");
551         }
552     }
553 
TEST_F(JsAppImplTest,SetAvoidAreaTest)554     TEST_F(JsAppImplTest, SetAvoidAreaTest)
555     {
556         AvoidAreas areas;
557         JsAppImpl::GetInstance().SetAvoidArea(areas);
558         bool ret = JsAppImpl::GetInstance().avoidInitialAreas == areas;
559         EXPECT_TRUE(ret);
560     }
561 
TEST_F(JsAppImplTest,UpdateAvoidArea2IdeTest)562     TEST_F(JsAppImplTest, UpdateAvoidArea2IdeTest)
563     {
564         const OHOS::Rosen::Rect value = {0, 0, 0, 0};
565         g_output = false;
566         JsAppImpl::GetInstance().UpdateAvoidArea2Ide("topRect", value);
567         EXPECT_TRUE(g_output);
568     }
569 
TEST_F(JsAppImplTest,GetWindowTest)570     TEST_F(JsAppImplTest, GetWindowTest)
571     {
572         JsAppImpl::GetInstance().isDebug = true;
573         OHOS::Rosen::Window* win1 = JsAppImpl::GetInstance().GetWindow();
574         EXPECT_TRUE(win1 == nullptr);
575 
576         JsAppImpl::GetInstance().isDebug = false;
577         JsAppImpl::GetInstance().ability =
578             OHOS::Ace::Platform::AceAbility::CreateInstance(JsAppImpl::GetInstance().aceRunArgs);
579         OHOS::Rosen::WMError errCode;
580         OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
581         auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
582         JsAppImpl::GetInstance().ability->SetWindow(window);
583         OHOS::Rosen::Window* win2 = JsAppImpl::GetInstance().GetWindow();
584         EXPECT_TRUE(window == win2);
585     }
586 
TEST_F(JsAppImplTest,InitAvoidAreasTest)587     TEST_F(JsAppImplTest, InitAvoidAreasTest)
588     {
589         OHOS::Rosen::WMError errCode;
590         OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
591         auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
592         g_getSystemBarPropertyByType = false;
593         JsAppImpl::GetInstance().InitAvoidAreas(window);
594         EXPECT_TRUE(g_getSystemBarPropertyByType);
595     }
596 
TEST_F(JsAppImplTest,AdaptDeviceTypeTest)597     TEST_F(JsAppImplTest, AdaptDeviceTypeTest)
598     {
599         int width = 300;
600         int height = 300;
601         std::string type = "wearable";
602         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
603         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::WATCH);
604         type = "tv";
605         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
606         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::TV);
607         type = "phone";
608         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
609         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::PHONE);
610         type = "default";
611         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
612         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::PHONE);
613         type = "2in1";
614         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
615         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::TWO_IN_ONE);
616         type = "tablet";
617         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
618         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::TABLET);
619         type = "car";
620         JsAppImpl::GetInstance().AdaptDeviceType(JsAppImpl::GetInstance().aceRunArgs, type, width, height);
621         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.deviceType, OHOS::Ace::DeviceType::CAR);
622     }
623 
TEST_F(JsAppImplTest,SetDeviceScreenDensityTest)624     TEST_F(JsAppImplTest, SetDeviceScreenDensityTest)
625     {
626         const int32_t screenDensity = 480;
627         std::string type = "wearable";
628         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
629         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
630         type = "tv";
631         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
632         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
633         type = "phone";
634         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
635         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
636         type = "default";
637         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
638         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
639         type = "2in1";
640         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
641         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
642         type = "tablet";
643         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
644         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
645         type = "car";
646         JsAppImpl::GetInstance().SetDeviceScreenDensity(screenDensity, type);
647         EXPECT_EQ(JsAppImpl::GetInstance().watchScreenDensity, screenDensity);
648     }
649 
TEST_F(JsAppImplTest,ParseSystemParamsTest)650     TEST_F(JsAppImplTest, ParseSystemParamsTest)
651     {
652         Json2::Value paramObj = JsonReader::CreateNull();
653         JsAppImpl::GetInstance().ParseSystemParams(JsAppImpl::GetInstance().aceRunArgs, paramObj);
654         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.colorMode, OHOS::Ace::ColorMode::DARK);
655 
656         Json2::Value paramObj2 = JsonReader::CreateObject();
657         int width = 666;
658         int height = 333;
659         int density = 480;
660         paramObj2.Add("width", width);
661         paramObj2.Add("height", height);
662         paramObj2.Add("colorMode", "light");
663         paramObj2.Add("orientation", "");
664         paramObj2.Add("deviceType", "phone");
665         paramObj2.Add("dpi", density);
666         paramObj2.Add("locale", "zh_Hans_CN");
667         JsAppImpl::GetInstance().ParseSystemParams(JsAppImpl::GetInstance().aceRunArgs, paramObj2);
668         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceWidth, width);
669         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceHeight, height);
670         EXPECT_EQ(JsAppImpl::GetInstance().aceRunArgs.deviceConfig.colorMode, OHOS::Ace::ColorMode::LIGHT);
671     }
672 
TEST_F(JsAppImplTest,GetDeviceTypeNameTest)673     TEST_F(JsAppImplTest, GetDeviceTypeNameTest)
674     {
675         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::WATCH), "watch");
676         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::TV), "tv");
677         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::PHONE), "phone");
678         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::TABLET), "tablet");
679         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::CAR), "car");
680         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::UNKNOWN), "");
681         EXPECT_EQ(JsAppImpl::GetInstance().GetDeviceTypeName(OHOS::Ace::DeviceType::WEARABLE), "");
682     }
683 
TEST_F(JsAppImplTest,CalculateAvoidAreaByTypeTest)684     TEST_F(JsAppImplTest, CalculateAvoidAreaByTypeTest)
685     {
686         OHOS::Rosen::SystemBarProperty property;
687         property.enable_ = true;
688         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
689             OHOS::Rosen::WindowType::WINDOW_TYPE_STATUS_BAR, property);
690         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
691             OHOS::Rosen::WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR, property);
692         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
693             OHOS::Rosen::WindowType::APP_MAIN_WINDOW_BASE, property);
694         property.enable_ = false;
695         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
696             OHOS::Rosen::WindowType::WINDOW_TYPE_STATUS_BAR, property);
697         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
698             OHOS::Rosen::WindowType::WINDOW_TYPE_NAVIGATION_INDICATOR, property);
699         JsAppImpl::GetInstance().CalculateAvoidAreaByType(
700             OHOS::Rosen::WindowType::APP_MAIN_WINDOW_BASE, property);
701     }
702 
TEST_F(JsAppImplTest,InitJsAppTest)703     TEST_F(JsAppImplTest, InitJsAppTest)
704     {
705         JsAppImpl::GetInstance().isStop = false;
706         std::thread thread1([]() {
707             CommandParser::GetInstance().argsMap.clear();
708             CommandParser::GetInstance().argsMap["-j"] = { "" };
709             CommandParser::GetInstance().argsMap["-s"] = { "" };
710             CommandParser::GetInstance().argsMap["-url"] = { "" };
711             CommandParser::GetInstance().argsMap["-lws"] = { "" };
712             CommandParser::GetInstance().argsMap["-cm"] = { "" };
713             CommandParser::GetInstance().argsMap["-av"] = { "" };
714             CommandParser::GetInstance().argsMap["-sd"] = { "" };
715             CommandParser::GetInstance().argsMap["-cc"] = { "" };
716             CommandParser::GetInstance().argsMap["-d"] = { "" };
717             CommandParser::GetInstance().argsMap["-p"] = { "" };
718             JsAppImpl::GetInstance().InitJsApp();
719             CommandParser::GetInstance().argsMap.clear();
720         });
721         thread1.detach();
722         std::this_thread::sleep_for(std::chrono::milliseconds(10));
723         JsAppImpl::GetInstance().isStop = true;
724         std::this_thread::sleep_for(std::chrono::milliseconds(10));
725         EXPECT_TRUE(JsAppImpl::GetInstance().isFinished);
726     }
727 }