Chromium Code Reviews| 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, OnCompletedFailure) { | |
| 174 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
| 175 kTestCreationTime, kTestUserRequested); | |
| 176 observer()->OnCompleted(request, | |
| 177 RequestNotifier::SavePageStatus::PRERENDER_FAILURE); | |
| 178 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 179 notifier()->last_notification_type()); | |
| 180 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 181 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 182 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 183 | |
| 184 notifier()->Reset(); | |
| 185 observer()->OnCompleted(request, | |
| 186 RequestNotifier::SavePageStatus::FOREGROUND_CANCELED); | |
| 187 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 188 notifier()->last_notification_type()); | |
| 189 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 190 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 191 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 192 | |
| 193 notifier()->Reset(); | |
| 194 observer()->OnCompleted(request, | |
| 195 RequestNotifier::SavePageStatus::SAVE_FAILED); | |
| 196 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 197 notifier()->last_notification_type()); | |
| 198 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 199 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 200 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 201 | |
| 202 notifier()->Reset(); | |
| 203 observer()->OnCompleted(request, RequestNotifier::SavePageStatus::EXPIRED); | |
| 204 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 205 notifier()->last_notification_type()); | |
| 206 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 207 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 208 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 209 | |
| 210 notifier()->Reset(); | |
| 211 observer()->OnCompleted( | |
| 212 request, RequestNotifier::SavePageStatus::RETRY_COUNT_EXCEEDED); | |
| 213 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 214 notifier()->last_notification_type()); | |
| 215 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 216 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 217 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 218 | |
| 219 notifier()->Reset(); | |
| 220 observer()->OnCompleted(request, RequestNotifier::SavePageStatus::REMOVED); | |
| 221 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
| 222 notifier()->last_notification_type()); | |
| 223 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
| 224 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
| 225 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
| 226 } | |
| 227 | |
|
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
| |
| 228 } // namespace offline_pages | |
| OLD | NEW |