| 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 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 } | 753 } |
| 754 | 754 |
| 755 // If the download attempts to resume and the resumption request fails, the | 755 // If the download attempts to resume and the resumption request fails, the |
| 756 // subsequent Start() call shouldn't update the origin state (URL redirect | 756 // subsequent Start() call shouldn't update the origin state (URL redirect |
| 757 // chains, Content-Disposition, download URL, etc..) | 757 // chains, Content-Disposition, download URL, etc..) |
| 758 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) { | 758 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) { |
| 759 const char kContentDisposition[] = "attachment; filename=foo"; | 759 const char kContentDisposition[] = "attachment; filename=foo"; |
| 760 const char kFirstETag[] = "ABC"; | 760 const char kFirstETag[] = "ABC"; |
| 761 const char kFirstLastModified[] = "Yesterday"; | 761 const char kFirstLastModified[] = "Yesterday"; |
| 762 const char kFirstURL[] = "http://www.example.com/download"; | 762 const char kFirstURL[] = "http://www.example.com/download"; |
| 763 const char kMimeType[] = "text/css"; |
| 763 create_info()->content_disposition = kContentDisposition; | 764 create_info()->content_disposition = kContentDisposition; |
| 764 create_info()->etag = kFirstETag; | 765 create_info()->etag = kFirstETag; |
| 765 create_info()->last_modified = kFirstLastModified; | 766 create_info()->last_modified = kFirstLastModified; |
| 766 create_info()->url_chain.push_back(GURL(kFirstURL)); | 767 create_info()->url_chain.push_back(GURL(kFirstURL)); |
| 768 create_info()->mime_type = kMimeType; |
| 767 | 769 |
| 768 DownloadItemImpl* item = CreateDownloadItem(); | 770 DownloadItemImpl* item = CreateDownloadItem(); |
| 769 MockDownloadFile* download_file = | 771 MockDownloadFile* download_file = |
| 770 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 772 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 771 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 773 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 772 EXPECT_EQ(kFirstETag, item->GetETag()); | 774 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 773 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 775 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 774 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 776 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 777 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 775 | 778 |
| 776 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 779 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
| 777 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | 780 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) |
| 778 .WillRepeatedly(Return(browser_context())); | 781 .WillRepeatedly(Return(browser_context())); |
| 779 EXPECT_CALL(*download_file, Detach()); | 782 EXPECT_CALL(*download_file, Detach()); |
| 780 item->DestinationObserverAsWeakPtr()->DestinationError( | 783 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 781 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 784 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 782 std::unique_ptr<crypto::SecureHash>()); | 785 std::unique_ptr<crypto::SecureHash>()); |
| 783 RunAllPendingInMessageLoops(); | 786 RunAllPendingInMessageLoops(); |
| 784 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 787 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 785 | 788 |
| 786 // Now change the create info. The changes should not cause the DownloadItem | 789 // Now change the create info. The changes should not cause the DownloadItem |
| 787 // to be updated. | 790 // to be updated. |
| 788 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 791 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 789 const char kSecondETag[] = "123"; | 792 const char kSecondETag[] = "123"; |
| 790 const char kSecondLastModified[] = "Today"; | 793 const char kSecondLastModified[] = "Today"; |
| 791 const char kSecondURL[] = "http://example.com/another-download"; | 794 const char kSecondURL[] = "http://example.com/another-download"; |
| 795 const char kSecondMimeType[] = "text/html"; |
| 792 create_info()->content_disposition = kSecondContentDisposition; | 796 create_info()->content_disposition = kSecondContentDisposition; |
| 793 create_info()->etag = kSecondETag; | 797 create_info()->etag = kSecondETag; |
| 794 create_info()->last_modified = kSecondLastModified; | 798 create_info()->last_modified = kSecondLastModified; |
| 795 create_info()->url_chain.clear(); | 799 create_info()->url_chain.clear(); |
| 796 create_info()->url_chain.push_back(GURL(kSecondURL)); | 800 create_info()->url_chain.push_back(GURL(kSecondURL)); |
| 801 create_info()->mime_type = kSecondMimeType; |
| 797 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | 802 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; |
| 798 | 803 |
| 799 // The following ends up calling DownloadItem::Start(), but shouldn't result | 804 // The following ends up calling DownloadItem::Start(), but shouldn't result |
| 800 // in an origin update. | 805 // in an origin update. |
| 801 download_file = CallDownloadItemStart(item, nullptr); | 806 download_file = CallDownloadItemStart(item, nullptr); |
| 802 | 807 |
| 803 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 808 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 804 EXPECT_EQ(kFirstETag, item->GetETag()); | 809 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 805 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 810 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 806 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 811 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 812 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 807 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 813 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 808 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | 814 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
| 809 } | 815 } |
| 810 | 816 |
| 817 // If the download resumption request succeeds, the origin state should be |
| 818 // updated |
| 819 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) { |
| 820 const char kContentDisposition[] = "attachment; filename=foo"; |
| 821 const char kFirstETag[] = "ABC"; |
| 822 const char kFirstLastModified[] = "Yesterday"; |
| 823 const char kFirstURL[] = "http://www.example.com/download"; |
| 824 const char kMimeType[] = "text/css"; |
| 825 create_info()->content_disposition = kContentDisposition; |
| 826 create_info()->etag = kFirstETag; |
| 827 create_info()->last_modified = kFirstLastModified; |
| 828 create_info()->url_chain.push_back(GURL(kFirstURL)); |
| 829 create_info()->mime_type = kMimeType; |
| 830 |
| 831 DownloadItemImpl* item = CreateDownloadItem(); |
| 832 MockDownloadFile* download_file = |
| 833 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 834 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
| 835 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) |
| 836 .WillRepeatedly(Return(browser_context())); |
| 837 EXPECT_CALL(*download_file, Detach()); |
| 838 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 839 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 840 std::unique_ptr<crypto::SecureHash>()); |
| 841 RunAllPendingInMessageLoops(); |
| 842 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 843 |
| 844 // Now change the create info. The changes should not cause the DownloadItem |
| 845 // to be updated. |
| 846 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 847 const char kSecondETag[] = "123"; |
| 848 const char kSecondLastModified[] = "Today"; |
| 849 const char kSecondURL[] = "http://example.com/another-download"; |
| 850 const char kSecondMimeType[] = "text/html"; |
| 851 create_info()->content_disposition = kSecondContentDisposition; |
| 852 create_info()->etag = kSecondETag; |
| 853 create_info()->last_modified = kSecondLastModified; |
| 854 create_info()->url_chain.clear(); |
| 855 create_info()->url_chain.push_back(GURL(kSecondURL)); |
| 856 create_info()->mime_type = kSecondMimeType; |
| 857 |
| 858 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
| 859 download_file = CallDownloadItemStart(item, &target_callback); |
| 860 |
| 861 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition()); |
| 862 EXPECT_EQ(kSecondETag, item->GetETag()); |
| 863 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime()); |
| 864 EXPECT_EQ(kSecondURL, item->GetURL().spec()); |
| 865 EXPECT_EQ(kSecondMimeType, item->GetMimeType()); |
| 866 |
| 867 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 868 } |
| 869 |
| 811 // Test that resumption uses the final URL in a URL chain when resuming. | 870 // Test that resumption uses the final URL in a URL chain when resuming. |
| 812 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { | 871 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { |
| 813 create_info()->save_info->prompt_for_save_location = false; | 872 create_info()->save_info->prompt_for_save_location = false; |
| 814 create_info()->url_chain.clear(); | 873 create_info()->url_chain.clear(); |
| 815 create_info()->url_chain.push_back(GURL("http://example.com/a")); | 874 create_info()->url_chain.push_back(GURL("http://example.com/a")); |
| 816 create_info()->url_chain.push_back(GURL("http://example.com/b")); | 875 create_info()->url_chain.push_back(GURL("http://example.com/b")); |
| 817 create_info()->url_chain.push_back(GURL("http://example.com/c")); | 876 create_info()->url_chain.push_back(GURL("http://example.com/c")); |
| 818 | 877 |
| 819 DownloadItemImpl* item = CreateDownloadItem(); | 878 DownloadItemImpl* item = CreateDownloadItem(); |
| 820 TestDownloadItemObserver observer(item); | 879 TestDownloadItemObserver observer(item); |
| (...skipping 1227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2048 | 2107 |
| 2049 item_->Cancel(true); | 2108 item_->Cancel(true); |
| 2050 RunAllPendingInMessageLoops(); | 2109 RunAllPendingInMessageLoops(); |
| 2051 } | 2110 } |
| 2052 | 2111 |
| 2053 TEST(MockDownloadItem, Compiles) { | 2112 TEST(MockDownloadItem, Compiles) { |
| 2054 MockDownloadItem mock_item; | 2113 MockDownloadItem mock_item; |
| 2055 } | 2114 } |
| 2056 | 2115 |
| 2057 } // namespace content | 2116 } // namespace content |
| OLD | NEW |