Chromium Code Reviews| 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_driver/device_info_service.h" | 5 #include "components/sync_driver/device_info_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "components/sync_driver/local_device_info_provider_mock.h" | 17 #include "components/sync_driver/local_device_info_provider_mock.h" |
| 18 #include "sync/api/data_batch.h" | 18 #include "sync/api/data_batch.h" |
| 19 #include "sync/api/metadata_batch.h" | 19 #include "sync/api/metadata_batch.h" |
| 20 #include "sync/api/model_type_store.h" | 20 #include "sync/api/model_type_store.h" |
| 21 #include "sync/internal_api/public/shared_model_type_processor.h" | |
| 21 #include "sync/internal_api/public/test/model_type_store_test_util.h" | 22 #include "sync/internal_api/public/test/model_type_store_test_util.h" |
| 22 #include "sync/protocol/data_type_state.pb.h" | 23 #include "sync/protocol/data_type_state.pb.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace sync_driver_v2 { | 26 namespace sync_driver_v2 { |
| 26 | 27 |
| 27 using syncer::SyncError; | 28 using syncer::SyncError; |
| 28 using syncer_v2::DataBatch; | 29 using syncer_v2::DataBatch; |
| 29 using syncer_v2::EntityChange; | 30 using syncer_v2::EntityChange; |
| 30 using syncer_v2::EntityChangeList; | 31 using syncer_v2::EntityChangeList; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 // and that we saw everything we expected. | 95 // and that we saw everything we expected. |
| 95 expected.erase(iter); | 96 expected.erase(iter); |
| 96 } | 97 } |
| 97 ASSERT_TRUE(expected.empty()); | 98 ASSERT_TRUE(expected.empty()); |
| 98 } | 99 } |
| 99 | 100 |
| 100 // Instead of actually processing anything, simply accumulates all instructions | 101 // Instead of actually processing anything, simply accumulates all instructions |
| 101 // in members that can then be accessed. TODO(skym): If this ends up being | 102 // in members that can then be accessed. TODO(skym): If this ends up being |
| 102 // useful for other model type unittests it should be moved out to a shared | 103 // useful for other model type unittests it should be moved out to a shared |
| 103 // location. | 104 // location. |
| 104 class FakeModelTypeChangeProcessor : public ModelTypeChangeProcessor { | 105 class FakeSharedModelTypeProcessor |
| 106 : public syncer_v2::SharedModelTypeProcessor { | |
| 105 public: | 107 public: |
| 106 FakeModelTypeChangeProcessor() {} | 108 FakeSharedModelTypeProcessor(syncer::ModelType type, |
| 107 ~FakeModelTypeChangeProcessor() override {} | 109 ModelTypeService* service) |
| 110 : SharedModelTypeProcessor(type, service) {} | |
| 111 ~FakeSharedModelTypeProcessor() override {} | |
| 108 | 112 |
| 109 void Put(const std::string& client_tag, | 113 void Put(const std::string& client_tag, |
| 110 scoped_ptr<EntityData> entity_data, | 114 scoped_ptr<EntityData> entity_data, |
| 111 MetadataChangeList* metadata_changes) override { | 115 MetadataChangeList* metadata_changes) override { |
| 112 put_map_.insert(std::make_pair(client_tag, std::move(entity_data))); | 116 put_map_.insert(std::make_pair(client_tag, std::move(entity_data))); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void Delete(const std::string& client_tag, | 119 void Delete(const std::string& client_tag, |
| 116 MetadataChangeList* metadata_changes) override { | 120 MetadataChangeList* metadata_changes) override { |
| 117 delete_set_.insert(client_tag); | 121 delete_set_.insert(client_tag); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 128 const MetadataBatch* metadata() const { return metadata_.get(); } | 132 const MetadataBatch* metadata() const { return metadata_.get(); } |
| 129 | 133 |
| 130 private: | 134 private: |
| 131 std::map<std::string, scoped_ptr<EntityData>> put_map_; | 135 std::map<std::string, scoped_ptr<EntityData>> put_map_; |
| 132 std::set<std::string> delete_set_; | 136 std::set<std::string> delete_set_; |
| 133 scoped_ptr<MetadataBatch> metadata_; | 137 scoped_ptr<MetadataBatch> metadata_; |
| 134 }; | 138 }; |
| 135 | 139 |
| 136 } // namespace | 140 } // namespace |
| 137 | 141 |
| 142 class FakeDeviceInfoService : public DeviceInfoService { | |
| 143 public: | |
| 144 FakeDeviceInfoService( | |
| 145 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, | |
| 146 const StoreFactoryFunction& callback) | |
| 147 : DeviceInfoService(local_device_info_provider, callback){}; | |
| 148 ~FakeDeviceInfoService() override{}; | |
| 149 | |
| 150 void set_processor(FakeSharedModelTypeProcessor* processor) { | |
| 151 processor_ = processor; | |
| 152 } | |
| 153 | |
| 154 protected: | |
| 155 scoped_ptr<syncer_v2::SharedModelTypeProcessor> | |
| 156 CreateSharedModelTypeProcessor(syncer::ModelType type) override { | |
| 157 return make_scoped_ptr(processor_); | |
| 158 } | |
| 159 | |
| 160 private: | |
| 161 FakeSharedModelTypeProcessor* processor_ = nullptr; | |
| 162 }; | |
| 163 | |
| 138 class DeviceInfoServiceTest : public testing::Test, | 164 class DeviceInfoServiceTest : public testing::Test, |
| 139 public DeviceInfoTracker::Observer { | 165 public DeviceInfoTracker::Observer { |
| 140 protected: | 166 protected: |
| 141 ~DeviceInfoServiceTest() override { | 167 ~DeviceInfoServiceTest() override { |
| 142 // Some tests may never initialize the service. | 168 // Some tests may never initialize the service. |
| 143 if (service_) | 169 if (service_) |
| 144 service_->RemoveObserver(this); | 170 service_->RemoveObserver(this); |
| 145 | 171 |
| 146 // Force all remaining (store) tasks to execute so we don't leak memory. | 172 // Force all remaining (store) tasks to execute so we don't leak memory. |
| 147 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 158 "client_1", | 184 "client_1", |
| 159 "Chromium 10k", | 185 "Chromium 10k", |
| 160 "Chrome 10k", | 186 "Chrome 10k", |
| 161 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | 187 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, |
| 162 "device_id")) {} | 188 "device_id")) {} |
| 163 | 189 |
| 164 // Initialized the service based on the current local device and store. Can | 190 // Initialized the service based on the current local device and store. Can |
| 165 // only be called once per run, as it passes |store_|. | 191 // only be called once per run, as it passes |store_|. |
| 166 void InitializeService() { | 192 void InitializeService() { |
| 167 ASSERT_TRUE(store_); | 193 ASSERT_TRUE(store_); |
| 168 service_.reset(new DeviceInfoService( | 194 service_.reset(new FakeDeviceInfoService( |
| 169 local_device_.get(), | 195 local_device_.get(), |
| 170 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, | 196 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, |
| 171 base::Passed(&store_)))); | 197 base::Passed(&store_)))); |
| 172 service_->AddObserver(this); | 198 service_->AddObserver(this); |
| 173 } | 199 } |
| 174 | 200 |
| 175 // Creates the service and runs any outstanding tasks. This will typically | 201 // Creates the service and runs any outstanding tasks. This will typically |
| 176 // cause all initialization callbacks between the sevice and store to fire. | 202 // cause all initialization callbacks between the sevice and store to fire. |
| 177 void InitializeAndPump() { | 203 void InitializeAndPump() { |
| 178 InitializeService(); | 204 InitializeService(); |
| 179 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
| 180 } | 206 } |
| 181 | 207 |
| 182 // Creates a new processor and sets it on the service. We typically need to | 208 // Creates a new processor and sets it on the service. We typically need to |
| 183 // pump in this scenario because metadata is going to need to be loading from | 209 // pump in this scenario because metadata is going to need to be loading from |
| 184 // the store and given to the processor, which is async. | 210 // the store and given to the processor, which is async. |
| 185 void SetProcessorAndPump() { | 211 void SetProcessorAndPump() { |
|
skym
2016/03/04 22:12:28
I don't think we need this method anymore. Origina
Gang Wu
2016/03/08 20:35:24
Will update where to trigger initial processor in
| |
| 186 processor_ = new FakeModelTypeChangeProcessor(); | 212 processor_ = |
| 187 service()->set_change_processor(make_scoped_ptr(processor_)); | 213 new FakeSharedModelTypeProcessor(syncer::DEVICE_INFO, service()); |
| 214 service()->set_processor(processor_); | |
| 215 service()->InitializeProcessor(syncer::DEVICE_INFO); | |
| 188 base::RunLoop().RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
| 189 } | 217 } |
| 190 | 218 |
| 191 // Generates a specifics object with slightly differing values. Will generate | 219 // Generates a specifics object with slightly differing values. Will generate |
| 192 // the same values on each run of a test because a simple counter is used to | 220 // the same values on each run of a test because a simple counter is used to |
| 193 // vary field values. | 221 // vary field values. |
| 194 DeviceInfoSpecifics GenerateTestSpecifics() { | 222 DeviceInfoSpecifics GenerateTestSpecifics() { |
| 195 int label = ++generated_count_; | 223 int label = ++generated_count_; |
| 196 DeviceInfoSpecifics specifics; | 224 DeviceInfoSpecifics specifics; |
| 197 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); | 225 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 int change_count() { return change_count_; } | 264 int change_count() { return change_count_; } |
| 237 | 265 |
| 238 // Allows overriding the provider before the service is initialized. | 266 // Allows overriding the provider before the service is initialized. |
| 239 void set_local_device(scoped_ptr<LocalDeviceInfoProviderMock> provider) { | 267 void set_local_device(scoped_ptr<LocalDeviceInfoProviderMock> provider) { |
| 240 ASSERT_FALSE(service_); | 268 ASSERT_FALSE(service_); |
| 241 std::swap(local_device_, provider); | 269 std::swap(local_device_, provider); |
| 242 } | 270 } |
| 243 LocalDeviceInfoProviderMock* local_device() { return local_device_.get(); } | 271 LocalDeviceInfoProviderMock* local_device() { return local_device_.get(); } |
| 244 | 272 |
| 245 // Allows access to the service after InitializeService() is called. | 273 // Allows access to the service after InitializeService() is called. |
| 246 DeviceInfoService* service() { | 274 FakeDeviceInfoService* service() { |
| 247 EXPECT_TRUE(service_); | 275 EXPECT_TRUE(service_); |
| 248 return service_.get(); | 276 return service_.get(); |
| 249 } | 277 } |
| 250 | 278 |
| 251 FakeModelTypeChangeProcessor* processor() { | 279 FakeSharedModelTypeProcessor* processor() { |
| 252 EXPECT_TRUE(processor_); | 280 EXPECT_TRUE(processor_); |
| 253 return processor_; | 281 return processor_; |
| 254 } | 282 } |
| 255 | 283 |
| 256 // Should only be called after the service has been initialized. Will first | 284 // Should only be called after the service has been initialized. Will first |
| 257 // recover the service's store, so another can be initialized later, and then | 285 // recover the service's store, so another can be initialized later, and then |
| 258 // deletes the service. | 286 // deletes the service. |
| 259 void ShutdownService() { | 287 void ShutdownService() { |
| 260 ASSERT_TRUE(service_); | 288 ASSERT_TRUE(service_); |
| 261 std::swap(store_, service_->store_); | 289 std::swap(store_, service_->store_); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 272 // CreateInMemoryStoreForTest. | 300 // CreateInMemoryStoreForTest. |
| 273 base::MessageLoop message_loop_; | 301 base::MessageLoop message_loop_; |
| 274 | 302 |
| 275 // Holds the store while the service is not initialized. | 303 // Holds the store while the service is not initialized. |
| 276 scoped_ptr<ModelTypeStore> store_; | 304 scoped_ptr<ModelTypeStore> store_; |
| 277 | 305 |
| 278 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; | 306 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; |
| 279 | 307 |
| 280 // Not initialized immediately (upon test's constructor). This allows each | 308 // Not initialized immediately (upon test's constructor). This allows each |
| 281 // test case to modify the dependencies the service will be constructed with. | 309 // test case to modify the dependencies the service will be constructed with. |
| 282 scoped_ptr<DeviceInfoService> service_; | 310 scoped_ptr<FakeDeviceInfoService> service_; |
| 283 | 311 |
| 284 // A non-owning pointer to the processor given to the service. Will be nullptr | 312 // A non-owning pointer to the processor given to the service. Will be nullptr |
| 285 // before being given to the service, to make ownership easier. | 313 // before being given to the service, to make ownership easier. |
| 286 FakeModelTypeChangeProcessor* processor_ = nullptr; | 314 FakeSharedModelTypeProcessor* processor_ = nullptr; |
| 287 | 315 |
| 288 // A monotonically increasing label for generated specifics objects with data | 316 // A monotonically increasing label for generated specifics objects with data |
| 289 // that is slightly different from eachother. | 317 // that is slightly different from eachother. |
| 290 int generated_count_ = 0; | 318 int generated_count_ = 0; |
| 291 }; | 319 }; |
| 292 | 320 |
| 293 namespace { | 321 namespace { |
| 294 | 322 |
| 295 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) { | 323 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) { |
| 296 InitializeService(); | 324 InitializeService(); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 delete_changes.push_back(EntityChange::CreateDelete("tag")); | 584 delete_changes.push_back(EntityChange::CreateDelete("tag")); |
| 557 const SyncError error = service()->ApplySyncChanges( | 585 const SyncError error = service()->ApplySyncChanges( |
| 558 service()->CreateMetadataChangeList(), delete_changes); | 586 service()->CreateMetadataChangeList(), delete_changes); |
| 559 EXPECT_FALSE(error.IsSet()); | 587 EXPECT_FALSE(error.IsSet()); |
| 560 EXPECT_EQ(0, change_count()); | 588 EXPECT_EQ(0, change_count()); |
| 561 } | 589 } |
| 562 | 590 |
| 563 } // namespace | 591 } // namespace |
| 564 | 592 |
| 565 } // namespace sync_driver_v2 | 593 } // namespace sync_driver_v2 |
| OLD | NEW |