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

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

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

Powered by Google App Engine
This is Rietveld 408576698