| 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" |
| 21 #include "extensions/browser/api/usb/usb_guid_map.h" |
| 19 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| 21 #include "extensions/common/value_builder.h" | 24 #include "extensions/common/value_builder.h" |
| 22 #include "extensions/shell/test/shell_apitest.h" | 25 #include "extensions/shell/test/shell_apitest.h" |
| 23 #include "extensions/test/extension_test_message_listener.h" | 26 #include "extensions/test/extension_test_message_listener.h" |
| 24 #include "extensions/test/result_catcher.h" | 27 #include "extensions/test/result_catcher.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 29 |
| 27 namespace extensions { | 30 namespace extensions { |
| 28 | 31 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // It saves reported |value| as JSON string to |*result| and runs |callback|. | 66 // It saves reported |value| as JSON string to |*result| and runs |callback|. |
| 64 void RecordDictAndRunCallback(std::string* result, | 67 void RecordDictAndRunCallback(std::string* result, |
| 65 const base::Closure& callback, | 68 const base::Closure& callback, |
| 66 const base::DictionaryValue& value) { | 69 const base::DictionaryValue& value) { |
| 67 JSONStringValueSerializer serializer(result); | 70 JSONStringValueSerializer serializer(result); |
| 68 EXPECT_TRUE(serializer.Serialize(value)); | 71 EXPECT_TRUE(serializer.Serialize(value)); |
| 69 if (!callback.is_null()) | 72 if (!callback.is_null()) |
| 70 callback.Run(); | 73 callback.Run(); |
| 71 } | 74 } |
| 72 | 75 |
| 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(); |
| 84 } |
| 85 |
| 73 // Tests for chrome.printerProvider API. | 86 // Tests for chrome.printerProvider API. |
| 74 class PrinterProviderApiTest : public ShellApiTest { | 87 class PrinterProviderApiTest : public ShellApiTest { |
| 75 public: | 88 public: |
| 76 enum PrintRequestDataType { | 89 enum PrintRequestDataType { |
| 77 PRINT_REQUEST_DATA_TYPE_NOT_SET, | 90 PRINT_REQUEST_DATA_TYPE_NOT_SET, |
| 78 PRINT_REQUEST_DATA_TYPE_FILE, | 91 PRINT_REQUEST_DATA_TYPE_FILE, |
| 79 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, | 92 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, |
| 80 PRINT_REQUEST_DATA_TYPE_BYTES | 93 PRINT_REQUEST_DATA_TYPE_BYTES |
| 81 }; | 94 }; |
| 82 | 95 |
| 83 PrinterProviderApiTest() {} | 96 PrinterProviderApiTest() {} |
| 84 ~PrinterProviderApiTest() override {} | 97 ~PrinterProviderApiTest() override {} |
| 85 | 98 |
| 86 void StartGetPrintersRequest( | 99 void StartGetPrintersRequest( |
| 87 const PrinterProviderAPI::GetPrintersCallback& callback) { | 100 const PrinterProviderAPI::GetPrintersCallback& callback) { |
| 88 PrinterProviderAPIFactory::GetInstance() | 101 PrinterProviderAPIFactory::GetInstance() |
| 89 ->GetForBrowserContext(browser_context()) | 102 ->GetForBrowserContext(browser_context()) |
| 90 ->DispatchGetPrintersRequested(callback); | 103 ->DispatchGetPrintersRequested(callback); |
| 91 } | 104 } |
| 92 | 105 |
| 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); |
| 113 } |
| 114 |
| 93 void StartPrintRequestWithNoData( | 115 void StartPrintRequestWithNoData( |
| 94 const std::string& extension_id, | 116 const std::string& extension_id, |
| 95 const PrinterProviderAPI::PrintCallback& callback) { | 117 const PrinterProviderAPI::PrintCallback& callback) { |
| 96 PrinterProviderPrintJob job; | 118 PrinterProviderPrintJob job; |
| 97 job.printer_id = extension_id + ":printer_id"; | 119 job.printer_id = extension_id + ":printer_id"; |
| 98 job.ticket_json = "{}"; | 120 job.ticket_json = "{}"; |
| 99 job.content_type = "application/pdf"; | 121 job.content_type = "application/pdf"; |
| 100 | 122 |
| 101 PrinterProviderAPIFactory::GetInstance() | 123 PrinterProviderAPIFactory::GetInstance() |
| 102 ->GetForBrowserContext(browser_context()) | 124 ->GetForBrowserContext(browser_context()) |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 StartCapabilityRequest( | 270 StartCapabilityRequest( |
| 249 extension_id, | 271 extension_id, |
| 250 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure())); | 272 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure())); |
| 251 | 273 |
| 252 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 274 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 253 | 275 |
| 254 run_loop.Run(); | 276 run_loop.Run(); |
| 255 EXPECT_EQ(expected_result, result); | 277 EXPECT_EQ(expected_result, result); |
| 256 } | 278 } |
| 257 | 279 |
| 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); |
| 289 |
| 290 std::string extension_id; |
| 291 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers", |
| 292 test_param, &extension_id); |
| 293 ASSERT_FALSE(extension_id.empty()); |
| 294 |
| 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(); |
| 302 |
| 303 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 304 } |
| 305 |
| 258 bool SimulateExtensionUnload(const std::string& extension_id) { | 306 bool SimulateExtensionUnload(const std::string& extension_id) { |
| 259 ExtensionRegistry* extension_registry = | 307 ExtensionRegistry* extension_registry = |
| 260 ExtensionRegistry::Get(browser_context()); | 308 ExtensionRegistry::Get(browser_context()); |
| 261 | 309 |
| 262 const Extension* extension = extension_registry->GetExtensionById( | 310 const Extension* extension = extension_registry->GetExtensionById( |
| 263 extension_id, ExtensionRegistry::ENABLED); | 311 extension_id, ExtensionRegistry::ENABLED); |
| 264 if (!extension) | 312 if (!extension) |
| 265 return false; | 313 return false; |
| 266 | 314 |
| 267 extension_registry->RemoveEnabled(extension_id); | 315 extension_registry->RemoveEnabled(extension_id); |
| 268 extension_registry->TriggerOnUnloaded( | 316 extension_registry->TriggerOnUnloaded( |
| 269 extension, UnloadedExtensionInfo::REASON_TERMINATE); | 317 extension, UnloadedExtensionInfo::REASON_TERMINATE); |
| 270 return true; | 318 return true; |
| 271 } | 319 } |
| 272 | 320 |
| 273 // Validates that set of printers reported by test apps via | 321 // Validates that set of printers reported by test apps via |
| 274 // chrome.printerProvider.onGetPritersRequested is the same as the set of | 322 // chrome.printerProvider.onGetPritersRequested is the same as the set of |
| 275 // printers in |expected_printers|. |expected_printers| contains list of | 323 // printers in |expected_printers|. |expected_printers| contains list of |
| 276 // printer objects formatted as a JSON string. It is assumed that the values | 324 // printer objects formatted as a JSON string. It is assumed that the values |
| 277 // in |expoected_printers| are unique. | 325 // in |expoected_printers| are unique. |
| 278 void ValidatePrinterListValue( | 326 void ValidatePrinterListValue( |
| 279 const base::ListValue& printers, | 327 const base::ListValue& printers, |
| 280 const ScopedVector<base::Value>& expected_printers) { | 328 const ScopedVector<base::Value>& expected_printers) { |
| 281 ASSERT_EQ(expected_printers.size(), printers.GetSize()); | 329 ASSERT_EQ(expected_printers.size(), printers.GetSize()); |
| 282 for (const base::Value* printer_value : expected_printers) { | 330 for (const base::Value* printer_value : expected_printers) { |
| 283 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) | 331 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) |
| 284 << "Unabe to find " << *printer_value << " in " << printers; | 332 << "Unable to find " << *printer_value << " in " << printers; |
| 285 } | 333 } |
| 286 } | 334 } |
| 287 | 335 |
| 336 protected: |
| 337 device::MockUsbService usb_service_; |
| 338 |
| 288 private: | 339 private: |
| 289 // Initializes |data_dir_| if needed and creates a file in it containing | 340 // Initializes |data_dir_| if needed and creates a file in it containing |
| 290 // provided data. | 341 // provided data. |
| 291 bool CreateTempFileWithContents(const char* data, | 342 bool CreateTempFileWithContents(const char* data, |
| 292 int size, | 343 int size, |
| 293 base::FilePath* path, | 344 base::FilePath* path, |
| 294 base::File::Info* file_info) { | 345 base::File::Info* file_info) { |
| 295 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) | 346 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) |
| 296 return false; | 347 return false; |
| 297 | 348 |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, | 802 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, |
| 752 &printers, run_loop.QuitClosure())); | 803 &printers, run_loop.QuitClosure())); |
| 753 | 804 |
| 754 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 805 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 755 | 806 |
| 756 run_loop.Run(); | 807 run_loop.Run(); |
| 757 | 808 |
| 758 EXPECT_TRUE(printers.empty()); | 809 EXPECT_TRUE(printers.empty()); |
| 759 } | 810 } |
| 760 | 811 |
| 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); |
| 817 |
| 818 std::string extension_id; |
| 819 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers", |
| 820 "OK", &extension_id); |
| 821 ASSERT_FALSE(extension_id.empty()); |
| 822 |
| 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(); |
| 840 |
| 841 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 842 } |
| 843 |
| 844 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoEmptyResponse) { |
| 845 RunUsbPrinterInfoRequestTest("EMPTY_RESPONSE"); |
| 846 } |
| 847 |
| 848 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoNoListener) { |
| 849 RunUsbPrinterInfoRequestTest("NO_LISTENER"); |
| 850 } |
| 851 |
| 761 } // namespace | 852 } // namespace |
| 762 | 853 |
| 763 } // namespace extensions | 854 } // namespace extensions |
| OLD | NEW |