summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/printer_job_handler_unittest.cc
blob: 443b13390265eb8b2ecf2016ad20850506193838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_path.h"
#include "base/location.h"
#include "base/md5.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/stringprintf.h"
#include "base/thread_task_runner_handle.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "chrome/service/cloud_print/cloud_print_service_helpers.h"
#include "chrome/service/cloud_print/cloud_print_token_store.h"
#include "chrome/service/cloud_print/print_system.h"
#include "chrome/service/cloud_print/printer_job_handler.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "printing/backend/print_backend.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::Exactly;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::Sequence;
using ::testing::SetArgPointee;
using ::testing::StrictMock;
using ::testing::_;

namespace cloud_print {

namespace {

using base::StringPrintf;

const char kExampleCloudPrintServerURL[] = "https://www.google.com/cloudprint/";

const char kExamplePrintTicket[] = "{\"MediaType\":\"plain\","
    "\"Resolution\":\"300x300dpi\",\"PageRegion\":\"Letter\","
    "\"InputSlot\":\"auto\",\"PageSize\":\"Letter\",\"EconoMode\":\"off\"}";


// The fillowing constants will all be constructed with StringPrintf. The
// following types of parameters are possible:
// job number(int): ID # of job from given job list. All job IDs follow the
// format __example_job_idN for some N.
// fetch reason(string): Fetch reason used by the code. The job list URL
// requested by PrinterJobHandler has an extra parameter that signifies when
// the request was triggered.
// status string(string): Status of print job, one of IN_PROGRESS, DONE or ERROR
// job object list(string/JSON formatted): a comma-separated list of job objects

// StringPrintf parameters: job number, job number, job number, job number
const char kExampleJobObject[] = "{"
"   \"tags\": ["
"    \"^own\""
"   ],"
"   \"printerName\": \"Example Printer\","
"   \"status\": \"QUEUED\","
"   \"ownerId\": \"sampleuser@gmail.com\","
"   \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI%d\","
"   \"printerid\": \"__example_printer_id\","
"   \"printerType\": \"GOOGLE\","
"   \"contentType\": \"text/html\","
"   \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI%d\","
"   \"id\": \"__example_job_id%d\","
"   \"message\": \"\","
"   \"title\": \"Example Job %d\","
"   \"errorCode\": \"\","
"   \"numberOfPages\": 3"
"  }";

// StringPrintf parameters: job object list
const char kExampleJobListResponse[] = "{"
" \"success\": true,"
" \"jobs\": ["
" %s"
" ],"
" \"xsrf_token\": \"AIp06DjUd3AV6BO0aujB9NvM2a9ZbogxOQ:1360021066932\","
" \"request\": {"
"  \"time\": \"0\","
"  \"users\": ["
"   \"sampleuser@gmail.com\""
"  ],"
"  \"params\": {"
"   \"printerid\": ["
"    \"__example_printer_id\""
"   ]"
"  },"
"  \"user\": \"sampleuser@gmail.com\""
" }"
"}";


// StringPrintf parameters: job number
const char kExampleJobID[] = "__example_job_id%d";

// StringPrintf parameters: job number
const char kExamplePrintTicketURI[] =
    "https://www.google.com/cloudprint/ticket?exampleURI%d";

// StringPrintf parameters: job number
const char kExamplePrintDownloadURI[] =
    "https://www.google.com/cloudprint/download?exampleURI%d";

// StringPrintf parameters: job number
const char kExampleUpdateDoneURI[] =
    "https://www.google.com/cloudprint/control?jobid=__example_job_id%d"
    "&status=DONE&code=0&message=&numpages=0&pagesprinted=0";

// StringPrintf parameters: job number
const char kExampleUpdateErrorURI[] =
    "https://www.google.com/cloudprint/control?jobid=__example_job_id%d"
    "&status=ERROR";

// StringPrintf parameters: fetch reason
const char kExamplePrinterJobListURI[] =
    "https://www.google.com/cloudprint/fetch"
    "?printerid=__example_printer_id&deb=%s";

// StringPrintf parameters: status string, job number, status string (repeat)
const char kExampleControlResponse[] = "{"
" \"success\": true,"
" \"message\": \"Print job updated successfully.\","
" \"xsrf_token\": \"AIp06DjKgbfGalbqzj23V1bU6i-vtR2B4w:1360023068789\","
" \"request\": {"
"  \"time\": \"0\","
"  \"users\": ["
"   \"sampleuser@gmail.com\""
"  ],"
"  \"params\": {"
"   \"xsrf\": ["
"    \"AIp06DgeGIETs42Cj28QWmxGPWVDiaXwVQ:1360023041852\""
"   ],"
"   \"status\": ["
"    \"%s\""
"   ],"
"   \"jobid\": ["
"    \"__example_job_id%d\""
"   ]"
"  },"
"  \"user\": \"sampleuser@gmail.com\""
" },"
" \"job\": {"
"  \"tags\": ["
"   \"^own\""
"  ],"
"  \"printerName\": \"Example Printer\","
"  \"status\": \"%s\","
"  \"ownerId\": \"sampleuser@gmail.com\","
"  \"ticketUrl\": \"https://www.google.com/cloudprint/ticket?exampleURI1\","
"  \"printerid\": \"__example_printer_id\","
"  \"contentType\": \"text/html\","
"  \"fileUrl\": \"https://www.google.com/cloudprint/download?exampleURI1\","
"  \"id\": \"__example_job_id1\","
"  \"message\": \"\","
"  \"title\": \"Example Job\","
"  \"errorCode\": \"\","
"  \"numberOfPages\": 3"
" }"
"}";

const char kExamplePrinterID[] = "__example_printer_id";

const char kExamplePrinterCapabilities[] = "";

const char kExampleCapsMimeType[] = "";

// These can stay empty
const char kExampleDefaults[] = "";

const char kExampleDefaultMimeType[] = "";

// Since we're not connecting to the server, this can be any non-empty string.
const char kExampleCloudPrintOAuthToken[] = "__SAMPLE_TOKEN";


// Not actually printing, no need for real PDF.
const char kExamplePrintData[] = "__EXAMPLE_PRINT_DATA";

const char kExampleJobDownloadResponseHeaders[] =
    "Content-Type: Application/PDF\n";

const char kExampleTicketDownloadResponseHeaders[] =
    "Content-Type: application/json\n";

const char kExamplePrinterName[] = "Example Printer";

const char kExamplePrinterDescription[] = "Example Description";

// These are functions used to construct the various sample strings.
std::string JobListResponse(int num_jobs) {
  std::string job_objects;
  for (int i = 0; i < num_jobs; i++) {
    job_objects = job_objects + StringPrintf(kExampleJobObject, i+1, i+1, i+1,
                                             i+1);
    if (i != num_jobs-1) job_objects = job_objects + ",";
  }
  return StringPrintf(kExampleJobListResponse, job_objects.c_str());
}

GURL JobListURI(const char* reason) {
  return GURL(StringPrintf(kExamplePrinterJobListURI, reason));
}

GURL DoneURI(int job_num) {
  return GURL(StringPrintf(kExampleUpdateDoneURI, job_num));
}

GURL ErrorURI(int job_num) {
  return GURL(StringPrintf(kExampleUpdateErrorURI, job_num));
}

GURL TicketURI(int job_num) {
  return GURL(StringPrintf(kExamplePrintTicketURI, job_num));
}

GURL DownloadURI(int job_num) {
  return GURL(StringPrintf(kExamplePrintDownloadURI, job_num));
}

GURL InProgressURI(int job_num) {
  return GetUrlForJobStatusUpdate(GURL(kExampleCloudPrintServerURL),
                                  StringPrintf(kExampleJobID, job_num),
                                  PRINT_JOB_STATUS_IN_PROGRESS,
                                  0);
}

std::string StatusResponse(int job_num, const char* status_string) {
  return StringPrintf(kExampleControlResponse,
                      status_string,
                      job_num,
                      status_string);
}

}  // namespace

class CloudPrintURLFetcherNoServiceProcess
    : public CloudPrintURLFetcher {
 public:
  CloudPrintURLFetcherNoServiceProcess()
      : context_getter_(new net::TestURLRequestContextGetter(
            base::ThreadTaskRunnerHandle::Get())) {}

 protected:
  net::URLRequestContextGetter* GetRequestContextGetter() override {
    return context_getter_.get();
  }

  ~CloudPrintURLFetcherNoServiceProcess() override {}

 private:
  scoped_refptr<net::URLRequestContextGetter> context_getter_;
};


class CloudPrintURLFetcherNoServiceProcessFactory
    : public CloudPrintURLFetcherFactory {
 public:
  CloudPrintURLFetcher* CreateCloudPrintURLFetcher() override {
    return new CloudPrintURLFetcherNoServiceProcess;
  }

  ~CloudPrintURLFetcherNoServiceProcessFactory() override {}
};


// This class handles the callback from FakeURLFetcher
// It is a separate class because callback methods must be
// on RefCounted classes

class TestURLFetcherCallback {
 public:
  scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
      const GURL& url,
      net::URLFetcherDelegate* d,
      const std::string& response_data,
      net::HttpStatusCode response_code,
      net::URLRequestStatus::Status status) {
    scoped_ptr<net::FakeURLFetcher> fetcher(
        new net::FakeURLFetcher(url, d, response_data, response_code, status));
    OnRequestCreate(url, fetcher.get());
    return fetcher;
  }
  MOCK_METHOD2(OnRequestCreate,
               void(const GURL&, net::FakeURLFetcher*));
};


class MockPrinterJobHandlerDelegate
    : public PrinterJobHandler::Delegate {
 public:
  MOCK_METHOD0(OnAuthError, void());
  MOCK_METHOD1(OnPrinterDeleted, void(const std::string& str));

  virtual ~MockPrinterJobHandlerDelegate() {}
};


class MockPrintServerWatcher
    : public PrintSystem::PrintServerWatcher {
 public:
  MOCK_METHOD1(StartWatching,
               bool(PrintSystem::PrintServerWatcher::Delegate* d));
  MOCK_METHOD0(StopWatching, bool());

  MockPrintServerWatcher();
  PrintSystem::PrintServerWatcher::Delegate* delegate() const {
    return delegate_;
  }

  friend class scoped_refptr<NiceMock<MockPrintServerWatcher> >;
  friend class scoped_refptr<StrictMock<MockPrintServerWatcher> >;
  friend class scoped_refptr<MockPrintServerWatcher>;

 protected:
  virtual ~MockPrintServerWatcher() {}

 private:
  PrintSystem::PrintServerWatcher::Delegate* delegate_;
};

class MockPrinterWatcher : public PrintSystem::PrinterWatcher {
 public:
  MOCK_METHOD1(StartWatching, bool(PrintSystem::PrinterWatcher::Delegate* d));
  MOCK_METHOD0(StopWatching, bool());
  MOCK_METHOD1(GetCurrentPrinterInfo,
               bool(printing::PrinterBasicInfo* printer_info));

