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

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

Powered by Google App Engine
This is Rietveld 408576698