Chromium Code Reviews| Index: components/sync_driver/device_info_service_unittest.cc |
| diff --git a/components/sync_driver/device_info_service_unittest.cc b/components/sync_driver/device_info_service_unittest.cc |
| index 5c46acaf910ea0b3dee2bca5cc21ac6cf7a06795..a230b7983793b77fefba955424d9225d112a3717 100644 |
| --- a/components/sync_driver/device_info_service_unittest.cc |
| +++ b/components/sync_driver/device_info_service_unittest.cc |
| @@ -16,6 +16,7 @@ |
| #include "base/strings/stringprintf.h" |
| #include "components/sync_driver/local_device_info_provider_mock.h" |
| #include "sync/api/data_batch.h" |
| +#include "sync/api/entity_data.h" |
| #include "sync/api/metadata_batch.h" |
| #include "sync/api/model_type_store.h" |
| #include "sync/internal_api/public/test/model_type_store_test_util.h" |
| @@ -29,6 +30,7 @@ using syncer_v2::DataBatch; |
| using syncer_v2::EntityChange; |
| using syncer_v2::EntityChangeList; |
| using syncer_v2::EntityData; |
| +using syncer_v2::EntityDataList; |
| using syncer_v2::EntityDataPtr; |
| using syncer_v2::MetadataBatch; |
| using syncer_v2::MetadataChangeList; |
| @@ -98,6 +100,18 @@ void AssertExpectedFromDataBatch( |
| ASSERT_TRUE(expected.empty()); |
| } |
| +// Creats an EntityData/EntityDataPtr around a copy of the given specifics. |
| +EntityDataPtr SpecificsToEntity(const DeviceInfoSpecifics& specifics) { |
| + EntityData data; |
| + // These tests do not care about the tag hash, but EntityData and friends |
| + // cannot differentiate between the default EntityData object if the hash |
| + // is unset, which causes pass/copy operations to no-op and things start to |
| + // break, so we throw in a junk value and forget about it. |
| + data.client_tag_hash = "junk"; |
| + *data.specifics.mutable_device_info() = specifics; |
| + return data.PassToPtr(); |
| +} |
| + |
| // Instead of actually processing anything, simply accumulates all instructions |
| // in members that can then be accessed. TODO(skym): If this ends up being |
| // useful for other model type unittests it should be moved out to a shared |
| @@ -208,6 +222,13 @@ class DeviceInfoServiceTest : public testing::Test, |
| return specifics; |
| } |
| + // Override to allow specific cache guids. |
| + DeviceInfoSpecifics GenerateTestSpecifics(const std::string& guid) { |
| + DeviceInfoSpecifics specifics(GenerateTestSpecifics()); |
| + specifics.set_cache_guid(guid); |
| + return specifics; |
| + } |
| + |
| // Helper method to reduce duplicated code between tests. Wraps the given |
| // specifics object in an EntityData and EntityChange of type ACTION_ADD, and |
| // pushes them onto the given change list. The corresponding client tag the |
| @@ -215,15 +236,9 @@ class DeviceInfoServiceTest : public testing::Test, |
| // service to generate the tag. |
| std::string PushBackEntityChangeAdd(const DeviceInfoSpecifics& specifics, |
| EntityChangeList* changes) { |
| - EntityData data; |
| - // These tests do not care about the tag hash, but EntityData and friends |
| - // cannot differentiate between the default EntityData object if the hash |
| - // is unset, which causes pass/copy operations to no-op and things start to |
| - // break, so we throw in a junk value and forget about it. |
| - data.client_tag_hash = "junk"; |
| - *data.specifics.mutable_device_info() = specifics; |
| - const std::string tag = service()->GetClientTag(data); |
| - changes->push_back(EntityChange::CreateAdd(tag, data.PassToPtr())); |
| + EntityDataPtr ptr = SpecificsToEntity(specifics); |
| + const std::string tag = service()->GetClientTag(ptr.value()); |
| + changes->push_back(EntityChange::CreateAdd(tag, ptr)); |
| return tag; |
| } |
| @@ -537,8 +552,8 @@ TEST_F(DeviceInfoServiceTest, ApplySyncChangesWithLocalGuid) { |
| // The point of this test is to try to apply remote changes that have the same |
| // cache guid as the local device. The service should ignore these changes |
| // since only it should be performing writes on its data. |
| - DeviceInfoSpecifics specifics = GenerateTestSpecifics(); |
| - specifics.set_cache_guid(local_device()->GetLocalDeviceInfo()->guid()); |
| + DeviceInfoSpecifics specifics = |
| + GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); |
| EntityChangeList data_changes; |
| const std::string tag = PushBackEntityChangeAdd(specifics, &data_changes); |
| @@ -558,6 +573,89 @@ TEST_F(DeviceInfoServiceTest, ApplyDeleteNonexistent) { |
| ASSERT_FALSE(error.IsSet()); |
| } |
| +TEST_F(DeviceInfoServiceTest, MergeBeforeInit) { |
| + InitializeService(); |
| + const SyncError error = service()->MergeSyncData( |
| + service()->CreateMetadataChangeList(), EntityDataList()); |
| + ASSERT_TRUE(error.IsSet()); |
| +} |
| + |
| +TEST_F(DeviceInfoServiceTest, MergeEmpty) { |
| + InitializeAndPump(); |
| + SetProcessorAndPump(); |
| + const SyncError error = service()->MergeSyncData( |
| + service()->CreateMetadataChangeList(), EntityDataList()); |
| + ASSERT_FALSE(error.IsSet()); |
| +} |
| + |
| +TEST_F(DeviceInfoServiceTest, MergeWithData) { |
| + const DeviceInfoSpecifics unique_local(GenerateTestSpecifics("unique_local")); |
| + const DeviceInfoSpecifics conflict_local(GenerateTestSpecifics("conflict")); |
| + DeviceInfoSpecifics conflict_remote(GenerateTestSpecifics("conflict")); |
| + DeviceInfoSpecifics unique_remote(GenerateTestSpecifics("unique_remote")); |
| + |
| + scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| + store()->WriteData(batch.get(), unique_local.cache_guid(), |
| + unique_local.SerializeAsString()); |
| + store()->WriteData(batch.get(), conflict_local.cache_guid(), |
| + conflict_local.SerializeAsString()); |
| + store()->CommitWriteBatch(std::move(batch), |
| + base::Bind(&AssertResultIsSuccess)); |
| + |
| + InitializeAndPump(); |
| + SetProcessorAndPump(); |
| + |
| + EntityDataList remote_input; |
| + remote_input.push_back(SpecificsToEntity(conflict_remote)); |
| + remote_input.push_back(SpecificsToEntity(unique_remote)); |
| + |
| + DataTypeState state; |
| + state.set_encryption_key_name("ekn"); |
| + scoped_ptr<MetadataChangeList> metadata_changes( |
| + service()->CreateMetadataChangeList()); |
| + metadata_changes->UpdateDataTypeState(state); |
| + |
|
maxbogue
2016/02/23 01:04:02
Add a TODO here to actually commit |metadata_chang
skym
2016/03/04 20:28:10
Whoopps, it should be getting passed into MergeSyn
|
| + const SyncError error = service()->MergeSyncData( |
| + service()->CreateMetadataChangeList(), remote_input); |
| + ASSERT_FALSE(error.IsSet()); |
| + |
| + // The remote should beat the local in conflict. |
| + ASSERT_EQ(3u, service()->GetAllDeviceInfo().size()); |
| + AssertEqual(unique_local, *service()->GetDeviceInfo("unique_local").get()); |
| + AssertEqual(unique_remote, *service()->GetDeviceInfo("unique_remote").get()); |
| + AssertEqual(conflict_remote, *service()->GetDeviceInfo("conflict").get()); |
| + |
| + // Service should have told the processor about the existance of unique_local. |
| + ASSERT_TRUE(processor()->delete_set().empty()); |
| + ASSERT_EQ(1u, processor()->put_map().size()); |
| + auto it = processor()->put_map().find("unique_local"); |
| + ASSERT_NE(processor()->put_map().end(), it); |
| + AssertEqual(unique_local, it->second->specifics.device_info()); |
| + |
| + // TODO(skym): Uncomment once SimpleMetadataChangeList::TransferChanges is |
| + // implemented. |
| + // ASSERT_EQ(state.encryption_key_name(), |
| + // processor()->metadata()->GetDataTypeState().encryption_key_name()); |
| +} |
| + |
| +TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { |
| + InitializeAndPump(); |
| + SetProcessorAndPump(); |
| + |
| + // Service should ignore this because it uses the local device's guid. |
| + DeviceInfoSpecifics specifics( |
| + GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid())); |
| + EntityDataList remote_input; |
| + remote_input.push_back(SpecificsToEntity(specifics)); |
| + |
| + const SyncError error = service()->MergeSyncData( |
| + service()->CreateMetadataChangeList(), remote_input); |
| + ASSERT_FALSE(error.IsSet()); |
| + ASSERT_TRUE(service()->GetAllDeviceInfo().empty()); |
| + ASSERT_TRUE(processor()->delete_set().empty()); |
| + ASSERT_TRUE(processor()->put_map().empty()); |
| +} |
| + |
| } // namespace |
| } // namespace sync_driver_v2 |