Merge remote-tracking branch 'origin/release/v2.13.0' into feat/contact-special-dates
# Conflicts: # app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt # app/src/main/java/de/jeanlucmakiola/calendula/ui/edit/EventEditScreen.kt # app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsScreen.kt # app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt # app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt
This commit is contained in:
@@ -5,8 +5,12 @@ import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.calendula.domain.EventFormField
|
||||
import de.jeanlucmakiola.calendula.domain.FontRole
|
||||
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarView
|
||||
import de.jeanlucmakiola.calendula.ui.common.IMPLEMENTED_VIEWS
|
||||
import de.jeanlucmakiola.calendula.ui.common.QuickSwitchConfig
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
@@ -150,6 +154,94 @@ class SettingsPrefsTest {
|
||||
assertThat(prefs.defaultFormFields.first()).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `quick-switch defaults to every view enabled in default order`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
val config = prefs.quickSwitchConfig.first()
|
||||
assertThat(config.order).containsExactlyElementsIn(IMPLEMENTED_VIEWS).inOrder()
|
||||
assertThat(config.enabled).containsExactlyElementsIn(IMPLEMENTED_VIEWS)
|
||||
assertThat(config.cycle).containsExactlyElementsIn(IMPLEMENTED_VIEWS).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `quick-switch config round-trips order and disabled views`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
val config = QuickSwitchConfig(
|
||||
order = listOf(CalendarView.Agenda, CalendarView.Month, CalendarView.Week, CalendarView.Day),
|
||||
enabled = setOf(CalendarView.Agenda, CalendarView.Month),
|
||||
)
|
||||
prefs.setQuickSwitchConfig(config)
|
||||
val loaded = prefs.quickSwitchConfig.first()
|
||||
assertThat(loaded.order).containsExactly(
|
||||
CalendarView.Agenda, CalendarView.Month, CalendarView.Week, CalendarView.Day,
|
||||
).inOrder()
|
||||
assertThat(loaded.enabled).containsExactly(CalendarView.Agenda, CalendarView.Month)
|
||||
assertThat(loaded.cycle).containsExactly(CalendarView.Agenda, CalendarView.Month).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `quick-switch parse appends missing views enabled and drops unknowns`(@TempDir tempDir: Path) = runTest {
|
||||
val store = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(store)
|
||||
// Only Day (disabled) and Week stored, plus a bogus name; Month & Agenda absent.
|
||||
store.updateData { p ->
|
||||
val m = p.toMutablePreferences()
|
||||
m[SettingsPrefs.QUICK_SWITCH_VIEWS_KEY] = "!Day,Week,Hologram"
|
||||
m
|
||||
}
|
||||
val config = prefs.quickSwitchConfig.first()
|
||||
// Stored order first (Day, Week), then the omitted views in default order.
|
||||
assertThat(config.order).containsExactly(
|
||||
CalendarView.Day, CalendarView.Week, CalendarView.Month, CalendarView.Agenda,
|
||||
).inOrder()
|
||||
// Day was explicitly disabled; the appended Month & Agenda default enabled.
|
||||
assertThat(config.enabled).containsExactly(
|
||||
CalendarView.Week, CalendarView.Month, CalendarView.Agenda,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `updateQuickSwitch transforms the stored value, so intents compose`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
// A toggle then a reorder, each expressed against the *stored* config —
|
||||
// the reorder must not resurrect the just-disabled view (the stale-UI-
|
||||
// snapshot race this API exists to prevent).
|
||||
prefs.updateQuickSwitch { it.copy(enabled = it.enabled - CalendarView.Day) }
|
||||
val newOrder = listOf(CalendarView.Agenda, CalendarView.Month, CalendarView.Week, CalendarView.Day)
|
||||
prefs.updateQuickSwitch { it.copy(order = newOrder) }
|
||||
val config = prefs.quickSwitchConfig.first()
|
||||
assertThat(config.order).containsExactlyElementsIn(newOrder).inOrder()
|
||||
assertThat(config.enabled).containsExactly(
|
||||
CalendarView.Agenda, CalendarView.Month, CalendarView.Week,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `drawer order defaults to the implemented order and round-trips`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
assertThat(prefs.drawerViewOrder.first()).containsExactlyElementsIn(IMPLEMENTED_VIEWS).inOrder()
|
||||
prefs.setDrawerViewOrder(
|
||||
listOf(CalendarView.Agenda, CalendarView.Day, CalendarView.Week, CalendarView.Month),
|
||||
)
|
||||
assertThat(prefs.drawerViewOrder.first()).containsExactly(
|
||||
CalendarView.Agenda, CalendarView.Day, CalendarView.Week, CalendarView.Month,
|
||||
).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `drawer order parse appends missing views and drops unknowns`(@TempDir tempDir: Path) = runTest {
|
||||
val store = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(store)
|
||||
store.updateData { p ->
|
||||
val m = p.toMutablePreferences()
|
||||
m[SettingsPrefs.DRAWER_VIEW_ORDER_KEY] = "Agenda,Nope,Week"
|
||||
m
|
||||
}
|
||||
assertThat(prefs.drawerViewOrder.first()).containsExactly(
|
||||
CalendarView.Agenda, CalendarView.Week, CalendarView.Month, CalendarView.Day,
|
||||
).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown stored form-field names are dropped`(@TempDir tempDir: Path) = runTest {
|
||||
val store = newDataStore(tempDir)
|
||||
@@ -245,6 +337,26 @@ class SettingsPrefsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `garbage per-calendar override values drop the entry, the none sentinel stays explicit`(
|
||||
@TempDir tempDir: Path,
|
||||
) = runTest {
|
||||
val store = newDataStore(tempDir)
|
||||
val prefs = SettingsPrefs(store)
|
||||
// Only "none" deliberately means an explicit no-reminder override; a
|
||||
// value with no valid minutes is garbage and must drop its entry so the
|
||||
// calendar inherits the global default instead of silently losing
|
||||
// reminders. Partially-valid values keep their salvageable minutes.
|
||||
store.updateData { p ->
|
||||
val m = p.toMutablePreferences()
|
||||
m[SettingsPrefs.CALENDAR_REMINDER_OVERRIDE_KEY] = "5=none;7=abc;8=;9=30,abc;11=15"
|
||||
m
|
||||
}
|
||||
assertThat(prefs.perCalendarReminderOverride.first()).containsExactly(
|
||||
5L, emptyList<Int>(), 9L, listOf(30), 11L, listOf(15),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `all-day default round-trips, including none`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
@@ -328,6 +440,19 @@ class SettingsPrefsTest {
|
||||
assertThat(prefs.snoozeMinutes.first()).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `custom-font stamps default to zero and bump per role independently`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = SettingsPrefs(newDataStore(tempDir))
|
||||
// Existing installs have no stamp key — that must read as 0.
|
||||
assertThat(prefs.brandFontStamp.first()).isEqualTo(0)
|
||||
assertThat(prefs.plainFontStamp.first()).isEqualTo(0)
|
||||
prefs.bumpCustomFontStamp(FontRole.BRAND)
|
||||
prefs.bumpCustomFontStamp(FontRole.BRAND)
|
||||
prefs.bumpCustomFontStamp(FontRole.PLAIN)
|
||||
assertThat(prefs.brandFontStamp.first()).isEqualTo(2)
|
||||
assertThat(prefs.plainFontStamp.first()).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `explicit week-start prefs resolve regardless of locale`() {
|
||||
assertThat(WeekStartPref.Day(DayOfWeek.MONDAY).resolveFirstDay(Locale.US))
|
||||
|
||||
@@ -52,4 +52,20 @@ class PostableAlertsTest {
|
||||
|
||||
assertThat(postable).isEqualTo(due)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `alert with unknown calendar id 0 is never treated as disabled`() {
|
||||
// Pre-upgrade snooze PendingIntents carry no calendar id (defaults to 0L).
|
||||
val preUpgrade = alert(1, calendarId = 0L)
|
||||
|
||||
assertThat(preUpgrade.isForDisabledCalendar(disabledCalendarIds = setOf(0L))).isFalse()
|
||||
assertThat(postableAlerts(listOf(preUpgrade), disabledCalendarIds = setOf(0L)))
|
||||
.containsExactly(preUpgrade)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isForDisabledCalendar matches only the disabled ids`() {
|
||||
assertThat(alert(1, calendarId = 200).isForDisabledCalendar(setOf(200))).isTrue()
|
||||
assertThat(alert(1, calendarId = 100).isForDisabledCalendar(setOf(200))).isFalse()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package de.jeanlucmakiola.calendula.data.reminders
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.nio.file.Path
|
||||
|
||||
class SuppressedReminderStoreTest {
|
||||
|
||||
private fun newDataStore(tempDir: Path): DataStore<Preferences> =
|
||||
PreferenceDataStoreFactory.create(
|
||||
produceFile = { tempDir.resolve("test_prefs.preferences_pb").toFile() },
|
||||
)
|
||||
|
||||
private fun alert(
|
||||
alertId: Long,
|
||||
calendarId: Long,
|
||||
endMillis: Long = Long.MAX_VALUE,
|
||||
title: String = "Event $alertId",
|
||||
location: String? = null,
|
||||
) = ReminderAlert(
|
||||
alertId = alertId,
|
||||
eventId = alertId * 10,
|
||||
calendarId = calendarId,
|
||||
beginMillis = 0L,
|
||||
endMillis = endMillis,
|
||||
title = title,
|
||||
location = location,
|
||||
isAllDay = false,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `encode then decode round-trips every field including delimiters`() {
|
||||
val original = alert(
|
||||
alertId = 7,
|
||||
calendarId = 42,
|
||||
endMillis = 123_456_789L,
|
||||
// Free-text with the field separator and other awkward characters.
|
||||
title = "Lunch | with | Alice",
|
||||
location = "Café, 3rd floor | room B",
|
||||
).copy(beginMillis = 100L, isAllDay = true)
|
||||
|
||||
val decoded = decodeStashEntry(encodeStashEntry(original))
|
||||
|
||||
assertThat(decoded).isEqualTo(original)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `decode returns null for a malformed entry`() {
|
||||
assertThat(decodeStashEntry("not-a-valid-entry")).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `null location round-trips`() {
|
||||
val original = alert(1, calendarId = 1, location = null)
|
||||
assertThat(decodeStashEntry(encodeStashEntry(original))).isEqualTo(original)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `recoverFor returns and removes only the re-enabled calendars`() = runTest {
|
||||
val store = SuppressedReminderStore(newDataStore(tempDir))
|
||||
val keep = alert(1, calendarId = 100)
|
||||
val recoverA = alert(2, calendarId = 200)
|
||||
val recoverB = alert(3, calendarId = 200)
|
||||
store.stash(listOf(keep, recoverA, recoverB), nowMillis = 0L)
|
||||
|
||||
val recovered = store.recoverFor(setOf(200L), nowMillis = 0L)
|
||||
|
||||
assertThat(recovered).containsExactly(recoverA, recoverB)
|
||||
// The still-disabled calendar's alert stays stashed; the recovered ones are gone.
|
||||
assertThat(store.recoverFor(setOf(100L, 200L), nowMillis = 0L)).containsExactly(keep)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stash drops alerts whose event already ended`() = runTest {
|
||||
val store = SuppressedReminderStore(newDataStore(tempDir))
|
||||
val past = alert(1, calendarId = 100, endMillis = 500L)
|
||||
val future = alert(2, calendarId = 100, endMillis = 2_000L)
|
||||
|
||||
store.stash(listOf(past, future), nowMillis = 1_000L)
|
||||
|
||||
assertThat(store.recoverFor(setOf(100L), nowMillis = 1_000L)).containsExactly(future)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `purgeExpired removes only entries past their event end`() = runTest {
|
||||
val store = SuppressedReminderStore(newDataStore(tempDir))
|
||||
// Stash both before "now" so neither is dropped on write, then advance time.
|
||||
store.stash(
|
||||
listOf(
|
||||
alert(1, calendarId = 100, endMillis = 500L),
|
||||
alert(2, calendarId = 100, endMillis = 2_000L),
|
||||
),
|
||||
nowMillis = 0L,
|
||||
)
|
||||
|
||||
store.purgeExpired(nowMillis = 1_000L)
|
||||
|
||||
assertThat(store.recoverFor(setOf(100L), nowMillis = 0L).map { it.alertId })
|
||||
.containsExactly(2L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stash replaces an existing entry with the same alert id`() = runTest {
|
||||
val store = SuppressedReminderStore(newDataStore(tempDir))
|
||||
store.stash(listOf(alert(1, calendarId = 100, title = "old")), nowMillis = 0L)
|
||||
store.stash(listOf(alert(1, calendarId = 100, title = "new")), nowMillis = 0L)
|
||||
|
||||
val recovered = store.recoverFor(setOf(100L), nowMillis = 0L)
|
||||
|
||||
assertThat(recovered).hasSize(1)
|
||||
assertThat(recovered.single().title).isEqualTo("new")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isRelevantAt is true up to the event end and false after`() {
|
||||
val a = alert(1, calendarId = 1, endMillis = 1_000L)
|
||||
assertThat(a.isRelevantAt(999L)).isTrue()
|
||||
assertThat(a.isRelevantAt(1_000L)).isTrue()
|
||||
assertThat(a.isRelevantAt(1_001L)).isFalse()
|
||||
}
|
||||
|
||||
@TempDir
|
||||
lateinit var tempDir: Path
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.calendula.data.prefs.CalendarReminderOverride
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* The pure emit rule behind [ReminderDefaultPicker]: a non-empty selection is
|
||||
* [CalendarReminderOverride.Minutes] (normalised), while clearing the last time
|
||||
* reverts to [CalendarReminderOverride.Inherit] on a per-calendar picker (so an
|
||||
* accidental toggle-undo can't silently wipe the calendar's default) and to
|
||||
* explicit [CalendarReminderOverride.None] on the global default.
|
||||
*/
|
||||
class ReminderOverrideForMinutesTest {
|
||||
|
||||
@Test
|
||||
fun `empty selection on a per-calendar picker reverts to inherit`() {
|
||||
assertThat(reminderOverrideForMinutes(emptyList(), allowInherit = true))
|
||||
.isEqualTo(CalendarReminderOverride.Inherit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty selection on the global default is explicit none`() {
|
||||
assertThat(reminderOverrideForMinutes(emptyList(), allowInherit = false))
|
||||
.isEqualTo(CalendarReminderOverride.None)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a non-empty selection is normalised minutes regardless of scope`() {
|
||||
assertThat(reminderOverrideForMinutes(listOf(30, 10, 10, 60), allowInherit = true))
|
||||
.isEqualTo(CalendarReminderOverride.Minutes(listOf(10, 30, 60)))
|
||||
assertThat(reminderOverrideForMinutes(listOf(30, 10, 10, 60), allowInherit = false))
|
||||
.isEqualTo(CalendarReminderOverride.Minutes(listOf(10, 30, 60)))
|
||||
}
|
||||
}
|
||||
@@ -91,4 +91,33 @@ class ViewBackStackTest {
|
||||
assertThat(viewBaseStack(CalendarView.Agenda, CalendarView.Agenda))
|
||||
.containsExactly(CalendarView.Agenda)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `next cycles through the configured views in order, wrapping around`() {
|
||||
val cycle = listOf(CalendarView.Month, CalendarView.Agenda)
|
||||
assertThat(CalendarView.Month.next(cycle)).isEqualTo(CalendarView.Agenda)
|
||||
// Wraps back to the first from the last.
|
||||
assertThat(CalendarView.Agenda.next(cycle)).isEqualTo(CalendarView.Month)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `next from a view outside the cycle lands on the first enabled view`() {
|
||||
// Day was disabled in the quick-switch cycle but is still the current view.
|
||||
val cycle = listOf(CalendarView.Week, CalendarView.Month)
|
||||
assertThat(CalendarView.Day.next(cycle)).isEqualTo(CalendarView.Week)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `next on an empty cycle stays put`() {
|
||||
assertThat(CalendarView.Week.next(emptyList())).isEqualTo(CalendarView.Week)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `quick-switch cycle keeps enabled views in their configured order`() {
|
||||
val config = QuickSwitchConfig(
|
||||
order = listOf(CalendarView.Agenda, CalendarView.Day, CalendarView.Month, CalendarView.Week),
|
||||
enabled = setOf(CalendarView.Agenda, CalendarView.Month),
|
||||
)
|
||||
assertThat(config.cycle).containsExactly(CalendarView.Agenda, CalendarView.Month).inOrder()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package de.jeanlucmakiola.calendula.ui.theme
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FontsTest {
|
||||
|
||||
@Test
|
||||
fun `fromToken round-trips every bundled font`() {
|
||||
BundledFont.entries.forEach { font ->
|
||||
assertThat(BundledFont.fromToken(font.token)).isEqualTo(font)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fromToken is null for system, custom, and unknown tokens`() {
|
||||
assertThat(BundledFont.fromToken(FONT_SYSTEM_TOKEN)).isNull()
|
||||
assertThat(BundledFont.fromToken(FONT_CUSTOM_TOKEN)).isNull()
|
||||
assertThat(BundledFont.fromToken("not-a-font")).isNull()
|
||||
assertThat(BundledFont.fromToken(null)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a bumped custom-font stamp breaks font-settings equality`() {
|
||||
// Re-importing keeps the same "custom" token, so only the stamp can make
|
||||
// the settings re-emit and invalidate remember() keys downstream.
|
||||
val before = AppFontSettings(brand = FONT_CUSTOM_TOKEN, brandStamp = 1)
|
||||
assertThat(before.copy(brandStamp = 2)).isNotEqualTo(before)
|
||||
assertThat(before.copy(plainStamp = 1)).isNotEqualTo(before)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `system for both roles returns the untouched default typography`() {
|
||||
assertThat(calendulaTypography(brand = null, plain = null))
|
||||
.isSameInstanceAs(CalendulaTypography)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `brand drives display and headline, plain drives title, body, and label`() {
|
||||
val brand = BundledFont.Lora.family
|
||||
val plain = BundledFont.JetBrainsMono.family
|
||||
val typography = calendulaTypography(brand = brand, plain = plain)
|
||||
|
||||
assertThat(typography.displayLarge.fontFamily).isEqualTo(brand)
|
||||
assertThat(typography.headlineSmall.fontFamily).isEqualTo(brand)
|
||||
assertThat(typography.titleLarge.fontFamily).isEqualTo(plain)
|
||||
assertThat(typography.bodyMedium.fontFamily).isEqualTo(plain)
|
||||
assertThat(typography.labelSmall.fontFamily).isEqualTo(plain)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a null role keeps that role's default family while the other is applied`() {
|
||||
val plain = BundledFont.Lora.family
|
||||
val typography = calendulaTypography(brand = null, plain = plain)
|
||||
|
||||
// Brand styles keep the Material default; plain styles switch.
|
||||
assertThat(typography.displayLarge.fontFamily)
|
||||
.isEqualTo(CalendulaTypography.displayLarge.fontFamily)
|
||||
assertThat(typography.bodyLarge.fontFamily).isEqualTo(plain)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user