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.
5 #include "chrome/browser/chrome_webusb_browser_client.h"
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"
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";
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";
34 class TestDeviceClient
: public device::DeviceClient
{
36 TestDeviceClient() : device::DeviceClient() {}
37 ~TestDeviceClient() override
{}
39 device::MockUsbService
& mock_usb_service() { return usb_service_
; }
42 device::UsbService
* GetUsbService() override
{ return &usb_service_
; }
44 device::MockUsbService usb_service_
;
49 class ChromeWebUsbBrowserClientTest
: public testing::Test
{
51 ChromeWebUsbBrowserClientTest() {}
53 ~ChromeWebUsbBrowserClientTest() override
= default;
55 void SetUp() override
{ message_center::MessageCenter::Initialize(); }
57 void TearDown() override
{ message_center::MessageCenter::Shutdown(); }
60 TestDeviceClient device_client_
;
61 ChromeWebUsbBrowserClient chrome_webusb_browser_client_
;
63 DISALLOW_COPY_AND_ASSIGN(ChromeWebUsbBrowserClientTest
);
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();
72 webusb::WebUsbDetector
webusb_detector(&chrome_webusb_browser_client_
);
74 device_client_
.mock_usb_service().AddDevice(device
);
76 message_center::MessageCenter
* message_center
=
77 message_center::MessageCenter::Get();
78 ASSERT_TRUE(message_center
!= nullptr);
80 message_center::Notification
* notification
=
81 message_center
->FindVisibleNotificationById(guid
);
82 ASSERT_TRUE(notification
!= nullptr);
84 base::string16 expected_title
= base::ASCIIToUTF16("Google Product A");
85 EXPECT_EQ(expected_title
, notification
->title());
87 base::string16 expected_message
=
88 base::ASCIIToUTF16("Click here to visit this page");
89 EXPECT_EQ(expected_message
, notification
->message());
91 EXPECT_TRUE(notification
->delegate() != nullptr);
93 device_client_
.mock_usb_service().RemoveDevice(device
);
95 // device is removed, so notification should be removed from the
97 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
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();
108 webusb::WebUsbDetector
webusb_detector(&chrome_webusb_browser_client_
);
110 device_client_
.mock_usb_service().AddDevice(device
);
112 message_center::MessageCenter
* message_center
=
113 message_center::MessageCenter::Get();
114 ASSERT_TRUE(message_center
!= nullptr);
116 // for device without product name, no notification is generated
117 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
119 device_client_
.mock_usb_service().RemoveDevice(device
);
121 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
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();
131 webusb::WebUsbDetector
webusb_detector(&chrome_webusb_browser_client_
);
133 device_client_
.mock_usb_service().AddDevice(device
);
135 message_center::MessageCenter
* message_center
=
136 message_center::MessageCenter::Get();
137 ASSERT_TRUE(message_center
!= nullptr);
139 // for device without landing page, no notification is generated
140 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
142 device_client_
.mock_usb_service().RemoveDevice(device
);
144 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
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();
153 webusb::WebUsbDetector
webusb_detector(&chrome_webusb_browser_client_
);
155 message_center::MessageCenter
* message_center
=
156 message_center::MessageCenter::Get();
157 ASSERT_TRUE(message_center
!= nullptr);
159 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
161 device_client_
.mock_usb_service().RemoveDevice(device
);
163 EXPECT_EQ(nullptr, message_center
->FindVisibleNotificationById(guid
));
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();
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();
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();
185 webusb::WebUsbDetector
webusb_detector(&chrome_webusb_browser_client_
);
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
);
191 message_center::MessageCenter
* message_center
=
192 message_center::MessageCenter::Get();
193 ASSERT_TRUE(message_center
!= nullptr);
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());
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());
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());
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());
219 EXPECT_TRUE(notification_1
->delegate() != nullptr);
220 EXPECT_TRUE(notification_2
->delegate() != nullptr);
221 EXPECT_TRUE(notification_3
->delegate() != nullptr);
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
);
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
));