OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/download/content/download_driver_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "content/public/test/fake_download_item.h" |
| 12 #include "content/public/test/mock_download_manager.h" |
| 13 #include "net/http/http_response_headers.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using testing::_; |
| 18 using testing::NiceMock; |
| 19 using testing::Return; |
| 20 |
| 21 namespace download { |
| 22 |
| 23 namespace { |
| 24 |
| 25 const char kFakeGuid[] = "fake_guid"; |
| 26 |
| 27 // Matcher to compare driver entries. Not all the memeber fields are compared. |
| 28 // Currently no comparison in non test code, so no operator== override for |
| 29 // driver entry. |
| 30 MATCHER_P(DriverEntryEuqual, entry, "") { |
| 31 return entry.guid == arg.guid && entry.state == arg.state && |
| 32 entry.bytes_downloaded == arg.bytes_downloaded; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class MockDriverClient : public DownloadDriver::Client { |
| 38 public: |
| 39 MOCK_METHOD0(OnDriverReady, void()); |
| 40 MOCK_METHOD1(OnDownloadCreated, void(const DriverEntry&)); |
| 41 MOCK_METHOD2(OnDownloadFailed, void(const DriverEntry&, int)); |
| 42 MOCK_METHOD2(OnDownloadSucceeded, |
| 43 void(const DriverEntry&, const base::FilePath&)); |
| 44 MOCK_METHOD1(OnDownloadUpdated, void(const DriverEntry&)); |
| 45 }; |
| 46 |
| 47 class DownloadDriverImplTest : public testing::Test { |
| 48 public: |
| 49 DownloadDriverImplTest() = default; |
| 50 ~DownloadDriverImplTest() override = default; |
| 51 |
| 52 void SetUp() override { |
| 53 driver_ = base::MakeUnique<DownloadDriverImpl>(&mock_manager_, |
| 54 base::FilePath("/")); |
| 55 } |
| 56 |
| 57 // TODO(xingliu): implements test download manager for embedders to test. |
| 58 NiceMock<content::MockDownloadManager> mock_manager_; |
| 59 MockDriverClient mock_client_; |
| 60 std::unique_ptr<DownloadDriverImpl> driver_; |
| 61 |
| 62 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(DownloadDriverImplTest); |
| 64 }; |
| 65 |
| 66 // Ensure the download manager can be initialized after the download driver. |
| 67 TEST_F(DownloadDriverImplTest, ManagerLateInitialization) { |
| 68 EXPECT_CALL(mock_manager_, IsManagerInitialized()) |
| 69 .Times(1) |
| 70 .WillOnce(Return(false)); |
| 71 driver_->Initialize(&mock_client_); |
| 72 |
| 73 EXPECT_CALL(mock_client_, OnDriverReady()); |
| 74 static_cast<content::DownloadManager::Observer*>(driver_.get()) |
| 75 ->OnManagerInitialized(); |
| 76 } |
| 77 |
| 78 // Ensures download updates from download items are propagated correctly. |
| 79 TEST_F(DownloadDriverImplTest, DownloadItemUpdateEvents) { |
| 80 using DownloadState = content::DownloadItem::DownloadState; |
| 81 using DownloadInterruptReason = content::DownloadInterruptReason; |
| 82 |
| 83 EXPECT_CALL(mock_manager_, IsManagerInitialized()) |
| 84 .Times(1) |
| 85 .WillOnce(Return(true)); |
| 86 EXPECT_CALL(mock_client_, OnDriverReady()).Times(1); |
| 87 driver_->Initialize(&mock_client_); |
| 88 |
| 89 content::FakeDownloadItem fake_item; |
| 90 fake_item.SetState(DownloadState::IN_PROGRESS); |
| 91 fake_item.SetGuid(kFakeGuid); |
| 92 fake_item.SetReceivedBytes(0); |
| 93 fake_item.SetTotalBytes(1024); |
| 94 DriverEntry entry = DownloadDriverImpl::CreateDriverEntry(&fake_item); |
| 95 |
| 96 EXPECT_CALL(mock_client_, OnDownloadUpdated(DriverEntryEuqual(entry))) |
| 97 .Times(1) |
| 98 .RetiresOnSaturation(); |
| 99 static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| 100 ->OnDownloadUpdated(&fake_item); |
| 101 |
| 102 // Nothing happens for cancelled state. |
| 103 fake_item.SetState(DownloadState::CANCELLED); |
| 104 static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| 105 ->OnDownloadUpdated(&fake_item); |
| 106 |
| 107 fake_item.SetReceivedBytes(1024); |
| 108 fake_item.SetState(DownloadState::COMPLETE); |
| 109 entry = DownloadDriverImpl::CreateDriverEntry(&fake_item); |
| 110 EXPECT_CALL(mock_client_, OnDownloadSucceeded(DriverEntryEuqual(entry), _)) |
| 111 .Times(1) |
| 112 .RetiresOnSaturation(); |
| 113 static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| 114 ->OnDownloadUpdated(&fake_item); |
| 115 |
| 116 fake_item.SetState(DownloadState::INTERRUPTED); |
| 117 fake_item.SetLastReason( |
| 118 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT); |
| 119 entry = DownloadDriverImpl::CreateDriverEntry(&fake_item); |
| 120 int reason = static_cast<int>( |
| 121 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT); |
| 122 EXPECT_CALL(mock_client_, OnDownloadFailed(DriverEntryEuqual(entry), reason)) |
| 123 .Times(1) |
| 124 .RetiresOnSaturation(); |
| 125 static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| 126 ->OnDownloadUpdated(&fake_item); |
| 127 } |
| 128 |
| 129 } // namespace download |
OLD | NEW |