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/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/values_test_util.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/extensions/test_extension_environment.h" |
18 #include "chrome/browser/local_discovery/pwg_raster_converter.h" | 20 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
19 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" | 21 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h" |
20 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "device/core/device_client.h" |
| 24 #include "device/usb/mock_usb_device.h" |
| 25 #include "device/usb/mock_usb_service.h" |
| 26 #include "extensions/browser/api/device_permissions_manager.h" |
22 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 27 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
23 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 28 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
24 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 29 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 30 #include "extensions/common/extension.h" |
| 31 #include "extensions/common/value_builder.h" |
25 #include "printing/pdf_render_settings.h" | 32 #include "printing/pdf_render_settings.h" |
26 #include "printing/pwg_raster_settings.h" | 33 #include "printing/pwg_raster_settings.h" |
27 #include "printing/units.h" | 34 #include "printing/units.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "ui/gfx/geometry/size.h" | 36 #include "ui/gfx/geometry/size.h" |
30 | 37 |
| 38 using device::MockUsbDevice; |
| 39 using device::MockUsbService; |
| 40 using extensions::DictionaryBuilder; |
| 41 using extensions::Extension; |
31 using extensions::PrinterProviderAPI; | 42 using extensions::PrinterProviderAPI; |
32 using extensions::PrinterProviderPrintJob; | 43 using extensions::PrinterProviderPrintJob; |
| 44 using extensions::TestExtensionEnvironment; |
33 using local_discovery::PWGRasterConverter; | 45 using local_discovery::PWGRasterConverter; |
34 | 46 |
35 namespace { | 47 namespace { |
36 | 48 |
37 // Printer id used for requests in tests. | 49 // Printer id used for requests in tests. |
38 const char kPrinterId[] = "printer_id"; | 50 const char kPrinterId[] = "printer_id"; |
39 | 51 |
40 // Printer list used a result for getPrinters. | 52 // Printer list used a result for getPrinters. |
41 const char kPrinterDescriptionList[] = | 53 const char kPrinterDescriptionList[] = |
42 "[{" | 54 "[{" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 void RecordPrintResult(size_t* call_count, | 165 void RecordPrintResult(size_t* call_count, |
154 bool* success_out, | 166 bool* success_out, |
155 std::string* status_out, | 167 std::string* status_out, |
156 bool success, | 168 bool success, |
157 const std::string& status) { | 169 const std::string& status) { |
158 ++(*call_count); | 170 ++(*call_count); |
159 *success_out = success; | 171 *success_out = success; |
160 *status_out = status; | 172 *status_out = status; |
161 } | 173 } |
162 | 174 |
| 175 // Used as a callback to StartGetUsbPrinters in tests. |
| 176 void RecordListValue(scoped_ptr<base::ListValue>* list_value_out, |
| 177 const base::Closure& callback, |
| 178 const base::ListValue& list_value) { |
| 179 list_value_out->reset(list_value.DeepCopy()); |
| 180 callback.Run(); |
| 181 } |
| 182 |
163 // Converts JSON string to base::ListValue object. | 183 // Converts JSON string to base::ListValue object. |
164 // On failure, returns NULL and fills |*error| string. | 184 // On failure, returns NULL and fills |*error| string. |
165 scoped_ptr<base::ListValue> GetJSONAsListValue(const std::string& json, | 185 scoped_ptr<base::ListValue> GetJSONAsListValue(const std::string& json, |
166 std::string* error) { | 186 std::string* error) { |
167 scoped_ptr<base::Value> deserialized( | 187 scoped_ptr<base::Value> deserialized( |
168 JSONStringValueDeserializer(json).Deserialize(NULL, error)); | 188 JSONStringValueDeserializer(json).Deserialize(NULL, error)); |
169 if (!deserialized) | 189 if (!deserialized) |
170 return scoped_ptr<base::ListValue>(); | 190 return scoped_ptr<base::ListValue>(); |
171 base::ListValue* list = nullptr; | 191 base::ListValue* list = nullptr; |
172 if (!deserialized->GetAsList(&list)) { | 192 if (!deserialized->GetAsList(&list)) { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 pending_capability_callbacks_; | 369 pending_capability_callbacks_; |
350 std::vector<PrintRequestInfo> pending_print_requests_; | 370 std::vector<PrintRequestInfo> pending_print_requests_; |
351 | 371 |
352 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); | 372 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI); |
353 }; | 373 }; |
354 | 374 |
355 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { | 375 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) { |
356 return new FakePrinterProviderAPI(); | 376 return new FakePrinterProviderAPI(); |
357 } | 377 } |
358 | 378 |
| 379 class FakeDeviceClient : public device::DeviceClient { |
| 380 public: |
| 381 FakeDeviceClient() {} |
| 382 |
| 383 // device::DeviceClient implementation: |
| 384 device::UsbService* GetUsbService() override { |
| 385 DCHECK(usb_service_); |
| 386 return usb_service_; |
| 387 } |
| 388 |
| 389 void set_usb_service(device::UsbService* service) { usb_service_ = service; } |
| 390 |
| 391 private: |
| 392 device::UsbService* usb_service_ = nullptr; |
| 393 }; |
| 394 |
359 } // namespace | 395 } // namespace |
360 | 396 |
361 class ExtensionPrinterHandlerTest : public testing::Test { | 397 class ExtensionPrinterHandlerTest : public testing::Test { |
362 public: | 398 public: |
363 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} | 399 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {} |
364 ~ExtensionPrinterHandlerTest() override = default; | 400 ~ExtensionPrinterHandlerTest() override = default; |
365 | 401 |
366 void SetUp() override { | 402 void SetUp() override { |
367 TestingProfile::Builder profile_builder; | 403 env_.reset(new TestExtensionEnvironment()); |
368 profile_builder.AddTestingFactory( | 404 extensions::PrinterProviderAPIFactory::GetInstance()->SetTestingFactory( |
369 extensions::PrinterProviderAPIFactory::GetInstance(), | 405 env_->profile(), &BuildTestingPrinterProviderAPI); |
370 &BuildTestingPrinterProviderAPI); | |
371 profile_ = profile_builder.Build(); | |
372 | |
373 extension_printer_handler_.reset(new ExtensionPrinterHandler( | 406 extension_printer_handler_.reset(new ExtensionPrinterHandler( |
374 profile_.get(), base::MessageLoop::current()->task_runner())); | 407 env_->profile(), base::MessageLoop::current()->task_runner())); |
375 | 408 |
376 pwg_raster_converter_ = new FakePWGRasterConverter(); | 409 pwg_raster_converter_ = new FakePWGRasterConverter(); |
377 extension_printer_handler_->SetPwgRasterConverterForTesting( | 410 extension_printer_handler_->SetPwgRasterConverterForTesting( |
378 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); | 411 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); |
| 412 device_client_.set_usb_service(&usb_service_); |
379 } | 413 } |
380 | 414 |
381 protected: | 415 protected: |
382 FakePrinterProviderAPI* GetPrinterProviderAPI() { | 416 FakePrinterProviderAPI* GetPrinterProviderAPI() { |
383 return static_cast<FakePrinterProviderAPI*>( | 417 return static_cast<FakePrinterProviderAPI*>( |
384 extensions::PrinterProviderAPIFactory::GetInstance() | 418 extensions::PrinterProviderAPIFactory::GetInstance() |
385 ->GetForBrowserContext(profile_.get())); | 419 ->GetForBrowserContext(env_->profile())); |
386 } | 420 } |
387 | 421 |
| 422 MockUsbService usb_service_; |
| 423 scoped_ptr<TestExtensionEnvironment> env_; |
388 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; | 424 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; |
389 | 425 |
390 FakePWGRasterConverter* pwg_raster_converter_; | 426 FakePWGRasterConverter* pwg_raster_converter_; |
391 | 427 |
392 private: | 428 private: |
393 content::TestBrowserThreadBundle thread_bundle_; | 429 FakeDeviceClient device_client_; |
394 | |
395 scoped_ptr<TestingProfile> profile_; | |
396 | 430 |
397 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); | 431 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); |
398 }; | 432 }; |
399 | 433 |
400 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { | 434 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { |
401 size_t call_count = 0; | 435 size_t call_count = 0; |
402 scoped_ptr<base::ListValue> printers; | 436 scoped_ptr<base::ListValue> printers; |
403 bool is_done = false; | 437 bool is_done = false; |
404 | 438 |
405 extension_printer_handler_->StartGetPrinters( | 439 extension_printer_handler_->StartGetPrinters( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 std::string error; | 476 std::string error; |
443 scoped_ptr<base::ListValue> original_printers( | 477 scoped_ptr<base::ListValue> original_printers( |
444 GetJSONAsListValue(kPrinterDescriptionList, &error)); | 478 GetJSONAsListValue(kPrinterDescriptionList, &error)); |
445 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; | 479 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; |
446 | 480 |
447 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); | 481 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); |
448 | 482 |
449 EXPECT_EQ(0u, call_count); | 483 EXPECT_EQ(0u, call_count); |
450 } | 484 } |
451 | 485 |
| 486 TEST_F(ExtensionPrinterHandlerTest, GetUsbPrinters) { |
| 487 scoped_refptr<MockUsbDevice> device0 = |
| 488 new MockUsbDevice(0, 0, "Google", "USB Printer", ""); |
| 489 usb_service_.AddDevice(device0); |
| 490 scoped_refptr<MockUsbDevice> device1 = |
| 491 new MockUsbDevice(0, 1, "Google", "USB Printer", ""); |
| 492 usb_service_.AddDevice(device1); |
| 493 |
| 494 const Extension* extension_1 = env_->MakeExtension(*base::test::ParseJson( |
| 495 "{" |
| 496 " \"name\": \"Provider 1\"," |
| 497 " \"app\": {" |
| 498 " \"background\": {" |
| 499 " \"scripts\": [\"background.js\"]" |
| 500 " }" |
| 501 " }," |
| 502 " \"permissions\": [" |
| 503 " \"printerProvider\"," |
| 504 " \"usb\"," |
| 505 " {" |
| 506 " \"usbDevices\": [" |
| 507 " { \"vendorId\": 0, \"productId\": 1 }" |
| 508 " ]" |
| 509 " }," |
| 510 " ]," |
| 511 " \"usb_printers\": {" |
| 512 " \"filters\": [" |
| 513 " { \"vendorId\": 0, \"productId\": 0 }," |
| 514 " { \"vendorId\": 0, \"productId\": 1 }" |
| 515 " ]" |
| 516 " }" |
| 517 "}"), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| 518 const Extension* extension_2 = env_->MakeExtension(*base::test::ParseJson( |
| 519 "{" |
| 520 " \"name\": \"Provider 2\"," |
| 521 " \"app\": {" |
| 522 " \"background\": {" |
| 523 " \"scripts\": [\"background.js\"]" |
| 524 " }" |
| 525 " }," |
| 526 " \"permissions\": [ \"printerProvider\", \"usb\" ]," |
| 527 " \"usb_printers\": {" |
| 528 " \"filters\": [" |
| 529 " { \"vendorId\": 0, \"productId\": 0 }," |
| 530 " { \"vendorId\": 0, \"productId\": 1 }" |
| 531 " ]" |
| 532 " }" |
| 533 "}"), "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); |
| 534 |
| 535 extensions::DevicePermissionsManager* permissions_manager = |
| 536 extensions::DevicePermissionsManager::Get(env_->profile()); |
| 537 permissions_manager->AllowUsbDevice(extension_2->id(), device0); |
| 538 |
| 539 scoped_ptr<base::ListValue> printers; |
| 540 base::RunLoop run_loop; |
| 541 extension_printer_handler_->StartGetUsbPrinters( |
| 542 base::Bind(&RecordListValue, &printers, run_loop.QuitClosure())); |
| 543 run_loop.Run(); |
| 544 |
| 545 EXPECT_TRUE(printers.get()); |
| 546 EXPECT_EQ(2u, printers->GetSize()); |
| 547 |
| 548 scoped_ptr<base::DictionaryValue> extension_1_entry( |
| 549 DictionaryBuilder().Set("extensionName", "Provider 1") |
| 550 .Set("extensionId", extension_1->id()).Build()); |
| 551 scoped_ptr<base::DictionaryValue> extension_2_entry( |
| 552 DictionaryBuilder().Set("extensionName", "Provider 2") |
| 553 .Set("extensionId", extension_2->id()).Build()); |
| 554 |
| 555 for (size_t i = 0; i < printers->GetSize(); ++i) { |
| 556 const base::DictionaryValue* printer_info; |
| 557 ASSERT_TRUE(printers->GetDictionary(i, &printer_info)); |
| 558 |
| 559 EXPECT_EQ(3u, printer_info->size()); |
| 560 std::string name; |
| 561 EXPECT_TRUE(printer_info->GetString("name", &name)); |
| 562 EXPECT_EQ("USB Printer", name); |
| 563 uint32 device_id; |
| 564 EXPECT_TRUE(printer_info->GetInteger( |
| 565 "usbDevice", reinterpret_cast<int*>(&device_id))); |
| 566 const base::ListValue* extensions; |
| 567 EXPECT_TRUE(printer_info->GetList("extensions", &extensions)); |
| 568 EXPECT_EQ(1u, extensions->GetSize()); |
| 569 |
| 570 if (device_id == device0->unique_id()) { |
| 571 EXPECT_TRUE(extensions->Find(*extension_1_entry) != extensions->end()); |
| 572 } else if (device_id == device1->unique_id()) { |
| 573 EXPECT_TRUE(extensions->Find(*extension_2_entry) != extensions->end()); |
| 574 } else { |
| 575 FAIL(); |
| 576 } |
| 577 } |
| 578 } |
| 579 |
452 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { | 580 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { |
453 size_t call_count = 0; | 581 size_t call_count = 0; |
454 std::string destination_id; | 582 std::string destination_id; |
455 scoped_ptr<base::DictionaryValue> capability; | 583 scoped_ptr<base::DictionaryValue> capability; |
456 | 584 |
457 extension_printer_handler_->StartGetCapability( | 585 extension_printer_handler_->StartGetCapability( |
458 kPrinterId, | 586 kPrinterId, |
459 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); | 587 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); |
460 | 588 |
461 EXPECT_EQ(0u, call_count); | 589 EXPECT_EQ(0u, call_count); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 extension_printer_handler_->StartPrint( | 915 extension_printer_handler_->StartPrint( |
788 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, | 916 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, |
789 kEmptyPrintTicket, gfx::Size(100, 100), print_data, | 917 kEmptyPrintTicket, gfx::Size(100, 100), print_data, |
790 base::Bind(&RecordPrintResult, &call_count, &success, &status)); | 918 base::Bind(&RecordPrintResult, &call_count, &success, &status)); |
791 | 919 |
792 EXPECT_EQ(1u, call_count); | 920 EXPECT_EQ(1u, call_count); |
793 | 921 |
794 EXPECT_FALSE(success); | 922 EXPECT_FALSE(success); |
795 EXPECT_EQ("INVALID_DATA", status); | 923 EXPECT_EQ("INVALID_DATA", status); |
796 } | 924 } |
OLD | NEW |