OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/reading_list/ios/reading_list_entry.h" | 5 #include "components/reading_list/ios/reading_list_entry.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/test/ios/wait_util.h" | 8 #include "base/test/ios/wait_util.h" |
9 #include "base/test/simple_test_tick_clock.h" | 9 #include "base/test/simple_test_tick_clock.h" |
10 #include "components/reading_list/ios/proto/reading_list.pb.h" | 10 #include "components/reading_list/ios/proto/reading_list.pb.h" |
11 #include "components/sync/protocol/reading_list_specifics.pb.h" | 11 #include "components/sync/protocol/reading_list_specifics.pb.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 const int kFirstBackoff = 10; | 15 const int kFirstBackoff = 10; |
16 const int kSecondBackoff = 10; | 16 const int kSecondBackoff = 10; |
17 const int kThirdBackoff = 60; | 17 const int kThirdBackoff = 60; |
18 const int kFourthBackoff = 120; | 18 const int kFourthBackoff = 120; |
19 const int kFifthBackoff = 120; | 19 const int kFifthBackoff = 120; |
20 | |
21 // Returns the number of microseconds since Jan 1st 1970. | |
22 int64_t Now() { | |
gambard
2017/02/22 12:30:16
Nit: I don't like this. I guess the "right" way to
| |
23 return (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | |
24 } | |
20 } // namespace | 25 } // namespace |
21 | 26 |
22 TEST(ReadingListEntry, CompareIgnoreTitle) { | 27 TEST(ReadingListEntry, CompareIgnoreTitle) { |
23 const ReadingListEntry e1(GURL("http://example.com"), "bar"); | 28 const ReadingListEntry e1(GURL("http://example.com"), "bar"); |
24 const ReadingListEntry e2(GURL("http://example.com"), "foo"); | 29 const ReadingListEntry e2(GURL("http://example.com"), "foo"); |
25 | 30 |
26 EXPECT_EQ(e1, e2); | 31 EXPECT_EQ(e1, e2); |
27 } | 32 } |
28 | 33 |
29 TEST(ReadingListEntry, CompareFailureIgnoreTitle) { | 34 TEST(ReadingListEntry, CompareFailureIgnoreTitle) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 ASSERT_EQ("bar", e.Title()); | 67 ASSERT_EQ("bar", e.Title()); |
63 ASSERT_EQ(e.CreationTime(), e.UpdateTitleTime()); | 68 ASSERT_EQ(e.CreationTime(), e.UpdateTitleTime()); |
64 | 69 |
65 base::test::ios::SpinRunLoopWithMinDelay( | 70 base::test::ios::SpinRunLoopWithMinDelay( |
66 base::TimeDelta::FromMilliseconds(5)); | 71 base::TimeDelta::FromMilliseconds(5)); |
67 e.SetTitle("foo"); | 72 e.SetTitle("foo"); |
68 EXPECT_GT(e.UpdateTitleTime(), e.CreationTime()); | 73 EXPECT_GT(e.UpdateTitleTime(), e.CreationTime()); |
69 EXPECT_EQ("foo", e.Title()); | 74 EXPECT_EQ("foo", e.Title()); |
70 } | 75 } |
71 | 76 |
72 TEST(ReadingListEntry, DistilledPathAndURL) { | 77 TEST(ReadingListEntry, DistilledInfo) { |
73 ReadingListEntry e(GURL("http://example.com"), "bar"); | 78 ReadingListEntry e(GURL("http://example.com"), "bar"); |
74 | 79 |
75 EXPECT_TRUE(e.DistilledPath().empty()); | 80 EXPECT_TRUE(e.DistilledPath().empty()); |
76 | 81 |
77 const base::FilePath distilled_path("distilled/page.html"); | 82 const base::FilePath distilled_path("distilled/page.html"); |
78 const GURL distilled_url("http://example.com/distilled"); | 83 const GURL distilled_url("http://example.com/distilled"); |
79 e.SetDistilledInfo(distilled_path, distilled_url); | 84 int64_t size = 50; |
85 int64_t before = Now(); | |
86 e.SetDistilledInfo(distilled_path, distilled_url, size); | |
87 int64_t after = Now(); | |
80 EXPECT_EQ(distilled_path, e.DistilledPath()); | 88 EXPECT_EQ(distilled_path, e.DistilledPath()); |
81 EXPECT_EQ(distilled_url, e.DistilledURL()); | 89 EXPECT_EQ(distilled_url, e.DistilledURL()); |
90 EXPECT_EQ(size, e.DistillationSize()); | |
91 EXPECT_GE(e.DistillationTime(), before); | |
92 EXPECT_GE(after, e.DistillationTime()); | |
82 } | 93 } |
83 | 94 |
84 TEST(ReadingListEntry, DistilledState) { | 95 TEST(ReadingListEntry, DistilledState) { |
85 ReadingListEntry e(GURL("http://example.com"), "bar"); | 96 ReadingListEntry e(GURL("http://example.com"), "bar"); |
86 | 97 |
87 EXPECT_EQ(ReadingListEntry::WAITING, e.DistilledState()); | 98 EXPECT_EQ(ReadingListEntry::WAITING, e.DistilledState()); |
88 | 99 |
89 e.SetDistilledState(ReadingListEntry::ERROR); | 100 e.SetDistilledState(ReadingListEntry::ERROR); |
90 EXPECT_EQ(ReadingListEntry::ERROR, e.DistilledState()); | 101 EXPECT_EQ(ReadingListEntry::ERROR, e.DistilledState()); |
91 | 102 |
92 const base::FilePath distilled_path("distilled/page.html"); | 103 const base::FilePath distilled_path("distilled/page.html"); |
93 const GURL distilled_url("http://example.com/distilled"); | 104 const GURL distilled_url("http://example.com/distilled"); |
94 e.SetDistilledInfo(distilled_path, distilled_url); | 105 e.SetDistilledInfo(distilled_path, distilled_url, 50); |
95 EXPECT_EQ(ReadingListEntry::PROCESSED, e.DistilledState()); | 106 EXPECT_EQ(ReadingListEntry::PROCESSED, e.DistilledState()); |
96 } | 107 } |
97 | 108 |
98 // Tests that the the time until next try increase exponentially when the state | 109 // Tests that the the time until next try increase exponentially when the state |
99 // changes from non-error to error. | 110 // changes from non-error to error. |
100 TEST(ReadingListEntry, TimeUntilNextTry) { | 111 TEST(ReadingListEntry, TimeUntilNextTry) { |
101 base::SimpleTestTickClock clock; | 112 base::SimpleTestTickClock clock; |
102 std::unique_ptr<net::BackoffEntry> backoff = | 113 std::unique_ptr<net::BackoffEntry> backoff = |
103 base::MakeUnique<net::BackoffEntry>(&ReadingListEntry::kBackoffPolicy, | 114 base::MakeUnique<net::BackoffEntry>(&ReadingListEntry::kBackoffPolicy, |
104 &clock); | 115 &clock); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 ReadingListEntry e(GURL("http://example.com"), "bar", std::move(backoff)); | 189 ReadingListEntry e(GURL("http://example.com"), "bar", std::move(backoff)); |
179 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor; | 190 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor; |
180 | 191 |
181 e.SetDistilledState(ReadingListEntry::ERROR); | 192 e.SetDistilledState(ReadingListEntry::ERROR); |
182 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(), | 193 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(), |
183 kFirstBackoff * fuzzing); | 194 kFirstBackoff * fuzzing); |
184 | 195 |
185 // Action. | 196 // Action. |
186 const base::FilePath distilled_path("distilled/page.html"); | 197 const base::FilePath distilled_path("distilled/page.html"); |
187 const GURL distilled_url("http://example.com/distilled"); | 198 const GURL distilled_url("http://example.com/distilled"); |
188 e.SetDistilledInfo(distilled_path, distilled_url); | 199 e.SetDistilledInfo(distilled_path, distilled_url, 50); |
189 | 200 |
190 // Test. | 201 // Test. |
191 EXPECT_EQ(0, e.TimeUntilNextTry().InSeconds()); | 202 EXPECT_EQ(0, e.TimeUntilNextTry().InSeconds()); |
192 e.SetDistilledState(ReadingListEntry::ERROR); | 203 e.SetDistilledState(ReadingListEntry::ERROR); |
193 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(), | 204 ASSERT_NEAR(kFirstBackoff, e.TimeUntilNextTry().InMinutes(), |
194 kFirstBackoff * fuzzing); | 205 kFirstBackoff * fuzzing); |
195 } | 206 } |
196 | 207 |
197 // Tests that the failed download counter is incremented when the state change | 208 // Tests that the failed download counter is incremented when the state change |
198 // from non-error to error. | 209 // from non-error to error. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 | 292 |
282 entry.SetDistilledState(ReadingListEntry::WILL_RETRY); | 293 entry.SetDistilledState(ReadingListEntry::WILL_RETRY); |
283 std::unique_ptr<reading_list::ReadingListLocal> will_retry_pb_entry( | 294 std::unique_ptr<reading_list::ReadingListLocal> will_retry_pb_entry( |
284 entry.AsReadingListLocal()); | 295 entry.AsReadingListLocal()); |
285 EXPECT_EQ(will_retry_pb_entry->distillation_state(), | 296 EXPECT_EQ(will_retry_pb_entry->distillation_state(), |
286 reading_list::ReadingListLocal::WILL_RETRY); | 297 reading_list::ReadingListLocal::WILL_RETRY); |
287 EXPECT_EQ(will_retry_pb_entry->failed_download_counter(), 1); | 298 EXPECT_EQ(will_retry_pb_entry->failed_download_counter(), 1); |
288 | 299 |
289 const base::FilePath distilled_path("distilled/page.html"); | 300 const base::FilePath distilled_path("distilled/page.html"); |
290 const GURL distilled_url("http://example.com/distilled"); | 301 const GURL distilled_url("http://example.com/distilled"); |
291 entry.SetDistilledInfo(distilled_path, distilled_url); | 302 int64_t size = 50; |
303 entry.SetDistilledInfo(distilled_path, distilled_url, size); | |
304 | |
292 entry.SetRead(true); | 305 entry.SetRead(true); |
293 entry.MarkEntryUpdated(); | 306 entry.MarkEntryUpdated(); |
294 EXPECT_NE(entry.UpdateTime(), creation_time_us); | 307 EXPECT_NE(entry.UpdateTime(), creation_time_us); |
295 std::unique_ptr<reading_list::ReadingListLocal> distilled_pb_entry( | 308 std::unique_ptr<reading_list::ReadingListLocal> distilled_pb_entry( |
296 entry.AsReadingListLocal()); | 309 entry.AsReadingListLocal()); |
297 EXPECT_EQ(distilled_pb_entry->creation_time_us(), creation_time_us); | 310 EXPECT_EQ(distilled_pb_entry->creation_time_us(), creation_time_us); |
298 EXPECT_EQ(distilled_pb_entry->update_time_us(), entry.UpdateTime()); | 311 EXPECT_EQ(distilled_pb_entry->update_time_us(), entry.UpdateTime()); |
299 EXPECT_NE(distilled_pb_entry->backoff(), ""); | 312 EXPECT_NE(distilled_pb_entry->backoff(), ""); |
300 EXPECT_EQ(distilled_pb_entry->status(), reading_list::ReadingListLocal::READ); | 313 EXPECT_EQ(distilled_pb_entry->status(), reading_list::ReadingListLocal::READ); |
301 EXPECT_EQ(distilled_pb_entry->distillation_state(), | 314 EXPECT_EQ(distilled_pb_entry->distillation_state(), |
302 reading_list::ReadingListLocal::PROCESSED); | 315 reading_list::ReadingListLocal::PROCESSED); |
303 EXPECT_EQ(distilled_pb_entry->distilled_path(), "distilled/page.html"); | 316 EXPECT_EQ(distilled_pb_entry->distilled_path(), "distilled/page.html"); |
304 EXPECT_EQ(distilled_pb_entry->failed_download_counter(), 0); | 317 EXPECT_EQ(distilled_pb_entry->failed_download_counter(), 0); |
318 EXPECT_EQ(distilled_pb_entry->distillation_time_us(), | |
319 entry.DistillationTime()); | |
320 EXPECT_EQ(distilled_pb_entry->distillation_size(), entry.DistillationSize()); | |
305 } | 321 } |
306 | 322 |
307 // Tests that the reading list entry is correctly parsed from | 323 // Tests that the reading list entry is correctly parsed from |
308 // sync_pb::ReadingListLocal. | 324 // sync_pb::ReadingListLocal. |
309 TEST(ReadingListEntry, FromReadingListLocal) { | 325 TEST(ReadingListEntry, FromReadingListLocal) { |
310 ReadingListEntry entry(GURL("http://example.com/"), "title"); | 326 ReadingListEntry entry(GURL("http://example.com/"), "title"); |
311 base::Time next_call = base::Time::Now() + entry.TimeUntilNextTry(); | 327 base::Time next_call = base::Time::Now() + entry.TimeUntilNextTry(); |
312 | 328 |
313 std::unique_ptr<reading_list::ReadingListLocal> pb_entry( | 329 std::unique_ptr<reading_list::ReadingListLocal> pb_entry( |
314 entry.AsReadingListLocal()); | 330 entry.AsReadingListLocal()); |
331 int64_t now = Now(); | |
315 | 332 |
316 pb_entry->set_entry_id("http://example.com/"); | 333 pb_entry->set_entry_id("http://example.com/"); |
317 pb_entry->set_url("http://example.com/"); | 334 pb_entry->set_url("http://example.com/"); |
318 pb_entry->set_title("title"); | 335 pb_entry->set_title("title"); |
319 pb_entry->set_creation_time_us(1); | 336 pb_entry->set_creation_time_us(1); |
320 pb_entry->set_update_time_us(2); | 337 pb_entry->set_update_time_us(2); |
321 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); | 338 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); |
322 pb_entry->set_distillation_state(reading_list::ReadingListLocal::WAITING); | 339 pb_entry->set_distillation_state(reading_list::ReadingListLocal::WAITING); |
323 pb_entry->set_failed_download_counter(2); | 340 pb_entry->set_failed_download_counter(2); |
341 pb_entry->set_distillation_time_us(now); | |
342 pb_entry->set_distillation_size(50); | |
324 | 343 |
325 std::unique_ptr<ReadingListEntry> waiting_entry( | 344 std::unique_ptr<ReadingListEntry> waiting_entry( |
326 ReadingListEntry::FromReadingListLocal(*pb_entry)); | 345 ReadingListEntry::FromReadingListLocal(*pb_entry)); |
327 EXPECT_EQ(waiting_entry->URL().spec(), "http://example.com/"); | 346 EXPECT_EQ(waiting_entry->URL().spec(), "http://example.com/"); |
328 EXPECT_EQ(waiting_entry->Title(), "title"); | 347 EXPECT_EQ(waiting_entry->Title(), "title"); |
329 EXPECT_EQ(waiting_entry->UpdateTime(), 2); | 348 EXPECT_EQ(waiting_entry->UpdateTime(), 2); |
330 EXPECT_EQ(waiting_entry->FailedDownloadCounter(), 2); | 349 EXPECT_EQ(waiting_entry->FailedDownloadCounter(), 2); |
331 EXPECT_EQ(waiting_entry->DistilledState(), ReadingListEntry::WAITING); | 350 EXPECT_EQ(waiting_entry->DistilledState(), ReadingListEntry::WAITING); |
332 EXPECT_EQ(waiting_entry->DistilledPath(), base::FilePath()); | 351 EXPECT_EQ(waiting_entry->DistilledPath(), base::FilePath()); |
352 EXPECT_EQ(waiting_entry->DistillationSize(), 50); | |
353 EXPECT_EQ(waiting_entry->DistillationTime(), now); | |
333 base::Time waiting_next_call = | 354 base::Time waiting_next_call = |
334 base::Time::Now() + waiting_entry->TimeUntilNextTry(); | 355 base::Time::Now() + waiting_entry->TimeUntilNextTry(); |
335 base::TimeDelta delta = next_call - waiting_next_call; | 356 base::TimeDelta delta = next_call - waiting_next_call; |
336 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); | 357 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); |
337 } | 358 } |
338 | 359 |
339 // Tests the merging of two ReadingListEntry. | 360 // Tests the merging of two ReadingListEntry. |
340 // Additional merging tests are done in | 361 // Additional merging tests are done in |
341 // ReadingListStoreTest.CompareEntriesForSync | 362 // ReadingListStoreTest.CompareEntriesForSync |
342 TEST(ReadingListEntry, MergeWithEntry) { | 363 TEST(ReadingListEntry, MergeWithEntry) { |
(...skipping 11 matching lines...) Expand all Loading... | |
354 EXPECT_EQ(local_entry.Title(), "title2"); | 375 EXPECT_EQ(local_entry.Title(), "title2"); |
355 EXPECT_FALSE(local_entry.HasBeenSeen()); | 376 EXPECT_FALSE(local_entry.HasBeenSeen()); |
356 EXPECT_EQ(local_entry.UpdateTime(), sync_update_time_us); | 377 EXPECT_EQ(local_entry.UpdateTime(), sync_update_time_us); |
357 EXPECT_EQ(local_entry.FailedDownloadCounter(), 1); | 378 EXPECT_EQ(local_entry.FailedDownloadCounter(), 1); |
358 EXPECT_EQ(local_entry.DistilledState(), ReadingListEntry::ERROR); | 379 EXPECT_EQ(local_entry.DistilledState(), ReadingListEntry::ERROR); |
359 base::Time merge_next_call = | 380 base::Time merge_next_call = |
360 base::Time::Now() + local_entry.TimeUntilNextTry(); | 381 base::Time::Now() + local_entry.TimeUntilNextTry(); |
361 base::TimeDelta delta = merge_next_call - next_call; | 382 base::TimeDelta delta = merge_next_call - next_call; |
362 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); | 383 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); |
363 } | 384 } |
OLD | NEW |