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 <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "components/sync/api/data_batch.h" | 18 #include "components/sync/api/data_batch.h" |
19 #include "components/sync/api/data_type_error_handler_mock.h" | 19 #include "components/sync/api/data_type_error_handler_mock.h" |
20 #include "components/sync/api/entity_data.h" | 20 #include "components/sync/api/entity_data.h" |
21 #include "components/sync/api/fake_model_type_change_processor.h" | 21 #include "components/sync/api/fake_model_type_change_processor.h" |
22 #include "components/sync/api/metadata_batch.h" | 22 #include "components/sync/api/metadata_batch.h" |
23 #include "components/sync/api/model_type_store.h" | 23 #include "components/sync/api/model_type_store.h" |
24 #include "components/sync/base/time.h" | 24 #include "components/sync/base/time.h" |
25 #include "components/sync/core/test/model_type_store_test_util.h" | 25 #include "components/sync/core/test/model_type_store_test_util.h" |
26 #include "components/sync/device_info/local_device_info_provider_mock.h" | 26 #include "components/sync/device_info/local_device_info_provider_mock.h" |
27 #include "components/sync/protocol/data_type_state.pb.h" | 27 #include "components/sync/protocol/data_type_state.pb.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 | 29 |
30 namespace sync_driver_v2 { | 30 namespace syncer { |
31 | 31 |
32 using base::Time; | 32 using base::Time; |
33 using base::TimeDelta; | 33 using base::TimeDelta; |
34 using syncer::SyncError; | |
35 using syncer_v2::DataBatch; | |
36 using syncer_v2::EntityChange; | |
37 using syncer_v2::EntityChangeList; | |
38 using syncer_v2::EntityData; | |
39 using syncer_v2::EntityDataMap; | |
40 using syncer_v2::EntityDataPtr; | |
41 using syncer_v2::MetadataBatch; | |
42 using syncer_v2::MetadataChangeList; | |
43 using syncer_v2::ModelTypeChangeProcessor; | |
44 using syncer_v2::ModelTypeService; | |
45 using syncer_v2::ModelTypeStore; | |
46 using syncer_v2::ModelTypeStoreTestUtil; | |
47 using syncer_v2::KeyAndData; | |
48 using sync_driver::DeviceInfo; | |
49 using sync_driver::DeviceInfoTracker; | |
50 using sync_driver::LocalDeviceInfoProviderMock; | |
51 using sync_pb::DataTypeState; | 34 using sync_pb::DataTypeState; |
52 using sync_pb::DeviceInfoSpecifics; | 35 using sync_pb::DeviceInfoSpecifics; |
53 using sync_pb::EntitySpecifics; | 36 using sync_pb::EntitySpecifics; |
54 | 37 |
55 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; | 38 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; |
56 using StorageKeyList = ModelTypeService::StorageKeyList; | 39 using StorageKeyList = ModelTypeService::StorageKeyList; |
57 using RecordList = ModelTypeStore::RecordList; | 40 using RecordList = ModelTypeStore::RecordList; |
58 using Result = ModelTypeStore::Result; | 41 using Result = ModelTypeStore::Result; |
59 using StartCallback = ModelTypeChangeProcessor::StartCallback; | 42 using StartCallback = ModelTypeChangeProcessor::StartCallback; |
60 using WriteBatch = ModelTypeStore::WriteBatch; | 43 using WriteBatch = ModelTypeStore::WriteBatch; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 EntityChangeList* changes) { | 116 EntityChangeList* changes) { |
134 EntityDataPtr ptr = SpecificsToEntity(specifics); | 117 EntityDataPtr ptr = SpecificsToEntity(specifics); |
135 changes->push_back(EntityChange::CreateAdd(specifics.cache_guid(), ptr)); | 118 changes->push_back(EntityChange::CreateAdd(specifics.cache_guid(), ptr)); |
136 return specifics.cache_guid(); | 119 return specifics.cache_guid(); |
137 } | 120 } |
138 | 121 |
139 // Instead of actually processing anything, simply accumulates all instructions | 122 // Instead of actually processing anything, simply accumulates all instructions |
140 // in members that can then be accessed. TODO(skym): If this ends up being | 123 // in members that can then be accessed. TODO(skym): If this ends up being |
141 // useful for other model type unittests it should be moved out to a shared | 124 // useful for other model type unittests it should be moved out to a shared |
142 // location. | 125 // location. |
143 class RecordingModelTypeChangeProcessor | 126 class RecordingModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { |
144 : public syncer_v2::FakeModelTypeChangeProcessor { | |
145 public: | 127 public: |
146 RecordingModelTypeChangeProcessor() {} | 128 RecordingModelTypeChangeProcessor() {} |
147 ~RecordingModelTypeChangeProcessor() override {} | 129 ~RecordingModelTypeChangeProcessor() override {} |
148 | 130 |
149 void Put(const std::string& storage_key, | 131 void Put(const std::string& storage_key, |
150 std::unique_ptr<EntityData> entity_data, | 132 std::unique_ptr<EntityData> entity_data, |
151 MetadataChangeList* metadata_changes) override { | 133 MetadataChangeList* metadata_changes) override { |
152 put_map_.insert(std::make_pair(storage_key, std::move(entity_data))); | 134 put_map_.insert(std::make_pair(storage_key, std::move(entity_data))); |
153 } | 135 } |
154 | 136 |
155 void Delete(const std::string& storage_key, | 137 void Delete(const std::string& storage_key, |
156 MetadataChangeList* metadata_changes) override { | 138 MetadataChangeList* metadata_changes) override { |
157 delete_set_.insert(storage_key); | 139 delete_set_.insert(storage_key); |
158 } | 140 } |
159 | 141 |
160 void OnMetadataLoaded(syncer::SyncError error, | 142 void OnMetadataLoaded(SyncError error, |
161 std::unique_ptr<MetadataBatch> batch) override { | 143 std::unique_ptr<MetadataBatch> batch) override { |
162 std::swap(metadata_, batch); | 144 std::swap(metadata_, batch); |
163 } | 145 } |
164 | 146 |
165 const std::map<std::string, std::unique_ptr<EntityData>>& put_map() const { | 147 const std::map<std::string, std::unique_ptr<EntityData>>& put_map() const { |
166 return put_map_; | 148 return put_map_; |
167 } | 149 } |
168 const std::set<std::string>& delete_set() const { return delete_set_; } | 150 const std::set<std::string>& delete_set() const { return delete_set_; } |
169 const MetadataBatch* metadata() const { return metadata_.get(); } | 151 const MetadataBatch* metadata() const { return metadata_.get(); } |
170 | 152 |
(...skipping 20 matching lines...) Expand all Loading... |
191 if (service_) | 173 if (service_) |
192 service_->RemoveObserver(this); | 174 service_->RemoveObserver(this); |
193 | 175 |
194 // Force all remaining (store) tasks to execute so we don't leak memory. | 176 // Force all remaining (store) tasks to execute so we don't leak memory. |
195 base::RunLoop().RunUntilIdle(); | 177 base::RunLoop().RunUntilIdle(); |
196 } | 178 } |
197 | 179 |
198 void OnDeviceInfoChange() override { change_count_++; } | 180 void OnDeviceInfoChange() override { change_count_++; } |
199 | 181 |
200 std::unique_ptr<ModelTypeChangeProcessor> CreateModelTypeChangeProcessor( | 182 std::unique_ptr<ModelTypeChangeProcessor> CreateModelTypeChangeProcessor( |
201 syncer::ModelType type, | 183 ModelType type, |
202 ModelTypeService* service) { | 184 ModelTypeService* service) { |
203 processor_ = new RecordingModelTypeChangeProcessor(); | 185 processor_ = new RecordingModelTypeChangeProcessor(); |
204 return base::WrapUnique(processor_); | 186 return base::WrapUnique(processor_); |
205 } | 187 } |
206 | 188 |
207 // Initialized the service based on the current local device and store. Can | 189 // Initialized the service based on the current local device and store. Can |
208 // only be called once per run, as it passes |store_|. | 190 // only be called once per run, as it passes |store_|. |
209 void InitializeService() { | 191 void InitializeService() { |
210 ASSERT_TRUE(store_); | 192 ASSERT_TRUE(store_); |
211 service_.reset(new DeviceInfoService( | 193 service_.reset(new DeviceInfoService( |
212 local_device_.get(), | 194 local_device_.get(), |
213 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, | 195 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, |
214 base::Passed(&store_)), | 196 base::Passed(&store_)), |
215 base::Bind(&DeviceInfoServiceTest::CreateModelTypeChangeProcessor, | 197 base::Bind(&DeviceInfoServiceTest::CreateModelTypeChangeProcessor, |
216 base::Unretained(this)))); | 198 base::Unretained(this)))); |
217 service_->AddObserver(this); | 199 service_->AddObserver(this); |
218 } | 200 } |
219 | 201 |
220 void OnSyncStarting() { | 202 void OnSyncStarting() { |
221 service()->OnSyncStarting( | 203 service()->OnSyncStarting(base::MakeUnique<DataTypeErrorHandlerMock>(), |
222 base::MakeUnique<syncer::DataTypeErrorHandlerMock>(), StartCallback()); | 204 StartCallback()); |
223 } | 205 } |
224 | 206 |
225 // Creates the service and runs any outstanding tasks. This will typically | 207 // Creates the service and runs any outstanding tasks. This will typically |
226 // cause all initialization callbacks between the sevice and store to fire. | 208 // cause all initialization callbacks between the sevice and store to fire. |
227 void InitializeAndPump() { | 209 void InitializeAndPump() { |
228 InitializeService(); | 210 InitializeService(); |
229 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
230 } | 212 } |
231 | 213 |
232 // Creates the service, runs any outstanding tasks, and then indicates to the | 214 // Creates the service, runs any outstanding tasks, and then indicates to the |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 DeviceInfoSpecifics specifics = | 581 DeviceInfoSpecifics specifics = |
600 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); | 582 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); |
601 EntityChangeList change_list; | 583 EntityChangeList change_list; |
602 PushBackEntityChangeAdd(specifics, &change_list); | 584 PushBackEntityChangeAdd(specifics, &change_list); |
603 | 585 |
604 // Should have a single change from reconciliation. | 586 // Should have a single change from reconciliation. |
605 EXPECT_TRUE( | 587 EXPECT_TRUE( |
606 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); | 588 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); |
607 EXPECT_EQ(1, change_count()); | 589 EXPECT_EQ(1, change_count()); |
608 // Ensure |last_updated| is about now, plus or minus a little bit. | 590 // Ensure |last_updated| is about now, plus or minus a little bit. |
609 Time last_updated( | 591 Time last_updated(ProtoTimeToTime(processor() |
610 syncer::ProtoTimeToTime(processor() | 592 ->put_map() |
611 ->put_map() | 593 .begin() |
612 .begin() | 594 ->second->specifics.device_info() |
613 ->second->specifics.device_info() | 595 .last_updated_timestamp())); |
614 .last_updated_timestamp())); | |
615 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated); | 596 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated); |
616 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated); | 597 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated); |
617 | 598 |
618 EXPECT_FALSE( | 599 EXPECT_FALSE( |
619 service() | 600 service() |
620 ->ApplySyncChanges(service()->CreateMetadataChangeList(), change_list) | 601 ->ApplySyncChanges(service()->CreateMetadataChangeList(), change_list) |
621 .IsSet()); | 602 .IsSet()); |
622 EXPECT_EQ(1, change_count()); | 603 EXPECT_EQ(1, change_count()); |
623 | 604 |
624 change_list.clear(); | 605 change_list.clear(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 | 688 |
708 RestartService(); | 689 RestartService(); |
709 ASSERT_EQ(state.encryption_key_name(), | 690 ASSERT_EQ(state.encryption_key_name(), |
710 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 691 processor()->metadata()->GetDataTypeState().encryption_key_name()); |
711 } | 692 } |
712 | 693 |
713 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { | 694 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { |
714 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); | 695 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); |
715 std::unique_ptr<DeviceInfoSpecifics> specifics( | 696 std::unique_ptr<DeviceInfoSpecifics> specifics( |
716 CopyToSpecifics(*local_device_info)); | 697 CopyToSpecifics(*local_device_info)); |
717 specifics->set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); | 698 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); |
718 const std::string guid = local_device_info->guid(); | 699 const std::string guid = local_device_info->guid(); |
719 | 700 |
720 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 701 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
721 store()->WriteData(batch.get(), guid, specifics->SerializeAsString()); | 702 store()->WriteData(batch.get(), guid, specifics->SerializeAsString()); |
722 store()->CommitWriteBatch(std::move(batch), | 703 store()->CommitWriteBatch(std::move(batch), |
723 base::Bind(&AssertResultIsSuccess)); | 704 base::Bind(&AssertResultIsSuccess)); |
724 | 705 |
725 InitializeAndPumpAndStart(); | 706 InitializeAndPumpAndStart(); |
726 | 707 |
727 EntityDataMap remote_input; | 708 EntityDataMap remote_input; |
728 remote_input[guid] = SpecificsToEntity(*specifics); | 709 remote_input[guid] = SpecificsToEntity(*specifics); |
729 | 710 |
730 const SyncError error = service()->MergeSyncData( | 711 const SyncError error = service()->MergeSyncData( |
731 service()->CreateMetadataChangeList(), remote_input); | 712 service()->CreateMetadataChangeList(), remote_input); |
732 EXPECT_FALSE(error.IsSet()); | 713 EXPECT_FALSE(error.IsSet()); |
733 EXPECT_EQ(0, change_count()); | 714 EXPECT_EQ(0, change_count()); |
734 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); | 715 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); |
735 EXPECT_TRUE(processor()->delete_set().empty()); | 716 EXPECT_TRUE(processor()->delete_set().empty()); |
736 EXPECT_TRUE(processor()->put_map().empty()); | 717 EXPECT_TRUE(processor()->put_map().empty()); |
737 } | 718 } |
738 | 719 |
739 TEST_F(DeviceInfoServiceTest, GetLastUpdateTime) { | 720 TEST_F(DeviceInfoServiceTest, GetLastUpdateTime) { |
740 Time time1(Time() + TimeDelta::FromDays(1)); | 721 Time time1(Time() + TimeDelta::FromDays(1)); |
741 | 722 |
742 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); | 723 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); |
743 DeviceInfoSpecifics specifics2(GenerateTestSpecifics()); | 724 DeviceInfoSpecifics specifics2(GenerateTestSpecifics()); |
744 specifics2.set_last_updated_timestamp(syncer::TimeToProtoTime(time1)); | 725 specifics2.set_last_updated_timestamp(TimeToProtoTime(time1)); |
745 | 726 |
746 EXPECT_EQ(Time(), GetLastUpdateTime(specifics1)); | 727 EXPECT_EQ(Time(), GetLastUpdateTime(specifics1)); |
747 EXPECT_EQ(time1, GetLastUpdateTime(specifics2)); | 728 EXPECT_EQ(time1, GetLastUpdateTime(specifics2)); |
748 } | 729 } |
749 | 730 |
750 TEST_F(DeviceInfoServiceTest, CountActiveDevices) { | 731 TEST_F(DeviceInfoServiceTest, CountActiveDevices) { |
751 InitializeAndPump(); | 732 InitializeAndPump(); |
752 EXPECT_EQ(0, service()->CountActiveDevices()); | 733 EXPECT_EQ(0, service()->CountActiveDevices()); |
753 | 734 |
754 DeviceInfoSpecifics specifics = | 735 DeviceInfoSpecifics specifics = |
755 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); | 736 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid()); |
756 EntityChangeList change_list; | 737 EntityChangeList change_list; |
757 PushBackEntityChangeAdd(specifics, &change_list); | 738 PushBackEntityChangeAdd(specifics, &change_list); |
758 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), | 739 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), |
759 change_list); | 740 change_list); |
760 EXPECT_EQ(0, service()->CountActiveDevices()); | 741 EXPECT_EQ(0, service()->CountActiveDevices()); |
761 | 742 |
762 change_list.clear(); | 743 change_list.clear(); |
763 specifics.set_last_updated_timestamp(syncer::TimeToProtoTime(Time::Now())); | 744 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); |
764 PushBackEntityChangeAdd(specifics, &change_list); | 745 PushBackEntityChangeAdd(specifics, &change_list); |
765 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), | 746 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), |
766 change_list); | 747 change_list); |
767 EXPECT_EQ(0, service()->CountActiveDevices()); | 748 EXPECT_EQ(0, service()->CountActiveDevices()); |
768 | 749 |
769 change_list.clear(); | 750 change_list.clear(); |
770 specifics.set_cache_guid("non-local"); | 751 specifics.set_cache_guid("non-local"); |
771 PushBackEntityChangeAdd(specifics, &change_list); | 752 PushBackEntityChangeAdd(specifics, &change_list); |
772 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), | 753 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), |
773 change_list); | 754 change_list); |
774 EXPECT_EQ(1, service()->CountActiveDevices()); | 755 EXPECT_EQ(1, service()->CountActiveDevices()); |
775 } | 756 } |
776 | 757 |
777 } // namespace | 758 } // namespace |
778 | 759 |
779 } // namespace sync_driver_v2 | 760 } // namespace syncer |
OLD | NEW |