Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: extensions/browser/api/printer_provider/printer_provider_apitest.cc

Issue 1148383002: Add onGetUsbPrinterInfoRequested event to printerProvider API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch away from NOTREACHED() and document API expectations. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // It saves reported |value| as JSON string to |*result| and runs |callback|. 65 // It saves reported |value| as JSON string to |*result| and runs |callback|.
64 void RecordDictAndRunCallback(std::string* result, 66 void RecordDictAndRunCallback(std::string* result,
65 const base::Closure& callback, 67 const base::Closure& callback,
66 const base::DictionaryValue& value) { 68 const base::DictionaryValue& value) {
67 JSONStringValueSerializer serializer(result); 69 JSONStringValueSerializer serializer(result);
68 EXPECT_TRUE(serializer.Serialize(value)); 70 EXPECT_TRUE(serializer.Serialize(value));
69 if (!callback.is_null()) 71 if (!callback.is_null())
70 callback.Run(); 72 callback.Run();
71 } 73 }
72 74
75 // Callback for PrinterProvider::DispatchGrantUsbPrinterAccess calls.
76 // It expects |value| to equal |expected_value| and runs |callback|.
77 void ExpectValueAndRunCallback(const base::Value* expected_value,
78 const base::Closure& callback,
79 const base::DictionaryValue& value) {
80 EXPECT_TRUE(value.Equals(expected_value));
81 if (!callback.is_null())
82 callback.Run();
83 }
84
73 // Tests for chrome.printerProvider API. 85 // Tests for chrome.printerProvider API.
74 class PrinterProviderApiTest : public ShellApiTest { 86 class PrinterProviderApiTest : public ShellApiTest {
75 public: 87 public:
76 enum PrintRequestDataType { 88 enum PrintRequestDataType {
77 PRINT_REQUEST_DATA_TYPE_NOT_SET, 89 PRINT_REQUEST_DATA_TYPE_NOT_SET,
78 PRINT_REQUEST_DATA_TYPE_FILE, 90 PRINT_REQUEST_DATA_TYPE_FILE,
79 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, 91 PRINT_REQUEST_DATA_TYPE_FILE_DELETED,
80 PRINT_REQUEST_DATA_TYPE_BYTES 92 PRINT_REQUEST_DATA_TYPE_BYTES
81 }; 93 };
82 94
83 PrinterProviderApiTest() {} 95 PrinterProviderApiTest() {}
84 ~PrinterProviderApiTest() override {} 96 ~PrinterProviderApiTest() override {}
85 97
86 void StartGetPrintersRequest( 98 void StartGetPrintersRequest(
87 const PrinterProviderAPI::GetPrintersCallback& callback) { 99 const PrinterProviderAPI::GetPrintersCallback& callback) {
88 PrinterProviderAPIFactory::GetInstance() 100 PrinterProviderAPIFactory::GetInstance()
89 ->GetForBrowserContext(browser_context()) 101 ->GetForBrowserContext(browser_context())
90 ->DispatchGetPrintersRequested(callback); 102 ->DispatchGetPrintersRequested(callback);
91 } 103 }
92 104
105 void StartGetUsbPrinterInfoRequest(
106 const std::string& extension_id,
107 scoped_refptr<device::UsbDevice> device,
108 const PrinterProviderAPI::GetPrinterInfoCallback& callback) {
109 PrinterProviderAPIFactory::GetInstance()
110 ->GetForBrowserContext(browser_context())
111 ->DispatchGetUsbPrinterInfoRequested(extension_id, device, callback);
112 }
113
93 void StartPrintRequestWithNoData( 114 void StartPrintRequestWithNoData(
94 const std::string& extension_id, 115 const std::string& extension_id,
95 const PrinterProviderAPI::PrintCallback& callback) { 116 const PrinterProviderAPI::PrintCallback& callback) {
96 PrinterProviderPrintJob job; 117 PrinterProviderPrintJob job;
97 job.printer_id = extension_id + ":printer_id"; 118 job.printer_id = extension_id + ":printer_id";
98 job.ticket_json = "{}"; 119 job.ticket_json = "{}";
99 job.content_type = "application/pdf"; 120 job.content_type = "application/pdf";
100 121
101 PrinterProviderAPIFactory::GetInstance() 122 PrinterProviderAPIFactory::GetInstance()
102 ->GetForBrowserContext(browser_context()) 123 ->GetForBrowserContext(browser_context())
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 StartCapabilityRequest( 269 StartCapabilityRequest(
249 extension_id, 270 extension_id,
250 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure())); 271 base::Bind(&RecordDictAndRunCallback, &result, run_loop.QuitClosure()));
251 272
252 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); 273 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
253 274
254 run_loop.Run(); 275 run_loop.Run();
255 EXPECT_EQ(expected_result, result); 276 EXPECT_EQ(expected_result, result);
256 } 277 }
257 278
279 // Run a test for the chrome.printerProvider.onGetUsbPrinterInfoRequested
280 // event.
281 // |test_param|: The test that should be run.
282 // |expected_result|: The printer info that the app is expected to report.
283 void RunUsbPrinterInfoRequestTest(const std::string& test_param) {
284 ResultCatcher catcher;
285 scoped_refptr<device::UsbDevice> device =
286 new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
287 usb_service_.AddDevice(device);
288
289 std::string extension_id;
290 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
291 test_param, &extension_id);
292 ASSERT_FALSE(extension_id.empty());
293
294 scoped_ptr<base::Value> expected_printer_info(new base::DictionaryValue());
295 base::RunLoop run_loop;
296 StartGetUsbPrinterInfoRequest(
297 extension_id, device,
298 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
299 run_loop.QuitClosure()));
300 run_loop.Run();
301
302 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
303 }
304
258 bool SimulateExtensionUnload(const std::string& extension_id) { 305 bool SimulateExtensionUnload(const std::string& extension_id) {
259 ExtensionRegistry* extension_registry = 306 ExtensionRegistry* extension_registry =
260 ExtensionRegistry::Get(browser_context()); 307 ExtensionRegistry::Get(browser_context());
261 308
262 const Extension* extension = extension_registry->GetExtensionById( 309 const Extension* extension = extension_registry->GetExtensionById(
263 extension_id, ExtensionRegistry::ENABLED); 310 extension_id, ExtensionRegistry::ENABLED);
264 if (!extension) 311 if (!extension)
265 return false; 312 return false;
266 313
267 extension_registry->RemoveEnabled(extension_id); 314 extension_registry->RemoveEnabled(extension_id);
268 extension_registry->TriggerOnUnloaded( 315 extension_registry->TriggerOnUnloaded(
269 extension, UnloadedExtensionInfo::REASON_TERMINATE); 316 extension, UnloadedExtensionInfo::REASON_TERMINATE);
270 return true; 317 return true;
271 } 318 }
272 319
273 // Validates that set of printers reported by test apps via 320 // Validates that set of printers reported by test apps via
274 // chrome.printerProvider.onGetPritersRequested is the same as the set of 321 // chrome.printerProvider.onGetPritersRequested is the same as the set of
275 // printers in |expected_printers|. |expected_printers| contains list of 322 // printers in |expected_printers|. |expected_printers| contains list of
276 // printer objects formatted as a JSON string. It is assumed that the values 323 // printer objects formatted as a JSON string. It is assumed that the values
277 // in |expoected_printers| are unique. 324 // in |expoected_printers| are unique.
278 void ValidatePrinterListValue( 325 void ValidatePrinterListValue(
279 const base::ListValue& printers, 326 const base::ListValue& printers,
280 const ScopedVector<base::Value>& expected_printers) { 327 const ScopedVector<base::Value>& expected_printers) {
281 ASSERT_EQ(expected_printers.size(), printers.GetSize()); 328 ASSERT_EQ(expected_printers.size(), printers.GetSize());
282 for (const base::Value* printer_value : expected_printers) { 329 for (const base::Value* printer_value : expected_printers) {
283 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) 330 EXPECT_TRUE(printers.Find(*printer_value) != printers.end())
284 << "Unabe to find " << *printer_value << " in " << printers; 331 << "Unable to find " << *printer_value << " in " << printers;
285 } 332 }
286 } 333 }
287 334
335 protected:
336 device::MockUsbService usb_service_;
337
288 private: 338 private:
289 // Initializes |data_dir_| if needed and creates a file in it containing 339 // Initializes |data_dir_| if needed and creates a file in it containing
290 // provided data. 340 // provided data.
291 bool CreateTempFileWithContents(const char* data, 341 bool CreateTempFileWithContents(const char* data,
292 int size, 342 int size,
293 base::FilePath* path, 343 base::FilePath* path,
294 base::File::Info* file_info) { 344 base::File::Info* file_info) {
295 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) 345 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir())
296 return false; 346 return false;
297 347
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone, 801 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
752 &printers, run_loop.QuitClosure())); 802 &printers, run_loop.QuitClosure()));
753 803
754 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); 804 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
755 805
756 run_loop.Run(); 806 run_loop.Run();
757 807
758 EXPECT_TRUE(printers.empty()); 808 EXPECT_TRUE(printers.empty());
759 } 809 }
760 810
811 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfo) {
812 ResultCatcher catcher;
813 scoped_refptr<device::UsbDevice> device =
814 new device::MockUsbDevice(0, 0, "Google", "USB Printer", "");
815 usb_service_.AddDevice(device);
816
817 std::string extension_id;
818 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
819 "OK", &extension_id);
820 ASSERT_FALSE(extension_id.empty());
821
822 scoped_ptr<base::Value> expected_printer_info(
823 DictionaryBuilder()
824 .Set("description", "This printer is a USB device.")
825 .Set("extensionId", extension_id)
826 .Set("extensionName", "Test USB printer provider")
827 .Set("id", base::StringPrintf("%s:usbDevice-%u", extension_id.c_str(),
828 device->unique_id()))
829 .Set("name", "Test Printer")
830 .Build());
831 base::RunLoop run_loop;
832 StartGetUsbPrinterInfoRequest(
833 extension_id, device,
834 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
835 run_loop.QuitClosure()));
836 run_loop.Run();
837
838 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
839 }
840
841 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoEmptyResponse) {
842 RunUsbPrinterInfoRequestTest("EMPTY_RESPONSE");
843 }
844
845 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, GetUsbPrinterInfoNoListener) {
846 RunUsbPrinterInfoRequestTest("NO_LISTENER");
847 }
848
761 } // namespace 849 } // namespace
762 850
763 } // namespace extensions 851 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698