| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/syncable/directory_unittest.h" | 5 #include "sync/syncable/directory_unittest.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/test/values_test_util.h" | 8 #include "base/test/values_test_util.h" |
| 9 #include "sync/internal_api/public/base/attachment_id_proto.h" | |
| 10 #include "sync/syncable/syncable_proto_util.h" | 9 #include "sync/syncable/syncable_proto_util.h" |
| 11 #include "sync/syncable/syncable_util.h" | 10 #include "sync/syncable/syncable_util.h" |
| 12 #include "sync/syncable/syncable_write_transaction.h" | 11 #include "sync/syncable/syncable_write_transaction.h" |
| 13 #include "sync/test/engine/test_syncable_utils.h" | 12 #include "sync/test/engine/test_syncable_utils.h" |
| 14 #include "sync/test/test_directory_backing_store.h" | 13 #include "sync/test/test_directory_backing_store.h" |
| 15 | 14 |
| 16 using base::ExpectDictBooleanValue; | 15 using base::ExpectDictBooleanValue; |
| 17 using base::ExpectDictStringValue; | 16 using base::ExpectDictStringValue; |
| 18 | 17 |
| 19 namespace syncer { | 18 namespace syncer { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 NULL, | 77 NULL, |
| 79 NULL, | 78 NULL, |
| 80 NULL)); | 79 NULL)); |
| 81 | 80 |
| 82 DirOpenResult open_result = | 81 DirOpenResult open_result = |
| 83 dir_->Open(kDirectoryName, &delegate_, NullTransactionObserver()); | 82 dir_->Open(kDirectoryName, &delegate_, NullTransactionObserver()); |
| 84 return open_result; | 83 return open_result; |
| 85 } | 84 } |
| 86 | 85 |
| 87 // Creates an empty entry and sets the ID field to a default one. | 86 // Creates an empty entry and sets the ID field to a default one. |
| 88 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type, | 87 void SyncableDirectoryTest::CreateEntry(const std::string& entryname) { |
| 89 const std::string& entryname) { | 88 CreateEntry(entryname, TestIdFactory::FromNumber(-99)); |
| 90 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(-99)); | |
| 91 } | 89 } |
| 92 | 90 |
| 93 // Creates an empty entry and sets the ID field to id. | 91 // Creates an empty entry and sets the ID field to id. |
| 94 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type, | 92 void SyncableDirectoryTest::CreateEntry(const std::string& entryname, |
| 95 const std::string& entryname, | |
| 96 const int id) { | 93 const int id) { |
| 97 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(id)); | 94 CreateEntry(entryname, TestIdFactory::FromNumber(id)); |
| 98 } | 95 } |
| 99 | 96 void SyncableDirectoryTest::CreateEntry(const std::string& entryname, Id id) { |
| 100 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type, | |
| 101 const std::string& entryname, | |
| 102 const Id& id) { | |
| 103 CreateEntryWithAttachmentMetadata( | |
| 104 model_type, entryname, id, sync_pb::AttachmentMetadata()); | |
| 105 } | |
| 106 | |
| 107 void SyncableDirectoryTest::CreateEntryWithAttachmentMetadata( | |
| 108 const ModelType& model_type, | |
| 109 const std::string& entryname, | |
| 110 const Id& id, | |
| 111 const sync_pb::AttachmentMetadata& attachment_metadata) { | |
| 112 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); | 97 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); |
| 113 MutableEntry me(&wtrans, CREATE, model_type, wtrans.root_id(), entryname); | 98 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), entryname); |
| 114 ASSERT_TRUE(me.good()); | 99 ASSERT_TRUE(me.good()); |
| 115 me.PutId(id); | 100 me.PutId(id); |
| 116 me.PutAttachmentMetadata(attachment_metadata); | |
| 117 me.PutIsUnsynced(true); | 101 me.PutIsUnsynced(true); |
| 118 } | 102 } |
| 119 | 103 |
| 120 void SyncableDirectoryTest::DeleteEntry(const Id& id) { | |
| 121 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | |
| 122 MutableEntry entry(&trans, GET_BY_ID, id); | |
| 123 ASSERT_TRUE(entry.good()); | |
| 124 entry.PutIsDel(true); | |
| 125 } | |
| 126 | |
| 127 DirOpenResult SyncableDirectoryTest::SimulateSaveAndReloadDir() { | 104 DirOpenResult SyncableDirectoryTest::SimulateSaveAndReloadDir() { |
| 128 if (!dir_->SaveChanges()) | 105 if (!dir_->SaveChanges()) |
| 129 return FAILED_IN_UNITTEST; | 106 return FAILED_IN_UNITTEST; |
| 130 | 107 |
| 131 return ReopenDirectory(); | 108 return ReopenDirectory(); |
| 132 } | 109 } |
| 133 | 110 |
| 134 DirOpenResult SyncableDirectoryTest::SimulateCrashAndReloadDir() { | 111 DirOpenResult SyncableDirectoryTest::SimulateCrashAndReloadDir() { |
| 135 return ReopenDirectory(); | 112 return ReopenDirectory(); |
| 136 } | 113 } |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 sync_pb::EntitySpecifics bookmark_specifics; | 404 sync_pb::EntitySpecifics bookmark_specifics; |
| 428 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics); | 405 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics); |
| 429 bookmark_specifics.mutable_bookmark()->set_url("url"); | 406 bookmark_specifics.mutable_bookmark()->set_url("url"); |
| 430 | 407 |
| 431 Id id1 = TestIdFactory::FromNumber(-1); | 408 Id id1 = TestIdFactory::FromNumber(-1); |
| 432 Id id2 = TestIdFactory::FromNumber(-2); | 409 Id id2 = TestIdFactory::FromNumber(-2); |
| 433 int64 handle1 = 0; | 410 int64 handle1 = 0; |
| 434 int64 handle2 = 0; | 411 int64 handle2 = 0; |
| 435 { | 412 { |
| 436 // Create two bookmark entries and save in database. | 413 // Create two bookmark entries and save in database. |
| 437 CreateEntry(BOOKMARKS, "item1", id1); | 414 CreateEntry("item1", id1); |
| 438 CreateEntry(BOOKMARKS, "item2", id2); | 415 CreateEntry("item2", id2); |
| 439 { | 416 { |
| 440 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | 417 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); |
| 441 MutableEntry item1(&trans, GET_BY_ID, id1); | 418 MutableEntry item1(&trans, GET_BY_ID, id1); |
| 442 ASSERT_TRUE(item1.good()); | 419 ASSERT_TRUE(item1.good()); |
| 443 handle1 = item1.GetMetahandle(); | 420 handle1 = item1.GetMetahandle(); |
| 444 item1.PutSpecifics(bookmark_specifics); | 421 item1.PutSpecifics(bookmark_specifics); |
| 445 item1.PutServerSpecifics(bookmark_specifics); | 422 item1.PutServerSpecifics(bookmark_specifics); |
| 446 MutableEntry item2(&trans, GET_BY_ID, id2); | 423 MutableEntry item2(&trans, GET_BY_ID, id2); |
| 447 ASSERT_TRUE(item2.good()); | 424 ASSERT_TRUE(item2.good()); |
| 448 handle2 = item2.GetMetahandle(); | 425 handle2 = item2.GetMetahandle(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 } | 522 } |
| 546 } | 523 } |
| 547 | 524 |
| 548 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { | 525 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { |
| 549 ReadTransaction rtrans(FROM_HERE, dir().get()); | 526 ReadTransaction rtrans(FROM_HERE, dir().get()); |
| 550 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 527 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
| 551 ASSERT_FALSE(e.good()); | 528 ASSERT_FALSE(e.good()); |
| 552 } | 529 } |
| 553 | 530 |
| 554 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) { | 531 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) { |
| 555 CreateEntry(BOOKMARKS, "rtc"); | 532 CreateEntry("rtc"); |
| 556 ReadTransaction rtrans(FROM_HERE, dir().get()); | 533 ReadTransaction rtrans(FROM_HERE, dir().get()); |
| 557 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 534 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
| 558 ASSERT_TRUE(e.good()); | 535 ASSERT_TRUE(e.good()); |
| 559 } | 536 } |
| 560 | 537 |
| 561 TEST_F(SyncableDirectoryTest, TestDelete) { | 538 TEST_F(SyncableDirectoryTest, TestDelete) { |
| 562 std::string name = "peanut butter jelly time"; | 539 std::string name = "peanut butter jelly time"; |
| 563 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | 540 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); |
| 564 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name); | 541 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name); |
| 565 ASSERT_TRUE(e1.good()); | 542 ASSERT_TRUE(e1.good()); |
| (...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1537 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i)); | 1514 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i)); |
| 1538 ASSERT_TRUE(base::PlatformThread::Create( | 1515 ASSERT_TRUE(base::PlatformThread::Create( |
| 1539 0, thread_delegates[i].get(), &threads[i])); | 1516 0, thread_delegates[i].get(), &threads[i])); |
| 1540 } | 1517 } |
| 1541 | 1518 |
| 1542 for (int i = 0; i < kThreadCount; ++i) { | 1519 for (int i = 0; i < kThreadCount; ++i) { |
| 1543 base::PlatformThread::Join(threads[i]); | 1520 base::PlatformThread::Join(threads[i]); |
| 1544 } | 1521 } |
| 1545 } | 1522 } |
| 1546 | 1523 |
| 1547 // Verify that Directory is notifed when a MutableEntry's AttachmentMetadata | |
| 1548 // changes. | |
| 1549 TEST_F(SyncableDirectoryTest, MutableEntry_PutAttachmentMetadata) { | |
| 1550 sync_pb::AttachmentMetadata attachment_metadata; | |
| 1551 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record(); | |
| 1552 sync_pb::AttachmentIdProto attachment_id_proto = | |
| 1553 syncer::CreateAttachmentIdProto(); | |
| 1554 *record->mutable_id() = attachment_id_proto; | |
| 1555 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1556 { | |
| 1557 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | |
| 1558 | |
| 1559 // Create an entry with attachment metadata and see that the attachment id | |
| 1560 // is not linked. | |
| 1561 MutableEntry entry( | |
| 1562 &trans, CREATE, PREFERENCES, trans.root_id(), "some entry"); | |
| 1563 entry.PutId(TestIdFactory::FromNumber(-1)); | |
| 1564 entry.PutIsUnsynced(true); | |
| 1565 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1566 | |
| 1567 // Now add the attachment metadata and see that Directory believes it is | |
| 1568 // linked. | |
| 1569 entry.PutAttachmentMetadata(attachment_metadata); | |
| 1570 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1571 | |
| 1572 // Clear out the attachment metadata and see that it's no longer linked. | |
| 1573 sync_pb::AttachmentMetadata empty_attachment_metadata; | |
| 1574 entry.PutAttachmentMetadata(empty_attachment_metadata); | |
| 1575 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1576 } | |
| 1577 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1578 } | |
| 1579 | |
| 1580 // Verify that deleted entries with attachments will retain the attachments. | |
| 1581 TEST_F(SyncableDirectoryTest, Directory_DeleteDoesNotUnlinkAttachments) { | |
| 1582 sync_pb::AttachmentMetadata attachment_metadata; | |
| 1583 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record(); | |
| 1584 sync_pb::AttachmentIdProto attachment_id_proto = | |
| 1585 syncer::CreateAttachmentIdProto(); | |
| 1586 *record->mutable_id() = attachment_id_proto; | |
| 1587 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1588 const Id id = TestIdFactory::FromNumber(-1); | |
| 1589 | |
| 1590 // Create an entry with attachment metadata and see that the attachment id | |
| 1591 // is linked. | |
| 1592 CreateEntryWithAttachmentMetadata( | |
| 1593 PREFERENCES, "some entry", id, attachment_metadata); | |
| 1594 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1595 | |
| 1596 // Delete the entry and see that it's still linked because the entry hasn't | |
| 1597 // yet been purged. | |
| 1598 DeleteEntry(id); | |
| 1599 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1600 | |
| 1601 // Reload the Directory, purging the deleted entry, and see that the | |
| 1602 // attachment is no longer linked. | |
| 1603 SimulateSaveAndReloadDir(); | |
| 1604 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1605 } | |
| 1606 | |
| 1607 // Verify that a given attachment can be referenced by multiple entries and that | |
| 1608 // any one of the references is sufficient to ensure it remains linked. | |
| 1609 TEST_F(SyncableDirectoryTest, Directory_LastReferenceUnlinksAttachments) { | |
| 1610 // Create one attachment. | |
| 1611 sync_pb::AttachmentMetadata attachment_metadata; | |
| 1612 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record(); | |
| 1613 sync_pb::AttachmentIdProto attachment_id_proto = | |
| 1614 syncer::CreateAttachmentIdProto(); | |
| 1615 *record->mutable_id() = attachment_id_proto; | |
| 1616 | |
| 1617 // Create two entries, each referencing the attachment. | |
| 1618 const Id id1 = TestIdFactory::FromNumber(-1); | |
| 1619 const Id id2 = TestIdFactory::FromNumber(-2); | |
| 1620 CreateEntryWithAttachmentMetadata( | |
| 1621 PREFERENCES, "some entry", id1, attachment_metadata); | |
| 1622 CreateEntryWithAttachmentMetadata( | |
| 1623 PREFERENCES, "some other entry", id2, attachment_metadata); | |
| 1624 | |
| 1625 // See that the attachment is considered linked. | |
| 1626 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1627 | |
| 1628 // Delete the first entry, reload the Directory, see that the attachment is | |
| 1629 // still linked. | |
| 1630 DeleteEntry(id1); | |
| 1631 SimulateSaveAndReloadDir(); | |
| 1632 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1633 | |
| 1634 // Delete the second entry, reload the Directory, see that the attachment is | |
| 1635 // no loner linked. | |
| 1636 DeleteEntry(id2); | |
| 1637 SimulateSaveAndReloadDir(); | |
| 1638 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto)); | |
| 1639 } | |
| 1640 | |
| 1641 } // namespace syncable | 1524 } // namespace syncable |
| 1642 | 1525 |
| 1643 } // namespace syncer | 1526 } // namespace syncer |
| OLD | NEW |