• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
18 #include "manage_inject_device.h"
19 #include "msg_head.h"
20 #include "processing_joystick_device.h"
21 #include "proto.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 using namespace OHOS::MMI;
28 } // namespace
29 
30 class ProcessingJoyStickDeviceTest : public testing::Test {
31 public:
SetUpTestCase(void)32     static void SetUpTestCase(void) {}
TearDownTestCase(void)33     static void TearDownTestCase(void) {}
34 };
35 
36 /**
37  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
38  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
39  * @tc.type: FUNC
40  * @tc.require:
41  */
42 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJsonDataToInputData, TestSize.Level1)
43 {
44     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputData.json";
45     std::string startDeviceCmd = "vuinput start joystick & ";
46     std::string closeDeviceCmd = "vuinput close all";
47     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
48     if (!startDevice) {
49         ASSERT_TRUE(false) << "Start device failed";
50     }
51     pclose(startDevice);
52     std::this_thread::sleep_for(std::chrono::seconds(1));
53     std::string jsonBuf = ReadJsonFile(path);
54     if (jsonBuf.empty()) {
55         ASSERT_TRUE(false) << "Read file failed" << path;
56     }
57     ManageInjectDevice manageInjectDevice;
58     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
59     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
60     if (!closeDevice) {
61         ASSERT_TRUE(false) << "Close device failed";
62     }
63     pclose(closeDevice);
64     std::this_thread::sleep_for(std::chrono::seconds(1));
65     EXPECT_EQ(ret, RET_OK);
66 }
67 
68 /**
69  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
70  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindEvents, TestSize.Level1)
75 {
76     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindEvents.json";
77     std::string startDeviceCmd = "vuinput start joystick & ";
78     std::string closeDeviceCmd = "vuinput close all";
79     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
80     if (!startDevice) {
81         ASSERT_TRUE(false) << "Start device failed";
82     }
83     pclose(startDevice);
84     std::string jsonBuf = ReadJsonFile(path);
85     if (jsonBuf.empty()) {
86         ASSERT_TRUE(false) << "Read file failed" << path;
87     }
88     ManageInjectDevice manageInjectDevice;
89     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
90     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
91     if (!closeDevice) {
92         ASSERT_TRUE(false) << "Close device failed";
93     }
94     pclose(closeDevice);
95     std::this_thread::sleep_for(std::chrono::seconds(1));
96     EXPECT_EQ(ret, RET_ERR);
97 }
98 
99 /**
100  * @tc.name:Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty
101  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty, TestSize.Level1)
106 {
107     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty.json";
108     std::string startDeviceCmd = "vuinput start joystick & ";
109     std::string closeDeviceCmd = "vuinput close all";
110     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
111     if (!startDevice) {
112         ASSERT_TRUE(false) << "Start device failed";
113     }
114     pclose(startDevice);
115     std::string jsonBuf = ReadJsonFile(path);
116     if (jsonBuf.empty()) {
117         ASSERT_TRUE(false) << "Read file failed" << path;
118     }
119     ManageInjectDevice manageInjectDevice;
120     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
121     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
122     if (!closeDevice) {
123         ASSERT_TRUE(false) << "Close device failed";
124     }
125     pclose(closeDevice);
126     std::this_thread::sleep_for(std::chrono::seconds(1));
127     EXPECT_EQ(ret, RET_ERR);
128 }
129 
130 /**
131  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue
132  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue, TestSize.Level1)
137 {
138     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue.json";
139     std::string startDeviceCmd = "vuinput start joystick & ";
140     std::string closeDeviceCmd = "vuinput close all";
141     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
142     if (!startDevice) {
143         ASSERT_TRUE(false) << "Start device failed";
144     }
145     pclose(startDevice);
146     std::string jsonBuf = ReadJsonFile(path);
147     if (jsonBuf.empty()) {
148         ASSERT_TRUE(false) << "Read file failed" << path;
149     }
150     ManageInjectDevice manageInjectDevice;
151     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
152     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
153     if (!closeDevice) {
154         ASSERT_TRUE(false) << "Close device failed";
155     }
156     pclose(closeDevice);
157     std::this_thread::sleep_for(std::chrono::seconds(1));
158     EXPECT_EQ(ret, RET_ERR);
159 }
160 
161 /**
162  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro
163  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
164  * @tc.type: FUNC
165  * @tc.require:
166  */
167 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro, TestSize.Level1)
168 {
169     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro.json";
170     std::string startDeviceCmd = "vuinput start joystick & ";
171     std::string closeDeviceCmd = "vuinput close all";
172     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
173     if (!startDevice) {
174         ASSERT_TRUE(false) << "Start device failed";
175     }
176     pclose(startDevice);
177     std::string jsonBuf = ReadJsonFile(path);
178     if (jsonBuf.empty()) {
179         ASSERT_TRUE(false) << "Read file failed" << path;
180     }
181     ManageInjectDevice manageInjectDevice;
182     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
183     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
184     if (!closeDevice) {
185         ASSERT_TRUE(false) << "Close device failed";
186     }
187     pclose(closeDevice);
188     std::this_thread::sleep_for(std::chrono::seconds(1));
189     EXPECT_EQ(ret, RET_ERR);
190 }
191 
192 /**
193  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker
194  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker, TestSize.Level1)
199 {
200     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker.json";
201     std::string startDeviceCmd = "vuinput start joystick & ";
202     std::string closeDeviceCmd = "vuinput close all";
203     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
204     if (!startDevice) {
205         ASSERT_TRUE(false) << "Start device failed";
206     }
207     pclose(startDevice);
208     std::string jsonBuf = ReadJsonFile(path);
209     if (jsonBuf.empty()) {
210         ASSERT_TRUE(false) << "Read file failed" << path;
211     }
212     ManageInjectDevice manageInjectDevice;
213     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
214     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
215     if (!closeDevice) {
216         ASSERT_TRUE(false) << "Close device failed";
217     }
218     pclose(closeDevice);
219     std::this_thread::sleep_for(std::chrono::seconds(1));
220     EXPECT_EQ(ret, RET_ERR);
221 }
222 
223 /**
224  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker
225  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
226  * @tc.type: FUNC
227  * @tc.require:
228  */
229 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker,
230          TestSize.Level1)
231 {
232     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker.json";
233     std::string startDeviceCmd = "vuinput start joystick & ";
234     std::string closeDeviceCmd = "vuinput close all";
235     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
236     if (!startDevice) {
237         ASSERT_TRUE(false) << "Start device failed";
238     }
239     pclose(startDevice);
240     std::string jsonBuf = ReadJsonFile(path);
241     if (jsonBuf.empty()) {
242         ASSERT_TRUE(false) << "Read file failed" << path;
243     }
244     ManageInjectDevice manageInjectDevice;
245     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
246     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
247     if (!closeDevice) {
248         ASSERT_TRUE(false) << "Close device failed";
249     }
250     pclose(closeDevice);
251     std::this_thread::sleep_for(std::chrono::seconds(1));
252     EXPECT_EQ(ret, RET_ERR);
253 }
254 
255 /**
256  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey
257  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
258  * @tc.type: FUNC
259  * @tc.require:
260  */
261 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey,
262          TestSize.Level1)
263 {
264     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey.json";
265     std::string startDeviceCmd = "vuinput start joystick & ";
266     std::string closeDeviceCmd = "vuinput close all";
267     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
268     if (!startDevice) {
269         ASSERT_TRUE(false) << "Start device failed";
270     }
271     pclose(startDevice);
272     std::string jsonBuf = ReadJsonFile(path);
273     if (jsonBuf.empty()) {
274         ASSERT_TRUE(false) << "Read file failed" << path;
275     }
276     ManageInjectDevice manageInjectDevice;
277     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
278     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
279     if (!closeDevice) {
280         ASSERT_TRUE(false) << "Close device failed";
281     }
282     pclose(closeDevice);
283     std::this_thread::sleep_for(std::chrono::seconds(1));
284     EXPECT_EQ(ret, RET_ERR);
285 }
286 
287 /**
288  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType
289  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
290  * @tc.type: FUNC
291  * @tc.require:
292  */
293 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType, TestSize.Level1)
294 {
295     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType.json";
296     std::string startDeviceCmd = "vuinput start joystick & ";
297     std::string closeDeviceCmd = "vuinput close all";
298     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
299     if (!startDevice) {
300         ASSERT_TRUE(false) << "Start device failed";
301     }
302     pclose(startDevice);
303     std::this_thread::sleep_for(std::chrono::seconds(1));
304     std::string jsonBuf = ReadJsonFile(path);
305     if (jsonBuf.empty()) {
306         ASSERT_TRUE(false) << "Read file failed" << path;
307     }
308     ManageInjectDevice manageInjectDevice;
309     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
310     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
311     if (!closeDevice) {
312         ASSERT_TRUE(false) << "Close device failed";
313     }
314     pclose(closeDevice);
315     std::this_thread::sleep_for(std::chrono::seconds(1));
316     EXPECT_EQ(ret, RET_OK);
317 }
318 
319 /**
320  * @tc.name:Test_TransformJoyStickJsonDataToInputDataHasBlockTime
321  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
322  * @tc.type: FUNC
323  * @tc.require:
324  */
325 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataHasBlockTime, TestSize.Level1)
326 {
327     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataHasBlockTime.json";
328     std::string startDeviceCmd = "vuinput start joystick & ";
329     std::string closeDeviceCmd = "vuinput close all";
330     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
331     if (!startDevice) {
332         ASSERT_TRUE(false) << "Start device failed";
333     }
334     pclose(startDevice);
335     std::this_thread::sleep_for(std::chrono::seconds(1));
336     std::string jsonBuf = ReadJsonFile(path);
337     if (jsonBuf.empty()) {
338         ASSERT_TRUE(false) << "Read file failed" << path;
339     }
340     ManageInjectDevice manageInjectDevice;
341     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
342     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
343     if (!closeDevice) {
344         ASSERT_TRUE(false) << "Close device failed";
345     }
346     pclose(closeDevice);
347     std::this_thread::sleep_for(std::chrono::seconds(1));
348     EXPECT_EQ(ret, RET_OK);
349 }
350 } // namespace MMI
351 } // namespace OHOS