• 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 <fstream>
17 #include <filesystem>
18 #include <sys/stat.h>
19 #include "gtest/gtest.h"
20 #define private public
21 #define protected public
22 #include "CommandLineInterface.h"
23 #include "FileSystem.h"
24 #include "JsAppImpl.h"
25 #include "TraceTool.h"
26 #include "CommandParser.h"
27 
28 namespace {
29     class JsAppImplTest : public ::testing::Test {
30     public:
JsAppImplTest()31         JsAppImplTest() {}
~JsAppImplTest()32         ~JsAppImplTest() {}
33     protected:
CopyFileToDirectory(const std::filesystem::path & sourceFile,const std::filesystem::path & destDirectory)34         static void CopyFileToDirectory(const std::filesystem::path& sourceFile,
35             const std::filesystem::path& destDirectory)
36         {
37             // 确保目标目录存在
38             if (!std::filesystem::exists(destDirectory)) {
39                 std::filesystem::create_directories(destDirectory);
40             }
41             // 生成目标文件路径
42             std::filesystem::path destFile = destDirectory / sourceFile.filename();
43             // 复制文件
44             std::filesystem::copy_file(sourceFile, destFile,
45                 std::filesystem::copy_options::overwrite_existing);
46             std::cout << "File copied successfully to " << destFile << std::endl;
47         }
48 
SetUpTestCase()49         static void SetUpTestCase()
50         {
51             CommandLineInterface::GetInstance().Init("pipeName");
52             TraceTool::GetInstance().InitPipe();
53             testConfigPath = FileSystem::GetApplicationPath() + FileSystem::separator + ".." +
54                 FileSystem::separator + "config" + FileSystem::separator;
55             std::string srcBrkPath = FileSystem::GetApplicationPath() + FileSystem::separator +
56                 "foundation/arkui/ui_lite/tools/qt/simulator/font/line_cj.brk";
57             std::string srcFontPath = FileSystem::GetApplicationPath() + FileSystem::separator +
58                 "foundation/arkui/ui_lite/tools/qt/simulator/font/SourceHanSansSC-Regular.otf";
59             CopyFileToDirectory(srcBrkPath, testConfigPath);
60             CopyFileToDirectory(srcFontPath, testConfigPath);
61         }
62 
63         static std::string testConfigPath;
64     };
65     std::string JsAppImplTest::testConfigPath = "";
66 
67     // 测试拷贝构造函数是否被删除
TEST_F(JsAppImplTest,CopyConstructorDeletedTest)68     TEST_F(JsAppImplTest, CopyConstructorDeletedTest)
69     {
70         EXPECT_TRUE(std::is_copy_constructible<JsApp>::value == false);
71     }
72 
73     // 测试赋值运算符是否被删除
TEST_F(JsAppImplTest,AssignmentOperatorDeletedTest)74     TEST_F(JsAppImplTest, AssignmentOperatorDeletedTest)
75     {
76         EXPECT_TRUE(std::is_copy_assignable<JsApp>::value == false);
77     }
78 
TEST_F(JsAppImplTest,StartTest)79     TEST_F(JsAppImplTest, StartTest)
80     {
81         JsAppImpl::GetInstance().isFinished = true;
82         JsAppImpl::GetInstance().Start();
83         std::this_thread::sleep_for(std::chrono::milliseconds(10)); // sleep 10 ms
84         JsAppImpl::GetInstance().isInterrupt = true;
85         EXPECT_FALSE(JsAppImpl::GetInstance().isFinished);
86     }
87 
TEST_F(JsAppImplTest,InterruptTest)88     TEST_F(JsAppImplTest, InterruptTest)
89     {
90         JsAppImpl::GetInstance().jsAbility = std::make_unique<OHOS::ACELite::JSAbility>();
91         JsAppImpl::GetInstance().Interrupt();
92         EXPECT_TRUE(JsAppImpl::GetInstance().isFinished);
93         EXPECT_TRUE(JsAppImpl::GetInstance().isInterrupt);
94     }
95 
TEST_F(JsAppImplTest,InitJsAppTest)96     TEST_F(JsAppImplTest, InitJsAppTest)
97     {
98         JsAppImpl::GetInstance().isFinished = true;
99         CommandParser::GetInstance().argsMap.clear();
100         CommandParser::GetInstance().argsMap["-s"] = { "" };
101         CommandParser::GetInstance().argsMap["-lws"] = { "" };
102         CommandParser::GetInstance().argsMap["-n"] = { "" };
103         CommandParser::GetInstance().argsMap["-hs"] = { "" };
104         CommandParser::GetInstance().argsMap["-d"] = { "" };
105         CommandParser::GetInstance().argsMap["-p"] = { "" };
106         CommandParser::GetInstance().argsMap["-j"] = { "" };
107         CommandParser::GetInstance().argsMap["-url"] = { "" };
108         JsAppImpl::GetInstance().InitJsApp();
109         CommandParser::GetInstance().argsMap.clear();
110         JsAppImpl::GetInstance().isInterrupt = true;
111         EXPECT_FALSE(JsAppImpl::GetInstance().isFinished);
112     }
113 
TEST_F(JsAppImplTest,StartJsAppTest1)114     TEST_F(JsAppImplTest, StartJsAppTest1)
115     {
116         // jsAbility not nullptr
117         JsAppImpl::GetInstance().isFinished = true;
118         JsAppImpl::GetInstance().jsAbility = std::make_unique<OHOS::ACELite::JSAbility>();
119         JsAppImpl::GetInstance().StartJsApp();
120         EXPECT_TRUE(JsAppImpl::GetInstance().isFinished);
121     }
122 
TEST_F(JsAppImplTest,StartJsAppTest2)123     TEST_F(JsAppImplTest, StartJsAppTest2)
124     {
125         JsAppImpl::GetInstance().jsAbility = nullptr;
126         JsAppImpl::GetInstance().isFinished = true;
127         // urlPath empty
128         JsAppImpl::GetInstance().isDebug = true;
129         JsAppImpl::GetInstance().debugServerPort = 9999;
130         JsAppImpl::GetInstance().urlPath = "";
131         JsAppImpl::GetInstance().StartJsApp();
132         EXPECT_TRUE(JsAppImpl::GetInstance().isFinished);
133     }
134 
TEST_F(JsAppImplTest,StartJsAppTest3)135     TEST_F(JsAppImplTest, StartJsAppTest3)
136     {
137         JsAppImpl::GetInstance().jsAbility = nullptr;
138         JsAppImpl::GetInstance().isFinished = true;
139         // urlPath not empty
140         JsAppImpl::GetInstance().isDebug = true;
141         JsAppImpl::GetInstance().debugServerPort = 9999;
142         JsAppImpl::GetInstance().urlPath = "pages/Index";
143         JsAppImpl::GetInstance().StartJsApp();
144         EXPECT_FALSE(JsAppImpl::GetInstance().isFinished);
145     }
146 }