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.
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 "extensions/browser/api/printer_provider/printer_provider_api.h"
17 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
18 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/value_builder.h"
22 #include "extensions/shell/test/shell_apitest.h"
23 #include "extensions/test/extension_test_message_listener.h"
24 #include "extensions/test/result_catcher.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace extensions
{
31 // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls.
32 // It appends items in |printers| to |*printers_out|. If |done| is set, it runs
34 void AppendPrintersAndRunCallbackIfDone(base::ListValue
* printers_out
,
35 const base::Closure
& callback
,
36 const base::ListValue
& printers
,
38 for (size_t i
= 0; i
< printers
.GetSize(); ++i
) {
39 const base::DictionaryValue
* printer
= NULL
;
40 EXPECT_TRUE(printers
.GetDictionary(i
, &printer
))
41 << "Found invalid printer value at index " << i
<< ": " << printers
;
43 printers_out
->Append(printer
->DeepCopy());
45 if (done
&& !callback
.is_null())
49 // Callback for PrinterProviderAPI::DispatchPrintRequested calls.
50 // It copies |value| to |*result| and runs |callback|.
51 void RecordPrintResultAndRunCallback(bool* result_success
,
52 std::string
* result_status
,
53 const base::Closure
& callback
,
55 const std::string
& status
) {
56 *result_success
= success
;
57 *result_status
= status
;
58 if (!callback
.is_null())
62 // Callback for PrinterProviderAPI::DispatchGetCapabilityRequested calls.
63 // It saves reported |value| as JSON string to |*result| and runs |callback|.
64 void RecordDictAndRunCallback(std::string
* result
,
65 const base::Closure
& callback
,
66 const base::DictionaryValue
& value
) {
67 JSONStringValueSerializer
serializer(result
);
68 EXPECT_TRUE(serializer
.Serialize(value
));
69 if (!callback
.is_null())
73 // Tests for chrome.printerProvider API.
74 class PrinterProviderApiTest
: public ShellApiTest
{
76 enum PrintRequestDataType
{
77 PRINT_REQUEST_DATA_TYPE_NOT_SET
,
78 PRINT_REQUEST_DATA_TYPE_FILE
,
79 PRINT_REQUEST_DATA_TYPE_FILE_DELETED
,
80 PRINT_REQUEST_DATA_TYPE_BYTES
83 PrinterProviderApiTest() {}
84 ~PrinterProviderApiTest() override
{}
86 void StartGetPrintersRequest(
87 const PrinterProviderAPI::GetPrintersCallback
& callback
) {
88 PrinterProviderAPIFactory::GetInstance()
89 ->GetForBrowserContext(browser_context())
90 ->DispatchGetPrintersRequested(callback
);
93 void StartPrintRequestWithNoData(
94 const std::string
& extension_id
,
95 const PrinterProviderAPI::PrintCallback
& callback
) {
96 PrinterProviderPrintJob job
;
97 job
.printer_id
= extension_id
+ ":printer_id";
98 job
.ticket_json
= "{}";
99 job
.content_type
= "application/pdf";
101 PrinterProviderAPIFactory::GetInstance()
102 ->GetForBrowserContext(browser_context())
103 ->DispatchPrintRequested(job
, callback
);
106 void StartPrintRequestUsingDocumentBytes(
107 const std::string
& extension_id
,
108 const PrinterProviderAPI::PrintCallback
& callback
) {
109 PrinterProviderPrintJob job
;
110 job
.printer_id
= extension_id
+ ":printer_id";
111 job
.job_title
= base::ASCIIToUTF16("Print job");
112 job
.ticket_json
= "{}";
113 job
.content_type
= "application/pdf";
114 const unsigned char kDocumentBytes
[] = {'b', 'y', 't', 'e', 's'};
116 new base::RefCountedBytes(kDocumentBytes
, arraysize(kDocumentBytes
));
118 PrinterProviderAPIFactory::GetInstance()
119 ->GetForBrowserContext(browser_context())
120 ->DispatchPrintRequested(job
, callback
);
123 bool StartPrintRequestUsingFileInfo(
124 const std::string
& extension_id
,
125 const PrinterProviderAPI::PrintCallback
& callback
) {
126 PrinterProviderPrintJob job
;
128 const char kBytes
[] = {'b', 'y', 't', 'e', 's'};
129 if (!CreateTempFileWithContents(kBytes
, static_cast<int>(arraysize(kBytes
)),
130 &job
.document_path
, &job
.file_info
)) {
131 ADD_FAILURE() << "Failed to create test file.";
135 job
.printer_id
= extension_id
+ ":printer_id";
136 job
.job_title
= base::ASCIIToUTF16("Print job");
137 job
.ticket_json
= "{}";
138 job
.content_type
= "image/pwg-raster";
140 PrinterProviderAPIFactory::GetInstance()
141 ->GetForBrowserContext(browser_context())
142 ->DispatchPrintRequested(job
, callback
);
146 void StartCapabilityRequest(
147 const std::string
& extension_id
,
148 const PrinterProviderAPI::GetCapabilityCallback
& callback
) {
149 PrinterProviderAPIFactory::GetInstance()
150 ->GetForBrowserContext(browser_context())
151 ->DispatchGetCapabilityRequested(extension_id
+ ":printer_id",
155 // Loads chrome.printerProvider test app and initializes is for test
157 // When the app's background page is loaded, the app will send 'loaded'
158 // message. As a response to the message it will expect string message
159 // specifying the test that should be run. When the app initializes its state
160 // (e.g. registers listener for a chrome.printerProvider event) it will send
161 // message 'ready', at which point the test may be started.
162 // If the app is successfully initialized, |*extension_id_out| will be set to
163 // the loaded extension's id, otherwise it will remain unchanged.
164 void InitializePrinterProviderTestApp(const std::string
& app_path
,
165 const std::string
& test_param
,
166 std::string
* extension_id_out
) {
167 ExtensionTestMessageListener
loaded_listener("loaded", true);
168 ExtensionTestMessageListener
ready_listener("ready", false);
170 const Extension
* extension
= LoadApp(app_path
);
171 ASSERT_TRUE(extension
);
172 const std::string extension_id
= extension
->id();
174 loaded_listener
.set_extension_id(extension_id
);
175 ready_listener
.set_extension_id(extension_id
);
177 ASSERT_TRUE(loaded_listener
.WaitUntilSatisfied());
179 loaded_listener
.Reply(test_param
);
181 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
183 *extension_id_out
= extension_id
;
186 // Runs a test for chrome.printerProvider.onPrintRequested event.
187 // |test_param|: The test that should be run.
188 // |expected_result|: The print result the app is expected to report.
189 void RunPrintRequestTestApp(const std::string
& test_param
,
190 PrintRequestDataType data_type
,
191 const std::string
& expected_result
) {
192 ResultCatcher catcher
;
194 std::string extension_id
;
195 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
196 test_param
, &extension_id
);
197 if (extension_id
.empty())
200 base::RunLoop run_loop
;
202 std::string print_status
;
203 PrinterProviderAPI::PrintCallback callback
=
204 base::Bind(&RecordPrintResultAndRunCallback
, &success
, &print_status
,
205 run_loop
.QuitClosure());
208 case PRINT_REQUEST_DATA_TYPE_NOT_SET
:
209 StartPrintRequestWithNoData(extension_id
, callback
);
211 case PRINT_REQUEST_DATA_TYPE_FILE
:
212 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id
, callback
));
214 case PRINT_REQUEST_DATA_TYPE_FILE_DELETED
:
215 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id
, callback
));
216 ASSERT_TRUE(data_dir_
.Delete());
218 case PRINT_REQUEST_DATA_TYPE_BYTES
:
219 StartPrintRequestUsingDocumentBytes(extension_id
, callback
);
223 if (data_type
!= PRINT_REQUEST_DATA_TYPE_NOT_SET
)
224 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
227 EXPECT_EQ(expected_result
, print_status
);
228 EXPECT_EQ(expected_result
== "OK", success
);
231 // Runs a test for chrome.printerProvider.onGetCapabilityRequested
233 // |test_param|: The test that should be run.
234 // |expected_result|: The printer capability the app is expected to report.
235 void RunPrinterCapabilitiesRequestTest(const std::string
& test_param
,
236 const std::string
& expected_result
) {
237 ResultCatcher catcher
;
239 std::string extension_id
;
240 InitializePrinterProviderTestApp(
241 "api_test/printer_provider/request_capability", test_param
,
243 if (extension_id
.empty())
246 base::RunLoop run_loop
;
248 StartCapabilityRequest(
250 base::Bind(&RecordDictAndRunCallback
, &result
, run_loop
.QuitClosure()));
252 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
255 EXPECT_EQ(expected_result
, result
);
258 bool SimulateExtensionUnload(const std::string
& extension_id
) {
259 ExtensionRegistry
* extension_registry
=
260 ExtensionRegistry::Get(browser_context());
262 const Extension
* extension
= extension_registry
->GetExtensionById(
263 extension_id
, ExtensionRegistry::ENABLED
);
267 extension_registry
->RemoveEnabled(extension_id
);
268 extension_registry
->TriggerOnUnloaded(
269 extension
, UnloadedExtensionInfo::REASON_TERMINATE
);
273 // Validates that set of printers reported by test apps via
274 // chrome.printerProvider.onGetPritersRequested is the same as the set of
275 // printers in |expected_printers|. |expected_printers| contains list of
276 // printer objects formatted as a JSON string. It is assumed that the values
277 // in |expoected_printers| are unique.
278 void ValidatePrinterListValue(
279 const base::ListValue
& printers
,
280 const ScopedVector
<base::Value
>& expected_printers
) {
281 ASSERT_EQ(expected_printers
.size(), printers
.GetSize());
282 for (const base::Value
* printer_value
: expected_printers
) {
283 EXPECT_TRUE(printers
.Find(*printer_value
) != printers
.end())
284 << "Unabe to find " << *printer_value
<< " in " << printers
;
289 // Initializes |data_dir_| if needed and creates a file in it containing
291 bool CreateTempFileWithContents(const char* data
,
293 base::FilePath
* path
,
294 base::File::Info
* file_info
) {
295 if (!data_dir_
.IsValid() && !data_dir_
.CreateUniqueTempDir())
298 *path
= data_dir_
.path().AppendASCII("data.pwg");
299 int written
= base::WriteFile(*path
, data
, size
);
302 if (!base::GetFileInfo(*path
, file_info
))
307 base::ScopedTempDir data_dir_
;
309 DISALLOW_COPY_AND_ASSIGN(PrinterProviderApiTest
);
312 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintJobSuccess
) {
313 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_BYTES
, "OK");
316 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintJobWithFileSuccess
) {
317 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE
, "OK");
320 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
321 PrintJobWithFile_FileDeletedBeforeDispatch
) {
322 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE_DELETED
,
326 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintJobAsyncSuccess
) {
327 RunPrintRequestTestApp("ASYNC_RESPONSE", PRINT_REQUEST_DATA_TYPE_BYTES
, "OK");
330 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintJobFailed
) {
331 RunPrintRequestTestApp("INVALID_TICKET", PRINT_REQUEST_DATA_TYPE_BYTES
,
335 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, NoPrintEventListener
) {
336 RunPrintRequestTestApp("NO_LISTENER", PRINT_REQUEST_DATA_TYPE_BYTES
,
340 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
341 PrintRequestInvalidCallbackParam
) {
342 RunPrintRequestTestApp("INVALID_VALUE", PRINT_REQUEST_DATA_TYPE_BYTES
,
346 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintRequestDataNotSet
) {
347 RunPrintRequestTestApp("IGNORE_CALLBACK", PRINT_REQUEST_DATA_TYPE_NOT_SET
,
351 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, PrintRequestAppUnloaded
) {
352 ResultCatcher catcher
;
354 std::string extension_id
;
355 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
356 "IGNORE_CALLBACK", &extension_id
);
357 ASSERT_FALSE(extension_id
.empty());
359 base::RunLoop run_loop
;
360 bool success
= false;
362 StartPrintRequestUsingDocumentBytes(
363 extension_id
, base::Bind(&RecordPrintResultAndRunCallback
, &success
,
364 &status
, run_loop
.QuitClosure()));
366 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
368 ASSERT_TRUE(SimulateExtensionUnload(extension_id
));
371 EXPECT_FALSE(success
);
372 EXPECT_EQ("FAILED", status
);
375 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetCapabilitySuccess
) {
376 RunPrinterCapabilitiesRequestTest("OK", "{\"capability\":\"value\"}");
379 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetCapabilityAsyncSuccess
) {
380 RunPrinterCapabilitiesRequestTest("ASYNC_RESPONSE",
381 "{\"capability\":\"value\"}");
384 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, EmptyCapability
) {
385 RunPrinterCapabilitiesRequestTest("EMPTY", "{}");
388 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, NoCapabilityEventListener
) {
389 RunPrinterCapabilitiesRequestTest("NO_LISTENER", "{}");
392 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, CapabilityInvalidValue
) {
393 RunPrinterCapabilitiesRequestTest("INVALID_VALUE", "{}");
396 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetCapabilityAppUnloaded
) {
397 ResultCatcher catcher
;
399 std::string extension_id
;
400 InitializePrinterProviderTestApp(
401 "api_test/printer_provider/request_capability", "IGNORE_CALLBACK",
403 ASSERT_FALSE(extension_id
.empty());
405 base::RunLoop run_loop
;
407 StartCapabilityRequest(
409 base::Bind(&RecordDictAndRunCallback
, &result
, run_loop
.QuitClosure()));
411 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
413 ASSERT_TRUE(SimulateExtensionUnload(extension_id
));
415 EXPECT_EQ("{}", result
);
418 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersSuccess
) {
419 ResultCatcher catcher
;
421 std::string extension_id
;
422 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
423 "OK", &extension_id
);
424 ASSERT_FALSE(extension_id
.empty());
426 base::RunLoop run_loop
;
427 base::ListValue printers
;
429 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
430 &printers
, run_loop
.QuitClosure()));
432 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
436 ScopedVector
<base::Value
> expected_printers
;
437 expected_printers
.push_back(
439 .Set("description", "Test printer")
440 .Set("extensionId", extension_id
)
441 .Set("extensionName", "Test printer provider")
442 .Set("id", base::StringPrintf("%s:printer1", extension_id
.c_str()))
443 .Set("name", "Printer 1")
445 expected_printers
.push_back(
447 .Set("extensionId", extension_id
)
448 .Set("extensionName", "Test printer provider")
450 base::StringPrintf("%s:printerNoDesc", extension_id
.c_str()))
451 .Set("name", "Printer 2")
454 ValidatePrinterListValue(printers
, expected_printers
);
457 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersAsyncSuccess
) {
458 ResultCatcher catcher
;
460 std::string extension_id
;
461 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
462 "ASYNC_RESPONSE", &extension_id
);
463 ASSERT_FALSE(extension_id
.empty());
465 base::RunLoop run_loop
;
466 base::ListValue printers
;
468 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
469 &printers
, run_loop
.QuitClosure()));
471 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
475 ScopedVector
<base::Value
> expected_printers
;
476 expected_printers
.push_back(
478 .Set("description", "Test printer")
479 .Set("extensionId", extension_id
)
480 .Set("extensionName", "Test printer provider")
481 .Set("id", base::StringPrintf("%s:printer1", extension_id
.c_str()))
482 .Set("name", "Printer 1")
485 ValidatePrinterListValue(printers
, expected_printers
);
488 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersTwoExtensions
) {
489 ResultCatcher catcher
;
491 std::string extension_id_1
;
492 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
493 "OK", &extension_id_1
);
494 ASSERT_FALSE(extension_id_1
.empty());
496 std::string extension_id_2
;
497 InitializePrinterProviderTestApp(
498 "api_test/printer_provider/request_printers_second", "OK",
500 ASSERT_FALSE(extension_id_2
.empty());
502 base::RunLoop run_loop
;
503 base::ListValue printers
;
505 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
506 &printers
, run_loop
.QuitClosure()));
508 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
509 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
513 ScopedVector
<base::Value
> expected_printers
;
514 expected_printers
.push_back(
516 .Set("description", "Test printer")
517 .Set("extensionId", extension_id_1
)
518 .Set("extensionName", "Test printer provider")
519 .Set("id", base::StringPrintf("%s:printer1", extension_id_1
.c_str()))
520 .Set("name", "Printer 1")
522 expected_printers
.push_back(
524 .Set("extensionId", extension_id_1
)
525 .Set("extensionName", "Test printer provider")
527 base::StringPrintf("%s:printerNoDesc", extension_id_1
.c_str()))
528 .Set("name", "Printer 2")
530 expected_printers
.push_back(
532 .Set("description", "Test printer")
533 .Set("extensionId", extension_id_2
)
534 .Set("extensionName", "Test printer provider")
535 .Set("id", base::StringPrintf("%s:printer1", extension_id_2
.c_str()))
536 .Set("name", "Printer 1")
538 expected_printers
.push_back(
540 .Set("extensionId", extension_id_2
)
541 .Set("extensionName", "Test printer provider")
543 base::StringPrintf("%s:printerNoDesc", extension_id_2
.c_str()))
544 .Set("name", "Printer 2")
547 ValidatePrinterListValue(printers
, expected_printers
);
550 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
551 GetPrintersTwoExtensionsBothUnloaded
) {
552 ResultCatcher catcher
;
554 std::string extension_id_1
;
555 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
556 "IGNORE_CALLBACK", &extension_id_1
);
557 ASSERT_FALSE(extension_id_1
.empty());
559 std::string extension_id_2
;
560 InitializePrinterProviderTestApp(
561 "api_test/printer_provider/request_printers_second", "IGNORE_CALLBACK",
563 ASSERT_FALSE(extension_id_2
.empty());
565 base::RunLoop run_loop
;
566 base::ListValue printers
;
568 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
569 &printers
, run_loop
.QuitClosure()));
571 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
572 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
574 ASSERT_TRUE(SimulateExtensionUnload(extension_id_1
));
575 ASSERT_TRUE(SimulateExtensionUnload(extension_id_2
));
579 EXPECT_TRUE(printers
.empty());
582 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
583 GetPrintersTwoExtensionsOneFails
) {
584 ResultCatcher catcher
;
586 std::string extension_id_1
;
587 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
588 "NOT_ARRAY", &extension_id_1
);
589 ASSERT_FALSE(extension_id_1
.empty());
591 std::string extension_id_2
;
592 InitializePrinterProviderTestApp(
593 "api_test/printer_provider/request_printers_second", "OK",
595 ASSERT_FALSE(extension_id_2
.empty());
597 base::RunLoop run_loop
;
598 base::ListValue printers
;
600 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
601 &printers
, run_loop
.QuitClosure()));
603 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
604 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
608 ScopedVector
<base::Value
> expected_printers
;
609 expected_printers
.push_back(
611 .Set("description", "Test printer")
612 .Set("extensionId", extension_id_2
)
613 .Set("extensionName", "Test printer provider")
614 .Set("id", base::StringPrintf("%s:printer1", extension_id_2
.c_str()))
615 .Set("name", "Printer 1")
617 expected_printers
.push_back(
619 .Set("extensionId", extension_id_2
)
620 .Set("extensionName", "Test printer provider")
622 base::StringPrintf("%s:printerNoDesc", extension_id_2
.c_str()))
623 .Set("name", "Printer 2")
626 ValidatePrinterListValue(printers
, expected_printers
);
629 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
630 GetPrintersTwoExtensionsOneWithNoListener
) {
631 ResultCatcher catcher
;
633 std::string extension_id_1
;
634 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
635 "NO_LISTENER", &extension_id_1
);
636 ASSERT_FALSE(extension_id_1
.empty());
638 std::string extension_id_2
;
639 InitializePrinterProviderTestApp(
640 "api_test/printer_provider/request_printers_second", "OK",
642 ASSERT_FALSE(extension_id_2
.empty());
644 base::RunLoop run_loop
;
645 base::ListValue printers
;
647 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
648 &printers
, run_loop
.QuitClosure()));
650 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
651 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
655 ScopedVector
<base::Value
> expected_printers
;
656 expected_printers
.push_back(
658 .Set("description", "Test printer")
659 .Set("extensionId", extension_id_2
)
660 .Set("extensionName", "Test printer provider")
661 .Set("id", base::StringPrintf("%s:printer1", extension_id_2
.c_str()))
662 .Set("name", "Printer 1")
664 expected_printers
.push_back(
666 .Set("extensionId", extension_id_2
)
667 .Set("extensionName", "Test printer provider")
669 base::StringPrintf("%s:printerNoDesc", extension_id_2
.c_str()))
670 .Set("name", "Printer 2")
673 ValidatePrinterListValue(printers
, expected_printers
);
676 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersNoListener
) {
677 ResultCatcher catcher
;
679 std::string extension_id
;
680 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
681 "NO_LISTENER", &extension_id
);
682 ASSERT_FALSE(extension_id
.empty());
684 base::RunLoop run_loop
;
685 base::ListValue printers
;
687 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
688 &printers
, run_loop
.QuitClosure()));
690 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
694 EXPECT_TRUE(printers
.empty());
697 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersNotArray
) {
698 ResultCatcher catcher
;
700 std::string extension_id
;
701 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
702 "NOT_ARRAY", &extension_id
);
703 ASSERT_FALSE(extension_id
.empty());
705 base::RunLoop run_loop
;
706 base::ListValue printers
;
708 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
709 &printers
, run_loop
.QuitClosure()));
711 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
715 EXPECT_TRUE(printers
.empty());
718 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
,
719 GetPrintersInvalidPrinterValueType
) {
720 ResultCatcher catcher
;
722 std::string extension_id
;
723 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
724 "INVALID_PRINTER_TYPE", &extension_id
);
725 ASSERT_FALSE(extension_id
.empty());
727 base::RunLoop run_loop
;
728 base::ListValue printers
;
730 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
731 &printers
, run_loop
.QuitClosure()));
733 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
737 EXPECT_TRUE(printers
.empty());
740 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest
, GetPrintersInvalidPrinterValue
) {
741 ResultCatcher catcher
;
743 std::string extension_id
;
744 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
745 "INVALID_PRINTER", &extension_id
);
746 ASSERT_FALSE(extension_id
.empty());
748 base::RunLoop run_loop
;
749 base::ListValue printers
;
751 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone
,
752 &printers
, run_loop
.QuitClosure()));
754 ASSERT_TRUE(catcher
.GetNextResult()) << catcher
.message();
758 EXPECT_TRUE(printers
.empty());
763 } // namespace extensions