OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/device_info/device_info_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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 EntityChangeList EntityAddList( | 151 EntityChangeList EntityAddList( |
152 const std::vector<DeviceInfoSpecifics>& specifics_list) { | 152 const std::vector<DeviceInfoSpecifics>& specifics_list) { |
153 EntityChangeList changes; | 153 EntityChangeList changes; |
154 for (const auto& specifics : specifics_list) { | 154 for (const auto& specifics : specifics_list) { |
155 changes.push_back(EntityChange::CreateAdd(specifics.cache_guid(), | 155 changes.push_back(EntityChange::CreateAdd(specifics.cache_guid(), |
156 SpecificsToEntity(specifics))); | 156 SpecificsToEntity(specifics))); |
157 } | 157 } |
158 return changes; | 158 return changes; |
159 } | 159 } |
160 | 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 | |
174 } // namespace | 161 } // namespace |
175 | 162 |
176 class DeviceInfoSyncBridgeTest : public testing::Test, | 163 class DeviceInfoSyncBridgeTest : public testing::Test, |
177 public DeviceInfoTracker::Observer { | 164 public DeviceInfoTracker::Observer { |
178 protected: | 165 protected: |
179 DeviceInfoSyncBridgeTest() | 166 DeviceInfoSyncBridgeTest() |
180 : store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), | 167 : store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), |
181 provider_(new LocalDeviceInfoProviderMock()) { | 168 provider_(new LocalDeviceInfoProviderMock()) { |
182 provider_->Initialize(CreateModel(kDefaultLocalSuffix)); | 169 provider_->Initialize(CreateModel(kDefaultLocalSuffix)); |
183 } | 170 } |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 auto error2 = bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | 552 auto error2 = bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), |
566 EntityAddList({specifics})); | 553 EntityAddList({specifics})); |
567 EXPECT_FALSE(error2); | 554 EXPECT_FALSE(error2); |
568 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); | 555 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); |
569 } | 556 } |
570 | 557 |
571 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { | 558 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { |
572 InitializeAndPump(); | 559 InitializeAndPump(); |
573 EXPECT_EQ(1, change_count()); | 560 EXPECT_EQ(1, change_count()); |
574 auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), | 561 auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), |
575 EntityDataMap()); | 562 EntityChangeList()); |
576 EXPECT_FALSE(error); | 563 EXPECT_FALSE(error); |
577 EXPECT_EQ(1, change_count()); | 564 EXPECT_EQ(1, change_count()); |
578 // TODO(skym): Stop sending local twice. The first of the two puts will | 565 // TODO(skym): Stop sending local twice. The first of the two puts will |
579 // probably happen before the processor is tracking metadata yet, and so there | 566 // probably happen before the processor is tracking metadata yet, and so there |
580 // should not be much overhead. | 567 // should not be much overhead. |
581 EXPECT_EQ(2u, processor().put_multimap().size()); | 568 EXPECT_EQ(2u, processor().put_multimap().size()); |
582 EXPECT_EQ(2u, processor().put_multimap().count( | 569 EXPECT_EQ(2u, processor().put_multimap().count( |
583 local_device()->GetLocalDeviceInfo()->guid())); | 570 local_device()->GetLocalDeviceInfo()->guid())); |
584 EXPECT_EQ(0u, processor().delete_set().size()); | 571 EXPECT_EQ(0u, processor().delete_set().size()); |
585 } | 572 } |
(...skipping 10 matching lines...) Expand all Loading... |
596 | 583 |
597 WriteToStore({unique_local, conflict_local}); | 584 WriteToStore({unique_local, conflict_local}); |
598 InitializeAndPump(); | 585 InitializeAndPump(); |
599 EXPECT_EQ(1, change_count()); | 586 EXPECT_EQ(1, change_count()); |
600 | 587 |
601 ModelTypeState state = StateWithEncryption("ekn"); | 588 ModelTypeState state = StateWithEncryption("ekn"); |
602 std::unique_ptr<MetadataChangeList> metadata_changes = | 589 std::unique_ptr<MetadataChangeList> metadata_changes = |
603 bridge()->CreateMetadataChangeList(); | 590 bridge()->CreateMetadataChangeList(); |
604 metadata_changes->UpdateModelTypeState(state); | 591 metadata_changes->UpdateModelTypeState(state); |
605 | 592 |
606 auto error = bridge()->MergeSyncData( | 593 auto error = |
607 std::move(metadata_changes), | 594 bridge()->MergeSyncData(std::move(metadata_changes), |
608 InlineEntityDataMap({conflict_remote, unique_remote})); | 595 EntityAddList({conflict_remote, unique_remote})); |
609 EXPECT_FALSE(error); | 596 EXPECT_FALSE(error); |
610 EXPECT_EQ(2, change_count()); | 597 EXPECT_EQ(2, change_count()); |
611 | 598 |
612 // The remote should beat the local in conflict. | 599 // The remote should beat the local in conflict. |
613 EXPECT_EQ(4u, bridge()->GetAllDeviceInfo().size()); | 600 EXPECT_EQ(4u, bridge()->GetAllDeviceInfo().size()); |
614 VerifyEqual(unique_local, | 601 VerifyEqual(unique_local, |
615 *bridge()->GetDeviceInfo(unique_local.cache_guid()).get()); | 602 *bridge()->GetDeviceInfo(unique_local.cache_guid()).get()); |
616 VerifyEqual(unique_remote, | 603 VerifyEqual(unique_remote, |
617 *bridge()->GetDeviceInfo(unique_remote.cache_guid()).get()); | 604 *bridge()->GetDeviceInfo(unique_remote.cache_guid()).get()); |
618 VerifyEqual(conflict_remote, *bridge()->GetDeviceInfo(conflict_guid).get()); | 605 VerifyEqual(conflict_remote, *bridge()->GetDeviceInfo(conflict_guid).get()); |
(...skipping 13 matching lines...) Expand all Loading... |
632 | 619 |
633 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) { | 620 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) { |
634 // If not recent, then reconcile is going to try to send an updated version to | 621 // If not recent, then reconcile is going to try to send an updated version to |
635 // Sync, which makes interpreting change_count() more difficult. | 622 // Sync, which makes interpreting change_count() more difficult. |
636 const DeviceInfoSpecifics specifics = | 623 const DeviceInfoSpecifics specifics = |
637 CreateSpecifics(kDefaultLocalSuffix, Time::Now()); | 624 CreateSpecifics(kDefaultLocalSuffix, Time::Now()); |
638 WriteToStore({specifics}); | 625 WriteToStore({specifics}); |
639 InitializeAndPump(); | 626 InitializeAndPump(); |
640 | 627 |
641 auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), | 628 auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), |
642 InlineEntityDataMap({specifics})); | 629 EntityAddList({specifics})); |
643 EXPECT_FALSE(error); | 630 EXPECT_FALSE(error); |
644 EXPECT_EQ(0, change_count()); | 631 EXPECT_EQ(0, change_count()); |
645 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | 632 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); |
646 EXPECT_TRUE(processor().delete_set().empty()); | 633 EXPECT_TRUE(processor().delete_set().empty()); |
647 EXPECT_TRUE(processor().put_multimap().empty()); | 634 EXPECT_TRUE(processor().put_multimap().empty()); |
648 } | 635 } |
649 | 636 |
650 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuidBeforeReconcile) { | 637 TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuidBeforeReconcile) { |
651 InitializeBridge(); | 638 InitializeBridge(); |
652 | 639 |
653 // The message loop is never pumped, which means local data/metadata is never | 640 // The message loop is never pumped, which means local data/metadata is never |
654 // loaded, and thus reconcile is never called. The bridge should ignore this | 641 // loaded, and thus reconcile is never called. The bridge should ignore this |
655 // EntityData because its cache guid is the same the local device's. | 642 // EntityData because its cache guid is the same the local device's. |
656 auto error = bridge()->MergeSyncData( | 643 auto error = bridge()->MergeSyncData( |
657 bridge()->CreateMetadataChangeList(), | 644 bridge()->CreateMetadataChangeList(), |
658 InlineEntityDataMap({CreateSpecifics(kDefaultLocalSuffix)})); | 645 EntityAddList({CreateSpecifics(kDefaultLocalSuffix)})); |
659 EXPECT_FALSE(error); | 646 EXPECT_FALSE(error); |
660 EXPECT_EQ(0, change_count()); | 647 EXPECT_EQ(0, change_count()); |
661 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); | 648 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); |
662 } | 649 } |
663 | 650 |
664 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndMerge) { | 651 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndMerge) { |
665 // This will initialize the provider a first time. | 652 // This will initialize the provider a first time. |
666 InitializeAndPump(); | 653 InitializeAndPump(); |
667 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | 654 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); |
668 | 655 |
669 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now()); | 656 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now()); |
670 | 657 |
671 local_device()->Clear(); | 658 local_device()->Clear(); |
672 auto error1 = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), | 659 auto error1 = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), |
673 InlineEntityDataMap({specifics})); | 660 EntityAddList({specifics})); |
674 EXPECT_FALSE(error1); | 661 EXPECT_FALSE(error1); |
675 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | 662 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); |
676 | 663 |
677 local_device()->Initialize(CreateModel(kDefaultLocalSuffix)); | 664 local_device()->Initialize(CreateModel(kDefaultLocalSuffix)); |
678 auto error2 = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), | 665 auto error2 = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), |
679 InlineEntityDataMap({specifics})); | 666 EntityAddList({specifics})); |
680 EXPECT_FALSE(error2); | 667 EXPECT_FALSE(error2); |
681 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); | 668 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); |
682 } | 669 } |
683 | 670 |
684 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { | 671 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { |
685 InitializeAndPump(); | 672 InitializeAndPump(); |
686 EXPECT_EQ(1, bridge()->CountActiveDevices()); | 673 EXPECT_EQ(1, bridge()->CountActiveDevices()); |
687 | 674 |
688 // Regardless of the time, these following two ApplySyncChanges(...) calls | 675 // Regardless of the time, these following two ApplySyncChanges(...) calls |
689 // have the same guid as the local device. | 676 // have the same guid as the local device. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 | 750 |
764 // Reloading from storage shouldn't contain remote data. | 751 // Reloading from storage shouldn't contain remote data. |
765 RestartBridge(); | 752 RestartBridge(); |
766 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | 753 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); |
767 EXPECT_EQ(4, change_count()); | 754 EXPECT_EQ(4, change_count()); |
768 } | 755 } |
769 | 756 |
770 } // namespace | 757 } // namespace |
771 | 758 |
772 } // namespace syncer | 759 } // namespace syncer |
OLD | NEW |