| 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 "content/browser/download/download_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 MOCK_METHOD0(OpenDownload, void()); | 110 MOCK_METHOD0(OpenDownload, void()); |
| 111 MOCK_METHOD0(ShowDownloadInShell, void()); | 111 MOCK_METHOD0(ShowDownloadInShell, void()); |
| 112 MOCK_METHOD0(ValidateDangerousDownload, void()); | 112 MOCK_METHOD0(ValidateDangerousDownload, void()); |
| 113 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); | 113 MOCK_METHOD1(StealDangerousDownload, void(const AcquireFileCallback&)); |
| 114 MOCK_METHOD3(UpdateProgress, void(int64_t, int64_t, const std::string&)); | 114 MOCK_METHOD3(UpdateProgress, void(int64_t, int64_t, const std::string&)); |
| 115 MOCK_METHOD1(Cancel, void(bool)); | 115 MOCK_METHOD1(Cancel, void(bool)); |
| 116 MOCK_METHOD0(MarkAsComplete, void()); | 116 MOCK_METHOD0(MarkAsComplete, void()); |
| 117 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); | 117 MOCK_METHOD1(OnAllDataSaved, void(const std::string&)); |
| 118 MOCK_METHOD0(OnDownloadedFileRemoved, void()); | 118 MOCK_METHOD0(OnDownloadedFileRemoved, void()); |
| 119 void Start(scoped_ptr<DownloadFile> download_file, | 119 void Start(scoped_ptr<DownloadFile> download_file, |
| 120 scoped_ptr<DownloadRequestHandleInterface> req_handle) override { | 120 scoped_ptr<DownloadRequestHandleInterface> req_handle, |
| 121 const DownloadCreateInfo& create_info) override { |
| 121 MockStart(download_file.get(), req_handle.get()); | 122 MockStart(download_file.get(), req_handle.get()); |
| 122 } | 123 } |
| 123 | 124 |
| 124 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); | 125 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); |
| 125 | 126 |
| 126 MOCK_METHOD0(Remove, void()); | 127 MOCK_METHOD0(Remove, void()); |
| 127 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); | 128 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); |
| 128 MOCK_CONST_METHOD0(CurrentSpeed, int64_t()); | 129 MOCK_CONST_METHOD0(CurrentSpeed, int64_t()); |
| 129 MOCK_CONST_METHOD0(PercentComplete, int()); | 130 MOCK_CONST_METHOD0(PercentComplete, int()); |
| 130 MOCK_CONST_METHOD0(AllDataSaved, bool()); | 131 MOCK_CONST_METHOD0(AllDataSaved, bool()); |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 } | 372 } |
| 372 | 373 |
| 373 class MockDownloadFileFactory | 374 class MockDownloadFileFactory |
| 374 : public DownloadFileFactory, | 375 : public DownloadFileFactory, |
| 375 public base::SupportsWeakPtr<MockDownloadFileFactory> { | 376 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
| 376 public: | 377 public: |
| 377 MockDownloadFileFactory() {} | 378 MockDownloadFileFactory() {} |
| 378 virtual ~MockDownloadFileFactory() {} | 379 virtual ~MockDownloadFileFactory() {} |
| 379 | 380 |
| 380 // Overridden method from DownloadFileFactory | 381 // Overridden method from DownloadFileFactory |
| 381 MOCK_METHOD8(MockCreateFile, MockDownloadFile*( | 382 MOCK_METHOD3(MockCreateFile, |
| 382 const DownloadSaveInfo&, | 383 MockDownloadFile*(const DownloadSaveInfo&, |
| 383 const base::FilePath&, | 384 bool, |
| 384 const GURL&, const GURL&, bool, | 385 ByteStreamReader*)); |
| 385 ByteStreamReader*, | |
| 386 const net::BoundNetLog&, | |
| 387 base::WeakPtr<DownloadDestinationObserver>)); | |
| 388 | 386 |
| 389 virtual DownloadFile* CreateFile( | 387 virtual DownloadFile* CreateFile( |
| 390 scoped_ptr<DownloadSaveInfo> save_info, | 388 const DownloadSaveInfo& save_info, |
| 391 const base::FilePath& default_download_directory, | 389 const base::FilePath& default_download_directory, |
| 392 const GURL& url, | 390 const GURL& url, |
| 393 const GURL& referrer_url, | 391 const GURL& referrer_url, |
| 394 bool calculate_hash, | 392 bool calculate_hash, |
| 395 scoped_ptr<ByteStreamReader> stream, | 393 base::File file_stream, |
| 394 scoped_ptr<ByteStreamReader> byte_stream, |
| 396 const net::BoundNetLog& bound_net_log, | 395 const net::BoundNetLog& bound_net_log, |
| 397 base::WeakPtr<DownloadDestinationObserver> observer) { | 396 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 398 return MockCreateFile(*save_info.get(), default_download_directory, url, | 397 return MockCreateFile(save_info, calculate_hash, byte_stream.get()); |
| 399 referrer_url, calculate_hash, | |
| 400 stream.get(), bound_net_log, observer); | |
| 401 } | 398 } |
| 402 }; | 399 }; |
| 403 | 400 |
| 404 class MockBrowserContext : public BrowserContext { | 401 class MockBrowserContext : public BrowserContext { |
| 405 public: | 402 public: |
| 406 MockBrowserContext() {} | 403 MockBrowserContext() {} |
| 407 ~MockBrowserContext() {} | 404 ~MockBrowserContext() {} |
| 408 | 405 |
| 409 MOCK_CONST_METHOD0(GetPath, base::FilePath()); | 406 MOCK_CONST_METHOD0(GetPath, base::FilePath()); |
| 410 MOCK_METHOD1(CreateZoomLevelDelegateMock, | 407 MOCK_METHOD1(CreateZoomLevelDelegateMock, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 438 class MockDownloadManagerObserver : public DownloadManager::Observer { | 435 class MockDownloadManagerObserver : public DownloadManager::Observer { |
| 439 public: | 436 public: |
| 440 MockDownloadManagerObserver() {} | 437 MockDownloadManagerObserver() {} |
| 441 ~MockDownloadManagerObserver() {} | 438 ~MockDownloadManagerObserver() {} |
| 442 MOCK_METHOD2(OnDownloadCreated, void( | 439 MOCK_METHOD2(OnDownloadCreated, void( |
| 443 DownloadManager*, DownloadItem*)); | 440 DownloadManager*, DownloadItem*)); |
| 444 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*)); | 441 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*)); |
| 445 MOCK_METHOD2(SelectFileDialogDisplayed, void(DownloadManager*, int32_t)); | 442 MOCK_METHOD2(SelectFileDialogDisplayed, void(DownloadManager*, int32_t)); |
| 446 }; | 443 }; |
| 447 | 444 |
| 445 class MockByteStreamReader : public ByteStreamReader { |
| 446 public: |
| 447 virtual ~MockByteStreamReader() {} |
| 448 MOCK_METHOD2(Read, StreamState(scoped_refptr<net::IOBuffer>*, size_t*)); |
| 449 MOCK_CONST_METHOD0(GetStatus, int()); |
| 450 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 451 }; |
| 452 |
| 448 } // namespace | 453 } // namespace |
| 449 | 454 |
| 450 class DownloadManagerTest : public testing::Test { | 455 class DownloadManagerTest : public testing::Test { |
| 451 public: | 456 public: |
| 452 static const char* kTestData; | 457 static const char* kTestData; |
| 453 static const size_t kTestDataLen; | 458 static const size_t kTestDataLen; |
| 454 | 459 |
| 455 DownloadManagerTest() | 460 DownloadManagerTest() |
| 456 : callback_called_(false), | 461 : callback_called_(false), |
| 457 ui_thread_(BrowserThread::UI, &message_loop_), | 462 ui_thread_(BrowserThread::UI, &message_loop_), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 522 uint32_t id = next_download_id_; | 527 uint32_t id = next_download_id_; |
| 523 ++next_download_id_; | 528 ++next_download_id_; |
| 524 info.request_handle.reset(new DownloadRequestHandle); | 529 info.request_handle.reset(new DownloadRequestHandle); |
| 525 download_manager_->CreateActiveItem(id, info); | 530 download_manager_->CreateActiveItem(id, info); |
| 526 DCHECK(mock_download_item_factory_->GetItem(id)); | 531 DCHECK(mock_download_item_factory_->GetItem(id)); |
| 527 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 532 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
| 528 // Satisfy expectation. If the item is created in StartDownload(), | 533 // Satisfy expectation. If the item is created in StartDownload(), |
| 529 // we call Start on it immediately, so we need to set that expectation | 534 // we call Start on it immediately, so we need to set that expectation |
| 530 // in the factory. | 535 // in the factory. |
| 531 scoped_ptr<DownloadRequestHandleInterface> req_handle; | 536 scoped_ptr<DownloadRequestHandleInterface> req_handle; |
| 532 item.Start(scoped_ptr<DownloadFile>(), std::move(req_handle)); | 537 item.Start(scoped_ptr<DownloadFile>(), std::move(req_handle), info); |
| 533 DCHECK(id < download_urls_.size()); | 538 DCHECK(id < download_urls_.size()); |
| 534 EXPECT_CALL(item, GetURL()).WillRepeatedly(ReturnRef(download_urls_[id])); | 539 EXPECT_CALL(item, GetURL()).WillRepeatedly(ReturnRef(download_urls_[id])); |
| 535 | 540 |
| 536 return item; | 541 return item; |
| 537 } | 542 } |
| 538 | 543 |
| 539 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 544 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
| 540 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 545 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
| 541 | 546 |
| 542 DCHECK(itemp); | 547 DCHECK(itemp); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 scoped_ptr<MockBrowserContext> mock_browser_context_; | 603 scoped_ptr<MockBrowserContext> mock_browser_context_; |
| 599 scoped_ptr<MockDownloadManagerObserver> observer_; | 604 scoped_ptr<MockDownloadManagerObserver> observer_; |
| 600 uint32_t next_download_id_; | 605 uint32_t next_download_id_; |
| 601 | 606 |
| 602 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 607 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 603 }; | 608 }; |
| 604 | 609 |
| 605 // Confirm the appropriate invocations occur when you start a download. | 610 // Confirm the appropriate invocations occur when you start a download. |
| 606 TEST_F(DownloadManagerTest, StartDownload) { | 611 TEST_F(DownloadManagerTest, StartDownload) { |
| 607 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 612 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 608 scoped_ptr<ByteStreamReader> stream; | 613 scoped_ptr<ByteStreamReader> stream(new MockByteStreamReader); |
| 609 uint32_t local_id(5); // Random value | 614 uint32_t local_id(5); // Random value |
| 610 base::FilePath download_path(FILE_PATH_LITERAL("download/path")); | 615 base::FilePath download_path(FILE_PATH_LITERAL("download/path")); |
| 611 | 616 |
| 612 EXPECT_FALSE(download_manager_->GetDownload(local_id)); | 617 EXPECT_FALSE(download_manager_->GetDownload(local_id)); |
| 613 | 618 |
| 614 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | 619 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) |
| 615 .WillOnce(Return()); | 620 .WillOnce(Return()); |
| 616 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) | 621 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) |
| 617 .WillOnce(RunCallback<0>(local_id)); | 622 .WillOnce(RunCallback<0>(local_id)); |
| 618 | 623 |
| 619 // Doing nothing will set the default download directory to null. | 624 // Doing nothing will set the default download directory to null. |
| 620 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); | 625 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); |
| 621 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | 626 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) |
| 622 .WillOnce(Return(true)); | 627 .WillOnce(Return(true)); |
| 623 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 628 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 624 ApplicationClientIdForFileScanning()) | 629 ApplicationClientIdForFileScanning()) |
| 625 .WillRepeatedly(Return("client-id")); | 630 .WillRepeatedly(Return("client-id")); |
| 626 MockDownloadFile* mock_file = new MockDownloadFile; | 631 MockDownloadFile* mock_file = new MockDownloadFile; |
| 627 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); | 632 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); |
| 628 EXPECT_CALL(*mock_download_file_factory_.get(), | 633 EXPECT_CALL(*mock_download_file_factory_.get(), |
| 629 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, | 634 MockCreateFile(Ref(*info->save_info.get()), true, stream.get())) |
| 630 stream.get(), _, _)) | |
| 631 .WillOnce(Return(mock_file)); | 635 .WillOnce(Return(mock_file)); |
| 632 | 636 |
| 633 download_manager_->StartDownload(std::move(info), std::move(stream), | 637 download_manager_->StartDownload(std::move(info), std::move(stream), |
| 634 DownloadUrlParameters::OnStartedCallback()); | 638 DownloadUrlParameters::OnStartedCallback()); |
| 635 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 639 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
| 636 } | 640 } |
| 637 | 641 |
| 638 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate | 642 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
| 639 // blocks starting. | 643 // blocks starting. |
| 640 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { | 644 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 EXPECT_CALL(GetMockDownloadItem(0), Remove()); | 724 EXPECT_CALL(GetMockDownloadItem(0), Remove()); |
| 721 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); | 725 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); |
| 722 | 726 |
| 723 url::Origin origin_to_clear(download_urls_[0]); | 727 url::Origin origin_to_clear(download_urls_[0]); |
| 724 int remove_count = download_manager_->RemoveDownloadsByOriginAndTime( | 728 int remove_count = download_manager_->RemoveDownloadsByOriginAndTime( |
| 725 origin_to_clear, base::Time(), base::Time::Max()); | 729 origin_to_clear, base::Time(), base::Time::Max()); |
| 726 EXPECT_EQ(remove_count, 1); | 730 EXPECT_EQ(remove_count, 1); |
| 727 } | 731 } |
| 728 | 732 |
| 729 } // namespace content | 733 } // namespace content |
| OLD | NEW |