  MockPrinterWatcher();
  PrintSystem::PrinterWatcher::Delegate* delegate() const { return delegate_; }

  friend class scoped_refptr<NiceMock<MockPrinterWatcher> >;
  friend class scoped_refptr<StrictMock<MockPrinterWatcher> >;
  friend class scoped_refptr<MockPrinterWatcher>;

 protected:
  virtual ~MockPrinterWatcher() {}

 private:
  PrintSystem::PrinterWatcher::Delegate* delegate_;
};


class MockJobSpooler : public PrintSystem::JobSpooler {
 public:
  MOCK_METHOD8(Spool, bool(
      const std::string& print_ticket,
      const std::string& print_ticket_mime_type,
      const base::FilePath& print_data_file_path,
      const std::string& print_data_mime_type,
      const std::string& printer_name,
      const std::string& job_title,
      const std::vector<std::string>& tags,
      PrintSystem::JobSpooler::Delegate* delegate));

  MockJobSpooler();
  PrintSystem::JobSpooler::Delegate* delegate() const  { return delegate_; }

  friend class scoped_refptr<NiceMock<MockJobSpooler> >;
  friend class scoped_refptr<StrictMock<MockJobSpooler> >;
  friend class scoped_refptr<MockJobSpooler>;

 protected:
  virtual ~MockJobSpooler() {}

 private:
  PrintSystem::JobSpooler::Delegate* delegate_;
};



class MockPrintSystem : public PrintSystem {
 public:
  MockPrintSystem();
  PrintSystem::PrintSystemResult succeed() {
    return PrintSystem::PrintSystemResult(true, "success");
  }

  PrintSystem::PrintSystemResult fail() {
    return PrintSystem::PrintSystemResult(false, "failure");
  }

  MockJobSpooler& JobSpooler() { return *job_spooler_.get(); }

  MockPrinterWatcher& PrinterWatcher() { return *printer_watcher_.get(); }

  MockPrintServerWatcher& PrintServerWatcher() {
    return *print_server_watcher_.get();
  }

  MOCK_METHOD0(Init, PrintSystem::PrintSystemResult());
  MOCK_METHOD1(EnumeratePrinters, PrintSystem::PrintSystemResult(
      printing::PrinterList* printer_list));

