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

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

Issue 2533193002: [Sync] Minor refactoring of DeviceInfoSyncBridgeTest. (Closed)
Patch Set: Removed used variable instead of hardcoded "ekn" Created 4 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_sync_bridge.h" 5 #include "components/sync/device_info/device_info_sync_bridge.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX); 61 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
62 specifics.set_sync_user_agent( 62 specifics.set_sync_user_agent(
63 base::StringPrintf(kSyncUserAgentFormat, suffix)); 63 base::StringPrintf(kSyncUserAgentFormat, suffix));
64 specifics.set_chrome_version( 64 specifics.set_chrome_version(
65 base::StringPrintf(kChromeVersionFormat, suffix)); 65 base::StringPrintf(kChromeVersionFormat, suffix));
66 specifics.set_signin_scoped_device_id( 66 specifics.set_signin_scoped_device_id(
67 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix)); 67 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix));
68 return specifics; 68 return specifics;
69 } 69 }
70 70
71 DeviceInfoSpecifics CreateSpecifics(int suffix, Time last_updated_timestamp) {
72 DeviceInfoSpecifics specifics = CreateSpecifics(suffix);
73 specifics.set_last_updated_timestamp(TimeToProtoTime(last_updated_timestamp));
74 return specifics;
75 }
76
71 std::unique_ptr<DeviceInfo> CreateModel(int suffix) { 77 std::unique_ptr<DeviceInfo> CreateModel(int suffix) {
72 return base::MakeUnique<DeviceInfo>( 78 return base::MakeUnique<DeviceInfo>(
73 base::StringPrintf(kGuidFormat, suffix), 79 base::StringPrintf(kGuidFormat, suffix),
74 base::StringPrintf(kClientNameFormat, suffix), 80 base::StringPrintf(kClientNameFormat, suffix),
75 base::StringPrintf(kChromeVersionFormat, suffix), 81 base::StringPrintf(kChromeVersionFormat, suffix),
76 base::StringPrintf(kSyncUserAgentFormat, suffix), kDeviceType, 82 base::StringPrintf(kSyncUserAgentFormat, suffix), kDeviceType,
77 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix)); 83 base::StringPrintf(kSigninScopedDeviceIdFormat, suffix));
78 } 84 }
79 85
86 ModelTypeState StateWithEncryption(const std::string& encryption_key_name) {
87 ModelTypeState state;
88 state.set_encryption_key_name(encryption_key_name);
89 return state;
90 }
91
80 void VerifyResultIsSuccess(Result result) { 92 void VerifyResultIsSuccess(Result result) {
81 EXPECT_EQ(Result::SUCCESS, result); 93 EXPECT_EQ(Result::SUCCESS, result);
82 } 94 }
83 95
84 void VerifyEqual(const DeviceInfoSpecifics& s1, const DeviceInfoSpecifics& s2) { 96 void VerifyEqual(const DeviceInfoSpecifics& s1, const DeviceInfoSpecifics& s2) {
85 EXPECT_EQ(s1.cache_guid(), s2.cache_guid()); 97 EXPECT_EQ(s1.cache_guid(), s2.cache_guid());
86 EXPECT_EQ(s1.client_name(), s2.client_name()); 98 EXPECT_EQ(s1.client_name(), s2.client_name());
87 EXPECT_EQ(s1.device_type(), s2.device_type()); 99 EXPECT_EQ(s1.device_type(), s2.device_type());
88 EXPECT_EQ(s1.sync_user_agent(), s2.sync_user_agent()); 100 EXPECT_EQ(s1.sync_user_agent(), s2.sync_user_agent());
89 EXPECT_EQ(s1.chrome_version(), s2.chrome_version()); 101 EXPECT_EQ(s1.chrome_version(), s2.chrome_version());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 142 }
131 143
132 std::string CacheGuidToTag(const std::string& guid) { 144 std::string CacheGuidToTag(const std::string& guid) {
133 return "DeviceInfo_" + guid; 145 return "DeviceInfo_" + guid;
134 } 146 }
135 147
136 // Helper method to reduce duplicated code between tests. Wraps the given 148 // Helper method to reduce duplicated code between tests. Wraps the given
137 // specifics objects in an EntityData and EntityChange of type ACTION_ADD, and 149 // specifics objects in an EntityData and EntityChange of type ACTION_ADD, and
138 // returns an EntityChangeList containing them all. Order is maintained. 150 // returns an EntityChangeList containing them all. Order is maintained.
139 EntityChangeList EntityAddList( 151 EntityChangeList EntityAddList(
140 std::vector<DeviceInfoSpecifics> specifics_list) { 152 const std::vector<DeviceInfoSpecifics>& specifics_list) {
141 EntityChangeList changes; 153 EntityChangeList changes;
142 for (const auto& specifics : specifics_list) { 154 for (const auto& specifics : specifics_list) {
143 changes.push_back(EntityChange::CreateAdd(specifics.cache_guid(), 155 changes.push_back(EntityChange::CreateAdd(specifics.cache_guid(),
144 SpecificsToEntity(specifics))); 156 SpecificsToEntity(specifics)));
145 } 157 }
146 return changes; 158 return changes;
147 } 159 }
148 160
161 // Similar helper to EntityAddList(...), only wraps in a EntityDataMap for a
162 // merge call. Order is irrelevant, since the map sorts by key. Should not
163 // contain multiple specifics with the same guid.
164 EntityDataMap InlineEntityDataMap(
165 const std::vector<DeviceInfoSpecifics>& specifics_list) {
166 EntityDataMap map;
167 for (const auto& specifics : specifics_list) {
168 EXPECT_EQ(map.end(), map.find(specifics.cache_guid()));
169 map[specifics.cache_guid()] = SpecificsToEntity(specifics);
170 }
171 return map;
172 }
173
149 // Instead of actually processing anything, simply accumulates all instructions 174 // Instead of actually processing anything, simply accumulates all instructions
150 // in members that can then be accessed. TODO(skym): If this ends up being 175 // in members that can then be accessed. TODO(skym): If this ends up being
151 // useful for other model type unittests it should be moved out to a shared 176 // useful for other model type unittests it should be moved out to a shared
152 // location. 177 // location.
153 class RecordingModelTypeChangeProcessor : public FakeModelTypeChangeProcessor { 178 class RecordingModelTypeChangeProcessor : public FakeModelTypeChangeProcessor {
154 public: 179 public:
155 RecordingModelTypeChangeProcessor() {} 180 RecordingModelTypeChangeProcessor() {}
156 ~RecordingModelTypeChangeProcessor() override {} 181 ~RecordingModelTypeChangeProcessor() override {}
157 182
158 void Put(const std::string& storage_key, 183 void Put(const std::string& storage_key,
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 bridge_.reset(); 298 bridge_.reset();
274 } 299 }
275 300
276 void RestartBridge() { 301 void RestartBridge() {
277 PumpAndShutdown(); 302 PumpAndShutdown();
278 InitializeAndPump(); 303 InitializeAndPump();
279 } 304 }
280 305
281 void ForcePulse() { bridge()->SendLocalData(); } 306 void ForcePulse() { bridge()->SendLocalData(); }
282 307
308 void WriteToStore(const std::vector<DeviceInfoSpecifics>& specifics_list,
309 std::unique_ptr<WriteBatch> batch) {
310 // The bridge only reads from the store during initialization, so if the
311 // |bridge_| has already been initialized, then it may not have an effect.
312 EXPECT_FALSE(bridge_);
313 for (auto& specifics : specifics_list) {
314 batch->WriteData(specifics.cache_guid(), specifics.SerializeAsString());
315 }
316 store()->CommitWriteBatch(std::move(batch),
317 base::Bind(&VerifyResultIsSuccess));
318 }
319
320 void WriteToStore(const std::vector<DeviceInfoSpecifics>& specifics_list,
321 ModelTypeState state) {
322 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
323 batch->GetMetadataChangeList()->UpdateModelTypeState(state);
324 WriteToStore(specifics_list, std::move(batch));
325 }
326
327 void WriteToStore(const std::vector<DeviceInfoSpecifics>& specifics_list) {
328 WriteToStore(specifics_list, store()->CreateWriteBatch());
329 }
330
283 private: 331 private:
284 int change_count_ = 0; 332 int change_count_ = 0;
285 333
286 // In memory model type store needs a MessageLoop. 334 // In memory model type store needs a MessageLoop.
287 base::MessageLoop message_loop_; 335 base::MessageLoop message_loop_;
288 336
289 // Holds the store while the bridge is not initialized. 337 // Holds the store while the bridge is not initialized.
290 std::unique_ptr<ModelTypeStore> store_; 338 std::unique_ptr<ModelTypeStore> store_;
291 339
292 // Provides information about the local device. Is initialized in each case's 340 // Provides information about the local device. Is initialized in each case's
293 // constructor with a model object created from |kDefaultLocalSuffix|. 341 // constructor with a model object created from |kDefaultLocalSuffix|.
294 std::unique_ptr<LocalDeviceInfoProviderMock> provider_; 342 std::unique_ptr<LocalDeviceInfoProviderMock> provider_;
295 343
296 // Not initialized immediately (upon test's constructor). This allows each 344 // Not initialized immediately (upon test's constructor). This allows each
297 // test case to modify the dependencies the bridge will be constructed with. 345 // test case to modify the dependencies the bridge will be constructed with.
298 std::unique_ptr<DeviceInfoSyncBridge> bridge_; 346 std::unique_ptr<DeviceInfoSyncBridge> bridge_;
299 347
300 // A non-owning pointer to the processor given to the bridge. Will be null 348 // A non-owning pointer to the processor given to the bridge. Will be null
301 // before being given to the bridge, to make ownership easier. 349 // before being given to the bridge, to make ownership easier.
302 RecordingModelTypeChangeProcessor* processor_ = nullptr; 350 RecordingModelTypeChangeProcessor* processor_ = nullptr;
303 }; 351 };
304 352
305 namespace { 353 namespace {
306 354
307 TEST_F(DeviceInfoSyncBridgeTest, EmptyDataReconciliation) { 355 TEST_F(DeviceInfoSyncBridgeTest, EmptyDataReconciliation) {
308 InitializeAndPump(); 356 InitializeAndPump();
309 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 357 const DeviceInfoList devices = bridge()->GetAllDeviceInfo();
310 ASSERT_EQ(1u, devices.size()); 358 ASSERT_EQ(1u, devices.size());
311 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 359 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
312 } 360 }
313 361
314 TEST_F(DeviceInfoSyncBridgeTest, EmptyDataReconciliationSlowLoad) { 362 TEST_F(DeviceInfoSyncBridgeTest, EmptyDataReconciliationSlowLoad) {
315 InitializeBridge(); 363 InitializeBridge();
316 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 364 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
317 base::RunLoop().RunUntilIdle(); 365 base::RunLoop().RunUntilIdle();
318 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 366 const DeviceInfoList devices = bridge()->GetAllDeviceInfo();
319 ASSERT_EQ(1u, devices.size()); 367 ASSERT_EQ(1u, devices.size());
320 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 368 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
321 } 369 }
322 370
323 TEST_F(DeviceInfoSyncBridgeTest, LocalProviderSubscription) { 371 TEST_F(DeviceInfoSyncBridgeTest, LocalProviderSubscription) {
324 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>()); 372 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
325 InitializeAndPump(); 373 InitializeAndPump();
326 374
327 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 375 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
328 local_device()->Initialize(CreateModel(1)); 376 local_device()->Initialize(CreateModel(1));
329 base::RunLoop().RunUntilIdle(); 377 base::RunLoop().RunUntilIdle();
330 378
331 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 379 const DeviceInfoList devices = bridge()->GetAllDeviceInfo();
332 ASSERT_EQ(1u, devices.size()); 380 ASSERT_EQ(1u, devices.size());
333 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 381 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
334 } 382 }
335 383
336 // Metadata shouldn't be loaded before the provider is initialized. 384 // Metadata shouldn't be loaded before the provider is initialized.
337 TEST_F(DeviceInfoSyncBridgeTest, LocalProviderInitRace) { 385 TEST_F(DeviceInfoSyncBridgeTest, LocalProviderInitRace) {
338 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>()); 386 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
339 InitializeAndPump(); 387 InitializeAndPump();
340 EXPECT_FALSE(processor()->metadata()); 388 EXPECT_FALSE(processor()->metadata());
341 389
(...skipping 21 matching lines...) Expand all
363 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagEmpty) { 411 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagEmpty) {
364 InitializeBridge(); 412 InitializeBridge();
365 EntitySpecifics entity_specifics; 413 EntitySpecifics entity_specifics;
366 entity_specifics.mutable_device_info(); 414 entity_specifics.mutable_device_info();
367 EntityData entity_data; 415 EntityData entity_data;
368 entity_data.specifics = entity_specifics; 416 entity_data.specifics = entity_specifics;
369 EXPECT_EQ(CacheGuidToTag(""), bridge()->GetClientTag(entity_data)); 417 EXPECT_EQ(CacheGuidToTag(""), bridge()->GetClientTag(entity_data));
370 } 418 }
371 419
372 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalData) { 420 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalData) {
373 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 421 const DeviceInfoSpecifics specifics = CreateSpecifics(1);
374 DeviceInfoSpecifics specifics = CreateSpecifics(1); 422 WriteToStore({specifics});
375 batch->WriteData(specifics.cache_guid(), specifics.SerializeAsString());
376 store()->CommitWriteBatch(std::move(batch),
377 base::Bind(&VerifyResultIsSuccess));
378
379 InitializeAndPump(); 423 InitializeAndPump();
380 424
381 ASSERT_EQ(2u, bridge()->GetAllDeviceInfo().size()); 425 ASSERT_EQ(2u, bridge()->GetAllDeviceInfo().size());
382 VerifyEqual(specifics, 426 VerifyEqual(specifics,
383 *bridge()->GetDeviceInfo(specifics.cache_guid()).get()); 427 *bridge()->GetDeviceInfo(specifics.cache_guid()).get());
384 } 428 }
385 429
386 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalMetadata) { 430 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalMetadata) {
387 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 431 WriteToStore(std::vector<DeviceInfoSpecifics>(), StateWithEncryption("ekn"));
388 ModelTypeState state;
389 state.set_encryption_key_name("ekn");
390 batch->GetMetadataChangeList()->UpdateModelTypeState(state);
391 store()->CommitWriteBatch(std::move(batch),
392 base::Bind(&VerifyResultIsSuccess));
393 InitializeAndPump(); 432 InitializeAndPump();
394 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 433
434 const DeviceInfoList devices = bridge()->GetAllDeviceInfo();
395 ASSERT_EQ(1u, devices.size()); 435 ASSERT_EQ(1u, devices.size());
396 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 436 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
397 EXPECT_EQ(1u, processor()->put_multimap().size()); 437 EXPECT_EQ(1u, processor()->put_multimap().size());
398 } 438 }
399 439
400 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalDataAndMetadata) { 440 TEST_F(DeviceInfoSyncBridgeTest, TestWithLocalDataAndMetadata) {
401 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 441 const DeviceInfoSpecifics specifics = CreateSpecifics(1);
402 DeviceInfoSpecifics specifics = CreateSpecifics(1); 442 ModelTypeState state = StateWithEncryption("ekn");
403 batch->WriteData(specifics.cache_guid(), specifics.SerializeAsString()); 443 WriteToStore({specifics}, state);
404 ModelTypeState state;
405 state.set_encryption_key_name("ekn");
406 batch->GetMetadataChangeList()->UpdateModelTypeState(state);
407 store()->CommitWriteBatch(std::move(batch),
408 base::Bind(&VerifyResultIsSuccess));
409
410 InitializeAndPump(); 444 InitializeAndPump();
411 445
412 ASSERT_EQ(2u, bridge()->GetAllDeviceInfo().size()); 446 ASSERT_EQ(2u, bridge()->GetAllDeviceInfo().size());
413 VerifyEqual(specifics, 447 VerifyEqual(specifics,
414 *bridge()->GetDeviceInfo(specifics.cache_guid()).get()); 448 *bridge()->GetDeviceInfo(specifics.cache_guid()).get());
415 EXPECT_TRUE(processor()->metadata()); 449 EXPECT_TRUE(processor()->metadata());
416 EXPECT_EQ(state.encryption_key_name(), 450 EXPECT_EQ(state.encryption_key_name(),
417 processor()->metadata()->GetModelTypeState().encryption_key_name()); 451 processor()->metadata()->GetModelTypeState().encryption_key_name());
418 } 452 }
419 453
420 TEST_F(DeviceInfoSyncBridgeTest, GetData) { 454 TEST_F(DeviceInfoSyncBridgeTest, GetData) {
421 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 455 const DeviceInfoSpecifics specifics1 = CreateSpecifics(1);
422 DeviceInfoSpecifics specifics1 = CreateSpecifics(1); 456 const DeviceInfoSpecifics specifics2 = CreateSpecifics(2);
423 DeviceInfoSpecifics specifics2 = CreateSpecifics(2); 457 const DeviceInfoSpecifics specifics3 = CreateSpecifics(3);
424 DeviceInfoSpecifics specifics3 = CreateSpecifics(3); 458 WriteToStore({specifics1, specifics2, specifics3});
425 batch->WriteData(specifics1.cache_guid(), specifics1.SerializeAsString());
426 batch->WriteData(specifics2.cache_guid(), specifics2.SerializeAsString());
427 batch->WriteData(specifics3.cache_guid(), specifics3.SerializeAsString());
428 store()->CommitWriteBatch(std::move(batch),
429 base::Bind(&VerifyResultIsSuccess));
430
431 InitializeAndPump(); 459 InitializeAndPump();
432 460
433 std::map<std::string, DeviceInfoSpecifics> expected{ 461 const std::map<std::string, DeviceInfoSpecifics> expected{
434 {specifics1.cache_guid(), specifics1}, 462 {specifics1.cache_guid(), specifics1},
435 {specifics3.cache_guid(), specifics3}}; 463 {specifics3.cache_guid(), specifics3}};
436 bridge()->GetData({specifics1.cache_guid(), specifics3.cache_guid()}, 464 bridge()->GetData({specifics1.cache_guid(), specifics3.cache_guid()},
437 base::Bind(&VerifyDataBatch, expected)); 465 base::Bind(&VerifyDataBatch, expected));
438 } 466 }
439 467
440 TEST_F(DeviceInfoSyncBridgeTest, GetDataMissing) { 468 TEST_F(DeviceInfoSyncBridgeTest, GetDataMissing) {
441 InitializeAndPump(); 469 InitializeAndPump();
442 bridge()->GetData({"does_not_exist"}, 470 bridge()->GetData({"does_not_exist"},
443 base::Bind(&VerifyDataBatch, 471 base::Bind(&VerifyDataBatch,
444 std::map<std::string, DeviceInfoSpecifics>())); 472 std::map<std::string, DeviceInfoSpecifics>()));
445 } 473 }
446 474
447 TEST_F(DeviceInfoSyncBridgeTest, GetAllData) { 475 TEST_F(DeviceInfoSyncBridgeTest, GetAllData) {
448 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 476 const DeviceInfoSpecifics specifics1 = CreateSpecifics(1);
449 DeviceInfoSpecifics specifics1 = CreateSpecifics(1); 477 const DeviceInfoSpecifics specifics2 = CreateSpecifics(2);
450 DeviceInfoSpecifics specifics2 = CreateSpecifics(2); 478 WriteToStore({specifics1, specifics2});
451 const std::string& guid1 = specifics1.cache_guid();
452 const std::string& guid2 = specifics2.cache_guid();
453 batch->WriteData(specifics1.cache_guid(), specifics1.SerializeAsString());
454 batch->WriteData(specifics2.cache_guid(), specifics2.SerializeAsString());
455 store()->CommitWriteBatch(std::move(batch),
456 base::Bind(&VerifyResultIsSuccess));
457
458 InitializeAndPump(); 479 InitializeAndPump();
459 480
460 std::map<std::string, DeviceInfoSpecifics> expected{{guid1, specifics1}, 481 const std::map<std::string, DeviceInfoSpecifics> expected{
461 {guid2, specifics2}}; 482 {specifics1.cache_guid(), specifics1},
462 bridge()->GetData({guid1, guid2}, base::Bind(&VerifyDataBatch, expected)); 483 {specifics2.cache_guid(), specifics2}};
484 bridge()->GetData({specifics1.cache_guid(), specifics2.cache_guid()},
485 base::Bind(&VerifyDataBatch, expected));
463 } 486 }
464 487
465 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesEmpty) { 488 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesEmpty) {
466 InitializeAndPump(); 489 InitializeAndPump();
467 EXPECT_EQ(1, change_count()); 490 EXPECT_EQ(1, change_count());
468 const SyncError error = bridge()->ApplySyncChanges( 491 const SyncError error = bridge()->ApplySyncChanges(
469 bridge()->CreateMetadataChangeList(), EntityChangeList()); 492 bridge()->CreateMetadataChangeList(), EntityChangeList());
470 EXPECT_FALSE(error.IsSet()); 493 EXPECT_FALSE(error.IsSet());
471 EXPECT_EQ(1, change_count()); 494 EXPECT_EQ(1, change_count());
472 } 495 }
473 496
474 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesInMemory) { 497 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesInMemory) {
475 InitializeAndPump(); 498 InitializeAndPump();
476 EXPECT_EQ(1, change_count()); 499 EXPECT_EQ(1, change_count());
477 500
478 DeviceInfoSpecifics specifics = CreateSpecifics(1); 501 const DeviceInfoSpecifics specifics = CreateSpecifics(1);
479 const SyncError error_on_add = bridge()->ApplySyncChanges( 502 const SyncError error_on_add = bridge()->ApplySyncChanges(
480 bridge()->CreateMetadataChangeList(), EntityAddList({specifics})); 503 bridge()->CreateMetadataChangeList(), EntityAddList({specifics}));
481 504
482 EXPECT_FALSE(error_on_add.IsSet()); 505 EXPECT_FALSE(error_on_add.IsSet());
483 std::unique_ptr<DeviceInfo> info = 506 std::unique_ptr<DeviceInfo> info =
484 bridge()->GetDeviceInfo(specifics.cache_guid()); 507 bridge()->GetDeviceInfo(specifics.cache_guid());
485 ASSERT_TRUE(info); 508 ASSERT_TRUE(info);
486 VerifyEqual(specifics, *info.get()); 509 VerifyEqual(specifics, *info.get());
487 EXPECT_EQ(2, change_count()); 510 EXPECT_EQ(2, change_count());
488 511
489 const SyncError error_on_delete = bridge()->ApplySyncChanges( 512 const SyncError error_on_delete = bridge()->ApplySyncChanges(
490 bridge()->CreateMetadataChangeList(), 513 bridge()->CreateMetadataChangeList(),
491 {EntityChange::CreateDelete(specifics.cache_guid())}); 514 {EntityChange::CreateDelete(specifics.cache_guid())});
492 515
493 EXPECT_FALSE(error_on_delete.IsSet()); 516 EXPECT_FALSE(error_on_delete.IsSet());
494 EXPECT_FALSE(bridge()->GetDeviceInfo(specifics.cache_guid())); 517 EXPECT_FALSE(bridge()->GetDeviceInfo(specifics.cache_guid()));
495 EXPECT_EQ(3, change_count()); 518 EXPECT_EQ(3, change_count());
496 } 519 }
497 520
498 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesStore) { 521 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesStore) {
499 InitializeAndPump(); 522 InitializeAndPump();
500 EXPECT_EQ(1, change_count()); 523 EXPECT_EQ(1, change_count());
501 524
502 DeviceInfoSpecifics specifics = CreateSpecifics(1); 525 const DeviceInfoSpecifics specifics = CreateSpecifics(1);
503 ModelTypeState state; 526 ModelTypeState state = StateWithEncryption("ekn");
504 state.set_encryption_key_name("ekn");
505 std::unique_ptr<MetadataChangeList> metadata_changes = 527 std::unique_ptr<MetadataChangeList> metadata_changes =
506 bridge()->CreateMetadataChangeList(); 528 bridge()->CreateMetadataChangeList();
507 metadata_changes->UpdateModelTypeState(state); 529 metadata_changes->UpdateModelTypeState(state);
508 530
509 const SyncError error = bridge()->ApplySyncChanges( 531 const SyncError error = bridge()->ApplySyncChanges(
510 std::move(metadata_changes), EntityAddList({specifics})); 532 std::move(metadata_changes), EntityAddList({specifics}));
511 EXPECT_FALSE(error.IsSet()); 533 EXPECT_FALSE(error.IsSet());
512 EXPECT_EQ(2, change_count()); 534 EXPECT_EQ(2, change_count());
513 535
514 RestartBridge(); 536 RestartBridge();
515 537
516 std::unique_ptr<DeviceInfo> info = 538 std::unique_ptr<DeviceInfo> info =
517 bridge()->GetDeviceInfo(specifics.cache_guid()); 539 bridge()->GetDeviceInfo(specifics.cache_guid());
518 ASSERT_TRUE(info); 540 ASSERT_TRUE(info);
519 VerifyEqual(specifics, *info.get()); 541 VerifyEqual(specifics, *info.get());
520 542
521 EXPECT_TRUE(processor()->metadata()); 543 EXPECT_TRUE(processor()->metadata());
522 EXPECT_EQ(state.encryption_key_name(), 544 EXPECT_EQ(state.encryption_key_name(),
523 processor()->metadata()->GetModelTypeState().encryption_key_name()); 545 processor()->metadata()->GetModelTypeState().encryption_key_name());
524 } 546 }
525 547
526 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesWithLocalGuid) { 548 TEST_F(DeviceInfoSyncBridgeTest, ApplySyncChangesWithLocalGuid) {
527 InitializeAndPump(); 549 InitializeAndPump();
528 550
529 // The bridge should ignore these changes using this specifics because its 551 // The bridge should ignore these changes using this specifics because its
530 // guid will match the local device. 552 // guid will match the local device.
531 DeviceInfoSpecifics specifics = CreateSpecifics(kDefaultLocalSuffix); 553 const DeviceInfoSpecifics specifics = CreateSpecifics(kDefaultLocalSuffix);
532 554
533 // Should have a single change from reconciliation. 555 // Should have a single change from reconciliation.
534 EXPECT_TRUE( 556 EXPECT_TRUE(
535 bridge()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid())); 557 bridge()->GetDeviceInfo(local_device()->GetLocalDeviceInfo()->guid()));
536 EXPECT_EQ(1, change_count()); 558 EXPECT_EQ(1, change_count());
537 // Ensure |last_updated| is about now, plus or minus a little bit. 559 // Ensure |last_updated| is about now, plus or minus a little bit.
538 Time last_updated(ProtoTimeToTime(processor() 560 const Time last_updated(ProtoTimeToTime(processor()
539 ->put_multimap() 561 ->put_multimap()
540 .begin() 562 .begin()
541 ->second->specifics.device_info() 563 ->second->specifics.device_info()
542 .last_updated_timestamp())); 564 .last_updated_timestamp()));
543 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated); 565 EXPECT_LT(Time::Now() - TimeDelta::FromMinutes(1), last_updated);
544 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated); 566 EXPECT_GT(Time::Now() + TimeDelta::FromMinutes(1), last_updated);
545 567
546 const SyncError error_on_add = bridge()->ApplySyncChanges( 568 const SyncError error_on_add = bridge()->ApplySyncChanges(
547 bridge()->CreateMetadataChangeList(), EntityAddList({specifics})); 569 bridge()->CreateMetadataChangeList(), EntityAddList({specifics}));
548 EXPECT_FALSE(error_on_add.IsSet()); 570 EXPECT_FALSE(error_on_add.IsSet());
549 EXPECT_EQ(1, change_count()); 571 EXPECT_EQ(1, change_count());
550 572
551 const SyncError error_on_delete = bridge()->ApplySyncChanges( 573 const SyncError error_on_delete = bridge()->ApplySyncChanges(
552 bridge()->CreateMetadataChangeList(), 574 bridge()->CreateMetadataChangeList(),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 TEST_F(DeviceInfoSyncBridgeTest, MergeWithData) { 606 TEST_F(DeviceInfoSyncBridgeTest, MergeWithData) {
585 const DeviceInfoSpecifics unique_local = CreateSpecifics(1); 607 const DeviceInfoSpecifics unique_local = CreateSpecifics(1);
586 DeviceInfoSpecifics conflict_local = CreateSpecifics(2); 608 DeviceInfoSpecifics conflict_local = CreateSpecifics(2);
587 DeviceInfoSpecifics conflict_remote = CreateSpecifics(3); 609 DeviceInfoSpecifics conflict_remote = CreateSpecifics(3);
588 const DeviceInfoSpecifics unique_remote = CreateSpecifics(4); 610 const DeviceInfoSpecifics unique_remote = CreateSpecifics(4);
589 611
590 const std::string conflict_guid = "conflict_guid"; 612 const std::string conflict_guid = "conflict_guid";
591 conflict_local.set_cache_guid(conflict_guid); 613 conflict_local.set_cache_guid(conflict_guid);
592 conflict_remote.set_cache_guid(conflict_guid); 614 conflict_remote.set_cache_guid(conflict_guid);
593 615
594 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch(); 616 WriteToStore({unique_local, conflict_local});
595 batch->WriteData(unique_local.cache_guid(), unique_local.SerializeAsString());
596 batch->WriteData(conflict_local.cache_guid(),
597 conflict_local.SerializeAsString());
598 store()->CommitWriteBatch(std::move(batch),
599 base::Bind(&VerifyResultIsSuccess));
600
601 InitializeAndPump(); 617 InitializeAndPump();
602 EXPECT_EQ(1, change_count()); 618 EXPECT_EQ(1, change_count());
603 619
604 EntityDataMap remote_input; 620 ModelTypeState state = StateWithEncryption("ekn");
605 remote_input[conflict_remote.cache_guid()] =
606 SpecificsToEntity(conflict_remote);
607 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote);
608
609 ModelTypeState state;
610 state.set_encryption_key_name("ekn");
611 std::unique_ptr<MetadataChangeList> metadata_changes = 621 std::unique_ptr<MetadataChangeList> metadata_changes =
612 bridge()->CreateMetadataChangeList(); 622 bridge()->CreateMetadataChangeList();
613 metadata_changes->UpdateModelTypeState(state); 623 metadata_changes->UpdateModelTypeState(state);
614 624
615 const SyncError error = 625 const SyncError error = bridge()->MergeSyncData(
616 bridge()->MergeSyncData(std::move(metadata_changes), remote_input); 626 std::move(metadata_changes),
627 InlineEntityDataMap({conflict_remote, unique_remote}));
617 EXPECT_FALSE(error.IsSet()); 628 EXPECT_FALSE(error.IsSet());
618 EXPECT_EQ(2, change_count()); 629 EXPECT_EQ(2, change_count());
619 630
620 // The remote should beat the local in conflict. 631 // The remote should beat the local in conflict.
621 EXPECT_EQ(4u, bridge()->GetAllDeviceInfo().size()); 632 EXPECT_EQ(4u, bridge()->GetAllDeviceInfo().size());
622 VerifyEqual(unique_local, 633 VerifyEqual(unique_local,
623 *bridge()->GetDeviceInfo(unique_local.cache_guid()).get()); 634 *bridge()->GetDeviceInfo(unique_local.cache_guid()).get());
624 VerifyEqual(unique_remote, 635 VerifyEqual(unique_remote,
625 *bridge()->GetDeviceInfo(unique_remote.cache_guid()).get()); 636 *bridge()->GetDeviceInfo(unique_remote.cache_guid()).get());
626 VerifyEqual(conflict_remote, *bridge()->GetDeviceInfo(conflict_guid).get()); 637 VerifyEqual(conflict_remote, *bridge()->GetDeviceInfo(conflict_guid).get());
627 638
628 // bridge should have told the processor about the existance of unique_local. 639 // bridge should have told the processor about the existance of unique_local.
629 EXPECT_TRUE(processor()->delete_set().empty()); 640 EXPECT_TRUE(processor()->delete_set().empty());
630 EXPECT_EQ(3u, processor()->put_multimap().size()); 641 EXPECT_EQ(3u, processor()->put_multimap().size());
631 EXPECT_EQ(1u, processor()->put_multimap().count(unique_local.cache_guid())); 642 EXPECT_EQ(1u, processor()->put_multimap().count(unique_local.cache_guid()));
632 const auto& it = processor()->put_multimap().find(unique_local.cache_guid()); 643 const auto& it = processor()->put_multimap().find(unique_local.cache_guid());
633 ASSERT_NE(processor()->put_multimap().end(), it); 644 ASSERT_NE(processor()->put_multimap().end(), it);
634 VerifyEqual(unique_local, it->second->specifics.device_info()); 645 VerifyEqual(unique_local, it->second->specifics.device_info());
635 646
636 RestartBridge(); 647 RestartBridge();
637 EXPECT_EQ(state.encryption_key_name(), 648 EXPECT_EQ(state.encryption_key_name(),
638 processor()->metadata()->GetModelTypeState().encryption_key_name()); 649 processor()->metadata()->GetModelTypeState().encryption_key_name());
639 } 650 }
640 651
641 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) { 652 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) {
642 const DeviceInfo* provider_info = local_device()->GetLocalDeviceInfo(); 653 // If not recent, then reconcile is going to try to send an updated version to
643 auto specifics = base::MakeUnique<DeviceInfoSpecifics>(CreateSpecifics(0)); 654 // Sync, which makes interpreting change_count() more difficult.
644 specifics->set_last_updated_timestamp(TimeToProtoTime(Time::Now())); 655 const DeviceInfoSpecifics specifics =
645 const std::string guid = provider_info->guid(); 656 CreateSpecifics(kDefaultLocalSuffix, Time::Now());
646 657 WriteToStore({specifics});
647 std::unique_ptr<WriteBatch> batch = store()->CreateWriteBatch();
648 batch->WriteData(guid, specifics->SerializeAsString());
649 store()->CommitWriteBatch(std::move(batch),
650 base::Bind(&VerifyResultIsSuccess));
651
652 InitializeAndPump(); 658 InitializeAndPump();
653 659
654 EntityDataMap remote_input;
655 remote_input[guid] = SpecificsToEntity(*specifics);
656
657 const SyncError error = bridge()->MergeSyncData( 660 const SyncError error = bridge()->MergeSyncData(
658 bridge()->CreateMetadataChangeList(), remote_input); 661 bridge()->CreateMetadataChangeList(), InlineEntityDataMap({specifics}));
659 EXPECT_FALSE(error.IsSet()); 662 EXPECT_FALSE(error.IsSet());
660 EXPECT_EQ(0, change_count()); 663 EXPECT_EQ(0, change_count());
661 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); 664 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
662 EXPECT_TRUE(processor()->delete_set().empty()); 665 EXPECT_TRUE(processor()->delete_set().empty());
663 EXPECT_TRUE(processor()->put_multimap().empty()); 666 EXPECT_TRUE(processor()->put_multimap().empty());
664 } 667 }
665 668
666 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuidBeforeReconcile) { 669 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuidBeforeReconcile) {
667 InitializeBridge(); 670 InitializeBridge();
668 const DeviceInfoSpecifics specifics = CreateSpecifics(kDefaultLocalSuffix);
669 671
670 // The message loop is never pumped, which means local data/metadata is never 672 // The message loop is never pumped, which means local data/metadata is never
671 // loaded, and thus reconcile is never called. The bridge should ignore this 673 // loaded, and thus reconcile is never called. The bridge should ignore this
672 // EntityData because its cache guid is the same the local device's. 674 // EntityData because its cache guid is the same the local device's.
673 const SyncError error = bridge()->MergeSyncData( 675 const SyncError error = bridge()->MergeSyncData(
674 bridge()->CreateMetadataChangeList(), 676 bridge()->CreateMetadataChangeList(),
675 {{specifics.cache_guid(), SpecificsToEntity(specifics)}}); 677 InlineEntityDataMap({CreateSpecifics(kDefaultLocalSuffix)}));
676 EXPECT_FALSE(error.IsSet()); 678 EXPECT_FALSE(error.IsSet());
677 EXPECT_EQ(0, change_count()); 679 EXPECT_EQ(0, change_count());
678 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 680 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
679 } 681 }
680 682
681 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { 683 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) {
682 InitializeAndPump(); 684 InitializeAndPump();
683 EXPECT_EQ(1, bridge()->CountActiveDevices()); 685 EXPECT_EQ(1, bridge()->CountActiveDevices());
684 686
685 DeviceInfoSpecifics specifics = CreateSpecifics(0); 687 // Regardless of the time, these following two ApplySyncChanges(...) calls
686 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 688 // have the same guid as the local device.
687 EntityAddList({specifics})); 689 bridge()->ApplySyncChanges(
690 bridge()->CreateMetadataChangeList(),
691 EntityAddList({CreateSpecifics(kDefaultLocalSuffix)}));
688 EXPECT_EQ(1, bridge()->CountActiveDevices()); 692 EXPECT_EQ(1, bridge()->CountActiveDevices());
689 693
690 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); 694 bridge()->ApplySyncChanges(
691 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 695 bridge()->CreateMetadataChangeList(),
692 EntityAddList({specifics})); 696 EntityAddList({CreateSpecifics(kDefaultLocalSuffix, Time::Now())}));
693 EXPECT_EQ(1, bridge()->CountActiveDevices()); 697 EXPECT_EQ(1, bridge()->CountActiveDevices());
694 698
695 specifics.set_cache_guid("non-local"); 699 // A different guid will actually contribute to the count.
696 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 700 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
697 EntityAddList({specifics})); 701 EntityAddList({CreateSpecifics(1, Time::Now())}));
698 EXPECT_EQ(2, bridge()->CountActiveDevices()); 702 EXPECT_EQ(2, bridge()->CountActiveDevices());
699 703
700 // Now set time to long ago in the past, it should not be active anymore. 704 // Now set time to long ago in the past, it should not be active anymore.
701 specifics.set_last_updated_timestamp(TimeToProtoTime(Time()));
702 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 705 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
703 EntityAddList({specifics})); 706 EntityAddList({CreateSpecifics(1)}));
704 EXPECT_EQ(1, bridge()->CountActiveDevices()); 707 EXPECT_EQ(1, bridge()->CountActiveDevices());
705 } 708 }
706 709
707 TEST_F(DeviceInfoSyncBridgeTest, MultipleOnProviderInitialized) { 710 TEST_F(DeviceInfoSyncBridgeTest, MultipleOnProviderInitialized) {
708 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>()); 711 set_provider(base::MakeUnique<LocalDeviceInfoProviderMock>());
709 InitializeAndPump(); 712 InitializeAndPump();
710 EXPECT_EQ(nullptr, processor()->metadata()); 713 EXPECT_EQ(nullptr, processor()->metadata());
711 714
712 // Verify the processor was given metadata. 715 // Verify the processor was given metadata.
713 local_device()->Initialize(CreateModel(0)); 716 local_device()->Initialize(CreateModel(0));
(...skipping 23 matching lines...) Expand all
737 ForcePulse(); 740 ForcePulse();
738 EXPECT_EQ(2, change_count()); 741 EXPECT_EQ(2, change_count());
739 EXPECT_EQ(2u, processor()->put_multimap().size()); 742 EXPECT_EQ(2u, processor()->put_multimap().size());
740 } 743 }
741 744
742 TEST_F(DeviceInfoSyncBridgeTest, DisableSync) { 745 TEST_F(DeviceInfoSyncBridgeTest, DisableSync) {
743 InitializeAndPump(); 746 InitializeAndPump();
744 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); 747 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
745 EXPECT_EQ(1, change_count()); 748 EXPECT_EQ(1, change_count());
746 749
747 DeviceInfoSpecifics specifics = CreateSpecifics(1); 750 const DeviceInfoSpecifics specifics = CreateSpecifics(1);
748 const SyncError error = bridge()->ApplySyncChanges( 751 const SyncError error = bridge()->ApplySyncChanges(
749 bridge()->CreateMetadataChangeList(), EntityAddList({specifics})); 752 bridge()->CreateMetadataChangeList(), EntityAddList({specifics}));
750 753
751 EXPECT_FALSE(error.IsSet()); 754 EXPECT_FALSE(error.IsSet());
752 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); 755 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size());
753 EXPECT_EQ(2, change_count()); 756 EXPECT_EQ(2, change_count());
754 757
755 // Should clear out all local data and notify observers. 758 // Should clear out all local data and notify observers.
756 bridge()->DisableSync(); 759 bridge()->DisableSync();
757 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 760 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
758 EXPECT_EQ(3, change_count()); 761 EXPECT_EQ(3, change_count());
759 762
760 // Reloading from storage shouldn't contain remote data. 763 // Reloading from storage shouldn't contain remote data.
761 RestartBridge(); 764 RestartBridge();
762 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); 765 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
763 EXPECT_EQ(4, change_count()); 766 EXPECT_EQ(4, change_count());
764 } 767 }
765 768
766 } // namespace 769 } // namespace
767 770
768 } // namespace syncer 771 } // namespace syncer
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698