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

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

Issue 10704052: Download filename determination refactor (3/3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with r148594 to and resolve conflicts with r148576 Created 8 years, 4 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 | Annotate | Revision Log
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 <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 57
58 class MockDownloadItemImpl : public DownloadItemImpl { 58 class MockDownloadItemImpl : public DownloadItemImpl {
59 public: 59 public:
60 // Use history constructor for minimal base object. 60 // Use history constructor for minimal base object.
61 MockDownloadItemImpl(DownloadItemImplDelegate* delegate) 61 MockDownloadItemImpl(DownloadItemImplDelegate* delegate)
62 : DownloadItemImpl(delegate, content::DownloadId(), 62 : DownloadItemImpl(delegate, content::DownloadId(),
63 content::DownloadPersistentStoreInfo(), 63 content::DownloadPersistentStoreInfo(),
64 net::BoundNetLog()) {} 64 net::BoundNetLog()) {}
65 virtual ~MockDownloadItemImpl() {} 65 virtual ~MockDownloadItemImpl() {}
66 66
67 MOCK_METHOD4(OnDownloadTargetDetermined,
68 void(const FilePath&, TargetDisposition,
69 content::DownloadDangerType, const FilePath&));
67 MOCK_METHOD1(AddObserver, void(content::DownloadItem::Observer*)); 70 MOCK_METHOD1(AddObserver, void(content::DownloadItem::Observer*));
68 MOCK_METHOD1(RemoveObserver, void(content::DownloadItem::Observer*)); 71 MOCK_METHOD1(RemoveObserver, void(content::DownloadItem::Observer*));
69 MOCK_METHOD0(UpdateObservers, void()); 72 MOCK_METHOD0(UpdateObservers, void());
70 MOCK_METHOD0(CanShowInFolder, bool()); 73 MOCK_METHOD0(CanShowInFolder, bool());
71 MOCK_METHOD0(CanOpenDownload, bool()); 74 MOCK_METHOD0(CanOpenDownload, bool());
72 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
73 MOCK_METHOD0(OpenDownload, void()); 76 MOCK_METHOD0(OpenDownload, void());
74 MOCK_METHOD0(ShowDownloadInShell, void()); 77 MOCK_METHOD0(ShowDownloadInShell, void());
75 MOCK_METHOD0(DangerousDownloadValidated, void()); 78 MOCK_METHOD0(DangerousDownloadValidated, void());
76 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
77 MOCK_METHOD1(Cancel, void(bool)); 80 MOCK_METHOD1(Cancel, void(bool));
78 MOCK_METHOD0(MarkAsComplete, void()); 81 MOCK_METHOD0(MarkAsComplete, void());
79 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 82 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
80 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); 83 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&));
81 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 84 MOCK_METHOD0(OnDownloadedFileRemoved, void());
82 MOCK_METHOD0(MaybeCompleteDownload, void()); 85 MOCK_METHOD0(MaybeCompleteDownload, void());
83 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 86 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
84 MOCK_METHOD1(Delete, void(DeleteReason)); 87 MOCK_METHOD1(Delete, void(DeleteReason));
85 MOCK_METHOD0(Remove, void()); 88 MOCK_METHOD0(Remove, void());
86 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 89 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
87 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 90 MOCK_CONST_METHOD0(CurrentSpeed, int64());
88 MOCK_CONST_METHOD0(PercentComplete, int()); 91 MOCK_CONST_METHOD0(PercentComplete, int());
89 MOCK_CONST_METHOD0(AllDataSaved, bool()); 92 MOCK_CONST_METHOD0(AllDataSaved, bool());
90 MOCK_METHOD0(TogglePause, void()); 93 MOCK_METHOD0(TogglePause, void());
91 MOCK_METHOD1(OnDownloadCompleting, void(DownloadFileManager*)); 94 MOCK_METHOD0(OnDownloadCompleting, void());
92 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 95 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
93 MOCK_CONST_METHOD0(IsPartialDownload, bool()); 96 MOCK_CONST_METHOD0(IsPartialDownload, bool());
94 MOCK_CONST_METHOD0(IsInProgress, bool()); 97 MOCK_CONST_METHOD0(IsInProgress, bool());
95 MOCK_CONST_METHOD0(IsCancelled, bool()); 98 MOCK_CONST_METHOD0(IsCancelled, bool());
96 MOCK_CONST_METHOD0(IsInterrupted, bool()); 99 MOCK_CONST_METHOD0(IsInterrupted, bool());
97 MOCK_CONST_METHOD0(IsComplete, bool()); 100 MOCK_CONST_METHOD0(IsComplete, bool());
98 MOCK_CONST_METHOD0(GetFullPath, const FilePath&()); 101 MOCK_CONST_METHOD0(GetFullPath, const FilePath&());
99 MOCK_CONST_METHOD0(GetTargetFilePath, const FilePath&()); 102 MOCK_CONST_METHOD0(GetTargetFilePath, const FilePath&());
100 MOCK_CONST_METHOD0(GetTargetDisposition, TargetDisposition()); 103 MOCK_CONST_METHOD0(GetTargetDisposition, TargetDisposition());
101 MOCK_METHOD3(OnTargetPathDetermined, void(const FilePath&,
102 TargetDisposition,
103 content::DownloadDangerType));
104 MOCK_METHOD1(OnTargetPathSelected, void(const FilePath&));
105 MOCK_METHOD1(OnContentCheckCompleted, void(content::DownloadDangerType)); 104 MOCK_METHOD1(OnContentCheckCompleted, void(content::DownloadDangerType));
106 MOCK_METHOD2(OnIntermediatePathDetermined, void(DownloadFileManager*,
107 const FilePath&));
108 MOCK_CONST_METHOD0(GetState, DownloadState()); 105 MOCK_CONST_METHOD0(GetState, DownloadState());
109 MOCK_CONST_METHOD0(GetUrlChain, const std::vector<GURL>&()); 106 MOCK_CONST_METHOD0(GetUrlChain, const std::vector<GURL>&());
110 MOCK_METHOD1(SetTotalBytes, void(int64)); 107 MOCK_METHOD1(SetTotalBytes, void(int64));
111 MOCK_CONST_METHOD0(GetURL, const GURL&()); 108 MOCK_CONST_METHOD0(GetURL, const GURL&());
112 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&()); 109 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&());
113 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&()); 110 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&());
114 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string()); 111 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string());
115 MOCK_CONST_METHOD0(GetContentDisposition, std::string()); 112 MOCK_CONST_METHOD0(GetContentDisposition, std::string());
116 MOCK_CONST_METHOD0(GetMimeType, std::string()); 113 MOCK_CONST_METHOD0(GetMimeType, std::string());
117 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string()); 114 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 147 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
151 MOCK_CONST_METHOD0(GetETag, const std::string&()); 148 MOCK_CONST_METHOD0(GetETag, const std::string&());
152 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 149 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
153 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 150 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
154 content::DownloadPersistentStoreInfo()); 151 content::DownloadPersistentStoreInfo());
155 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 152 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
156 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 153 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
157 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 154 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
158 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 155 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
159 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 156 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
160 MOCK_METHOD1(OffThreadCancel, void(DownloadFileManager* file_manager)); 157 MOCK_METHOD0(OffThreadCancel, void());
161 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 158 MOCK_CONST_METHOD1(DebugString, std::string(bool));
162 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 159 MOCK_METHOD0(MockDownloadOpenForTesting, void());
163 MOCK_METHOD1(GetExternalData, ExternalData*(const void*)); 160 MOCK_METHOD1(GetExternalData, ExternalData*(const void*));
164 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*)); 161 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*));
165 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*)); 162 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*));
166 }; 163 };
167 164
168 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 165 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
169 public: 166 public:
170 MockDownloadManagerDelegate(); 167 MockDownloadManagerDelegate();
171 virtual ~MockDownloadManagerDelegate(); 168 virtual ~MockDownloadManagerDelegate();
172 169
173 MOCK_METHOD0(Shutdown, void()); 170 MOCK_METHOD0(Shutdown, void());
174 MOCK_METHOD0(GetNextId, content::DownloadId()); 171 MOCK_METHOD0(GetNextId, content::DownloadId());
175 MOCK_METHOD1(ShouldStartDownload, bool(int32)); 172 MOCK_METHOD2(DetermineDownloadTarget,
176 MOCK_METHOD1(ChooseDownloadPath, void(DownloadItem*)); 173 bool(DownloadItem* item,
177 MOCK_METHOD1(GetIntermediatePath, FilePath(const DownloadItem&)); 174 const content::DownloadTargetCallback&));
178 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); 175 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*());
179 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 176 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
180 MOCK_METHOD2(ShouldCompleteDownload, bool( 177 MOCK_METHOD2(ShouldCompleteDownload, bool(
181 DownloadItem*, const base::Closure&)); 178 DownloadItem*, const base::Closure&));
182 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*)); 179 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*));
183 MOCK_METHOD0(GenerateFileHash, bool()); 180 MOCK_METHOD0(GenerateFileHash, bool());
184 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*)); 181 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*));
185 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*)); 182 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*));
186 MOCK_METHOD2(UpdatePathForItemInPersistentStore, 183 MOCK_METHOD2(UpdatePathForItemInPersistentStore,
187 void(DownloadItem*, const FilePath&)); 184 void(DownloadItem*, const FilePath&));
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 585 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
589 .WillOnce(Return(true)); 586 .WillOnce(Return(true));
590 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile( 587 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
591 info.get(), static_cast<content::ByteStreamReader*>(NULL), 588 info.get(), static_cast<content::ByteStreamReader*>(NULL),
592 download_manager_.get(), true, _, _)); 589 download_manager_.get(), true, _, _));
593 590
594 download_manager_->StartDownload(info.Pass(), stream.Pass()); 591 download_manager_->StartDownload(info.Pass(), stream.Pass());
595 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 592 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
596 } 593 }
597 594
598 // Does the DownloadManager prompt when requested?
599 TEST_F(DownloadManagerTest, RestartDownload) {
600 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
601 .WillOnce(Return());
602 EXPECT_CALL(GetMockObserver(), SelectFileDialogDisplayed(
603 download_manager_.get(), 0)).WillOnce(Return());
604 // Put a mock we have a handle to on the download manager.
605 MockDownloadItemImpl& item(AddItemToManager());
606 int download_id = item.GetId();
607
608 // Confirm we're internally consistent.
609 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id));
610
611 ScopedTempDir download_dir;
612 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
613 FilePath expected_path(download_dir.path().Append(
614 FILE_PATH_LITERAL("location")));
615
616 EXPECT_CALL(item, GetTargetDisposition())
617 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_PROMPT));
618 EXPECT_CALL(GetMockDownloadManagerDelegate(), ChooseDownloadPath(&item));
619 download_manager_->RestartDownload(download_id);
620
621 // The alternative pathway goes straight to OnTargetPathAvailable,
622 // so it more naturally belongs below.
623 }
624
625 // Do the results of GetIntermediatePath get passed through to the
626 // download? Note that this path is tested from RestartDownload
627 // to test the non-prompting path in RestartDownload as well.
628 TEST_F(DownloadManagerTest, OnTargetPathAvailable) {
629 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
630 .WillOnce(Return());
631 // Put a mock we have a handle to on the download manager.
632 MockDownloadItemImpl& item(AddItemToManager());
633
634 ScopedTempDir download_dir;
635 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
636 FilePath target_path(download_dir.path().Append(
637 FILE_PATH_LITERAL("location")));
638 FilePath intermediate_path(download_dir.path().Append(
639 FILE_PATH_LITERAL("location.crdownload")));
640
641 EXPECT_CALL(item, GetTargetDisposition())
642 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE));
643 EXPECT_CALL(GetMockDownloadManagerDelegate(),
644 GetIntermediatePath(Ref(item)))
645 .WillOnce(Return(intermediate_path));
646 // Finesse DCHECK with WillRepeatedly.
647 EXPECT_CALL(item, GetTargetFilePath())
648 .WillRepeatedly(ReturnRef(target_path));
649 EXPECT_CALL(item, OnIntermediatePathDetermined(
650 &GetMockDownloadFileManager(), intermediate_path));
651 download_manager_->RestartDownload(item.GetId());
652 }
653
654 // Do the results of an OnDownloadInterrupted get passed through properly 595 // Do the results of an OnDownloadInterrupted get passed through properly
655 // to the DownloadItem? 596 // to the DownloadItem?
656 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { 597 TEST_F(DownloadManagerTest, OnDownloadInterrupted) {
657 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 598 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
658 .WillOnce(Return()); 599 .WillOnce(Return());
659 // Put a mock we have a handle to on the download manager. 600 // Put a mock we have a handle to on the download manager.
660 MockDownloadItemImpl& item(AddItemToManager()); 601 MockDownloadItemImpl& item(AddItemToManager());
661 int download_id = item.GetId(); 602 int download_id = item.GetId();
662 603
663 content::DownloadInterruptReason reason( 604 content::DownloadInterruptReason reason(
(...skipping 12 matching lines...) Expand all
676 // Put a mock we have a handle to on the download manager. 617 // Put a mock we have a handle to on the download manager.
677 MockDownloadItemImpl& item(AddItemToManager()); 618 MockDownloadItemImpl& item(AddItemToManager());
678 619
679 EXPECT_CALL(item, IsPersisted()) 620 EXPECT_CALL(item, IsPersisted())
680 .WillRepeatedly(Return(false)); 621 .WillRepeatedly(Return(false));
681 EXPECT_CALL(item, GetState()) 622 EXPECT_CALL(item, GetState())
682 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 623 .WillRepeatedly(Return(DownloadItem::CANCELLED));
683 EXPECT_CALL(item, GetDbHandle()) 624 EXPECT_CALL(item, GetDbHandle())
684 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 625 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
685 626
686 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); 627 EXPECT_CALL(item, OffThreadCancel());
687 DownloadStopped(&item); 628 DownloadStopped(&item);
688 // TODO(rdsmith): Confirm that the download item is no longer on the 629 // TODO(rdsmith): Confirm that the download item is no longer on the
689 // active list by calling download_manager_->GetActiveDownloadItem(id). 630 // active list by calling download_manager_->GetActiveDownloadItem(id).
690 // Currently, the item is left on the active list for rendez-vous with 631 // Currently, the item is left on the active list for rendez-vous with
691 // the history system :-{. 632 // the history system :-{.
692 } 633 }
693 634
694 // Does DownloadStopped remove Download from appropriate queues? 635 // Does DownloadStopped remove Download from appropriate queues?
695 // This test tests persisted downloads. 636 // This test tests persisted downloads.
696 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 637 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
(...skipping 11 matching lines...) Expand all
708 649
709 EXPECT_CALL(item, IsPersisted()) 650 EXPECT_CALL(item, IsPersisted())
710 .WillRepeatedly(Return(true)); 651 .WillRepeatedly(Return(true));
711 EXPECT_CALL(GetMockDownloadManagerDelegate(), 652 EXPECT_CALL(GetMockDownloadManagerDelegate(),
712 UpdateItemInPersistentStore(&item)); 653 UpdateItemInPersistentStore(&item));
713 EXPECT_CALL(item, GetState()) 654 EXPECT_CALL(item, GetState())
714 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 655 .WillRepeatedly(Return(DownloadItem::CANCELLED));
715 EXPECT_CALL(item, GetDbHandle()) 656 EXPECT_CALL(item, GetDbHandle())
716 .WillRepeatedly(Return(db_handle)); 657 .WillRepeatedly(Return(db_handle));
717 658
718 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); 659 EXPECT_CALL(item, OffThreadCancel());
719 DownloadStopped(&item); 660 DownloadStopped(&item);
720 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 661 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
721 } 662 }
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | content/public/browser/download_item.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698