| 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 using syncable::Id; |
| 15 using syncable::MutableEntry; |
| 16 using syncable::UNITTEST; |
| 17 using syncable::WriteTransaction; |
| 18 |
| 19 namespace csync { |
| 20 |
| 21 TestEntryFactory::TestEntryFactory(syncable::Directory *dir) |
| 22 : directory_(dir), next_revision_(1) { |
| 23 } |
| 24 |
| 25 TestEntryFactory::~TestEntryFactory() { } |
| 26 |
| 27 void TestEntryFactory::CreateUnappliedNewItemWithParent( |
| 28 const string& item_id, |
| 29 const sync_pb::EntitySpecifics& specifics, |
| 30 const string& parent_id) { |
| 31 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 32 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 33 Id::CreateFromServerId(item_id)); |
| 34 DCHECK(entry.good()); |
| 35 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 36 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 37 |
| 38 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 39 entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id)); |
| 40 entry.Put(syncable::SERVER_IS_DIR, true); |
| 41 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 42 } |
| 43 |
| 44 void TestEntryFactory::CreateUnappliedNewItem( |
| 45 const string& item_id, |
| 46 const sync_pb::EntitySpecifics& specifics, |
| 47 bool is_unique) { |
| 48 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 49 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 50 Id::CreateFromServerId(item_id)); |
| 51 DCHECK(entry.good()); |
| 52 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 53 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 54 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
| 55 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); |
| 56 entry.Put(syncable::SERVER_IS_DIR, false); |
| 57 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 58 if (is_unique) // For top-level nodes. |
| 59 entry.Put(syncable::UNIQUE_SERVER_TAG, item_id); |
| 60 } |
| 61 |
| 62 void TestEntryFactory::CreateUnsyncedItem( |
| 63 const Id& item_id, |
| 64 const Id& parent_id, |
| 65 const string& name, |
| 66 bool is_folder, |
| 67 syncable::ModelType model_type, |
| 68 int64* metahandle_out) { |
| 69 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 70 Id predecessor_id; |
| 71 DCHECK( |
| 72 directory()->GetLastChildIdForTest(&trans, parent_id, &predecessor_id)); |
| 73 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); |
| 74 DCHECK(entry.good()); |
| 75 entry.Put(syncable::ID, item_id); |
| 76 entry.Put(syncable::BASE_VERSION, |
| 77 item_id.ServerKnows() ? GetNextRevision() : 0); |
| 78 entry.Put(syncable::IS_UNSYNCED, true); |
| 79 entry.Put(syncable::IS_DIR, is_folder); |
| 80 entry.Put(syncable::IS_DEL, false); |
| 81 entry.Put(syncable::PARENT_ID, parent_id); |
| 82 CHECK(entry.PutPredecessor(predecessor_id)); |
| 83 sync_pb::EntitySpecifics default_specifics; |
| 84 syncable::AddDefaultFieldValue(model_type, &default_specifics); |
| 85 entry.Put(syncable::SPECIFICS, default_specifics); |
| 86 if (item_id.ServerKnows()) { |
| 87 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 88 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 89 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 90 entry.Put(syncable::SERVER_IS_DEL, false); |
| 91 } |
| 92 if (metahandle_out) |
| 93 *metahandle_out = entry.Get(syncable::META_HANDLE); |
| 94 } |
| 95 |
| 96 int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem( |
| 97 const string& name, |
| 98 syncable::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, syncable::ModelType |
| 119 model_type, bool is_folder) { |
| 120 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); |
| 121 |
| 122 syncable::Id parent_id(TestIdFactory::root()); |
| 123 syncable::Id item_id(TestIdFactory::MakeServer(name)); |
| 124 int64 version = GetNextRevision(); |
| 125 |
| 126 sync_pb::EntitySpecifics default_specifics; |
| 127 syncable::AddDefaultFieldValue(model_type, &default_specifics); |
| 128 |
| 129 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); |
| 130 if (!entry.good()) { |
| 131 NOTREACHED(); |
| 132 return syncable::kInvalidMetaHandle; |
| 133 } |
| 134 |
| 135 entry.Put(syncable::ID, item_id); |
| 136 entry.Put(syncable::BASE_VERSION, version); |
| 137 entry.Put(syncable::IS_UNSYNCED, false); |
| 138 entry.Put(syncable::NON_UNIQUE_NAME, name); |
| 139 entry.Put(syncable::IS_DIR, is_folder); |
| 140 entry.Put(syncable::IS_DEL, false); |
| 141 entry.Put(syncable::PARENT_ID, parent_id); |
| 142 |
| 143 if (!entry.PutPredecessor(TestIdFactory::root())) { |
| 144 NOTREACHED(); |
| 145 return syncable::kInvalidMetaHandle; |
| 146 } |
| 147 entry.Put(syncable::SPECIFICS, default_specifics); |
| 148 |
| 149 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
| 150 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
| 151 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X"); |
| 152 entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y")); |
| 153 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
| 154 entry.Put(syncable::SERVER_IS_DEL, false); |
| 155 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
| 156 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
| 157 |
| 158 return entry.Get(syncable::META_HANDLE); |
| 159 } |
| 160 |
| 161 syncable::Directory* TestEntryFactory::directory() { |
| 162 return directory_; |
| 163 } |
| 164 |
| 165 int64 TestEntryFactory::GetNextRevision() { |
| 166 return next_revision_++; |
| 167 } |
| 168 |
| 169 } // namespace csync |
| OLD | NEW |