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" |
9 #include "sync/syncable/syncable_proto_util.h" | 10 #include "sync/syncable/syncable_proto_util.h" |
10 #include "sync/syncable/syncable_util.h" | 11 #include "sync/syncable/syncable_util.h" |
11 #include "sync/syncable/syncable_write_transaction.h" | 12 #include "sync/syncable/syncable_write_transaction.h" |
12 #include "sync/test/engine/test_syncable_utils.h" | 13 #include "sync/test/engine/test_syncable_utils.h" |
13 #include "sync/test/test_directory_backing_store.h" | 14 #include "sync/test/test_directory_backing_store.h" |
14 | 15 |
15 using base::ExpectDictBooleanValue; | 16 using base::ExpectDictBooleanValue; |
16 using base::ExpectDictStringValue; | 17 using base::ExpectDictStringValue; |
17 | 18 |
18 namespace syncer { | 19 namespace syncer { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 NULL, | 78 NULL, |
78 NULL, | 79 NULL, |
79 NULL)); | 80 NULL)); |
80 | 81 |
81 DirOpenResult open_result = | 82 DirOpenResult open_result = |
82 dir_->Open(kDirectoryName, &delegate_, NullTransactionObserver()); | 83 dir_->Open(kDirectoryName, &delegate_, NullTransactionObserver()); |
83 return open_result; | 84 return open_result; |
84 } | 85 } |
85 | 86 |
86 // Creates an empty entry and sets the ID field to a default one. | 87 // Creates an empty entry and sets the ID field to a default one. |
87 void SyncableDirectoryTest::CreateEntry(const std::string& entryname) { | 88 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type, |
88 CreateEntry(entryname, TestIdFactory::FromNumber(-99)); | 89 const std::string& entryname) { |
| 90 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(-99)); |
89 } | 91 } |
90 | 92 |
91 // Creates an empty entry and sets the ID field to id. | 93 // Creates an empty entry and sets the ID field to id. |
92 void SyncableDirectoryTest::CreateEntry(const std::string& entryname, | 94 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type, |
| 95 const std::string& entryname, |
93 const int id) { | 96 const int id) { |
94 CreateEntry(entryname, TestIdFactory::FromNumber(id)); | 97 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(id)); |
95 } | 98 } |
96 void SyncableDirectoryTest::CreateEntry(const std::string& entryname, Id id) { | 99 |
| 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) { |
97 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); | 112 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); |
98 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), entryname); | 113 MutableEntry me(&wtrans, CREATE, model_type, wtrans.root_id(), entryname); |
99 ASSERT_TRUE(me.good()); | 114 ASSERT_TRUE(me.good()); |
100 me.PutId(id); | 115 me.PutId(id); |
| 116 me.PutAttachmentMetadata(attachment_metadata); |
101 me.PutIsUnsynced(true); | 117 me.PutIsUnsynced(true); |
102 } | 118 } |
103 | 119 |
| 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 |
104 DirOpenResult SyncableDirectoryTest::SimulateSaveAndReloadDir() { | 127 DirOpenResult SyncableDirectoryTest::SimulateSaveAndReloadDir() { |
105 if (!dir_->SaveChanges()) | 128 if (!dir_->SaveChanges()) |
106 return FAILED_IN_UNITTEST; | 129 return FAILED_IN_UNITTEST; |
107 | 130 |
108 return ReopenDirectory(); | 131 return ReopenDirectory(); |
109 } | 132 } |
110 | 133 |
111 DirOpenResult SyncableDirectoryTest::SimulateCrashAndReloadDir() { | 134 DirOpenResult SyncableDirectoryTest::SimulateCrashAndReloadDir() { |
112 return ReopenDirectory(); | 135 return ReopenDirectory(); |
113 } | 136 } |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 sync_pb::EntitySpecifics bookmark_specifics; | 427 sync_pb::EntitySpecifics bookmark_specifics; |
405 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics); | 428 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics); |
406 bookmark_specifics.mutable_bookmark()->set_url("url"); | 429 bookmark_specifics.mutable_bookmark()->set_url("url"); |
407 | 430 |
408 Id id1 = TestIdFactory::FromNumber(-1); | 431 Id id1 = TestIdFactory::FromNumber(-1); |
409 Id id2 = TestIdFactory::FromNumber(-2); | 432 Id id2 = TestIdFactory::FromNumber(-2); |
410 int64 handle1 = 0; | 433 int64 handle1 = 0; |
411 int64 handle2 = 0; | 434 int64 handle2 = 0; |
412 { | 435 { |
413 // Create two bookmark entries and save in database. | 436 // Create two bookmark entries and save in database. |
414 CreateEntry("item1", id1); | 437 CreateEntry(BOOKMARKS, "item1", id1); |
415 CreateEntry("item2", id2); | 438 CreateEntry(BOOKMARKS, "item2", id2); |
416 { | 439 { |
417 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | 440 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); |
418 MutableEntry item1(&trans, GET_BY_ID, id1); | 441 MutableEntry item1(&trans, GET_BY_ID, id1); |
419 ASSERT_TRUE(item1.good()); | 442 ASSERT_TRUE(item1.good()); |
420 handle1 = item1.GetMetahandle(); | 443 handle1 = item1.GetMetahandle(); |
421 item1.PutSpecifics(bookmark_specifics); | 444 item1.PutSpecifics(bookmark_specifics); |
422 item1.PutServerSpecifics(bookmark_specifics); | 445 item1.PutServerSpecifics(bookmark_specifics); |
423 MutableEntry item2(&trans, GET_BY_ID, id2); | 446 MutableEntry item2(&trans, GET_BY_ID, id2); |
424 ASSERT_TRUE(item2.good()); | 447 ASSERT_TRUE(item2.good()); |
425 handle2 = item2.GetMetahandle(); | 448 handle2 = item2.GetMetahandle(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 } | 545 } |
523 } | 546 } |
524 | 547 |
525 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { | 548 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { |
526 ReadTransaction rtrans(FROM_HERE, dir().get()); | 549 ReadTransaction rtrans(FROM_HERE, dir().get()); |
527 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 550 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
528 ASSERT_FALSE(e.good()); | 551 ASSERT_FALSE(e.good()); |
529 } | 552 } |
530 | 553 |
531 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) { | 554 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) { |
532 CreateEntry("rtc"); | 555 CreateEntry(BOOKMARKS, "rtc"); |
533 ReadTransaction rtrans(FROM_HERE, dir().get()); | 556 ReadTransaction rtrans(FROM_HERE, dir().get()); |
534 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 557 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
535 ASSERT_TRUE(e.good()); | 558 ASSERT_TRUE(e.good()); |
536 } | 559 } |
537 | 560 |
538 TEST_F(SyncableDirectoryTest, TestDelete) { | 561 TEST_F(SyncableDirectoryTest, TestDelete) { |
539 std::string name = "peanut butter jelly time"; | 562 std::string name = "peanut butter jelly time"; |
540 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); | 563 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get()); |
541 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name); | 564 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name); |
542 ASSERT_TRUE(e1.good()); | 565 ASSERT_TRUE(e1.good()); |
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1514 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i)); | 1537 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i)); |
1515 ASSERT_TRUE(base::PlatformThread::Create( | 1538 ASSERT_TRUE(base::PlatformThread::Create( |
1516 0, thread_delegates[i].get(), &threads[i])); | 1539 0, thread_delegates[i].get(), &threads[i])); |
1517 } | 1540 } |
1518 | 1541 |
1519 for (int i = 0; i < kThreadCount; ++i) { | 1542 for (int i = 0; i < kThreadCount; ++i) { |
1520 base::PlatformThread::Join(threads[i]); | 1543 base::PlatformThread::Join(threads[i]); |
1521 } | 1544 } |
1522 } | 1545 } |
1523 | 1546 |
| 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 |
1524 } // namespace syncable | 1641 } // namespace syncable |
1525 | 1642 |
1526 } // namespace syncer | 1643 } // namespace syncer |
OLD | NEW |