Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: components/offline_pages/downloads/download_notifying_observer_unittest.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: more rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/client_policy_controller.h"
11 #include "components/offline_pages/downloads/offline_page_download_notifier.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace offline_pages {
15
16 namespace {
17 static const int64_t kTestOfflineId = 42L;
18 static const char kTestUrl[] = "http://foo.com/bar";
19 static const char kTestGuid[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1";
20 static const ClientId kTestClientId(kDownloadNamespace, kTestGuid);
21 static const base::Time kTestCreationTime = base::Time::Now();
22 static const bool kTestUserRequested = true;
23
24 enum class LastNotificationType {
25 NONE,
26 DOWNLOAD_SUCCESSFUL,
27 DOWNLOAD_FAILED,
28 DOWNLOAD_PROGRESS,
29 DOWNLOAD_PAUSED,
30 DOWNLOAD_INTERRUPTED,
31 DOWNLOAD_CANCELED,
32 };
33
34 class TestNotifier : public OfflinePageDownloadNotifier {
35 public:
36 TestNotifier();
37 ~TestNotifier() override;
38
39 // OfflinePageDownloadNotifier implementation:
40 void NotifyDownloadSuccessful(const DownloadUIItem& item) override;
41 void NotifyDownloadFailed(const DownloadUIItem& item) override;
42 void NotifyDownloadProgress(const DownloadUIItem& item) override;
43 void NotifyDownloadPaused(const DownloadUIItem& item) override;
44 void NotifyDownloadInterrupted(const DownloadUIItem& item) override;
45 void NotifyDownloadCanceled(const DownloadUIItem& item) override;
46
47 void Reset();
48
49 LastNotificationType last_notification_type() const {
50 return last_notification_type_;
51 }
52
53 DownloadUIItem* download_item() const { return download_item_.get(); }
54
55 private:
56 LastNotificationType last_notification_type_;
57 std::unique_ptr<DownloadUIItem> download_item_;
58 };
59
60 TestNotifier::TestNotifier()
61 : last_notification_type_(LastNotificationType::NONE) {}
62
63 TestNotifier::~TestNotifier() {}
64
65 void TestNotifier::NotifyDownloadSuccessful(const DownloadUIItem& item) {
66 last_notification_type_ = LastNotificationType::DOWNLOAD_SUCCESSFUL;
67 download_item_.reset(new DownloadUIItem(item));
68 }
69
70 void TestNotifier::NotifyDownloadFailed(const DownloadUIItem& item) {
71 last_notification_type_ = LastNotificationType::DOWNLOAD_FAILED;
72 download_item_.reset(new DownloadUIItem(item));
73 }
74
75 void TestNotifier::NotifyDownloadProgress(const DownloadUIItem& item) {
76 last_notification_type_ = LastNotificationType::DOWNLOAD_PROGRESS;
77 download_item_.reset(new DownloadUIItem(item));
78 }
79
80 void TestNotifier::NotifyDownloadPaused(const DownloadUIItem& item) {
81 last_notification_type_ = LastNotificationType::DOWNLOAD_PAUSED;
82 download_item_.reset(new DownloadUIItem(item));
83 }
84
85 void TestNotifier::NotifyDownloadInterrupted(const DownloadUIItem& item) {
86 last_notification_type_ = LastNotificationType::DOWNLOAD_INTERRUPTED;
87 download_item_.reset(new DownloadUIItem(item));
88 }
89
90 void TestNotifier::NotifyDownloadCanceled(const DownloadUIItem& item) {
91 last_notification_type_ = LastNotificationType::DOWNLOAD_CANCELED;
92 download_item_.reset(new DownloadUIItem(item));
93 }
94
95 void TestNotifier::Reset() {
96 last_notification_type_ = LastNotificationType::NONE;
97 download_item_.reset(nullptr);
98 }
99
100 } // namespace
101
102 class DownloadNotifyingObserverTest : public testing::Test {
103 public:
104 DownloadNotifyingObserverTest();
105 ~DownloadNotifyingObserverTest() override;
106
107 // testing::Test implementation:
108 void SetUp() override;
109
110 TestNotifier* notifier() const { return notifier_; }
111 DownloadNotifyingObserver* observer() { return observer_.get(); }
112
113 private:
114 TestNotifier* notifier_;
115 std::unique_ptr<DownloadNotifyingObserver> observer_;
116 std::unique_ptr<ClientPolicyController> policy_controller_;
117 };
118
119 DownloadNotifyingObserverTest::DownloadNotifyingObserverTest() {}
120
121 DownloadNotifyingObserverTest::~DownloadNotifyingObserverTest() {}
122
123 void DownloadNotifyingObserverTest::SetUp() {
124 std::unique_ptr<TestNotifier> notifier(new TestNotifier);
125 policy_controller_.reset(new ClientPolicyController());
126 notifier_ = notifier.get();
127 observer_.reset(new DownloadNotifyingObserver(std::move(notifier),
128 policy_controller_.get()));
129 }
130
131 TEST_F(DownloadNotifyingObserverTest, OnAdded) {
132 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
133 kTestCreationTime, kTestUserRequested);
134 observer()->OnAdded(request);
135 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS,
136 notifier()->last_notification_type());
137 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
138 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
139 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
140 }
141
142 TEST_F(DownloadNotifyingObserverTest, OnChangedToPaused) {
143 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
144 kTestCreationTime, kTestUserRequested);
145 request.set_request_state(SavePageRequest::RequestState::PAUSED);
146 observer()->OnChanged(request);
147 EXPECT_EQ(LastNotificationType::DOWNLOAD_PAUSED,
148 notifier()->last_notification_type());
149 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
150 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
151 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
152 }
153
154 TEST_F(DownloadNotifyingObserverTest, OnChangedToAvailable) {
155 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
156 kTestCreationTime, kTestUserRequested);
157 request.set_request_state(SavePageRequest::RequestState::AVAILABLE);
158 observer()->OnChanged(request);
159 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS,
160 notifier()->last_notification_type());
161 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
162 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
163 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
164 }
165
166 TEST_F(DownloadNotifyingObserverTest, OnCompletedSuccess) {
167 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
168 kTestCreationTime, kTestUserRequested);
169 observer()->OnCompleted(request,
170 RequestNotifier::BackgroundSavePageResult::SUCCESS);
171 EXPECT_EQ(LastNotificationType::DOWNLOAD_SUCCESSFUL,
172 notifier()->last_notification_type());
173 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
174 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
175 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
176 }
177
178 TEST_F(DownloadNotifyingObserverTest, OnCompletedCanceled) {
179 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
180 kTestCreationTime, kTestUserRequested);
181 observer()->OnCompleted(request,
182 RequestNotifier::BackgroundSavePageResult::REMOVED);
183 EXPECT_EQ(LastNotificationType::DOWNLOAD_CANCELED,
184 notifier()->last_notification_type());
185 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
186 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
187 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
188 }
189
190 TEST_F(DownloadNotifyingObserverTest, OnCompletedFailure) {
191 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId,
192 kTestCreationTime, kTestUserRequested);
193 observer()->OnCompleted(
194 request, RequestNotifier::BackgroundSavePageResult::PRERENDER_FAILURE);
195 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED,
196 notifier()->last_notification_type());
197 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
198 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
199 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
200
201 notifier()->Reset();
202 observer()->OnCompleted(
203 request, RequestNotifier::BackgroundSavePageResult::FOREGROUND_CANCELED);
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::BackgroundSavePageResult::SAVE_FAILED);
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,
221 RequestNotifier::BackgroundSavePageResult::EXPIRED);
222 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED,
223 notifier()->last_notification_type());
224 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
225 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
226 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
227
228 notifier()->Reset();
229 observer()->OnCompleted(
230 request, RequestNotifier::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED);
231 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED,
232 notifier()->last_notification_type());
233 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid);
234 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url);
235 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time);
236 }
237
238 TEST_F(DownloadNotifyingObserverTest, NamespacesNotVisibleInUI) {
239 std::vector<std::string> name_spaces = {kBookmarkNamespace, kLastNNamespace,
240 kCCTNamespace, kDefaultNamespace};
241
242 for (auto name_space : name_spaces) {
243 ClientId invisible_client_id(name_space, kTestGuid);
244 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), invisible_client_id,
245 kTestCreationTime, kTestUserRequested);
246 observer()->OnAdded(request);
247 EXPECT_EQ(LastNotificationType::NONE, notifier()->last_notification_type());
248 }
249 }
250
251 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698