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

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

Issue 2568543003: [Sync] Actively guard against provider being cleared in DeviceInfoSyncBridge. (Closed)
Patch Set: Updated comments slightly. 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 | « components/sync/device_info/device_info_sync_bridge.cc ('k') | 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
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;
29 using base::Time; 30 using base::Time;
30 using base::TimeDelta; 31 using base::TimeDelta;
31 using sync_pb::DeviceInfoSpecifics; 32 using sync_pb::DeviceInfoSpecifics;
32 using sync_pb::EntitySpecifics; 33 using sync_pb::EntitySpecifics;
33 using sync_pb::ModelTypeState; 34 using sync_pb::ModelTypeState;
34 35
35 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>; 36 using DeviceInfoList = std::vector<std::unique_ptr<DeviceInfo>>;
36 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList; 37 using StorageKeyList = ModelTypeSyncBridge::StorageKeyList;
37 using RecordList = ModelTypeStore::RecordList; 38 using RecordList = ModelTypeStore::RecordList;
38 using Result = ModelTypeStore::Result; 39 using Result = ModelTypeStore::Result;
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 DeviceInfoSyncBridge* bridge() { 281 DeviceInfoSyncBridge* bridge() {
281 EXPECT_TRUE(bridge_); 282 EXPECT_TRUE(bridge_);
282 return bridge_.get(); 283 return bridge_.get();
283 } 284 }
284 285
285 RecordingModelTypeChangeProcessor* processor() { 286 RecordingModelTypeChangeProcessor* processor() {
286 EXPECT_TRUE(processor_); 287 EXPECT_TRUE(processor_);
287 return processor_; 288 return processor_;
288 } 289 }
289 290
291 const OneShotTimer& pulse_timer() { return bridge()->pulse_timer_; }
292
290 // Should only be called after the bridge has been initialized. Will first 293 // Should only be called after the bridge has been initialized. Will first
291 // recover the bridge's store, so another can be initialized later, and then 294 // recover the bridge's store, so another can be initialized later, and then
292 // deletes the bridge. 295 // deletes the bridge.
293 void PumpAndShutdown() { 296 void PumpAndShutdown() {
294 ASSERT_TRUE(bridge_); 297 ASSERT_TRUE(bridge_);
295 base::RunLoop().RunUntilIdle(); 298 base::RunLoop().RunUntilIdle();
296 std::swap(store_, bridge_->store_); 299 std::swap(store_, bridge_->store_);
297 bridge_->RemoveObserver(this); 300 bridge_->RemoveObserver(this);
298 bridge_.reset(); 301 bridge_.reset();
299 } 302 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 local_device()->Initialize(CreateModel(1)); 394 local_device()->Initialize(CreateModel(1));
392 base::RunLoop().RunUntilIdle(); 395 base::RunLoop().RunUntilIdle();
393 396
394 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 397 DeviceInfoList devices = bridge()->GetAllDeviceInfo();
395 ASSERT_EQ(1u, devices.size()); 398 ASSERT_EQ(1u, devices.size());
396 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 399 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
397 400
398 EXPECT_TRUE(processor()->metadata()); 401 EXPECT_TRUE(processor()->metadata());
399 } 402 }
400 403
404 // Simulate shutting down sync during the ModelTypeStore callbacks. The pulse
405 // timer should still be initialized, even though reconcile never occurs.
406 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderDuringInit) {
407 InitializeBridge();
408 local_device()->Clear();
409 base::RunLoop().RunUntilIdle();
410 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
411 EXPECT_TRUE(pulse_timer().IsRunning());
412 }
413
401 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) { 414 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) {
402 InitializeBridge(); 415 InitializeBridge();
403 const std::string guid = "abc"; 416 const std::string guid = "abc";
404 EntitySpecifics entity_specifics; 417 EntitySpecifics entity_specifics;
405 entity_specifics.mutable_device_info()->set_cache_guid(guid); 418 entity_specifics.mutable_device_info()->set_cache_guid(guid);
406 EntityData entity_data; 419 EntityData entity_data;
407 entity_data.specifics = entity_specifics; 420 entity_data.specifics = entity_specifics;
408 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data)); 421 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data));
409 } 422 }
410 423
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) { 593 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) {
581 InitializeAndPump(); 594 InitializeAndPump();
582 EXPECT_EQ(1, change_count()); 595 EXPECT_EQ(1, change_count());
583 const SyncError error = 596 const SyncError error =
584 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 597 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
585 {EntityChange::CreateDelete("guid")}); 598 {EntityChange::CreateDelete("guid")});
586 EXPECT_FALSE(error.IsSet()); 599 EXPECT_FALSE(error.IsSet());
587 EXPECT_EQ(1, change_count()); 600 EXPECT_EQ(1, change_count());
588 } 601 }
589 602
603 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndApply) {
604 // This will initialize the provider a first time.
605 InitializeAndPump();
606 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
607
608 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now());
609
610 local_device()->Clear();
611 SyncError error = bridge()->ApplySyncChanges(
612 bridge()->CreateMetadataChangeList(), EntityAddList({specifics}));
613 EXPECT_FALSE(error.IsSet());
614 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
615
616 local_device()->Initialize(CreateModel(kDefaultLocalSuffix));
617 error = bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
618 EntityAddList({specifics}));
619 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size());
620 }
621
590 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { 622 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) {
591 InitializeAndPump(); 623 InitializeAndPump();
592 EXPECT_EQ(1, change_count()); 624 EXPECT_EQ(1, change_count());
593 const SyncError error = bridge()->MergeSyncData( 625 const SyncError error = bridge()->MergeSyncData(
594 bridge()->CreateMetadataChangeList(), EntityDataMap()); 626 bridge()->CreateMetadataChangeList(), EntityDataMap());
595 EXPECT_FALSE(error.IsSet()); 627 EXPECT_FALSE(error.IsSet());
596 EXPECT_EQ(1, change_count()); 628 EXPECT_EQ(1, change_count());
597 // TODO(skym): Stop sending local twice. The first of the two puts will 629 // TODO(skym): Stop sending local twice. The first of the two puts will
598 // probably happen before the processor is tracking metadata yet, and so there 630 // probably happen before the processor is tracking metadata yet, and so there
599 // should not be much overhead. 631 // should not be much overhead.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 // loaded, and thus reconcile is never called. The bridge should ignore this 705 // loaded, and thus reconcile is never called. The bridge should ignore this
674 // EntityData because its cache guid is the same the local device's. 706 // EntityData because its cache guid is the same the local device's.
675 const SyncError error = bridge()->MergeSyncData( 707 const SyncError error = bridge()->MergeSyncData(
676 bridge()->CreateMetadataChangeList(), 708 bridge()->CreateMetadataChangeList(),
677 InlineEntityDataMap({CreateSpecifics(kDefaultLocalSuffix)})); 709 InlineEntityDataMap({CreateSpecifics(kDefaultLocalSuffix)}));
678 EXPECT_FALSE(error.IsSet()); 710 EXPECT_FALSE(error.IsSet());
679 EXPECT_EQ(0, change_count()); 711 EXPECT_EQ(0, change_count());
680 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 712 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
681 } 713 }
682 714
715 TEST_F(DeviceInfoSyncBridgeTest, ClearProviderAndMerge) {
716 // This will initialize the provider a first time.
717 InitializeAndPump();
718 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
719
720 const DeviceInfoSpecifics specifics = CreateSpecifics(1, Time::Now());
721
722 local_device()->Clear();
723 SyncError error = bridge()->MergeSyncData(
724 bridge()->CreateMetadataChangeList(), InlineEntityDataMap({specifics}));
725 EXPECT_FALSE(error.IsSet());
726 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
727 local_device()->Initialize(CreateModel(kDefaultLocalSuffix));
728 error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
729 InlineEntityDataMap({specifics}));
730 EXPECT_EQ(2u, bridge()->GetAllDeviceInfo().size());
731 }
732
683 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { 733 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) {
684 InitializeAndPump(); 734 InitializeAndPump();
685 EXPECT_EQ(1, bridge()->CountActiveDevices()); 735 EXPECT_EQ(1, bridge()->CountActiveDevices());
686 736
687 // Regardless of the time, these following two ApplySyncChanges(...) calls 737 // Regardless of the time, these following two ApplySyncChanges(...) calls
688 // have the same guid as the local device. 738 // have the same guid as the local device.
689 bridge()->ApplySyncChanges( 739 bridge()->ApplySyncChanges(
690 bridge()->CreateMetadataChangeList(), 740 bridge()->CreateMetadataChangeList(),
691 EntityAddList({CreateSpecifics(kDefaultLocalSuffix)})); 741 EntityAddList({CreateSpecifics(kDefaultLocalSuffix)}));
692 EXPECT_EQ(1, bridge()->CountActiveDevices()); 742 EXPECT_EQ(1, bridge()->CountActiveDevices());
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 812
763 // Reloading from storage shouldn't contain remote data. 813 // Reloading from storage shouldn't contain remote data.
764 RestartBridge(); 814 RestartBridge();
765 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); 815 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
766 EXPECT_EQ(4, change_count()); 816 EXPECT_EQ(4, change_count());
767 } 817 }
768 818
769 } // namespace 819 } // namespace
770 820
771 } // namespace syncer 821 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/device_info/device_info_sync_bridge.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698