| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sync/test/test_entry_factory.h" |
| 6 |
| 7 #include "sync/syncable/directory.h" |
| 8 #include "sync/syncable/mutable_entry.h" |
| 9 #include "sync/syncable/syncable_id.h" |
| 10 #include "sync/syncable/write_transaction.h" |
| 11 #include "sync/test/engine/test_id_factory.h" |
| 12 |
| 13 using std::string; |
| 14 |
| 15 namespace syncer { |
| 16 |
| 17 using syncable::Id; |
| 18 using syncable::MutableEntry; |
| 19 using syncable::UNITTEST; |
| 20 using syncable::WriteTransaction; |
| 21 |
| 22 TestEntryFactory::TestEntryFactory(syncable::Directory *dir) |
| 23 : directory_(dir), next_revision_(1) { |
| 24 } |
| 25 |
| 26 TestEntryFactory::~TestEntryFactory() { } |
| 27 |
| 28 void TestEntryFactory::CreateUnappliedNewItemWithParent( |
| 29 const string& item_id, |
| 30 const sync_pb::EntitySpecifics& specifics, |
| 31 const string& parent_id) { |
| 32 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 33 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 34 Id::CreateFromServerId(item_id)); |
| 35 DCHECK(entry.good()); |
| 36 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 37 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 38 |
| 39 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 40 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); |
| 41 entry.Put(syncable::SERVER_IS_DIR, true); |
| 42 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 43 } |
| 44 |
| 45 void TestEntryFactory::CreateUnappliedNewItem( |
| 46 const string& item_id, |
| 47 const sync_pb::EntitySpecifics& specifics, |
| 48 bool is_unique) { |
| 49 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 50 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 51 Id::CreateFromServerId(item_id)); |
| 52 DCHECK(entry.good()); |
| 53 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 54 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 55 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 56 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); |
| 57 entry.Put(syncable::SERVER_IS_DIR, false); |
| 58 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 59 if (is_unique) // For top-level nodes. |
| 60 entry.Put(syncable::UNIQUE_SERVER_TAG, item_id); |
| 61 } |
| 62 |
| 63 void TestEntryFactory::CreateUnsyncedItem( |
| 64 const Id& item_id, |
| 65 const Id& parent_id, |
| 66 const string& name, |
| 67 bool is_folder, |
| 68 ModelType model_type, |
| 69 int64* metahandle_out) { |
| 70 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 71 Id predecessor_id; |
| 72 DCHECK( |
| 73 directory()->GetLastChildIdForTest(&trans, parent_id, &predecessor_id)); |
| 74 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); |
| 75 DCHECK(entry.good()); |
| 76 entry.Put(syncable::ID, item_id); |
| 77 entry.Put(syncable::BASE_VERSION, |
| 78 item_id.ServerKnows() ? GetNextRevision() : 0); |
| 79 entry.Put(syncable::IS_UNSYNCED, true); |
| 80 entry.Put(syncable::IS_DIR, is_folder); |
| 81 entry.Put(syncable::IS_DEL, false); |
| 82 entry.Put(syncable::PARENT_ID, parent_id); |
| 83 CHECK(entry.PutPredecessor(predecessor_id)); |
| 84 sync_pb::EntitySpecifics default_specifics; |
| 85 AddDefaultFieldValue(model_type, &default_specifics); |
| 86 entry.Put(syncable::SPECIFICS, default_specifics); |
| 87 if (item_id.ServerKnows()) { |
| 88 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 89 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 90 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 91 entry.Put(syncable::SERVER_IS_DEL, false); |
| 92 } |
| 93 if (metahandle_out) |
| 94 *metahandle_out = entry.Get(syncable::META_HANDLE); |
| 95 } |
| 96 |
| 97 int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem( |
| 98 const string& name, ModelType model_type) { |
| 99 int64 metahandle = 0; |
| 100 CreateUnsyncedItem( |
| 101 TestIdFactory::MakeServer(name), TestIdFactory::root(), |
| 102 name, false, model_type, &metahandle); |
| 103 |
| 104 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 105 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); |
| 106 if (!entry.good()) { |
| 107 NOTREACHED(); |
| 108 return syncable::kInvalidMetaHandle; |
| 109 } |
| 110 |
| 111 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 112 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 113 |
| 114 return metahandle; |
| 115 } |
| 116 |
| 117 int64 TestEntryFactory::CreateSyncedItem( |
| 118 const std::string& name, ModelType model_type, bool is_folder) { |
| 119 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 120 |
| 121 syncable::Id parent_id(TestIdFactory::root()); |
| 122 syncable::Id item_id(TestIdFactory::MakeServer(name)); |
| 123 int64 version = GetNextRevision(); |
| 124 |
| 125 sync_pb::EntitySpecifics default_specifics; |
| 126 AddDefaultFieldValue(model_type, &default_specifics); |
| 127 |
| 128 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); |
| 129 if (!entry.good()) { |
| 130 NOTREACHED(); |
| 131 return syncable::kInvalidMetaHandle; |
| 132 } |
| 133 |
| 134 entry.Put(syncable::ID, item_id); |
| 135 entry.Put(syncable::BASE_VERSION, version); |
| 136 entry.Put(syncable::IS_UNSYNCED, false); |
| 137 entry.Put(syncable::NON_UNIQUE_NAME, name); |
| 138 entry.Put(syncable::IS_DIR, is_folder); |
| 139 entry.Put(syncable::IS_DEL, false); |
| 140 entry.Put(syncable::PARENT_ID, parent_id); |
| 141 |
| 142 if (!entry.PutPredecessor(TestIdFactory::root())) { |
| 143 NOTREACHED(); |
| 144 return syncable::kInvalidMetaHandle; |
| 145 } |
| 146 entry.Put(syncable::SPECIFICS, default_specifics); |
| 147 |
| 148 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 149 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 150 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X"); |
| 151 entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y")); |
| 152 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 153 entry.Put(syncable::SERVER_IS_DEL, false); |
| 154 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 155 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 156 |
| 157 return entry.Get(syncable::META_HANDLE); |
| 158 } |
| 159 |
| 160 syncable::Directory* TestEntryFactory::directory() { |
| 161 return directory_; |
| 162 } |
| 163 |
| 164 int64 TestEntryFactory::GetNextRevision() { |
| 165 return next_revision_++; |
| 166 } |
| 167 |
| 168 } // namespace syncer |
| OLD | NEW |