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.
5 #include "chrome/browser/chromeos/dbus/printer_service_provider.h"
7 #include "base/command_line.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
10 #include "chrome/browser/chromeos/profiles/profile_helper.h"
11 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/notifications/notification_test_util.h"
13 #include "chrome/browser/notifications/notification_ui_manager.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chromeos/chromeos_switches.h"
16 #include "chromeos/dbus/services/service_provider_test_helper.h"
17 #include "components/user_manager/fake_user_manager.h"
18 #include "dbus/message.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/value_builder.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
26 const char kPrinterAdded
[] = "PrinterAdded";
28 const char kTestUserId
[] = "test_user";
30 const char kPrinterAppExistsDelegateIDTemplate
[] =
31 "system.printer.printer_provider_exists/%s:%s";
33 const char kPrinterAppNotFoundDelegateIDTemplate
[] =
34 "system.printer.no_printer_provider_found/%s:%s";
36 class MockPrinterServiceProvider
: public PrinterServiceProvider
{
38 MOCK_METHOD2(ShowCloudPrintHelp
,
39 void(const std::string
& vendor
, const std::string
& product
));
42 class PrinterServiceProviderTest
: public testing::Test
{
44 void SetUp() override
{
45 test_helper_
.SetUp(kPrinterAdded
, &service_provider_
);
48 void TearDown() override
{ test_helper_
.TearDown(); }
51 MockPrinterServiceProvider service_provider_
;
52 ServiceProviderTestHelper test_helper_
;
55 class PrinterServiceProviderAppSearchEnabledTest
: public testing::Test
{
57 PrinterServiceProviderAppSearchEnabledTest()
58 : user_manager_(new user_manager::FakeUserManager()),
59 user_manager_enabler_(user_manager_
) {}
61 ~PrinterServiceProviderAppSearchEnabledTest() override
= default;
63 void SetUp() override
{
64 base::CommandLine::ForCurrentProcess()->AppendSwitch(
65 switches::kEnablePrinterAppSearch
);
66 EXPECT_CALL(service_provider_
, ShowCloudPrintHelp(testing::_
, testing::_
))
68 service_provider_
.SetNotificationUIManagerForTesting(
69 ¬ification_ui_manager_
);
74 const user_manager::User
* user
= user_manager_
->AddUser(kTestUserId
);
75 profile_
.set_profile_name(kTestUserId
);
76 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
80 bool InvokePrinterAdded(const std::string
& vendor_id
,
81 const std::string
& product_id
,
83 *error
= std::string();
84 test_helper_
.SetUp(kPrinterAdded
, &service_provider_
);
86 dbus::MethodCall
method_call(kLibCrosServiceInterface
, kPrinterAdded
);
87 dbus::MessageWriter
writer(&method_call
);
88 writer
.AppendString(vendor_id
);
89 writer
.AppendString(product_id
);
91 // Call the PrinterAdded method.
92 scoped_ptr
<dbus::Response
> response(test_helper_
.CallMethod(&method_call
));
94 // An empty response should be returned.
97 dbus::MessageReader
reader(response
.get());
98 if (reader
.HasMoreData()) {
99 *error
= "Non empty response";
103 *error
= "No response.";
107 // Tear down the test helper so it can be reused in the test.
108 test_helper_
.TearDown();
112 // Creates a test extension with the provided permissions.
113 scoped_refptr
<extensions::Extension
> CreateTestExtension(
114 extensions::ListBuilder
* permissions_builder
) {
115 return extensions::ExtensionBuilder()
116 .SetID("fake_extension_id")
118 extensions::DictionaryBuilder()
119 .Set("name", "Printer provider extension")
120 .Set("manifest_version", 2)
121 .Set("version", "1.0")
122 // Needed to enable usb API.
124 extensions::DictionaryBuilder().Set(
126 extensions::DictionaryBuilder().Set(
128 extensions::ListBuilder().Append("bg.js"))))
129 .Set("permissions", *permissions_builder
))
133 StubNotificationUIManager notification_ui_manager_
;
134 TestingProfile profile_
;
135 user_manager::FakeUserManager
* user_manager_
;
136 chromeos::ScopedUserManagerEnabler user_manager_enabler_
;
138 MockPrinterServiceProvider service_provider_
;
139 ServiceProviderTestHelper test_helper_
;
142 DISALLOW_COPY_AND_ASSIGN(PrinterServiceProviderAppSearchEnabledTest
);
145 TEST_F(PrinterServiceProviderTest
, ShowCloudPrintHelp
) {
146 dbus::MethodCall
method_call(kLibCrosServiceInterface
, kPrinterAdded
);
147 dbus::MessageWriter
writer(&method_call
);
148 writer
.AppendString("123");
149 writer
.AppendString("456");
151 EXPECT_CALL(service_provider_
, ShowCloudPrintHelp("123", "456"))
154 // Call the PrinterAdded method.
155 scoped_ptr
<dbus::Response
> response(test_helper_
.CallMethod(&method_call
));
157 // An empty response should be returned.
158 ASSERT_TRUE(response
.get());
159 dbus::MessageReader
reader(response
.get());
160 ASSERT_FALSE(reader
.HasMoreData());
163 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, ShowFindAppNotification
) {
167 ASSERT_TRUE(InvokePrinterAdded("123", "456", &error
)) << error
;
169 ASSERT_EQ(1u, notification_ui_manager_
.GetNotificationCount());
170 const Notification
& notification
=
171 notification_ui_manager_
.GetNotificationAt(0);
172 EXPECT_EQ("123:456", notification
.tag());
174 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate
, "123", "456"),
175 notification
.delegate_id());
178 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, ShowAppFoundNotification
) {
181 scoped_refptr
<extensions::Extension
> extension
= CreateTestExtension(
182 &extensions::ListBuilder()
184 .Append("printerProvider")
185 .Append(extensions::DictionaryBuilder().Set(
186 "usbDevices", extensions::ListBuilder().Append(
187 extensions::DictionaryBuilder()
188 .Set("vendorId", 0x123)
189 .Set("productId", 0x456)))));
191 extensions::ExtensionRegistry::Get(&profile_
)->AddEnabled(extension
));
194 ASSERT_TRUE(InvokePrinterAdded("123", "456", &error
)) << error
;
196 ASSERT_EQ(1u, notification_ui_manager_
.GetNotificationCount());
197 const Notification
& notification
=
198 notification_ui_manager_
.GetNotificationAt(0);
199 EXPECT_EQ("123:456", notification
.tag());
201 base::StringPrintf(kPrinterAppExistsDelegateIDTemplate
, "123", "456"),
202 notification
.delegate_id());
205 TEST_F(PrinterServiceProviderAppSearchEnabledTest
,
206 UsbHandlerExists_NotPrinterProvider
) {
209 scoped_refptr
<extensions::Extension
> extension
=
210 CreateTestExtension(&extensions::ListBuilder().Append("usb").Append(
211 extensions::DictionaryBuilder().Set(
213 extensions::ListBuilder().Append(extensions::DictionaryBuilder()
214 .Set("vendorId", 0x123)
215 .Set("productId", 0xf56)))));
217 extensions::ExtensionRegistry::Get(&profile_
)->AddEnabled(extension
));
220 ASSERT_TRUE(InvokePrinterAdded("123", "f56", &error
)) << error
;
222 ASSERT_EQ(1u, notification_ui_manager_
.GetNotificationCount());
223 const Notification
& notification
=
224 notification_ui_manager_
.GetNotificationAt(0);
225 EXPECT_EQ("123:F56", notification
.tag());
227 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate
, "123", "F56"),
228 notification
.delegate_id());
231 TEST_F(PrinterServiceProviderAppSearchEnabledTest
,
232 PrinterProvider_DifferentUsbProductId
) {
235 scoped_refptr
<extensions::Extension
> extension
= CreateTestExtension(
236 &extensions::ListBuilder()
238 .Append("printerProvider")
239 .Append(extensions::DictionaryBuilder().Set(
240 "usbDevices", extensions::ListBuilder().Append(
241 extensions::DictionaryBuilder()
242 .Set("vendorId", 0x123)
243 .Set("productId", 0x001)))));
245 extensions::ExtensionRegistry::Get(&profile_
)->AddEnabled(extension
));
248 ASSERT_TRUE(InvokePrinterAdded("123", "456", &error
)) << error
;
250 ASSERT_EQ(1u, notification_ui_manager_
.GetNotificationCount());
251 const Notification
& notification
=
252 notification_ui_manager_
.GetNotificationAt(0);
253 EXPECT_EQ("123:456", notification
.tag());
255 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate
, "123", "456"),
256 notification
.delegate_id());
259 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, VendorIdOutOfBounds
) {
263 ASSERT_TRUE(InvokePrinterAdded("1F123", "456", &error
)) << error
;
265 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
268 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, ProductIdNaN
) {
272 ASSERT_TRUE(InvokePrinterAdded("123", "xxx", &error
)) << error
;
274 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
277 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, VendorIdNaN
) {
281 ASSERT_TRUE(InvokePrinterAdded("xxxfoo", "456", &error
)) << error
;
283 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
286 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, ProductIdOutOfBounds
) {
290 ASSERT_TRUE(InvokePrinterAdded("123", "1F456", &error
)) << error
;
292 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
295 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, NegativeProductId
) {
299 ASSERT_TRUE(InvokePrinterAdded("123", "-1", &error
)) << error
;
301 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
304 TEST_F(PrinterServiceProviderAppSearchEnabledTest
, PrintersAddedWithNoIdArgs
) {
307 test_helper_
.SetUp(kPrinterAdded
, &service_provider_
);
308 dbus::MethodCall
method_call(kLibCrosServiceInterface
, kPrinterAdded
);
309 dbus::MessageWriter
writer(&method_call
);
311 // Call the PrinterAdded method.
312 scoped_ptr
<dbus::Response
> response(test_helper_
.CallMethod(&method_call
));
314 // An empty response should be returned.
315 ASSERT_TRUE(response
.get());
316 dbus::MessageReader
reader(response
.get());
317 ASSERT_FALSE(reader
.HasMoreData());
318 test_helper_
.TearDown();
320 EXPECT_EQ(0u, notification_ui_manager_
.GetNotificationCount());
323 } // namespace chromeos