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

Side by Side Diff: components/reading_list/ios/reading_list_entry_unittest.cc

Issue 2763233003: Move ReadingList model to components/reading_list/core (Closed)
Patch Set: feedback Created 3 years, 9 months 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/reading_list/ios/reading_list_entry.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "components/reading_list/ios/proto/reading_list.pb.h"
10 #include "components/sync/protocol/reading_list_specifics.pb.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14 const int kFirstBackoff = 10;
15 const int kSecondBackoff = 10;
16 const int kThirdBackoff = 60;
17 const int kFourthBackoff = 120;
18 const int kFifthBackoff = 120;
19
20 } // namespace
21
22 TEST(ReadingListEntry, CompareIgnoreTitle) {
23 const ReadingListEntry e1(GURL("http://example.com"), "bar",
24 base::Time::FromTimeT(10));
25 const ReadingListEntry e2(GURL("http://example.com"), "foo",
26 base::Time::FromTimeT(20));
27
28 EXPECT_EQ(e1, e2);
29 }
30
31 TEST(ReadingListEntry, CompareFailureIgnoreTitleAndCreationTime) {
32 const ReadingListEntry e1(GURL("http://example.com"), "bar",
33 base::Time::FromTimeT(10));
34 const ReadingListEntry e2(GURL("http://example.org"), "bar",
35 base::Time::FromTimeT(10));
36
37 EXPECT_FALSE(e1 == e2);
38 }
39
40 TEST(ReadingListEntry, MovesAreEquals) {
41 ReadingListEntry e1(GURL("http://example.com"), "bar",
42 base::Time::FromTimeT(10));
43 ReadingListEntry e2(GURL("http://example.com"), "bar",
44 base::Time::FromTimeT(10));
45 EXPECT_EQ(e1, e2);
46 EXPECT_EQ(e1.Title(), e2.Title());
47 EXPECT_EQ(e1.CreationTime(), e2.CreationTime());
48
49 ReadingListEntry e3(std::move(e1));
50
51 EXPECT_EQ(e3, e2);
52 EXPECT_EQ(e3.Title(), e2.Title());
53 EXPECT_EQ(e3.CreationTime(), e2.CreationTime());
54 }
55
56 TEST(ReadingListEntry, ReadState) {
57 ReadingListEntry e(GURL("http://example.com"), "bar",
58 base::Time::FromTimeT(10));
59 EXPECT_FALSE(e.HasBeenSeen());
60 EXPECT_FALSE(e.IsRead());
61 e.SetRead(false, base::Time::FromTimeT(20));
62 EXPECT_EQ(e.CreationTime(), 10 * base::Time::kMicrosecondsPerSecond);
63 EXPECT_EQ(e.UpdateTime(), 10 * base::Time::kMicrosecondsPerSecond);
64 EXPECT_EQ(e.UpdateTitleTime(), 10 * base::Time::kMicrosecondsPerSecond);
65 EXPECT_TRUE(e.HasBeenSeen());
66 EXPECT_FALSE(e.IsRead());
67 e.SetRead(true, base::Time::FromTimeT(30));
68 EXPECT_EQ(e.CreationTime(), 10 * base::Time::kMicrosecondsPerSecond);
69 EXPECT_EQ(e.UpdateTime(), 30 * base::Time::kMicrosecondsPerSecond);
70 EXPECT_EQ(e.UpdateTitleTime(), 10 * base::Time::kMicrosecondsPerSecond);
71 EXPECT_TRUE(e.HasBeenSeen());
72 EXPECT_TRUE(e.IsRead());
73 }
74
75 TEST(ReadingListEntry, UpdateTitle) {
76 ReadingListEntry e(GURL("http://example.com"), "bar",
77 base::Time::FromTimeT(10));
78 EXPECT_EQ("bar", e.Title());
79 // Getters are in microseconds.
80 EXPECT_EQ(e.CreationTime(), 10 * base::Time::kMicrosecondsPerSecond);
81 EXPECT_EQ(e.UpdateTitleTime(), 10 * base::Time::kMicrosecondsPerSecond);
82
83 e.SetTitle("foo", base::Time::FromTimeT(15));
84 EXPECT_EQ(e.UpdateTitleTime(), 15 * base::Time::kMicrosecondsPerSecond);
85 EXPECT_EQ("foo", e.Title());
86 }
87
88 TEST(ReadingListEntry, DistilledInfo) {
89 ReadingListEntry e(GURL("http://example.com"), "bar",
90 base::Time::FromTimeT(10));
91
92 EXPECT_TRUE(e.DistilledPath().empty());
93
94 const base::FilePath distilled_path("distilled/page.html");
95 const GURL distilled_url("http://example.com/distilled");
96 int64_t size = 50;
97 int64_t time = 100;
98 e.SetDistilledInfo(distilled_path, distilled_url, size,
99 base::Time::FromTimeT(time));
100 EXPECT_EQ(distilled_path, e.DistilledPath());
101 EXPECT_EQ(distilled_url, e.DistilledURL());
102 EXPECT_EQ(size, e.DistillationSize());
103 EXPECT_EQ(e.DistillationTime(), time * base::Time::kMicrosecondsPerSecond);
104 }
105
106 TEST(ReadingListEntry, DistilledState) {
107 ReadingListEntry e(GURL("http://example.com"), "bar",
108 base::Time::FromTimeT(10));
109
110 EXPECT_EQ(ReadingListEntry::WAITING, e.DistilledState());
111
112 e.SetDistilledState(ReadingListEntry::ERROR);
113 EXPECT_EQ(ReadingListEntry::ERROR, e.DistilledState());
114
115 const base::FilePath distilled_path("distilled/page.html");
116 const GURL distilled_url("http://example.com/distilled");
117 e.SetDistilledInfo(distilled_path, distilled_url, 50,
118 base::Time::FromTimeT(100));
119 EXPECT_EQ(ReadingListEntry::PROCESSED, e.DistilledState());
120 }
121
122 // Tests that the the time until next try increase exponentially when the state
123 // changes from non-error to error.
124 TEST(ReadingListEntry, TimeUntilNextTry) {
125 base::SimpleTestTickClock clock;
126 std::unique_ptr<net::BackoffEntry> backoff =
127 base::MakeUnique<net::BackoffEntry>(&ReadingListEntry::kBackoffPolicy,
128 &clock);
129
130 ReadingListEntry e(GURL("http://example.com"), "bar",
131 base::Time::FromTimeT(10), std::move(backoff));
132
133 // Allow twice the jitter as test is not instantaneous.
134 double fuzzing = 2 * ReadingListEntry::kBackoffPolicy.jitter_factor;
135
136 EXPECT_EQ(0, e.TimeUntilNextTry().InSeconds());
137
138 // First error.
139 e.SetDistilledState(ReadingListEntry::ERROR);
140 int nextTry = e.TimeUntilNextTry().InMinutes();
141 EXPECT_NEAR(kFirstBackoff, nextTry, kFirstBackoff * fuzzing);
142 e.SetDistilledState(ReadingListEntry::WILL_RETRY);
143 EXPECT_EQ(nextTry, e.TimeUntilNextTry().InMinutes());
144
145 e.SetDistilledState(ReadingListEntry::PROCESSING);
146 EXPECT_EQ(nextTry, e.TimeUntilNextTry().InMinutes());
147
148 // Second error.
149 e.SetDistilledState(ReadingListEntry::WILL_RETRY);
150 nextTry = e.TimeUntilNextTry().InMinutes();
151 EXPECT_NEAR(kSecondBackoff, nextTry, kSecondBackoff * fuzzing);
152 e.SetDistilledState(ReadingListEntry::ERROR);
153 EXPECT_EQ(nextTry, e.TimeUntilNextTry().InMinutes());
154
155 e.SetDistilledState(ReadingListEntry::PROCESSING);
156 EXPECT_EQ(nextTry, e.TimeUntilNextTry().InMinutes());
157
158 // Third error.
159 e.SetDistilledState(ReadingListEntry::WILL_RETRY);
160 EXPECT_NEAR(kThirdBackoff, e.TimeUntilNextTry().InMinutes(),
161 kThirdBackoff * fuzzing);
162
163 // Fourth error.
164 e.SetDistilledState(ReadingListEntry::PROCESSING);
165 e.SetDistilledState(ReadingListEntry::ERROR);
166 EXPECT_NEAR(kFourthBackoff, e.TimeUntilNextTry().InMinutes(),
167 kFourthBackoff * fuzzing);
168
169 // Fifth error.
170 e.SetDistilledState(ReadingListEntry::PROCESSING);
171 e.SetDistilledState(ReadingListEntry::ERROR);
172 EXPECT_NEAR(kFifthBackoff, e.TimeUntilNextTry().InMinutes(),
173 kFifthBackoff * fuzzing);
174 }
175
176 // Tests that if the time until next try is in the past, 0 is returned.
177 TEST(ReadingListEntry, TimeUntilNextTryInThePast) {
178 // Setup.
179 base::SimpleTestTickClock clock;
180 std::unique_ptr<net::BackoffEntry> backoff =
181 base::MakeUnique<net::BackoffEntry>(&ReadingListEntry::kBackoffPolicy,
182 &clock);
183 ReadingListEntry e(GURL("http://example.com"), "bar",
184 base::Time::FromTimeT(10), std::move(backoff));
185 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor;
186
187 e.SetDistilledState(ReadingListEntry::ERROR);
188 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(),
189 kFirstBackoff * fuzzing);
190
191 // Action.
192 clock.Advance(base::TimeDelta::FromMinutes(kFirstBackoff * 2));
193
194 // Test.
195 EXPECT_EQ(0, e.TimeUntilNextTry().InMilliseconds());
196 }
197
198 // Tests that if the entry gets a distilled URL, 0 is returned.
199 TEST(ReadingListEntry, ResetTimeUntilNextTry) {
200 // Setup.
201 base::SimpleTestTickClock clock;
202 std::unique_ptr<net::BackoffEntry> backoff =
203 base::MakeUnique<net::BackoffEntry>(&ReadingListEntry::kBackoffPolicy,
204 &clock);
205 ReadingListEntry e(GURL("http://example.com"), "bar",
206 base::Time::FromTimeT(10), std::move(backoff));
207 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor;
208
209 e.SetDistilledState(ReadingListEntry::ERROR);
210 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(),
211 kFirstBackoff * fuzzing);
212
213 // Action.
214 const base::FilePath distilled_path("distilled/page.html");
215 const GURL distilled_url("http://example.com/distilled");
216 e.SetDistilledInfo(distilled_path, distilled_url, 50,
217 base::Time::FromTimeT(100));
218
219 // Test.
220 EXPECT_EQ(0, e.TimeUntilNextTry().InSeconds());
221 e.SetDistilledState(ReadingListEntry::ERROR);
222 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(),
223 kFirstBackoff * fuzzing);
224 }
225
226 // Tests that the failed download counter is incremented when the state change
227 // from non-error to error.
228 TEST(ReadingListEntry, FailedDownloadCounter) {
229 ReadingListEntry e(GURL("http://example.com"), "bar",
230 base::Time::FromTimeT(10));
231
232 EXPECT_EQ(0, e.FailedDownloadCounter());
233
234 e.SetDistilledState(ReadingListEntry::ERROR);
235 EXPECT_EQ(1, e.FailedDownloadCounter());
236 e.SetDistilledState(ReadingListEntry::WILL_RETRY);
237 EXPECT_EQ(1, e.FailedDownloadCounter());
238
239 e.SetDistilledState(ReadingListEntry::PROCESSING);
240 EXPECT_EQ(1, e.FailedDownloadCounter());
241
242 e.SetDistilledState(ReadingListEntry::WILL_RETRY);
243 EXPECT_EQ(2, e.FailedDownloadCounter());
244 e.SetDistilledState(ReadingListEntry::ERROR);
245 EXPECT_EQ(2, e.FailedDownloadCounter());
246 }
247
248 // Tests that the reading list entry is correctly encoded to
249 // sync_pb::ReadingListSpecifics.
250 TEST(ReadingListEntry, AsReadingListSpecifics) {
251 ReadingListEntry entry(GURL("http://example.com"), "bar",
252 base::Time::FromTimeT(10));
253 int64_t creation_time_us = entry.UpdateTime();
254
255 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry(
256 entry.AsReadingListSpecifics());
257 EXPECT_EQ(pb_entry->entry_id(), "http://example.com/");
258 EXPECT_EQ(pb_entry->url(), "http://example.com/");
259 EXPECT_EQ(pb_entry->title(), "bar");
260 EXPECT_EQ(pb_entry->creation_time_us(), creation_time_us);
261 EXPECT_EQ(pb_entry->update_time_us(), entry.UpdateTime());
262 EXPECT_EQ(pb_entry->status(), sync_pb::ReadingListSpecifics::UNSEEN);
263
264 entry.SetRead(true, base::Time::FromTimeT(15));
265 // Getters are in microseconds.
266 EXPECT_EQ(entry.CreationTime(), 10 * base::Time::kMicrosecondsPerSecond);
267 EXPECT_EQ(entry.UpdateTime(), 15 * base::Time::kMicrosecondsPerSecond);
268 std::unique_ptr<sync_pb::ReadingListSpecifics> updated_pb_entry(
269 entry.AsReadingListSpecifics());
270 EXPECT_EQ(updated_pb_entry->creation_time_us(), creation_time_us);
271 EXPECT_EQ(updated_pb_entry->update_time_us(), entry.UpdateTime());
272 EXPECT_EQ(updated_pb_entry->status(), sync_pb::ReadingListSpecifics::READ);
273 }
274
275 // Tests that the reading list entry is correctly parsed from
276 // sync_pb::ReadingListSpecifics.
277 TEST(ReadingListEntry, FromReadingListSpecifics) {
278 std::unique_ptr<sync_pb::ReadingListSpecifics> pb_entry =
279 base::MakeUnique<sync_pb::ReadingListSpecifics>();
280 pb_entry->set_entry_id("http://example.com/");
281 pb_entry->set_url("http://example.com/");
282 pb_entry->set_title("title");
283 pb_entry->set_creation_time_us(1);
284 pb_entry->set_update_time_us(2);
285 pb_entry->set_update_title_time_us(3);
286 pb_entry->set_status(sync_pb::ReadingListSpecifics::UNREAD);
287
288 std::unique_ptr<ReadingListEntry> entry(
289 ReadingListEntry::FromReadingListSpecifics(*pb_entry,
290 base::Time::FromTimeT(10)));
291 EXPECT_EQ(entry->URL().spec(), "http://example.com/");
292 EXPECT_EQ(entry->Title(), "title");
293 EXPECT_EQ(entry->CreationTime(), 1);
294 EXPECT_EQ(entry->UpdateTime(), 2);
295 EXPECT_EQ(entry->UpdateTitleTime(), 3);
296 EXPECT_EQ(entry->FailedDownloadCounter(), 0);
297 }
298
299 // Tests that the reading list entry is correctly encoded to
300 // reading_list::ReadingListLocal.
301 TEST(ReadingListEntry, AsReadingListLocal) {
302 ReadingListEntry entry(GURL("http://example.com/"), "foo",
303 base::Time::FromTimeT(10));
304 int64_t creation_time_us = entry.UpdateTime();
305 entry.SetTitle("bar", base::Time::FromTimeT(20));
306 entry.MarkEntryUpdated(base::Time::FromTimeT(30));
307
308 std::unique_ptr<reading_list::ReadingListLocal> pb_entry(
309 entry.AsReadingListLocal(base::Time::FromTimeT(40)));
310 EXPECT_EQ(pb_entry->entry_id(), "http://example.com/");
311 EXPECT_EQ(pb_entry->url(), "http://example.com/");
312 EXPECT_EQ(pb_entry->title(), "bar");
313 EXPECT_EQ(pb_entry->creation_time_us(), creation_time_us);
314 EXPECT_EQ(pb_entry->update_time_us(), entry.UpdateTime());
315 EXPECT_EQ(pb_entry->status(), reading_list::ReadingListLocal::UNSEEN);
316 EXPECT_EQ(pb_entry->distillation_state(),
317 reading_list::ReadingListLocal::WAITING);
318 EXPECT_EQ(pb_entry->distilled_path(), "");
319 EXPECT_EQ(pb_entry->failed_download_counter(), 0);
320 EXPECT_NE(pb_entry->backoff(), "");
321
322 entry.SetDistilledState(ReadingListEntry::WILL_RETRY);
323 std::unique_ptr<reading_list::ReadingListLocal> will_retry_pb_entry(
324 entry.AsReadingListLocal(base::Time::FromTimeT(50)));
325 EXPECT_EQ(will_retry_pb_entry->distillation_state(),
326 reading_list::ReadingListLocal::WILL_RETRY);
327 EXPECT_EQ(will_retry_pb_entry->failed_download_counter(), 1);
328
329 const base::FilePath distilled_path("distilled/page.html");
330 const GURL distilled_url("http://example.com/distilled");
331 int64_t size = 50;
332 entry.SetDistilledInfo(distilled_path, distilled_url, size,
333 base::Time::FromTimeT(100));
334
335 entry.SetRead(true, base::Time::FromTimeT(20));
336 entry.MarkEntryUpdated(base::Time::FromTimeT(30));
337 EXPECT_NE(entry.UpdateTime(), creation_time_us);
338 std::unique_ptr<reading_list::ReadingListLocal> distilled_pb_entry(
339 entry.AsReadingListLocal(base::Time::FromTimeT(40)));
340 EXPECT_EQ(distilled_pb_entry->creation_time_us(), creation_time_us);
341 EXPECT_EQ(distilled_pb_entry->update_time_us(), entry.UpdateTime());
342 EXPECT_NE(distilled_pb_entry->backoff(), "");
343 EXPECT_EQ(distilled_pb_entry->status(), reading_list::ReadingListLocal::READ);
344 EXPECT_EQ(distilled_pb_entry->distillation_state(),
345 reading_list::ReadingListLocal::PROCESSED);
346 EXPECT_EQ(distilled_pb_entry->distilled_path(), "distilled/page.html");
347 EXPECT_EQ(distilled_pb_entry->failed_download_counter(), 0);
348 EXPECT_EQ(distilled_pb_entry->distillation_time_us(),
349 entry.DistillationTime());
350 EXPECT_EQ(distilled_pb_entry->distillation_size(), entry.DistillationSize());
351 }
352
353 // Tests that the reading list entry is correctly parsed from
354 // sync_pb::ReadingListLocal.
355 TEST(ReadingListEntry, FromReadingListLocal) {
356 ReadingListEntry entry(GURL("http://example.com/"), "title",
357 base::Time::FromTimeT(10));
358 entry.SetDistilledState(ReadingListEntry::ERROR);
359
360 std::unique_ptr<reading_list::ReadingListLocal> pb_entry(
361 entry.AsReadingListLocal(base::Time::FromTimeT(10)));
362 int64_t now = 12345;
363
364 pb_entry->set_entry_id("http://example.com/");
365 pb_entry->set_url("http://example.com/");
366 pb_entry->set_title("title");
367 pb_entry->set_creation_time_us(1);
368 pb_entry->set_update_time_us(2);
369 pb_entry->set_update_title_time_us(3);
370 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD);
371 pb_entry->set_distillation_state(reading_list::ReadingListLocal::WAITING);
372 pb_entry->set_failed_download_counter(2);
373 pb_entry->set_distillation_time_us(now);
374 pb_entry->set_distillation_size(50);
375
376 std::unique_ptr<ReadingListEntry> waiting_entry(
377 ReadingListEntry::FromReadingListLocal(*pb_entry,
378 base::Time::FromTimeT(20)));
379 EXPECT_EQ(waiting_entry->URL().spec(), "http://example.com/");
380 EXPECT_EQ(waiting_entry->Title(), "title");
381 EXPECT_EQ(waiting_entry->UpdateTime(), 2);
382 EXPECT_EQ(waiting_entry->UpdateTitleTime(), 3);
383 EXPECT_EQ(waiting_entry->FailedDownloadCounter(), 2);
384 EXPECT_EQ(waiting_entry->DistilledState(), ReadingListEntry::WAITING);
385 EXPECT_EQ(waiting_entry->DistilledPath(), base::FilePath());
386 EXPECT_EQ(waiting_entry->DistillationSize(), 50);
387 EXPECT_EQ(waiting_entry->DistillationTime(), now);
388 // Allow twice the jitter as test is not instantaneous.
389 double fuzzing = 2 * ReadingListEntry::kBackoffPolicy.jitter_factor;
390 int nextTry = waiting_entry->TimeUntilNextTry().InMinutes();
391 EXPECT_NEAR(kFirstBackoff, nextTry, kFirstBackoff * fuzzing);
392 }
393
394 // Tests the merging of two ReadingListEntry.
395 // Additional merging tests are done in
396 // ReadingListStoreTest.CompareEntriesForSync
397 TEST(ReadingListEntry, MergeWithEntry) {
398 ReadingListEntry local_entry(GURL("http://example.com/"), "title",
399 base::Time::FromTimeT(10));
400 local_entry.SetDistilledState(ReadingListEntry::ERROR);
401 local_entry.SetTitle("title updated", base::Time::FromTimeT(30));
402 int64_t local_update_time_us = local_entry.UpdateTime();
403
404 ReadingListEntry sync_entry(GURL("http://example.com/"), "title2",
405 base::Time::FromTimeT(20));
406 sync_entry.SetDistilledState(ReadingListEntry::ERROR);
407 int64_t sync_update_time_us = sync_entry.UpdateTime();
408 EXPECT_NE(local_update_time_us, sync_update_time_us);
409 local_entry.MergeWithEntry(sync_entry);
410 EXPECT_EQ(local_entry.URL().spec(), "http://example.com/");
411 EXPECT_EQ(local_entry.Title(), "title updated");
412 EXPECT_EQ(local_entry.UpdateTitleTime(),
413 30 * base::Time::kMicrosecondsPerSecond);
414 EXPECT_FALSE(local_entry.HasBeenSeen());
415 EXPECT_EQ(local_entry.UpdateTime(), sync_update_time_us);
416 EXPECT_EQ(local_entry.FailedDownloadCounter(), 1);
417 EXPECT_EQ(local_entry.DistilledState(), ReadingListEntry::ERROR);
418 // Allow twice the jitter as test is not instantaneous.
419 double fuzzing = 2 * ReadingListEntry::kBackoffPolicy.jitter_factor;
420 int nextTry = local_entry.TimeUntilNextTry().InMinutes();
421 EXPECT_NEAR(kFirstBackoff, nextTry, kFirstBackoff * fuzzing);
422 }
OLDNEW
« no previous file with comments | « components/reading_list/ios/reading_list_entry.cc ('k') | components/reading_list/ios/reading_list_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698