| 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 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // useful for other model type unittests it should be moved out to a shared | 121 // useful for other model type unittests it should be moved out to a shared |
| 122 // location. | 122 // location. |
| 123 class RecordingModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { | 123 class RecordingModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { |
| 124 public: | 124 public: |
| 125 RecordingModelTypeChangeProcessor() {} | 125 RecordingModelTypeChangeProcessor() {} |
| 126 ~RecordingModelTypeChangeProcessor() override {} | 126 ~RecordingModelTypeChangeProcessor() override {} |
| 127 | 127 |
| 128 void Put(const std::string& storage_key, | 128 void Put(const std::string& storage_key, |
| 129 std::unique_ptr<EntityData> entity_data, | 129 std::unique_ptr<EntityData> entity_data, |
| 130 MetadataChangeList* metadata_changes) override { | 130 MetadataChangeList* metadata_changes) override { |
| 131 put_map_.insert(std::make_pair(storage_key, std::move(entity_data))); | 131 put_multimap_.insert(std::make_pair(storage_key, std::move(entity_data))); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void Delete(const std::string& storage_key, | 134 void Delete(const std::string& storage_key, |
| 135 MetadataChangeList* metadata_changes) override { | 135 MetadataChangeList* metadata_changes) override { |
| 136 delete_set_.insert(storage_key); | 136 delete_set_.insert(storage_key); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void OnMetadataLoaded(SyncError error, | 139 void OnMetadataLoaded(SyncError error, |
| 140 std::unique_ptr<MetadataBatch> batch) override { | 140 std::unique_ptr<MetadataBatch> batch) override { |
| 141 std::swap(metadata_, batch); | 141 std::swap(metadata_, batch); |
| 142 } | 142 } |
| 143 | 143 |
| 144 const std::map<std::string, std::unique_ptr<EntityData>>& put_map() const { | 144 const std::multimap<std::string, std::unique_ptr<EntityData>>& put_multimap() |
| 145 return put_map_; | 145 const { |
| 146 return put_multimap_; |
| 146 } | 147 } |
| 147 const std::set<std::string>& delete_set() const { return delete_set_; } | 148 const std::set<std::string>& delete_set() const { return delete_set_; } |
| 148 const MetadataBatch* metadata() const { return metadata_.get(); } | 149 const MetadataBatch* metadata() const { return metadata_.get(); } |
| 149 | 150 |
| 150 private: | 151 private: |
| 151 std::map<std::string, std::unique_ptr<EntityData>> put_map_; | 152 std::multimap<std::string, std::unique_ptr<EntityData>> put_multimap_; |
| 152 std::set<std::string> delete_set_; | 153 std::set<std::string> delete_set_; |
| 153 std::unique_ptr<MetadataBatch> metadata_; | 154 std::unique_ptr<MetadataBatch> metadata_; |
| 154 }; | 155 }; |
| 155 | 156 |
| 156 } // namespace | 157 } // namespace |
| 157 | 158 |
| 158 class DeviceInfoServiceTest : public testing::Test, | 159 class DeviceInfoServiceTest : public testing::Test, |
| 159 public DeviceInfoTracker::Observer { | 160 public DeviceInfoTracker::Observer { |
| 160 protected: | 161 protected: |
| 161 DeviceInfoServiceTest() | 162 DeviceInfoServiceTest() |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 std::swap(store_, service_->store_); | 271 std::swap(store_, service_->store_); |
| 271 service_->RemoveObserver(this); | 272 service_->RemoveObserver(this); |
| 272 service_.reset(); | 273 service_.reset(); |
| 273 } | 274 } |
| 274 | 275 |
| 275 void RestartService() { | 276 void RestartService() { |
| 276 PumpAndShutdown(); | 277 PumpAndShutdown(); |
| 277 InitializeAndPump(); | 278 InitializeAndPump(); |
| 278 } | 279 } |
| 279 | 280 |
| 281 void ForcePulse() { service()->SendLocalData(); } |
| 282 |
| 280 Time GetLastUpdateTime(const DeviceInfoSpecifics& specifics) { | 283 Time GetLastUpdateTime(const DeviceInfoSpecifics& specifics) { |
| 281 return DeviceInfoService::GetLastUpdateTime(specifics); | 284 return DeviceInfoService::GetLastUpdateTime(specifics); |
| 282 } | 285 } |
| 283 | 286 |
| 284 private: | 287 private: |
| 285 int change_count_; | 288 int change_count_; |
| 286 | 289 |
| 287 // In memory model type store needs a MessageLoop. | 290 // In memory model type store needs a MessageLoop. |
| 288 base::MessageLoop message_loop_; | 291 base::MessageLoop message_loop_; |
| 289 | 292 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 394 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 392 ModelTypeState state; | 395 ModelTypeState state; |
| 393 state.set_encryption_key_name("ekn"); | 396 state.set_encryption_key_name("ekn"); |
| 394 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); | 397 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); |
| 395 store()->CommitWriteBatch(std::move(batch), | 398 store()->CommitWriteBatch(std::move(batch), |
| 396 base::Bind(&AssertResultIsSuccess)); | 399 base::Bind(&AssertResultIsSuccess)); |
| 397 InitializeAndPump(); | 400 InitializeAndPump(); |
| 398 DeviceInfoList devices = service()->GetAllDeviceInfo(); | 401 DeviceInfoList devices = service()->GetAllDeviceInfo(); |
| 399 ASSERT_EQ(1u, devices.size()); | 402 ASSERT_EQ(1u, devices.size()); |
| 400 ASSERT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); | 403 ASSERT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); |
| 401 EXPECT_EQ(1u, processor()->put_map().size()); | 404 EXPECT_EQ(1u, processor()->put_multimap().size()); |
| 402 } | 405 } |
| 403 | 406 |
| 404 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) { | 407 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) { |
| 405 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 408 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 406 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); | 409 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); |
| 407 store()->WriteData(batch.get(), specifics.cache_guid(), | 410 store()->WriteData(batch.get(), specifics.cache_guid(), |
| 408 specifics.SerializeAsString()); | 411 specifics.SerializeAsString()); |
| 409 ModelTypeState state; | 412 ModelTypeState state; |
| 410 state.set_encryption_key_name("ekn"); | 413 state.set_encryption_key_name("ekn"); |
| 411 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); | 414 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); | 561 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); |
| 559 EntityChangeList change_list; | 562 EntityChangeList change_list; |
| 560 PushBackEntityChangeAdd(specifics, &change_list); | 563 PushBackEntityChangeAdd(specifics, &change_list); |
| 561 | 564 |
| 562 // Should have a single change from reconciliation. | 565 // Should have a single change from reconciliation. |
| 563 EXPECT_TRUE( | 566 EXPECT_TRUE( |
| 564 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); | 567 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); |
| 565 EXPECT_EQ(1, change_count()); | 568 EXPECT_EQ(1, change_count()); |
| 566 // Ensure |last_updated| is about now, plus or minus a little bit. | 569 // Ensure |last_updated| is about now, plus or minus a little bit. |
| 567 Time last_updated(ProtoTimeToTime(processor() | 570 Time last_updated(ProtoTimeToTime(processor() |
| 568 ->put_map() | 571 ->put_multimap() |
| 569 .begin() | 572 .begin() |
| 570 ->second->specifics.device_info() | 573 ->second->specifics.device_info() |
| 571 .last_updated_timestamp())); | 574 .last_updated_timestamp())); |
| 572 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated); | 575 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated); |
| 573 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated); | 576 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated); |
| 574 | 577 |
| 575 EXPECT_FALSE( | 578 EXPECT_FALSE( |
| 576 service() | 579 service() |
| 577 ->ApplySyncChanges(service()->CreateMetadataChangeList(), change_list) | 580 ->ApplySyncChanges(service()->CreateMetadataChangeList(), change_list) |
| 578 .IsSet()); | 581 .IsSet()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 598 EXPECT_EQ(1, change_count()); | 601 EXPECT_EQ(1, change_count()); |
| 599 } | 602 } |
| 600 | 603 |
| 601 TEST_F(DeviceInfoServiceTest, MergeEmpty) { | 604 TEST_F(DeviceInfoServiceTest, MergeEmpty) { |
| 602 InitializeAndPump(); | 605 InitializeAndPump(); |
| 603 EXPECT_EQ(1, change_count()); | 606 EXPECT_EQ(1, change_count()); |
| 604 const SyncError error = service()->MergeSyncData( | 607 const SyncError error = service()->MergeSyncData( |
| 605 service()->CreateMetadataChangeList(), EntityDataMap()); | 608 service()->CreateMetadataChangeList(), EntityDataMap()); |
| 606 EXPECT_FALSE(error.IsSet()); | 609 EXPECT_FALSE(error.IsSet()); |
| 607 EXPECT_EQ(1, change_count()); | 610 EXPECT_EQ(1, change_count()); |
| 608 EXPECT_EQ(1u, processor()->put_map().size()); | 611 // TODO(skym): Stop sending local twice. The first of the two puts will |
| 612 // probably happen before the processor is tracking metadata yet, and so there |
| 613 // should not be much overhead. |
| 614 EXPECT_EQ(2u, processor()->put_multimap().size()); |
| 615 EXPECT_EQ(2u, processor()->put_multimap().count( |
| 616 local_device()->GetLocalDeviceInfo()->guid())); |
| 609 EXPECT_EQ(0u, processor()->delete_set().size()); | 617 EXPECT_EQ(0u, processor()->delete_set().size()); |
| 610 } | 618 } |
| 611 | 619 |
| 612 TEST_F(DeviceInfoServiceTest, MergeWithData) { | 620 TEST_F(DeviceInfoServiceTest, MergeWithData) { |
| 613 const std::string conflict_guid = "conflict_guid"; | 621 const std::string conflict_guid = "conflict_guid"; |
| 614 const DeviceInfoSpecifics unique_local = | 622 const DeviceInfoSpecifics unique_local = |
| 615 GenerateTestSpecifics("unique_local_guid"); | 623 GenerateTestSpecifics("unique_local_guid"); |
| 616 const DeviceInfoSpecifics conflict_local = | 624 const DeviceInfoSpecifics conflict_local = |
| 617 GenerateTestSpecifics(conflict_guid); | 625 GenerateTestSpecifics(conflict_guid); |
| 618 const DeviceInfoSpecifics conflict_remote = | 626 const DeviceInfoSpecifics conflict_remote = |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 // The remote should beat the local in conflict. | 658 // The remote should beat the local in conflict. |
| 651 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size()); | 659 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size()); |
| 652 AssertEqual(unique_local, | 660 AssertEqual(unique_local, |
| 653 *service()->GetDeviceInfo(unique_local.cache_guid()).get()); | 661 *service()->GetDeviceInfo(unique_local.cache_guid()).get()); |
| 654 AssertEqual(unique_remote, | 662 AssertEqual(unique_remote, |
| 655 *service()->GetDeviceInfo(unique_remote.cache_guid()).get()); | 663 *service()->GetDeviceInfo(unique_remote.cache_guid()).get()); |
| 656 AssertEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get()); | 664 AssertEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get()); |
| 657 | 665 |
| 658 // Service should have told the processor about the existance of unique_local. | 666 // Service should have told the processor about the existance of unique_local. |
| 659 EXPECT_TRUE(processor()->delete_set().empty()); | 667 EXPECT_TRUE(processor()->delete_set().empty()); |
| 660 EXPECT_EQ(2u, processor()->put_map().size()); | 668 EXPECT_EQ(3u, processor()->put_multimap().size()); |
| 661 const auto& it = processor()->put_map().find(unique_local.cache_guid()); | 669 EXPECT_EQ(1u, processor()->put_multimap().count(unique_local.cache_guid())); |
| 662 ASSERT_NE(processor()->put_map().end(), it); | 670 const auto& it = processor()->put_multimap().find(unique_local.cache_guid()); |
| 671 ASSERT_NE(processor()->put_multimap().end(), it); |
| 663 AssertEqual(unique_local, it->second->specifics.device_info()); | 672 AssertEqual(unique_local, it->second->specifics.device_info()); |
| 664 | 673 |
| 665 RestartService(); | 674 RestartService(); |
| 666 ASSERT_EQ(state.encryption_key_name(), | 675 ASSERT_EQ(state.encryption_key_name(), |
| 667 processor()->metadata()->GetModelTypeState().encryption_key_name()); | 676 processor()->metadata()->GetModelTypeState().encryption_key_name()); |
| 668 } | 677 } |
| 669 | 678 |
| 670 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { | 679 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { |
| 671 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); | 680 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); |
| 672 std::unique_ptr<DeviceInfoSpecifics> specifics = | 681 std::unique_ptr<DeviceInfoSpecifics> specifics = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 683 | 692 |
| 684 EntityDataMap remote_input; | 693 EntityDataMap remote_input; |
| 685 remote_input[guid] = SpecificsToEntity(*specifics); | 694 remote_input[guid] = SpecificsToEntity(*specifics); |
| 686 | 695 |
| 687 const SyncError error = service()->MergeSyncData( | 696 const SyncError error = service()->MergeSyncData( |
| 688 service()->CreateMetadataChangeList(), remote_input); | 697 service()->CreateMetadataChangeList(), remote_input); |
| 689 EXPECT_FALSE(error.IsSet()); | 698 EXPECT_FALSE(error.IsSet()); |
| 690 EXPECT_EQ(0, change_count()); | 699 EXPECT_EQ(0, change_count()); |
| 691 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); | 700 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); |
| 692 EXPECT_TRUE(processor()->delete_set().empty()); | 701 EXPECT_TRUE(processor()->delete_set().empty()); |
| 693 EXPECT_TRUE(processor()->put_map().empty()); | 702 EXPECT_TRUE(processor()->put_multimap().empty()); |
| 694 } | 703 } |
| 695 | 704 |
| 696 TEST_F(DeviceInfoServiceTest, GetLastUpdateTime) { | 705 TEST_F(DeviceInfoServiceTest, GetLastUpdateTime) { |
| 697 Time time1(Time() + TimeDelta::FromDays(1)); | 706 Time time1(Time() + TimeDelta::FromDays(1)); |
| 698 | 707 |
| 699 DeviceInfoSpecifics specifics1 = GenerateTestSpecifics(); | 708 DeviceInfoSpecifics specifics1 = GenerateTestSpecifics(); |
| 700 DeviceInfoSpecifics specifics2 = GenerateTestSpecifics(); | 709 DeviceInfoSpecifics specifics2 = GenerateTestSpecifics(); |
| 701 specifics2.set_last_updated_timestamp(TimeToProtoTime(time1)); | 710 specifics2.set_last_updated_timestamp(TimeToProtoTime(time1)); |
| 702 | 711 |
| 703 EXPECT_EQ(Time(), GetLastUpdateTime(specifics1)); | 712 EXPECT_EQ(Time(), GetLastUpdateTime(specifics1)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 const MetadataBatch* metadata = processor()->metadata(); | 751 const MetadataBatch* metadata = processor()->metadata(); |
| 743 EXPECT_NE(nullptr, metadata); | 752 EXPECT_NE(nullptr, metadata); |
| 744 | 753 |
| 745 // Pointer address of metadata should remain constant because the processor | 754 // Pointer address of metadata should remain constant because the processor |
| 746 // should not have been given new metadata. | 755 // should not have been given new metadata. |
| 747 local_device()->Initialize(CreateDeviceInfo()); | 756 local_device()->Initialize(CreateDeviceInfo()); |
| 748 base::RunLoop().RunUntilIdle(); | 757 base::RunLoop().RunUntilIdle(); |
| 749 EXPECT_EQ(metadata, processor()->metadata()); | 758 EXPECT_EQ(metadata, processor()->metadata()); |
| 750 } | 759 } |
| 751 | 760 |
| 761 TEST_F(DeviceInfoServiceTest, SendLocalData) { |
| 762 InitializeAndPump(); |
| 763 EXPECT_EQ(1, service()->CountActiveDevices()); |
| 764 EXPECT_EQ(1, change_count()); |
| 765 EXPECT_EQ(1u, processor()->put_multimap().size()); |
| 766 |
| 767 ForcePulse(); |
| 768 EXPECT_EQ(1, service()->CountActiveDevices()); |
| 769 EXPECT_EQ(2, change_count()); |
| 770 EXPECT_EQ(2u, processor()->put_multimap().size()); |
| 771 |
| 772 local_device()->Clear(); |
| 773 ForcePulse(); |
| 774 EXPECT_EQ(1, service()->CountActiveDevices()); |
| 775 EXPECT_EQ(2, change_count()); |
| 776 EXPECT_EQ(2u, processor()->put_multimap().size()); |
| 777 } |
| 778 |
| 752 } // namespace | 779 } // namespace |
| 753 | 780 |
| 754 } // namespace syncer | 781 } // namespace syncer |
| OLD | NEW |