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

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: @r165669 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_CONST_METHOD1(DebugString, std::string(bool));
167 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 171 MOCK_METHOD0(MockDownloadOpenForTesting, void());
172 // May be called when using --vmodule
173 virtual std::string DebugString(bool verbose) const OVERRIDE { return ""; }
168 }; 174 };
169 175
170 class MockDownloadManagerDelegate : public DownloadManagerDelegate { 176 class MockDownloadManagerDelegate : public DownloadManagerDelegate {
171 public: 177 public:
172 MockDownloadManagerDelegate(); 178 MockDownloadManagerDelegate();
173 virtual ~MockDownloadManagerDelegate(); 179 virtual ~MockDownloadManagerDelegate();
174 180
175 MOCK_METHOD0(Shutdown, void()); 181 MOCK_METHOD0(Shutdown, void());
176 MOCK_METHOD0(GetNextId, DownloadId()); 182 MOCK_METHOD0(GetNextId, DownloadId());
177 MOCK_METHOD2(DetermineDownloadTarget, 183 MOCK_METHOD2(DetermineDownloadTarget,
178 bool(DownloadItem* item, 184 bool(DownloadItem* item,
179 const DownloadTargetCallback&)); 185 const DownloadTargetCallback&));
180 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); 186 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*());
181 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 187 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
182 MOCK_METHOD2(ShouldCompleteDownload, 188 MOCK_METHOD2(ShouldCompleteDownload,
183 bool(DownloadItem*, const base::Closure&)); 189 bool(DownloadItem*, const base::Closure&));
184 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*)); 190 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*));
185 MOCK_METHOD0(GenerateFileHash, bool()); 191 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*, 192 MOCK_METHOD4(GetSaveDir, void(BrowserContext*,
194 FilePath*, FilePath*, bool*)); 193 FilePath*, FilePath*, bool*));
195 MOCK_METHOD5(ChooseSavePath, void( 194 MOCK_METHOD5(ChooseSavePath, void(
196 WebContents*, const FilePath&, const FilePath::StringType&, 195 WebContents*, const FilePath&, const FilePath::StringType&,
197 bool, const SavePackagePathPickedCallback&)); 196 bool, const SavePackagePathPickedCallback&));
198 }; 197 };
199 198
200 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 199 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
201 200
202 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 201 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
(...skipping 17 matching lines...) Expand all
220 MockDownloadItemImpl* PopItem(); 219 MockDownloadItemImpl* PopItem();
221 220
222 // 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
223 // we don't keep dangling pointers. 222 // we don't keep dangling pointers.
224 void RemoveItem(int id); 223 void RemoveItem(int id);
225 224
226 // Overridden methods from DownloadItemFactory. 225 // Overridden methods from DownloadItemFactory.
227 virtual DownloadItemImpl* CreatePersistedItem( 226 virtual DownloadItemImpl* CreatePersistedItem(
228 DownloadItemImplDelegate* delegate, 227 DownloadItemImplDelegate* delegate,
229 DownloadId download_id, 228 DownloadId download_id,
230 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,
231 const net::BoundNetLog& bound_net_log) OVERRIDE; 238 const net::BoundNetLog& bound_net_log) OVERRIDE;
232 virtual DownloadItemImpl* CreateActiveItem( 239 virtual DownloadItemImpl* CreateActiveItem(
233 DownloadItemImplDelegate* delegate, 240 DownloadItemImplDelegate* delegate,
234 const DownloadCreateInfo& info, 241 const DownloadCreateInfo& info,
235 scoped_ptr<DownloadRequestHandleInterface> request_handle, 242 scoped_ptr<DownloadRequestHandleInterface> request_handle,
236 const net::BoundNetLog& bound_net_log) OVERRIDE; 243 const net::BoundNetLog& bound_net_log) OVERRIDE;
237 virtual DownloadItemImpl* CreateSavePageItem( 244 virtual DownloadItemImpl* CreateSavePageItem(
238 DownloadItemImplDelegate* delegate, 245 DownloadItemImplDelegate* delegate,
239 const FilePath& path, 246 const FilePath& path,
240 const GURL& url, 247 const GURL& url,
(...skipping 30 matching lines...) Expand all
271 } 278 }
272 279
273 void MockDownloadItemFactory::RemoveItem(int id) { 280 void MockDownloadItemFactory::RemoveItem(int id) {
274 DCHECK(items_.find(id) != items_.end()); 281 DCHECK(items_.find(id) != items_.end());
275 items_.erase(id); 282 items_.erase(id);
276 } 283 }
277 284
278 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem( 285 DownloadItemImpl* MockDownloadItemFactory::CreatePersistedItem(
279 DownloadItemImplDelegate* delegate, 286 DownloadItemImplDelegate* delegate,
280 DownloadId download_id, 287 DownloadId download_id,
281 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,
282 const net::BoundNetLog& bound_net_log) { 297 const net::BoundNetLog& bound_net_log) {
283 int local_id = download_id.local(); 298 int local_id = download_id.local();
284 DCHECK(items_.find(local_id) == items_.end()); 299 DCHECK(items_.find(local_id) == items_.end());
285 300
286 MockDownloadItemImpl* result = 301 MockDownloadItemImpl* result =
287 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 302 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
288 EXPECT_CALL(*result, GetId()) 303 EXPECT_CALL(*result, GetId())
289 .WillRepeatedly(Return(local_id)); 304 .WillRepeatedly(Return(local_id));
290 items_[local_id] = result; 305 items_[local_id] = result;
291 306
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 download_manager_->DetermineDownloadTarget( 554 download_manager_->DetermineDownloadTarget(
540 item, base::Bind( 555 item, base::Bind(
541 &DownloadManagerTest::DownloadTargetDeterminedCallback, 556 &DownloadManagerTest::DownloadTargetDeterminedCallback,
542 base::Unretained(this))); 557 base::Unretained(this)));
543 } 558 }
544 559
545 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 560 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
546 // For DCHECK in AddDownloadItemToHistory. Don't want to use 561 // For DCHECK in AddDownloadItemToHistory. Don't want to use
547 // WillRepeatedly as it may have to return true after this. 562 // WillRepeatedly as it may have to return true after this.
548 if (DCHECK_IS_ON()) 563 if (DCHECK_IS_ON())
549 EXPECT_CALL(item, IsPersisted())
550 .WillRepeatedly(Return(false));
551
552 EXPECT_CALL(item, SetDbHandle(db_handle));
553 EXPECT_CALL(item, SetIsPersisted());
554 EXPECT_CALL(item, GetDbHandle())
555 .WillRepeatedly(Return(db_handle));
556
557 // Null out ShowDownloadInBrowser 564 // Null out ShowDownloadInBrowser
558 EXPECT_CALL(item, GetWebContents()) 565 EXPECT_CALL(item, GetWebContents())
559 .WillOnce(Return(static_cast<WebContents*>(NULL))); 566 .WillOnce(Return(static_cast<WebContents*>(NULL)));
560 EXPECT_CALL(GetMockDownloadManagerDelegate(), 567 EXPECT_CALL(GetMockDownloadManagerDelegate(),
561 GetAlternativeWebContentsToNotifyForDownload()) 568 GetAlternativeWebContentsToNotifyForDownload())
562 .WillOnce(Return(static_cast<WebContents*>(NULL))); 569 .WillOnce(Return(static_cast<WebContents*>(NULL)));
563 570
564 EXPECT_CALL(item, IsInProgress()) 571 EXPECT_CALL(item, IsInProgress())
565 .WillOnce(Return(true)); 572 .WillOnce(Return(true));
566 573
567 // History addition should result in a call into MaybeCompleteDownload(). 574 // History addition should result in a call into MaybeCompleteDownload().
568 EXPECT_CALL(item, MaybeCompleteDownload()) 575 EXPECT_CALL(item, MaybeCompleteDownload())
569 .WillOnce(Return()); 576 .WillOnce(Return());
570
571 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
572 } 577 }
573 578
574 protected: 579 protected:
575 // Key test variable; we'll keep it available to sub-classes. 580 // Key test variable; we'll keep it available to sub-classes.
576 scoped_refptr<DownloadManagerImpl> download_manager_; 581 scoped_refptr<DownloadManagerImpl> download_manager_;
577 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; 582 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
578 583
579 // Target detetermined callback. 584 // Target detetermined callback.
580 bool callback_called_; 585 bool callback_called_;
581 FilePath target_path_; 586 FilePath target_path_;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 callback_called_ = false; 656 callback_called_ = false;
652 DetermineDownloadTarget(&item); 657 DetermineDownloadTarget(&item);
653 EXPECT_TRUE(callback_called_); 658 EXPECT_TRUE(callback_called_);
654 EXPECT_EQ(path, target_path_); 659 EXPECT_EQ(path, target_path_);
655 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_); 660 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_);
656 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_); 661 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_);
657 EXPECT_EQ(path, intermediate_path_); 662 EXPECT_EQ(path, intermediate_path_);
658 } 663 }
659 664
660 // Does DownloadStopped remove Download from appropriate queues? 665 // Does DownloadStopped remove Download from appropriate queues?
661 // This test tests non-persisted downloads. 666 TEST_F(DownloadManagerTest, OnDownloadStopped) {
662 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
663 // Put a mock we have a handle to on the download manager. 667 // Put a mock we have a handle to on the download manager.
664 MockDownloadItemImpl& item(AddItemToManager()); 668 MockDownloadItemImpl& item(AddItemToManager());
665 669
666 EXPECT_CALL(item, IsPersisted())
667 .WillRepeatedly(Return(false));
668 EXPECT_CALL(item, GetState()) 670 EXPECT_CALL(item, GetState())
669 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 671 .WillRepeatedly(Return(DownloadItem::CANCELLED));
670 EXPECT_CALL(item, GetDbHandle())
671 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
672 672
673 DownloadStopped(&item); 673 DownloadStopped(&item);
674 } 674 }
675
676 // Does DownloadStopped remove Download from appropriate queues?
677 // This test tests persisted downloads.
678 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
679 // Put a mock we have a handle to on the download manager.
680 MockDownloadItemImpl& item(AddItemToManager());
681 int64 db_handle = 0x7;
682 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
683 .WillOnce(Return());
684 AddItemToHistory(item, db_handle);
685
686 EXPECT_CALL(item, IsPersisted())
687 .WillRepeatedly(Return(true));
688 EXPECT_CALL(GetMockDownloadManagerDelegate(),
689 UpdateItemInPersistentStore(&item));
690 EXPECT_CALL(item, GetState())
691 .WillRepeatedly(Return(DownloadItem::CANCELLED));
692 EXPECT_CALL(item, GetDbHandle())
693 .WillRepeatedly(Return(db_handle));
694
695 DownloadStopped(&item);
696 }
697 675
698 } // namespace content 676 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698