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

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: Merged to LKGR. 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 28 matching lines...) Expand all
73 MOCK_METHOD0(CanShowInFolder, bool()); 73 MOCK_METHOD0(CanShowInFolder, bool());
74 MOCK_METHOD0(CanOpenDownload, bool()); 74 MOCK_METHOD0(CanOpenDownload, bool());
75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); 75 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool());
76 MOCK_METHOD0(OpenDownload, void()); 76 MOCK_METHOD0(OpenDownload, void());
77 MOCK_METHOD0(ShowDownloadInShell, void()); 77 MOCK_METHOD0(ShowDownloadInShell, void());
78 MOCK_METHOD0(DangerousDownloadValidated, void()); 78 MOCK_METHOD0(DangerousDownloadValidated, void());
79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); 79 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&));
80 MOCK_METHOD1(Cancel, void(bool)); 80 MOCK_METHOD1(Cancel, void(bool));
81 MOCK_METHOD0(MarkAsComplete, void()); 81 MOCK_METHOD0(MarkAsComplete, void());
82 MOCK_METHOD1(DelayedDownloadOpened, void(bool)); 82 MOCK_METHOD1(DelayedDownloadOpened, void(bool));
83 MOCK_METHOD2(OnAllDataSaved, void(int64, const std::string&)); 83 MOCK_METHOD1(OnAllDataSaved, void(const std::string&));
84 MOCK_METHOD0(OnDownloadedFileRemoved, void()); 84 MOCK_METHOD0(OnDownloadedFileRemoved, void());
85 MOCK_METHOD0(MaybeCompleteDownload, void()); 85 MOCK_METHOD0(MaybeCompleteDownload, void());
86 virtual void Start(
87 scoped_ptr<content::DownloadFile> download_file) OVERRIDE {
88 MockStart(download_file.get());
89 }
90
91 MOCK_METHOD1(MockStart, void(content::DownloadFile*));
92
86 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason)); 93 MOCK_METHOD1(Interrupt, void(DownloadInterruptReason));
87 MOCK_METHOD1(Delete, void(DeleteReason)); 94 MOCK_METHOD1(Delete, void(DeleteReason));
88 MOCK_METHOD0(Remove, void()); 95 MOCK_METHOD0(Remove, void());
89 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); 96 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*));
90 MOCK_CONST_METHOD0(CurrentSpeed, int64()); 97 MOCK_CONST_METHOD0(CurrentSpeed, int64());
91 MOCK_CONST_METHOD0(PercentComplete, int()); 98 MOCK_CONST_METHOD0(PercentComplete, int());
92 MOCK_CONST_METHOD0(AllDataSaved, bool()); 99 MOCK_CONST_METHOD0(AllDataSaved, bool());
93 MOCK_METHOD0(TogglePause, void()); 100 MOCK_METHOD0(TogglePause, void());
94 MOCK_METHOD0(OnDownloadCompleting, void()); 101 MOCK_METHOD0(OnDownloadCompleting, void());
95 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); 102 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&()); 154 MOCK_CONST_METHOD0(GetLastModifiedTime, const std::string&());
148 MOCK_CONST_METHOD0(GetETag, const std::string&()); 155 MOCK_CONST_METHOD0(GetETag, const std::string&());
149 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason()); 156 MOCK_CONST_METHOD0(GetLastReason, DownloadInterruptReason());
150 MOCK_CONST_METHOD0(GetPersistentStoreInfo, 157 MOCK_CONST_METHOD0(GetPersistentStoreInfo,
151 content::DownloadPersistentStoreInfo()); 158 content::DownloadPersistentStoreInfo());
152 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 159 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
153 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*()); 160 MOCK_CONST_METHOD0(GetWebContents, content::WebContents*());
154 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath()); 161 MOCK_CONST_METHOD0(GetFileNameToReportUser, FilePath());
155 MOCK_METHOD1(SetDisplayName, void(const FilePath&)); 162 MOCK_METHOD1(SetDisplayName, void(const FilePath&));
156 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath()); 163 MOCK_CONST_METHOD0(GetUserVerifiedFilePath, FilePath());
157 MOCK_METHOD0(OffThreadCancel, void());
158 MOCK_CONST_METHOD1(DebugString, std::string(bool)); 164 MOCK_CONST_METHOD1(DebugString, std::string(bool));
159 MOCK_METHOD0(MockDownloadOpenForTesting, void()); 165 MOCK_METHOD0(MockDownloadOpenForTesting, void());
160 }; 166 };
161 167
162 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { 168 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate {
163 public: 169 public:
164 MockDownloadManagerDelegate(); 170 MockDownloadManagerDelegate();
165 virtual ~MockDownloadManagerDelegate(); 171 virtual ~MockDownloadManagerDelegate();
166 172
167 MOCK_METHOD0(Shutdown, void()); 173 MOCK_METHOD0(Shutdown, void());
(...skipping 17 matching lines...) Expand all
185 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*)); 191 MOCK_METHOD4(GetSaveDir, void(WebContents*, FilePath*, FilePath*, bool*));
186 MOCK_METHOD5(ChooseSavePath, void( 192 MOCK_METHOD5(ChooseSavePath, void(
187 WebContents*, const FilePath&, const FilePath::StringType&, 193 WebContents*, const FilePath&, const FilePath::StringType&,
188 bool, const content::SavePackagePathPickedCallback&)); 194 bool, const content::SavePackagePathPickedCallback&));
189 }; 195 };
190 196
191 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {} 197 MockDownloadManagerDelegate::MockDownloadManagerDelegate() {}
192 198
193 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {} 199 MockDownloadManagerDelegate::~MockDownloadManagerDelegate() {}
194 200
195 class MockDownloadFileManager : public DownloadFileManager { 201 class NullDownloadItemImplDelegate : public DownloadItemImplDelegate {
196 public: 202 public:
197 MockDownloadFileManager(); 203 // Safe to null this out even if it doesn't do anything because none
198 204 // of these functions will ever be called; this class just exists
199 void CreateDownloadFile( 205 // to have something to pass to the DownloadItemImpl base class
200 scoped_ptr<DownloadCreateInfo> info, 206 // of MockDownloadItemImpl.
201 scoped_ptr<content::ByteStreamReader> stream, 207 virtual void DelegateStart(DownloadItemImpl* download) OVERRIDE {
202 scoped_refptr<content::DownloadManager> download_manager, 208 NOTREACHED();
203 bool hash_needed,
204 const net::BoundNetLog& bound_net_log,
205 const CreateDownloadFileCallback& callback) OVERRIDE {
206 // Note that scoped_refptr<> on download manager is also stripped
207 // to make mock comparisons easier. Comparing the scoped_refptr<>
208 // works, but holds a reference to the DownloadManager until
209 // MockDownloadFileManager destruction, which messes up destruction
210 // testing.
211 MockCreateDownloadFile(info.get(), stream.get(), download_manager.get(),
212 hash_needed, bound_net_log, callback);
213 } 209 }
214
215 MOCK_METHOD6(MockCreateDownloadFile, void(
216 DownloadCreateInfo* info,
217 content::ByteStreamReader* stream,
218 content::DownloadManager* download_manager,
219 bool hash_needed,
220 const net::BoundNetLog& bound_net_log,
221 const CreateDownloadFileCallback& callback));
222 MOCK_METHOD0(Shutdown, void());
223 MOCK_METHOD1(CancelDownload, void(content::DownloadId));
224 MOCK_METHOD2(CompleteDownload, void(content::DownloadId,
225 const base::Closure&));
226 MOCK_METHOD1(OnDownloadManagerShutdown, void(content::DownloadManager*));
227 MOCK_METHOD4(RenameDownloadFile, void(content::DownloadId,
228 const FilePath&,
229 bool,
230 const RenameCompletionCallback&));
231 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int());
232 protected:
233 virtual ~MockDownloadFileManager();
234 }; 210 };
235 211
236 MockDownloadFileManager::MockDownloadFileManager()
237 : DownloadFileManager(NULL) {}
238
239 MockDownloadFileManager::~MockDownloadFileManager() {}
240
241 class MockDownloadItemFactory 212 class MockDownloadItemFactory
242 : public content::DownloadItemFactory, 213 : public content::DownloadItemFactory,
243 public base::SupportsWeakPtr<MockDownloadItemFactory> { 214 public base::SupportsWeakPtr<MockDownloadItemFactory> {
244 public: 215 public:
245 MockDownloadItemFactory(); 216 MockDownloadItemFactory();
246 virtual ~MockDownloadItemFactory(); 217 virtual ~MockDownloadItemFactory();
247 218
248 // Access to map of created items. 219 // Access to map of created items.
249 // TODO(rdsmith): Could add type (save page, persisted, etc.) 220 // TODO(rdsmith): Could add type (save page, persisted, etc.)
250 // functionality if it's ever needed by consumers. 221 // functionality if it's ever needed by consumers.
(...skipping 25 matching lines...) Expand all
276 DownloadItemImplDelegate* delegate, 247 DownloadItemImplDelegate* delegate,
277 const FilePath& path, 248 const FilePath& path,
278 const GURL& url, 249 const GURL& url,
279 bool is_otr, 250 bool is_otr,
280 content::DownloadId download_id, 251 content::DownloadId download_id,
281 const std::string& mime_type, 252 const std::string& mime_type,
282 const net::BoundNetLog& bound_net_log) OVERRIDE; 253 const net::BoundNetLog& bound_net_log) OVERRIDE;
283 254
284 private: 255 private:
285 std::map<int32, MockDownloadItemImpl*> items_; 256 std::map<int32, MockDownloadItemImpl*> items_;
286 DownloadItemImplDelegate item_delegate_; 257 NullDownloadItemImplDelegate item_delegate_;
287 258
288 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); 259 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory);
289 }; 260 };
290 261
291 MockDownloadItemFactory::MockDownloadItemFactory() {} 262 MockDownloadItemFactory::MockDownloadItemFactory() {}
292 263
293 MockDownloadItemFactory::~MockDownloadItemFactory() {} 264 MockDownloadItemFactory::~MockDownloadItemFactory() {}
294 265
295 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) { 266 MockDownloadItemImpl* MockDownloadItemFactory::GetItem(int id) {
296 if (items_.find(id) == items_.end()) 267 if (items_.find(id) == items_.end())
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 scoped_ptr<DownloadRequestHandleInterface> request_handle, 308 scoped_ptr<DownloadRequestHandleInterface> request_handle,
338 bool is_otr, 309 bool is_otr,
339 const net::BoundNetLog& bound_net_log) { 310 const net::BoundNetLog& bound_net_log) {
340 int local_id = info.download_id.local(); 311 int local_id = info.download_id.local();
341 DCHECK(items_.find(local_id) == items_.end()); 312 DCHECK(items_.find(local_id) == items_.end());
342 313
343 MockDownloadItemImpl* result = 314 MockDownloadItemImpl* result =
344 new StrictMock<MockDownloadItemImpl>(&item_delegate_); 315 new StrictMock<MockDownloadItemImpl>(&item_delegate_);
345 EXPECT_CALL(*result, GetId()) 316 EXPECT_CALL(*result, GetId())
346 .WillRepeatedly(Return(local_id)); 317 .WillRepeatedly(Return(local_id));
318 EXPECT_CALL(*result, GetGlobalId())
319 .WillRepeatedly(Return(content::DownloadId(delegate, local_id)));
347 items_[local_id] = result; 320 items_[local_id] = result;
348 321
322 // Active items are created and then immediately are called to start
323 // the download.
324 EXPECT_CALL(*result, MockStart(_));
325
349 return result; 326 return result;
350 } 327 }
351 328
352 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( 329 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem(
353 DownloadItemImplDelegate* delegate, 330 DownloadItemImplDelegate* delegate,
354 const FilePath& path, 331 const FilePath& path,
355 const GURL& url, 332 const GURL& url,
356 bool is_otr, 333 bool is_otr,
357 content::DownloadId download_id, 334 content::DownloadId download_id,
358 const std::string& mime_type, 335 const std::string& mime_type,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 388
412 DownloadManagerTest() 389 DownloadManagerTest()
413 : ui_thread_(content::BrowserThread::UI, &message_loop_), 390 : ui_thread_(content::BrowserThread::UI, &message_loop_),
414 file_thread_(content::BrowserThread::FILE, &message_loop_), 391 file_thread_(content::BrowserThread::FILE, &message_loop_),
415 next_download_id_(0) { 392 next_download_id_(0) {
416 } 393 }
417 394
418 // We tear down everything in TearDown(). 395 // We tear down everything in TearDown().
419 ~DownloadManagerTest() {} 396 ~DownloadManagerTest() {}
420 397
421 // Create a MockDownloadItemFactory, MockDownloadManagerDelegate, 398 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate,
422 // and MockDownloadFileManager, then create a DownloadManager that points 399 // then create a DownloadManager that points
423 // at all of those. 400 // at all of those.
424 virtual void SetUp() { 401 virtual void SetUp() {
425 DCHECK(!download_manager_.get()); 402 DCHECK(!download_manager_.get());
426 403
427 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr(); 404 mock_download_item_factory_ = (new MockDownloadItemFactory())->AsWeakPtr();
428 mock_download_manager_delegate_.reset( 405 mock_download_manager_delegate_.reset(
429 new StrictMock<MockDownloadManagerDelegate>); 406 new StrictMock<MockDownloadManagerDelegate>);
430 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown()) 407 EXPECT_CALL(*mock_download_manager_delegate_.get(), Shutdown())
431 .WillOnce(Return()); 408 .WillOnce(Return());
432 mock_download_file_manager_ = new StrictMock<MockDownloadFileManager>;
433 EXPECT_CALL(*mock_download_file_manager_.get(),
434 OnDownloadManagerShutdown(_));
435 mock_browser_context_.reset(new StrictMock<MockBrowserContext>); 409 mock_browser_context_.reset(new StrictMock<MockBrowserContext>);
436 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord()) 410 EXPECT_CALL(*mock_browser_context_.get(), IsOffTheRecord())
437 .WillRepeatedly(Return(false)); 411 .WillRepeatedly(Return(false));
438 412
439 download_manager_ = new DownloadManagerImpl( 413 download_manager_ = new DownloadManagerImpl(
440 mock_download_file_manager_.get(),
441 scoped_ptr<content::DownloadItemFactory>( 414 scoped_ptr<content::DownloadItemFactory>(
442 mock_download_item_factory_.get()).Pass(), NULL); 415 mock_download_item_factory_.get()).Pass(), NULL);
443 observer_.reset(new MockDownloadManagerObserver()); 416 observer_.reset(new MockDownloadManagerObserver());
444 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 417 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
445 .WillOnce(Return()); 418 .WillOnce(Return());
446 download_manager_->AddObserver(observer_.get()); 419 download_manager_->AddObserver(observer_.get());
447 download_manager_->SetDelegate(mock_download_manager_delegate_.get()); 420 download_manager_->SetDelegate(mock_download_manager_delegate_.get());
448 download_manager_->Init(mock_browser_context_.get()); 421 download_manager_->Init(mock_browser_context_.get());
449 } 422 }
450 423
451 virtual void TearDown() { 424 virtual void TearDown() {
452 while (MockDownloadItemImpl* 425 while (MockDownloadItemImpl*
453 item = mock_download_item_factory_->PopItem()) { 426 item = mock_download_item_factory_->PopItem()) {
454 EXPECT_CALL(*item, GetSafetyState()) 427 EXPECT_CALL(*item, GetSafetyState())
455 .WillOnce(Return(content::DownloadItem::SAFE)); 428 .WillOnce(Return(content::DownloadItem::SAFE));
456 EXPECT_CALL(*item, IsPartialDownload()) 429 EXPECT_CALL(*item, IsPartialDownload())
457 .WillOnce(Return(false)); 430 .WillOnce(Return(false));
458 } 431 }
459 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get())) 432 EXPECT_CALL(GetMockObserver(), ManagerGoingDown(download_manager_.get()))
460 .WillOnce(Return()); 433 .WillOnce(Return());
461 434
462 download_manager_->Shutdown(); 435 download_manager_->Shutdown();
463 download_manager_ = NULL; 436 download_manager_ = NULL;
464 message_loop_.RunAllPending(); 437 message_loop_.RunAllPending();
465 ASSERT_EQ(NULL, mock_download_item_factory_.get()); 438 ASSERT_EQ(NULL, mock_download_item_factory_.get());
466 message_loop_.RunAllPending(); 439 message_loop_.RunAllPending();
467 mock_download_manager_delegate_.reset(); 440 mock_download_manager_delegate_.reset();
468 mock_download_file_manager_ = NULL;
469 mock_browser_context_.reset(); 441 mock_browser_context_.reset();
470 } 442 }
471 443
472 // Returns download id. 444 // Returns download id.
473 MockDownloadItemImpl& AddItemToManager() { 445 MockDownloadItemImpl& AddItemToManager() {
474 DownloadCreateInfo info; 446 DownloadCreateInfo info;
475 447
476 static const char* kDownloadIdDomain = "Test download id domain"; 448 static const char* kDownloadIdDomain = "Test download id domain";
477 449
478 // Args are ignored except for download id, so everything else can be 450 // Args are ignored except for download id, so everything else can be
479 // null. 451 // null.
480 int id = next_download_id_; 452 int id = next_download_id_;
481 ++next_download_id_; 453 ++next_download_id_;
482 info.download_id = content::DownloadId(kDownloadIdDomain, id); 454 info.download_id = content::DownloadId(kDownloadIdDomain, id);
483 info.request_handle = DownloadRequestHandle(); 455 info.request_handle = DownloadRequestHandle();
484 download_manager_->CreateDownloadItem(&info); 456 EXPECT_CALL(GetMockObserver(),
457 OnDownloadCreated(download_manager_.get(), _));
458 download_manager_->CreateDownloadItem(&info, net::BoundNetLog());
485 459
486 DCHECK(mock_download_item_factory_->GetItem(id)); 460 DCHECK(mock_download_item_factory_->GetItem(id));
487 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); 461 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id));
488 ON_CALL(item, GetId()) 462 // Satisfy expectation. If the item is created in StartDownload(),
489 .WillByDefault(Return(id)); 463 // we call Start on it immediately, so we need to set that expectation
464 // in the factory.
465 item.Start(scoped_ptr<content::DownloadFile>());
490 466
491 return item; 467 return item;
492 } 468 }
493 469
494 MockDownloadItemImpl& GetMockDownloadItem(int id) { 470 MockDownloadItemImpl& GetMockDownloadItem(int id) {
495 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); 471 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id);
496 472
497 DCHECK(itemp); 473 DCHECK(itemp);
498 return *itemp; 474 return *itemp;
499 } 475 }
500 476
501 void RemoveMockDownloadItem(int id) { 477 void RemoveMockDownloadItem(int id) {
502 // Owned by DownloadManager; should be deleted there. 478 // Owned by DownloadManager; should be deleted there.
503 mock_download_item_factory_->RemoveItem(id); 479 mock_download_item_factory_->RemoveItem(id);
504 } 480 }
505 481
506 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() { 482 MockDownloadManagerDelegate& GetMockDownloadManagerDelegate() {
507 return *mock_download_manager_delegate_; 483 return *mock_download_manager_delegate_;
508 } 484 }
509 485
510 MockDownloadFileManager& GetMockDownloadFileManager() {
511 return *mock_download_file_manager_;
512 }
513
514 MockDownloadManagerObserver& GetMockObserver() { 486 MockDownloadManagerObserver& GetMockObserver() {
515 return *observer_; 487 return *observer_;
516 } 488 }
517 489
518 // Probe at private internals. 490 // Probe at private internals.
519 void DownloadStopped(DownloadItemImpl* item) { 491 void DownloadStopped(DownloadItemImpl* item) {
520 download_manager_->DownloadStopped(item); 492 download_manager_->DownloadStopped(item);
521 } 493 }
522 494
495 void DelegateStart(DownloadItemImpl* item) {
496 download_manager_->DelegateStart(item);
497 }
498
523 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) { 499 void AddItemToHistory(MockDownloadItemImpl& item, int64 db_handle) {
524 // For DCHECK in AddDownloadItemToHistory. Don't want to use 500 // For DCHECK in AddDownloadItemToHistory. Don't want to use
525 // WillRepeatedly as it may have to return true after this. 501 // WillRepeatedly as it may have to return true after this.
526 if (DCHECK_IS_ON()) 502 if (DCHECK_IS_ON())
527 EXPECT_CALL(item, IsPersisted()) 503 EXPECT_CALL(item, IsPersisted())
528 .WillRepeatedly(Return(false)); 504 .WillRepeatedly(Return(false));
529 505
530 EXPECT_CALL(item, SetDbHandle(db_handle)); 506 EXPECT_CALL(item, SetDbHandle(db_handle));
531 EXPECT_CALL(item, SetIsPersisted()); 507 EXPECT_CALL(item, SetIsPersisted());
532 EXPECT_CALL(item, GetDbHandle()) 508 EXPECT_CALL(item, GetDbHandle())
(...skipping 19 matching lines...) Expand all
552 protected: 528 protected:
553 // Key test variable; we'll keep it available to sub-classes. 529 // Key test variable; we'll keep it available to sub-classes.
554 scoped_refptr<DownloadManagerImpl> download_manager_; 530 scoped_refptr<DownloadManagerImpl> download_manager_;
555 531
556 private: 532 private:
557 MessageLoopForUI message_loop_; 533 MessageLoopForUI message_loop_;
558 content::TestBrowserThread ui_thread_; 534 content::TestBrowserThread ui_thread_;
559 content::TestBrowserThread file_thread_; 535 content::TestBrowserThread file_thread_;
560 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; 536 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_;
561 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; 537 scoped_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_;
562 scoped_refptr<MockDownloadFileManager> mock_download_file_manager_;
563 scoped_ptr<MockBrowserContext> mock_browser_context_; 538 scoped_ptr<MockBrowserContext> mock_browser_context_;
564 scoped_ptr<MockDownloadManagerObserver> observer_; 539 scoped_ptr<MockDownloadManagerObserver> observer_;
565 int next_download_id_; 540 int next_download_id_;
566 541
567 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 542 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
568 }; 543 };
569 544
570 // Confirm the appropriate invocations occur when you start a download. 545 // Confirm the appropriate invocations occur when you start a download.
571 TEST_F(DownloadManagerTest, StartDownload) { 546 TEST_F(DownloadManagerTest, StartDownload) {
572 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 547 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
573 scoped_ptr<content::ByteStreamReader> stream; 548 scoped_ptr<content::ByteStreamReader> stream;
574 int32 local_id(5); // Random value 549 int32 local_id(5); // Random value
575 550
576 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id)); 551 EXPECT_FALSE(download_manager_->GetActiveDownloadItem(local_id));
577 552
578 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) 553 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
579 .WillOnce(Return()); 554 .WillOnce(Return());
580 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId()) 555 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId())
581 .WillOnce(Return(content::DownloadId(this, local_id))); 556 .WillOnce(Return(content::DownloadId(this, local_id)));
582 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) 557 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash())
583 .WillOnce(Return(true)); 558 .WillOnce(Return(true));
584 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile(
585 info.get(), static_cast<content::ByteStreamReader*>(NULL),
586 download_manager_.get(), true, _, _));
587 559
588 download_manager_->StartDownload(info.Pass(), stream.Pass()); 560 download_manager_->StartDownload(info.Pass(), stream.Pass());
589 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id)); 561 EXPECT_TRUE(download_manager_->GetActiveDownloadItem(local_id));
590 } 562 }
591 563
592 // Do the results of an OnDownloadInterrupted get passed through properly 564 // Confirm that calling DelegateStart behaves properly if the delegate
593 // to the DownloadItem? 565 // blocks starting.
594 TEST_F(DownloadManagerTest, OnDownloadInterrupted) { 566 TEST_F(DownloadManagerTest, DelegateStart_True) {
595 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
596 .WillOnce(Return());
597 // Put a mock we have a handle to on the download manager. 567 // Put a mock we have a handle to on the download manager.
598 MockDownloadItemImpl& item(AddItemToManager()); 568 MockDownloadItemImpl& item(AddItemToManager());
599 int download_id = item.GetId();
600 569
601 content::DownloadInterruptReason reason( 570 EXPECT_CALL(GetMockDownloadManagerDelegate(),
602 content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 571 DetermineDownloadTarget(&item, _))
572 .WillOnce(Return(true));
573 DelegateStart(&item);
574 }
603 575
604 EXPECT_CALL(item, Interrupt(reason)); 576 // Confirm that calling DelegateStart behaves properly if the delegate
605 download_manager_->OnDownloadInterrupted(download_id, reason); 577 // allows starting. This also tests OnDownloadTargetDetermined.
606 EXPECT_EQ(&item, download_manager_->GetActiveDownloadItem(download_id)); 578 TEST_F(DownloadManagerTest, DelegateStart_False) {
579 // Put a mock we have a handle to on the download manager.
580 MockDownloadItemImpl& item(AddItemToManager());
581
582 EXPECT_CALL(GetMockDownloadManagerDelegate(),
583 DetermineDownloadTarget(&item, _))
584 .WillOnce(Return(false));
585 EXPECT_CALL(item, OnDownloadTargetDetermined(
586 _, content::DownloadItem::TARGET_DISPOSITION_OVERWRITE,
587 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, _));
588 FilePath null_path;
589 EXPECT_CALL(item, GetForcedFilePath())
590 .WillOnce(ReturnRef(null_path));
591 DelegateStart(&item);
607 } 592 }
608 593
609 // Does DownloadStopped remove Download from appropriate queues? 594 // Does DownloadStopped remove Download from appropriate queues?
610 // This test tests non-persisted downloads. 595 // This test tests non-persisted downloads.
611 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) { 596 TEST_F(DownloadManagerTest, OnDownloadStopped_NonPersisted) {
612 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
613 .WillOnce(Return());
614 // Put a mock we have a handle to on the download manager. 597 // Put a mock we have a handle to on the download manager.
615 MockDownloadItemImpl& item(AddItemToManager()); 598 MockDownloadItemImpl& item(AddItemToManager());
616 599
617 EXPECT_CALL(item, IsPersisted()) 600 EXPECT_CALL(item, IsPersisted())
618 .WillRepeatedly(Return(false)); 601 .WillRepeatedly(Return(false));
619 EXPECT_CALL(item, GetState()) 602 EXPECT_CALL(item, GetState())
620 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 603 .WillRepeatedly(Return(DownloadItem::CANCELLED));
621 EXPECT_CALL(item, GetDbHandle()) 604 EXPECT_CALL(item, GetDbHandle())
622 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle)); 605 .WillRepeatedly(Return(DownloadItem::kUninitializedHandle));
623 606
624 EXPECT_CALL(item, OffThreadCancel());
625 DownloadStopped(&item); 607 DownloadStopped(&item);
626 // TODO(rdsmith): Confirm that the download item is no longer on the 608 // TODO(rdsmith): Confirm that the download item is no longer on the
627 // active list by calling download_manager_->GetActiveDownloadItem(id). 609 // active list by calling download_manager_->GetActiveDownloadItem(id).
628 // Currently, the item is left on the active list for rendez-vous with 610 // Currently, the item is left on the active list for rendez-vous with
629 // the history system :-{. 611 // the history system :-{.
630 } 612 }
631 613
632 // Does DownloadStopped remove Download from appropriate queues? 614 // Does DownloadStopped remove Download from appropriate queues?
633 // This test tests persisted downloads. 615 // This test tests persisted downloads.
634 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) { 616 TEST_F(DownloadManagerTest, OnDownloadStopped_Persisted) {
635 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _))
636 .WillOnce(Return());
637 // Put a mock we have a handle to on the download manager. 617 // Put a mock we have a handle to on the download manager.
638 MockDownloadItemImpl& item(AddItemToManager()); 618 MockDownloadItemImpl& item(AddItemToManager());
639 int download_id = item.GetId(); 619 int download_id = item.GetId();
640 int64 db_handle = 0x7; 620 int64 db_handle = 0x7;
641 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get())) 621 EXPECT_CALL(GetMockObserver(), ModelChanged(download_manager_.get()))
642 .WillOnce(Return()); 622 .WillOnce(Return());
643 AddItemToHistory(item, db_handle); 623 AddItemToHistory(item, db_handle);
644 624
645 EXPECT_CALL(item, IsPersisted()) 625 EXPECT_CALL(item, IsPersisted())
646 .WillRepeatedly(Return(true)); 626 .WillRepeatedly(Return(true));
647 EXPECT_CALL(GetMockDownloadManagerDelegate(), 627 EXPECT_CALL(GetMockDownloadManagerDelegate(),
648 UpdateItemInPersistentStore(&item)); 628 UpdateItemInPersistentStore(&item));
649 EXPECT_CALL(item, GetState()) 629 EXPECT_CALL(item, GetState())
650 .WillRepeatedly(Return(DownloadItem::CANCELLED)); 630 .WillRepeatedly(Return(DownloadItem::CANCELLED));
651 EXPECT_CALL(item, GetDbHandle()) 631 EXPECT_CALL(item, GetDbHandle())
652 .WillRepeatedly(Return(db_handle)); 632 .WillRepeatedly(Return(db_handle));
653 633
654 EXPECT_CALL(item, OffThreadCancel());
655 DownloadStopped(&item); 634 DownloadStopped(&item);
656 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id)); 635 EXPECT_EQ(NULL, download_manager_->GetActiveDownloadItem(download_id));
657 } 636 }
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | content/browser/download/download_resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698