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

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

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r168573 Created 8 years, 1 month 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/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // has a |default_download_directory| that matches |download_directory|. 58 // has a |default_download_directory| that matches |download_directory|.
59 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") { 59 MATCHER_P2(DownloadCreateInfoWithDefaultPath, info, download_directory, "") {
60 return arg == info && 60 return arg == info &&
61 arg->default_download_directory == download_directory; 61 arg->default_download_directory == download_directory;
62 } 62 }
63 63
64 class MockDownloadItemImpl : public DownloadItemImpl { 64 class MockDownloadItemImpl : public DownloadItemImpl {
65 public: 65 public:
66 // Use history constructor for minimal base object. 66 // Use history constructor for minimal base object.
67 MockDownloadItemImpl(DownloadItemImplDelegate* delegate) 67 MockDownloadItemImpl(DownloadItemImplDelegate* delegate)
68 : DownloadItemImpl(delegate, DownloadId(), 68 : DownloadItemImpl(
69 DownloadPersistentStoreInfo(), 69 delegate,
70 net::BoundNetLog()) {} 70 content::DownloadId(),
71 FilePath(),
72 GURL(),
73 GURL(),
74 base::Time(),
75 base::Time(),
76 0,
77 0,
78 DownloadItem::IN_PROGRESS,
79 false,
80 net::BoundNetLog()) {}
71 virtual ~MockDownloadItemImpl() {} 81 virtual ~MockDownloadItemImpl() {}
72 82
73 MOCK_METHOD4(OnDownloadTargetDetermined, 83 MOCK_METHOD4(OnDownloadTargetDetermined,
74 void(const FilePath&, TargetDisposition, 84 void(const FilePath&, TargetDisposition,
75 DownloadDangerType, const FilePath&)); 85 DownloadDangerType, const FilePath&));
76 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*)); 86 MOCK_METHOD1(AddObserver, void(DownloadItem::Observer*));
77 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*)); 87 MOCK_METHOD1(RemoveObserver, void(DownloadItem::Observer*));
78 MOCK_METHOD0(UpdateObservers, void()); 88 MOCK_METHOD0(UpdateObservers, void());
79 MOCK_METHOD0(CanShowInFolder, bool()); 89 MOCK_METHOD0(CanShowInFolder, bool());
80 MOCK_METHOD0(CanOpenDownload, bool()); 90 MOCK_METHOD0(CanOpenDownload, bool());
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 MOCK_CONST_METHOD0(GetReferrerCharset, std::string()); 135 MOCK_CONST_METHOD0(GetReferrerCharset, std::string());
126 MOCK_CONST_METHOD0(GetRemoteAddress, std::string()); 136 MOCK_CONST_METHOD0(GetRemoteAddress, std::string());
127 MOCK_CONST_METHOD0(GetTotalBytes, int64()); 137 MOCK_CONST_METHOD0(GetTotalBytes, int64());
128 MOCK_CONST_METHOD0(GetReceivedBytes, int64()); 138 MOCK_CONST_METHOD0(GetReceivedBytes, int64());
129 MOCK_CONST_METHOD0(GetHashState, const std::string&()); 139 MOCK_CONST_METHOD0(GetHashState, const std::string&());
130 MOCK_CONST_METHOD0(GetHash, const std::string&()); 140 MOCK_CONST_METHOD0(GetHash, const std::string&());
131 MOCK_CONST_METHOD0(GetId, int32()); 141 MOCK_CONST_METHOD0(GetId, int32());
132 MOCK_CONST_METHOD0(GetGlobalId, DownloadId()); 142 MOCK_CONST_METHOD0(GetGlobalId, DownloadId());
133 MOCK_CONST_METHOD0(GetStartTime, base::Time()); 143 MOCK_CONST_METHOD0(GetStartTime, base::Time());
134 MOCK_CONST_METHOD0(GetEndTime, base::Time()); 144 MOCK_CONST_METHOD0(GetEndTime, base::Time());
135 MOCK_METHOD0(SetIsPersisted, void());
136 MOCK_CONST_METHOD0(IsPersisted, bool());
137 MOCK_METHOD1(SetDbHandle, void(int64));
138 MOCK_CONST_METHOD0(GetDbHandle, int64());
139 MOCK_METHOD0(GetDownloadManager, DownloadManager*()); 145 MOCK_METHOD0(GetDownloadManager, DownloadManager*());
140 MOCK_CONST_METHOD0(IsPaused, bool()); 146 MOCK_CONST_METHOD0(IsPaused, bool());
141 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool()); 147 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool());
142 MOCK_METHOD1(SetOpenWhenComplete, void(bool)); 148 MOCK_METHOD1(SetOpenWhenComplete, void(bool));
143 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool()); 149 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool());
144 MOCK_CONST_METHOD0(GetSafetyState, SafetyState()); 150 MOCK_CONST_METHOD0(GetSafetyState, SafetyState());
145 MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType()); 151 MOCK_CONST_METHOD0(GetDangerType, DownloadDangerType());
146 MOCK_CONST_METHOD0(IsDangerous, bool()); 152 MOCK_CONST_METHOD0(IsDangerous, bool());
147 MOCK_METHOD0(GetAutoOpened, bool()); 153 MOCK_METHOD0(GetAutoOpened, bool());
148 MOCK_CONST_METHOD0(GetForcedFilePath, const FilePath&()); 154 MOCK_CONST_METHOD0(GetForcedFilePath, const FilePath&());
149 MOCK_CONST_METHOD0(HasUserGesture, bool()); 155 MOCK_CONST_METHOD0(HasUserGesture, bool());
150 MOCK_CONST_METHOD0(GetTransitionType, PageTransition()); 156 MOCK_CONST_METHOD0(GetTransitionType, PageTransition());
151 MOCK_CONST_METHOD0(IsTemporary, bool()); 157 MOCK_CONST_METHOD0(IsTemporary, bool());
152 MOCK_METHOD1(SetIsTemporary, void(bool)); 158 MOCK_METHOD1(SetIsTemporary, void(bool));
153 MOCK_METHOD1(SetOpened, void(bool)); 159 MOCK_METHOD1(SetOpened, void(bool));
154 MOCK_CONST_METHOD0(GetOpened, bool()); 160 MOCK_CONST_METHOD0(GetOpened, bool());
155 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 161 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
156 MOCK_CONST_METHOD0(GetETag, const std::string&()); 162 MOCK_CONST_METHOD0(GetETag, const std::string&());
157 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 163 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
158 MOCK_CONST_METHOD0(GetPersistentStoreInfo, DownloadPersistentStoreInfo());
159 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); 164 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
160 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); 165 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
161 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 166 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
162 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 167 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
163 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 168 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
164 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 169 MOCK_METHOD0(MockDownloadOpenForTesting, void());
165 // May be called when vlog is on. 170 // May be called when vlog is on.
166 virtual std::string DebugString(bool verbose) const OVERRIDE { return ""; } 171 virtual std::string DebugString(bool verbose) const OVERRIDE { return ""; }
167 }; 172 };
168 173
169 class MockDownloadManagerDelegate : public DownloadManagerDelegate { 174 class MockDownloadManagerDelegate : public DownloadManagerDelegate {
170 public: 175 public:
171 MockDownloadManagerDelegate(); 176 MockDownloadManagerDelegate();
172 virtual ~MockDownloadManagerDelegate(); 177 virtual ~MockDownloadManagerDelegate();
173 178
174 MOCK_METHOD0(Shutdown, void()); 179 MOCK_METHOD0(Shutdown, void());
175 MOCK_METHOD0(GetNextId, DownloadId()); 180 MOCK_METHOD0(GetNextId, DownloadId());
176 MOCK_METHOD2(DetermineDownloadTarget, 181 MOCK_METHOD2(DetermineDownloadTarget,
177 bool(DownloadItem* item, 182 bool(DownloadItem* item,
178 const DownloadTargetCallback&)); 183 const DownloadTargetCallback&));
179 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); 184 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*());
180 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 185 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
181 MOCK_METHOD2(ShouldCompleteDownload, 186 MOCK_METHOD2(ShouldCompleteDownload,
182 bool(DownloadItem*, const base::Closure&)); 187 bool(DownloadItem*, const base::Closure&));
183 MOCK_METHOD2(ShouldOpenDownload, 188 MOCK_METHOD2(ShouldOpenDownload,
184 bool(DownloadItem*, const DownloadOpenDelayedCallback&)); 189 bool(DownloadItem*, const DownloadOpenDelayedCallback&));
185 MOCK_METHOD0(GenerateFileHash, bool()); 190 MOCK_METHOD0(GenerateFileHash, bool());
186 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*));
187 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*));
188 MOCK_METHOD2(UpdatePathForItemInPersistentStore,
189 void(DownloadItem*, const FilePath&));
190 MOCK_METHOD1(RemoveItemFromPersistentStore, void(DownloadItem*));
191 MOCK_METHOD2(RemoveItemsFromPersistentStoreBetween, void(
192 base::Time remove_begin, base::Time remove_end));
193 MOCK_METHOD4(GetSaveDir, void(BrowserContext*, 191 MOCK_METHOD4(GetSaveDir, void(BrowserContext*,
194 FilePath*, FilePath*, bool*)); 192 FilePath*, FilePath*, bool*));
195 MOCK_METHOD5(ChooseSavePath, void( 193 MOCK_METHOD5(ChooseSavePath, void(
196 WebContents*, const FilePath&, const FilePath::StringType&, 194 WebContents*, const FilePath&, const FilePath::StringType&,
197 bool, const SavePackagePathPickedCallback&)); 195 bool, const SavePackagePathPickedCallback&));
198 }; 196 };
199 197
200 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 198 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
201 199
202 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 200 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
(...skipping 17 matching lines...) Expand all
220 MockDownloadItemImpl* PopItem(); 218 MockDownloadItemImpl* PopItem();
221 219
222 // Should be called when the item of this id is removed so that 220 // Should be called when the item of this id is removed so that
223 // we don't keep dangling pointers. 221 // we don't keep dangling pointers.
224 void RemoveItem(int id); 222 void RemoveItem(int id);
225 223
226 // Overridden methods from DownloadItemFactory. 224 // Overridden methods from DownloadItemFactory.
227 virtual DownloadItemImpl* CreatePersistedItem( 225 virtual DownloadItemImpl* CreatePersistedItem(
228 DownloadItemImplDelegate* delegate, 226 DownloadItemImplDelegate* delegate,
229 DownloadId download_id, 227 DownloadId download_id,
230 const DownloadPersistentStoreInfo& info, 228 const FilePath& path,
229 const GURL& url,
230 const GURL& referrer_url,
231 const base::Time& start_time,
232 const base::Time& end_time,
233 int64 received_bytes,
234 int64 total_bytes,
235 DownloadItem::DownloadState state,
236 bool opened,
231 const net::BoundNetLog& bound_net_log) OVERRIDE; 237 const net::BoundNetLog& bound_net_log) OVERRIDE;
232 virtual DownloadItemImpl* CreateActiveItem( 238 virtual DownloadItemImpl* CreateActiveItem(
233 DownloadItemImplDelegate* delegate, 239 DownloadItemImplDelegate* delegate,
234 const DownloadCreateInfo& info, 240 const DownloadCreateInfo& info,
235 scoped_ptr<DownloadRequestHandleInterface> request_handle, 241 scoped_ptr<DownloadRequestHandleInterface> request_handle,
236 const net::BoundNetLog& bound_net_log) OVERRIDE; 242 const net::BoundNetLog& bound_net_log) OVERRIDE;
237 virtual DownloadItemImpl* CreateSavePageItem( 243 virtual DownloadItemImpl* CreateSavePageItem(
238 DownloadItemImplDelegate* delegate, 244 DownloadItemImplDelegate* delegate,
239 const FilePath& path, 245 const FilePath& path,
240 const GURL& url, 246 const GURL& url,
(...skipping 30 matching lines...) Expand all
271 } 277 }
272 278
273 void MockDownloadItemFactory::RemoveItem(int id) { 279 void MockDownloadItemFactory::RemoveItem(int id) {
274 DCHECK(items_.find(id) != items_.end()); 280 DCHECK(items_.find(id) != items_.end());
275 items_.erase(id); 281 items_.erase(id);
276 } 282 }
277 283
278 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem( 284 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem(
279 DownloadItemImplDelegate* delegate, 285 DownloadItemImplDelegate* delegate,
280 DownloadId download_id, 286 DownloadId download_id,
281 const DownloadPersistentStoreInfo& info, 287 const FilePath& path,
288 const GURL& url,
289 const GURL& referrer_url,
290 const base::Time& start_time,
291 const base::Time& end_time,
292 int64 received_bytes,
293 int64 total_bytes,
294 DownloadItem::DownloadState state,
295 bool opened,
282 const net::BoundNetLog& bound_net_log) { 296 const net::BoundNetLog& bound_net_log) {
283 int local_id = download_id.local(); 297 int local_id = download_id.local();
284 DCHECK(items_.find(local_id) == items_.end()); 298 DCHECK(items_.find(local_id) == items_.end());
285 299
286 MockDownloadItemImpl* result = 300 MockDownloadItemImpl* result =
287 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 301 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
288 EXPECT_CALL(*result, GetId()) 302 EXPECT_CALL(*result, GetId())
289 .WillRepeatedly(Return(local_id)); 303 .WillRepeatedly(Return(local_id));
290 items_[local_id] = result; 304 items_[local_id] = result;
291 305
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 download_manager_->DetermineDownloadTarget( 550 download_manager_->DetermineDownloadTarget(
537 item, base::Bind( 551 item, base::Bind(
538 &DownloadManagerTest::DownloadTargetDeterminedCallback, 552 &DownloadManagerTest::DownloadTargetDeterminedCallback,
539 base::Unretained(this))); 553 base::Unretained(this)));
540 } 554 }
541 555
542 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 556 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
543 // For DCHECK in AddDownloadItemToHistory. Don't want to use 557 // For DCHECK in AddDownloadItemToHistory. Don't want to use
544 // WillRepeatedly as it may have to return true after this. 558 // WillRepeatedly as it may have to return true after this.
545 if (DCHECK_IS_ON()) 559 if (DCHECK_IS_ON())
546 EXPECT_CALL(item, IsPersisted())
547 .WillRepeatedly(Return(false));
548
549 EXPECT_CALL(item, SetDbHandle(db_handle));
550 EXPECT_CALL(item, SetIsPersisted());
551 EXPECT_CALL(item, GetDbHandle())
552 .WillRepeatedly(Return(db_handle));
553
554 // Null out ShowDownloadInBrowser 560 // Null out ShowDownloadInBrowser
555 EXPECT_CALL(item, GetWebContents()) 561 EXPECT_CALL(item, GetWebContents())
556 .WillOnce(Return(static_cast<WebContents*>(NULL))); 562 .WillOnce(Return(static_cast<WebContents*>(NULL)));
557 EXPECT_CALL(GetMockDownloadManagerDelegate(), 563 EXPECT_CALL(GetMockDownloadManagerDelegate(),
558 GetAlternativeWebContentsToNotifyForDownload()) 564 GetAlternativeWebContentsToNotifyForDownload())
559 .WillOnce(Return(static_cast<WebContents*>(NULL))); 565 .WillOnce(Return(static_cast<WebContents*>(NULL)));
560 566
561 EXPECT_CALL(item, IsInProgress()) 567 EXPECT_CALL(item, IsInProgress())
562 .WillOnce(Return(true)); 568 .WillOnce(Return(true));
563 569
564 // History addition should result in a call into MaybeCompleteDownload(). 570 // History addition should result in a call into MaybeCompleteDownload().
565 EXPECT_CALL(item, MaybeCompleteDownload()) 571 EXPECT_CALL(item, MaybeCompleteDownload())
566 .WillOnce(Return()); 572 .WillOnce(Return());
567
568 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
569 } 573 }
570 574
571 protected: 575 protected:
572 // Key test variable; we'll keep it available to sub-classes. 576 // Key test variable; we'll keep it available to sub-classes.
573 scoped_refptr<DownloadManagerImpl> download_manager_; 577 scoped_refptr<DownloadManagerImpl> download_manager_;
574 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; 578 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
575 579
576 // Target detetermined callback. 580 // Target detetermined callback.
577 bool callback_called_; 581 bool callback_called_;
578 FilePath target_path_; 582 FilePath target_path_;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 callback_called_ = false; 652 callback_called_ = false;
649 DetermineDownloadTarget(&item); 653 DetermineDownloadTarget(&item);
650 EXPECT_TRUE(callback_called_); 654 EXPECT_TRUE(callback_called_);
651 EXPECT_EQ(path, target_path_); 655 EXPECT_EQ(path, target_path_);
652 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_); 656 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_);
653 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_); 657 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_);
654 EXPECT_EQ(path, intermediate_path_); 658 EXPECT_EQ(path, intermediate_path_);
655 } 659 }
656 660
657 // Does DownloadStopped remove Download from appropriate queues? 661 // Does DownloadStopped remove Download from appropriate queues?
658 // This test tests non-persisted downloads. 662 TEST_F(DownloadManagerTest, OnDownloadStopped) {
659 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
660 // Put a mock we have a handle to on the download manager. 663 // Put a mock we have a handle to on the download manager.
661 MockDownloadItemImpl& item(AddItemToManager()); 664 MockDownloadItemImpl& item(AddItemToManager());
662 665
663 EXPECT_CALL(item, IsPersisted())
664 .WillRepeatedly(Return(false));
665 EXPECT_CALL(item, GetState()) 666 EXPECT_CALL(item, GetState())
666 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 667 .WillRepeatedly(Return(DownloadItem::CANCELLED));
667 EXPECT_CALL(item, GetDbHandle())
668 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
669 668
670 DownloadStopped(&item); 669 DownloadStopped(&item);
671 } 670 }
672
673 // Does DownloadStopped remove Download from appropriate queues?
674 // This test tests persisted downloads.
675 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
676 // Put a mock we have a handle to on the download manager.
677 MockDownloadItemImpl& item(AddItemToManager());
678 int64 db_handle = 0x7;
679 AddItemToHistory(item, db_handle);
680
681 EXPECT_CALL(item, IsPersisted())
682 .WillRepeatedly(Return(true));
683 EXPECT_CALL(GetMockDownloadManagerDelegate(),
684 UpdateItemInPersistentStore(&item));
685 EXPECT_CALL(item, GetState())
686 .WillRepeatedly(Return(DownloadItem::CANCELLED));
687 EXPECT_CALL(item, GetDbHandle())
688 .WillRepeatedly(Return(db_handle));
689
690 DownloadStopped(&item);
691 }
692 671
693 } // namespace content 672 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698