| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/sync/api/fake_model_type_service.h" | 5 #include "components/sync/api/fake_model_type_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "components/sync/core/data_batch_impl.h" | 9 #include "components/sync/core/data_batch_impl.h" |
| 10 #include "components/sync/core/simple_metadata_change_list.h" | 10 #include "components/sync/core/simple_metadata_change_list.h" |
| 11 #include "components/sync/syncable/syncable_util.h" | 11 #include "components/sync/syncable/syncable_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 using sync_pb::EntitySpecifics; | 14 using sync_pb::EntitySpecifics; |
| 15 using sync_pb::EntityMetadata; | 15 using sync_pb::EntityMetadata; |
| 16 using sync_pb::DataTypeState; | 16 using sync_pb::DataTypeState; |
| 17 | 17 |
| 18 namespace syncer_v2 { | 18 namespace syncer { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // It is intentionally very difficult to copy an EntityData, as in normal code | 22 // It is intentionally very difficult to copy an EntityData, as in normal code |
| 23 // we never want to. However, since we store the data as an EntityData for the | 23 // we never want to. However, since we store the data as an EntityData for the |
| 24 // test code here, this function is needed to manually copy it. | 24 // test code here, this function is needed to manually copy it. |
| 25 std::unique_ptr<EntityData> CopyEntityData(const EntityData& old_data) { | 25 std::unique_ptr<EntityData> CopyEntityData(const EntityData& old_data) { |
| 26 std::unique_ptr<EntityData> new_data(new EntityData()); | 26 std::unique_ptr<EntityData> new_data(new EntityData()); |
| 27 new_data->id = old_data.id; | 27 new_data->id = old_data.id; |
| 28 new_data->client_tag_hash = old_data.client_tag_hash; | 28 new_data->client_tag_hash = old_data.client_tag_hash; |
| 29 new_data->non_unique_name = old_data.non_unique_name; | 29 new_data->non_unique_name = old_data.non_unique_name; |
| 30 new_data->specifics = old_data.specifics; | 30 new_data->specifics = old_data.specifics; |
| 31 new_data->creation_time = old_data.creation_time; | 31 new_data->creation_time = old_data.creation_time; |
| 32 new_data->modification_time = old_data.modification_time; | 32 new_data->modification_time = old_data.modification_time; |
| 33 return new_data; | 33 return new_data; |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 // static | 38 // static |
| 39 std::string FakeModelTypeService::ClientTagFromKey(const std::string& key) { | 39 std::string FakeModelTypeService::ClientTagFromKey(const std::string& key) { |
| 40 return "ClientTag_" + key; | 40 return "ClientTag_" + key; |
| 41 } | 41 } |
| 42 | 42 |
| 43 // static | 43 // static |
| 44 std::string FakeModelTypeService::TagHashFromKey(const std::string& key) { | 44 std::string FakeModelTypeService::TagHashFromKey(const std::string& key) { |
| 45 return syncer::syncable::GenerateSyncableHash( | 45 return syncable::GenerateSyncableHash( |
| 46 syncer::PREFERENCES, FakeModelTypeService::ClientTagFromKey(key)); | 46 PREFERENCES, FakeModelTypeService::ClientTagFromKey(key)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // static | 49 // static |
| 50 EntitySpecifics FakeModelTypeService::GenerateSpecifics( | 50 EntitySpecifics FakeModelTypeService::GenerateSpecifics( |
| 51 const std::string& key, | 51 const std::string& key, |
| 52 const std::string& value) { | 52 const std::string& value) { |
| 53 EntitySpecifics specifics; | 53 EntitySpecifics specifics; |
| 54 specifics.mutable_preference()->set_name(key); | 54 specifics.mutable_preference()->set_name(key); |
| 55 specifics.mutable_preference()->set_value(value); | 55 specifics.mutable_preference()->set_value(value); |
| 56 return specifics; | 56 return specifics; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 void FakeModelTypeService::Store::Reset() { | 128 void FakeModelTypeService::Store::Reset() { |
| 129 data_change_count_ = 0; | 129 data_change_count_ = 0; |
| 130 metadata_change_count_ = 0; | 130 metadata_change_count_ = 0; |
| 131 data_store_.clear(); | 131 data_store_.clear(); |
| 132 metadata_store_.clear(); | 132 metadata_store_.clear(); |
| 133 data_type_state_.Clear(); | 133 data_type_state_.Clear(); |
| 134 } | 134 } |
| 135 | 135 |
| 136 FakeModelTypeService::FakeModelTypeService( | 136 FakeModelTypeService::FakeModelTypeService( |
| 137 const ChangeProcessorFactory& change_processor_factory) | 137 const ChangeProcessorFactory& change_processor_factory) |
| 138 : ModelTypeService(change_processor_factory, syncer::PREFERENCES) {} | 138 : ModelTypeService(change_processor_factory, PREFERENCES) {} |
| 139 | 139 |
| 140 FakeModelTypeService::~FakeModelTypeService() { | 140 FakeModelTypeService::~FakeModelTypeService() { |
| 141 CheckPostConditions(); | 141 CheckPostConditions(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 EntitySpecifics FakeModelTypeService::WriteItem(const std::string& key, | 144 EntitySpecifics FakeModelTypeService::WriteItem(const std::string& key, |
| 145 const std::string& value) { | 145 const std::string& value) { |
| 146 std::unique_ptr<EntityData> entity_data = GenerateEntityData(key, value); | 146 std::unique_ptr<EntityData> entity_data = GenerateEntityData(key, value); |
| 147 EntitySpecifics specifics_copy = entity_data->specifics; | 147 EntitySpecifics specifics_copy = entity_data->specifics; |
| 148 WriteItem(key, std::move(entity_data)); | 148 WriteItem(key, std::move(entity_data)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 169 change_processor()->Delete(key, change_list.get()); | 169 change_processor()->Delete(key, change_list.get()); |
| 170 ApplyMetadataChangeList(std::move(change_list)); | 170 ApplyMetadataChangeList(std::move(change_list)); |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 | 173 |
| 174 std::unique_ptr<MetadataChangeList> | 174 std::unique_ptr<MetadataChangeList> |
| 175 FakeModelTypeService::CreateMetadataChangeList() { | 175 FakeModelTypeService::CreateMetadataChangeList() { |
| 176 return std::unique_ptr<MetadataChangeList>(new SimpleMetadataChangeList()); | 176 return std::unique_ptr<MetadataChangeList>(new SimpleMetadataChangeList()); |
| 177 } | 177 } |
| 178 | 178 |
| 179 syncer::SyncError FakeModelTypeService::MergeSyncData( | 179 SyncError FakeModelTypeService::MergeSyncData( |
| 180 std::unique_ptr<MetadataChangeList> metadata_changes, | 180 std::unique_ptr<MetadataChangeList> metadata_changes, |
| 181 EntityDataMap data_map) { | 181 EntityDataMap data_map) { |
| 182 if (service_error_.IsSet()) { | 182 if (service_error_.IsSet()) { |
| 183 syncer::SyncError error = service_error_; | 183 SyncError error = service_error_; |
| 184 service_error_ = syncer::SyncError(); | 184 service_error_ = SyncError(); |
| 185 return error; | 185 return error; |
| 186 } | 186 } |
| 187 // Commit any local entities that aren't being overwritten by the server. | 187 // Commit any local entities that aren't being overwritten by the server. |
| 188 for (const auto& kv : db_.all_data()) { | 188 for (const auto& kv : db_.all_data()) { |
| 189 if (data_map.find(kv.first) == data_map.end()) { | 189 if (data_map.find(kv.first) == data_map.end()) { |
| 190 change_processor()->Put(kv.first, CopyEntityData(*kv.second), | 190 change_processor()->Put(kv.first, CopyEntityData(*kv.second), |
| 191 metadata_changes.get()); | 191 metadata_changes.get()); |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 // Store any new remote entities. | 194 // Store any new remote entities. |
| 195 for (const auto& kv : data_map) { | 195 for (const auto& kv : data_map) { |
| 196 db_.PutData(kv.first, kv.second.value()); | 196 db_.PutData(kv.first, kv.second.value()); |
| 197 } | 197 } |
| 198 ApplyMetadataChangeList(std::move(metadata_changes)); | 198 ApplyMetadataChangeList(std::move(metadata_changes)); |
| 199 return syncer::SyncError(); | 199 return SyncError(); |
| 200 } | 200 } |
| 201 | 201 |
| 202 syncer::SyncError FakeModelTypeService::ApplySyncChanges( | 202 SyncError FakeModelTypeService::ApplySyncChanges( |
| 203 std::unique_ptr<MetadataChangeList> metadata_changes, | 203 std::unique_ptr<MetadataChangeList> metadata_changes, |
| 204 EntityChangeList entity_changes) { | 204 EntityChangeList entity_changes) { |
| 205 if (service_error_.IsSet()) { | 205 if (service_error_.IsSet()) { |
| 206 syncer::SyncError error = service_error_; | 206 SyncError error = service_error_; |
| 207 service_error_ = syncer::SyncError(); | 207 service_error_ = SyncError(); |
| 208 return error; | 208 return error; |
| 209 } | 209 } |
| 210 for (const EntityChange& change : entity_changes) { | 210 for (const EntityChange& change : entity_changes) { |
| 211 switch (change.type()) { | 211 switch (change.type()) { |
| 212 case EntityChange::ACTION_ADD: | 212 case EntityChange::ACTION_ADD: |
| 213 EXPECT_FALSE(db_.HasData(change.storage_key())); | 213 EXPECT_FALSE(db_.HasData(change.storage_key())); |
| 214 db_.PutData(change.storage_key(), change.data()); | 214 db_.PutData(change.storage_key(), change.data()); |
| 215 break; | 215 break; |
| 216 case EntityChange::ACTION_UPDATE: | 216 case EntityChange::ACTION_UPDATE: |
| 217 EXPECT_TRUE(db_.HasData(change.storage_key())); | 217 EXPECT_TRUE(db_.HasData(change.storage_key())); |
| 218 db_.PutData(change.storage_key(), change.data()); | 218 db_.PutData(change.storage_key(), change.data()); |
| 219 break; | 219 break; |
| 220 case EntityChange::ACTION_DELETE: | 220 case EntityChange::ACTION_DELETE: |
| 221 EXPECT_TRUE(db_.HasData(change.storage_key())); | 221 EXPECT_TRUE(db_.HasData(change.storage_key())); |
| 222 db_.RemoveData(change.storage_key()); | 222 db_.RemoveData(change.storage_key()); |
| 223 break; | 223 break; |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 ApplyMetadataChangeList(std::move(metadata_changes)); | 226 ApplyMetadataChangeList(std::move(metadata_changes)); |
| 227 return syncer::SyncError(); | 227 return SyncError(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 void FakeModelTypeService::ApplyMetadataChangeList( | 230 void FakeModelTypeService::ApplyMetadataChangeList( |
| 231 std::unique_ptr<MetadataChangeList> change_list) { | 231 std::unique_ptr<MetadataChangeList> change_list) { |
| 232 DCHECK(change_list); | 232 DCHECK(change_list); |
| 233 SimpleMetadataChangeList* changes = | 233 SimpleMetadataChangeList* changes = |
| 234 static_cast<SimpleMetadataChangeList*>(change_list.get()); | 234 static_cast<SimpleMetadataChangeList*>(change_list.get()); |
| 235 const auto& metadata_changes = changes->GetMetadataChanges(); | 235 const auto& metadata_changes = changes->GetMetadataChanges(); |
| 236 for (const auto& kv : metadata_changes) { | 236 for (const auto& kv : metadata_changes) { |
| 237 switch (kv.second.type) { | 237 switch (kv.second.type) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 254 case SimpleMetadataChangeList::CLEAR: | 254 case SimpleMetadataChangeList::CLEAR: |
| 255 db_.set_data_type_state(DataTypeState()); | 255 db_.set_data_type_state(DataTypeState()); |
| 256 break; | 256 break; |
| 257 } | 257 } |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 void FakeModelTypeService::GetData(StorageKeyList keys, DataCallback callback) { | 261 void FakeModelTypeService::GetData(StorageKeyList keys, DataCallback callback) { |
| 262 if (service_error_.IsSet()) { | 262 if (service_error_.IsSet()) { |
| 263 callback.Run(service_error_, nullptr); | 263 callback.Run(service_error_, nullptr); |
| 264 service_error_ = syncer::SyncError(); | 264 service_error_ = SyncError(); |
| 265 return; | 265 return; |
| 266 } | 266 } |
| 267 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 267 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
| 268 for (const std::string& key : keys) { | 268 for (const std::string& key : keys) { |
| 269 DCHECK(db_.HasData(key)) << "No data for " << key; | 269 DCHECK(db_.HasData(key)) << "No data for " << key; |
| 270 batch->Put(key, CopyEntityData(db_.GetData(key))); | 270 batch->Put(key, CopyEntityData(db_.GetData(key))); |
| 271 } | 271 } |
| 272 callback.Run(syncer::SyncError(), std::move(batch)); | 272 callback.Run(SyncError(), std::move(batch)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 void FakeModelTypeService::GetAllData(DataCallback callback) { | 275 void FakeModelTypeService::GetAllData(DataCallback callback) { |
| 276 if (service_error_.IsSet()) { | 276 if (service_error_.IsSet()) { |
| 277 callback.Run(service_error_, nullptr); | 277 callback.Run(service_error_, nullptr); |
| 278 service_error_ = syncer::SyncError(); | 278 service_error_ = SyncError(); |
| 279 return; | 279 return; |
| 280 } | 280 } |
| 281 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); | 281 std::unique_ptr<DataBatchImpl> batch(new DataBatchImpl()); |
| 282 for (const auto& kv : db_.all_data()) { | 282 for (const auto& kv : db_.all_data()) { |
| 283 batch->Put(kv.first, CopyEntityData(*kv.second)); | 283 batch->Put(kv.first, CopyEntityData(*kv.second)); |
| 284 } | 284 } |
| 285 callback.Run(syncer::SyncError(), std::move(batch)); | 285 callback.Run(SyncError(), std::move(batch)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 std::string FakeModelTypeService::GetClientTag(const EntityData& entity_data) { | 288 std::string FakeModelTypeService::GetClientTag(const EntityData& entity_data) { |
| 289 return ClientTagFromKey(entity_data.specifics.preference().name()); | 289 return ClientTagFromKey(entity_data.specifics.preference().name()); |
| 290 } | 290 } |
| 291 | 291 |
| 292 std::string FakeModelTypeService::GetStorageKey(const EntityData& entity_data) { | 292 std::string FakeModelTypeService::GetStorageKey(const EntityData& entity_data) { |
| 293 return entity_data.specifics.preference().name(); | 293 return entity_data.specifics.preference().name(); |
| 294 } | 294 } |
| 295 | 295 |
| 296 void FakeModelTypeService::OnChangeProcessorSet() {} | 296 void FakeModelTypeService::OnChangeProcessorSet() {} |
| 297 | 297 |
| 298 void FakeModelTypeService::SetServiceError( | 298 void FakeModelTypeService::SetServiceError(SyncError::ErrorType error_type) { |
| 299 syncer::SyncError::ErrorType error_type) { | |
| 300 DCHECK(!service_error_.IsSet()); | 299 DCHECK(!service_error_.IsSet()); |
| 301 service_error_ = syncer::SyncError(FROM_HERE, error_type, "TestError", | 300 service_error_ = SyncError(FROM_HERE, error_type, "TestError", PREFERENCES); |
| 302 syncer::PREFERENCES); | |
| 303 } | 301 } |
| 304 | 302 |
| 305 ConflictResolution FakeModelTypeService::ResolveConflict( | 303 ConflictResolution FakeModelTypeService::ResolveConflict( |
| 306 const EntityData& local_data, | 304 const EntityData& local_data, |
| 307 const EntityData& remote_data) const { | 305 const EntityData& remote_data) const { |
| 308 DCHECK(conflict_resolution_); | 306 DCHECK(conflict_resolution_); |
| 309 return std::move(*conflict_resolution_); | 307 return std::move(*conflict_resolution_); |
| 310 } | 308 } |
| 311 | 309 |
| 312 void FakeModelTypeService::SetConflictResolution( | 310 void FakeModelTypeService::SetConflictResolution( |
| 313 ConflictResolution resolution) { | 311 ConflictResolution resolution) { |
| 314 conflict_resolution_.reset(new ConflictResolution(std::move(resolution))); | 312 conflict_resolution_.reset(new ConflictResolution(std::move(resolution))); |
| 315 } | 313 } |
| 316 | 314 |
| 317 void FakeModelTypeService::CheckPostConditions() { | 315 void FakeModelTypeService::CheckPostConditions() { |
| 318 DCHECK(!service_error_.IsSet()); | 316 DCHECK(!service_error_.IsSet()); |
| 319 } | 317 } |
| 320 | 318 |
| 321 } // namespace syncer_v2 | 319 } // namespace syncer |
| OLD | NEW |