OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/usb/web_usb_detector.h" | 10 #include "chrome/browser/usb/web_usb_detector.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 }; | 53 }; |
54 | 54 |
55 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { | 55 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { |
56 base::string16 product_name = base::UTF8ToUTF16(kProductName_1); | 56 base::string16 product_name = base::UTF8ToUTF16(kProductName_1); |
57 GURL landing_page(kLandingPage_1); | 57 GURL landing_page(kLandingPage_1); |
58 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | 58 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
59 0, 1, "Google", kProductName_1, "002", landing_page)); | 59 0, 1, "Google", kProductName_1, "002", landing_page)); |
60 std::string guid = device->guid(); | 60 std::string guid = device->guid(); |
61 | 61 |
62 WebUsbDetector web_usb_detector; | 62 WebUsbDetector web_usb_detector; |
| 63 web_usb_detector.Initialize(); |
63 | 64 |
64 device_client_.usb_service()->AddDevice(device); | 65 device_client_.usb_service()->AddDevice(device); |
65 message_center::Notification* notification = | 66 message_center::Notification* notification = |
66 message_center_->FindVisibleNotificationById(guid); | 67 message_center_->FindVisibleNotificationById(guid); |
67 ASSERT_TRUE(notification != nullptr); | 68 ASSERT_TRUE(notification != nullptr); |
68 base::string16 expected_title = | 69 base::string16 expected_title = |
69 base::ASCIIToUTF16("Google Product A detected"); | 70 base::ASCIIToUTF16("Google Product A detected"); |
70 EXPECT_EQ(expected_title, notification->title()); | 71 EXPECT_EQ(expected_title, notification->title()); |
71 base::string16 expected_message = | 72 base::string16 expected_message = |
72 base::ASCIIToUTF16("Go to www.google.com/A to connect."); | 73 base::ASCIIToUTF16("Go to www.google.com/A to connect."); |
73 EXPECT_EQ(expected_message, notification->message()); | 74 EXPECT_EQ(expected_message, notification->message()); |
74 EXPECT_TRUE(notification->delegate() != nullptr); | 75 EXPECT_TRUE(notification->delegate() != nullptr); |
75 | 76 |
76 device_client_.usb_service()->RemoveDevice(device); | 77 device_client_.usb_service()->RemoveDevice(device); |
77 // Device is removed, so notification should be removed from the | 78 // Device is removed, so notification should be removed from the |
78 // message_center too. | 79 // message_center too. |
79 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 80 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
80 } | 81 } |
81 | 82 |
82 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { | 83 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { |
83 std::string product_name = ""; | 84 std::string product_name = ""; |
84 GURL landing_page(kLandingPage_1); | 85 GURL landing_page(kLandingPage_1); |
85 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | 86 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
86 0, 1, "Google", product_name, "002", landing_page)); | 87 0, 1, "Google", product_name, "002", landing_page)); |
87 std::string guid = device->guid(); | 88 std::string guid = device->guid(); |
88 | 89 |
89 WebUsbDetector web_usb_detector; | 90 WebUsbDetector web_usb_detector; |
| 91 web_usb_detector.Initialize(); |
90 | 92 |
91 device_client_.usb_service()->AddDevice(device); | 93 device_client_.usb_service()->AddDevice(device); |
92 // For device without product name, no notification is generated. | 94 // For device without product name, no notification is generated. |
93 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 95 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
94 | 96 |
95 device_client_.usb_service()->RemoveDevice(device); | 97 device_client_.usb_service()->RemoveDevice(device); |
96 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 98 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
97 } | 99 } |
98 | 100 |
99 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { | 101 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { |
100 GURL landing_page(""); | 102 GURL landing_page(""); |
101 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | 103 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
102 0, 1, "Google", kProductName_1, "002", landing_page)); | 104 0, 1, "Google", kProductName_1, "002", landing_page)); |
103 std::string guid = device->guid(); | 105 std::string guid = device->guid(); |
104 | 106 |
105 WebUsbDetector web_usb_detector; | 107 WebUsbDetector web_usb_detector; |
| 108 web_usb_detector.Initialize(); |
106 | 109 |
107 device_client_.usb_service()->AddDevice(device); | 110 device_client_.usb_service()->AddDevice(device); |
108 // For device without landing page, no notification is generated. | 111 // For device without landing page, no notification is generated. |
109 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 112 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
110 | 113 |
111 device_client_.usb_service()->RemoveDevice(device); | 114 device_client_.usb_service()->RemoveDevice(device); |
112 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 115 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
113 } | 116 } |
114 | 117 |
115 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { | 118 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { |
116 GURL landing_page(kLandingPage_1); | 119 GURL landing_page(kLandingPage_1); |
117 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | 120 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( |
118 0, 1, "Google", kProductName_1, "002", landing_page)); | 121 0, 1, "Google", kProductName_1, "002", landing_page)); |
119 std::string guid = device->guid(); | 122 std::string guid = device->guid(); |
120 | 123 |
121 // USB device was added before web_usb_detector was created. | 124 // USB device was added before web_usb_detector was created. |
122 device_client_.usb_service()->AddDevice(device); | 125 device_client_.usb_service()->AddDevice(device); |
123 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 126 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
124 | 127 |
125 WebUsbDetector web_usb_detector; | 128 WebUsbDetector web_usb_detector; |
| 129 web_usb_detector.Initialize(); |
126 | 130 |
127 device_client_.usb_service()->RemoveDevice(device); | 131 device_client_.usb_service()->RemoveDevice(device); |
128 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); | 132 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); |
129 } | 133 } |
130 | 134 |
131 TEST_F( | 135 TEST_F( |
132 WebUsbDetectorTest, | 136 WebUsbDetectorTest, |
133 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated)
{ | 137 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated)
{ |
134 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); | 138 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); |
135 GURL landing_page_1(kLandingPage_1); | 139 GURL landing_page_1(kLandingPage_1); |
(...skipping 23 matching lines...) Expand all Loading... |
159 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 163 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
160 | 164 |
161 device_client_.usb_service()->RemoveDevice(device_1); | 165 device_client_.usb_service()->RemoveDevice(device_1); |
162 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 166 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
163 device_client_.usb_service()->RemoveDevice(device_2); | 167 device_client_.usb_service()->RemoveDevice(device_2); |
164 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); | 168 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); |
165 device_client_.usb_service()->RemoveDevice(device_3); | 169 device_client_.usb_service()->RemoveDevice(device_3); |
166 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 170 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
167 | 171 |
168 WebUsbDetector web_usb_detector; | 172 WebUsbDetector web_usb_detector; |
| 173 web_usb_detector.Initialize(); |
169 } | 174 } |
170 | 175 |
171 TEST_F( | 176 TEST_F( |
172 WebUsbDetectorTest, | 177 WebUsbDetectorTest, |
173 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) { | 178 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) { |
174 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); | 179 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); |
175 GURL landing_page_1(kLandingPage_1); | 180 GURL landing_page_1(kLandingPage_1); |
176 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( | 181 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( |
177 0, 1, "Google", kProductName_1, "002", landing_page_1)); | 182 0, 1, "Google", kProductName_1, "002", landing_page_1)); |
178 std::string guid_1 = device_1->guid(); | 183 std::string guid_1 = device_1->guid(); |
(...skipping 12 matching lines...) Expand all Loading... |
191 | 196 |
192 // Three usb devices were added before web_usb_detector was created. | 197 // Three usb devices were added before web_usb_detector was created. |
193 device_client_.usb_service()->AddDevice(device_1); | 198 device_client_.usb_service()->AddDevice(device_1); |
194 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 199 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
195 device_client_.usb_service()->AddDevice(device_2); | 200 device_client_.usb_service()->AddDevice(device_2); |
196 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); | 201 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); |
197 device_client_.usb_service()->AddDevice(device_3); | 202 device_client_.usb_service()->AddDevice(device_3); |
198 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 203 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
199 | 204 |
200 WebUsbDetector web_usb_detector; | 205 WebUsbDetector web_usb_detector; |
| 206 web_usb_detector.Initialize(); |
201 | 207 |
202 device_client_.usb_service()->RemoveDevice(device_1); | 208 device_client_.usb_service()->RemoveDevice(device_1); |
203 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 209 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
204 device_client_.usb_service()->RemoveDevice(device_2); | 210 device_client_.usb_service()->RemoveDevice(device_2); |
205 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); | 211 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); |
206 device_client_.usb_service()->RemoveDevice(device_3); | 212 device_client_.usb_service()->RemoveDevice(device_3); |
207 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 213 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
208 } | 214 } |
209 | 215 |
210 TEST_F(WebUsbDetectorTest, | 216 TEST_F(WebUsbDetectorTest, |
(...skipping 16 matching lines...) Expand all Loading... |
227 6, 7, "Google", kProductName_3, "008", landing_page_3)); | 233 6, 7, "Google", kProductName_3, "008", landing_page_3)); |
228 std::string guid_3 = device_3->guid(); | 234 std::string guid_3 = device_3->guid(); |
229 | 235 |
230 // Two usb devices were added before web_usb_detector was created. | 236 // Two usb devices were added before web_usb_detector was created. |
231 device_client_.usb_service()->AddDevice(device_1); | 237 device_client_.usb_service()->AddDevice(device_1); |
232 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 238 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
233 device_client_.usb_service()->AddDevice(device_3); | 239 device_client_.usb_service()->AddDevice(device_3); |
234 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 240 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
235 | 241 |
236 WebUsbDetector web_usb_detector; | 242 WebUsbDetector web_usb_detector; |
| 243 web_usb_detector.Initialize(); |
237 | 244 |
238 device_client_.usb_service()->RemoveDevice(device_1); | 245 device_client_.usb_service()->RemoveDevice(device_1); |
239 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 246 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
240 | 247 |
241 device_client_.usb_service()->AddDevice(device_2); | 248 device_client_.usb_service()->AddDevice(device_2); |
242 message_center::Notification* notification = | 249 message_center::Notification* notification = |
243 message_center_->FindVisibleNotificationById(guid_2); | 250 message_center_->FindVisibleNotificationById(guid_2); |
244 ASSERT_TRUE(notification != nullptr); | 251 ASSERT_TRUE(notification != nullptr); |
245 base::string16 expected_title = | 252 base::string16 expected_title = |
246 base::ASCIIToUTF16("Google Product B detected"); | 253 base::ASCIIToUTF16("Google Product B detected"); |
(...skipping 23 matching lines...) Expand all Loading... |
270 3, 4, "Google", kProductName_2, "005", landing_page_2)); | 277 3, 4, "Google", kProductName_2, "005", landing_page_2)); |
271 std::string guid_2 = device_2->guid(); | 278 std::string guid_2 = device_2->guid(); |
272 | 279 |
273 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); | 280 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); |
274 GURL landing_page_3(kLandingPage_3); | 281 GURL landing_page_3(kLandingPage_3); |
275 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( | 282 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( |
276 6, 7, "Google", kProductName_3, "008", landing_page_3)); | 283 6, 7, "Google", kProductName_3, "008", landing_page_3)); |
277 std::string guid_3 = device_3->guid(); | 284 std::string guid_3 = device_3->guid(); |
278 | 285 |
279 WebUsbDetector web_usb_detector; | 286 WebUsbDetector web_usb_detector; |
| 287 web_usb_detector.Initialize(); |
280 | 288 |
281 device_client_.usb_service()->AddDevice(device_1); | 289 device_client_.usb_service()->AddDevice(device_1); |
282 message_center::Notification* notification_1 = | 290 message_center::Notification* notification_1 = |
283 message_center_->FindVisibleNotificationById(guid_1); | 291 message_center_->FindVisibleNotificationById(guid_1); |
284 ASSERT_TRUE(notification_1 != nullptr); | 292 ASSERT_TRUE(notification_1 != nullptr); |
285 base::string16 expected_title_1 = | 293 base::string16 expected_title_1 = |
286 base::ASCIIToUTF16("Google Product A detected"); | 294 base::ASCIIToUTF16("Google Product A detected"); |
287 EXPECT_EQ(expected_title_1, notification_1->title()); | 295 EXPECT_EQ(expected_title_1, notification_1->title()); |
288 base::string16 expected_message_1 = | 296 base::string16 expected_message_1 = |
289 base::ASCIIToUTF16("Go to www.google.com/A to connect."); | 297 base::ASCIIToUTF16("Go to www.google.com/A to connect."); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 3, 4, "Google", kProductName_2, "005", landing_page_2)); | 345 3, 4, "Google", kProductName_2, "005", landing_page_2)); |
338 std::string guid_2 = device_2->guid(); | 346 std::string guid_2 = device_2->guid(); |
339 | 347 |
340 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); | 348 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); |
341 GURL landing_page_3(kLandingPage_3); | 349 GURL landing_page_3(kLandingPage_3); |
342 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( | 350 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( |
343 6, 7, "Google", kProductName_3, "008", landing_page_3)); | 351 6, 7, "Google", kProductName_3, "008", landing_page_3)); |
344 std::string guid_3 = device_3->guid(); | 352 std::string guid_3 = device_3->guid(); |
345 | 353 |
346 WebUsbDetector web_usb_detector; | 354 WebUsbDetector web_usb_detector; |
| 355 web_usb_detector.Initialize(); |
347 | 356 |
348 device_client_.usb_service()->AddDevice(device_1); | 357 device_client_.usb_service()->AddDevice(device_1); |
349 message_center::Notification* notification_1 = | 358 message_center::Notification* notification_1 = |
350 message_center_->FindVisibleNotificationById(guid_1); | 359 message_center_->FindVisibleNotificationById(guid_1); |
351 ASSERT_TRUE(notification_1 != nullptr); | 360 ASSERT_TRUE(notification_1 != nullptr); |
352 base::string16 expected_title_1 = | 361 base::string16 expected_title_1 = |
353 base::ASCIIToUTF16("Google Product A detected"); | 362 base::ASCIIToUTF16("Google Product A detected"); |
354 EXPECT_EQ(expected_title_1, notification_1->title()); | 363 EXPECT_EQ(expected_title_1, notification_1->title()); |
355 base::string16 expected_message_1 = | 364 base::string16 expected_message_1 = |
356 base::ASCIIToUTF16("Go to www.google.com/A to connect."); | 365 base::ASCIIToUTF16("Go to www.google.com/A to connect."); |
(...skipping 26 matching lines...) Expand all Loading... |
383 base::ASCIIToUTF16("Go to www.google.com/C to connect."); | 392 base::ASCIIToUTF16("Go to www.google.com/C to connect."); |
384 EXPECT_EQ(expected_message_3, notification_3->message()); | 393 EXPECT_EQ(expected_message_3, notification_3->message()); |
385 EXPECT_TRUE(notification_3->delegate() != nullptr); | 394 EXPECT_TRUE(notification_3->delegate() != nullptr); |
386 | 395 |
387 device_client_.usb_service()->RemoveDevice(device_1); | 396 device_client_.usb_service()->RemoveDevice(device_1); |
388 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); | 397 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); |
389 | 398 |
390 device_client_.usb_service()->RemoveDevice(device_3); | 399 device_client_.usb_service()->RemoveDevice(device_3); |
391 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); | 400 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); |
392 } | 401 } |
OLD | NEW |