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/entity_data.h" | 19 #include "sync/api/entity_data.h" |
20 #include "sync/api/metadata_batch.h" | 20 #include "sync/api/metadata_batch.h" |
21 #include "sync/api/model_type_store.h" | 21 #include "sync/api/model_type_store.h" |
22 #include "sync/internal_api/public/shared_model_type_processor.h" | |
22 #include "sync/internal_api/public/test/model_type_store_test_util.h" | 23 #include "sync/internal_api/public/test/model_type_store_test_util.h" |
23 #include "sync/protocol/data_type_state.pb.h" | 24 #include "sync/protocol/data_type_state.pb.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace sync_driver_v2 { | 27 namespace sync_driver_v2 { |
27 | 28 |
28 using syncer::SyncError; | 29 using syncer::SyncError; |
29 using syncer_v2::DataBatch; | 30 using syncer_v2::DataBatch; |
30 using syncer_v2::EntityChange; | 31 using syncer_v2::EntityChange; |
31 using syncer_v2::EntityChangeList; | 32 using syncer_v2::EntityChangeList; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 // break, so we throw in a junk value and forget about it. | 109 // break, so we throw in a junk value and forget about it. |
109 data.client_tag_hash = "junk"; | 110 data.client_tag_hash = "junk"; |
110 *data.specifics.mutable_device_info() = specifics; | 111 *data.specifics.mutable_device_info() = specifics; |
111 return data.PassToPtr(); | 112 return data.PassToPtr(); |
112 } | 113 } |
113 | 114 |
114 // Instead of actually processing anything, simply accumulates all instructions | 115 // Instead of actually processing anything, simply accumulates all instructions |
115 // in members that can then be accessed. TODO(skym): If this ends up being | 116 // in members that can then be accessed. TODO(skym): If this ends up being |
116 // useful for other model type unittests it should be moved out to a shared | 117 // useful for other model type unittests it should be moved out to a shared |
117 // location. | 118 // location. |
118 class FakeModelTypeChangeProcessor : public ModelTypeChangeProcessor { | 119 class FakeSharedModelTypeProcessor |
120 : public syncer_v2::SharedModelTypeProcessor { | |
maxbogue
2016/03/22 21:40:12
Why is this now an actual SharedModelTypeProcessor
Gang Wu
2016/03/24 15:56:25
Done.
| |
119 public: | 121 public: |
120 FakeModelTypeChangeProcessor() {} | 122 FakeSharedModelTypeProcessor(syncer::ModelType type, |
121 ~FakeModelTypeChangeProcessor() override {} | 123 ModelTypeService* service) |
124 : SharedModelTypeProcessor(type, service) {} | |
125 ~FakeSharedModelTypeProcessor() override {} | |
122 | 126 |
123 void Put(const std::string& client_tag, | 127 void Put(const std::string& client_tag, |
124 scoped_ptr<EntityData> entity_data, | 128 scoped_ptr<EntityData> entity_data, |
125 MetadataChangeList* metadata_changes) override { | 129 MetadataChangeList* metadata_changes) override { |
126 put_map_.insert(std::make_pair(client_tag, std::move(entity_data))); | 130 put_map_.insert(std::make_pair(client_tag, std::move(entity_data))); |
127 } | 131 } |
128 | 132 |
129 void Delete(const std::string& client_tag, | 133 void Delete(const std::string& client_tag, |
130 MetadataChangeList* metadata_changes) override { | 134 MetadataChangeList* metadata_changes) override { |
131 delete_set_.insert(client_tag); | 135 delete_set_.insert(client_tag); |
(...skipping 10 matching lines...) Expand all Loading... | |
142 const MetadataBatch* metadata() const { return metadata_.get(); } | 146 const MetadataBatch* metadata() const { return metadata_.get(); } |
143 | 147 |
144 private: | 148 private: |
145 std::map<std::string, scoped_ptr<EntityData>> put_map_; | 149 std::map<std::string, scoped_ptr<EntityData>> put_map_; |
146 std::set<std::string> delete_set_; | 150 std::set<std::string> delete_set_; |
147 scoped_ptr<MetadataBatch> metadata_; | 151 scoped_ptr<MetadataBatch> metadata_; |
148 }; | 152 }; |
149 | 153 |
150 } // namespace | 154 } // namespace |
151 | 155 |
156 class FakeDeviceInfoService : public DeviceInfoService { | |
maxbogue
2016/03/22 21:40:12
You shouldn't need this class. It's not adding any
Gang Wu
2016/03/24 15:56:25
Done.
| |
157 public: | |
158 FakeDeviceInfoService( | |
159 sync_driver::LocalDeviceInfoProvider* local_device_info_provider, | |
160 const StoreFactoryFunction& callback, | |
161 const SharedProcessorFactory& shared_processor_factory) | |
162 : DeviceInfoService(local_device_info_provider, | |
163 callback, | |
164 shared_processor_factory) {} | |
165 ~FakeDeviceInfoService() override{}; | |
166 ModelTypeChangeProcessor* CreateProcessorForTest() { | |
167 return create_change_processor(); | |
168 } | |
169 }; | |
170 | |
152 class DeviceInfoServiceTest : public testing::Test, | 171 class DeviceInfoServiceTest : public testing::Test, |
153 public DeviceInfoTracker::Observer { | 172 public DeviceInfoTracker::Observer { |
154 protected: | 173 protected: |
155 ~DeviceInfoServiceTest() override { | 174 ~DeviceInfoServiceTest() override { |
156 // Some tests may never initialize the service. | 175 // Some tests may never initialize the service. |
157 if (service_) | 176 if (service_) |
158 service_->RemoveObserver(this); | 177 service_->RemoveObserver(this); |
159 | 178 |
160 // Force all remaining (store) tasks to execute so we don't leak memory. | 179 // Force all remaining (store) tasks to execute so we don't leak memory. |
161 base::RunLoop().RunUntilIdle(); | 180 base::RunLoop().RunUntilIdle(); |
162 } | 181 } |
163 | 182 |
164 void OnDeviceInfoChange() override { change_count_++; } | 183 void OnDeviceInfoChange() override { change_count_++; } |
165 | 184 |
166 protected: | 185 scoped_ptr<ModelTypeChangeProcessor> CreateSharedModelTypeProcessor( |
186 syncer::ModelType type, | |
187 ModelTypeService* service) { | |
188 processor_ = new FakeSharedModelTypeProcessor(syncer::DEVICE_INFO, service); | |
189 return make_scoped_ptr(processor_); | |
190 } | |
191 | |
167 DeviceInfoServiceTest() | 192 DeviceInfoServiceTest() |
168 : change_count_(0), | 193 : change_count_(0), |
169 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), | 194 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), |
170 local_device_(new LocalDeviceInfoProviderMock( | 195 local_device_(new LocalDeviceInfoProviderMock( |
171 "guid_1", | 196 "guid_1", |
172 "client_1", | 197 "client_1", |
173 "Chromium 10k", | 198 "Chromium 10k", |
174 "Chrome 10k", | 199 "Chrome 10k", |
175 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | 200 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, |
176 "device_id")) {} | 201 "device_id")) {} |
177 | 202 |
178 // Initialized the service based on the current local device and store. Can | 203 // Initialized the service based on the current local device and store. Can |
179 // only be called once per run, as it passes |store_|. | 204 // only be called once per run, as it passes |store_|. |
180 void InitializeService() { | 205 void InitializeService() { |
181 ASSERT_TRUE(store_); | 206 ASSERT_TRUE(store_); |
182 service_.reset(new DeviceInfoService( | 207 service_.reset(new FakeDeviceInfoService( |
183 local_device_.get(), | 208 local_device_.get(), |
184 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, | 209 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, |
185 base::Passed(&store_)))); | 210 base::Passed(&store_)), |
211 base::Bind(&DeviceInfoServiceTest::CreateSharedModelTypeProcessor, | |
212 base::Unretained(this)))); | |
186 service_->AddObserver(this); | 213 service_->AddObserver(this); |
187 } | 214 } |
188 | 215 |
189 // Creates the service and runs any outstanding tasks. This will typically | 216 // Creates the service and runs any outstanding tasks. This will typically |
190 // cause all initialization callbacks between the sevice and store to fire. | 217 // cause all initialization callbacks between the sevice and store to fire. |
191 void InitializeAndPump() { | 218 void InitializeAndPump() { |
192 InitializeService(); | 219 InitializeService(); |
193 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
194 } | 221 } |
195 | 222 |
196 // Creates a new processor and sets it on the service. We typically need to | 223 // Creates a new processor and sets it on the service. We typically need to |
197 // pump in this scenario because metadata is going to need to be loading from | 224 // pump in this scenario because metadata is going to need to be loading from |
198 // the store and given to the processor, which is async. | 225 // the store and given to the processor, which is async. |
199 void SetProcessorAndPump() { | 226 void SetProcessorAndPump() { |
200 processor_ = new FakeModelTypeChangeProcessor(); | 227 EXPECT_EQ(processor_, service_->CreateProcessorForTest()); |
201 service()->set_change_processor(make_scoped_ptr(processor_)); | 228 ASSERT_TRUE(processor_); |
202 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
203 } | 230 } |
204 | 231 |
205 // Generates a specifics object with slightly differing values. Will generate | 232 // Generates a specifics object with slightly differing values. Will generate |
206 // the same values on each run of a test because a simple counter is used to | 233 // the same values on each run of a test because a simple counter is used to |
207 // vary field values. | 234 // vary field values. |
208 DeviceInfoSpecifics GenerateTestSpecifics() { | 235 DeviceInfoSpecifics GenerateTestSpecifics() { |
209 int label = ++generated_count_; | 236 int label = ++generated_count_; |
210 DeviceInfoSpecifics specifics; | 237 DeviceInfoSpecifics specifics; |
211 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); | 238 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 std::swap(local_device_, provider); | 283 std::swap(local_device_, provider); |
257 } | 284 } |
258 LocalDeviceInfoProviderMock* local_device() { return local_device_.get(); } | 285 LocalDeviceInfoProviderMock* local_device() { return local_device_.get(); } |
259 | 286 |
260 // Allows access to the service after InitializeService() is called. | 287 // Allows access to the service after InitializeService() is called. |
261 DeviceInfoService* service() { | 288 DeviceInfoService* service() { |
262 EXPECT_TRUE(service_); | 289 EXPECT_TRUE(service_); |
263 return service_.get(); | 290 return service_.get(); |
264 } | 291 } |
265 | 292 |
266 FakeModelTypeChangeProcessor* processor() { | 293 FakeSharedModelTypeProcessor* processor() { |
267 EXPECT_TRUE(processor_); | 294 EXPECT_TRUE(processor_); |
268 return processor_; | 295 return processor_; |
269 } | 296 } |
270 | 297 |
271 // Should only be called after the service has been initialized. Will first | 298 // Should only be called after the service has been initialized. Will first |
272 // recover the service's store, so another can be initialized later, and then | 299 // recover the service's store, so another can be initialized later, and then |
273 // deletes the service. | 300 // deletes the service. |
274 void ShutdownService() { | 301 void ShutdownService() { |
275 ASSERT_TRUE(service_); | 302 ASSERT_TRUE(service_); |
276 std::swap(store_, service_->store_); | 303 std::swap(store_, service_->store_); |
(...skipping 10 matching lines...) Expand all Loading... | |
287 // CreateInMemoryStoreForTest. | 314 // CreateInMemoryStoreForTest. |
288 base::MessageLoop message_loop_; | 315 base::MessageLoop message_loop_; |
289 | 316 |
290 // Holds the store while the service is not initialized. | 317 // Holds the store while the service is not initialized. |
291 scoped_ptr<ModelTypeStore> store_; | 318 scoped_ptr<ModelTypeStore> store_; |
292 | 319 |
293 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; | 320 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; |
294 | 321 |
295 // Not initialized immediately (upon test's constructor). This allows each | 322 // Not initialized immediately (upon test's constructor). This allows each |
296 // test case to modify the dependencies the service will be constructed with. | 323 // test case to modify the dependencies the service will be constructed with. |
297 scoped_ptr<DeviceInfoService> service_; | 324 scoped_ptr<FakeDeviceInfoService> service_; |
298 | 325 |
299 // A non-owning pointer to the processor given to the service. Will be nullptr | 326 // A non-owning pointer to the processor given to the service. Will be nullptr |
300 // before being given to the service, to make ownership easier. | 327 // before being given to the service, to make ownership easier. |
301 FakeModelTypeChangeProcessor* processor_ = nullptr; | 328 FakeSharedModelTypeProcessor* processor_ = nullptr; |
302 | 329 |
303 // A monotonically increasing label for generated specifics objects with data | 330 // A monotonically increasing label for generated specifics objects with data |
304 // that is slightly different from eachother. | 331 // that is slightly different from eachother. |
305 int generated_count_ = 0; | 332 int generated_count_ = 0; |
306 }; | 333 }; |
307 | 334 |
308 namespace { | 335 namespace { |
309 | 336 |
310 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) { | 337 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) { |
311 InitializeService(); | 338 InitializeService(); |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
659 EXPECT_FALSE(error.IsSet()); | 686 EXPECT_FALSE(error.IsSet()); |
660 EXPECT_EQ(0, change_count()); | 687 EXPECT_EQ(0, change_count()); |
661 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); | 688 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); |
662 EXPECT_TRUE(processor()->delete_set().empty()); | 689 EXPECT_TRUE(processor()->delete_set().empty()); |
663 EXPECT_TRUE(processor()->put_map().empty()); | 690 EXPECT_TRUE(processor()->put_map().empty()); |
664 } | 691 } |
665 | 692 |
666 } // namespace | 693 } // namespace |
667 | 694 |
668 } // namespace sync_driver_v2 | 695 } // namespace sync_driver_v2 |
OLD | NEW |