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

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

Issue 2824923002: Suppress WebUSB notifications when appropriate (Closed)
Patch Set: Test histograms Created 3 years, 7 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
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 <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 "base/test/histogram_tester.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/ui/browser_list.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/usb/web_usb_detector.h" 14 #include "chrome/browser/usb/web_usb_detector.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile_manager.h"
11 #include "device/base/mock_device_client.h" 18 #include "device/base/mock_device_client.h"
12 #include "device/usb/mock_usb_device.h" 19 #include "device/usb/mock_usb_device.h"
13 #include "device/usb/mock_usb_service.h" 20 #include "device/usb/mock_usb_service.h"
14 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/message_center/message_center.h" 22 #include "ui/message_center/message_center.h"
16 #include "ui/message_center/notification.h" 23 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notification_delegate.h" 24 #include "ui/message_center/notification_delegate.h"
18 #include "url/gurl.h" 25 #include "url/gurl.h"
19 26
20 // These tests are disabled because WebUsbDetector::Initialize is a noop on 27 // These tests are disabled because WebUsbDetector::Initialize is a noop on
21 // Windows due to jank and hangs caused by enumerating devices. 28 // Windows due to jank and hangs caused by enumerating devices.
22 // https://crbug.com/656702 29 // https://crbug.com/656702
23 #if !defined(OS_WIN) 30 #if !defined(OS_WIN)
24 namespace { 31 namespace {
25 32
26 // USB device product name. 33 // USB device product name.
27 const char* kProductName_1 = "Google Product A"; 34 const char* kProductName_1 = "Google Product A";
28 const char* kProductName_2 = "Google Product B"; 35 const char* kProductName_2 = "Google Product B";
29 const char* kProductName_3 = "Google Product C"; 36 const char* kProductName_3 = "Google Product C";
30 37
31 // USB device landing page. 38 // USB device landing page.
32 const char* kLandingPage_1 = "https://www.google.com/A"; 39 const char* kLandingPage_1 = "https://www.google.com/A";
33 const char* kLandingPage_2 = "https://www.google.com/B"; 40 const char* kLandingPage_2 = "https://www.google.com/B";
34 const char* kLandingPage_3 = "https://www.google.com/C"; 41 const char* kLandingPage_3 = "https://www.google.com/C";
35 42
36 } // namespace 43 } // namespace
37 44
38 class WebUsbDetectorTest : public testing::Test { 45 class WebUsbDetectorTest : public BrowserWithTestWindowTest {
39 public: 46 public:
40 WebUsbDetectorTest() {} 47 WebUsbDetectorTest() : profile_manager_(TestingBrowserProcess::GetGlobal()) {}
41 ~WebUsbDetectorTest() override = default; 48 ~WebUsbDetectorTest() override = default;
42 49
50 // Use the profile_manager_'s profile so that we can manage which one is most
51 // recently active.
52 TestingProfile* CreateProfile() override {
53 return profile_manager_.CreateTestingProfile("test@example.com");
54 }
55
56 // Since the profile is owned by profile_manager_, we do not need to destroy
57 // it.
58 void DestroyProfile(TestingProfile* profile) override {}
59
43 void SetUp() override { 60 void SetUp() override {
44 message_center::MessageCenter::Initialize(); 61 message_center::MessageCenter::Initialize();
45 message_center_ = message_center::MessageCenter::Get(); 62 message_center_ = message_center::MessageCenter::Get();
46 ASSERT_TRUE(message_center_ != nullptr); 63 ASSERT_TRUE(message_center_ != nullptr);
64
65 ASSERT_TRUE(profile_manager_.SetUp());
66 BrowserWithTestWindowTest::SetUp();
67 profile_manager_.UpdateLastUser(profile());
68 BrowserList::SetLastActive(browser());
69
70 web_usb_detector_.reset(new WebUsbDetector());
47 } 71 }
48 72
49 void TearDown() override { message_center::MessageCenter::Shutdown(); } 73 void TearDown() override {
74 BrowserWithTestWindowTest::TearDown();
75 message_center::MessageCenter::Shutdown();
76 web_usb_detector_.reset(nullptr);
77 }
78
79 void Initialize() { web_usb_detector_->Initialize(); }
50 80
51 protected: 81 protected:
52 device::MockDeviceClient device_client_; 82 device::MockDeviceClient device_client_;
53 message_center::MessageCenter* message_center_; 83 message_center::MessageCenter* message_center_;
54 84
55 private: 85 private:
56 DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); 86 DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest);
87 std::unique_ptr<WebUsbDetector> web_usb_detector_;
88 TestingProfileManager profile_manager_;
57 }; 89 };
58 90
59 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { 91 TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) {
60 base::string16 product_name = base::UTF8ToUTF16(kProductName_1); 92 base::string16 product_name = base::UTF8ToUTF16(kProductName_1);
61 GURL landing_page(kLandingPage_1); 93 GURL landing_page(kLandingPage_1);
62 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 94 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
63 0, 1, "Google", kProductName_1, "002", landing_page)); 95 0, 1, "Google", kProductName_1, "002", landing_page));
64 std::string guid = device->guid(); 96 std::string guid = device->guid();
65 97
66 WebUsbDetector web_usb_detector; 98 Initialize();
67 web_usb_detector.Initialize();
68 99
69 device_client_.usb_service()->AddDevice(device); 100 device_client_.usb_service()->AddDevice(device);
70 message_center::Notification* notification = 101 message_center::Notification* notification =
71 message_center_->FindVisibleNotificationById(guid); 102 message_center_->FindVisibleNotificationById(guid);
72 ASSERT_TRUE(notification != nullptr); 103 ASSERT_TRUE(notification != nullptr);
73 base::string16 expected_title = 104 base::string16 expected_title =
74 base::ASCIIToUTF16("Google Product A detected"); 105 base::ASCIIToUTF16("Google Product A detected");
75 EXPECT_EQ(expected_title, notification->title()); 106 EXPECT_EQ(expected_title, notification->title());
76 base::string16 expected_message = 107 base::string16 expected_message =
77 base::ASCIIToUTF16("Go to www.google.com/A to connect."); 108 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
78 EXPECT_EQ(expected_message, notification->message()); 109 EXPECT_EQ(expected_message, notification->message());
79 EXPECT_TRUE(notification->delegate() != nullptr); 110 EXPECT_TRUE(notification->delegate() != nullptr);
80 111
81 device_client_.usb_service()->RemoveDevice(device); 112 device_client_.usb_service()->RemoveDevice(device);
82 // Device is removed, so notification should be removed from the 113 // Device is removed, so notification should be removed from the
83 // message_center too. 114 // message_center too.
84 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 115 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
85 } 116 }
86 117
87 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { 118 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) {
88 std::string product_name = ""; 119 std::string product_name = "";
89 GURL landing_page(kLandingPage_1); 120 GURL landing_page(kLandingPage_1);
90 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 121 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
91 0, 1, "Google", product_name, "002", landing_page)); 122 0, 1, "Google", product_name, "002", landing_page));
92 std::string guid = device->guid(); 123 std::string guid = device->guid();
93 124
94 WebUsbDetector web_usb_detector; 125 Initialize();
95 web_usb_detector.Initialize();
96 126
97 device_client_.usb_service()->AddDevice(device); 127 device_client_.usb_service()->AddDevice(device);
98 // For device without product name, no notification is generated. 128 // For device without product name, no notification is generated.
99 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 129 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
100 130
101 device_client_.usb_service()->RemoveDevice(device); 131 device_client_.usb_service()->RemoveDevice(device);
102 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 132 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
103 } 133 }
104 134
105 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { 135 TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) {
106 GURL landing_page(""); 136 GURL landing_page("");
107 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 137 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
108 0, 1, "Google", kProductName_1, "002", landing_page)); 138 0, 1, "Google", kProductName_1, "002", landing_page));
109 std::string guid = device->guid(); 139 std::string guid = device->guid();
110 140
111 WebUsbDetector web_usb_detector; 141 Initialize();
112 web_usb_detector.Initialize();
113 142
114 device_client_.usb_service()->AddDevice(device); 143 device_client_.usb_service()->AddDevice(device);
115 // For device without landing page, no notification is generated. 144 // For device without landing page, no notification is generated.
116 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 145 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
117 146
118 device_client_.usb_service()->RemoveDevice(device); 147 device_client_.usb_service()->RemoveDevice(device);
119 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 148 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
120 } 149 }
121 150
122 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { 151 TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) {
123 GURL landing_page(kLandingPage_1); 152 GURL landing_page(kLandingPage_1);
124 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( 153 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice(
125 0, 1, "Google", kProductName_1, "002", landing_page)); 154 0, 1, "Google", kProductName_1, "002", landing_page));
126 std::string guid = device->guid(); 155 std::string guid = device->guid();
127 156
128 // USB device was added before web_usb_detector was created. 157 // USB device was added before web_usb_detector was created.
129 device_client_.usb_service()->AddDevice(device); 158 device_client_.usb_service()->AddDevice(device);
130 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 159 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
131 160
132 WebUsbDetector web_usb_detector; 161 Initialize();
133 web_usb_detector.Initialize();
134 162
135 device_client_.usb_service()->RemoveDevice(device); 163 device_client_.usb_service()->RemoveDevice(device);
136 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid)); 164 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid) == nullptr);
137 } 165 }
138 166
139 TEST_F( 167 TEST_F(
140 WebUsbDetectorTest, 168 WebUsbDetectorTest,
141 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated) { 169 ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated) {
142 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 170 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
143 GURL landing_page_1(kLandingPage_1); 171 GURL landing_page_1(kLandingPage_1);
144 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 172 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
145 0, 1, "Google", kProductName_1, "002", landing_page_1)); 173 0, 1, "Google", kProductName_1, "002", landing_page_1));
146 std::string guid_1 = device_1->guid(); 174 std::string guid_1 = device_1->guid();
147 175
148 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 176 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
149 GURL landing_page_2(kLandingPage_2); 177 GURL landing_page_2(kLandingPage_2);
150 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 178 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
151 3, 4, "Google", kProductName_2, "005", landing_page_2)); 179 3, 4, "Google", kProductName_2, "005", landing_page_2));
152 std::string guid_2 = device_2->guid(); 180 std::string guid_2 = device_2->guid();
153 181
154 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 182 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
155 GURL landing_page_3(kLandingPage_3); 183 GURL landing_page_3(kLandingPage_3);
156 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 184 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
157 6, 7, "Google", kProductName_3, "008", landing_page_3)); 185 6, 7, "Google", kProductName_3, "008", landing_page_3));
158 std::string guid_3 = device_3->guid(); 186 std::string guid_3 = device_3->guid();
159 187
160 // Three usb devices were added and removed before web_usb_detector was 188 // Three usb devices were added and removed before web_usb_detector was
161 // created. 189 // created.
162 device_client_.usb_service()->AddDevice(device_1); 190 device_client_.usb_service()->AddDevice(device_1);
163 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 191 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
164 device_client_.usb_service()->AddDevice(device_2); 192 device_client_.usb_service()->AddDevice(device_2);
165 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 193 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
166 device_client_.usb_service()->AddDevice(device_3); 194 device_client_.usb_service()->AddDevice(device_3);
167 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 195 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
168 196
169 device_client_.usb_service()->RemoveDevice(device_1); 197 device_client_.usb_service()->RemoveDevice(device_1);
170 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 198 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
171 device_client_.usb_service()->RemoveDevice(device_2); 199 device_client_.usb_service()->RemoveDevice(device_2);
172 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 200 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
173 device_client_.usb_service()->RemoveDevice(device_3); 201 device_client_.usb_service()->RemoveDevice(device_3);
174 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 202 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
175 203
176 WebUsbDetector web_usb_detector; 204 WebUsbDetector web_usb_detector;
177 web_usb_detector.Initialize(); 205 web_usb_detector.Initialize();
178 } 206 }
179 207
180 TEST_F( 208 TEST_F(
181 WebUsbDetectorTest, 209 WebUsbDetectorTest,
182 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) { 210 ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) {
183 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 211 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
184 GURL landing_page_1(kLandingPage_1); 212 GURL landing_page_1(kLandingPage_1);
185 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 213 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
186 0, 1, "Google", kProductName_1, "002", landing_page_1)); 214 0, 1, "Google", kProductName_1, "002", landing_page_1));
187 std::string guid_1 = device_1->guid(); 215 std::string guid_1 = device_1->guid();
188 216
189 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 217 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
190 GURL landing_page_2(kLandingPage_2); 218 GURL landing_page_2(kLandingPage_2);
191 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 219 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
192 3, 4, "Google", kProductName_2, "005", landing_page_2)); 220 3, 4, "Google", kProductName_2, "005", landing_page_2));
193 std::string guid_2 = device_2->guid(); 221 std::string guid_2 = device_2->guid();
194 222
195 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 223 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
196 GURL landing_page_3(kLandingPage_3); 224 GURL landing_page_3(kLandingPage_3);
197 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 225 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
198 6, 7, "Google", kProductName_3, "008", landing_page_3)); 226 6, 7, "Google", kProductName_3, "008", landing_page_3));
199 std::string guid_3 = device_3->guid(); 227 std::string guid_3 = device_3->guid();
200 228
201 // Three usb devices were added before web_usb_detector was created. 229 // Three usb devices were added before web_usb_detector was created.
202 device_client_.usb_service()->AddDevice(device_1); 230 device_client_.usb_service()->AddDevice(device_1);
203 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 231 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
204 device_client_.usb_service()->AddDevice(device_2); 232 device_client_.usb_service()->AddDevice(device_2);
205 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 233 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
206 device_client_.usb_service()->AddDevice(device_3); 234 device_client_.usb_service()->AddDevice(device_3);
207 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 235 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
208 236
209 WebUsbDetector web_usb_detector; 237 Initialize();
210 web_usb_detector.Initialize();
211 238
212 device_client_.usb_service()->RemoveDevice(device_1); 239 device_client_.usb_service()->RemoveDevice(device_1);
213 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 240 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
214 device_client_.usb_service()->RemoveDevice(device_2); 241 device_client_.usb_service()->RemoveDevice(device_2);
215 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 242 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
216 device_client_.usb_service()->RemoveDevice(device_3); 243 device_client_.usb_service()->RemoveDevice(device_3);
217 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 244 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
218 } 245 }
219 246
220 TEST_F(WebUsbDetectorTest, 247 TEST_F(WebUsbDetectorTest,
221 TwoUsbDevicesWereThereBeforeAndThenRemovedAndNewUsbDeviceAdded) { 248 TwoUsbDevicesWereThereBeforeAndThenRemovedAndNewUsbDeviceAdded) {
222 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 249 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
223 GURL landing_page_1(kLandingPage_1); 250 GURL landing_page_1(kLandingPage_1);
224 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 251 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
225 0, 1, "Google", kProductName_1, "002", landing_page_1)); 252 0, 1, "Google", kProductName_1, "002", landing_page_1));
226 std::string guid_1 = device_1->guid(); 253 std::string guid_1 = device_1->guid();
227 254
228 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 255 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
229 GURL landing_page_2(kLandingPage_2); 256 GURL landing_page_2(kLandingPage_2);
230 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 257 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
231 3, 4, "Google", kProductName_2, "005", landing_page_2)); 258 3, 4, "Google", kProductName_2, "005", landing_page_2));
232 std::string guid_2 = device_2->guid(); 259 std::string guid_2 = device_2->guid();
233 260
234 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 261 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
235 GURL landing_page_3(kLandingPage_3); 262 GURL landing_page_3(kLandingPage_3);
236 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 263 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
237 6, 7, "Google", kProductName_3, "008", landing_page_3)); 264 6, 7, "Google", kProductName_3, "008", landing_page_3));
238 std::string guid_3 = device_3->guid(); 265 std::string guid_3 = device_3->guid();
239 266
240 // Two usb devices were added before web_usb_detector was created. 267 // Two usb devices were added before web_usb_detector was created.
241 device_client_.usb_service()->AddDevice(device_1); 268 device_client_.usb_service()->AddDevice(device_1);
242 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 269 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
243 device_client_.usb_service()->AddDevice(device_3); 270 device_client_.usb_service()->AddDevice(device_3);
244 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 271 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
245 272
246 WebUsbDetector web_usb_detector; 273 Initialize();
247 web_usb_detector.Initialize();
248 274
249 device_client_.usb_service()->RemoveDevice(device_1); 275 device_client_.usb_service()->RemoveDevice(device_1);
250 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 276 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
251 277
252 device_client_.usb_service()->AddDevice(device_2); 278 device_client_.usb_service()->AddDevice(device_2);
253 message_center::Notification* notification = 279 message_center::Notification* notification =
254 message_center_->FindVisibleNotificationById(guid_2); 280 message_center_->FindVisibleNotificationById(guid_2);
255 ASSERT_TRUE(notification != nullptr); 281 ASSERT_TRUE(notification != nullptr);
256 base::string16 expected_title = 282 base::string16 expected_title =
257 base::ASCIIToUTF16("Google Product B detected"); 283 base::ASCIIToUTF16("Google Product B detected");
258 EXPECT_EQ(expected_title, notification->title()); 284 EXPECT_EQ(expected_title, notification->title());
259 base::string16 expected_message = 285 base::string16 expected_message =
260 base::ASCIIToUTF16("Go to www.google.com/B to connect."); 286 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
261 EXPECT_EQ(expected_message, notification->message()); 287 EXPECT_EQ(expected_message, notification->message());
262 EXPECT_TRUE(notification->delegate() != nullptr); 288 EXPECT_TRUE(notification->delegate() != nullptr);
263 289
264 device_client_.usb_service()->RemoveDevice(device_3); 290 device_client_.usb_service()->RemoveDevice(device_3);
265 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 291 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
266 292
267 device_client_.usb_service()->RemoveDevice(device_2); 293 device_client_.usb_service()->RemoveDevice(device_2);
268 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 294 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
269 } 295 }
270 296
271 TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) { 297 TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) {
272 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 298 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
273 GURL landing_page_1(kLandingPage_1); 299 GURL landing_page_1(kLandingPage_1);
274 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 300 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
275 0, 1, "Google", kProductName_1, "002", landing_page_1)); 301 0, 1, "Google", kProductName_1, "002", landing_page_1));
276 std::string guid_1 = device_1->guid(); 302 std::string guid_1 = device_1->guid();
277 303
278 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 304 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
279 GURL landing_page_2(kLandingPage_2); 305 GURL landing_page_2(kLandingPage_2);
280 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 306 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
281 3, 4, "Google", kProductName_2, "005", landing_page_2)); 307 3, 4, "Google", kProductName_2, "005", landing_page_2));
282 std::string guid_2 = device_2->guid(); 308 std::string guid_2 = device_2->guid();
283 309
284 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 310 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
285 GURL landing_page_3(kLandingPage_3); 311 GURL landing_page_3(kLandingPage_3);
286 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 312 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
287 6, 7, "Google", kProductName_3, "008", landing_page_3)); 313 6, 7, "Google", kProductName_3, "008", landing_page_3));
288 std::string guid_3 = device_3->guid(); 314 std::string guid_3 = device_3->guid();
289 315
290 WebUsbDetector web_usb_detector; 316 Initialize();
291 web_usb_detector.Initialize();
292 317
293 device_client_.usb_service()->AddDevice(device_1); 318 device_client_.usb_service()->AddDevice(device_1);
294 message_center::Notification* notification_1 = 319 message_center::Notification* notification_1 =
295 message_center_->FindVisibleNotificationById(guid_1); 320 message_center_->FindVisibleNotificationById(guid_1);
296 ASSERT_TRUE(notification_1 != nullptr); 321 ASSERT_TRUE(notification_1 != nullptr);
297 base::string16 expected_title_1 = 322 base::string16 expected_title_1 =
298 base::ASCIIToUTF16("Google Product A detected"); 323 base::ASCIIToUTF16("Google Product A detected");
299 EXPECT_EQ(expected_title_1, notification_1->title()); 324 EXPECT_EQ(expected_title_1, notification_1->title());
300 base::string16 expected_message_1 = 325 base::string16 expected_message_1 =
301 base::ASCIIToUTF16("Go to www.google.com/A to connect."); 326 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
302 EXPECT_EQ(expected_message_1, notification_1->message()); 327 EXPECT_EQ(expected_message_1, notification_1->message());
303 EXPECT_TRUE(notification_1->delegate() != nullptr); 328 EXPECT_TRUE(notification_1->delegate() != nullptr);
304 329
305 device_client_.usb_service()->RemoveDevice(device_1); 330 device_client_.usb_service()->RemoveDevice(device_1);
306 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 331 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
307 332
308 device_client_.usb_service()->AddDevice(device_2); 333 device_client_.usb_service()->AddDevice(device_2);
309 message_center::Notification* notification_2 = 334 message_center::Notification* notification_2 =
310 message_center_->FindVisibleNotificationById(guid_2); 335 message_center_->FindVisibleNotificationById(guid_2);
311 ASSERT_TRUE(notification_2 != nullptr); 336 ASSERT_TRUE(notification_2 != nullptr);
312 base::string16 expected_title_2 = 337 base::string16 expected_title_2 =
313 base::ASCIIToUTF16("Google Product B detected"); 338 base::ASCIIToUTF16("Google Product B detected");
314 EXPECT_EQ(expected_title_2, notification_2->title()); 339 EXPECT_EQ(expected_title_2, notification_2->title());
315 base::string16 expected_message_2 = 340 base::string16 expected_message_2 =
316 base::ASCIIToUTF16("Go to www.google.com/B to connect."); 341 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
317 EXPECT_EQ(expected_message_2, notification_2->message()); 342 EXPECT_EQ(expected_message_2, notification_2->message());
318 EXPECT_TRUE(notification_2->delegate() != nullptr); 343 EXPECT_TRUE(notification_2->delegate() != nullptr);
319 344
320 device_client_.usb_service()->RemoveDevice(device_2); 345 device_client_.usb_service()->RemoveDevice(device_2);
321 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 346 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
322 347
323 device_client_.usb_service()->AddDevice(device_3); 348 device_client_.usb_service()->AddDevice(device_3);
324 message_center::Notification* notification_3 = 349 message_center::Notification* notification_3 =
325 message_center_->FindVisibleNotificationById(guid_3); 350 message_center_->FindVisibleNotificationById(guid_3);
326 ASSERT_TRUE(notification_3 != nullptr); 351 ASSERT_TRUE(notification_3 != nullptr);
327 base::string16 expected_title_3 = 352 base::string16 expected_title_3 =
328 base::ASCIIToUTF16("Google Product C detected"); 353 base::ASCIIToUTF16("Google Product C detected");
329 EXPECT_EQ(expected_title_3, notification_3->title()); 354 EXPECT_EQ(expected_title_3, notification_3->title());
330 base::string16 expected_message_3 = 355 base::string16 expected_message_3 =
331 base::ASCIIToUTF16("Go to www.google.com/C to connect."); 356 base::ASCIIToUTF16("Go to www.google.com/C to connect.");
332 EXPECT_EQ(expected_message_3, notification_3->message()); 357 EXPECT_EQ(expected_message_3, notification_3->message());
333 EXPECT_TRUE(notification_3->delegate() != nullptr); 358 EXPECT_TRUE(notification_3->delegate() != nullptr);
334 359
335 device_client_.usb_service()->RemoveDevice(device_3); 360 device_client_.usb_service()->RemoveDevice(device_3);
336 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 361 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
337 } 362 }
338 363
339 TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) { 364 TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) {
340 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); 365 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1);
341 GURL landing_page_1(kLandingPage_1); 366 GURL landing_page_1(kLandingPage_1);
342 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( 367 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
343 0, 1, "Google", kProductName_1, "002", landing_page_1)); 368 0, 1, "Google", kProductName_1, "002", landing_page_1));
344 std::string guid_1 = device_1->guid(); 369 std::string guid_1 = device_1->guid();
345 370
346 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); 371 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2);
347 GURL landing_page_2(kLandingPage_2); 372 GURL landing_page_2(kLandingPage_2);
348 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( 373 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice(
349 3, 4, "Google", kProductName_2, "005", landing_page_2)); 374 3, 4, "Google", kProductName_2, "005", landing_page_2));
350 std::string guid_2 = device_2->guid(); 375 std::string guid_2 = device_2->guid();
351 376
352 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); 377 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3);
353 GURL landing_page_3(kLandingPage_3); 378 GURL landing_page_3(kLandingPage_3);
354 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( 379 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice(
355 6, 7, "Google", kProductName_3, "008", landing_page_3)); 380 6, 7, "Google", kProductName_3, "008", landing_page_3));
356 std::string guid_3 = device_3->guid(); 381 std::string guid_3 = device_3->guid();
357 382
358 WebUsbDetector web_usb_detector; 383 Initialize();
359 web_usb_detector.Initialize();
360 384
361 device_client_.usb_service()->AddDevice(device_1); 385 device_client_.usb_service()->AddDevice(device_1);
362 message_center::Notification* notification_1 = 386 message_center::Notification* notification_1 =
363 message_center_->FindVisibleNotificationById(guid_1); 387 message_center_->FindVisibleNotificationById(guid_1);
364 ASSERT_TRUE(notification_1 != nullptr); 388 ASSERT_TRUE(notification_1 != nullptr);
365 base::string16 expected_title_1 = 389 base::string16 expected_title_1 =
366 base::ASCIIToUTF16("Google Product A detected"); 390 base::ASCIIToUTF16("Google Product A detected");
367 EXPECT_EQ(expected_title_1, notification_1->title()); 391 EXPECT_EQ(expected_title_1, notification_1->title());
368 base::string16 expected_message_1 = 392 base::string16 expected_message_1 =
369 base::ASCIIToUTF16("Go to www.google.com/A to connect."); 393 base::ASCIIToUTF16("Go to www.google.com/A to connect.");
370 EXPECT_EQ(expected_message_1, notification_1->message()); 394 EXPECT_EQ(expected_message_1, notification_1->message());
371 EXPECT_TRUE(notification_1->delegate() != nullptr); 395 EXPECT_TRUE(notification_1->delegate() != nullptr);
372 396
373 device_client_.usb_service()->AddDevice(device_2); 397 device_client_.usb_service()->AddDevice(device_2);
374 message_center::Notification* notification_2 = 398 message_center::Notification* notification_2 =
375 message_center_->FindVisibleNotificationById(guid_2); 399 message_center_->FindVisibleNotificationById(guid_2);
376 ASSERT_TRUE(notification_2 != nullptr); 400 ASSERT_TRUE(notification_2 != nullptr);
377 base::string16 expected_title_2 = 401 base::string16 expected_title_2 =
378 base::ASCIIToUTF16("Google Product B detected"); 402 base::ASCIIToUTF16("Google Product B detected");
379 EXPECT_EQ(expected_title_2, notification_2->title()); 403 EXPECT_EQ(expected_title_2, notification_2->title());
380 base::string16 expected_message_2 = 404 base::string16 expected_message_2 =
381 base::ASCIIToUTF16("Go to www.google.com/B to connect."); 405 base::ASCIIToUTF16("Go to www.google.com/B to connect.");
382 EXPECT_EQ(expected_message_2, notification_2->message()); 406 EXPECT_EQ(expected_message_2, notification_2->message());
383 EXPECT_TRUE(notification_2->delegate() != nullptr); 407 EXPECT_TRUE(notification_2->delegate() != nullptr);
384 408
385 device_client_.usb_service()->RemoveDevice(device_2); 409 device_client_.usb_service()->RemoveDevice(device_2);
386 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_2)); 410 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_2) == nullptr);
387 411
388 device_client_.usb_service()->AddDevice(device_3); 412 device_client_.usb_service()->AddDevice(device_3);
389 message_center::Notification* notification_3 = 413 message_center::Notification* notification_3 =
390 message_center_->FindVisibleNotificationById(guid_3); 414 message_center_->FindVisibleNotificationById(guid_3);
391 ASSERT_TRUE(notification_3 != nullptr); 415 ASSERT_TRUE(notification_3 != nullptr);
392 base::string16 expected_title_3 = 416 base::string16 expected_title_3 =
393 base::ASCIIToUTF16("Google Product C detected"); 417 base::ASCIIToUTF16("Google Product C detected");
394 EXPECT_EQ(expected_title_3, notification_3->title()); 418 EXPECT_EQ(expected_title_3, notification_3->title());
395 base::string16 expected_message_3 = 419 base::string16 expected_message_3 =
396 base::ASCIIToUTF16("Go to www.google.com/C to connect."); 420 base::ASCIIToUTF16("Go to www.google.com/C to connect.");
397 EXPECT_EQ(expected_message_3, notification_3->message()); 421 EXPECT_EQ(expected_message_3, notification_3->message());
398 EXPECT_TRUE(notification_3->delegate() != nullptr); 422 EXPECT_TRUE(notification_3->delegate() != nullptr);
399 423
400 device_client_.usb_service()->RemoveDevice(device_1); 424 device_client_.usb_service()->RemoveDevice(device_1);
401 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_1)); 425 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
402 426
403 device_client_.usb_service()->RemoveDevice(device_3); 427 device_client_.usb_service()->RemoveDevice(device_3);
404 EXPECT_EQ(nullptr, message_center_->FindVisibleNotificationById(guid_3)); 428 EXPECT_TRUE(message_center_->FindVisibleNotificationById(guid_3) == nullptr);
405 } 429 }
430
431 TEST_F(WebUsbDetectorTest, UsbDeviceAddedWhileActiveTabUrlIsLandingPage) {
432 GURL landing_page_1(kLandingPage_1);
433 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
434 0, 1, "Google", kProductName_1, "002", landing_page_1));
435 std::string guid_1 = device_1->guid();
436
437 Initialize();
438
439 AddTab(browser(), landing_page_1);
440
441 device_client_.usb_service()->AddDevice(device_1);
442 ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
443 }
444
445 TEST_F(WebUsbDetectorTest, UsbDeviceAddedBeforeActiveTabUrlIsLandingPage) {
446 GURL landing_page_1(kLandingPage_1);
447 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
448 0, 1, "Google", kProductName_1, "002", landing_page_1));
449 std::string guid_1 = device_1->guid();
450
451 base::HistogramTester histogram_tester;
452 Initialize();
453
454 device_client_.usb_service()->AddDevice(device_1);
455 ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) != nullptr);
456
457 AddTab(browser(), landing_page_1);
458 ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
459 histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 3, 1);
460 }
461
462 TEST_F(WebUsbDetectorTest,
463 NotificationClickedWhileInactiveTabUrlIsLandingPage) {
464 GURL landing_page_1(kLandingPage_1);
465 GURL landing_page_2(kLandingPage_2);
466 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
467 0, 1, "Google", kProductName_1, "002", landing_page_1));
468 std::string guid_1 = device_1->guid();
469 TabStripModel* tab_strip_model = browser()->tab_strip_model();
470
471 base::HistogramTester histogram_tester;
472 Initialize();
473
474 AddTab(browser(), landing_page_1);
475 AddTab(browser(), landing_page_2);
476
477 device_client_.usb_service()->AddDevice(device_1);
478 message_center::Notification* notification_1 =
479 message_center_->FindVisibleNotificationById(guid_1);
480 ASSERT_TRUE(notification_1 != nullptr);
481 EXPECT_EQ(2, tab_strip_model->count());
482
483 notification_1->Click();
484 EXPECT_EQ(2, tab_strip_model->count());
485 content::WebContents* web_contents =
486 tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
487 EXPECT_EQ(landing_page_1, web_contents->GetURL());
488 ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
489 histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1);
490 }
491
492 TEST_F(WebUsbDetectorTest, NotificationClickedWhileNoTabUrlIsLandingPage) {
493 GURL landing_page_1(kLandingPage_1);
494 GURL landing_page_2(kLandingPage_2);
495 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice(
496 0, 1, "Google", kProductName_1, "002", landing_page_1));
497 std::string guid_1 = device_1->guid();
498 TabStripModel* tab_strip_model = browser()->tab_strip_model();
499
500 base::HistogramTester histogram_tester;
501 Initialize();
502
503 device_client_.usb_service()->AddDevice(device_1);
504 message_center::Notification* notification_1 =
505 message_center_->FindVisibleNotificationById(guid_1);
506 ASSERT_TRUE(notification_1 != nullptr);
507 EXPECT_EQ(0, tab_strip_model->count());
508
509 notification_1->Click();
510 EXPECT_EQ(1, tab_strip_model->count());
511 content::WebContents* web_contents =
512 tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
513 EXPECT_EQ(landing_page_1, web_contents->GetURL());
514 ASSERT_TRUE(message_center_->FindVisibleNotificationById(guid_1) == nullptr);
515 histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1);
516 }
517
406 #endif // !OS_WIN 518 #endif // !OS_WIN
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698