• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 /**
17  * @file analog_headset_test.cpp
18  *
19  * @brief The test equipment is an analog headset (headset_jack_index);
20  * First test the API of the input module called;
21  * Then test the headset plug-in recognition function, including four
22  * comprehensive tests: normal multiple plug-in, fast, slow, headset
23  * and headphone switching.
24  *
25  * @since 1.0
26  */
27 
28 #include <cstdint>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <fcntl.h>
32 #include <gtest/gtest.h>
33 #include <securec.h>
34 #include <string>
35 #include <unistd.h>
36 #include "analog_headset_ev.h"
37 #include "hdf_log.h"
38 #include "input_manager.h"
39 #include "osal_time.h"
40 
41 #define HDF_LOG_TAG HDF_AUDIO_DRIVER
42 namespace {
43 const int INIT_DEFAULT_VALUE = 255;
44 const int NAME_MAX_LEN = 10;
45 const int KEEP_ALIVE_TIME_MS = 15000;
46 using namespace testing::ext;
CommonGetInputInterface(void)47 static IInputInterface *CommonGetInputInterface(void)
48 {
49     int32_t ret;
50     static IInputInterface *inputIf = NULL;
51 
52     if (inputIf != NULL) {
53         return inputIf;
54     }
55     ret = GetInputInterface(&inputIf);
56     if (ret != INPUT_SUCCESS) {
57         HDF_LOGE("%s: get input hdi failed, ret %d", __func__, ret);
58     }
59 
60     return inputIf;
61 }
62 
63 class HdfAnalogHeadsetTest : public testing::Test {
64 public:
65     static void SetUpTestCase();
66     static void TearDownTestCase();
67     void SetUp();
68     void TearDown();
69 };
70 
SetUpTestCase()71 void HdfAnalogHeadsetTest::SetUpTestCase()
72 {
73     CommonGetInputInterface();
74 }
75 
TearDownTestCase()76 void HdfAnalogHeadsetTest::TearDownTestCase()
77 {
78     IInputInterface *inputHdi = CommonGetInputInterface();
79     ReleaseInputInterface(inputHdi);
80 }
81 
SetUp()82 void HdfAnalogHeadsetTest::SetUp()
83 {
84 }
85 
TearDown()86 void HdfAnalogHeadsetTest::TearDown()
87 {
88 }
89 
90 /**
91  * API test, start from here.
92  */
93 
94 /**
95   * @tc.name: OpenInputDev001
96   * @tc.desc: Open input device test.
97   * @tc.type: FUNC
98   * @tc.require: AR000GGUU3
99   */
100 HWTEST_F(HdfAnalogHeadsetTest, OpenInputDev001, TestSize.Level1)
101 {
102     int32_t ret;
103     IInputInterface *inputHdi = CommonGetInputInterface();
104 
105     if ((inputHdi == NULL) || (inputHdi->iInputManager == NULL)) {
106         HDF_LOGE("%s: inputHdi or inputHdi->iInputManager is NULL.", __func__);
107         return;
108     }
109 
110     ret = inputHdi->iInputManager->OpenInputDevice(HEADSET_JACK_INDEX);
111     if (ret != INPUT_SUCCESS) {
112         HDF_LOGE("%s: [OpenInputDevice] failed, ret %d.", __func__, ret);
113         return;
114     }
115     EXPECT_EQ(ret, INPUT_SUCCESS);
116 }
117 
118 /**
119   * @tc.name: GetInputDevice001
120   * @tc.desc: Get input device info test.
121   * @tc.type: FUNC
122   * @tc.require: AR000GGUU3
123   */
124 HWTEST_F(HdfAnalogHeadsetTest, GetInputDevice001, TestSize.Level1)
125 {
126     int32_t ret;
127     DeviceInfo *dev = NULL;
128     IInputInterface *inputHdi = CommonGetInputInterface();
129 
130     if ((inputHdi == NULL) || (inputHdi->iInputManager == NULL)) {
131         HDF_LOGE("%s: inputHdi or inputHdi->iInputManager is NULL.", __func__);
132         return;
133     }
134     ret = inputHdi->iInputManager->GetInputDevice(HEADSET_JACK_INDEX, &dev);
135     if (ret != INPUT_SUCCESS) {
136         HDF_LOGE("%s: get device1 failed, ret %d", __func__, ret);
137     } else {
138         if (dev != NULL) {
139             HDF_LOGI("%s: devindex = %u, devType = %u", __func__, dev->devIndex, dev->devType);
140             HDF_LOGI("%s: chipInfo = %s, vendorName = %s, chipName = %s",
141                 __func__, dev->chipInfo, dev->vendorName, dev->chipName);
142         }
143     }
144     EXPECT_EQ(ret, INPUT_SUCCESS);
145 }
146 
147 /**
148   * @tc.name: GetVendorName001
149   * @tc.desc: Get device vendor name test.
150   * @tc.type: FUNC
151   * @tc.require: AR000GGUU3
152   */
153 HWTEST_F(HdfAnalogHeadsetTest, GetVendorName001, TestSize.Level1)
154 {
155     int32_t ret;
156     char vendorName[NAME_MAX_LEN] = { 0 };
157     IInputInterface *inputHdi = CommonGetInputInterface();
158 
159     if ((inputHdi == NULL) || (inputHdi->iInputController == NULL)) {
160         HDF_LOGE("%s: inputHdi or inputHdi->iInputController is NULL.", __func__);
161         return;
162     }
163     ret = inputHdi->iInputController->GetVendorName(HEADSET_JACK_INDEX, vendorName, NAME_MAX_LEN);
164     if (ret != INPUT_SUCCESS) {
165         HDF_LOGE("%s: get device1's vendor name failed, ret %d", __func__, ret);
166     } else {
167         HDF_LOGI("%s: device1's vendor name is %s:", __func__, vendorName);
168     }
169     EXPECT_EQ(ret, INPUT_SUCCESS);
170 }
171 
172 /**
173   * @tc.name: GetChipName001
174   * @tc.desc: Get device chip name test.
175   * @tc.type: FUNC
176   * @tc.require: AR000GGUU3
177   */
178 HWTEST_F(HdfAnalogHeadsetTest, GetChipName001, TestSize.Level1)
179 {
180     int32_t ret;
181     char chipName[NAME_MAX_LEN] = { 0 };
182     IInputInterface *inputHdi = CommonGetInputInterface();
183 
184     if ((inputHdi == NULL) || (inputHdi->iInputController == NULL)) {
185         HDF_LOGE("%s: inputHdi or inputHdi->iInputController is NULL.", __func__);
186         return;
187     }
188     ret = inputHdi->iInputController->GetChipName(HEADSET_JACK_INDEX, chipName, NAME_MAX_LEN);
189     if (ret != INPUT_SUCCESS) {
190         HDF_LOGE("%s: get device1's chip name failed, ret %d", __func__, ret);
191     } else {
192         HDF_LOGI("%s: device1's chip name is %s", __func__, chipName);
193     }
194     EXPECT_EQ(ret, INPUT_SUCCESS);
195 }
196 
197 /**
198   * @tc.name: GetDeviceType001
199   * @tc.desc: Get input device type test.
200   * @tc.type: FUNC
201   * @tc.require: AR000GGUU3
202   */
203 HWTEST_F(HdfAnalogHeadsetTest, GetDeviceType001, TestSize.Level1)
204 {
205     int32_t ret;
206     uint32_t devType = INIT_DEFAULT_VALUE;
207     IInputInterface *inputHdi = CommonGetInputInterface();
208 
209     if ((inputHdi == NULL) || (inputHdi->iInputController == NULL)) {
210         HDF_LOGE("%s: inputHdi or inputHdi->iInputController is NULL.", __func__);
211         return;
212     }
213     ret = inputHdi->iInputController->GetDeviceType(HEADSET_JACK_INDEX, &devType);
214     if (ret != INPUT_SUCCESS) {
215         HDF_LOGE("%s: get device1's type failed, ret %d", __func__, ret);
216     } else {
217         HDF_LOGI("%s: device1's type is %u", __func__, devType);
218     }
219     EXPECT_EQ(ret, INPUT_SUCCESS);
220 }
221 
222 /**
223   * @tc.name: GetChipInfo001
224   * @tc.desc: Get input device chip info test.
225   * @tc.type: FUNC
226   * @tc.require: AR000GGUU3
227   */
228 HWTEST_F(HdfAnalogHeadsetTest, GetChipInfo001, TestSize.Level1)
229 {
230     int32_t ret;
231     char chipInfo[CHIP_INFO_LEN] = { 0 };
232     IInputInterface *inputHdi = CommonGetInputInterface();
233 
234     if ((inputHdi == NULL) || (inputHdi->iInputController == NULL)) {
235         HDF_LOGE("%s: inputHdi or inputHdi->iInputController is NULL.", __func__);
236         return;
237     }
238     ret = inputHdi->iInputController->GetChipInfo(HEADSET_JACK_INDEX, chipInfo, CHIP_INFO_LEN);
239     if (ret != INPUT_SUCCESS) {
240         HDF_LOGE("%s: get device1's chip info failed, ret %d", __func__, ret);
241     } else {
242         HDF_LOGI("%s: device1's chip info is %s", __func__, chipInfo);
243     }
244     EXPECT_EQ(ret, INPUT_SUCCESS);
245 }
246 
247 /**
248   * @tc.name: CloseInputDevice001
249   * @tc.desc: Close input device test.
250   * @tc.type: FUNC
251   * @tc.require: AR000GGUU3
252   */
253 HWTEST_F(HdfAnalogHeadsetTest, CloseInputDevice001, TestSize.Level1)
254 {
255     int32_t ret;
256     IInputInterface *inputHdi = CommonGetInputInterface();
257 
258     if ((inputHdi == NULL) || (inputHdi->iInputManager == NULL)) {
259         HDF_LOGE("%s: inputHdi or inputHdi->iInputManager is NULL.", __func__);
260         return;
261     }
262     ret = inputHdi->iInputManager->CloseInputDevice(HEADSET_JACK_INDEX);
263     if (ret != INPUT_SUCCESS) {
264         HDF_LOGE("%s: close device1 failed, ret %d", __func__, ret);
265     }
266     EXPECT_EQ(ret, INPUT_SUCCESS);
267 }
268 
269 /**
270  * Callback API test begin here.
271  */
ReportHandleInputEvent(const EventPackage * pkg)272 static void ReportHandleInputEvent(const EventPackage *pkg)
273 {
274     bool in;
275     bool invalidBtn = false;
276     static struct JackNotifyInfo headsetHookBtn;
277 
278     if (pkg == nullptr) {
279         HDF_LOGE("%s: pkg is nullptr.", __func__);
280         return;
281     }
282 
283     if (pkg->type != EV_KEY) {
284         HDF_LOGE("%s: [pkg->type != EV_KEY] is true.", __func__);
285         return;
286     }
287     switch (pkg->code) {
288         case KEY_JACK_HEADSET:
289             invalidBtn = true;
290             break;
291         case KEY_JACK_HEADPHONE:
292             invalidBtn = true;
293             break;
294         default:
295             break;
296     }
297     if (invalidBtn) {
298         in = (!!pkg->value) ? true : false;
299         headsetHookBtn.jackStatus = in;
300         headsetHookBtn.jackType = pkg->code;
301         if (in) {
302             printf("%s plugs in.\n", (pkg->code == KEY_JACK_HEADSET) ? "headset" : "headphone");
303         } else {
304             printf("jack plugs out.\n");
305         }
306     }
307 
308     return;
309 }
310 
ReportEventPkgCallback(const EventPackage ** pkgs,uint32_t count,uint32_t devIndex)311 static void ReportEventPkgCallback(const EventPackage **pkgs, uint32_t count, uint32_t devIndex)
312 {
313     int32_t i;
314     (void)devIndex;
315 
316     if (pkgs == NULL) {
317         HDF_LOGE("%s: pkgs is NULL.", __func__);
318         return;
319     }
320     for (i = 0; i < count; i++) {
321         if (pkgs[i]->type == EV_SYN) {
322             break;
323         }
324         printf("%s: pkgs[%d] = 0x%x, 0x%x, %d.\t", __func__, i, pkgs[i]->type, pkgs[i]->code, pkgs[i]->value);
325         ReportHandleInputEvent(pkgs[i]);
326     }
327 }
328 
ReportHotPlugEventPkgCallback(const HotPlugEvent * msg)329 static void ReportHotPlugEventPkgCallback(const HotPlugEvent *msg)
330 {
331     if (msg == NULL) {
332         HDF_LOGE("%s: msg is NULL.", __func__);
333         return;
334     }
335     printf("%s: status =%d devId=%d type =%d", __func__, msg->status, msg->devIndex, msg->devType);
336 }
337 
338 /**
339   * @tc.name: RegisterReportCallback001
340   * @tc.desc:  Registers a callback for reporting subscribed data of specified input devices to test.
341   * @tc.type: FUNC
342   * @tc.require: AR000GGUU3
343   */
344 HWTEST_F(HdfAnalogHeadsetTest, RegisterReportCallback001, TestSize.Level1)
345 {
346     int32_t ret;
347     static InputEventCb eventCb;
348     IInputInterface *inputHdi = CommonGetInputInterface();
349 
350     if ((inputHdi == NULL) || (inputHdi->iInputReporter == NULL) || (inputHdi->iInputManager == NULL)) {
351         HDF_LOGE("%s: inputHdi, iInputReporter or iInputManager is NULL.", __func__);
352         return;
353     }
354     ret = inputHdi->iInputManager->OpenInputDevice(HEADSET_JACK_INDEX);
355     if (ret != INPUT_SUCCESS) {
356         HDF_LOGE("%s: open device[%d] failed, ret %d", __func__, HEADSET_JACK_INDEX, ret);
357     }
358     EXPECT_EQ(ret, INPUT_SUCCESS);
359 
360     eventCb.EventPkgCallback = ReportEventPkgCallback;
361     ret = inputHdi->iInputReporter->RegisterReportCallback(HEADSET_JACK_INDEX, &eventCb);
362     if (ret != INPUT_SUCCESS) {
363         HDF_LOGE("%s: [RegisterReportCallback] failed, ret %d", __func__, ret);
364     } else {
365         HDF_LOGI("%s: wait 15s for testing, pls touch the panel now", __func__);
366         HDF_LOGI("%s: The event data is as following:", __func__);
367     }
368     EXPECT_EQ(ret, INPUT_SUCCESS);
369 }
370 
371 /**
372   * @tc.name: UnregisterReportCallback001
373   * @tc.desc: Unregisters the callback for reporting subscribed data of specified input devices to test.
374   * @tc.type: FUNC
375   * @tc.require: AR000GGUU3
376   */
377 HWTEST_F(HdfAnalogHeadsetTest, UnregisterReportCallback001, TestSize.Level1)
378 {
379     int32_t ret;
380     IInputInterface *inputHdi = CommonGetInputInterface();
381 
382     if ((inputHdi == NULL) || (inputHdi->iInputReporter == NULL) || (inputHdi->iInputManager == NULL)) {
383         HDF_LOGE("%s: inputHdi, iInputReporter or iInputManager is NULL.", __func__);
384         return;
385     }
386 
387     ret = inputHdi->iInputReporter->UnregisterReportCallback(HEADSET_JACK_INDEX);
388     if (ret != INPUT_SUCCESS) {
389         HDF_LOGE("%s: unregister callback failed for device1, ret %d", __func__, ret);
390     }
391     EXPECT_EQ(ret, INPUT_SUCCESS);
392 
393     ret = inputHdi->iInputManager->CloseInputDevice(HEADSET_JACK_INDEX);
394     if (ret != INPUT_SUCCESS) {
395         HDF_LOGE("%s: close device1 failed, ret %d", __func__, ret);
396     } else {
397         HDF_LOGI("%s: Close the device1 successfully after all test", __func__);
398     }
399     EXPECT_EQ(ret, INPUT_SUCCESS);
400 }
401 
402 /**
403   * @tc.name: RegisterHotPlugCallback001
404   * @tc.desc: Registers a hot plug callback to the HDIs for input devices to test.
405   * @tc.type: FUNC
406   * @tc.require: AR000GGUU3
407   */
408 HWTEST_F(HdfAnalogHeadsetTest, RegisterHotPlugCallback001, TestSize.Level1)
409 {
410     int32_t ret;
411     static InputHostCb hotPlugCb;
412     IInputInterface *inputHdi = CommonGetInputInterface();
413 
414     if ((inputHdi == NULL) || (inputHdi->iInputReporter == NULL) || (inputHdi->iInputManager == NULL)) {
415         HDF_LOGE("%s: inputHdi, iInputReporter or iInputManager is NULL.", __func__);
416         return;
417     }
418     ret = inputHdi->iInputManager->OpenInputDevice(HEADSET_JACK_INDEX);
419     if (ret != INPUT_SUCCESS) {
420         HDF_LOGE("%s: open device[%d] failed, ret %d", __func__, HEADSET_JACK_INDEX, ret);
421     }
422     EXPECT_EQ(ret, INPUT_SUCCESS);
423 
424     hotPlugCb.HotPlugCallback = ReportHotPlugEventPkgCallback;
425     ret = inputHdi->iInputReporter->RegisterHotPlugCallback(&hotPlugCb);
426     if (ret != INPUT_SUCCESS) {
427         HDF_LOGE("%s: [RegisterHotPlugCallback] failed, ret %d", __func__, ret);
428     } else {
429         HDF_LOGI("%s: wait 15s for testing, pls touch the panel now", __func__);
430         HDF_LOGI("%s: The event data is as following:", __func__);
431     }
432     EXPECT_EQ(ret, INPUT_SUCCESS);
433 }
434 
435 /**
436   * @tc.name: UnregisterHotPlugCallback001
437   * @tc.desc: Unregisters the hot plug callback of input devices to test.
438   * @tc.type: FUNC
439   * @tc.require: AR000GGUU3
440   */
441 HWTEST_F(HdfAnalogHeadsetTest, UnregisterHotPlugCallback001, TestSize.Level1)
442 {
443     int32_t ret;
444     IInputInterface *inputHdi = CommonGetInputInterface();
445 
446     if ((inputHdi == NULL) || (inputHdi->iInputReporter == NULL) || (inputHdi->iInputManager == NULL)) {
447         HDF_LOGE("%s: inputHdi, iInputReporter or iInputManager is NULL.", __func__);
448         return;
449     }
450 
451     ret = inputHdi->iInputReporter->UnregisterHotPlugCallback();
452     if (ret != INPUT_SUCCESS) {
453         HDF_LOGE("%s: unregister callback failed for device1, ret %d", __func__, ret);
454     }
455     EXPECT_EQ(ret, INPUT_SUCCESS);
456 
457     ret = inputHdi->iInputManager->CloseInputDevice(HEADSET_JACK_INDEX);
458     if (ret != INPUT_SUCCESS) {
459         HDF_LOGE("%s: close device1 failed, ret %d", __func__, ret);
460     } else {
461         HDF_LOGI("%s: Close the device1 successfully after all test", __func__);
462     }
463     EXPECT_EQ(ret, INPUT_SUCCESS);
464 }
465 
466 /**
467  * Comprehensive test, start from here.
468  *
469  */
AnalogHeadsetTestInit()470 static int32_t AnalogHeadsetTestInit()
471 {
472     int32_t ret;
473     static InputEventCb eventCb;
474     IInputInterface *inputHdi = CommonGetInputInterface();
475 
476     if ((inputHdi == NULL) || (inputHdi->iInputReporter == NULL) || (inputHdi->iInputManager == NULL)) {
477         HDF_LOGE("%s: inputHdi, iInputReporter or iInputManager is NULL.", __func__);
478         return INPUT_FAILURE;
479     }
480 
481     ret = inputHdi->iInputManager->OpenInputDevice(HEADSET_JACK_INDEX);
482     if (ret != INPUT_SUCCESS) {
483         HDF_LOGE("%s: open device[%d] failed, ret %d", __func__, HEADSET_JACK_INDEX, ret);
484         ReleaseInputInterface(inputHdi);
485         return INPUT_FAILURE;
486     }
487 
488     eventCb.EventPkgCallback = ReportEventPkgCallback;
489     ret = inputHdi->iInputReporter->RegisterReportCallback(HEADSET_JACK_INDEX, &eventCb);
490     if (ret != INPUT_SUCCESS) {
491         HDF_LOGE("%s: [RegisterReportCallback] failed, ret %d", __func__, ret);
492         ReleaseInputInterface(inputHdi);
493         return INPUT_FAILURE;
494     }
495 
496     return INPUT_SUCCESS;
497 }
498 
499 /**
500   * @tc.name: AnalogHeadsetFunctionTest001
501   * @tc.desc: Comprehensive test entrance of analog earphone.
502   * @tc.type: FUNC
503   * @tc.require: AR000GGUU3
504   */
505 HWTEST_F(HdfAnalogHeadsetTest, AnalogHeadsetFunctionTest001, TestSize.Level1)
506 {
507     int32_t ret;
508 
509     ret = AnalogHeadsetTestInit();
510 
511     printf("TEST1: Multiple plugging, wait 15s for testing, please hotplug now\n");
512     printf("The event data is as following:\n");
513     OsalMSleep(KEEP_ALIVE_TIME_MS);
514 
515     printf("\nTEST2: Switch HEADSET and HEADPHONE, wait 15s for testing, please hotplug now\n");
516     printf("The event data is as following:\n");
517     OsalMSleep(KEEP_ALIVE_TIME_MS);
518 
519     HDF_LOGI("%s: finished.", __func__);
520     EXPECT_EQ(ret, INPUT_SUCCESS);
521 }
522 }
523