OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/webusb/webusb_detector.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/strings/string16.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "components/webusb/webusb_browser_client.h" | |
11 #include "device/core/device_client.h" | |
12 #include "device/usb/mock_usb_device.h" | |
13 #include "device/usb/mock_usb_service.h" | |
14 #include "device/usb/usb_device.h" | |
15 #include "device/usb/usb_service.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "url/gurl.h" | |
19 | |
20 namespace webusb { | |
21 | |
22 namespace { | |
23 | |
24 class FakeDeviceClient : public device::DeviceClient { | |
25 public: | |
26 FakeDeviceClient() : usb_service_(nullptr) {} | |
27 | |
28 ~FakeDeviceClient() override {} | |
29 | |
30 // device::DeviceClient implementation: | |
31 device::UsbService* GetUsbService() override { return usb_service_; } | |
32 | |
33 void set_usb_service(device::UsbService* service) { usb_service_ = service; } | |
34 | |
35 private: | |
36 device::UsbService* usb_service_; | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
Same suggestion to use MockUsbService directly her
juncai
2015/09/08 21:14:00
Done.
| |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(FakeDeviceClient); | |
39 }; | |
40 | |
41 class MockWebUsbBrowserClient : public webusb::WebUsbBrowserClient { | |
42 public: | |
43 MockWebUsbBrowserClient() {} | |
44 | |
45 ~MockWebUsbBrowserClient() override {} | |
46 | |
47 // webusb::WebUsbBrowserClient implementation | |
48 MOCK_METHOD3(OnDeviceAdded, | |
49 void(const base::string16& product_name, | |
50 const GURL& landing_page, | |
51 const std::string& notification_id)); | |
52 | |
53 // webusb::WebUsbBrowserClient implementation | |
54 MOCK_METHOD1(OnDeviceRemoved, void(const std::string& notification_id)); | |
55 | |
56 private: | |
57 DISALLOW_COPY_AND_ASSIGN(MockWebUsbBrowserClient); | |
58 }; | |
59 | |
60 } // namespace | |
61 | |
62 class WebUsbDetectorTest : public testing::Test { | |
63 public: | |
64 WebUsbDetectorTest() {} | |
65 | |
66 ~WebUsbDetectorTest() override = default; | |
67 | |
68 void Initialize(scoped_refptr<device::MockUsbDevice> device, | |
69 webusb::WebUsbBrowserClient* webusb_browser_client, | |
70 bool set_usb_service_for_device_client) { | |
71 device_ = device; | |
72 usb_service_.reset(new device::MockUsbService()); | |
73 device_client_.reset(new FakeDeviceClient()); | |
74 if (set_usb_service_for_device_client) { | |
75 device_client_->set_usb_service(usb_service_.get()); | |
76 } | |
77 webusb_detector_.reset(new webusb::WebUsbDetector(webusb_browser_client)); | |
78 } | |
79 | |
80 protected: | |
81 void InvokeUsbAdded() { usb_service_->AddDevice(device_); } | |
82 | |
83 void InvokeUsbRemoved() { usb_service_->RemoveDevice(device_); } | |
84 | |
85 scoped_refptr<device::MockUsbDevice> device_; | |
86 scoped_ptr<device::MockUsbService> usb_service_; | |
87 scoped_ptr<FakeDeviceClient> device_client_; | |
88 scoped_ptr<webusb::WebUsbDetector> webusb_detector_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); | |
91 }; | |
92 | |
93 TEST_F(WebUsbDetectorTest, UsbDevicePluggedIn) { | |
94 std::string product_string = "Google Product A"; | |
95 GURL landing_page("https://www.google.com"); | |
96 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
97 0, 1, "Google", product_string, "001", landing_page)); | |
98 | |
99 MockWebUsbBrowserClient mock_webusb_browser_client; | |
100 | |
101 base::string16 product_name = base::UTF8ToUTF16(product_string); | |
102 std::string guid = device->guid(); | |
103 EXPECT_CALL(mock_webusb_browser_client, | |
104 OnDeviceAdded(product_name, landing_page, guid)) | |
105 .Times(1); | |
106 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); | |
107 | |
108 Initialize(device, &mock_webusb_browser_client, true); | |
109 | |
110 InvokeUsbAdded(); | |
111 } | |
112 | |
113 TEST_F(WebUsbDetectorTest, UsbDevicePluggedInAndRemoved) { | |
114 std::string product_string = "Google Product A"; | |
115 GURL landing_page("https://www.google.com"); | |
116 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
117 0, 1, "Google", product_string, "001", landing_page)); | |
118 | |
119 MockWebUsbBrowserClient mock_webusb_browser_client; | |
120 | |
121 base::string16 product_name = base::UTF8ToUTF16(product_string); | |
122 std::string guid = device->guid(); | |
123 EXPECT_CALL(mock_webusb_browser_client, | |
124 OnDeviceAdded(product_name, landing_page, guid)) | |
125 .Times(1); | |
126 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); | |
127 | |
128 Initialize(device, &mock_webusb_browser_client, true); | |
129 | |
130 InvokeUsbAdded(); | |
131 InvokeUsbRemoved(); | |
132 } | |
133 | |
134 TEST_F(WebUsbDetectorTest, UsbDeviceWithEmptyProductNamePluggedInAndRemoved) { | |
135 std::string product_string = ""; | |
136 GURL landing_page("https://www.google.com"); | |
137 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
138 0, 1, "Google", product_string, "001", landing_page)); | |
139 | |
140 MockWebUsbBrowserClient mock_webusb_browser_client; | |
141 | |
142 std::string guid = device->guid(); | |
143 EXPECT_CALL(mock_webusb_browser_client, | |
144 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
145 .Times(0); | |
146 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); | |
147 | |
148 Initialize(device, &mock_webusb_browser_client, true); | |
149 | |
150 InvokeUsbAdded(); | |
151 InvokeUsbRemoved(); | |
152 } | |
153 | |
154 TEST_F(WebUsbDetectorTest, UsbDeviceNoLandingPagePluggedInAndRemoved) { | |
155 std::string product_string = "Google Product B"; | |
156 GURL landing_page(""); | |
157 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
158 0, 1, "Google", product_string, "002", landing_page)); | |
159 | |
160 MockWebUsbBrowserClient mock_webusb_browser_client; | |
161 | |
162 std::string guid = device->guid(); | |
163 EXPECT_CALL(mock_webusb_browser_client, | |
164 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
165 .Times(0); | |
166 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); | |
167 | |
168 Initialize(device, &mock_webusb_browser_client, true); | |
169 | |
170 InvokeUsbAdded(); | |
171 InvokeUsbRemoved(); | |
172 } | |
173 | |
174 TEST_F(WebUsbDetectorTest, WebUsbBrowserClientIsNullptr) { | |
175 std::string product_string = "Google Product A"; | |
176 GURL landing_page("https://www.google.com"); | |
177 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
178 0, 1, "Google", product_string, "001", landing_page)); | |
179 | |
180 MockWebUsbBrowserClient mock_webusb_browser_client; | |
181 | |
182 EXPECT_CALL(mock_webusb_browser_client, | |
183 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
184 .Times(0); | |
185 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); | |
186 | |
187 Initialize(device, nullptr, true); | |
188 | |
189 InvokeUsbAdded(); | |
190 InvokeUsbRemoved(); | |
191 } | |
192 | |
193 // GetUsbService is nullptr | |
194 TEST_F(WebUsbDetectorTest, GetUsbServiceIsNullptr) { | |
195 std::string product_string = "Google Product A"; | |
196 GURL landing_page("https://www.google.com"); | |
197 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
198 0, 1, "Google", product_string, "001", landing_page)); | |
199 | |
200 MockWebUsbBrowserClient mock_webusb_browser_client; | |
201 | |
202 EXPECT_CALL(mock_webusb_browser_client, | |
203 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
204 .Times(0); | |
205 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); | |
206 | |
207 Initialize(device, &mock_webusb_browser_client, false); | |
208 | |
209 InvokeUsbAdded(); | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
It's weird for this test to have a UsbService but
juncai
2015/09/08 21:14:00
Done.
| |
210 InvokeUsbRemoved(); | |
211 } | |
212 | |
213 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { | |
214 std::string product_string = "Google Product A"; | |
215 GURL landing_page("https://www.google.com"); | |
216 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
217 0, 1, "Google", product_string, "001", landing_page)); | |
218 | |
219 MockWebUsbBrowserClient mock_webusb_browser_client; | |
220 | |
221 std::string guid = device->guid(); | |
222 EXPECT_CALL(mock_webusb_browser_client, | |
223 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
224 .Times(0); | |
225 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(1); | |
226 | |
227 Initialize(device, &mock_webusb_browser_client, true); | |
228 | |
229 InvokeUsbRemoved(); | |
230 } | |
231 | |
232 TEST_F(WebUsbDetectorTest, UsbDevicePluggedInAndRemovedMultipleTimes) { | |
233 std::string product_string = "Google Product A"; | |
234 GURL landing_page("https://www.google.com"); | |
235 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
236 0, 1, "Google", product_string, "001", landing_page)); | |
237 | |
238 MockWebUsbBrowserClient mock_webusb_browser_client; | |
239 | |
240 base::string16 product_name = base::UTF8ToUTF16(product_string); | |
241 std::string guid = device->guid(); | |
242 EXPECT_CALL(mock_webusb_browser_client, | |
243 OnDeviceAdded(product_name, landing_page, guid)) | |
244 .Times(3); | |
245 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(3); | |
246 | |
247 Initialize(device, &mock_webusb_browser_client, true); | |
248 | |
249 InvokeUsbAdded(); | |
250 InvokeUsbRemoved(); | |
251 InvokeUsbAdded(); | |
252 InvokeUsbRemoved(); | |
253 InvokeUsbAdded(); | |
254 InvokeUsbRemoved(); | |
255 } | |
256 | |
257 TEST_F(WebUsbDetectorTest, UsbDevicePluggedInMultipleTimes) { | |
258 std::string product_string = "Google Product A"; | |
259 GURL landing_page("https://www.google.com"); | |
260 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
261 0, 1, "Google", product_string, "001", landing_page)); | |
262 | |
263 MockWebUsbBrowserClient mock_webusb_browser_client; | |
264 | |
265 base::string16 product_name = base::UTF8ToUTF16(product_string); | |
266 std::string guid = device->guid(); | |
267 EXPECT_CALL(mock_webusb_browser_client, | |
268 OnDeviceAdded(product_name, landing_page, guid)) | |
269 .Times(3); | |
270 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(testing::_)).Times(0); | |
271 | |
272 Initialize(device, &mock_webusb_browser_client, true); | |
273 | |
274 InvokeUsbAdded(); | |
275 InvokeUsbAdded(); | |
276 InvokeUsbAdded(); | |
277 } | |
278 | |
279 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndRemovedMultipleTimes) { | |
280 std::string product_string = "Google Product A"; | |
281 GURL landing_page("https://www.google.com"); | |
282 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
283 0, 1, "Google", product_string, "001", landing_page)); | |
284 | |
285 MockWebUsbBrowserClient mock_webusb_browser_client; | |
286 | |
287 std::string guid = device->guid(); | |
288 EXPECT_CALL(mock_webusb_browser_client, | |
289 OnDeviceAdded(testing::_, testing::_, testing::_)) | |
290 .Times(0); | |
291 EXPECT_CALL(mock_webusb_browser_client, OnDeviceRemoved(guid)).Times(3); | |
292 | |
293 Initialize(device, &mock_webusb_browser_client, true); | |
294 | |
295 InvokeUsbRemoved(); | |
296 InvokeUsbRemoved(); | |
297 InvokeUsbRemoved(); | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
This is kind of a bug in the mocks... it shouldn't
juncai
2015/09/08 21:14:00
Done.
| |
298 } | |
299 | |
300 } // namespace webusb | |
OLD | NEW |