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