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

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

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 namespace content { 52 namespace content {
53 class ByteStreamReader; 53 class ByteStreamReader;
54 } 54 }
55 55
56 namespace { 56 namespace {
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(
63 content::DownloadPersistentStoreInfo(), 63 delegate,
64 net::BoundNetLog()) {} 64 content::DownloadId(),
65 FilePath(),
66 GURL(),
67 GURL(),
68 base::Time(),
69 base::Time(),
70 0,
71 0,
72 DownloadItem::IN_PROGRESS,
73 false,
74 net::BoundNetLog()) {}
65 virtual ~MockDownloadItemImpl() {} 75 virtual ~MockDownloadItemImpl() {}
66 76
67 MOCK_METHOD4(OnDownloadTargetDetermined, 77 MOCK_METHOD4(OnDownloadTargetDetermined,
68 void(const FilePath&, TargetDisposition, 78 void(const FilePath&, TargetDisposition,
69 content::DownloadDangerType, const FilePath&)); 79 content::DownloadDangerType, const FilePath&));
70 MOCK_METHOD1(AddObserver, void(content::DownloadItem::Observer*)); 80 MOCK_METHOD1(AddObserver, void(content::DownloadItem::Observer*));
71 MOCK_METHOD1(RemoveObserver, void(content::DownloadItem::Observer*)); 81 MOCK_METHOD1(RemoveObserver, void(content::DownloadItem::Observer*));
72 MOCK_METHOD0(UpdateObservers, void()); 82 MOCK_METHOD0(UpdateObservers, void());
73 MOCK_METHOD0(CanShowInFolder, bool()); 83 MOCK_METHOD0(CanShowInFolder, bool());
74 MOCK_METHOD0(CanOpenDownload, bool()); 84 MOCK_METHOD0(CanOpenDownload, bool());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 MOCK_CONST_METHOD0(GetReferrerCharset, std::string()); 125 MOCK_CONST_METHOD0(GetReferrerCharset, std::string());
116 MOCK_CONST_METHOD0(GetRemoteAddress, std::string()); 126 MOCK_CONST_METHOD0(GetRemoteAddress, std::string());
117 MOCK_CONST_METHOD0(GetTotalBytes, int64()); 127 MOCK_CONST_METHOD0(GetTotalBytes, int64());
118 MOCK_CONST_METHOD0(GetReceivedBytes, int64()); 128 MOCK_CONST_METHOD0(GetReceivedBytes, int64());
119 MOCK_CONST_METHOD0(GetHashState, const std::string&()); 129 MOCK_CONST_METHOD0(GetHashState, const std::string&());
120 MOCK_CONST_METHOD0(GetHash, const std::string&()); 130 MOCK_CONST_METHOD0(GetHash, const std::string&());
121 MOCK_CONST_METHOD0(GetId, int32()); 131 MOCK_CONST_METHOD0(GetId, int32());
122 MOCK_CONST_METHOD0(GetGlobalId, content::DownloadId()); 132 MOCK_CONST_METHOD0(GetGlobalId, content::DownloadId());
123 MOCK_CONST_METHOD0(GetStartTime, base::Time()); 133 MOCK_CONST_METHOD0(GetStartTime, base::Time());
124 MOCK_CONST_METHOD0(GetEndTime, base::Time()); 134 MOCK_CONST_METHOD0(GetEndTime, base::Time());
125 MOCK_METHOD0(SetIsPersisted, void());
126 MOCK_CONST_METHOD0(IsPersisted, bool());
127 MOCK_METHOD1(SetDbHandle, void(int64));
128 MOCK_CONST_METHOD0(GetDbHandle, int64());
129 MOCK_METHOD0(GetDownloadManager, content::DownloadManager*()); 135 MOCK_METHOD0(GetDownloadManager, content::DownloadManager*());
130 MOCK_CONST_METHOD0(IsPaused, bool()); 136 MOCK_CONST_METHOD0(IsPaused, bool());
131 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool()); 137 MOCK_CONST_METHOD0(GetOpenWhenComplete, bool());
132 MOCK_METHOD1(SetOpenWhenComplete, void(bool)); 138 MOCK_METHOD1(SetOpenWhenComplete, void(bool));
133 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool()); 139 MOCK_CONST_METHOD0(GetFileExternallyRemoved, bool());
134 MOCK_CONST_METHOD0(GetSafetyState, SafetyState()); 140 MOCK_CONST_METHOD0(GetSafetyState, SafetyState());
135 MOCK_CONST_METHOD0(GetDangerType, content::DownloadDangerType()); 141 MOCK_CONST_METHOD0(GetDangerType, content::DownloadDangerType());
136 MOCK_CONST_METHOD0(IsDangerous, bool()); 142 MOCK_CONST_METHOD0(IsDangerous, bool());
137 MOCK_METHOD0(GetAutoOpened, bool()); 143 MOCK_METHOD0(GetAutoOpened, bool());
138 MOCK_CONST_METHOD0(GetTargetName, FilePath()); 144 MOCK_CONST_METHOD0(GetTargetName, FilePath());
139 MOCK_CONST_METHOD0(GetForcedFilePath, const FilePath&()); 145 MOCK_CONST_METHOD0(GetForcedFilePath, const FilePath&());
140 MOCK_CONST_METHOD0(HasUserGesture, bool()); 146 MOCK_CONST_METHOD0(HasUserGesture, bool());
141 MOCK_CONST_METHOD0(GetTransitionType, content::PageTransition()); 147 MOCK_CONST_METHOD0(GetTransitionType, content::PageTransition());
142 MOCK_CONST_METHOD0(IsTemporary, bool()); 148 MOCK_CONST_METHOD0(IsTemporary, bool());
143 MOCK_METHOD1(SetIsTemporary, void(bool)); 149 MOCK_METHOD1(SetIsTemporary, void(bool));
144 MOCK_METHOD1(SetOpened, void(bool)); 150 MOCK_METHOD1(SetOpened, void(bool));
145 MOCK_CONST_METHOD0(GetOpened, bool()); 151 MOCK_CONST_METHOD0(GetOpened, bool());
146 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 152 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
147 MOCK_CONST_METHOD0(GetETag, const std::string&()); 153 MOCK_CONST_METHOD0(GetETag, const std::string&());
148 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 154 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
149 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
150 content::DownloadPersistentStoreInfo());
151 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 155 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
152 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 156 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
153 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 157 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
154 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 158 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
155 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 159 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
156 MOCK_METHOD0(OffThreadCancel, void()); 160 MOCK_METHOD0(OffThreadCancel, void());
157 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 161 MOCK_CONST_METHOD1(DebugString, std::string(bool));
158 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 162 MOCK_METHOD0(MockDownloadOpenForTesting, void());
159 }; 163 };
160 164
161 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 165 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
162 public: 166 public:
163 MockDownloadManagerDelegate(); 167 MockDownloadManagerDelegate();
164 virtual ~MockDownloadManagerDelegate(); 168 virtual ~MockDownloadManagerDelegate();
165 169
166 MOCK_METHOD0(Shutdown, void()); 170 MOCK_METHOD0(Shutdown, void());
167 MOCK_METHOD0(GetNextId, content::DownloadId()); 171 MOCK_METHOD0(GetNextId, content::DownloadId());
168 MOCK_METHOD2(DetermineDownloadTarget, 172 MOCK_METHOD2(DetermineDownloadTarget,
169 bool(DownloadItem* item, 173 bool(DownloadItem* item,
170 const content::DownloadTargetCallback&)); 174 const content::DownloadTargetCallback&));
171 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); 175 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*());
172 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 176 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
173 MOCK_METHOD2(ShouldCompleteDownload, bool( 177 MOCK_METHOD2(ShouldCompleteDownload, bool(
174 DownloadItem*, const base::Closure&)); 178 DownloadItem*, const base::Closure&));
175 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*)); 179 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*));
176 MOCK_METHOD0(GenerateFileHash, bool()); 180 MOCK_METHOD0(GenerateFileHash, bool());
177 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*));
178 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*));
179 MOCK_METHOD2(UpdatePathForItemInPersistentStore,
180 void(DownloadItem*, const FilePath&));
181 MOCK_METHOD1(RemoveItemFromPersistentStore, void(DownloadItem*));
182 MOCK_METHOD2(RemoveItemsFromPersistentStoreBetween, void(
183 base::Time remove_begin, base::Time remove_end));
184 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); 181 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*));
185 MOCK_METHOD5(ChooseSavePath, void( 182 MOCK_METHOD5(ChooseSavePath, void(
186 WebContents*, const FilePath&, const FilePath::StringType&, 183 WebContents*, const FilePath&, const FilePath::StringType&,
187 bool, const content::SavePackagePathPickedCallback&)); 184 bool, const content::SavePackagePathPickedCallback&));
188 }; 185 };
189 186
190 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 187 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
191 188
192 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 189 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
193 190
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 MockDownloadItemImpl* PopItem(); 253 MockDownloadItemImpl* PopItem();
257 254
258 // Should be called when the item of this id is removed so that 255 // Should be called when the item of this id is removed so that
259 // we don't keep dangling pointers. 256 // we don't keep dangling pointers.
260 void RemoveItem(int id); 257 void RemoveItem(int id);
261 258
262 // Overridden methods from DownloadItemFactory. 259 // Overridden methods from DownloadItemFactory.
263 virtual DownloadItemImpl* CreatePersistedItem( 260 virtual DownloadItemImpl* CreatePersistedItem(
264 DownloadItemImplDelegate* delegate, 261 DownloadItemImplDelegate* delegate,
265 content::DownloadId download_id, 262 content::DownloadId download_id,
266 const content::DownloadPersistentStoreInfo& info, 263 const FilePath& path,
264 const GURL& url,
265 const GURL& referrer_url,
266 const base::Time& start_time,
267 const base::Time& end_time,
268 int64 received_bytes,
269 int64 total_bytes,
270 DownloadItem::DownloadState state,
271 bool opened,
267 const net::BoundNetLog& bound_net_log) OVERRIDE; 272 const net::BoundNetLog& bound_net_log) OVERRIDE;
268 virtual DownloadItemImpl* CreateActiveItem( 273 virtual DownloadItemImpl* CreateActiveItem(
269 DownloadItemImplDelegate* delegate, 274 DownloadItemImplDelegate* delegate,
270 const DownloadCreateInfo& info, 275 const DownloadCreateInfo& info,
271 scoped_ptr<DownloadRequestHandleInterface> request_handle, 276 scoped_ptr<DownloadRequestHandleInterface> request_handle,
272 const net::BoundNetLog& bound_net_log) OVERRIDE; 277 const net::BoundNetLog& bound_net_log) OVERRIDE;
273 virtual DownloadItemImpl* CreateSavePageItem( 278 virtual DownloadItemImpl* CreateSavePageItem(
274 DownloadItemImplDelegate* delegate, 279 DownloadItemImplDelegate* delegate,
275 const FilePath& path, 280 const FilePath& path,
276 const GURL& url, 281 const GURL& url,
(...skipping 30 matching lines...) Expand all
307 } 312 }
308 313
309 void MockDownloadItemFactory::RemoveItem(int id) { 314 void MockDownloadItemFactory::RemoveItem(int id) {
310 DCHECK(items_.find(id) != items_.end()); 315 DCHECK(items_.find(id) != items_.end());
311 items_.erase(id); 316 items_.erase(id);
312 } 317 }
313 318
314 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem( 319 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem(
315 DownloadItemImplDelegate* delegate, 320 DownloadItemImplDelegate* delegate,
316 content::DownloadId download_id, 321 content::DownloadId download_id,
317 const content::DownloadPersistentStoreInfo& info, 322 const FilePath& path,
323 const GURL& url,
324 const GURL& referrer_url,
325 const base::Time& start_time,
326 const base::Time& end_time,
327 int64 received_bytes,
328 int64 total_bytes,
329 DownloadItem::DownloadState state,
330 bool opened,
318 const net::BoundNetLog& bound_net_log) { 331 const net::BoundNetLog& bound_net_log) {
319 int local_id = download_id.local(); 332 int local_id = download_id.local();
320 DCHECK(items_.find(local_id) == items_.end()); 333 DCHECK(items_.find(local_id) == items_.end());
321 334
322 MockDownloadItemImpl* result = 335 MockDownloadItemImpl* result =
323 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 336 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
324 EXPECT_CALL(*result, GetId()) 337 EXPECT_CALL(*result, GetId())
325 .WillRepeatedly(Return(local_id)); 338 .WillRepeatedly(Return(local_id));
326 items_[local_id] = result; 339 items_[local_id] = result;
327 340
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 521
509 MockDownloadManagerObserver& GetMockObserver() { 522 MockDownloadManagerObserver& GetMockObserver() {
510 return *observer_; 523 return *observer_;
511 } 524 }
512 525
513 // Probe at private internals. 526 // Probe at private internals.
514 void DownloadStopped(DownloadItemImpl* item) { 527 void DownloadStopped(DownloadItemImpl* item) {
515 download_manager_->DownloadStopped(item); 528 download_manager_->DownloadStopped(item);
516 } 529 }
517 530
518 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 531 void DelegateStart(DownloadItemImpl* item) {
519 // For DCHECK in AddDownloadItemToHistory. Don't want to use 532 download_manager_->DelegateStart(item);
520 // WillRepeatedly as it may have to return true after this.
521 if (DCHECK_IS_ON())
522 EXPECT_CALL(item, IsPersisted())
523 .WillRepeatedly(Return(false));
524
525 EXPECT_CALL(item, SetDbHandle(db_handle));
526 EXPECT_CALL(item, SetIsPersisted());
527 EXPECT_CALL(item, GetDbHandle())
528 .WillRepeatedly(Return(db_handle));
529
530 // Null out ShowDownloadInBrowser
531 EXPECT_CALL(item, GetWebContents())
532 .WillOnce(Return(static_cast<WebContents*>(NULL)));
533 EXPECT_CALL(GetMockDownloadManagerDelegate(),
534 GetAlternativeWebContentsToNotifyForDownload())
535 .WillOnce(Return(static_cast<WebContents*>(NULL)));
536
537 EXPECT_CALL(item, IsInProgress())
538 .WillOnce(Return(true));
539
540 // Null out MaybeCompleteDownload
541 EXPECT_CALL(item, AllDataSaved())
542 .WillOnce(Return(false));
543
544 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
545 } 533 }
546 534
547 protected: 535 protected:
548 // Key test variable; we'll keep it available to sub-classes. 536 // Key test variable; we'll keep it available to sub-classes.
549 scoped_refptr<DownloadManagerImpl> download_manager_; 537 scoped_refptr<DownloadManagerImpl> download_manager_;
550 538
551 private: 539 private:
552 MessageLoopForUI message_loop_; 540 MessageLoopForUI message_loop_;
553 content::TestBrowserThread ui_thread_; 541 content::TestBrowserThread ui_thread_;
554 content::TestBrowserThread file_thread_; 542 content::TestBrowserThread file_thread_;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 583
596 content::DownloadInterruptReason reason( 584 content::DownloadInterruptReason reason(
597 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 585 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
598 586
599 EXPECT_CALL(item, Interrupt(reason)); 587 EXPECT_CALL(item, Interrupt(reason));
600 download_manager_->OnDownloadInterrupted(download_id, reason); 588 download_manager_->OnDownloadInterrupted(download_id, reason);
601 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); 589 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id));
602 } 590 }
603 591
604 // Does DownloadStopped remove Download from appropriate queues? 592 // Does DownloadStopped remove Download from appropriate queues?
605 // This test tests non-persisted downloads. 593 TEST_F(DownloadManagerTest, OnDownloadStopped) {
606 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
607 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 594 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
608 .WillOnce(Return()); 595 .WillOnce(Return());
609 // Put a mock we have a handle to on the download manager. 596 // Put a mock we have a handle to on the download manager.
610 MockDownloadItemImpl& item(AddItemToManager()); 597 MockDownloadItemImpl& item(AddItemToManager());
611 598
612 EXPECT_CALL(item, IsPersisted())
613 .WillRepeatedly(Return(false));
614 EXPECT_CALL(item, GetState()) 599 EXPECT_CALL(item, GetState())
615 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 600 .WillRepeatedly(Return(DownloadItem::CANCELLED));
616 EXPECT_CALL(item, GetDbHandle())
617 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
618 601
619 EXPECT_CALL(item, OffThreadCancel()); 602 EXPECT_CALL(item, OffThreadCancel());
620 DownloadStopped(&item); 603 DownloadStopped(&item);
621 // TODO(rdsmith): Confirm that the download item is no longer on the 604 // TODO(rdsmith): Confirm that the download item is no longer on the
622 // active list by calling download_manager_->GetActiveDownloadItem(id). 605 // active list by calling download_manager_->GetActiveDownloadItem(id).
623 // Currently, the item is left on the active list for rendez-vous with 606 // Currently, the item is left on the active list for rendez-vous with
624 // the history system :-{. 607 // the history system :-{.
625 } 608 }
626
627 // Does DownloadStopped remove Download from appropriate queues?
628 // This test tests persisted downloads.
629 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
630 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
631 .WillOnce(Return());
632 // Put a mock we have a handle to on the download manager.
633 MockDownloadItemImpl& item(AddItemToManager());
634 int download_id = item.GetId();
635 int64 db_handle = 0x7;
636 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
637 .WillOnce(Return());
638 AddItemToHistory(item, db_handle);
639
640 EXPECT_CALL(item, IsPersisted())
641 .WillRepeatedly(Return(true));
642 EXPECT_CALL(GetMockDownloadManagerDelegate(),
643 UpdateItemInPersistentStore(&item));
644 EXPECT_CALL(item, GetState())
645 .WillRepeatedly(Return(DownloadItem::CANCELLED));
646 EXPECT_CALL(item, GetDbHandle())
647 .WillRepeatedly(Return(db_handle));
648
649 EXPECT_CALL(item, OffThreadCancel());
650 DownloadStopped(&item);
651 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
652 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698