1 /*
2 * Copyright (c) 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 #include <cstdint>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <string>
22 #include <unistd.h>
23 #include "hdf_log.h"
24 #include "input_device_manager.h"
25 #include "input_manager.h"
26 #include "osal_time.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::Input;
30 static IInputInterface *g_inputInterface;
31 static InputEventCb g_callback;
32 static InputHostCb g_hotplugCb;
33 static int32_t g_touchIndex;
34 static const int32_t KEEP_ALIVE_TIME_MS = 3000;
35 static const int32_t INVALID_INDEX = 15;
36 static const int32_t INVALID_INDEX1 = -1;
37 static const int32_t MAX_DEVICES = 32;
38 class HdiInputTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void HdiInputTest::SetUpTestCase()
47 {
48 int32_t ret = GetInputInterface(&g_inputInterface);
49 if (ret != INPUT_SUCCESS) {
50 printf("%s: get input hdi failed, ret %d\n", __func__, ret);
51 }
52 }
53
TearDownTestCase()54 void HdiInputTest::TearDownTestCase()
55 {
56 ReleaseInputInterface(&g_inputInterface);
57 }
58
SetUp()59 void HdiInputTest::SetUp()
60 {
61 }
62
TearDown()63 void HdiInputTest::TearDown()
64 {
65 }
66
67 #define INPUT_CHECK_NULL_POINTER(pointer, ret) do { \
68 if ((pointer) == nullptr) { \
69 printf("%s: null pointer", __func__); \
70 ASSERT_EQ ((ret), INPUT_SUCCESS); \
71 } \
72 } while (0)
73
ReportEventPkgCallback(const InputEventPackage ** pkgs,uint32_t count,uint32_t devIndex)74 static void ReportEventPkgCallback(const InputEventPackage **pkgs, uint32_t count, uint32_t devIndex)
75 {
76 if (pkgs == nullptr) {
77 printf("%s: pkgs is null\n", __func__);
78 return;
79 }
80 for (uint32_t i = 0; i < count; i++) {
81 printf("device action Index: %u devIndex: %u type: %u code: %u value %d\n",
82 i, devIndex, pkgs[i]->type, pkgs[i]->code, pkgs[i]->value);
83 }
84 }
85
ReportHotPlugEventPkgCallback(const InputHotPlugEvent * msg)86 static void ReportHotPlugEventPkgCallback(const InputHotPlugEvent *msg)
87 {
88 if (msg == nullptr) {
89 printf("%s: msg is null\n", __func__);
90 return;
91 }
92 printf("%s: device hotplug action devIndex: %u devType: %u status: %u\n", __func__,
93 msg->devIndex, msg->devType, msg->status);
94 if (msg->status == INPUT_DEVICE_STATUS_OPENED) {
95 EXPECT_EQ(g_inputInterface->iInputManager->OpenInputDevice(msg->devIndex), INPUT_SUCCESS);
96 } else if (msg->status == INPUT_DEVICE_STATUS_CLOSED) {
97 EXPECT_EQ(g_inputInterface->iInputManager->CloseInputDevice(msg->devIndex), INPUT_SUCCESS);
98 } else {
99 // do nothing
100 }
101 }
102
103 /**
104 * @tc.name: ScanInputDevice001
105 * @tc.desc: scan input device test
106 * @tc.type: FUNC
107 * @tc.require: AR000F867R
108 */
109 HWTEST_F(HdiInputTest, ScanInputDevice001, TestSize.Level1)
110 {
111 InputDevDesc sta[MAX_DEVICES] = {0};
112 printf("%s: [Input] ScanInputDevice001 enter %d\n", __func__, __LINE__);
113 int32_t ret;
114 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
115 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
116 ret = g_inputInterface->iInputManager->ScanInputDevice(sta, sizeof(sta) / sizeof(InputDevDesc));
117 if (ret == INPUT_SUCCESS) {
118 printf("%s: ScanInputDevice result: %d, %d, %d, %d\n",
119 __func__, sta[0].devType, sta[0].devIndex, sta[1].devType, sta[1].devIndex);
120 }
121 for (int32_t i = 1; i < MAX_DEVICES; i++) {
122 if (sta[i].devIndex == 0) {
123 break;
124 }
125 if (sta[i].devType == INDEV_TYPE_TOUCH) {
126 g_touchIndex = sta[i].devIndex;
127 }
128 }
129 EXPECT_EQ(ret, INPUT_SUCCESS);
130 }
131
132 /**
133 * @tc.name: OpenInputDev001
134 * @tc.desc: open input device test
135 * @tc.type: FUNC
136 * @tc.require: AR000F867R
137 */
138 HWTEST_F(HdiInputTest, OpenInputDev001, TestSize.Level1)
139 {
140 printf("%s: [Input] OpenInputDev001 enter %d\n", __func__, __LINE__);
141 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
142 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
143 int32_t ret = g_inputInterface->iInputManager->OpenInputDevice(g_touchIndex);
144 if (ret != INPUT_SUCCESS) {
145 printf("%s: open device1 failed, ret %d\n", __func__, ret);
146 }
147 EXPECT_EQ(ret, INPUT_SUCCESS);
148 }
149
150 /**
151 * @tc.name: OpenInputDevice002
152 * @tc.desc: open input device test
153 * @tc.type: FUNC
154 * @tc.require: AR000F867R
155 */
156 HWTEST_F(HdiInputTest, OpenInputDevice002, TestSize.Level1)
157 {
158 printf("%s: [Input] OpenInputDev002 enter %d\n", __func__, __LINE__);
159 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
160 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
161 /* Device "15" is used for testing nonexistent device node */
162 int32_t ret = g_inputInterface->iInputManager->OpenInputDevice(INVALID_INDEX);
163 if (ret != HDF_SUCCESS) {
164 printf("%s: device %d dose not exist, can't open it, ret %d\n", __func__, INVALID_INDEX, ret);
165 }
166 EXPECT_NE(ret, INPUT_SUCCESS);
167 }
168
169
170 /**
171 * @tc.name: OpenInputDevice003
172 * @tc.desc: open input device test
173 * @tc.type: FUNC
174 * @tc.require: AR000F867R
175 */
176 HWTEST_F(HdiInputTest, OpenInputDevice003, TestSize.Level1)
177 {
178 printf("%s: [Input] OpenInputDev003 enter %d\n", __func__, __LINE__);
179 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
180 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
181 /* Device "-1" is used for testing nonexistent device node */
182 int32_t ret = g_inputInterface->iInputManager->OpenInputDevice(INVALID_INDEX1);
183 if (ret != HDF_SUCCESS) {
184 printf("%s: device %d dose not exist, can't open it, ret %d\n", __func__, INVALID_INDEX1, ret);
185 }
186 EXPECT_NE(ret, INPUT_SUCCESS);
187 }
188
189 /**
190 * @tc.name: CloseInputDevice001
191 * @tc.desc: close input device test
192 * @tc.type: FUNC
193 * @tc.require: AR000F867T, AR000F8QNL
194 */
195 HWTEST_F(HdiInputTest, CloseInputDevice001, TestSize.Level1)
196 {
197 printf("%s: [Input] CloseInputDev001 enter %d\n", __func__, __LINE__);
198 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
199 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
200 int32_t ret = g_inputInterface->iInputManager->CloseInputDevice(g_touchIndex);
201 if (ret != INPUT_SUCCESS) {
202 printf("%s: close device %d failed, ret %d\n", __func__, g_touchIndex, ret);
203 }
204 EXPECT_EQ(ret, INPUT_SUCCESS);
205 }
206
207 /**
208 * @tc.name: CloseInputDevice002
209 * @tc.desc: close input device test
210 * @tc.type: FUNC
211 * @tc.require: AR000F867T
212 */
213 HWTEST_F(HdiInputTest, CloseInputDevice002, TestSize.Level1)
214 {
215 printf("%s: [Input] CloseInputDev002 enter %d\n", __func__, __LINE__);
216 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
217 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
218 /* Device "15" is used for testing nonexistent device node */
219 int32_t ret = g_inputInterface->iInputManager->CloseInputDevice(INVALID_INDEX);
220 if (ret == INPUT_FAILURE) {
221 printf("%s: device %d doesn't exist, can't close it, ret %d\n", __func__, INVALID_INDEX, ret);
222 }
223 EXPECT_NE(ret, INPUT_SUCCESS);
224 }
225
226 /**
227 * @tc.name: CloseInputDevice003
228 * @tc.desc: close input device test
229 * @tc.type: FUNC
230 * @tc.require: AR000F867T
231 */
232 HWTEST_F(HdiInputTest, CloseInputDevice003, TestSize.Level1)
233 {
234 printf("%s: [Input] CloseInputDev002 enter %d\n", __func__, __LINE__);
235 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
236 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
237 /* Device "-1" is used for testing nonexistent device node */
238 int32_t ret = g_inputInterface->iInputManager->CloseInputDevice(INVALID_INDEX1);
239 if (ret == INPUT_FAILURE) {
240 printf("%s: device %d doesn't exist, can't close it, ret %d\n", __func__, INVALID_INDEX1, ret);
241 }
242 EXPECT_NE(ret, INPUT_SUCCESS);
243 }
244
245 /**
246 * @tc.name: GetInputDevice001
247 * @tc.desc: get input device info test
248 * @tc.type: FUNC
249 * @tc.require: AR000F867S
250 */
251 HWTEST_F(HdiInputTest, GetInputDevice001, TestSize.Level1)
252 {
253 printf("%s: [Input] GetInputDevice001 enter %d\n", __func__, __LINE__);
254 InputDeviceInfo *dev = new InputDeviceInfo();
255 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
256 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
257 int32_t ret = g_inputInterface->iInputManager->GetInputDevice(g_touchIndex, &dev);
258 if (ret != INPUT_SUCCESS) {
259 printf("%s: get device %d failed, ret %d\n", __func__, g_touchIndex, ret);
260 }
261 printf("GetInputDevice001 %s: devIndex = %u, devType = %u\n", __func__, dev->devIndex, dev->devType);
262 printf("GetInputDevice001: chipInfo = %s, vendorName = %s, chipName = %s, devName = %s\n",
263 dev->chipInfo, dev->vendorName, dev->chipName, dev->attrSet.devName);
264 printf("GetInputDevice001: busType = %u, vendor = %u, product = %u, version = %u\n",
265 dev->attrSet.id.busType, dev->attrSet.id.vendor, dev->attrSet.id.product, dev->attrSet.id.version);
266 EXPECT_EQ(ret, INPUT_SUCCESS);
267 }
268
269 /**
270 * @tc.name: GetInputDevice002
271 * @tc.desc: get input device info test
272 * @tc.type: FUNC
273 * @tc.require: AR000F867S
274 */
275 HWTEST_F(HdiInputTest, GetInputDevice002, TestSize.Level1)
276 {
277 printf("%s: [Input] GetInputDevice002 enter %d\n", __func__, __LINE__);
278 InputDeviceInfo *dev = new InputDeviceInfo();
279 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
280 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
281 int32_t ret = g_inputInterface->iInputManager->GetInputDevice(INVALID_INDEX1, &dev);
282 if (ret != INPUT_SUCCESS) {
283 printf("%s: get device %d failed, ret %d\n", __func__, INVALID_INDEX1, ret);
284 }
285 EXPECT_NE(ret, INPUT_SUCCESS);
286 }
287
288 /**
289 * @tc.name: GetInputDevice003
290 * @tc.desc: get input device info test
291 * @tc.type: FUNC
292 * @tc.require: AR000F867S
293 */
294 HWTEST_F(HdiInputTest, GetInputDevice003, TestSize.Level1)
295 {
296 printf("%s: [Input] GetInputDevice003 enter %d\n", __func__, __LINE__);
297 InputDeviceInfo *dev = new InputDeviceInfo();
298 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
299 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
300 int32_t ret = g_inputInterface->iInputManager->GetInputDevice(INVALID_INDEX, &dev);
301 if (ret != INPUT_SUCCESS) {
302 printf("%s: get device %d failed, ret %d\n", __func__, INVALID_INDEX, ret);
303 }
304 EXPECT_NE(ret, INPUT_SUCCESS);
305 }
306
307 /**
308 * @tc.name: GetInputDeviceList001
309 * @tc.desc: get input device list info test
310 * @tc.type: FUNC
311 * @tc.require: AR000F8680
312 */
313 HWTEST_F(HdiInputTest, GetInputDeviceList001, TestSize.Level1)
314 {
315 printf("%s: [Input] GetInputDeviceList001 enter\n", __func__);
316 int32_t ret;
317 uint32_t num = 0;
318 InputDeviceInfo *dev = new InputDeviceInfo[MAX_INPUT_DEV_NUM] {};
319 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
320 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
321 ret = g_inputInterface->iInputManager->GetInputDeviceList(&num, &dev, MAX_INPUT_DEV_NUM);
322 if (ret != INPUT_SUCCESS) {
323 printf("%s: get device list failed, ret %d\n", __func__, ret);
324 }
325 /* num <= MAX_INPUT_DEV_NUM return true */
326 ASSERT_LE(num, MAX_INPUT_DEV_NUM);
327 for (uint32_t i = 0; i < num; i++) {
328 printf("%s: num = %u, device[%u]'s info is:\n", __func__, num, i);
329 printf("%s: index = %u, devType = %u\n", __func__, (dev + i)->devIndex, (dev + i)->devType);
330 printf("%s: chipInfo = %s, vendorName = %s, chipName = %s, devName = %s\n",
331 __func__, (dev + i)->chipInfo, (dev + i)->vendorName, (dev + i)->chipName, (dev + i)->attrSet.devName);
332 }
333 EXPECT_EQ(ret, INPUT_SUCCESS);
334 }
335
336 /**
337 * @tc.name: RegisterCallbackAndReportData001
338 * @tc.desc: get input device chip info test
339 * @tc.type: FUNC
340 * @tc.require: AR000F8682, AR000F8QNL
341 */
342 HWTEST_F(HdiInputTest, RegisterCallbackAndReportData001, TestSize.Level1)
343 {
344 printf("%s: [Input] RegisterCallbackAndReportData001 enter\n", __func__);
345 int32_t ret;
346 g_callback.EventPkgCallback = ReportEventPkgCallback;
347 g_hotplugCb.HotPlugCallback = ReportHotPlugEventPkgCallback;
348 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
349 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputReporter, INPUT_NULL_PTR);
350 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
351 ret = g_inputInterface->iInputReporter->RegisterReportCallback(g_touchIndex, &g_callback);
352 if (ret != INPUT_SUCCESS) {
353 printf("%s: register callback failed for device %d, ret %d\n", __func__, g_touchIndex, ret);
354 }
355 EXPECT_EQ(ret, INPUT_SUCCESS);
356 ret = g_inputInterface->iInputManager->OpenInputDevice(g_touchIndex);
357 EXPECT_EQ(ret, INPUT_SUCCESS);
358 printf("%s: wait 3s for testing, pls touch the panel now\n", __func__);
359 printf("%s: The event data is as following:\n", __func__);
360 OsalMSleep(KEEP_ALIVE_TIME_MS);
361 }
362
363 /**
364 * @tc.name: UnregisterReportCallback001
365 * @tc.desc: get input device chip info test
366 * @tc.type: FUNC
367 * @tc.require: SR000F867Q
368 */
369 HWTEST_F(HdiInputTest, UnregisterReportCallback001, TestSize.Level1)
370 {
371 printf("%s: [Input] UnregisterReportCallback001 enter\n", __func__);
372 int32_t ret;
373 INPUT_CHECK_NULL_POINTER(g_inputInterface, INPUT_NULL_PTR);
374 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputReporter, INPUT_NULL_PTR);
375 INPUT_CHECK_NULL_POINTER(g_inputInterface->iInputManager, INPUT_NULL_PTR);
376
377 ret = g_inputInterface->iInputReporter->UnregisterReportCallback(g_touchIndex);
378 if (ret != INPUT_SUCCESS) {
379 printf("%s: unregister callback failed for device %d, ret %d\n", __func__, g_touchIndex, ret);
380 }
381 EXPECT_EQ(ret, INPUT_SUCCESS);
382 ret = g_inputInterface->iInputManager->CloseInputDevice(g_touchIndex);
383 if (ret != INPUT_SUCCESS) {
384 printf("%s: close device %d failed, ret %d\n", __func__, g_touchIndex, ret);
385 }
386 EXPECT_EQ(ret, INPUT_SUCCESS);
387 }
388