Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1548)

Side by Side Diff: content/browser/download/download_item_impl_unittest.cc

Issue 16994004: Remove DownloadItem::Is*() in favor of DI::GetState() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@di-getstate-2
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 746 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
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_EQ(DownloadItem::IN_PROGRESS, item->GetState());
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_EQ(DownloadItem::INTERRUPTED, item->GetState());
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 TEST_F(DownloadItemTest, Canceled) { 790 TEST_F(DownloadItemTest, Canceled) {
791 DownloadItemImpl* item = CreateDownloadItem(); 791 DownloadItemImpl* item = CreateDownloadItem();
792 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 792 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
793 793
794 // Confirm cancel sets state properly. 794 // Confirm cancel sets state properly.
795 EXPECT_CALL(*download_file, Cancel()); 795 EXPECT_CALL(*download_file, Cancel());
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 EXPECT_EQ("", item->GetHashState()); 884 EXPECT_EQ("", item->GetHashState());
885 EXPECT_TRUE(item->AllDataSaved()); 885 EXPECT_TRUE(item->AllDataSaved());
886 } 886 }
887 887
888 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { 888 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) {
889 DownloadItemImpl* item = CreateDownloadItem(); 889 DownloadItemImpl* item = CreateDownloadItem();
890 MockDownloadFile* download_file = 890 MockDownloadFile* download_file =
891 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 891 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
892 892
893 // InProgress 893 // InProgress
894 ASSERT_TRUE(item->IsInProgress()); 894 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
895 ASSERT_FALSE(item->GetTargetFilePath().empty()); 895 ASSERT_FALSE(item->GetTargetFilePath().empty());
896 EXPECT_TRUE(item->CanShowInFolder()); 896 EXPECT_TRUE(item->CanShowInFolder());
897 EXPECT_TRUE(item->CanOpenDownload()); 897 EXPECT_TRUE(item->CanOpenDownload());
898 898
899 // Complete 899 // Complete
900 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 900 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _))
901 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 901 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
902 base::FilePath(kDummyPath))); 902 base::FilePath(kDummyPath)));
903 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 903 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
904 .WillOnce(Return(true)); 904 .WillOnce(Return(true));
905 EXPECT_CALL(*download_file, FullPath()) 905 EXPECT_CALL(*download_file, FullPath())
906 .WillOnce(Return(base::FilePath())); 906 .WillOnce(Return(base::FilePath()));
907 EXPECT_CALL(*download_file, Detach()); 907 EXPECT_CALL(*download_file, Detach());
908 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 908 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
909 RunAllPendingInMessageLoops(); 909 RunAllPendingInMessageLoops();
910 910
911 ASSERT_TRUE(item->IsComplete()); 911 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
912 EXPECT_TRUE(item->CanShowInFolder()); 912 EXPECT_TRUE(item->CanShowInFolder());
913 EXPECT_TRUE(item->CanOpenDownload()); 913 EXPECT_TRUE(item->CanOpenDownload());
914 } 914 }
915 915
916 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { 916 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) {
917 DownloadItemImpl* item = CreateDownloadItem(); 917 DownloadItemImpl* item = CreateDownloadItem();
918 MockDownloadFile* download_file = 918 MockDownloadFile* download_file =
919 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 919 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
920 item->SetIsTemporary(true); 920 item->SetIsTemporary(true);
921 921
922 // InProgress Temporary 922 // InProgress Temporary
923 ASSERT_TRUE(item->IsInProgress()); 923 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
924 ASSERT_FALSE(item->GetTargetFilePath().empty()); 924 ASSERT_FALSE(item->GetTargetFilePath().empty());
925 ASSERT_TRUE(item->IsTemporary()); 925 ASSERT_TRUE(item->IsTemporary());
926 EXPECT_FALSE(item->CanShowInFolder()); 926 EXPECT_FALSE(item->CanShowInFolder());
927 EXPECT_FALSE(item->CanOpenDownload()); 927 EXPECT_FALSE(item->CanOpenDownload());
928 928
929 // Complete Temporary 929 // Complete Temporary
930 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 930 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
931 .WillOnce(Return(true)); 931 .WillOnce(Return(true));
932 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 932 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _))
933 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 933 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
934 base::FilePath(kDummyPath))); 934 base::FilePath(kDummyPath)));
935 EXPECT_CALL(*download_file, FullPath()) 935 EXPECT_CALL(*download_file, FullPath())
936 .WillOnce(Return(base::FilePath())); 936 .WillOnce(Return(base::FilePath()));
937 EXPECT_CALL(*download_file, Detach()); 937 EXPECT_CALL(*download_file, Detach());
938 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 938 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string());
939 RunAllPendingInMessageLoops(); 939 RunAllPendingInMessageLoops();
940 940
941 ASSERT_TRUE(item->IsComplete()); 941 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
942 EXPECT_FALSE(item->CanShowInFolder()); 942 EXPECT_FALSE(item->CanShowInFolder());
943 EXPECT_FALSE(item->CanOpenDownload()); 943 EXPECT_FALSE(item->CanOpenDownload());
944 } 944 }
945 945
946 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { 946 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) {
947 DownloadItemImpl* item = CreateDownloadItem(); 947 DownloadItemImpl* item = CreateDownloadItem();
948 MockDownloadFile* download_file = 948 MockDownloadFile* download_file =
949 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 949 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
950 950
951 EXPECT_CALL(*download_file, Cancel()); 951 EXPECT_CALL(*download_file, Cancel());
952 item->DestinationObserverAsWeakPtr()->DestinationError( 952 item->DestinationObserverAsWeakPtr()->DestinationError(
953 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 953 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
954 RunAllPendingInMessageLoops(); 954 RunAllPendingInMessageLoops();
955 955
956 ASSERT_TRUE(item->IsInterrupted()); 956 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState());
957 ASSERT_FALSE(item->GetTargetFilePath().empty()); 957 ASSERT_FALSE(item->GetTargetFilePath().empty());
958 EXPECT_FALSE(item->CanShowInFolder()); 958 EXPECT_FALSE(item->CanShowInFolder());
959 EXPECT_FALSE(item->CanOpenDownload()); 959 EXPECT_FALSE(item->CanOpenDownload());
960 } 960 }
961 961
962 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { 962 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) {
963 DownloadItemImpl* item = CreateDownloadItem(); 963 DownloadItemImpl* item = CreateDownloadItem();
964 MockDownloadFile* download_file = 964 MockDownloadFile* download_file =
965 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 965 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
966 966
967 EXPECT_CALL(*download_file, Cancel()); 967 EXPECT_CALL(*download_file, Cancel());
968 item->Cancel(true); 968 item->Cancel(true);
969 RunAllPendingInMessageLoops(); 969 RunAllPendingInMessageLoops();
970 970
971 ASSERT_TRUE(item->IsCancelled()); 971 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState());
972 EXPECT_FALSE(item->CanShowInFolder()); 972 EXPECT_FALSE(item->CanShowInFolder());
973 EXPECT_FALSE(item->CanOpenDownload()); 973 EXPECT_FALSE(item->CanOpenDownload());
974 } 974 }
975 975
976 // Test various aspects of the delegate completion blocker. 976 // Test various aspects of the delegate completion blocker.
977 977
978 // Just allowing completion. 978 // Just allowing completion.
979 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { 979 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) {
980 // Test to confirm that if we have a callback that returns true, 980 // Test to confirm that if we have a callback that returns true,
981 // we complete immediately. 981 // we complete immediately.
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 base::Unretained(&returned_path))); 1200 base::Unretained(&returned_path)));
1201 RunAllPendingInMessageLoops(); 1201 RunAllPendingInMessageLoops();
1202 EXPECT_TRUE(returned_path.empty()); 1202 EXPECT_TRUE(returned_path.empty());
1203 } 1203 }
1204 1204
1205 TEST(MockDownloadItem, Compiles) { 1205 TEST(MockDownloadItem, Compiles) {
1206 MockDownloadItem mock_item; 1206 MockDownloadItem mock_item;
1207 } 1207 }
1208 1208
1209 } // namespace content 1209 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698