Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / extension_printer_handler_unittest.cc
blob94905a51b920c0ea932ba359ee6b6e4f57d6dd40
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
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/json/json_string_value_serializer.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/values_test_util.h"
19 #include "base/values.h"
20 #include "chrome/browser/extensions/test_extension_environment.h"
21 #include "chrome/browser/local_discovery/pwg_raster_converter.h"
22 #include "chrome/browser/ui/webui/print_preview/extension_printer_handler.h"
23 #include "chrome/test/base/testing_profile.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"
28 #include "extensions/browser/api/printer_provider/printer_provider_api.h"
29 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.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"
33 #include "printing/pdf_render_settings.h"
34 #include "printing/pwg_raster_settings.h"
35 #include "printing/units.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "ui/gfx/geometry/size.h"
39 using device::MockUsbDevice;
40 using device::MockUsbService;
41 using extensions::DictionaryBuilder;
42 using extensions::Extension;
43 using extensions::PrinterProviderAPI;
44 using extensions::PrinterProviderPrintJob;
45 using extensions::TestExtensionEnvironment;
46 using local_discovery::PWGRasterConverter;
48 namespace {
50 // Printer id used for requests in tests.
51 const char kPrinterId[] = "printer_id";
53 // Printer list used a result for getPrinters.
54 const char kPrinterDescriptionList[] =
55 "[{"
56 " \"id\": \"printer1\","
57 " \"name\": \"Printer 1\""
58 "}, {"
59 " \"id\": \"printer2\","
60 " \"name\": \"Printer 2\","
61 " \"description\": \"Test printer 2\""
62 "}]";
64 // Printer capability for printer that supports all content types.
65 const char kAllContentTypesSupportedPrinter[] =
66 "{"
67 " \"version\": \"1.0\","
68 " \"printer\": {"
69 " \"supported_content_type\": ["
70 " {\"content_type\": \"*/*\"}"
71 " ]"
72 " }"
73 "}";
75 // Printer capability for a printer that supports PDF.
76 const char kPdfSupportedPrinter[] =
77 "{"
78 " \"version\": \"1.0\","
79 " \"printer\": {"
80 " \"supported_content_type\": ["
81 " {\"content_type\": \"application/pdf\"},"
82 " {\"content_type\": \"image/pwg-raster\"}"
83 " ]"
84 " }"
85 "}";
87 // Printer capability for a printer that supportd only PWG raster.
88 const char kPWGRasterOnlyPrinterSimpleDescription[] =
89 "{"
90 " \"version\": \"1.0\","
91 " \"printer\": {"
92 " \"supported_content_type\": ["
93 " {\"content_type\": \"image/pwg-raster\"}"
94 " ]"
95 " }"
96 "}";
98 // Printer capability for a printer that supportd only PWG raster that has
99 // options other that supported_content_type set.
100 const char kPWGRasterOnlyPrinter[] =
102 " \"version\": \"1.0\","
103 " \"printer\": {"
104 " \"supported_content_type\": ["
105 " {\"content_type\": \"image/pwg-raster\"}"
106 " ],"
107 " \"pwg_raster_config\": {"
108 " \"document_sheet_back\": \"FLIPPED\","
109 " \"reverse_order_streaming\": true,"
110 " \"rotate_all_pages\": true"
111 " },"
112 " \"dpi\": {"
113 " \"option\": [{"
114 " \"horizontal_dpi\": 100,"
115 " \"vertical_dpi\": 200,"
116 " \"is_default\": true"
117 " }]"
118 " }"
119 " }"
120 "}";
122 // Print ticket with no parameters set.
123 const char kEmptyPrintTicket[] = "{\"version\": \"1.0\"}";
125 // Print ticket that has duplex parameter set.
126 const char kPrintTicketWithDuplex[] =
128 " \"version\": \"1.0\","
129 " \"print\": {"
130 " \"duplex\": {\"type\": \"LONG_EDGE\"}"
131 " }"
132 "}";
134 // An extension with permission for 1 printer it supports.
135 const char kExtension1[] =
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 "}";
160 // An extension with permission for none of the printers it supports.
161 const char kExtension2[] =
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 "}";
178 const char kContentTypePDF[] = "application/pdf";
179 const char kContentTypePWG[] = "image/pwg-raster";
181 // Print request status considered to be successful by fake PrinterProviderAPI.
182 const char kPrintRequestSuccess[] = "OK";
184 // Used as a callback to StartGetPrinters in tests.
185 // Increases |*call_count| and records values returned by StartGetPrinters.
186 void RecordPrinterList(size_t* call_count,
187 scoped_ptr<base::ListValue>* printers_out,
188 bool* is_done_out,
189 const base::ListValue& printers,
190 bool is_done) {
191 ++(*call_count);
192 printers_out->reset(printers.DeepCopy());
193 *is_done_out = is_done;
196 // Used as a callback to StartGetCapability in tests.
197 // Increases |*call_count| and records values returned by StartGetCapability.
198 void RecordCapability(size_t* call_count,
199 std::string* destination_id_out,
200 scoped_ptr<base::DictionaryValue>* capability_out,
201 const std::string& destination_id,
202 const base::DictionaryValue& capability) {
203 ++(*call_count);
204 *destination_id_out = destination_id;
205 capability_out->reset(capability.DeepCopy());
208 // Used as a callback to StartPrint in tests.
209 // Increases |*call_count| and records values returned by StartPrint.
210 void RecordPrintResult(size_t* call_count,
211 bool* success_out,
212 std::string* status_out,
213 bool success,
214 const std::string& status) {
215 ++(*call_count);
216 *success_out = success;
217 *status_out = status;
220 // Converts JSON string to base::ListValue object.
221 // On failure, returns NULL and fills |*error| string.
222 scoped_ptr<base::ListValue> GetJSONAsListValue(const std::string& json,
223 std::string* error) {
224 scoped_ptr<base::Value> deserialized(
225 JSONStringValueDeserializer(json).Deserialize(NULL, error));
226 if (!deserialized)
227 return scoped_ptr<base::ListValue>();
228 base::ListValue* list = nullptr;
229 if (!deserialized->GetAsList(&list)) {
230 *error = "Value is not a list.";
231 return scoped_ptr<base::ListValue>();
233 return scoped_ptr<base::ListValue>(list->DeepCopy());
236 // Converts JSON string to base::DictionaryValue object.
237 // On failure, returns NULL and fills |*error| string.
238 scoped_ptr<base::DictionaryValue> GetJSONAsDictionaryValue(
239 const std::string& json,
240 std::string* error) {
241 scoped_ptr<base::Value> deserialized(
242 JSONStringValueDeserializer(json).Deserialize(NULL, error));
243 if (!deserialized)
244 return scoped_ptr<base::DictionaryValue>();
245 base::DictionaryValue* dictionary;
246 if (!deserialized->GetAsDictionary(&dictionary)) {
247 *error = "Value is not a dictionary.";
248 return scoped_ptr<base::DictionaryValue>();
250 return scoped_ptr<base::DictionaryValue>(dictionary->DeepCopy());
253 std::string RefCountedMemoryToString(
254 const scoped_refptr<base::RefCountedMemory>& memory) {
255 return std::string(memory->front_as<char>(), memory->size());
258 // Fake PWGRasterconverter used in the tests.
259 class FakePWGRasterConverter : public PWGRasterConverter {
260 public:
261 FakePWGRasterConverter() : fail_conversion_(false), initialized_(false) {}
262 ~FakePWGRasterConverter() override = default;
264 // PWGRasterConverter implementation. It writes |data| to a temp file.
265 // Also, remembers conversion and bitmap settings passed into the method.
266 void Start(base::RefCountedMemory* data,
267 const printing::PdfRenderSettings& conversion_settings,
268 const printing::PwgRasterSettings& bitmap_settings,
269 const ResultCallback& callback) override {
270 if (fail_conversion_) {
271 callback.Run(false, base::FilePath());
272 return;
275 if (!initialized_ && !temp_dir_.CreateUniqueTempDir()) {
276 ADD_FAILURE() << "Unable to create target dir for cenverter";
277 callback.Run(false, base::FilePath());
278 return;
281 initialized_ = true;
283 path_ = temp_dir_.path().AppendASCII("output.pwg");
284 std::string data_str(data->front_as<char>(), data->size());
285 int written = WriteFile(path_, data_str.c_str(), data_str.size());
286 if (written != static_cast<int>(data_str.size())) {
287 ADD_FAILURE() << "Failed to write pwg raster file.";
288 callback.Run(false, base::FilePath());
289 return;
292 conversion_settings_ = conversion_settings;
293 bitmap_settings_ = bitmap_settings;
295 callback.Run(true, path_);
298 // Makes |Start| method always return an error.
299 void FailConversion() { fail_conversion_ = true; }
301 const base::FilePath& path() { return path_; }
302 const printing::PdfRenderSettings& conversion_settings() const {
303 return conversion_settings_;
306 const printing::PwgRasterSettings& bitmap_settings() const {
307 return bitmap_settings_;
310 private:
311 base::ScopedTempDir temp_dir_;
313 base::FilePath path_;
314 printing::PdfRenderSettings conversion_settings_;
315 printing::PwgRasterSettings bitmap_settings_;
316 bool fail_conversion_;
317 bool initialized_;
319 DISALLOW_COPY_AND_ASSIGN(FakePWGRasterConverter);
322 // Information about received print requests.
323 struct PrintRequestInfo {
324 PrinterProviderAPI::PrintCallback callback;
325 PrinterProviderPrintJob job;
328 // Fake PrinterProviderAPI used in tests.
329 // It caches requests issued to API and exposes methods to trigger their
330 // callbacks.
331 class FakePrinterProviderAPI : public PrinterProviderAPI {
332 public:
333 FakePrinterProviderAPI() = default;
334 ~FakePrinterProviderAPI() override = default;
336 void DispatchGetPrintersRequested(
337 const PrinterProviderAPI::GetPrintersCallback& callback) override {
338 pending_printers_callbacks_.push_back(callback);
341 void DispatchGetCapabilityRequested(
342 const std::string& destination_id,
343 const PrinterProviderAPI::GetCapabilityCallback& callback) override {
344 pending_capability_callbacks_.push_back(base::Bind(callback));
347 void DispatchPrintRequested(
348 const PrinterProviderPrintJob& job,
349 const PrinterProviderAPI::PrintCallback& callback) override {
350 PrintRequestInfo request_info;
351 request_info.callback = callback;
352 request_info.job = job;
354 pending_print_requests_.push_back(request_info);
357 size_t pending_get_printers_count() const {
358 return pending_printers_callbacks_.size();
361 const PrinterProviderPrintJob* GetPrintJob(
362 const extensions::Extension* extension,
363 int request_id) const override {
364 ADD_FAILURE() << "Not reached";
365 return nullptr;
368 void TriggerNextGetPrintersCallback(const base::ListValue& printers,
369 bool done) {
370 ASSERT_GT(pending_get_printers_count(), 0u);
371 pending_printers_callbacks_[0].Run(printers, done);
372 pending_printers_callbacks_.erase(pending_printers_callbacks_.begin());
375 size_t pending_get_capability_count() const {
376 return pending_capability_callbacks_.size();
379 void TriggerNextGetCapabilityCallback(
380 const base::DictionaryValue& description) {
381 ASSERT_GT(pending_get_capability_count(), 0u);
382 pending_capability_callbacks_[0].Run(description);
383 pending_capability_callbacks_.erase(pending_capability_callbacks_.begin());
386 size_t pending_print_count() const { return pending_print_requests_.size(); }
388 const PrinterProviderPrintJob* GetNextPendingPrintJob() const {
389 EXPECT_GT(pending_print_count(), 0u);
390 if (pending_print_count() == 0)
391 return NULL;
392 return &pending_print_requests_[0].job;
395 void TriggerNextPrintCallback(const std::string& result) {
396 ASSERT_GT(pending_print_count(), 0u);
397 pending_print_requests_[0].callback.Run(result == kPrintRequestSuccess,
398 result);
399 pending_print_requests_.erase(pending_print_requests_.begin());
402 private:
403 std::vector<PrinterProviderAPI::GetPrintersCallback>
404 pending_printers_callbacks_;
405 std::vector<PrinterProviderAPI::GetCapabilityCallback>
406 pending_capability_callbacks_;
407 std::vector<PrintRequestInfo> pending_print_requests_;
409 DISALLOW_COPY_AND_ASSIGN(FakePrinterProviderAPI);
412 KeyedService* BuildTestingPrinterProviderAPI(content::BrowserContext* context) {
413 return new FakePrinterProviderAPI();
416 class FakeDeviceClient : public device::DeviceClient {
417 public:
418 FakeDeviceClient() {}
420 // device::DeviceClient implementation:
421 device::UsbService* GetUsbService() override {
422 DCHECK(usb_service_);
423 return usb_service_;
426 void set_usb_service(device::UsbService* service) { usb_service_ = service; }
428 private:
429 device::UsbService* usb_service_ = nullptr;
432 } // namespace
434 class ExtensionPrinterHandlerTest : public testing::Test {
435 public:
436 ExtensionPrinterHandlerTest() : pwg_raster_converter_(NULL) {}
437 ~ExtensionPrinterHandlerTest() override = default;
439 void SetUp() override {
440 extensions::PrinterProviderAPIFactory::GetInstance()->SetTestingFactory(
441 env_.profile(), &BuildTestingPrinterProviderAPI);
442 extension_printer_handler_.reset(new ExtensionPrinterHandler(
443 env_.profile(), base::MessageLoop::current()->task_runner()));
445 pwg_raster_converter_ = new FakePWGRasterConverter();
446 extension_printer_handler_->SetPwgRasterConverterForTesting(
447 scoped_ptr<PWGRasterConverter>(pwg_raster_converter_));
448 device_client_.set_usb_service(&usb_service_);
451 protected:
452 FakePrinterProviderAPI* GetPrinterProviderAPI() {
453 return static_cast<FakePrinterProviderAPI*>(
454 extensions::PrinterProviderAPIFactory::GetInstance()
455 ->GetForBrowserContext(env_.profile()));
458 MockUsbService usb_service_;
459 TestExtensionEnvironment env_;
460 scoped_ptr<ExtensionPrinterHandler> extension_printer_handler_;
462 FakePWGRasterConverter* pwg_raster_converter_;
464 private:
465 FakeDeviceClient device_client_;
467 DISALLOW_COPY_AND_ASSIGN(ExtensionPrinterHandlerTest);
470 TEST_F(ExtensionPrinterHandlerTest, GetPrinters) {
471 size_t call_count = 0;
472 scoped_ptr<base::ListValue> printers;
473 bool is_done = false;
475 extension_printer_handler_->StartGetPrinters(
476 base::Bind(&RecordPrinterList, &call_count, &printers, &is_done));
478 EXPECT_FALSE(printers.get());
479 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
480 ASSERT_TRUE(fake_api);
481 ASSERT_EQ(1u, fake_api->pending_get_printers_count());
483 std::string error;
484 scoped_ptr<base::ListValue> original_printers(
485 GetJSONAsListValue(kPrinterDescriptionList, &error));
486 ASSERT_TRUE(original_printers) << "Failed to deserialize printers: " << error;
488 fake_api->TriggerNextGetPrintersCallback(*original_printers, true);
490 EXPECT_EQ(1u, call_count);
491 EXPECT_TRUE(is_done);
492 ASSERT_TRUE(printers.get());
493 EXPECT_TRUE(printers->Equals(original_printers.get()))
494 << *printers << ", expected: " << *original_printers;
497 TEST_F(ExtensionPrinterHandlerTest, GetPrinters_Reset) {
498 size_t call_count = 0;
499 scoped_ptr<base::ListValue> printers;
500 bool is_done = false;
502 extension_printer_handler_->StartGetPrinters(
503 base::Bind(&RecordPrinterList, &call_count, &printers, &is_done));
505 EXPECT_FALSE(printers.get());
506 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
507 ASSERT_TRUE(fake_api);
508 ASSERT_EQ(1u, fake_api->pending_get_printers_count());
510 extension_printer_handler_->Reset();
512 std::string error;
513 scoped_ptr<base::ListValue> original_printers(
514 GetJSONAsListValue(kPrinterDescriptionList, &error));
515 ASSERT_TRUE(original_printers) << "Error deserializing printers: " << error;
517 fake_api->TriggerNextGetPrintersCallback(*original_printers, true);
519 EXPECT_EQ(0u, call_count);
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);
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");
535 extensions::DevicePermissionsManager* permissions_manager =
536 extensions::DevicePermissionsManager::Get(env_.profile());
537 permissions_manager->AllowUsbDevice(extension_2->id(), device0);
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));
545 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
546 ASSERT_TRUE(fake_api);
547 ASSERT_EQ(1u, fake_api->pending_get_printers_count());
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());
576 fake_api->TriggerNextGetPrintersCallback(base::ListValue(), true);
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|.
584 TEST_F(ExtensionPrinterHandlerTest, GetCapability) {
585 size_t call_count = 0;
586 std::string destination_id;
587 scoped_ptr<base::DictionaryValue> capability;
589 extension_printer_handler_->StartGetCapability(
590 kPrinterId,
591 base::Bind(&RecordCapability, &call_count, &destination_id, &capability));
593 EXPECT_EQ(0u, call_count);
595 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
596 ASSERT_TRUE(fake_api);
597 ASSERT_EQ(1u, fake_api->pending_get_capability_count());
599 std::string error;
600 scoped_ptr<base::DictionaryValue> original_capability(
601 GetJSONAsDictionaryValue(kPWGRasterOnlyPrinterSimpleDescription, &error));
602 ASSERT_TRUE(original_capability)
603 << "Error deserializing capability: " << error;
605 fake_api->TriggerNextGetCapabilityCallback(*original_capability);
607 EXPECT_EQ(1u, call_count);
608 EXPECT_EQ(kPrinterId, destination_id);
609 ASSERT_TRUE(capability.get());
610 EXPECT_TRUE(capability->Equals(original_capability.get()))
611 << *capability << ", expected: " << *original_capability;
614 TEST_F(ExtensionPrinterHandlerTest, GetCapability_Reset) {
615 size_t call_count = 0;
616 std::string destination_id;
617 scoped_ptr<base::DictionaryValue> capability;
619 extension_printer_handler_->StartGetCapability(
620 kPrinterId,
621 base::Bind(&RecordCapability, &call_count, &destination_id, &capability));
623 EXPECT_EQ(0u, call_count);
625 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
626 ASSERT_TRUE(fake_api);
627 ASSERT_EQ(1u, fake_api->pending_get_capability_count());
629 extension_printer_handler_->Reset();
631 std::string error;
632 scoped_ptr<base::DictionaryValue> original_capability(
633 GetJSONAsDictionaryValue(kPWGRasterOnlyPrinterSimpleDescription, &error));
634 ASSERT_TRUE(original_capability)
635 << "Error deserializing capability: " << error;
637 fake_api->TriggerNextGetCapabilityCallback(*original_capability);
639 EXPECT_EQ(0u, call_count);
642 TEST_F(ExtensionPrinterHandlerTest, Print_Pdf) {
643 size_t call_count = 0;
644 bool success = false;
645 std::string status;
647 scoped_refptr<base::RefCountedString> print_data(
648 new base::RefCountedString());
649 print_data->data() = "print data, PDF";
650 base::string16 title = base::ASCIIToUTF16("Title");
652 extension_printer_handler_->StartPrint(
653 kPrinterId, kPdfSupportedPrinter, title, kEmptyPrintTicket,
654 gfx::Size(100, 100), print_data,
655 base::Bind(&RecordPrintResult, &call_count, &success, &status));
657 EXPECT_EQ(0u, call_count);
658 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
659 ASSERT_TRUE(fake_api);
660 ASSERT_EQ(1u, fake_api->pending_print_count());
662 const PrinterProviderPrintJob* print_job = fake_api->GetNextPendingPrintJob();
663 ASSERT_TRUE(print_job);
665 EXPECT_EQ(kPrinterId, print_job->printer_id);
666 EXPECT_EQ(title, print_job->job_title);
667 EXPECT_EQ(kEmptyPrintTicket, print_job->ticket_json);
668 EXPECT_EQ(kContentTypePDF, print_job->content_type);
669 EXPECT_TRUE(print_job->document_path.empty());
670 ASSERT_TRUE(print_job->document_bytes);
671 EXPECT_EQ(print_data->data(),
672 RefCountedMemoryToString(print_job->document_bytes));
674 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
676 EXPECT_EQ(1u, call_count);
677 EXPECT_TRUE(success);
678 EXPECT_EQ(kPrintRequestSuccess, status);
681 TEST_F(ExtensionPrinterHandlerTest, Print_Pdf_Reset) {
682 size_t call_count = 0;
683 bool success = false;
684 std::string status;
686 scoped_refptr<base::RefCountedString> print_data(
687 new base::RefCountedString());
688 print_data->data() = "print data, PDF";
689 base::string16 title = base::ASCIIToUTF16("Title");
691 extension_printer_handler_->StartPrint(
692 kPrinterId, kPdfSupportedPrinter, title, kEmptyPrintTicket,
693 gfx::Size(100, 100), print_data,
694 base::Bind(&RecordPrintResult, &call_count, &success, &status));
696 EXPECT_EQ(0u, call_count);
697 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
698 ASSERT_TRUE(fake_api);
699 ASSERT_EQ(1u, fake_api->pending_print_count());
701 extension_printer_handler_->Reset();
703 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
705 EXPECT_EQ(0u, call_count);
708 TEST_F(ExtensionPrinterHandlerTest, Print_All) {
709 size_t call_count = 0;
710 bool success = false;
711 std::string status;
713 scoped_refptr<base::RefCountedString> print_data(
714 new base::RefCountedString());
715 print_data->data() = "print data, PDF";
716 base::string16 title = base::ASCIIToUTF16("Title");
718 extension_printer_handler_->StartPrint(
719 kPrinterId, kAllContentTypesSupportedPrinter, title, kEmptyPrintTicket,
720 gfx::Size(100, 100), print_data,
721 base::Bind(&RecordPrintResult, &call_count, &success, &status));
723 EXPECT_EQ(0u, call_count);
725 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
726 ASSERT_TRUE(fake_api);
727 ASSERT_EQ(1u, fake_api->pending_print_count());
729 const PrinterProviderPrintJob* print_job = fake_api->GetNextPendingPrintJob();
730 ASSERT_TRUE(print_job);
732 EXPECT_EQ(kPrinterId, print_job->printer_id);
733 EXPECT_EQ(title, print_job->job_title);
734 EXPECT_EQ(kEmptyPrintTicket, print_job->ticket_json);
735 EXPECT_EQ(kContentTypePDF, print_job->content_type);
736 EXPECT_TRUE(print_job->document_path.empty());
737 ASSERT_TRUE(print_job->document_bytes);
738 EXPECT_EQ(print_data->data(),
739 RefCountedMemoryToString(print_job->document_bytes));
741 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
743 EXPECT_EQ(1u, call_count);
744 EXPECT_TRUE(success);
745 EXPECT_EQ(kPrintRequestSuccess, status);
748 TEST_F(ExtensionPrinterHandlerTest, Print_Pwg) {
749 size_t call_count = 0;
750 bool success = false;
751 std::string status;
753 scoped_refptr<base::RefCountedString> print_data(
754 new base::RefCountedString());
755 print_data->data() = "print data, PDF";
756 base::string16 title = base::ASCIIToUTF16("Title");
758 extension_printer_handler_->StartPrint(
759 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title,
760 kEmptyPrintTicket, gfx::Size(100, 50), print_data,
761 base::Bind(&RecordPrintResult, &call_count, &success, &status));
763 EXPECT_EQ(0u, call_count);
765 base::RunLoop().RunUntilIdle();
767 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
768 ASSERT_TRUE(fake_api);
769 ASSERT_EQ(1u, fake_api->pending_print_count());
771 EXPECT_EQ(printing::TRANSFORM_NORMAL,
772 pwg_raster_converter_->bitmap_settings().odd_page_transform);
773 EXPECT_FALSE(pwg_raster_converter_->bitmap_settings().rotate_all_pages);
774 EXPECT_FALSE(pwg_raster_converter_->bitmap_settings().reverse_page_order);
776 EXPECT_EQ(printing::kDefaultPdfDpi,
777 pwg_raster_converter_->conversion_settings().dpi());
778 EXPECT_TRUE(pwg_raster_converter_->conversion_settings().autorotate());
779 EXPECT_EQ("0,0 208x416", // vertically_oriented_size * dpi / points_per_inch
780 pwg_raster_converter_->conversion_settings().area().ToString());
782 const PrinterProviderPrintJob* print_job = fake_api->GetNextPendingPrintJob();
783 ASSERT_TRUE(print_job);
785 EXPECT_EQ(kPrinterId, print_job->printer_id);
786 EXPECT_EQ(title, print_job->job_title);
787 EXPECT_EQ(kEmptyPrintTicket, print_job->ticket_json);
788 EXPECT_EQ(kContentTypePWG, print_job->content_type);
789 EXPECT_FALSE(print_job->document_bytes);
790 EXPECT_FALSE(print_job->document_path.empty());
791 EXPECT_EQ(pwg_raster_converter_->path(), print_job->document_path);
792 EXPECT_EQ(static_cast<int64_t>(print_data->size()),
793 print_job->file_info.size);
795 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
797 EXPECT_EQ(1u, call_count);
798 EXPECT_TRUE(success);
799 EXPECT_EQ(kPrintRequestSuccess, status);
802 TEST_F(ExtensionPrinterHandlerTest, Print_Pwg_NonDefaultSettings) {
803 size_t call_count = 0;
804 bool success = false;
805 std::string status;
807 scoped_refptr<base::RefCountedString> print_data(
808 new base::RefCountedString());
809 print_data->data() = "print data, PDF";
810 base::string16 title = base::ASCIIToUTF16("Title");
812 extension_printer_handler_->StartPrint(
813 kPrinterId, kPWGRasterOnlyPrinter, title, kPrintTicketWithDuplex,
814 gfx::Size(100, 50), print_data,
815 base::Bind(&RecordPrintResult, &call_count, &success, &status));
817 EXPECT_EQ(0u, call_count);
819 base::RunLoop().RunUntilIdle();
821 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
822 ASSERT_TRUE(fake_api);
823 ASSERT_EQ(1u, fake_api->pending_print_count());
825 EXPECT_EQ(printing::TRANSFORM_FLIP_VERTICAL,
826 pwg_raster_converter_->bitmap_settings().odd_page_transform);
827 EXPECT_TRUE(pwg_raster_converter_->bitmap_settings().rotate_all_pages);
828 EXPECT_TRUE(pwg_raster_converter_->bitmap_settings().reverse_page_order);
830 EXPECT_EQ(200, // max(vertical_dpi, horizontal_dpi)
831 pwg_raster_converter_->conversion_settings().dpi());
832 EXPECT_TRUE(pwg_raster_converter_->conversion_settings().autorotate());
833 EXPECT_EQ("0,0 138x277", // vertically_oriented_size * dpi / points_per_inch
834 pwg_raster_converter_->conversion_settings().area().ToString());
836 const PrinterProviderPrintJob* print_job = fake_api->GetNextPendingPrintJob();
837 ASSERT_TRUE(print_job);
839 EXPECT_EQ(kPrinterId, print_job->printer_id);
840 EXPECT_EQ(title, print_job->job_title);
841 EXPECT_EQ(kPrintTicketWithDuplex, print_job->ticket_json);
842 EXPECT_EQ(kContentTypePWG, print_job->content_type);
843 EXPECT_FALSE(print_job->document_bytes);
844 EXPECT_FALSE(print_job->document_path.empty());
845 EXPECT_EQ(pwg_raster_converter_->path(), print_job->document_path);
846 EXPECT_EQ(static_cast<int64_t>(print_data->size()),
847 print_job->file_info.size);
849 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
851 EXPECT_EQ(1u, call_count);
852 EXPECT_TRUE(success);
853 EXPECT_EQ(kPrintRequestSuccess, status);
856 TEST_F(ExtensionPrinterHandlerTest, Print_Pwg_Reset) {
857 size_t call_count = 0;
858 bool success = false;
859 std::string status;
861 scoped_refptr<base::RefCountedString> print_data(
862 new base::RefCountedString());
863 print_data->data() = "print data, PDF";
864 base::string16 title = base::ASCIIToUTF16("Title");
866 extension_printer_handler_->StartPrint(
867 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title,
868 kEmptyPrintTicket, gfx::Size(100, 50), print_data,
869 base::Bind(&RecordPrintResult, &call_count, &success, &status));
871 EXPECT_EQ(0u, call_count);
873 base::RunLoop().RunUntilIdle();
875 FakePrinterProviderAPI* fake_api = GetPrinterProviderAPI();
876 ASSERT_TRUE(fake_api);
877 ASSERT_EQ(1u, fake_api->pending_print_count());
879 extension_printer_handler_->Reset();
881 fake_api->TriggerNextPrintCallback(kPrintRequestSuccess);
883 EXPECT_EQ(0u, call_count);
886 TEST_F(ExtensionPrinterHandlerTest, Print_Pwg_InvalidTicket) {
887 size_t call_count = 0;
888 bool success = false;
889 std::string status;
891 scoped_refptr<base::RefCountedString> print_data(
892 new base::RefCountedString());
893 print_data->data() = "print data, PDF";
894 base::string16 title = base::ASCIIToUTF16("Title");
896 extension_printer_handler_->StartPrint(
897 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title,
898 "{}" /* ticket */, gfx::Size(100, 100), print_data,
899 base::Bind(&RecordPrintResult, &call_count, &success, &status));
901 EXPECT_EQ(1u, call_count);
903 EXPECT_FALSE(success);
904 EXPECT_EQ("INVALID_TICKET", status);
907 TEST_F(ExtensionPrinterHandlerTest, Print_Pwg_FailedConversion) {
908 size_t call_count = 0;
909 bool success = false;
910 std::string status;
912 pwg_raster_converter_->FailConversion();
914 scoped_refptr<base::RefCountedString> print_data(
915 new base::RefCountedString());
916 print_data->data() = "print data, PDF";
917 base::string16 title = base::ASCIIToUTF16("Title");
919 extension_printer_handler_->StartPrint(
920 kPrinterId, kPWGRasterOnlyPrinterSimpleDescription, title,
921 kEmptyPrintTicket, gfx::Size(100, 100), print_data,
922 base::Bind(&RecordPrintResult, &call_count, &success, &status));
924 EXPECT_EQ(1u, call_count);
926 EXPECT_FALSE(success);
927 EXPECT_EQ("INVALID_DATA", status);