• 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 <json/json.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     Json::CharReaderBuilder builder;
146     JSONCPP_STRING err;
147     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
148     Json::Value deviceJson;
149     auto parseResult = reader->parse(deviceStr.c_str(), deviceStr.c_str() + deviceStr.length(), &deviceJson, &err);
150     EXPECT_TRUE(parseResult);
151 
152     // valid device
153     UsbDevice device(deviceJson);
154     EXPECT_NE(device.GetiProduct(), 0);
155     EXPECT_NE(device.GetiManufacturer(), 0);
156     EXPECT_GT(device.GetConfigCount(), 0);
157 
158     // valid config
159     USBConfig config;
160     device.GetConfig(0, config);
161     EXPECT_NE(config.GetAttributes(), 0);
162     EXPECT_GT(config.GetInterfaceCount(), 0);
163 
164     // valid interface
165     UsbInterface interface;
166     config.GetInterface(0, interface);
167     EXPECT_NE(interface.GetClass(), 0);
168     EXPECT_NE(interface.GetEndpointCount(), 0);
169 
170     // valid endpoint
171     auto endpoint = interface.GetEndpoint(0);
172     EXPECT_NE(endpoint.value().GetAttributes(), 0);
173 }
174 
175 /**
176  * @tc.name: SUB_USB_Broadcast_0400
177  * @tc.desc: usb device detached event
178  * @tc.type: FUNC
179  */
180 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0400, TestSize.Level1)
181 {
182     MatchingSkills matchingSkills;
183     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
184     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
185     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
186     CommonEventManager::SubscribeCommonEvent(subscriber);
187 
188     std::cout << "please disconnect the device to the host" << std::endl;
189 
190     // block until UsbSubscriberTest post
191     sem_wait(&UsbEventTest::testSem_);
192     CommonEventManager::UnSubscribeCommonEvent(subscriber);
193 
194     auto &want = UsbSubscriberTest::eventData_.GetWant();
195     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
196 
197     std::string deviceStr = UsbSubscriberTest::eventData_.GetData();
198     std::cout << deviceStr << std::endl;
199     Json::CharReaderBuilder builder;
200     JSONCPP_STRING err;
201     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
202     Json::Value deviceJson;
203     auto parseResult = reader->parse(deviceStr.c_str(), deviceStr.c_str() + deviceStr.length(), &deviceJson, &err);
204     EXPECT_TRUE(parseResult);
205 
206     // valid device
207     UsbDevice device(deviceJson);
208     EXPECT_NE(device.GetiProduct(), 0);
209     EXPECT_NE(device.GetiManufacturer(), 0);
210 }
211 
212 /**
213  * @tc.name: SUB_USB_Broadcast_0500
214  * @tc.desc: usb port change to host event
215  * @tc.type: FUNC
216  */
217 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0500, TestSize.Level1)
218 {
219     MatchingSkills matchingSkills;
220     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
221     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
222     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
223     CommonEventManager::SubscribeCommonEvent(subscriber);
224     std::cout << "please switch port to host" << std::endl;
225 
226     // block until UsbSubscriberTest post
227     sem_wait(&UsbEventTest::testSem_);
228     CommonEventManager::UnSubscribeCommonEvent(subscriber);
229 
230     auto &want = UsbSubscriberTest::eventData_.GetWant();
231     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
232 
233     std::string portStr = UsbSubscriberTest::eventData_.GetData();
234     std::cout << portStr << std::endl;
235     Json::CharReaderBuilder builder;
236     JSONCPP_STRING err;
237     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
238     Json::Value portJson;
239     auto parseResult = reader->parse(portStr.c_str(), portStr.c_str() + portStr.length(), &portJson, &err);
240     EXPECT_TRUE(parseResult);
241 
242     // valid event
243     EXPECT_EQ(portJson["mode"].asInt(), UsbSrvSupport::PORT_MODE_HOST);
244 }
245 
246 /**
247  * @tc.name: SUB_USB_Broadcast_0600
248  * @tc.desc: usb port change to device event
249  * @tc.type: FUNC
250  */
251 HWTEST_F(UsbEventTest, SUB_USB_Broadcast_0600, TestSize.Level1)
252 {
253     MatchingSkills matchingSkills;
254     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
255     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
256     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
257     CommonEventManager::SubscribeCommonEvent(subscriber);
258     std::cout << "please switch port to device" << std::endl;
259 
260     // block until UsbSubscriberTest post
261     sem_wait(&UsbEventTest::testSem_);
262     CommonEventManager::UnSubscribeCommonEvent(subscriber);
263 
264     auto &want = UsbSubscriberTest::eventData_.GetWant();
265     EXPECT_EQ(want.GetAction(), CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED);
266 
267     std::string portStr = UsbSubscriberTest::eventData_.GetData();
268     std::cout << portStr << std::endl;
269     Json::CharReaderBuilder builder;
270     JSONCPP_STRING err;
271     const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
272     Json::Value portJson;
273     auto parseResult = reader->parse(portStr.c_str(), portStr.c_str() + portStr.length(), &portJson, &err);
274     EXPECT_TRUE(parseResult);
275 
276     // valid event
277     EXPECT_EQ(portJson["mode"].asInt(), UsbSrvSupport::PORT_MODE_DEVICE);
278 }
279 } // namespace USB
280 } // namespace OHOS
281