juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 5 | #include <memory> |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 6 | #include <string> |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 7 | #include <utility> |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 8 | |
| 9 | #include "base/macros.h" |
| 10 | #include "base/strings/string16.h" |
| 11 | #include "base/strings/utf_string_conversions.h" |
Devlin Cronin | 626d80c | 2018-06-01 01:08:36 | [diff] [blame] | 12 | #include "base/test/metrics/histogram_tester.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame^] | 13 | #include "build/chromeos_buildflags.h" |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/profiles/profile_helper.h" |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 15 | #include "chrome/browser/notifications/notification_display_service.h" |
| 16 | #include "chrome/browser/notifications/notification_display_service_tester.h" |
| 17 | #include "chrome/browser/notifications/system_notification_helper.h" |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile_manager.h" |
| 19 | #include "chrome/browser/ui/browser_list.h" |
| 20 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 21 | #include "chrome/browser/usb/web_usb_detector.h" |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 22 | #include "chrome/test/base/browser_with_test_window_test.h" |
| 23 | #include "chrome/test/base/testing_browser_process.h" |
| 24 | #include "chrome/test/base/testing_profile_manager.h" |
Donna Wu | 4dc9df3 | 2019-05-20 03:56:21 | [diff] [blame] | 25 | #include "services/device/public/cpp/test/fake_usb_device_info.h" |
| 26 | #include "services/device/public/cpp/test/fake_usb_device_manager.h" |
| 27 | #include "services/device/public/mojom/usb_device.mojom.h" |
| 28 | #include "services/device/public/mojom/usb_manager.mojom.h" |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
Evan Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 30 | #include "ui/message_center/public/cpp/notification.h" |
| 31 | #include "ui/message_center/public/cpp/notification_delegate.h" |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 32 | #include "url/gurl.h" |
| 33 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame^] | 34 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Evan Stade | 3a827a0 | 2019-08-16 23:13:53 | [diff] [blame] | 35 | #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 36 | #include "components/user_manager/scoped_user_manager.h" |
| 37 | #include "components/user_manager/user_manager.h" |
| 38 | #include "components/user_manager/user_names.h" |
| 39 | #endif |
| 40 | |
reillyg | dfd32df | 2016-10-18 02:42:16 | [diff] [blame] | 41 | // These tests are disabled because WebUsbDetector::Initialize is a noop on |
| 42 | // Windows due to jank and hangs caused by enumerating devices. |
| 43 | // https://crbug.com/656702 |
| 44 | #if !defined(OS_WIN) |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 45 | namespace { |
| 46 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 47 | const char* kProfileName = "[email protected]"; |
| 48 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 49 | // USB device product name. |
| 50 | const char* kProductName_1 = "Google Product A"; |
| 51 | const char* kProductName_2 = "Google Product B"; |
| 52 | const char* kProductName_3 = "Google Product C"; |
| 53 | |
| 54 | // USB device landing page. |
| 55 | const char* kLandingPage_1 = "https://www.google.com/A"; |
| 56 | const char* kLandingPage_2 = "https://www.google.com/B"; |
| 57 | const char* kLandingPage_3 = "https://www.google.com/C"; |
James Hollyer | c59c9c4 | 2018-12-20 03:39:06 | [diff] [blame] | 58 | const char* kLandingPage_1_fuzzed = "https://www.google.com/A/fuzzy"; |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 59 | |
| 60 | } // namespace |
| 61 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 62 | class WebUsbDetectorTest : public BrowserWithTestWindowTest { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 63 | public: |
Evan Stade | ef1543f | 2017-11-17 20:20:13 | [diff] [blame] | 64 | WebUsbDetectorTest() {} |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 65 | ~WebUsbDetectorTest() override = default; |
| 66 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 67 | TestingProfile* CreateProfile() override { |
Evan Stade | ef1543f | 2017-11-17 20:20:13 | [diff] [blame] | 68 | return profile_manager()->CreateTestingProfile(kProfileName); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 69 | } |
| 70 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 71 | void SetUp() override { |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 72 | BrowserWithTestWindowTest::SetUp(); |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame^] | 73 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Evan Stade | 3a827a0 | 2019-08-16 23:13:53 | [diff] [blame] | 74 | user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>( |
| 75 | std::make_unique<chromeos::FakeChromeUserManager>()); |
| 76 | |
| 77 | GetFakeUserManager()->AddUser(user_manager::StubAccountId()); |
| 78 | GetFakeUserManager()->LoginUser(user_manager::StubAccountId()); |
| 79 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 80 | chromeos::ProfileHelper::Get()->SetActiveUserIdForTesting(kProfileName); |
| 81 | #endif |
| 82 | BrowserList::SetLastActive(browser()); |
Toni Barzic | b598380e | 2018-11-26 19:14:10 | [diff] [blame] | 83 | TestingBrowserProcess::GetGlobal()->SetSystemNotificationHelper( |
| 84 | std::make_unique<SystemNotificationHelper>()); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 85 | display_service_ = std::make_unique<NotificationDisplayServiceTester>( |
Toni Barzic | b598380e | 2018-11-26 19:14:10 | [diff] [blame] | 86 | nullptr /* profile */); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 87 | |
| 88 | web_usb_detector_.reset(new WebUsbDetector()); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 89 | // Set a fake USB device manager before Initialize(). |
Miyoung Shin | 2dd36fa7 | 2019-08-29 03:57:00 | [diff] [blame] | 90 | mojo::PendingRemote<device::mojom::UsbDeviceManager> device_manager; |
| 91 | device_manager_.AddReceiver( |
| 92 | device_manager.InitWithNewPipeAndPassReceiver()); |
| 93 | web_usb_detector_->SetDeviceManagerForTesting(std::move(device_manager)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void TearDown() override { |
| 97 | BrowserWithTestWindowTest::TearDown(); |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame^] | 98 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Evan Stade | 3a827a0 | 2019-08-16 23:13:53 | [diff] [blame] | 99 | user_manager_enabler_.reset(); |
| 100 | #endif |
| 101 | web_usb_detector_.reset(); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void Initialize() { web_usb_detector_->Initialize(); } |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 105 | |
| 106 | protected: |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame^] | 107 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Evan Stade | 3a827a0 | 2019-08-16 23:13:53 | [diff] [blame] | 108 | chromeos::FakeChromeUserManager* GetFakeUserManager() { |
| 109 | return static_cast<chromeos::FakeChromeUserManager*>( |
| 110 | user_manager::UserManager::Get()); |
| 111 | } |
| 112 | |
| 113 | std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_; |
| 114 | #endif |
| 115 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 116 | device::FakeUsbDeviceManager device_manager_; |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 117 | std::unique_ptr<WebUsbDetector> web_usb_detector_; |
| 118 | std::unique_ptr<NotificationDisplayServiceTester> display_service_; |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | DISALLOW_COPY_AND_ASSIGN(WebUsbDetectorTest); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | TEST_F(WebUsbDetectorTest, UsbDeviceAddedAndRemoved) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 125 | GURL landing_page(kLandingPage_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 126 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 127 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 128 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 129 | auto device = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 130 | 0, 1, "Google", kProductName_1, "002", landing_page); |
| 131 | device_manager_.AddDevice(device); |
| 132 | base::RunLoop().RunUntilIdle(); |
| 133 | |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 134 | base::Optional<message_center::Notification> notification = |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 135 | display_service_->GetNotification(device->guid()); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 136 | ASSERT_TRUE(notification); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 137 | base::string16 expected_title = |
| 138 | base::ASCIIToUTF16("Google Product A detected"); |
| 139 | EXPECT_EQ(expected_title, notification->title()); |
| 140 | base::string16 expected_message = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 141 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 142 | EXPECT_EQ(expected_message, notification->message()); |
| 143 | EXPECT_TRUE(notification->delegate() != nullptr); |
| 144 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 145 | device_manager_.RemoveDevice(device); |
| 146 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 147 | // Device is removed, so notification should be removed too. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 148 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_F(WebUsbDetectorTest, UsbDeviceWithoutProductNameAddedAndRemoved) { |
Lei Zhang | 9b0be80 | 2020-05-05 21:11:48 | [diff] [blame] | 152 | std::string product_name; |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 153 | GURL landing_page(kLandingPage_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 154 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 155 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 156 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 157 | auto device = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 158 | 0, 1, "Google", product_name, "002", landing_page); |
| 159 | device_manager_.AddDevice(device); |
| 160 | base::RunLoop().RunUntilIdle(); |
| 161 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 162 | // For device without product name, no notification is generated. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 163 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 164 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 165 | device_manager_.RemoveDevice(device); |
| 166 | base::RunLoop().RunUntilIdle(); |
| 167 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(WebUsbDetectorTest, UsbDeviceWithoutLandingPageAddedAndRemoved) { |
| 171 | GURL landing_page(""); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 172 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 173 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 174 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 175 | auto device = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 176 | 0, 1, "Google", kProductName_1, "002", landing_page); |
| 177 | device_manager_.AddDevice(device); |
| 178 | base::RunLoop().RunUntilIdle(); |
| 179 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 180 | // For device without landing page, no notification is generated. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 181 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 182 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 183 | device_manager_.RemoveDevice(device); |
| 184 | base::RunLoop().RunUntilIdle(); |
| 185 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(WebUsbDetectorTest, UsbDeviceWasThereBeforeAndThenRemoved) { |
| 189 | GURL landing_page(kLandingPage_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 190 | |
| 191 | // USB device was added before web_usb_detector was created. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 192 | auto device = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 193 | 0, 1, "Google", kProductName_1, "002", landing_page); |
| 194 | device_manager_.AddDevice(device); |
| 195 | base::RunLoop().RunUntilIdle(); |
| 196 | |
| 197 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 198 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 199 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 200 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 201 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 202 | device_manager_.RemoveDevice(device); |
| 203 | base::RunLoop().RunUntilIdle(); |
| 204 | EXPECT_FALSE(display_service_->GetNotification(device->guid())); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | TEST_F( |
| 208 | WebUsbDetectorTest, |
| 209 | ThreeUsbDevicesWereThereBeforeAndThenRemovedBeforeWebUsbDetectorWasCreated) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 210 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 211 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 212 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 213 | std::string guid_1 = device_1->guid(); |
| 214 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 215 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 216 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 217 | 3, 4, "Google", kProductName_2, "005", landing_page_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 218 | std::string guid_2 = device_2->guid(); |
| 219 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 220 | GURL landing_page_3(kLandingPage_3); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 221 | auto device_3 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 222 | 6, 7, "Google", kProductName_3, "008", landing_page_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 223 | std::string guid_3 = device_3->guid(); |
| 224 | |
| 225 | // Three usb devices were added and removed before web_usb_detector was |
| 226 | // created. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 227 | device_manager_.AddDevice(device_1); |
| 228 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 229 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 230 | |
| 231 | device_manager_.AddDevice(device_2); |
| 232 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 233 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 234 | |
| 235 | device_manager_.AddDevice(device_3); |
| 236 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 237 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 238 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 239 | device_manager_.RemoveDevice(device_1); |
| 240 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 241 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 242 | |
| 243 | device_manager_.RemoveDevice(device_2); |
| 244 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 245 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 246 | |
| 247 | device_manager_.RemoveDevice(device_3); |
| 248 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 249 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 250 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 251 | Initialize(); |
| 252 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F( |
| 256 | WebUsbDetectorTest, |
| 257 | ThreeUsbDevicesWereThereBeforeAndThenRemovedAfterWebUsbDetectorWasCreated) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 258 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 259 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 260 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 261 | std::string guid_1 = device_1->guid(); |
| 262 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 263 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 264 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 265 | 3, 4, "Google", kProductName_2, "005", landing_page_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 266 | std::string guid_2 = device_2->guid(); |
| 267 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 268 | GURL landing_page_3(kLandingPage_3); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 269 | auto device_3 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 270 | 6, 7, "Google", kProductName_3, "008", landing_page_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 271 | std::string guid_3 = device_3->guid(); |
| 272 | |
| 273 | // Three usb devices were added before web_usb_detector was created. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 274 | device_manager_.AddDevice(device_1); |
| 275 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 276 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 277 | |
| 278 | device_manager_.AddDevice(device_2); |
| 279 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 280 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 281 | |
| 282 | device_manager_.AddDevice(device_3); |
| 283 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 284 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 285 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 286 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 287 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 288 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 289 | device_manager_.RemoveDevice(device_1); |
| 290 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 291 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 292 | |
| 293 | device_manager_.RemoveDevice(device_2); |
| 294 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 295 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 296 | |
| 297 | device_manager_.RemoveDevice(device_3); |
| 298 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 299 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | TEST_F(WebUsbDetectorTest, |
| 303 | TwoUsbDevicesWereThereBeforeAndThenRemovedAndNewUsbDeviceAdded) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 304 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 305 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 306 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 307 | std::string guid_1 = device_1->guid(); |
| 308 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 309 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 310 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 311 | 3, 4, "Google", kProductName_2, "005", landing_page_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 312 | std::string guid_2 = device_2->guid(); |
| 313 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 314 | // Two usb devices were added before web_usb_detector was created. |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 315 | device_manager_.AddDevice(device_1); |
| 316 | device_manager_.AddDevice(device_2); |
| 317 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 318 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 319 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 320 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 321 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 322 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 323 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 324 | device_manager_.RemoveDevice(device_1); |
| 325 | device_manager_.RemoveDevice(device_2); |
| 326 | base::RunLoop().RunUntilIdle(); |
| 327 | |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 328 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 329 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 330 | device_manager_.AddDevice(device_2); |
| 331 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 332 | base::Optional<message_center::Notification> notification = |
| 333 | display_service_->GetNotification(guid_2); |
| 334 | ASSERT_TRUE(notification); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 335 | base::string16 expected_title = |
| 336 | base::ASCIIToUTF16("Google Product B detected"); |
| 337 | EXPECT_EQ(expected_title, notification->title()); |
| 338 | base::string16 expected_message = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 339 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 340 | EXPECT_EQ(expected_message, notification->message()); |
| 341 | EXPECT_TRUE(notification->delegate() != nullptr); |
| 342 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 343 | device_manager_.RemoveDevice(device_2); |
| 344 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 345 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST_F(WebUsbDetectorTest, ThreeUsbDevicesAddedAndRemoved) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 349 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 350 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 351 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 352 | std::string guid_1 = device_1->guid(); |
| 353 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 354 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 355 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 356 | 3, 4, "Google", kProductName_2, "005", landing_page_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 357 | std::string guid_2 = device_2->guid(); |
| 358 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 359 | GURL landing_page_3(kLandingPage_3); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 360 | auto device_3 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 361 | 6, 7, "Google", kProductName_3, "008", landing_page_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 362 | std::string guid_3 = device_3->guid(); |
| 363 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 364 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 365 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 366 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 367 | device_manager_.AddDevice(device_1); |
| 368 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 369 | base::Optional<message_center::Notification> notification_1 = |
| 370 | display_service_->GetNotification(guid_1); |
| 371 | ASSERT_TRUE(notification_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 372 | base::string16 expected_title_1 = |
| 373 | base::ASCIIToUTF16("Google Product A detected"); |
| 374 | EXPECT_EQ(expected_title_1, notification_1->title()); |
| 375 | base::string16 expected_message_1 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 376 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 377 | EXPECT_EQ(expected_message_1, notification_1->message()); |
| 378 | EXPECT_TRUE(notification_1->delegate() != nullptr); |
| 379 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 380 | device_manager_.RemoveDevice(device_1); |
| 381 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 382 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 383 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 384 | device_manager_.AddDevice(device_2); |
| 385 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 386 | base::Optional<message_center::Notification> notification_2 = |
| 387 | display_service_->GetNotification(guid_2); |
| 388 | ASSERT_TRUE(notification_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 389 | base::string16 expected_title_2 = |
| 390 | base::ASCIIToUTF16("Google Product B detected"); |
| 391 | EXPECT_EQ(expected_title_2, notification_2->title()); |
| 392 | base::string16 expected_message_2 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 393 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 394 | EXPECT_EQ(expected_message_2, notification_2->message()); |
| 395 | EXPECT_TRUE(notification_2->delegate() != nullptr); |
| 396 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 397 | device_manager_.RemoveDevice(device_2); |
| 398 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 399 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 400 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 401 | device_manager_.AddDevice(device_3); |
| 402 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 403 | base::Optional<message_center::Notification> notification_3 = |
| 404 | display_service_->GetNotification(guid_3); |
| 405 | ASSERT_TRUE(notification_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 406 | base::string16 expected_title_3 = |
| 407 | base::ASCIIToUTF16("Google Product C detected"); |
| 408 | EXPECT_EQ(expected_title_3, notification_3->title()); |
| 409 | base::string16 expected_message_3 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 410 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 411 | EXPECT_EQ(expected_message_3, notification_3->message()); |
| 412 | EXPECT_TRUE(notification_3->delegate() != nullptr); |
| 413 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 414 | device_manager_.RemoveDevice(device_3); |
| 415 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 416 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | TEST_F(WebUsbDetectorTest, ThreeUsbDeviceAddedAndRemovedDifferentOrder) { |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 420 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 421 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 422 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 423 | std::string guid_1 = device_1->guid(); |
| 424 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 425 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 426 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 427 | 3, 4, "Google", kProductName_2, "005", landing_page_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 428 | std::string guid_2 = device_2->guid(); |
| 429 | |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 430 | GURL landing_page_3(kLandingPage_3); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 431 | auto device_3 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 432 | 6, 7, "Google", kProductName_3, "008", landing_page_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 433 | std::string guid_3 = device_3->guid(); |
| 434 | |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 435 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 436 | base::RunLoop().RunUntilIdle(); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 437 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 438 | device_manager_.AddDevice(device_1); |
| 439 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 440 | base::Optional<message_center::Notification> notification_1 = |
| 441 | display_service_->GetNotification(guid_1); |
| 442 | ASSERT_TRUE(notification_1); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 443 | base::string16 expected_title_1 = |
| 444 | base::ASCIIToUTF16("Google Product A detected"); |
| 445 | EXPECT_EQ(expected_title_1, notification_1->title()); |
| 446 | base::string16 expected_message_1 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 447 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 448 | EXPECT_EQ(expected_message_1, notification_1->message()); |
| 449 | EXPECT_TRUE(notification_1->delegate() != nullptr); |
| 450 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 451 | device_manager_.AddDevice(device_2); |
| 452 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 453 | base::Optional<message_center::Notification> notification_2 = |
| 454 | display_service_->GetNotification(guid_2); |
| 455 | ASSERT_TRUE(notification_2); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 456 | base::string16 expected_title_2 = |
| 457 | base::ASCIIToUTF16("Google Product B detected"); |
| 458 | EXPECT_EQ(expected_title_2, notification_2->title()); |
| 459 | base::string16 expected_message_2 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 460 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 461 | EXPECT_EQ(expected_message_2, notification_2->message()); |
| 462 | EXPECT_TRUE(notification_2->delegate() != nullptr); |
| 463 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 464 | device_manager_.RemoveDevice(device_2); |
| 465 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 466 | EXPECT_FALSE(display_service_->GetNotification(guid_2)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 467 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 468 | device_manager_.AddDevice(device_3); |
| 469 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 470 | base::Optional<message_center::Notification> notification_3 = |
| 471 | display_service_->GetNotification(guid_3); |
| 472 | ASSERT_TRUE(notification_3); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 473 | base::string16 expected_title_3 = |
| 474 | base::ASCIIToUTF16("Google Product C detected"); |
| 475 | EXPECT_EQ(expected_title_3, notification_3->title()); |
| 476 | base::string16 expected_message_3 = |
Reilly Grant | 85797cd | 2017-10-16 18:10:12 | [diff] [blame] | 477 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 478 | EXPECT_EQ(expected_message_3, notification_3->message()); |
| 479 | EXPECT_TRUE(notification_3->delegate() != nullptr); |
| 480 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 481 | device_manager_.RemoveDevice(device_1); |
| 482 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 483 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 484 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 485 | device_manager_.RemoveDevice(device_3); |
| 486 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 487 | EXPECT_FALSE(display_service_->GetNotification(guid_3)); |
juncai | 1da1203 | 2016-07-15 19:10:12 | [diff] [blame] | 488 | } |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 489 | |
| 490 | TEST_F(WebUsbDetectorTest, UsbDeviceAddedWhileActiveTabUrlIsLandingPage) { |
| 491 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 492 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 493 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 494 | std::string guid_1 = device_1->guid(); |
| 495 | |
| 496 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 497 | base::RunLoop().RunUntilIdle(); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 498 | |
| 499 | AddTab(browser(), landing_page_1); |
| 500 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 501 | device_manager_.AddDevice(device_1); |
| 502 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 503 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | TEST_F(WebUsbDetectorTest, UsbDeviceAddedBeforeActiveTabUrlIsLandingPage) { |
| 507 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 508 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 509 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 510 | std::string guid_1 = device_1->guid(); |
| 511 | |
| 512 | base::HistogramTester histogram_tester; |
| 513 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 514 | base::RunLoop().RunUntilIdle(); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 515 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 516 | device_manager_.AddDevice(device_1); |
| 517 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 518 | EXPECT_TRUE(display_service_->GetNotification(guid_1)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 519 | |
| 520 | AddTab(browser(), landing_page_1); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 521 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 522 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 3, 1); |
| 523 | } |
| 524 | |
| 525 | TEST_F(WebUsbDetectorTest, |
| 526 | NotificationClickedWhileInactiveTabUrlIsLandingPage) { |
| 527 | GURL landing_page_1(kLandingPage_1); |
| 528 | GURL landing_page_2(kLandingPage_2); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 529 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 530 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 531 | std::string guid_1 = device_1->guid(); |
| 532 | TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 533 | |
| 534 | base::HistogramTester histogram_tester; |
| 535 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 536 | base::RunLoop().RunUntilIdle(); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 537 | |
| 538 | AddTab(browser(), landing_page_1); |
| 539 | AddTab(browser(), landing_page_2); |
| 540 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 541 | device_manager_.AddDevice(device_1); |
| 542 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 543 | base::Optional<message_center::Notification> notification_1 = |
| 544 | display_service_->GetNotification(guid_1); |
| 545 | ASSERT_TRUE(notification_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 546 | EXPECT_EQ(2, tab_strip_model->count()); |
| 547 | |
Evan Stade | e092109 | 2018-04-04 21:13:09 | [diff] [blame] | 548 | notification_1->delegate()->Click(base::nullopt, base::nullopt); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 549 | EXPECT_EQ(2, tab_strip_model->count()); |
| 550 | content::WebContents* web_contents = |
| 551 | tab_strip_model->GetWebContentsAt(tab_strip_model->active_index()); |
| 552 | EXPECT_EQ(landing_page_1, web_contents->GetURL()); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 553 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 554 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1); |
| 555 | } |
| 556 | |
| 557 | TEST_F(WebUsbDetectorTest, NotificationClickedWhileNoTabUrlIsLandingPage) { |
| 558 | GURL landing_page_1(kLandingPage_1); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 559 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 560 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 561 | std::string guid_1 = device_1->guid(); |
| 562 | TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 563 | |
| 564 | base::HistogramTester histogram_tester; |
| 565 | Initialize(); |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 566 | base::RunLoop().RunUntilIdle(); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 567 | |
Donna Wu | 341df42f | 2018-11-02 15:18:57 | [diff] [blame] | 568 | device_manager_.AddDevice(device_1); |
| 569 | base::RunLoop().RunUntilIdle(); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 570 | base::Optional<message_center::Notification> notification_1 = |
| 571 | display_service_->GetNotification(guid_1); |
| 572 | ASSERT_TRUE(notification_1); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 573 | EXPECT_EQ(0, tab_strip_model->count()); |
| 574 | |
Evan Stade | e092109 | 2018-04-04 21:13:09 | [diff] [blame] | 575 | notification_1->delegate()->Click(base::nullopt, base::nullopt); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 576 | EXPECT_EQ(1, tab_strip_model->count()); |
| 577 | content::WebContents* web_contents = |
| 578 | tab_strip_model->GetWebContentsAt(tab_strip_model->active_index()); |
| 579 | EXPECT_EQ(landing_page_1, web_contents->GetURL()); |
Evan Stade | ea9be28f | 2018-02-06 18:32:31 | [diff] [blame] | 580 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
cco3 | dca47e2 | 2017-05-19 19:23:01 | [diff] [blame] | 581 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1); |
| 582 | } |
James Hollyer | c59c9c4 | 2018-12-20 03:39:06 | [diff] [blame] | 583 | |
| 584 | TEST_F(WebUsbDetectorTest, UsbDeviceAddedBeforeActiveTabFuzzyUrlIsLandingPage) { |
| 585 | GURL landing_page_1(kLandingPage_1); |
| 586 | GURL landing_page_1_fuzzed(kLandingPage_1_fuzzed); |
| 587 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 588 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 589 | std::string guid_1 = device_1->guid(); |
| 590 | |
| 591 | base::HistogramTester histogram_tester; |
| 592 | Initialize(); |
| 593 | base::RunLoop().RunUntilIdle(); |
| 594 | |
| 595 | device_manager_.AddDevice(device_1); |
| 596 | base::RunLoop().RunUntilIdle(); |
| 597 | EXPECT_TRUE(display_service_->GetNotification(guid_1)); |
| 598 | |
| 599 | AddTab(browser(), landing_page_1_fuzzed); |
| 600 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 601 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 3, 1); |
| 602 | } |
| 603 | |
| 604 | TEST_F(WebUsbDetectorTest, UsbDeviceAddedWhileActiveTabFuzzyUrlIsLandingPage) { |
| 605 | GURL landing_page_1(kLandingPage_1); |
| 606 | GURL landing_page_1_fuzzed(kLandingPage_1_fuzzed); |
| 607 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 608 | 0, 1, "Google", kProductName_3, "002", landing_page_1); |
| 609 | std::string guid_1 = device_1->guid(); |
| 610 | |
| 611 | Initialize(); |
| 612 | base::RunLoop().RunUntilIdle(); |
| 613 | |
| 614 | AddTab(browser(), landing_page_1_fuzzed); |
| 615 | |
| 616 | device_manager_.AddDevice(device_1); |
| 617 | base::RunLoop().RunUntilIdle(); |
| 618 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 619 | } |
| 620 | |
James Hollyer | d7cf310 | 2019-01-03 00:07:24 | [diff] [blame] | 621 | TEST_F(WebUsbDetectorTest, TwoDevicesSameLandingPageAddedRemovedAndAddedAgain) { |
| 622 | GURL landing_page_1(kLandingPage_1); |
| 623 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 624 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 625 | std::string guid_1 = device_1->guid(); |
| 626 | |
| 627 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 628 | 3, 4, "Google", kProductName_2, "005", landing_page_1); |
| 629 | std::string guid_2 = device_2->guid(); |
| 630 | |
| 631 | Initialize(); |
| 632 | base::RunLoop().RunUntilIdle(); |
| 633 | |
| 634 | device_manager_.AddDevice(device_1); |
| 635 | base::RunLoop().RunUntilIdle(); |
| 636 | base::Optional<message_center::Notification> notification_1 = |
| 637 | display_service_->GetNotification(guid_1); |
| 638 | ASSERT_TRUE(notification_1); |
| 639 | base::string16 expected_title_1 = |
| 640 | base::ASCIIToUTF16("Google Product A detected"); |
| 641 | EXPECT_EQ(expected_title_1, notification_1->title()); |
| 642 | base::string16 expected_message_1 = |
| 643 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
| 644 | EXPECT_EQ(expected_message_1, notification_1->message()); |
| 645 | EXPECT_TRUE(notification_1->delegate() != nullptr); |
| 646 | |
| 647 | device_manager_.AddDevice(device_2); |
| 648 | base::RunLoop().RunUntilIdle(); |
| 649 | ASSERT_FALSE(display_service_->GetNotification(guid_2)); |
| 650 | |
| 651 | device_manager_.RemoveDevice(device_2); |
| 652 | base::RunLoop().RunUntilIdle(); |
| 653 | EXPECT_TRUE(display_service_->GetNotification(guid_1)); |
| 654 | |
| 655 | device_manager_.RemoveDevice(device_1); |
| 656 | base::RunLoop().RunUntilIdle(); |
| 657 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 658 | |
| 659 | device_manager_.AddDevice(device_2); |
| 660 | base::RunLoop().RunUntilIdle(); |
| 661 | base::Optional<message_center::Notification> notification_2 = |
| 662 | display_service_->GetNotification(guid_2); |
| 663 | ASSERT_TRUE(notification_2); |
| 664 | base::string16 expected_title_2 = |
| 665 | base::ASCIIToUTF16("Google Product B detected"); |
| 666 | EXPECT_EQ(expected_title_2, notification_2->title()); |
| 667 | base::string16 expected_message_2 = |
| 668 | base::ASCIIToUTF16("Go to www.google.com to connect."); |
| 669 | EXPECT_EQ(expected_message_2, notification_2->message()); |
| 670 | EXPECT_TRUE(notification_2->delegate() != nullptr); |
| 671 | |
| 672 | device_manager_.AddDevice(device_1); |
| 673 | base::RunLoop().RunUntilIdle(); |
| 674 | ASSERT_FALSE(display_service_->GetNotification(guid_1)); |
| 675 | } |
| 676 | |
| 677 | TEST_F( |
| 678 | WebUsbDetectorTest, |
| 679 | DeviceWithSameLandingPageAddedAfterNotificationClickedAndThenNewTabActive) { |
| 680 | GURL landing_page_1(kLandingPage_1); |
| 681 | GURL landing_page_2(kLandingPage_2); |
| 682 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 683 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 684 | std::string guid_1 = device_1->guid(); |
| 685 | |
| 686 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 687 | 0, 1, "Google", kProductName_2, "002", landing_page_1); |
| 688 | std::string guid_2 = device_2->guid(); |
| 689 | |
| 690 | TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 691 | |
| 692 | base::HistogramTester histogram_tester; |
| 693 | Initialize(); |
| 694 | base::RunLoop().RunUntilIdle(); |
| 695 | |
| 696 | device_manager_.AddDevice(device_1); |
| 697 | base::RunLoop().RunUntilIdle(); |
| 698 | base::Optional<message_center::Notification> notification_1 = |
| 699 | display_service_->GetNotification(guid_1); |
| 700 | ASSERT_TRUE(notification_1); |
| 701 | EXPECT_EQ(0, tab_strip_model->count()); |
| 702 | |
| 703 | notification_1->delegate()->Click(base::nullopt, base::nullopt); |
| 704 | EXPECT_EQ(1, tab_strip_model->count()); |
| 705 | content::WebContents* web_contents = |
| 706 | tab_strip_model->GetWebContentsAt(tab_strip_model->active_index()); |
| 707 | EXPECT_EQ(landing_page_1, web_contents->GetURL()); |
| 708 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 709 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1); |
| 710 | |
| 711 | AddTab(browser(), landing_page_2); |
| 712 | |
| 713 | device_manager_.AddDevice(device_2); |
| 714 | base::RunLoop().RunUntilIdle(); |
| 715 | ASSERT_TRUE(display_service_->GetNotification(guid_2)); |
| 716 | } |
| 717 | |
James Hollyer | c59c9c4 | 2018-12-20 03:39:06 | [diff] [blame] | 718 | TEST_F(WebUsbDetectorTest, |
| 719 | NotificationClickedWhileInactiveTabFuzzyUrlIsLandingPage) { |
| 720 | GURL landing_page_1(kLandingPage_1); |
| 721 | GURL landing_page_1_fuzzed(kLandingPage_1_fuzzed); |
| 722 | GURL landing_page_2(kLandingPage_2); |
| 723 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 724 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 725 | std::string guid_1 = device_1->guid(); |
| 726 | TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
| 727 | |
| 728 | base::HistogramTester histogram_tester; |
| 729 | Initialize(); |
| 730 | base::RunLoop().RunUntilIdle(); |
| 731 | |
| 732 | AddTab(browser(), landing_page_1_fuzzed); |
| 733 | AddTab(browser(), landing_page_2); |
| 734 | |
| 735 | device_manager_.AddDevice(device_1); |
| 736 | base::RunLoop().RunUntilIdle(); |
| 737 | base::Optional<message_center::Notification> notification_1 = |
| 738 | display_service_->GetNotification(guid_1); |
| 739 | ASSERT_TRUE(notification_1); |
| 740 | EXPECT_EQ(2, tab_strip_model->count()); |
| 741 | |
| 742 | notification_1->delegate()->Click(base::nullopt, base::nullopt); |
| 743 | EXPECT_EQ(2, tab_strip_model->count()); |
| 744 | content::WebContents* web_contents = |
| 745 | tab_strip_model->GetWebContentsAt(tab_strip_model->active_index()); |
| 746 | EXPECT_EQ(landing_page_1_fuzzed, web_contents->GetURL()); |
| 747 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 748 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 2, 1); |
| 749 | } |
| 750 | |
James Hollyer | d7cf310 | 2019-01-03 00:07:24 | [diff] [blame] | 751 | TEST_F(WebUsbDetectorTest, |
| 752 | DeviceWithSameLandingPageAddedAfterPageVisitedAndNewTabActive) { |
| 753 | GURL landing_page_1(kLandingPage_1); |
| 754 | GURL landing_page_2(kLandingPage_2); |
| 755 | auto device_1 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 756 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 757 | std::string guid_1 = device_1->guid(); |
| 758 | |
| 759 | auto device_2 = base::MakeRefCounted<device::FakeUsbDeviceInfo>( |
| 760 | 0, 1, "Google", kProductName_1, "002", landing_page_1); |
| 761 | std::string guid_2 = device_2->guid(); |
| 762 | |
| 763 | base::HistogramTester histogram_tester; |
| 764 | Initialize(); |
| 765 | base::RunLoop().RunUntilIdle(); |
| 766 | |
| 767 | device_manager_.AddDevice(device_1); |
| 768 | base::RunLoop().RunUntilIdle(); |
| 769 | EXPECT_TRUE(display_service_->GetNotification(guid_1)); |
| 770 | |
| 771 | AddTab(browser(), landing_page_1); |
| 772 | EXPECT_FALSE(display_service_->GetNotification(guid_1)); |
| 773 | histogram_tester.ExpectUniqueSample("WebUsb.NotificationClosed", 3, 1); |
| 774 | |
| 775 | AddTab(browser(), landing_page_2); |
| 776 | device_manager_.AddDevice(device_2); |
| 777 | base::RunLoop().RunUntilIdle(); |
| 778 | EXPECT_TRUE(display_service_->GetNotification(guid_2)); |
| 779 | } |
| 780 | |
reillyg | dfd32df | 2016-10-18 02:42:16 | [diff] [blame] | 781 | #endif // !OS_WIN |