diff --git a/app/src/main/java/mozilla/lockbox/store/DataStore.kt b/app/src/main/java/mozilla/lockbox/store/DataStore.kt index 13b197d92..42adf2db8 100644 --- a/app/src/main/java/mozilla/lockbox/store/DataStore.kt +++ b/app/src/main/java/mozilla/lockbox/store/DataStore.kt @@ -129,9 +129,11 @@ open class DataStore( if (item != null) { backend.delete(item.id) .asSingle(coroutineContext) - .subscribe() + .subscribe { _ -> + dispatcher.dispatch(DataStoreAction.Sync) + } .addTo(compositeDisposable) - sync() + deletedItemSubject.accept(Consumable(item)) } } catch (loginsStorageException: LoginsStorageException) { @@ -230,9 +232,12 @@ open class DataStore( } } - private fun syncIfRequired() { + @VisibleForTesting( + otherwise = VisibleForTesting.PRIVATE + ) + fun syncIfRequired() { if (timingSupport.shouldSync) { - this.sync() + dispatcher.dispatch(DataStoreAction.Sync) timingSupport.storeNextSyncTime() } } diff --git a/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt b/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt index fc32c2ec1..8d9293598 100644 --- a/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt +++ b/app/src/test/java/mozilla/lockbox/store/DataStoreTest.kt @@ -15,6 +15,7 @@ import mozilla.components.concept.sync.AccessTokenInfo import mozilla.lockbox.DisposingTest import mozilla.lockbox.action.DataStoreAction import mozilla.lockbox.action.LifecycleAction +import mozilla.lockbox.extensions.filterByType import mozilla.lockbox.flux.Dispatcher import mozilla.lockbox.mocks.MockDataStoreSupport import mozilla.lockbox.model.FixedSyncCredentials @@ -33,6 +34,7 @@ import org.mockito.Mockito.never import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.powermock.api.mockito.PowerMockito +import java.util.concurrent.atomic.AtomicBoolean import org.powermock.api.mockito.PowerMockito.`when` as whenCalled @ExperimentalCoroutinesApi @@ -87,6 +89,22 @@ class DataStoreTest : DisposingTest() { Assert.assertEquals(0, listIterator.next().size) } + @Test + fun testSyncIfRequired_dispatchesSyncAction() { + whenCalled(timingSupport.shouldSync).thenReturn(true) + val isSyncing = AtomicBoolean(false) + val sub = dispatcher.register + .filterByType(DataStoreAction::class.java) + .subscribe { + isSyncing.set(true) + } + + subject.syncIfRequired() + + Assert.assertTrue(isSyncing.get()) + sub.dispose() + } + @Test fun testLockUnlock_shouldNotSync() { val stateIterator = this.subject.state.blockingIterable().iterator()