• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <fcntl.h>
18 
19 #include "input_device.h"
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 using namespace testing::ext;
25 } // namespace
26 
27 class InputDeviceTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {}
TearDownTestCase(void)30     static void TearDownTestCase(void) {}
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 };
34 
35 /**
36  * @tc.name: InputDeviceTest_DefaultConstructor
37  * @tc.desc: Test default constructor of InputDevice
38  * @tc.type: FUNC
39  * @tc.require:
40  */
41 HWTEST_F(InputDeviceTest, InputDeviceTest_DefaultConstructor, TestSize.Level1)
42 {
43     InputDevice device;
44     EXPECT_EQ(device.GetId(), 0);
45     EXPECT_EQ(device.GetFd(), -1);
46     EXPECT_TRUE(device.GetPath().empty());
47     EXPECT_TRUE(device.GetName().empty());
48     EXPECT_FALSE(device.IsOpen());
49 }
50 
51 /**
52  * @tc.name: InputDeviceTest_ParamConstructor
53  * @tc.desc: Test parameterized constructor of InputDevice
54  * @tc.type: FUNC
55  * @tc.require:
56  */
57 HWTEST_F(InputDeviceTest, InputDeviceTest_ParamConstructor, TestSize.Level1)
58 {
59     InputDevice device("/non/existent/path", 123);
60     EXPECT_EQ(device.GetId(), 123);
61     EXPECT_EQ(device.GetPath(), "/non/existent/path");
62     EXPECT_FALSE(device.IsOpen());
63 }
64 
65 /**
66  * @tc.name: InputDeviceTest_MoveConstructor
67  * @tc.desc: Test move constructor of InputDevice
68  * @tc.type: FUNC
69  * @tc.require:
70  */
71 HWTEST_F(InputDeviceTest, InputDeviceTest_MoveConstructor, TestSize.Level1)
72 {
73     InputDevice device1;
74     device1.SetId(42);
75     device1.SetPath("/test/path");
76     device1.SetName("TestDevice");
77 
78     InputDevice device2(std::move(device1));
79     EXPECT_EQ(device2.GetId(), 42);
80     EXPECT_EQ(device2.GetPath(), "/test/path");
81     EXPECT_EQ(device2.GetName(), "TestDevice");
82 
83     EXPECT_TRUE(device1.GetPath().empty());
84     EXPECT_TRUE(device1.GetName().empty());
85 }
86 
87 /**
88  * @tc.name: InputDeviceTest_MoveAssignment
89  * @tc.desc: Test move assignment operator of InputDevice
90  * @tc.type: FUNC
91  * @tc.require:
92  */
93 HWTEST_F(InputDeviceTest, InputDeviceTest_MoveAssignment, TestSize.Level1)
94 {
95     InputDevice device1;
96     device1.SetId(42);
97     device1.SetPath("/test/path");
98     device1.SetName("TestDevice");
99 
100     InputDevice device2;
101     device2 = std::move(device1);
102     EXPECT_EQ(device2.GetId(), 42);
103     EXPECT_EQ(device2.GetPath(), "/test/path");
104     EXPECT_EQ(device2.GetName(), "TestDevice");
105 
106     EXPECT_TRUE(device1.GetPath().empty());
107     EXPECT_TRUE(device1.GetName().empty());
108 }
109 
110 /**
111  * @tc.name: InputDeviceTest_SettersGetters
112  * @tc.desc: Test setters and getters of InputDevice
113  * @tc.type: FUNC
114  * @tc.require:
115  */
116 HWTEST_F(InputDeviceTest, InputDeviceTest_SettersGetters, TestSize.Level1)
117 {
118     InputDevice device;
119 
120     device.SetId(123);
121     EXPECT_EQ(device.GetId(), 123);
122 
123     device.SetPath("/some/path");
124     EXPECT_EQ(device.GetPath(), "/some/path");
125 
126     device.SetName("Device Name");
127     EXPECT_EQ(device.GetName(), "Device Name");
128 }
129 
130 /**
131  * @tc.name: InputDeviceTest_TrimString
132  * @tc.desc: Test TrimString method
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(InputDeviceTest, InputDeviceTest_TrimString, TestSize.Level1)
137 {
138     std::string str1 = "  test string  ";
139     TrimString(str1);
140     EXPECT_EQ(str1, "test string");
141 
142     std::string str2 = "test";
143     TrimString(str2);
144     EXPECT_EQ(str2, "test");
145 
146     std::string str3 = "  ";
147     TrimString(str3);
148     EXPECT_TRUE(str3.empty());
149 
150     std::string str4 = "";
151     TrimString(str4);
152     EXPECT_TRUE(str4.empty());
153 }
154 
155 /**
156  * @tc.name: InputDeviceTest_RemovePrefix
157  * @tc.desc: Test RemovePrefix method
158  * @tc.type: FUNC
159  * @tc.require:
160  */
161 HWTEST_F(InputDeviceTest, InputDeviceTest_RemovePrefix, TestSize.Level1)
162 {
163     std::string str1 = "PREFIX:value";
164     EXPECT_TRUE(RemovePrefix(str1, "PREFIX:"));
165     EXPECT_EQ(str1, "value");
166 
167     std::string str2 = "NON_PREFIX:value";
168     EXPECT_FALSE(RemovePrefix(str2, "PREFIX:"));
169     EXPECT_EQ(str2, "NON_PREFIX:value");
170 
171     std::string str3 = "PREFIX:  value  ";
172     EXPECT_TRUE(RemovePrefix(str3, "PREFIX:"));
173     EXPECT_NE(str3, "value");
174 }
175 
176 /**
177  * @tc.name: InputDeviceTest_InitFromTextLine_Valid
178  * @tc.desc: Test initializing device from valid text line
179  * @tc.type: FUNC
180  * @tc.require:
181  */
182 HWTEST_F(InputDeviceTest, InputDeviceTest_InitFromTextLine_Valid, TestSize.Level1)
183 {
184     InputDevice device;
185     EXPECT_TRUE(device.InitFromTextLine("DEVICE: 42 | /dev/input/event0 | Keyboard|adfa9924"));
186     EXPECT_EQ(device.GetId(), 42);
187     EXPECT_EQ(device.GetPath(), "/dev/input/event0");
188     EXPECT_EQ(device.GetName(), "Keyboard");
189 }
190 
191 /**
192  * @tc.name: InputDeviceTest_InitFromTextLine_Invalid
193  * @tc.desc: Test initializing device from invalid text lines
194  * @tc.type: FUNC
195  * @tc.require:
196  */
197 HWTEST_F(InputDeviceTest, InputDeviceTest_InitFromTextLine_Invalid, TestSize.Level1)
198 {
199     InputDevice device;
200     EXPECT_FALSE(device.InitFromTextLine("42 | /dev/input/event0 | Keyboard|adfa9921"));
201     EXPECT_FALSE(device.InitFromTextLine("DEVICE: 42 /dev/input/event0 | Keyboard|adfa9922"));
202     EXPECT_FALSE(device.InitFromTextLine("DEVICE: 42 | /dev/input/event0 Keyboard|adfa9923"));
203     EXPECT_FALSE(device.InitFromTextLine("DEVICE: abc | /dev/input/event0 | Keyboard|adfa9924"));
204 }
205 
206 /**
207  * @tc.name: InputDeviceTest_WriteEvents_EmptyVector
208  * @tc.desc: Test writing empty vector of events
209  * @tc.type: FUNC
210  * @tc.require:
211  */
212 HWTEST_F(InputDeviceTest, InputDeviceTest_WriteEvents_EmptyVector, TestSize.Level1)
213 {
214     InputDevice device;
215     std::vector<input_event> events;
216     EXPECT_FALSE(device.WriteEvents(events));
217 }
218 
219 /**
220  * @tc.name: InputDeviceTest_VerifyDeviceMatch_001
221  * @tc.desc: Test verify device match when name is empty
222  * @tc.type: FUNC
223  * @tc.require:
224  */
225 HWTEST_F(InputDeviceTest, InputDeviceTest_VerifyDeviceMatch_001, TestSize.Level0) {
226     InputDevice device;
227     device.name_ = "";
228     EXPECT_TRUE(device.VerifyDeviceMatch());
229 }
230 
231 
232 /**
233  * @tc.name: InputDeviceTest_VerifyDeviceMatch_002
234  * @tc.desc: Test verify device match when realpath is failed
235  * @tc.type: FUNC
236  * @tc.require:
237  */
238 HWTEST_F(InputDeviceTest, InputDeviceTest_VerifyDeviceMatch_002, TestSize.Level0) {
239     InputDevice device;
240     device.name_ = "Keyboard";
241 
242     device.path_ = "\\dev\\input\\event0";
243     EXPECT_FALSE(device.VerifyDeviceMatch());
244 
245     device.path_ = "/dev/input/invalid";
246     EXPECT_FALSE(device.VerifyDeviceMatch());
247 }
248 
249 /**
250  * @tc.name: InputDeviceTest_VerifyDeviceMatch_WhenDeviceOpenFailed
251  * @tc.desc: Test verify device match when device match failed
252  * @tc.type: FUNC
253  * @tc.require:
254  */
255 HWTEST_F(InputDeviceTest, InputDeviceTest_VerifyDeviceMatch_003, TestSize.Level0) {
256     InputDevice device;
257     device.path_ = "/dev/input/event0";
258     device.name_ = "Keyboard1";
259     EXPECT_FALSE(device.VerifyDeviceMatch());
260 }
261 
262 /**
263  * @tc.name: InputDeviceTest_OpenDevice_001
264  * @tc.desc: Test close device wether the device already opend
265  * @tc.type: FUNC
266  * @tc.require:
267  */
268 HWTEST_F(InputDeviceTest, InputDeviceTest_OpenDevice_001, TestSize.Level0) {
269     InputDevice device;
270     device.path_ = "/dev/input/event0";
271 
272     int fd = open(device.path_.c_str(), O_RDONLY);
273     EXPECT_NE(fd, -1);
274     if (fd != -1) {
275         close(fd);
276     }
277 
278     bool result = device.OpenDevice(O_RDONLY);
279     EXPECT_TRUE(result);
280 }
281 
282 /**
283  * @tc.name: InputDeviceTest_OpenDevice_002
284  * @tc.desc: Test the device path realpath check failed
285  * @tc.type: FUNC
286  * @tc.require:
287  */
288 HWTEST_F(InputDeviceTest, InputDeviceTest_OpenDevice_002, TestSize.Level0) {
289     InputDevice device;
290     device.path_ = "";
291 
292     bool result = device.OpenDevice(O_RDONLY);
293     EXPECT_FALSE(result);
294 }
295 
296 /**
297  * @tc.name: InputDeviceTest_OpenDevice_003
298  * @tc.desc: Test the device path open failed
299  * @tc.type: FUNC
300  * @tc.require:
301  */
302 HWTEST_F(InputDeviceTest, InputDeviceTest_OpenDevice_003, TestSize.Level0) {
303     InputDevice device;
304     device.path_ = "/dev/input/nonexistent";
305 
306     bool result = device.OpenDevice(O_RDONLY);
307     EXPECT_FALSE(result);
308 }
309 
310 /**
311  * @tc.name: InputDeviceTest_OpenDevice_004
312  * @tc.desc: Test the device path open succeed
313  * @tc.type: FUNC
314  * @tc.require:
315  */
316 HWTEST_F(InputDeviceTest, InputDeviceTest_OpenDevice_004, TestSize.Level0) {
317     InputDevice device;
318     device.path_ = "/dev/input/event0";
319 
320     int fd = open(device.path_.c_str(), O_RDONLY);
321     EXPECT_NE(fd, -1);
322     if (fd != -1) {
323         close(fd);
324     }
325 
326     bool result = device.OpenDevice(O_RDONLY);
327     EXPECT_TRUE(result);
328 }
329 } // namespace MMI
330 } // namespace OHOS