1
0
Fork 0

Use A-C preferences property delegates

master
Tiger Oakes 2019-09-10 10:50:28 -07:00 committed by Emily Kager
parent 6b76ee75ff
commit 3c07cacf4d
4 changed files with 21 additions and 132 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -10,13 +10,16 @@ import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
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.Config
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.settings.PhoneFeature
import org.mozilla.fenix.settings.sharedpreferences.PreferencesHolder
import org.mozilla.fenix.settings.sharedpreferences.booleanPreference
import java.security.InvalidParameterException
/**
@ -70,11 +73,10 @@ class Settings private constructor(
default = false
)
var defaultSearchEngineName: String
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
set(name) = preferences.edit()
.putString(appContext.getPreferenceKey(R.string.pref_key_search_engine), name)
.apply()
var defaultSearchEngineName by stringPreference(
appContext.getPreferenceKey(R.string.pref_key_search_engine),
default = ""
)
val isCrashReportingEnabled: Boolean
get() = isCrashReportEnabledInBuild &&
@ -108,14 +110,10 @@ class Settings private constructor(
default = true
)
var fontSizeFactor: Float
get() = preferences.getFloat(
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
1f
)
set(value) = preferences.edit()
.putFloat(appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale), value)
.apply()
var fontSizeFactor by floatPreference(
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
default = 1f
)
val shouldShowHistorySuggestions by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_search_browsing_history),
@ -162,8 +160,10 @@ class Settings private constructor(
}
@VisibleForTesting(otherwise = PRIVATE)
internal val autoBounceQuickActionSheetCount: Int
get() = preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), 0)
internal val autoBounceQuickActionSheetCount by intPreference(
appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
default = 0
)
fun incrementAutomaticBounceQuickActionSheetCount() {
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(
appContext.getPreferenceKey(R.string.pref_key_fxa_has_synced_items),

View File

@ -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) }
}
}