-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for Theme appearance preferences #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SayanthRock
wants to merge
1
commit into
main
Choose a base branch
from
test/appearance-preferences-168628106807272731
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2023-11-20 - [Testing preferences that use EncryptedSharedPreferences] | ||
| **Learning:** `EncryptedSharedPreferences` requires an Android Keystore which doesn't work correctly in basic Robolectric tests due to missing cryptographic algorithm implementations (java.security.KeyStoreException). | ||
| **Action:** When testing classes like `AppearancePreferences` that rely on `EncryptedSharedPreferences`, create a `Fake` wrapper class that overrides the internal `SharedPreferences` object with a basic, in-memory implementation for testing, rather than trying to configure Robolectric to support the Android KeyStore correctly. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
app/src/test/java/com/rockmusic/app/presentation/theme/AppearancePreferencesTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package com.rockmusic.app.presentation.theme | ||
|
|
||
| import android.content.Context | ||
| import android.content.SharedPreferences | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
| import androidx.compose.runtime.saveable.SaverScope | ||
|
|
||
| class FakeAppearancePreferences(context: Context) : AppearancePreferences(context) { | ||
| private val store = mutableMapOf<String, Any>() | ||
|
|
||
| override val preferences: SharedPreferences = object : SharedPreferences { | ||
| override fun getAll(): MutableMap<String, *> = store.toMutableMap() | ||
| override fun getString(key: String, defValue: String?): String? = store[key] as? String ?: defValue | ||
| override fun getStringSet(key: String, defValues: MutableSet<String>?): MutableSet<String>? { @Suppress("UNCHECKED_CAST") return store[key] as? MutableSet<String> ?: defValues } | ||
| override fun getInt(key: String, defValue: Int): Int = store[key] as? Int ?: defValue | ||
| override fun getLong(key: String, defValue: Long): Long = store[key] as? Long ?: defValue | ||
| override fun getFloat(key: String, defValue: Float): Float = store[key] as? Float ?: defValue | ||
| override fun getBoolean(key: String, defValue: Boolean): Boolean = store[key] as? Boolean ?: defValue | ||
| override fun contains(key: String): Boolean = store.containsKey(key) | ||
|
|
||
| override fun edit(): SharedPreferences.Editor = object : SharedPreferences.Editor { | ||
| override fun putString(key: String, value: String?): SharedPreferences.Editor { | ||
| if (value == null) store.remove(key) else store[key] = value | ||
| return this | ||
| } | ||
| override fun putStringSet(key: String, values: MutableSet<String>?): SharedPreferences.Editor { | ||
| if (values == null) store.remove(key) else store[key] = values | ||
| return this | ||
| } | ||
| override fun putInt(key: String, value: Int): SharedPreferences.Editor { store[key] = value; return this } | ||
| override fun putLong(key: String, value: Long): SharedPreferences.Editor { store[key] = value; return this } | ||
| override fun putFloat(key: String, value: Float): SharedPreferences.Editor { store[key] = value; return this } | ||
| override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor { store[key] = value; return this } | ||
| override fun remove(key: String): SharedPreferences.Editor { store.remove(key); return this } | ||
| override fun clear(): SharedPreferences.Editor { store.clear(); return this } | ||
| override fun commit(): Boolean = true | ||
| override fun apply() {} | ||
| } | ||
|
|
||
| override fun registerOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {} | ||
| override fun unregisterOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {} | ||
| } | ||
| } | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [34]) | ||
| class AppearancePreferencesTest { | ||
|
|
||
| private lateinit var preferences: FakeAppearancePreferences | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| preferences = FakeAppearancePreferences(ApplicationProvider.getApplicationContext()) | ||
| } | ||
|
|
||
| @Test | ||
| fun load_returnsDefaultSettings_whenNoPreferencesSaved() { | ||
| val settings = preferences.load() | ||
|
|
||
| assertEquals(RockThemeMode.SYSTEM, settings.themeMode) | ||
| assertTrue(settings.useSystemColor) | ||
| assertTrue(settings.useBlurFrames) | ||
| } | ||
|
|
||
| @Test | ||
| fun save_and_load_restoresSavedSettings() { | ||
| val customSettings = AppearanceSettings( | ||
| themeMode = RockThemeMode.DARK, | ||
| useSystemColor = false, | ||
| useBlurFrames = false, | ||
| ) | ||
|
|
||
| preferences.save(customSettings) | ||
|
|
||
| val loadedSettings = preferences.load() | ||
|
|
||
| assertEquals(RockThemeMode.DARK, loadedSettings.themeMode) | ||
| assertFalse(loadedSettings.useSystemColor) | ||
| assertFalse(loadedSettings.useBlurFrames) | ||
| } | ||
|
|
||
| @Test | ||
| fun load_handlesInvalidEnumSafely() { | ||
| preferences.preferences.edit().putString("theme_mode", "INVALID_ENUM_VALUE").commit() | ||
| val loadedSettings = preferences.load() | ||
| assertEquals(RockThemeMode.SYSTEM, loadedSettings.themeMode) | ||
| } | ||
|
|
||
| @Test | ||
| fun saver_savesAndRestoresCorrectly() { | ||
| val customSettings = AppearanceSettings( | ||
| themeMode = RockThemeMode.LIGHT, | ||
| useSystemColor = false, | ||
| useBlurFrames = false, | ||
| ) | ||
|
|
||
| val savedList = with(AppearanceSettingsSaver) { | ||
| SaverScope { true }.save(customSettings) | ||
| } as List<*> | ||
|
|
||
| assertEquals(RockThemeMode.LIGHT.name, savedList[0]) | ||
| assertEquals(false, savedList[1]) | ||
| assertEquals(false, savedList[2]) | ||
|
|
||
| val restoredSettings = AppearanceSettingsSaver.restore(savedList as Any) | ||
|
|
||
| assertEquals(customSettings, restoredSettings) | ||
| } | ||
|
|
||
| @Test | ||
| fun saver_handlesInvalidEnumSafely() { | ||
| val invalidList = listOf( | ||
| "INVALID_ENUM_VALUE", | ||
| true, | ||
| true, | ||
| ) | ||
|
|
||
| val restoredSettings = AppearanceSettingsSaver.restore(invalidList) | ||
|
|
||
| assertEquals(RockThemeMode.SYSTEM, restoredSettings?.themeMode) | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.