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