Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: components/sync/device_info/device_info_service_unittest.cc

Issue 2461723002: [Sync] DeviceInfoService static method and error cleanup. (Closed)
Patch Set: removing stdint.h from device_info_service.h Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 23 matching lines...) Expand all
34 34
35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; 35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>;
36 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList; 36 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList;
37 using RecordList = ModelTypeStore::RecordList; 37 using RecordList = ModelTypeStore::RecordList;
38 using Result = ModelTypeStore::Result; 38 using Result = ModelTypeStore::Result;
39 using StartCallback = ModelTypeChangeProcessor::StartCallback; 39 using StartCallback = ModelTypeChangeProcessor::StartCallback;
40 using WriteBatch = ModelTypeStore::WriteBatch; 40 using WriteBatch = ModelTypeStore::WriteBatch;
41 41
42 namespace { 42 namespace {
43 43
44 std::unique_ptr<DeviceInfo> CreateDeviceInfo() { 44 const char kGuidFormat[] = "cache guid %d";
45 const char kClientNameFormat[] = "client name %d";
46 const char kChromeVersionFormat[] = "chrome version %d";
47 const char kSyncUserAgentFormat[] = "sync user agent %d";
48 const char kSigninScopedDeviceIdFormat[] = "signin scoped device id %d";
49 const sync_pb::SyncEnums::DeviceType kDeviceType =
50 sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
51
52 // The |provider_| is first initialized with a model object created with this
53 // suffix. Local suffix can be changed by setting the provider and then
54 // initializing. Remote data should use other suffixes.
55 const int kDefaultLocalSuffix = 0;
56
57 DeviceInfoSpecifics CreateSpecifics(int suffix) {
58 DeviceInfoSpecifics specifics;
59 specifics.set_cache_guid(base::StringPrintf(kGuidFormat, suffix));
60 specifics.set_client_name(base::StringPrintf(kClientNameFormat, suffix));
61 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
62 specifics.set_sync_user_agent(
63 base::StringPrintf(kSyncUserAgentFormat, suffix));
64 specifics.set_chrome_version(
65 base::StringPrintf(kChromeVersionFormat, suffix));
66 specifics.set_signin_scoped_device_id(
67 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix));
68 return specifics;
69 }
70
71 std::unique_ptr<DeviceInfo> CreateModel(int suffix) {
45 return base::MakeUnique<DeviceInfo>( 72 return base::MakeUnique<DeviceInfo>(
46 "guid_1", "client_1", "Chromium 10k", "Chrome 10k", 73 base::StringPrintf(kGuidFormat, suffix),
47 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id"); 74 base::StringPrintf(kClientNameFormat, suffix),
75 base::StringPrintf(kChromeVersionFormat, suffix),
76 base::StringPrintf(kSyncUserAgentFormat, suffix), kDeviceType,
77 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix));
48 } 78 }
49 79
50 void VerifyResultIsSuccess(Result result) { 80 void VerifyResultIsSuccess(Result result) {
51 EXPECT_EQ(Result::SUCCESS, result); 81 EXPECT_EQ(Result::SUCCESS, result);
52 } 82 }
53 83
54 void VerifyEqual(const DeviceInfoSpecifics& s1, const DeviceInfoSpecifics& s2) { 84 void VerifyEqual(const DeviceInfoSpecifics& s1, const DeviceInfoSpecifics& s2) {
55 EXPECT_EQ(s1.cache_guid(), s2.cache_guid()); 85 EXPECT_EQ(s1.cache_guid(), s2.cache_guid());
56 EXPECT_EQ(s1.client_name(), s2.client_name()); 86 EXPECT_EQ(s1.client_name(), s2.client_name());
57 EXPECT_EQ(s1.device_type(), s2.device_type()); 87 EXPECT_EQ(s1.device_type(), s2.device_type());
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 std::set<std::string> delete_set_; 183 std::set<std::string> delete_set_;
154 std::unique_ptr<MetadataBatch> metadata_; 184 std::unique_ptr<MetadataBatch> metadata_;
155 }; 185 };
156 186
157 } // namespace 187 } // namespace
158 188
159 class DeviceInfoServiceTest : public testing::Test, 189 class DeviceInfoServiceTest : public testing::Test,
160 public DeviceInfoTracker::Observer { 190 public DeviceInfoTracker::Observer {
161 protected: 191 protected:
162 DeviceInfoServiceTest() 192 DeviceInfoServiceTest()
163 : change_count_(0), 193 : store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()),
164 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), 194 provider_(new LocalDeviceInfoProviderMock()) {
165 local_device_(new LocalDeviceInfoProviderMock()) { 195 provider_->Initialize(CreateModel(kDefaultLocalSuffix));
166 local_device_->Initialize(CreateDeviceInfo());
167 } 196 }
168 197
169 ~DeviceInfoServiceTest() override { 198 ~DeviceInfoServiceTest() override {
170 // Some tests may never initialize the service. 199 // Some tests may never initialize the service.
171 if (service_) 200 if (service_)
172 service_->RemoveObserver(this); 201 service_->RemoveObserver(this);
173 202
174 // Force all remaining (store) tasks to execute so we don't leak memory. 203 // Force all remaining (store) tasks to execute so we don't leak memory.
175 base::RunLoop().RunUntilIdle(); 204 base::RunLoop().RunUntilIdle();
176 } 205 }
177 206
178 void OnDeviceInfoChange() override { change_count_++; } 207 void OnDeviceInfoChange() override { change_count_++; }
179 208
180 std::unique_ptr<ModelTypeChangeProcessor> CreateModelTypeChangeProcessor( 209 std::unique_ptr<ModelTypeChangeProcessor> CreateModelTypeChangeProcessor(
181 ModelType type, 210 ModelType type,
182 ModelTypeSyncBridge* bridge) { 211 ModelTypeSyncBridge* bridge) {
183 auto processor = base::MakeUnique<RecordingModelTypeChangeProcessor>(); 212 auto processor = base::MakeUnique<RecordingModelTypeChangeProcessor>();
184 processor_ = processor.get(); 213 processor_ = processor.get();
185 return std::move(processor); 214 return std::move(processor);
186 } 215 }
187 216
188 // Initialized the service based on the current local device and store. Can 217 // Initialized the service based on the current local device and store. Can
189 // only be called once per run, as it passes |store_|. 218 // only be called once per run, as it passes |store_|.
190 void InitializeService() { 219 void InitializeService() {
191 ASSERT_TRUE(store_); 220 ASSERT_TRUE(store_);
192 service_ = base::MakeUnique<DeviceInfoService>( 221 service_ = base::MakeUnique<DeviceInfoService>(
193 local_device_.get(), 222 provider_.get(),
194 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, 223 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback,
195 base::Passed(&store_)), 224 base::Passed(&store_)),
196 base::Bind(&DeviceInfoServiceTest::CreateModelTypeChangeProcessor, 225 base::Bind(&DeviceInfoServiceTest::CreateModelTypeChangeProcessor,
197 base::Unretained(this))); 226 base::Unretained(this)));
198 service_->AddObserver(this); 227 service_->AddObserver(this);
199 } 228 }
200 229
201 // Creates the service and runs any outstanding tasks. This will typically 230 // Creates the service and runs any outstanding tasks. This will typically
202 // cause all initialization callbacks between the sevice and store to fire. 231 // cause all initialization callbacks between the sevice and store to fire.
203 void InitializeAndPump() { 232 void InitializeAndPump() {
204 InitializeService(); 233 InitializeService();
205 base::RunLoop().RunUntilIdle(); 234 base::RunLoop().RunUntilIdle();
206 } 235 }
207 236
208 // Generates a specifics object with slightly differing values. Will generate
209 // the same values on each run of a test because a simple counter is used to
210 // vary field values.
211 DeviceInfoSpecifics GenerateTestSpecifics() {
212 int label = ++generated_count_;
213 DeviceInfoSpecifics specifics;
214 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label));
215 specifics.set_client_name(base::StringPrintf("client name %d", label));
216 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
217 specifics.set_sync_user_agent(
218 base::StringPrintf("sync user agent %d", label));
219 specifics.set_chrome_version(
220 base::StringPrintf("chrome version %d", label));
221 specifics.set_signin_scoped_device_id(
222 base::StringPrintf("signin scoped device id %d", label));
223 return specifics;
224 }
225
226 std::unique_ptr<DeviceInfoSpecifics> CopyToSpecifics(const DeviceInfo& info) {
227 return DeviceInfoService::CopyToSpecifics(info);
228 }
229
230 // Override to allow specific cache guids.
231 DeviceInfoSpecifics GenerateTestSpecifics(const std::string& guid) {
232 DeviceInfoSpecifics specifics = GenerateTestSpecifics();
233 specifics.set_cache_guid(guid);
234 return specifics;
235 }
236
237 // Allows access to the store before that will ultimately be used to 237 // Allows access to the store before that will ultimately be used to
238 // initialize the service. 238 // initialize the service.
239 ModelTypeStore* store() { 239 ModelTypeStore* store() {
240 EXPECT_TRUE(store_); 240 EXPECT_TRUE(store_);
241 return store_.get(); 241 return store_.get();
242 } 242 }
243 243
244 // Get the number of times the service notifies observers of changes. 244 // Get the number of times the service notifies observers of changes.
245 int change_count() { return change_count_; } 245 int change_count() { return change_count_; }
246 246
247 // Allows overriding the provider before the service is initialized. 247 // Allows overriding the provider before the service is initialized.
248 void set_local_device(std::unique_ptr<LocalDeviceInfoProviderMock> provider) { 248 void set_provider(std::unique_ptr<LocalDeviceInfoProviderMock> provider) {
249 ASSERT_FALSE(service_); 249 ASSERT_FALSE(service_);
250 std::swap(local_device_, provider); 250 std::swap(provider_, provider);
251 } 251 }
252 LocalDeviceInfoProviderMock* local_device() { return local_device_.get(); } 252 LocalDeviceInfoProviderMock* local_device() { return provider_.get(); }
253 253
254 // Allows access to the service after InitializeService() is called. 254 // Allows access to the service after InitializeService() is called.
255 DeviceInfoService* service() { 255 DeviceInfoService* service() {
256 EXPECT_TRUE(service_); 256 EXPECT_TRUE(service_);
257 return service_.get(); 257 return service_.get();
258 } 258 }
259 259
260 RecordingModelTypeChangeProcessor* processor() { 260 RecordingModelTypeChangeProcessor* processor() {
261 EXPECT_TRUE(processor_); 261 EXPECT_TRUE(processor_);
262 return processor_; 262 return processor_;
(...skipping 10 matching lines...) Expand all
273 service_.reset(); 273 service_.reset();
274 } 274 }
275 275
276 void RestartService() { 276 void RestartService() {
277 PumpAndShutdown(); 277 PumpAndShutdown();
278 InitializeAndPump(); 278 InitializeAndPump();
279 } 279 }
280 280
281 void ForcePulse() { service()->SendLocalData(); } 281 void ForcePulse() { service()->SendLocalData(); }
282 282
283 Time GetLastUpdateTime(const DeviceInfoSpecifics& specifics) {
284 return DeviceInfoService::GetLastUpdateTime(specifics);
285 }
286
287 private: 283 private:
288 int change_count_; 284 int change_count_ = 0;
289 285
290 // In memory model type store needs a MessageLoop. 286 // In memory model type store needs a MessageLoop.
291 base::MessageLoop message_loop_; 287 base::MessageLoop message_loop_;
292 288
293 // Holds the store while the service is not initialized. 289 // Holds the store while the service is not initialized.
294 std::unique_ptr<ModelTypeStore> store_; 290 std::unique_ptr<ModelTypeStore> store_;
295 291
296 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; 292 // Provides information about the local device. Is initialized in each case's
293 // constructor with a model object created from |kDefaultLocalSuffix|.
294 std::unique_ptr<LocalDeviceInfoProviderMock> provider_;
297 295
298 // Not initialized immediately (upon test's constructor). This allows each 296 // Not initialized immediately (upon test's constructor). This allows each
299 // test case to modify the dependencies the service will be constructed with. 297 // test case to modify the dependencies the service will be constructed with.
300 std::unique_ptr<DeviceInfoService> service_; 298 std::unique_ptr<DeviceInfoService> service_;
301 299
302 // A non-owning pointer to the processor given to the service. Will be nullptr 300 // A non-owning pointer to the processor given to the service. Will be nullptr
303 // before being given to the service, to make ownership easier. 301 // before being given to the service, to make ownership easier.
304 RecordingModelTypeChangeProcessor* processor_ = nullptr; 302 RecordingModelTypeChangeProcessor* processor_ = nullptr;
305
306 // A monotonically increasing label for generated specifics objects with data
307 // that is slightly different from eachother.
308 int generated_count_ = 0;
309 }; 303 };
310 304
311 namespace { 305 namespace {
312 306
313 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) { 307 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliation) {
314 InitializeAndPump(); 308 InitializeAndPump();
315 DeviceInfoList devices = service()->GetAllDeviceInfo(); 309 DeviceInfoList devices = service()->GetAllDeviceInfo();
316 ASSERT_EQ(1u, devices.size()); 310 ASSERT_EQ(1u, devices.size());
317 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 311 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
318 } 312 }
319 313
320 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliationSlowLoad) { 314 TEST_F(DeviceInfoServiceTest, EmptyDataReconciliationSlowLoad) {
321 InitializeService(); 315 InitializeService();
322 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size()); 316 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size());
323 base::RunLoop().RunUntilIdle(); 317 base::RunLoop().RunUntilIdle();
324 DeviceInfoList devices = service()->GetAllDeviceInfo(); 318 DeviceInfoList devices = service()->GetAllDeviceInfo();
325 ASSERT_EQ(1u, devices.size()); 319 ASSERT_EQ(1u, devices.size());
326 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 320 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
327 } 321 }
328 322
329 TEST_F(DeviceInfoServiceTest, LocalProviderSubscription) { 323 TEST_F(DeviceInfoServiceTest, LocalProviderSubscription) {
330 set_local_device(base::MakeUnique<LocalDeviceInfoProviderMock>()); 324 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
331 InitializeAndPump(); 325 InitializeAndPump();
332 326
333 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size()); 327 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size());
334 local_device()->Initialize(CreateDeviceInfo()); 328 local_device()->Initialize(CreateModel(1));
335 base::RunLoop().RunUntilIdle(); 329 base::RunLoop().RunUntilIdle();
336 330
337 DeviceInfoList devices = service()->GetAllDeviceInfo(); 331 DeviceInfoList devices = service()->GetAllDeviceInfo();
338 ASSERT_EQ(1u, devices.size()); 332 ASSERT_EQ(1u, devices.size());
339 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 333 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
340 } 334 }
341 335
342 // Metadata shouldn't be loaded before the provider is initialized. 336 // Metadata shouldn't be loaded before the provider is initialized.
343 TEST_F(DeviceInfoServiceTest, LocalProviderInitRace) { 337 TEST_F(DeviceInfoServiceTest, LocalProviderInitRace) {
344 set_local_device(base::MakeUnique<LocalDeviceInfoProviderMock>()); 338 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
345 InitializeAndPump(); 339 InitializeAndPump();
346 EXPECT_FALSE(processor()->metadata()); 340 EXPECT_FALSE(processor()->metadata());
347 341
348 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size()); 342 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size());
349 local_device()->Initialize(CreateDeviceInfo()); 343 local_device()->Initialize(CreateModel(1));
350 base::RunLoop().RunUntilIdle(); 344 base::RunLoop().RunUntilIdle();
351 345
352 DeviceInfoList devices = service()->GetAllDeviceInfo(); 346 DeviceInfoList devices = service()->GetAllDeviceInfo();
353 ASSERT_EQ(1u, devices.size()); 347 ASSERT_EQ(1u, devices.size());
354 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 348 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
355 349
356 EXPECT_TRUE(processor()->metadata()); 350 EXPECT_TRUE(processor()->metadata());
357 } 351 }
358 352
359 TEST_F(DeviceInfoServiceTest, GetClientTagNormal) { 353 TEST_F(DeviceInfoServiceTest, GetClientTagNormal) {
(...skipping 10 matching lines...) Expand all
370 InitializeService(); 364 InitializeService();
371 EntitySpecifics entity_specifics; 365 EntitySpecifics entity_specifics;
372 entity_specifics.mutable_device_info(); 366 entity_specifics.mutable_device_info();
373 EntityData entity_data; 367 EntityData entity_data;
374 entity_data.specifics = entity_specifics; 368 entity_data.specifics = entity_specifics;
375 EXPECT_EQ(CacheGuidToTag(""), service()->GetClientTag(entity_data)); 369 EXPECT_EQ(CacheGuidToTag(""), service()->GetClientTag(entity_data));
376 } 370 }
377 371
378 TEST_F(DeviceInfoServiceTest, TestWithLocalData) { 372 TEST_F(DeviceInfoServiceTest, TestWithLocalData) {
379 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 373 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
380 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); 374 DeviceInfoSpecifics specifics = CreateSpecifics(1);
381 store()->WriteData(batch.get(), specifics.cache_guid(), 375 store()->WriteData(batch.get(), specifics.cache_guid(),
382 specifics.SerializeAsString()); 376 specifics.SerializeAsString());
383 store()->CommitWriteBatch(std::move(batch), 377 store()->CommitWriteBatch(std::move(batch),
384 base::Bind(&VerifyResultIsSuccess)); 378 base::Bind(&VerifyResultIsSuccess));
385 379
386 InitializeAndPump(); 380 InitializeAndPump();
387 381
388 ASSERT_EQ(2u, service()->GetAllDeviceInfo().size()); 382 ASSERT_EQ(2u, service()->GetAllDeviceInfo().size());
389 VerifyEqual(specifics, 383 VerifyEqual(specifics,
390 *service()->GetDeviceInfo(specifics.cache_guid()).get()); 384 *service()->GetDeviceInfo(specifics.cache_guid()).get());
391 } 385 }
392 386
393 TEST_F(DeviceInfoServiceTest, TestWithLocalMetadata) { 387 TEST_F(DeviceInfoServiceTest, TestWithLocalMetadata) {
394 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 388 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
395 ModelTypeState state; 389 ModelTypeState state;
396 state.set_encryption_key_name("ekn"); 390 state.set_encryption_key_name("ekn");
397 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); 391 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString());
398 store()->CommitWriteBatch(std::move(batch), 392 store()->CommitWriteBatch(std::move(batch),
399 base::Bind(&VerifyResultIsSuccess)); 393 base::Bind(&VerifyResultIsSuccess));
400 InitializeAndPump(); 394 InitializeAndPump();
401 DeviceInfoList devices = service()->GetAllDeviceInfo(); 395 DeviceInfoList devices = service()->GetAllDeviceInfo();
402 ASSERT_EQ(1u, devices.size()); 396 ASSERT_EQ(1u, devices.size());
403 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 397 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
404 EXPECT_EQ(1u, processor()->put_multimap().size()); 398 EXPECT_EQ(1u, processor()->put_multimap().size());
405 } 399 }
406 400
407 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) { 401 TEST_F(DeviceInfoServiceTest, TestWithLocalDataAndMetadata) {
408 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 402 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
409 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); 403 DeviceInfoSpecifics specifics = CreateSpecifics(1);
410 store()->WriteData(batch.get(), specifics.cache_guid(), 404 store()->WriteData(batch.get(), specifics.cache_guid(),
411 specifics.SerializeAsString()); 405 specifics.SerializeAsString());
412 ModelTypeState state; 406 ModelTypeState state;
413 state.set_encryption_key_name("ekn"); 407 state.set_encryption_key_name("ekn");
414 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); 408 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString());
415 store()->CommitWriteBatch(std::move(batch), 409 store()->CommitWriteBatch(std::move(batch),
416 base::Bind(&VerifyResultIsSuccess)); 410 base::Bind(&VerifyResultIsSuccess));
417 411
418 InitializeAndPump(); 412 InitializeAndPump();
419 413
420 ASSERT_EQ(2u, service()->GetAllDeviceInfo().size()); 414 ASSERT_EQ(2u, service()->GetAllDeviceInfo().size());
421 VerifyEqual(specifics, 415 VerifyEqual(specifics,
422 *service()->GetDeviceInfo(specifics.cache_guid()).get()); 416 *service()->GetDeviceInfo(specifics.cache_guid()).get());
423 EXPECT_TRUE(processor()->metadata()); 417 EXPECT_TRUE(processor()->metadata());
424 EXPECT_EQ(state.encryption_key_name(), 418 EXPECT_EQ(state.encryption_key_name(),
425 processor()->metadata()->GetModelTypeState().encryption_key_name()); 419 processor()->metadata()->GetModelTypeState().encryption_key_name());
426 } 420 }
427 421
428 TEST_F(DeviceInfoServiceTest, GetData) { 422 TEST_F(DeviceInfoServiceTest, GetData) {
429 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 423 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
430 DeviceInfoSpecifics specifics1 = GenerateTestSpecifics(); 424 DeviceInfoSpecifics specifics1 = CreateSpecifics(1);
431 DeviceInfoSpecifics specifics2 = GenerateTestSpecifics(); 425 DeviceInfoSpecifics specifics2 = CreateSpecifics(2);
432 DeviceInfoSpecifics specifics3 = GenerateTestSpecifics(); 426 DeviceInfoSpecifics specifics3 = CreateSpecifics(3);
433 store()->WriteData(batch.get(), specifics1.cache_guid(), 427 store()->WriteData(batch.get(), specifics1.cache_guid(),
434 specifics1.SerializeAsString()); 428 specifics1.SerializeAsString());
435 store()->WriteData(batch.get(), specifics2.cache_guid(), 429 store()->WriteData(batch.get(), specifics2.cache_guid(),
436 specifics2.SerializeAsString()); 430 specifics2.SerializeAsString());
437 store()->WriteData(batch.get(), specifics3.cache_guid(), 431 store()->WriteData(batch.get(), specifics3.cache_guid(),
438 specifics3.SerializeAsString()); 432 specifics3.SerializeAsString());
439 store()->CommitWriteBatch(std::move(batch), 433 store()->CommitWriteBatch(std::move(batch),
440 base::Bind(&VerifyResultIsSuccess)); 434 base::Bind(&VerifyResultIsSuccess));
441 435
442 InitializeAndPump(); 436 InitializeAndPump();
443 437
444 std::map<std::string, DeviceInfoSpecifics> expected{ 438 std::map<std::string, DeviceInfoSpecifics> expected{
445 {specifics1.cache_guid(), specifics1}, 439 {specifics1.cache_guid(), specifics1},
446 {specifics3.cache_guid(), specifics3}}; 440 {specifics3.cache_guid(), specifics3}};
447 service()->GetData({specifics1.cache_guid(), specifics3.cache_guid()}, 441 service()->GetData({specifics1.cache_guid(), specifics3.cache_guid()},
448 base::Bind(&VerifyDataBatch, expected)); 442 base::Bind(&VerifyDataBatch, expected));
449 } 443 }
450 444
451 TEST_F(DeviceInfoServiceTest, GetDataMissing) { 445 TEST_F(DeviceInfoServiceTest, GetDataMissing) {
452 InitializeAndPump(); 446 InitializeAndPump();
453 service()->GetData({"does_not_exist"}, 447 service()->GetData({"does_not_exist"},
454 base::Bind(&VerifyDataBatch, 448 base::Bind(&VerifyDataBatch,
455 std::map<std::string, DeviceInfoSpecifics>())); 449 std::map<std::string, DeviceInfoSpecifics>()));
456 } 450 }
457 451
458 TEST_F(DeviceInfoServiceTest, GetAllData) { 452 TEST_F(DeviceInfoServiceTest, GetAllData) {
459 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 453 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
460 DeviceInfoSpecifics specifics1 = GenerateTestSpecifics(); 454 DeviceInfoSpecifics specifics1 = CreateSpecifics(1);
461 DeviceInfoSpecifics specifics2 = GenerateTestSpecifics(); 455 DeviceInfoSpecifics specifics2 = CreateSpecifics(2);
462 const std::string& guid1 = specifics1.cache_guid(); 456 const std::string& guid1 = specifics1.cache_guid();
463 const std::string& guid2 = specifics2.cache_guid(); 457 const std::string& guid2 = specifics2.cache_guid();
464 store()->WriteData(batch.get(), specifics1.cache_guid(), 458 store()->WriteData(batch.get(), specifics1.cache_guid(),
465 specifics1.SerializeAsString()); 459 specifics1.SerializeAsString());
466 store()->WriteData(batch.get(), specifics2.cache_guid(), 460 store()->WriteData(batch.get(), specifics2.cache_guid(),
467 specifics2.SerializeAsString()); 461 specifics2.SerializeAsString());
468 store()->CommitWriteBatch(std::move(batch), 462 store()->CommitWriteBatch(std::move(batch),
469 base::Bind(&VerifyResultIsSuccess)); 463 base::Bind(&VerifyResultIsSuccess));
470 464
471 InitializeAndPump(); 465 InitializeAndPump();
472 466
473 std::map<std::string, DeviceInfoSpecifics> expected{{guid1, specifics1}, 467 std::map<std::string, DeviceInfoSpecifics> expected{{guid1, specifics1},
474 {guid2, specifics2}}; 468 {guid2, specifics2}};
475 service()->GetData({guid1, guid2}, base::Bind(&VerifyDataBatch, expected)); 469 service()->GetData({guid1, guid2}, base::Bind(&VerifyDataBatch, expected));
476 } 470 }
477 471
478 TEST_F(DeviceInfoServiceTest, ApplySyncChangesEmpty) { 472 TEST_F(DeviceInfoServiceTest, ApplySyncChangesEmpty) {
479 InitializeAndPump(); 473 InitializeAndPump();
480 EXPECT_EQ(1, change_count()); 474 EXPECT_EQ(1, change_count());
481 const SyncError error = service()->ApplySyncChanges( 475 const SyncError error = service()->ApplySyncChanges(
482 service()->CreateMetadataChangeList(), EntityChangeList()); 476 service()->CreateMetadataChangeList(), EntityChangeList());
483 EXPECT_FALSE(error.IsSet()); 477 EXPECT_FALSE(error.IsSet());
484 EXPECT_EQ(1, change_count()); 478 EXPECT_EQ(1, change_count());
485 } 479 }
486 480
487 TEST_F(DeviceInfoServiceTest, ApplySyncChangesInMemory) { 481 TEST_F(DeviceInfoServiceTest, ApplySyncChangesInMemory) {
488 InitializeAndPump(); 482 InitializeAndPump();
489 EXPECT_EQ(1, change_count()); 483 EXPECT_EQ(1, change_count());
490 484
491 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); 485 DeviceInfoSpecifics specifics = CreateSpecifics(1);
492 const SyncError error_on_add = service()->ApplySyncChanges( 486 const SyncError error_on_add = service()->ApplySyncChanges(
493 service()->CreateMetadataChangeList(), EntityAddList({specifics})); 487 service()->CreateMetadataChangeList(), EntityAddList({specifics}));
494 488
495 EXPECT_FALSE(error_on_add.IsSet()); 489 EXPECT_FALSE(error_on_add.IsSet());
496 std::unique_ptr<DeviceInfo> info = 490 std::unique_ptr<DeviceInfo> info =
497 service()->GetDeviceInfo(specifics.cache_guid()); 491 service()->GetDeviceInfo(specifics.cache_guid());
498 ASSERT_TRUE(info); 492 ASSERT_TRUE(info);
499 VerifyEqual(specifics, *info.get()); 493 VerifyEqual(specifics, *info.get());
500 EXPECT_EQ(2, change_count()); 494 EXPECT_EQ(2, change_count());
501 495
502 const SyncError error_on_delete = service()->ApplySyncChanges( 496 const SyncError error_on_delete = service()->ApplySyncChanges(
503 service()->CreateMetadataChangeList(), 497 service()->CreateMetadataChangeList(),
504 {EntityChange::CreateDelete(specifics.cache_guid())}); 498 {EntityChange::CreateDelete(specifics.cache_guid())});
505 499
506 EXPECT_FALSE(error_on_delete.IsSet()); 500 EXPECT_FALSE(error_on_delete.IsSet());
507 EXPECT_FALSE(service()->GetDeviceInfo(specifics.cache_guid())); 501 EXPECT_FALSE(service()->GetDeviceInfo(specifics.cache_guid()));
508 EXPECT_EQ(3, change_count()); 502 EXPECT_EQ(3, change_count());
509 } 503 }
510 504
511 TEST_F(DeviceInfoServiceTest, ApplySyncChangesStore) { 505 TEST_F(DeviceInfoServiceTest, ApplySyncChangesStore) {
512 InitializeAndPump(); 506 InitializeAndPump();
513 EXPECT_EQ(1, change_count()); 507 EXPECT_EQ(1, change_count());
514 508
515 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); 509 DeviceInfoSpecifics specifics = CreateSpecifics(1);
516 ModelTypeState state; 510 ModelTypeState state;
517 state.set_encryption_key_name("ekn"); 511 state.set_encryption_key_name("ekn");
518 std::unique_ptr<MetadataChangeList> metadata_changes = 512 std::unique_ptr<MetadataChangeList> metadata_changes =
519 service()->CreateMetadataChangeList(); 513 service()->CreateMetadataChangeList();
520 metadata_changes->UpdateModelTypeState(state); 514 metadata_changes->UpdateModelTypeState(state);
521 515
522 const SyncError error = service()->ApplySyncChanges( 516 const SyncError error = service()->ApplySyncChanges(
523 std::move(metadata_changes), EntityAddList({specifics})); 517 std::move(metadata_changes), EntityAddList({specifics}));
524 EXPECT_FALSE(error.IsSet()); 518 EXPECT_FALSE(error.IsSet());
525 EXPECT_EQ(2, change_count()); 519 EXPECT_EQ(2, change_count());
526 520
527 RestartService(); 521 RestartService();
528 522
529 std::unique_ptr<DeviceInfo> info = 523 std::unique_ptr<DeviceInfo> info =
530 service()->GetDeviceInfo(specifics.cache_guid()); 524 service()->GetDeviceInfo(specifics.cache_guid());
531 ASSERT_TRUE(info); 525 ASSERT_TRUE(info);
532 VerifyEqual(specifics, *info.get()); 526 VerifyEqual(specifics, *info.get());
533 527
534 EXPECT_TRUE(processor()->metadata()); 528 EXPECT_TRUE(processor()->metadata());
535 EXPECT_EQ(state.encryption_key_name(), 529 EXPECT_EQ(state.encryption_key_name(),
536 processor()->metadata()->GetModelTypeState().encryption_key_name()); 530 processor()->metadata()->GetModelTypeState().encryption_key_name());
537 } 531 }
538 532
539 TEST_F(DeviceInfoServiceTest, ApplySyncChangesWithLocalGuid) { 533 TEST_F(DeviceInfoServiceTest, ApplySyncChangesWithLocalGuid) {
540 InitializeAndPump(); 534 InitializeAndPump();
541 535
542 // The point of this test is to try to apply remote changes that have the same 536 // The service should ignore these changes using this specifics because its
543 // cache guid as the local device. The service should ignore these changes 537 // guid will match the local device.
544 // since only it should be performing writes on its data. 538 DeviceInfoSpecifics specifics = CreateSpecifics(kDefaultLocalSuffix);
545 DeviceInfoSpecifics specifics =
546 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid());
547 539
548 // Should have a single change from reconciliation. 540 // Should have a single change from reconciliation.
549 EXPECT_TRUE( 541 EXPECT_TRUE(
550 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); 542 service()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid()));
551 EXPECT_EQ(1, change_count()); 543 EXPECT_EQ(1, change_count());
552 // Ensure |last_updated| is about now, plus or minus a little bit. 544 // Ensure |last_updated| is about now, plus or minus a little bit.
553 Time last_updated(ProtoTimeToTime(processor() 545 Time last_updated(ProtoTimeToTime(processor()
554 ->put_multimap() 546 ->put_multimap()
555 .begin() 547 .begin()
556 ->second->specifics.device_info() 548 ->second->specifics.device_info()
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 // TODO(skym): Stop sending local twice. The first of the two puts will 582 // TODO(skym): Stop sending local twice. The first of the two puts will
591 // probably happen before the processor is tracking metadata yet, and so there 583 // probably happen before the processor is tracking metadata yet, and so there
592 // should not be much overhead. 584 // should not be much overhead.
593 EXPECT_EQ(2u, processor()->put_multimap().size()); 585 EXPECT_EQ(2u, processor()->put_multimap().size());
594 EXPECT_EQ(2u, processor()->put_multimap().count( 586 EXPECT_EQ(2u, processor()->put_multimap().count(
595 local_device()->GetLocalDeviceInfo()->guid())); 587 local_device()->GetLocalDeviceInfo()->guid()));
596 EXPECT_EQ(0u, processor()->delete_set().size()); 588 EXPECT_EQ(0u, processor()->delete_set().size());
597 } 589 }
598 590
599 TEST_F(DeviceInfoServiceTest, MergeWithData) { 591 TEST_F(DeviceInfoServiceTest, MergeWithData) {
592 const DeviceInfoSpecifics unique_local = CreateSpecifics(1);
593 DeviceInfoSpecifics conflict_local = CreateSpecifics(2);
594 DeviceInfoSpecifics conflict_remote = CreateSpecifics(3);
595 const DeviceInfoSpecifics unique_remote = CreateSpecifics(4);
596
600 const std::string conflict_guid = "conflict_guid"; 597 const std::string conflict_guid = "conflict_guid";
601 const DeviceInfoSpecifics unique_local = 598 conflict_local.set_cache_guid(conflict_guid);
602 GenerateTestSpecifics("unique_local_guid"); 599 conflict_remote.set_cache_guid(conflict_guid);
603 const DeviceInfoSpecifics conflict_local =
604 GenerateTestSpecifics(conflict_guid);
605 const DeviceInfoSpecifics conflict_remote =
606 GenerateTestSpecifics(conflict_guid);
607 const DeviceInfoSpecifics unique_remote =
608 GenerateTestSpecifics("unique_remote_guid");
609 600
610 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 601 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
611 store()->WriteData(batch.get(), unique_local.cache_guid(), 602 store()->WriteData(batch.get(), unique_local.cache_guid(),
612 unique_local.SerializeAsString()); 603 unique_local.SerializeAsString());
613 store()->WriteData(batch.get(), conflict_local.cache_guid(), 604 store()->WriteData(batch.get(), conflict_local.cache_guid(),
614 conflict_local.SerializeAsString()); 605 conflict_local.SerializeAsString());
615 store()->CommitWriteBatch(std::move(batch), 606 store()->CommitWriteBatch(std::move(batch),
616 base::Bind(&VerifyResultIsSuccess)); 607 base::Bind(&VerifyResultIsSuccess));
617 608
618 InitializeAndPump(); 609 InitializeAndPump();
619 EXPECT_EQ(1, change_count()); 610 EXPECT_EQ(1, change_count());
620 611
621 EntityDataMap remote_input; 612 EntityDataMap remote_input;
622 remote_input[conflict_remote.cache_guid()] = 613 remote_input[conflict_remote.cache_guid()] =
623 SpecificsToEntity(conflict_remote); 614 SpecificsToEntity(conflict_remote);
624 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote); 615 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote);
625 616
626 ModelTypeState state; 617 ModelTypeState state;
627 state.set_encryption_key_name("ekn"); 618 state.set_encryption_key_name("ekn");
628 std::unique_ptr<MetadataChangeList> metadata_changes( 619 std::unique_ptr<MetadataChangeList> metadata_changes =
629 service()->CreateMetadataChangeList()); 620 service()->CreateMetadataChangeList();
630 metadata_changes->UpdateModelTypeState(state); 621 metadata_changes->UpdateModelTypeState(state);
631 622
632 const SyncError error = 623 const SyncError error =
633 service()->MergeSyncData(std::move(metadata_changes), remote_input); 624 service()->MergeSyncData(std::move(metadata_changes), remote_input);
634 EXPECT_FALSE(error.IsSet()); 625 EXPECT_FALSE(error.IsSet());
635 EXPECT_EQ(2, change_count()); 626 EXPECT_EQ(2, change_count());
636 627
637 // The remote should beat the local in conflict. 628 // The remote should beat the local in conflict.
638 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size()); 629 EXPECT_EQ(4u, service()->GetAllDeviceInfo().size());
639 VerifyEqual(unique_local, 630 VerifyEqual(unique_local,
640 *service()->GetDeviceInfo(unique_local.cache_guid()).get()); 631 *service()->GetDeviceInfo(unique_local.cache_guid()).get());
641 VerifyEqual(unique_remote, 632 VerifyEqual(unique_remote,
642 *service()->GetDeviceInfo(unique_remote.cache_guid()).get()); 633 *service()->GetDeviceInfo(unique_remote.cache_guid()).get());
643 VerifyEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get()); 634 VerifyEqual(conflict_remote, *service()->GetDeviceInfo(conflict_guid).get());
644 635
645 // Service should have told the processor about the existance of unique_local. 636 // Service should have told the processor about the existance of unique_local.
646 EXPECT_TRUE(processor()->delete_set().empty()); 637 EXPECT_TRUE(processor()->delete_set().empty());
647 EXPECT_EQ(3u, processor()->put_multimap().size()); 638 EXPECT_EQ(3u, processor()->put_multimap().size());
648 EXPECT_EQ(1u, processor()->put_multimap().count(unique_local.cache_guid())); 639 EXPECT_EQ(1u, processor()->put_multimap().count(unique_local.cache_guid()));
649 const auto& it = processor()->put_multimap().find(unique_local.cache_guid()); 640 const auto& it = processor()->put_multimap().find(unique_local.cache_guid());
650 ASSERT_NE(processor()->put_multimap().end(), it); 641 ASSERT_NE(processor()->put_multimap().end(), it);
651 VerifyEqual(unique_local, it->second->specifics.device_info()); 642 VerifyEqual(unique_local, it->second->specifics.device_info());
652 643
653 RestartService(); 644 RestartService();
654 EXPECT_EQ(state.encryption_key_name(), 645 EXPECT_EQ(state.encryption_key_name(),
655 processor()->metadata()->GetModelTypeState().encryption_key_name()); 646 processor()->metadata()->GetModelTypeState().encryption_key_name());
656 } 647 }
657 648
658 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { 649 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) {
659 const DeviceInfo* local_device_info = local_device()->GetLocalDeviceInfo(); 650 const DeviceInfo* provider_info = local_device()->GetLocalDeviceInfo();
660 std::unique_ptr<DeviceInfoSpecifics> specifics = 651 auto specifics = base::MakeUnique<DeviceInfoSpecifics>(CreateSpecifics(0));
661 CopyToSpecifics(*local_device_info);
662 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); 652 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now()));
663 const std::string guid = local_device_info->guid(); 653 const std::string guid = provider_info->guid();
664 654
665 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 655 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
666 store()->WriteData(batch.get(), guid, specifics->SerializeAsString()); 656 store()->WriteData(batch.get(), guid, specifics->SerializeAsString());
667 store()->CommitWriteBatch(std::move(batch), 657 store()->CommitWriteBatch(std::move(batch),
668 base::Bind(&VerifyResultIsSuccess)); 658 base::Bind(&VerifyResultIsSuccess));
669 659
670 InitializeAndPump(); 660 InitializeAndPump();
671 661
672 EntityDataMap remote_input; 662 EntityDataMap remote_input;
673 remote_input[guid] = SpecificsToEntity(*specifics); 663 remote_input[guid] = SpecificsToEntity(*specifics);
674 664
675 const SyncError error = service()->MergeSyncData( 665 const SyncError error = service()->MergeSyncData(
676 service()->CreateMetadataChangeList(), remote_input); 666 service()->CreateMetadataChangeList(), remote_input);
677 EXPECT_FALSE(error.IsSet()); 667 EXPECT_FALSE(error.IsSet());
678 EXPECT_EQ(0, change_count()); 668 EXPECT_EQ(0, change_count());
679 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); 669 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size());
680 EXPECT_TRUE(processor()->delete_set().empty()); 670 EXPECT_TRUE(processor()->delete_set().empty());
681 EXPECT_TRUE(processor()->put_multimap().empty()); 671 EXPECT_TRUE(processor()->put_multimap().empty());
682 } 672 }
683 673
684 TEST_F(DeviceInfoServiceTest, GetLastUpdateTime) {
685 Time time1(Time() + TimeDelta::FromDays(1));
686
687 DeviceInfoSpecifics specifics1 = GenerateTestSpecifics();
688 DeviceInfoSpecifics specifics2 = GenerateTestSpecifics();
689 specifics2.set_last_updated_timestamp(TimeToProtoTime(time1));
690
691 EXPECT_EQ(Time(), GetLastUpdateTime(specifics1));
692 EXPECT_EQ(time1, GetLastUpdateTime(specifics2));
693 }
694
695 TEST_F(DeviceInfoServiceTest, CountActiveDevices) { 674 TEST_F(DeviceInfoServiceTest, CountActiveDevices) {
696 InitializeAndPump(); 675 InitializeAndPump();
697 EXPECT_EQ(1, service()->CountActiveDevices()); 676 EXPECT_EQ(1, service()->CountActiveDevices());
698 677
699 DeviceInfoSpecifics specifics = 678 DeviceInfoSpecifics specifics = CreateSpecifics(0);
700 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid());
701 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), 679 service()->ApplySyncChanges(service()->CreateMetadataChangeList(),
702 EntityAddList({specifics})); 680 EntityAddList({specifics}));
703 EXPECT_EQ(1, service()->CountActiveDevices()); 681 EXPECT_EQ(1, service()->CountActiveDevices());
704 682
705 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); 683 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now()));
706 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), 684 service()->ApplySyncChanges(service()->CreateMetadataChangeList(),
707 EntityAddList({specifics})); 685 EntityAddList({specifics}));
708 EXPECT_EQ(1, service()->CountActiveDevices()); 686 EXPECT_EQ(1, service()->CountActiveDevices());
709 687
710 specifics.set_cache_guid("non-local"); 688 specifics.set_cache_guid("non-local");
711 service()->ApplySyncChanges(service()->CreateMetadataChangeList(), 689 service()->ApplySyncChanges(service()->CreateMetadataChangeList(),
712 EntityAddList({specifics})); 690 EntityAddList({specifics}));
713 EXPECT_EQ(2, service()->CountActiveDevices()); 691 EXPECT_EQ(2, service()->CountActiveDevices());
692
693 // Now set time to long ago in the past, it should not be active anymore.
694 specifics.set_last_updated_timestamp(TimeToProtoTime(Time()));
695 service()->ApplySyncChanges(service()->CreateMetadataChangeList(),
696 EntityAddList({specifics}));
697 EXPECT_EQ(1, service()->CountActiveDevices());
714 } 698 }
715 699
716 TEST_F(DeviceInfoServiceTest, MultipleOnProviderInitialized) { 700 TEST_F(DeviceInfoServiceTest, MultipleOnProviderInitialized) {
717 set_local_device(base::MakeUnique<LocalDeviceInfoProviderMock>()); 701 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
718 InitializeAndPump(); 702 InitializeAndPump();
719 EXPECT_EQ(nullptr, processor()->metadata()); 703 EXPECT_EQ(nullptr, processor()->metadata());
720 704
721 // Verify the processor was given metadata. 705 // Verify the processor was given metadata.
722 local_device()->Initialize(CreateDeviceInfo()); 706 local_device()->Initialize(CreateModel(0));
723 base::RunLoop().RunUntilIdle(); 707 base::RunLoop().RunUntilIdle();
724 const MetadataBatch* metadata = processor()->metadata(); 708 const MetadataBatch* metadata = processor()->metadata();
725 EXPECT_NE(nullptr, metadata); 709 EXPECT_NE(nullptr, metadata);
726 710
727 // Pointer address of metadata should remain constant because the processor 711 // Pointer address of metadata should remain constant because the processor
728 // should not have been given new metadata. 712 // should not have been given new metadata.
729 local_device()->Initialize(CreateDeviceInfo()); 713 local_device()->Initialize(CreateModel(0));
730 base::RunLoop().RunUntilIdle(); 714 base::RunLoop().RunUntilIdle();
731 EXPECT_EQ(metadata, processor()->metadata()); 715 EXPECT_EQ(metadata, processor()->metadata());
732 } 716 }
733 717
734 TEST_F(DeviceInfoServiceTest, SendLocalData) { 718 TEST_F(DeviceInfoServiceTest, SendLocalData) {
735 InitializeAndPump(); 719 InitializeAndPump();
736 EXPECT_EQ(1, change_count()); 720 EXPECT_EQ(1, change_count());
737 EXPECT_EQ(1u, processor()->put_multimap().size()); 721 EXPECT_EQ(1u, processor()->put_multimap().size());
738 722
739 ForcePulse(); 723 ForcePulse();
740 EXPECT_EQ(2, change_count()); 724 EXPECT_EQ(2, change_count());
741 EXPECT_EQ(2u, processor()->put_multimap().size()); 725 EXPECT_EQ(2u, processor()->put_multimap().size());
742 726
743 // After clearing, pulsing should no-op and not result in a processor put or 727 // After clearing, pulsing should no-op and not result in a processor put or
744 // a notification to observers. 728 // a notification to observers.
745 local_device()->Clear(); 729 local_device()->Clear();
746 ForcePulse(); 730 ForcePulse();
747 EXPECT_EQ(2, change_count()); 731 EXPECT_EQ(2, change_count());
748 EXPECT_EQ(2u, processor()->put_multimap().size()); 732 EXPECT_EQ(2u, processor()->put_multimap().size());
749 } 733 }
750 734
751 TEST_F(DeviceInfoServiceTest, DisableSync) { 735 TEST_F(DeviceInfoServiceTest, DisableSync) {
752 InitializeAndPump(); 736 InitializeAndPump();
753 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); 737 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size());
754 EXPECT_EQ(1, change_count()); 738 EXPECT_EQ(1, change_count());
755 739
756 DeviceInfoSpecifics specifics = GenerateTestSpecifics(); 740 DeviceInfoSpecifics specifics = CreateSpecifics(1);
757 const SyncError error = service()->ApplySyncChanges( 741 const SyncError error = service()->ApplySyncChanges(
758 service()->CreateMetadataChangeList(), EntityAddList({specifics})); 742 service()->CreateMetadataChangeList(), EntityAddList({specifics}));
759 743
760 EXPECT_FALSE(error.IsSet()); 744 EXPECT_FALSE(error.IsSet());
761 EXPECT_EQ(2u, service()->GetAllDeviceInfo().size()); 745 EXPECT_EQ(2u, service()->GetAllDeviceInfo().size());
762 EXPECT_EQ(2, change_count()); 746 EXPECT_EQ(2, change_count());
763 747
764 // Should clear out all local data and notify observers. 748 // Should clear out all local data and notify observers.
765 service()->DisableSync(); 749 service()->DisableSync();
766 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size()); 750 EXPECT_EQ(0u, service()->GetAllDeviceInfo().size());
767 EXPECT_EQ(3, change_count()); 751 EXPECT_EQ(3, change_count());
768 752
769 // Reloading from storage shouldn't contain remote data. 753 // Reloading from storage shouldn't contain remote data.
770 RestartService(); 754 RestartService();
771 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size()); 755 EXPECT_EQ(1u, service()->GetAllDeviceInfo().size());
772 EXPECT_EQ(4, change_count()); 756 EXPECT_EQ(4, change_count());
773 } 757 }
774 758
775 } // namespace 759 } // namespace
776 760
777 } // namespace syncer 761 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/device_info/device_info_service.cc ('k') | components/sync/model/model_type_sync_bridge.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698