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