OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/test/values_test_util.h" |
17 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/extensions/test_extension_environment.h" |
18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 21 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
19 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" | 22 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
20 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "device/core/device_client.h" |
| 25 #include "device/usb/mock_usb_device.h" |
| 26 #include "device/usb/mock_usb_service.h" |
| 27 #include "extensions/browser/api/device_permissions_manager.h" |
22 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 28 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
23 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 29 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
24 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 30 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 31 #include "extensions/common/extension.h" |
| 32 #include "extensions/common/value_builder.h" |
25 #include "printing/pdf_render_settings.h" | 33 #include "printing/pdf_render_settings.h" |
26 #include "printing/pwg_raster_settings.h" | 34 #include "printing/pwg_raster_settings.h" |
27 #include "printing/units.h" | 35 #include "printing/units.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "ui/gfx/geometry/size.h" | 37 #include "ui/gfx/geometry/size.h" |
30 | 38 |
| 39 using device::MockUsbDevice; |
| 40 using device::MockUsbService; |
| 41 using extensions::DictionaryBuilder; |
| 42 using extensions::Extension; |
31 using extensions::PrinterProviderAPI; | 43 using extensions::PrinterProviderAPI; |
32 using extensions::PrinterProviderPrintJob; | 44 using extensions::PrinterProviderPrintJob; |
| 45 using extensions::TestExtensionEnvironment; |
33 using local_discovery::PWGRasterConverter; | 46 using local_discovery::PWGRasterConverter; |
34 | 47 |
35 namespace { | 48 namespace { |
36 | 49 |
37 // Printer id used for requests in tests. | 50 // Printer id used for requests in tests. |
38 const char kPrinterId[] = "printer_id"; | 51 const char kPrinterId[] = "printer_id"; |
39 | 52 |
40 // Printer list used a result for getPrinters. | 53 // Printer list used a result for getPrinters. |
41 const char kPrinterDescriptionList[] = | 54 const char kPrinterDescriptionList[] = |
42 "[{" | 55 "[{" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 124 |
112 // Print ticket that has duplex parameter set. | 125 // Print ticket that has duplex parameter set. |
113 const char kPrintTicketWithDuplex[] = | 126 const char kPrintTicketWithDuplex[] = |
114 "{" | 127 "{" |
115 " \"version\": \"1.0\"," | 128 " \"version\": \"1.0\"," |
116 " \"print\": {" | 129 " \"print\": {" |
117 " \"duplex\": {\"type\": \"LONG_EDGE\"}" | 130 " \"duplex\": {\"type\": \"LONG_EDGE\"}" |
118 " }" | 131 " }" |
119 "}"; | 132 "}"; |
120 | 133 |
| 134 // An extension with permission for 1 printer it supports. |
| 135 const char kExtension1[] = |
| 136 "{" |
| 137 " \"name\": \"Provider 1\"," |
| 138 " \"app\": {" |
| 139 " \"background\": {" |
| 140 " \"scripts\": [\"background.js\"]" |
| 141 " }" |
| 142 " }," |
| 143 " \"permissions\": [" |
| 144 " \"printerProvider\"," |
| 145 " \"usb\"," |
| 146 " {" |
| 147 " \"usbDevices\": [" |
| 148 " { \"vendorId\": 0, \"productId\": 1 }" |
| 149 " ]" |
| 150 " }," |
| 151 " ]," |
| 152 " \"usb_printers\": {" |
| 153 " \"filters\": [" |
| 154 " { \"vendorId\": 0, \"productId\": 0 }," |
| 155 " { \"vendorId\": 0, \"productId\": 1 }" |
| 156 " ]" |
| 157 " }" |
| 158 "}"; |
| 159 |
| 160 // An extension with permission for none of the printers it supports. |
| 161 const char kExtension2[] = |
| 162 "{" |
| 163 " \"name\": \"Provider 2\"," |
| 164 " \"app\": {" |
| 165 " \"background\": {" |
| 166 " \"scripts\": [\"background.js\"]" |
| 167 " }" |
| 168 " }," |
| 169 " \"permissions\": [ \"printerProvider\", \"usb\" ]," |
| 170 " \"usb_printers\": {" |
| 171 " \"filters\": [" |
| 172 " { \"vendorId\": 0, \"productId\": 0 }," |
| 173 " { \"vendorId\": 0, \"productId\": 1 }" |
| 174 " ]" |
| 175 " }" |
| 176 "}"; |
| 177 |
121 const char kContentTypePDF[] = "application/pdf"; | 178 const char kContentTypePDF[] = "application/pdf"; |
122 const char kContentTypePWG[] = "image/pwg-raster"; | 179 const char kContentTypePWG[] = "image/pwg-raster"; |
123 | 180 |
124 // Print request status considered to be successful by fake PrinterProviderAPI. | 181 // Print request status considered to be successful by fake PrinterProviderAPI. |
125 const char kPrintRequestSuccess[] = "OK"; | 182 const char kPrintRequestSuccess[] = "OK"; |
126 | 183 |
127 // Used as a callback to StartGetPrinters in tests. | 184 // Used as a callback to StartGetPrinters in tests. |
128 // Increases |*call_count| and records values returned by StartGetPrinters. | 185 // Increases |*call_count| and records values returned by StartGetPrinters. |
129 void RecordPrinterList(size_t* call_count, | 186 void RecordPrinterList(size_t* call_count, |
130 scoped_ptr<base::ListValue>* printers_out, | 187 scoped_ptr<base::ListValue>* printers_out, |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 pending_capability_callbacks_; | 406 pending_capability_callbacks_; |
350 std::vector<PrintRequestInfo> pending_print_requests_; | 407 std::vector<PrintRequestInfo> pending_print_requests_; |
351 | 408 |
352 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); | 409 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); |
353 }; | 410 }; |
354 | 411 |
355 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { | 412 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { |
356 return new FakePrinterProviderAPI(); | 413 return new FakePrinterProviderAPI(); |
357 } | 414 } |
358 | 415 |
| 416 class FakeDeviceClient : public device::DeviceClient { |
| 417 public: |
| 418 FakeDeviceClient() {} |
| 419 |
| 420 // device::DeviceClient implementation: |
| 421 device::UsbService* GetUsbService() override { |
| 422 DCHECK(usb_service_); |
| 423 return usb_service_; |
| 424 } |
| 425 |
| 426 void set_usb_service(device::UsbService* service) { usb_service_ = service; } |
| 427 |
| 428 private: |
| 429 device::UsbService* usb_service_ = nullptr; |
| 430 }; |
| 431 |
359 } // namespace | 432 } // namespace |
360 | 433 |
361 class ExtensionPrinterHandlerTest : public testing::Test { | 434 class ExtensionPrinterHandlerTest : public testing::Test { |
362 public: | 435 public: |
363 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} | 436 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} |
364 ~ExtensionPrinterHandlerTest() override = default; | 437 ~ExtensionPrinterHandlerTest() override = default; |
365 | 438 |
366 void SetUp() override { | 439 void SetUp() override { |
367 TestingProfile::Builder profile_builder; | 440 extensions::PrinterProviderAPIFactory::GetInstance()->SetTestingFactory( |
368 profile_builder.AddTestingFactory( | 441 env_.profile(), &BuildTestingPrinterProviderAPI); |
369 extensions::PrinterProviderAPIFactory::GetInstance(), | |
370 &BuildTestingPrinterProviderAPI); | |
371 profile_ = profile_builder.Build(); | |
372 | |
373 extension_printer_handler_.reset(new ExtensionPrinterHandler( | 442 extension_printer_handler_.reset(new ExtensionPrinterHandler( |
374 profile_.get(), base::MessageLoop::current()->task_runner())); | 443 env_.profile(), base::MessageLoop::current()->task_runner())); |
375 | 444 |
376 pwg_raster_converter_ = new FakePWGRasterConverter(); | 445 pwg_raster_converter_ = new FakePWGRasterConverter(); |
377 extension_printer_handler_->SetPwgRasterConverterForTesting( | 446 extension_printer_handler_->SetPwgRasterConverterForTesting( |
378 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); | 447 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); |
| 448 device_client_.set_usb_service(&usb_service_); |
379 } | 449 } |
380 | 450 |
381 protected: | 451 protected: |
382 FakePrinterProviderAPI* GetPrinterProviderAPI() { | 452 FakePrinterProviderAPI* GetPrinterProviderAPI() { |
383 return static_cast<FakePrinterProviderAPI*>( | 453 return static_cast<FakePrinterProviderAPI*>( |
384 extensions::PrinterProviderAPIFactory::GetInstance() | 454 extensions::PrinterProviderAPIFactory::GetInstance() |
385 ->GetForBrowserContext(profile_.get())); | 455 ->GetForBrowserContext(env_.profile())); |
386 } | 456 } |
387 | 457 |
| 458 MockUsbService usb_service_; |
| 459 TestExtensionEnvironment env_; |
388 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; | 460 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; |
389 | 461 |
390 FakePWGRasterConverter* pwg_raster_converter_; | 462 FakePWGRasterConverter* pwg_raster_converter_; |
391 | 463 |
392 private: | 464 private: |
393 content::TestBrowserThreadBundle thread_bundle_; | 465 FakeDeviceClient device_client_; |
394 | |
395 scoped_ptr<TestingProfile> profile_; | |
396 | 466 |
397 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); | 467 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); |
398 }; | 468 }; |
399 | 469 |
400 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { | 470 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { |
401 size_t call_count = 0; | 471 size_t call_count = 0; |
402 scoped_ptr<base::ListValue> printers; | 472 scoped_ptr<base::ListValue> printers; |
403 bool is_done = false; | 473 bool is_done = false; |
404 | 474 |
405 extension_printer_handler_->StartGetPrinters( | 475 extension_printer_handler_->StartGetPrinters( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 std::string error; | 512 std::string error; |
443 scoped_ptr<base::ListValue> original_printers( | 513 scoped_ptr<base::ListValue> original_printers( |
444 GetJSONAsListValue(kPrinterDescriptionList, &error)); | 514 GetJSONAsListValue(kPrinterDescriptionList, &error)); |
445 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; | 515 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; |
446 | 516 |
447 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); | 517 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); |
448 | 518 |
449 EXPECT_EQ(0u, call_count); | 519 EXPECT_EQ(0u, call_count); |
450 } | 520 } |
451 | 521 |
| 522 TEST_F(ExtensionPrinterHandlerTest, GetUsbPrinters) { |
| 523 scoped_refptr<MockUsbDevice> device0 = |
| 524 new MockUsbDevice(0, 0, "Google", "USB Printer", ""); |
| 525 usb_service_.AddDevice(device0); |
| 526 scoped_refptr<MockUsbDevice> device1 = |
| 527 new MockUsbDevice(0, 1, "Google", "USB Printer", ""); |
| 528 usb_service_.AddDevice(device1); |
| 529 |
| 530 const Extension* extension_1 = env_.MakeExtension( |
| 531 *base::test::ParseJson(kExtension1), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| 532 const Extension* extension_2 = env_.MakeExtension( |
| 533 *base::test::ParseJson(kExtension2), "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); |
| 534 |
| 535 extensions::DevicePermissionsManager* permissions_manager = |
| 536 extensions::DevicePermissionsManager::Get(env_.profile()); |
| 537 permissions_manager->AllowUsbDevice(extension_2->id(), device0); |
| 538 |
| 539 size_t call_count = 0; |
| 540 scoped_ptr<base::ListValue> printers; |
| 541 bool is_done = false; |
| 542 extension_printer_handler_->StartGetPrinters( |
| 543 base::Bind(&RecordPrinterList, &call_count, &printers, &is_done)); |
| 544 |
| 545 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI(); |
| 546 ASSERT_TRUE(fake_api); |
| 547 ASSERT_EQ(1u, fake_api->pending_get_printers_count()); |
| 548 |
| 549 EXPECT_EQ(1u, call_count); |
| 550 EXPECT_FALSE(is_done); |
| 551 EXPECT_TRUE(printers.get()); |
| 552 EXPECT_EQ(2u, printers->GetSize()); |
| 553 scoped_ptr<base::DictionaryValue> extension_1_entry( |
| 554 DictionaryBuilder() |
| 555 .Set("id", base::StringPrintf("provisional-usb:%s:%u", |
| 556 extension_1->id().c_str(), |
| 557 device0->unique_id())) |
| 558 .Set("name", "USB Printer") |
| 559 .Set("extensionName", "Provider 1") |
| 560 .Set("extensionId", extension_1->id()) |
| 561 .Set("provisional", true) |
| 562 .Build()); |
| 563 scoped_ptr<base::DictionaryValue> extension_2_entry( |
| 564 DictionaryBuilder() |
| 565 .Set("id", base::StringPrintf("provisional-usb:%s:%u", |
| 566 extension_2->id().c_str(), |
| 567 device1->unique_id())) |
| 568 .Set("name", "USB Printer") |
| 569 .Set("extensionName", "Provider 2") |
| 570 .Set("extensionId", extension_2->id()) |
| 571 .Set("provisional", true) |
| 572 .Build()); |
| 573 EXPECT_TRUE(printers->Find(*extension_1_entry) != printers->end()); |
| 574 EXPECT_TRUE(printers->Find(*extension_2_entry) != printers->end()); |
| 575 |
| 576 fake_api->TriggerNextGetPrintersCallback(base::ListValue(), true); |
| 577 |
| 578 EXPECT_EQ(2u, call_count); |
| 579 EXPECT_TRUE(is_done); |
| 580 EXPECT_TRUE(printers.get()); |
| 581 EXPECT_EQ(0u, printers->GetSize()); // RecordPrinterList resets |printers|. |
| 582 } |
| 583 |
452 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { | 584 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { |
453 size_t call_count = 0; | 585 size_t call_count = 0; |
454 std::string destination_id; | 586 std::string destination_id; |
455 scoped_ptr<base::DictionaryValue> capability; | 587 scoped_ptr<base::DictionaryValue> capability; |
456 | 588 |
457 extension_printer_handler_->StartGetCapability( | 589 extension_printer_handler_->StartGetCapability( |
458 kPrinterId, | 590 kPrinterId, |
459 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); | 591 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); |
460 | 592 |
461 EXPECT_EQ(0u, call_count); | 593 EXPECT_EQ(0u, call_count); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 extension_printer_handler_->StartPrint( | 919 extension_printer_handler_->StartPrint( |
788 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, | 920 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, |
789 kEmptyPrintTicket, gfx::Size(100, 100), print_data, | 921 kEmptyPrintTicket, gfx::Size(100, 100), print_data, |
790 base::Bind(&RecordPrintResult, &call_count, &success, &status)); | 922 base::Bind(&RecordPrintResult, &call_count, &success, &status)); |
791 | 923 |
792 EXPECT_EQ(1u, call_count); | 924 EXPECT_EQ(1u, call_count); |
793 | 925 |
794 EXPECT_FALSE(success); | 926 EXPECT_FALSE(success); |
795 EXPECT_EQ("INVALID_DATA", status); | 927 EXPECT_EQ("INVALID_DATA", status); |
796 } | 928 } |
OLD | NEW |