OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/download/download_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <iterator> | 9 #include <iterator> |
10 #include <map> | 10 #include <map> |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 const base::FilePath::CharType kDummyIntermediatePath[] = | 54 const base::FilePath::CharType kDummyIntermediatePath[] = |
55 FILE_PATH_LITERAL("/testpathx"); | 55 FILE_PATH_LITERAL("/testpathx"); |
56 | 56 |
57 namespace content { | 57 namespace content { |
58 | 58 |
59 namespace { | 59 namespace { |
60 | 60 |
61 class MockDelegate : public DownloadItemImplDelegate { | 61 class MockDelegate : public DownloadItemImplDelegate { |
62 public: | 62 public: |
63 MockDelegate() : DownloadItemImplDelegate() { | 63 MockDelegate() : DownloadItemImplDelegate() { |
| 64 browser_context_.reset(new TestBrowserContext); |
64 SetDefaultExpectations(); | 65 SetDefaultExpectations(); |
65 } | 66 } |
66 | 67 |
67 MOCK_METHOD2(DetermineDownloadTarget, void( | 68 MOCK_METHOD2(DetermineDownloadTarget, void( |
68 DownloadItemImpl*, const DownloadTargetCallback&)); | 69 DownloadItemImpl*, const DownloadTargetCallback&)); |
69 MOCK_METHOD2(ShouldCompleteDownload, | 70 MOCK_METHOD2(ShouldCompleteDownload, |
70 bool(DownloadItemImpl*, const base::Closure&)); | 71 bool(DownloadItemImpl*, const base::Closure&)); |
71 MOCK_METHOD2(ShouldOpenDownload, | 72 MOCK_METHOD2(ShouldOpenDownload, |
72 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); | 73 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); |
73 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&)); | 74 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const base::FilePath&)); |
74 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 75 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
75 | 76 |
76 void ResumeInterruptedDownload(std::unique_ptr<DownloadUrlParameters> params, | 77 void ResumeInterruptedDownload(std::unique_ptr<DownloadUrlParameters> params, |
77 uint32_t id) override { | 78 uint32_t id) override { |
78 MockResumeInterruptedDownload(params.get(), id); | 79 MockResumeInterruptedDownload(params.get(), id); |
79 } | 80 } |
80 MOCK_METHOD2(MockResumeInterruptedDownload, | 81 MOCK_METHOD2(MockResumeInterruptedDownload, |
81 void(DownloadUrlParameters* params, uint32_t id)); | 82 void(DownloadUrlParameters* params, uint32_t id)); |
82 | 83 |
83 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 84 BrowserContext* GetBrowserContext() const override { |
| 85 return browser_context_.get(); |
| 86 } |
| 87 |
84 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 88 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
85 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 89 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
86 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 90 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
87 | 91 |
88 void VerifyAndClearExpectations() { | 92 void VerifyAndClearExpectations() { |
89 ::testing::Mock::VerifyAndClearExpectations(this); | 93 ::testing::Mock::VerifyAndClearExpectations(this); |
90 SetDefaultExpectations(); | 94 SetDefaultExpectations(); |
91 } | 95 } |
92 | 96 |
93 private: | 97 private: |
94 void SetDefaultExpectations() { | 98 void SetDefaultExpectations() { |
95 EXPECT_CALL(*this, AssertStateConsistent(_)) | 99 EXPECT_CALL(*this, AssertStateConsistent(_)) |
96 .WillRepeatedly(Return()); | 100 .WillRepeatedly(Return()); |
97 EXPECT_CALL(*this, ShouldOpenFileBasedOnExtension(_)) | 101 EXPECT_CALL(*this, ShouldOpenFileBasedOnExtension(_)) |
98 .WillRepeatedly(Return(false)); | 102 .WillRepeatedly(Return(false)); |
99 EXPECT_CALL(*this, ShouldOpenDownload(_, _)) | 103 EXPECT_CALL(*this, ShouldOpenDownload(_, _)) |
100 .WillRepeatedly(Return(true)); | 104 .WillRepeatedly(Return(true)); |
101 } | 105 } |
| 106 |
| 107 std::unique_ptr<TestBrowserContext> browser_context_; |
102 }; | 108 }; |
103 | 109 |
104 class MockRequestHandle : public DownloadRequestHandleInterface { | 110 class MockRequestHandle : public DownloadRequestHandleInterface { |
105 public: | 111 public: |
106 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 112 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
107 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 113 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
108 MOCK_CONST_METHOD0(PauseRequest, void()); | 114 MOCK_CONST_METHOD0(PauseRequest, void()); |
109 MOCK_CONST_METHOD0(ResumeRequest, void()); | 115 MOCK_CONST_METHOD0(ResumeRequest, void()); |
110 MOCK_CONST_METHOD0(CancelRequest, void()); | 116 MOCK_CONST_METHOD0(CancelRequest, void()); |
111 MOCK_CONST_METHOD0(DebugString, std::string()); | 117 MOCK_CONST_METHOD0(DebugString, std::string()); |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { | 616 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { |
611 DownloadItemImpl* item = CreateDownloadItem(); | 617 DownloadItemImpl* item = CreateDownloadItem(); |
612 TestDownloadItemObserver observer(item); | 618 TestDownloadItemObserver observer(item); |
613 MockDownloadFile* download_file = | 619 MockDownloadFile* download_file = |
614 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 620 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
615 | 621 |
616 // Interrupt the download, using a continuable interrupt. | 622 // Interrupt the download, using a continuable interrupt. |
617 EXPECT_CALL(*download_file, FullPath()) | 623 EXPECT_CALL(*download_file, FullPath()) |
618 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 624 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
619 EXPECT_CALL(*download_file, Detach()); | 625 EXPECT_CALL(*download_file, Detach()); |
620 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
621 .WillRepeatedly(Return(browser_context())); | |
622 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); | 626 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); |
623 item->DestinationObserverAsWeakPtr()->DestinationError( | 627 item->DestinationObserverAsWeakPtr()->DestinationError( |
624 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 628 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
625 std::unique_ptr<crypto::SecureHash>()); | 629 std::unique_ptr<crypto::SecureHash>()); |
626 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 630 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
627 // Since the download is resumed automatically, the interrupt count doesn't | 631 // Since the download is resumed automatically, the interrupt count doesn't |
628 // increase. | 632 // increase. |
629 ASSERT_EQ(0, observer.interrupt_count()); | 633 ASSERT_EQ(0, observer.interrupt_count()); |
630 | 634 |
631 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 635 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 item->DestinationObserverAsWeakPtr()); | 701 item->DestinationObserverAsWeakPtr()); |
698 TestDownloadItemObserver observer(item); | 702 TestDownloadItemObserver observer(item); |
699 MockDownloadFile* mock_download_file(nullptr); | 703 MockDownloadFile* mock_download_file(nullptr); |
700 std::unique_ptr<DownloadFile> download_file; | 704 std::unique_ptr<DownloadFile> download_file; |
701 MockRequestHandle* mock_request_handle(nullptr); | 705 MockRequestHandle* mock_request_handle(nullptr); |
702 std::unique_ptr<DownloadRequestHandleInterface> request_handle; | 706 std::unique_ptr<DownloadRequestHandleInterface> request_handle; |
703 DownloadItemImplDelegate::DownloadTargetCallback callback; | 707 DownloadItemImplDelegate::DownloadTargetCallback callback; |
704 | 708 |
705 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 709 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
706 .WillRepeatedly(SaveArg<1>(&callback)); | 710 .WillRepeatedly(SaveArg<1>(&callback)); |
707 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
708 .WillRepeatedly(Return(browser_context())); | |
709 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) | 711 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) |
710 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); | 712 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); |
711 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { | 713 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
712 SCOPED_TRACE(::testing::Message() << "Iteration " << i); | 714 SCOPED_TRACE(::testing::Message() << "Iteration " << i); |
713 | 715 |
714 mock_download_file = new NiceMock<MockDownloadFile>; | 716 mock_download_file = new NiceMock<MockDownloadFile>; |
715 download_file.reset(mock_download_file); | 717 download_file.reset(mock_download_file); |
716 mock_request_handle = new NiceMock<MockRequestHandle>; | 718 mock_request_handle = new NiceMock<MockRequestHandle>; |
717 request_handle.reset(mock_request_handle); | 719 request_handle.reset(mock_request_handle); |
718 | 720 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 DownloadItemImpl* item = CreateDownloadItem(); | 773 DownloadItemImpl* item = CreateDownloadItem(); |
772 MockDownloadFile* download_file = | 774 MockDownloadFile* download_file = |
773 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 775 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
774 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 776 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
775 EXPECT_EQ(kFirstETag, item->GetETag()); | 777 EXPECT_EQ(kFirstETag, item->GetETag()); |
776 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 778 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
777 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 779 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
778 EXPECT_EQ(kMimeType, item->GetMimeType()); | 780 EXPECT_EQ(kMimeType, item->GetMimeType()); |
779 | 781 |
780 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 782 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
781 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
782 .WillRepeatedly(Return(browser_context())); | |
783 EXPECT_CALL(*download_file, Detach()); | 783 EXPECT_CALL(*download_file, Detach()); |
784 item->DestinationObserverAsWeakPtr()->DestinationError( | 784 item->DestinationObserverAsWeakPtr()->DestinationError( |
785 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 785 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
786 std::unique_ptr<crypto::SecureHash>()); | 786 std::unique_ptr<crypto::SecureHash>()); |
787 RunAllPendingInMessageLoops(); | 787 RunAllPendingInMessageLoops(); |
788 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 788 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
789 | 789 |
790 // Now change the create info. The changes should not cause the DownloadItem | 790 // Now change the create info. The changes should not cause the DownloadItem |
791 // to be updated. | 791 // to be updated. |
792 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 792 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 create_info()->content_disposition = kContentDisposition; | 826 create_info()->content_disposition = kContentDisposition; |
827 create_info()->etag = kFirstETag; | 827 create_info()->etag = kFirstETag; |
828 create_info()->last_modified = kFirstLastModified; | 828 create_info()->last_modified = kFirstLastModified; |
829 create_info()->url_chain.push_back(GURL(kFirstURL)); | 829 create_info()->url_chain.push_back(GURL(kFirstURL)); |
830 create_info()->mime_type = kMimeType; | 830 create_info()->mime_type = kMimeType; |
831 | 831 |
832 DownloadItemImpl* item = CreateDownloadItem(); | 832 DownloadItemImpl* item = CreateDownloadItem(); |
833 MockDownloadFile* download_file = | 833 MockDownloadFile* download_file = |
834 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 834 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
835 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 835 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
836 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
837 .WillRepeatedly(Return(browser_context())); | |
838 EXPECT_CALL(*download_file, Detach()); | 836 EXPECT_CALL(*download_file, Detach()); |
839 item->DestinationObserverAsWeakPtr()->DestinationError( | 837 item->DestinationObserverAsWeakPtr()->DestinationError( |
840 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 838 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
841 std::unique_ptr<crypto::SecureHash>()); | 839 std::unique_ptr<crypto::SecureHash>()); |
842 RunAllPendingInMessageLoops(); | 840 RunAllPendingInMessageLoops(); |
843 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 841 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
844 | 842 |
845 // Now change the create info. The changes should not cause the DownloadItem | 843 // Now change the create info. The changes should not cause the DownloadItem |
846 // to be updated. | 844 // to be updated. |
847 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 845 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
(...skipping 30 matching lines...) Expand all Loading... |
878 | 876 |
879 DownloadItemImpl* item = CreateDownloadItem(); | 877 DownloadItemImpl* item = CreateDownloadItem(); |
880 TestDownloadItemObserver observer(item); | 878 TestDownloadItemObserver observer(item); |
881 MockDownloadFile* download_file = | 879 MockDownloadFile* download_file = |
882 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 880 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
883 | 881 |
884 // Interrupt the download, using a continuable interrupt. | 882 // Interrupt the download, using a continuable interrupt. |
885 EXPECT_CALL(*download_file, FullPath()) | 883 EXPECT_CALL(*download_file, FullPath()) |
886 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 884 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
887 EXPECT_CALL(*download_file, Detach()); | 885 EXPECT_CALL(*download_file, Detach()); |
888 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
889 .WillRepeatedly(Return(browser_context())); | |
890 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( | 886 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( |
891 Property(&DownloadUrlParameters::url, | 887 Property(&DownloadUrlParameters::url, |
892 GURL("http://example.com/c")), | 888 GURL("http://example.com/c")), |
893 _)) | 889 _)) |
894 .Times(1); | 890 .Times(1); |
895 item->DestinationObserverAsWeakPtr()->DestinationError( | 891 item->DestinationObserverAsWeakPtr()->DestinationError( |
896 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 892 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
897 std::unique_ptr<crypto::SecureHash>()); | 893 std::unique_ptr<crypto::SecureHash>()); |
898 | 894 |
899 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 895 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
(...skipping 1216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2116 | 2112 |
2117 item_->Cancel(true); | 2113 item_->Cancel(true); |
2118 RunAllPendingInMessageLoops(); | 2114 RunAllPendingInMessageLoops(); |
2119 } | 2115 } |
2120 | 2116 |
2121 TEST(MockDownloadItem, Compiles) { | 2117 TEST(MockDownloadItem, Compiles) { |
2122 MockDownloadItem mock_item; | 2118 MockDownloadItem mock_item; |
2123 } | 2119 } |
2124 | 2120 |
2125 } // namespace content | 2121 } // namespace content |
OLD | NEW |