Skip to content
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

Fix reversed logic for migration of the legacy initially paused option #351

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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,
)
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/chiller3/bcr/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/chiller3/bcr/rule/RecordRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>?): Boolean = true

companion object {
Expand All @@ -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<String>?): Boolean =
contactLookupKeys?.isEmpty() ?: false

Expand All @@ -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<String>?): Boolean =
contactLookupKeys != null && lookupKey in contactLookupKeys

Expand Down