| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 "components/sync/core/test/test_entry_factory.h" | |
| 6 | |
| 7 #include "components/sync/syncable/directory.h" | |
| 8 #include "components/sync/syncable/entry.h" | |
| 9 #include "components/sync/syncable/mutable_entry.h" | |
| 10 #include "components/sync/syncable/syncable_id.h" | |
| 11 #include "components/sync/syncable/syncable_read_transaction.h" | |
| 12 #include "components/sync/syncable/syncable_util.h" | |
| 13 #include "components/sync/syncable/syncable_write_transaction.h" | |
| 14 #include "components/sync/test/engine/test_id_factory.h" | |
| 15 | |
| 16 using std::string; | |
| 17 | |
| 18 namespace syncer { | |
| 19 | |
| 20 using syncable::Id; | |
| 21 using syncable::MutableEntry; | |
| 22 using syncable::UNITTEST; | |
| 23 using syncable::WriteTransaction; | |
| 24 | |
| 25 TestEntryFactory::TestEntryFactory(syncable::Directory* dir) | |
| 26 : directory_(dir), next_revision_(1) {} | |
| 27 | |
| 28 TestEntryFactory::~TestEntryFactory() {} | |
| 29 | |
| 30 int64_t TestEntryFactory::CreateUnappliedNewItemWithParent( | |
| 31 const string& item_id, | |
| 32 const sync_pb::EntitySpecifics& specifics, | |
| 33 const string& parent_id) { | |
| 34 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 35 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 36 Id::CreateFromServerId(item_id)); | |
| 37 DCHECK(entry.good()); | |
| 38 entry.PutServerVersion(GetNextRevision()); | |
| 39 entry.PutIsUnappliedUpdate(true); | |
| 40 | |
| 41 entry.PutServerNonUniqueName(item_id); | |
| 42 entry.PutServerParentId(Id::CreateFromServerId(parent_id)); | |
| 43 entry.PutServerIsDir(true); | |
| 44 entry.PutServerSpecifics(specifics); | |
| 45 return entry.GetMetahandle(); | |
| 46 } | |
| 47 | |
| 48 int64_t TestEntryFactory::CreateUnappliedNewBookmarkItemWithParent( | |
| 49 const string& item_id, | |
| 50 const sync_pb::EntitySpecifics& specifics, | |
| 51 const string& parent_id) { | |
| 52 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 53 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 54 Id::CreateFromServerId(item_id)); | |
| 55 DCHECK(entry.good()); | |
| 56 entry.PutServerVersion(GetNextRevision()); | |
| 57 entry.PutIsUnappliedUpdate(true); | |
| 58 | |
| 59 entry.PutServerNonUniqueName(item_id); | |
| 60 entry.PutServerParentId(Id::CreateFromServerId(parent_id)); | |
| 61 entry.PutServerIsDir(true); | |
| 62 entry.PutServerSpecifics(specifics); | |
| 63 | |
| 64 return entry.GetMetahandle(); | |
| 65 } | |
| 66 | |
| 67 int64_t TestEntryFactory::CreateUnappliedNewItem( | |
| 68 const string& item_id, | |
| 69 const sync_pb::EntitySpecifics& specifics, | |
| 70 bool is_unique) { | |
| 71 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 72 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 73 Id::CreateFromServerId(item_id)); | |
| 74 DCHECK(entry.good()); | |
| 75 entry.PutServerVersion(GetNextRevision()); | |
| 76 entry.PutIsUnappliedUpdate(true); | |
| 77 entry.PutServerNonUniqueName(item_id); | |
| 78 entry.PutServerParentId(syncable::Id::GetRoot()); | |
| 79 entry.PutServerIsDir(is_unique); | |
| 80 entry.PutServerSpecifics(specifics); | |
| 81 if (is_unique) { // For top-level nodes. | |
| 82 entry.PutUniqueServerTag( | |
| 83 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics))); | |
| 84 } | |
| 85 return entry.GetMetahandle(); | |
| 86 } | |
| 87 | |
| 88 void TestEntryFactory::CreateUnsyncedItem(const Id& item_id, | |
| 89 const Id& parent_id, | |
| 90 const string& name, | |
| 91 bool is_folder, | |
| 92 ModelType model_type, | |
| 93 int64_t* metahandle_out) { | |
| 94 if (is_folder) { | |
| 95 DCHECK_EQ(model_type, BOOKMARKS); | |
| 96 } | |
| 97 | |
| 98 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 99 | |
| 100 MutableEntry entry(&trans, syncable::CREATE, model_type, parent_id, name); | |
| 101 DCHECK(entry.good()); | |
| 102 entry.PutId(item_id); | |
| 103 entry.PutBaseVersion(item_id.ServerKnows() ? GetNextRevision() : 0); | |
| 104 entry.PutIsUnsynced(true); | |
| 105 entry.PutIsDir(is_folder); | |
| 106 entry.PutIsDel(false); | |
| 107 entry.PutParentId(parent_id); | |
| 108 sync_pb::EntitySpecifics default_specifics; | |
| 109 AddDefaultFieldValue(model_type, &default_specifics); | |
| 110 entry.PutSpecifics(default_specifics); | |
| 111 | |
| 112 if (item_id.ServerKnows()) { | |
| 113 entry.PutServerSpecifics(default_specifics); | |
| 114 entry.PutServerIsDir(false); | |
| 115 entry.PutServerParentId(parent_id); | |
| 116 entry.PutServerIsDel(false); | |
| 117 } | |
| 118 if (metahandle_out) | |
| 119 *metahandle_out = entry.GetMetahandle(); | |
| 120 } | |
| 121 | |
| 122 int64_t TestEntryFactory::CreateUnappliedAndUnsyncedBookmarkItem( | |
| 123 const string& name) { | |
| 124 int64_t metahandle = 0; | |
| 125 CreateUnsyncedItem(TestIdFactory::MakeServer(name), TestIdFactory::root(), | |
| 126 name, false, BOOKMARKS, &metahandle); | |
| 127 | |
| 128 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 129 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); | |
| 130 if (!entry.good()) { | |
| 131 NOTREACHED(); | |
| 132 return syncable::kInvalidMetaHandle; | |
| 133 } | |
| 134 | |
| 135 entry.PutIsUnappliedUpdate(true); | |
| 136 entry.PutServerVersion(GetNextRevision()); | |
| 137 | |
| 138 return metahandle; | |
| 139 } | |
| 140 | |
| 141 int64_t TestEntryFactory::CreateSyncedItem(const std::string& name, | |
| 142 ModelType model_type, | |
| 143 bool is_folder) { | |
| 144 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 145 | |
| 146 syncable::Id parent_id(TestIdFactory::root()); | |
| 147 syncable::Id item_id(TestIdFactory::MakeServer(name)); | |
| 148 int64_t version = GetNextRevision(); | |
| 149 | |
| 150 MutableEntry entry(&trans, syncable::CREATE, model_type, parent_id, name); | |
| 151 if (!entry.good()) { | |
| 152 NOTREACHED(); | |
| 153 return syncable::kInvalidMetaHandle; | |
| 154 } | |
| 155 | |
| 156 entry.PutId(item_id); | |
| 157 entry.PutBaseVersion(version); | |
| 158 entry.PutIsUnsynced(false); | |
| 159 entry.PutNonUniqueName(name); | |
| 160 entry.PutIsDir(is_folder); | |
| 161 entry.PutIsDel(false); | |
| 162 entry.PutParentId(parent_id); | |
| 163 | |
| 164 entry.PutServerVersion(GetNextRevision()); | |
| 165 entry.PutIsUnappliedUpdate(false); | |
| 166 entry.PutServerNonUniqueName(name); | |
| 167 entry.PutServerParentId(parent_id); | |
| 168 entry.PutServerIsDir(is_folder); | |
| 169 entry.PutServerIsDel(false); | |
| 170 entry.PutServerSpecifics(entry.GetSpecifics()); | |
| 171 | |
| 172 return entry.GetMetahandle(); | |
| 173 } | |
| 174 | |
| 175 int64_t TestEntryFactory::CreateUnappliedRootNode(ModelType model_type) { | |
| 176 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); | |
| 177 sync_pb::EntitySpecifics specifics; | |
| 178 AddDefaultFieldValue(model_type, &specifics); | |
| 179 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); | |
| 180 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
| 181 node_id); | |
| 182 DCHECK(entry.good()); | |
| 183 // Make it look like sort of like a pending creation from the server. | |
| 184 // The SERVER_PARENT_ID and UNIQUE_CLIENT_TAG aren't quite right, but | |
| 185 // it's good enough for our purposes. | |
| 186 entry.PutServerVersion(1); | |
| 187 entry.PutIsUnappliedUpdate(true); | |
| 188 entry.PutServerIsDir(false); | |
| 189 entry.PutServerParentId(TestIdFactory::root()); | |
| 190 entry.PutServerSpecifics(specifics); | |
| 191 entry.PutNonUniqueName("xyz"); | |
| 192 | |
| 193 return entry.GetMetahandle(); | |
| 194 } | |
| 195 | |
| 196 bool TestEntryFactory::SetServerSpecificsForItem( | |
| 197 int64_t meta_handle, | |
| 198 const sync_pb::EntitySpecifics specifics) { | |
| 199 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 200 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 201 if (!entry.good()) { | |
| 202 return false; | |
| 203 } | |
| 204 entry.PutServerSpecifics(specifics); | |
| 205 entry.PutIsUnappliedUpdate(true); | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 bool TestEntryFactory::SetLocalSpecificsForItem( | |
| 210 int64_t meta_handle, | |
| 211 const sync_pb::EntitySpecifics specifics) { | |
| 212 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 213 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 214 if (!entry.good()) { | |
| 215 return false; | |
| 216 } | |
| 217 entry.PutSpecifics(specifics); | |
| 218 entry.PutIsUnsynced(true); | |
| 219 return true; | |
| 220 } | |
| 221 | |
| 222 const sync_pb::EntitySpecifics& TestEntryFactory::GetServerSpecificsForItem( | |
| 223 int64_t meta_handle) const { | |
| 224 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 225 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 226 DCHECK(entry.good()); | |
| 227 return entry.GetServerSpecifics(); | |
| 228 } | |
| 229 | |
| 230 const sync_pb::EntitySpecifics& TestEntryFactory::GetLocalSpecificsForItem( | |
| 231 int64_t meta_handle) const { | |
| 232 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 233 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 234 DCHECK(entry.good()); | |
| 235 return entry.GetSpecifics(); | |
| 236 } | |
| 237 | |
| 238 bool TestEntryFactory::SetServerAttachmentMetadataForItem( | |
| 239 int64_t meta_handle, | |
| 240 const sync_pb::AttachmentMetadata metadata) { | |
| 241 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 242 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 243 if (!entry.good()) { | |
| 244 return false; | |
| 245 } | |
| 246 entry.PutServerAttachmentMetadata(metadata); | |
| 247 entry.PutIsUnappliedUpdate(true); | |
| 248 return true; | |
| 249 } | |
| 250 | |
| 251 bool TestEntryFactory::SetLocalAttachmentMetadataForItem( | |
| 252 int64_t meta_handle, | |
| 253 const sync_pb::AttachmentMetadata metadata) { | |
| 254 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | |
| 255 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 256 if (!entry.good()) { | |
| 257 return false; | |
| 258 } | |
| 259 entry.PutAttachmentMetadata(metadata); | |
| 260 entry.PutIsUnsynced(true); | |
| 261 return true; | |
| 262 } | |
| 263 | |
| 264 const sync_pb::AttachmentMetadata& | |
| 265 TestEntryFactory::GetServerAttachmentMetadataForItem( | |
| 266 int64_t meta_handle) const { | |
| 267 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 268 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 269 DCHECK(entry.good()); | |
| 270 return entry.GetServerAttachmentMetadata(); | |
| 271 } | |
| 272 | |
| 273 const sync_pb::AttachmentMetadata& | |
| 274 TestEntryFactory::GetLocalAttachmentMetadataForItem(int64_t meta_handle) const { | |
| 275 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 276 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 277 DCHECK(entry.good()); | |
| 278 return entry.GetAttachmentMetadata(); | |
| 279 } | |
| 280 | |
| 281 bool TestEntryFactory::GetIsUnsyncedForItem(int64_t meta_handle) const { | |
| 282 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 283 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 284 if (!entry.good()) { | |
| 285 NOTREACHED(); | |
| 286 return false; | |
| 287 } | |
| 288 return entry.GetIsUnsynced(); | |
| 289 } | |
| 290 | |
| 291 bool TestEntryFactory::GetIsUnappliedForItem(int64_t meta_handle) const { | |
| 292 syncable::ReadTransaction trans(FROM_HERE, directory_); | |
| 293 syncable::Entry entry(&trans, syncable::GET_BY_HANDLE, meta_handle); | |
| 294 if (!entry.good()) { | |
| 295 NOTREACHED(); | |
| 296 return false; | |
| 297 } | |
| 298 return entry.GetIsUnappliedUpdate(); | |
| 299 } | |
| 300 | |
| 301 int64_t TestEntryFactory::GetNextRevision() { | |
| 302 return next_revision_++; | |
| 303 } | |
| 304 | |
| 305 } // namespace syncer | |
| OLD | NEW |