Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

Defake the skin migration test

Rishabh Bajpai29d6fe7bc51c
diff --git a/app/src/main/java/com/gotcha/data/SettingsRepository.kt b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
index 76f759b..c6f774c 100644
--- a/app/src/main/java/com/gotcha/data/SettingsRepository.kt
+++ b/app/src/main/java/com/gotcha/data/SettingsRepository.kt
@@ -483,6 +483,13 @@ internal fun migrateSkinId(legacyThemeMode: String?): String =
else -> SKIN_VELLUM
}
+/**
+ * Maps a retired stored skin id to its replacement, or null to leave the
+ * stored value alone. Pure so the mapping is testable without preferences.
+ */
+internal fun retiredSkinIdReplacement(stored: String?): String? =
+ if (stored == "nocturne") Skins.Samosa.id else null
+
/** The preference file [SettingsRepository] encrypts into. */
internal const val SETTINGS_PREFS_FILE = "gotcha_settings"
@@ -596,9 +603,9 @@ class SettingsRepository(context: Context) : SettingsStore {
// The retired faceted theme persists here as "nocturne" until it is
// rewritten. Migrate it on first read so every reader of the setting,
// not just the theme resolver, sees the replacement id.
- if (stored == "nocturne") {
- prefs.edit().putString(KEY_SKIN_ID, Skins.Samosa.id).apply()
- return Skins.Samosa.id
+ retiredSkinIdReplacement(stored)?.let { replacement ->
+ prefs.edit().putString(KEY_SKIN_ID, replacement).apply()
+ return replacement
}
if (stored != null) return stored
val migrated = migrateSkinId(prefs.getString(KEY_LEGACY_THEME_MODE, null))
diff --git a/app/src/test/java/com/gotcha/data/SettingsSkinMigrationTest.kt b/app/src/test/java/com/gotcha/data/SettingsSkinMigrationTest.kt
index 32c8197..407d95e 100644
--- a/app/src/test/java/com/gotcha/data/SettingsSkinMigrationTest.kt
+++ b/app/src/test/java/com/gotcha/data/SettingsSkinMigrationTest.kt
@@ -1,59 +1,33 @@
package com.gotcha.data
-import android.content.Context
-import androidx.test.core.app.ApplicationProvider
-import com.gotcha.testsupport.FakeAndroidKeyStore
import com.gotcha.ui.theme.Skins
import org.junit.Assert.assertEquals
-import org.junit.Before
+import org.junit.Assert.assertNull
import org.junit.Test
-import org.junit.runner.RunWith
-import org.robolectric.RobolectricTestRunner
-import org.robolectric.annotation.Config
/**
- * The retired faceted theme ("nocturne") migrates to Samosa at the data layer,
- * so every reader of the stored setting sees the replacement id without the
- * user having to re-select an already-selected tile.
+ * The retired faceted theme ("nocturne") migrates to Samosa; every other
+ * stored id passes through untouched.
*/
-@RunWith(RobolectricTestRunner::class)
-@Config(sdk = [34])
class SettingsSkinMigrationTest {
- private val context: Context = ApplicationProvider.getApplicationContext()
- private lateinit var repository: SettingsRepository
-
- @Before
- fun setUp() {
- FakeAndroidKeyStore.setUp()
- // Hermetic prefs: the suite shares one JVM, and EncryptedSharedPrefs
- // reads the file from disk on creation while apply() flushes async,
- // so a leftover file from another class races the first read. A
- // synchronous delete leaves no race.
- context.deleteSharedPreferences(SETTINGS_PREFS_FILE)
- repository = SettingsRepository(context)
- }
-
@Test
- fun `a stored nocturne id loads as samosa`() {
- repository.save(Settings(skinId = "nocturne"))
-
- assertEquals(Skins.Samosa.id, repository.load().skinId)
- assertEquals(Skins.Samosa.id, repository.loadSkinId())
+ fun `nocturne maps to samosa`() {
+ assertEquals(Skins.Samosa.id, retiredSkinIdReplacement("nocturne"))
}
@Test
- fun `the migration persists, so the stale id is gone on the next load`() {
- repository.save(Settings(skinId = "nocturne"))
- repository.load()
-
- assertEquals(Skins.Samosa.id, SettingsRepository(context).loadSkinId())
+ fun `current ids map to nothing`() {
+ Skins.all.forEach { skin ->
+ assertNull(
+ "a current id must not migrate, got ${retiredSkinIdReplacement(skin.id)}",
+ retiredSkinIdReplacement(skin.id)
+ )
+ }
}
@Test
- fun `other stored ids pass through untouched`() {
- repository.save(Settings(skinId = Skins.Orchid.id))
-
- assertEquals(Skins.Orchid.id, repository.loadSkinId())
+ fun `a missing id maps to nothing`() {
+ assertNull(retiredSkinIdReplacement(null))
}
}