[fenix] No issue: Clean up preferences code (https://github.com/mozilla-mobile/fenix/pull/4699)
parent
409cea2128
commit
9e9246ada3
@ -0,0 +1,20 @@
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.Preference
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
/**
|
||||
* Updates the corresponding [android.content.SharedPreferences] when the boolean [Preference] is changed.
|
||||
* The preference key is used as the shared preference key.
|
||||
*/
|
||||
class SharedPreferenceUpdater : Preference.OnPreferenceChangeListener {
|
||||
|
||||
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
|
||||
val newBooleanValue = newValue as? Boolean ?: return false
|
||||
Settings.getInstance(preference.context).preferences.edit {
|
||||
putBoolean(preference.key, newBooleanValue)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
@ -1,45 +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 mozilla.components.feature.sitepermissions.SitePermissionsRules
|
||||
import java.security.InvalidParameterException
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
private class SitePermissionsRulesActionPreference(
|
||||
private val key: String
|
||||
) : ReadWriteProperty<PreferencesHolder, SitePermissionsRules.Action> {
|
||||
|
||||
override fun getValue(thisRef: PreferencesHolder, property: KProperty<*>): SitePermissionsRules.Action =
|
||||
intToAction(thisRef.preferences.getInt(key, ASK_TO_ALLOW_INT))
|
||||
|
||||
override fun setValue(thisRef: PreferencesHolder, property: KProperty<*>, value: SitePermissionsRules.Action) {
|
||||
thisRef.preferences.edit().putInt(key, actionToInt(value)).apply()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val BLOCKED_INT = 0
|
||||
private const val ASK_TO_ALLOW_INT = 1
|
||||
|
||||
private fun actionToInt(action: SitePermissionsRules.Action) = when (action) {
|
||||
SitePermissionsRules.Action.BLOCKED -> BLOCKED_INT
|
||||
SitePermissionsRules.Action.ASK_TO_ALLOW -> ASK_TO_ALLOW_INT
|
||||
}
|
||||
|
||||
private fun intToAction(action: Int) = when (action) {
|
||||
BLOCKED_INT -> SitePermissionsRules.Action.BLOCKED
|
||||
ASK_TO_ALLOW_INT -> SitePermissionsRules.Action.ASK_TO_ALLOW
|
||||
else -> throw InvalidParameterException("$action is not a valid SitePermissionsRules.Action")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Property delegate for getting and setting a [SitePermissionsRules.Action] preference.
|
||||
*/
|
||||
fun sitePermissionsRulesActionPreference(
|
||||
key: String
|
||||
): ReadWriteProperty<PreferencesHolder, SitePermissionsRules.Action> = SitePermissionsRulesActionPreference(key)
|
@ -1,73 +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 mozilla.components.feature.sitepermissions.SitePermissionsRules
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class SitePermissionsRulesActionPreferenceTest {
|
||||
|
||||
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.putInt(any(), any()) } returns editor
|
||||
every { editor.apply() } just runs
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getter returns action from shared preferences`() {
|
||||
val holder = object : PreferencesHolder {
|
||||
override val preferences = sharedPrefs
|
||||
val test by sitePermissionsRulesActionPreference("test_preference_key")
|
||||
}
|
||||
every { sharedPrefs.getInt("test_preference_key", 1) } returns 0
|
||||
|
||||
assertEquals(SitePermissionsRules.Action.BLOCKED, holder.test)
|
||||
verify { sharedPrefs.getInt("test_preference_key", 1) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `setter applies boolean to shared preferences`() {
|
||||
val holder = object : PreferencesHolder {
|
||||
override val preferences = sharedPrefs
|
||||
var test by sitePermissionsRulesActionPreference("pref")
|
||||
}
|
||||
holder.test = SitePermissionsRules.Action.BLOCKED
|
||||
|
||||
verify { editor.putInt("pref", 0) }
|
||||
verify { editor.apply() }
|
||||
|
||||
holder.test = SitePermissionsRules.Action.ASK_TO_ALLOW
|
||||
|
||||
verify { editor.putInt("pref", 1) }
|
||||
verify { editor.apply() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getter defaults to ASK_TO_ALLOW`() {
|
||||
every { sharedPrefs.getInt("key", 1) } returns 1
|
||||
val holder = object : PreferencesHolder {
|
||||
override val preferences = sharedPrefs
|
||||
val action by sitePermissionsRulesActionPreference("key")
|
||||
}
|
||||
|
||||
assertEquals(SitePermissionsRules.Action.ASK_TO_ALLOW, holder.action)
|
||||
verify { sharedPrefs.getInt("key", 1) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue