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 class FakeDeviceClient : public device::DeviceClient { | |
27 public: | |
28 FakeDeviceClient() : usb_service_(nullptr) {} | |
29 | |
30 ~FakeDeviceClient() override {} | |
31 | |
32 // device::DeviceClient implementation: | |
33 device::UsbService* GetUsbService() override { return usb_service_; } | |
34 | |
35 void set_usb_service(device::UsbService* service) { usb_service_ = service; } | |
Lei Zhang
2015/09/04 21:10:18
Just roll this into the ctor?
juncai
2015/09/08 21:14:00
Done.
| |
36 | |
37 private: | |
38 device::UsbService* usb_service_; | |
39 | |
40 DISALLOW_COPY_AND_ASSIGN(FakeDeviceClient); | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 class ChromeWebUsbBrowserClientTest : public testing::Test { | |
46 public: | |
47 ChromeWebUsbBrowserClientTest() {} | |
48 | |
49 ~ChromeWebUsbBrowserClientTest() override = default; | |
50 | |
51 void SetUp() override { message_center::MessageCenter::Initialize(); } | |
52 | |
53 void TearDown() override { message_center::MessageCenter::Shutdown(); } | |
54 | |
55 void Initialize(scoped_refptr<device::MockUsbDevice> device) { | |
56 device_ = device; | |
57 usb_service_.reset(new device::MockUsbService()); | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
The pattern in other tests that need a MockUsbServ
juncai
2015/09/08 21:14:00
Done.
| |
58 device_client_.reset(new FakeDeviceClient()); | |
59 device_client_->set_usb_service(usb_service_.get()); | |
60 webusb_browser_client_.reset(new ChromeWebUsbBrowserClient()); | |
61 webusb_detector_.reset( | |
62 new webusb::WebUsbDetector(webusb_browser_client_.get())); | |
63 } | |
64 | |
65 protected: | |
66 void InvokeUsbAdded() { usb_service_->AddDevice(device_); } | |
67 | |
68 void InvokeUsbRemoved() { usb_service_->RemoveDevice(device_); } | |
69 | |
70 scoped_refptr<device::MockUsbDevice> device_; | |
71 scoped_ptr<device::MockUsbService> usb_service_; | |
72 scoped_ptr<FakeDeviceClient> device_client_; | |
73 scoped_ptr<webusb::WebUsbBrowserClient> webusb_browser_client_; | |
74 scoped_ptr<webusb::WebUsbDetector> webusb_detector_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(ChromeWebUsbBrowserClientTest); | |
77 }; | |
78 | |
79 TEST_F(ChromeWebUsbBrowserClientTest, UsbDevicePluggedInAndRemoved) { | |
80 std::string product_string = "Google Product A"; | |
81 GURL landing_page("https://www.google.com"); | |
82 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
83 0, 1, "Google", product_string, "001", landing_page)); | |
84 std::string guid = device->guid(); | |
85 | |
86 Initialize(device); | |
87 | |
88 InvokeUsbAdded(); | |
89 | |
90 message_center::MessageCenter* message_center = | |
91 message_center::MessageCenter::Get(); | |
92 EXPECT_TRUE(message_center != nullptr); | |
Lei Zhang
2015/09/04 21:10:18
You can omit the != nullptr, ditto below.
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
ASSERT_TRUE so that later code doesn't crash deref
juncai
2015/09/08 21:14:00
Done.
juncai
2015/09/08 21:14:00
Done.
| |
93 | |
94 message_center::Notification* notification = | |
95 message_center->FindVisibleNotificationById(guid); | |
96 EXPECT_TRUE(notification != nullptr); | |
97 | |
98 base::string16 expected_title = | |
99 l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
Use the literal English strings here (with base::A
juncai
2015/09/08 21:14:00
Done.
| |
100 base::UTF8ToUTF16(product_string)); | |
Lei Zhang
2015/09/04 21:10:18
nit: you can use ASCIIToUTF16()
juncai
2015/09/08 21:14:00
Done.
| |
101 EXPECT_EQ(expected_title, notification->title()); | |
102 | |
103 base::string16 expected_message = | |
104 l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION); | |
105 EXPECT_EQ(expected_message, notification->message()); | |
106 | |
107 message_center::NotificationDelegate* notification_delegate = | |
108 notification->delegate(); | |
109 EXPECT_TRUE(notification_delegate != nullptr); | |
Reilly Grant (use Gerrit)
2015/09/04 21:23:34
EXPECT_TRUE(notification->delegate() != nullptr);
juncai
2015/09/08 21:14:00
Done.
| |
110 | |
111 InvokeUsbRemoved(); | |
112 | |
113 notification = message_center->FindVisibleNotificationById(guid); | |
114 // device is removed, so notification should be removed from the | |
115 // message_center too | |
116 EXPECT_EQ(nullptr, notification); | |
117 } | |
118 | |
119 TEST_F(ChromeWebUsbBrowserClientTest, | |
120 UsbDeviceNoLandingPagePluggedInAndRemoved) { | |
121 std::string product_string = "Google Product A"; | |
122 GURL landing_page(""); | |
123 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
124 0, 1, "Google", product_string, "001", landing_page)); | |
125 std::string guid = device->guid(); | |
126 | |
127 Initialize(device); | |
128 | |
129 InvokeUsbAdded(); | |
130 | |
131 message_center::MessageCenter* message_center = | |
132 message_center::MessageCenter::Get(); | |
133 EXPECT_TRUE(message_center != nullptr); | |
134 | |
135 message_center::Notification* notification = | |
136 message_center->FindVisibleNotificationById(guid); | |
137 | |
138 // for device with no landing page, no notification is generated | |
139 EXPECT_EQ(nullptr, notification); | |
140 | |
141 InvokeUsbRemoved(); | |
142 | |
143 notification = message_center->FindVisibleNotificationById(guid); | |
144 EXPECT_EQ(nullptr, notification); | |
145 } | |
146 | |
147 TEST_F(ChromeWebUsbBrowserClientTest, UsbDeviceWasThereBeforeAndThenRemoved) { | |
148 std::string product_string = "Google Product A"; | |
149 GURL landing_page("https://www.google.com"); | |
150 scoped_refptr<device::MockUsbDevice> device(new device::MockUsbDevice( | |
151 0, 1, "Google", product_string, "001", landing_page)); | |
152 std::string guid = device->guid(); | |
153 | |
154 Initialize(device); | |
155 | |
156 message_center::MessageCenter* message_center = | |
157 message_center::MessageCenter::Get(); | |
158 EXPECT_TRUE(message_center != nullptr); | |
159 | |
160 message_center::Notification* notification = | |
161 message_center->FindVisibleNotificationById(guid); | |
162 EXPECT_EQ(nullptr, notification); | |
163 | |
164 InvokeUsbRemoved(); | |
165 | |
166 notification = message_center->FindVisibleNotificationById(guid); | |
167 EXPECT_EQ(nullptr, notification); | |
168 } | |
OLD | NEW |