Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / service / cloud_print / printer_job_handler_unittest.cc
bloba7147efeb736ab131995270796b79e0c0f47f4cc
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.
5 #include "base/files/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/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/common/cloud_print/cloud_print_constants.h"
13 #include "chrome/service/cloud_print/cloud_print_service_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/http/http_status_code.h"
19 #include "net/url_request/test_url_fetcher_factory.h"
20 #include "net/url_request/url_request_status.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "printing/backend/print_backend.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using ::testing::AtLeast;
27 using ::testing::DoAll;
28 using ::testing::Exactly;
29 using ::testing::Invoke;
30 using ::testing::InvokeWithoutArgs;
31 using ::testing::NiceMock;
32 using ::testing::Return;
33 using ::testing::SaveArg;
34 using ::testing::Sequence;
35 using ::testing::SetArgPointee;
36 using ::testing::StrictMock;
37 using ::testing::_;
39 namespace cloud_print {
41 namespace {
43 using base::StringPrintf;
45 const char kExampleCloudPrintServerURL[] = "https://www.google.com/cloudprint/";
47 const char kExamplePrintTicket[] = "{\"MediaType\":\"plain\","
48 "\"Resolution\":\"300x300dpi\",\"PageRegion\":\"Letter\","
49 "\"InputSlot\":\"auto\",\"PageSize\":\"Letter\",\"EconoMode\":\"off\"}";
52 // The fillowing constants will all be constructed with StringPrintf. The
53 // following types of parameters are possible:
54 // job number(int): ID # of job from given job list. All job IDs follow the
55 // format __example_job_idN for some N.
56 // fetch reason(string): Fetch reason used by the code. The job list URL
57 // requested by PrinterJobHandler has an extra parameter that signifies when
58 // the request was triggered.
59 // status string(string): Status of print job, one of IN_PROGRESS, DONE or ERROR
60 // job object list(string/JSON formatted): a comma-separated list of job objects
62 // StringPrintf parameters: job number, job number, job number, job number
63 const char kExampleJobObject[] = "{"
64 " \"tags\": ["
65 " \"^own\""
66 " ],"
67 " \"printerName\": \"Example Printer\","
68 " \"status\": \"QUEUED\","
69 " \"ownerId\": \"sampleuser@gmail.com\","
70 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI%d\","
71 " \"printerid\": \"__example_printer_id\","
72 " \"printerType\": \"GOOGLE\","
73 " \"contentType\": \"text/html\","
74 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI%d\","
75 " \"id\": \"__example_job_id%d\","
76 " \"message\": \"\","
77 " \"title\": \"Example Job %d\","
78 " \"errorCode\": \"\","
79 " \"numberOfPages\": 3"
80 " }";
82 // StringPrintf parameters: job object list
83 const char kExampleJobListResponse[] = "{"
84 " \"success\": true,"
85 " \"jobs\": ["
86 " %s"
87 " ],"
88 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\","
89 " \"request\": {"
90 " \"time\": \"0\","
91 " \"users\": ["
92 " \"sampleuser@gmail.com\""
93 " ],"
94 " \"params\": {"
95 " \"printerid\": ["
96 " \"__example_printer_id\""
97 " ]"
98 " },"
99 " \"user\": \"sampleuser@gmail.com\""
100 " }"
101 "}";
104 // StringPrintf parameters: job number
105 const char kExampleJobID[] = "__example_job_id%d";
107 // StringPrintf parameters: job number
108 const char kExamplePrintTicketURI[] =
109 "https://www.google.com/cloudprint/ticket?exampleURI%d";
111 // StringPrintf parameters: job number
112 const char kExamplePrintDownloadURI[] =
113 "https://www.google.com/cloudprint/download?exampleURI%d";
115 // StringPrintf parameters: job number
116 const char kExampleUpdateDoneURI[] =
117 "https://www.google.com/cloudprint/control?jobid=__example_job_id%d"
118 "&status=DONE&code=0&message=&numpages=0&pagesprinted=0";
120 // StringPrintf parameters: job number
121 const char kExampleUpdateErrorURI[] =
122 "https://www.google.com/cloudprint/control?jobid=__example_job_id%d"
123 "&status=ERROR";
125 // StringPrintf parameters: fetch reason
126 const char kExamplePrinterJobListURI[] =
127 "https://www.google.com/cloudprint/fetch"
128 "?printerid=__example_printer_id&deb=%s";
130 // StringPrintf parameters: status string, job number, status string (repeat)
131 const char kExampleControlResponse[] = "{"
132 " \"success\": true,"
133 " \"message\": \"Print job updated successfully.\","
134 " \"xsrf_token\": \"AIp06DjKgbfGalbqzj23V1bU6i-vtR2B4w:1360023068789\","
135 " \"request\": {"
136 " \"time\": \"0\","
137 " \"users\": ["
138 " \"sampleuser@gmail.com\""
139 " ],"
140 " \"params\": {"
141 " \"xsrf\": ["
142 " \"AIp06DgeGIETs42Cj28QWmxGPWVDiaXwVQ:1360023041852\""
143 " ],"
144 " \"status\": ["
145 " \"%s\""
146 " ],"
147 " \"jobid\": ["
148 " \"__example_job_id%d\""
149 " ]"
150 " },"
151 " \"user\": \"sampleuser@gmail.com\""
152 " },"
153 " \"job\": {"
154 " \"tags\": ["
155 " \"^own\""
156 " ],"
157 " \"printerName\": \"Example Printer\","
158 " \"status\": \"%s\","
159 " \"ownerId\": \"sampleuser@gmail.com\","
160 " \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\","
161 " \"printerid\": \"__example_printer_id\","
162 " \"contentType\": \"text/html\","
163 " \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\","
164 " \"id\": \"__example_job_id1\","
165 " \"message\": \"\","
166 " \"title\": \"Example Job\","
167 " \"errorCode\": \"\","
168 " \"numberOfPages\": 3"
169 " }"
170 "}";
172 const char kExamplePrinterID[] = "__example_printer_id";
174 const char kExamplePrinterCapabilities[] = "";
176 const char kExampleCapsMimeType[] = "";
178 // These can stay empty
179 const char kExampleDefaults[] = "";
181 const char kExampleDefaultMimeType[] = "";
183 // Since we're not connecting to the server, this can be any non-empty string.
184 const char kExampleCloudPrintOAuthToken[] = "__SAMPLE_TOKEN";
187 // Not actually printing, no need for real PDF.
188 const char kExamplePrintData[] = "__EXAMPLE_PRINT_DATA";
190 const char kExampleJobDownloadResponseHeaders[] =
191 "Content-Type: Application/PDF\n";
193 const char kExampleTicketDownloadResponseHeaders[] =
194 "Content-Type: application/json\n";
196 const char kExamplePrinterName[] = "Example Printer";
198 const char kExamplePrinterDescription[] = "Example Description";
200 // These are functions used to construct the various sample strings.
201 std::string JobListResponse(int num_jobs) {
202 std::string job_objects;
203 for (int i = 0; i < num_jobs; i++) {
204 job_objects = job_objects + StringPrintf(kExampleJobObject, i+1, i+1, i+1,
205 i+1);
206 if (i != num_jobs-1) job_objects = job_objects + ",";
208 return StringPrintf(kExampleJobListResponse, job_objects.c_str());
211 GURL JobListURI(const char* reason) {
212 return GURL(StringPrintf(kExamplePrinterJobListURI, reason));
215 GURL DoneURI(int job_num) {
216 return GURL(StringPrintf(kExampleUpdateDoneURI, job_num));
219 GURL ErrorURI(int job_num) {
220 return GURL(StringPrintf(kExampleUpdateErrorURI, job_num));
223 GURL TicketURI(int job_num) {
224 return GURL(StringPrintf(kExamplePrintTicketURI, job_num));
227 GURL DownloadURI(int job_num) {
228 return GURL(StringPrintf(kExamplePrintDownloadURI, job_num));
231 GURL InProgressURI(int job_num) {
232 return GetUrlForJobStatusUpdate(GURL(kExampleCloudPrintServerURL),
233 StringPrintf(kExampleJobID, job_num),
234 PRINT_JOB_STATUS_IN_PROGRESS,
238 std::string StatusResponse(int job_num, const char* status_string) {
239 return StringPrintf(kExampleControlResponse,
240 status_string,
241 job_num,
242 status_string);
245 } // namespace
247 class CloudPrintURLFetcherNoServiceProcess
248 : public CloudPrintURLFetcher {
249 public:
250 CloudPrintURLFetcherNoServiceProcess() :
251 context_getter_(new net::TestURLRequestContextGetter(
252 base::MessageLoopProxy::current())) {}
253 protected:
254 net::URLRequestContextGetter* GetRequestContextGetter() override {
255 return context_getter_.get();
258 ~CloudPrintURLFetcherNoServiceProcess() override {}
260 private:
261 scoped_refptr<net::URLRequestContextGetter> context_getter_;
265 class CloudPrintURLFetcherNoServiceProcessFactory
266 : public CloudPrintURLFetcherFactory {
267 public:
268 CloudPrintURLFetcher* CreateCloudPrintURLFetcher() override {
269 return new CloudPrintURLFetcherNoServiceProcess;
272 ~CloudPrintURLFetcherNoServiceProcessFactory() override {}
276 // This class handles the callback from FakeURLFetcher
277 // It is a separate class because callback methods must be
278 // on RefCounted classes
280 class TestURLFetcherCallback {
281 public:
282 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
283 const GURL& url,
284 net::URLFetcherDelegate* d,
285 const std::string& response_data,
286 net::HttpStatusCode response_code,
287 net::URLRequestStatus::Status status) {
288 scoped_ptr<net::FakeURLFetcher> fetcher(
289 new net::FakeURLFetcher(url, d, response_data, response_code, status));
290 OnRequestCreate(url, fetcher.get());
291 return fetcher.Pass();
293 MOCK_METHOD2(OnRequestCreate,
294 void(const GURL&, net::FakeURLFetcher*));
298 class MockPrinterJobHandlerDelegate
299 : public PrinterJobHandler::Delegate {
300 public:
301 MOCK_METHOD0(OnAuthError, void());
302 MOCK_METHOD1(OnPrinterDeleted, void(const std::string& str));
304 virtual ~MockPrinterJobHandlerDelegate() {}
308 class MockPrintServerWatcher
309 : public PrintSystem::PrintServerWatcher {
310 public:
311 MOCK_METHOD1(StartWatching,
312 bool(PrintSystem::PrintServerWatcher::Delegate* d));
313 MOCK_METHOD0(StopWatching, bool());
315 MockPrintServerWatcher();
316 PrintSystem::PrintServerWatcher::Delegate* delegate() const {
317 return delegate_;
320 friend class scoped_refptr<NiceMock<MockPrintServerWatcher> >;
321 friend class scoped_refptr<StrictMock<MockPrintServerWatcher> >;
322 friend class scoped_refptr<MockPrintServerWatcher>;
324 protected:
325 virtual ~MockPrintServerWatcher() {}
327 private:
328 PrintSystem::PrintServerWatcher::Delegate* delegate_;
331 class MockPrinterWatcher : public PrintSystem::PrinterWatcher {
332 public:
333 MOCK_METHOD1(StartWatching, bool(PrintSystem::PrinterWatcher::Delegate* d));
334 MOCK_METHOD0(StopWatching, bool());
335 MOCK_METHOD1(GetCurrentPrinterInfo,
336 bool(printing::PrinterBasicInfo* printer_info));
338 MockPrinterWatcher();
339 PrintSystem::PrinterWatcher::Delegate* delegate() const { return delegate_; }
341 friend class scoped_refptr<NiceMock<MockPrinterWatcher> >;
342 friend class scoped_refptr<StrictMock<MockPrinterWatcher> >;
343 friend class scoped_refptr<MockPrinterWatcher>;
345 protected:
346 virtual ~MockPrinterWatcher() {}
348 private:
349 PrintSystem::PrinterWatcher::Delegate* delegate_;
353 class MockJobSpooler : public PrintSystem::JobSpooler {
354 public:
355 MOCK_METHOD8(Spool, bool(
356 const std::string& print_ticket,
357 const std::string& print_ticket_mime_type,
358 const base::FilePath& print_data_file_path,
359 const std::string& print_data_mime_type,
360 const std::string& printer_name,
361 const std::string& job_title,
362 const std::vector<std::string>& tags,
363 PrintSystem::JobSpooler::Delegate* delegate));
365 MockJobSpooler();
366 PrintSystem::JobSpooler::Delegate* delegate() const { return delegate_; }
368 friend class scoped_refptr<NiceMock<MockJobSpooler> >;
369 friend class scoped_refptr<StrictMock<MockJobSpooler> >;
370 friend class scoped_refptr<MockJobSpooler>;
372 protected:
373 virtual ~MockJobSpooler() {}
375 private:
376 PrintSystem::JobSpooler::Delegate* delegate_;
381 class MockPrintSystem : public PrintSystem {
382 public:
383 MockPrintSystem();
384 PrintSystem::PrintSystemResult succeed() {
385 return PrintSystem::PrintSystemResult(true, "success");
388 PrintSystem::PrintSystemResult fail() {
389 return PrintSystem::PrintSystemResult(false, "failure");
392 MockJobSpooler& JobSpooler() { return *job_spooler_.get(); }
394 MockPrinterWatcher& PrinterWatcher() { return *printer_watcher_.get(); }
396 MockPrintServerWatcher& PrintServerWatcher() {
397 return *print_server_watcher_.get();
400 MOCK_METHOD0(Init, PrintSystem::PrintSystemResult());
401 MOCK_METHOD1(EnumeratePrinters, PrintSystem::PrintSystemResult(
402 printing::PrinterList* printer_list));
404 MOCK_METHOD2(
405 GetPrinterCapsAndDefaults,
406 void(const std::string& printer_name,
407 const PrintSystem::PrinterCapsAndDefaultsCallback& callback));
409 MOCK_METHOD1(IsValidPrinter, bool(const std::string& printer_name));
411 MOCK_METHOD3(ValidatePrintTicket,
412 bool(const std::string& printer_name,
413 const std::string& print_ticket_data,
414 const std::string& print_ticket_mime_type));
416 MOCK_METHOD3(GetJobDetails, bool(const std::string& printer_name,
417 PlatformJobId job_id,
418 PrintJobDetails* job_details));
420 MOCK_METHOD0(CreatePrintServerWatcher, PrintSystem::PrintServerWatcher*());
421 MOCK_METHOD1(CreatePrinterWatcher,
422 PrintSystem::PrinterWatcher*(const std::string& printer_name));
423 MOCK_METHOD0(CreateJobSpooler, PrintSystem::JobSpooler*());
425 MOCK_METHOD0(UseCddAndCjt, bool());
426 MOCK_METHOD0(GetSupportedMimeTypes, std::string());
428 friend class scoped_refptr<NiceMock<MockPrintSystem> >;
429 friend class scoped_refptr<StrictMock<MockPrintSystem> >;
430 friend class scoped_refptr<MockPrintSystem>;
432 protected:
433 virtual ~MockPrintSystem() {}
435 private:
436 scoped_refptr<MockJobSpooler> job_spooler_;
437 scoped_refptr<MockPrinterWatcher> printer_watcher_;
438 scoped_refptr<MockPrintServerWatcher> print_server_watcher_;
442 class PrinterJobHandlerTest : public ::testing::Test {
443 public:
444 PrinterJobHandlerTest();
445 void SetUp() override;
446 void TearDown() override;
447 void IdleOut();
448 bool GetPrinterInfo(printing::PrinterBasicInfo* info);
449 void SendCapsAndDefaults(
450 const std::string& printer_name,
451 const PrintSystem::PrinterCapsAndDefaultsCallback& callback);
452 void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher);
453 void AddTicketMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher);
454 bool PostSpoolSuccess();
455 void SetUpJobSuccessTest(int job_num);
456 void BeginTest(int timeout_seconds);
457 void MakeJobFetchReturnNoJobs();
459 static void MessageLoopQuitNowHelper(base::MessageLoop* message_loop);
460 static void MessageLoopQuitSoonHelper(base::MessageLoop* message_loop);
462 base::MessageLoopForIO loop_;
463 TestURLFetcherCallback url_callback_;
464 MockPrinterJobHandlerDelegate jobhandler_delegate_;
465 CloudPrintTokenStore token_store_;
466 CloudPrintURLFetcherNoServiceProcessFactory cloud_print_factory_;
467 scoped_refptr<PrinterJobHandler> job_handler_;
468 scoped_refptr<NiceMock<MockPrintSystem> > print_system_;
469 net::FakeURLFetcherFactory factory_;
470 printing::PrinterBasicInfo basic_info_;
471 printing::PrinterCapsAndDefaults caps_and_defaults_;
472 PrinterJobHandler::PrinterInfoFromCloud info_from_cloud_;
476 void PrinterJobHandlerTest::SetUp() {
477 basic_info_.printer_name = kExamplePrinterName;
478 basic_info_.printer_description = kExamplePrinterDescription;
479 basic_info_.is_default = 0;
481 info_from_cloud_.printer_id = kExamplePrinterID;
482 info_from_cloud_.tags_hash = GetHashOfPrinterInfo(basic_info_);
484 info_from_cloud_.caps_hash = base::MD5String(kExamplePrinterCapabilities);
485 info_from_cloud_.current_xmpp_timeout = 300;
486 info_from_cloud_.pending_xmpp_timeout = 0;
488 caps_and_defaults_.printer_capabilities = kExamplePrinterCapabilities;
489 caps_and_defaults_.caps_mime_type = kExampleCapsMimeType;
490 caps_and_defaults_.printer_defaults = kExampleDefaults;
491 caps_and_defaults_.defaults_mime_type = kExampleDefaultMimeType;
493 print_system_ = new NiceMock<MockPrintSystem>();
495 token_store_.SetToken(kExampleCloudPrintOAuthToken);
497 ON_CALL(print_system_->PrinterWatcher(), GetCurrentPrinterInfo(_))
498 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo));
500 ON_CALL(*print_system_.get(), GetPrinterCapsAndDefaults(_, _))
501 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults));
503 CloudPrintURLFetcher::set_factory(&cloud_print_factory_);
506 void PrinterJobHandlerTest::MakeJobFetchReturnNoJobs() {
507 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
508 JobListResponse(0), net::HTTP_OK,
509 net::URLRequestStatus::SUCCESS);
510 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
511 JobListResponse(0), net::HTTP_OK,
512 net::URLRequestStatus::SUCCESS);
513 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
514 JobListResponse(0), net::HTTP_OK,
515 net::URLRequestStatus::SUCCESS);
518 void PrinterJobHandlerTest::MessageLoopQuitNowHelper(
519 base::MessageLoop* message_loop) {
520 message_loop->QuitWhenIdle();
523 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper(
524 base::MessageLoop* message_loop) {
525 message_loop->message_loop_proxy()->PostTask(
526 FROM_HERE, base::Bind(&MessageLoopQuitNowHelper, message_loop));
529 PrinterJobHandlerTest::PrinterJobHandlerTest()
530 : factory_(NULL, base::Bind(&TestURLFetcherCallback::CreateURLFetcher,
531 base::Unretained(&url_callback_))) {
534 bool PrinterJobHandlerTest::PostSpoolSuccess() {
535 base::MessageLoop::current()->PostTask(
536 FROM_HERE,
537 base::Bind(&PrinterJobHandler::OnJobSpoolSucceeded, job_handler_, 0));
539 // Everything that would be posted on the printer thread queue
540 // has been posted, we can tell the main message loop to quit when idle
541 // and not worry about it idling while the print thread does work
542 base::MessageLoop::current()->PostTask(
543 FROM_HERE, base::Bind(&MessageLoopQuitSoonHelper, &loop_));
544 return true;
547 void PrinterJobHandlerTest::AddMimeHeader(const GURL& url,
548 net::FakeURLFetcher* fetcher) {
549 scoped_refptr<net::HttpResponseHeaders> download_headers =
550 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders);
551 fetcher->set_response_headers(download_headers);
554 void PrinterJobHandlerTest::AddTicketMimeHeader(const GURL& url,
555 net::FakeURLFetcher* fetcher) {
556 scoped_refptr<net::HttpResponseHeaders> download_headers =
557 new net::HttpResponseHeaders(kExampleTicketDownloadResponseHeaders);
558 fetcher->set_response_headers(download_headers);
562 void PrinterJobHandlerTest::SetUpJobSuccessTest(int job_num) {
563 factory_.SetFakeResponse(TicketURI(job_num),
564 kExamplePrintTicket, net::HTTP_OK,
565 net::URLRequestStatus::SUCCESS);
566 factory_.SetFakeResponse(DownloadURI(job_num),
567 kExamplePrintData, net::HTTP_OK,
568 net::URLRequestStatus::SUCCESS);
570 factory_.SetFakeResponse(DoneURI(job_num),
571 StatusResponse(job_num, "DONE"),
572 net::HTTP_OK,
573 net::URLRequestStatus::SUCCESS);
574 factory_.SetFakeResponse(InProgressURI(job_num),
575 StatusResponse(job_num, "IN_PROGRESS"),
576 net::HTTP_OK,
577 net::URLRequestStatus::SUCCESS);
579 // The times requirement is relaxed for the ticket URI
580 // in order to accommodate TicketDownloadFailureTest
581 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(job_num), _))
582 .Times(AtLeast(1))
583 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader));
585 EXPECT_CALL(url_callback_, OnRequestCreate(DownloadURI(job_num), _))
586 .Times(Exactly(1))
587 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader));
589 EXPECT_CALL(url_callback_, OnRequestCreate(InProgressURI(job_num), _))
590 .Times(Exactly(1));
592 EXPECT_CALL(url_callback_, OnRequestCreate(DoneURI(job_num), _))
593 .Times(Exactly(1));
595 EXPECT_CALL(print_system_->JobSpooler(),
596 Spool(kExamplePrintTicket, _, _, _, _, _, _, _))
597 .Times(Exactly(1))
598 .WillOnce(InvokeWithoutArgs(this,
599 &PrinterJobHandlerTest::PostSpoolSuccess));
602 void PrinterJobHandlerTest::BeginTest(int timeout_seconds) {
603 job_handler_ = new PrinterJobHandler(basic_info_,
604 info_from_cloud_,
605 GURL(kExampleCloudPrintServerURL),
606 print_system_.get(),
607 &jobhandler_delegate_);
609 job_handler_->Initialize();
611 base::MessageLoop::current()->PostDelayedTask(
612 FROM_HERE,
613 base::Bind(&PrinterJobHandlerTest::MessageLoopQuitSoonHelper,
614 base::MessageLoop::current()),
615 base::TimeDelta::FromSeconds(timeout_seconds));
617 base::MessageLoop::current()->Run();
620 void PrinterJobHandlerTest::SendCapsAndDefaults(
621 const std::string& printer_name,
622 const PrintSystem::PrinterCapsAndDefaultsCallback& callback) {
623 callback.Run(true, printer_name, caps_and_defaults_);
626 bool PrinterJobHandlerTest::GetPrinterInfo(printing::PrinterBasicInfo* info) {
627 *info = basic_info_;
628 return true;
631 void PrinterJobHandlerTest::TearDown() {
632 IdleOut();
633 CloudPrintURLFetcher::set_factory(NULL);
636 void PrinterJobHandlerTest::IdleOut() {
637 base::MessageLoop::current()->RunUntilIdle();
640 MockPrintServerWatcher::MockPrintServerWatcher() : delegate_(NULL) {
641 ON_CALL(*this, StartWatching(_))
642 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true)));
643 ON_CALL(*this, StopWatching()).WillByDefault(Return(true));
647 MockPrinterWatcher::MockPrinterWatcher() : delegate_(NULL) {
648 ON_CALL(*this, StartWatching(_))
649 .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true)));
650 ON_CALL(*this, StopWatching()).WillByDefault(Return(true));
653 MockJobSpooler::MockJobSpooler() : delegate_(NULL) {
654 ON_CALL(*this, Spool(_, _, _, _, _, _, _, _))
655 .WillByDefault(DoAll(SaveArg<7>(&delegate_), Return(true)));
658 MockPrintSystem::MockPrintSystem()
659 : job_spooler_(new NiceMock<MockJobSpooler>()),
660 printer_watcher_(new NiceMock<MockPrinterWatcher>()),
661 print_server_watcher_(new NiceMock<MockPrintServerWatcher>()) {
662 ON_CALL(*this, CreateJobSpooler()).WillByDefault(Return(job_spooler_.get()));
664 ON_CALL(*this, CreatePrinterWatcher(_))
665 .WillByDefault(Return(printer_watcher_.get()));
667 ON_CALL(*this, CreatePrintServerWatcher())
668 .WillByDefault(Return(print_server_watcher_.get()));
670 ON_CALL(*this, IsValidPrinter(_)).
671 WillByDefault(Return(true));
673 ON_CALL(*this, ValidatePrintTicket(_, _, _)).
674 WillByDefault(Return(true));
677 // This test simulates an end-to-end printing of a document
678 // but tests only non-failure cases.
679 // Disabled - http://crbug.com/184245
680 TEST_F(PrinterJobHandlerTest, DISABLED_HappyPathTest) {
681 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
682 JobListResponse(1), net::HTTP_OK,
683 net::URLRequestStatus::SUCCESS);
684 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
685 JobListResponse(0), net::HTTP_OK,
686 net::URLRequestStatus::SUCCESS);
688 EXPECT_CALL(url_callback_,
689 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
690 .Times(Exactly(1));
691 EXPECT_CALL(url_callback_,
692 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
693 .Times(Exactly(1));
695 SetUpJobSuccessTest(1);
696 BeginTest(20);
699 TEST_F(PrinterJobHandlerTest, TicketDownloadFailureTest) {
700 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
701 JobListResponse(2), net::HTTP_OK,
702 net::URLRequestStatus::SUCCESS);
703 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
704 JobListResponse(2), net::HTTP_OK,
705 net::URLRequestStatus::SUCCESS);
706 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
707 JobListResponse(0), net::HTTP_OK,
708 net::URLRequestStatus::SUCCESS);
709 factory_.SetFakeResponse(TicketURI(1), std::string(),
710 net::HTTP_INTERNAL_SERVER_ERROR,
711 net::URLRequestStatus::FAILED);
713 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _))
714 .Times(AtLeast(1))
715 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader));
717 EXPECT_CALL(url_callback_,
718 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
719 .Times(AtLeast(1));
721 EXPECT_CALL(url_callback_,
722 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
723 .Times(AtLeast(1));
725 EXPECT_CALL(url_callback_,
726 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
727 .Times(AtLeast(1));
729 SetUpJobSuccessTest(2);
730 BeginTest(20);
733 // TODO(noamsml): Figure out how to make this test not take 1 second and
734 // re-enable it
735 TEST_F(PrinterJobHandlerTest, DISABLED_ManyFailureTest) {
736 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
737 JobListResponse(1), net::HTTP_OK,
738 net::URLRequestStatus::SUCCESS);
739 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
740 JobListResponse(1), net::HTTP_OK,
741 net::URLRequestStatus::SUCCESS);
742 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
743 JobListResponse(1), net::HTTP_OK,
744 net::URLRequestStatus::SUCCESS);
745 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
746 JobListResponse(0), net::HTTP_OK,
747 net::URLRequestStatus::SUCCESS);
749 EXPECT_CALL(url_callback_,
750 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
751 .Times(AtLeast(1));
753 EXPECT_CALL(url_callback_,
754 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
755 .Times(AtLeast(1));
757 EXPECT_CALL(url_callback_,
758 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
759 .Times(AtLeast(1));
761 EXPECT_CALL(url_callback_,
762 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _))
763 .Times(AtLeast(1));
765 SetUpJobSuccessTest(1);
767 factory_.SetFakeResponse(TicketURI(1),
768 std::string(),
769 net::HTTP_INTERNAL_SERVER_ERROR,
770 net::URLRequestStatus::FAILED);
772 loop_.PostDelayedTask(FROM_HERE,
773 base::Bind(&net::FakeURLFetcherFactory::SetFakeResponse,
774 base::Unretained(&factory_),
775 TicketURI(1),
776 kExamplePrintTicket,
777 net::HTTP_OK,
778 net::URLRequestStatus::SUCCESS),
779 base::TimeDelta::FromSeconds(1));
782 BeginTest(5);
786 // TODO(noamsml): Figure out how to make this test not take ~64-~2048 (depending
787 // constant values) seconds and re-enable it
788 TEST_F(PrinterJobHandlerTest, DISABLED_CompleteFailureTest) {
789 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
790 JobListResponse(1), net::HTTP_OK,
791 net::URLRequestStatus::SUCCESS);
792 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
793 JobListResponse(1), net::HTTP_OK,
794 net::URLRequestStatus::SUCCESS);
795 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
796 JobListResponse(1), net::HTTP_OK,
797 net::URLRequestStatus::SUCCESS);
798 factory_.SetFakeResponse(ErrorURI(1), StatusResponse(1, "ERROR"),
799 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
800 factory_.SetFakeResponse(TicketURI(1), std::string(),
801 net::HTTP_INTERNAL_SERVER_ERROR,
802 net::URLRequestStatus::FAILED);
804 EXPECT_CALL(url_callback_,
805 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
806 .Times(AtLeast(1));
808 EXPECT_CALL(url_callback_,
809 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
810 .Times(AtLeast(1));
812 EXPECT_CALL(url_callback_,
813 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _))
814 .Times(AtLeast(1));
816 EXPECT_CALL(url_callback_, OnRequestCreate(ErrorURI(1), _))
817 .Times(Exactly(1))
818 .WillOnce(InvokeWithoutArgs(
819 this, &PrinterJobHandlerTest::MakeJobFetchReturnNoJobs));
821 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _))
822 .Times(AtLeast(kNumRetriesBeforeAbandonJob));
824 BeginTest(70);
827 } // namespace cloud_print