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

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: Created 5 years, 7 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"
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
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
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
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, GetUsbPrintersTwoExtensions) {
790 ResultCatcher catcher;
791 usb_service_.AddDevice(new MockUsbDevice(0, 0, "Google", "USB Printer", ""));
792 usb_service_.AddDevice(new MockUsbDevice(0, 1, "Google", "USB Printer", ""));
793
794 std::string extension_id_1;
795 InitializePrinterProviderTestApp("api_test/printer_provider/usb_printers",
796 "OK", &extension_id_1);
797 ASSERT_FALSE(extension_id_1.empty());
798
799 std::string extension_id_2;
800 InitializePrinterProviderTestApp(
801 "api_test/printer_provider/usb_printers_second", "OK", &extension_id_2);
802 ASSERT_FALSE(extension_id_2.empty());
803
804 base::ListValue printers;
805 {
806 base::RunLoop run_loop;
807 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
808 &printers, run_loop.QuitClosure()));
809 run_loop.Run();
810 }
811
812 ScopedVector<base::Value> expected_printers;
813 expected_printers.push_back(
814 DictionaryBuilder()
815 .Set("extensions", ListBuilder()
816 .Append(DictionaryBuilder()
817 .Set("extensionId", extension_id_2)
818 .Set("extensionName",
819 "Test USB printer provider"))
820 .Append(DictionaryBuilder()
821 .Set("extensionId", extension_id_1)
822 .Set("extensionName",
823 "Test USB printer provider")))
824 .Set("name", "USB Printer")
825 .Set("usbDevice", 0)
826 .Build());
827 expected_printers.push_back(
828 DictionaryBuilder()
829 .Set("extensions",
830 ListBuilder().Append(
831 DictionaryBuilder()
832 .Set("extensionId", extension_id_2)
833 .Set("extensionName", "Test USB printer provider")))
834 .Set("name", "USB Printer")
835 .Set("usbDevice", 1)
836 .Build());
837
838 ValidatePrinterListValue(printers, expected_printers);
839
840 scoped_ptr<base::Value> expected_printer_info(
841 DictionaryBuilder()
842 .Set("description", "This printer is a USB device.")
843 .Set("id", "granted-0")
844 .Set("name", "Test Printer")
845 .Set("usbDevice", 0)
846 .Build());
847 {
848 base::RunLoop run_loop;
849 StartGrantUsbPrinterAccess(
850 extension_id_1, 0,
851 base::Bind(&ExpectValueAndRunCallback, expected_printer_info.get(),
852 run_loop.QuitClosure()));
853 run_loop.Run();
854 }
855
856 printers.Clear();
857 {
858 base::RunLoop run_loop;
859 StartGetPrintersRequest(base::Bind(&AppendPrintersAndRunCallbackIfDone,
860 &printers, run_loop.QuitClosure()));
861 run_loop.Run();
862 }
863
864 expected_printers.push_back(
865 DictionaryBuilder()
866 .Set("description", "This printer is a USB device.")
867 .Set("extensionId", extension_id_1)
868 .Set("extensionName", "Test USB printer provider")
869 .Set("id",
870 base::StringPrintf("%s:enumerated-0", extension_id_1.c_str()))
871 .Set("name", "Test Printer")
872 .Set("usbDevice", 0)
873 .Build());
874
875 ValidatePrinterListValue(printers, expected_printers);
876
877 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
878 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
879 }
880
761 } // namespace 881 } // namespace
762 882
763 } // namespace extensions 883 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698