| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "chrome/browser/chrome_webusb_browser_client.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/strings/string16.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "components/webusb/webusb_detector.h" | |
| 13 #include "device/core/mock_device_client.h" | |
| 14 #include "device/usb/mock_usb_device.h" | |
| 15 #include "device/usb/mock_usb_service.h" | |
| 16 #include "device/usb/usb_device.h" | |
| 17 #include "device/usb/usb_service.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 #include "ui/message_center/message_center.h" | |
| 20 #include "ui/message_center/notification.h" | |
| 21 #include "ui/message_center/notification_delegate.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // usb device product name | |
| 27 const char* kProductName_1 = "Google Product A"; | |
| 28 const char* kProductName_2 = "Google Product B"; | |
| 29 const char* kProductName_3 = "Google Product C"; | |
| 30 | |
| 31 // usb device landing page | |
| 32 const char* kLandingPage_1 = "https://www.google.com/A"; | |
| 33 const char* kLandingPage_2 = "https://www.google.com/B"; | |
| 34 const char* kLandingPage_3 = "https://www.google.com/C"; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 class ChromeWebUsbBrowserClientTest : public testing::Test { | |
| 39 public: | |
| 40 ChromeWebUsbBrowserClientTest() {} | |
| 41 | |
| 42 ~ChromeWebUsbBrowserClientTest() override = default; | |
| 43 | |
| 44 void SetUp() override { message_center::MessageCenter::Initialize(); } | |
| 45 | |
| 46 void TearDown() override { message_center::MessageCenter::Shutdown(); } | |
| 47 | |
| 48 protected: | |
| 49 device::MockDeviceClient device_client_; | |
| 50 ChromeWebUsbBrowserClient chrome_webusb_browser_client_; | |
| 51 | |
| 52 private: | |
| 53 DISALLOW_COPY_AND_ASSIGN(ChromeWebUsbBrowserClientTest); | |
| 54 }; | |
| 55 | |
| 56 TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceAddedAndRemoved) { | |
| 57 GURL landing_page(kLandingPage_1); | |
| 58 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
| 59 0, 1, "Google", kProductName_1, "002", landing_page)); | |
| 60 std::string guid = device->guid(); | |
| 61 | |
| 62 webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); | |
| 63 | |
| 64 device_client_.usb_service()->AddDevice(device); | |
| 65 | |
| 66 message_center::MessageCenter* message_center = | |
| 67 message_center::MessageCenter::Get(); | |
| 68 ASSERT_TRUE(message_center != nullptr); | |
| 69 | |
| 70 message_center::Notification* notification = | |
| 71 message_center->FindVisibleNotificationById(guid); | |
| 72 ASSERT_TRUE(notification != nullptr); | |
| 73 | |
| 74 base::string16 expected_title = | |
| 75 base::ASCIIToUTF16("Google Product A detected"); | |
| 76 EXPECT_EQ(expected_title, notification->title()); | |
| 77 | |
| 78 base::string16 expected_message = | |
| 79 base::ASCIIToUTF16("Go to www.google.com/A to connect."); | |
| 80 EXPECT_EQ(expected_message, notification->message()); | |
| 81 | |
| 82 EXPECT_TRUE(notification->delegate() != nullptr); | |
| 83 | |
| 84 device_client_.usb_service()->RemoveDevice(device); | |
| 85 | |
| 86 // device is removed, so notification should be removed from the | |
| 87 // message_center too | |
| 88 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 89 } | |
| 90 | |
| 91 TEST_F(ChromeWebUsbBrowserClientTest, | |
| 92 UsbDeviceWithoutProductNameAddedAndRemoved) { | |
| 93 std::string product_name = ""; | |
| 94 GURL landing_page(kLandingPage_1); | |
| 95 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
| 96 0, 1, "Google", product_name, "002", landing_page)); | |
| 97 std::string guid = device->guid(); | |
| 98 | |
| 99 webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); | |
| 100 | |
| 101 device_client_.usb_service()->AddDevice(device); | |
| 102 | |
| 103 message_center::MessageCenter* message_center = | |
| 104 message_center::MessageCenter::Get(); | |
| 105 ASSERT_TRUE(message_center != nullptr); | |
| 106 | |
| 107 // for device without product name, no notification is generated | |
| 108 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 109 | |
| 110 device_client_.usb_service()->RemoveDevice(device); | |
| 111 | |
| 112 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 113 } | |
| 114 | |
| 115 TEST_F(ChromeWebUsbBrowserClientTest, | |
| 116 UsbDeviceWithoutLandingPageAddedAndRemoved) { | |
| 117 GURL landing_page(""); | |
| 118 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
| 119 0, 1, "Google", kProductName_1, "002", landing_page)); | |
| 120 std::string guid = device->guid(); | |
| 121 | |
| 122 webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); | |
| 123 | |
| 124 device_client_.usb_service()->AddDevice(device); | |
| 125 | |
| 126 message_center::MessageCenter* message_center = | |
| 127 message_center::MessageCenter::Get(); | |
| 128 ASSERT_TRUE(message_center != nullptr); | |
| 129 | |
| 130 // for device without landing page, no notification is generated | |
| 131 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 132 | |
| 133 device_client_.usb_service()->RemoveDevice(device); | |
| 134 | |
| 135 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 136 } | |
| 137 | |
| 138 TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceWasThereBeforeAndThenRemoved) { | |
| 139 GURL landing_page(kLandingPage_1); | |
| 140 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
| 141 0, 1, "Google", kProductName_1, "002", landing_page)); | |
| 142 std::string guid = device->guid(); | |
| 143 | |
| 144 webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); | |
| 145 | |
| 146 message_center::MessageCenter* message_center = | |
| 147 message_center::MessageCenter::Get(); | |
| 148 ASSERT_TRUE(message_center != nullptr); | |
| 149 | |
| 150 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 151 | |
| 152 device_client_.usb_service()->RemoveDevice(device); | |
| 153 | |
| 154 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid)); | |
| 155 } | |
| 156 | |
| 157 TEST_F(ChromeWebUsbBrowserClientTest, ThreeUsbDevicesAddedAndRemoved) { | |
| 158 base::string16 product_name_1 = base::UTF8ToUTF16(kProductName_1); | |
| 159 GURL landing_page_1(kLandingPage_1); | |
| 160 scoped_refptr<device::MockUsbDevice> device_1(new device::MockUsbDevice( | |
| 161 0, 1, "Google", kProductName_1, "002", landing_page_1)); | |
| 162 std::string guid_1 = device_1->guid(); | |
| 163 | |
| 164 base::string16 product_name_2 = base::UTF8ToUTF16(kProductName_2); | |
| 165 GURL landing_page_2(kLandingPage_2); | |
| 166 scoped_refptr<device::MockUsbDevice> device_2(new device::MockUsbDevice( | |
| 167 3, 4, "Google", kProductName_2, "005", landing_page_2)); | |
| 168 std::string guid_2 = device_2->guid(); | |
| 169 | |
| 170 base::string16 product_name_3 = base::UTF8ToUTF16(kProductName_3); | |
| 171 GURL landing_page_3(kLandingPage_3); | |
| 172 scoped_refptr<device::MockUsbDevice> device_3(new device::MockUsbDevice( | |
| 173 6, 7, "Google", kProductName_3, "008", landing_page_3)); | |
| 174 std::string guid_3 = device_3->guid(); | |
| 175 | |
| 176 webusb::WebUsbDetector webusb_detector(&chrome_webusb_browser_client_); | |
| 177 | |
| 178 device_client_.usb_service()->AddDevice(device_1); | |
| 179 device_client_.usb_service()->AddDevice(device_2); | |
| 180 device_client_.usb_service()->AddDevice(device_3); | |
| 181 | |
| 182 message_center::MessageCenter* message_center = | |
| 183 message_center::MessageCenter::Get(); | |
| 184 ASSERT_TRUE(message_center != nullptr); | |
| 185 | |
| 186 message_center::Notification* notification_1 = | |
| 187 message_center->FindVisibleNotificationById(guid_1); | |
| 188 ASSERT_TRUE(notification_1 != nullptr); | |
| 189 base::string16 expected_title_1 = | |
| 190 base::ASCIIToUTF16("Google Product A detected"); | |
| 191 EXPECT_EQ(expected_title_1, notification_1->title()); | |
| 192 base::string16 expected_message_1 = | |
| 193 base::ASCIIToUTF16("Go to www.google.com/A to connect."); | |
| 194 EXPECT_EQ(expected_message_1, notification_1->message()); | |
| 195 | |
| 196 message_center::Notification* notification_2 = | |
| 197 message_center->FindVisibleNotificationById(guid_2); | |
| 198 ASSERT_TRUE(notification_2 != nullptr); | |
| 199 base::string16 expected_title_2 = | |
| 200 base::ASCIIToUTF16("Google Product B detected"); | |
| 201 EXPECT_EQ(expected_title_2, notification_2->title()); | |
| 202 base::string16 expected_message_2 = | |
| 203 base::ASCIIToUTF16("Go to www.google.com/B to connect."); | |
| 204 EXPECT_EQ(expected_message_2, notification_2->message()); | |
| 205 | |
| 206 message_center::Notification* notification_3 = | |
| 207 message_center->FindVisibleNotificationById(guid_3); | |
| 208 ASSERT_TRUE(notification_3 != nullptr); | |
| 209 base::string16 expected_title_3 = | |
| 210 base::ASCIIToUTF16("Google Product C detected"); | |
| 211 EXPECT_EQ(expected_title_3, notification_3->title()); | |
| 212 base::string16 expected_message_3 = | |
| 213 base::ASCIIToUTF16("Go to www.google.com/C to connect."); | |
| 214 EXPECT_EQ(expected_message_3, notification_3->message()); | |
| 215 | |
| 216 EXPECT_TRUE(notification_1->delegate() != nullptr); | |
| 217 EXPECT_TRUE(notification_2->delegate() != nullptr); | |
| 218 EXPECT_TRUE(notification_3->delegate() != nullptr); | |
| 219 | |
| 220 device_client_.usb_service()->RemoveDevice(device_1); | |
| 221 device_client_.usb_service()->RemoveDevice(device_2); | |
| 222 device_client_.usb_service()->RemoveDevice(device_3); | |
| 223 | |
| 224 // devices are removed, so notifications should be removed from the | |
| 225 // message_center too | |
| 226 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid_1)); | |
| 227 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid_2)); | |
| 228 EXPECT_EQ(nullptr, message_center->FindVisibleNotificationById(guid_3)); | |
| 229 } | |
| OLD | NEW |