Skip to content

Commit

Permalink
fix(tests) + refactor(Prefs)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Jan 16, 2025
1 parent a06fa61 commit c79b4ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
12 changes: 6 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/settings/Prefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ import kotlin.reflect.KProperty
// TODO move this to `com.ichi2.anki.preferences`
// after the UI classes of that package are moved to `com.ichi2.anki.ui.preferences`
object Prefs {
private val prefs by lazy { AnkiDroidApp.sharedPrefs() }
private val sharedPrefs get() = AnkiDroidApp.sharedPrefs()

@VisibleForTesting
fun getBoolean(
key: String,
defValue: Boolean,
): Boolean = prefs.getBoolean(key, defValue)
): Boolean = sharedPrefs.getBoolean(key, defValue)

@VisibleForTesting
fun putBoolean(
key: String,
value: Boolean,
) {
prefs.edit { putBoolean(key, value) }
sharedPrefs.edit { putBoolean(key, value) }
}

@VisibleForTesting
fun getString(
key: String,
defValue: String?,
): String? = prefs.getString(key, defValue)
): String? = sharedPrefs.getString(key, defValue)

@VisibleForTesting
fun putString(
key: String,
value: String?,
) {
prefs.edit { putString(key, value) }
sharedPrefs.edit { putString(key, value) }
}

@VisibleForTesting
fun getInt(
key: String,
defValue: Int,
): Int = prefs.getInt(key, defValue)
): Int = sharedPrefs.getInt(key, defValue)

@VisibleForTesting
fun <E> getEnum(
Expand Down
29 changes: 16 additions & 13 deletions AnkiDroid/src/test/java/com/ichi2/anki/settings/PrefsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.ichi2.anki.settings

import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder
import com.ichi2.anki.AnkiDroidApp
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mockito.anyBoolean
Expand All @@ -33,15 +33,19 @@ import kotlin.reflect.KMutableProperty
import kotlin.reflect.KVisibility
import kotlin.reflect.full.memberProperties

class PrefsTest {
@BeforeEach
fun setup() {
AnkiDroidApp.sharedPreferencesTestingOverride =
spy(SPMockBuilder().createSharedPreferences())
class PrefsTest : OnSharedPreferenceChangeListener {
private var lastSetKey: String? = null

override fun onSharedPreferenceChanged(
sharedPreferences: SharedPreferences?,
key: String?,
) {
lastSetKey = key
}

@Test
fun `booleanSetting getter and setter work`() {
AnkiDroidApp.sharedPreferencesTestingOverride = SPMockBuilder().createSharedPreferences()
var setting by Prefs.booleanPref("boolKey", false)
assertThat(setting, equalTo(false))

Expand All @@ -51,6 +55,7 @@ class PrefsTest {

@Test
fun `stringSetting getter and setter work`() {
AnkiDroidApp.sharedPreferencesTestingOverride = SPMockBuilder().createSharedPreferences()
var setting by Prefs.stringPref("stringKey", "defaultValue")
assertThat(setting, equalTo("defaultValue"))

Expand All @@ -60,7 +65,8 @@ class PrefsTest {

@Test
fun `getters and setters use the same key`() {
val sharedPrefsSpy = AnkiDroidApp.sharedPreferencesTestingOverride!!
val sharedPrefsSpy = spy(SPMockBuilder().createSharedPreferences())
AnkiDroidApp.sharedPreferencesTestingOverride = sharedPrefsSpy

var getterKey = ""
doAnswer { invocation ->
Expand All @@ -72,10 +78,7 @@ class PrefsTest {
whenever(sharedPrefsSpy).getInt(anyString(), anyInt())
}

var setterKey: String? = ""
val listener = OnSharedPreferenceChangeListener { _, key -> setterKey = key }
sharedPrefsSpy.registerOnSharedPreferenceChangeListener(listener)

sharedPrefsSpy.registerOnSharedPreferenceChangeListener(this)
for (property in Prefs::class.memberProperties) {
if (property.visibility != KVisibility.PUBLIC || property !is KMutableProperty<*>) continue

Expand All @@ -87,8 +90,8 @@ class PrefsTest {
else -> continue
}

assertThat("The getter and setter of '${property.name}' use the same key", getterKey, equalTo(setterKey))
assertThat("The getter and setter of '${property.name}' use the same key", getterKey, equalTo(lastSetKey))
}
sharedPrefsSpy.unregisterOnSharedPreferenceChangeListener(listener)
sharedPrefsSpy.unregisterOnSharedPreferenceChangeListener(this)
}
}

0 comments on commit c79b4ec

Please sign in to comment.