Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / printer_provider / printer_provider_apitest.cc
blobdfe5086ed684e52ab5349be678cf38bf9c1db4a3
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/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "extensions/browser/api/printer_provider/printer_provider_api.h"
16 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h"
17 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/shell/test/shell_apitest.h"
21 #include "extensions/test/extension_test_message_listener.h"
22 #include "extensions/test/result_catcher.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace {
27 using extensions::PrinterProviderAPI;
28 using extensions::PrinterProviderAPIFactory;
30 // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls.
31 // It appends items in |printers| to |*printers_out|. If |done| is set, it runs
32 // |callback|.
33 void AppendPrintersAndRunCallbackIfDone(base::ListValue* printers_out,
34 const base::Closure& callback,
35 const base::ListValue& printers,
36 bool done) {
37 for (size_t i = 0; i < printers.GetSize(); ++i) {
38 const base::DictionaryValue* printer = NULL;
39 EXPECT_TRUE(printers.GetDictionary(i, &printer))
40 << "Found invalid printer value at index " << i << ": " << printers;
41 if (printer)
42 printers_out->Append(printer->DeepCopy());
44 if (done && !callback.is_null())
45 callback.Run();
48 // Callback for PrinterProviderAPI::DispatchPrintRequested calls.
49 // It copies |value| to |*result| and runs |callback|.
50 void RecordPrintResultAndRunCallback(bool* result_success,
51 std::string* result_status,
52 const base::Closure& callback,
53 bool success,
54 const std::string& status) {
55 *result_success = success;
56 *result_status = status;
57 if (!callback.is_null())
58 callback.Run();
61 // Callback for PrinterProviderAPI::DispatchGetCapabilityRequested calls.
62 // It saves reported |value| as JSON string to |*result| and runs |callback|.
63 void RecordDictAndRunCallback(std::string* result,
64 const base::Closure& callback,
65 const base::DictionaryValue& value) {
66 JSONStringValueSerializer serializer(result);
67 EXPECT_TRUE(serializer.Serialize(value));
68 if (!callback.is_null())
69 callback.Run();
72 // Tests for chrome.printerProvider API.
73 class PrinterProviderApiTest : public extensions::ShellApiTest {
74 public:
75 enum PrintRequestDataType {
76 PRINT_REQUEST_DATA_TYPE_NOT_SET,
77 PRINT_REQUEST_DATA_TYPE_FILE,
78 PRINT_REQUEST_DATA_TYPE_FILE_DELETED,
79 PRINT_REQUEST_DATA_TYPE_BYTES
82 PrinterProviderApiTest() {}
83 ~PrinterProviderApiTest() override {}
85 void StartGetPrintersRequest(
86 const PrinterProviderAPI::GetPrintersCallback& callback) {
87 PrinterProviderAPIFactory::GetInstance()
88 ->GetForBrowserContext(browser_context())
89 ->DispatchGetPrintersRequested(callback);
92 void StartPrintRequestWithNoData(
93 const std::string& extension_id,
94 const PrinterProviderAPI::PrintCallback& callback) {
95 extensions::PrinterProviderPrintJob job;
96 job.printer_id = extension_id + ":printer_id";
97 job.ticket_json = "{}";
98 job.content_type = "application/pdf";
100 PrinterProviderAPIFactory::GetInstance()
101 ->GetForBrowserContext(browser_context())
102 ->DispatchPrintRequested(job, callback);
105 void StartPrintRequestUsingDocumentBytes(
106 const std::string& extension_id,
107 const PrinterProviderAPI::PrintCallback& callback) {
108 extensions::PrinterProviderPrintJob job;
109 job.printer_id = extension_id + ":printer_id";
110 job.job_title = base::ASCIIToUTF16("Print job");
111 job.ticket_json = "{}";
112 job.content_type = "application/pdf";
113 const unsigned char kDocumentBytes[] = {'b', 'y', 't', 'e', 's'};
114 job.document_bytes =
115 new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes));
117 PrinterProviderAPIFactory::GetInstance()
118 ->GetForBrowserContext(browser_context())
119 ->DispatchPrintRequested(job, callback);
122 bool StartPrintRequestUsingFileInfo(
123 const std::string& extension_id,
124 const PrinterProviderAPI::PrintCallback& callback) {
125 extensions::PrinterProviderPrintJob job;
127 const char kBytes[] = {'b', 'y', 't', 'e', 's'};
128 if (!CreateTempFileWithContents(kBytes, static_cast<int>(arraysize(kBytes)),
129 &job.document_path, &job.file_info)) {
130 ADD_FAILURE() << "Failed to create test file.";
131 return false;
134 job.printer_id = extension_id + ":printer_id";
135 job.job_title = base::ASCIIToUTF16("Print job");
136 job.ticket_json = "{}";
137 job.content_type = "image/pwg-raster";
139 PrinterProviderAPIFactory::GetInstance()
140 ->GetForBrowserContext(browser_context())
141 ->DispatchPrintRequested(job, callback);
142 return true;
145 void StartCapabilityRequest(
146 const std::string& extension_id,
147 const PrinterProviderAPI::GetCapabilityCallback& callback) {
148 PrinterProviderAPIFactory::GetInstance()
149 ->GetForBrowserContext(browser_context())
150 ->DispatchGetCapabilityRequested(extension_id + ":printer_id",
151 callback);
154 // Loads chrome.printerProvider test app and initializes is for test
155 // |test_param|.
156 // When the app's background page is loaded, the app will send 'loaded'
157 // message. As a response to the message it will expect string message
158 // specifying the test that should be run. When the app initializes its state
159 // (e.g. registers listener for a chrome.printerProvider event) it will send
160 // message 'ready', at which point the test may be started.
161 // If the app is successfully initialized, |*extension_id_out| will be set to
162 // the loaded extension's id, otherwise it will remain unchanged.
163 void InitializePrinterProviderTestApp(const std::string& app_path,
164 const std::string& test_param,
165 std::string* extension_id_out) {
166 ExtensionTestMessageListener loaded_listener("loaded", true);
167 ExtensionTestMessageListener ready_listener("ready", false);
169 const extensions::Extension* extension = LoadApp(app_path);
170 ASSERT_TRUE(extension);
171 const std::string extension_id = extension->id();
173 loaded_listener.set_extension_id(extension_id);
174 ready_listener.set_extension_id(extension_id);
176 ASSERT_TRUE(loaded_listener.WaitUntilSatisfied());
178 loaded_listener.Reply(test_param);
180 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
182 *extension_id_out = extension_id;
185 // Runs a test for chrome.printerProvider.onPrintRequested event.
186 // |test_param|: The test that should be run.
187 // |expected_result|: The print result the app is expected to report.
188 void RunPrintRequestTestApp(const std::string& test_param,
189 PrintRequestDataType data_type,
190 const std::string& expected_result) {
191 extensions::ResultCatcher catcher;
193 std::string extension_id;
194 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
195 test_param, &extension_id);
196 if (extension_id.empty())
197 return;
199 base::RunLoop run_loop;
200 bool success;
201 std::string print_status;
202 PrinterProviderAPI::PrintCallback callback =
203 base::Bind(&RecordPrintResultAndRunCallback, &success, &print_status,
204 run_loop.QuitClosure());
206 switch (data_type) {
207 case PRINT_REQUEST_DATA_TYPE_NOT_SET:
208 StartPrintRequestWithNoData(extension_id, callback);
209 break;
210 case PRINT_REQUEST_DATA_TYPE_FILE:
211 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback));
212 break;
213 case PRINT_REQUEST_DATA_TYPE_FILE_DELETED:
214 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback));
215 ASSERT_TRUE(data_dir_.Delete());
216 break;
217 case PRINT_REQUEST_DATA_TYPE_BYTES:
218 StartPrintRequestUsingDocumentBytes(extension_id, callback);
219 break;
222 if (data_type != PRINT_REQUEST_DATA_TYPE_NOT_SET)
223 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
225 run_loop.Run();
226 EXPECT_EQ(expected_result, print_status);
227 EXPECT_EQ(expected_result == "OK", success);
230 // Runs a test for chrome.printerProvider.onGetCapabilityRequested
231 // event.
232 // |test_param|: The test that should be run.
233 // |expected_result|: The printer capability the app is expected to report.
234 void RunPrinterCapabilitiesRequestTest(const std::string& test_param,
235 const std::string& expected_result) {
236 extensions::ResultCatcher catcher;
238 std::string extension_id;
239 InitializePrinterProviderTestApp(
240 "api_test/printer_provider/request_capability", test_param,
241 &extension_id);
242 if (extension_id.empty())
243 return;
245 base::RunLoop run_loop;
246 std::string result;
247 StartCapabilityRequest(
248 extension_id,
249 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure()));
251 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
253 run_loop.Run();
254 EXPECT_EQ(expected_result, result);
257 bool SimulateExtensionUnload(const std::string& extension_id) {
258 extensions::ExtensionRegistry* extension_registry =
259 extensions::ExtensionRegistry::Get(browser_context());
261 const extensions::Extension* extension =
262 extension_registry->GetExtensionById(
263 extension_id, extensions::ExtensionRegistry::ENABLED);
264 if (!extension)
265 return false;
267 extension_registry->RemoveEnabled(extension_id);
268 extension_registry->TriggerOnUnloaded(
269 extension, extensions::UnloadedExtensionInfo::REASON_TERMINATE);
270 return true;
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 std::vector<std::string>& expected_printers) {
281 ASSERT_EQ(expected_printers.size(), printers.GetSize());
282 for (size_t i = 0; i < expected_printers.size(); ++i) {
283 JSONStringValueDeserializer deserializer(expected_printers[i]);
284 int error_code;
285 scoped_ptr<base::Value> printer_value(
286 deserializer.Deserialize(&error_code, NULL));
287 ASSERT_TRUE(printer_value) << "Failed to deserialize "
288 << expected_printers[i] << ": "
289 << "error code " << error_code;
290 EXPECT_TRUE(printers.Find(*printer_value) != printers.end())
291 << "Unabe to find " << *printer_value << " in " << printers;
295 private:
296 // Initializes |data_dir_| if needed and creates a file in it containing
297 // provided data.
298 bool CreateTempFileWithContents(const char* data,
299 int size,
300 base::FilePath* path,
301 base::File::Info* file_info) {
302 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir())
303 return false;
305 *path = data_dir_.path().AppendASCII("data.pwg");
306 int written = base::WriteFile(*path, data, size);
307 if (written != size)
308 return false;
309 if (!base::GetFileInfo(*path, file_info))
310 return false;
311 return true;
314 base::ScopedTempDir data_dir_;
316 DISALLOW_COPY_AND_ASSIGN(PrinterProviderApiTest);
319 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobSuccess) {
320 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_BYTES, "OK");
323 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobWithFileSuccess) {
324 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE, "OK");
327 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
328 PrintJobWithFile_FileDeletedBeforeDispatch) {
329 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE_DELETED,
330 "INVALID_DATA");
333 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobAsyncSuccess) {
334 RunPrintRequestTestApp("ASYNC_RESPONSE", PRINT_REQUEST_DATA_TYPE_BYTES, "OK");
337 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobFailed) {
338 RunPrintRequestTestApp("INVALID_TICKET", PRINT_REQUEST_DATA_TYPE_BYTES,
339 "INVALID_TICKET");
342 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoPrintEventListener) {
343 RunPrintRequestTestApp("NO_LISTENER", PRINT_REQUEST_DATA_TYPE_BYTES,
344 "FAILED");
347 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
348 PrintRequestInvalidCallbackParam) {
349 RunPrintRequestTestApp("INVALID_VALUE", PRINT_REQUEST_DATA_TYPE_BYTES,
350 "FAILED");
353 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestDataNotSet) {
354 RunPrintRequestTestApp("IGNORE_CALLBACK", PRINT_REQUEST_DATA_TYPE_NOT_SET,
355 "FAILED");
358 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestAppUnloaded) {
359 extensions::ResultCatcher catcher;
361 std::string extension_id;
362 InitializePrinterProviderTestApp("api_test/printer_provider/request_print",
363 "IGNORE_CALLBACK", &extension_id);
364 ASSERT_FALSE(extension_id.empty());
366 base::RunLoop run_loop;
367 bool success = false;
368 std::string status;
369 StartPrintRequestUsingDocumentBytes(
370 extension_id, base::Bind(&RecordPrintResultAndRunCallback, &success,
371 &status, run_loop.QuitClosure()));
373 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
375 ASSERT_TRUE(SimulateExtensionUnload(extension_id));
377 run_loop.Run();
378 EXPECT_FALSE(success);
379 EXPECT_EQ("FAILED", status);
382 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilitySuccess) {
383 RunPrinterCapabilitiesRequestTest("OK", "{\"capability\":\"value\"}");
386 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilityAsyncSuccess) {
387 RunPrinterCapabilitiesRequestTest("ASYNC_RESPONSE",
388 "{\"capability\":\"value\"}");
391 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, EmptyCapability) {
392 RunPrinterCapabilitiesRequestTest("EMPTY", "{}");
395 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoCapabilityEventListener) {
396 RunPrinterCapabilitiesRequestTest("NO_LISTENER", "{}");
399 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, CapabilityInvalidValue) {
400 RunPrinterCapabilitiesRequestTest("INVALID_VALUE", "{}");
403 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetCapabilityAppUnloaded) {
404 extensions::ResultCatcher catcher;
406 std::string extension_id;
407 InitializePrinterProviderTestApp(
408 "api_test/printer_provider/request_capability", "IGNORE_CALLBACK",
409 &extension_id);
410 ASSERT_FALSE(extension_id.empty());
412 base::RunLoop run_loop;
413 std::string result;
414 StartCapabilityRequest(
415 extension_id,
416 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure()));
418 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
420 ASSERT_TRUE(SimulateExtensionUnload(extension_id));
421 run_loop.Run();
422 EXPECT_EQ("{}", result);
425 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersSuccess) {
426 extensions::ResultCatcher catcher;
428 std::string extension_id;
429 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
430 "OK", &extension_id);
431 ASSERT_FALSE(extension_id.empty());
433 base::RunLoop run_loop;
434 base::ListValue printers;
436 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
437 &printers, run_loop.QuitClosure()));
439 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
441 run_loop.Run();
443 std::vector<std::string> expected_printers;
444 expected_printers.push_back(base::StringPrintf(
446 "\"description\":\"Test printer\","
447 "\"extensionId\":\"%s\","
448 "\"extensionName\": \"Test printer provider\","
449 "\"id\":\"%s:printer1\","
450 "\"name\":\"Printer 1\""
451 "}",
452 extension_id.c_str(), extension_id.c_str()));
453 expected_printers.push_back(base::StringPrintf(
455 "\"extensionId\":\"%s\","
456 "\"extensionName\": \"Test printer provider\","
457 "\"id\":\"%s:printerNoDesc\","
458 "\"name\":\"Printer 2\""
459 "}",
460 extension_id.c_str(), extension_id.c_str()));
462 ValidatePrinterListValue(printers, expected_printers);
465 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersAsyncSuccess) {
466 extensions::ResultCatcher catcher;
468 std::string extension_id;
469 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
470 "ASYNC_RESPONSE", &extension_id);
471 ASSERT_FALSE(extension_id.empty());
473 base::RunLoop run_loop;
474 base::ListValue printers;
476 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
477 &printers, run_loop.QuitClosure()));
479 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
481 run_loop.Run();
483 std::vector<std::string> expected_printers;
484 expected_printers.push_back(base::StringPrintf(
486 "\"description\":\"Test printer\","
487 "\"extensionId\":\"%s\","
488 "\"extensionName\": \"Test printer provider\","
489 "\"id\":\"%s:printer1\","
490 "\"name\":\"Printer 1\""
491 "}",
492 extension_id.c_str(), extension_id.c_str()));
494 ValidatePrinterListValue(printers, expected_printers);
497 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersTwoExtensions) {
498 extensions::ResultCatcher catcher;
500 std::string extension_id_1;
501 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
502 "OK", &extension_id_1);
503 ASSERT_FALSE(extension_id_1.empty());
505 std::string extension_id_2;
506 InitializePrinterProviderTestApp(
507 "api_test/printer_provider/request_printers_second", "OK",
508 &extension_id_2);
509 ASSERT_FALSE(extension_id_2.empty());
511 base::RunLoop run_loop;
512 base::ListValue printers;
514 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
515 &printers, run_loop.QuitClosure()));
517 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
518 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
520 run_loop.Run();
522 std::vector<std::string> expected_printers;
523 expected_printers.push_back(base::StringPrintf(
525 "\"description\":\"Test printer\","
526 "\"extensionId\":\"%s\","
527 "\"extensionName\": \"Test printer provider\","
528 "\"id\":\"%s:printer1\","
529 "\"name\":\"Printer 1\""
530 "}",
531 extension_id_1.c_str(), extension_id_1.c_str()));
532 expected_printers.push_back(base::StringPrintf(
534 "\"extensionId\":\"%s\","
535 "\"extensionName\": \"Test printer provider\","
536 "\"id\":\"%s:printerNoDesc\","
537 "\"name\":\"Printer 2\""
538 "}",
539 extension_id_1.c_str(), extension_id_1.c_str()));
540 expected_printers.push_back(base::StringPrintf(
542 "\"description\":\"Test printer\","
543 "\"extensionId\":\"%s\","
544 "\"extensionName\": \"Test printer provider\","
545 "\"id\":\"%s:printer1\","
546 "\"name\":\"Printer 1\""
547 "}",
548 extension_id_2.c_str(), extension_id_2.c_str()));
549 expected_printers.push_back(base::StringPrintf(
551 "\"extensionId\":\"%s\","
552 "\"extensionName\": \"Test printer provider\","
553 "\"id\":\"%s:printerNoDesc\","
554 "\"name\":\"Printer 2\""
555 "}",
556 extension_id_2.c_str(), extension_id_2.c_str()));
558 ValidatePrinterListValue(printers, expected_printers);
561 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
562 GetPrintersTwoExtensionsBothUnloaded) {
563 extensions::ResultCatcher catcher;
565 std::string extension_id_1;
566 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
567 "IGNORE_CALLBACK", &extension_id_1);
568 ASSERT_FALSE(extension_id_1.empty());
570 std::string extension_id_2;
571 InitializePrinterProviderTestApp(
572 "api_test/printer_provider/request_printers_second", "IGNORE_CALLBACK",
573 &extension_id_2);
574 ASSERT_FALSE(extension_id_2.empty());
576 base::RunLoop run_loop;
577 base::ListValue printers;
579 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
580 &printers, run_loop.QuitClosure()));
582 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
583 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
585 ASSERT_TRUE(SimulateExtensionUnload(extension_id_1));
586 ASSERT_TRUE(SimulateExtensionUnload(extension_id_2));
588 run_loop.Run();
590 EXPECT_TRUE(printers.empty());
593 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
594 GetPrintersTwoExtensionsOneFails) {
595 extensions::ResultCatcher catcher;
597 std::string extension_id_1;
598 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
599 "NOT_ARRAY", &extension_id_1);
600 ASSERT_FALSE(extension_id_1.empty());
602 std::string extension_id_2;
603 InitializePrinterProviderTestApp(
604 "api_test/printer_provider/request_printers_second", "OK",
605 &extension_id_2);
606 ASSERT_FALSE(extension_id_2.empty());
608 base::RunLoop run_loop;
609 base::ListValue printers;
611 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
612 &printers, run_loop.QuitClosure()));
614 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
615 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
617 run_loop.Run();
619 std::vector<std::string> expected_printers;
620 expected_printers.push_back(base::StringPrintf(
622 "\"description\":\"Test printer\","
623 "\"extensionId\":\"%s\","
624 "\"extensionName\": \"Test printer provider\","
625 "\"id\":\"%s:printer1\","
626 "\"name\":\"Printer 1\""
627 "}",
628 extension_id_2.c_str(), extension_id_2.c_str()));
629 expected_printers.push_back(base::StringPrintf(
631 "\"extensionId\":\"%s\","
632 "\"extensionName\": \"Test printer provider\","
633 "\"id\":\"%s:printerNoDesc\","
634 "\"name\":\"Printer 2\""
635 "}",
636 extension_id_2.c_str(), extension_id_2.c_str()));
638 ValidatePrinterListValue(printers, expected_printers);
641 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
642 GetPrintersTwoExtensionsOneWithNoListener) {
643 extensions::ResultCatcher catcher;
645 std::string extension_id_1;
646 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
647 "NO_LISTENER", &extension_id_1);
648 ASSERT_FALSE(extension_id_1.empty());
650 std::string extension_id_2;
651 InitializePrinterProviderTestApp(
652 "api_test/printer_provider/request_printers_second", "OK",
653 &extension_id_2);
654 ASSERT_FALSE(extension_id_2.empty());
656 base::RunLoop run_loop;
657 base::ListValue printers;
659 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
660 &printers, run_loop.QuitClosure()));
662 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
663 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
665 run_loop.Run();
667 std::vector<std::string> expected_printers;
668 expected_printers.push_back(base::StringPrintf(
670 "\"description\":\"Test printer\","
671 "\"extensionId\":\"%s\","
672 "\"extensionName\": \"Test printer provider\","
673 "\"id\":\"%s:printer1\","
674 "\"name\":\"Printer 1\""
675 "}",
676 extension_id_2.c_str(), extension_id_2.c_str()));
677 expected_printers.push_back(base::StringPrintf(
679 "\"extensionId\":\"%s\","
680 "\"extensionName\": \"Test printer provider\","
681 "\"id\":\"%s:printerNoDesc\","
682 "\"name\":\"Printer 2\""
683 "}",
684 extension_id_2.c_str(), extension_id_2.c_str()));
686 ValidatePrinterListValue(printers, expected_printers);
689 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersNoListener) {
690 extensions::ResultCatcher catcher;
692 std::string extension_id;
693 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
694 "NO_LISTENER", &extension_id);
695 ASSERT_FALSE(extension_id.empty());
697 base::RunLoop run_loop;
698 base::ListValue printers;
700 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
701 &printers, run_loop.QuitClosure()));
703 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
705 run_loop.Run();
707 EXPECT_TRUE(printers.empty());
710 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersNotArray) {
711 extensions::ResultCatcher catcher;
713 std::string extension_id;
714 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
715 "NOT_ARRAY", &extension_id);
716 ASSERT_FALSE(extension_id.empty());
718 base::RunLoop run_loop;
719 base::ListValue printers;
721 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
722 &printers, run_loop.QuitClosure()));
724 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
726 run_loop.Run();
728 EXPECT_TRUE(printers.empty());
731 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest,
732 GetPrintersInvalidPrinterValueType) {
733 extensions::ResultCatcher catcher;
735 std::string extension_id;
736 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
737 "INVALID_PRINTER_TYPE", &extension_id);
738 ASSERT_FALSE(extension_id.empty());
740 base::RunLoop run_loop;
741 base::ListValue printers;
743 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
744 &printers, run_loop.QuitClosure()));
746 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
748 run_loop.Run();
750 EXPECT_TRUE(printers.empty());
753 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetPrintersInvalidPrinterValue) {
754 extensions::ResultCatcher catcher;
756 std::string extension_id;
757 InitializePrinterProviderTestApp("api_test/printer_provider/request_printers",
758 "INVALID_PRINTER", &extension_id);
759 ASSERT_FALSE(extension_id.empty());
761 base::RunLoop run_loop;
762 base::ListValue printers;
764 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
765 &printers, run_loop.QuitClosure()));
767 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
769 run_loop.Run();
771 EXPECT_TRUE(printers.empty());
774 } // namespace