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