Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: sync/internal_api/test/test_entry_factory.cc

Issue 11817010: sync: Initialize entries with a valid model type (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix broken DCHECKs Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sync/internal_api/sync_manager_impl_unittest.cc ('k') | sync/internal_api/write_node.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 bool lookup_result = directory_->GetLastChildIdForTest(
80 MutableEntry entry(&trans, syncable::CREATE, parent_id, name); 105 &trans, parent_id, &predecessor_id);
106 DCHECK(lookup_result);
107 }
108
109 MutableEntry entry(&trans, syncable::CREATE, model_type, parent_id, name);
81 DCHECK(entry.good()); 110 DCHECK(entry.good());
82 entry.Put(syncable::ID, item_id); 111 entry.Put(syncable::ID, item_id);
83 entry.Put(syncable::BASE_VERSION, 112 entry.Put(syncable::BASE_VERSION,
84 item_id.ServerKnows() ? GetNextRevision() : 0); 113 item_id.ServerKnows() ? GetNextRevision() : 0);
85 entry.Put(syncable::IS_UNSYNCED, true); 114 entry.Put(syncable::IS_UNSYNCED, true);
86 entry.Put(syncable::IS_DIR, is_folder); 115 entry.Put(syncable::IS_DIR, is_folder);
87 entry.Put(syncable::IS_DEL, false); 116 entry.Put(syncable::IS_DEL, false);
88 entry.Put(syncable::PARENT_ID, parent_id); 117 entry.Put(syncable::PARENT_ID, parent_id);
89 CHECK(entry.PutPredecessor(predecessor_id));
90 sync_pb::EntitySpecifics default_specifics; 118 sync_pb::EntitySpecifics default_specifics;
91 AddDefaultFieldValue(model_type, &default_specifics); 119 AddDefaultFieldValue(model_type, &default_specifics);
92 entry.Put(syncable::SPECIFICS, default_specifics); 120 entry.Put(syncable::SPECIFICS, default_specifics);
121
122 // Bookmarks get inserted at the end of the list.
123 if (model_type == BOOKMARKS) {
124 bool put_result = entry.PutPredecessor(predecessor_id);
125 DCHECK(put_result);
126 }
127
93 if (item_id.ServerKnows()) { 128 if (item_id.ServerKnows()) {
94 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); 129 entry.Put(syncable::SERVER_SPECIFICS, default_specifics);
95 entry.Put(syncable::SERVER_IS_DIR, is_folder); 130 entry.Put(syncable::SERVER_IS_DIR, false);
96 entry.Put(syncable::SERVER_PARENT_ID, parent_id); 131 entry.Put(syncable::SERVER_PARENT_ID, parent_id);
97 entry.Put(syncable::SERVER_IS_DEL, false); 132 entry.Put(syncable::SERVER_IS_DEL, false);
98 } 133 }
99 if (metahandle_out) 134 if (metahandle_out)
100 *metahandle_out = entry.Get(syncable::META_HANDLE); 135 *metahandle_out = entry.Get(syncable::META_HANDLE);
101 } 136 }
102 137
103 int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem( 138 int64 TestEntryFactory::CreateUnappliedAndUnsyncedBookmarkItem(
104 const string& name, 139 const string& name) {
105 ModelType model_type) {
106 int64 metahandle = 0; 140 int64 metahandle = 0;
107 CreateUnsyncedItem( 141 CreateUnsyncedItem(
108 TestIdFactory::MakeServer(name), TestIdFactory::root(), 142 TestIdFactory::MakeServer(name), TestIdFactory::root(),
109 name, false, model_type, &metahandle); 143 name, false, BOOKMARKS, &metahandle);
110 144
111 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); 145 WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
112 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle); 146 MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle);
113 if (!entry.good()) { 147 if (!entry.good()) {
114 NOTREACHED(); 148 NOTREACHED();
115 return syncable::kInvalidMetaHandle; 149 return syncable::kInvalidMetaHandle;
116 } 150 }
117 151
118 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); 152 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
119 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); 153 entry.Put(syncable::SERVER_VERSION, GetNextRevision());
120 154
121 return metahandle; 155 return metahandle;
122 } 156 }
123 157
124 int64 TestEntryFactory::CreateSyncedItem( 158 int64 TestEntryFactory::CreateSyncedItem(
125 const std::string& name, ModelType model_type, bool is_folder) { 159 const std::string& name, ModelType model_type, bool is_folder) {
126 WriteTransaction trans(FROM_HERE, UNITTEST, directory_); 160 WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
127 161
128 syncable::Id parent_id(TestIdFactory::root()); 162 syncable::Id parent_id(TestIdFactory::root());
129 syncable::Id item_id(TestIdFactory::MakeServer(name)); 163 syncable::Id item_id(TestIdFactory::MakeServer(name));
130 int64 version = GetNextRevision(); 164 int64 version = GetNextRevision();
131 165
132 sync_pb::EntitySpecifics default_specifics; 166 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()) { 167 if (!entry.good()) {
137 NOTREACHED(); 168 NOTREACHED();
138 return syncable::kInvalidMetaHandle; 169 return syncable::kInvalidMetaHandle;
139 } 170 }
140 171
141 entry.Put(syncable::ID, item_id); 172 entry.Put(syncable::ID, item_id);
142 entry.Put(syncable::BASE_VERSION, version); 173 entry.Put(syncable::BASE_VERSION, version);
143 entry.Put(syncable::IS_UNSYNCED, false); 174 entry.Put(syncable::IS_UNSYNCED, false);
144 entry.Put(syncable::NON_UNIQUE_NAME, name); 175 entry.Put(syncable::NON_UNIQUE_NAME, name);
145 entry.Put(syncable::IS_DIR, is_folder); 176 entry.Put(syncable::IS_DIR, is_folder);
146 entry.Put(syncable::IS_DEL, false); 177 entry.Put(syncable::IS_DEL, false);
147 entry.Put(syncable::PARENT_ID, parent_id); 178 entry.Put(syncable::PARENT_ID, parent_id);
148 179
180 // TODO(sync): Place bookmarks at the end of the list?
149 if (!entry.PutPredecessor(TestIdFactory::root())) { 181 if (!entry.PutPredecessor(TestIdFactory::root())) {
150 NOTREACHED(); 182 NOTREACHED();
151 return syncable::kInvalidMetaHandle; 183 return syncable::kInvalidMetaHandle;
152 } 184 }
153 entry.Put(syncable::SPECIFICS, default_specifics);
154 185
155 entry.Put(syncable::SERVER_VERSION, GetNextRevision()); 186 entry.Put(syncable::SERVER_VERSION, GetNextRevision());
156 entry.Put(syncable::IS_UNAPPLIED_UPDATE, true); 187 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false);
157 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X"); 188 entry.Put(syncable::SERVER_NON_UNIQUE_NAME, name);
158 entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y")); 189 entry.Put(syncable::SERVER_PARENT_ID, parent_id);
159 entry.Put(syncable::SERVER_IS_DIR, is_folder); 190 entry.Put(syncable::SERVER_IS_DIR, is_folder);
160 entry.Put(syncable::SERVER_IS_DEL, false); 191 entry.Put(syncable::SERVER_IS_DEL, false);
161 entry.Put(syncable::SERVER_SPECIFICS, default_specifics); 192 entry.Put(syncable::SERVER_SPECIFICS, entry.Get(syncable::SPECIFICS));
162 entry.Put(syncable::SERVER_PARENT_ID, parent_id);
163 193
164 return entry.Get(syncable::META_HANDLE); 194 return entry.Get(syncable::META_HANDLE);
165 } 195 }
166 196
167 int64 TestEntryFactory::CreateUnappliedRootNode( 197 int64 TestEntryFactory::CreateUnappliedRootNode(
168 ModelType model_type) { 198 ModelType model_type) {
169 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_); 199 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, directory_);
170 sync_pb::EntitySpecifics specifics; 200 sync_pb::EntitySpecifics specifics;
171 AddDefaultFieldValue(model_type, &specifics); 201 AddDefaultFieldValue(model_type, &specifics);
172 syncable::Id node_id = TestIdFactory::MakeServer("xyz"); 202 syncable::Id node_id = TestIdFactory::MakeServer("xyz");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 return false; 276 return false;
247 } 277 }
248 return entry.Get(syncable::IS_UNAPPLIED_UPDATE); 278 return entry.Get(syncable::IS_UNAPPLIED_UPDATE);
249 } 279 }
250 280
251 int64 TestEntryFactory::GetNextRevision() { 281 int64 TestEntryFactory::GetNextRevision() {
252 return next_revision_++; 282 return next_revision_++;
253 } 283 }
254 284
255 } // namespace syncer 285 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/sync_manager_impl_unittest.cc ('k') | sync/internal_api/write_node.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698