From e1bc3c8eda9e47eda7f9f5e0e26271d331cb8951 Mon Sep 17 00:00:00 2001 From: Andrew Gunnerson Date: Tue, 23 May 2023 13:11:47 -0400 Subject: [PATCH] Fix reversed logic for migration of the legacy initially paused option Fixes: #350 Signed-off-by: Andrew Gunnerson --- .../chiller3/bcr/ExampleInstrumentedTest.kt | 24 ------ .../chiller3/bcr/PreferenceMigrationTest.kt | 78 +++++++++++++++++++ .../main/java/com/chiller3/bcr/Preferences.kt | 6 +- .../java/com/chiller3/bcr/rule/RecordRule.kt | 6 +- 4 files changed, 84 insertions(+), 30 deletions(-) delete mode 100644 app/src/androidTest/java/com/chiller3/bcr/ExampleInstrumentedTest.kt create mode 100644 app/src/androidTest/java/com/chiller3/bcr/PreferenceMigrationTest.kt diff --git a/app/src/androidTest/java/com/chiller3/bcr/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/chiller3/bcr/ExampleInstrumentedTest.kt deleted file mode 100644 index 6dc91f3df..000000000 --- a/app/src/androidTest/java/com/chiller3/bcr/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.chiller3.bcr - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.chiller3.bcr", appContext.packageName) - } -} diff --git a/app/src/androidTest/java/com/chiller3/bcr/PreferenceMigrationTest.kt b/app/src/androidTest/java/com/chiller3/bcr/PreferenceMigrationTest.kt new file mode 100644 index 000000000..b20bdfab7 --- /dev/null +++ b/app/src/androidTest/java/com/chiller3/bcr/PreferenceMigrationTest.kt @@ -0,0 +1,78 @@ +package com.chiller3.bcr + +import androidx.core.content.edit +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.chiller3.bcr.rule.RecordRule + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +@RunWith(AndroidJUnit4::class) +class PreferenceMigrationTest { + private fun withNoRecordRules(block: (prefs: Preferences) -> Unit) { + val context = InstrumentationRegistry.getInstrumentation().targetContext + val prefs = Preferences(context) + val oldRecordRules = prefs.recordRules + + try { + prefs.recordRules = null + block(prefs) + } finally { + prefs.recordRules = oldRecordRules + } + } + + @Test + fun migrateInitiallyPausedUnset() { + withNoRecordRules { prefs -> + prefs.prefs.edit { + remove(Preferences.PREF_INITIALLY_PAUSED) + } + + prefs.migrateInitiallyPaused() + + assertEquals(null, prefs.recordRules) + } + } + + @Test + fun migrateInitiallyPausedOn() { + withNoRecordRules { prefs -> + prefs.prefs.edit { + putBoolean(Preferences.PREF_INITIALLY_PAUSED, true) + } + + prefs.migrateInitiallyPaused() + + assertEquals( + listOf( + RecordRule.UnknownCalls(false), + RecordRule.AllCalls(false), + ), + prefs.recordRules, + ) + } + } + + @Test + fun migrateInitiallyPausedOff() { + withNoRecordRules { prefs -> + prefs.prefs.edit { + putBoolean(Preferences.PREF_INITIALLY_PAUSED, false) + } + + prefs.migrateInitiallyPaused() + + assertEquals( + listOf( + RecordRule.UnknownCalls(true), + RecordRule.AllCalls(true), + ), + prefs.recordRules, + ) + } + } +} diff --git a/app/src/main/java/com/chiller3/bcr/Preferences.kt b/app/src/main/java/com/chiller3/bcr/Preferences.kt index 618d4701e..0556ee71e 100644 --- a/app/src/main/java/com/chiller3/bcr/Preferences.kt +++ b/app/src/main/java/com/chiller3/bcr/Preferences.kt @@ -32,7 +32,7 @@ class Preferences(private val context: Context) { const val PREF_RULE_PREFIX = "rule_" // Legacy preferences - private const val PREF_INITIALLY_PAUSED = "initially_paused" + internal const val PREF_INITIALLY_PAUSED = "initially_paused" // Not associated with a UI preference private const val PREF_DEBUG_MODE = "debug_mode" @@ -60,7 +60,7 @@ class Preferences(private val context: Context) { key == PREF_FORMAT_NAME || key.startsWith(PREF_FORMAT_PARAM_PREFIX) } - private val prefs = PreferenceManager.getDefaultSharedPreferences(context) + internal val prefs = PreferenceManager.getDefaultSharedPreferences(context) /** * Get a unsigned integer preference value. @@ -266,7 +266,7 @@ class Preferences(private val context: Context) { */ fun migrateInitiallyPaused() { if (prefs.contains(PREF_INITIALLY_PAUSED)) { - val oldValue = prefs.getBoolean(PREF_INITIALLY_PAUSED, false) + val oldValue = !prefs.getBoolean(PREF_INITIALLY_PAUSED, false) recordRules = listOf( RecordRule.UnknownCalls(oldValue), RecordRule.AllCalls(oldValue), diff --git a/app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt b/app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt index f7018d9cf..e5ee448b4 100644 --- a/app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt +++ b/app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt @@ -23,7 +23,7 @@ sealed class RecordRule { editor.putBoolean(prefix + PREF_SUFFIX_RECORD, record) } - class AllCalls(override val record: Boolean) : RecordRule() { + data class AllCalls(override val record: Boolean) : RecordRule() { override fun matches(contactLookupKeys: Collection?): Boolean = true companion object { @@ -35,7 +35,7 @@ sealed class RecordRule { } } - class UnknownCalls(override val record: Boolean) : RecordRule() { + data class UnknownCalls(override val record: Boolean) : RecordRule() { override fun matches(contactLookupKeys: Collection?): Boolean = contactLookupKeys?.isEmpty() ?: false @@ -48,7 +48,7 @@ sealed class RecordRule { } } - class Contact(val lookupKey: String, override val record: Boolean) : RecordRule() { + data class Contact(val lookupKey: String, override val record: Boolean) : RecordRule() { override fun matches(contactLookupKeys: Collection?): Boolean = contactLookupKeys != null && lookupKey in contactLookupKeys