| 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" | |
| 17 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/test/values_test_util.h" | |
| 19 #include "base/values.h" | 17 #include "base/values.h" |
| 20 #include "chrome/browser/extensions/test_extension_environment.h" | |
| 21 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
| 22 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" | 19 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 24 #include "device/core/device_client.h" | 21 #include "content/public/test/test_browser_thread_bundle.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" | |
| 28 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 22 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 29 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 23 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
| 30 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 24 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 31 #include "extensions/common/extension.h" | |
| 32 #include "extensions/common/value_builder.h" | |
| 33 #include "printing/pdf_render_settings.h" | 25 #include "printing/pdf_render_settings.h" |
| 34 #include "printing/pwg_raster_settings.h" | 26 #include "printing/pwg_raster_settings.h" |
| 35 #include "printing/units.h" | 27 #include "printing/units.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 37 #include "ui/gfx/geometry/size.h" | 29 #include "ui/gfx/geometry/size.h" |
| 38 | 30 |
| 39 using device::MockUsbDevice; | |
| 40 using device::MockUsbService; | |
| 41 using extensions::DictionaryBuilder; | |
| 42 using extensions::Extension; | |
| 43 using extensions::PrinterProviderAPI; | 31 using extensions::PrinterProviderAPI; |
| 44 using extensions::PrinterProviderPrintJob; | 32 using extensions::PrinterProviderPrintJob; |
| 45 using extensions::TestExtensionEnvironment; | |
| 46 using local_discovery::PWGRasterConverter; | 33 using local_discovery::PWGRasterConverter; |
| 47 | 34 |
| 48 namespace { | 35 namespace { |
| 49 | 36 |
| 50 // Printer id used for requests in tests. | 37 // Printer id used for requests in tests. |
| 51 const char kPrinterId[] = "printer_id"; | 38 const char kPrinterId[] = "printer_id"; |
| 52 | 39 |
| 53 // Printer list used a result for getPrinters. | 40 // Printer list used a result for getPrinters. |
| 54 const char kPrinterDescriptionList[] = | 41 const char kPrinterDescriptionList[] = |
| 55 "[{" | 42 "[{" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 111 |
| 125 // Print ticket that has duplex parameter set. | 112 // Print ticket that has duplex parameter set. |
| 126 const char kPrintTicketWithDuplex[] = | 113 const char kPrintTicketWithDuplex[] = |
| 127 "{" | 114 "{" |
| 128 " \"version\": \"1.0\"," | 115 " \"version\": \"1.0\"," |
| 129 " \"print\": {" | 116 " \"print\": {" |
| 130 " \"duplex\": {\"type\": \"LONG_EDGE\"}" | 117 " \"duplex\": {\"type\": \"LONG_EDGE\"}" |
| 131 " }" | 118 " }" |
| 132 "}"; | 119 "}"; |
| 133 | 120 |
| 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 | |
| 178 const char kContentTypePDF[] = "application/pdf"; | 121 const char kContentTypePDF[] = "application/pdf"; |
| 179 const char kContentTypePWG[] = "image/pwg-raster"; | 122 const char kContentTypePWG[] = "image/pwg-raster"; |
| 180 | 123 |
| 181 // Print request status considered to be successful by fake PrinterProviderAPI. | 124 // Print request status considered to be successful by fake PrinterProviderAPI. |
| 182 const char kPrintRequestSuccess[] = "OK"; | 125 const char kPrintRequestSuccess[] = "OK"; |
| 183 | 126 |
| 184 // Used as a callback to StartGetPrinters in tests. | 127 // Used as a callback to StartGetPrinters in tests. |
| 185 // Increases |*call_count| and records values returned by StartGetPrinters. | 128 // Increases |*call_count| and records values returned by StartGetPrinters. |
| 186 void RecordPrinterList(size_t* call_count, | 129 void RecordPrinterList(size_t* call_count, |
| 187 scoped_ptr<base::ListValue>* printers_out, | 130 scoped_ptr<base::ListValue>* printers_out, |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 pending_capability_callbacks_; | 349 pending_capability_callbacks_; |
| 407 std::vector<PrintRequestInfo> pending_print_requests_; | 350 std::vector<PrintRequestInfo> pending_print_requests_; |
| 408 | 351 |
| 409 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); | 352 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); |
| 410 }; | 353 }; |
| 411 | 354 |
| 412 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { | 355 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { |
| 413 return new FakePrinterProviderAPI(); | 356 return new FakePrinterProviderAPI(); |
| 414 } | 357 } |
| 415 | 358 |
| 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 | |
| 432 } // namespace | 359 } // namespace |
| 433 | 360 |
| 434 class ExtensionPrinterHandlerTest : public testing::Test { | 361 class ExtensionPrinterHandlerTest : public testing::Test { |
| 435 public: | 362 public: |
| 436 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} | 363 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} |
| 437 ~ExtensionPrinterHandlerTest() override = default; | 364 ~ExtensionPrinterHandlerTest() override = default; |
| 438 | 365 |
| 439 void SetUp() override { | 366 void SetUp() override { |
| 440 extensions::PrinterProviderAPIFactory::GetInstance()->SetTestingFactory( | 367 TestingProfile::Builder profile_builder; |
| 441 env_.profile(), &BuildTestingPrinterProviderAPI); | 368 profile_builder.AddTestingFactory( |
| 369 extensions::PrinterProviderAPIFactory::GetInstance(), |
| 370 &BuildTestingPrinterProviderAPI); |
| 371 profile_ = profile_builder.Build(); |
| 372 |
| 442 extension_printer_handler_.reset(new ExtensionPrinterHandler( | 373 extension_printer_handler_.reset(new ExtensionPrinterHandler( |
| 443 env_.profile(), base::MessageLoop::current()->task_runner())); | 374 profile_.get(), base::MessageLoop::current()->task_runner())); |
| 444 | 375 |
| 445 pwg_raster_converter_ = new FakePWGRasterConverter(); | 376 pwg_raster_converter_ = new FakePWGRasterConverter(); |
| 446 extension_printer_handler_->SetPwgRasterConverterForTesting( | 377 extension_printer_handler_->SetPwgRasterConverterForTesting( |
| 447 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); | 378 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); |
| 448 device_client_.set_usb_service(&usb_service_); | |
| 449 } | 379 } |
| 450 | 380 |
| 451 protected: | 381 protected: |
| 452 FakePrinterProviderAPI* GetPrinterProviderAPI() { | 382 FakePrinterProviderAPI* GetPrinterProviderAPI() { |
| 453 return static_cast<FakePrinterProviderAPI*>( | 383 return static_cast<FakePrinterProviderAPI*>( |
| 454 extensions::PrinterProviderAPIFactory::GetInstance() | 384 extensions::PrinterProviderAPIFactory::GetInstance() |
| 455 ->GetForBrowserContext(env_.profile())); | 385 ->GetForBrowserContext(profile_.get())); |
| 456 } | 386 } |
| 457 | 387 |
| 458 MockUsbService usb_service_; | |
| 459 TestExtensionEnvironment env_; | |
| 460 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; | 388 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; |
| 461 | 389 |
| 462 FakePWGRasterConverter* pwg_raster_converter_; | 390 FakePWGRasterConverter* pwg_raster_converter_; |
| 463 | 391 |
| 464 private: | 392 private: |
| 465 FakeDeviceClient device_client_; | 393 content::TestBrowserThreadBundle thread_bundle_; |
| 394 |
| 395 scoped_ptr<TestingProfile> profile_; |
| 466 | 396 |
| 467 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); | 397 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); |
| 468 }; | 398 }; |
| 469 | 399 |
| 470 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { | 400 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { |
| 471 size_t call_count = 0; | 401 size_t call_count = 0; |
| 472 scoped_ptr<base::ListValue> printers; | 402 scoped_ptr<base::ListValue> printers; |
| 473 bool is_done = false; | 403 bool is_done = false; |
| 474 | 404 |
| 475 extension_printer_handler_->StartGetPrinters( | 405 extension_printer_handler_->StartGetPrinters( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 std::string error; | 442 std::string error; |
| 513 scoped_ptr<base::ListValue> original_printers( | 443 scoped_ptr<base::ListValue> original_printers( |
| 514 GetJSONAsListValue(kPrinterDescriptionList, &error)); | 444 GetJSONAsListValue(kPrinterDescriptionList, &error)); |
| 515 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; | 445 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; |
| 516 | 446 |
| 517 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); | 447 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); |
| 518 | 448 |
| 519 EXPECT_EQ(0u, call_count); | 449 EXPECT_EQ(0u, call_count); |
| 520 } | 450 } |
| 521 | 451 |
| 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 | |
| 584 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { | 452 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { |
| 585 size_t call_count = 0; | 453 size_t call_count = 0; |
| 586 std::string destination_id; | 454 std::string destination_id; |
| 587 scoped_ptr<base::DictionaryValue> capability; | 455 scoped_ptr<base::DictionaryValue> capability; |
| 588 | 456 |
| 589 extension_printer_handler_->StartGetCapability( | 457 extension_printer_handler_->StartGetCapability( |
| 590 kPrinterId, | 458 kPrinterId, |
| 591 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); | 459 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); |
| 592 | 460 |
| 593 EXPECT_EQ(0u, call_count); | 461 EXPECT_EQ(0u, call_count); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 919 extension_printer_handler_->StartPrint( | 787 extension_printer_handler_->StartPrint( |
| 920 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, | 788 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, |
| 921 kEmptyPrintTicket, gfx::Size(100, 100), print_data, | 789 kEmptyPrintTicket, gfx::Size(100, 100), print_data, |
| 922 base::Bind(&RecordPrintResult, &call_count, &success, &status)); | 790 base::Bind(&RecordPrintResult, &call_count, &success, &status)); |
| 923 | 791 |
| 924 EXPECT_EQ(1u, call_count); | 792 EXPECT_EQ(1u, call_count); |
| 925 | 793 |
| 926 EXPECT_FALSE(success); | 794 EXPECT_FALSE(success); |
| 927 EXPECT_EQ("INVALID_DATA", status); | 795 EXPECT_EQ("INVALID_DATA", status); |
| 928 } | 796 } |
| OLD | NEW |