OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/md5.h" | 6 #include "base/md5.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "chrome/common/cloud_print/cloud_print_constants.h" | 12 #include "chrome/common/cloud_print/cloud_print_constants.h" |
13 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 13 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
14 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 14 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
15 #include "chrome/service/cloud_print/print_system.h" | 15 #include "chrome/service/cloud_print/print_system.h" |
16 #include "chrome/service/cloud_print/printer_job_handler.h" | 16 #include "chrome/service/cloud_print/printer_job_handler.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 18 #include "net/http/http_status_code.h" |
18 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
20 #include "printing/backend/print_backend.h" | 21 #include "printing/backend/print_backend.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 using ::testing::AtLeast; | 25 using ::testing::AtLeast; |
25 using ::testing::DoAll; | 26 using ::testing::DoAll; |
26 using ::testing::Exactly; | 27 using ::testing::Exactly; |
27 using ::testing::Invoke; | 28 using ::testing::Invoke; |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // This class handles the callback from FakeURLFetcher | 275 // This class handles the callback from FakeURLFetcher |
275 // It is a separate class because callback methods must be | 276 // It is a separate class because callback methods must be |
276 // on RefCounted classes | 277 // on RefCounted classes |
277 | 278 |
278 class TestURLFetcherCallback { | 279 class TestURLFetcherCallback { |
279 public: | 280 public: |
280 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 281 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
281 const GURL& url, | 282 const GURL& url, |
282 net::URLFetcherDelegate* d, | 283 net::URLFetcherDelegate* d, |
283 const std::string& response_data, | 284 const std::string& response_data, |
284 bool success) { | 285 net::HttpStatusCode response_code) { |
285 scoped_ptr<net::FakeURLFetcher> fetcher( | 286 scoped_ptr<net::FakeURLFetcher> fetcher( |
286 new net::FakeURLFetcher(url, d, response_data, success)); | 287 new net::FakeURLFetcher(url, d, response_data, response_code)); |
287 OnRequestCreate(url, fetcher.get()); | 288 OnRequestCreate(url, fetcher.get()); |
288 return fetcher.Pass(); | 289 return fetcher.Pass(); |
289 } | 290 } |
290 MOCK_METHOD2(OnRequestCreate, | 291 MOCK_METHOD2(OnRequestCreate, |
291 void(const GURL&, net::FakeURLFetcher*)); | 292 void(const GURL&, net::FakeURLFetcher*)); |
292 }; | 293 }; |
293 | 294 |
294 | 295 |
295 class MockPrinterJobHandlerDelegate | 296 class MockPrinterJobHandlerDelegate |
296 : public PrinterJobHandler::Delegate { | 297 : public PrinterJobHandler::Delegate { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo)); | 491 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::GetPrinterInfo)); |
491 | 492 |
492 ON_CALL(*print_system_.get(), GetPrinterCapsAndDefaults(_, _)) | 493 ON_CALL(*print_system_.get(), GetPrinterCapsAndDefaults(_, _)) |
493 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults)); | 494 .WillByDefault(Invoke(this, &PrinterJobHandlerTest::SendCapsAndDefaults)); |
494 | 495 |
495 CloudPrintURLFetcher::set_factory(&cloud_print_factory_); | 496 CloudPrintURLFetcher::set_factory(&cloud_print_factory_); |
496 } | 497 } |
497 | 498 |
498 void PrinterJobHandlerTest::MakeJobFetchReturnNoJobs() { | 499 void PrinterJobHandlerTest::MakeJobFetchReturnNoJobs() { |
499 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), | 500 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), |
500 JobListResponse(0), true); | 501 JobListResponse(0), net::HTTP_OK); |
501 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), | 502 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), |
502 JobListResponse(0), true); | 503 JobListResponse(0), net::HTTP_OK); |
503 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), | 504 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), |
504 JobListResponse(0), true); | 505 JobListResponse(0), net::HTTP_OK); |
505 } | 506 } |
506 | 507 |
507 void PrinterJobHandlerTest::MessageLoopQuitNowHelper( | 508 void PrinterJobHandlerTest::MessageLoopQuitNowHelper( |
508 base::MessageLoop* message_loop) { | 509 base::MessageLoop* message_loop) { |
509 message_loop->QuitWhenIdle(); | 510 message_loop->QuitWhenIdle(); |
510 } | 511 } |
511 | 512 |
512 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper( | 513 void PrinterJobHandlerTest::MessageLoopQuitSoonHelper( |
513 base::MessageLoop* message_loop) { | 514 base::MessageLoop* message_loop) { |
514 message_loop->message_loop_proxy()->PostTask( | 515 message_loop->message_loop_proxy()->PostTask( |
(...skipping 21 matching lines...) Expand all Loading... |
536 void PrinterJobHandlerTest::AddMimeHeader(const GURL& url, | 537 void PrinterJobHandlerTest::AddMimeHeader(const GURL& url, |
537 net::FakeURLFetcher* fetcher) { | 538 net::FakeURLFetcher* fetcher) { |
538 scoped_refptr<net::HttpResponseHeaders> download_headers = | 539 scoped_refptr<net::HttpResponseHeaders> download_headers = |
539 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders); | 540 new net::HttpResponseHeaders(kExampleJobDownloadResponseHeaders); |
540 fetcher->set_response_headers(download_headers); | 541 fetcher->set_response_headers(download_headers); |
541 } | 542 } |
542 | 543 |
543 | 544 |
544 void PrinterJobHandlerTest::SetUpJobSuccessTest(int job_num) { | 545 void PrinterJobHandlerTest::SetUpJobSuccessTest(int job_num) { |
545 factory_.SetFakeResponse(TicketURI(job_num), | 546 factory_.SetFakeResponse(TicketURI(job_num), |
546 kExamplePrintTicket, true); | 547 kExamplePrintTicket, net::HTTP_OK); |
547 factory_.SetFakeResponse(DownloadURI(job_num), | 548 factory_.SetFakeResponse(DownloadURI(job_num), |
548 kExamplePrintData, true); | 549 kExamplePrintData, net::HTTP_OK); |
549 | 550 |
550 factory_.SetFakeResponse(DoneURI(job_num), | 551 factory_.SetFakeResponse(DoneURI(job_num), |
551 StatusResponse(job_num, "DONE"), | 552 StatusResponse(job_num, "DONE"), |
552 true); | 553 net::HTTP_OK); |
553 factory_.SetFakeResponse(InProgressURI(job_num), | 554 factory_.SetFakeResponse(InProgressURI(job_num), |
554 StatusResponse(job_num, "IN_PROGRESS"), | 555 StatusResponse(job_num, "IN_PROGRESS"), |
555 true); | 556 net::HTTP_OK); |
556 | 557 |
557 // The times requirement is relaxed for the ticket URI | 558 // The times requirement is relaxed for the ticket URI |
558 // in order to accommodate TicketDownloadFailureTest | 559 // in order to accommodate TicketDownloadFailureTest |
559 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(job_num), _)) | 560 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(job_num), _)) |
560 .Times(AtLeast(1)); | 561 .Times(AtLeast(1)); |
561 | 562 |
562 EXPECT_CALL(url_callback_, OnRequestCreate(DownloadURI(job_num), _)) | 563 EXPECT_CALL(url_callback_, OnRequestCreate(DownloadURI(job_num), _)) |
563 .Times(Exactly(1)) | 564 .Times(Exactly(1)) |
564 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader)); | 565 .WillOnce(Invoke(this, &PrinterJobHandlerTest::AddMimeHeader)); |
565 | 566 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 | 650 |
650 ON_CALL(*this, ValidatePrintTicket(_, _)). | 651 ON_CALL(*this, ValidatePrintTicket(_, _)). |
651 WillByDefault(Return(true)); | 652 WillByDefault(Return(true)); |
652 }; | 653 }; |
653 | 654 |
654 // This test simulates an end-to-end printing of a document | 655 // This test simulates an end-to-end printing of a document |
655 // but tests only non-failure cases. | 656 // but tests only non-failure cases. |
656 // Disabled - http://crbug.com/184245 | 657 // Disabled - http://crbug.com/184245 |
657 TEST_F(PrinterJobHandlerTest, DISABLED_HappyPathTest) { | 658 TEST_F(PrinterJobHandlerTest, DISABLED_HappyPathTest) { |
658 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), | 659 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), |
659 JobListResponse(1), true); | 660 JobListResponse(1), net::HTTP_OK); |
660 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), | 661 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), |
661 JobListResponse(0), true); | 662 JobListResponse(0), net::HTTP_OK); |
662 | 663 |
663 EXPECT_CALL(url_callback_, | 664 EXPECT_CALL(url_callback_, |
664 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) | 665 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) |
665 .Times(Exactly(1)); | 666 .Times(Exactly(1)); |
666 EXPECT_CALL(url_callback_, | 667 EXPECT_CALL(url_callback_, |
667 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) | 668 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) |
668 .Times(Exactly(1)); | 669 .Times(Exactly(1)); |
669 | 670 |
670 SetUpJobSuccessTest(1); | 671 SetUpJobSuccessTest(1); |
671 BeginTest(20); | 672 BeginTest(20); |
672 } | 673 } |
673 | 674 |
674 TEST_F(PrinterJobHandlerTest, TicketDownloadFailureTest) { | 675 TEST_F(PrinterJobHandlerTest, TicketDownloadFailureTest) { |
675 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), | 676 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), |
676 JobListResponse(2), true); | 677 JobListResponse(2), net::HTTP_OK); |
677 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), | 678 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), |
678 JobListResponse(2), true); | 679 JobListResponse(2), net::HTTP_OK); |
679 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), | 680 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), |
680 JobListResponse(0), true); | 681 JobListResponse(0), net::HTTP_OK); |
681 factory_.SetFakeResponse(TicketURI(1), std::string(), false); | 682 factory_.SetFakeResponse(TicketURI(1), std::string(), |
| 683 net::HTTP_INTERNAL_SERVER_ERROR); |
682 | 684 |
683 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _)) | 685 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _)) |
684 .Times(AtLeast(1)); | 686 .Times(AtLeast(1)); |
685 | 687 |
686 EXPECT_CALL(url_callback_, | 688 EXPECT_CALL(url_callback_, |
687 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) | 689 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) |
688 .Times(AtLeast(1)); | 690 .Times(AtLeast(1)); |
689 | 691 |
690 EXPECT_CALL(url_callback_, | 692 EXPECT_CALL(url_callback_, |
691 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) | 693 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) |
692 .Times(AtLeast(1)); | 694 .Times(AtLeast(1)); |
693 | 695 |
694 EXPECT_CALL(url_callback_, | 696 EXPECT_CALL(url_callback_, |
695 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) | 697 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) |
696 .Times(AtLeast(1)); | 698 .Times(AtLeast(1)); |
697 | 699 |
698 SetUpJobSuccessTest(2); | 700 SetUpJobSuccessTest(2); |
699 BeginTest(20); | 701 BeginTest(20); |
700 } | 702 } |
701 | 703 |
702 // TODO(noamsml): Figure out how to make this test not take 1 second and | 704 // TODO(noamsml): Figure out how to make this test not take 1 second and |
703 // re-enable it | 705 // re-enable it |
704 TEST_F(PrinterJobHandlerTest, DISABLED_ManyFailureTest) { | 706 TEST_F(PrinterJobHandlerTest, DISABLED_ManyFailureTest) { |
705 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), | 707 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), |
706 JobListResponse(1), true); | 708 JobListResponse(1), net::HTTP_OK); |
707 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), | 709 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), |
708 JobListResponse(1), true); | 710 JobListResponse(1), net::HTTP_OK); |
709 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), | 711 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), |
710 JobListResponse(1), true); | 712 JobListResponse(1), net::HTTP_OK); |
711 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), | 713 factory_.SetFakeResponse(JobListURI(kJobFetchReasonQueryMore), |
712 JobListResponse(0), true); | 714 JobListResponse(0), net::HTTP_OK); |
713 | 715 |
714 EXPECT_CALL(url_callback_, | 716 EXPECT_CALL(url_callback_, |
715 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) | 717 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) |
716 .Times(AtLeast(1)); | 718 .Times(AtLeast(1)); |
717 | 719 |
718 EXPECT_CALL(url_callback_, | 720 EXPECT_CALL(url_callback_, |
719 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) | 721 OnRequestCreate(JobListURI(kJobFetchReasonQueryMore), _)) |
720 .Times(AtLeast(1)); | 722 .Times(AtLeast(1)); |
721 | 723 |
722 EXPECT_CALL(url_callback_, | 724 EXPECT_CALL(url_callback_, |
723 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) | 725 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) |
724 .Times(AtLeast(1)); | 726 .Times(AtLeast(1)); |
725 | 727 |
726 EXPECT_CALL(url_callback_, | 728 EXPECT_CALL(url_callback_, |
727 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _)) | 729 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _)) |
728 .Times(AtLeast(1)); | 730 .Times(AtLeast(1)); |
729 | 731 |
730 SetUpJobSuccessTest(1); | 732 SetUpJobSuccessTest(1); |
731 | 733 |
732 factory_.SetFakeResponse(TicketURI(1), std::string(), false); | 734 factory_.SetFakeResponse(TicketURI(1), |
| 735 std::string(), |
| 736 net::HTTP_INTERNAL_SERVER_ERROR); |
733 | 737 |
734 loop_.PostDelayedTask(FROM_HERE, | 738 loop_.PostDelayedTask(FROM_HERE, |
735 base::Bind(&net::FakeURLFetcherFactory::SetFakeResponse, | 739 base::Bind(&net::FakeURLFetcherFactory::SetFakeResponse, |
736 base::Unretained(&factory_), | 740 base::Unretained(&factory_), |
737 TicketURI(1), | 741 TicketURI(1), |
738 kExamplePrintTicket, | 742 kExamplePrintTicket, |
739 true), | 743 net::HTTP_OK), |
740 base::TimeDelta::FromSeconds(1)); | 744 base::TimeDelta::FromSeconds(1)); |
741 | 745 |
742 | 746 |
743 BeginTest(5); | 747 BeginTest(5); |
744 } | 748 } |
745 | 749 |
746 | 750 |
747 // TODO(noamsml): Figure out how to make this test not take ~64-~2048 (depending | 751 // TODO(noamsml): Figure out how to make this test not take ~64-~2048 (depending |
748 // constant values) seconds and re-enable it | 752 // constant values) seconds and re-enable it |
749 TEST_F(PrinterJobHandlerTest, DISABLED_CompleteFailureTest) { | 753 TEST_F(PrinterJobHandlerTest, DISABLED_CompleteFailureTest) { |
750 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), | 754 factory_.SetFakeResponse(JobListURI(kJobFetchReasonStartup), |
751 JobListResponse(1), true); | 755 JobListResponse(1), net::HTTP_OK); |
752 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), | 756 factory_.SetFakeResponse(JobListURI(kJobFetchReasonFailure), |
753 JobListResponse(1), true); | 757 JobListResponse(1), net::HTTP_OK); |
754 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), | 758 factory_.SetFakeResponse(JobListURI(kJobFetchReasonRetry), |
755 JobListResponse(1), true); | 759 JobListResponse(1), net::HTTP_OK); |
756 factory_.SetFakeResponse(ErrorURI(1), StatusResponse(1, "ERROR"), true); | 760 factory_.SetFakeResponse(ErrorURI(1), StatusResponse(1, "ERROR"), |
757 factory_.SetFakeResponse(TicketURI(1), std::string(), false); | 761 net::HTTP_OK); |
| 762 factory_.SetFakeResponse(TicketURI(1), std::string(), |
| 763 net::HTTP_INTERNAL_SERVER_ERROR); |
758 | 764 |
759 EXPECT_CALL(url_callback_, | 765 EXPECT_CALL(url_callback_, |
760 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) | 766 OnRequestCreate(JobListURI(kJobFetchReasonStartup), _)) |
761 .Times(AtLeast(1)); | 767 .Times(AtLeast(1)); |
762 | 768 |
763 EXPECT_CALL(url_callback_, | 769 EXPECT_CALL(url_callback_, |
764 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) | 770 OnRequestCreate(JobListURI(kJobFetchReasonFailure), _)) |
765 .Times(AtLeast(1)); | 771 .Times(AtLeast(1)); |
766 | 772 |
767 EXPECT_CALL(url_callback_, | 773 EXPECT_CALL(url_callback_, |
768 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _)) | 774 OnRequestCreate(JobListURI(kJobFetchReasonRetry), _)) |
769 .Times(AtLeast(1)); | 775 .Times(AtLeast(1)); |
770 | 776 |
771 EXPECT_CALL(url_callback_, OnRequestCreate(ErrorURI(1), _)) | 777 EXPECT_CALL(url_callback_, OnRequestCreate(ErrorURI(1), _)) |
772 .Times(Exactly(1)) | 778 .Times(Exactly(1)) |
773 .WillOnce(InvokeWithoutArgs( | 779 .WillOnce(InvokeWithoutArgs( |
774 this, &PrinterJobHandlerTest::MakeJobFetchReturnNoJobs)); | 780 this, &PrinterJobHandlerTest::MakeJobFetchReturnNoJobs)); |
775 | 781 |
776 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _)) | 782 EXPECT_CALL(url_callback_, OnRequestCreate(TicketURI(1), _)) |
777 .Times(AtLeast(kNumRetriesBeforeAbandonJob)); | 783 .Times(AtLeast(kNumRetriesBeforeAbandonJob)); |
778 | 784 |
779 BeginTest(70); | 785 BeginTest(70); |
780 } | 786 } |
781 | 787 |
782 } // namespace cloud_print | 788 } // namespace cloud_print |
OLD | NEW |