OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/chromeos/printer_detector/printer_detector.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
18 #include "chrome/browser/chromeos/printer_detector/printer_detector_factory.h" | |
19 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
20 #include "chrome/browser/extensions/test_extension_system.h" | |
21 #include "chrome/browser/notifications/notification.h" | |
22 #include "chrome/browser/notifications/notification_test_util.h" | |
23 #include "chrome/browser/notifications/notification_ui_manager.h" | |
24 #include "chrome/test/base/testing_profile.h" | |
25 #include "content/public/test/test_browser_thread_bundle.h" | |
26 #include "device/base/mock_device_client.h" | |
27 #include "device/usb/mock_usb_device.h" | |
28 #include "device/usb/mock_usb_service.h" | |
29 #include "device/usb/usb_descriptors.h" | |
30 #include "device/usb/usb_service.h" | |
31 #include "extensions/browser/extension_registry.h" | |
32 #include "extensions/common/extension_builder.h" | |
33 #include "extensions/common/value_builder.h" | |
34 #include "testing/gtest/include/gtest/gtest.h" | |
35 | |
36 using extensions::DictionaryBuilder; | |
37 using extensions::ListBuilder; | |
38 | |
39 namespace chromeos { | |
40 | |
41 namespace { | |
42 | |
43 const uint8_t kPrinterInterfaceClass = 7; | |
44 | |
45 const char kTestUserId[] = "test_user"; | |
46 | |
47 const char kPrinterAppExistsDelegateIDTemplate[] = | |
48 "system.printer.printer_provider_exists/%s:%s"; | |
49 | |
50 const char kPrinterAppNotFoundDelegateIDTemplate[] = | |
51 "system.printer.no_printer_provider_found/%s:%s"; | |
52 | |
53 std::unique_ptr<KeyedService> CreatePrinterDetector( | |
54 content::BrowserContext* context) { | |
55 return PrinterDetector::CreateLegacy(Profile::FromBrowserContext(context)); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 // TODO(tbarzic): Rename this test. | |
61 class PrinterDetectorAppSearchEnabledTest : public testing::Test { | |
62 public: | |
63 PrinterDetectorAppSearchEnabledTest() | |
64 : user_manager_(new chromeos::FakeChromeUserManager()), | |
65 user_manager_enabler_(user_manager_) {} | |
66 | |
67 ~PrinterDetectorAppSearchEnabledTest() override = default; | |
68 | |
69 void SetUp() override { | |
70 device_client_.GetUsbService(); | |
71 // Make sure the profile is created after adding the switch and setting up | |
72 // device client. | |
73 profile_.reset(new TestingProfile()); | |
74 chromeos::PrinterDetectorFactory::GetInstance()->SetTestingFactoryAndUse( | |
75 profile_.get(), &CreatePrinterDetector); | |
76 AddTestUser(); | |
77 SetExtensionSystemReady(profile_.get()); | |
78 } | |
79 | |
80 protected: | |
81 void SetExtensionSystemReady(TestingProfile* profile) { | |
82 extensions::TestExtensionSystem* test_extension_system = | |
83 static_cast<extensions::TestExtensionSystem*>( | |
84 extensions::ExtensionSystem::Get(profile)); | |
85 test_extension_system->SetReady(); | |
86 base::RunLoop().RunUntilIdle(); | |
87 } | |
88 | |
89 void AddTestUser() { | |
90 const user_manager::User* user = | |
91 user_manager_->AddUser(AccountId::FromUserEmail(kTestUserId)); | |
92 profile_->set_profile_name(kTestUserId); | |
93 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( | |
94 user, profile_.get()); | |
95 chromeos::PrinterDetectorFactory::GetInstance() | |
96 ->Get(profile_.get()) | |
97 ->SetNotificationUIManagerForTesting(¬ification_ui_manager_); | |
98 } | |
99 | |
100 void InvokeUsbAdded(uint16_t vendor_id, | |
101 uint16_t product_id, | |
102 uint8_t interface_class) { | |
103 device::UsbConfigDescriptor config(1, false, false, 0); | |
104 config.interfaces.emplace_back(1, 0, interface_class, 0, 0); | |
105 device_client_.usb_service()->AddDevice( | |
106 new device::MockUsbDevice(vendor_id, product_id, config)); | |
107 } | |
108 | |
109 // Creates a test extension with the provided permissions. | |
110 scoped_refptr<extensions::Extension> CreateTestExtension( | |
111 std::unique_ptr<base::ListValue> permissions_builder, | |
112 std::unique_ptr<base::DictionaryValue> usb_printers_builder) { | |
113 return extensions::ExtensionBuilder() | |
114 .SetID("fake_extension_id") | |
115 .SetManifest( | |
116 DictionaryBuilder() | |
117 .Set("name", "Printer provider extension") | |
118 .Set("manifest_version", 2) | |
119 .Set("version", "1.0") | |
120 // Needed to enable usb API. | |
121 .Set("app", | |
122 DictionaryBuilder() | |
123 .Set("background", | |
124 DictionaryBuilder() | |
125 .Set("scripts", | |
126 ListBuilder().Append("bg.js").Build()) | |
127 .Build()) | |
128 .Build()) | |
129 .Set("permissions", std::move(permissions_builder)) | |
130 .Set("usb_printers", std::move(usb_printers_builder)) | |
131 .Build()) | |
132 .Build(); | |
133 } | |
134 | |
135 content::TestBrowserThreadBundle thread_bundle_; | |
136 StubNotificationUIManager notification_ui_manager_; | |
137 chromeos::FakeChromeUserManager* user_manager_; | |
138 chromeos::ScopedUserManagerEnabler user_manager_enabler_; | |
139 device::MockDeviceClient device_client_; | |
140 std::unique_ptr<TestingProfile> profile_; | |
141 | |
142 private: | |
143 DISALLOW_COPY_AND_ASSIGN(PrinterDetectorAppSearchEnabledTest); | |
144 }; | |
145 | |
146 TEST_F(PrinterDetectorAppSearchEnabledTest, ShowFindAppNotification) { | |
147 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
148 | |
149 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
150 const Notification& notification = | |
151 notification_ui_manager_.GetNotificationAt(0); | |
152 EXPECT_EQ("123:456", notification.tag()); | |
153 EXPECT_EQ( | |
154 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "456"), | |
155 notification.delegate_id()); | |
156 } | |
157 | |
158 TEST_F(PrinterDetectorAppSearchEnabledTest, ShowAppFoundNotification) { | |
159 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
160 ListBuilder() | |
161 .Append("usb") | |
162 .Append("printerProvider") | |
163 .Append(DictionaryBuilder() | |
164 .Set("usbDevices", ListBuilder() | |
165 .Append(DictionaryBuilder() | |
166 .Set("vendorId", 123) | |
167 .Set("productId", 456) | |
168 .Build()) | |
169 .Build()) | |
170 .Build()) | |
171 .Build(), | |
172 DictionaryBuilder().Set("filters", ListBuilder().Build()).Build()); | |
173 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
174 ->AddEnabled(extension)); | |
175 | |
176 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
177 | |
178 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
179 const Notification& notification = | |
180 notification_ui_manager_.GetNotificationAt(0); | |
181 EXPECT_EQ("123:456", notification.tag()); | |
182 EXPECT_EQ( | |
183 base::StringPrintf(kPrinterAppExistsDelegateIDTemplate, "123", "456"), | |
184 notification.delegate_id()); | |
185 } | |
186 | |
187 TEST_F(PrinterDetectorAppSearchEnabledTest, | |
188 UsbHandlerExists_NotPrinterProvider) { | |
189 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
190 ListBuilder() | |
191 .Append("usb") | |
192 .Append(DictionaryBuilder() | |
193 .Set("usbDevices", ListBuilder() | |
194 .Append(DictionaryBuilder() | |
195 .Set("vendorId", 123) | |
196 .Set("productId", 756) | |
197 .Build()) | |
198 .Build()) | |
199 .Build()) | |
200 .Build(), | |
201 DictionaryBuilder().Set("filters", ListBuilder().Build()).Build()); | |
202 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
203 ->AddEnabled(extension)); | |
204 | |
205 InvokeUsbAdded(123, 756, kPrinterInterfaceClass); | |
206 | |
207 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
208 const Notification& notification = | |
209 notification_ui_manager_.GetNotificationAt(0); | |
210 EXPECT_EQ("123:756", notification.tag()); | |
211 EXPECT_EQ( | |
212 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "756"), | |
213 notification.delegate_id()); | |
214 } | |
215 | |
216 TEST_F(PrinterDetectorAppSearchEnabledTest, | |
217 PrinterProvider_DifferentUsbProductId) { | |
218 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
219 ListBuilder() | |
220 .Append("usb") | |
221 .Append("printerProvider") | |
222 .Append(DictionaryBuilder() | |
223 .Set("usbDevices", ListBuilder() | |
224 .Append(DictionaryBuilder() | |
225 .Set("vendorId", 123) | |
226 .Set("productId", 001) | |
227 .Build()) | |
228 .Build()) | |
229 .Build()) | |
230 .Build(), | |
231 DictionaryBuilder().Set("filters", ListBuilder().Build()).Build()); | |
232 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
233 ->AddEnabled(extension)); | |
234 | |
235 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
236 | |
237 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
238 const Notification& notification = | |
239 notification_ui_manager_.GetNotificationAt(0); | |
240 EXPECT_EQ("123:456", notification.tag()); | |
241 EXPECT_EQ( | |
242 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "456"), | |
243 notification.delegate_id()); | |
244 } | |
245 | |
246 TEST_F(PrinterDetectorAppSearchEnabledTest, | |
247 PrinterProvider_UsbPrinters_NotFound) { | |
248 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
249 ListBuilder().Append("usb").Append("printerProvider").Build(), | |
250 DictionaryBuilder() | |
251 .Set("filters", ListBuilder() | |
252 .Append(DictionaryBuilder() | |
253 .Set("vendorId", 123) | |
254 .Set("productId", 001) | |
255 .Build()) | |
256 .Build()) | |
257 .Build()); | |
258 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
259 ->AddEnabled(extension)); | |
260 | |
261 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
262 | |
263 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
264 const Notification& notification = | |
265 notification_ui_manager_.GetNotificationAt(0); | |
266 EXPECT_EQ("123:456", notification.tag()); | |
267 EXPECT_EQ( | |
268 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "456"), | |
269 notification.delegate_id()); | |
270 } | |
271 | |
272 TEST_F(PrinterDetectorAppSearchEnabledTest, | |
273 PrinterProvider_UsbPrinters_WithProductId) { | |
274 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
275 ListBuilder().Append("usb").Append("printerProvider").Build(), | |
276 DictionaryBuilder() | |
277 .Set("filters", ListBuilder() | |
278 .Append(DictionaryBuilder() | |
279 .Set("vendorId", 123) | |
280 .Set("productId", 456) | |
281 .Build()) | |
282 .Build()) | |
283 .Build()); | |
284 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
285 ->AddEnabled(extension)); | |
286 | |
287 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
288 | |
289 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
290 const Notification& notification = | |
291 notification_ui_manager_.GetNotificationAt(0); | |
292 EXPECT_EQ("123:456", notification.tag()); | |
293 EXPECT_EQ( | |
294 base::StringPrintf(kPrinterAppExistsDelegateIDTemplate, "123", "456"), | |
295 notification.delegate_id()); | |
296 } | |
297 | |
298 TEST_F(PrinterDetectorAppSearchEnabledTest, | |
299 PrinterProvider_UsbPrinters_WithInterfaceClass) { | |
300 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
301 ListBuilder().Append("usb").Append("printerProvider").Build(), | |
302 DictionaryBuilder() | |
303 .Set("filters", | |
304 ListBuilder() | |
305 .Append(DictionaryBuilder() | |
306 .Set("vendorId", 123) | |
307 .Set("interfaceClass", kPrinterInterfaceClass) | |
308 .Build()) | |
309 .Build()) | |
310 .Build()); | |
311 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
312 ->AddEnabled(extension)); | |
313 | |
314 InvokeUsbAdded(123, 456, kPrinterInterfaceClass); | |
315 | |
316 ASSERT_EQ(1u, notification_ui_manager_.GetNotificationCount()); | |
317 const Notification& notification = | |
318 notification_ui_manager_.GetNotificationAt(0); | |
319 EXPECT_EQ("123:456", notification.tag()); | |
320 EXPECT_EQ( | |
321 base::StringPrintf(kPrinterAppExistsDelegateIDTemplate, "123", "456"), | |
322 notification.delegate_id()); | |
323 } | |
324 | |
325 TEST_F(PrinterDetectorAppSearchEnabledTest, IgnoreNonPrinters) { | |
326 scoped_refptr<extensions::Extension> extension = CreateTestExtension( | |
327 ListBuilder().Append("usb").Append("printerProvider").Build(), | |
328 DictionaryBuilder() | |
329 .Set("filters", | |
330 ListBuilder() | |
331 .Append(DictionaryBuilder() | |
332 .Set("vendorId", 123) | |
333 .Set("interfaceClass", kPrinterInterfaceClass) | |
334 .Build()) | |
335 .Build()) | |
336 .Build()); | |
337 ASSERT_TRUE(extensions::ExtensionRegistry::Get(profile_.get()) | |
338 ->AddEnabled(extension)); | |
339 | |
340 InvokeUsbAdded(123, 456, 1); | |
341 | |
342 ASSERT_EQ(0u, notification_ui_manager_.GetNotificationCount()); | |
343 } | |
344 | |
345 } // namespace chromeos | |
OLD | NEW |