Commit 5847a955 authored by MozLando's avatar MozLando
Browse files

Merge #7425



7425: Closes #7331: Clear sync state on disconnect r=pocmo a=grigoryk

We simply just weren't doing this.



Co-authored-by: default avatarGrisha Kruglov <gkruglov@mozilla.com>
parents 22a4b7a0 9c06c4de
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import mozilla.components.service.fxa.sync.SyncManager
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.service.fxa.sync.SyncStatusObserver
import mozilla.components.service.fxa.sync.WorkManagerSyncManager
import mozilla.components.service.fxa.sync.clearSyncState
import mozilla.components.support.base.crash.CrashReporting
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.base.observer.Observable
@@ -622,6 +623,7 @@ open class FxaAccountManager(
                        // and extra overhead is quite small.
                        SyncAuthInfoCache(context).clear()
                        SyncEnginesStorage(context).clear()
                        clearSyncState(context)
                        // Re-initialize account.
                        account = createAccount(serverConfig)

+11 −6
Original line number Diff line number Diff line
@@ -474,12 +474,9 @@ fun getLastSynced(context: Context): Long {
        .getLong(SYNC_LAST_SYNCED_KEY, 0)
}

internal fun setLastSynced(context: Context, ts: Long) {
    context
        .getSharedPreferences(SYNC_STATE_PREFS_KEY, Context.MODE_PRIVATE)
        .edit()
        .putLong(SYNC_LAST_SYNCED_KEY, ts)
        .apply()
internal fun clearSyncState(context: Context) {
    context.getSharedPreferences(SYNC_STATE_PREFS_KEY, Context.MODE_PRIVATE)
        .edit().clear().apply()
}

internal fun getSyncState(context: Context): String? {
@@ -488,6 +485,14 @@ internal fun getSyncState(context: Context): String? {
        .getString(SYNC_STATE_KEY, null)
}

internal fun setLastSynced(context: Context, ts: Long) {
    context
        .getSharedPreferences(SYNC_STATE_PREFS_KEY, Context.MODE_PRIVATE)
        .edit()
        .putLong(SYNC_LAST_SYNCED_KEY, ts)
        .apply()
}

internal fun setSyncState(context: Context, state: String) {
    context
        .getSharedPreferences(SYNC_STATE_PREFS_KEY, Context.MODE_PRIVATE)
+37 −0
Original line number Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fxa.sync

import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class WorkManagerSyncManagerTest {
    @Test
    fun `sync state access`() {
        assertNull(getSyncState(testContext))
        assertEquals(0L, getLastSynced(testContext))

        // 'clear' doesn't blow up for empty state
        clearSyncState(testContext)
        // ... and doesn't affect anything, either
        assertNull(getSyncState(testContext))
        assertEquals(0L, getLastSynced(testContext))

        setSyncState(testContext, "some state")
        assertEquals("some state", getSyncState(testContext))

        setLastSynced(testContext, 123L)
        assertEquals(123L, getLastSynced(testContext))

        clearSyncState(testContext)
        assertNull(getSyncState(testContext))
        assertEquals(0L, getLastSynced(testContext))
    }
}
 No newline at end of file