Add Preference tests

This commit is contained in:
Lucas Lima 2020-06-28 00:54:24 -03:00
parent 2623a8fd36
commit fa9b114599
No known key found for this signature in database
GPG key ID: C5EEF4C30BFBF8D7
5 changed files with 118 additions and 77 deletions

View file

@ -1,33 +0,0 @@
package dev.lucasnlm.antimine.mocks
import dev.lucasnlm.antimine.common.level.models.Minefield
import dev.lucasnlm.antimine.core.control.ControlStyle
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
class MockPreferencesRepository : IPreferencesRepository {
override fun customGameMode(): Minefield = Minefield(9, 9, 9)
override fun updateCustomGameMode(minefield: Minefield) { }
override fun controlStyle(): ControlStyle = ControlStyle.Standard
override fun useControlStyle(controlStyle: ControlStyle) { }
override fun getBoolean(key: String, defaultValue: Boolean): Boolean = false
override fun getInt(key: String, defaultValue: Int): Int = 0
override fun putBoolean(key: String, value: Boolean) { }
override fun putInt(key: String, value: Int) { }
override fun useFlagAssistant(): Boolean = true
override fun useHapticFeedback(): Boolean = false
override fun useLargeAreas(): Boolean = false
override fun useAnimations(): Boolean = false
override fun useQuestionMark(): Boolean = true
}

View file

@ -62,7 +62,6 @@ dependencies {
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01"
kapt "androidx.hilt:hilt-compiler:1.0.0-alpha01"
// Room
api 'androidx.room:room-runtime:2.2.5'
api 'androidx.room:room-ktx:2.2.5'
@ -85,6 +84,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.24.0'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0'
testImplementation "io.mockk:mockk:1.10.0"
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'

View file

@ -2,45 +2,33 @@ package dev.lucasnlm.antimine.core.preferences
import android.content.Context
import androidx.preference.PreferenceManager
import dev.lucasnlm.antimine.common.level.models.Minefield
import javax.inject.Inject
interface IPreferencesManager {
fun getBoolean(key: String, defaultValue: Boolean): Boolean
fun putBoolean(key: String, value: Boolean)
fun getInt(key: String, defaultValue: Int): Int
fun putInt(key: String, value: Int)
fun removeKey(key: String)
fun contains(key: String): Boolean
}
class PreferencesManager @Inject constructor(
private val context: Context
) {
): IPreferencesManager {
private val preferences by lazy {
PreferenceManager.getDefaultSharedPreferences(context)
}
fun getCustomMode() = Minefield(
preferences.getInt(PREFERENCE_CUSTOM_GAME_WIDTH, 9),
preferences.getInt(PREFERENCE_CUSTOM_GAME_HEIGHT, 9),
preferences.getInt(PREFERENCE_CUSTOM_GAME_MINES, 9)
)
override fun getBoolean(key: String, defaultValue: Boolean) = preferences.getBoolean(key, defaultValue)
fun updateCustomMode(customMinefield: Minefield) {
preferences.edit().apply {
putInt(PREFERENCE_CUSTOM_GAME_WIDTH, customMinefield.width)
putInt(PREFERENCE_CUSTOM_GAME_HEIGHT, customMinefield.height)
putInt(PREFERENCE_CUSTOM_GAME_MINES, customMinefield.mines)
}.apply()
}
override fun putBoolean(key: String, value: Boolean) = preferences.edit().putBoolean(key, value).apply()
fun getBoolean(key: String, defaultValue: Boolean) = preferences.getBoolean(key, defaultValue)
override fun getInt(key: String, defaultValue: Int) = preferences.getInt(key, defaultValue)
fun putBoolean(key: String, value: Boolean) = preferences.edit().putBoolean(key, value).apply()
override fun putInt(key: String, value: Int) = preferences.edit().putInt(key, value).apply()
fun getInt(key: String, defaultValue: Int) = preferences.getInt(key, defaultValue)
override fun contains(key: String): Boolean = preferences.contains(key)
fun putInt(key: String, value: Int) = preferences.edit().putInt(key, value).apply()
fun removeKey(key: String) {
preferences.edit().remove(key).apply()
}
companion object {
private const val PREFERENCE_CUSTOM_GAME_WIDTH = "preference_custom_game_width"
private const val PREFERENCE_CUSTOM_GAME_HEIGHT = "preference_custom_game_height"
private const val PREFERENCE_CUSTOM_GAME_MINES = "preference_custom_game_mines"
}
override fun removeKey(key: String) = preferences.edit().remove(key).apply()
}

View file

@ -23,17 +23,25 @@ interface IPreferencesRepository {
}
class PreferencesRepository(
private val preferencesManager: PreferencesManager
private val preferencesManager: IPreferencesManager
) : IPreferencesRepository {
init {
migrateOldPreferences()
}
override fun customGameMode(): Minefield =
preferencesManager.getCustomMode()
override fun customGameMode(): Minefield = Minefield(
preferencesManager.getInt(PREFERENCE_CUSTOM_GAME_WIDTH, 9),
preferencesManager.getInt(PREFERENCE_CUSTOM_GAME_HEIGHT, 9),
preferencesManager.getInt(PREFERENCE_CUSTOM_GAME_MINES, 9)
)
override fun updateCustomGameMode(minefield: Minefield) =
preferencesManager.updateCustomMode(minefield)
override fun updateCustomGameMode(minefield: Minefield) {
preferencesManager.apply {
putInt(PREFERENCE_CUSTOM_GAME_WIDTH, minefield.width)
putInt(PREFERENCE_CUSTOM_GAME_HEIGHT, minefield.height)
putInt(PREFERENCE_CUSTOM_GAME_MINES, minefield.mines)
}
}
override fun getBoolean(key: String, defaultValue: Boolean): Boolean =
preferencesManager.getBoolean(key, defaultValue)
@ -48,33 +56,49 @@ class PreferencesRepository(
preferencesManager.putInt(key, value)
override fun useFlagAssistant(): Boolean =
getBoolean("preference_assistant", true)
getBoolean(PREFERENCE_ASSISTANT, true)
override fun useHapticFeedback(): Boolean =
getBoolean("preference_vibration", true)
getBoolean(PREFERENCE_VIBRATION, true)
override fun useLargeAreas(): Boolean =
getBoolean("preference_large_area", false)
getBoolean(PREFERENCE_USE_LARGE_TILE, false)
override fun useAnimations(): Boolean =
getBoolean("preference_animation", true)
getBoolean(PREFERENCE_ANIMATION, true)
override fun useQuestionMark(): Boolean =
getBoolean("preference_use_question_mark", true)
getBoolean(PREFERENCE_QUESTION_MARK, true)
override fun controlStyle(): ControlStyle {
val index = getInt("preference_control_style", -1)
val index = getInt(PREFERENCE_CONTROL_STYLE, -1)
return ControlStyle.values().getOrNull(index) ?: ControlStyle.Standard
}
override fun useControlStyle(controlStyle: ControlStyle) {
putInt("preference_control_style", controlStyle.ordinal)
putInt(PREFERENCE_CONTROL_STYLE, controlStyle.ordinal)
}
private fun migrateOldPreferences() {
if (getBoolean("preference_double_click_open", false)) {
useControlStyle(ControlStyle.DoubleClick)
preferencesManager.removeKey("preference_double_click_open")
if (preferencesManager.contains(PREFERENCE_OLD_DOUBLE_CLICK)) {
if (getBoolean(PREFERENCE_OLD_DOUBLE_CLICK, false)) {
useControlStyle(ControlStyle.DoubleClick)
}
preferencesManager.removeKey(PREFERENCE_OLD_DOUBLE_CLICK)
}
}
private companion object {
private const val PREFERENCE_VIBRATION = "preference_vibration"
private const val PREFERENCE_ASSISTANT = "preference_assistant"
private const val PREFERENCE_ANIMATION = "preference_animation"
private const val PREFERENCE_USE_LARGE_TILE = "preference_large_area"
private const val PREFERENCE_QUESTION_MARK = "preference_use_question_mark"
private const val PREFERENCE_CONTROL_STYLE = "preference_control_style"
private const val PREFERENCE_OLD_DOUBLE_CLICK = "preference_double_click_open"
private const val PREFERENCE_CUSTOM_GAME_WIDTH = "preference_custom_game_width"
private const val PREFERENCE_CUSTOM_GAME_HEIGHT = "preference_custom_game_height"
private const val PREFERENCE_CUSTOM_GAME_MINES = "preference_custom_game_mines"
}
}

View file

@ -0,0 +1,62 @@
package dev.lucasnlm.antimine.core.preferences
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
private class TestPreferenceManager: IPreferencesManager {
val values = mutableMapOf<String, Any>()
override fun getBoolean(key: String, defaultValue: Boolean): Boolean {
return values.getOrDefault(key, defaultValue) as Boolean
}
override fun putBoolean(key: String, value: Boolean) {
values[key] = value
}
override fun getInt(key: String, defaultValue: Int): Int {
return values.getOrDefault(key, defaultValue) as Int
}
override fun putInt(key: String, value: Int) {
values[key] = value
}
override fun removeKey(key: String) {
values.remove(key)
}
override fun contains(key: String): Boolean {
return values.containsKey(key)
}
}
class PreferencesRepositoryTest {
@Test
fun testMigrationWhenUsingDoubleClick() {
val preferenceManager = TestPreferenceManager()
preferenceManager.putBoolean("preference_double_click_open", true)
assertTrue(preferenceManager.values["preference_double_click_open"] as Boolean)
val preferencesRepository = PreferencesRepository(preferenceManager)
assertTrue(preferenceManager.values["preference_double_click_open"] == null)
assertEquals(1, preferenceManager.values["preference_control_style"])
assertFalse(preferencesRepository.getBoolean("preference_double_click_open", false))
}
@Test
fun testMigrationWhenNotUsingDoubleClick() {
val preferenceManager = TestPreferenceManager()
preferenceManager.putBoolean("preference_double_click_open", false)
assertFalse(preferenceManager.values["preference_double_click_open"] as Boolean)
val preferencesRepository = PreferencesRepository(preferenceManager)
assertTrue(preferenceManager.values["preference_double_click_open"] == null)
assertFalse(preferencesRepository.getBoolean("preference_double_click_open", false))
}
}