Chromium Code Reviews| Index: components/offline_pages/downloads/download_notifying_observer_unittest.cc |
| diff --git a/components/offline_pages/downloads/download_notifying_observer_unittest.cc b/components/offline_pages/downloads/download_notifying_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d4dd4dad3f0ea22286ee68d462cb71419120055a |
| --- /dev/null |
| +++ b/components/offline_pages/downloads/download_notifying_observer_unittest.cc |
| @@ -0,0 +1,228 @@ |
| +// Copyright 2016 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/offline_pages/downloads/download_notifying_observer.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "components/offline_pages/background/save_page_request.h" |
| +#include "components/offline_pages/client_namespace_constants.h" |
| +#include "components/offline_pages/downloads/offline_page_download_notifier.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace offline_pages { |
| + |
| +namespace { |
| +static const int64_t kTestOfflineId = 42L; |
| +static const char kTestUrl[] = "http://foo.com/bar"; |
| +static const char kTestGuid[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; |
| +static const ClientId kTestClientId(kDownloadNamespace, kTestGuid); |
| +static const base::Time kTestCreationTime = base::Time::Now(); |
| +static const bool kTestUserRequested = true; |
| + |
| +enum class LastNotificationType { |
| + NONE, |
| + DOWNLOAD_SUCCESSFUL, |
| + DOWNLOAD_FAILED, |
| + DOWNLOAD_PROGRESS, |
| + DOWNLOAD_PAUSED, |
| + DOWNLOAD_INTERRUPTED, |
| + DOWNLOAD_CANCELED, |
| +}; |
| + |
| +class TestNotifier : public OfflinePageDownloadNotifier { |
| + public: |
| + TestNotifier(); |
| + ~TestNotifier() override; |
| + |
| + // OfflinePageDownloadNotifier implementation: |
| + void NotifyDownloadSuccessful(const DownloadUIItem& item) override; |
| + void NotifyDownloadFailed(const DownloadUIItem& item) override; |
| + void NotifyDownloadProgress(const DownloadUIItem& item) override; |
| + void NotifyDownloadPaused(const DownloadUIItem& item) override; |
| + void NotifyDownloadInterrupted(const DownloadUIItem& item) override; |
| + void NotifyDownloadCanceled(const DownloadUIItem& item) override; |
| + |
| + void Reset(); |
| + |
| + LastNotificationType last_notification_type() const { |
| + return last_notification_type_; |
| + } |
| + |
| + DownloadUIItem* download_item() const { return download_item_.get(); } |
| + |
| + private: |
| + LastNotificationType last_notification_type_; |
| + std::unique_ptr<DownloadUIItem> download_item_; |
| +}; |
| + |
| +TestNotifier::TestNotifier() |
| + : last_notification_type_(LastNotificationType::NONE) {} |
| + |
| +TestNotifier::~TestNotifier() {} |
| + |
| +void TestNotifier::NotifyDownloadSuccessful(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_SUCCESSFUL; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::NotifyDownloadFailed(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_FAILED; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::NotifyDownloadProgress(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_PROGRESS; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::NotifyDownloadPaused(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_PAUSED; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::NotifyDownloadInterrupted(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_INTERRUPTED; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::NotifyDownloadCanceled(const DownloadUIItem& item) { |
| + last_notification_type_ = LastNotificationType::DOWNLOAD_CANCELED; |
| + download_item_.reset(new DownloadUIItem(item)); |
| +} |
| + |
| +void TestNotifier::Reset() { |
| + last_notification_type_ = LastNotificationType::NONE; |
| + download_item_.reset(nullptr); |
| +} |
| + |
| +} // namespace |
| + |
| +class DownloadNotifyingObserverTest : public testing::Test { |
| + public: |
| + DownloadNotifyingObserverTest(); |
| + ~DownloadNotifyingObserverTest() override; |
| + |
| + // testing::Test implementation: |
| + void SetUp() override; |
| + |
| + TestNotifier* notifier() const { return notifier_; } |
| + DownloadNotifyingObserver* observer() { return observer_.get(); } |
| + |
| + private: |
| + TestNotifier* notifier_; |
| + std::unique_ptr<DownloadNotifyingObserver> observer_; |
| +}; |
| + |
| +DownloadNotifyingObserverTest::DownloadNotifyingObserverTest() {} |
| + |
| +DownloadNotifyingObserverTest::~DownloadNotifyingObserverTest() {} |
| + |
| +void DownloadNotifyingObserverTest::SetUp() { |
| + std::unique_ptr<TestNotifier> notifier(new TestNotifier); |
| + notifier_ = notifier.get(); |
| + observer_.reset(new DownloadNotifyingObserver(std::move(notifier))); |
| +} |
| + |
| +TEST_F(DownloadNotifyingObserverTest, OnAdded) { |
| + SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| + kTestCreationTime, kTestUserRequested); |
| + observer()->OnAdded(request); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| +} |
| + |
| +TEST_F(DownloadNotifyingObserverTest, OnChangedToPaused) { |
| + SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| + kTestCreationTime, kTestUserRequested); |
| + request.set_request_state(SavePageRequest::RequestState::PAUSED); |
| + observer()->OnChanged(request); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_PAUSED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| +} |
| + |
| +TEST_F(DownloadNotifyingObserverTest, OnChangedToAvailable) { |
| + SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| + kTestCreationTime, kTestUserRequested); |
| + request.set_request_state(SavePageRequest::RequestState::AVAILABLE); |
| + observer()->OnChanged(request); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| +} |
| + |
| +TEST_F(DownloadNotifyingObserverTest, OnCompletedSuccess) { |
| + SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| + kTestCreationTime, kTestUserRequested); |
| + observer()->OnCompleted(request, RequestNotifier::SavePageStatus::SUCCESS); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_SUCCESSFUL, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| +} |
| + |
| +TEST_F(DownloadNotifyingObserverTest, OnCompletedFailure) { |
| + SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| + kTestCreationTime, kTestUserRequested); |
| + observer()->OnCompleted(request, |
| + RequestNotifier::SavePageStatus::PRERENDER_FAILURE); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| + |
| + notifier()->Reset(); |
| + observer()->OnCompleted(request, |
| + RequestNotifier::SavePageStatus::FOREGROUND_CANCELED); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| + |
| + notifier()->Reset(); |
| + observer()->OnCompleted(request, |
| + RequestNotifier::SavePageStatus::SAVE_FAILED); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| + |
| + notifier()->Reset(); |
| + observer()->OnCompleted(request, RequestNotifier::SavePageStatus::EXPIRED); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| + |
| + notifier()->Reset(); |
| + observer()->OnCompleted( |
| + request, RequestNotifier::SavePageStatus::RETRY_COUNT_EXCEEDED); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| + |
| + notifier()->Reset(); |
| + observer()->OnCompleted(request, RequestNotifier::SavePageStatus::REMOVED); |
| + EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| + notifier()->last_notification_type()); |
| + EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| + EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| + EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| +} |
| + |
|
Pete Williamson
2016/08/24 17:02:41
It might be good to add a test for the deleted cas
fgorski
2016/08/24 18:25:23
Not sure what you mean. I think REMOVED covered ab
|
| +} // namespace offline_pages |