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_store.h" | 5 #include "components/reading_list/ios/reading_list_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #import "base/test/ios/wait_util.h" | 14 #import "base/test/ios/wait_util.h" |
15 #include "components/reading_list/ios/reading_list_model_impl.h" | 15 #include "components/reading_list/ios/reading_list_model_impl.h" |
16 #include "components/sync/model/fake_model_type_change_processor.h" | 16 #include "components/sync/model/fake_model_type_change_processor.h" |
17 #include "components/sync/model/model_type_store_test_util.h" | 17 #include "components/sync/model/model_type_store_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
| 20 // Tests that the transition from |entryA| to |entryB| is possible (|possible| |
| 21 // is true) or not. |
| 22 void ExpectAB(const sync_pb::ReadingListSpecifics& entryA, |
| 23 const sync_pb::ReadingListSpecifics& entryB, |
| 24 bool possible) { |
| 25 EXPECT_EQ(ReadingListStore::CompareEntriesForSync(entryA, entryB), possible); |
| 26 std::unique_ptr<ReadingListEntry> a = |
| 27 ReadingListEntry::FromReadingListSpecifics(entryA); |
| 28 std::unique_ptr<ReadingListEntry> b = |
| 29 ReadingListEntry::FromReadingListSpecifics(entryB); |
| 30 a->MergeWithEntry(*b); |
| 31 std::unique_ptr<sync_pb::ReadingListSpecifics> mergedEntry = |
| 32 a->AsReadingListSpecifics(); |
| 33 if (possible) { |
| 34 // If transition is possible, the merge should be B. |
| 35 EXPECT_EQ(entryB.SerializeAsString(), mergedEntry->SerializeAsString()); |
| 36 } else { |
| 37 // If transition is not possible, the transition shold be possible to the |
| 38 // merged state. |
| 39 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, *mergedEntry)); |
| 40 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryB, *mergedEntry)); |
| 41 } |
| 42 } |
| 43 |
20 class FakeModelTypeChangeProcessorObserver { | 44 class FakeModelTypeChangeProcessorObserver { |
21 public: | 45 public: |
22 virtual void Put(const std::string& client_tag, | 46 virtual void Put(const std::string& client_tag, |
23 std::unique_ptr<syncer::EntityData> entity_data, | 47 std::unique_ptr<syncer::EntityData> entity_data, |
24 syncer::MetadataChangeList* metadata_change_list) = 0; | 48 syncer::MetadataChangeList* metadata_change_list) = 0; |
25 | 49 |
26 virtual void Delete(const std::string& client_tag, | 50 virtual void Delete(const std::string& client_tag, |
27 syncer::MetadataChangeList* metadata_change_list) = 0; | 51 syncer::MetadataChangeList* metadata_change_list) = 0; |
28 }; | 52 }; |
29 | 53 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 syncer::SyncError error = reading_list_store_->ApplySyncChanges( | 310 syncer::SyncError error = reading_list_store_->ApplySyncChanges( |
287 reading_list_store_->CreateMetadataChangeList(), delete_changes); | 311 reading_list_store_->CreateMetadataChangeList(), delete_changes); |
288 AssertCounts(0, 0, 0, 1, 0); | 312 AssertCounts(0, 0, 0, 1, 0); |
289 EXPECT_EQ(sync_removed_.size(), 1u); | 313 EXPECT_EQ(sync_removed_.size(), 1u); |
290 EXPECT_EQ(sync_removed_.count("http://read.example.com/"), 1u); | 314 EXPECT_EQ(sync_removed_.count("http://read.example.com/"), 1u); |
291 } | 315 } |
292 | 316 |
293 TEST_F(ReadingListStoreTest, CompareEntriesForSync) { | 317 TEST_F(ReadingListStoreTest, CompareEntriesForSync) { |
294 sync_pb::ReadingListSpecifics entryA; | 318 sync_pb::ReadingListSpecifics entryA; |
295 sync_pb::ReadingListSpecifics entryB; | 319 sync_pb::ReadingListSpecifics entryB; |
296 entryA.set_url("http://foo.bar"); | 320 entryA.set_entry_id("http://foo.bar/"); |
297 entryB.set_url("http://foo.bar"); | 321 entryB.set_entry_id("http://foo.bar/"); |
| 322 entryA.set_url("http://foo.bar/"); |
| 323 entryB.set_url("http://foo.bar/"); |
298 entryA.set_title("Foo Bar"); | 324 entryA.set_title("Foo Bar"); |
299 entryB.set_title("Foo Bar"); | 325 entryB.set_title("Foo Bar"); |
300 entryA.set_status(sync_pb::ReadingListSpecifics::UNREAD); | 326 entryA.set_status(sync_pb::ReadingListSpecifics::UNREAD); |
301 entryB.set_status(sync_pb::ReadingListSpecifics::UNREAD); | 327 entryB.set_status(sync_pb::ReadingListSpecifics::UNREAD); |
302 entryA.set_creation_time_us(10); | 328 entryA.set_creation_time_us(10); |
303 entryB.set_creation_time_us(10); | 329 entryB.set_creation_time_us(10); |
| 330 entryA.set_first_read_time_us(50); |
| 331 entryB.set_first_read_time_us(50); |
304 entryA.set_update_time_us(100); | 332 entryA.set_update_time_us(100); |
305 entryB.set_update_time_us(100); | 333 entryB.set_update_time_us(100); |
306 // Equal entries can be submitted. | 334 // Equal entries can be submitted. |
307 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 335 ExpectAB(entryA, entryB, true); |
308 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 336 ExpectAB(entryB, entryA, true); |
309 | 337 |
310 // Try to update each field. | 338 // Try to update each field. |
311 | 339 |
312 // You cannot change the URL of an entry. | 340 // You cannot change the URL of an entry. |
313 entryA.set_url("http://foo.foo"); | 341 entryA.set_url("http://foo.foo/"); |
314 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 342 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); |
315 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 343 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); |
316 entryA.set_url("http://foo.bar"); | 344 entryA.set_url("http://foo.bar/"); |
317 | 345 |
318 // You can set a title to a title later in alphabetical order. | 346 // You can set a title to a title later in alphabetical order. |
319 entryA.set_title(""); | 347 entryA.set_title(""); |
320 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 348 ExpectAB(entryA, entryB, true); |
321 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 349 ExpectAB(entryB, entryA, false); |
322 entryA.set_title("Foo Aar"); | 350 entryA.set_title("Foo Aar"); |
323 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 351 ExpectAB(entryA, entryB, true); |
324 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 352 ExpectAB(entryB, entryA, false); |
325 entryA.set_title("Foo Ba"); | 353 entryA.set_title("Foo Ba"); |
326 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 354 ExpectAB(entryA, entryB, true); |
327 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 355 ExpectAB(entryB, entryA, false); |
328 entryA.set_title("Foo Bar"); | 356 entryA.set_title("Foo Bar"); |
329 | 357 |
330 entryA.set_creation_time_us(9); | 358 entryA.set_creation_time_us(9); |
331 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 359 ExpectAB(entryA, entryB, true); |
332 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 360 ExpectAB(entryB, entryA, false); |
| 361 entryA.set_first_read_time_us(51); |
| 362 ExpectAB(entryA, entryB, true); |
| 363 ExpectAB(entryB, entryA, false); |
| 364 entryA.set_first_read_time_us(49); |
| 365 ExpectAB(entryA, entryB, true); |
| 366 ExpectAB(entryB, entryA, false); |
| 367 entryA.set_first_read_time_us(0); |
| 368 ExpectAB(entryA, entryB, true); |
| 369 ExpectAB(entryB, entryA, false); |
| 370 entryA.set_first_read_time_us(50); |
| 371 entryB.set_first_read_time_us(0); |
| 372 ExpectAB(entryA, entryB, true); |
| 373 ExpectAB(entryB, entryA, false); |
| 374 entryB.set_first_read_time_us(50); |
333 entryA.set_creation_time_us(10); | 375 entryA.set_creation_time_us(10); |
| 376 entryA.set_first_read_time_us(51); |
| 377 ExpectAB(entryA, entryB, true); |
| 378 ExpectAB(entryB, entryA, false); |
| 379 entryA.set_first_read_time_us(0); |
| 380 ExpectAB(entryA, entryB, true); |
| 381 ExpectAB(entryB, entryA, false); |
| 382 entryA.set_first_read_time_us(50); |
334 | 383 |
335 entryA.set_update_time_us(99); | 384 entryA.set_update_time_us(99); |
336 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 385 ExpectAB(entryA, entryB, true); |
337 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 386 ExpectAB(entryB, entryA, false); |
338 sync_pb::ReadingListSpecifics::ReadingListEntryStatus status_oder[3] = { | 387 sync_pb::ReadingListSpecifics::ReadingListEntryStatus status_oder[3] = { |
339 sync_pb::ReadingListSpecifics::UNSEEN, | 388 sync_pb::ReadingListSpecifics::UNSEEN, |
340 sync_pb::ReadingListSpecifics::UNREAD, | 389 sync_pb::ReadingListSpecifics::UNREAD, |
341 sync_pb::ReadingListSpecifics::READ}; | 390 sync_pb::ReadingListSpecifics::READ}; |
342 for (int index_a = 0; index_a < 3; index_a++) { | 391 for (int index_a = 0; index_a < 3; index_a++) { |
343 entryA.set_status(status_oder[index_a]); | 392 entryA.set_status(status_oder[index_a]); |
344 for (int index_b = 0; index_b < 3; index_b++) { | 393 for (int index_b = 0; index_b < 3; index_b++) { |
345 entryB.set_status(status_oder[index_b]); | 394 entryB.set_status(status_oder[index_b]); |
346 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 395 ExpectAB(entryA, entryB, true); |
347 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 396 ExpectAB(entryB, entryA, false); |
348 } | 397 } |
349 } | 398 } |
350 entryA.set_update_time_us(100); | 399 entryA.set_update_time_us(100); |
351 for (int index_a = 0; index_a < 3; index_a++) { | 400 for (int index_a = 0; index_a < 3; index_a++) { |
352 entryA.set_status(status_oder[index_a]); | 401 entryA.set_status(status_oder[index_a]); |
353 entryB.set_status(status_oder[index_a]); | 402 entryB.set_status(status_oder[index_a]); |
354 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 403 ExpectAB(entryA, entryB, true); |
355 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 404 ExpectAB(entryB, entryA, true); |
356 for (int index_b = index_a + 1; index_b < 3; index_b++) { | 405 for (int index_b = index_a + 1; index_b < 3; index_b++) { |
357 entryB.set_status(status_oder[index_b]); | 406 entryB.set_status(status_oder[index_b]); |
358 EXPECT_TRUE(ReadingListStore::CompareEntriesForSync(entryA, entryB)); | 407 ExpectAB(entryA, entryB, true); |
359 EXPECT_FALSE(ReadingListStore::CompareEntriesForSync(entryB, entryA)); | 408 ExpectAB(entryB, entryA, false); |
360 } | 409 } |
361 } | 410 } |
362 } | 411 } |
OLD | NEW |