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 "base/callback.h" | 5 #include "base/callback.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "content/browser/byte_stream.h" | 10 #include "content/browser/byte_stream.h" |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 EXPECT_EQ(reason, item->GetLastReason()); | 750 EXPECT_EQ(reason, item->GetLastReason()); |
751 | 751 |
752 // Cancel should kill it. | 752 // Cancel should kill it. |
753 item->Cancel(true); | 753 item->Cancel(true); |
754 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 754 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
755 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 755 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
756 } | 756 } |
757 | 757 |
758 // Destination errors that occur before the intermediate rename shouldn't cause | 758 // Destination errors that occur before the intermediate rename shouldn't cause |
759 // the download to be marked as interrupted until after the intermediate rename. | 759 // the download to be marked as interrupted until after the intermediate rename. |
760 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename) { | 760 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Restart) { |
761 DownloadItemImpl* item = CreateDownloadItem(); | 761 DownloadItemImpl* item = CreateDownloadItem(); |
762 DownloadItemImplDelegate::DownloadTargetCallback callback; | 762 DownloadItemImplDelegate::DownloadTargetCallback callback; |
763 MockDownloadFile* download_file = | 763 MockDownloadFile* download_file = |
764 AddDownloadFileToDownloadItem(item, &callback); | 764 AddDownloadFileToDownloadItem(item, &callback); |
765 item->DestinationObserverAsWeakPtr()->DestinationError( | 765 item->DestinationObserverAsWeakPtr()->DestinationError( |
766 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 766 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
767 ASSERT_TRUE(item->IsInProgress()); | 767 ASSERT_TRUE(item->IsInProgress()); |
768 | 768 |
769 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); | 769 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); |
770 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 770 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
771 base::FilePath new_intermediate_path( | 771 base::FilePath new_intermediate_path( |
772 final_path.InsertBeforeExtensionASCII("y")); | 772 final_path.InsertBeforeExtensionASCII("y")); |
773 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 773 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
774 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 774 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
775 new_intermediate_path)); | 775 new_intermediate_path)); |
776 EXPECT_CALL(*download_file, Cancel()) | 776 EXPECT_CALL(*download_file, Cancel()) |
777 .Times(1); | 777 .Times(1); |
778 | 778 |
779 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 779 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
780 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 780 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
781 RunAllPendingInMessageLoops(); | 781 RunAllPendingInMessageLoops(); |
782 // All the callbacks should have happened by now. | 782 // All the callbacks should have happened by now. |
783 ::testing::Mock::VerifyAndClearExpectations(download_file); | 783 ::testing::Mock::VerifyAndClearExpectations(download_file); |
784 mock_delegate()->VerifyAndClearExpectations(); | 784 mock_delegate()->VerifyAndClearExpectations(); |
785 EXPECT_TRUE(item->IsInterrupted()); | 785 EXPECT_TRUE(item->IsInterrupted()); |
786 EXPECT_TRUE(item->GetFullPath().empty()); | 786 EXPECT_TRUE(item->GetFullPath().empty()); |
787 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 787 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
788 } | 788 } |
789 | 789 |
| 790 // As above. But if the download can be resumed by continuing, then the |
| 791 // intermediate path should be retained when the download is interrupted after |
| 792 // the intermediate rename succeeds. |
| 793 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) { |
| 794 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 795 switches::kEnableDownloadResumption); |
| 796 DownloadItemImpl* item = CreateDownloadItem(); |
| 797 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 798 MockDownloadFile* download_file = |
| 799 AddDownloadFileToDownloadItem(item, &callback); |
| 800 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 801 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); |
| 802 ASSERT_TRUE(item->IsInProgress()); |
| 803 |
| 804 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 805 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 806 base::FilePath new_intermediate_path( |
| 807 final_path.InsertBeforeExtensionASCII("y")); |
| 808 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 809 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 810 new_intermediate_path)); |
| 811 EXPECT_CALL(*download_file, FullPath()) |
| 812 .WillOnce(Return(base::FilePath(new_intermediate_path))); |
| 813 EXPECT_CALL(*download_file, Detach()); |
| 814 |
| 815 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 816 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 817 RunAllPendingInMessageLoops(); |
| 818 // All the callbacks should have happened by now. |
| 819 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 820 mock_delegate()->VerifyAndClearExpectations(); |
| 821 EXPECT_TRUE(item->IsInterrupted()); |
| 822 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 823 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 824 } |
| 825 |
| 826 // As above. If the intermediate rename fails, then the interrupt reason should |
| 827 // be set to the destination error and the intermediate path should be empty. |
| 828 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Failed) { |
| 829 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 830 switches::kEnableDownloadResumption); |
| 831 DownloadItemImpl* item = CreateDownloadItem(); |
| 832 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 833 MockDownloadFile* download_file = |
| 834 AddDownloadFileToDownloadItem(item, &callback); |
| 835 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 836 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); |
| 837 ASSERT_TRUE(item->IsInProgress()); |
| 838 |
| 839 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 840 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 841 base::FilePath new_intermediate_path( |
| 842 final_path.InsertBeforeExtensionASCII("y")); |
| 843 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 844 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
| 845 new_intermediate_path)); |
| 846 EXPECT_CALL(*download_file, Cancel()) |
| 847 .Times(1); |
| 848 |
| 849 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 850 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 851 RunAllPendingInMessageLoops(); |
| 852 // All the callbacks should have happened by now. |
| 853 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 854 mock_delegate()->VerifyAndClearExpectations(); |
| 855 EXPECT_TRUE(item->IsInterrupted()); |
| 856 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
| 857 EXPECT_TRUE(item->GetFullPath().empty()); |
| 858 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 859 } |
| 860 |
790 TEST_F(DownloadItemTest, Canceled) { | 861 TEST_F(DownloadItemTest, Canceled) { |
791 DownloadItemImpl* item = CreateDownloadItem(); | 862 DownloadItemImpl* item = CreateDownloadItem(); |
792 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 863 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
793 | 864 |
794 // Confirm cancel sets state properly. | 865 // Confirm cancel sets state properly. |
795 EXPECT_CALL(*download_file, Cancel()); | 866 EXPECT_CALL(*download_file, Cancel()); |
796 item->Cancel(true); | 867 item->Cancel(true); |
797 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 868 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
798 } | 869 } |
799 | 870 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1200 base::Unretained(&returned_path))); | 1271 base::Unretained(&returned_path))); |
1201 RunAllPendingInMessageLoops(); | 1272 RunAllPendingInMessageLoops(); |
1202 EXPECT_TRUE(returned_path.empty()); | 1273 EXPECT_TRUE(returned_path.empty()); |
1203 } | 1274 } |
1204 | 1275 |
1205 TEST(MockDownloadItem, Compiles) { | 1276 TEST(MockDownloadItem, Compiles) { |
1206 MockDownloadItem mock_item; | 1277 MockDownloadItem mock_item; |
1207 } | 1278 } |
1208 | 1279 |
1209 } // namespace content | 1280 } // namespace content |
OLD | NEW |