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