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" |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
6 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
7 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
8 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
10 #include "extensions/browser/api/printer_provider/printer_provider_api.h" | 14 #include "extensions/browser/api/printer_provider/printer_provider_api.h" |
11 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" | 15 #include "extensions/browser/api/printer_provider/printer_provider_api_factory.h
" |
12 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" | 16 #include "extensions/browser/api/printer_provider/printer_provider_print_job.h" |
13 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
14 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
15 #include "extensions/shell/test/shell_apitest.h" | 19 #include "extensions/shell/test/shell_apitest.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 const base::DictionaryValue& value) { | 64 const base::DictionaryValue& value) { |
61 JSONStringValueSerializer serializer(result); | 65 JSONStringValueSerializer serializer(result); |
62 EXPECT_TRUE(serializer.Serialize(value)); | 66 EXPECT_TRUE(serializer.Serialize(value)); |
63 if (!callback.is_null()) | 67 if (!callback.is_null()) |
64 callback.Run(); | 68 callback.Run(); |
65 } | 69 } |
66 | 70 |
67 // Tests for chrome.printerProvider API. | 71 // Tests for chrome.printerProvider API. |
68 class PrinterProviderApiTest : public extensions::ShellApiTest { | 72 class PrinterProviderApiTest : public extensions::ShellApiTest { |
69 public: | 73 public: |
| 74 enum PrintRequestDataType { |
| 75 PRINT_REQUEST_DATA_TYPE_NOT_SET, |
| 76 PRINT_REQUEST_DATA_TYPE_FILE, |
| 77 PRINT_REQUEST_DATA_TYPE_FILE_DELETED, |
| 78 PRINT_REQUEST_DATA_TYPE_BYTES |
| 79 }; |
| 80 |
70 PrinterProviderApiTest() {} | 81 PrinterProviderApiTest() {} |
71 ~PrinterProviderApiTest() override {} | 82 ~PrinterProviderApiTest() override {} |
72 | 83 |
73 void StartGetPrintersRequest( | 84 void StartGetPrintersRequest( |
74 const PrinterProviderAPI::GetPrintersCallback& callback) { | 85 const PrinterProviderAPI::GetPrintersCallback& callback) { |
75 PrinterProviderAPIFactory::GetInstance() | 86 PrinterProviderAPIFactory::GetInstance() |
76 ->GetForBrowserContext(browser_context()) | 87 ->GetForBrowserContext(browser_context()) |
77 ->DispatchGetPrintersRequested(callback); | 88 ->DispatchGetPrintersRequested(callback); |
78 } | 89 } |
79 | 90 |
80 void StartPrintRequest(const std::string& extension_id, | 91 void StartPrintRequestWithNoData( |
81 const PrinterProviderAPI::PrintCallback& callback) { | 92 const std::string& extension_id, |
| 93 const PrinterProviderAPI::PrintCallback& callback) { |
82 extensions::PrinterProviderPrintJob job; | 94 extensions::PrinterProviderPrintJob job; |
83 job.printer_id = extension_id + ":printer_id"; | 95 job.printer_id = extension_id + ":printer_id"; |
84 job.ticket_json = "{}"; | 96 job.ticket_json = "{}"; |
85 job.content_type = "content_type"; | 97 job.content_type = "application/pdf"; |
86 const unsigned char kDocumentBytes[] = {'b', 'y', 't', 'e', 's', 0}; | 98 |
| 99 PrinterProviderAPIFactory::GetInstance() |
| 100 ->GetForBrowserContext(browser_context()) |
| 101 ->DispatchPrintRequested(job, callback); |
| 102 } |
| 103 |
| 104 void StartPrintRequestUsingDocumentBytes( |
| 105 const std::string& extension_id, |
| 106 const PrinterProviderAPI::PrintCallback& callback) { |
| 107 extensions::PrinterProviderPrintJob job; |
| 108 job.printer_id = extension_id + ":printer_id"; |
| 109 job.ticket_json = "{}"; |
| 110 job.content_type = "application/pdf"; |
| 111 const unsigned char kDocumentBytes[] = {'b', 'y', 't', 'e', 's'}; |
87 job.document_bytes = | 112 job.document_bytes = |
88 new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes)); | 113 new base::RefCountedBytes(kDocumentBytes, arraysize(kDocumentBytes)); |
89 | 114 |
90 PrinterProviderAPIFactory::GetInstance() | 115 PrinterProviderAPIFactory::GetInstance() |
91 ->GetForBrowserContext(browser_context()) | 116 ->GetForBrowserContext(browser_context()) |
92 ->DispatchPrintRequested(job, callback); | 117 ->DispatchPrintRequested(job, callback); |
93 } | 118 } |
94 | 119 |
| 120 bool StartPrintRequestUsingFileInfo( |
| 121 const std::string& extension_id, |
| 122 const PrinterProviderAPI::PrintCallback& callback) { |
| 123 extensions::PrinterProviderPrintJob job; |
| 124 |
| 125 const char kBytes[] = {'b', 'y', 't', 'e', 's'}; |
| 126 if (!CreateTempFileWithContents(kBytes, static_cast<int>(arraysize(kBytes)), |
| 127 &job.document_path, &job.file_info)) { |
| 128 ADD_FAILURE() << "Failed to create test file."; |
| 129 return false; |
| 130 } |
| 131 |
| 132 job.printer_id = extension_id + ":printer_id"; |
| 133 job.ticket_json = "{}"; |
| 134 job.content_type = "image/pwg-raster"; |
| 135 |
| 136 PrinterProviderAPIFactory::GetInstance() |
| 137 ->GetForBrowserContext(browser_context()) |
| 138 ->DispatchPrintRequested(job, callback); |
| 139 return true; |
| 140 } |
| 141 |
95 void StartCapabilityRequest( | 142 void StartCapabilityRequest( |
96 const std::string& extension_id, | 143 const std::string& extension_id, |
97 const PrinterProviderAPI::GetCapabilityCallback& callback) { | 144 const PrinterProviderAPI::GetCapabilityCallback& callback) { |
98 PrinterProviderAPIFactory::GetInstance() | 145 PrinterProviderAPIFactory::GetInstance() |
99 ->GetForBrowserContext(browser_context()) | 146 ->GetForBrowserContext(browser_context()) |
100 ->DispatchGetCapabilityRequested(extension_id + ":printer_id", | 147 ->DispatchGetCapabilityRequested(extension_id + ":printer_id", |
101 callback); | 148 callback); |
102 } | 149 } |
103 | 150 |
104 // Loads chrome.printerProvider test app and initializes is for test | 151 // Loads chrome.printerProvider test app and initializes is for test |
(...skipping 24 matching lines...) Expand all Loading... |
129 | 176 |
130 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); | 177 ASSERT_TRUE(ready_listener.WaitUntilSatisfied()); |
131 | 178 |
132 *extension_id_out = extension_id; | 179 *extension_id_out = extension_id; |
133 } | 180 } |
134 | 181 |
135 // Runs a test for chrome.printerProvider.onPrintRequested event. | 182 // Runs a test for chrome.printerProvider.onPrintRequested event. |
136 // |test_param|: The test that should be run. | 183 // |test_param|: The test that should be run. |
137 // |expected_result|: The print result the app is expected to report. | 184 // |expected_result|: The print result the app is expected to report. |
138 void RunPrintRequestTestApp(const std::string& test_param, | 185 void RunPrintRequestTestApp(const std::string& test_param, |
| 186 PrintRequestDataType data_type, |
139 const std::string& expected_result) { | 187 const std::string& expected_result) { |
140 extensions::ResultCatcher catcher; | 188 extensions::ResultCatcher catcher; |
141 | 189 |
142 std::string extension_id; | 190 std::string extension_id; |
143 InitializePrinterProviderTestApp("api_test/printer_provider/request_print", | 191 InitializePrinterProviderTestApp("api_test/printer_provider/request_print", |
144 test_param, &extension_id); | 192 test_param, &extension_id); |
145 if (extension_id.empty()) | 193 if (extension_id.empty()) |
146 return; | 194 return; |
147 | 195 |
148 base::RunLoop run_loop; | 196 base::RunLoop run_loop; |
149 bool success; | 197 bool success; |
150 std::string print_status; | 198 std::string print_status; |
151 StartPrintRequest(extension_id, | 199 PrinterProviderAPI::PrintCallback callback = |
152 base::Bind(&RecordPrintResultAndRunCallback, &success, | 200 base::Bind(&RecordPrintResultAndRunCallback, &success, &print_status, |
153 &print_status, run_loop.QuitClosure())); | 201 run_loop.QuitClosure()); |
154 | 202 |
155 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 203 switch (data_type) { |
| 204 case PRINT_REQUEST_DATA_TYPE_NOT_SET: |
| 205 StartPrintRequestWithNoData(extension_id, callback); |
| 206 break; |
| 207 case PRINT_REQUEST_DATA_TYPE_FILE: |
| 208 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback)); |
| 209 break; |
| 210 case PRINT_REQUEST_DATA_TYPE_FILE_DELETED: |
| 211 ASSERT_TRUE(StartPrintRequestUsingFileInfo(extension_id, callback)); |
| 212 ASSERT_TRUE(data_dir_.Delete()); |
| 213 break; |
| 214 case PRINT_REQUEST_DATA_TYPE_BYTES: |
| 215 StartPrintRequestUsingDocumentBytes(extension_id, callback); |
| 216 break; |
| 217 } |
| 218 |
| 219 if (data_type != PRINT_REQUEST_DATA_TYPE_NOT_SET) |
| 220 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
156 | 221 |
157 run_loop.Run(); | 222 run_loop.Run(); |
158 EXPECT_EQ(expected_result, print_status); | 223 EXPECT_EQ(expected_result, print_status); |
159 EXPECT_EQ(expected_result == "OK", success); | 224 EXPECT_EQ(expected_result == "OK", success); |
160 } | 225 } |
161 | 226 |
162 // Runs a test for chrome.printerProvider.onGetCapabilityRequested | 227 // Runs a test for chrome.printerProvider.onGetCapabilityRequested |
163 // event. | 228 // event. |
164 // |test_param|: The test that should be run. | 229 // |test_param|: The test that should be run. |
165 // |expected_result|: The printer capability the app is expected to report. | 230 // |expected_result|: The printer capability the app is expected to report. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 serializer.Deserialize(&error_code, NULL)); | 283 serializer.Deserialize(&error_code, NULL)); |
219 ASSERT_TRUE(printer_value) << "Failed to deserialize " | 284 ASSERT_TRUE(printer_value) << "Failed to deserialize " |
220 << expected_printers[i] << ": " | 285 << expected_printers[i] << ": " |
221 << "error code " << error_code; | 286 << "error code " << error_code; |
222 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) | 287 EXPECT_TRUE(printers.Find(*printer_value) != printers.end()) |
223 << "Unabe to find " << *printer_value << " in " << printers; | 288 << "Unabe to find " << *printer_value << " in " << printers; |
224 } | 289 } |
225 } | 290 } |
226 | 291 |
227 private: | 292 private: |
| 293 // Initializes |data_dir_| if needed and creates a file in it containing |
| 294 // provided data. |
| 295 bool CreateTempFileWithContents(const char* data, |
| 296 int size, |
| 297 base::FilePath* path, |
| 298 base::File::Info* file_info) { |
| 299 if (!data_dir_.IsValid() && !data_dir_.CreateUniqueTempDir()) |
| 300 return false; |
| 301 |
| 302 *path = data_dir_.path().AppendASCII("data.pwg"); |
| 303 int written = base::WriteFile(*path, data, size); |
| 304 if (written != size) |
| 305 return false; |
| 306 if (!base::GetFileInfo(*path, file_info)) |
| 307 return false; |
| 308 return true; |
| 309 } |
| 310 |
| 311 base::ScopedTempDir data_dir_; |
| 312 |
228 DISALLOW_COPY_AND_ASSIGN(PrinterProviderApiTest); | 313 DISALLOW_COPY_AND_ASSIGN(PrinterProviderApiTest); |
229 }; | 314 }; |
230 | 315 |
231 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobSuccess) { | 316 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobSuccess) { |
232 RunPrintRequestTestApp("OK", "OK"); | 317 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_BYTES, "OK"); |
| 318 } |
| 319 |
| 320 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobWithFileSuccess) { |
| 321 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE, "OK"); |
| 322 } |
| 323 |
| 324 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, |
| 325 PrintJobWithFile_FileDeletedBeforeDispatch) { |
| 326 RunPrintRequestTestApp("OK", PRINT_REQUEST_DATA_TYPE_FILE_DELETED, |
| 327 "INVALID_DATA"); |
233 } | 328 } |
234 | 329 |
235 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobAsyncSuccess) { | 330 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobAsyncSuccess) { |
236 RunPrintRequestTestApp("ASYNC_RESPONSE", "OK"); | 331 RunPrintRequestTestApp("ASYNC_RESPONSE", PRINT_REQUEST_DATA_TYPE_BYTES, "OK"); |
237 } | 332 } |
238 | 333 |
239 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobFailed) { | 334 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintJobFailed) { |
240 RunPrintRequestTestApp("INVALID_TICKET", "INVALID_TICKET"); | 335 RunPrintRequestTestApp("INVALID_TICKET", PRINT_REQUEST_DATA_TYPE_BYTES, |
| 336 "INVALID_TICKET"); |
241 } | 337 } |
242 | 338 |
243 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoPrintEventListener) { | 339 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, NoPrintEventListener) { |
244 RunPrintRequestTestApp("NO_LISTENER", "FAILED"); | 340 RunPrintRequestTestApp("NO_LISTENER", PRINT_REQUEST_DATA_TYPE_BYTES, |
| 341 "FAILED"); |
245 } | 342 } |
246 | 343 |
247 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, | 344 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, |
248 PrintRequestInvalidCallbackParam) { | 345 PrintRequestInvalidCallbackParam) { |
249 RunPrintRequestTestApp("INVALID_VALUE", "FAILED"); | 346 RunPrintRequestTestApp("INVALID_VALUE", PRINT_REQUEST_DATA_TYPE_BYTES, |
| 347 "FAILED"); |
| 348 } |
| 349 |
| 350 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestDataNotSet) { |
| 351 RunPrintRequestTestApp("IGNORE_CALLBACK", PRINT_REQUEST_DATA_TYPE_NOT_SET, |
| 352 "FAILED"); |
250 } | 353 } |
251 | 354 |
252 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestAppUnloaded) { | 355 IN_PROC_BROWSER_TEST_F(PrinterProviderApiTest, PrintRequestAppUnloaded) { |
253 extensions::ResultCatcher catcher; | 356 extensions::ResultCatcher catcher; |
254 | 357 |
255 std::string extension_id; | 358 std::string extension_id; |
256 InitializePrinterProviderTestApp("api_test/printer_provider/request_print", | 359 InitializePrinterProviderTestApp("api_test/printer_provider/request_print", |
257 "IGNORE_CALLBACK", &extension_id); | 360 "IGNORE_CALLBACK", &extension_id); |
258 ASSERT_FALSE(extension_id.empty()); | 361 ASSERT_FALSE(extension_id.empty()); |
259 | 362 |
260 base::RunLoop run_loop; | 363 base::RunLoop run_loop; |
261 bool success = false; | 364 bool success = false; |
262 std::string status; | 365 std::string status; |
263 StartPrintRequest(extension_id, | 366 StartPrintRequestUsingDocumentBytes( |
264 base::Bind(&RecordPrintResultAndRunCallback, &success, | 367 extension_id, base::Bind(&RecordPrintResultAndRunCallback, &success, |
265 &status, run_loop.QuitClosure())); | 368 &status, run_loop.QuitClosure())); |
266 | 369 |
267 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 370 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
268 | 371 |
269 ASSERT_TRUE(SimulateExtensionUnload(extension_id)); | 372 ASSERT_TRUE(SimulateExtensionUnload(extension_id)); |
270 | 373 |
271 run_loop.Run(); | 374 run_loop.Run(); |
272 EXPECT_FALSE(success); | 375 EXPECT_FALSE(success); |
273 EXPECT_EQ("FAILED", status); | 376 EXPECT_EQ("FAILED", status); |
274 } | 377 } |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 &printers, run_loop.QuitClosure())); | 751 &printers, run_loop.QuitClosure())); |
649 | 752 |
650 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 753 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
651 | 754 |
652 run_loop.Run(); | 755 run_loop.Run(); |
653 | 756 |
654 EXPECT_TRUE(printers.empty()); | 757 EXPECT_TRUE(printers.empty()); |
655 } | 758 } |
656 | 759 |
657 } // namespace | 760 } // namespace |
OLD | NEW |