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 | 20 |
21 // Returns the number of microseconds since Jan 1st 1970. | |
22 int64_t Now() { | |
23 return (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds(); | |
24 } | |
25 } // namespace | 21 } // namespace |
26 | 22 |
27 TEST(ReadingListEntry, CompareIgnoreTitle) { | 23 TEST(ReadingListEntry, CompareIgnoreTitle) { |
28 const ReadingListEntry e1(GURL("http://example.com"), "bar"); | 24 const ReadingListEntry e1(GURL("http://example.com"), "bar"); |
29 const ReadingListEntry e2(GURL("http://example.com"), "foo"); | 25 const ReadingListEntry e2(GURL("http://example.com"), "foo"); |
30 | 26 |
31 EXPECT_EQ(e1, e2); | 27 EXPECT_EQ(e1, e2); |
32 } | 28 } |
33 | 29 |
34 TEST(ReadingListEntry, CompareFailureIgnoreTitle) { | 30 TEST(ReadingListEntry, CompareFailureIgnoreTitle) { |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 EXPECT_EQ(distilled_pb_entry->failed_download_counter(), 0); | 311 EXPECT_EQ(distilled_pb_entry->failed_download_counter(), 0); |
316 EXPECT_EQ(distilled_pb_entry->distillation_time_us(), | 312 EXPECT_EQ(distilled_pb_entry->distillation_time_us(), |
317 entry.DistillationTime()); | 313 entry.DistillationTime()); |
318 EXPECT_EQ(distilled_pb_entry->distillation_size(), entry.DistillationSize()); | 314 EXPECT_EQ(distilled_pb_entry->distillation_size(), entry.DistillationSize()); |
319 } | 315 } |
320 | 316 |
321 // Tests that the reading list entry is correctly parsed from | 317 // Tests that the reading list entry is correctly parsed from |
322 // sync_pb::ReadingListLocal. | 318 // sync_pb::ReadingListLocal. |
323 TEST(ReadingListEntry, FromReadingListLocal) { | 319 TEST(ReadingListEntry, FromReadingListLocal) { |
324 ReadingListEntry entry(GURL("http://example.com/"), "title"); | 320 ReadingListEntry entry(GURL("http://example.com/"), "title"); |
325 base::Time next_call = base::Time::Now() + entry.TimeUntilNextTry(); | 321 entry.SetDistilledState(ReadingListEntry::ERROR); |
326 | 322 |
327 std::unique_ptr<reading_list::ReadingListLocal> pb_entry( | 323 std::unique_ptr<reading_list::ReadingListLocal> pb_entry( |
328 entry.AsReadingListLocal()); | 324 entry.AsReadingListLocal()); |
329 int64_t now = Now(); | 325 int64_t now = 12345; |
330 | 326 |
331 pb_entry->set_entry_id("http://example.com/"); | 327 pb_entry->set_entry_id("http://example.com/"); |
332 pb_entry->set_url("http://example.com/"); | 328 pb_entry->set_url("http://example.com/"); |
333 pb_entry->set_title("title"); | 329 pb_entry->set_title("title"); |
334 pb_entry->set_creation_time_us(1); | 330 pb_entry->set_creation_time_us(1); |
335 pb_entry->set_update_time_us(2); | 331 pb_entry->set_update_time_us(2); |
336 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); | 332 pb_entry->set_status(reading_list::ReadingListLocal::UNREAD); |
337 pb_entry->set_distillation_state(reading_list::ReadingListLocal::WAITING); | 333 pb_entry->set_distillation_state(reading_list::ReadingListLocal::WAITING); |
338 pb_entry->set_failed_download_counter(2); | 334 pb_entry->set_failed_download_counter(2); |
339 pb_entry->set_distillation_time_us(now); | 335 pb_entry->set_distillation_time_us(now); |
340 pb_entry->set_distillation_size(50); | 336 pb_entry->set_distillation_size(50); |
341 | 337 |
342 std::unique_ptr<ReadingListEntry> waiting_entry( | 338 std::unique_ptr<ReadingListEntry> waiting_entry( |
343 ReadingListEntry::FromReadingListLocal(*pb_entry)); | 339 ReadingListEntry::FromReadingListLocal(*pb_entry)); |
344 EXPECT_EQ(waiting_entry->URL().spec(), "http://example.com/"); | 340 EXPECT_EQ(waiting_entry->URL().spec(), "http://example.com/"); |
345 EXPECT_EQ(waiting_entry->Title(), "title"); | 341 EXPECT_EQ(waiting_entry->Title(), "title"); |
346 EXPECT_EQ(waiting_entry->UpdateTime(), 2); | 342 EXPECT_EQ(waiting_entry->UpdateTime(), 2); |
347 EXPECT_EQ(waiting_entry->FailedDownloadCounter(), 2); | 343 EXPECT_EQ(waiting_entry->FailedDownloadCounter(), 2); |
348 EXPECT_EQ(waiting_entry->DistilledState(), ReadingListEntry::WAITING); | 344 EXPECT_EQ(waiting_entry->DistilledState(), ReadingListEntry::WAITING); |
349 EXPECT_EQ(waiting_entry->DistilledPath(), base::FilePath()); | 345 EXPECT_EQ(waiting_entry->DistilledPath(), base::FilePath()); |
350 EXPECT_EQ(waiting_entry->DistillationSize(), 50); | 346 EXPECT_EQ(waiting_entry->DistillationSize(), 50); |
351 EXPECT_EQ(waiting_entry->DistillationTime(), now); | 347 EXPECT_EQ(waiting_entry->DistillationTime(), now); |
352 base::Time waiting_next_call = | 348 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor; |
353 base::Time::Now() + waiting_entry->TimeUntilNextTry(); | 349 int nextTry = waiting_entry->TimeUntilNextTry().InMinutes(); |
354 base::TimeDelta delta = next_call - waiting_next_call; | 350 EXPECT_NEAR(kFirstBackoff, nextTry, kFirstBackoff * fuzzing); |
355 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); | |
356 } | 351 } |
357 | 352 |
358 // Tests the merging of two ReadingListEntry. | 353 // Tests the merging of two ReadingListEntry. |
359 // Additional merging tests are done in | 354 // Additional merging tests are done in |
360 // ReadingListStoreTest.CompareEntriesForSync | 355 // ReadingListStoreTest.CompareEntriesForSync |
361 TEST(ReadingListEntry, MergeWithEntry) { | 356 TEST(ReadingListEntry, MergeWithEntry) { |
362 ReadingListEntry local_entry(GURL("http://example.com/"), "title"); | 357 ReadingListEntry local_entry(GURL("http://example.com/"), "title"); |
363 local_entry.SetDistilledState(ReadingListEntry::ERROR); | 358 local_entry.SetDistilledState(ReadingListEntry::ERROR); |
364 base::Time next_call = base::Time::Now() + local_entry.TimeUntilNextTry(); | |
365 int64_t local_update_time_us = local_entry.UpdateTime(); | 359 int64_t local_update_time_us = local_entry.UpdateTime(); |
366 | 360 |
367 ReadingListEntry sync_entry(GURL("http://example.com/"), "title2"); | 361 ReadingListEntry sync_entry(GURL("http://example.com/"), "title2"); |
368 sync_entry.SetDistilledState(ReadingListEntry::ERROR); | 362 sync_entry.SetDistilledState(ReadingListEntry::ERROR); |
369 int64_t sync_update_time_us = sync_entry.UpdateTime(); | 363 int64_t sync_update_time_us = sync_entry.UpdateTime(); |
370 EXPECT_NE(local_update_time_us, sync_update_time_us); | 364 EXPECT_NE(local_update_time_us, sync_update_time_us); |
371 local_entry.MergeWithEntry(sync_entry); | 365 local_entry.MergeWithEntry(sync_entry); |
372 EXPECT_EQ(local_entry.URL().spec(), "http://example.com/"); | 366 EXPECT_EQ(local_entry.URL().spec(), "http://example.com/"); |
373 EXPECT_EQ(local_entry.Title(), "title2"); | 367 EXPECT_EQ(local_entry.Title(), "title2"); |
374 EXPECT_FALSE(local_entry.HasBeenSeen()); | 368 EXPECT_FALSE(local_entry.HasBeenSeen()); |
375 EXPECT_EQ(local_entry.UpdateTime(), sync_update_time_us); | 369 EXPECT_EQ(local_entry.UpdateTime(), sync_update_time_us); |
376 EXPECT_EQ(local_entry.FailedDownloadCounter(), 1); | 370 EXPECT_EQ(local_entry.FailedDownloadCounter(), 1); |
377 EXPECT_EQ(local_entry.DistilledState(), ReadingListEntry::ERROR); | 371 EXPECT_EQ(local_entry.DistilledState(), ReadingListEntry::ERROR); |
378 base::Time merge_next_call = | 372 double fuzzing = ReadingListEntry::kBackoffPolicy.jitter_factor; |
379 base::Time::Now() + local_entry.TimeUntilNextTry(); | 373 int nextTry = local_entry.TimeUntilNextTry().InMinutes(); |
380 base::TimeDelta delta = merge_next_call - next_call; | 374 EXPECT_NEAR(kFirstBackoff, nextTry, kFirstBackoff * fuzzing); |
381 EXPECT_NEAR(delta.InMillisecondsRoundedUp(), 0, 10); | |
382 } | 375 } |
OLD | NEW |