| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "components/sync/base/time.h" | 16 #include "components/sync/base/time.h" |
| 17 #include "components/sync/device_info/local_device_info_provider_mock.h" | 17 #include "components/sync/device_info/local_device_info_provider_mock.h" |
| 18 #include "components/sync/model/data_batch.h" | 18 #include "components/sync/model/data_batch.h" |
| 19 #include "components/sync/model/data_type_error_handler_mock.h" | 19 #include "components/sync/model/data_type_error_handler_mock.h" |
| 20 #include "components/sync/model/entity_data.h" | 20 #include "components/sync/model/entity_data.h" |
| 21 #include "components/sync/model/fake_model_type_change_processor.h" | 21 #include "components/sync/model/fake_model_type_change_processor.h" |
| 22 #include "components/sync/model/metadata_batch.h" | 22 #include "components/sync/model/metadata_batch.h" |
| 23 #include "components/sync/model/model_type_store_test_util.h" | 23 #include "components/sync/model/model_type_store_test_util.h" |
| 24 #include "components/sync/protocol/model_type_state.pb.h" | 24 #include "components/sync/protocol/model_type_state.pb.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace syncer { | 27 namespace syncer { |
| 28 | 28 |
| 29 using base::OneShotTimer; | |
| 30 using base::Time; | 29 using base::Time; |
| 31 using base::TimeDelta; | 30 using base::TimeDelta; |
| 32 using sync_pb::DeviceInfoSpecifics; | 31 using sync_pb::DeviceInfoSpecifics; |
| 33 using sync_pb::EntitySpecifics; | 32 using sync_pb::EntitySpecifics; |
| 34 using sync_pb::ModelTypeState; | 33 using sync_pb::ModelTypeState; |
| 35 | 34 |
| 36 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; | 35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; |
| 37 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList; | 36 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList; |
| 38 using RecordList = ModelTypeStore::RecordList; | 37 using RecordList = ModelTypeStore::RecordList; |
| 39 using Result = ModelTypeStore::Result; | 38 using Result = ModelTypeStore::Result; |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 DeviceInfoSyncBridge* bridge() { | 255 DeviceInfoSyncBridge* bridge() { |
| 257 EXPECT_TRUE(bridge_); | 256 EXPECT_TRUE(bridge_); |
| 258 return bridge_.get(); | 257 return bridge_.get(); |
| 259 } | 258 } |
| 260 | 259 |
| 261 RecordingModelTypeChangeProcessor* processor() { | 260 RecordingModelTypeChangeProcessor* processor() { |
| 262 EXPECT_TRUE(processor_); | 261 EXPECT_TRUE(processor_); |
| 263 return processor_; | 262 return processor_; |
| 264 } | 263 } |
| 265 | 264 |
| 266 const OneShotTimer& pulse_timer() { return bridge()->pulse_timer_; } | |
| 267 | |
| 268 // Should only be called after the bridge has been initialized. Will first | 265 // Should only be called after the bridge has been initialized. Will first |
| 269 // recover the bridge's store, so another can be initialized later, and then | 266 // recover the bridge's store, so another can be initialized later, and then |
| 270 // deletes the bridge. | 267 // deletes the bridge. |
| 271 void PumpAndShutdown() { | 268 void PumpAndShutdown() { |
| 272 ASSERT_TRUE(bridge_); | 269 ASSERT_TRUE(bridge_); |
| 273 base::RunLoop().RunUntilIdle(); | 270 base::RunLoop().RunUntilIdle(); |
| 274 std::swap(store_, bridge_->store_); | 271 std::swap(store_, bridge_->store_); |
| 275 bridge_->RemoveObserver(this); | 272 bridge_->RemoveObserver(this); |
| 276 bridge_.reset(); | 273 bridge_.reset(); |
| 277 } | 274 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 local_device()->Initialize(CreateModel(1)); | 343 local_device()->Initialize(CreateModel(1)); |
| 347 base::RunLoop().RunUntilIdle(); | 344 base::RunLoop().RunUntilIdle(); |
| 348 | 345 |
| 349 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); | 346 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); |
| 350 ASSERT_EQ(1u, devices.size()); | 347 ASSERT_EQ(1u, devices.size()); |
| 351 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); | 348 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); |
| 352 | 349 |
| 353 EXPECT_TRUE(processor()->metadata()); | 350 EXPECT_TRUE(processor()->metadata()); |
| 354 } | 351 } |
| 355 | 352 |
| 356 // Simulate shutting down sync during the ModelTypeStore callbacks. The pulse | |
| 357 // timer should still be initialized, even though reconcile never occurs. | |
| 358 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderDuringInit) { | |
| 359 InitializeBridge(); | |
| 360 local_device()->Clear(); | |
| 361 base::RunLoop().RunUntilIdle(); | |
| 362 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); | |
| 363 EXPECT_TRUE(pulse_timer().IsRunning()); | |
| 364 } | |
| 365 | |
| 366 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) { | 353 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) { |
| 367 InitializeBridge(); | 354 InitializeBridge(); |
| 368 const std::string guid = "abc"; | 355 const std::string guid = "abc"; |
| 369 EntitySpecifics entity_specifics; | 356 EntitySpecifics entity_specifics; |
| 370 entity_specifics.mutable_device_info()->set_cache_guid(guid); | 357 entity_specifics.mutable_device_info()->set_cache_guid(guid); |
| 371 EntityData entity_data; | 358 EntityData entity_data; |
| 372 entity_data.specifics = entity_specifics; | 359 entity_data.specifics = entity_specifics; |
| 373 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data)); | 360 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data)); |
| 374 } | 361 } |
| 375 | 362 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) { | 558 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) { |
| 572 InitializeAndPump(); | 559 InitializeAndPump(); |
| 573 EXPECT_EQ(1, change_count()); | 560 EXPECT_EQ(1, change_count()); |
| 574 const SyncError error = | 561 const SyncError error = |
| 575 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | 562 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), |
| 576 {EntityChange::CreateDelete("guid")}); | 563 {EntityChange::CreateDelete("guid")}); |
| 577 EXPECT_FALSE(error.IsSet()); | 564 EXPECT_FALSE(error.IsSet()); |
| 578 EXPECT_EQ(1, change_count()); | 565 EXPECT_EQ(1, change_count()); |
| 579 } | 566 } |
| 580 | 567 |
| 581 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndApply) { | |
| 582 // This will initialize the provider a first time. | |
| 583 InitializeAndPump(); | |
| 584 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | |
| 585 | |
| 586 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now()); | |
| 587 | |
| 588 local_device()->Clear(); | |
| 589 SyncError error = bridge()->ApplySyncChanges( | |
| 590 bridge()->CreateMetadataChangeList(), EntityAddList({specifics})); | |
| 591 EXPECT_FALSE(error.IsSet()); | |
| 592 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | |
| 593 | |
| 594 local_device()->Initialize(CreateModel(kDefaultLocalSuffix)); | |
| 595 error = bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | |
| 596 EntityAddList({specifics})); | |
| 597 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); | |
| 598 } | |
| 599 | |
| 600 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { | 568 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { |
| 601 InitializeAndPump(); | 569 InitializeAndPump(); |
| 602 EXPECT_EQ(1, change_count()); | 570 EXPECT_EQ(1, change_count()); |
| 603 const SyncError error = bridge()->MergeSyncData( | 571 const SyncError error = bridge()->MergeSyncData( |
| 604 bridge()->CreateMetadataChangeList(), EntityDataMap()); | 572 bridge()->CreateMetadataChangeList(), EntityDataMap()); |
| 605 EXPECT_FALSE(error.IsSet()); | 573 EXPECT_FALSE(error.IsSet()); |
| 606 EXPECT_EQ(1, change_count()); | 574 EXPECT_EQ(1, change_count()); |
| 607 // TODO(skym): Stop sending local twice. The first of the two puts will | 575 // TODO(skym): Stop sending local twice. The first of the two puts will |
| 608 // probably happen before the processor is tracking metadata yet, and so there | 576 // probably happen before the processor is tracking metadata yet, and so there |
| 609 // should not be much overhead. | 577 // should not be much overhead. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 // loaded, and thus reconcile is never called. The bridge should ignore this | 671 // loaded, and thus reconcile is never called. The bridge should ignore this |
| 704 // EntityData because its cache guid is the same the local device's. | 672 // EntityData because its cache guid is the same the local device's. |
| 705 const SyncError error = bridge()->MergeSyncData( | 673 const SyncError error = bridge()->MergeSyncData( |
| 706 bridge()->CreateMetadataChangeList(), | 674 bridge()->CreateMetadataChangeList(), |
| 707 {{specifics.cache_guid(), SpecificsToEntity(specifics)}}); | 675 {{specifics.cache_guid(), SpecificsToEntity(specifics)}}); |
| 708 EXPECT_FALSE(error.IsSet()); | 676 EXPECT_FALSE(error.IsSet()); |
| 709 EXPECT_EQ(0, change_count()); | 677 EXPECT_EQ(0, change_count()); |
| 710 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); | 678 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); |
| 711 } | 679 } |
| 712 | 680 |
| 713 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndMerge) { | |
| 714 // This will initialize the provider a first time. | |
| 715 InitializeAndPump(); | |
| 716 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | |
| 717 | |
| 718 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now()); | |
| 719 | |
| 720 local_device()->Clear(); | |
| 721 SyncError error = bridge()->MergeSyncData( | |
| 722 bridge()->CreateMetadataChangeList(), InlineEntityDataMap({specifics})); | |
| 723 EXPECT_FALSE(error.IsSet()); | |
| 724 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | |
| 725 local_device()->Initialize(CreateModel(kDefaultLocalSuffix)); | |
| 726 error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), | |
| 727 InlineEntityDataMap({specifics})); | |
| 728 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size()); | |
| 729 } | |
| 730 | |
| 731 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { | 681 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { |
| 732 InitializeAndPump(); | 682 InitializeAndPump(); |
| 733 EXPECT_EQ(1, bridge()->CountActiveDevices()); | 683 EXPECT_EQ(1, bridge()->CountActiveDevices()); |
| 734 | 684 |
| 735 DeviceInfoSpecifics specifics = CreateSpecifics(0); | 685 DeviceInfoSpecifics specifics = CreateSpecifics(0); |
| 736 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | 686 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), |
| 737 EntityAddList({specifics})); | 687 EntityAddList({specifics})); |
| 738 EXPECT_EQ(1, bridge()->CountActiveDevices()); | 688 EXPECT_EQ(1, bridge()->CountActiveDevices()); |
| 739 | 689 |
| 740 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); | 690 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 | 759 |
| 810 // Reloading from storage shouldn't contain remote data. | 760 // Reloading from storage shouldn't contain remote data. |
| 811 RestartBridge(); | 761 RestartBridge(); |
| 812 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); | 762 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); |
| 813 EXPECT_EQ(4, change_count()); | 763 EXPECT_EQ(4, change_count()); |
| 814 } | 764 } |
| 815 | 765 |
| 816 } // namespace | 766 } // namespace |
| 817 | 767 |
| 818 } // namespace syncer | 768 } // namespace syncer |
| OLD | NEW |