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 } // namespace
28 
29 class ProcessingJoyStickDeviceTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
CheckTransformJson(const std::string path)33     static void CheckTransformJson(const std::string path)
34     {
35         std::string startDeviceCmd = "vuinput start joystick & ";
36         std::string closeDeviceCmd = "vuinput close all";
37         FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
38         if (!startDevice) {
39             ASSERT_TRUE(false) << "Start device failed";
40         }
41         pclose(startDevice);
42         std::this_thread::sleep_for(std::chrono::seconds(1));
43         std::string jsonBuf = ReadJsonFile(path);
44         if (jsonBuf.empty()) {
45             ASSERT_TRUE(false) << "Read file failed" << path;
46         }
47         ManageInjectDevice manageInjectDevice;
48         auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
49         FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
50         if (!closeDevice) {
51             ASSERT_TRUE(false) << "Close device failed";
52         }
53         pclose(closeDevice);
54         std::this_thread::sleep_for(std::chrono::seconds(1));
55         EXPECT_EQ(ret, RET_OK);
56     }
CheckJoyStickJson(const std::string path)57     static void CheckJoyStickJson(const std::string path)
58     {
59         std::string startDeviceCmd = "vuinput start joystick & ";
60         std::string closeDeviceCmd = "vuinput close all";
61         FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
62         if (!startDevice) {
63             ASSERT_TRUE(false) << "Start device failed";
64         }
65         pclose(startDevice);
66         std::string jsonBuf = ReadJsonFile(path);
67         if (jsonBuf.empty()) {
68             ASSERT_TRUE(false) << "Read file failed" << path;
69         }
70         ManageInjectDevice manageInjectDevice;
71         auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
72         FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
73         if (!closeDevice) {
74             ASSERT_TRUE(false) << "Close device failed";
75         }
76         pclose(closeDevice);
77         std::this_thread::sleep_for(std::chrono::seconds(1));
78         EXPECT_EQ(ret, RET_ERR);
79     }
80 };
81 
82 /**
83  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
84  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJsonDataToInputData, TestSize.Level1)
89 {
90     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputData.json";
91     CheckTransformJson(path);
92 }
93 
94 /**
95  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
96  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
97  * @tc.type: FUNC
98  * @tc.require:
99  */
100 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindEvents, TestSize.Level1)
101 {
102     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindEvents.json";
103     CheckJoyStickJson(path);
104 }
105 
106 /**
107  * @tc.name:Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty
108  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty, TestSize.Level1)
113 {
114     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataEventsIsEmpty.json";
115     CheckJoyStickJson(path);
116 }
117 
118 /**
119  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue
120  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
121  * @tc.type: FUNC
122  * @tc.require:
123  */
124 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue, TestSize.Level1)
125 {
126     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindKeyValue.json";
127     CheckJoyStickJson(path);
128 }
129 
130 /**
131  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro
132  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro, TestSize.Level1)
137 {
138     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindKeyValueInThro.json";
139     CheckJoyStickJson(path);
140 }
141 
142 /**
143  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker
144  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
145  * @tc.type: FUNC
146  * @tc.require:
147  */
148 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker, TestSize.Level1)
149 {
150     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindEventInRocker.json";
151     CheckJoyStickJson(path);
152 }
153 
154 /**
155  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker
156  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
157  * @tc.type: FUNC
158  * @tc.require:
159  */
160 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker,
161          TestSize.Level1)
162 {
163     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInRocker.json";
164     CheckJoyStickJson(path);
165 }
166 
167 /**
168  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey
169  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
170  * @tc.type: FUNC
171  * @tc.require:
172  */
173 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey,
174          TestSize.Level1)
175 {
176     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindDirectionInDirectionKey.json";
177     CheckJoyStickJson(path);
178 }
179 
180 /**
181  * @tc.name:Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType
182  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
183  * @tc.type: FUNC
184  * @tc.require:
185  */
186 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType, TestSize.Level1)
187 {
188     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataNotFindAnyEventType.json";
189     CheckTransformJson(path);
190 }
191 
192 /**
193  * @tc.name:Test_TransformJoyStickJsonDataToInputDataHasBlockTime
194  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
195  * @tc.type: FUNC
196  * @tc.require:
197  */
198 HWTEST_F(ProcessingJoyStickDeviceTest, Test_TransformJoyStickJsonDataToInputDataHasBlockTime, TestSize.Level1)
199 {
200     const std::string path = "/data/json/Test_TransformJoyStickJsonDataToInputDataHasBlockTime.json";
201     CheckTransformJson(path);
202 }
203 } // namespace MMI
204 } // namespace OHOS