Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / printer_provider / printer_provider_apitest.cc
blob643499cf8b19ffdce3186e8a8247cea99963a599
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 "base/bind.h"
6 #include "base/files/file.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/run_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "device/usb/mock_usb_device.h"
17 #include "device/usb/mock_usb_service.h"
18 #include "extensions/browser/api/printer_provider/printer_provider_api.h"
19 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
20 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
21 #include "extensions/browser/api/usb/usb_guid_map.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/value_builder.h"
25 #include "extensions/shell/test/shell_apitest.h"
26 #include "extensions/test/extension_test_message_listener.h"
27 #include "extensions/test/result_catcher.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 namespace extensions {
32 namespace {
34 // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls.
35 // It appends items in |printers| to |*printers_out|. If |done| is set, it runs
36 // |callback|.
37 void AppendPrintersAndRunCallbackIfDone(base::ListValue* printers_out,
38 const base::Closure& callback,
39 const base::ListValue& printers,
40 bool done) {
41 for (size_t i = 0; i < printers.GetSize(); ++i) {
42 const base::DictionaryValue* printer = NULL;
43 EXPECT_TRUE(printers.GetDictionary(i, &printer))
44 << "Found invalid printer value at index " << i << ": " << printers;
45 if (printer)
46 printers_out->Append(printer->DeepCopy());
48 if (done && !callback.is_null())
49 callback.Run();
52 // Callback for PrinterProviderAPI::DispatchPrintRequested calls.
53 // It copies |value| to |*result| and runs |callback|.
54 void RecordPrintResultAndRunCallback(bool* result_success,
55 std::string* result_status,
56 const base::Closure& callback,
57 bool success,
58 const std::string& status) {
59 *result_success = success;
60 *result_status = status;
61 if (!callback.is_null())
62 callback.Run();
65 // Callback for PrinterProviderAPI::DispatchGetCapabilityRequested calls.
66 // It saves reported |value| as JSON string to |*result| and runs |callback|.
67 void RecordDictAndRunCallback(std::string* result,
68 const base::Closure& callback,
69 const base::DictionaryValue& value) {
70 JSONStringValueSerializer serializer(result);
71 EXPECT_TRUE(serializer.Serialize(value));
72 if (!callback.is_null())
73 callback.Run();
76 // Callback for PrinterProvider::DispatchGrantUsbPrinterAccess calls.
77 // It expects |value| to equal |expected_value| and runs |callback|.
78 void ExpectValueAndRunCallback(const base::Value* expected_value,
79 const base::Closure& callback,
80 const base::DictionaryValue& value) {
81 EXPECT_TRUE(value.Equals(expected_value));
82 if (!callback.is_null())
83 callback.Run();
86 // Tests for chrome.printerProvider API.
87 class PrinterProviderApiTest : public ShellApiTest {
88 public:
89 enum PrintRequestDataType {
90 PRINT_REQUEST_DATA_TYPE_NOT_SET,
91 PRINT_REQUEST_DATA_TYPE_FILE,
92 PRINT_REQUEST_DATA_TYPE_FILE_DELETED,
93 PRINT_REQUEST_DATA_TYPE_BYTES
96 PrinterProviderApiTest() {}
97 ~PrinterProviderApiTest() override {}
99 void StartGetPrintersRequest(
100 const PrinterProviderAPI::GetPrintersCallback& callback) {
101 PrinterProviderAPIFactory::GetInstance()
102 ->GetForBrowserContext(browser_context())
103 ->DispatchGetPrintersRequested(callback);
106 void StartGetUsbPrinterInfoRequest(
107 const std::string& extension_id,
108 scoped_refptr<device::UsbDevice> device,
109 const PrinterProviderAPI::GetPrinterInfoCallback& callback) {
110 PrinterProviderAPIFactory::GetInstance()
111 ->GetForBrowserContext(browser_context())
112 ->DispatchGetUsbPrinterInfoRequested(extension_id, device, callback);
115 void StartPrintRequestWithNoData(
116 const std::string& extension_id,
117 const PrinterProviderAPI::PrintCallback& callback) {
118 PrinterProviderPrintJob job;
119 job.printer_id = extension_id + ":printer_id";
120 job.ticket_json = "{}";
121 job.content_type = "application/pdf";
123 PrinterProviderAPIFactory::GetInstance()
124 ->GetForBrowserContext(browser_context())
125 ->DispatchPrintRequested(job, callback);
128 void StartPrintRequestUsingDocumentBytes(
129 const std::string& extension_id,
130 const PrinterProviderAPI::PrintCallback& callback) {
131 PrinterProviderPrintJob job;
132 job.printer_id = extension_id + ":printer_id";
133 job.job_title = base::ASCIIToUTF16("Print job");
134 job.ticket_json = "{}";
135 job.content_type = "application/pdf";
136 const unsigned char kDocumentBytes[] = {'b', 'y', 't', 'e', 's'};
137 job.document_bytes =
138 new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes));
140 PrinterProviderAPIFactory::GetInstance()
141 ->GetForBrowserContext(browser_context())
142 ->DispatchPrintRequested(job, callback);
145 bool StartPrintRequestUsingFileInfo(
146 const std::string& extension_id,
147 const PrinterProviderAPI::PrintCallback& callback) {
148 PrinterProviderPrintJob job;
150 const char kBytes[] = {'b', 'y', 't', 'e', 's'};
151 if (!CreateTempFileWithContents(kBytes, static_cast<int>(arraysize(kBytes)),
152 &job.document_path, &job.file_info)) {
153 ADD_FAILURE() << "Failed to create test file.";
154 return false;
157 job.printer_id = extension_id + ":printer_id";
158 job.job_title = base::ASCIIToUTF16("Print job");
159 job.ticket_json = "{}";
160 job.content_type = "image/pwg-raster";
162 PrinterProviderAPIFactory::GetInstance()
163 ->GetForBrowserContext(browser_context())
164 ->DispatchPrintRequested(job, callback);
165 return true;
168 void StartCapabilityRequest(
169 const std::string& extension_id,
170 const PrinterProviderAPI::GetCapabilityCallback& callback) {
171 PrinterProviderAPIFactory::GetInstance()
172 ->GetForBrowserContext(browser_context())
173 ->DispatchGetCapabilityRequested(extension_id + ":printer_id",
174 callback);
177 // Loads chrome.printerProvider test app and initializes is for test
178 // |test_param|.
179 // When the app's background page is loaded, the app will send 'loaded'
180 // message. As a response to the message it will expect string message
181 // specifying the test that should be run. When the app initializes its state
182 // (e.g. registers listener for a chrome.printerProvider event) it will send
183 // message 'ready', at which point the test may be started.
184 // If the app is successfully initialized, |*extension_id_out| will be set to
185 // the loaded extension's id, otherwise it will remain unchanged.
186 void InitializePrinterProviderTestApp(const std::string& app_path,
187 const std::string& test_param,
188 std::string* extension_id_out) {
189 ExtensionTestMessageListener loaded_listener("loaded", true);
190 ExtensionTestMessageListener ready_listener("ready", false);
192 const Extension* extension = LoadApp(app_path);
193 ASSERT_TRUE(extension);
194 const std::string extension_id = extension->id();
196 loaded_listener.set_extension_id(extension_id);
197 ready_listener.set_extension_id(extension_id);
199 ASSERT_TRUE(loaded_listener.WaitUntilSatisfied());
201 loaded_listener.Reply(test_param);
203 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
205 *extension_id_out = extension_id;
208 // Runs a test for chrome.printerProvider.onPrintRequested event.
209 // |test_param|: The test that should be run.
210 // |expected_result|: The print result the app is expected to report.
211 void RunPrintRequestTestApp(const std::string& test_param,
212 PrintRequestDataType data_type,
213 const std::string& expected_result) {
214 ResultCatcher catcher;
216 std::string extension_id;
217 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
218 test_param, &extension_id);
219 if (extension_id.empty())
220 return;
222 base::RunLoop run_loop;
223 bool success;
224 std::string print_status;
225 PrinterProviderAPI::PrintCallback callback =
226 base::Bind(&RecordPrintResultAndRunCallback, &success, &print_status,
227 run_loop.QuitClosure());
229 switch (data_type) {
230 case PRINT_REQUEST_DATA_TYPE_NOT_SET:
231 StartPrintRequestWithNoData(extension_id, callback);
232 break;
233 case PRINT_REQUEST_DATA_TYPE_FILE:
234 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback));
235 break;
236 case PRINT_REQUEST_DATA_TYPE_FILE_DELETED:
237 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback));
238 ASSERT_TRUE(data_dir_.Delete());
239 break;
240 case PRINT_REQUEST_DATA_TYPE_BYTES:
241 StartPrintRequestUsingDocumentBytes(extension_id, callback);
242 break;
245 if (data_type != PRINT_REQUEST_DATA_TYPE_NOT_SET)
246 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
248 run_loop.Run();
249 EXPECT_EQ(expected_result, print_status);
250 EXPECT_EQ(expected_result == "OK", success);
253 // Runs a test for chrome.printerProvider.onGetCapabilityRequested
254 // event.
255 // |test_param|: The test that should be run.
256 // |expected_result|: The printer capability the app is expected to report.
257 void RunPrinterCapabilitiesRequestTest(const std::string& test_param,
258 const std::string& expected_result) {
259 ResultCatcher catcher;
261 std::string extension_id;
262 InitializePrinterProviderTestApp(
263 "api_test/printer_provider/request_capability", test_param,
264 &extension_id);
265 if (extension_id.empty())
266 return;
268 base::RunLoop run_loop;
269 std::string result;
270 StartCapabilityRequest(
271 extension_id,
272 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure()));
274 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
276 run_loop.Run();
277 EXPECT_EQ(expected_result, result);
280 // Run a test for the chrome.printerProvider.onGetUsbPrinterInfoRequested
281 // event.
282 // |test_param|: The test that should be run.
283 // |expected_result|: The printer info that the app is expected to report.
284 void RunUsbPrinterInfoRequestTest(const std::string& test_param) {
285 ResultCatcher catcher;
286 scoped_refptr<device::UsbDevice> device =
287 new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
288 usb_service_.AddDevice(device);
290 std::string extension_id;
291 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
292 test_param, &extension_id);
293 ASSERT_FALSE(extension_id.empty());
295 scoped_ptr<base::Value> expected_printer_info(new base::DictionaryValue());
296 base::RunLoop run_loop;
297 StartGetUsbPrinterInfoRequest(
298 extension_id, device,
299 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
300 run_loop.QuitClosure()));
301 run_loop.Run();
303 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
306 bool SimulateExtensionUnload(const std::string& extension_id) {
307 ExtensionRegistry* extension_registry =
308 ExtensionRegistry::Get(browser_context());
310 const Extension* extension = extension_registry->GetExtensionById(
311 extension_id, ExtensionRegistry::ENABLED);
312 if (!extension)
313 return false;
315 extension_registry->RemoveEnabled(extension_id);
316 extension_registry->TriggerOnUnloaded(
317 extension, UnloadedExtensionInfo::REASON_TERMINATE);
318 return true;
321 // Validates that set of printers reported by test apps via
322 // chrome.printerProvider.onGetPritersRequested is the same as the set of
323 // printers in |expected_printers|. |expected_printers| contains list of
324 // printer objects formatted as a JSON string. It is assumed that the values
325 // in |expoected_printers| are unique.
326 void ValidatePrinterListValue(
327 const base::ListValue& printers,
328 const ScopedVector<base::Value>& expected_printers) {
329 ASSERT_EQ(expected_printers.size(), printers.GetSize());
330 for (const base::Value* printer_value : expected_printers) {
331 EXPECT_TRUE(printers.Find(*printer_value) != printers.end())
332 << "Unable to find " << *printer_value << " in " << printers;
336 protected:
337 device::MockUsbService usb_service_;
339 private:
340 // Initializes |data_dir_| if needed and creates a file in it containing
341 // provided data.
342 bool CreateTempFileWithContents(const char* data,
343 int size,
344 base::FilePath* path,
345 base::File::Info* file_info) {
346 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir())
347 return false;
349 *path = data_dir_.path().AppendASCII("data.pwg");
350 int written = base::WriteFile(*path, data, size);
351 if (written != size)
352 return false;
353 if (!base::GetFileInfo(*path, file_info))
354 return false;
355 return true;
358 base::ScopedTempDir data_dir_;
360 DISALLOW_COPY_AND_ASSIGN(PrinterProviderApiTest);
363 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobSuccess) {
364 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_BYTES, "OK");
367 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobWithFileSuccess) {
368 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE, "OK");
371 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
372 PrintJobWithFile_FileDeletedBeforeDispatch) {
373 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE_DELETED,
374 "INVALID_DATA");
377 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobAsyncSuccess) {
378 RunPrintRequestTestApp("ASYNC_RESPONSE", PRINT_REQUEST_DATA_TYPE_BYTES, "OK");
381 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobFailed) {
382 RunPrintRequestTestApp("INVALID_TICKET", PRINT_REQUEST_DATA_TYPE_BYTES,
383 "INVALID_TICKET");
386 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoPrintEventListener) {
387 RunPrintRequestTestApp("NO_LISTENER", PRINT_REQUEST_DATA_TYPE_BYTES,
388 "FAILED");
391 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
392 PrintRequestInvalidCallbackParam) {
393 RunPrintRequestTestApp("INVALID_VALUE", PRINT_REQUEST_DATA_TYPE_BYTES,
394 "FAILED");
397 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestDataNotSet) {
398 RunPrintRequestTestApp("IGNORE_CALLBACK", PRINT_REQUEST_DATA_TYPE_NOT_SET,
399 "FAILED");
402 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestAppUnloaded) {
403 ResultCatcher catcher;
405 std::string extension_id;
406 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
407 "IGNORE_CALLBACK", &extension_id);
408 ASSERT_FALSE(extension_id.empty());
410 base::RunLoop run_loop;
411 bool success = false;
412 std::string status;
413 StartPrintRequestUsingDocumentBytes(
414 extension_id, base::Bind(&RecordPrintResultAndRunCallback, &success,
415 &status, run_loop.QuitClosure()));
417 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
419 ASSERT_TRUE(SimulateExtensionUnload(extension_id));
421 run_loop.Run();
422 EXPECT_FALSE(success);
423 EXPECT_EQ("FAILED", status);
426 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilitySuccess) {
427 RunPrinterCapabilitiesRequestTest("OK", "{\"capability\":\"value\"}");
430 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilityAsyncSuccess) {
431 RunPrinterCapabilitiesRequestTest("ASYNC_RESPONSE",
432 "{\"capability\":\"value\"}");
435 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, EmptyCapability) {
436 RunPrinterCapabilitiesRequestTest("EMPTY", "{}");
439 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoCapabilityEventListener) {
440 RunPrinterCapabilitiesRequestTest("NO_LISTENER", "{}");
443 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, CapabilityInvalidValue) {
444 RunPrinterCapabilitiesRequestTest("INVALID_VALUE", "{}");
447 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilityAppUnloaded) {
448 ResultCatcher catcher;
450 std::string extension_id;
451 InitializePrinterProviderTestApp(
452 "api_test/printer_provider/request_capability", "IGNORE_CALLBACK",
453 &extension_id);
454 ASSERT_FALSE(extension_id.empty());
456 base::RunLoop run_loop;
457 std::string result;
458 StartCapabilityRequest(
459 extension_id,
460 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure()));
462 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
464 ASSERT_TRUE(SimulateExtensionUnload(extension_id));
465 run_loop.Run();
466 EXPECT_EQ("{}", result);
469 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersSuccess) {
470 ResultCatcher catcher;
472 std::string extension_id;
473 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
474 "OK", &extension_id);
475 ASSERT_FALSE(extension_id.empty());
477 base::RunLoop run_loop;
478 base::ListValue printers;
480 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
481 &printers, run_loop.QuitClosure()));
483 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
485 run_loop.Run();
487 ScopedVector<base::Value> expected_printers;
488 expected_printers.push_back(
489 DictionaryBuilder()
490 .Set("description", "Test printer")
491 .Set("extensionId", extension_id)
492 .Set("extensionName", "Test printer provider")
493 .Set("id", base::StringPrintf("%s:printer1", extension_id.c_str()))
494 .Set("name", "Printer 1")
495 .Build());
496 expected_printers.push_back(
497 DictionaryBuilder()
498 .Set("extensionId", extension_id)
499 .Set("extensionName", "Test printer provider")
500 .Set("id",
501 base::StringPrintf("%s:printerNoDesc", extension_id.c_str()))
502 .Set("name", "Printer 2")
503 .Build());
505 ValidatePrinterListValue(printers, expected_printers);
508 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersAsyncSuccess) {
509 ResultCatcher catcher;
511 std::string extension_id;
512 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
513 "ASYNC_RESPONSE", &extension_id);
514 ASSERT_FALSE(extension_id.empty());
516 base::RunLoop run_loop;
517 base::ListValue printers;
519 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
520 &printers, run_loop.QuitClosure()));
522 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
524 run_loop.Run();
526 ScopedVector<base::Value> expected_printers;
527 expected_printers.push_back(
528 DictionaryBuilder()
529 .Set("description", "Test printer")
530 .Set("extensionId", extension_id)
531 .Set("extensionName", "Test printer provider")
532 .Set("id", base::StringPrintf("%s:printer1", extension_id.c_str()))
533 .Set("name", "Printer 1")
534 .Build());
536 ValidatePrinterListValue(printers, expected_printers);
539 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersTwoExtensions) {
540 ResultCatcher catcher;
542 std::string extension_id_1;
543 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
544 "OK", &extension_id_1);
545 ASSERT_FALSE(extension_id_1.empty());
547 std::string extension_id_2;
548 InitializePrinterProviderTestApp(
549 "api_test/printer_provider/request_printers_second", "OK",
550 &extension_id_2);
551 ASSERT_FALSE(extension_id_2.empty());
553 base::RunLoop run_loop;
554 base::ListValue printers;
556 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
557 &printers, run_loop.QuitClosure()));
559 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
560 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
562 run_loop.Run();
564 ScopedVector<base::Value> expected_printers;
565 expected_printers.push_back(
566 DictionaryBuilder()
567 .Set("description", "Test printer")
568 .Set("extensionId", extension_id_1)
569 .Set("extensionName", "Test printer provider")
570 .Set("id", base::StringPrintf("%s:printer1", extension_id_1.c_str()))
571 .Set("name", "Printer 1")
572 .Build());
573 expected_printers.push_back(
574 DictionaryBuilder()
575 .Set("extensionId", extension_id_1)
576 .Set("extensionName", "Test printer provider")
577 .Set("id",
578 base::StringPrintf("%s:printerNoDesc", extension_id_1.c_str()))
579 .Set("name", "Printer 2")
580 .Build());
581 expected_printers.push_back(
582 DictionaryBuilder()
583 .Set("description", "Test printer")
584 .Set("extensionId", extension_id_2)
585 .Set("extensionName", "Test printer provider")
586 .Set("id", base::StringPrintf("%s:printer1", extension_id_2.c_str()))
587 .Set("name", "Printer 1")
588 .Build());
589 expected_printers.push_back(
590 DictionaryBuilder()
591 .Set("extensionId", extension_id_2)
592 .Set("extensionName", "Test printer provider")
593 .Set("id",
594 base::StringPrintf("%s:printerNoDesc", extension_id_2.c_str()))
595 .Set("name", "Printer 2")
596 .Build());
598 ValidatePrinterListValue(printers, expected_printers);
601 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
602 GetPrintersTwoExtensionsBothUnloaded) {
603 ResultCatcher catcher;
605 std::string extension_id_1;
606 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
607 "IGNORE_CALLBACK", &extension_id_1);
608 ASSERT_FALSE(extension_id_1.empty());
610 std::string extension_id_2;
611 InitializePrinterProviderTestApp(
612 "api_test/printer_provider/request_printers_second", "IGNORE_CALLBACK",
613 &extension_id_2);
614 ASSERT_FALSE(extension_id_2.empty());
616 base::RunLoop run_loop;
617 base::ListValue printers;
619 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
620 &printers, run_loop.QuitClosure()));
622 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
623 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
625 ASSERT_TRUE(SimulateExtensionUnload(extension_id_1));
626 ASSERT_TRUE(SimulateExtensionUnload(extension_id_2));
628 run_loop.Run();
630 EXPECT_TRUE(printers.empty());
633 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
634 GetPrintersTwoExtensionsOneFails) {
635 ResultCatcher catcher;
637 std::string extension_id_1;
638 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
639 "NOT_ARRAY", &extension_id_1);
640 ASSERT_FALSE(extension_id_1.empty());
642 std::string extension_id_2;
643 InitializePrinterProviderTestApp(
644 "api_test/printer_provider/request_printers_second", "OK",
645 &extension_id_2);
646 ASSERT_FALSE(extension_id_2.empty());
648 base::RunLoop run_loop;
649 base::ListValue printers;
651 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
652 &printers, run_loop.QuitClosure()));
654 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
655 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
657 run_loop.Run();
659 ScopedVector<base::Value> expected_printers;
660 expected_printers.push_back(
661 DictionaryBuilder()
662 .Set("description", "Test printer")
663 .Set("extensionId", extension_id_2)
664 .Set("extensionName", "Test printer provider")
665 .Set("id", base::StringPrintf("%s:printer1", extension_id_2.c_str()))
666 .Set("name", "Printer 1")
667 .Build());
668 expected_printers.push_back(
669 DictionaryBuilder()
670 .Set("extensionId", extension_id_2)
671 .Set("extensionName", "Test printer provider")
672 .Set("id",
673 base::StringPrintf("%s:printerNoDesc", extension_id_2.c_str()))
674 .Set("name", "Printer 2")
675 .Build());
677 ValidatePrinterListValue(printers, expected_printers);
680 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
681 GetPrintersTwoExtensionsOneWithNoListener) {
682 ResultCatcher catcher;
684 std::string extension_id_1;
685 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
686 "NO_LISTENER", &extension_id_1);
687 ASSERT_FALSE(extension_id_1.empty());
689 std::string extension_id_2;
690 InitializePrinterProviderTestApp(
691 "api_test/printer_provider/request_printers_second", "OK",
692 &extension_id_2);
693 ASSERT_FALSE(extension_id_2.empty());
695 base::RunLoop run_loop;
696 base::ListValue printers;
698 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
699 &printers, run_loop.QuitClosure()));
701 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
702 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
704 run_loop.Run();
706 ScopedVector<base::Value> expected_printers;
707 expected_printers.push_back(
708 DictionaryBuilder()
709 .Set("description", "Test printer")
710 .Set("extensionId", extension_id_2)
711 .Set("extensionName", "Test printer provider")
712 .Set("id", base::StringPrintf("%s:printer1", extension_id_2.c_str()))
713 .Set("name", "Printer 1")
714 .Build());
715 expected_printers.push_back(
716 DictionaryBuilder()
717 .Set("extensionId", extension_id_2)
718 .Set("extensionName", "Test printer provider")
719 .Set("id",
720 base::StringPrintf("%s:printerNoDesc", extension_id_2.c_str()))
721 .Set("name", "Printer 2")
722 .Build());
724 ValidatePrinterListValue(printers, expected_printers);
727 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersNoListener) {
728 ResultCatcher catcher;
730 std::string extension_id;
731 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
732 "NO_LISTENER", &extension_id);
733 ASSERT_FALSE(extension_id.empty());
735 base::RunLoop run_loop;
736 base::ListValue printers;
738 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
739 &printers, run_loop.QuitClosure()));
741 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
743 run_loop.Run();
745 EXPECT_TRUE(printers.empty());
748 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersNotArray) {
749 ResultCatcher catcher;
751 std::string extension_id;
752 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
753 "NOT_ARRAY", &extension_id);
754 ASSERT_FALSE(extension_id.empty());
756 base::RunLoop run_loop;
757 base::ListValue printers;
759 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
760 &printers, run_loop.QuitClosure()));
762 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
764 run_loop.Run();
766 EXPECT_TRUE(printers.empty());
769 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
770 GetPrintersInvalidPrinterValueType) {
771 ResultCatcher catcher;
773 std::string extension_id;
774 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
775 "INVALID_PRINTER_TYPE", &extension_id);
776 ASSERT_FALSE(extension_id.empty());
778 base::RunLoop run_loop;
779 base::ListValue printers;
781 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
782 &printers, run_loop.QuitClosure()));
784 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
786 run_loop.Run();
788 EXPECT_TRUE(printers.empty());
791 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersInvalidPrinterValue) {
792 ResultCatcher catcher;
794 std::string extension_id;
795 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
796 "INVALID_PRINTER", &extension_id);
797 ASSERT_FALSE(extension_id.empty());
799 base::RunLoop run_loop;
800 base::ListValue printers;
802 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
803 &printers, run_loop.QuitClosure()));
805 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
807 run_loop.Run();
809 EXPECT_TRUE(printers.empty());
812 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfo) {
813 ResultCatcher catcher;
814 scoped_refptr<device::UsbDevice> device =
815 new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
816 usb_service_.AddDevice(device);
818 std::string extension_id;
819 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
820 "OK", &extension_id);
821 ASSERT_FALSE(extension_id.empty());
823 UsbGuidMap* guid_map = UsbGuidMap::Get(browser_context());
824 scoped_ptr<base::Value> expected_printer_info(
825 DictionaryBuilder()
826 .Set("description", "This printer is a USB device.")
827 .Set("extensionId", extension_id)
828 .Set("extensionName", "Test USB printer provider")
829 .Set("id",
830 base::StringPrintf("%s:usbDevice-%u", extension_id.c_str(),
831 guid_map->GetIdFromGuid(device->guid())))
832 .Set("name", "Test Printer")
833 .Build());
834 base::RunLoop run_loop;
835 StartGetUsbPrinterInfoRequest(
836 extension_id, device,
837 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
838 run_loop.QuitClosure()));
839 run_loop.Run();
841 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
844 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoEmptyResponse) {
845 RunUsbPrinterInfoRequestTest("EMPTY_RESPONSE");
848 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoNoListener) {
849 RunUsbPrinterInfoRequestTest("NO_LISTENER");
852 } // namespace
854 } // namespace extensions