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

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

Issue 10912173: Replace the DownloadFileManager with direct ownership of DownloadFileImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to LKGR (r159273) Created 8 years, 2 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" 21 #include "content/browser/download/download_file_factory.h"
22 #include "content/browser/download/download_item_factory.h" 22 #include "content/browser/download/download_item_factory.h"
23 #include "content/browser/download/download_item_impl.h" 23 #include "content/browser/download/download_item_impl.h"
24 #include "content/browser/download/download_item_impl_delegate.h" 24 #include "content/browser/download/download_item_impl_delegate.h"
25 #include "content/browser/download/download_manager_impl.h" 25 #include "content/browser/download/download_manager_impl.h"
26 #include "content/browser/download/download_request_handle.h" 26 #include "content/browser/download/download_request_handle.h"
27 #include "content/browser/download/mock_download_file.h" 27 #include "content/browser/download/mock_download_file.h"
28 #include "content/public/browser/browser_context.h" 28 #include "content/public/browser/browser_context.h"
29 #include "content/public/browser/download_interrupt_reasons.h" 29 #include "content/public/browser/download_interrupt_reasons.h"
30 #include "content/public/browser/download_item.h" 30 #include "content/public/browser/download_item.h"
31 #include "content/public/browser/download_manager_delegate.h" 31 #include "content/public/browser/download_manager_delegate.h"
32 #include "content/public/test/mock_download_item.h" 32 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread.h" 34 #include "content/public/test/test_browser_thread.h"
35 #include "net/base/net_log.h"
35 #include "net/base/net_util.h" 36 #include "net/base/net_util.h"
36 #include "testing/gmock/include/gmock/gmock.h" 37 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gmock_mutant.h" 38 #include "testing/gmock_mutant.h"
38 #include "testing/gtest/include/gtest/gtest.h" 39 #include "testing/gtest/include/gtest/gtest.h"
39 40
40 using ::testing::AllOf; 41 using ::testing::AllOf;
41 using ::testing::DoAll; 42 using ::testing::DoAll;
42 using ::testing::Eq; 43 using ::testing::Eq;
43 using ::testing::Ref; 44 using ::testing::Ref;
44 using ::testing::Return; 45 using ::testing::Return;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 MOCK_METHOD0(CanShowInFolder, bool()); 83 MOCK_METHOD0(CanShowInFolder, bool());
83 MOCK_METHOD0(CanOpenDownload, bool()); 84 MOCK_METHOD0(CanOpenDownload, bool());
84 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 85 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
85 MOCK_METHOD0(OpenDownload, void()); 86 MOCK_METHOD0(OpenDownload, void());
86 MOCK_METHOD0(ShowDownloadInShell, void()); 87 MOCK_METHOD0(ShowDownloadInShell, void());
87 MOCK_METHOD0(DangerousDownloadValidated, void()); 88 MOCK_METHOD0(DangerousDownloadValidated, void());
88 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 89 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
89 MOCK_METHOD1(Cancel, void(bool)); 90 MOCK_METHOD1(Cancel, void(bool));
90 MOCK_METHOD0(MarkAsComplete, void()); 91 MOCK_METHOD0(MarkAsComplete, void());
91 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 92 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
92 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); 93 MOCK_METHOD1(OnAllDataSaved, void(const std::string&));
93 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 94 MOCK_METHOD0(OnDownloadedFileRemoved, void());
94 MOCK_METHOD0(MaybeCompleteDownload, void()); 95 MOCK_METHOD0(MaybeCompleteDownload, void());
96 virtual void Start(
97 scoped_ptr<content::DownloadFile> download_file) OVERRIDE {
98 MockStart(download_file.get());
99 }
100
101 MOCK_METHOD1(MockStart, void(content::DownloadFile*));
102
95 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 103 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
96 MOCK_METHOD1(Delete, void(DeleteReason)); 104 MOCK_METHOD1(Delete, void(DeleteReason));
97 MOCK_METHOD0(Remove, void()); 105 MOCK_METHOD0(Remove, void());
98 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 106 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
99 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 107 MOCK_CONST_METHOD0(CurrentSpeed, int64());
100 MOCK_CONST_METHOD0(PercentComplete, int()); 108 MOCK_CONST_METHOD0(PercentComplete, int());
101 MOCK_CONST_METHOD0(AllDataSaved, bool()); 109 MOCK_CONST_METHOD0(AllDataSaved, bool());
102 MOCK_METHOD0(TogglePause, void()); 110 MOCK_METHOD0(TogglePause, void());
103 MOCK_METHOD0(OnDownloadCompleting, void()); 111 MOCK_METHOD0(OnDownloadCompleting, void());
104 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 112 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 162 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
155 MOCK_CONST_METHOD0(GetETag, const std::string&()); 163 MOCK_CONST_METHOD0(GetETag, const std::string&());
156 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 164 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
157 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 165 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
158 content::DownloadPersistentStoreInfo()); 166 content::DownloadPersistentStoreInfo());
159 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 167 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
160 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 168 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
161 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 169 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
162 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 170 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
163 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 171 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
164 MOCK_METHOD0(OffThreadCancel, void());
165 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 172 MOCK_CONST_METHOD1(DebugString, std::string(bool));
166 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 173 MOCK_METHOD0(MockDownloadOpenForTesting, void());
167 }; 174 };
168 175
169 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 176 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
170 public: 177 public:
171 MockDownloadManagerDelegate(); 178 MockDownloadManagerDelegate();
172 virtual ~MockDownloadManagerDelegate(); 179 virtual ~MockDownloadManagerDelegate();
173 180
174 MOCK_METHOD0(Shutdown, void()); 181 MOCK_METHOD0(Shutdown, void());
(...skipping 18 matching lines...) Expand all
193 FilePath*, FilePath*, bool*)); 200 FilePath*, FilePath*, bool*));
194 MOCK_METHOD5(ChooseSavePath, void( 201 MOCK_METHOD5(ChooseSavePath, void(
195 WebContents*, const FilePath&, const FilePath::StringType&, 202 WebContents*, const FilePath&, const FilePath::StringType&,
196 bool, const content::SavePackagePathPickedCallback&)); 203 bool, const content::SavePackagePathPickedCallback&));
197 }; 204 };
198 205
199 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 206 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
200 207
201 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 208 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
202 209
203 class MockDownloadFileManager : public DownloadFileManager {
204 public:
205 MockDownloadFileManager();
206
207 void CreateDownloadFile(
208 scoped_ptr<DownloadCreateInfo> info,
209 scoped_ptr<content::ByteStreamReader> stream,
210 scoped_refptr<content::DownloadManager> download_manager,
211 bool hash_needed,
212 const net::BoundNetLog& bound_net_log,
213 const CreateDownloadFileCallback& callback) OVERRIDE {
214 // Note that scoped_refptr<> on download manager is also stripped
215 // to make mock comparisons easier. Comparing the scoped_refptr<>
216 // works, but holds a reference to the DownloadManager until
217 // MockDownloadFileManager destruction, which messes up destruction
218 // testing.
219 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(),
220 hash_needed, bound_net_log, callback);
221 }
222
223 MOCK_METHOD6(MockCreateDownloadFile, void(
224 DownloadCreateInfo* info,
225 content::ByteStreamReader* stream,
226 content::DownloadManager* download_manager,
227 bool hash_needed,
228 const net::BoundNetLog& bound_net_log,
229 const CreateDownloadFileCallback& callback));
230 MOCK_METHOD0(Shutdown, void());
231 MOCK_METHOD1(CancelDownload, void(content::DownloadId));
232 MOCK_METHOD2(CompleteDownload, void(content::DownloadId,
233 const base::Closure&));
234 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*));
235 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId,
236 const FilePath&,
237 bool,
238 const RenameCompletionCallback&));
239 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int());
240 protected:
241 virtual ~MockDownloadFileManager();
242 };
243
244 MockDownloadFileManager::MockDownloadFileManager()
245 : DownloadFileManager(NULL) {}
246
247 MockDownloadFileManager::~MockDownloadFileManager() {}
248
249 class MockDownloadItemFactory 210 class MockDownloadItemFactory
250 : public content::DownloadItemFactory, 211 : public content::DownloadItemFactory,
251 public base::SupportsWeakPtr<MockDownloadItemFactory> { 212 public base::SupportsWeakPtr<MockDownloadItemFactory> {
252 public: 213 public:
253 MockDownloadItemFactory(); 214 MockDownloadItemFactory();
254 virtual ~MockDownloadItemFactory(); 215 virtual ~MockDownloadItemFactory();
255 216
256 // Access to map of created items. 217 // Access to map of created items.
257 // TODO(rdsmith): Could add type (save page, persisted, etc.) 218 // TODO(rdsmith): Could add type (save page, persisted, etc.)
258 // functionality if it's ever needed by consumers. 219 // functionality if it's ever needed by consumers.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 const DownloadCreateInfo& info, 303 const DownloadCreateInfo& info,
343 scoped_ptr<DownloadRequestHandleInterface> request_handle, 304 scoped_ptr<DownloadRequestHandleInterface> request_handle,
344 const net::BoundNetLog& bound_net_log) { 305 const net::BoundNetLog& bound_net_log) {
345 int local_id = info.download_id.local(); 306 int local_id = info.download_id.local();
346 DCHECK(items_.find(local_id) == items_.end()); 307 DCHECK(items_.find(local_id) == items_.end());
347 308
348 MockDownloadItemImpl* result = 309 MockDownloadItemImpl* result =
349 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 310 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
350 EXPECT_CALL(*result, GetId()) 311 EXPECT_CALL(*result, GetId())
351 .WillRepeatedly(Return(local_id)); 312 .WillRepeatedly(Return(local_id));
313 EXPECT_CALL(*result, GetGlobalId())
314 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
352 items_[local_id] = result; 315 items_[local_id] = result;
353 316
317 // Active items are created and then immediately are called to start
318 // the download.
319 EXPECT_CALL(*result, MockStart(_));
320
354 return result; 321 return result;
355 } 322 }
356 323
357 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 324 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
358 DownloadItemImplDelegate* delegate, 325 DownloadItemImplDelegate* delegate,
359 const FilePath& path, 326 const FilePath& path,
360 const GURL& url, 327 const GURL& url,
361 content::DownloadId download_id, 328 content::DownloadId download_id,
362 const std::string& mime_type, 329 const std::string& mime_type,
363 const net::BoundNetLog& bound_net_log) { 330 const net::BoundNetLog& bound_net_log) {
364 int local_id = download_id.local(); 331 int local_id = download_id.local();
365 DCHECK(items_.find(local_id) == items_.end()); 332 DCHECK(items_.find(local_id) == items_.end());
366 333
367 MockDownloadItemImpl* result = 334 MockDownloadItemImpl* result =
368 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 335 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
369 EXPECT_CALL(*result, GetId()) 336 EXPECT_CALL(*result, GetId())
370 .WillRepeatedly(Return(local_id)); 337 .WillRepeatedly(Return(local_id));
371 items_[local_id] = result; 338 items_[local_id] = result;
372 339
373 return result; 340 return result;
374 } 341 }
375 342
343 class MockDownloadFileFactory
344 : public content::DownloadFileFactory,
345 public base::SupportsWeakPtr<MockDownloadFileFactory> {
346 public:
347 MockDownloadFileFactory() {}
348 virtual ~MockDownloadFileFactory() {}
349
350 // Overridden method from DownloadFileFactory
351 MOCK_METHOD9(MockCreateFile, content::DownloadFile*(
352 const content::DownloadSaveInfo&,
353 const FilePath&,
354 GURL, GURL, int64, bool,
355 content::ByteStreamReader*,
356 const net::BoundNetLog&,
357 base::WeakPtr<content::DownloadDestinationObserver>));
358
359 virtual content::DownloadFile* CreateFile(
360 const content::DownloadSaveInfo& save_info,
361 const FilePath& default_download_directory,
362 GURL url,
363 GURL referrer_url,
364 int64 received_bytes,
365 bool calculate_hash,
366 scoped_ptr<content::ByteStreamReader> stream,
367 const net::BoundNetLog& bound_net_log,
368 base::WeakPtr<content::DownloadDestinationObserver> observer) {
369 return MockCreateFile(save_info, default_download_directory, url,
370 referrer_url, received_bytes, calculate_hash,
371 stream.get(), bound_net_log, observer);
372 }
373 };
374
376 class MockBrowserContext : public content::BrowserContext { 375 class MockBrowserContext : public content::BrowserContext {
377 public: 376 public:
378 MockBrowserContext() {} 377 MockBrowserContext() {}
379 ~MockBrowserContext() {} 378 ~MockBrowserContext() {}
380 379
381 MOCK_METHOD0(GetPath, FilePath()); 380 MOCK_METHOD0(GetPath, FilePath());
382 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); 381 MOCK_CONST_METHOD0(IsOffTheRecord, bool());
383 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*()); 382 MOCK_METHOD0(GetRequestContext, net::URLRequestContextGetter*());
384 MOCK_METHOD1(GetRequestContextForRenderProcess, 383 MOCK_METHOD1(GetRequestContextForRenderProcess,
385 net::URLRequestContextGetter*(int renderer_child_id)); 384 net::URLRequestContextGetter*(int renderer_child_id));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 421
423 DownloadManagerTest() 422 DownloadManagerTest()
424 : ui_thread_(content::BrowserThread::UI, &message_loop_), 423 : ui_thread_(content::BrowserThread::UI, &message_loop_),
425 file_thread_(content::BrowserThread::FILE, &message_loop_), 424 file_thread_(content::BrowserThread::FILE, &message_loop_),
426 next_download_id_(0) { 425 next_download_id_(0) {
427 } 426 }
428 427
429 // We tear down everything in TearDown(). 428 // We tear down everything in TearDown().
430 ~DownloadManagerTest() {} 429 ~DownloadManagerTest() {}
431 430
432 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, 431 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
433 // and MockDownloadFileManager, then create a DownloadManager that points 432 // then create a DownloadManager that points
434 // at all of those. 433 // at all of those.
435 virtual void SetUp() { 434 virtual void SetUp() {
436 DCHECK(!download_manager_.get()); 435 DCHECK(!download_manager_.get());
437 436
438 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 437 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
438 mock_download_file_factory_ = (new MockDownloadFileFactory())->AsWeakPtr();
439 mock_download_manager_delegate_.reset( 439 mock_download_manager_delegate_.reset(
440 new StrictMock<MockDownloadManagerDelegate>); 440 new StrictMock<MockDownloadManagerDelegate>);
441 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 441 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
442 .WillOnce(Return()); 442 .WillOnce(Return());
443 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
444 EXPECT_CALL(*mock_download_file_manager_.get(),
445 OnDownloadManagerShutdown(_));
446 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 443 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
447 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 444 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
448 .WillRepeatedly(Return(false)); 445 .WillRepeatedly(Return(false));
449 446
450 download_manager_ = new DownloadManagerImpl( 447 download_manager_ = new DownloadManagerImpl(NULL);
451 mock_download_file_manager_.get(), 448 download_manager_->SetDownloadItemFactoryForTesting(
452 scoped_ptr<content::DownloadItemFactory>( 449 scoped_ptr<content::DownloadItemFactory>(
453 mock_download_item_factory_.get()).Pass(), NULL); 450 mock_download_item_factory_.get()).Pass());
451 download_manager_->SetDownloadFileFactoryForTesting(
452 scoped_ptr<content::DownloadFileFactory>(
453 mock_download_file_factory_.get()).Pass());
454 observer_.reset(new MockDownloadManagerObserver()); 454 observer_.reset(new MockDownloadManagerObserver());
455 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 455 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
456 .WillOnce(Return()); 456 .WillOnce(Return());
457 download_manager_->AddObserver(observer_.get()); 457 download_manager_->AddObserver(observer_.get());
458 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 458 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
459 download_manager_->Init(mock_browser_context_.get()); 459 download_manager_->Init(mock_browser_context_.get());
460 } 460 }
461 461
462 virtual void TearDown() { 462 virtual void TearDown() {
463 while (MockDownloadItemImpl* 463 while (MockDownloadItemImpl*
464 item = mock_download_item_factory_->PopItem()) { 464 item = mock_download_item_factory_->PopItem()) {
465 EXPECT_CALL(*item, GetSafetyState()) 465 EXPECT_CALL(*item, GetSafetyState())
466 .WillOnce(Return(content::DownloadItem::SAFE)); 466 .WillOnce(Return(content::DownloadItem::SAFE));
467 EXPECT_CALL(*item, IsPartialDownload()) 467 EXPECT_CALL(*item, IsPartialDownload())
468 .WillOnce(Return(false)); 468 .WillOnce(Return(false));
469 } 469 }
470 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 470 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
471 .WillOnce(Return()); 471 .WillOnce(Return());
472 472
473 download_manager_->Shutdown(); 473 download_manager_->Shutdown();
474 download_manager_ = NULL; 474 download_manager_ = NULL;
475 message_loop_.RunAllPending(); 475 message_loop_.RunAllPending();
476 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 476 ASSERT_EQ(NULL, mock_download_item_factory_.get());
477 ASSERT_EQ(NULL, mock_download_file_factory_.get());
477 message_loop_.RunAllPending(); 478 message_loop_.RunAllPending();
478 mock_download_manager_delegate_.reset(); 479 mock_download_manager_delegate_.reset();
479 mock_download_file_manager_ = NULL;
480 mock_browser_context_.reset(); 480 mock_browser_context_.reset();
481 } 481 }
482 482
483 // Returns download id. 483 // Returns download id.
484 MockDownloadItemImpl& AddItemToManager() { 484 MockDownloadItemImpl& AddItemToManager() {
485 DownloadCreateInfo info; 485 DownloadCreateInfo info;
486 486
487 static const char* kDownloadIdDomain = "Test download id domain"; 487 static const char* kDownloadIdDomain = "Test download id domain";
488 488
489 // Args are ignored except for download id, so everything else can be 489 // Args are ignored except for download id, so everything else can be
490 // null. 490 // null.
491 int id = next_download_id_; 491 int id = next_download_id_;
492 ++next_download_id_; 492 ++next_download_id_;
493 info.download_id = content::DownloadId(kDownloadIdDomain, id); 493 info.download_id = content::DownloadId(kDownloadIdDomain, id);
494 info.request_handle = DownloadRequestHandle(); 494 info.request_handle = DownloadRequestHandle();
495 download_manager_->CreateDownloadItem(&info); 495 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
496 496
497 DCHECK(mock_download_item_factory_->GetItem(id)); 497 DCHECK(mock_download_item_factory_->GetItem(id));
498 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 498 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
499 ON_CALL(item, GetId()) 499 // Satisfy expectation. If the item is created in StartDownload(),
500 .WillByDefault(Return(id)); 500 // we call Start on it immediately, so we need to set that expectation
501 // in the factory.
502 item.Start(scoped_ptr<content::DownloadFile>());
501 503
502 return item; 504 return item;
503 } 505 }
504 506
505 MockDownloadItemImpl& GetMockDownloadItem(int id) { 507 MockDownloadItemImpl& GetMockDownloadItem(int id) {
506 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 508 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
507 509
508 DCHECK(itemp); 510 DCHECK(itemp);
509 return *itemp; 511 return *itemp;
510 } 512 }
511 513
512 void RemoveMockDownloadItem(int id) { 514 void RemoveMockDownloadItem(int id) {
513 // Owned by DownloadManager; should be deleted there. 515 // Owned by DownloadManager; should be deleted there.
514 mock_download_item_factory_->RemoveItem(id); 516 mock_download_item_factory_->RemoveItem(id);
515 } 517 }
516 518
517 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 519 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
518 return *mock_download_manager_delegate_; 520 return *mock_download_manager_delegate_;
519 } 521 }
520 522
521 MockDownloadFileManager& GetMockDownloadFileManager() {
522 return *mock_download_file_manager_;
523 }
524
525 MockDownloadManagerObserver& GetMockObserver() { 523 MockDownloadManagerObserver& GetMockObserver() {
526 return *observer_; 524 return *observer_;
527 } 525 }
528 526
529 // Probe at private internals. 527 // Probe at private internals.
530 void DownloadStopped(DownloadItemImpl* item) { 528 void DownloadStopped(DownloadItemImpl* item) {
531 download_manager_->DownloadStopped(item); 529 download_manager_->DownloadStopped(item);
532 } 530 }
533 531
532 void DownloadTargetDeterminedCallback(
533 const FilePath& target_path,
534 content::DownloadItem::TargetDisposition disposition,
535 content::DownloadDangerType danger_type,
536 const FilePath& intermediate_path) {
537 callback_called_ = true;
538 target_path_ = target_path;
539 target_disposition_ = disposition;
540 danger_type_ = danger_type;
541 intermediate_path_ = intermediate_path;
542 }
543
544 void DetermineDownloadTarget(DownloadItemImpl* item) {
545 download_manager_->DetermineDownloadTarget(
546 item, base::Bind(
547 &DownloadManagerTest::DownloadTargetDeterminedCallback,
548 base::Unretained(this)));
549 }
550
534 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 551 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
535 // For DCHECK in AddDownloadItemToHistory. Don't want to use 552 // For DCHECK in AddDownloadItemToHistory. Don't want to use
536 // WillRepeatedly as it may have to return true after this. 553 // WillRepeatedly as it may have to return true after this.
537 if (DCHECK_IS_ON()) 554 if (DCHECK_IS_ON())
538 EXPECT_CALL(item, IsPersisted()) 555 EXPECT_CALL(item, IsPersisted())
539 .WillRepeatedly(Return(false)); 556 .WillRepeatedly(Return(false));
540 557
541 EXPECT_CALL(item, SetDbHandle(db_handle)); 558 EXPECT_CALL(item, SetDbHandle(db_handle));
542 EXPECT_CALL(item, SetIsPersisted()); 559 EXPECT_CALL(item, SetIsPersisted());
543 EXPECT_CALL(item, GetDbHandle()) 560 EXPECT_CALL(item, GetDbHandle())
(...skipping 12 matching lines...) Expand all
556 // Null out MaybeCompleteDownload 573 // Null out MaybeCompleteDownload
557 EXPECT_CALL(item, AllDataSaved()) 574 EXPECT_CALL(item, AllDataSaved())
558 .WillOnce(Return(false)); 575 .WillOnce(Return(false));
559 576
560 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle); 577 download_manager_->OnItemAddedToPersistentStore(item.GetId(), db_handle);
561 } 578 }
562 579
563 protected: 580 protected:
564 // Key test variable; we'll keep it available to sub-classes. 581 // Key test variable; we'll keep it available to sub-classes.
565 scoped_refptr<DownloadManagerImpl> download_manager_; 582 scoped_refptr<DownloadManagerImpl> download_manager_;
583 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_;
584
585 // Target detetermined callback.
586 bool callback_called_;
587 FilePath target_path_;
588 content::DownloadItem::TargetDisposition target_disposition_;
589 content::DownloadDangerType danger_type_;
590 FilePath intermediate_path_;
566 591
567 private: 592 private:
568 MessageLoopForUI message_loop_; 593 MessageLoopForUI message_loop_;
569 content::TestBrowserThread ui_thread_; 594 content::TestBrowserThread ui_thread_;
570 content::TestBrowserThread file_thread_; 595 content::TestBrowserThread file_thread_;
571 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 596 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
572 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 597 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
573 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
574 scoped_ptr<MockBrowserContext> mock_browser_context_; 598 scoped_ptr<MockBrowserContext> mock_browser_context_;
575 scoped_ptr<MockDownloadManagerObserver> observer_; 599 scoped_ptr<MockDownloadManagerObserver> observer_;
576 int next_download_id_; 600 int next_download_id_;
577 601
578 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 602 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
579 }; 603 };
580 604
581 // Confirm the appropriate invocations occur when you start a download. 605 // Confirm the appropriate invocations occur when you start a download.
582 TEST_F(DownloadManagerTest, StartDownload) { 606 TEST_F(DownloadManagerTest, StartDownload) {
583 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 607 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
584 scoped_ptr<content::ByteStreamReader> stream; 608 scoped_ptr<content::ByteStreamReader> stream;
585 int32 local_id(5); // Random value 609 int32 local_id(5); // Random value
586 FilePath download_path(FILE_PATH_LITERAL("download/path")); 610 FilePath download_path(FILE_PATH_LITERAL("download/path"));
587 611
588 EXPECT_FALSE(download_manager_->GetDownload(local_id)); 612 EXPECT_FALSE(download_manager_->GetDownload(local_id));
589 613
590 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 614 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
591 .WillOnce(Return()); 615 .WillOnce(Return());
592 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 616 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
593 .WillOnce(Return(content::DownloadId(this, local_id))); 617 .WillOnce(Return(content::DownloadId(this, local_id)));
618
619 // Doing nothing will set the default download directory to null.
620 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _));
594 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 621 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
595 .WillOnce(Return(true)); 622 .WillOnce(Return(true));
596 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_,_,_,_)) 623 EXPECT_CALL(*mock_download_file_factory_.get(),
597 .WillOnce(SetArgPointee<2>(download_path)); 624 MockCreateFile(Ref(info->save_info), _, _, _, 0, true,
598 625 stream.get(), _, _));
599 // The CreateDownloadFile call should specify a DownloadCreateInfo that
600 // includes the result of the GetSaveDir() call.
601 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
602 DownloadCreateInfoWithDefaultPath(info.get(), download_path),
603 static_cast<content::ByteStreamReader*>(NULL),
604 download_manager_.get(), true, _, _));
605 626
606 download_manager_->StartDownload(info.Pass(), stream.Pass()); 627 download_manager_->StartDownload(info.Pass(), stream.Pass());
607 EXPECT_TRUE(download_manager_->GetDownload(local_id)); 628 EXPECT_TRUE(download_manager_->GetDownload(local_id));
608 } 629 }
609 630
610 // Do the results of an OnDownloadInterrupted get passed through properly 631 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
611 // to the DownloadItem? 632 // blocks starting.
612 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { 633 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) {
613 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 634 // Put a mock we have a handle to on the download manager.
614 .WillOnce(Return()); 635 MockDownloadItemImpl& item(AddItemToManager());
636
637 EXPECT_CALL(GetMockDownloadManagerDelegate(),
638 DetermineDownloadTarget(&item, _))
639 .WillOnce(Return(true));
640 DetermineDownloadTarget(&item);
641 }
642
643 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate
644 // allows starting. This also tests OnDownloadTargetDetermined.
645 TEST_F(DownloadManagerTest, DetermineDownloadTarget_False) {
646 // Put a mock we have a handle to on the download manager.
647 MockDownloadItemImpl& item(AddItemToManager());
648
649 FilePath path(FILE_PATH_LITERAL("random_filepath.txt"));
650 EXPECT_CALL(GetMockDownloadManagerDelegate(),
651 DetermineDownloadTarget(&item, _))
652 .WillOnce(Return(false));
653 EXPECT_CALL(item, GetForcedFilePath())
654 .WillOnce(ReturnRef(path));
655
656 // Confirm that the callback was called with the right values in this case.
657 callback_called_ = false;
658 DetermineDownloadTarget(&item);
659 EXPECT_TRUE(callback_called_);
660 EXPECT_EQ(path, target_path_);
661 EXPECT_EQ(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
662 target_disposition_);
663 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_);
664 EXPECT_EQ(path, intermediate_path_);
665 }
666
667 // Does DownloadStopped remove Download from appropriate queues?
668 // This test tests non-persisted downloads.
669 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
670 // Put a mock we have a handle to on the download manager.
671 MockDownloadItemImpl& item(AddItemToManager());
672
673 EXPECT_CALL(item, IsPersisted())
674 .WillRepeatedly(Return(false));
675 EXPECT_CALL(item, GetState())
676 .WillRepeatedly(Return(DownloadItem::CANCELLED));
677 EXPECT_CALL(item, GetDbHandle())
678 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
679
680 DownloadStopped(&item);
681 // TODO(rdsmith): Confirm that the download item is no longer on the
682 // active list by calling download_manager_->GetActiveDownloadItem(id).
683 // Currently, the item is left on the active list for rendez-vous with
684 // the history system :-{.
685 }
686
687 // Does DownloadStopped remove Download from appropriate queues?
688 // This test tests persisted downloads.
689 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
615 // Put a mock we have a handle to on the download manager. 690 // Put a mock we have a handle to on the download manager.
616 MockDownloadItemImpl& item(AddItemToManager()); 691 MockDownloadItemImpl& item(AddItemToManager());
617 int download_id = item.GetId(); 692 int download_id = item.GetId();
693 int64 db_handle = 0x7;
694 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
695 .WillOnce(Return());
696 AddItemToHistory(item, db_handle);
618 697
619 content::DownloadInterruptReason reason( 698 EXPECT_CALL(item, IsPersisted())
620 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 699 .WillRepeatedly(Return(true));
700 EXPECT_CALL(GetMockDownloadManagerDelegate(),
701 UpdateItemInPersistentStore(&item));
702 EXPECT_CALL(item, GetState())
703 .WillRepeatedly(Return(DownloadItem::CANCELLED));
704 EXPECT_CALL(item, GetDbHandle())
705 .WillRepeatedly(Return(db_handle));
621 706
622 EXPECT_CALL(item, Interrupt(reason)); 707 DownloadStopped(&item);
623 download_manager_->OnDownloadInterrupted(download_id, reason); 708 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
624 EXPECT_EQ(&item, download_manager_->GetDownload(download_id));
625 } 709 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698