From a1fd9216f117a3dacef42ea5e45e9e9b242675cb Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 27 Dec 2018 15:41:00 -0800 Subject: [PATCH] Reset sync right away if devices size <= 1 --- .../brave_sync/brave_sync_service_impl.cc | 19 +++++++++++++------ .../brave_sync/brave_sync_service_unittest.cc | 12 ++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/components/brave_sync/brave_sync_service_impl.cc b/components/brave_sync/brave_sync_service_impl.cc index 54ce0399c38c..cb76609e0f92 100644 --- a/components/brave_sync/brave_sync_service_impl.cc +++ b/components/brave_sync/brave_sync_service_impl.cc @@ -227,11 +227,17 @@ void BraveSyncServiceImpl::OnDeleteDevice(const std::string& device_id) { void BraveSyncServiceImpl::OnResetSync() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - const std::string device_id = sync_prefs_->GetThisDeviceId(); - - // We have to send delete record and wait for library deleted response then we - // can reset it by ResetInternal() - OnDeleteDevice(device_id); + auto sync_devices = sync_prefs_->GetSyncDevices(); + // If there is only one or no devices left, we won't get back resolved sync + // record back. + if (sync_devices->size() <= 1) + ResetSyncInternal(); + else { + // We have to send delete record and wait for library deleted response then + // we can reset it by ResetInternal() + const std::string device_id = sync_prefs_->GetThisDeviceId(); + OnDeleteDevice(device_id); + } } void BraveSyncServiceImpl::GetSettingsAndDevices( @@ -479,7 +485,8 @@ void BraveSyncServiceImpl::OnResolvedPreferences(const RecordsList& records) { record->action == jslib::SyncRecord::Action::A_DELETE && actually_merged); contains_only_one_device = sync_devices->size() < 2 && - record->action == jslib::SyncRecord::Action::A_DELETE; + record->action == jslib::SyncRecord::Action::A_DELETE && + actually_merged; } } // for each device diff --git a/components/brave_sync/brave_sync_service_unittest.cc b/components/brave_sync/brave_sync_service_unittest.cc index e67fc7393206..37468429520b 100644 --- a/components/brave_sync/brave_sync_service_unittest.cc +++ b/components/brave_sync/brave_sync_service_unittest.cc @@ -483,21 +483,13 @@ TEST_F(BraveSyncServiceTest, OnDeleteDeviceWhenOneDevice) { EXPECT_TRUE(DevicesContains(devices.get(), "2", "device2")); using brave_sync::jslib::SyncRecord; - // device 2 => device 1 - EXPECT_CALL(*sync_client(), SendSyncRecords).Times(2); + EXPECT_CALL(*sync_client(), SendSyncRecords).Times(1); sync_service()->OnDeleteDevice("2"); RecordsList resolved_records; auto resolved_record = SyncRecord::Clone(*records.at(1)); resolved_record->action = jslib::SyncRecord::Action::A_DELETE; resolved_records.push_back(std::move(resolved_record)); - EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(1); - sync_service()->OnResolvedPreferences(resolved_records); - - resolved_records.clear(); - resolved_record = SyncRecord::Clone(*records.at(0)); - resolved_record->action = jslib::SyncRecord::Action::A_DELETE; - resolved_records.push_back(std::move(resolved_record)); EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(3); sync_service()->OnResolvedPreferences(resolved_records); @@ -534,7 +526,7 @@ TEST_F(BraveSyncServiceTest, OnDeleteDeviceWhenSelfDeleted) { auto resolved_record = SyncRecord::Clone(*records.at(0)); resolved_record->action = jslib::SyncRecord::Action::A_DELETE; resolved_records.push_back(std::move(resolved_record)); - EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(3); + EXPECT_CALL(*observer(), OnSyncStateChanged(sync_service())).Times(5); sync_service()->OnResolvedPreferences(resolved_records); auto devices_final = sync_service()->sync_prefs_->GetSyncDevices();