| 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/device_info/device_info_service.h" | 5 #include "components/sync/device_info/device_info_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "components/sync/api/data_batch.h" | 16 #include "components/sync/api/data_batch.h" |
| 17 #include "components/sync/api/data_type_error_handler_mock.h" | 17 #include "components/sync/api/data_type_error_handler_mock.h" |
| 18 #include "components/sync/api/entity_data.h" | 18 #include "components/sync/api/entity_data.h" |
| 19 #include "components/sync/api/fake_model_type_change_processor.h" | 19 #include "components/sync/api/fake_model_type_change_processor.h" |
| 20 #include "components/sync/api/metadata_batch.h" | 20 #include "components/sync/api/metadata_batch.h" |
| 21 #include "components/sync/base/time.h" | 21 #include "components/sync/base/time.h" |
| 22 #include "components/sync/core/test/model_type_store_test_util.h" | 22 #include "components/sync/core/test/model_type_store_test_util.h" |
| 23 #include "components/sync/device_info/local_device_info_provider_mock.h" | 23 #include "components/sync/device_info/local_device_info_provider_mock.h" |
| 24 #include "components/sync/protocol/data_type_state.pb.h" | 24 #include "components/sync/protocol/model_type_state.pb.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace syncer { | 27 namespace syncer { |
| 28 | 28 |
| 29 using base::Time; | 29 using base::Time; |
| 30 using base::TimeDelta; | 30 using base::TimeDelta; |
| 31 using sync_pb::DataTypeState; | |
| 32 using sync_pb::DeviceInfoSpecifics; | 31 using sync_pb::DeviceInfoSpecifics; |
| 33 using sync_pb::EntitySpecifics; | 32 using sync_pb::EntitySpecifics; |
| 33 using sync_pb::ModelTypeState; |
| 34 | 34 |
| 35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; | 35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; |
| 36 using StorageKeyList = ModelTypeService::StorageKeyList; | 36 using StorageKeyList = ModelTypeService::StorageKeyList; |
| 37 using RecordList = ModelTypeStore::RecordList; | 37 using RecordList = ModelTypeStore::RecordList; |
| 38 using Result = ModelTypeStore::Result; | 38 using Result = ModelTypeStore::Result; |
| 39 using StartCallback = ModelTypeChangeProcessor::StartCallback; | 39 using StartCallback = ModelTypeChangeProcessor::StartCallback; |
| 40 using WriteBatch = ModelTypeStore::WriteBatch; | 40 using WriteBatch = ModelTypeStore::WriteBatch; |
| 41 | 41 |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 | 404 |
| 405 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); | 405 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); |
| 406 ASSERT_EQ(1u, all_device_info.size()); | 406 ASSERT_EQ(1u, all_device_info.size()); |
| 407 AssertEqual(specifics, *all_device_info[0]); | 407 AssertEqual(specifics, *all_device_info[0]); |
| 408 AssertEqual(specifics, | 408 AssertEqual(specifics, |
| 409 *service()->GetDeviceInfo(specifics.cache_guid()).get()); | 409 *service()->GetDeviceInfo(specifics.cache_guid()).get()); |
| 410 } | 410 } |
| 411 | 411 |
| 412 TEST_F(DeviceInfoServiceTest, TestWithLocalMetadata) { | 412 TEST_F(DeviceInfoServiceTest, TestWithLocalMetadata) { |
| 413 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 413 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 414 DataTypeState state; | 414 ModelTypeState state; |
| 415 state.set_encryption_key_name("ekn"); | 415 state.set_encryption_key_name("ekn"); |
| 416 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); | 416 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); |
| 417 store()->CommitWriteBatch(std::move(batch), | 417 store()->CommitWriteBatch(std::move(batch), |
| 418 base::Bind(&AssertResultIsSuccess)); | 418 base::Bind(&AssertResultIsSuccess)); |
| 419 InitializeAndPump(); | 419 InitializeAndPump(); |
| 420 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); | 420 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); |
| 421 ASSERT_EQ(1u, all_device_info.size()); | 421 ASSERT_EQ(1u, all_device_info.size()); |
| 422 ASSERT_TRUE( | 422 ASSERT_TRUE( |
| 423 local_device()->GetLocalDeviceInfo()->Equals(*all_device_info[0])); | 423 local_device()->GetLocalDeviceInfo()->Equals(*all_device_info[0])); |
| 424 EXPECT_EQ(1u, processor()->put_map().size()); | 424 EXPECT_EQ(1u, processor()->put_map().size()); |
| 425 } | 425 } |
| 426 | 426 |
| 427 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) { | 427 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) { |
| 428 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 428 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 429 DeviceInfoSpecifics specifics(GenerateTestSpecifics()); | 429 DeviceInfoSpecifics specifics(GenerateTestSpecifics()); |
| 430 store()->WriteData(batch.get(), specifics.cache_guid(), | 430 store()->WriteData(batch.get(), specifics.cache_guid(), |
| 431 specifics.SerializeAsString()); | 431 specifics.SerializeAsString()); |
| 432 DataTypeState state; | 432 ModelTypeState state; |
| 433 state.set_encryption_key_name("ekn"); | 433 state.set_encryption_key_name("ekn"); |
| 434 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); | 434 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); |
| 435 store()->CommitWriteBatch(std::move(batch), | 435 store()->CommitWriteBatch(std::move(batch), |
| 436 base::Bind(&AssertResultIsSuccess)); | 436 base::Bind(&AssertResultIsSuccess)); |
| 437 | 437 |
| 438 InitializeAndPump(); | 438 InitializeAndPump(); |
| 439 | 439 |
| 440 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); | 440 DeviceInfoList all_device_info(service()->GetAllDeviceInfo()); |
| 441 ASSERT_EQ(2u, all_device_info.size()); | 441 ASSERT_EQ(2u, all_device_info.size()); |
| 442 AssertEqual(specifics, | 442 AssertEqual(specifics, |
| 443 *service()->GetDeviceInfo(specifics.cache_guid()).get()); | 443 *service()->GetDeviceInfo(specifics.cache_guid()).get()); |
| 444 ASSERT_TRUE(processor()->metadata()); | 444 ASSERT_TRUE(processor()->metadata()); |
| 445 ASSERT_EQ(state.encryption_key_name(), | 445 ASSERT_EQ(state.encryption_key_name(), |
| 446 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 446 processor()->metadata()->GetModelTypeState().encryption_key_name()); |
| 447 } | 447 } |
| 448 | 448 |
| 449 TEST_F(DeviceInfoServiceTest, GetData) { | 449 TEST_F(DeviceInfoServiceTest, GetData) { |
| 450 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 450 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 451 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); | 451 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); |
| 452 DeviceInfoSpecifics specifics2(GenerateTestSpecifics()); | 452 DeviceInfoSpecifics specifics2(GenerateTestSpecifics()); |
| 453 DeviceInfoSpecifics specifics3(GenerateTestSpecifics()); | 453 DeviceInfoSpecifics specifics3(GenerateTestSpecifics()); |
| 454 store()->WriteData(batch.get(), specifics1.cache_guid(), | 454 store()->WriteData(batch.get(), specifics1.cache_guid(), |
| 455 specifics1.SerializeAsString()); | 455 specifics1.SerializeAsString()); |
| 456 store()->WriteData(batch.get(), specifics2.cache_guid(), | 456 store()->WriteData(batch.get(), specifics2.cache_guid(), |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 EXPECT_FALSE(service()->GetDeviceInfo(specifics.cache_guid())); | 539 EXPECT_FALSE(service()->GetDeviceInfo(specifics.cache_guid())); |
| 540 EXPECT_EQ(2, change_count()); | 540 EXPECT_EQ(2, change_count()); |
| 541 } | 541 } |
| 542 | 542 |
| 543 TEST_F(DeviceInfoServiceTest, ApplySyncChangesStore) { | 543 TEST_F(DeviceInfoServiceTest, ApplySyncChangesStore) { |
| 544 InitializeAndPump(); | 544 InitializeAndPump(); |
| 545 | 545 |
| 546 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); | 546 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); |
| 547 EntityChangeList data_changes; | 547 EntityChangeList data_changes; |
| 548 PushBackEntityChangeAdd(specifics, &data_changes); | 548 PushBackEntityChangeAdd(specifics, &data_changes); |
| 549 DataTypeState state; | 549 ModelTypeState state; |
| 550 state.set_encryption_key_name("ekn"); | 550 state.set_encryption_key_name("ekn"); |
| 551 std::unique_ptr<MetadataChangeList> metadata_changes( | 551 std::unique_ptr<MetadataChangeList> metadata_changes( |
| 552 service()->CreateMetadataChangeList()); | 552 service()->CreateMetadataChangeList()); |
| 553 metadata_changes->UpdateDataTypeState(state); | 553 metadata_changes->UpdateModelTypeState(state); |
| 554 | 554 |
| 555 const SyncError error = | 555 const SyncError error = |
| 556 service()->ApplySyncChanges(std::move(metadata_changes), data_changes); | 556 service()->ApplySyncChanges(std::move(metadata_changes), data_changes); |
| 557 EXPECT_FALSE(error.IsSet()); | 557 EXPECT_FALSE(error.IsSet()); |
| 558 EXPECT_EQ(1, change_count()); | 558 EXPECT_EQ(1, change_count()); |
| 559 | 559 |
| 560 RestartService(); | 560 RestartService(); |
| 561 | 561 |
| 562 std::unique_ptr<DeviceInfo> info = | 562 std::unique_ptr<DeviceInfo> info = |
| 563 service()->GetDeviceInfo(specifics.cache_guid()); | 563 service()->GetDeviceInfo(specifics.cache_guid()); |
| 564 ASSERT_TRUE(info); | 564 ASSERT_TRUE(info); |
| 565 AssertEqual(specifics, *info.get()); | 565 AssertEqual(specifics, *info.get()); |
| 566 | 566 |
| 567 EXPECT_TRUE(processor()->metadata()); | 567 EXPECT_TRUE(processor()->metadata()); |
| 568 EXPECT_EQ(state.encryption_key_name(), | 568 EXPECT_EQ(state.encryption_key_name(), |
| 569 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 569 processor()->metadata()->GetModelTypeState().encryption_key_name()); |
| 570 } | 570 } |
| 571 | 571 |
| 572 TEST_F(DeviceInfoServiceTest, ApplySyncChangesWithLocalGuid) { | 572 TEST_F(DeviceInfoServiceTest, ApplySyncChangesWithLocalGuid) { |
| 573 InitializeAndPumpAndStart(); | 573 InitializeAndPumpAndStart(); |
| 574 | 574 |
| 575 // The point of this test is to try to apply remote changes that have the same | 575 // The point of this test is to try to apply remote changes that have the same |
| 576 // cache guid as the local device. The service should ignore these changes | 576 // cache guid as the local device. The service should ignore these changes |
| 577 // since only it should be performing writes on its data. | 577 // since only it should be performing writes on its data. |
| 578 DeviceInfoSpecifics specifics = | 578 DeviceInfoSpecifics specifics = |
| 579 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); | 579 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 base::Bind(&AssertResultIsSuccess)); | 650 base::Bind(&AssertResultIsSuccess)); |
| 651 | 651 |
| 652 InitializeAndPumpAndStart(); | 652 InitializeAndPumpAndStart(); |
| 653 EXPECT_EQ(1, change_count()); | 653 EXPECT_EQ(1, change_count()); |
| 654 | 654 |
| 655 EntityDataMap remote_input; | 655 EntityDataMap remote_input; |
| 656 remote_input[conflict_remote.cache_guid()] = | 656 remote_input[conflict_remote.cache_guid()] = |
| 657 SpecificsToEntity(conflict_remote); | 657 SpecificsToEntity(conflict_remote); |
| 658 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote); | 658 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote); |
| 659 | 659 |
| 660 DataTypeState state; | 660 ModelTypeState state; |
| 661 state.set_encryption_key_name("ekn"); | 661 state.set_encryption_key_name("ekn"); |
| 662 std::unique_ptr<MetadataChangeList> metadata_changes( | 662 std::unique_ptr<MetadataChangeList> metadata_changes( |
| 663 service()->CreateMetadataChangeList()); | 663 service()->CreateMetadataChangeList()); |
| 664 metadata_changes->UpdateDataTypeState(state); | 664 metadata_changes->UpdateModelTypeState(state); |
| 665 | 665 |
| 666 const SyncError error = | 666 const SyncError error = |
| 667 service()->MergeSyncData(std::move(metadata_changes), remote_input); | 667 service()->MergeSyncData(std::move(metadata_changes), remote_input); |
| 668 EXPECT_FALSE(error.IsSet()); | 668 EXPECT_FALSE(error.IsSet()); |
| 669 EXPECT_EQ(2, change_count()); | 669 EXPECT_EQ(2, change_count()); |
| 670 | 670 |
| 671 // The remote should beat the local in conflict. | 671 // The remote should beat the local in conflict. |
| 672 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size()); | 672 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size()); |
| 673 AssertEqual(unique_local, | 673 AssertEqual(unique_local, |
| 674 *service()->GetDeviceInfo(unique_local.cache_guid()).get()); | 674 *service()->GetDeviceInfo(unique_local.cache_guid()).get()); |
| 675 AssertEqual(unique_remote, | 675 AssertEqual(unique_remote, |
| 676 *service()->GetDeviceInfo(unique_remote.cache_guid()).get()); | 676 *service()->GetDeviceInfo(unique_remote.cache_guid()).get()); |
| 677 AssertEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get()); | 677 AssertEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get()); |
| 678 | 678 |
| 679 // Service should have told the processor about the existance of unique_local. | 679 // Service should have told the processor about the existance of unique_local. |
| 680 EXPECT_TRUE(processor()->delete_set().empty()); | 680 EXPECT_TRUE(processor()->delete_set().empty()); |
| 681 EXPECT_EQ(2u, processor()->put_map().size()); | 681 EXPECT_EQ(2u, processor()->put_map().size()); |
| 682 const auto& it = processor()->put_map().find(unique_local.cache_guid()); | 682 const auto& it = processor()->put_map().find(unique_local.cache_guid()); |
| 683 ASSERT_NE(processor()->put_map().end(), it); | 683 ASSERT_NE(processor()->put_map().end(), it); |
| 684 AssertEqual(unique_local, it->second->specifics.device_info()); | 684 AssertEqual(unique_local, it->second->specifics.device_info()); |
| 685 | 685 |
| 686 RestartService(); | 686 RestartService(); |
| 687 ASSERT_EQ(state.encryption_key_name(), | 687 ASSERT_EQ(state.encryption_key_name(), |
| 688 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 688 processor()->metadata()->GetModelTypeState().encryption_key_name()); |
| 689 } | 689 } |
| 690 | 690 |
| 691 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { | 691 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { |
| 692 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); | 692 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); |
| 693 std::unique_ptr<DeviceInfoSpecifics> specifics( | 693 std::unique_ptr<DeviceInfoSpecifics> specifics( |
| 694 CopyToSpecifics(*local_device_info)); | 694 CopyToSpecifics(*local_device_info)); |
| 695 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); | 695 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); |
| 696 const std::string guid = local_device_info->guid(); | 696 const std::string guid = local_device_info->guid(); |
| 697 | 697 |
| 698 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 698 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 specifics.set_cache_guid("non-local"); | 748 specifics.set_cache_guid("non-local"); |
| 749 PushBackEntityChangeAdd(specifics, &change_list); | 749 PushBackEntityChangeAdd(specifics, &change_list); |
| 750 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), | 750 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), |
| 751 change_list); | 751 change_list); |
| 752 EXPECT_EQ(1, service()->CountActiveDevices()); | 752 EXPECT_EQ(1, service()->CountActiveDevices()); |
| 753 } | 753 } |
| 754 | 754 |
| 755 } // namespace | 755 } // namespace |
| 756 | 756 |
| 757 } // namespace syncer | 757 } // namespace syncer |
| OLD | NEW |