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