Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file.h" | 6 #include "base/files/file.h" |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.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" | |
| 16 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 18 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
| 17 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h " | 19 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h " |
| 18 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 20 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
| 19 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 21 #include "extensions/common/value_builder.h" | 23 #include "extensions/common/value_builder.h" |
| 22 #include "extensions/shell/test/shell_apitest.h" | 24 #include "extensions/shell/test/shell_apitest.h" |
| 23 #include "extensions/test/extension_test_message_listener.h" | 25 #include "extensions/test/extension_test_message_listener.h" |
| 24 #include "extensions/test/result_catcher.h" | 26 #include "extensions/test/result_catcher.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 28 |
| 29 using device::MockUsbDevice; | |
| 30 using device::MockUsbService; | |
| 31 | |
| 27 namespace extensions { | 32 namespace extensions { |
| 28 | 33 |
| 29 namespace { | 34 namespace { |
| 30 | 35 |
| 31 // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls. | 36 // Callback for PrinterProviderAPI::DispatchGetPrintersRequested calls. |
| 32 // It appends items in |printers| to |*printers_out|. If |done| is set, it runs | 37 // It appends items in |printers| to |*printers_out|. If |done| is set, it runs |
| 33 // |callback|. | 38 // |callback|. |
| 34 void AppendPrintersAndRunCallbackIfDone(base::ListValue* printers_out, | 39 void AppendPrintersAndRunCallbackIfDone(base::ListValue* printers_out, |
| 35 const base::Closure& callback, | 40 const base::Closure& callback, |
| 36 const base::ListValue& printers, | 41 const base::ListValue& printers, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 63 // It saves reported |value| as JSON string to |*result| and runs |callback|. | 68 // It saves reported |value| as JSON string to |*result| and runs |callback|. |
| 64 void RecordDictAndRunCallback(std::string* result, | 69 void RecordDictAndRunCallback(std::string* result, |
| 65 const base::Closure& callback, | 70 const base::Closure& callback, |
| 66 const base::DictionaryValue& value) { | 71 const base::DictionaryValue& value) { |
| 67 JSONStringValueSerializer serializer(result); | 72 JSONStringValueSerializer serializer(result); |
| 68 EXPECT_TRUE(serializer.Serialize(value)); | 73 EXPECT_TRUE(serializer.Serialize(value)); |
| 69 if (!callback.is_null()) | 74 if (!callback.is_null()) |
| 70 callback.Run(); | 75 callback.Run(); |
| 71 } | 76 } |
| 72 | 77 |
| 78 // Callback for PrinterProvider::DispatchGrantUsbPrinterAccess calls. | |
| 79 // It expects |value| to equal |expected_value| and runs |callback|. | |
| 80 void ExpectValueAndRunCallback(const base::Value* expected_value, | |
| 81 const base::Closure& callback, | |
| 82 const base::DictionaryValue& value) { | |
| 83 EXPECT_TRUE(value.Equals(expected_value)); | |
| 84 if (!callback.is_null()) { | |
| 85 callback.Run(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 73 // Tests for chrome.printerProvider API. | 89 // Tests for chrome.printerProvider API. |
| 74 class PrinterProviderApiTest : public ShellApiTest { | 90 class PrinterProviderApiTest : public ShellApiTest { |
| 75 public: | 91 public: |
| 76 enum PrintRequestDataType { | 92 enum PrintRequestDataType { |
| 77 PRINT_REQUEST_DATA_TYPE_NOT_SET, | 93 PRINT_REQUEST_DATA_TYPE_NOT_SET, |
| 78 PRINT_REQUEST_DATA_TYPE_FILE, | 94 PRINT_REQUEST_DATA_TYPE_FILE, |
| 79 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, | 95 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, |
| 80 PRINT_REQUEST_DATA_TYPE_BYTES | 96 PRINT_REQUEST_DATA_TYPE_BYTES |
| 81 }; | 97 }; |
| 82 | 98 |
| 83 PrinterProviderApiTest() {} | 99 PrinterProviderApiTest() {} |
| 84 ~PrinterProviderApiTest() override {} | 100 ~PrinterProviderApiTest() override {} |
| 85 | 101 |
| 86 void StartGetPrintersRequest( | 102 void StartGetPrintersRequest( |
| 87 const PrinterProviderAPI::GetPrintersCallback& callback) { | 103 const PrinterProviderAPI::GetPrintersCallback& callback) { |
| 88 PrinterProviderAPIFactory::GetInstance() | 104 PrinterProviderAPIFactory::GetInstance() |
| 89 ->GetForBrowserContext(browser_context()) | 105 ->GetForBrowserContext(browser_context()) |
| 90 ->DispatchGetPrintersRequested(callback); | 106 ->DispatchGetPrintersRequested(callback); |
| 91 } | 107 } |
| 92 | 108 |
| 109 void StartGrantUsbPrinterAccess( | |
| 110 const std::string& extension_id, | |
| 111 int device_id, | |
| 112 const PrinterProviderAPI::UsbAccessGrantedCallback& callback) { | |
| 113 PrinterProviderAPIFactory::GetInstance() | |
| 114 ->GetForBrowserContext(browser_context()) | |
| 115 ->DispatchGrantUsbPrinterAccess(extension_id, device_id, callback); | |
| 116 } | |
| 117 | |
| 93 void StartPrintRequestWithNoData( | 118 void StartPrintRequestWithNoData( |
| 94 const std::string& extension_id, | 119 const std::string& extension_id, |
| 95 const PrinterProviderAPI::PrintCallback& callback) { | 120 const PrinterProviderAPI::PrintCallback& callback) { |
| 96 PrinterProviderPrintJob job; | 121 PrinterProviderPrintJob job; |
| 97 job.printer_id = extension_id + ":printer_id"; | 122 job.printer_id = extension_id + ":printer_id"; |
| 98 job.ticket_json = "{}"; | 123 job.ticket_json = "{}"; |
| 99 job.content_type = "application/pdf"; | 124 job.content_type = "application/pdf"; |
| 100 | 125 |
| 101 PrinterProviderAPIFactory::GetInstance() | 126 PrinterProviderAPIFactory::GetInstance() |
| 102 ->GetForBrowserContext(browser_context()) | 127 ->GetForBrowserContext(browser_context()) |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 // chrome.printerProvider.onGetPritersRequested is the same as the set of | 299 // chrome.printerProvider.onGetPritersRequested is the same as the set of |
| 275 // printers in |expected_printers|. |expected_printers| contains list of | 300 // printers in |expected_printers|. |expected_printers| contains list of |
| 276 // printer objects formatted as a JSON string. It is assumed that the values | 301 // printer objects formatted as a JSON string. It is assumed that the values |
| 277 // in |expoected_printers| are unique. | 302 // in |expoected_printers| are unique. |
| 278 void ValidatePrinterListValue( | 303 void ValidatePrinterListValue( |
| 279 const base::ListValue& printers, | 304 const base::ListValue& printers, |
| 280 const ScopedVector<base::Value>& expected_printers) { | 305 const ScopedVector<base::Value>& expected_printers) { |
| 281 ASSERT_EQ(expected_printers.size(), printers.GetSize()); | 306 ASSERT_EQ(expected_printers.size(), printers.GetSize()); |
| 282 for (const base::Value* printer_value : expected_printers) { | 307 for (const base::Value* printer_value : expected_printers) { |
| 283 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) | 308 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) |
| 284 << "Unabe to find " << *printer_value << " in " << printers; | 309 << "Unable to find " << *printer_value << " in " << printers; |
| 285 } | 310 } |
| 286 } | 311 } |
| 287 | 312 |
| 313 protected: | |
| 314 MockUsbService usb_service_; | |
| 315 | |
| 288 private: | 316 private: |
| 289 // Initializes |data_dir_| if needed and creates a file in it containing | 317 // Initializes |data_dir_| if needed and creates a file in it containing |
| 290 // provided data. | 318 // provided data. |
| 291 bool CreateTempFileWithContents(const char* data, | 319 bool CreateTempFileWithContents(const char* data, |
| 292 int size, | 320 int size, |
| 293 base::FilePath* path, | 321 base::FilePath* path, |
| 294 base::File::Info* file_info) { | 322 base::File::Info* file_info) { |
| 295 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) | 323 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) |
| 296 return false; | 324 return false; |
| 297 | 325 |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 751 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, | 779 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, |
| 752 &printers, run_loop.QuitClosure())); | 780 &printers, run_loop.QuitClosure())); |
| 753 | 781 |
| 754 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 782 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 755 | 783 |
| 756 run_loop.Run(); | 784 run_loop.Run(); |
| 757 | 785 |
| 758 EXPECT_TRUE(printers.empty()); | 786 EXPECT_TRUE(printers.empty()); |
| 759 } | 787 } |
| 760 | 788 |
| 789 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GrantUsbPrinterAccess) { | |
| 790 ResultCatcher catcher; | |
| 791 usb_service_.AddDevice(new MockUsbDevice(0, 0, "Google", "USB Printer", "")); | |
| 792 | |
| 793 std::string extension_id; | |
| 794 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers", | |
| 795 "OK", &extension_id); | |
| 796 ASSERT_FALSE(extension_id.empty()); | |
| 797 | |
| 798 base::ListValue printers; | |
| 799 { | |
| 800 base::RunLoop run_loop; | |
| 801 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, | |
| 802 &printers, run_loop.QuitClosure())); | |
| 803 run_loop.Run(); | |
| 804 } | |
| 805 | |
| 806 ScopedVector<base::Value> expected_printers; | |
| 807 ValidatePrinterListValue(printers, expected_printers); | |
| 808 | |
| 809 scoped_ptr<base::Value> expected_printer_info( | |
| 810 DictionaryBuilder() | |
| 811 .Set("description", "This printer is a USB device.") | |
| 812 .Set("id", "granted-0") | |
| 813 .Set("name", "Test Printer") | |
| 814 .Build()); | |
| 815 { | |
| 816 base::RunLoop run_loop; | |
| 817 StartGrantUsbPrinterAccess( | |
| 818 extension_id, 0, | |
| 819 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(), | |
| 820 run_loop.QuitClosure())); | |
| 821 run_loop.Run(); | |
| 822 } | |
| 823 | |
| 824 printers.Clear(); | |
| 825 { | |
| 826 base::RunLoop run_loop; | |
| 827 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, | |
| 828 &printers, run_loop.QuitClosure())); | |
| 829 run_loop.Run(); | |
| 830 } | |
| 831 | |
| 832 expected_printers.push_back( | |
| 833 DictionaryBuilder() | |
| 834 .Set("description", "This printer is a USB device.") | |
| 835 .Set("extensionId", extension_id) | |
| 836 .Set("extensionName", "Test USB printer provider") | |
| 837 .Set("id", | |
| 838 base::StringPrintf("%s:enumerated-0", extension_id.c_str())) | |
| 839 .Set("name", "Test Printer") | |
| 840 .Build()); | |
| 841 | |
| 842 ValidatePrinterListValue(printers, expected_printers); | |
| 843 | |
| 844 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
| 845 } | |
|
tbarzic
2015/05/27 00:11:03
can you add a test case for when the extension doe
Reilly Grant (use Gerrit)
2015/05/28 21:04:10
Done.
| |
| 846 | |
| 761 } // namespace | 847 } // namespace |
| 762 | 848 |
| 763 } // namespace extensions | 849 } // namespace extensions |
| OLD | NEW |