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"
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
;
39 namespace cloud_print
{
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
[] = "{"
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\","
77 " \"title\": \"Example Job %d\","
78 " \"errorCode\": \"\","
79 " \"numberOfPages\": 3"
82 // StringPrintf parameters: job object list
83 const char kExampleJobListResponse
[] = "{"
88 " \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\","
92 " \"sampleuser@gmail.com\""
96 " \"__example_printer_id\""
99 " \"user\": \"sampleuser@gmail.com\""
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"
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\","
138 " \"sampleuser@gmail.com\""
142 " \"AIp06DgeGIETs42Cj28QWmxGPWVDiaXwVQ:1360023041852\""
148 " \"__example_job_id%d\""
151 " \"user\": \"sampleuser@gmail.com\""
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"
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,
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
,
247 class CloudPrintURLFetcherNoServiceProcess
248 : public CloudPrintURLFetcher
{
250 CloudPrintURLFetcherNoServiceProcess() :
251 context_getter_(new net::TestURLRequestContextGetter(
252 base::MessageLoopProxy::current())) {}
254 net::URLRequestContextGetter
* GetRequestContextGetter() override
{
255 return context_getter_
.get();
258 ~CloudPrintURLFetcherNoServiceProcess() override
{}
261 scoped_refptr
<net::URLRequestContextGetter
> context_getter_
;
265 class CloudPrintURLFetcherNoServiceProcessFactory
266 : public CloudPrintURLFetcherFactory
{
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
{
282 scoped_ptr
<net::FakeURLFetcher
> CreateURLFetcher(
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
{
301 MOCK_METHOD0(OnAuthError
, void());
302 MOCK_METHOD1(OnPrinterDeleted
, void(const std::string
& str
));
304 virtual ~MockPrinterJobHandlerDelegate() {}
308 class MockPrintServerWatcher
309 : public PrintSystem::PrintServerWatcher
{
311 MOCK_METHOD1(StartWatching
,
312 bool(PrintSystem::PrintServerWatcher::Delegate
* d
));
313 MOCK_METHOD0(StopWatching
, bool());
315 MockPrintServerWatcher();
316 PrintSystem::PrintServerWatcher::Delegate
* delegate() const {
320 friend class scoped_refptr
<NiceMock
<MockPrintServerWatcher
> >;
321 friend class scoped_refptr
<StrictMock
<MockPrintServerWatcher
> >;
322 friend class scoped_refptr
<MockPrintServerWatcher
>;
325 virtual ~MockPrintServerWatcher() {}
328 PrintSystem::PrintServerWatcher::Delegate
* delegate_
;
331 class MockPrinterWatcher
: public PrintSystem::PrinterWatcher
{
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
>;
346 virtual ~MockPrinterWatcher() {}
349 PrintSystem::PrinterWatcher::Delegate
* delegate_
;
353 class MockJobSpooler
: public PrintSystem::JobSpooler
{
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
));
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
>;
373 virtual ~MockJobSpooler() {}
376 PrintSystem::JobSpooler::Delegate
* delegate_
;
381 class MockPrintSystem
: public PrintSystem
{
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
));
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
>;
433 virtual ~MockPrintSystem() {}
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
{
444 PrinterJobHandlerTest();
445 void SetUp() override
;
446 void TearDown() override
;
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(
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_
));
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"),
573 net::URLRequestStatus::SUCCESS
);
574 factory_
.SetFakeResponse(InProgressURI(job_num
),
575 StatusResponse(job_num
, "IN_PROGRESS"),
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
), _
))
583 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader
));
585 EXPECT_CALL(url_callback_
, OnRequestCreate(DownloadURI(job_num
), _
))
587 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader
));
589 EXPECT_CALL(url_callback_
, OnRequestCreate(InProgressURI(job_num
), _
))
592 EXPECT_CALL(url_callback_
, OnRequestCreate(DoneURI(job_num
), _
))
595 EXPECT_CALL(print_system_
->JobSpooler(),
596 Spool(kExamplePrintTicket
, _
, _
, _
, _
, _
, _
, _
))
598 .WillOnce(InvokeWithoutArgs(this,
599 &PrinterJobHandlerTest::PostSpoolSuccess
));
602 void PrinterJobHandlerTest::BeginTest(int timeout_seconds
) {
603 job_handler_
= new PrinterJobHandler(basic_info_
,
605 GURL(kExampleCloudPrintServerURL
),
607 &jobhandler_delegate_
);
609 job_handler_
->Initialize();
611 base::MessageLoop::current()->PostDelayedTask(
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
) {
631 void PrinterJobHandlerTest::TearDown() {
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
), _
))
691 EXPECT_CALL(url_callback_
,
692 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore
), _
))
695 SetUpJobSuccessTest(1);
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), _
))
715 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader
));
717 EXPECT_CALL(url_callback_
,
718 OnRequestCreate(JobListURI(kJobFetchReasonStartup
), _
))
721 EXPECT_CALL(url_callback_
,
722 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore
), _
))
725 EXPECT_CALL(url_callback_
,
726 OnRequestCreate(JobListURI(kJobFetchReasonFailure
), _
))
729 SetUpJobSuccessTest(2);
733 // TODO(noamsml): Figure out how to make this test not take 1 second and
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
), _
))
753 EXPECT_CALL(url_callback_
,
754 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore
), _
))
757 EXPECT_CALL(url_callback_
,
758 OnRequestCreate(JobListURI(kJobFetchReasonFailure
), _
))
761 EXPECT_CALL(url_callback_
,
762 OnRequestCreate(JobListURI(kJobFetchReasonRetry
), _
))
765 SetUpJobSuccessTest(1);
767 factory_
.SetFakeResponse(TicketURI(1),
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_
),
778 net::URLRequestStatus::SUCCESS
),
779 base::TimeDelta::FromSeconds(1));
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
), _
))
808 EXPECT_CALL(url_callback_
,
809 OnRequestCreate(JobListURI(kJobFetchReasonFailure
), _
))
812 EXPECT_CALL(url_callback_
,
813 OnRequestCreate(JobListURI(kJobFetchReasonRetry
), _
))
816 EXPECT_CALL(url_callback_
, OnRequestCreate(ErrorURI(1), _
))
818 .WillOnce(InvokeWithoutArgs(
819 this, &PrinterJobHandlerTest::MakeJobFetchReturnNoJobs
));
821 EXPECT_CALL(url_callback_
, OnRequestCreate(TicketURI(1), _
))
822 .Times(AtLeast(kNumRetriesBeforeAbandonJob
));
827 } // namespace cloud_print