• 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 #include <gtest/gtest.h>
17 #include "cJSON.h"
18 #include <semaphore.h>
19 
20 #include <iostream>
21 
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "usb_srv_client.h"
25 #include "usb_srv_support.h"
26 
27 using namespace OHOS::EventFwk;
28 using namespace testing::ext;
29 using namespace OHOS::USB;
30 
31 namespace OHOS {
32 namespace USB {
33 class UsbEventTest : public testing::Test {
34 public:
35     void SetUp() override;
36     void TearDown() override;
37 
38     static sem_t testSem_;
39 };
40 
41 sem_t UsbEventTest::testSem_ {};
42 
43 class UsbSubscriberTest : public CommonEventSubscriber {
44 public:
UsbSubscriberTest(const CommonEventSubscribeInfo & sp)45     explicit UsbSubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) {}
46 
OnReceiveEvent(const CommonEventData & data)47     void OnReceiveEvent(const CommonEventData &data) override
48     {
49         USB_HILOGI(MODULE_USB_SERVICE, "recv event ok");
50         eventData_ = data;
51         sem_post(&UsbEventTest::testSem_);
52     }
53 
54     static CommonEventData eventData_;
55 };
56 
57 CommonEventData UsbSubscriberTest::eventData_ {};
58 
SetUp()59 void UsbEventTest::SetUp()
60 {
61     sem_init(&UsbEventTest::testSem_, 1, 0);
62 }
63 
TearDown()64 void UsbEventTest::TearDown()
65 {
66     sem_destroy(&UsbEventTest::testSem_);
67 }
68 
69 /**
70  * @tc.name: SUB_USB_Broadcast_0100
71  * @tc.desc: usb gadget connected event
72  * @tc.type: FUNC
73  */
74 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0100, TestSize.Level1)
75 {
76     // subscribe usb common event
77     MatchingSkills matchingSkills;
78     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
79     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
80     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
81     CommonEventManager::SubscribeCommonEvent(subscriber);
82 
83     std::cout << "please connect the gadget to some host" << std::endl;
84 
85     // block until UsbSubscriberTest post
86     sem_wait(&UsbEventTest::testSem_);
87     CommonEventManager::UnSubscribeCommonEvent(subscriber);
88 
89     auto &want = UsbSubscriberTest::eventData_.GetWant();
90     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_STATE);
91     EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::CONNECTED}, false));
92     EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::FUNCTION_NAME_HDC}, false));
93 }
94 
95 /**
96  * @tc.name: SUB_USB_Broadcast_0200
97  * @tc.desc: usb gadget disconnected event
98  * @tc.type: FUNC
99  */
100 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0200, TestSize.Level1)
101 {
102     MatchingSkills matchingSkills;
103     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
104     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
105     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
106     CommonEventManager::SubscribeCommonEvent(subscriber);
107 
108     std::cout << "please disconnect the gadget" << std::endl;
109 
110     // block until UsbSubscriberTest post
111     sem_wait(&UsbEventTest::testSem_);
112     CommonEventManager::UnSubscribeCommonEvent(subscriber);
113 
114     auto &want = UsbSubscriberTest::eventData_.GetWant();
115     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_STATE);
116     EXPECT_FALSE(want.GetBoolParam(std::string {UsbSrvSupport::CONNECTED}, true));
117     EXPECT_TRUE(want.GetBoolParam(std::string {UsbSrvSupport::FUNCTION_NAME_HDC}, false));
118 }
119 
120 /**
121  * @tc.name: SUB_USB_Broadcast_0300
122  * @tc.desc: usb device attached event
123  * @tc.type: FUNC
124  */
125 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0300, TestSize.Level1)
126 {
127     MatchingSkills matchingSkills;
128     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
129     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
130     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
131     CommonEventManager::SubscribeCommonEvent(subscriber);
132 
133     std::cout << "please connect a device to the host" << std::endl;
134 
135     // block until UsbSubscriberTest post
136     sem_wait(&UsbEventTest::testSem_);
137     CommonEventManager::UnSubscribeCommonEvent(subscriber);
138 
139     auto &want = UsbSubscriberTest::eventData_.GetWant();
140     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
141 
142     // parse string to Json::Value
143     std::string deviceStr = UsbSubscriberTest::eventData_.GetData();
144     std::cout << deviceStr << std::endl;
145     cJSON* deviceJson = cJSON_Parse(deviceStr.c_str());
146     if (!deviceJson) {
147         USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_0300 error, parse json string error");
148     }
149     EXPECT_TRUE(deviceJson);
150 
151     // valid device
152     UsbDevice device(deviceJson);
153     EXPECT_NE(device.GetiProduct(), 0);
154     EXPECT_NE(device.GetiManufacturer(), 0);
155     EXPECT_GT(device.GetConfigCount(), 0);
156 
157     // valid config
158     USBConfig config;
159     device.GetConfig(0, config);
160     EXPECT_NE(config.GetAttributes(), 0);
161     EXPECT_GT(config.GetInterfaceCount(), 0);
162 
163     // valid interface
164     UsbInterface interface;
165     config.GetInterface(0, interface);
166     EXPECT_NE(interface.GetClass(), 0);
167     EXPECT_NE(interface.GetEndpointCount(), 0);
168 
169     // valid endpoint
170     auto endpoint = interface.GetEndpoint(0);
171     EXPECT_NE(endpoint.value().GetAttributes(), 0);
172 }
173 
174 /**
175  * @tc.name: SUB_USB_Broadcast_0400
176  * @tc.desc: usb device detached event
177  * @tc.type: FUNC
178  */
179 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0400, TestSize.Level1)
180 {
181     MatchingSkills matchingSkills;
182     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
183     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
184     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
185     CommonEventManager::SubscribeCommonEvent(subscriber);
186 
187     std::cout << "please disconnect the device to the host" << std::endl;
188 
189     // block until UsbSubscriberTest post
190     sem_wait(&UsbEventTest::testSem_);
191     CommonEventManager::UnSubscribeCommonEvent(subscriber);
192 
193     auto &want = UsbSubscriberTest::eventData_.GetWant();
194     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
195 
196     std::string deviceStr = UsbSubscriberTest::eventData_.GetData();
197     std::cout << deviceStr << std::endl;
198     cJSON* deviceJson = cJSON_Parse(deviceStr.c_str());
199     if (!deviceJson) {
200         USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_0400 error, parse json string error");
201     }
202     EXPECT_TRUE(deviceJson);
203 
204     // valid device
205     UsbDevice device(deviceJson);
206     EXPECT_NE(device.GetiProduct(), 0);
207     EXPECT_NE(device.GetiManufacturer(), 0);
208 }
209 #ifdef SUPPORT_PORT_CHNAGE_TEST
210 /**
211  * @tc.name: SUB_USB_Broadcast_0500
212  * @tc.desc: usb port change to host event
213  * @tc.type: FUNC
214  */
215 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0500, TestSize.Level1)
216 {
217     MatchingSkills matchingSkills;
218     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
219     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
220     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
221     CommonEventManager::SubscribeCommonEvent(subscriber);
222     std::cout << "please switch port to host" << std::endl;
223 
224     // block until UsbSubscriberTest post
225     sem_wait(&UsbEventTest::testSem_);
226     CommonEventManager::UnSubscribeCommonEvent(subscriber);
227 
228     auto &want = UsbSubscriberTest::eventData_.GetWant();
229     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
230 
231     std::string portStr = UsbSubscriberTest::eventData_.GetData();
232     std::cout << portStr << std::endl;
233     cJSON* portJson = cJSON_Parse(portStr.c_str());
234     if (!portJson) {
235         USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_0500 error, parse json string error");
236     }
237     EXPECT_TRUE(portJson);
238 
239     // valid event
240     cJSON* jsonMode = cJSON_GetObjectItem(portJson, "mode");
241     EXPECT_EQ(jsonMode->valueint, UsbSrvSupport::PORT_MODE_HOST);
242 }
243 
244 /**
245  * @tc.name: SUB_USB_Broadcast_0600
246  * @tc.desc: usb port change to device event
247  * @tc.type: FUNC
248  */
249 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0600, TestSize.Level1)
250 {
251     MatchingSkills matchingSkills;
252     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
253     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
254     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
255     CommonEventManager::SubscribeCommonEvent(subscriber);
256     std::cout << "please switch port to device" << std::endl;
257 
258     // block until UsbSubscriberTest post
259     sem_wait(&UsbEventTest::testSem_);
260     CommonEventManager::UnSubscribeCommonEvent(subscriber);
261 
262     auto &want = UsbSubscriberTest::eventData_.GetWant();
263     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
264 
265     std::string portStr = UsbSubscriberTest::eventData_.GetData();
266     std::cout << portStr << std::endl;
267     cJSON* portJson = cJSON_Parse(portStr.c_str());
268     if (!portJson) {
269         USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_0600 error, parse json string error");
270     }
271     EXPECT_TRUE(portJson);
272 
273     // valid event
274     cJSON* jsonMode = cJSON_GetObjectItem(portJson, "mode");
275     EXPECT_EQ(jsonMode->valueint, UsbSrvSupport::PORT_MODE_DEVICE);
276 }
277 #endif //SUPPORT_PORT_CHNAGE_TEST
278 } // namespace USB
279 } // namespace OHOS
280