| 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 | 9 |
| 10 #include <map> |
| 10 #include <memory> | 11 #include <memory> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <string> | 13 #include <string> |
| 13 #include <utility> | 14 #include <utility> |
| 14 | 15 |
| 15 #include "base/bind.h" | 16 #include "base/bind.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/guid.h" | 18 #include "base/guid.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 : public DownloadFileFactory, | 284 : public DownloadFileFactory, |
| 284 public base::SupportsWeakPtr<MockDownloadFileFactory> { | 285 public base::SupportsWeakPtr<MockDownloadFileFactory> { |
| 285 public: | 286 public: |
| 286 MockDownloadFileFactory() {} | 287 MockDownloadFileFactory() {} |
| 287 virtual ~MockDownloadFileFactory() {} | 288 virtual ~MockDownloadFileFactory() {} |
| 288 | 289 |
| 289 // Overridden method from DownloadFileFactory | 290 // Overridden method from DownloadFileFactory |
| 290 MOCK_METHOD2(MockCreateFile, | 291 MOCK_METHOD2(MockCreateFile, |
| 291 MockDownloadFile*(const DownloadSaveInfo&, ByteStreamReader*)); | 292 MockDownloadFile*(const DownloadSaveInfo&, ByteStreamReader*)); |
| 292 | 293 |
| 293 virtual DownloadFile* CreateFile( | 294 DownloadFile* CreateFile( |
| 294 std::unique_ptr<DownloadSaveInfo> save_info, | 295 std::unique_ptr<DownloadSaveInfo> save_info, |
| 295 const base::FilePath& default_download_directory, | 296 const base::FilePath& default_download_directory, |
| 296 std::unique_ptr<ByteStreamReader> byte_stream, | 297 std::unique_ptr<ByteStreamReader> byte_stream, |
| 297 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | 298 const std::vector<DownloadItem::ReceivedSlice>& received_slices, |
| 298 const net::NetLogWithSource& net_log, | 299 const net::NetLogWithSource& net_log, |
| 299 base::WeakPtr<DownloadDestinationObserver> observer) override { | 300 base::WeakPtr<DownloadDestinationObserver> observer) override { |
| 300 return MockCreateFile(*save_info, byte_stream.get()); | 301 return MockCreateFile(*save_info, byte_stream.get()); |
| 301 } | 302 } |
| 302 }; | 303 }; |
| 303 | 304 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 | 372 |
| 372 } // namespace | 373 } // namespace |
| 373 | 374 |
| 374 class DownloadManagerTest : public testing::Test { | 375 class DownloadManagerTest : public testing::Test { |
| 375 public: | 376 public: |
| 376 static const char* kTestData; | 377 static const char* kTestData; |
| 377 static const size_t kTestDataLen; | 378 static const size_t kTestDataLen; |
| 378 | 379 |
| 379 DownloadManagerTest() | 380 DownloadManagerTest() |
| 380 : callback_called_(false), | 381 : callback_called_(false), |
| 382 target_disposition_(DownloadItem::TARGET_DISPOSITION_OVERWRITE), |
| 383 danger_type_(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), |
| 384 interrupt_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
| 381 ui_thread_(BrowserThread::UI, &message_loop_), | 385 ui_thread_(BrowserThread::UI, &message_loop_), |
| 382 file_thread_(BrowserThread::FILE, &message_loop_), | 386 file_thread_(BrowserThread::FILE, &message_loop_), |
| 383 next_download_id_(0) { | 387 next_download_id_(0) {} |
| 384 } | |
| 385 | 388 |
| 386 // We tear down everything in TearDown(). | 389 // We tear down everything in TearDown(). |
| 387 ~DownloadManagerTest() override {} | 390 ~DownloadManagerTest() override {} |
| 388 | 391 |
| 389 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate, | 392 // Create a MockDownloadItemFactory and MockDownloadManagerDelegate, |
| 390 // then create a DownloadManager that points | 393 // then create a DownloadManager that points |
| 391 // at all of those. | 394 // at all of those. |
| 392 void SetUp() override { | 395 void SetUp() override { |
| 393 DCHECK(!download_manager_); | 396 DCHECK(!download_manager_); |
| 394 | 397 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 } | 480 } |
| 478 | 481 |
| 479 MockDownloadManagerObserver& GetMockObserver() { | 482 MockDownloadManagerObserver& GetMockObserver() { |
| 480 return *observer_; | 483 return *observer_; |
| 481 } | 484 } |
| 482 | 485 |
| 483 void DownloadTargetDeterminedCallback( | 486 void DownloadTargetDeterminedCallback( |
| 484 const base::FilePath& target_path, | 487 const base::FilePath& target_path, |
| 485 DownloadItem::TargetDisposition disposition, | 488 DownloadItem::TargetDisposition disposition, |
| 486 DownloadDangerType danger_type, | 489 DownloadDangerType danger_type, |
| 487 const base::FilePath& intermediate_path) { | 490 const base::FilePath& intermediate_path, |
| 491 DownloadInterruptReason interrupt_reason) { |
| 488 callback_called_ = true; | 492 callback_called_ = true; |
| 489 target_path_ = target_path; | 493 target_path_ = target_path; |
| 490 target_disposition_ = disposition; | 494 target_disposition_ = disposition; |
| 491 danger_type_ = danger_type; | 495 danger_type_ = danger_type; |
| 492 intermediate_path_ = intermediate_path; | 496 intermediate_path_ = intermediate_path; |
| 497 interrupt_reason_ = interrupt_reason; |
| 493 } | 498 } |
| 494 | 499 |
| 495 void DetermineDownloadTarget(DownloadItemImpl* item) { | 500 void DetermineDownloadTarget(DownloadItemImpl* item) { |
| 496 download_manager_->DetermineDownloadTarget( | 501 download_manager_->DetermineDownloadTarget( |
| 497 item, base::Bind( | 502 item, base::Bind( |
| 498 &DownloadManagerTest::DownloadTargetDeterminedCallback, | 503 &DownloadManagerTest::DownloadTargetDeterminedCallback, |
| 499 base::Unretained(this))); | 504 base::Unretained(this))); |
| 500 } | 505 } |
| 501 | 506 |
| 502 protected: | 507 protected: |
| 503 // Key test variable; we'll keep it available to sub-classes. | 508 // Key test variable; we'll keep it available to sub-classes. |
| 504 std::unique_ptr<DownloadManagerImpl> download_manager_; | 509 std::unique_ptr<DownloadManagerImpl> download_manager_; |
| 505 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; | 510 base::WeakPtr<MockDownloadFileFactory> mock_download_file_factory_; |
| 506 | 511 |
| 507 // Target detetermined callback. | 512 // Target detetermined callback. |
| 508 bool callback_called_; | 513 bool callback_called_; |
| 509 base::FilePath target_path_; | 514 base::FilePath target_path_; |
| 510 DownloadItem::TargetDisposition target_disposition_; | 515 DownloadItem::TargetDisposition target_disposition_; |
| 511 DownloadDangerType danger_type_; | 516 DownloadDangerType danger_type_; |
| 512 base::FilePath intermediate_path_; | 517 base::FilePath intermediate_path_; |
| 518 DownloadInterruptReason interrupt_reason_; |
| 513 | 519 |
| 514 std::vector<GURL> download_urls_; | 520 std::vector<GURL> download_urls_; |
| 515 | 521 |
| 516 private: | 522 private: |
| 517 base::MessageLoopForUI message_loop_; | 523 base::MessageLoopForUI message_loop_; |
| 518 TestBrowserThread ui_thread_; | 524 TestBrowserThread ui_thread_; |
| 519 TestBrowserThread file_thread_; | 525 TestBrowserThread file_thread_; |
| 520 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; | 526 base::WeakPtr<MockDownloadItemFactory> mock_download_item_factory_; |
| 521 std::unique_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; | 527 std::unique_ptr<MockDownloadManagerDelegate> mock_download_manager_delegate_; |
| 522 std::unique_ptr<MockBrowserContext> mock_browser_context_; | 528 std::unique_ptr<MockBrowserContext> mock_browser_context_; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 MockDownloadItemImpl& item(AddItemToManager()); | 582 MockDownloadItemImpl& item(AddItemToManager()); |
| 577 | 583 |
| 578 base::FilePath path(FILE_PATH_LITERAL("random_filepath.txt")); | 584 base::FilePath path(FILE_PATH_LITERAL("random_filepath.txt")); |
| 579 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 585 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 580 DetermineDownloadTarget(&item, _)) | 586 DetermineDownloadTarget(&item, _)) |
| 581 .WillOnce(Return(false)); | 587 .WillOnce(Return(false)); |
| 582 EXPECT_CALL(item, GetForcedFilePath()) | 588 EXPECT_CALL(item, GetForcedFilePath()) |
| 583 .WillOnce(ReturnRef(path)); | 589 .WillOnce(ReturnRef(path)); |
| 584 | 590 |
| 585 // Confirm that the callback was called with the right values in this case. | 591 // Confirm that the callback was called with the right values in this case. |
| 586 callback_called_ = false; | |
| 587 DetermineDownloadTarget(&item); | 592 DetermineDownloadTarget(&item); |
| 588 EXPECT_TRUE(callback_called_); | 593 EXPECT_TRUE(callback_called_); |
| 589 EXPECT_EQ(path, target_path_); | 594 EXPECT_EQ(path, target_path_); |
| 590 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_); | 595 EXPECT_EQ(DownloadItem::TARGET_DISPOSITION_OVERWRITE, target_disposition_); |
| 591 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_); | 596 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, danger_type_); |
| 592 EXPECT_EQ(path, intermediate_path_); | 597 EXPECT_EQ(path, intermediate_path_); |
| 598 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason_); |
| 593 } | 599 } |
| 594 | 600 |
| 595 TEST_F(DownloadManagerTest, GetDownloadByGuid) { | 601 TEST_F(DownloadManagerTest, GetDownloadByGuid) { |
| 596 for (uint32_t i = 0; i < 4; ++i) | 602 for (uint32_t i = 0; i < 4; ++i) |
| 597 AddItemToManager(); | 603 AddItemToManager(); |
| 598 | 604 |
| 599 MockDownloadItemImpl& item = GetMockDownloadItem(0); | 605 MockDownloadItemImpl& item = GetMockDownloadItem(0); |
| 600 DownloadItem* result = download_manager_->GetDownloadByGuid(item.GetGuid()); | 606 DownloadItem* result = download_manager_->GetDownloadByGuid(item.GetGuid()); |
| 601 ASSERT_TRUE(result); | 607 ASSERT_TRUE(result); |
| 602 ASSERT_EQ(static_cast<DownloadItem*>(&item), result); | 608 ASSERT_EQ(static_cast<DownloadItem*>(&item), result); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); | 647 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); |
| 642 | 648 |
| 643 base::Callback<bool(const GURL&)> url_filter = | 649 base::Callback<bool(const GURL&)> url_filter = |
| 644 GetSingleURLFilter(download_urls_[0]); | 650 GetSingleURLFilter(download_urls_[0]); |
| 645 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( | 651 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( |
| 646 url_filter, base::Time(), base::Time::Max()); | 652 url_filter, base::Time(), base::Time::Max()); |
| 647 EXPECT_EQ(remove_count, 1); | 653 EXPECT_EQ(remove_count, 1); |
| 648 } | 654 } |
| 649 | 655 |
| 650 } // namespace content | 656 } // namespace content |
| OLD | NEW |