Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / dbus / printer_service_provider_unittest.cc
blob7dd3e9907cbb0e8c8511e71a448f1f85d104f1e1
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"
24 namespace chromeos {
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 {
37 public:
38 MOCK_METHOD2(ShowCloudPrintHelp,
39 void(const std::string& vendor, const std::string& product));
42 class PrinterServiceProviderTest : public testing::Test {
43 public:
44 void SetUp() override {
45 test_helper_.SetUp(kPrinterAdded, &service_provider_);
48 void TearDown() override { test_helper_.TearDown(); }
50 protected:
51 MockPrinterServiceProvider service_provider_;
52 ServiceProviderTestHelper test_helper_;
55 class PrinterServiceProviderAppSearchEnabledTest : public testing::Test {
56 public:
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::_))
67 .Times(0);
68 service_provider_.SetNotificationUIManagerForTesting(
69 &notification_ui_manager_);
72 protected:
73 void AddTestUser() {
74 const user_manager::User* user = user_manager_->AddUser(kTestUserId);
75 profile_.set_profile_name(kTestUserId);
76 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting(
77 user, &profile_);
80 bool InvokePrinterAdded(const std::string& vendor_id,
81 const std::string& product_id,
82 std::string* error) {
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.
95 bool success = true;
96 if (response.get()) {
97 dbus::MessageReader reader(response.get());
98 if (reader.HasMoreData()) {
99 *error = "Non empty response";
100 success = false;
102 } else {
103 *error = "No response.";
104 success = false;
107 // Tear down the test helper so it can be reused in the test.
108 test_helper_.TearDown();
109 return success;
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")
117 .SetManifest(
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.
123 .Set("app",
124 extensions::DictionaryBuilder().Set(
125 "background",
126 extensions::DictionaryBuilder().Set(
127 "scripts",
128 extensions::ListBuilder().Append("bg.js"))))
129 .Set("permissions", *permissions_builder))
130 .Build();
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_;
141 private:
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"))
152 .Times(1);
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) {
164 AddTestUser();
166 std::string error;
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());
173 EXPECT_EQ(
174 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "456"),
175 notification.delegate_id());
178 TEST_F(PrinterServiceProviderAppSearchEnabledTest, ShowAppFoundNotification) {
179 AddTestUser();
181 scoped_refptr<extensions::Extension> extension = CreateTestExtension(
182 &extensions::ListBuilder()
183 .Append("usb")
184 .Append("printerProvider")
185 .Append(extensions::DictionaryBuilder().Set(
186 "usbDevices", extensions::ListBuilder().Append(
187 extensions::DictionaryBuilder()
188 .Set("vendorId", 0x123)
189 .Set("productId", 0x456)))));
190 ASSERT_TRUE(
191 extensions::ExtensionRegistry::Get(&profile_)->AddEnabled(extension));
193 std::string error;
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());
200 EXPECT_EQ(
201 base::StringPrintf(kPrinterAppExistsDelegateIDTemplate, "123", "456"),
202 notification.delegate_id());
205 TEST_F(PrinterServiceProviderAppSearchEnabledTest,
206 UsbHandlerExists_NotPrinterProvider) {
207 AddTestUser();
209 scoped_refptr<extensions::Extension> extension =
210 CreateTestExtension(&extensions::ListBuilder().Append("usb").Append(
211 extensions::DictionaryBuilder().Set(
212 "usbDevices",
213 extensions::ListBuilder().Append(extensions::DictionaryBuilder()
214 .Set("vendorId", 0x123)
215 .Set("productId", 0xf56)))));
216 ASSERT_TRUE(
217 extensions::ExtensionRegistry::Get(&profile_)->AddEnabled(extension));
219 std::string error;
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());
226 EXPECT_EQ(
227 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "F56"),
228 notification.delegate_id());
231 TEST_F(PrinterServiceProviderAppSearchEnabledTest,
232 PrinterProvider_DifferentUsbProductId) {
233 AddTestUser();
235 scoped_refptr<extensions::Extension> extension = CreateTestExtension(
236 &extensions::ListBuilder()
237 .Append("usb")
238 .Append("printerProvider")
239 .Append(extensions::DictionaryBuilder().Set(
240 "usbDevices", extensions::ListBuilder().Append(
241 extensions::DictionaryBuilder()
242 .Set("vendorId", 0x123)
243 .Set("productId", 0x001)))));
244 ASSERT_TRUE(
245 extensions::ExtensionRegistry::Get(&profile_)->AddEnabled(extension));
247 std::string error;
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());
254 EXPECT_EQ(
255 base::StringPrintf(kPrinterAppNotFoundDelegateIDTemplate, "123", "456"),
256 notification.delegate_id());
259 TEST_F(PrinterServiceProviderAppSearchEnabledTest, VendorIdOutOfBounds) {
260 AddTestUser();
262 std::string error;
263 ASSERT_TRUE(InvokePrinterAdded("1F123", "456", &error)) << error;
265 EXPECT_EQ(0u, notification_ui_manager_.GetNotificationCount());
268 TEST_F(PrinterServiceProviderAppSearchEnabledTest, ProductIdNaN) {
269 AddTestUser();
271 std::string error;
272 ASSERT_TRUE(InvokePrinterAdded("123", "xxx", &error)) << error;
274 EXPECT_EQ(0u, notification_ui_manager_.GetNotificationCount());
277 TEST_F(PrinterServiceProviderAppSearchEnabledTest, VendorIdNaN) {
278 AddTestUser();
280 std::string error;
281 ASSERT_TRUE(InvokePrinterAdded("xxxfoo", "456", &error)) << error;
283 EXPECT_EQ(0u, notification_ui_manager_.GetNotificationCount());
286 TEST_F(PrinterServiceProviderAppSearchEnabledTest, ProductIdOutOfBounds) {
287 AddTestUser();
289 std::string error;
290 ASSERT_TRUE(InvokePrinterAdded("123", "1F456", &error)) << error;
292 EXPECT_EQ(0u, notification_ui_manager_.GetNotificationCount());
295 TEST_F(PrinterServiceProviderAppSearchEnabledTest, NegativeProductId) {
296 AddTestUser();
298 std::string error;
299 ASSERT_TRUE(InvokePrinterAdded("123", "-1", &error)) << error;
301 EXPECT_EQ(0u, notification_ui_manager_.GetNotificationCount());
304 TEST_F(PrinterServiceProviderAppSearchEnabledTest, PrintersAddedWithNoIdArgs) {
305 AddTestUser();
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