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

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

Issue 2594863003: [Sync] Actively guard against provider being cleared in DeviceInfoSyncBridge. (Closed)
Patch Set: 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 DeviceInfoSyncBridge* bridge() { 256 DeviceInfoSyncBridge* bridge() {
256 EXPECT_TRUE(bridge_); 257 EXPECT_TRUE(bridge_);
257 return bridge_.get(); 258 return bridge_.get();
258 } 259 }
259 260
260 RecordingModelTypeChangeProcessor* processor() { 261 RecordingModelTypeChangeProcessor* processor() {
261 EXPECT_TRUE(processor_); 262 EXPECT_TRUE(processor_);
262 return processor_; 263 return processor_;
263 } 264 }
264 265
266 const OneShotTimer& pulse_timer() { return bridge()->pulse_timer_; }
267
265 // Should only be called after the bridge has been initialized. Will first 268 // Should only be called after the bridge has been initialized. Will first
266 // recover the bridge's store, so another can be initialized later, and then 269 // recover the bridge's store, so another can be initialized later, and then
267 // deletes the bridge. 270 // deletes the bridge.
268 void PumpAndShutdown() { 271 void PumpAndShutdown() {
269 ASSERT_TRUE(bridge_); 272 ASSERT_TRUE(bridge_);
270 base::RunLoop().RunUntilIdle(); 273 base::RunLoop().RunUntilIdle();
271 std::swap(store_, bridge_->store_); 274 std::swap(store_, bridge_->store_);
272 bridge_->RemoveObserver(this); 275 bridge_->RemoveObserver(this);
273 bridge_.reset(); 276 bridge_.reset();
274 } 277 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 local_device()->Initialize(CreateModel(1)); 346 local_device()->Initialize(CreateModel(1));
344 base::RunLoop().RunUntilIdle(); 347 base::RunLoop().RunUntilIdle();
345 348
346 DeviceInfoList devices = bridge()->GetAllDeviceInfo(); 349 DeviceInfoList devices = bridge()->GetAllDeviceInfo();
347 ASSERT_EQ(1u, devices.size()); 350 ASSERT_EQ(1u, devices.size());
348 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0])); 351 EXPECT_TRUE(local_device()->GetLocalDeviceInfo()->Equals(*devices[0]));
349 352
350 EXPECT_TRUE(processor()->metadata()); 353 EXPECT_TRUE(processor()->metadata());
351 } 354 }
352 355
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
353 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) { 366 TEST_F(DeviceInfoSyncBridgeTest, GetClientTagNormal) {
354 InitializeBridge(); 367 InitializeBridge();
355 const std::string guid = "abc"; 368 const std::string guid = "abc";
356 EntitySpecifics entity_specifics; 369 EntitySpecifics entity_specifics;
357 entity_specifics.mutable_device_info()->set_cache_guid(guid); 370 entity_specifics.mutable_device_info()->set_cache_guid(guid);
358 EntityData entity_data; 371 EntityData entity_data;
359 entity_data.specifics = entity_specifics; 372 entity_data.specifics = entity_specifics;
360 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data)); 373 EXPECT_EQ(CacheGuidToTag(guid), bridge()->GetClientTag(entity_data));
361 } 374 }
362 375
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) { 571 TEST_F(DeviceInfoSyncBridgeTest, ApplyDeleteNonexistent) {
559 InitializeAndPump(); 572 InitializeAndPump();
560 EXPECT_EQ(1, change_count()); 573 EXPECT_EQ(1, change_count());
561 const SyncError error = 574 const SyncError error =
562 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 575 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
563 {EntityChange::CreateDelete("guid")}); 576 {EntityChange::CreateDelete("guid")});
564 EXPECT_FALSE(error.IsSet()); 577 EXPECT_FALSE(error.IsSet());
565 EXPECT_EQ(1, change_count()); 578 EXPECT_EQ(1, change_count());
566 } 579 }
567 580
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
568 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { 600 TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) {
569 InitializeAndPump(); 601 InitializeAndPump();
570 EXPECT_EQ(1, change_count()); 602 EXPECT_EQ(1, change_count());
571 const SyncError error = bridge()->MergeSyncData( 603 const SyncError error = bridge()->MergeSyncData(
572 bridge()->CreateMetadataChangeList(), EntityDataMap()); 604 bridge()->CreateMetadataChangeList(), EntityDataMap());
573 EXPECT_FALSE(error.IsSet()); 605 EXPECT_FALSE(error.IsSet());
574 EXPECT_EQ(1, change_count()); 606 EXPECT_EQ(1, change_count());
575 // TODO(skym): Stop sending local twice. The first of the two puts will 607 // TODO(skym): Stop sending local twice. The first of the two puts will
576 // probably happen before the processor is tracking metadata yet, and so there 608 // probably happen before the processor is tracking metadata yet, and so there
577 // should not be much overhead. 609 // should not be much overhead.
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 // loaded, and thus reconcile is never called. The bridge should ignore this 703 // 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. 704 // EntityData because its cache guid is the same the local device's.
673 const SyncError error = bridge()->MergeSyncData( 705 const SyncError error = bridge()->MergeSyncData(
674 bridge()->CreateMetadataChangeList(), 706 bridge()->CreateMetadataChangeList(),
675 {{specifics.cache_guid(), SpecificsToEntity(specifics)}}); 707 {{specifics.cache_guid(), SpecificsToEntity(specifics)}});
676 EXPECT_FALSE(error.IsSet()); 708 EXPECT_FALSE(error.IsSet());
677 EXPECT_EQ(0, change_count()); 709 EXPECT_EQ(0, change_count());
678 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size()); 710 EXPECT_EQ(0u, bridge()->GetAllDeviceInfo().size());
679 } 711 }
680 712
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
681 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) { 731 TEST_F(DeviceInfoSyncBridgeTest, CountActiveDevices) {
682 InitializeAndPump(); 732 InitializeAndPump();
683 EXPECT_EQ(1, bridge()->CountActiveDevices()); 733 EXPECT_EQ(1, bridge()->CountActiveDevices());
684 734
685 DeviceInfoSpecifics specifics = CreateSpecifics(0); 735 DeviceInfoSpecifics specifics = CreateSpecifics(0);
686 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), 736 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(),
687 EntityAddList({specifics})); 737 EntityAddList({specifics}));
688 EXPECT_EQ(1, bridge()->CountActiveDevices()); 738 EXPECT_EQ(1, bridge()->CountActiveDevices());
689 739
690 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now())); 740 specifics.set_last_updated_timestamp(TimeToProtoTime(Time::Now()));
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 809
760 // Reloading from storage shouldn't contain remote data. 810 // Reloading from storage shouldn't contain remote data.
761 RestartBridge(); 811 RestartBridge();
762 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size()); 812 EXPECT_EQ(1u, bridge()->GetAllDeviceInfo().size());
763 EXPECT_EQ(4, change_count()); 813 EXPECT_EQ(4, change_count());
764 } 814 }
765 815
766 } // namespace 816 } // namespace
767 817
768 } // namespace syncer 818 } // 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