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.
5 #include "sync/internal_api/public/test/test_entry_factory.h"
7 #include "sync/syncable/directory.h"
8 #include "sync/syncable/entry.h"
9 #include "sync/syncable/mutable_entry.h"
10 #include "sync/syncable/syncable_id.h"
11 #include "sync/syncable/syncable_read_transaction.h"
12 #include "sync/syncable/syncable_util.h"
13 #include "sync/syncable/syncable_write_transaction.h"
14 #include "sync/test/engine/test_id_factory.h"
21 using syncable::MutableEntry
;
22 using syncable::UNITTEST
;
23 using syncable::WriteTransaction
;
25 TestEntryFactory::TestEntryFactory(syncable::Directory
*dir
)
26 : directory_(dir
), next_revision_(1) {
29 TestEntryFactory::~TestEntryFactory() { }
31 int64
TestEntryFactory::CreateUnappliedNewItemWithParent(
32 const string
& item_id
,
33 const sync_pb::EntitySpecifics
& specifics
,
34 const string
& parent_id
) {
35 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
36 MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
37 Id::CreateFromServerId(item_id
));
39 entry
.PutServerVersion(GetNextRevision());
40 entry
.PutIsUnappliedUpdate(true);
42 entry
.PutServerNonUniqueName(item_id
);
43 entry
.PutServerParentId(Id::CreateFromServerId(parent_id
));
44 entry
.PutServerIsDir(true);
45 entry
.PutServerSpecifics(specifics
);
46 return entry
.GetMetahandle();
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
));
57 entry
.PutServerVersion(GetNextRevision());
58 entry
.PutIsUnappliedUpdate(true);
60 entry
.PutServerNonUniqueName(item_id
);
61 entry
.PutServerParentId(Id::CreateFromServerId(parent_id
));
62 entry
.PutServerIsDir(true);
63 entry
.PutServerSpecifics(specifics
);
65 return entry
.GetMetahandle();
68 int64
TestEntryFactory::CreateUnappliedNewItem(
69 const string
& item_id
,
70 const sync_pb::EntitySpecifics
& specifics
,
72 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
73 MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
74 Id::CreateFromServerId(item_id
));
76 entry
.PutServerVersion(GetNextRevision());
77 entry
.PutIsUnappliedUpdate(true);
78 entry
.PutServerNonUniqueName(item_id
);
79 entry
.PutServerParentId(syncable::GetNullId());
80 entry
.PutServerIsDir(is_unique
);
81 entry
.PutServerSpecifics(specifics
);
82 if (is_unique
) { // For top-level nodes.
83 entry
.PutUniqueServerTag(
84 ModelTypeToRootTag(GetModelTypeFromSpecifics(specifics
)));
86 return entry
.GetMetahandle();
89 void TestEntryFactory::CreateUnsyncedItem(
95 int64
* metahandle_out
) {
97 DCHECK_EQ(model_type
, BOOKMARKS
);
100 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
102 MutableEntry
entry(&trans
, syncable::CREATE
, model_type
, parent_id
, name
);
103 DCHECK(entry
.good());
104 entry
.PutId(item_id
);
105 entry
.PutBaseVersion(
106 item_id
.ServerKnows() ? GetNextRevision() : 0);
107 entry
.PutIsUnsynced(true);
108 entry
.PutIsDir(is_folder
);
109 entry
.PutIsDel(false);
110 entry
.PutParentId(parent_id
);
111 sync_pb::EntitySpecifics default_specifics
;
112 AddDefaultFieldValue(model_type
, &default_specifics
);
113 entry
.PutSpecifics(default_specifics
);
115 if (item_id
.ServerKnows()) {
116 entry
.PutServerSpecifics(default_specifics
);
117 entry
.PutServerIsDir(false);
118 entry
.PutServerParentId(parent_id
);
119 entry
.PutServerIsDel(false);
122 *metahandle_out
= entry
.GetMetahandle();
125 int64
TestEntryFactory::CreateUnappliedAndUnsyncedBookmarkItem(
126 const string
& name
) {
127 int64 metahandle
= 0;
129 TestIdFactory::MakeServer(name
), TestIdFactory::root(),
130 name
, false, BOOKMARKS
, &metahandle
);
132 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
133 MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, metahandle
);
136 return syncable::kInvalidMetaHandle
;
139 entry
.PutIsUnappliedUpdate(true);
140 entry
.PutServerVersion(GetNextRevision());
145 int64
TestEntryFactory::CreateSyncedItem(
146 const std::string
& name
, ModelType model_type
, bool is_folder
) {
147 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
149 syncable::Id
parent_id(TestIdFactory::root());
150 syncable::Id
item_id(TestIdFactory::MakeServer(name
));
151 int64 version
= GetNextRevision();
153 MutableEntry
entry(&trans
, syncable::CREATE
, model_type
, parent_id
, name
);
156 return syncable::kInvalidMetaHandle
;
159 entry
.PutId(item_id
);
160 entry
.PutBaseVersion(version
);
161 entry
.PutIsUnsynced(false);
162 entry
.PutNonUniqueName(name
);
163 entry
.PutIsDir(is_folder
);
164 entry
.PutIsDel(false);
165 entry
.PutParentId(parent_id
);
167 entry
.PutServerVersion(GetNextRevision());
168 entry
.PutIsUnappliedUpdate(false);
169 entry
.PutServerNonUniqueName(name
);
170 entry
.PutServerParentId(parent_id
);
171 entry
.PutServerIsDir(is_folder
);
172 entry
.PutServerIsDel(false);
173 entry
.PutServerSpecifics(entry
.GetSpecifics());
175 return entry
.GetMetahandle();
178 int64
TestEntryFactory::CreateUnappliedRootNode(
179 ModelType model_type
) {
180 syncable::WriteTransaction
trans(FROM_HERE
, syncable::UNITTEST
, directory_
);
181 sync_pb::EntitySpecifics specifics
;
182 AddDefaultFieldValue(model_type
, &specifics
);
183 syncable::Id node_id
= TestIdFactory::MakeServer("xyz");
184 syncable::MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
186 DCHECK(entry
.good());
187 // Make it look like sort of like a pending creation from the server.
188 // The SERVER_PARENT_ID and UNIQUE_CLIENT_TAG aren't quite right, but
189 // it's good enough for our purposes.
190 entry
.PutServerVersion(1);
191 entry
.PutIsUnappliedUpdate(true);
192 entry
.PutServerIsDir(false);
193 entry
.PutServerParentId(TestIdFactory::root());
194 entry
.PutServerSpecifics(specifics
);
195 entry
.PutNonUniqueName("xyz");
197 return entry
.GetMetahandle();
200 bool TestEntryFactory::SetServerSpecificsForItem(
202 const sync_pb::EntitySpecifics specifics
) {
203 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
204 MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
208 entry
.PutServerSpecifics(specifics
);
209 entry
.PutIsUnappliedUpdate(true);
213 bool TestEntryFactory::SetLocalSpecificsForItem(
215 const sync_pb::EntitySpecifics specifics
) {
216 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
217 MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
221 entry
.PutSpecifics(specifics
);
222 entry
.PutIsUnsynced(true);
226 const sync_pb::EntitySpecifics
& TestEntryFactory::GetServerSpecificsForItem(
227 int64 meta_handle
) const {
228 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
229 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
230 DCHECK(entry
.good());
231 return entry
.GetServerSpecifics();
234 const sync_pb::EntitySpecifics
& TestEntryFactory::GetLocalSpecificsForItem(
235 int64 meta_handle
) const {
236 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
237 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
238 DCHECK(entry
.good());
239 return entry
.GetSpecifics();
242 bool TestEntryFactory::SetServerAttachmentMetadataForItem(
244 const sync_pb::AttachmentMetadata metadata
) {
245 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
246 MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
250 entry
.PutServerAttachmentMetadata(metadata
);
251 entry
.PutIsUnappliedUpdate(true);
256 bool TestEntryFactory::SetLocalAttachmentMetadataForItem(
258 const sync_pb::AttachmentMetadata metadata
) {
259 WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory_
);
260 MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
264 entry
.PutAttachmentMetadata(metadata
);
265 entry
.PutIsUnsynced(true);
269 const sync_pb::AttachmentMetadata
&
270 TestEntryFactory::GetServerAttachmentMetadataForItem(int64 meta_handle
) const {
271 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
272 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
273 DCHECK(entry
.good());
274 return entry
.GetServerAttachmentMetadata();
277 const sync_pb::AttachmentMetadata
&
278 TestEntryFactory::GetLocalAttachmentMetadataForItem(int64 meta_handle
) const {
279 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
280 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
281 DCHECK(entry
.good());
282 return entry
.GetAttachmentMetadata();
285 bool TestEntryFactory::GetIsUnsyncedForItem(int64 meta_handle
) const {
286 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
287 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
292 return entry
.GetIsUnsynced();
295 bool TestEntryFactory::GetIsUnappliedForItem(int64 meta_handle
) const {
296 syncable::ReadTransaction
trans(FROM_HERE
, directory_
);
297 syncable::Entry
entry(&trans
, syncable::GET_BY_HANDLE
, meta_handle
);
302 return entry
.GetIsUnappliedUpdate();
305 int64
TestEntryFactory::GetNextRevision() {
306 return next_revision_
++;
309 } // namespace syncer