OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/offline_pages/downloads/download_notifying_observer.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/offline_pages/background/save_page_request.h" |
| 9 #include "components/offline_pages/client_namespace_constants.h" |
| 10 #include "components/offline_pages/downloads/offline_page_download_notifier.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace offline_pages { |
| 14 |
| 15 namespace { |
| 16 static const int64_t kTestOfflineId = 42L; |
| 17 static const char kTestUrl[] = "http://foo.com/bar"; |
| 18 static const char kTestGuid[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; |
| 19 static const ClientId kTestClientId(kDownloadNamespace, kTestGuid); |
| 20 static const base::Time kTestCreationTime = base::Time::Now(); |
| 21 static const bool kTestUserRequested = true; |
| 22 |
| 23 enum class LastNotificationType { |
| 24 NONE, |
| 25 DOWNLOAD_SUCCESSFUL, |
| 26 DOWNLOAD_FAILED, |
| 27 DOWNLOAD_PROGRESS, |
| 28 DOWNLOAD_PAUSED, |
| 29 DOWNLOAD_INTERRUPTED, |
| 30 DOWNLOAD_CANCELED, |
| 31 }; |
| 32 |
| 33 class TestNotifier : public OfflinePageDownloadNotifier { |
| 34 public: |
| 35 TestNotifier(); |
| 36 ~TestNotifier() override; |
| 37 |
| 38 // OfflinePageDownloadNotifier implementation: |
| 39 void NotifyDownloadSuccessful(const DownloadUIItem& item) override; |
| 40 void NotifyDownloadFailed(const DownloadUIItem& item) override; |
| 41 void NotifyDownloadProgress(const DownloadUIItem& item) override; |
| 42 void NotifyDownloadPaused(const DownloadUIItem& item) override; |
| 43 void NotifyDownloadInterrupted(const DownloadUIItem& item) override; |
| 44 void NotifyDownloadCanceled(const DownloadUIItem& item) override; |
| 45 |
| 46 void Reset(); |
| 47 |
| 48 LastNotificationType last_notification_type() const { |
| 49 return last_notification_type_; |
| 50 } |
| 51 |
| 52 DownloadUIItem* download_item() const { return download_item_.get(); } |
| 53 |
| 54 private: |
| 55 LastNotificationType last_notification_type_; |
| 56 std::unique_ptr<DownloadUIItem> download_item_; |
| 57 }; |
| 58 |
| 59 TestNotifier::TestNotifier() |
| 60 : last_notification_type_(LastNotificationType::NONE) {} |
| 61 |
| 62 TestNotifier::~TestNotifier() {} |
| 63 |
| 64 void TestNotifier::NotifyDownloadSuccessful(const DownloadUIItem& item) { |
| 65 last_notification_type_ = LastNotificationType::DOWNLOAD_SUCCESSFUL; |
| 66 download_item_.reset(new DownloadUIItem(item)); |
| 67 } |
| 68 |
| 69 void TestNotifier::NotifyDownloadFailed(const DownloadUIItem& item) { |
| 70 last_notification_type_ = LastNotificationType::DOWNLOAD_FAILED; |
| 71 download_item_.reset(new DownloadUIItem(item)); |
| 72 } |
| 73 |
| 74 void TestNotifier::NotifyDownloadProgress(const DownloadUIItem& item) { |
| 75 last_notification_type_ = LastNotificationType::DOWNLOAD_PROGRESS; |
| 76 download_item_.reset(new DownloadUIItem(item)); |
| 77 } |
| 78 |
| 79 void TestNotifier::NotifyDownloadPaused(const DownloadUIItem& item) { |
| 80 last_notification_type_ = LastNotificationType::DOWNLOAD_PAUSED; |
| 81 download_item_.reset(new DownloadUIItem(item)); |
| 82 } |
| 83 |
| 84 void TestNotifier::NotifyDownloadInterrupted(const DownloadUIItem& item) { |
| 85 last_notification_type_ = LastNotificationType::DOWNLOAD_INTERRUPTED; |
| 86 download_item_.reset(new DownloadUIItem(item)); |
| 87 } |
| 88 |
| 89 void TestNotifier::NotifyDownloadCanceled(const DownloadUIItem& item) { |
| 90 last_notification_type_ = LastNotificationType::DOWNLOAD_CANCELED; |
| 91 download_item_.reset(new DownloadUIItem(item)); |
| 92 } |
| 93 |
| 94 void TestNotifier::Reset() { |
| 95 last_notification_type_ = LastNotificationType::NONE; |
| 96 download_item_.reset(nullptr); |
| 97 } |
| 98 |
| 99 } // namespace |
| 100 |
| 101 class DownloadNotifyingObserverTest : public testing::Test { |
| 102 public: |
| 103 DownloadNotifyingObserverTest(); |
| 104 ~DownloadNotifyingObserverTest() override; |
| 105 |
| 106 // testing::Test implementation: |
| 107 void SetUp() override; |
| 108 |
| 109 TestNotifier* notifier() const { return notifier_; } |
| 110 DownloadNotifyingObserver* observer() { return observer_.get(); } |
| 111 |
| 112 private: |
| 113 TestNotifier* notifier_; |
| 114 std::unique_ptr<DownloadNotifyingObserver> observer_; |
| 115 }; |
| 116 |
| 117 DownloadNotifyingObserverTest::DownloadNotifyingObserverTest() {} |
| 118 |
| 119 DownloadNotifyingObserverTest::~DownloadNotifyingObserverTest() {} |
| 120 |
| 121 void DownloadNotifyingObserverTest::SetUp() { |
| 122 std::unique_ptr<TestNotifier> notifier(new TestNotifier); |
| 123 notifier_ = notifier.get(); |
| 124 observer_.reset(new DownloadNotifyingObserver(std::move(notifier))); |
| 125 } |
| 126 |
| 127 TEST_F(DownloadNotifyingObserverTest, OnAdded) { |
| 128 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 129 kTestCreationTime, kTestUserRequested); |
| 130 observer()->OnAdded(request); |
| 131 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, |
| 132 notifier()->last_notification_type()); |
| 133 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 134 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 135 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 136 } |
| 137 |
| 138 TEST_F(DownloadNotifyingObserverTest, OnChangedToPaused) { |
| 139 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 140 kTestCreationTime, kTestUserRequested); |
| 141 request.set_request_state(SavePageRequest::RequestState::PAUSED); |
| 142 observer()->OnChanged(request); |
| 143 EXPECT_EQ(LastNotificationType::DOWNLOAD_PAUSED, |
| 144 notifier()->last_notification_type()); |
| 145 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 146 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 147 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 148 } |
| 149 |
| 150 TEST_F(DownloadNotifyingObserverTest, OnChangedToAvailable) { |
| 151 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 152 kTestCreationTime, kTestUserRequested); |
| 153 request.set_request_state(SavePageRequest::RequestState::AVAILABLE); |
| 154 observer()->OnChanged(request); |
| 155 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, |
| 156 notifier()->last_notification_type()); |
| 157 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 158 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 159 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 160 } |
| 161 |
| 162 TEST_F(DownloadNotifyingObserverTest, OnCompletedSuccess) { |
| 163 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 164 kTestCreationTime, kTestUserRequested); |
| 165 observer()->OnCompleted(request, RequestNotifier::SavePageStatus::SUCCESS); |
| 166 EXPECT_EQ(LastNotificationType::DOWNLOAD_SUCCESSFUL, |
| 167 notifier()->last_notification_type()); |
| 168 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 169 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 170 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 171 } |
| 172 |
| 173 TEST_F(DownloadNotifyingObserverTest, OnCompletedCanceled) { |
| 174 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 175 kTestCreationTime, kTestUserRequested); |
| 176 observer()->OnCompleted(request, RequestNotifier::SavePageStatus::REMOVED); |
| 177 EXPECT_EQ(LastNotificationType::DOWNLOAD_CANCELED, |
| 178 notifier()->last_notification_type()); |
| 179 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 180 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 181 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 182 } |
| 183 |
| 184 TEST_F(DownloadNotifyingObserverTest, OnCompletedFailure) { |
| 185 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, |
| 186 kTestCreationTime, kTestUserRequested); |
| 187 observer()->OnCompleted(request, |
| 188 RequestNotifier::SavePageStatus::PRERENDER_FAILURE); |
| 189 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| 190 notifier()->last_notification_type()); |
| 191 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 192 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 193 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 194 |
| 195 notifier()->Reset(); |
| 196 observer()->OnCompleted(request, |
| 197 RequestNotifier::SavePageStatus::FOREGROUND_CANCELED); |
| 198 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| 199 notifier()->last_notification_type()); |
| 200 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 201 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 202 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 203 |
| 204 notifier()->Reset(); |
| 205 observer()->OnCompleted(request, |
| 206 RequestNotifier::SavePageStatus::SAVE_FAILED); |
| 207 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| 208 notifier()->last_notification_type()); |
| 209 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 210 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 211 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 212 |
| 213 notifier()->Reset(); |
| 214 observer()->OnCompleted(request, RequestNotifier::SavePageStatus::EXPIRED); |
| 215 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| 216 notifier()->last_notification_type()); |
| 217 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 218 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 219 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 220 |
| 221 notifier()->Reset(); |
| 222 observer()->OnCompleted( |
| 223 request, RequestNotifier::SavePageStatus::RETRY_COUNT_EXCEEDED); |
| 224 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, |
| 225 notifier()->last_notification_type()); |
| 226 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); |
| 227 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); |
| 228 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); |
| 229 } |
| 230 |
| 231 } // namespace offline_pages |
OLD | NEW |