OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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/syncable_id.h" | 10 #include "sync/syncable/syncable_id.h" |
11 #include "sync/syncable/syncable_read_transaction.h" | 11 #include "sync/syncable/syncable_read_transaction.h" |
12 #include "sync/syncable/syncable_util.h" | |
12 #include "sync/syncable/syncable_write_transaction.h" | 13 #include "sync/syncable/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 return entry.Get(syncable::META_HANDLE); | |
66 } | |
67 | |
48 int64 TestEntryFactory::CreateUnappliedNewItem( | 68 int64 TestEntryFactory::CreateUnappliedNewItem( |
49 const string& item_id, | 69 const string& item_id, |
50 const sync_pb::EntitySpecifics& specifics, | 70 const sync_pb::EntitySpecifics& specifics, |
51 bool is_unique) { | 71 bool is_unique) { |
52 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 72 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
53 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | 73 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
54 Id::CreateFromServerId(item_id)); | 74 Id::CreateFromServerId(item_id)); |
55 DCHECK(entry.good()); | 75 DCHECK(entry.good()); |
56 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 76 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
57 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 77 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
58 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); | 78 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id); |
59 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); | 79 entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId()); |
60 entry.Put(syncable::SERVER_IS_DIR, is_unique); | 80 entry.Put(syncable::SERVER_IS_DIR, is_unique); |
61 entry.Put(syncable::SERVER_SPECIFICS, specifics); | 81 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
62 if (is_unique) { // For top-level nodes. | 82 if (is_unique) { // For top-level nodes. |
63 entry.Put(syncable::UNIQUE_SERVER_TAG, | 83 entry.Put(syncable::UNIQUE_SERVER_TAG, |
64 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics))); | 84 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics))); |
65 } | 85 } |
66 return entry.Get(syncable::META_HANDLE); | 86 return entry.Get(syncable::META_HANDLE); |
67 } | 87 } |
68 | 88 |
69 void TestEntryFactory::CreateUnsyncedItem( | 89 void TestEntryFactory::CreateUnsyncedItem( |
70 const Id& item_id, | 90 const Id& item_id, |
71 const Id& parent_id, | 91 const Id& parent_id, |
72 const string& name, | 92 const string& name, |
73 bool is_folder, | 93 bool is_folder, |
74 ModelType model_type, | 94 ModelType model_type, |
75 int64* metahandle_out) { | 95 int64* metahandle_out) { |
96 if (is_folder) { | |
97 DCHECK_EQ(model_type, BOOKMARKS); | |
98 } | |
99 | |
76 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 100 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
101 | |
77 Id predecessor_id; | 102 Id predecessor_id; |
78 DCHECK( | 103 if (model_type == BOOKMARKS) { |
79 directory_->GetLastChildIdForTest(&trans, parent_id, &predecessor_id)); | 104 DCHECK(directory_->GetLastChildIdForTest( |
80 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); | 105 &trans, parent_id, &predecessor_id)); |
106 } | |
107 | |
108 MutableEntry entry(&trans, syncable::CREATE, model_type, parent_id, name); | |
81 DCHECK(entry.good()); | 109 DCHECK(entry.good()); |
82 entry.Put(syncable::ID, item_id); | 110 entry.Put(syncable::ID, item_id); |
83 entry.Put(syncable::BASE_VERSION, | 111 entry.Put(syncable::BASE_VERSION, |
84 item_id.ServerKnows() ? GetNextRevision() : 0); | 112 item_id.ServerKnows() ? GetNextRevision() : 0); |
85 entry.Put(syncable::IS_UNSYNCED, true); | 113 entry.Put(syncable::IS_UNSYNCED, true); |
86 entry.Put(syncable::IS_DIR, is_folder); | 114 entry.Put(syncable::IS_DIR, is_folder); |
87 entry.Put(syncable::IS_DEL, false); | 115 entry.Put(syncable::IS_DEL, false); |
88 entry.Put(syncable::PARENT_ID, parent_id); | 116 entry.Put(syncable::PARENT_ID, parent_id); |
89 CHECK(entry.PutPredecessor(predecessor_id)); | |
90 sync_pb::EntitySpecifics default_specifics; | 117 sync_pb::EntitySpecifics default_specifics; |
91 AddDefaultFieldValue(model_type, &default_specifics); | 118 AddDefaultFieldValue(model_type, &default_specifics); |
92 entry.Put(syncable::SPECIFICS, default_specifics); | 119 entry.Put(syncable::SPECIFICS, default_specifics); |
120 | |
121 // Bookmarks get inserted at the end of the list. | |
122 if (model_type == BOOKMARKS) { | |
123 DCHECK(entry.PutPredecessor(predecessor_id)); | |
rlarocque
2013/01/11 18:41:14
I bet this is the cause of the buildbot failures.
| |
124 } | |
125 | |
93 if (item_id.ServerKnows()) { | 126 if (item_id.ServerKnows()) { |
94 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); | 127 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); |
95 entry.Put(syncable::SERVER_IS_DIR, is_folder); | 128 entry.Put(syncable::SERVER_IS_DIR, false); |
96 entry.Put(syncable::SERVER_PARENT_ID, parent_id); | 129 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
97 entry.Put(syncable::SERVER_IS_DEL, false); | 130 entry.Put(syncable::SERVER_IS_DEL, false); |
98 } | 131 } |
99 if (metahandle_out) | 132 if (metahandle_out) |
100 *metahandle_out = entry.Get(syncable::META_HANDLE); | 133 *metahandle_out = entry.Get(syncable::META_HANDLE); |
101 } | 134 } |
102 | 135 |
103 int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem( | 136 int64 TestEntryFactory::CreateUnappliedAndUnsyncedBookmarkItem( |
104 const string& name, | 137 const string& name) { |
105 ModelType model_type) { | |
106 int64 metahandle = 0; | 138 int64 metahandle = 0; |
107 CreateUnsyncedItem( | 139 CreateUnsyncedItem( |
108 TestIdFactory::MakeServer(name), TestIdFactory::root(), | 140 TestIdFactory::MakeServer(name), TestIdFactory::root(), |
109 name, false, model_type, &metahandle); | 141 name, false, BOOKMARKS, &metahandle); |
110 | 142 |
111 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 143 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
112 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); | 144 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); |
113 if (!entry.good()) { | 145 if (!entry.good()) { |
114 NOTREACHED(); | 146 NOTREACHED(); |
115 return syncable::kInvalidMetaHandle; | 147 return syncable::kInvalidMetaHandle; |
116 } | 148 } |
117 | 149 |
118 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 150 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); |
119 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 151 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
120 | 152 |
121 return metahandle; | 153 return metahandle; |
122 } | 154 } |
123 | 155 |
124 int64 TestEntryFactory::CreateSyncedItem( | 156 int64 TestEntryFactory::CreateSyncedItem( |
125 const std::string& name, ModelType model_type, bool is_folder) { | 157 const std::string& name, ModelType model_type, bool is_folder) { |
126 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); | 158 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); |
127 | 159 |
128 syncable::Id parent_id(TestIdFactory::root()); | 160 syncable::Id parent_id(TestIdFactory::root()); |
129 syncable::Id item_id(TestIdFactory::MakeServer(name)); | 161 syncable::Id item_id(TestIdFactory::MakeServer(name)); |
130 int64 version = GetNextRevision(); | 162 int64 version = GetNextRevision(); |
131 | 163 |
132 sync_pb::EntitySpecifics default_specifics; | 164 MutableEntry entry(&trans, syncable::CREATE, model_type, parent_id, name); |
133 AddDefaultFieldValue(model_type, &default_specifics); | |
134 | |
135 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); | |
136 if (!entry.good()) { | 165 if (!entry.good()) { |
137 NOTREACHED(); | 166 NOTREACHED(); |
138 return syncable::kInvalidMetaHandle; | 167 return syncable::kInvalidMetaHandle; |
139 } | 168 } |
140 | 169 |
141 entry.Put(syncable::ID, item_id); | 170 entry.Put(syncable::ID, item_id); |
142 entry.Put(syncable::BASE_VERSION, version); | 171 entry.Put(syncable::BASE_VERSION, version); |
143 entry.Put(syncable::IS_UNSYNCED, false); | 172 entry.Put(syncable::IS_UNSYNCED, false); |
144 entry.Put(syncable::NON_UNIQUE_NAME, name); | 173 entry.Put(syncable::NON_UNIQUE_NAME, name); |
145 entry.Put(syncable::IS_DIR, is_folder); | 174 entry.Put(syncable::IS_DIR, is_folder); |
146 entry.Put(syncable::IS_DEL, false); | 175 entry.Put(syncable::IS_DEL, false); |
147 entry.Put(syncable::PARENT_ID, parent_id); | 176 entry.Put(syncable::PARENT_ID, parent_id); |
148 | 177 |
178 // TODO(sync): Place bookmarks at the end of the list? | |
149 if (!entry.PutPredecessor(TestIdFactory::root())) { | 179 if (!entry.PutPredecessor(TestIdFactory::root())) { |
150 NOTREACHED(); | 180 NOTREACHED(); |
151 return syncable::kInvalidMetaHandle; | 181 return syncable::kInvalidMetaHandle; |
152 } | 182 } |
153 entry.Put(syncable::SPECIFICS, default_specifics); | |
154 | 183 |
155 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); | 184 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); |
156 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); | 185 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
157 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X"); | 186 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, name); |
158 entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y")); | 187 entry.Put(syncable::SERVER_PARENT_ID, parent_id); |
159 entry.Put(syncable::SERVER_IS_DIR, is_folder); | 188 entry.Put(syncable::SERVER_IS_DIR, is_folder); |
160 entry.Put(syncable::SERVER_IS_DEL, false); | 189 entry.Put(syncable::SERVER_IS_DEL, false); |
161 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); | 190 entry.Put(syncable::SERVER_SPECIFICS, entry.Get(syncable::SPECIFICS)); |
162 entry.Put(syncable::SERVER_PARENT_ID, parent_id); | |
163 | 191 |
164 return entry.Get(syncable::META_HANDLE); | 192 return entry.Get(syncable::META_HANDLE); |
165 } | 193 } |
166 | 194 |
167 int64 TestEntryFactory::CreateUnappliedRootNode( | 195 int64 TestEntryFactory::CreateUnappliedRootNode( |
168 ModelType model_type) { | 196 ModelType model_type) { |
169 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); | 197 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); |
170 sync_pb::EntitySpecifics specifics; | 198 sync_pb::EntitySpecifics specifics; |
171 AddDefaultFieldValue(model_type, &specifics); | 199 AddDefaultFieldValue(model_type, &specifics); |
172 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); | 200 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 return false; | 274 return false; |
247 } | 275 } |
248 return entry.Get(syncable::IS_UNAPPLIED_UPDATE); | 276 return entry.Get(syncable::IS_UNAPPLIED_UPDATE); |
249 } | 277 } |
250 | 278 |
251 int64 TestEntryFactory::GetNextRevision() { | 279 int64 TestEntryFactory::GetNextRevision() { |
252 return next_revision_++; | 280 return next_revision_++; |
253 } | 281 } |
254 | 282 |
255 } // namespace syncer | 283 } // namespace syncer |
OLD | NEW |