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 "virtual_device.h"
19 
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 using namespace testing::ext;
24 } // namespace
25 
26 class VirtualDeviceTest : public testing::Test {
27 public:
SetUpTestCase(void)28     static void SetUpTestCase(void) {}
TearDownTestCase(void)29     static void TearDownTestCase(void) {}
30 
31     const std::string DEVICE = "Virtual Mouse";
32     const uint16_t BUS_TYPE = BUS_USB;
33     const uint16_t VENDOR_ID = 0x93a;
34     const uint16_t PRODUCT_ID = 0x2510;
35 };
36 
37 /**
38  * @tc.name:Test_CreateHandle_mouse
39  * @tc.desc:Verify VirtualDevice function CreateHandle
40  * @tc.type: FUNC
41  * @tc.require:
42  */
43 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_mouse, TestSize.Level1)
44 {
45     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
46     std::string deviceType = "mouse";
47     auto ret = device.CreateHandle(deviceType);
48     EXPECT_TRUE(ret);
49 }
50 
51 /**
52  * @tc.name:Test_CreateHandle_keyboard
53  * @tc.desc:Verify VirtualDevice function CreateHandle
54  * @tc.type: FUNC
55  * @tc.require:
56  */
57 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_keyboard, TestSize.Level1)
58 {
59     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
60     std::string deviceType = "keyboard";
61     auto ret = device.CreateHandle(deviceType);
62     EXPECT_TRUE(ret);
63 }
64 
65 /**
66  * @tc.name:Test_CreateHandle_knob
67  * @tc.desc:Verify VirtualDevice function CreateHandle
68  * @tc.type: FUNC
69  * @tc.require:
70  */
71 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_knob, TestSize.Level1)
72 {
73     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
74     std::string deviceType = "knob";
75     auto ret = device.CreateHandle(deviceType);
76     EXPECT_TRUE(ret);
77 }
78 
79 /**
80  * @tc.name:Test_CreateHandle_joystick
81  * @tc.desc:Verify VirtualDevice function CreateHandle
82  * @tc.type: FUNC
83  * @tc.require:
84  */
85 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_joystick, TestSize.Level1)
86 {
87     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
88     std::string deviceType = "joystick";
89     auto ret = device.CreateHandle(deviceType);
90     EXPECT_TRUE(ret);
91 }
92 
93 /**
94  * @tc.name:Test_CreateHandle_trackball
95  * @tc.desc:Verify VirtualDevice function CreateHandle
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_trackball, TestSize.Level1)
100 {
101     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
102     std::string deviceType = "trackball";
103     auto ret = device.CreateHandle(deviceType);
104     EXPECT_TRUE(ret);
105 }
106 
107 /**
108  * @tc.name:Test_CreateHandle_remotecontrol
109  * @tc.desc:Verify VirtualDevice function CreateHandle
110  * @tc.type: FUNC
111  * @tc.require:
112  */
113 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_remotecontrol, TestSize.Level1)
114 {
115     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
116     std::string deviceType = "remotecontrol";
117     auto ret = device.CreateHandle(deviceType);
118     EXPECT_TRUE(ret);
119 }
120 
121 /**
122  * @tc.name:Test_CreateHandle_trackpad
123  * @tc.desc:Verify VirtualDevice function CreateHandle
124  * @tc.type: FUNC
125  * @tc.require:
126  */
127 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_trackpad, TestSize.Level1)
128 {
129     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
130     std::string deviceType = "trackpad";
131     auto ret = device.CreateHandle(deviceType);
132     EXPECT_TRUE(ret);
133 }
134 
135 /**
136  * @tc.name:Test_CreateHandle_gamepad
137  * @tc.desc:Verify VirtualDevice function CreateHandle
138  * @tc.type: FUNC
139  * @tc.require:
140  */
141 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_gamepad, TestSize.Level1)
142 {
143     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
144     std::string deviceType = "gamepad";
145     auto ret = device.CreateHandle(deviceType);
146     EXPECT_TRUE(ret);
147 }
148 
149 /**
150  * @tc.name:Test_CreateHandle_touchpad
151  * @tc.desc:Verify VirtualDevice function CreateHandle
152  * @tc.type: FUNC
153  * @tc.require:
154  */
155 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_touchpad, TestSize.Level1)
156 {
157     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
158     std::string deviceType = "touchpad";
159     auto ret = device.CreateHandle(deviceType);
160     EXPECT_TRUE(ret);
161 }
162 
163 /**
164  * @tc.name:Test_CreateHandle_touchscreen
165  * @tc.desc:Verify VirtualDevice function CreateHandle
166  * @tc.type: FUNC
167  * @tc.require:
168  */
169 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_touchscreen, TestSize.Level1)
170 {
171     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
172     std::string deviceType = "touchscreen";
173     auto ret = device.CreateHandle(deviceType);
174     EXPECT_TRUE(ret);
175 }
176 
177 /**
178  * @tc.name:Test_CreateHandle_phone
179  * @tc.desc:Verify VirtualDevice function CreateHandle
180  * @tc.type: FUNC
181  * @tc.require:
182  */
183 HWTEST_F(VirtualDeviceTest, Test_CreateHandle_phone, TestSize.Level1)
184 {
185     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
186     std::string deviceType = "phone";
187     auto ret = device.CreateHandle(deviceType);
188     EXPECT_FALSE(ret);
189 }
190 
191 /**
192  * @tc.name:Test_AddDevice_false01
193  * @tc.desc:Verify VirtualDevice function AddDevice
194  * @tc.type: FUNC
195  * @tc.require:
196  */
197 HWTEST_F(VirtualDeviceTest, Test_AddDevice_false01, TestSize.Level1)
198 {
199     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
200     std::string deviceName = "";
201     auto ret = device.AddDevice(deviceName);
202     EXPECT_FALSE(ret);
203 }
204 
205 /**
206  * @tc.name:Test_AddDevice_true
207  * @tc.desc:Verify VirtualDevice function AddDevice
208  * @tc.type: FUNC
209  * @tc.require:
210  */
211 HWTEST_F(VirtualDeviceTest, Test_AddDevice_true, TestSize.Level1)
212 {
213     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
214     std::string deviceName = "mouse";
215     auto ret = device.AddDevice(deviceName);
216     EXPECT_TRUE(ret);
217 }
218 
219 /**
220  * @tc.name:Test_AddDevice_false02
221  * @tc.desc:Verify VirtualDevice function AddDevice
222  * @tc.type: FUNC
223  * @tc.require:
224  */
225 HWTEST_F(VirtualDeviceTest, Test_AddDevice_false02, TestSize.Level1)
226 {
227     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
228     std::string deviceName = "falseName";
229     auto ret = device.AddDevice(deviceName);
230     EXPECT_FALSE(ret);
231 }
232 
233 /**
234  * @tc.name:Test_CloseDevice_false02
235  * @tc.desc:Verify VirtualDevice function CloseDevice
236  * @tc.type: FUNC
237  * @tc.require:
238  */
239 HWTEST_F(VirtualDeviceTest, Test_CloseDevice_false02, TestSize.Level1)
240 {
241     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
242     std::vector<std::string> fileList;
243     std::string fileName = "falseArgv";
244     fileList.push_back("close ");
245     fileList.push_back("falseArgv ");
246     auto ret = device.CloseDevice(fileName, fileList);
247     EXPECT_FALSE(ret);
248 }
249 
250 /**
251  * @tc.name:Test_FindDevice_listfalse01
252  * @tc.desc:Verify VirtualDevice function CommandBranch
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(VirtualDeviceTest, Test_FindDevice_listfalse01, TestSize.Level1)
257 {
258     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
259     std::vector<std::string> argvList;
260     argvList.push_back("binName ");
261     argvList.push_back("list");
262     auto ret = device.CommandBranch(argvList);
263     EXPECT_FALSE(ret);
264 }
265 
266 /**
267  * @tc.name:Test_FindDevice_listfalse02
268  * @tc.desc:Verify VirtualDevice function CommandBranch
269  * @tc.type: FUNC
270  * @tc.require:
271  */
272 HWTEST_F(VirtualDeviceTest, Test_FindDevice_listfalse02, TestSize.Level1)
273 {
274     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
275     std::vector<std::string> argvList;
276     argvList.push_back("binName ");
277     argvList.push_back("list ");
278     argvList.push_back("falseArgv");
279     auto ret = device.CommandBranch(argvList);
280     EXPECT_FALSE(ret);
281 }
282 
283 /**
284  * @tc.name:Test_FindDevice_addFalse
285  * @tc.desc:Verify VirtualDevice function CommandBranch
286  * @tc.type: FUNC
287  * @tc.require:
288  */
289 HWTEST_F(VirtualDeviceTest, Test_FindDevice_addFalse, TestSize.Level1)
290 {
291     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
292     std::vector<std::string> argvList;
293     argvList.push_back("binName ");
294     argvList.push_back("start ");
295     argvList.push_back("falseArgv");
296     auto ret = device.CommandBranch(argvList);
297     EXPECT_FALSE(ret);
298 }
299 
300 /**
301  * @tc.name:Test_FindDevice_addTrue
302  * @tc.desc:Verify VirtualDevice function CommandBranch
303  * @tc.type: FUNC
304  * @tc.require:
305  */
306 HWTEST_F(VirtualDeviceTest, Test_FindDevice_addTrue, TestSize.Level1)
307 {
308     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
309     std::vector<std::string> argvList;
310     argvList.push_back("binName ");
311     argvList.push_back("start");
312     argvList.push_back("mouse");
313     auto ret = device.CommandBranch(argvList);
314     EXPECT_TRUE(ret);
315 }
316 
317 /**
318  * @tc.name:Test_FindDevice_closeFalse01
319  * @tc.desc:Verify VirtualDevice function CommandBranch
320  * @tc.type: FUNC
321  * @tc.require:
322  */
323 HWTEST_F(VirtualDeviceTest, Test_FindDevice_closeFalse01, TestSize.Level1)
324 {
325     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
326     std::vector<std::string> argvList;
327     argvList.push_back("binName ");
328     argvList.push_back("close ");
329     argvList.push_back("falsePid");
330     auto ret = device.CommandBranch(argvList);
331     EXPECT_FALSE(ret);
332 }
333 
334 /**
335  * @tc.name:Test_FindDevice_closeTrue01
336  * @tc.desc:Verify VirtualDevice function CommandBranch
337  * @tc.type: FUNC
338  * @tc.require:
339  */
340 HWTEST_F(VirtualDeviceTest, Test_FindDevice_closeTrue01, TestSize.Level1)
341 {
342     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
343     std::string symbolFileTest;
344     symbolFileTest.append(g_folderPath).append("1111111").append("_").append("testDevice");
345     std::ofstream flagFile;
346     flagFile.open(symbolFileTest.c_str());
347     flagFile.close(symbolFileTest.c_str());
348     std::vector<std::string> argvList;
349     argvList.push_back("binName ");
350     argvList.push_back("close ");
351     argvList.push_back("1111111");
352     auto ret = device.CommandBranch(argvList);
353     EXPECT_FALSE(ret);
354 }
355 
356 /**
357  * @tc.name:Test_FindDevice_mkdirFalse01
358  * @tc.desc:Verify VirtualDevice function CommandBranch
359  * @tc.type: FUNC
360  * @tc.require:
361  */
362 HWTEST_F(VirtualDeviceTest, Test_FindDevice_mkdirFalse01, TestSize.Level1)
363 {
364     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
365     std::vector<std::string> argvList;
366     argvList.push_back("binName ");
367     argvList.push_back("close ");
368     argvList.push_back("falsePid");
369     auto ret = device.CommandBranch(argvList);
370     EXPECT_FALSE(ret);
371 }
372 
373 /**
374  * @tc.name:Test_FindDevice_False01
375  * @tc.desc:Verify VirtualDevice function CommandBranch
376  * @tc.type: FUNC
377  * @tc.require:
378  */
379 HWTEST_F(VirtualDeviceTest, Test_FindDevice_False01, TestSize.Level1)
380 {
381     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
382     std::vector<std::string> argvList;
383     argvList.push_back("binName ");
384     argvList.push_back("falseArgv ");
385     auto ret = device.CommandBranch(argvList);
386     EXPECT_FALSE(ret);
387 }
388 
389 /**
390  * @tc.name:Test_DoIoctl_false
391  * @tc.desc:Verify VirtualDevice function DoIoctl
392  * @tc.type: FUNC
393  * @tc.require:
394  */
395 HWTEST_F(VirtualDeviceTest, Test_DoIoctl_false, TestSize.Level1)
396 {
397     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
398     auto ret = device.DoIoctl(-1, UI_SET_KEYBIT, KEY_POWER);
399     EXPECT_FALSE(ret);
400 }
401 
402 /**
403  * @tc.name:Test_SetUp_01
404  * @tc.desc:Verify VirtualDevice function SetUp
405  * @tc.type: FUNC
406  * @tc.require:
407  */
408 HWTEST_F(VirtualDeviceTest, Test_SetUp_01, TestSize.Level1)
409 {
410     VirtualDevice device(DEVICE, BUS_TYPE, VENDOR_ID, PRODUCT_ID);
411     auto ret = device.SetUp();
412     EXPECT_TRUE(ret);
413 }
414 } // namespace MMI
415 } // namespace OHOS