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 env_.reset(new TestExtensionEnvironment()); |
368 profile_builder.AddTestingFactory( | 441 extensions::PrinterProviderAPIFactory::GetInstance()->SetTestingFactory( |
369 extensions::PrinterProviderAPIFactory::GetInstance(), | 442 env_->profile(), &BuildTestingPrinterProviderAPI); |
370 &BuildTestingPrinterProviderAPI); | |
371 profile_ = profile_builder.Build(); | |
372 | |
373 extension_printer_handler_.reset(new ExtensionPrinterHandler( | 443 extension_printer_handler_.reset(new ExtensionPrinterHandler( |
374 profile_.get(), base::MessageLoop::current()->task_runner())); | 444 env_->profile(), base::MessageLoop::current()->task_runner())); |
375 | 445 |
376 pwg_raster_converter_ = new FakePWGRasterConverter(); | 446 pwg_raster_converter_ = new FakePWGRasterConverter(); |
377 extension_printer_handler_->SetPwgRasterConverterForTesting( | 447 extension_printer_handler_->SetPwgRasterConverterForTesting( |
378 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); | 448 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_)); |
449 device_client_.set_usb_service(&usb_service_); | |
379 } | 450 } |
380 | 451 |
381 protected: | 452 protected: |
382 FakePrinterProviderAPI* GetPrinterProviderAPI() { | 453 FakePrinterProviderAPI* GetPrinterProviderAPI() { |
383 return static_cast<FakePrinterProviderAPI*>( | 454 return static_cast<FakePrinterProviderAPI*>( |
384 extensions::PrinterProviderAPIFactory::GetInstance() | 455 extensions::PrinterProviderAPIFactory::GetInstance() |
385 ->GetForBrowserContext(profile_.get())); | 456 ->GetForBrowserContext(env_->profile())); |
386 } | 457 } |
387 | 458 |
459 MockUsbService usb_service_; | |
460 scoped_ptr<TestExtensionEnvironment> env_; | |
Vitaly Buka (NO REVIEWS)
2015/05/28 22:50:55
maybe just TestExtensionEnvironment env_;
| |
388 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; | 461 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_; |
389 | 462 |
390 FakePWGRasterConverter* pwg_raster_converter_; | 463 FakePWGRasterConverter* pwg_raster_converter_; |
391 | 464 |
392 private: | 465 private: |
393 content::TestBrowserThreadBundle thread_bundle_; | 466 FakeDeviceClient device_client_; |
394 | |
395 scoped_ptr<TestingProfile> profile_; | |
396 | 467 |
397 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); | 468 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest); |
398 }; | 469 }; |
399 | 470 |
400 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { | 471 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) { |
401 size_t call_count = 0; | 472 size_t call_count = 0; |
402 scoped_ptr<base::ListValue> printers; | 473 scoped_ptr<base::ListValue> printers; |
403 bool is_done = false; | 474 bool is_done = false; |
404 | 475 |
405 extension_printer_handler_->StartGetPrinters( | 476 extension_printer_handler_->StartGetPrinters( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
442 std::string error; | 513 std::string error; |
443 scoped_ptr<base::ListValue> original_printers( | 514 scoped_ptr<base::ListValue> original_printers( |
444 GetJSONAsListValue(kPrinterDescriptionList, &error)); | 515 GetJSONAsListValue(kPrinterDescriptionList, &error)); |
445 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; | 516 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error; |
446 | 517 |
447 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); | 518 fake_api->TriggerNextGetPrintersCallback(*original_printers, true); |
448 | 519 |
449 EXPECT_EQ(0u, call_count); | 520 EXPECT_EQ(0u, call_count); |
450 } | 521 } |
451 | 522 |
523 TEST_F(ExtensionPrinterHandlerTest, GetUsbPrinters) { | |
524 scoped_refptr<MockUsbDevice> device0 = | |
525 new MockUsbDevice(0, 0, "Google", "USB Printer", ""); | |
526 usb_service_.AddDevice(device0); | |
527 scoped_refptr<MockUsbDevice> device1 = | |
528 new MockUsbDevice(0, 1, "Google", "USB Printer", ""); | |
529 usb_service_.AddDevice(device1); | |
530 | |
531 const Extension* extension_1 = env_->MakeExtension( | |
532 *base::test::ParseJson(kExtension1), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | |
533 const Extension* extension_2 = env_->MakeExtension( | |
534 *base::test::ParseJson(kExtension2), "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); | |
535 | |
536 extensions::DevicePermissionsManager* permissions_manager = | |
537 extensions::DevicePermissionsManager::Get(env_->profile()); | |
538 permissions_manager->AllowUsbDevice(extension_2->id(), device0); | |
539 | |
540 size_t call_count = 0; | |
541 scoped_ptr<base::ListValue> printers; | |
542 bool is_done = false; | |
543 extension_printer_handler_->StartGetPrinters( | |
544 base::Bind(&RecordPrinterList, &call_count, &printers, &is_done)); | |
545 | |
546 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI(); | |
547 ASSERT_TRUE(fake_api); | |
548 ASSERT_EQ(1u, fake_api->pending_get_printers_count()); | |
549 | |
550 EXPECT_EQ(1u, call_count); | |
551 EXPECT_FALSE(is_done); | |
552 EXPECT_TRUE(printers.get()); | |
553 EXPECT_EQ(2u, printers->GetSize()); | |
554 scoped_ptr<base::DictionaryValue> extension_1_entry( | |
555 DictionaryBuilder() | |
556 .Set("id", base::StringPrintf("provisional-usb:%s:%u", | |
557 extension_1->id().c_str(), | |
558 device0->unique_id())) | |
559 .Set("name", "USB Printer") | |
560 .Set("extensionName", "Provider 1") | |
561 .Set("extensionId", extension_1->id()) | |
562 .Set("provisional", true) | |
563 .Build()); | |
564 scoped_ptr<base::DictionaryValue> extension_2_entry( | |
565 DictionaryBuilder() | |
566 .Set("id", base::StringPrintf("provisional-usb:%s:%u", | |
567 extension_2->id().c_str(), | |
568 device1->unique_id())) | |
569 .Set("name", "USB Printer") | |
570 .Set("extensionName", "Provider 2") | |
571 .Set("extensionId", extension_2->id()) | |
572 .Set("provisional", true) | |
573 .Build()); | |
574 EXPECT_TRUE(printers->Find(*extension_1_entry) != printers->end()); | |
575 EXPECT_TRUE(printers->Find(*extension_2_entry) != printers->end()); | |
576 | |
577 fake_api->TriggerNextGetPrintersCallback(base::ListValue(), true); | |
578 | |
579 EXPECT_EQ(2u, call_count); | |
580 EXPECT_TRUE(is_done); | |
581 EXPECT_TRUE(printers.get()); | |
582 EXPECT_EQ(0u, printers->GetSize()); // RecordPrinterList resets |printers|. | |
583 } | |
584 | |
452 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { | 585 TEST_F(ExtensionPrinterHandlerTest, GetCapability) { |
453 size_t call_count = 0; | 586 size_t call_count = 0; |
454 std::string destination_id; | 587 std::string destination_id; |
455 scoped_ptr<base::DictionaryValue> capability; | 588 scoped_ptr<base::DictionaryValue> capability; |
456 | 589 |
457 extension_printer_handler_->StartGetCapability( | 590 extension_printer_handler_->StartGetCapability( |
458 kPrinterId, | 591 kPrinterId, |
459 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); | 592 base::Bind(&RecordCapability, &call_count, &destination_id, &capability)); |
460 | 593 |
461 EXPECT_EQ(0u, call_count); | 594 EXPECT_EQ(0u, call_count); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
787 extension_printer_handler_->StartPrint( | 920 extension_printer_handler_->StartPrint( |
788 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, | 921 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title, |
789 kEmptyPrintTicket, gfx::Size(100, 100), print_data, | 922 kEmptyPrintTicket, gfx::Size(100, 100), print_data, |
790 base::Bind(&RecordPrintResult, &call_count, &success, &status)); | 923 base::Bind(&RecordPrintResult, &call_count, &success, &status)); |
791 | 924 |
792 EXPECT_EQ(1u, call_count); | 925 EXPECT_EQ(1u, call_count); |
793 | 926 |
794 EXPECT_FALSE(success); | 927 EXPECT_FALSE(success); |
795 EXPECT_EQ("INVALID_DATA", status); | 928 EXPECT_EQ("INVALID_DATA", status); |
796 } | 929 } |
OLD | NEW |