Use A-C preferences property delegates
parent
6b76ee75ff
commit
3c07cacf4d
|
@ -1,27 +0,0 @@
|
||||||
/* 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.settings.sharedpreferences
|
|
||||||
|
|
||||||
import kotlin.properties.ReadWriteProperty
|
|
||||||
import kotlin.reflect.KProperty
|
|
||||||
|
|
||||||
private class BooleanPreference(
|
|
||||||
private val key: String,
|
|
||||||
private val default: Boolean
|
|
||||||
) : ReadWriteProperty<PreferencesHolder, Boolean> {
|
|
||||||
|
|
||||||
override fun getValue(thisRef: PreferencesHolder, property: KProperty<*>): Boolean =
|
|
||||||
thisRef.preferences.getBoolean(key, default)
|
|
||||||
|
|
||||||
override fun setValue(thisRef: PreferencesHolder, property: KProperty<*>, value: Boolean) {
|
|
||||||
thisRef.preferences.edit().putBoolean(key, value).apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Property delegate for getting and setting a boolean shared preference.
|
|
||||||
*/
|
|
||||||
fun booleanPreference(key: String, default: Boolean): ReadWriteProperty<PreferencesHolder, Boolean> =
|
|
||||||
BooleanPreference(key, default)
|
|
|
@ -1,11 +0,0 @@
|
||||||
/* 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.settings.sharedpreferences
|
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
|
|
||||||
interface PreferencesHolder {
|
|
||||||
val preferences: SharedPreferences
|
|
||||||
}
|
|
|
@ -10,13 +10,16 @@ import android.content.SharedPreferences
|
||||||
import androidx.annotation.VisibleForTesting
|
import androidx.annotation.VisibleForTesting
|
||||||
import androidx.annotation.VisibleForTesting.PRIVATE
|
import androidx.annotation.VisibleForTesting.PRIVATE
|
||||||
import mozilla.components.feature.sitepermissions.SitePermissionsRules
|
import mozilla.components.feature.sitepermissions.SitePermissionsRules
|
||||||
|
import mozilla.components.support.ktx.android.content.PreferencesHolder
|
||||||
|
import mozilla.components.support.ktx.android.content.booleanPreference
|
||||||
|
import mozilla.components.support.ktx.android.content.floatPreference
|
||||||
|
import mozilla.components.support.ktx.android.content.intPreference
|
||||||
|
import mozilla.components.support.ktx.android.content.stringPreference
|
||||||
import org.mozilla.fenix.BuildConfig
|
import org.mozilla.fenix.BuildConfig
|
||||||
import org.mozilla.fenix.Config
|
import org.mozilla.fenix.Config
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
import org.mozilla.fenix.ext.getPreferenceKey
|
import org.mozilla.fenix.ext.getPreferenceKey
|
||||||
import org.mozilla.fenix.settings.PhoneFeature
|
import org.mozilla.fenix.settings.PhoneFeature
|
||||||
import org.mozilla.fenix.settings.sharedpreferences.PreferencesHolder
|
|
||||||
import org.mozilla.fenix.settings.sharedpreferences.booleanPreference
|
|
||||||
import java.security.InvalidParameterException
|
import java.security.InvalidParameterException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,11 +73,10 @@ class Settings private constructor(
|
||||||
default = false
|
default = false
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultSearchEngineName: String
|
var defaultSearchEngineName by stringPreference(
|
||||||
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
|
appContext.getPreferenceKey(R.string.pref_key_search_engine),
|
||||||
set(name) = preferences.edit()
|
default = ""
|
||||||
.putString(appContext.getPreferenceKey(R.string.pref_key_search_engine), name)
|
)
|
||||||
.apply()
|
|
||||||
|
|
||||||
val isCrashReportingEnabled: Boolean
|
val isCrashReportingEnabled: Boolean
|
||||||
get() = isCrashReportEnabledInBuild &&
|
get() = isCrashReportEnabledInBuild &&
|
||||||
|
@ -108,14 +110,10 @@ class Settings private constructor(
|
||||||
default = true
|
default = true
|
||||||
)
|
)
|
||||||
|
|
||||||
var fontSizeFactor: Float
|
var fontSizeFactor by floatPreference(
|
||||||
get() = preferences.getFloat(
|
|
||||||
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
|
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
|
||||||
1f
|
default = 1f
|
||||||
)
|
)
|
||||||
set(value) = preferences.edit()
|
|
||||||
.putFloat(appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale), value)
|
|
||||||
.apply()
|
|
||||||
|
|
||||||
val shouldShowHistorySuggestions by booleanPreference(
|
val shouldShowHistorySuggestions by booleanPreference(
|
||||||
appContext.getPreferenceKey(R.string.pref_key_search_browsing_history),
|
appContext.getPreferenceKey(R.string.pref_key_search_browsing_history),
|
||||||
|
@ -162,8 +160,10 @@ class Settings private constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting(otherwise = PRIVATE)
|
@VisibleForTesting(otherwise = PRIVATE)
|
||||||
internal val autoBounceQuickActionSheetCount: Int
|
internal val autoBounceQuickActionSheetCount by intPreference(
|
||||||
get() = preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), 0)
|
appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
|
||||||
|
default = 0
|
||||||
|
)
|
||||||
|
|
||||||
fun incrementAutomaticBounceQuickActionSheetCount() {
|
fun incrementAutomaticBounceQuickActionSheetCount() {
|
||||||
preferences.edit().putInt(
|
preferences.edit().putInt(
|
||||||
|
@ -193,7 +193,10 @@ class Settings private constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var fxaSignedIn by booleanPreference(appContext.getPreferenceKey(R.string.pref_key_fxa_signed_in), default = true)
|
var fxaSignedIn by booleanPreference(
|
||||||
|
appContext.getPreferenceKey(R.string.pref_key_fxa_signed_in),
|
||||||
|
default = true
|
||||||
|
)
|
||||||
|
|
||||||
var fxaHasSyncedItems by booleanPreference(
|
var fxaHasSyncedItems by booleanPreference(
|
||||||
appContext.getPreferenceKey(R.string.pref_key_fxa_has_synced_items),
|
appContext.getPreferenceKey(R.string.pref_key_fxa_has_synced_items),
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
/* 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.settings.sharedpreferences
|
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import io.mockk.every
|
|
||||||
import io.mockk.just
|
|
||||||
import io.mockk.mockk
|
|
||||||
import io.mockk.runs
|
|
||||||
import io.mockk.verify
|
|
||||||
import org.junit.Assert.assertTrue
|
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class BooleanPreferenceTest {
|
|
||||||
|
|
||||||
private lateinit var sharedPrefs: SharedPreferences
|
|
||||||
private lateinit var editor: SharedPreferences.Editor
|
|
||||||
|
|
||||||
@Before
|
|
||||||
fun setup() {
|
|
||||||
sharedPrefs = mockk(relaxed = true)
|
|
||||||
editor = mockk()
|
|
||||||
|
|
||||||
every { sharedPrefs.edit() } returns editor
|
|
||||||
every { editor.putBoolean(any(), any()) } returns editor
|
|
||||||
every { editor.apply() } just runs
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `getter returns boolean from shared preferences`() {
|
|
||||||
val holder = object : PreferencesHolder {
|
|
||||||
override val preferences = sharedPrefs
|
|
||||||
val test by booleanPreference("test_pref_key", default = false)
|
|
||||||
}
|
|
||||||
every { sharedPrefs.getBoolean("test_pref_key", false) } returns true
|
|
||||||
|
|
||||||
assertTrue(holder.test)
|
|
||||||
verify { sharedPrefs.getBoolean("test_pref_key", false) }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `setter applies boolean to shared preferences`() {
|
|
||||||
val holder = object : PreferencesHolder {
|
|
||||||
override val preferences = sharedPrefs
|
|
||||||
var test by booleanPreference("pref", default = true)
|
|
||||||
}
|
|
||||||
holder.test = false
|
|
||||||
|
|
||||||
verify { editor.putBoolean("pref", false) }
|
|
||||||
verify { editor.apply() }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `getter uses default value`() {
|
|
||||||
val holderFalse = object : PreferencesHolder {
|
|
||||||
override val preferences = sharedPrefs
|
|
||||||
val test by booleanPreference("test_pref_key", default = false)
|
|
||||||
}
|
|
||||||
// Call the getter for the test
|
|
||||||
holderFalse.test
|
|
||||||
|
|
||||||
verify { sharedPrefs.getBoolean("test_pref_key", false) }
|
|
||||||
|
|
||||||
val holderTrue = object : PreferencesHolder {
|
|
||||||
override val preferences = sharedPrefs
|
|
||||||
val test by booleanPreference("test_pref_key", default = true)
|
|
||||||
}
|
|
||||||
// Call the getter for the test
|
|
||||||
holderTrue.test
|
|
||||||
|
|
||||||
verify { sharedPrefs.getBoolean("test_pref_key", true) }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue