| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/internal_api/public/test/test_entry_factory.h" | 5 #include "sync/internal_api/public/test/test_entry_factory.h" |
| 6 | 6 |
| 7 #include "sync/syncable/directory.h" | 7 #include "sync/syncable/directory.h" |
| 8 #include "sync/syncable/entry.h" | 8 #include "sync/syncable/entry.h" |
| 9 #include "sync/syncable/mutable_entry.h" | 9 #include "sync/syncable/mutable_entry.h" |
| 10 #include "sync/syncable/read_transaction.h" | 10 #include "sync/syncable/read_transaction.h" |
| 11 #include "sync/syncable/syncable_id.h" | 11 #include "sync/syncable/syncable_id.h" |
| 12 #include "sync/syncable/syncable_util.h" |
| 12 #include "sync/syncable/write_transaction.h" | 13 #include "sync/syncable/write_transaction.h" |
| 13 #include "sync/test/engine/test_id_factory.h" | 14 #include "sync/test/engine/test_id_factory.h" |
| 14 | 15 |
| 15 using std::string; | 16 using std::string; |
| 16 | 17 |
| 17 namespace syncer { | 18 namespace syncer { |
| 18 | 19 |
| 19 using syncable::Id; | 20 using syncable::Id; |
| 20 using syncable::MutableEntry; | 21 using syncable::MutableEntry; |
| 21 using syncable::UNITTEST; | 22 using syncable::UNITTEST; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 39 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 39 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 40 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 40 | 41 |
| 41 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); | 42 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 42 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); | 43 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); |
| 43 entry.Put(syncable::SERVER_IS_DIR, true); | 44 entry.Put(syncable::SERVER_IS_DIR, true); |
| 44 entry.Put(syncable::SERVER_SPECIFICS, specifics); | 45 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 45 return entry.Get(syncable::META_HANDLE); | 46 return entry.Get(syncable::META_HANDLE); |
| 46 } | 47 } |
| 47 | 48 |
| 49 int64 TestEntryFactory::CreateUnappliedNewBookmarkItemWithParent( |
| 50 const string& item_id, |
| 51 const sync_pb::EntitySpecifics& specifics, |
| 52 const string& parent_id) { |
| 53 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 54 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 55 Id::CreateFromServerId(item_id)); |
| 56 DCHECK(entry.good()); |
| 57 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 58 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 59 |
| 60 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 61 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); |
| 62 entry.Put(syncable::SERVER_IS_DIR, true); |
| 63 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 64 |
| 65 // Not entirely accurate, but at least it's valid and unique. |
| 66 // (Normally the ID should be a client local ID because this is calculated at |
| 67 // creation time.) |
| 68 UniquePosition pos = UniquePosition::InitialPosition( |
| 69 syncable::GenerateSyncableBookmarkHash( |
| 70 directory_->cache_guid(), entry.Get(syncable::ID).value())); |
| 71 entry.Put(syncable::SERVER_UNIQUE_POSITION, pos); |
| 72 return entry.Get(syncable::META_HANDLE); |
| 73 } |
| 74 |
| 48 int64 TestEntryFactory::CreateUnappliedNewItem( | 75 int64 TestEntryFactory::CreateUnappliedNewItem( |
| 49 const string& item_id, | 76 const string& item_id, |
| 50 const sync_pb::EntitySpecifics& specifics, | 77 const sync_pb::EntitySpecifics& specifics, |
| 51 bool is_unique) { | 78 bool is_unique) { |
| 52 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 79 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 53 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | 80 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 54 Id::CreateFromServerId(item_id)); | 81 Id::CreateFromServerId(item_id)); |
| 55 DCHECK(entry.good()); | 82 DCHECK(entry.good()); |
| 56 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 83 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 57 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 84 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 58 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); | 85 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 59 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); | 86 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); |
| 60 entry.Put(syncable::SERVER_IS_DIR, is_unique); | 87 entry.Put(syncable::SERVER_IS_DIR, is_unique); |
| 61 entry.Put(syncable::SERVER_SPECIFICS, specifics); | 88 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 62 if (is_unique) { // For top-level nodes. | 89 if (is_unique) { // For top-level nodes. |
| 63 entry.Put(syncable::UNIQUE_SERVER_TAG, | 90 entry.Put(syncable::UNIQUE_SERVER_TAG, |
| 64 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics))); | 91 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics))); |
| 65 } | 92 } |
| 66 return entry.Get(syncable::META_HANDLE); | 93 return entry.Get(syncable::META_HANDLE); |
| 67 } | 94 } |
| 68 | 95 |
| 69 void TestEntryFactory::CreateUnsyncedItem( | 96 void TestEntryFactory::CreateUnsyncedItem( |
| 70 const Id& item_id, | 97 const Id& item_id, |
| 71 const Id& parent_id, | 98 const Id& parent_id, |
| 72 const string& name, | 99 const string& name, |
| 73 bool is_folder, | |
| 74 ModelType model_type, | 100 ModelType model_type, |
| 75 int64* metahandle_out) { | 101 int64* metahandle_out) { |
| 76 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 102 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 77 Id predecessor_id; | 103 MutableEntry entry(&trans, syncable::CREATE_UNIQUE, |
| 78 DCHECK( | 104 model_type, parent_id, name); |
| 79 directory_->GetLastChildIdForTest(&trans, parent_id, &predecessor_id)); | |
| 80 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); | |
| 81 DCHECK(entry.good()); | 105 DCHECK(entry.good()); |
| 82 entry.Put(syncable::ID, item_id); | 106 entry.Put(syncable::ID, item_id); |
| 83 entry.Put(syncable::BASE_VERSION, | 107 entry.Put(syncable::BASE_VERSION, |
| 84 item_id.ServerKnows() ? GetNextRevision() : 0); | 108 item_id.ServerKnows() ? GetNextRevision() : 0); |
| 85 entry.Put(syncable::IS_UNSYNCED, true); | 109 entry.Put(syncable::IS_UNSYNCED, true); |
| 86 entry.Put(syncable::IS_DIR, is_folder); | 110 entry.Put(syncable::IS_DIR, false); |
| 87 entry.Put(syncable::IS_DEL, false); | 111 entry.Put(syncable::IS_DEL, false); |
| 88 entry.Put(syncable::PARENT_ID, parent_id); | 112 entry.Put(syncable::PARENT_ID, parent_id); |
| 89 CHECK(entry.PutPredecessor(predecessor_id)); | |
| 90 sync_pb::EntitySpecifics default_specifics; | 113 sync_pb::EntitySpecifics default_specifics; |
| 91 AddDefaultFieldValue(model_type, &default_specifics); | 114 AddDefaultFieldValue(model_type, &default_specifics); |
| 92 entry.Put(syncable::SPECIFICS, default_specifics); | 115 entry.Put(syncable::SPECIFICS, default_specifics); |
| 93 if (item_id.ServerKnows()) { | 116 if (item_id.ServerKnows()) { |
| 94 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); | 117 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 95 entry.Put(syncable::SERVER_IS_DIR, is_folder); | 118 entry.Put(syncable::SERVER_IS_DIR, false); |
| 96 entry.Put(syncable::SERVER_PARENT_ID, parent_id); | 119 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 97 entry.Put(syncable::SERVER_IS_DEL, false); | 120 entry.Put(syncable::SERVER_IS_DEL, false); |
| 98 } | 121 } |
| 99 if (metahandle_out) | 122 if (metahandle_out) |
| 100 *metahandle_out = entry.Get(syncable::META_HANDLE); | 123 *metahandle_out = entry.Get(syncable::META_HANDLE); |
| 101 } | 124 } |
| 102 | 125 |
| 103 int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem( | 126 void TestEntryFactory::CreateUnsyncedBookmarkItem( |
| 127 const Id& item_id, |
| 128 const Id& parent_id, |
| 104 const string& name, | 129 const string& name, |
| 105 ModelType model_type) { | 130 bool is_folder, |
| 131 int64* metahandle_out) { |
| 132 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 133 MutableEntry entry(&trans, syncable::CREATE_BOOKMARK, parent_id, name); |
| 134 DCHECK(entry.good()); |
| 135 entry.Put(syncable::ID, item_id); |
| 136 entry.Put(syncable::BASE_VERSION, |
| 137 item_id.ServerKnows() ? GetNextRevision() : 0); |
| 138 entry.Put(syncable::IS_UNSYNCED, true); |
| 139 entry.Put(syncable::IS_DIR, is_folder); |
| 140 entry.Put(syncable::IS_DEL, false); |
| 141 entry.Put(syncable::PARENT_ID, parent_id); |
| 142 sync_pb::EntitySpecifics default_specifics; |
| 143 AddDefaultFieldValue(BOOKMARKS, &default_specifics); |
| 144 entry.Put(syncable::SPECIFICS, default_specifics); |
| 145 if (item_id.ServerKnows()) { |
| 146 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 147 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 148 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 149 entry.Put(syncable::SERVER_IS_DEL, false); |
| 150 } |
| 151 if (metahandle_out) |
| 152 *metahandle_out = entry.Get(syncable::META_HANDLE); |
| 153 } |
| 154 |
| 155 int64 TestEntryFactory::CreateUnappliedAndUnsyncedBookmarkItem( |
| 156 const string& name) { |
| 106 int64 metahandle = 0; | 157 int64 metahandle = 0; |
| 107 CreateUnsyncedItem( | 158 CreateUnsyncedBookmarkItem( |
| 108 TestIdFactory::MakeServer(name), TestIdFactory::root(), | 159 TestIdFactory::MakeServer(name), TestIdFactory::root(), |
| 109 name, false, model_type, &metahandle); | 160 name, false, &metahandle); |
| 110 | 161 |
| 111 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 162 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 112 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); | 163 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); |
| 113 if (!entry.good()) { | 164 if (!entry.good()) { |
| 114 NOTREACHED(); | 165 NOTREACHED(); |
| 115 return syncable::kInvalidMetaHandle; | 166 return syncable::kInvalidMetaHandle; |
| 116 } | 167 } |
| 117 | 168 |
| 118 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 169 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 119 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 170 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 120 | 171 |
| 121 return metahandle; | 172 return metahandle; |
| 122 } | 173 } |
| 123 | 174 |
| 124 int64 TestEntryFactory::CreateSyncedItem( | 175 int64 TestEntryFactory::CreateSyncedItem( |
| 125 const std::string& name, ModelType model_type, bool is_folder) { | 176 const std::string& name, ModelType model_type, bool is_folder) { |
| 177 DCHECK_NE(BOOKMARKS, model_type); |
| 126 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 178 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 127 | 179 |
| 128 syncable::Id parent_id(TestIdFactory::root()); | 180 syncable::Id parent_id(TestIdFactory::root()); |
| 129 syncable::Id item_id(TestIdFactory::MakeServer(name)); | 181 syncable::Id item_id(TestIdFactory::MakeServer(name)); |
| 130 int64 version = GetNextRevision(); | 182 int64 version = GetNextRevision(); |
| 131 | 183 |
| 132 sync_pb::EntitySpecifics default_specifics; | 184 MutableEntry entry( |
| 133 AddDefaultFieldValue(model_type, &default_specifics); | 185 &trans, syncable::CREATE_UNIQUE, model_type, parent_id, name); |
| 134 | |
| 135 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); | |
| 136 if (!entry.good()) { | 186 if (!entry.good()) { |
| 137 NOTREACHED(); | 187 NOTREACHED(); |
| 138 return syncable::kInvalidMetaHandle; | 188 return syncable::kInvalidMetaHandle; |
| 139 } | 189 } |
| 140 | 190 |
| 141 entry.Put(syncable::ID, item_id); | 191 entry.Put(syncable::ID, item_id); |
| 142 entry.Put(syncable::BASE_VERSION, version); | 192 entry.Put(syncable::BASE_VERSION, version); |
| 143 entry.Put(syncable::IS_UNSYNCED, false); | 193 entry.Put(syncable::IS_UNSYNCED, false); |
| 144 entry.Put(syncable::NON_UNIQUE_NAME, name); | 194 entry.Put(syncable::NON_UNIQUE_NAME, name); |
| 145 entry.Put(syncable::IS_DIR, is_folder); | 195 entry.Put(syncable::IS_DIR, is_folder); |
| 146 entry.Put(syncable::IS_DEL, false); | 196 entry.Put(syncable::IS_DEL, false); |
| 147 entry.Put(syncable::PARENT_ID, parent_id); | 197 entry.Put(syncable::PARENT_ID, parent_id); |
| 148 | 198 |
| 149 if (!entry.PutPredecessor(TestIdFactory::root())) { | 199 if (!entry.PutPredecessor(TestIdFactory::root())) { |
| 150 NOTREACHED(); | 200 NOTREACHED(); |
| 151 return syncable::kInvalidMetaHandle; | 201 return syncable::kInvalidMetaHandle; |
| 152 } | 202 } |
| 153 entry.Put(syncable::SPECIFICS, default_specifics); | |
| 154 | 203 |
| 155 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 204 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 156 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 205 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 157 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X"); | 206 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, name); |
| 158 entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y")); | 207 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 159 entry.Put(syncable::SERVER_IS_DIR, is_folder); | 208 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 160 entry.Put(syncable::SERVER_IS_DEL, false); | 209 entry.Put(syncable::SERVER_IS_DEL, false); |
| 161 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); | 210 entry.Put(syncable::SERVER_SPECIFICS, entry.Get(syncable::SPECIFICS)); |
| 162 entry.Put(syncable::SERVER_PARENT_ID, parent_id); | |
| 163 | 211 |
| 164 return entry.Get(syncable::META_HANDLE); | 212 return entry.Get(syncable::META_HANDLE); |
| 165 } | 213 } |
| 214 |
| 215 int64 TestEntryFactory::CreateSyncedBookmarkItem( |
| 216 const std::string& name, bool is_folder) { |
| 217 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
| 218 |
| 219 syncable::Id parent_id(TestIdFactory::root()); |
| 220 syncable::Id item_id(TestIdFactory::MakeServer(name)); |
| 221 int64 version = GetNextRevision(); |
| 222 |
| 223 MutableEntry entry(&trans, syncable::CREATE_BOOKMARK, parent_id, name); |
| 224 if (!entry.good()) { |
| 225 NOTREACHED(); |
| 226 return syncable::kInvalidMetaHandle; |
| 227 } |
| 228 |
| 229 entry.Put(syncable::ID, item_id); |
| 230 entry.Put(syncable::BASE_VERSION, version); |
| 231 entry.Put(syncable::IS_UNSYNCED, false); |
| 232 entry.Put(syncable::NON_UNIQUE_NAME, name); |
| 233 entry.Put(syncable::IS_DIR, is_folder); |
| 234 entry.Put(syncable::IS_DEL, false); |
| 235 entry.Put(syncable::PARENT_ID, parent_id); |
| 236 |
| 237 if (!entry.PutPredecessor(TestIdFactory::root())) { |
| 238 NOTREACHED(); |
| 239 return syncable::kInvalidMetaHandle; |
| 240 } |
| 241 |
| 242 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 243 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 244 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, name); |
| 245 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 246 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 247 entry.Put(syncable::SERVER_IS_DEL, false); |
| 248 entry.Put(syncable::SERVER_SPECIFICS, entry.Get(syncable::SPECIFICS)); |
| 249 |
| 250 return entry.Get(syncable::META_HANDLE); |
| 251 } |
| 166 | 252 |
| 167 int64 TestEntryFactory::CreateUnappliedRootNode( | 253 int64 TestEntryFactory::CreateUnappliedRootNode( |
| 168 ModelType model_type) { | 254 ModelType model_type) { |
| 169 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); | 255 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); |
| 170 sync_pb::EntitySpecifics specifics; | 256 sync_pb::EntitySpecifics specifics; |
| 171 AddDefaultFieldValue(model_type, &specifics); | 257 AddDefaultFieldValue(model_type, &specifics); |
| 172 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); | 258 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); |
| 173 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | 259 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 174 node_id); | 260 node_id); |
| 175 DCHECK(entry.good()); | 261 DCHECK(entry.good()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 return false; | 332 return false; |
| 247 } | 333 } |
| 248 return entry.Get(syncable::IS_UNAPPLIED_UPDATE); | 334 return entry.Get(syncable::IS_UNAPPLIED_UPDATE); |
| 249 } | 335 } |
| 250 | 336 |
| 251 int64 TestEntryFactory::GetNextRevision() { | 337 int64 TestEntryFactory::GetNextRevision() { |
| 252 return next_revision_++; | 338 return next_revision_++; |
| 253 } | 339 } |
| 254 | 340 |
| 255 } // namespace syncer | 341 } // namespace syncer |
| OLD | NEW |