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

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

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated comments. 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"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/browser/download/byte_stream.h" 19 #include "content/browser/download/byte_stream.h"
20 #include "content/browser/download/download_create_info.h" 20 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_file_manager.h"
22 #include "content/browser/download/download_item_factory.h" 21 #include "content/browser/download/download_item_factory.h"
23 #include "content/browser/download/download_item_impl.h" 22 #include "content/browser/download/download_item_impl.h"
24 #include "content/browser/download/download_item_impl_delegate.h" 23 #include "content/browser/download/download_item_impl_delegate.h"
25 #include "content/browser/download/download_manager_impl.h" 24 #include "content/browser/download/download_manager_impl.h"
26 #include "content/browser/download/download_request_handle.h" 25 #include "content/browser/download/download_request_handle.h"
27 #include "content/browser/download/mock_download_file.h" 26 #include "content/browser/download/mock_download_file.h"
28 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/browser_context.h"
29 #include "content/public/browser/download_interrupt_reasons.h" 28 #include "content/public/browser/download_interrupt_reasons.h"
30 #include "content/public/browser/download_item.h" 29 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager_delegate.h" 30 #include "content/public/browser/download_manager_delegate.h"
32 #include "content/public/test/mock_download_item.h" 31 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h" 32 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread.h" 33 #include "content/public/test/test_browser_thread.h"
34 #include "net/base/net_log.h"
35 #include "net/base/net_util.h" 35 #include "net/base/net_util.h"
36 #include "testing/gmock/include/gmock/gmock.h" 36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gmock_mutant.h" 37 #include "testing/gmock_mutant.h"
38 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
39 39
40 using ::testing::DoAll; 40 using ::testing::DoAll;
41 using ::testing::Ref; 41 using ::testing::Ref;
42 using ::testing::Return; 42 using ::testing::Return;
43 using ::testing::ReturnRef; 43 using ::testing::ReturnRef;
44 using ::testing::SetArgPointee; 44 using ::testing::SetArgPointee;
(...skipping 25 matching lines...) Expand all
70 MOCK_METHOD0(CanShowInFolder, bool()); 70 MOCK_METHOD0(CanShowInFolder, bool());
71 MOCK_METHOD0(CanOpenDownload, bool()); 71 MOCK_METHOD0(CanOpenDownload, bool());
72 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 72 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
73 MOCK_METHOD0(OpenDownload, void()); 73 MOCK_METHOD0(OpenDownload, void());
74 MOCK_METHOD0(ShowDownloadInShell, void()); 74 MOCK_METHOD0(ShowDownloadInShell, void());
75 MOCK_METHOD0(DangerousDownloadValidated, void()); 75 MOCK_METHOD0(DangerousDownloadValidated, void());
76 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 76 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
77 MOCK_METHOD1(Cancel, void(bool)); 77 MOCK_METHOD1(Cancel, void(bool));
78 MOCK_METHOD0(MarkAsComplete, void()); 78 MOCK_METHOD0(MarkAsComplete, void());
79 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 79 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
80 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); 80 MOCK_METHOD1(OnAllDataSaved, void(const std::string&));
81 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 81 MOCK_METHOD0(OnDownloadedFileRemoved, void());
82 MOCK_METHOD0(MaybeCompleteDownload, void()); 82 MOCK_METHOD0(MaybeCompleteDownload, void());
83 virtual void Start(
84 scoped_ptr<content::DownloadFile> download_file) {
benjhayden 2012/08/01 18:06:02 OVERRIDE?
Randy Smith (Not in Mondays) 2012/08/03 17:32:44 Ooops. Done.
85 MockStart(download_file.get());
86 }
87
88 MOCK_METHOD1(MockStart, void(content::DownloadFile*));
89
83 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 90 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
84 MOCK_METHOD1(Delete, void(DeleteReason)); 91 MOCK_METHOD1(Delete, void(DeleteReason));
85 MOCK_METHOD0(Remove, void()); 92 MOCK_METHOD0(Remove, void());
86 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 93 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
87 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 94 MOCK_CONST_METHOD0(CurrentSpeed, int64());
88 MOCK_CONST_METHOD0(PercentComplete, int()); 95 MOCK_CONST_METHOD0(PercentComplete, int());
89 MOCK_CONST_METHOD0(AllDataSaved, bool()); 96 MOCK_CONST_METHOD0(AllDataSaved, bool());
90 MOCK_METHOD0(TogglePause, void()); 97 MOCK_METHOD0(TogglePause, void());
91 MOCK_METHOD1(OnDownloadCompleting, void(DownloadFileManager*)); 98 MOCK_METHOD0(OnDownloadCompleting, void());
92 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 99 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
93 MOCK_CONST_METHOD0(IsPartialDownload, bool()); 100 MOCK_CONST_METHOD0(IsPartialDownload, bool());
94 MOCK_CONST_METHOD0(IsInProgress, bool()); 101 MOCK_CONST_METHOD0(IsInProgress, bool());
95 MOCK_CONST_METHOD0(IsCancelled, bool()); 102 MOCK_CONST_METHOD0(IsCancelled, bool());
96 MOCK_CONST_METHOD0(IsInterrupted, bool()); 103 MOCK_CONST_METHOD0(IsInterrupted, bool());
97 MOCK_CONST_METHOD0(IsComplete, bool()); 104 MOCK_CONST_METHOD0(IsComplete, bool());
98 MOCK_CONST_METHOD0(GetFullPath, const FilePath&()); 105 MOCK_CONST_METHOD0(GetFullPath, const FilePath&());
99 MOCK_CONST_METHOD0(GetTargetFilePath, const FilePath&()); 106 MOCK_CONST_METHOD0(GetTargetFilePath, const FilePath&());
100 MOCK_CONST_METHOD0(GetTargetDisposition, TargetDisposition()); 107 MOCK_CONST_METHOD0(GetTargetDisposition, TargetDisposition());
101 MOCK_METHOD3(OnTargetPathDetermined, void(const FilePath&, 108 MOCK_METHOD3(OnTargetPathDetermined, void(const FilePath&,
102 TargetDisposition, 109 TargetDisposition,
103 content::DownloadDangerType)); 110 content::DownloadDangerType));
104 MOCK_METHOD1(OnTargetPathSelected, void(const FilePath&)); 111 MOCK_METHOD1(OnTargetPathSelected, void(const FilePath&));
105 MOCK_METHOD1(OnContentCheckCompleted, void(content::DownloadDangerType)); 112 MOCK_METHOD1(OnContentCheckCompleted, void(content::DownloadDangerType));
106 MOCK_METHOD2(OnIntermediatePathDetermined, void(DownloadFileManager*, 113 MOCK_METHOD1(OnIntermediatePathDetermined, void(const FilePath&));
107 const FilePath&));
108 MOCK_CONST_METHOD0(GetState, DownloadState()); 114 MOCK_CONST_METHOD0(GetState, DownloadState());
109 MOCK_CONST_METHOD0(GetUrlChain, const std::vector<GURL>&()); 115 MOCK_CONST_METHOD0(GetUrlChain, const std::vector<GURL>&());
110 MOCK_METHOD1(SetTotalBytes, void(int64)); 116 MOCK_METHOD1(SetTotalBytes, void(int64));
111 MOCK_CONST_METHOD0(GetURL, const GURL&()); 117 MOCK_CONST_METHOD0(GetURL, const GURL&());
112 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&()); 118 MOCK_CONST_METHOD0(GetOriginalUrl, const GURL&());
113 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&()); 119 MOCK_CONST_METHOD0(GetReferrerUrl, const GURL&());
114 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string()); 120 MOCK_CONST_METHOD0(GetSuggestedFilename, std::string());
115 MOCK_CONST_METHOD0(GetContentDisposition, std::string()); 121 MOCK_CONST_METHOD0(GetContentDisposition, std::string());
116 MOCK_CONST_METHOD0(GetMimeType, std::string()); 122 MOCK_CONST_METHOD0(GetMimeType, std::string());
117 MOCK_CONST_METHOD0(GetOriginalMimeType, std::string()); 123 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&()); 156 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
151 MOCK_CONST_METHOD0(GetETag, const std::string&()); 157 MOCK_CONST_METHOD0(GetETag, const std::string&());
152 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 158 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
153 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 159 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
154 content::DownloadPersistentStoreInfo()); 160 content::DownloadPersistentStoreInfo());
155 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 161 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
156 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 162 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
157 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 163 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
158 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 164 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
159 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 165 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
160 MOCK_METHOD1(OffThreadCancel, void(DownloadFileManager* file_manager)); 166 MOCK_METHOD0(OffThreadCancel, void());
161 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 167 MOCK_CONST_METHOD1(DebugString, std::string(bool));
162 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 168 MOCK_METHOD0(MockDownloadOpenForTesting, void());
163 MOCK_METHOD1(GetExternalData, ExternalData*(const void*)); 169 MOCK_METHOD1(GetExternalData, ExternalData*(const void*));
164 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*)); 170 MOCK_CONST_METHOD1(GetExternalData, const ExternalData*(const void*));
165 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*)); 171 MOCK_METHOD2(SetExternalData, void(const void*, ExternalData*));
166 }; 172 };
167 173
168 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 174 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
169 public: 175 public:
170 MockDownloadManagerDelegate(); 176 MockDownloadManagerDelegate();
(...skipping 20 matching lines...) Expand all
191 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); 197 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*));
192 MOCK_METHOD5(ChooseSavePath, void( 198 MOCK_METHOD5(ChooseSavePath, void(
193 WebContents*, const FilePath&, const FilePath::StringType&, 199 WebContents*, const FilePath&, const FilePath::StringType&,
194 bool, const content::SavePackagePathPickedCallback&)); 200 bool, const content::SavePackagePathPickedCallback&));
195 }; 201 };
196 202
197 MockDownloadManagerDelegate::MockDownloadManagerDelegate() { } 203 MockDownloadManagerDelegate::MockDownloadManagerDelegate() { }
198 204
199 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() { } 205 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() { }
200 206
201 class MockDownloadFileManager : public DownloadFileManager { 207 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate {
202 public: 208 public:
203 MockDownloadFileManager(); 209 // Save to null this out even if it doesn't do anything because none
benjhayden 2012/08/01 18:06:02 Safe*?
Randy Smith (Not in Mondays) 2012/08/03 17:32:44 Done.
204 210 // of these functions will ever be called; this class just exists
205 void CreateDownloadFile( 211 // to have something to pass to the DownloadItemImpl base class
206 scoped_ptr<DownloadCreateInfo> info, 212 // of MockDownloadItemImpl.
207 scoped_ptr<content::ByteStreamReader> stream, 213 virtual void DelegateStart(DownloadItemImpl* download) {
benjhayden 2012/08/01 18:06:02 OVERRIDE?
Randy Smith (Not in Mondays) 2012/08/03 17:32:44 Done.
208 scoped_refptr<content::DownloadManager> download_manager, 214 NOTREACHED();
209 bool hash_needed,
210 const net::BoundNetLog& bound_net_log,
211 const CreateDownloadFileCallback& callback) OVERRIDE {
212 // Note that scoped_refptr<> on download manager is also stripped
213 // to make mock comparisons easier. Comparing the scoped_refptr<>
214 // works, but holds a reference to the DownloadManager until
215 // MockDownloadFileManager destruction, which messes up destruction
216 // testing.
217 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(),
218 hash_needed, bound_net_log, callback);
219 } 215 }
220
221 MOCK_METHOD6(MockCreateDownloadFile, void(
222 DownloadCreateInfo* info,
223 content::ByteStreamReader* stream,
224 content::DownloadManager* download_manager,
225 bool hash_needed,
226 const net::BoundNetLog& bound_net_log,
227 const CreateDownloadFileCallback& callback));
228 MOCK_METHOD0(Shutdown, void());
229 MOCK_METHOD1(CancelDownload, void(content::DownloadId));
230 MOCK_METHOD2(CompleteDownload, void(content::DownloadId,
231 const base::Closure&));
232 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*));
233 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId,
234 const FilePath&,
235 bool,
236 const RenameCompletionCallback&));
237 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int());
238 protected:
239 virtual ~MockDownloadFileManager();
240 }; 216 };
241 217
242 MockDownloadFileManager::MockDownloadFileManager()
243 : DownloadFileManager(NULL) { }
244
245 MockDownloadFileManager::~MockDownloadFileManager() { }
246
247 class MockDownloadItemFactory 218 class MockDownloadItemFactory
248 : public content::DownloadItemFactory, 219 : public content::DownloadItemFactory,
249 public base::SupportsWeakPtr<MockDownloadItemFactory> { 220 public base::SupportsWeakPtr<MockDownloadItemFactory> {
250 public: 221 public:
251 MockDownloadItemFactory(); 222 MockDownloadItemFactory();
252 virtual ~MockDownloadItemFactory(); 223 virtual ~MockDownloadItemFactory();
253 224
254 // Access to map of created items. 225 // Access to map of created items.
255 // TODO(rdsmith): Could add type (save page, persisted, etc.) 226 // TODO(rdsmith): Could add type (save page, persisted, etc.)
256 // functionality if it's ever needed by consumers. 227 // functionality if it's ever needed by consumers.
(...skipping 25 matching lines...) Expand all
282 DownloadItemImplDelegate* delegate, 253 DownloadItemImplDelegate* delegate,
283 const FilePath& path, 254 const FilePath& path,
284 const GURL& url, 255 const GURL& url,
285 bool is_otr, 256 bool is_otr,
286 content::DownloadId download_id, 257 content::DownloadId download_id,
287 const std::string& mime_type, 258 const std::string& mime_type,
288 const net::BoundNetLog& bound_net_log) OVERRIDE; 259 const net::BoundNetLog& bound_net_log) OVERRIDE;
289 260
290 private: 261 private:
291 std::map<int32, MockDownloadItemImpl*> items_; 262 std::map<int32, MockDownloadItemImpl*> items_;
292 DownloadItemImplDelegate item_delegate_; 263 NullDownloadItemImplDelegate item_delegate_;
293 264
294 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); 265 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
295 }; 266 };
296 267
297 MockDownloadItemFactory::MockDownloadItemFactory() {} 268 MockDownloadItemFactory::MockDownloadItemFactory() {}
298 269
299 MockDownloadItemFactory::~MockDownloadItemFactory() {} 270 MockDownloadItemFactory::~MockDownloadItemFactory() {}
300 271
301 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { 272 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
302 if (items_.find(id) == items_.end()) 273 if (items_.find(id) == items_.end())
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 scoped_ptr<DownloadRequestHandleInterface> request_handle, 314 scoped_ptr<DownloadRequestHandleInterface> request_handle,
344 bool is_otr, 315 bool is_otr,
345 const net::BoundNetLog& bound_net_log) { 316 const net::BoundNetLog& bound_net_log) {
346 int local_id = info.download_id.local(); 317 int local_id = info.download_id.local();
347 DCHECK(items_.find(local_id) == items_.end()); 318 DCHECK(items_.find(local_id) == items_.end());
348 319
349 MockDownloadItemImpl* result = 320 MockDownloadItemImpl* result =
350 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 321 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
351 EXPECT_CALL(*result, GetId()) 322 EXPECT_CALL(*result, GetId())
352 .WillRepeatedly(Return(local_id)); 323 .WillRepeatedly(Return(local_id));
324 EXPECT_CALL(*result, GetGlobalId())
325 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
353 items_[local_id] = result; 326 items_[local_id] = result;
354 327
328 // Active items are created and then immediately are called to start
329 // the download.
330 EXPECT_CALL(*result, MockStart(_));
331
355 return result; 332 return result;
356 } 333 }
357 334
358 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 335 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
359 DownloadItemImplDelegate* delegate, 336 DownloadItemImplDelegate* delegate,
360 const FilePath& path, 337 const FilePath& path,
361 const GURL& url, 338 const GURL& url,
362 bool is_otr, 339 bool is_otr,
363 content::DownloadId download_id, 340 content::DownloadId download_id,
364 const std::string& mime_type, 341 const std::string& mime_type,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 382
406 DownloadManagerTest() 383 DownloadManagerTest()
407 : ui_thread_(content::BrowserThread::UI, &message_loop_), 384 : ui_thread_(content::BrowserThread::UI, &message_loop_),
408 file_thread_(content::BrowserThread::FILE, &message_loop_), 385 file_thread_(content::BrowserThread::FILE, &message_loop_),
409 next_download_id_(0) { 386 next_download_id_(0) {
410 } 387 }
411 388
412 // We tear down everything in TearDown(). 389 // We tear down everything in TearDown().
413 ~DownloadManagerTest() {} 390 ~DownloadManagerTest() {}
414 391
415 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, 392 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
416 // and MockDownloadFileManager, then create a DownloadManager that points 393 // then create a DownloadManager that points
417 // at all of those. 394 // at all of those.
418 virtual void SetUp() { 395 virtual void SetUp() {
419 DCHECK(!download_manager_.get()); 396 DCHECK(!download_manager_.get());
420 397
421 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 398 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
422 mock_download_manager_delegate_.reset( 399 mock_download_manager_delegate_.reset(
423 new StrictMock<MockDownloadManagerDelegate>); 400 new StrictMock<MockDownloadManagerDelegate>);
424 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 401 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
425 .WillOnce(Return()); 402 .WillOnce(Return());
426 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
427 EXPECT_CALL(*mock_download_file_manager_.get(),
428 OnDownloadManagerShutdown(_));
429 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 403 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
430 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 404 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
431 .WillRepeatedly(Return(false)); 405 .WillRepeatedly(Return(false));
432 406
433 download_manager_ = new DownloadManagerImpl( 407 download_manager_ = new DownloadManagerImpl(
434 mock_download_file_manager_.get(),
435 scoped_ptr<content::DownloadItemFactory>( 408 scoped_ptr<content::DownloadItemFactory>(
436 mock_download_item_factory_.get()).Pass(), NULL); 409 mock_download_item_factory_.get()).Pass(), NULL);
437 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 410 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
438 download_manager_->Init(mock_browser_context_.get()); 411 download_manager_->Init(mock_browser_context_.get());
439 } 412 }
440 413
441 virtual void TearDown() { 414 virtual void TearDown() {
442 while (MockDownloadItemImpl* 415 while (MockDownloadItemImpl*
443 item = mock_download_item_factory_->PopItem()) { 416 item = mock_download_item_factory_->PopItem()) {
444 EXPECT_CALL(*item, GetSafetyState()) 417 EXPECT_CALL(*item, GetSafetyState())
445 .WillOnce(Return(content::DownloadItem::SAFE)); 418 .WillOnce(Return(content::DownloadItem::SAFE));
446 EXPECT_CALL(*item, IsPartialDownload()) 419 EXPECT_CALL(*item, IsPartialDownload())
447 .WillOnce(Return(false)); 420 .WillOnce(Return(false));
448 } 421 }
449 422
450 download_manager_->Shutdown(); 423 download_manager_->Shutdown();
451 download_manager_ = NULL; 424 download_manager_ = NULL;
452 message_loop_.RunAllPending(); 425 message_loop_.RunAllPending();
453 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 426 ASSERT_EQ(NULL, mock_download_item_factory_.get());
454 message_loop_.RunAllPending(); 427 message_loop_.RunAllPending();
455 mock_download_manager_delegate_.reset(); 428 mock_download_manager_delegate_.reset();
456 mock_download_file_manager_ = NULL;
457 mock_browser_context_.reset(); 429 mock_browser_context_.reset();
458 } 430 }
459 431
460 // Returns download id. 432 // Returns download id.
461 MockDownloadItemImpl& AddItemToManager() { 433 MockDownloadItemImpl& AddItemToManager() {
462 DownloadCreateInfo info; 434 DownloadCreateInfo info;
463 435
464 static const char* kDownloadIdDomain = "Test download id domain"; 436 static const char* kDownloadIdDomain = "Test download id domain";
465 437
466 // Args are ignored except for download id, so everything else can be 438 // Args are ignored except for download id, so everything else can be
467 // null. 439 // null.
468 int id = next_download_id_; 440 int id = next_download_id_;
469 ++next_download_id_; 441 ++next_download_id_;
470 info.download_id = content::DownloadId(kDownloadIdDomain, id); 442 info.download_id = content::DownloadId(kDownloadIdDomain, id);
471 info.request_handle = DownloadRequestHandle(); 443 info.request_handle = DownloadRequestHandle();
472 download_manager_->CreateDownloadItem(&info); 444 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
473 445
474 DCHECK(mock_download_item_factory_->GetItem(id)); 446 DCHECK(mock_download_item_factory_->GetItem(id));
475 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 447 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
476 ON_CALL(item, GetId()) 448 // Satisfy expectation. If the item is created in StartDownload(),
477 .WillByDefault(Return(id)); 449 // we call Start on it immediately, so we need to set that expectation
450 // in the factory.
451 item.Start(scoped_ptr<content::DownloadFile>());
478 452
479 return item; 453 return item;
480 } 454 }
481 455
482 MockDownloadItemImpl& GetMockDownloadItem(int id) { 456 MockDownloadItemImpl& GetMockDownloadItem(int id) {
483 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 457 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
484 458
485 DCHECK(itemp); 459 DCHECK(itemp);
486 return *itemp; 460 return *itemp;
487 } 461 }
488 462
489 void RemoveMockDownloadItem(int id) { 463 void RemoveMockDownloadItem(int id) {
490 // Owned by DownloadManager; should be deleted there. 464 // Owned by DownloadManager; should be deleted there.
491 mock_download_item_factory_->RemoveItem(id); 465 mock_download_item_factory_->RemoveItem(id);
492 } 466 }
493 467
494 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 468 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
495 return *mock_download_manager_delegate_; 469 return *mock_download_manager_delegate_;
496 } 470 }
497 471
498 MockDownloadFileManager& GetMockDownloadFileManager() {
499 return *mock_download_file_manager_;
500 }
501
502 // Probe at private internals. 472 // Probe at private internals.
503 void DownloadStopped(DownloadItemImpl* item) { 473 void DownloadStopped(DownloadItemImpl* item) {
504 download_manager_->DownloadStopped(item); 474 download_manager_->DownloadStopped(item);
505 } 475 }
506 476
477 void DelegateStart(DownloadItemImpl* item) {
478 download_manager_->DelegateStart(item);
479 }
480
507 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 481 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
508 // For DCHECK in AddDownloadItemToHistory. Don't want to use 482 // For DCHECK in AddDownloadItemToHistory. Don't want to use
509 // WillRepeatedly as it may have to return true after this. 483 // WillRepeatedly as it may have to return true after this.
510 if (DCHECK_IS_ON()) 484 if (DCHECK_IS_ON())
511 EXPECT_CALL(item, IsPersisted()) 485 EXPECT_CALL(item, IsPersisted())
512 .WillRepeatedly(Return(false)); 486 .WillRepeatedly(Return(false));
513 487
514 EXPECT_CALL(item, SetDbHandle(db_handle)); 488 EXPECT_CALL(item, SetDbHandle(db_handle));
515 EXPECT_CALL(item, SetIsPersisted()); 489 EXPECT_CALL(item, SetIsPersisted());
516 EXPECT_CALL(item, GetDbHandle()) 490 EXPECT_CALL(item, GetDbHandle())
(...skipping 19 matching lines...) Expand all
536 protected: 510 protected:
537 // Key test variable; we'll keep it available to sub-classes. 511 // Key test variable; we'll keep it available to sub-classes.
538 scoped_refptr<DownloadManagerImpl> download_manager_; 512 scoped_refptr<DownloadManagerImpl> download_manager_;
539 513
540 private: 514 private:
541 MessageLoopForUI message_loop_; 515 MessageLoopForUI message_loop_;
542 content::TestBrowserThread ui_thread_; 516 content::TestBrowserThread ui_thread_;
543 content::TestBrowserThread file_thread_; 517 content::TestBrowserThread file_thread_;
544 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 518 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
545 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 519 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
546 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
547 scoped_ptr<MockBrowserContext> mock_browser_context_; 520 scoped_ptr<MockBrowserContext> mock_browser_context_;
548 int next_download_id_; 521 int next_download_id_;
549 522
550 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 523 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
551 }; 524 };
552 525
553 // Confirm the appropriate invocations occur when you start a download. 526 // Confirm the appropriate invocations occur when you start a download.
554 TEST_F(DownloadManagerTest, StartDownload) { 527 TEST_F(DownloadManagerTest, StartDownload) {
555 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 528 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
556 scoped_ptr<content::ByteStreamReader> stream; 529 scoped_ptr<content::ByteStreamReader> stream;
557 int32 local_id(5); // Random value 530 int32 local_id(5); // Random value
558 531
559 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); 532 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id));
560 533
561 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 534 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
562 .WillOnce(Return(content::DownloadId(this, local_id))); 535 .WillOnce(Return(content::DownloadId(this, local_id)));
563 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 536 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
564 .WillOnce(Return(true)); 537 .WillOnce(Return(true));
565 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
566 info.get(), static_cast<content::ByteStreamReader*>(NULL),
567 download_manager_.get(), true, _, _));
568 538
569 download_manager_->StartDownload(info.Pass(), stream.Pass()); 539 download_manager_->StartDownload(info.Pass(), stream.Pass());
570 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 540 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
571 } 541 }
572 542
573 // Does the DownloadManager prompt when requested? 543 // Confirm that calling DelegateStart behaves properly if the delegate
544 // blocks starting.
545 TEST_F(DownloadManagerTest, DelegateStart_False) {
546 // Put a mock we have a handle to on the download manager.
547 MockDownloadItemImpl& item(AddItemToManager());
548 int download_id = item.GetId();
549
550 EXPECT_CALL(GetMockDownloadManagerDelegate(),
551 ShouldStartDownload(download_id))
552 .WillOnce(Return(false));
553 DelegateStart(&item);
554 }
555
556 // Does the DownloadManager prompt when requested? Lumping in
557 // test for DelegateStart when the delegate doesn't block the download in here.
574 TEST_F(DownloadManagerTest, RestartDownload) { 558 TEST_F(DownloadManagerTest, RestartDownload) {
575 // Put a mock we have a handle to on the download manager. 559 // Put a mock we have a handle to on the download manager.
576 MockDownloadItemImpl& item(AddItemToManager()); 560 MockDownloadItemImpl& item(AddItemToManager());
577 int download_id = item.GetId(); 561 int download_id = item.GetId();
578 562
579 // Confirm we're internally consistent. 563 // Confirm we're internally consistent.
580 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); 564 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id));
581 565
582 ScopedTempDir download_dir; 566 ScopedTempDir download_dir;
583 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); 567 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
584 FilePath expected_path(download_dir.path().Append( 568 FilePath expected_path(download_dir.path().Append(
585 FILE_PATH_LITERAL("location"))); 569 FILE_PATH_LITERAL("location")));
586 570
571 EXPECT_CALL(GetMockDownloadManagerDelegate(),
572 ShouldStartDownload(download_id))
573 .WillOnce(Return(true));
587 EXPECT_CALL(item, GetTargetDisposition()) 574 EXPECT_CALL(item, GetTargetDisposition())
588 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); 575 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_PROMPT));
589 EXPECT_CALL(GetMockDownloadManagerDelegate(), ChooseDownloadPath(&item)); 576 EXPECT_CALL(GetMockDownloadManagerDelegate(), ChooseDownloadPath(&item));
590 download_manager_->RestartDownload(download_id); 577 DelegateStart(&item);
591 578
592 // The alternative pathway goes straight to OnTargetPathAvailable, 579 // The alternative pathway goes straight to OnTargetPathAvailable,
593 // so it more naturally belongs below. 580 // so it more naturally belongs below.
594 } 581 }
595 582
596 // Do the results of GetIntermediatePath get passed through to the 583 // Do the results of GetIntermediatePath get passed through to the
597 // download? Note that this path is tested from RestartDownload 584 // download? Note that this path is tested from RestartDownload
598 // to test the non-prompting path in RestartDownload as well. 585 // to test the non-prompting path in RestartDownload as well.
599 TEST_F(DownloadManagerTest, OnTargetPathAvailable) { 586 TEST_F(DownloadManagerTest, OnTargetPathAvailable) {
600 // Put a mock we have a handle to on the download manager. 587 // Put a mock we have a handle to on the download manager.
601 MockDownloadItemImpl& item(AddItemToManager()); 588 MockDownloadItemImpl& item(AddItemToManager());
602 589
603 ScopedTempDir download_dir; 590 ScopedTempDir download_dir;
604 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); 591 ASSERT_TRUE(download_dir.CreateUniqueTempDir());
605 FilePath target_path(download_dir.path().Append( 592 FilePath target_path(download_dir.path().Append(
606 FILE_PATH_LITERAL("location"))); 593 FILE_PATH_LITERAL("location")));
607 FilePath intermediate_path(download_dir.path().Append( 594 FilePath intermediate_path(download_dir.path().Append(
608 FILE_PATH_LITERAL("location.crdownload"))); 595 FILE_PATH_LITERAL("location.crdownload")));
609 596
610 EXPECT_CALL(item, GetTargetDisposition()) 597 EXPECT_CALL(item, GetTargetDisposition())
611 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); 598 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE));
612 EXPECT_CALL(GetMockDownloadManagerDelegate(), 599 EXPECT_CALL(GetMockDownloadManagerDelegate(),
613 GetIntermediatePath(Ref(item))) 600 GetIntermediatePath(Ref(item)))
614 .WillOnce(Return(intermediate_path)); 601 .WillOnce(Return(intermediate_path));
615 // Finesse DCHECK with WillRepeatedly. 602 // Finesse DCHECK with WillRepeatedly.
616 EXPECT_CALL(item, GetTargetFilePath()) 603 EXPECT_CALL(item, GetTargetFilePath())
617 .WillRepeatedly(ReturnRef(target_path)); 604 .WillRepeatedly(ReturnRef(target_path));
618 EXPECT_CALL(item, OnIntermediatePathDetermined( 605 EXPECT_CALL(item, OnIntermediatePathDetermined(
619 &GetMockDownloadFileManager(), intermediate_path)); 606 intermediate_path));
620 download_manager_->RestartDownload(item.GetId()); 607 download_manager_->RestartDownload(item.GetId());
621 } 608 }
622 609
623 // Do the results of an OnDownloadInterrupted get passed through properly
624 // to the DownloadItem?
625 TEST_F(DownloadManagerTest, OnDownloadInterrupted) {
626 // Put a mock we have a handle to on the download manager.
627 MockDownloadItemImpl& item(AddItemToManager());
628 int download_id = item.GetId();
629
630 content::DownloadInterruptReason reason(
631 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
632
633 EXPECT_CALL(item, Interrupt(reason));
634 download_manager_->OnDownloadInterrupted(download_id, reason);
635 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id));
636 }
637
638 // Does DownloadStopped remove Download from appropriate queues? 610 // Does DownloadStopped remove Download from appropriate queues?
639 // This test tests non-persisted downloads. 611 // This test tests non-persisted downloads.
640 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { 612 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
641 // Put a mock we have a handle to on the download manager. 613 // Put a mock we have a handle to on the download manager.
642 MockDownloadItemImpl& item(AddItemToManager()); 614 MockDownloadItemImpl& item(AddItemToManager());
643 615
644 EXPECT_CALL(item, IsPersisted()) 616 EXPECT_CALL(item, IsPersisted())
645 .WillRepeatedly(Return(false)); 617 .WillRepeatedly(Return(false));
646 EXPECT_CALL(item, GetState()) 618 EXPECT_CALL(item, GetState())
647 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 619 .WillRepeatedly(Return(DownloadItem::CANCELLED));
648 EXPECT_CALL(item, GetDbHandle()) 620 EXPECT_CALL(item, GetDbHandle())
649 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 621 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
650 622
651 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); 623 EXPECT_CALL(item, OffThreadCancel());
652 DownloadStopped(&item); 624 DownloadStopped(&item);
653 // TODO(rdsmith): Confirm that the download item is no longer on the 625 // TODO(rdsmith): Confirm that the download item is no longer on the
654 // active list by calling download_manager_->GetActiveDownloadItem(id). 626 // active list by calling download_manager_->GetActiveDownloadItem(id).
655 // Currently, the item is left on the active list for rendez-vous with 627 // Currently, the item is left on the active list for rendez-vous with
656 // the history system :-{. 628 // the history system :-{.
657 } 629 }
658 630
659 // Does DownloadStopped remove Download from appropriate queues? 631 // Does DownloadStopped remove Download from appropriate queues?
660 // This test tests persisted downloads. 632 // This test tests persisted downloads.
661 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 633 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
662 // Put a mock we have a handle to on the download manager. 634 // Put a mock we have a handle to on the download manager.
663 MockDownloadItemImpl& item(AddItemToManager()); 635 MockDownloadItemImpl& item(AddItemToManager());
664 int download_id = item.GetId(); 636 int download_id = item.GetId();
665 int64 db_handle = 0x7; 637 int64 db_handle = 0x7;
666 EXPECT_CALL(item, GetExternalData(_)) 638 EXPECT_CALL(item, GetExternalData(_))
667 .WillOnce(Return(static_cast<DownloadItem::ExternalData*>(NULL))); 639 .WillOnce(Return(static_cast<DownloadItem::ExternalData*>(NULL)));
668 AddItemToHistory(item, db_handle); 640 AddItemToHistory(item, db_handle);
669 641
670 EXPECT_CALL(item, IsPersisted()) 642 EXPECT_CALL(item, IsPersisted())
671 .WillRepeatedly(Return(true)); 643 .WillRepeatedly(Return(true));
672 EXPECT_CALL(GetMockDownloadManagerDelegate(), 644 EXPECT_CALL(GetMockDownloadManagerDelegate(),
673 UpdateItemInPersistentStore(&item)); 645 UpdateItemInPersistentStore(&item));
674 EXPECT_CALL(item, GetState()) 646 EXPECT_CALL(item, GetState())
675 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 647 .WillRepeatedly(Return(DownloadItem::CANCELLED));
676 EXPECT_CALL(item, GetDbHandle()) 648 EXPECT_CALL(item, GetDbHandle())
677 .WillRepeatedly(Return(db_handle)); 649 .WillRepeatedly(Return(db_handle));
678 650
679 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); 651 EXPECT_CALL(item, OffThreadCancel());
680 DownloadStopped(&item); 652 DownloadStopped(&item);
681 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 653 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
682 } 654 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698