1 /*
2 * Copyright (c) 2024-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 "usbd_accessory_test.h"
17
18 #include <cstdint>
19 #include <cstdio>
20 #include <iostream>
21 #include <vector>
22 #include <string>
23
24 #include "UsbSubscriberTest.h"
25 #include "hdf_log.h"
26 #include "v1_2/iusb_interface.h"
27 #include "v1_2/usb_types.h"
28
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::USB;
32 using namespace std;
33 using namespace OHOS::HDI::Usb::V1_0;
34 using namespace OHOS::HDI::Usb::V1_2;
35
36 sptr<UsbSubscriberTest> UsbdAccessoryTest::subscriber_ = nullptr;
37
38 sptr<OHOS::HDI::Usb::V1_2::IUsbInterface> g_usbInterface = nullptr;
39
SetUpTestCase(void)40 void UsbdAccessoryTest::SetUpTestCase(void)
41 {
42 g_usbInterface = OHOS::HDI::Usb::V1_2::IUsbInterface::Get();
43 if (g_usbInterface == nullptr) {
44 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
45 exit(0);
46 }
47 std::cout << "please connect accessory, press enter to continue" << std::endl;
48 int c;
49 do {
50 c = getchar();
51 } while (c != '\n' && c != EOF);
52 }
53
TearDownTestCase(void)54 void UsbdAccessoryTest::TearDownTestCase(void)
55 {
56 }
57
SetUp(void)58 void UsbdAccessoryTest::SetUp(void)
59 {
60 }
61
TearDown(void)62 void UsbdAccessoryTest::TearDown(void)
63 {
64 }
65
66 /**
67 * @tc.number : SUB_USB_DeviceManager_HDI_Accessory_0100
68 * @tc.name : GetAccessoryInfoTest001
69 * @tc.desc : int32_t GetAccessoryInfo
70 * @tc.desc : Positive test: parameters correctly
71 * @tc.size : MediumTest
72 * @tc.type : Function
73 * @tc.level : Level 3
74 */
75 HWTEST_F(UsbdAccessoryTest, GetAccessoryInfoTest001, Function | MediumTest | Level1)
76 {
77 HDF_LOGI("Case Start : GetAccessoryInfoTest001 : GetAccessoryInfo");
78 vector<string> accessoryInfo;
79 auto ret = g_usbInterface->GetAccessoryInfo(accessoryInfo);
80 EXPECT_TRUE(ret == 0);
81 HDF_LOGI("UsbdAccessoryTest::GetAccessoryInfoTest001 %{public}d ret=%{public}d", __LINE__, ret);
82 EXPECT_TRUE(!accessoryInfo.empty()) << "accessoryInfo NULL";
83 HDF_LOGI("UsbdAccessoryTest::GetAccessoryInfoTest001 %{public}d size=%{public}zu", __LINE__,
84 accessoryInfo.size());
85 }
86
87 /**
88 * @tc.number : SUB_USB_DeviceManager_HDI_Accessory_0200
89 * @tc.name : OpenAccessoryTest001
90 * @tc.desc : int32_t OpenAccessory
91 * @tc.desc : Positive test: parameters correctly
92 * @tc.size : MediumTest
93 * @tc.type : Function
94 * @tc.level : Level 3
95 */
96 HWTEST_F(UsbdAccessoryTest, OpenAccessoryTest001, Function | MediumTest | Level1)
97 {
98 HDF_LOGI("Case Start : OpenAccessoryTest001 : OpenAccessory");
99 int32_t fd;
100 auto ret = g_usbInterface->OpenAccessory(fd);
101 EXPECT_TRUE(ret == 0);
102 HDF_LOGI("UsbdAccessoryTest::OpenAccessoryTest001 %{public}d ret=%{public}d", __LINE__, ret);
103 g_usbInterface->CloseAccessory(fd);
104 }
105
106 /**
107 * @tc.number : SUB_USB_DeviceManager_HDI_Accessory_0300
108 * @tc.name : OpenAccessoryTest002
109 * @tc.desc : int32_t OpenAccessory
110 * @tc.desc : Positive test: parameters correctly
111 * @tc.size : MediumTest
112 * @tc.type : Function
113 * @tc.level : Level 3
114 */
115 HWTEST_F(UsbdAccessoryTest, OpenAccessoryTest002, Function | MediumTest | Level1)
116 {
117 HDF_LOGI("Case Start : OpenAccessoryTest002 : OpenAccessory");
118 int32_t fd;
119 auto ret = g_usbInterface->OpenAccessory(fd);
120 EXPECT_TRUE(ret == 0);
121 HDF_LOGI("UsbdAccessoryTest::OpenAccessoryTest002 %{public}d ret=%{public}d", __LINE__, ret);
122 ret = g_usbInterface->OpenAccessory(fd);
123 EXPECT_TRUE(ret != 0);
124 HDF_LOGI("UsbdAccessoryTest::OpenAccessoryTest002 %{public}d ret=%{public}d", __LINE__, ret);
125 g_usbInterface->CloseAccessory(fd);
126 }
127
128 /**
129 * @tc.number : SUB_USB_DeviceManager_HDI_Accessory_0400
130 * @tc.name : CloseAccessoryTest001
131 * @tc.desc : int32_t CloseAccessory
132 * @tc.desc : Positive test: parameters correctly
133 * @tc.size : MediumTest
134 * @tc.type : Function
135 * @tc.level : Level 3
136 */
137 HWTEST_F(UsbdAccessoryTest, CloseAccessoryTest001, Function | MediumTest | Level1)
138 {
139 HDF_LOGI("Case Start : CloseAccessoryTest001 : CloseAccessory");
140 int32_t fd;
141 auto ret = g_usbInterface->OpenAccessory(fd);
142 EXPECT_TRUE(ret == 0);
143 HDF_LOGI("UsbdAccessoryTest::CloseAccessoryTest001 %{public}d ret=%{public}d", __LINE__, ret);
144 ret = g_usbInterface->CloseAccessory(fd);
145 EXPECT_TRUE(ret == 0);
146 HDF_LOGI("UsbdAccessoryTest::CloseAccessoryTest001 %{public}d ret=%{public}d", __LINE__, ret);
147 }
148