Chromium Code Reviews| 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 "testing/gmock/include/gmock/gmock.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using testing::NiceMock; | |
| 17 using testing::_; | |
| 18 | |
| 19 namespace download { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char kFakeGuid[] = "fake_guid"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 class MockDriverObserver : public DownloadDriver::Observer { | |
| 28 public: | |
| 29 MOCK_METHOD0(OnDriverReady, void()); | |
| 30 MOCK_METHOD1(OnDownloadCreated, void(const std::string&)); | |
| 31 MOCK_METHOD2(OnDownloadFailed, void(const std::string&, int)); | |
| 32 MOCK_METHOD3(OnDownloadSucceeded, | |
| 33 void(const std::string&, const base::FilePath&, uint64_t)); | |
| 34 MOCK_METHOD2(OnDownloadUpdated, void(const std::string&, uint64_t)); | |
| 35 }; | |
| 36 | |
| 37 class DownloadDriverImplTest : public testing::Test { | |
| 38 public: | |
| 39 DownloadDriverImplTest() = default; | |
| 40 ~DownloadDriverImplTest() override = default; | |
| 41 | |
| 42 void SetUp() override { | |
| 43 driver_ = base::MakeUnique<DownloadDriverImpl>(&mock_manager_); | |
| 44 } | |
| 45 | |
| 46 // TODO(xingliu): implements test download manager for embedders to test. | |
| 47 NiceMock<content::MockDownloadManager> mock_manager_; | |
| 48 MockDriverObserver mock_observer_; | |
| 49 std::unique_ptr<DownloadDriverImpl> driver_; | |
| 50 | |
| 51 private: | |
| 52 DISALLOW_COPY_AND_ASSIGN(DownloadDriverImplTest); | |
| 53 }; | |
| 54 | |
| 55 // Ensures download updates from download items are propagated correctly. | |
| 56 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.
| |
| 57 using DownloadState = content::DownloadItem::DownloadState; | |
| 58 using DownloadInterruptReason = content::DownloadInterruptReason; | |
| 59 | |
| 60 driver_->SetObserver(&mock_observer_); | |
| 61 | |
| 62 content::FakeDownloadItem fake_item; | |
| 63 fake_item.SetState(DownloadState::IN_PROGRESS); | |
| 64 fake_item.SetGuid(kFakeGuid); | |
| 65 EXPECT_CALL(mock_observer_, OnDownloadUpdated(kFakeGuid, _)) | |
| 66 .Times(1) | |
| 67 .RetiresOnSaturation(); | |
| 68 static_cast<content::DownloadItem::Observer*>(driver_.get()) | |
| 69 ->OnDownloadUpdated(&fake_item); | |
| 70 | |
| 71 // Nothing happens for cancelled state. | |
| 72 fake_item.SetState(DownloadState::CANCELLED); | |
| 73 static_cast<content::DownloadItem::Observer*>(driver_.get()) | |
| 74 ->OnDownloadUpdated(&fake_item); | |
| 75 | |
| 76 fake_item.SetReceivedBytes(1024); | |
| 77 fake_item.SetState(DownloadState::COMPLETE); | |
| 78 EXPECT_CALL(mock_observer_, | |
| 79 OnDownloadSucceeded(kFakeGuid, _, fake_item.GetReceivedBytes())) | |
| 80 .Times(1) | |
| 81 .RetiresOnSaturation(); | |
| 82 static_cast<content::DownloadItem::Observer*>(driver_.get()) | |
| 83 ->OnDownloadUpdated(&fake_item); | |
| 84 | |
| 85 fake_item.SetState(DownloadState::INTERRUPTED); | |
| 86 fake_item.SetLastReason( | |
| 87 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT); | |
| 88 EXPECT_CALL( | |
| 89 mock_observer_, | |
| 90 OnDownloadFailed( | |
| 91 kFakeGuid, | |
| 92 static_cast<int>(DownloadInterruptReason:: | |
| 93 DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT))) | |
| 94 .Times(1) | |
| 95 .RetiresOnSaturation(); | |
| 96 static_cast<content::DownloadItem::Observer*>(driver_.get()) | |
| 97 ->OnDownloadUpdated(&fake_item); | |
| 98 | |
| 99 // Nothing happens if observer is dettached. | |
| 100 driver_->SetObserver(nullptr); | |
| 101 static_cast<content::DownloadItem::Observer*>(driver_.get()) | |
| 102 ->OnDownloadUpdated(&fake_item); | |
| 103 } | |
| 104 | |
| 105 } // namespace download | |
| OLD | NEW |