• 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 
16 #include <string>
17 #include <thread>
18 #include <fstream>
19 #include <cstdio>
20 #include <unistd.h>
21 #include "gtest/gtest.h"
22 #define private public
23 #include "CommandLineInterface.h"
24 #include "CommandLineFactory.h"
25 #include "CommandParser.h"
26 #include "SharedData.h"
27 #include "MockGlobalResult.h"
28 #include "VirtualScreen.h"
29 using namespace std;
30 
31 namespace {
32     std::string g_configPath = R"(
33         {
34         "setting": {
35         "1.0.0": {
36         "KeepScreenOnState": {
37             "args": {
38             "KeepScreenOnState": true
39             }
40         },
41         "BrightnessMode": {
42             "args": {
43             "BrightnessMode": 0
44             }
45         },
46         "Brightness": {
47             "args": {
48             "Brightness": 170
49             }
50         },
51         "WearingState": {
52             "args": {
53             "WearingState": true
54             }
55         },
56         "Barometer": {
57             "args": {
58             "Barometer": 101325
59             }
60         },
61         "HeartRate": {
62             "args": {
63             "HeartRate": 100
64             }
65         },
66         "StepCount": {
67             "args": {
68             "StepCount": 0
69             }
70         },
71         "Location": {
72             "args": {
73             "latitude": 39.914417,
74             "longitude": 116.39647
75             }
76         },
77         "ChargeMode": {
78             "args": {
79             "ChargeMode": 0
80             }
81         },
82         "Power": {
83             "args": {
84             "Power": 0.1
85             }
86         },
87         "Language": {
88             "args": {
89             "Language": "en-US"
90             }
91         }
92         },
93         "1.0.1": {
94         "Language": {
95             "args": {
96             "Language": "zh-CN"
97             }
98         }
99         }
100         },
101         "frontend": {
102         "1.0.0": {
103         "Resolution": {
104             "args": {
105             "Resolution": "454*454"
106             }
107         },
108         "DeviceType": {
109             "args": {
110             "DeviceType": "liteWearable"
111             }
112         }
113         }
114         }
115         }
116     )";
117 
118     // 测试复制构造函数是否被删除
TEST(CommandLineInterfaceTest,DefaultConstructorDeletedTest)119     TEST(CommandLineInterfaceTest, DefaultConstructorDeletedTest)
120     {
121         EXPECT_TRUE(std::is_copy_constructible<CommandLineInterface>::value == false);
122     }
123 
124     // 测试赋值运算符是否被删除
TEST(CommandLineInterfaceTest,AssignmentOperatorDeletedTest)125     TEST(CommandLineInterfaceTest, AssignmentOperatorDeletedTest)
126     {
127         EXPECT_TRUE(std::is_copy_assignable<CommandLineInterface>::value == false);
128     }
129 
TEST(CommandLineInterfaceTest,GetInstanceTest)130     TEST(CommandLineInterfaceTest, GetInstanceTest)
131     {
132         CommandLineInterface& instance1 = CommandLineInterface::GetInstance();
133         CommandLineInterface& instance2 = CommandLineInterface::GetInstance();
134         EXPECT_EQ(&instance1, &instance2);
135     }
136 
TEST(CommandLineInterfaceTest,InitPipeTest)137     TEST(CommandLineInterfaceTest, InitPipeTest)
138     {
139         CommandLineInterface::GetInstance().InitPipe("phone");
140         EXPECT_TRUE(CommandLineInterface::isPipeConnected);
141     }
142 
TEST(CommandLineInterfaceTest,ProcessCommandTest)143     TEST(CommandLineInterfaceTest, ProcessCommandTest)
144     {
145         // normal
146         g_input = false;
147         CommandLineInterface::GetInstance().ProcessCommand();
148         EXPECT_TRUE(g_input);
149         // socket is null
150         g_input = false;
151         std::unique_ptr<LocalSocket> temp = std::move(CommandLineInterface::GetInstance().socket);
152         CommandLineInterface::GetInstance().socket = nullptr;
153         CommandLineInterface::GetInstance().ProcessCommand();
154         EXPECT_FALSE(g_input);
155         CommandLineInterface::GetInstance().socket = std::move(temp);
156         // isFirstWsSend
157         g_input = false;
158         CommandLineInterface::GetInstance().isPipeConnected = true;
159         VirtualScreen::isWebSocketListening = true;
160         CommandLineInterface::GetInstance().isFirstWsSend = true;
161         CommandLineInterface::GetInstance().ProcessCommand();
162         EXPECT_FALSE(CommandLineInterface::GetInstance().isFirstWsSend);
163         EXPECT_TRUE(g_input);
164     }
165 
TEST(CommandLineInterfaceTest,ProcessCommandMessageTest)166     TEST(CommandLineInterfaceTest, ProcessCommandMessageTest)
167     {
168         // normal
169         g_output = false;
170         std::string msg = R"({"type" : "action", "command" : "MousePress", "version" : "1.0.1"})";
171         CommandLineInterface::GetInstance().ProcessCommandMessage(msg);
172         EXPECT_TRUE(g_output);
173         // json parse failed
174         g_output = false;
175         std::string msg0 = R"({"type" : "aaaaa", "command" : "MousePress", "bbbb" : "1.0.1", "args" : {}})";
176         CommandLineInterface::GetInstance().ProcessCommandMessage("");
177         EXPECT_FALSE(g_output);
178         // json args invalid
179         g_output = false;
180         std::string msg1 = R"({"type" : "aaaaa", "command" : "MousePress", "bbbb" : "1.0.1"})";
181         CommandLineInterface::GetInstance().ProcessCommandMessage(msg1);
182         EXPECT_FALSE(g_output);
183         // cmd type error
184         g_output = false;
185         std::string msg2 = R"({"type" : "aaaaa", "command" : "MousePress", "version" : "1.0.1"})";
186         CommandLineInterface::GetInstance().ProcessCommandMessage(msg2);
187         EXPECT_FALSE(g_output);
188         // static card
189         g_output = false;
190         CommandParser::GetInstance().staticCard = true;
191         CommandLineInterface::GetInstance().ProcessCommandMessage(msg);
192         CommandParser::GetInstance().staticCard = false;
193         EXPECT_FALSE(g_output);
194     }
195 
TEST(CommandLineInterfaceTest,ProcessCommandValidateTest)196     TEST(CommandLineInterfaceTest, ProcessCommandValidateTest)
197     {
198         CommandLineInterface& instance = CommandLineInterface::GetInstance();
199         std::string msg = "{}";
200         Json2::Value jsonData1 = JsonReader::ParseJsonData2(msg);
201         EXPECT_FALSE(instance.ProcessCommandValidate(false, jsonData1, "Failed to parse the JSON"));
202         msg = "[]";
203         Json2::Value jsonData2 = JsonReader::ParseJsonData2(msg);
204         EXPECT_FALSE(instance.ProcessCommandValidate(true, jsonData2, "Command is not a object"));
205         msg = R"({"type" : "action", "command" : "MousePress"})";
206         Json2::Value jsonData3 = JsonReader::ParseJsonData2(msg);
207         EXPECT_FALSE(instance.ProcessCommandValidate(true, jsonData3, "Command error"));
208         msg = R"({"type" : "action", "command" : "MousePress", "version" : "s.0.1"})";
209         Json2::Value jsonData4 = JsonReader::ParseJsonData2(msg);
210         EXPECT_FALSE(instance.ProcessCommandValidate(true, jsonData4, "Invalid command version"));
211         msg = R"({"type" : "action", "command" : "MousePress", "version" : "1.0.1"})";
212         Json2::Value jsonData5 = JsonReader::ParseJsonData2(msg);
213         EXPECT_TRUE(instance.ProcessCommandValidate(true, jsonData5, ""));
214     }
215 
TEST(CommandLineInterfaceTest,GetCommandTypeTest)216     TEST(CommandLineInterfaceTest, GetCommandTypeTest)
217     {
218         CommandLineInterface& instance = CommandLineInterface::GetInstance();
219         EXPECT_EQ(instance.GetCommandType(""), CommandLine::CommandType::INVALID);
220         EXPECT_EQ(instance.GetCommandType("set"), CommandLine::CommandType::SET);
221         EXPECT_EQ(instance.GetCommandType("get"), CommandLine::CommandType::GET);
222         EXPECT_EQ(instance.GetCommandType("action"), CommandLine::CommandType::ACTION);
223     }
224 
TEST(CommandLineInterfaceTest,InitTest)225     TEST(CommandLineInterfaceTest, InitTest)
226     {
227         CommandLineInterface& instance = CommandLineInterface::GetInstance();
228         instance.Init("phone");
229         EXPECT_TRUE(CommandLineFactory::typeMap.size() > 0);
230         EXPECT_TRUE(CommandLineInterface::isPipeConnected);
231     }
232 
TEST(CommandLineInterfaceTest,IsStaticIgnoreCmdTest)233     TEST(CommandLineInterfaceTest, IsStaticIgnoreCmdTest)
234     {
235         CommandLineInterface& instance = CommandLineInterface::GetInstance();
236         std::string msg = "Language";
237         EXPECT_FALSE(instance.IsStaticIgnoreCmd(msg));
238         std::string msg1 = "Language1";
239         EXPECT_TRUE(instance.IsStaticIgnoreCmd(msg1));
240     }
241 
InitSharedData(std::string deviceType)242     void InitSharedData(std::string deviceType)
243     {
244         if (deviceType == "liteWearable" || "smartVersion") {
245             SharedData<bool>(SharedDataType::KEEP_SCREEN_ON, true);
246             SharedData<uint8_t>(SharedDataType::BATTERY_STATUS, (uint8_t)ChargeState::NOCHARGE,
247                                 (uint8_t)ChargeState::NOCHARGE, (uint8_t)ChargeState::CHARGING);
248             // The brightness ranges from 1 to 255. The default value is 255.
249             SharedData<uint8_t>(SharedDataType::BRIGHTNESS_VALUE, 255, 1, 255);
250             SharedData<uint8_t>(SharedDataType::BRIGHTNESS_MODE, (uint8_t)BrightnessMode::MANUAL,
251                                 (uint8_t)BrightnessMode::MANUAL, (uint8_t)BrightnessMode::AUTO);
252             // The value ranges from 0 to 999999. The default value is 0.
253             SharedData<uint32_t>(SharedDataType::SUMSTEP_VALUE, 0, 0, 999999);
254             // The volume ranges from 0.0 to 1.0. The default value is 1.0.
255             SharedData<double>(SharedDataType::VOLUME_VALUE, 1.0, 0.0, 1.0);
256             // Battery level range: 0.0–1.0; default: 1.0
257             SharedData<double>(SharedDataType::BATTERY_LEVEL, 1.0, 0.0, 1.0);
258             // Heart rate range: 0 to 255. The default value is 80.
259             SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, 80, 0, 255);
260             SharedData<string>(SharedDataType::LANGUAGE, "zh-CN");
261             // The value ranges from 180 to 180. The default value is 0.
262             SharedData<double>(SharedDataType::LONGITUDE, 0, -180, 180);
263             // The atmospheric pressure ranges from 0 to 999900. The default value is 101325.
264             SharedData<uint32_t>(SharedDataType::PRESSURE_VALUE, 101325, 0, 999900);
265             SharedData<bool>(SharedDataType::WEARING_STATE, true);
266             // The value ranges from -90 to 90. The default value is 0.
267             SharedData<double>(SharedDataType::LATITUDE, 0, -90, 90);
268         } else {
269             SharedData<string>(SharedDataType::LANGUAGE, "zh_CN");
270             SharedData<string>(SharedDataType::LAN, "zh");
271             SharedData<string>(SharedDataType::REGION, "CN");
272         }
273     }
274 
TEST(CommandLineInterfaceTest,ReadAndApplyConfigTest)275     TEST(CommandLineInterfaceTest, ReadAndApplyConfigTest)
276     {
277         string deviceType = "liteWearable";
278         CommandParser::GetInstance().deviceType = deviceType;
279         CommandLineInterface& instance = CommandLineInterface::GetInstance();
280         // path is empty
281         instance.ReadAndApplyConfig("");
282         EXPECT_FALSE(SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON));
283         // path not empty
284         instance.Init(deviceType);
285         InitSharedData(deviceType);
286         char buffer[FILENAME_MAX];
287         if (getcwd(buffer, FILENAME_MAX) != nullptr) {
288             std::string currDir = std::string(buffer);
289             std::string currFile = currDir + "/config.json";
290             // 创建文件流对象并打开文件
291             std::ofstream file(currFile);
292             // 检查文件是否成功打开
293             if (file.is_open()) {
294                 file << g_configPath;
295                 file.close();
296                 instance.ReadAndApplyConfig(currFile);
297             } else {
298                 printf("Error creating file!\n");\
299                 EXPECT_TRUE(false);
300             }
301         } else {
302             printf("error: getcwd failed\n");
303             EXPECT_TRUE(false);
304         }
305         EXPECT_TRUE(SharedData<bool>::GetData(SharedDataType::KEEP_SCREEN_ON));
306         EXPECT_EQ(SharedData<uint8_t>::GetData(SharedDataType::BRIGHTNESS_VALUE), 170);
307         EXPECT_EQ(SharedData<uint8_t>::GetData(SharedDataType::HEARTBEAT_VALUE), 100);
308     }
309 
TEST(CommandLineInterfaceTest,ApplyConfigCommandsTest_Err)310     TEST(CommandLineInterfaceTest, ApplyConfigCommandsTest_Err)
311     {
312         g_output = false;
313         CommandLineInterface::GetInstance().ApplyConfigCommands("MousePress", nullptr);
314         EXPECT_FALSE(g_output);
315     }
316 
TEST(CommandLineInterfaceTest,CreatCommandToSendDataTest_Err)317     TEST(CommandLineInterfaceTest, CreatCommandToSendDataTest_Err)
318     {
319         g_output = false;
320         Json2::Value val;
321         CommandLineInterface::GetInstance().CreatCommandToSendData("aaaa", val, "set");
322         EXPECT_TRUE(g_output);
323     }
324 
TEST(CommandLineInterfaceTest,SendJsonDataTest)325     TEST(CommandLineInterfaceTest, SendJsonDataTest)
326     {
327         g_output = false;
328         std::string msg = R"({"type":"action","command":"MousePress"})";
329         Json2::Value jsonData = JsonReader::ParseJsonData2(msg);
330         CommandLineInterface::SendJsonData(jsonData);
331         EXPECT_TRUE(g_output);
332     }
333 
TEST(CommandLineInterfaceTest,SendJSHeapMemoryTest)334     TEST(CommandLineInterfaceTest, SendJSHeapMemoryTest)
335     {
336         g_output = false;
337         CommandLineInterface::GetInstance().SendJSHeapMemory(1, 1, 1);
338         EXPECT_TRUE(g_output);
339     }
340 
TEST(CommandLineInterfaceTest,SendWebsocketStartupSignalTest)341     TEST(CommandLineInterfaceTest, SendWebsocketStartupSignalTest)
342     {
343         g_output = false;
344         CommandLineInterface::GetInstance().SendWebsocketStartupSignal();
345         EXPECT_TRUE(g_output);
346     }
347 
TEST(CommandLineInterfaceTest,CreatCommandToSendDataTest)348     TEST(CommandLineInterfaceTest, CreatCommandToSendDataTest)
349     {
350         string deviceType = "phone";
351         CommandParser::GetInstance().deviceType = deviceType;
352         CommandLineInterface::GetInstance().Init(deviceType);
353         g_output = false;
354         EXPECT_FALSE(g_output);
355         Json2::Value val;
356         CommandLineInterface::GetInstance().CreatCommandToSendData("LoadContent", val, "get");
357         EXPECT_TRUE(g_output);
358     }
359 
TEST(CommandLineInterfaceTest,ApplyConfigMembersTest_Err)360     TEST(CommandLineInterfaceTest, ApplyConfigMembersTest_Err)
361     {
362         string deviceType = "liteWearable";
363         CommandParser::GetInstance().deviceType = deviceType;
364         CommandLineInterface::GetInstance().Init(deviceType);
365         std::string jsonStr = R"({ "Language" : { "args" : { "Language" : "zh-CN" }},
366                                     "Language2" : { "args1" : { "Language" : "zh-CN" }},
367                                     "Language3" : "",
368                                     "Language4" : { "args" : ""}})";
369         Json2::Value::Members members= { "Language", "Language2", "Language3", "Language4" };
370         Json2::Value commands = JsonReader::ParseJsonData2(jsonStr);
371         CommandLineInterface::GetInstance().ApplyConfigMembers(commands, members);
372         std::string language = SharedData<string>::GetData(SharedDataType::LANGUAGE);
373         EXPECT_EQ(language, "zh-CN");
374     }
375 
TEST(CommandLineInterfaceTest,ApplyConfigTest_Err)376     TEST(CommandLineInterfaceTest, ApplyConfigTest_Err)
377     {
378         g_output = false;
379         std::string jsonStr1 = R"({ "setting" : { "1.0.1" : "aaa"}})";
380         Json2::Value json1 = JsonReader::ParseJsonData2(jsonStr1);
381         CommandLineInterface::GetInstance().ApplyConfig(json1);
382         EXPECT_FALSE(g_output);
383         g_output = false;
384         std::string jsonStr2 = R"({ "setting" : "aaa"})";
385         Json2::Value json2 = JsonReader::ParseJsonData2(jsonStr2);
386         CommandLineInterface::GetInstance().ApplyConfig(json2);
387         EXPECT_FALSE(g_output);
388     }
389 
TEST(CommandLineInterfaceTest,SendJSHeapMemoryTest_Err)390     TEST(CommandLineInterfaceTest, SendJSHeapMemoryTest_Err)
391     {
392         g_output = false;
393         std::unique_ptr<LocalSocket> temp = std::move(CommandLineInterface::GetInstance().socket);
394         CommandLineInterface::GetInstance().socket = nullptr;
395         CommandLineInterface::GetInstance().SendJSHeapMemory(0, 0, 0);
396         EXPECT_FALSE(g_output);
397         CommandLineInterface::GetInstance().socket = std::move(temp);
398     }
399 }
400