1 /*
2 * Copyright (c) 2023 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 "usb_event_mock_test.h"
16
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <json/json.h>
20 #include <semaphore.h>
21
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "usb_common_test.h"
25 #include "usb_device.h"
26 #include "usb_device_pipe.h"
27 #include "usb_srv_client.h"
28 #include "usb_srv_support.h"
29
30 using namespace OHOS;
31 using namespace OHOS::HDI::Usb::V1_0;
32 using namespace OHOS::USB;
33 using namespace OHOS::EventFwk;
34 using namespace OHOS::USB::Common;
35 using namespace std;
36 using namespace testing::ext;
37
38 using ::testing::Eq;
39 using ::testing::Exactly;
40 using ::testing::Ge;
41 using ::testing::Le;
42 using ::testing::Ne;
43 using ::testing::Return;
44
45 namespace OHOS {
46 namespace USB {
47 sem_t UsbEventMockTest::testSem_ {};
48 sptr<MockUsbImpl> UsbEventMockTest::mockUsbImpl_ = nullptr;
49 sptr<UsbService> UsbEventMockTest::usbSrv_ = nullptr;
50
51 class UsbSubscriberMockTest : public CommonEventSubscriber {
52 public:
UsbSubscriberMockTest(const CommonEventSubscribeInfo & sp)53 explicit UsbSubscriberMockTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) {}
54
OnReceiveEvent(const CommonEventData & data)55 void OnReceiveEvent(const CommonEventData &data) override
56 {
57 USB_HILOGI(MODULE_USB_SERVICE, "recv event ok");
58 eventData_ = data;
59 sem_post(&UsbEventMockTest::testSem_);
60 }
61
62 static CommonEventData eventData_;
63 };
64
65 CommonEventData UsbSubscriberMockTest::eventData_ {};
66
SetUpTestCase(void)67 void UsbEventMockTest::SetUpTestCase(void)
68 {
69 USB_HILOGI(MODULE_USB_SERVICE, "UsbEventMockTest SetUpTestCase Start");
70 UsbCommonTest::SetTestCaseHapApply();
71
72 usbSrv_ = DelayedSpSingleton<UsbService>::GetInstance();
73 EXPECT_NE(usbSrv_, nullptr);
74 mockUsbImpl_ = DelayedSpSingleton<MockUsbImpl>::GetInstance();
75 EXPECT_NE(mockUsbImpl_, nullptr);
76
77 usbSrv_->SetUsbd(mockUsbImpl_);
78
79 sptr<UsbServiceSubscriber> iSubscriber = new UsbServiceSubscriber();
80 EXPECT_NE(iSubscriber, nullptr);
81 mockUsbImpl_->BindUsbdSubscriber(iSubscriber);
82 }
83
TearDownTestCase(void)84 void UsbEventMockTest::TearDownTestCase(void)
85 {
86 USB_HILOGI(MODULE_USB_SERVICE, "UsbEventMockTest TearDownTestCase enter");
87 sptr<IUsbInterface> usbd = IUsbInterface::Get();
88 mockUsbImpl_->UnbindUsbdSubscriber(nullptr);
89 usbSrv_->SetUsbd(usbd);
90
91 mockUsbImpl_ = nullptr;
92 usbSrv_ = nullptr;
93 DelayedSpSingleton<UsbService>::DestroyInstance();
94 DelayedSpSingleton<MockUsbImpl>::DestroyInstance();
95 }
96
SetUp()97 void UsbEventMockTest::SetUp()
98 {
99 sem_init(&UsbEventMockTest::testSem_, 1, 0);
100 }
101
TearDown()102 void UsbEventMockTest::TearDown()
103 {
104 sem_destroy(&UsbEventMockTest::testSem_);
105 }
106
107 /**
108 * @tc.name: SUB_USB_Broadcast_0100
109 * @tc.desc: usb gadget connected event
110 * @tc.desc: Positive test: parameters correctly
111 * @tc.type: FUNC
112 */
113 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0100, TestSize.Level1)
114 {
115 MatchingSkills matchingSkills;
116 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
117 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
118 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
119 CommonEventManager::SubscribeCommonEvent(subscriber);
120
121 USBDeviceInfo info;
122 info.status = ACT_UPDEVICE;
123 EXPECT_CALL(*mockUsbImpl_, GetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
124 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
125 EXPECT_EQ(0, ret);
126
127 sem_wait(&UsbEventMockTest::testSem_);
128 CommonEventManager::UnSubscribeCommonEvent(subscriber);
129 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
130 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_STATE);
131 EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::CONNECTED}, false));
132 EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::FUNCTION_NAME_HDC}, false));
133 }
134
135 /**
136 * @tc.name: SUB_USB_Broadcast_0200
137 * @tc.desc: usb gadget disconnected event
138 * @tc.desc: Positive test: parameters correctly
139 * @tc.type: FUNC
140 */
141 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0200, TestSize.Level1)
142 {
143 MatchingSkills matchingSkills;
144 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
145 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
146 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
147 CommonEventManager::SubscribeCommonEvent(subscriber);
148
149 USBDeviceInfo info;
150 info.status = ACT_DOWNDEVICE;
151 EXPECT_CALL(*mockUsbImpl_, GetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
152 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
153 EXPECT_EQ(0, ret);
154
155 sem_wait(&UsbEventMockTest::testSem_);
156 CommonEventManager::UnSubscribeCommonEvent(subscriber);
157 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
158 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_STATE);
159 EXPECT_FALSE(want.GetBoolParam(std::string {UsbSrvSupport::CONNECTED}, true));
160 EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::FUNCTION_NAME_HDC}, false));
161 }
162
163 /**
164 * @tc.name: SUB_USB_Broadcast_0300
165 * @tc.desc: usb device attached event
166 * @tc.desc: Positive test: parameters correctly
167 * @tc.type: FUNC
168 */
169 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0300, TestSize.Level1)
170 {
171 MatchingSkills matchingSkills;
172 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
173 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
174 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
175 CommonEventManager::SubscribeCommonEvent(subscriber);
176
177 USBDeviceInfo info = {ACT_DEVUP, BUS_NUM_OK, DEV_ADDR_OK};
178 EXPECT_CALL(*mockUsbImpl_, OpenDevice(testing::_)).WillRepeatedly(Return(0));
179 EXPECT_CALL(*mockUsbImpl_, CloseDevice(testing::_)).WillRepeatedly(Return(0));
180 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
181 EXPECT_EQ(0, ret);
182
183 sem_wait(&UsbEventMockTest::testSem_);
184 CommonEventManager::UnSubscribeCommonEvent(subscriber);
185 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
186 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
187 std::string deviceStr = UsbSubscriberMockTest::eventData_.GetData();
188 Json::CharReaderBuilder builder;
189 JSONCPP_STRING err;
190 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
191 Json::Value deviceJson;
192 auto parseResult = reader->parse(deviceStr.c_str(), deviceStr.c_str() + deviceStr.length(), &deviceJson, &err);
193 EXPECT_TRUE(parseResult);
194 UsbDevice device(deviceJson);
195 EXPECT_NE(device.GetiProduct(), 0);
196 EXPECT_NE(device.GetiManufacturer(), 0);
197 EXPECT_GT(device.GetConfigCount(), 0);
198 USBConfig config;
199 device.GetConfig(0, config);
200 EXPECT_NE(config.GetAttributes(), 0);
201 EXPECT_GT(config.GetInterfaceCount(), 0);
202 UsbInterface interface;
203 config.GetInterface(0, interface);
204 EXPECT_NE(interface.GetClass(), 0);
205 EXPECT_NE(interface.GetEndpointCount(), 0);
206 auto endpoint = interface.GetEndpoint(0);
207 EXPECT_NE(endpoint.value().GetAttributes(), 0);
208
209 info.status = ACT_DOWNDEVICE;
210 EXPECT_CALL(*mockUsbImpl_, CloseDevice(testing::_)).WillRepeatedly(Return(0));
211 ret = mockUsbImpl_->SubscriberDeviceEvent(info);
212 EXPECT_EQ(0, ret);
213 }
214
215 /**
216 * @tc.name: SUB_USB_Broadcast_0400
217 * @tc.desc: usb device detached event
218 * @tc.desc: Positive test: parameters correctly
219 * @tc.type: FUNC
220 */
221 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0400, TestSize.Level1)
222 {
223 USBDeviceInfo info = {ACT_DEVUP, BUS_NUM_OK, DEV_ADDR_OK};
224 EXPECT_CALL(*mockUsbImpl_, OpenDevice(testing::_)).WillRepeatedly(Return(0));
225 EXPECT_CALL(*mockUsbImpl_, CloseDevice(testing::_)).WillRepeatedly(Return(0));
226 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
227 EXPECT_EQ(0, ret);
228
229 MatchingSkills matchingSkills;
230 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
231 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
232 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
233 CommonEventManager::SubscribeCommonEvent(subscriber);
234
235 info.status = ACT_DEVDOWN;
236 EXPECT_CALL(*mockUsbImpl_, CloseDevice(testing::_)).WillRepeatedly(Return(0));
237 ret = mockUsbImpl_->SubscriberDeviceEvent(info);
238 EXPECT_EQ(0, ret);
239
240 sem_wait(&UsbEventMockTest::testSem_);
241 CommonEventManager::UnSubscribeCommonEvent(subscriber);
242 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
243 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
244 std::string deviceStr = UsbSubscriberMockTest::eventData_.GetData();
245 Json::CharReaderBuilder builder;
246 JSONCPP_STRING err;
247 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
248 Json::Value deviceJson;
249 auto parseResult = reader->parse(deviceStr.c_str(), deviceStr.c_str() + deviceStr.length(), &deviceJson, &err);
250 EXPECT_TRUE(parseResult);
251 UsbDevice device(deviceJson);
252 EXPECT_NE(device.GetiProduct(), 0);
253 EXPECT_NE(device.GetiManufacturer(), 0);
254 }
255
256 /**
257 * @tc.name: SUB_USB_Broadcast_0500
258 * @tc.desc: usb port change to host event
259 * @tc.desc: Positive test: parameters correctly
260 * @tc.type: FUNC
261 */
262 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0500, TestSize.Level1)
263 {
264 MatchingSkills matchingSkills;
265 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
266 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
267 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
268 CommonEventManager::SubscribeCommonEvent(subscriber);
269
270 int32_t portId = DEFAULT_PORT_ID;
271 int32_t powerRole = UsbSrvSupport::POWER_ROLE_SOURCE;
272 int32_t dataRole = UsbSrvSupport::DATA_ROLE_HOST;
273 auto ret = mockUsbImpl_->SetPortRole(portId, powerRole, dataRole);
274 EXPECT_EQ(0, ret);
275
276 sem_wait(&UsbEventMockTest::testSem_);
277 CommonEventManager::UnSubscribeCommonEvent(subscriber);
278 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
279 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
280 std::string portStr = UsbSubscriberMockTest::eventData_.GetData();
281 Json::CharReaderBuilder builder;
282 JSONCPP_STRING err;
283 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
284 Json::Value portJson;
285 auto parseResult = reader->parse(portStr.c_str(), portStr.c_str() + portStr.length(), &portJson, &err);
286 EXPECT_TRUE(parseResult);
287 EXPECT_EQ(portJson["mode"].asInt(), UsbSrvSupport::PORT_MODE_HOST);
288 }
289
290 /**
291 * @tc.name: SUB_USB_Broadcast_0600
292 * @tc.desc: usb port change to device event
293 * @tc.desc: Positive test: parameters correctly
294 * @tc.type: FUNC
295 */
296 HWTEST_F(UsbEventMockTest, SUB_USB_Broadcast_0600, TestSize.Level1)
297 {
298 MatchingSkills matchingSkills;
299 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
300 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
301 std::shared_ptr<UsbSubscriberMockTest> subscriber = std::make_shared<UsbSubscriberMockTest>(subscriberInfo);
302 CommonEventManager::SubscribeCommonEvent(subscriber);
303
304 int32_t portId = DEFAULT_PORT_ID;
305 int32_t powerRole = UsbSrvSupport::POWER_ROLE_SINK;
306 int32_t dataRole = UsbSrvSupport::DATA_ROLE_DEVICE;
307 auto ret = mockUsbImpl_->SetPortRole(portId, powerRole, dataRole);
308 EXPECT_EQ(0, ret);
309
310 sem_wait(&UsbEventMockTest::testSem_);
311 CommonEventManager::UnSubscribeCommonEvent(subscriber);
312 auto &want = UsbSubscriberMockTest::eventData_.GetWant();
313 EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
314 std::string portStr = UsbSubscriberMockTest::eventData_.GetData();
315 Json::CharReaderBuilder builder;
316 JSONCPP_STRING err;
317 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
318 Json::Value portJson;
319 auto parseResult = reader->parse(portStr.c_str(), portStr.c_str() + portStr.length(), &portJson, &err);
320 EXPECT_TRUE(parseResult);
321 EXPECT_EQ(portJson["mode"].asInt(), UsbSrvSupport::PORT_MODE_DEVICE);
322 }
323 } // namespace USB
324 } // namespace OHOS
325