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/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, OnAddedAsAvailable) { | |
132 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
133 kTestCreationTime, kTestUserRequested); | |
134 request.set_request_state(SavePageRequest::RequestState::AVAILABLE); | |
135 observer()->OnAdded(request); | |
136 EXPECT_EQ(LastNotificationType::DOWNLOAD_INTERRUPTED, | |
137 notifier()->last_notification_type()); | |
138 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
139 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
140 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
141 } | |
142 | |
143 TEST_F(DownloadNotifyingObserverTest, OnAddedAsOffling) { | |
144 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
145 kTestCreationTime, kTestUserRequested); | |
146 request.set_request_state(SavePageRequest::RequestState::OFFLINING); | |
147 observer()->OnAdded(request); | |
148 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, | |
149 notifier()->last_notification_type()); | |
150 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
151 } | |
152 | |
153 TEST_F(DownloadNotifyingObserverTest, OnAddedAsPaused) { | |
154 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
155 kTestCreationTime, kTestUserRequested); | |
156 request.set_request_state(SavePageRequest::RequestState::PAUSED); | |
157 observer()->OnAdded(request); | |
158 EXPECT_EQ(LastNotificationType::DOWNLOAD_PAUSED, | |
159 notifier()->last_notification_type()); | |
160 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
161 } | |
162 | |
163 TEST_F(DownloadNotifyingObserverTest, OnChangedToPaused) { | |
164 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
165 kTestCreationTime, kTestUserRequested); | |
166 request.set_request_state(SavePageRequest::RequestState::PAUSED); | |
167 observer()->OnChanged(request); | |
168 EXPECT_EQ(LastNotificationType::DOWNLOAD_PAUSED, | |
169 notifier()->last_notification_type()); | |
170 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
171 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
172 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
173 } | |
174 | |
175 TEST_F(DownloadNotifyingObserverTest, OnChangedToAvailable) { | |
176 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
177 kTestCreationTime, kTestUserRequested); | |
178 request.set_request_state(SavePageRequest::RequestState::AVAILABLE); | |
179 observer()->OnChanged(request); | |
180 EXPECT_EQ(LastNotificationType::DOWNLOAD_INTERRUPTED, | |
181 notifier()->last_notification_type()); | |
182 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
183 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
184 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
185 } | |
186 | |
187 TEST_F(DownloadNotifyingObserverTest, OnChangedToOfflining) { | |
188 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
189 kTestCreationTime, kTestUserRequested); | |
190 request.set_request_state(SavePageRequest::RequestState::OFFLINING); | |
191 observer()->OnChanged(request); | |
192 EXPECT_EQ(LastNotificationType::DOWNLOAD_PROGRESS, | |
193 notifier()->last_notification_type()); | |
194 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
195 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
196 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
197 } | |
198 | |
199 TEST_F(DownloadNotifyingObserverTest, OnCompletedSuccess) { | |
200 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
201 kTestCreationTime, kTestUserRequested); | |
202 observer()->OnCompleted(request, | |
203 RequestNotifier::BackgroundSavePageResult::SUCCESS); | |
204 EXPECT_EQ(LastNotificationType::DOWNLOAD_SUCCESSFUL, | |
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 | |
211 TEST_F(DownloadNotifyingObserverTest, OnCompletedCanceled) { | |
212 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
213 kTestCreationTime, kTestUserRequested); | |
214 observer()->OnCompleted(request, | |
215 RequestNotifier::BackgroundSavePageResult::REMOVED); | |
216 EXPECT_EQ(LastNotificationType::DOWNLOAD_CANCELED, | |
217 notifier()->last_notification_type()); | |
218 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
219 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
220 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
221 } | |
222 | |
223 TEST_F(DownloadNotifyingObserverTest, OnCompletedFailure) { | |
224 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), kTestClientId, | |
225 kTestCreationTime, kTestUserRequested); | |
226 observer()->OnCompleted( | |
227 request, RequestNotifier::BackgroundSavePageResult::PRERENDER_FAILURE); | |
228 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
229 notifier()->last_notification_type()); | |
230 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
231 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
232 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
233 | |
234 notifier()->Reset(); | |
235 observer()->OnCompleted( | |
236 request, RequestNotifier::BackgroundSavePageResult::FOREGROUND_CANCELED); | |
237 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
238 notifier()->last_notification_type()); | |
239 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
240 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
241 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
242 | |
243 notifier()->Reset(); | |
244 observer()->OnCompleted( | |
245 request, RequestNotifier::BackgroundSavePageResult::SAVE_FAILED); | |
246 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
247 notifier()->last_notification_type()); | |
248 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
249 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
250 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
251 | |
252 notifier()->Reset(); | |
253 observer()->OnCompleted(request, | |
254 RequestNotifier::BackgroundSavePageResult::EXPIRED); | |
255 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
256 notifier()->last_notification_type()); | |
257 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
258 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
259 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
260 | |
261 notifier()->Reset(); | |
262 observer()->OnCompleted( | |
263 request, RequestNotifier::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED); | |
264 EXPECT_EQ(LastNotificationType::DOWNLOAD_FAILED, | |
265 notifier()->last_notification_type()); | |
266 EXPECT_EQ(kTestGuid, notifier()->download_item()->guid); | |
267 EXPECT_EQ(GURL(kTestUrl), notifier()->download_item()->url); | |
268 EXPECT_EQ(kTestCreationTime, notifier()->download_item()->start_time); | |
269 } | |
270 | |
271 TEST_F(DownloadNotifyingObserverTest, NamespacesNotVisibleInUI) { | |
272 std::vector<std::string> name_spaces = {kBookmarkNamespace, kLastNNamespace, | |
273 kCCTNamespace, kDefaultNamespace}; | |
274 | |
275 for (auto name_space : name_spaces) { | |
276 ClientId invisible_client_id(name_space, kTestGuid); | |
277 SavePageRequest request(kTestOfflineId, GURL(kTestUrl), invisible_client_id, | |
278 kTestCreationTime, kTestUserRequested); | |
279 observer()->OnAdded(request); | |
280 EXPECT_EQ(LastNotificationType::NONE, notifier()->last_notification_type()); | |
281 } | |
282 } | |
283 | |
284 } // namespace offline_pages | |
OLD | NEW |