Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/usb/web_usb_detector_unittest.cc

Issue 2146663002: Move //components/webusb/ to //chrome/browser/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merged web_usb_browser_client.* to web_usb_detector.* Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/usb/web_usb_detector.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/webusb/webusb_detector.h"
6
7 #include <string> 5 #include <string>
8 6
9 #include "base/macros.h" 7 #include "base/macros.h"
10 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
12 #include "components/webusb/webusb_browser_client.h" 10 #include "chrome/browser/usb/web_usb_detector.h"
13 #include "device/core/mock_device_client.h" 11 #include "device/core/mock_device_client.h"
14 #include "device/usb/mock_usb_device.h" 12 #include "device/usb/mock_usb_device.h"
15 #include "device/usb/mock_usb_service.h" 13 #include "device/usb/mock_usb_service.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notification_delegate.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 19
20 namespace { 20 namespace {
21 21
22 // usb device product name 22 // USB device product name.
23 const char* kProductName_1 = "Google Product A"; 23 const char* kProductName_1 = "Google Product A";
24 const char* kProductName_2 = "Google Product B"; 24 const char* kProductName_2 = "Google Product B";
25 const char* kProductName_3 = "Google Product C"; 25 const char* kProductName_3 = "Google Product C";
26 26
27 // usb device landing page 27 // USB device landing page.
28 const char* kLandingPage_1 = "https://www.google.com/A"; 28 const char* kLandingPage_1 = "https://www.google.com/A";
29 const char* kLandingPage_2 = "https://www.google.com/B"; 29 const char* kLandingPage_2 = "https://www.google.com/B";
30 const char* kLandingPage_3 = "https://www.google.com/C"; 30 const char* kLandingPage_3 = "https://www.google.com/C";
31 31
32 } // namespace 32 } // namespace
33 33
34 namespace webusb {
35
36 namespace {
37
38 class MockWebUsbBrowserClient : public webusb::WebUsbBrowserClient {
39 public:
40 MockWebUsbBrowserClient() {}
41
42 ~MockWebUsbBrowserClient() override {}
43
44 // webusb::WebUsbBrowserClient implementation
45 MOCK_METHOD3(OnDeviceAdded,
46 void(const base::string16& product_name,
47 const GURL& landing_page,
48 const std::string& notification_id));
49
50 // webusb::WebUsbBrowserClient implementation
51 MOCK_METHOD1(OnDeviceRemoved, void(const std::string& notification_id));
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(MockWebUsbBrowserClient);
55 };
56
57 } // namespace
58
59 class WebUsbDetectorTest : public testing::Test { 34 class WebUsbDetectorTest : public testing::Test {
60 public: 35 public:
61 WebUsbDetectorTest() {} 36 WebUsbDetectorTest() {}
37 ~WebUsbDetectorTest() override = default;
62 38
63 ~WebUsbDetectorTest() override = default; 39 void SetUp() override {
40 message_center::MessageCenter::Initialize();
41 message_center_ = message_center::MessageCenter::Get();
42 ASSERT_TRUE(message_center_ != nullptr);
43 }
44
45 void TearDown() override { message_center::MessageCenter::Shutdown(); }
64 46
65 protected: 47 protected:
66 device::MockDeviceClient device_client_; 48 device::MockDeviceClient device_client_;
67 MockWebUsbBrowserClient mock_webusb_browser_client_; 49 message_center::MessageCenter* message_center_;
68 50
69 private: 51 private:
70 DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); 52 DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest);
71 }; 53 };
72 54
73 TEST_F(WebUsbDetectorTest, UsbDeviceAdded) {
74 base::string16 product_name = base::UTF8ToUTF16(kProductName_1);
75 GURL landing_page(kLandingPage_1);
76 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
77 0, 1, "Google", kProductName_1, "002", landing_page));
78 std::string guid = device->guid();
79
80 EXPECT_CALL(mock_webusb_browser_client_,
81 OnDeviceAdded(product_name, landing_page, guid))
82 .Times(1);
83 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(testing::_))
84 .Times(0);
85
86 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
87
88 device_client_.usb_service()->AddDevice(device);
89 }
90
91 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { 55 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) {
92 base::string16 product_name = base::UTF8ToUTF16(kProductName_1); 56 base::string16 product_name = base::UTF8ToUTF16(kProductName_1);
93 GURL landing_page(kLandingPage_1); 57 GURL landing_page(kLandingPage_1);
94 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 58 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
95 0, 1, "Google", kProductName_1, "002", landing_page)); 59 0, 1, "Google", kProductName_1, "002", landing_page));
96 std::string guid = device->guid(); 60 std::string guid = device->guid();
97 61
98 { 62 WebUsbDetector web_usb_detector;
99 testing::InSequence s;
100 EXPECT_CALL(mock_webusb_browser_client_,
101 OnDeviceAdded(product_name, landing_page, guid))
102 .Times(1);
103 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid)).Times(1);
104 }
105
106 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
107 63
108 device_client_.usb_service()->AddDevice(device); 64 device_client_.usb_service()->AddDevice(device);
65 message_center::Notification* notification =
66 message_center_->FindVisibleNotificationById(guid);
67 ASSERT_TRUE(notification != nullptr);
68 base::string16 expected_title =
69 base::ASCIIToUTF16("Google Product A detected");
70 EXPECT_EQ(expected_title, notification->title());
71 base::string16 expected_message =
72 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
73 EXPECT_EQ(expected_message, notification->message());
74 EXPECT_TRUE(notification->delegate() != nullptr);
75
109 device_client_.usb_service()->RemoveDevice(device); 76 device_client_.usb_service()->RemoveDevice(device);
77 // Device is removed, so notification should be removed from the
78 // message_center too.
79 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
110 } 80 }
111 81
112 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { 82 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) {
113 std::string product_name = ""; 83 std::string product_name = "";
114 GURL landing_page(kLandingPage_1); 84 GURL landing_page(kLandingPage_1);
115 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 85 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
116 0, 1, "Google", product_name, "002", landing_page)); 86 0, 1, "Google", product_name, "002", landing_page));
117 std::string guid = device->guid(); 87 std::string guid = device->guid();
118 88
119 EXPECT_CALL(mock_webusb_browser_client_, 89 WebUsbDetector web_usb_detector;
120 OnDeviceAdded(testing::_, testing::_, testing::_))
121 .Times(0);
122 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid)).Times(1);
123
124 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
125 90
126 device_client_.usb_service()->AddDevice(device); 91 device_client_.usb_service()->AddDevice(device);
92 // For device without product name, no notification is generated.
93 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
94
127 device_client_.usb_service()->RemoveDevice(device); 95 device_client_.usb_service()->RemoveDevice(device);
96 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
128 } 97 }
129 98
130 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { 99 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) {
131 GURL landing_page(""); 100 GURL landing_page("");
132 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 101 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
133 0, 1, "Google", kProductName_1, "002", landing_page)); 102 0, 1, "Google", kProductName_1, "002", landing_page));
134 std::string guid = device->guid(); 103 std::string guid = device->guid();
135 104
136 EXPECT_CALL(mock_webusb_browser_client_, 105 WebUsbDetector web_usb_detector;
137 OnDeviceAdded(testing::_, testing::_, testing::_))
138 .Times(0);
139 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid)).Times(1);
140
141 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
142 106
143 device_client_.usb_service()->AddDevice(device); 107 device_client_.usb_service()->AddDevice(device);
108 // For device without landing page, no notification is generated.
109 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
110
144 device_client_.usb_service()->RemoveDevice(device); 111 device_client_.usb_service()->RemoveDevice(device);
145 } 112 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
146
147 TEST_F(WebUsbDetectorTest, WebUsbBrowserClientIsNullptr) {
148 GURL landing_page(kLandingPage_1);
149 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
150 0, 1, "Google", kProductName_1, "002", landing_page));
151
152 EXPECT_CALL(mock_webusb_browser_client_,
153 OnDeviceAdded(testing::_, testing::_, testing::_))
154 .Times(0);
155 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(testing::_))
156 .Times(0);
157
158 webusb::WebUsbDetector webusb_detector(nullptr);
159
160 device_client_.usb_service()->AddDevice(device);
161 device_client_.usb_service()->RemoveDevice(device);
162 }
163
164 TEST_F(WebUsbDetectorTest, NoUsbService) {
165 GURL landing_page(kLandingPage_1);
166 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
167 0, 1, "Google", kProductName_1, "002", landing_page));
168
169 EXPECT_CALL(mock_webusb_browser_client_,
170 OnDeviceAdded(testing::_, testing::_, testing::_))
171 .Times(0);
172 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(testing::_))
173 .Times(0);
174
175 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
176 } 113 }
177 114
178 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { 115 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) {
179 GURL landing_page(kLandingPage_1); 116 GURL landing_page(kLandingPage_1);
180 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 117 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
181 0, 1, "Google", kProductName_1, "002", landing_page)); 118 0, 1, "Google", kProductName_1, "002", landing_page));
182 std::string guid = device->guid(); 119 std::string guid = device->guid();
183 120
184 EXPECT_CALL(mock_webusb_browser_client_, 121 // USB device was added before web_usb_detector was created.
185 OnDeviceAdded(testing::_, testing::_, testing::_)) 122 device_client_.usb_service()->AddDevice(device);
186 .Times(0); 123 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
187 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid)).Times(1);
188 124
189 // usb device was added before webusb_detector was created 125 WebUsbDetector web_usb_detector;
190 device_client_.usb_service()->AddDevice(device);
191
192 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
193 126
194 device_client_.usb_service()->RemoveDevice(device); 127 device_client_.usb_service()->RemoveDevice(device);
128 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid));
195 } 129 }
196 130
197 TEST_F( 131 TEST_F(
198 WebUsbDetectorTest, 132 WebUsbDetectorTest,
199 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated) { 133 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated) {
200 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 134 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
201 GURL landing_page_1(kLandingPage_1); 135 GURL landing_page_1(kLandingPage_1);
202 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 136 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
203 0, 1, "Google", kProductName_1, "002", landing_page_1)); 137 0, 1, "Google", kProductName_1, "002", landing_page_1));
204 std::string guid_1 = device_1->guid(); 138 std::string guid_1 = device_1->guid();
205 139
206 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 140 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
207 GURL landing_page_2(kLandingPage_2); 141 GURL landing_page_2(kLandingPage_2);
208 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 142 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
209 3, 4, "Google", kProductName_2, "005", landing_page_2)); 143 3, 4, "Google", kProductName_2, "005", landing_page_2));
210 std::string guid_2 = device_2->guid(); 144 std::string guid_2 = device_2->guid();
211 145
212 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 146 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
213 GURL landing_page_3(kLandingPage_3); 147 GURL landing_page_3(kLandingPage_3);
214 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 148 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
215 6, 7, "Google", kProductName_3, "008", landing_page_3)); 149 6, 7, "Google", kProductName_3, "008", landing_page_3));
216 std::string guid_3 = device_3->guid(); 150 std::string guid_3 = device_3->guid();
217 151
218 EXPECT_CALL(mock_webusb_browser_client_, 152 // Three usb devices were added and removed before web_usb_detector was
219 OnDeviceAdded(product_name_1, landing_page_1, guid_1)) 153 // created.
220 .Times(0);
221 EXPECT_CALL(mock_webusb_browser_client_,
222 OnDeviceAdded(product_name_2, landing_page_2, guid_2))
223 .Times(0);
224 EXPECT_CALL(mock_webusb_browser_client_,
225 OnDeviceAdded(product_name_3, landing_page_3, guid_3))
226 .Times(0);
227
228 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_1)).Times(0);
229 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_2)).Times(0);
230 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_3)).Times(0);
231
232 // three usb devices were added and removed before webusb_detector was
233 // created
234 device_client_.usb_service()->AddDevice(device_1); 154 device_client_.usb_service()->AddDevice(device_1);
155 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
235 device_client_.usb_service()->AddDevice(device_2); 156 device_client_.usb_service()->AddDevice(device_2);
157 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
236 device_client_.usb_service()->AddDevice(device_3); 158 device_client_.usb_service()->AddDevice(device_3);
159 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
237 160
238 device_client_.usb_service()->RemoveDevice(device_1); 161 device_client_.usb_service()->RemoveDevice(device_1);
162 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
239 device_client_.usb_service()->RemoveDevice(device_2); 163 device_client_.usb_service()->RemoveDevice(device_2);
164 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
240 device_client_.usb_service()->RemoveDevice(device_3); 165 device_client_.usb_service()->RemoveDevice(device_3);
166 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
241 167
242 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_); 168 WebUsbDetector web_usb_detector;
243 } 169 }
244 170
245 TEST_F( 171 TEST_F(
246 WebUsbDetectorTest, 172 WebUsbDetectorTest,
247 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) { 173 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) {
248 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 174 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
249 GURL landing_page_1(kLandingPage_1); 175 GURL landing_page_1(kLandingPage_1);
250 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 176 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
251 0, 1, "Google", kProductName_1, "002", landing_page_1)); 177 0, 1, "Google", kProductName_1, "002", landing_page_1));
252 std::string guid_1 = device_1->guid(); 178 std::string guid_1 = device_1->guid();
253 179
254 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 180 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
255 GURL landing_page_2(kLandingPage_2); 181 GURL landing_page_2(kLandingPage_2);
256 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 182 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
257 3, 4, "Google", kProductName_2, "005", landing_page_2)); 183 3, 4, "Google", kProductName_2, "005", landing_page_2));
258 std::string guid_2 = device_2->guid(); 184 std::string guid_2 = device_2->guid();
259 185
260 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 186 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
261 GURL landing_page_3(kLandingPage_3); 187 GURL landing_page_3(kLandingPage_3);
262 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 188 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
263 6, 7, "Google", kProductName_3, "008", landing_page_3)); 189 6, 7, "Google", kProductName_3, "008", landing_page_3));
264 std::string guid_3 = device_3->guid(); 190 std::string guid_3 = device_3->guid();
265 191
266 EXPECT_CALL(mock_webusb_browser_client_, 192 // Three usb devices were added before web_usb_detector was created.
267 OnDeviceAdded(product_name_1, landing_page_1, guid_1)) 193 device_client_.usb_service()->AddDevice(device_1);
268 .Times(0); 194 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
269 EXPECT_CALL(mock_webusb_browser_client_, 195 device_client_.usb_service()->AddDevice(device_2);
270 OnDeviceAdded(product_name_2, landing_page_2, guid_2)) 196 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
271 .Times(0); 197 device_client_.usb_service()->AddDevice(device_3);
272 EXPECT_CALL(mock_webusb_browser_client_, 198 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
273 OnDeviceAdded(product_name_3, landing_page_3, guid_3))
274 .Times(0);
275 199
276 { 200 WebUsbDetector web_usb_detector;
277 testing::InSequence s;
278
279 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_1)).Times(1);
280 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_2)).Times(1);
281 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_3)).Times(1);
282 }
283
284 // three usb devices were added before webusb_detector was created
285 device_client_.usb_service()->AddDevice(device_1);
286 device_client_.usb_service()->AddDevice(device_2);
287 device_client_.usb_service()->AddDevice(device_3);
288
289 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
290 201
291 device_client_.usb_service()->RemoveDevice(device_1); 202 device_client_.usb_service()->RemoveDevice(device_1);
203 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
292 device_client_.usb_service()->RemoveDevice(device_2); 204 device_client_.usb_service()->RemoveDevice(device_2);
205 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
293 device_client_.usb_service()->RemoveDevice(device_3); 206 device_client_.usb_service()->RemoveDevice(device_3);
207 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
294 } 208 }
295 209
296 TEST_F(WebUsbDetectorTest, 210 TEST_F(WebUsbDetectorTest,
297 TwoUsbDevicesWereThereBeforeAndThenRemovedAndNewUsbDeviceAdded) { 211 TwoUsbDevicesWereThereBeforeAndThenRemovedAndNewUsbDeviceAdded) {
298 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 212 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
299 GURL landing_page_1(kLandingPage_1); 213 GURL landing_page_1(kLandingPage_1);
300 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 214 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
301 0, 1, "Google", kProductName_1, "002", landing_page_1)); 215 0, 1, "Google", kProductName_1, "002", landing_page_1));
302 std::string guid_1 = device_1->guid(); 216 std::string guid_1 = device_1->guid();
303 217
304 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 218 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
305 GURL landing_page_2(kLandingPage_2); 219 GURL landing_page_2(kLandingPage_2);
306 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 220 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
307 3, 4, "Google", kProductName_2, "005", landing_page_2)); 221 3, 4, "Google", kProductName_2, "005", landing_page_2));
308 std::string guid_2 = device_2->guid(); 222 std::string guid_2 = device_2->guid();
309 223
310 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 224 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
311 GURL landing_page_3(kLandingPage_3); 225 GURL landing_page_3(kLandingPage_3);
312 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 226 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
313 6, 7, "Google", kProductName_3, "008", landing_page_3)); 227 6, 7, "Google", kProductName_3, "008", landing_page_3));
314 std::string guid_3 = device_3->guid(); 228 std::string guid_3 = device_3->guid();
315 229
316 EXPECT_CALL(mock_webusb_browser_client_, 230 // Two usb devices were added before web_usb_detector was created.
317 OnDeviceAdded(product_name_1, landing_page_1, guid_1)) 231 device_client_.usb_service()->AddDevice(device_1);
318 .Times(0); 232 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
319 EXPECT_CALL(mock_webusb_browser_client_, 233 device_client_.usb_service()->AddDevice(device_3);
320 OnDeviceAdded(product_name_3, landing_page_3, guid_3)) 234 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
321 .Times(0);
322 235
323 { 236 WebUsbDetector web_usb_detector;
324 testing::InSequence s;
325
326 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_1)).Times(1);
327 EXPECT_CALL(mock_webusb_browser_client_,
328 OnDeviceAdded(product_name_2, landing_page_2, guid_2))
329 .Times(1);
330 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_3)).Times(1);
331 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_2)).Times(1);
332 }
333
334 // two usb devices were added before webusb_detector was created
335 device_client_.usb_service()->AddDevice(device_1);
336 device_client_.usb_service()->AddDevice(device_3);
337
338 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
339 237
340 device_client_.usb_service()->RemoveDevice(device_1); 238 device_client_.usb_service()->RemoveDevice(device_1);
239 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
240
341 device_client_.usb_service()->AddDevice(device_2); 241 device_client_.usb_service()->AddDevice(device_2);
242 message_center::Notification* notification =
243 message_center_->FindVisibleNotificationById(guid_2);
244 ASSERT_TRUE(notification != nullptr);
245 base::string16 expected_title =
246 base::ASCIIToUTF16("Google Product B detected");
247 EXPECT_EQ(expected_title, notification->title());
248 base::string16 expected_message =
249 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
250 EXPECT_EQ(expected_message, notification->message());
251 EXPECT_TRUE(notification->delegate() != nullptr);
252
342 device_client_.usb_service()->RemoveDevice(device_3); 253 device_client_.usb_service()->RemoveDevice(device_3);
254 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
255
343 device_client_.usb_service()->RemoveDevice(device_2); 256 device_client_.usb_service()->RemoveDevice(device_2);
257 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
344 } 258 }
345 259
346 TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) { 260 TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
347 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 261 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
348 GURL landing_page_1(kLandingPage_1); 262 GURL landing_page_1(kLandingPage_1);
349 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 263 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
350 0, 1, "Google", kProductName_1, "002", landing_page_1)); 264 0, 1, "Google", kProductName_1, "002", landing_page_1));
351 std::string guid_1 = device_1->guid(); 265 std::string guid_1 = device_1->guid();
352 266
353 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 267 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
354 GURL landing_page_2(kLandingPage_2); 268 GURL landing_page_2(kLandingPage_2);
355 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 269 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
356 3, 4, "Google", kProductName_2, "005", landing_page_2)); 270 3, 4, "Google", kProductName_2, "005", landing_page_2));
357 std::string guid_2 = device_2->guid(); 271 std::string guid_2 = device_2->guid();
358 272
359 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 273 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
360 GURL landing_page_3(kLandingPage_3); 274 GURL landing_page_3(kLandingPage_3);
361 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 275 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
362 6, 7, "Google", kProductName_3, "008", landing_page_3)); 276 6, 7, "Google", kProductName_3, "008", landing_page_3));
363 std::string guid_3 = device_3->guid(); 277 std::string guid_3 = device_3->guid();
364 278
365 { 279 WebUsbDetector web_usb_detector;
366 testing::InSequence s;
367
368 EXPECT_CALL(mock_webusb_browser_client_,
369 OnDeviceAdded(product_name_1, landing_page_1, guid_1))
370 .Times(1);
371 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_1)).Times(1);
372
373 EXPECT_CALL(mock_webusb_browser_client_,
374 OnDeviceAdded(product_name_2, landing_page_2, guid_2))
375 .Times(1);
376 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_2)).Times(1);
377
378 EXPECT_CALL(mock_webusb_browser_client_,
379 OnDeviceAdded(product_name_3, landing_page_3, guid_3))
380 .Times(1);
381 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_3)).Times(1);
382 }
383
384 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
385 280
386 device_client_.usb_service()->AddDevice(device_1); 281 device_client_.usb_service()->AddDevice(device_1);
282 message_center::Notification* notification_1 =
283 message_center_->FindVisibleNotificationById(guid_1);
284 ASSERT_TRUE(notification_1 != nullptr);
285 base::string16 expected_title_1 =
286 base::ASCIIToUTF16("Google Product A detected");
287 EXPECT_EQ(expected_title_1, notification_1->title());
288 base::string16 expected_message_1 =
289 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
290 EXPECT_EQ(expected_message_1, notification_1->message());
291 EXPECT_TRUE(notification_1->delegate() != nullptr);
292
387 device_client_.usb_service()->RemoveDevice(device_1); 293 device_client_.usb_service()->RemoveDevice(device_1);
294 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
295
388 device_client_.usb_service()->AddDevice(device_2); 296 device_client_.usb_service()->AddDevice(device_2);
297 message_center::Notification* notification_2 =
298 message_center_->FindVisibleNotificationById(guid_2);
299 ASSERT_TRUE(notification_2 != nullptr);
300 base::string16 expected_title_2 =
301 base::ASCIIToUTF16("Google Product B detected");
302 EXPECT_EQ(expected_title_2, notification_2->title());
303 base::string16 expected_message_2 =
304 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
305 EXPECT_EQ(expected_message_2, notification_2->message());
306 EXPECT_TRUE(notification_2->delegate() != nullptr);
307
389 device_client_.usb_service()->RemoveDevice(device_2); 308 device_client_.usb_service()->RemoveDevice(device_2);
309 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
310
390 device_client_.usb_service()->AddDevice(device_3); 311 device_client_.usb_service()->AddDevice(device_3);
312 message_center::Notification* notification_3 =
313 message_center_->FindVisibleNotificationById(guid_3);
314 ASSERT_TRUE(notification_3 != nullptr);
315 base::string16 expected_title_3 =
316 base::ASCIIToUTF16("Google Product C detected");
317 EXPECT_EQ(expected_title_3, notification_3->title());
318 base::string16 expected_message_3 =
319 base::ASCIIToUTF16("Go to www.google.com/C to connect.");
320 EXPECT_EQ(expected_message_3, notification_3->message());
321 EXPECT_TRUE(notification_3->delegate() != nullptr);
322
391 device_client_.usb_service()->RemoveDevice(device_3); 323 device_client_.usb_service()->RemoveDevice(device_3);
324 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
392 } 325 }
393 326
394 TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) { 327 TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
395 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 328 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
396 GURL landing_page_1(kLandingPage_1); 329 GURL landing_page_1(kLandingPage_1);
397 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 330 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
398 0, 1, "Google", kProductName_1, "002", landing_page_1)); 331 0, 1, "Google", kProductName_1, "002", landing_page_1));
399 std::string guid_1 = device_1->guid(); 332 std::string guid_1 = device_1->guid();
400 333
401 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 334 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
402 GURL landing_page_2(kLandingPage_2); 335 GURL landing_page_2(kLandingPage_2);
403 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 336 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
404 3, 4, "Google", kProductName_2, "005", landing_page_2)); 337 3, 4, "Google", kProductName_2, "005", landing_page_2));
405 std::string guid_2 = device_2->guid(); 338 std::string guid_2 = device_2->guid();
406 339
407 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 340 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
408 GURL landing_page_3(kLandingPage_3); 341 GURL landing_page_3(kLandingPage_3);
409 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 342 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
410 6, 7, "Google", kProductName_3, "008", landing_page_3)); 343 6, 7, "Google", kProductName_3, "008", landing_page_3));
411 std::string guid_3 = device_3->guid(); 344 std::string guid_3 = device_3->guid();
412 345
413 { 346 WebUsbDetector web_usb_detector;
414 testing::InSequence s;
415
416 EXPECT_CALL(mock_webusb_browser_client_,
417 OnDeviceAdded(product_name_1, landing_page_1, guid_1))
418 .Times(1);
419
420 EXPECT_CALL(mock_webusb_browser_client_,
421 OnDeviceAdded(product_name_2, landing_page_2, guid_2))
422 .Times(1);
423
424 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_2)).Times(1);
425
426 EXPECT_CALL(mock_webusb_browser_client_,
427 OnDeviceAdded(product_name_3, landing_page_3, guid_3))
428 .Times(1);
429
430 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_1)).Times(1);
431
432 EXPECT_CALL(mock_webusb_browser_client_, OnDeviceRemoved(guid_3)).Times(1);
433 }
434
435 webusb::WebUsbDetector webusb_detector(&mock_webusb_browser_client_);
436 347
437 device_client_.usb_service()->AddDevice(device_1); 348 device_client_.usb_service()->AddDevice(device_1);
349 message_center::Notification* notification_1 =
350 message_center_->FindVisibleNotificationById(guid_1);
351 ASSERT_TRUE(notification_1 != nullptr);
352 base::string16 expected_title_1 =
353 base::ASCIIToUTF16("Google Product A detected");
354 EXPECT_EQ(expected_title_1, notification_1->title());
355 base::string16 expected_message_1 =
356 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
357 EXPECT_EQ(expected_message_1, notification_1->message());
358 EXPECT_TRUE(notification_1->delegate() != nullptr);
359
438 device_client_.usb_service()->AddDevice(device_2); 360 device_client_.usb_service()->AddDevice(device_2);
361 message_center::Notification* notification_2 =
362 message_center_->FindVisibleNotificationById(guid_2);
363 ASSERT_TRUE(notification_2 != nullptr);
364 base::string16 expected_title_2 =
365 base::ASCIIToUTF16("Google Product B detected");
366 EXPECT_EQ(expected_title_2, notification_2->title());
367 base::string16 expected_message_2 =
368 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
369 EXPECT_EQ(expected_message_2, notification_2->message());
370 EXPECT_TRUE(notification_2->delegate() != nullptr);
371
439 device_client_.usb_service()->RemoveDevice(device_2); 372 device_client_.usb_service()->RemoveDevice(device_2);
373 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2));
374
440 device_client_.usb_service()->AddDevice(device_3); 375 device_client_.usb_service()->AddDevice(device_3);
376 message_center::Notification* notification_3 =
377 message_center_->FindVisibleNotificationById(guid_3);
378 ASSERT_TRUE(notification_3 != nullptr);
379 base::string16 expected_title_3 =
380 base::ASCIIToUTF16("Google Product C detected");
381 EXPECT_EQ(expected_title_3, notification_3->title());
382 base::string16 expected_message_3 =
383 base::ASCIIToUTF16("Go to www.google.com/C to connect.");
384 EXPECT_EQ(expected_message_3, notification_3->message());
385 EXPECT_TRUE(notification_3->delegate() != nullptr);
386
441 device_client_.usb_service()->RemoveDevice(device_1); 387 device_client_.usb_service()->RemoveDevice(device_1);
388 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1));
389
442 device_client_.usb_service()->RemoveDevice(device_3); 390 device_client_.usb_service()->RemoveDevice(device_3);
391 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3));
443 } 392 }
444
445 } // namespace webusb
OLDNEW
« no previous file with comments | « chrome/browser/usb/web_usb_detector.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698