1 /*
2 * Copyright (c) 2021 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 "usbd_device_test.h"
16 #include <iostream>
17 #include <vector>
18 #include "hdf_log.h"
19 #include "usb_param.h"
20 #include "usbd_client.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::USB;
25 using namespace std;
26
27 const int SLEEP_TIME = 3;
28 const uint8_t BUS_NUM_1 = 1;
29 const uint8_t DEV_ADDR_2 = 2;
30 const uint8_t BUS_NUM_255 = 255;
31
SetUpTestCase(void)32 void UsbdDeviceTest::SetUpTestCase(void)
33 {
34 auto ret = UsbdClient::GetInstance().SetPortRole(1, 1, 1);
35 sleep(SLEEP_TIME);
36 HDF_LOGI("UsbdFunctionTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
37 ASSERT_TRUE(ret == 0);
38 if (ret != 0) {
39 exit(0);
40 }
41 std::cout << "please connect device, press enter to continue" << std::endl;
42 int c;
43 while ((c = getchar()) != '\n' && c != EOF) {
44 }
45 }
46
TearDownTestCase(void)47 void UsbdDeviceTest::TearDownTestCase(void) {}
48
SetUp(void)49 void UsbdDeviceTest::SetUp(void) {}
50
TearDown(void)51 void UsbdDeviceTest::TearDown(void) {}
52
53 /**
54 * @tc.name: UsbdDevice001
55 * @tc.desc: Test functions to OpenDevice
56 * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
57 * @tc.type: FUNC
58 */
59 HWTEST_F(UsbdDeviceTest, UsbdOpenDevice001, Function | MediumTest | Level1)
60 {
61 uint8_t busNum = BUS_NUM_1;
62 uint8_t devAddr = DEV_ADDR_2;
63 struct UsbDev dev = {busNum, devAddr};
64 auto ret = UsbdClient::GetInstance().OpenDevice(dev);
65 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d OpenDevice result =%{public}d", __LINE__, ret);
66 ASSERT_TRUE(ret == 0);
67 }
68
69 /**
70 * @tc.name: UsbdDevice002
71 * @tc.desc: Test functions to OpenDevice
72 * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
73 * @tc.type: FUNC
74 */
75 HWTEST_F(UsbdDeviceTest, UsbdOpenDevice002, Function | MediumTest | Level1)
76 {
77 uint8_t busNum = BUS_NUM_255;
78 uint8_t devAddr = DEV_ADDR_2;
79 struct UsbDev dev = {busNum, devAddr};
80 auto ret = UsbdClient::GetInstance().OpenDevice(dev);
81 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d", __LINE__, ret);
82 ASSERT_TRUE(ret != 0);
83 }
84
85 /**
86 * @tc.name: UsbdDevice011
87 * @tc.desc: Test functions to CloseDevice
88 * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
89 * @tc.type: FUNC
90 */
91 HWTEST_F(UsbdDeviceTest, UsbdCloseDevice001, Function | MediumTest | Level1)
92 {
93 uint8_t busNum = BUS_NUM_1;
94 uint8_t devAddr = DEV_ADDR_2;
95 struct UsbDev dev = {busNum, devAddr};
96 auto ret = UsbdClient::GetInstance().OpenDevice(dev);
97 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d", __LINE__, ret);
98 ASSERT_TRUE(ret == 0);
99 ret = UsbdClient::GetInstance().CloseDevice(dev);
100 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d Close result=%{public}d", __LINE__, ret);
101 ASSERT_TRUE(ret == 0);
102 }
103
104 /**
105 * @tc.name: UsbdDevice012
106 * @tc.desc: Test functions to CloseDevice
107 * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
108 * @tc.type: FUNC
109 */
110 HWTEST_F(UsbdDeviceTest, UsbdCloseDevice002, Function | MediumTest | Level1)
111 {
112 uint8_t busNum = BUS_NUM_1;
113 uint8_t devAddr = DEV_ADDR_2;
114 struct UsbDev dev = {busNum, devAddr};
115 auto ret = UsbdClient::GetInstance().OpenDevice(dev);
116 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d", __LINE__, ret);
117 ASSERT_TRUE(ret == 0);
118 dev.busNum = BUS_NUM_255;
119 ret = UsbdClient::GetInstance().CloseDevice(dev);
120 HDF_LOGI("UsbdDeviceTest:: Line:%{public}d Close result=%{public}d", __LINE__, ret);
121 ASSERT_TRUE(ret != 0);
122 dev.busNum = BUS_NUM_1;
123 UsbdClient::GetInstance().CloseDevice(dev);
124 }
125
126