Chromium Code Reviews| Index: components/download/content/download_driver_impl_unittest.cc |
| diff --git a/components/download/content/download_driver_impl_unittest.cc b/components/download/content/download_driver_impl_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..09459ef24ca0527e12bf665b3c54b89220e83ff0 |
| --- /dev/null |
| +++ b/components/download/content/download_driver_impl_unittest.cc |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/download/content/download_driver_impl.h" |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "content/public/test/fake_download_item.h" |
| +#include "content/public/test/mock_download_manager.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::NiceMock; |
| +using testing::_; |
| + |
| +namespace download { |
| + |
| +namespace { |
| + |
| +const char kFakeGuid[] = "fake_guid"; |
| + |
| +} // namespace |
| + |
| +class MockDriverObserver : public DownloadDriver::Observer { |
| + public: |
| + MOCK_METHOD0(OnDriverReady, void()); |
| + MOCK_METHOD1(OnDownloadCreated, void(const std::string&)); |
| + MOCK_METHOD2(OnDownloadFailed, void(const std::string&, int)); |
| + MOCK_METHOD3(OnDownloadSucceeded, |
| + void(const std::string&, const base::FilePath&, uint64_t)); |
| + MOCK_METHOD2(OnDownloadUpdated, void(const std::string&, uint64_t)); |
| +}; |
| + |
| +class DownloadDriverImplTest : public testing::Test { |
| + public: |
| + DownloadDriverImplTest() = default; |
| + ~DownloadDriverImplTest() override = default; |
| + |
| + void SetUp() override { |
| + driver_ = base::MakeUnique<DownloadDriverImpl>(&mock_manager_); |
| + } |
| + |
| + // TODO(xingliu): implements test download manager for embedders to test. |
| + NiceMock<content::MockDownloadManager> mock_manager_; |
| + MockDriverObserver mock_observer_; |
| + std::unique_ptr<DownloadDriverImpl> driver_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(DownloadDriverImplTest); |
| +}; |
| + |
| +// Ensures download updates from download items are propagated correctly. |
| +TEST_F(DownloadDriverImplTest, DownloadItemUpdateEvents) { |
|
David Trainor- moved to gerrit
2017/05/15 20:08:15
Can we test things like setup flow as well?
xingliu
2017/05/17 19:34:52
Done.
|
| + using DownloadState = content::DownloadItem::DownloadState; |
| + using DownloadInterruptReason = content::DownloadInterruptReason; |
| + |
| + driver_->SetObserver(&mock_observer_); |
| + |
| + content::FakeDownloadItem fake_item; |
| + fake_item.SetState(DownloadState::IN_PROGRESS); |
| + fake_item.SetGuid(kFakeGuid); |
| + EXPECT_CALL(mock_observer_, OnDownloadUpdated(kFakeGuid, _)) |
| + .Times(1) |
| + .RetiresOnSaturation(); |
| + static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| + ->OnDownloadUpdated(&fake_item); |
| + |
| + // Nothing happens for cancelled state. |
| + fake_item.SetState(DownloadState::CANCELLED); |
| + static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| + ->OnDownloadUpdated(&fake_item); |
| + |
| + fake_item.SetReceivedBytes(1024); |
| + fake_item.SetState(DownloadState::COMPLETE); |
| + EXPECT_CALL(mock_observer_, |
| + OnDownloadSucceeded(kFakeGuid, _, fake_item.GetReceivedBytes())) |
| + .Times(1) |
| + .RetiresOnSaturation(); |
| + static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| + ->OnDownloadUpdated(&fake_item); |
| + |
| + fake_item.SetState(DownloadState::INTERRUPTED); |
| + fake_item.SetLastReason( |
| + DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT); |
| + EXPECT_CALL( |
| + mock_observer_, |
| + OnDownloadFailed( |
| + kFakeGuid, |
| + static_cast<int>(DownloadInterruptReason:: |
| + DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT))) |
| + .Times(1) |
| + .RetiresOnSaturation(); |
| + static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| + ->OnDownloadUpdated(&fake_item); |
| + |
| + // Nothing happens if observer is dettached. |
| + driver_->SetObserver(nullptr); |
| + static_cast<content::DownloadItem::Observer*>(driver_.get()) |
| + ->OnDownloadUpdated(&fake_item); |
| +} |
| + |
| +} // namespace download |