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 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 } // namespace
28 class ProcessingGamePadDeviceTest : public testing::Test {
29 public:
SetUpTestCase(void)30     static void SetUpTestCase(void) {}
TearDownTestCase(void)31     static void TearDownTestCase(void) {}
CheckGamePadJson(const std::string path)32     static void CheckGamePadJson(const std::string path)
33     {
34         std::string startDeviceCmd = "vuinput start gamepad & ";
35         std::string closeDeviceCmd = "vuinput close all";
36         FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
37         if (!startDevice) {
38             ASSERT_TRUE(false) << "Start device failed";
39         }
40         pclose(startDevice);
41         std::string jsonBuf = ReadJsonFile(path);
42         if (jsonBuf.empty()) {
43             ASSERT_TRUE(false) << "Read file failed" << path;
44         }
45         ManageInjectDevice manageInjectDevice;
46         auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
47         FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
48         if (!closeDevice) {
49             ASSERT_TRUE(false) << "Close device failed";
50         }
51         pclose(closeDevice);
52         std::this_thread::sleep_for(std::chrono::seconds(1));
53         EXPECT_EQ(ret, RET_ERR);
54     }
55 };
56 
57 /**
58  * @tc.name:Test_TransformGamePadJsonDataToInputData
59  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
60  * @tc.type: FUNC
61  * @tc.require:
62  */
63 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputData, TestSize.Level1)
64 {
65     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputData.json";
66     std::string startDeviceCmd = "vuinput start gamepad & ";
67     std::string closeDeviceCmd = "vuinput close all";
68     FILE* startDevice = popen(startDeviceCmd.c_str(), "rw");
69     if (!startDevice) {
70         ASSERT_TRUE(false) << "Start device failed";
71     }
72     pclose(startDevice);
73     std::this_thread::sleep_for(std::chrono::seconds(1));
74     std::string jsonBuf = ReadJsonFile(path);
75     if (jsonBuf.empty()) {
76         ASSERT_TRUE(false) << "Read file failed" << path;
77     }
78     ManageInjectDevice manageInjectDevice;
79     auto ret = manageInjectDevice.TransformJsonData(DataInit(jsonBuf, false));
80     FILE* closeDevice = popen(closeDeviceCmd.c_str(), "rw");
81     if (!closeDevice) {
82         ASSERT_TRUE(false) << "Close device failed";
83     }
84     pclose(closeDevice);
85     std::this_thread::sleep_for(std::chrono::seconds(1));
86     EXPECT_EQ(ret, RET_OK);
87 }
88 
89 /**
90  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindEvents
91  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
92  * @tc.type: FUNC
93  * @tc.require:
94  */
95 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindEvents, TestSize.Level1)
96 {
97     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindEvents.json";
98     CheckGamePadJson(path);
99 }
100 
101 /**
102  * @tc.name:Test_TransformGamePadJsonDataToInputDataEventsIsEmpty
103  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
104  * @tc.type: FUNC
105  * @tc.require:
106  */
107 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataEventsIsEmpty, TestSize.Level1)
108 {
109     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataEventsIsEmpty.json";
110     CheckGamePadJson(path);
111 }
112 
113 /**
114  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress
115  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
116  * @tc.type: FUNC
117  * @tc.require:
118  */
119 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress, TestSize.Level1)
120 {
121     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindKeyValueInPress.json";
122     CheckGamePadJson(path);
123 }
124 
125 /**
126  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindEventInRocker
127  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindEventInRocker, TestSize.Level1)
132 {
133     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindEventInRocker.json";
134     CheckGamePadJson(path);
135 }
136 
137 /**
138  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker
139  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
140  * @tc.type: FUNC
141  * @tc.require:
142  */
143 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker, TestSize.Level1)
144 {
145     const std::string path = "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRocker.json";
146     CheckGamePadJson(path);
147 }
148 
149 /**
150  * @tc.name:Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey
151  * @tc.desc:Verify ManageInjectDevice function TransformJsonData
152  * @tc.type: FUNC
153  * @tc.require:
154  */
155 HWTEST_F(ProcessingGamePadDeviceTest, Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey,
156          TestSize.Level1)
157 {
158     const std::string path =
159         "/data/json/Test_TransformGamePadJsonDataToInputDataNotFindDirectionInRockerDirectionKey.json";
160     CheckGamePadJson(path);
161 }
162 } // namespace MMI
163 } // namespace OHOS