  MOCK_METHOD2(
      GetPrinterCapsAndDefaults,
      void(const std::string& printer_name,
           const PrintSystem::PrinterCapsAndDefaultsCallback& callback));

  MOCK_METHOD1(IsValidPrinter, bool(const std::string& printer_name));

  MOCK_METHOD3(ValidatePrintTicket,
               bool(const std::string& printer_name,
                    const std::string& print_ticket_data,
                    const std::string& print_ticket_mime_type));

  MOCK_METHOD3(GetJobDetails, bool(const std::string& printer_name,
                                    PlatformJobId job_id,
                                    PrintJobDetails* job_details));

  MOCK_METHOD0(CreatePrintServerWatcher, PrintSystem::PrintServerWatcher*());
  MOCK_METHOD1(CreatePrinterWatcher,
               PrintSystem::PrinterWatcher*(const std::string& printer_name));
  MOCK_METHOD0(CreateJobSpooler, PrintSystem::JobSpooler*());

  MOCK_METHOD0(UseCddAndCjt, bool());
  MOCK_METHOD0(GetSupportedMimeTypes, std::string());

  friend class scoped_refptr<NiceMock<MockPrintSystem> >;
  friend class scoped_refptr<StrictMock<MockPrintSystem> >;
  friend class scoped_refptr<MockPrintSystem>;

 protected:
  virtual ~MockPrintSystem() {}

 private:
  scoped_refptr<MockJobSpooler> job_spooler_;
  scoped_refptr<MockPrinterWatcher> printer_watcher_;
  scoped_refptr<MockPrintServerWatcher> print_server_watcher_;
};


class PrinterJobHandlerTest : public ::testing::Test {
 public:
  PrinterJobHandlerTest();
  void SetUp() override;
  void TearDown() override;
  void IdleOut();
  bool GetPrinterInfo(printing::PrinterBasicInfo* info);
  void SendCapsAndDefaults(
      const std::string& printer_name,
      const PrintSystem::PrinterCapsAndDefaultsCallback& callback);
  void AddMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher);
  void AddTicketMimeHeader(const GURL& url, net::FakeURLFetcher* fetcher);
  bool PostSpoolSuccess();
  void SetUpJobSuccessTest(int job_num);
  void BeginTest(int timeout_seconds);
  void MakeJobFetchReturnNoJobs();

  static void MessageLoopQuitNowHelper(base::MessageLoop* message_loop);
  static void MessageLoopQuitSoonHelper(base::MessageLoop* message_loop);

  base::MessageLoopForIO loop_;
  TestURLFetcherCallback url_callback_;
  MockPrinterJobHandlerDelegate jobhandler_delegate_;
  CloudPrintTokenStore token_store_;
  CloudPrintURLFetcherNoServiceProcessFactory cloud_print_factory_;
  scoped_refptr<PrinterJobHandler> job_handler_;
  scoped_refptr<NiceMock<MockPrintSystem> > print_system_;
  net::FakeURLFetcherFactory factory_;
  printing::PrinterBasicInfo basic_info_;
  printing::PrinterCapsAndDefaults caps_and_defaults_;
  PrinterJobHandler::PrinterInfoFromCloud info_from_cloud_;
};


void PrinterJobHandlerTest::SetUp() {
  basic_info_.printer_name = kExamplePrinterName;
  basic_info_.printer_description = kExamplePrinterDescription;
  basic_info_.is_default = 0;

  info_from_cloud_.printer_id = kExamplePrinterID;
  info_from_cloud_.tags_hash = GetHashOfPrinterInfo(basic_info_);

  info_from_cloud_.caps_hash = base::MD5String(kExamplePrinterCapabilities);
  info_from_cloud_.current_xmpp_timeout = 300;
  info_from_cloud_.pending_xmpp_timeout = 0;

  caps_and_defaults_.printer_capabilities = kExamplePrinterCapabilities;
  caps_and_defaults_.caps_mime_type = kExampleCapsMimeType;
  caps_and_defaults_.printer_defaults = kExampleDefaults;
  caps_and_defaults_.defaults_mime_type = kExampleDefaultMimeType;

  print_system_ = new NiceMock<MockPrintSystem>();

  token_store_.SetToken(kExampleCloudPrintOAuthToken);

  ON_CALL(print_system_->PrinterWatcher(), GetCurrentPrinterInfo(_))
      .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo));

  ON_CALL(*print_system_.get(), GetPrinterCapsAndDefaults(_, _))
      .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults));

  CloudPrintURLFetcher::set_factory(&cloud_print_factory_);
}

