Add tests for FenixOnboarding (#11057)
parent
63e529ac2b
commit
221792c074
|
@ -6,32 +6,46 @@ package org.mozilla.fenix.onboarding
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.core.content.edit
|
import androidx.annotation.VisibleForTesting
|
||||||
|
import mozilla.components.support.ktx.android.content.PreferencesHolder
|
||||||
|
import mozilla.components.support.ktx.android.content.intPreference
|
||||||
import org.mozilla.fenix.components.metrics.Event
|
import org.mozilla.fenix.components.metrics.Event
|
||||||
import org.mozilla.fenix.ext.components
|
import org.mozilla.fenix.ext.components
|
||||||
|
|
||||||
class FenixOnboarding(context: Context) {
|
class FenixOnboarding(context: Context) : PreferencesHolder {
|
||||||
|
|
||||||
private val metrics = context.components.analytics.metrics
|
private val metrics = context.components.analytics.metrics
|
||||||
private val onboardingPrefs = context.getSharedPreferences(
|
override val preferences: SharedPreferences = context.getSharedPreferences(
|
||||||
PREF_NAME_ONBOARDING_KEY,
|
PREF_NAME_ONBOARDING_KEY,
|
||||||
Context.MODE_PRIVATE
|
Context.MODE_PRIVATE
|
||||||
)
|
)
|
||||||
|
|
||||||
private var SharedPreferences.onboardedVersion: Int
|
private var onboardedVersion by intPreference(LAST_VERSION_ONBOARDING_KEY, default = 0)
|
||||||
get() = getInt(LAST_VERSION_ONBOARDING_KEY, 0)
|
|
||||||
set(version) { edit { putInt(LAST_VERSION_ONBOARDING_KEY, version) } }
|
|
||||||
|
|
||||||
fun finish() {
|
fun finish() {
|
||||||
onboardingPrefs.onboardedVersion = CURRENT_ONBOARDING_VERSION
|
onboardedVersion = CURRENT_ONBOARDING_VERSION
|
||||||
metrics.track(Event.DismissedOnboarding)
|
metrics.track(Event.DismissedOnboarding)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun userHasBeenOnboarded() = onboardingPrefs.onboardedVersion == CURRENT_ONBOARDING_VERSION
|
fun userHasBeenOnboarded() = onboardedVersion == CURRENT_ONBOARDING_VERSION
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val CURRENT_ONBOARDING_VERSION = 1
|
/**
|
||||||
|
* The current onboarding version. When incremented,
|
||||||
|
* users who were previously onboarded will be show the onboarding again.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
internal const val CURRENT_ONBOARDING_VERSION = 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the shared preferences file.
|
||||||
|
*/
|
||||||
private const val PREF_NAME_ONBOARDING_KEY = "fenix.onboarding"
|
private const val PREF_NAME_ONBOARDING_KEY = "fenix.onboarding"
|
||||||
private const val LAST_VERSION_ONBOARDING_KEY = "fenix.onboarding.last_version"
|
|
||||||
|
/**
|
||||||
|
* Key for [onboardedVersion].
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
internal const val LAST_VERSION_ONBOARDING_KEY = "fenix.onboarding.last_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
/* 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 org.mozilla.fenix.onboarding
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Context.MODE_PRIVATE
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.mozilla.fenix.components.metrics.Event
|
||||||
|
import org.mozilla.fenix.components.metrics.MetricController
|
||||||
|
import org.mozilla.fenix.ext.components
|
||||||
|
import org.mozilla.fenix.onboarding.FenixOnboarding.Companion.CURRENT_ONBOARDING_VERSION
|
||||||
|
import org.mozilla.fenix.onboarding.FenixOnboarding.Companion.LAST_VERSION_ONBOARDING_KEY
|
||||||
|
|
||||||
|
class FenixOnboardingTest {
|
||||||
|
|
||||||
|
private lateinit var onboarding: FenixOnboarding
|
||||||
|
private lateinit var preferences: SharedPreferences
|
||||||
|
private lateinit var preferencesEditor: SharedPreferences.Editor
|
||||||
|
private lateinit var metrics: MetricController
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
preferences = mockk()
|
||||||
|
preferencesEditor = mockk(relaxed = true)
|
||||||
|
metrics = mockk()
|
||||||
|
val context = mockk<Context>()
|
||||||
|
every { preferences.edit() } returns preferencesEditor
|
||||||
|
every { metrics.track(any()) } returns Unit
|
||||||
|
every { context.components.analytics.metrics } returns metrics
|
||||||
|
every { context.getSharedPreferences(any(), MODE_PRIVATE) } returns preferences
|
||||||
|
|
||||||
|
onboarding = FenixOnboarding(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUserHasBeenOnboarded() {
|
||||||
|
every {
|
||||||
|
preferences.getInt(LAST_VERSION_ONBOARDING_KEY, any())
|
||||||
|
} returns 0
|
||||||
|
assertFalse(onboarding.userHasBeenOnboarded())
|
||||||
|
|
||||||
|
every {
|
||||||
|
preferences.getInt(LAST_VERSION_ONBOARDING_KEY, any())
|
||||||
|
} returns CURRENT_ONBOARDING_VERSION
|
||||||
|
assertTrue(onboarding.userHasBeenOnboarded())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFinish() {
|
||||||
|
onboarding.finish()
|
||||||
|
verify { preferencesEditor.putInt(LAST_VERSION_ONBOARDING_KEY, CURRENT_ONBOARDING_VERSION) }
|
||||||
|
verify { metrics.track(Event.DismissedOnboarding) }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue