| 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<DownloadRequestHandleInterface> req_handle, |
| 112 scoped_ptr<DownloadRequestHandleInterface> req_handle) OVERRIDE { | 112 const DownloadCreateInfo& create_info) OVERRIDE { |
| 113 MockStart(download_file.get(), req_handle.get()); | 113 MockStart(download_file.get(), req_handle.get()); |
| 114 } | 114 } |
| 115 | 115 |
| 116 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); | 116 MOCK_METHOD2(MockStart, void(DownloadFile*, DownloadRequestHandleInterface*)); |
| 117 | 117 |
| 118 MOCK_METHOD0(Remove, void()); | 118 MOCK_METHOD0(Remove, void()); |
| 119 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); | 119 MOCK_CONST_METHOD1(TimeRemaining, bool(base::TimeDelta*)); |
| 120 MOCK_CONST_METHOD0(CurrentSpeed, int64()); | 120 MOCK_CONST_METHOD0(CurrentSpeed, int64()); |
| 121 MOCK_CONST_METHOD0(PercentComplete, int()); | 121 MOCK_CONST_METHOD0(PercentComplete, int()); |
| 122 MOCK_CONST_METHOD0(AllDataSaved, bool()); | 122 MOCK_CONST_METHOD0(AllDataSaved, bool()); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 } | 357 } |
| 358 | 358 |
| 359 class MockDownloadFileFactory | 359 class MockDownloadFileFactory |
| 360 : public DownloadFileFactory, | 360 : public DownloadFileFactory, |
| 361 public base::SupportsWeakPtr<MockDownloadFileFactory> { | 361 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
| 362 public: | 362 public: |
| 363 MockDownloadFileFactory() {} | 363 MockDownloadFileFactory() {} |
| 364 virtual ~MockDownloadFileFactory() {} | 364 virtual ~MockDownloadFileFactory() {} |
| 365 | 365 |
| 366 // Overridden method from DownloadFileFactory | 366 // Overridden method from DownloadFileFactory |
| 367 MOCK_METHOD8(MockCreateFile, MockDownloadFile*( | 367 MOCK_METHOD3(MockCreateFile, |
| 368 const DownloadSaveInfo&, | 368 MockDownloadFile*(const DownloadSaveInfo&, |
| 369 const base::FilePath&, | 369 bool, |
| 370 const GURL&, const GURL&, bool, | 370 ByteStreamReader*)); |
| 371 ByteStreamReader*, | |
| 372 const net::BoundNetLog&, | |
| 373 base::WeakPtr<DownloadDestinationObserver>)); | |
| 374 | 371 |
| 375 virtual DownloadFile* CreateFile( | 372 virtual DownloadFile* CreateFile( |
| 376 scoped_ptr<DownloadSaveInfo> save_info, | 373 const DownloadSaveInfo& save_info, |
| 377 const base::FilePath& default_download_directory, | 374 const base::FilePath& default_download_directory, |
| 378 const GURL& url, | 375 const GURL& url, |
| 379 const GURL& referrer_url, | 376 const GURL& referrer_url, |
| 380 bool calculate_hash, | 377 bool calculate_hash, |
| 381 scoped_ptr<ByteStreamReader> stream, | 378 scoped_ptr<net::FileStream> file_stream, |
| 379 scoped_ptr<ByteStreamReader> byte_stream, |
| 382 const net::BoundNetLog& bound_net_log, | 380 const net::BoundNetLog& bound_net_log, |
| 383 base::WeakPtr<DownloadDestinationObserver> observer) { | 381 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 384 return MockCreateFile(*save_info.get(), default_download_directory, url, | 382 return MockCreateFile(save_info, calculate_hash, byte_stream.get()); |
| 385 referrer_url, calculate_hash, | |
| 386 stream.get(), bound_net_log, observer); | |
| 387 } | 383 } |
| 388 }; | 384 }; |
| 389 | 385 |
| 390 class MockBrowserContext : public BrowserContext { | 386 class MockBrowserContext : public BrowserContext { |
| 391 public: | 387 public: |
| 392 MockBrowserContext() {} | 388 MockBrowserContext() {} |
| 393 ~MockBrowserContext() {} | 389 ~MockBrowserContext() {} |
| 394 | 390 |
| 395 MOCK_CONST_METHOD0(GetPath, base::FilePath()); | 391 MOCK_CONST_METHOD0(GetPath, base::FilePath()); |
| 396 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); | 392 MOCK_CONST_METHOD0(IsOffTheRecord, bool()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 public: | 432 public: |
| 437 MockDownloadManagerObserver() {} | 433 MockDownloadManagerObserver() {} |
| 438 ~MockDownloadManagerObserver() {} | 434 ~MockDownloadManagerObserver() {} |
| 439 MOCK_METHOD2(OnDownloadCreated, void( | 435 MOCK_METHOD2(OnDownloadCreated, void( |
| 440 DownloadManager*, DownloadItem*)); | 436 DownloadManager*, DownloadItem*)); |
| 441 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*)); | 437 MOCK_METHOD1(ManagerGoingDown, void(DownloadManager*)); |
| 442 MOCK_METHOD2(SelectFileDialogDisplayed, void( | 438 MOCK_METHOD2(SelectFileDialogDisplayed, void( |
| 443 DownloadManager*, int32)); | 439 DownloadManager*, int32)); |
| 444 }; | 440 }; |
| 445 | 441 |
| 442 class MockByteStreamReader : public ByteStreamReader { |
| 443 public: |
| 444 virtual ~MockByteStreamReader() {} |
| 445 MOCK_METHOD2(Read, StreamState(scoped_refptr<net::IOBuffer>*, size_t*)); |
| 446 MOCK_CONST_METHOD0(GetStatus, int()); |
| 447 MOCK_METHOD1(RegisterCallback, void(const base::Closure&)); |
| 448 }; |
| 449 |
| 446 } // namespace | 450 } // namespace |
| 447 | 451 |
| 448 class DownloadManagerTest : public testing::Test { | 452 class DownloadManagerTest : public testing::Test { |
| 449 public: | 453 public: |
| 450 static const char* kTestData; | 454 static const char* kTestData; |
| 451 static const size_t kTestDataLen; | 455 static const size_t kTestDataLen; |
| 452 | 456 |
| 453 DownloadManagerTest() | 457 DownloadManagerTest() |
| 454 : callback_called_(false), | 458 : callback_called_(false), |
| 455 ui_thread_(BrowserThread::UI, &message_loop_), | 459 ui_thread_(BrowserThread::UI, &message_loop_), |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 uint32 id = next_download_id_; | 521 uint32 id = next_download_id_; |
| 518 ++next_download_id_; | 522 ++next_download_id_; |
| 519 info.request_handle = DownloadRequestHandle(); | 523 info.request_handle = DownloadRequestHandle(); |
| 520 download_manager_->CreateActiveItem(id, info); | 524 download_manager_->CreateActiveItem(id, info); |
| 521 DCHECK(mock_download_item_factory_->GetItem(id)); | 525 DCHECK(mock_download_item_factory_->GetItem(id)); |
| 522 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); | 526 MockDownloadItemImpl& item(*mock_download_item_factory_->GetItem(id)); |
| 523 // Satisfy expectation. If the item is created in StartDownload(), | 527 // Satisfy expectation. If the item is created in StartDownload(), |
| 524 // we call Start on it immediately, so we need to set that expectation | 528 // we call Start on it immediately, so we need to set that expectation |
| 525 // in the factory. | 529 // in the factory. |
| 526 scoped_ptr<DownloadRequestHandleInterface> req_handle; | 530 scoped_ptr<DownloadRequestHandleInterface> req_handle; |
| 527 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass()); | 531 item.Start(scoped_ptr<DownloadFile>(), req_handle.Pass(), info); |
| 528 | 532 |
| 529 return item; | 533 return item; |
| 530 } | 534 } |
| 531 | 535 |
| 532 MockDownloadItemImpl& GetMockDownloadItem(int id) { | 536 MockDownloadItemImpl& GetMockDownloadItem(int id) { |
| 533 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); | 537 MockDownloadItemImpl* itemp = mock_download_item_factory_->GetItem(id); |
| 534 | 538 |
| 535 DCHECK(itemp); | 539 DCHECK(itemp); |
| 536 return *itemp; | 540 return *itemp; |
| 537 } | 541 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 scoped_ptr<MockBrowserContext> mock_browser_context_; | 593 scoped_ptr<MockBrowserContext> mock_browser_context_; |
| 590 scoped_ptr<MockDownloadManagerObserver> observer_; | 594 scoped_ptr<MockDownloadManagerObserver> observer_; |
| 591 uint32 next_download_id_; | 595 uint32 next_download_id_; |
| 592 | 596 |
| 593 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 597 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 594 }; | 598 }; |
| 595 | 599 |
| 596 // Confirm the appropriate invocations occur when you start a download. | 600 // Confirm the appropriate invocations occur when you start a download. |
| 597 TEST_F(DownloadManagerTest, StartDownload) { | 601 TEST_F(DownloadManagerTest, StartDownload) { |
| 598 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 602 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 599 scoped_ptr<ByteStreamReader> stream; | 603 scoped_ptr<ByteStreamReader> stream(new MockByteStreamReader); |
| 600 uint32 local_id(5); // Random value | 604 uint32 local_id(5); // Random value |
| 601 base::FilePath download_path(FILE_PATH_LITERAL("download/path")); | 605 base::FilePath download_path(FILE_PATH_LITERAL("download/path")); |
| 602 | 606 |
| 603 EXPECT_FALSE(download_manager_->GetDownload(local_id)); | 607 EXPECT_FALSE(download_manager_->GetDownload(local_id)); |
| 604 | 608 |
| 605 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) | 609 EXPECT_CALL(GetMockObserver(), OnDownloadCreated(download_manager_.get(), _)) |
| 606 .WillOnce(Return()); | 610 .WillOnce(Return()); |
| 607 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) | 611 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetNextId(_)) |
| 608 .WillOnce(RunCallback<0>(local_id)); | 612 .WillOnce(RunCallback<0>(local_id)); |
| 609 | 613 |
| 610 // Doing nothing will set the default download directory to null. | 614 // Doing nothing will set the default download directory to null. |
| 611 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); | 615 EXPECT_CALL(GetMockDownloadManagerDelegate(), GetSaveDir(_, _, _, _)); |
| 612 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | 616 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) |
| 613 .WillOnce(Return(true)); | 617 .WillOnce(Return(true)); |
| 614 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 618 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 615 ApplicationClientIdForFileScanning()) | 619 ApplicationClientIdForFileScanning()) |
| 616 .WillRepeatedly(Return("client-id")); | 620 .WillRepeatedly(Return("client-id")); |
| 617 MockDownloadFile* mock_file = new MockDownloadFile; | 621 MockDownloadFile* mock_file = new MockDownloadFile; |
| 618 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); | 622 EXPECT_CALL(*mock_file, SetClientGuid("client-id")); |
| 619 EXPECT_CALL(*mock_download_file_factory_.get(), | 623 EXPECT_CALL(*mock_download_file_factory_.get(), |
| 620 MockCreateFile(Ref(*info->save_info.get()), _, _, _, true, | 624 MockCreateFile(Ref(*info->save_info.get()), true, stream.get())) |
| 621 stream.get(), _, _)) | |
| 622 .WillOnce(Return(mock_file)); | 625 .WillOnce(Return(mock_file)); |
| 623 | 626 |
| 624 download_manager_->StartDownload( | 627 download_manager_->StartDownload( |
| 625 info.Pass(), stream.Pass(), DownloadUrlParameters::OnStartedCallback()); | 628 info.Pass(), stream.Pass(), DownloadUrlParameters::OnStartedCallback()); |
| 626 EXPECT_TRUE(download_manager_->GetDownload(local_id)); | 629 EXPECT_TRUE(download_manager_->GetDownload(local_id)); |
| 627 } | 630 } |
| 628 | 631 |
| 629 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate | 632 // Confirm that calling DetermineDownloadTarget behaves properly if the delegate |
| 630 // blocks starting. | 633 // blocks starting. |
| 631 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { | 634 TEST_F(DownloadManagerTest, DetermineDownloadTarget_True) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 .WillOnce(Return()); | 695 .WillOnce(Return()); |
| 693 EXPECT_CALL(GetMockDownloadItem(3), Remove()) | 696 EXPECT_CALL(GetMockDownloadItem(3), Remove()) |
| 694 .Times(0); | 697 .Times(0); |
| 695 | 698 |
| 696 download_manager_->RemoveAllDownloads(); | 699 download_manager_->RemoveAllDownloads(); |
| 697 // Because we're mocking the download item, the Remove call doesn't | 700 // Because we're mocking the download item, the Remove call doesn't |
| 698 // result in them being removed from the DownloadManager list. | 701 // result in them being removed from the DownloadManager list. |
| 699 } | 702 } |
| 700 | 703 |
| 701 } // namespace content | 704 } // namespace content |
| OLD | NEW |