void PrinterJobHandlerTest::MakeJobFetchReturnNoJobs() {
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
}

void PrinterJobHandlerTest::MessageLoopQuitNowHelper(
    base::MessageLoop* message_loop) {
  message_loop->QuitWhenIdle();
}

void PrinterJobHandlerTest::MessageLoopQuitSoonHelper(
    base::MessageLoop* message_loop) {
  message_loop->task_runner()->PostTask(
      FROM_HERE, base::Bind(&MessageLoopQuitNowHelper, message_loop));
}

PrinterJobHandlerTest::PrinterJobHandlerTest()
    : factory_(NULL, base::Bind(&TestURLFetcherCallback::CreateURLFetcher,
                                base::Unretained(&url_callback_))) {
}

bool PrinterJobHandlerTest::PostSpoolSuccess() {
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE,
      base::Bind(&PrinterJobHandler::OnJobSpoolSucceeded, job_handler_, 0));

  // Everything that would be posted on the printer thread queue
  // has been posted, we can tell the main message loop to quit when idle
  // and not worry about it idling while the print thread does work
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::Bind(&MessageLoopQuitSoonHelper, &loop_));
  return true;
}

void PrinterJobHandlerTest::AddMimeHeader(const GURL& url,
                                          net::FakeURLFetcher* fetcher) {
  scoped_refptr<net::HttpResponseHeaders> download_headers =
      new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders);
  fetcher->set_response_headers(download_headers);
}

void PrinterJobHandlerTest::AddTicketMimeHeader(const GURL& url,
                                                net::FakeURLFetcher* fetcher) {
  scoped_refptr<net::HttpResponseHeaders> download_headers =
      new net::HttpResponseHeaders(kExampleTicketDownloadResponseHeaders);
  fetcher->set_response_headers(download_headers);
}


void PrinterJobHandlerTest::SetUpJobSuccessTest(int job_num) {
  factory_.SetFakeResponse(TicketURI(job_num),
                           kExamplePrintTicket, net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(DownloadURI(job_num),
                           kExamplePrintData, net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);

  factory_.SetFakeResponse(DoneURI(job_num),
                           StatusResponse(job_num, "DONE"),
                           net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(InProgressURI(job_num),
                           StatusResponse(job_num, "IN_PROGRESS"),
                           net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);

  // The times requirement is relaxed for the ticket URI
  // in order to accommodate TicketDownloadFailureTest
  EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(job_num), _))
      .Times(AtLeast(1))
      .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader));

  EXPECT_CALL(url_callback_, OnRequestCreate(DownloadURI(job_num), _))
      .Times(Exactly(1))
      .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader));

  EXPECT_CALL(url_callback_, OnRequestCreate(InProgressURI(job_num), _))
      .Times(Exactly(1));

  EXPECT_CALL(url_callback_, OnRequestCreate(DoneURI(job_num), _))
      .Times(Exactly(1));

  EXPECT_CALL(print_system_->JobSpooler(),
              Spool(kExamplePrintTicket, _, _, _, _, _, _, _))
      .Times(Exactly(1))
      .WillOnce(InvokeWithoutArgs(this,
                                  &PrinterJobHandlerTest::PostSpoolSuccess));
}

void PrinterJobHandlerTest::BeginTest(int timeout_seconds) {
  job_handler_ = new PrinterJobHandler(basic_info_,
                                       info_from_cloud_,
                                       GURL(kExampleCloudPrintServerURL),
                                       print_system_.get(),
                                       &jobhandler_delegate_);

  job_handler_->Initialize();

  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&PrinterJobHandlerTest::MessageLoopQuitSoonHelper,
                 base::MessageLoop::current()),
      base::TimeDelta::FromSeconds(timeout_seconds));

  base::MessageLoop::current()->Run();
}

void PrinterJobHandlerTest::SendCapsAndDefaults(
    const std::string& printer_name,
    const PrintSystem::PrinterCapsAndDefaultsCallback& callback) {
  callback.Run(true, printer_name, caps_and_defaults_);
}

bool PrinterJobHandlerTest::GetPrinterInfo(printing::PrinterBasicInfo* info) {
  *info = basic_info_;
  return true;
}

void PrinterJobHandlerTest::TearDown() {
  IdleOut();
  CloudPrintURLFetcher::set_factory(NULL);
}

void PrinterJobHandlerTest::IdleOut() {
  base::MessageLoop::current()->RunUntilIdle();
}

