• 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_game_pad_device.h"
21 #include "proto.h"
22 
23 
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 using namespace testing::ext;
28 using namespace OHOS::MMI;
29 } // namespace
30 class ProcessingGamePadDeviceTest : 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_TransformGamePadJsonDataToInputData
38  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
39  * @tc.type: FUNC
40  * @tc.require:
41  */
42 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputData, TestSize.Level1)
43 {
44     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputData.json";
45     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataNotFindEvents
70  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindEvents, TestSize.Level1)
75 {
76     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindEvents.json";
77     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataEventsIsEmpty
101  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataEventsIsEmpty, TestSize.Level1)
106 {
107     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataEventsIsEmpty.json";
108     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress
132  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress, TestSize.Level1)
137 {
138     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress.json";
139     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataNotFindEventInRocker
163  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
164  * @tc.type: FUNC
165  * @tc.require:
166  */
167 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindEventInRocker, TestSize.Level1)
168 {
169     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindEventInRocker.json";
170     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker
194  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker, TestSize.Level1)
199 {
200     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker.json";
201     std::string startDeviceCmd = "vuinput start gamepad & ";
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_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
225  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
226  * @tc.type: FUNC
227  * @tc.require:
228  */
229 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey,
230          TestSize.Level1)
231 {
232     const std::string path =
233      "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey.json";
234     std::string startDeviceCmd = "vuinput start gamepad & ";
235     std::string closeDeviceCmd = "vuinput close all";
236     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
237     if (!startDevice) {
238         ASSERT_TRUE(false) << "Start device failed";
239     }
240     pclose(startDevice);
241     std::string jsonBuf = ReadJsonFile(path);
242     if (jsonBuf.empty()) {
243         ASSERT_TRUE(false) << "Read file failed" << path;
244     }
245     ManageInjectDevice manageInjectDevice;
246     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
247     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
248     if (!closeDevice) {
249         ASSERT_TRUE(false) << "Close device failed";
250     }
251     pclose(closeDevice);
252     std::this_thread::sleep_for(std::chrono::seconds(1));
253     EXPECT_EQ(ret, RET_ERR);
254 }
255 } // namespace MMI
256 } // namespace OHOS