OLD | NEW |
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/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); | 100 MOCK_METHOD0(ShouldOpenFileBasedOnExtension, bool()); |
101 MOCK_METHOD0(OpenDownload, void()); | 101 MOCK_METHOD0(OpenDownload, void()); |
102 MOCK_METHOD0(ShowDownloadInShell, void()); | 102 MOCK_METHOD0(ShowDownloadInShell, void()); |
103 MOCK_METHOD0(ValidateDangerousDownload, void()); | 103 MOCK_METHOD0(ValidateDangerousDownload, void()); |
104 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); | 104 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); |
105 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); | 105 MOCK_METHOD3(UpdateProgress, void(int64, int64, const std::string&)); |
106 MOCK_METHOD1(Cancel, void(bool)); | 106 MOCK_METHOD1(Cancel, void(bool)); |
107 MOCK_METHOD0(MarkAsComplete, void()); | 107 MOCK_METHOD0(MarkAsComplete, void()); |
108 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); | 108 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); |
109 MOCK_METHOD0(OnDownloadedFileRemoved, void()); | 109 MOCK_METHOD0(OnDownloadedFileRemoved, void()); |
110 virtual void Start( | 110 virtual void Start(scoped_ptr<DownloadFile> download_file, |
111 scoped_ptr<DownloadFile> download_file, | 111 scoped_ptr<DownloadRequestHandle> req_handle) OVERRIDE { |
112 scoped_ptr<DownloadRequestHandleInterface> req_handle) OVERRIDE { | |
113 MockStart(download_file.get(), req_handle.get()); | 112 MockStart(download_file.get(), req_handle.get()); |
114 } | 113 } |
115 | 114 |
116 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); | 115 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandle*)); |
117 | 116 |
118 MOCK_METHOD0(Remove, void()); | 117 MOCK_METHOD0(Remove, void()); |
119 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); | 118 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); |
120 MOCK_CONST_METHOD0(CurrentSpeed, int64()); | 119 MOCK_CONST_METHOD0(CurrentSpeed, int64()); |
121 MOCK_CONST_METHOD0(PercentComplete, int()); | 120 MOCK_CONST_METHOD0(PercentComplete, int()); |
122 MOCK_CONST_METHOD0(AllDataSaved, bool()); | 121 MOCK_CONST_METHOD0(AllDataSaved, bool()); |
123 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); | 122 MOCK_CONST_METHOD1(MatchesQuery, bool(const string16& query)); |
124 MOCK_CONST_METHOD0(IsDone, bool()); | 123 MOCK_CONST_METHOD0(IsDone, bool()); |
125 MOCK_CONST_METHOD0(GetFullPath, const base::FilePath&()); | 124 MOCK_CONST_METHOD0(GetFullPath, const base::FilePath&()); |
126 MOCK_CONST_METHOD0(GetTargetFilePath, const base::FilePath&()); | 125 MOCK_CONST_METHOD0(GetTargetFilePath, const base::FilePath&()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 const base::Time& end_time, | 235 const base::Time& end_time, |
237 const std::string& etag, | 236 const std::string& etag, |
238 const std::string& last_modofied, | 237 const std::string& last_modofied, |
239 int64 received_bytes, | 238 int64 received_bytes, |
240 int64 total_bytes, | 239 int64 total_bytes, |
241 DownloadItem::DownloadState state, | 240 DownloadItem::DownloadState state, |
242 DownloadDangerType danger_type, | 241 DownloadDangerType danger_type, |
243 DownloadInterruptReason interrupt_reason, | 242 DownloadInterruptReason interrupt_reason, |
244 bool opened, | 243 bool opened, |
245 const net::BoundNetLog& bound_net_log) OVERRIDE; | 244 const net::BoundNetLog& bound_net_log) OVERRIDE; |
| 245 |
246 virtual DownloadItemImpl* CreateActiveItem( | 246 virtual DownloadItemImpl* CreateActiveItem( |
247 DownloadItemImplDelegate* delegate, | 247 DownloadItemImplDelegate* delegate, |
248 uint32 download_id, | 248 uint32 download_id, |
249 const DownloadCreateInfo& info, | 249 const DownloadCreateInfo& info, |
250 const net::BoundNetLog& bound_net_log) OVERRIDE; | 250 const net::BoundNetLog& bound_net_log) OVERRIDE; |
251 virtual DownloadItemImpl* CreateSavePageItem( | |
252 DownloadItemImplDelegate* delegate, | |
253 uint32 download_id, | |
254 const base::FilePath& path, | |
255 const GURL& url, | |
256 const std::string& mime_type, | |
257 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
258 const net::BoundNetLog& bound_net_log) OVERRIDE; | |
259 | 251 |
260 private: | 252 private: |
261 std::map<uint32, MockDownloadItemImpl*> items_; | 253 std::map<uint32, MockDownloadItemImpl*> items_; |
262 DownloadItemImplDelegate item_delegate_; | 254 DownloadItemImplDelegate item_delegate_; |
263 | 255 |
264 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); | 256 DISALLOW_COPY_AND_ASSIGN(MockDownloadItemFactory); |
265 }; | 257 }; |
266 | 258 |
267 MockDownloadItemFactory::MockDownloadItemFactory() {} | 259 MockDownloadItemFactory::MockDownloadItemFactory() {} |
268 | 260 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 .WillRepeatedly(Return(download_id)); | 322 .WillRepeatedly(Return(download_id)); |
331 items_[download_id] = result; | 323 items_[download_id] = result; |
332 | 324 |
333 // Active items are created and then immediately are called to start | 325 // Active items are created and then immediately are called to start |
334 // the download. | 326 // the download. |
335 EXPECT_CALL(*result, MockStart(_, _)); | 327 EXPECT_CALL(*result, MockStart(_, _)); |
336 | 328 |
337 return result; | 329 return result; |
338 } | 330 } |
339 | 331 |
340 DownloadItemImpl* MockDownloadItemFactory::CreateSavePageItem( | |
341 DownloadItemImplDelegate* delegate, | |
342 uint32 download_id, | |
343 const base::FilePath& path, | |
344 const GURL& url, | |
345 const std::string& mime_type, | |
346 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
347 const net::BoundNetLog& bound_net_log) { | |
348 DCHECK(items_.find(download_id) == items_.end()); | |
349 | |
350 MockDownloadItemImpl* result = | |
351 new StrictMock<MockDownloadItemImpl>(&item_delegate_); | |
352 EXPECT_CALL(*result, GetId()) | |
353 .WillRepeatedly(Return(download_id)); | |
354 items_[download_id] = result; | |
355 | |
356 return result; | |
357 } | |
358 | |
359 class MockDownloadFileFactory | 332 class MockDownloadFileFactory |
360 : public DownloadFileFactory, | 333 : public DownloadFileFactory, |
361 public base::SupportsWeakPtr<MockDownloadFileFactory> { | 334 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
362 public: | 335 public: |
363 MockDownloadFileFactory() {} | 336 MockDownloadFileFactory() {} |
364 virtual ~MockDownloadFileFactory() {} | 337 virtual ~MockDownloadFileFactory() {} |
365 | 338 |
366 // Overridden method from DownloadFileFactory | 339 // Overridden method from DownloadFileFactory |
367 MOCK_METHOD8(MockCreateFile, MockDownloadFile*( | 340 MOCK_METHOD8(MockCreateFile, MockDownloadFile*( |
368 const DownloadSaveInfo&, | 341 const DownloadSaveInfo&, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 466 } |
494 | 467 |
495 // Returns download id. | 468 // Returns download id. |
496 MockDownloadItemImpl& AddItemToManager() { | 469 MockDownloadItemImpl& AddItemToManager() { |
497 DownloadCreateInfo info; | 470 DownloadCreateInfo info; |
498 | 471 |
499 // Args are ignored except for download id, so everything else can be | 472 // Args are ignored except for download id, so everything else can be |
500 // null. | 473 // null. |
501 uint32 id = next_download_id_; | 474 uint32 id = next_download_id_; |
502 ++next_download_id_; | 475 ++next_download_id_; |
503 info.request_handle = DownloadRequestHandle(); | |
504 download_manager_->CreateActiveItem(id, info); | 476 download_manager_->CreateActiveItem(id, info); |
505 DCHECK(mock_download_item_factory_->GetItem(id)); | 477 DCHECK(mock_download_item_factory_->GetItem(id)); |
506 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 478 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
507 // Satisfy expectation. If the item is created in StartDownload(), | 479 // Satisfy expectation. If the item is created in StartDownload(), |
508 // we call Start on it immediately, so we need to set that expectation | 480 // we call Start on it immediately, so we need to set that expectation |
509 // in the factory. | 481 // in the factory. |
510 scoped_ptr<DownloadRequestHandleInterface> req_handle; | 482 scoped_ptr<DownloadRequestHandle> req_handle; |
511 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass()); | 483 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass()); |
512 | 484 |
513 return item; | 485 return item; |
514 } | 486 } |
515 | 487 |
516 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 488 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
517 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 489 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
518 | 490 |
519 DCHECK(itemp); | 491 DCHECK(itemp); |
520 return *itemp; | 492 return *itemp; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 570 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
599 ApplicationClientIdForFileScanning()) | 571 ApplicationClientIdForFileScanning()) |
600 .WillRepeatedly(Return("client-id")); | 572 .WillRepeatedly(Return("client-id")); |
601 MockDownloadFile* mock_file = new MockDownloadFile; | 573 MockDownloadFile* mock_file = new MockDownloadFile; |
602 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); | 574 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); |
603 EXPECT_CALL(*mock_download_file_factory_.get(), | 575 EXPECT_CALL(*mock_download_file_factory_.get(), |
604 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, | 576 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, |
605 stream.get(), _, _)) | 577 stream.get(), _, _)) |
606 .WillOnce(Return(mock_file)); | 578 .WillOnce(Return(mock_file)); |
607 | 579 |
608 download_manager_->StartDownload( | 580 download_manager_->StartDownloadWithActiveRequest( |
609 info.Pass(), stream.Pass(), DownloadUrlParameters::OnStartedCallback()); | 581 scoped_ptr<DownloadRequestHandle>(), info.Pass()); |
610 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 582 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
611 } | 583 } |
612 | 584 |
613 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate | 585 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
614 // blocks starting. | 586 // blocks starting. |
615 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { | 587 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
616 // Put a mock we have a handle to on the download manager. | 588 // Put a mock we have a handle to on the download manager. |
617 MockDownloadItemImpl& item(AddItemToManager()); | 589 MockDownloadItemImpl& item(AddItemToManager()); |
618 EXPECT_CALL(item, GetState()) | 590 EXPECT_CALL(item, GetState()) |
619 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); | 591 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 .WillOnce(Return()); | 648 .WillOnce(Return()); |
677 EXPECT_CALL(GetMockDownloadItem(3), Remove()) | 649 EXPECT_CALL(GetMockDownloadItem(3), Remove()) |
678 .Times(0); | 650 .Times(0); |
679 | 651 |
680 download_manager_->RemoveAllDownloads(); | 652 download_manager_->RemoveAllDownloads(); |
681 // Because we're mocking the download item, the Remove call doesn't | 653 // Because we're mocking the download item, the Remove call doesn't |
682 // result in them being removed from the DownloadManager list. | 654 // result in them being removed from the DownloadManager list. |
683 } | 655 } |
684 | 656 |
685 } // namespace content | 657 } // namespace content |
OLD | NEW |