OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/file_path.h" | |
6 #include "base/md5.h" | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/message_loop_proxy.h" | |
11 #include "base/stringprintf.h" | |
12 #include "chrome/common/cloud_print/cloud_print_constants.h" | |
13 #include "chrome/service/cloud_print/cloud_print_helpers.h" | |
14 #include "chrome/service/cloud_print/cloud_print_token_store.h" | |
15 #include "chrome/service/cloud_print/print_system.h" | |
16 #include "chrome/service/cloud_print/printer_job_handler.h" | |
17 #include "net/http/http_response_headers.h" | |
18 #include "net/url_request/test_url_fetcher_factory.h" | |
19 #include "net/url_request/url_request_test_util.h" | |
20 #include "printing/backend/print_backend.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 using ::testing::AtLeast; | |
25 using ::testing::Exactly; | |
26 using ::testing::Sequence; | |
27 using ::testing::Return; | |
28 using ::testing::SaveArg; | |
29 using ::testing::DoAll; | |
30 using ::testing::_; | |
31 using ::testing::NiceMock; | |
32 using ::testing::StrictMock; | |
33 using ::testing::Invoke; | |
34 using ::testing::SetArgPointee; | |
35 using ::testing::InvokeWithoutArgs; | |
36 | |
37 namespace cloud_print { | |
38 | |
39 const char kExampleJobListResponse[] = "{" | |
40 " \"success\": true," | |
41 " \"jobs\": [" | |
42 " {" | |
43 " \"tags\": [" | |
44 " \"^own\"" | |
45 " ]," | |
46 " \"printerName\": \"Example Printer\"," | |
47 " \"status\": \"QUEUED\"," | |
48 " \"ownerId\": \"sampleuser@gmail.com\"," | |
49 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\"," | |
50 " \"printerid\": \"__example_printer_id\"," | |
51 " \"printerType\": \"GOOGLE\"," | |
52 " \"contentType\": \"text/html\"," | |
53 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\"," | |
54 " \"id\": \"__example_job_id1\"," | |
55 " \"message\": \"\"," | |
56 " \"title\": \"Example Job 1\"," | |
57 " \"errorCode\": \"\"," | |
58 " \"numberOfPages\": 3" | |
59 " }" | |
60 " ]," | |
61 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\"," | |
62 " \"request\": {" | |
63 " \"time\": \"0\"," | |
64 " \"users\": [" | |
65 " \"sampleuser@gmail.com\"" | |
66 " ]," | |
67 " \"params\": {" | |
68 " \"printerid\": [" | |
69 " \"__example_printer_id\"" | |
70 " ]" | |
71 " }," | |
72 " \"user\": \"sampleuser@gmail.com\"" | |
73 " }" | |
74 "}"; | |
75 | |
76 const char kExampleJobListResponseEmpty[] = "{" | |
77 " \"success\": true," | |
78 " \"jobs\": [" | |
79 " ]," | |
80 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\"," | |
81 " \"request\": {" | |
82 " \"time\": \"0\"," | |
83 " \"users\": [" | |
84 " \"sampleuser@gmail.com\"" | |
85 " ]," | |
86 " \"params\": {" | |
87 " \"printerid\": [" | |
88 " \"__example_printer_id\"" | |
89 " ]" | |
90 " }," | |
91 " \"user\": \"sampleuser@gmail.com\"" | |
92 " }" | |
93 "}"; | |
94 | |
95 | |
96 | |
97 const char kExampleJobID[] = "__example_job_id1"; | |
98 | |
99 const char kExampleCloudPrintServerURL[] = "https://www.google.com/cloudprint/"; | |
100 | |
101 const char kExamplePrintTicket[] = "{\"MediaType\":\"plain\"," | |
102 "\"Resolution\":\"300x300dpi\",\"PageRegion\":\"Letter\"," | |
103 "\"InputSlot\":\"auto\",\"PageSize\":\"Letter\",\"EconoMode\":\"off\"}"; | |
104 | |
105 const char kExamplePrintTicketURI[] = | |
106 "https://www.google.com/cloudprint/ticket?exampleURI1"; | |
107 | |
108 const char kExamplePrintDownloadURI[] = | |
109 "https://www.google.com/cloudprint/download?exampleURI1"; | |
110 | |
111 // Use StringPrintf to construct | |
112 const char kExamplePrinterJobListURI[] = | |
113 "https://www.google.com/cloudprint/fetch" | |
114 "?printerid=__example_printer_id&deb=%s"; | |
115 | |
116 // Use StringPrintf to construct | |
117 const char kExamplePrinterJobControlURI[] = | |
118 "https://www.google.com/cloudprint/control" | |
119 "?jobid=__example_printer_id&status=%s"; | |
120 | |
121 | |
122 // Use StringPrintf to construct | |
123 const char kExampleControlResponse[] = "{" | |
124 " \"success\": true," | |
125 " \"message\": \"Print job updated successfully.\"," | |
126 " \"xsrf_token\": \"AIp06DjKgbfGalbqzj23V1bU6i-vtR2B4w:1360023068789\"," | |
127 " \"request\": {" | |
128 " \"time\": \"0\"," | |
129 " \"users\": [" | |
130 " \"sampleuser@gmail.com\"" | |
131 " ]," | |
132 " \"params\": {" | |
133 " \"xsrf\": [" | |
134 " \"AIp06DgeGIETs42Cj28QWmxGPWVDiaXwVQ:1360023041852\"" | |
135 " ]," | |
136 " \"status\": [" | |
137 " \"%s\"" | |
138 " ]," | |
139 " \"jobid\": [" | |
140 " \"__example_job_id1\"" | |
141 " ]" | |
142 " }," | |
143 " \"user\": \"sampleuser@gmail.com\"" | |
144 " }," | |
145 " \"job\": {" | |
146 " \"tags\": [" | |
147 " \"^own\"" | |
148 " ]," | |
149 " \"printerName\": \"Example Printer\"," | |
150 " \"status\": \"%s\"," | |
151 " \"ownerId\": \"sampleuser@gmail.com\"," | |
152 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\"," | |
153 " \"printerid\": \"__example_printer_id\"," | |
154 " \"contentType\": \"text/html\"," | |
155 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\"," | |
156 " \"id\": \"__example_job_id1\"," | |
157 " \"message\": \"\"," | |
158 " \"title\": \"Example Job\"," | |
159 " \"errorCode\": \"\"," | |
160 " \"numberOfPages\": 3" | |
161 " }" | |
162 "}"; | |
163 | |
164 const char kExamplePrinterID[] = "__example_printer_id"; | |
165 | |
166 const char kExamplePrinterCapabilities[] = ""; | |
167 | |
168 const char kExampleCapsMimeType[] = ""; | |
169 | |
170 // These can stay empty | |
171 const char kExampleDefaults[] = ""; | |
172 | |
173 const char kExampleDefaultMimeType[] = ""; | |
174 | |
175 // Since we're not connecting to the server, this can be any non-empty string. | |
176 const char kExampleCloudPrintOAuthToken[] = "__SAMPLE_TOKEN"; | |
177 | |
178 | |
179 // Not actually printing, no need for real PDF. | |
180 const char kExamplePrintData[] = "__EXAMPLE_PRINT_DATA"; | |
181 | |
182 const char kExampleJobDownloadResponseHeaders[] = | |
183 "Content-Type: Application/PDF\n"; | |
184 | |
185 const char kExampleUpdateDoneURL[] = | |
186 "https://www.google.com/cloudprint/control?jobid=__example_job_id1" | |
187 "&status=DONE&code=0&message=&numpages=0&pagesprinted=0"; | |
188 | |
189 const char kExamplePrinterName[] = "Example Printer"; | |
190 | |
191 const char kExamplePrinterDescription[] = "Example Description"; | |
192 | |
193 class CloudPrintURLFetcherNoServiceProcess | |
194 : public CloudPrintURLFetcher { | |
195 protected: | |
196 virtual net::URLRequestContextGetter* GetRequestContextGetter() { | |
197 return new net::TestURLRequestContextGetter( | |
198 base::MessageLoopProxy::current()); | |
199 } | |
200 | |
201 virtual ~CloudPrintURLFetcherNoServiceProcess() {} | |
202 }; | |
203 | |
204 | |
205 class CloudPrintURLFetcherNoServiceProcessFactory | |
206 : public CloudPrintURLFetcherFactory { | |
207 public: | |
208 virtual CloudPrintURLFetcher* CreateCloudPrintURLFetcher() { | |
209 return new CloudPrintURLFetcherNoServiceProcess; | |
210 } | |
211 | |
212 virtual ~CloudPrintURLFetcherNoServiceProcessFactory() {} | |
213 }; | |
214 | |
215 | |
216 // This class handles the callback from FakeURLFetcher | |
217 // It is a separate class because callback methods must be | |
218 // on RefCounted classes | |
219 | |
220 class TestURLFetcherCallback { | |
221 public: | |
222 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | |
223 const GURL& url, | |
224 net::URLFetcherDelegate* d, | |
225 const std::string& response_data, | |
226 bool success) { | |
227 net::FakeURLFetcher* fetcher = new net::FakeURLFetcher(url, d, | |
Vitaly Buka (NO REVIEWS)
2013/02/14 01:25:27
put new() result into smart pointers if you can
Li
Noam Samuel (WRONG ACCOUNT)
2013/02/14 18:30:39
Done.
| |
228 response_data, | |
229 success); | |
230 OnRequestCreate(url, fetcher); | |
231 return scoped_ptr<net::FakeURLFetcher>(fetcher); | |
232 } | |
233 MOCK_METHOD2(OnRequestCreate, | |
234 void(const GURL&, net::FakeURLFetcher*)); | |
235 }; | |
236 | |
237 | |
238 class MockPrinterJobHandlerDelegate | |
239 : public PrinterJobHandler::Delegate { | |
240 public: | |
241 MOCK_METHOD0(OnAuthError, void()); | |
242 MOCK_METHOD1(OnPrinterDeleted, void(const std::string& str)); | |
243 | |
244 virtual ~MockPrinterJobHandlerDelegate() {} | |
245 }; | |
246 | |
247 | |
248 class MockPrintServerWatcher | |
249 : public PrintSystem::PrintServerWatcher { | |
250 public: | |
251 MOCK_METHOD1(StartWatching, | |
252 bool(PrintSystem::PrintServerWatcher::Delegate* d)); | |
253 MOCK_METHOD0(StopWatching, bool()); | |
254 | |
255 MockPrintServerWatcher(); | |
256 PrintSystem::PrintServerWatcher::Delegate* delegate() const { | |
257 return delegate_; | |
258 } | |
259 | |
260 friend class NiceMock<MockPrintServerWatcher>; | |
261 friend class StrictMock<MockPrintServerWatcher>; | |
262 friend class scoped_refptr<NiceMock<MockPrintServerWatcher> >; | |
263 friend class scoped_refptr<StrictMock<MockPrintServerWatcher> >; | |
264 friend class scoped_refptr<MockPrintServerWatcher>; | |
265 private: | |
266 virtual ~MockPrintServerWatcher() {} | |
267 | |
268 PrintSystem::PrintServerWatcher::Delegate* delegate_; | |
269 }; | |
270 | |
271 class MockPrinterWatcher : public PrintSystem::PrinterWatcher { | |
272 public: | |
273 MOCK_METHOD1(StartWatching, bool(PrintSystem::PrinterWatcher::Delegate* d)); | |
274 MOCK_METHOD0(StopWatching, bool()); | |
275 MOCK_METHOD1(GetCurrentPrinterInfo, | |
276 bool(printing::PrinterBasicInfo* printer_info)); | |
277 | |
278 MockPrinterWatcher(); | |
279 PrintSystem::PrinterWatcher::Delegate* delegate() const { return delegate_; } | |
280 | |
281 friend class NiceMock<MockPrinterWatcher>; | |
282 friend class StrictMock<MockPrinterWatcher>; | |
283 friend class scoped_refptr<NiceMock<MockPrinterWatcher> >; | |
284 friend class scoped_refptr<StrictMock<MockPrinterWatcher> >; | |
285 friend class scoped_refptr<MockPrinterWatcher>; | |
286 | |
287 private: | |
288 virtual ~MockPrinterWatcher() {} | |
289 | |
290 PrintSystem::PrinterWatcher::Delegate* delegate_; | |
291 }; | |
292 | |
293 | |
294 class MockJobSpooler : public PrintSystem::JobSpooler { | |
295 public: | |
296 MOCK_METHOD7(Spool, bool( | |
297 const std::string& print_ticket, | |
298 const FilePath& print_data_file_path, | |
299 const std::string& print_data_mime_type, | |
300 const std::string& printer_name, | |
301 const std::string& job_title, | |
302 const std::vector<std::string>& tags, | |
303 PrintSystem::JobSpooler::Delegate* delegate)); | |
304 | |
305 MockJobSpooler(); | |
306 PrintSystem::JobSpooler::Delegate* delegate() const { return delegate_; } | |
307 | |
308 friend class NiceMock<MockJobSpooler>; | |
309 friend class StrictMock<MockJobSpooler>; | |
310 friend class scoped_refptr<NiceMock<MockJobSpooler> >; | |
311 friend class scoped_refptr<StrictMock<MockJobSpooler> >; | |
312 friend class scoped_refptr<MockJobSpooler>; | |
313 | |
314 private: | |
315 virtual ~MockJobSpooler() {} | |
316 | |
317 PrintSystem::JobSpooler::Delegate* delegate_; | |
318 }; | |
319 | |
320 | |
321 | |
322 class MockPrintSystem : public PrintSystem { | |
323 public: | |
324 MockPrintSystem(); | |
325 PrintSystem::PrintSystemResult succeed() { | |
326 return PrintSystem::PrintSystemResult(true, "success"); | |
327 } | |
328 | |
329 PrintSystem::PrintSystemResult fail() { | |
330 return PrintSystem::PrintSystemResult(false, "failure"); | |
331 } | |
332 | |
333 MockJobSpooler& JobSpooler() { | |
334 return *job_spooler_; | |
335 } | |
336 | |
337 MockPrinterWatcher& PrinterWatcher() { | |
338 return *printer_watcher_; | |
339 } | |
340 | |
341 MockPrintServerWatcher& PrintServerWatcher() { | |
342 return *print_server_watcher_; | |
343 } | |
344 | |
345 MOCK_METHOD0(Init, PrintSystem::PrintSystemResult()); | |
346 MOCK_METHOD1(EnumeratePrinters, PrintSystem::PrintSystemResult( | |
347 printing::PrinterList* printer_list)); | |
348 | |
349 MOCK_METHOD2( | |
350 GetPrinterCapsAndDefaults, | |
351 void(const std::string& printer_name, | |
352 const PrintSystem::PrinterCapsAndDefaultsCallback& callback)); | |
353 | |
354 MOCK_METHOD1(IsValidPrinter, bool(const std::string& printer_name)); | |
355 | |
356 MOCK_METHOD2(ValidatePrintTicket, bool(const std::string& printer_name, | |
357 const std::string& print_ticket_data)); | |
358 | |
359 MOCK_METHOD3(GetJobDetails, bool(const std::string& printer_name, | |
360 PlatformJobId job_id, | |
361 PrintJobDetails* job_details)); | |
362 | |
363 MOCK_METHOD0(CreatePrintServerWatcher, PrintSystem::PrintServerWatcher*()); | |
364 MOCK_METHOD1(CreatePrinterWatcher, | |
365 PrintSystem::PrinterWatcher*(const std::string& printer_name)); | |
366 MOCK_METHOD0(CreateJobSpooler, PrintSystem::JobSpooler*()); | |
367 | |
368 MOCK_METHOD0(GetSupportedMimeTypes, std::string()); | |
369 | |
370 friend class NiceMock<MockPrintSystem>; | |
371 friend class StrictMock<MockPrintSystem>; | |
372 friend class scoped_refptr<NiceMock<MockPrintSystem> >; | |
373 friend class scoped_refptr<StrictMock<MockPrintSystem> >; | |
374 friend class scoped_refptr<MockPrintServerWatcher>; | |
375 | |
376 private: | |
377 virtual ~MockPrintSystem() {} | |
378 | |
379 scoped_refptr<MockJobSpooler> job_spooler_; | |
380 scoped_refptr<MockPrinterWatcher> printer_watcher_; | |
381 scoped_refptr<MockPrintServerWatcher> print_server_watcher_; | |
382 }; | |
383 | |
384 | |
385 class PrinterJobHandlerTest : public ::testing::Test { | |
386 public: | |
387 PrinterJobHandlerTest(); | |
388 void SetUp() OVERRIDE; | |
389 void TearDown() OVERRIDE; | |
390 void IdleOut(); | |
391 bool GetPrinterInfo(printing::PrinterBasicInfo* info); | |
392 void SendCapsAndDefaults( | |
393 const std::string& printer_name, | |
394 const PrintSystem::PrinterCapsAndDefaultsCallback& callback); | |
395 void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher); | |
396 bool PostSpoolSuccess(); | |
397 | |
398 static void MessageLoopQuitNowHelper(MessageLoop* message_loop); | |
399 static void MessageLoopQuitSoonHelper(MessageLoop* message_loop); | |
400 | |
401 MessageLoopForIO loop_; | |
402 TestURLFetcherCallback url_callback_; | |
403 MockPrinterJobHandlerDelegate jobhandler_delegate_; | |
404 CloudPrintTokenStore token_store_; | |
405 CloudPrintURLFetcherNoServiceProcessFactory cloud_print_factory_; | |
406 scoped_refptr<PrinterJobHandler> job_handler_; | |
407 scoped_refptr<NiceMock<MockPrintSystem> > print_system_; | |
408 net::FakeURLFetcherFactory factory_; | |
409 printing::PrinterBasicInfo basic_info_; | |
410 printing::PrinterCapsAndDefaults caps_and_defaults_; | |
411 PrinterJobHandler::PrinterInfoFromCloud info_from_cloud_; | |
412 }; | |
413 | |
414 | |
415 void PrinterJobHandlerTest::SetUp() { | |
416 basic_info_.printer_name = kExamplePrinterName; | |
417 basic_info_.printer_description = kExamplePrinterDescription; | |
418 basic_info_.is_default = 0; | |
419 | |
420 info_from_cloud_.printer_id = kExamplePrinterID; | |
421 info_from_cloud_.tags_hash = GetHashOfPrinterInfo(basic_info_); | |
422 | |
423 info_from_cloud_.caps_hash = base::MD5String(kExamplePrinterCapabilities); | |
424 | |
425 caps_and_defaults_.printer_capabilities = kExamplePrinterCapabilities; | |
426 caps_and_defaults_.caps_mime_type = kExampleCapsMimeType; | |
427 caps_and_defaults_.printer_defaults = kExampleDefaults; | |
428 caps_and_defaults_.defaults_mime_type = kExampleDefaultMimeType; | |
429 | |
430 print_system_ = new NiceMock<MockPrintSystem>(); | |
431 | |
432 token_store_.SetToken(kExampleCloudPrintOAuthToken); | |
433 | |
434 ON_CALL(print_system_->PrinterWatcher(), GetCurrentPrinterInfo(_)) | |
435 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo)); | |
436 | |
437 ON_CALL(*print_system_, GetPrinterCapsAndDefaults(_, _)) | |
438 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults)); | |
439 | |
440 CloudPrintURLFetcher::set_factory(&cloud_print_factory_); | |
441 } | |
442 | |
443 void PrinterJobHandlerTest::MessageLoopQuitNowHelper( | |
444 MessageLoop* message_loop) { | |
445 message_loop->QuitWhenIdle(); | |
446 } | |
447 | |
448 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper( | |
449 MessageLoop* message_loop) { | |
450 message_loop->message_loop_proxy()->PostTask( | |
451 FROM_HERE, | |
452 base::Bind(&MessageLoopQuitNowHelper, message_loop)); | |
453 } | |
454 | |
455 PrinterJobHandlerTest::PrinterJobHandlerTest() | |
456 : factory_(NULL, base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | |
457 base::Unretained(&url_callback_))) { | |
458 } | |
Vitaly Buka (NO REVIEWS)
2013/02/14 01:25:27
Missaligned
base::Bind(&TestURLFetcherCallback::Cr
Noam Samuel (WRONG ACCOUNT)
2013/02/14 18:30:39
Done.
| |
459 | |
460 bool PrinterJobHandlerTest::PostSpoolSuccess() { | |
461 MessageLoop::current()->PostTask( | |
462 FROM_HERE, | |
463 base::Bind( | |
464 &PrinterJobHandler::OnJobSpoolSucceeded, | |
465 job_handler_, 0)); | |
466 | |
467 // Everything that would be posted on the printer thread queue | |
468 // has been posted, we can tell the main message loop to quit when idle | |
469 // and not worry about it idling while the print thread does work | |
470 MessageLoop::current()->PostTask(FROM_HERE, | |
471 base::Bind(&MessageLoopQuitSoonHelper, &loop_)); | |
472 return true; | |
473 } | |
474 | |
475 void PrinterJobHandlerTest::AddMimeHeader( | |
476 const GURL& url, | |
Vitaly Buka (NO REVIEWS)
2013/02/14 01:25:27
http://dev.chromium.org/developers/coding-style
It
Noam Samuel (WRONG ACCOUNT)
2013/02/14 18:30:39
Done.
| |
477 net::FakeURLFetcher* fetcher) { | |
478 scoped_refptr<net::HttpResponseHeaders> download_headers = | |
479 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders); | |
480 fetcher->set_response_headers(download_headers); | |
481 } | |
482 | |
483 | |
484 void PrinterJobHandlerTest::SendCapsAndDefaults( | |
485 const std::string& printer_name, | |
486 const PrintSystem::PrinterCapsAndDefaultsCallback& callback) { | |
487 callback.Run(true, printer_name, caps_and_defaults_); | |
488 } | |
489 | |
490 bool PrinterJobHandlerTest::GetPrinterInfo(printing::PrinterBasicInfo* info) { | |
491 *info = basic_info_; | |
492 return true; | |
493 } | |
494 | |
495 void PrinterJobHandlerTest::TearDown() { | |
496 IdleOut(); | |
497 CloudPrintURLFetcher::set_factory(NULL); | |
498 } | |
499 | |
500 void PrinterJobHandlerTest::IdleOut() { | |
501 MessageLoop::current()->RunUntilIdle(); | |
502 } | |
503 | |
504 MockPrintServerWatcher::MockPrintServerWatcher() : delegate_(NULL) { | |
505 ON_CALL(*this, StartWatching(_)) | |
506 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true))); | |
507 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
508 } | |
509 | |
510 | |
511 MockPrinterWatcher::MockPrinterWatcher() : delegate_(NULL) { | |
512 ON_CALL(*this, StartWatching(_)) | |
513 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true))); | |
514 ON_CALL(*this, StopWatching()).WillByDefault(Return(true)); | |
515 } | |
516 | |
517 MockJobSpooler::MockJobSpooler() : delegate_(NULL) { | |
518 ON_CALL(*this, Spool(_, _, _, _, _, _, _)) | |
519 .WillByDefault(DoAll(SaveArg<6>(&delegate_), Return(true))); | |
520 } | |
521 | |
522 | |
523 MockPrintSystem::MockPrintSystem() | |
524 : job_spooler_(new NiceMock<MockJobSpooler>()), | |
525 printer_watcher_(new NiceMock<MockPrinterWatcher>()), | |
526 print_server_watcher_(new NiceMock<MockPrintServerWatcher>()) { | |
527 ON_CALL(*this, CreateJobSpooler()) | |
528 .WillByDefault(Return(job_spooler_)); | |
529 | |
530 ON_CALL(*this, CreatePrinterWatcher(_)) | |
531 .WillByDefault(Return(printer_watcher_)); | |
532 | |
533 ON_CALL(*this, CreatePrintServerWatcher()) | |
534 .WillByDefault(Return(print_server_watcher_)); | |
535 | |
536 ON_CALL(*this, IsValidPrinter(_)). | |
537 WillByDefault(Return(true)); | |
538 | |
539 ON_CALL(*this, ValidatePrintTicket(_, _)). | |
540 WillByDefault(Return(true)); | |
541 }; | |
542 | |
543 // This test simulates an end-to-end printing of a document | |
544 // but tests only non-failure cases. | |
545 TEST_F(PrinterJobHandlerTest, HappyPathTest) { | |
546 GURL InProgressURL = | |
547 GetUrlForJobStatusUpdate(GURL(kExampleCloudPrintServerURL), | |
548 kExampleJobID, | |
549 PRINT_JOB_STATUS_IN_PROGRESS); | |
550 | |
551 factory_.SetFakeResponse(kExamplePrintTicketURI, kExamplePrintTicket, true); | |
552 factory_.SetFakeResponse(kExamplePrintDownloadURI, kExamplePrintData, true); | |
553 factory_.SetFakeResponse( | |
554 StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonStartup), | |
555 kExampleJobListResponse, true); | |
556 | |
557 | |
558 factory_.SetFakeResponse( | |
559 base::StringPrintf(kExamplePrinterJobListURI, kJobFetchReasonQueryMore), | |
560 kExampleJobListResponseEmpty, true); | |
561 | |
562 factory_.SetFakeResponse( | |
563 kExampleUpdateDoneURL, | |
564 base::StringPrintf(kExampleControlResponse, "DONE", "DONE"), true); | |
565 | |
566 factory_.SetFakeResponse( | |
567 InProgressURL.spec(), | |
568 base::StringPrintf(kExampleControlResponse, | |
569 "IN_PROGRESS", "IN_PROGRESS"), | |
570 true); | |
571 | |
572 | |
573 job_handler_ = new PrinterJobHandler(basic_info_, info_from_cloud_, | |
574 GURL(kExampleCloudPrintServerURL), | |
575 print_system_, &jobhandler_delegate_); | |
576 | |
577 EXPECT_CALL(url_callback_, OnRequestCreate( | |
578 GURL(base::StringPrintf(kExamplePrinterJobListURI, "startup")), _)) | |
579 .Times(Exactly(1)); | |
580 | |
581 EXPECT_CALL(url_callback_, OnRequestCreate( | |
582 GURL(base::StringPrintf(kExamplePrinterJobListURI, "querymore")), _)) | |
583 .Times(Exactly(1)); | |
584 | |
585 EXPECT_CALL(url_callback_, OnRequestCreate(GURL(kExamplePrintTicketURI), _)) | |
586 .Times(Exactly(1)); | |
587 | |
588 EXPECT_CALL(url_callback_, OnRequestCreate( | |
589 GURL(kExamplePrintDownloadURI), _)) | |
590 .Times(Exactly(1)) | |
591 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader)); | |
592 | |
593 EXPECT_CALL(url_callback_, OnRequestCreate(InProgressURL, _)) | |
594 .Times(Exactly(1)); | |
595 | |
596 EXPECT_CALL(url_callback_, OnRequestCreate(GURL(kExampleUpdateDoneURL), _)) | |
597 .Times(Exactly(1)); | |
598 | |
599 EXPECT_CALL(print_system_->JobSpooler(), | |
600 Spool(kExamplePrintTicket, _, _, _, _, _, _)) | |
601 .Times(Exactly(1)) | |
602 .WillOnce(InvokeWithoutArgs(this, | |
603 &PrinterJobHandlerTest::PostSpoolSuccess)); | |
604 | |
605 | |
606 job_handler_->Initialize(); | |
607 | |
608 MessageLoop::current()->PostDelayedTask( | |
609 FROM_HERE, | |
610 base::Bind(&PrinterJobHandlerTest::MessageLoopQuitSoonHelper, | |
611 MessageLoop::current()), | |
612 base::TimeDelta::FromSeconds(1)); | |
613 | |
614 MessageLoop::current()->Run(); | |
615 } | |
616 | |
617 } // namespace cloud_print | |
618 | |
OLD | NEW |