MockPrintServerWatcher::MockPrintServerWatcher() : delegate_(NULL) {
  ON_CALL(*this, StartWatching(_))
      .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true)));
  ON_CALL(*this, StopWatching()).WillByDefault(Return(true));
}


MockPrinterWatcher::MockPrinterWatcher() : delegate_(NULL) {
  ON_CALL(*this, StartWatching(_))
      .WillByDefault(DoAll(SaveArg<0>(&delegate_), Return(true)));
  ON_CALL(*this, StopWatching()).WillByDefault(Return(true));
}

MockJobSpooler::MockJobSpooler() : delegate_(NULL) {
  ON_CALL(*this, Spool(_, _, _, _, _, _, _, _))
      .WillByDefault(DoAll(SaveArg<7>(&delegate_), Return(true)));
}

MockPrintSystem::MockPrintSystem()
    : job_spooler_(new NiceMock<MockJobSpooler>()),
      printer_watcher_(new NiceMock<MockPrinterWatcher>()),
      print_server_watcher_(new NiceMock<MockPrintServerWatcher>()) {
  ON_CALL(*this, CreateJobSpooler()).WillByDefault(Return(job_spooler_.get()));

  ON_CALL(*this, CreatePrinterWatcher(_))
      .WillByDefault(Return(printer_watcher_.get()));

  ON_CALL(*this, CreatePrintServerWatcher())
      .WillByDefault(Return(print_server_watcher_.get()));

  ON_CALL(*this, IsValidPrinter(_)).
      WillByDefault(Return(true));

  ON_CALL(*this, ValidatePrintTicket(_, _, _)).
      WillByDefault(Return(true));
};

// This test simulates an end-to-end printing of a document
// but tests only non-failure cases.
// Disabled - http://crbug.com/184245
TEST_F(PrinterJobHandlerTest, DISABLED_HappyPathTest) {
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
      .Times(Exactly(1));
  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
      .Times(Exactly(1));

  SetUpJobSuccessTest(1);
  BeginTest(20);
}

TEST_F(PrinterJobHandlerTest, TicketDownloadFailureTest) {
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
                           JobListResponse(2), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
                           JobListResponse(2), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(TicketURI(1), std::string(),
                           net::HTTP_INTERNAL_SERVER_ERROR,
                           net::URLRequestStatus::FAILED);

  EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _))
      .Times(AtLeast(1))
      .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddTicketMimeHeader));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
      .Times(AtLeast(1));

  SetUpJobSuccessTest(2);
  BeginTest(20);
}

// TODO(noamsml): Figure out how to make this test not take 1 second and
// re-enable it
TEST_F(PrinterJobHandlerTest, DISABLED_ManyFailureTest) {
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore),
                           JobListResponse(0), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonRetry), _))
      .Times(AtLeast(1));

  SetUpJobSuccessTest(1);

  factory_.SetFakeResponse(TicketURI(1),
                           std::string(),
                           net::HTTP_INTERNAL_SERVER_ERROR,
                           net::URLRequestStatus::FAILED);

  loop_.task_runner()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&net::FakeURLFetcherFactory::SetFakeResponse,
                 base::Unretained(&factory_), TicketURI(1), kExamplePrintTicket,
                 net::HTTP_OK, net::URLRequestStatus::SUCCESS),
      base::TimeDelta::FromSeconds(1));

  BeginTest(5);
}


// TODO(noamsml): Figure out how to make this test not take ~64-~2048 (depending
// constant values) seconds and re-enable it
TEST_F(PrinterJobHandlerTest, DISABLED_CompleteFailureTest) {
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry),
                           JobListResponse(1), net::HTTP_OK,
                           net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(ErrorURI(1), StatusResponse(1, "ERROR"),
                           net::HTTP_OK, net::URLRequestStatus::SUCCESS);
  factory_.SetFakeResponse(TicketURI(1), std::string(),
                           net::HTTP_INTERNAL_SERVER_ERROR,
                           net::URLRequestStatus::FAILED);

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonStartup), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonFailure), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_,
              OnRequestCreate(JobListURI(kJobFetchReasonRetry), _))
      .Times(AtLeast(1));

  EXPECT_CALL(url_callback_, OnRequestCreate(ErrorURI(1), _))
      .Times(Exactly(1))
      .WillOnce(InvokeWithoutArgs(
          this, &PrinterJobHandlerTest::MakeJobFetchReturnNoJobs));

  EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _))
      .Times(AtLeast(kNumRetriesBeforeAbandonJob));

  BeginTest(70);
}

}  // namespace cloud_print