OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/processor_entity_tracker.h" | 5 #include "sync/internal_api/public/processor_entity_tracker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
8 #include <utility> | 10 #include <utility> |
9 | 11 |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
12 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
13 #include "sync/internal_api/public/non_blocking_sync_common.h" | 15 #include "sync/internal_api/public/non_blocking_sync_common.h" |
14 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
15 #include "sync/syncable/syncable_util.h" | 17 #include "sync/syncable/syncable_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace syncer_v2 { | 20 namespace syncer_v2 { |
19 | 21 |
20 // Some simple sanity tests for the ProcessorEntityTracker. | 22 // Some simple sanity tests for the ProcessorEntityTracker. |
(...skipping 16 matching lines...) Expand all Loading... |
37 sync_pb::PreferenceSpecifics* pref_specifics = | 39 sync_pb::PreferenceSpecifics* pref_specifics = |
38 specifics.mutable_preference(); | 40 specifics.mutable_preference(); |
39 pref_specifics->set_name(kClientTag); | 41 pref_specifics->set_name(kClientTag); |
40 pref_specifics->set_value("pref.value"); | 42 pref_specifics->set_value("pref.value"); |
41 } | 43 } |
42 | 44 |
43 static std::string GetSyncableHash(const std::string& tag) { | 45 static std::string GetSyncableHash(const std::string& tag) { |
44 return syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES, tag); | 46 return syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES, tag); |
45 } | 47 } |
46 | 48 |
47 scoped_ptr<ProcessorEntityTracker> NewLocalItem(const std::string& tag) { | 49 std::unique_ptr<ProcessorEntityTracker> NewLocalItem(const std::string& tag) { |
48 return scoped_ptr<ProcessorEntityTracker>(ProcessorEntityTracker::CreateNew( | 50 return std::unique_ptr<ProcessorEntityTracker>( |
49 tag, GetSyncableHash(tag), "", kCtime)); | 51 ProcessorEntityTracker::CreateNew(tag, GetSyncableHash(tag), "", |
| 52 kCtime)); |
50 } | 53 } |
51 | 54 |
52 scoped_ptr<ProcessorEntityTracker> NewLocalItem( | 55 std::unique_ptr<ProcessorEntityTracker> NewLocalItem( |
53 const std::string& tag, | 56 const std::string& tag, |
54 const sync_pb::EntitySpecifics& specifics) { | 57 const sync_pb::EntitySpecifics& specifics) { |
55 scoped_ptr<ProcessorEntityTracker> entity(NewLocalItem(tag)); | 58 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem(tag)); |
56 MakeLocalChange(entity.get(), specifics); | 59 MakeLocalChange(entity.get(), specifics); |
57 return entity; | 60 return entity; |
58 } | 61 } |
59 | 62 |
60 void MakeLocalChange(ProcessorEntityTracker* entity, | 63 void MakeLocalChange(ProcessorEntityTracker* entity, |
61 const sync_pb::EntitySpecifics& specifics) { | 64 const sync_pb::EntitySpecifics& specifics) { |
62 scoped_ptr<EntityData> entity_data = make_scoped_ptr(new EntityData()); | 65 std::unique_ptr<EntityData> entity_data = |
| 66 base::WrapUnique(new EntityData()); |
63 entity_data->client_tag_hash = entity->metadata().client_tag_hash(); | 67 entity_data->client_tag_hash = entity->metadata().client_tag_hash(); |
64 entity_data->specifics = specifics; | 68 entity_data->specifics = specifics; |
65 entity_data->non_unique_name = "foo"; | 69 entity_data->non_unique_name = "foo"; |
66 entity_data->modification_time = kMtime; | 70 entity_data->modification_time = kMtime; |
67 entity->MakeLocalChange(std::move(entity_data)); | 71 entity->MakeLocalChange(std::move(entity_data)); |
68 } | 72 } |
69 | 73 |
70 scoped_ptr<ProcessorEntityTracker> NewServerItem() { | 74 std::unique_ptr<ProcessorEntityTracker> NewServerItem() { |
71 return scoped_ptr<ProcessorEntityTracker>(ProcessorEntityTracker::CreateNew( | 75 return std::unique_ptr<ProcessorEntityTracker>( |
72 kClientTag, kClientTagHash, kServerId, kCtime)); | 76 ProcessorEntityTracker::CreateNew(kClientTag, kClientTagHash, kServerId, |
| 77 kCtime)); |
73 } | 78 } |
74 | 79 |
75 scoped_ptr<ProcessorEntityTracker> NewServerItem( | 80 std::unique_ptr<ProcessorEntityTracker> NewServerItem( |
76 int64_t version, | 81 int64_t version, |
77 const sync_pb::EntitySpecifics& specifics) { | 82 const sync_pb::EntitySpecifics& specifics) { |
78 scoped_ptr<ProcessorEntityTracker> entity(NewServerItem()); | 83 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem()); |
79 RecordAcceptedUpdate(entity.get(), version, specifics); | 84 RecordAcceptedUpdate(entity.get(), version, specifics); |
80 return entity; | 85 return entity; |
81 } | 86 } |
82 | 87 |
83 void RecordAcceptedUpdate(ProcessorEntityTracker* entity, | 88 void RecordAcceptedUpdate(ProcessorEntityTracker* entity, |
84 int64_t version, | 89 int64_t version, |
85 const sync_pb::EntitySpecifics& specifics) { | 90 const sync_pb::EntitySpecifics& specifics) { |
86 RecordAcceptedUpdate(entity, version, specifics, kMtime); | 91 RecordAcceptedUpdate(entity, version, specifics, kMtime); |
87 } | 92 } |
88 | 93 |
89 void RecordAcceptedUpdate(ProcessorEntityTracker* entity, | 94 void RecordAcceptedUpdate(ProcessorEntityTracker* entity, |
90 int64_t version, | 95 int64_t version, |
91 const sync_pb::EntitySpecifics& specifics, | 96 const sync_pb::EntitySpecifics& specifics, |
92 base::Time mtime) { | 97 base::Time mtime) { |
93 EntityData data; | 98 EntityData data; |
94 data.id = entity->metadata().server_id(); | 99 data.id = entity->metadata().server_id(); |
95 data.client_tag_hash = entity->metadata().client_tag_hash(); | 100 data.client_tag_hash = entity->metadata().client_tag_hash(); |
96 data.modification_time = mtime; | 101 data.modification_time = mtime; |
97 data.specifics = specifics; | 102 data.specifics = specifics; |
98 | 103 |
99 UpdateResponseData response_data; | 104 UpdateResponseData response_data; |
100 response_data.response_version = version; | 105 response_data.response_version = version; |
101 response_data.entity = data.PassToPtr(); | 106 response_data.entity = data.PassToPtr(); |
102 | 107 |
103 entity->RecordAcceptedUpdate(response_data); | 108 entity->RecordAcceptedUpdate(response_data); |
104 } | 109 } |
105 | 110 |
106 bool HasSpecificsHash( | 111 bool HasSpecificsHash( |
107 const scoped_ptr<ProcessorEntityTracker>& entity) const { | 112 const std::unique_ptr<ProcessorEntityTracker>& entity) const { |
108 return !entity->metadata().specifics_hash().empty(); | 113 return !entity->metadata().specifics_hash().empty(); |
109 } | 114 } |
110 | 115 |
111 const std::string kServerId; | 116 const std::string kServerId; |
112 const std::string kClientTag; | 117 const std::string kClientTag; |
113 const std::string kClientTagHash; | 118 const std::string kClientTagHash; |
114 const base::Time kCtime; | 119 const base::Time kCtime; |
115 const base::Time kMtime; | 120 const base::Time kMtime; |
116 sync_pb::EntitySpecifics specifics; | 121 sync_pb::EntitySpecifics specifics; |
117 }; | 122 }; |
118 | 123 |
119 TEST_F(ProcessorEntityTrackerTest, NewItem) { | 124 TEST_F(ProcessorEntityTrackerTest, NewItem) { |
120 scoped_ptr<ProcessorEntityTracker> entity(NewLocalItem("asdf")); | 125 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem("asdf")); |
121 | 126 |
122 EXPECT_EQ(entity->client_tag(), "asdf"); | 127 EXPECT_EQ(entity->client_tag(), "asdf"); |
123 EXPECT_EQ(entity->metadata().client_tag_hash(), GetSyncableHash("asdf")); | 128 EXPECT_EQ(entity->metadata().client_tag_hash(), GetSyncableHash("asdf")); |
124 | 129 |
125 EXPECT_FALSE(entity->HasCommitData()); | 130 EXPECT_FALSE(entity->HasCommitData()); |
126 EXPECT_FALSE(HasSpecificsHash(entity)); | 131 EXPECT_FALSE(HasSpecificsHash(entity)); |
127 | 132 |
128 EXPECT_FALSE(entity->IsUnsynced()); | 133 EXPECT_FALSE(entity->IsUnsynced()); |
129 EXPECT_FALSE(entity->UpdateIsReflection(1)); | 134 EXPECT_FALSE(entity->UpdateIsReflection(1)); |
130 } | 135 } |
131 | 136 |
132 TEST_F(ProcessorEntityTrackerTest, NewLocalItem) { | 137 TEST_F(ProcessorEntityTrackerTest, NewLocalItem) { |
133 scoped_ptr<ProcessorEntityTracker> entity(NewLocalItem("asdf", specifics)); | 138 std::unique_ptr<ProcessorEntityTracker> entity( |
| 139 NewLocalItem("asdf", specifics)); |
134 | 140 |
135 EXPECT_EQ(1, entity->metadata().sequence_number()); | 141 EXPECT_EQ(1, entity->metadata().sequence_number()); |
136 EXPECT_TRUE(entity->HasCommitData()); | 142 EXPECT_TRUE(entity->HasCommitData()); |
137 EXPECT_TRUE(HasSpecificsHash(entity)); | 143 EXPECT_TRUE(HasSpecificsHash(entity)); |
138 EXPECT_TRUE(entity->IsUnsynced()); | 144 EXPECT_TRUE(entity->IsUnsynced()); |
139 EXPECT_FALSE(entity->UpdateIsReflection(1)); | 145 EXPECT_FALSE(entity->UpdateIsReflection(1)); |
140 | 146 |
141 entity->ReceiveCommitResponse("id", 1, 1, ""); | 147 entity->ReceiveCommitResponse("id", 1, 1, ""); |
142 | 148 |
143 EXPECT_FALSE(entity->HasCommitData()); | 149 EXPECT_FALSE(entity->HasCommitData()); |
144 EXPECT_TRUE(HasSpecificsHash(entity)); | 150 EXPECT_TRUE(HasSpecificsHash(entity)); |
145 EXPECT_FALSE(entity->IsUnsynced()); | 151 EXPECT_FALSE(entity->IsUnsynced()); |
146 EXPECT_TRUE(entity->UpdateIsReflection(1)); | 152 EXPECT_TRUE(entity->UpdateIsReflection(1)); |
147 } | 153 } |
148 | 154 |
149 TEST_F(ProcessorEntityTrackerTest, FromServerUpdate) { | 155 TEST_F(ProcessorEntityTrackerTest, FromServerUpdate) { |
150 scoped_ptr<ProcessorEntityTracker> entity(NewServerItem()); | 156 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem()); |
151 | 157 |
152 EXPECT_EQ(entity->client_tag(), kClientTag); | 158 EXPECT_EQ(entity->client_tag(), kClientTag); |
153 EXPECT_EQ(entity->metadata().client_tag_hash(), kClientTagHash); | 159 EXPECT_EQ(entity->metadata().client_tag_hash(), kClientTagHash); |
154 EXPECT_FALSE(HasSpecificsHash(entity)); | 160 EXPECT_FALSE(HasSpecificsHash(entity)); |
155 | 161 |
156 RecordAcceptedUpdate(entity.get(), 10, specifics); | 162 RecordAcceptedUpdate(entity.get(), 10, specifics); |
157 | 163 |
158 // No data cached but the specifics hash should be updated. | 164 // No data cached but the specifics hash should be updated. |
159 EXPECT_FALSE(entity->HasCommitData()); | 165 EXPECT_FALSE(entity->HasCommitData()); |
160 EXPECT_TRUE(HasSpecificsHash(entity)); | 166 EXPECT_TRUE(HasSpecificsHash(entity)); |
161 EXPECT_FALSE(entity->IsUnsynced()); | 167 EXPECT_FALSE(entity->IsUnsynced()); |
162 EXPECT_TRUE(entity->UpdateIsReflection(9)); | 168 EXPECT_TRUE(entity->UpdateIsReflection(9)); |
163 EXPECT_TRUE(entity->UpdateIsReflection(10)); | 169 EXPECT_TRUE(entity->UpdateIsReflection(10)); |
164 EXPECT_FALSE(entity->UpdateIsReflection(11)); | 170 EXPECT_FALSE(entity->UpdateIsReflection(11)); |
165 } | 171 } |
166 | 172 |
167 // Tombstones should behave just like regular updates. Mostly. The strangest | 173 // Tombstones should behave just like regular updates. Mostly. The strangest |
168 // thing about them is that they don't have specifics, so it can be hard to | 174 // thing about them is that they don't have specifics, so it can be hard to |
169 // detect their type. Fortunately, this class doesn't care about types in | 175 // detect their type. Fortunately, this class doesn't care about types in |
170 // received updates. | 176 // received updates. |
171 TEST_F(ProcessorEntityTrackerTest, TombstoneUpdate) { | 177 TEST_F(ProcessorEntityTrackerTest, TombstoneUpdate) { |
172 // Empty EntitySpecifics indicates tombstone update. | 178 // Empty EntitySpecifics indicates tombstone update. |
173 scoped_ptr<ProcessorEntityTracker> entity( | 179 std::unique_ptr<ProcessorEntityTracker> entity( |
174 NewServerItem(10, sync_pb::EntitySpecifics())); | 180 NewServerItem(10, sync_pb::EntitySpecifics())); |
175 | 181 |
176 EXPECT_EQ(kClientTagHash, entity->metadata().client_tag_hash()); | 182 EXPECT_EQ(kClientTagHash, entity->metadata().client_tag_hash()); |
177 EXPECT_FALSE(entity->HasCommitData()); | 183 EXPECT_FALSE(entity->HasCommitData()); |
178 EXPECT_FALSE(HasSpecificsHash(entity)); | 184 EXPECT_FALSE(HasSpecificsHash(entity)); |
179 EXPECT_FALSE(entity->IsUnsynced()); | 185 EXPECT_FALSE(entity->IsUnsynced()); |
180 EXPECT_TRUE(entity->UpdateIsReflection(9)); | 186 EXPECT_TRUE(entity->UpdateIsReflection(9)); |
181 EXPECT_TRUE(entity->UpdateIsReflection(10)); | 187 EXPECT_TRUE(entity->UpdateIsReflection(10)); |
182 EXPECT_FALSE(entity->UpdateIsReflection(11)); | 188 EXPECT_FALSE(entity->UpdateIsReflection(11)); |
183 } | 189 } |
184 | 190 |
185 // Apply a deletion update. | 191 // Apply a deletion update. |
186 TEST_F(ProcessorEntityTrackerTest, ApplyUpdate) { | 192 TEST_F(ProcessorEntityTrackerTest, ApplyUpdate) { |
187 // Start with a non-deleted state with version 10. | 193 // Start with a non-deleted state with version 10. |
188 scoped_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); | 194 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); |
189 | 195 |
190 EXPECT_TRUE(HasSpecificsHash(entity)); | 196 EXPECT_TRUE(HasSpecificsHash(entity)); |
191 | 197 |
192 // A deletion update one version later. | 198 // A deletion update one version later. |
193 RecordAcceptedUpdate(entity.get(), 11, sync_pb::EntitySpecifics(), | 199 RecordAcceptedUpdate(entity.get(), 11, sync_pb::EntitySpecifics(), |
194 kMtime + base::TimeDelta::FromSeconds(10)); | 200 kMtime + base::TimeDelta::FromSeconds(10)); |
195 | 201 |
196 EXPECT_FALSE(HasSpecificsHash(entity)); | 202 EXPECT_FALSE(HasSpecificsHash(entity)); |
197 EXPECT_FALSE(entity->IsUnsynced()); | 203 EXPECT_FALSE(entity->IsUnsynced()); |
198 EXPECT_TRUE(entity->UpdateIsReflection(11)); | 204 EXPECT_TRUE(entity->UpdateIsReflection(11)); |
199 EXPECT_FALSE(entity->UpdateIsReflection(12)); | 205 EXPECT_FALSE(entity->UpdateIsReflection(12)); |
200 } | 206 } |
201 | 207 |
202 TEST_F(ProcessorEntityTrackerTest, LocalChange) { | 208 TEST_F(ProcessorEntityTrackerTest, LocalChange) { |
203 // Start with a non-deleted state with version 10. | 209 // Start with a non-deleted state with version 10. |
204 scoped_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); | 210 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); |
205 | 211 |
206 std::string specifics_hash = entity->metadata().specifics_hash(); | 212 std::string specifics_hash = entity->metadata().specifics_hash(); |
207 | 213 |
208 // Make a local change with different specifics. | 214 // Make a local change with different specifics. |
209 sync_pb::EntitySpecifics specifics2; | 215 sync_pb::EntitySpecifics specifics2; |
210 specifics2.CopyFrom(specifics); | 216 specifics2.CopyFrom(specifics); |
211 specifics2.mutable_preference()->set_value("new.pref.value"); | 217 specifics2.mutable_preference()->set_value("new.pref.value"); |
212 MakeLocalChange(entity.get(), specifics2); | 218 MakeLocalChange(entity.get(), specifics2); |
213 | 219 |
214 EXPECT_NE(entity->metadata().specifics_hash(), specifics_hash); | 220 EXPECT_NE(entity->metadata().specifics_hash(), specifics_hash); |
215 EXPECT_TRUE(entity->HasCommitData()); | 221 EXPECT_TRUE(entity->HasCommitData()); |
216 EXPECT_TRUE(entity->IsUnsynced()); | 222 EXPECT_TRUE(entity->IsUnsynced()); |
217 EXPECT_TRUE(entity->UpdateIsReflection(10)); | 223 EXPECT_TRUE(entity->UpdateIsReflection(10)); |
218 EXPECT_FALSE(entity->UpdateIsReflection(11)); | 224 EXPECT_FALSE(entity->UpdateIsReflection(11)); |
219 } | 225 } |
220 | 226 |
221 TEST_F(ProcessorEntityTrackerTest, LocalDeletion) { | 227 TEST_F(ProcessorEntityTrackerTest, LocalDeletion) { |
222 // Start with a non-deleted state with version 10. | 228 // Start with a non-deleted state with version 10. |
223 scoped_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); | 229 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem(10, specifics)); |
224 EXPECT_TRUE(HasSpecificsHash(entity)); | 230 EXPECT_TRUE(HasSpecificsHash(entity)); |
225 | 231 |
226 // Make a local delete. | 232 // Make a local delete. |
227 entity->Delete(); | 233 entity->Delete(); |
228 | 234 |
229 EXPECT_FALSE(HasSpecificsHash(entity)); | 235 EXPECT_FALSE(HasSpecificsHash(entity)); |
230 EXPECT_FALSE(entity->HasCommitData()); | 236 EXPECT_FALSE(entity->HasCommitData()); |
231 EXPECT_FALSE(entity->RequiresCommitData()); | 237 EXPECT_FALSE(entity->RequiresCommitData()); |
232 EXPECT_TRUE(entity->IsUnsynced()); | 238 EXPECT_TRUE(entity->IsUnsynced()); |
233 EXPECT_TRUE(entity->UpdateIsReflection(10)); | 239 EXPECT_TRUE(entity->UpdateIsReflection(10)); |
234 EXPECT_FALSE(entity->UpdateIsReflection(11)); | 240 EXPECT_FALSE(entity->UpdateIsReflection(11)); |
235 } | 241 } |
236 | 242 |
237 // Verify generation of CommitRequestData from ProcessorEntityTracker. | 243 // Verify generation of CommitRequestData from ProcessorEntityTracker. |
238 // Verify that the sequence number increments on local changes. | 244 // Verify that the sequence number increments on local changes. |
239 TEST_F(ProcessorEntityTrackerTest, InitializeCommitRequestData) { | 245 TEST_F(ProcessorEntityTrackerTest, InitializeCommitRequestData) { |
240 scoped_ptr<ProcessorEntityTracker> entity(NewLocalItem(kClientTag)); | 246 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem(kClientTag)); |
241 MakeLocalChange(entity.get(), specifics); | 247 MakeLocalChange(entity.get(), specifics); |
242 | 248 |
243 CommitRequestData commit_request; | 249 CommitRequestData commit_request; |
244 entity->InitializeCommitRequestData(&commit_request); | 250 entity->InitializeCommitRequestData(&commit_request); |
245 | 251 |
246 EXPECT_EQ(1, commit_request.sequence_number); | 252 EXPECT_EQ(1, commit_request.sequence_number); |
247 EXPECT_EQ(kUncommittedVersion, commit_request.base_version); | 253 EXPECT_EQ(kUncommittedVersion, commit_request.base_version); |
248 | 254 |
249 const EntityData& data = commit_request.entity.value(); | 255 const EntityData& data = commit_request.entity.value(); |
250 EXPECT_EQ(entity->metadata().client_tag_hash(), data.client_tag_hash); | 256 EXPECT_EQ(entity->metadata().client_tag_hash(), data.client_tag_hash); |
(...skipping 16 matching lines...) Expand all Loading... |
267 entity->Delete(); | 273 entity->Delete(); |
268 | 274 |
269 entity->InitializeCommitRequestData(&commit_request); | 275 entity->InitializeCommitRequestData(&commit_request); |
270 const EntityData& data3 = commit_request.entity.value(); | 276 const EntityData& data3 = commit_request.entity.value(); |
271 | 277 |
272 EXPECT_EQ(3, commit_request.sequence_number); | 278 EXPECT_EQ(3, commit_request.sequence_number); |
273 EXPECT_TRUE(data3.is_deleted()); | 279 EXPECT_TRUE(data3.is_deleted()); |
274 } | 280 } |
275 | 281 |
276 } // namespace syncer_v2 | 282 } // namespace syncer_v2 |
OLD | NEW |