feat(worldclock): the tab, the picker, and the face that answers at a glance

A hero face for home over a list of cities, each carrying its time, its offset
and how many days apart it is from you. The dial morphs between a circle and a
sun with the hour *there*: the one showpiece M0 reserved, spent on information
rather than decoration — a glance says "it is the middle of the night for them"
with no sentence needed.

The picker searches the device's own zones by word prefix. The list reorders by
drag **and** by move-up/move-down actions, because a drag-only reorder is
unreachable by a screen reader; each row reads as one sentence rather than
three texts in child order. Home is set by hand or left following the device.
At the twenty-fourth city the refusal is written on the screen, not only in the
content description.
This commit is contained in:
makiolaj committed 2026-09-22 13:05:18 +02:00
1 parent 5d11921c19
commit 26f1effb4f
16 files changed
+2394 -9

No files matched your search

@@ -0,0 +1,87 @@
package de.jeanlucmakiola.clockula.testing
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import de.jeanlucmakiola.clockula.data.prefs.ClockPrefs
import de.jeanlucmakiola.clockula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.clockula.data.worldclocks.WorldClockRepository
import de.jeanlucmakiola.clockula.data.worldclocks.WorldClockRepositoryImpl
import de.jeanlucmakiola.clockula.data.zones.ZoneDirectory
import de.jeanlucmakiola.clockula.domain.WorldClock
import de.jeanlucmakiola.clockula.ui.worldclock.WorldClockSource
import de.jeanlucmakiola.clockula.ui.worldclock.WorldClockViewModel
import de.jeanlucmakiola.floret.prefs.PrefStore
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import java.nio.file.Path
import kotlin.time.Instant
/**
* The **real** [WorldClockViewModel] over the real [WorldClockSource], the real
* [ZoneDirectory], the real [WorldClockRepositoryImpl] across [FakeWorldClockDao]
* (which *extends* the abstract DAO, so `addIfAbsent` and `reorder`'s real
* `@Transaction` bodies are under test) and a real DataStore under a `@TempDir`
* — `StopwatchEngineHarness`'s arrangement, for a tab with no engine (M8 D23).
*/
class WorldClockHarness(
tempDir: Path,
storeScope: CoroutineScope,
io: CoroutineDispatcher,
now: Instant = T0,
val dao: FakeWorldClockDao = FakeWorldClockDao(),
) {
val wallClock: FakeWallClock = FakeWallClock(now)
val zones: FakeZoneProvider = FakeZoneProvider()
val ticker: FakeTicker = FakeTicker()
val names: FakeZoneNames = FakeZoneNames()
val store: PrefStore = PrefStore(
PreferenceDataStoreFactory.create(
scope = storeScope,
produceFile = { tempDir.resolve("clockula_prefs_test.preferences_pb").toFile() },
),
)
val settings: SettingsPrefs = SettingsPrefs(store)
val worldClocks: WorldClockRepository = WorldClockRepositoryImpl(dao)
val directory: ZoneDirectory = ZoneDirectory(names, zones, io)
internal val source: WorldClockSource = WorldClockSource(
worldClocks = worldClocks,
settings = settings,
directory = directory,
zones = zones,
wallClock = wallClock,
ticker = ticker,
)
val viewModel: WorldClockViewModel = WorldClockViewModel(
worldClocks = worldClocks,
settings = settings,
source = source,
)
/** The table as storage holds it right now, in `sort_order` then `id`. */
suspend fun stored(): List<WorldClock> = worldClocks.worldClocks().first()
/** `home_zone_id` as the store holds it right now. */
suspend fun storedHomeZoneId(): String? = store.flow(ClockPrefs.homeZoneId).first()
}
/** The harness on `runTest`'s own scheduler, so nothing waits on a real clock. */
fun TestScope.worldClockHarness(
tempDir: Path,
now: Instant = T0,
dao: FakeWorldClockDao = FakeWorldClockDao(),
): WorldClockHarness = WorldClockHarness(
tempDir = tempDir,
storeScope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
io = UnconfinedTestDispatcher(testScheduler),
now = now,
dao = dao,
)
@@ -0,0 +1,246 @@
package de.jeanlucmakiola.clockula.ui.worldclock
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.domain.WorldClock
import de.jeanlucmakiola.clockula.domain.worldclock.DayLabel
import de.jeanlucmakiola.clockula.domain.worldclock.OffsetLabel
import de.jeanlucmakiola.clockula.domain.worldclock.ZoneEntry
import de.jeanlucmakiola.clockula.testing.BERLIN
import de.jeanlucmakiola.clockula.testing.T0
import de.jeanlucmakiola.clockula.testing.UTC_ZONE
import org.junit.jupiter.api.Test
import java.time.LocalTime
import java.time.ZoneId
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Instant
/**
* §5.7 — 18 cases. The tab as pure data: the row's name, its offset and day
* relative to **home**, the truncation that makes the 200 ms tick cheap (D18),
* and the row whose zone the device no longer knows (D14).
*/
class WorldClockRowsTest {
private val newYork = "America/New_York"
private val kolkata = "Asia/Kolkata"
private val honolulu = "Pacific/Honolulu"
private val kiritimati = "Pacific/Kiritimati"
private val sydney = "Australia/Sydney"
private val berlin = "Europe/Berlin"
/** 2024-01-15T12:00:00Z. */
private val january: Instant = Instant.fromEpochMilliseconds(1_705_320_000_000L)
/** 2024-07-15T12:00:00Z. */
private val july: Instant = Instant.fromEpochMilliseconds(1_721_044_800_000L)
/** 2023-11-14T12:00:00Z — midday in UTC. */
private val midday: Instant = Instant.fromEpochMilliseconds(1_699_963_200_000L)
private val entries: Map<String, ZoneEntry> = listOf(
ZoneEntry(berlin, "Berlin", "Germany"),
ZoneEntry(newYork, "New York", "United States"),
ZoneEntry(kolkata, "Kolkata", "India"),
ZoneEntry(honolulu, "Honolulu", "United States"),
ZoneEntry(kiritimati, "Kiritimati", "Kiribati"),
ZoneEntry(sydney, "Sydney", "Australia"),
).associateBy { it.zoneId }
private fun clock(
id: Long = 1L,
zoneId: String,
label: String? = null,
sortOrder: Int = 0,
) = WorldClock(id = id, zoneId = zoneId, label = label, sortOrder = sortOrder)
private fun rows(
clocks: List<WorldClock>,
homeZone: ZoneId = BERLIN,
now: Instant = T0,
zoneNames: Map<String, String?> = emptyMap(),
) = WorldClockRows.from(clocks, homeZone, now, entries, zoneNames)
// --- from ---
/** §5.7 #1 */
@Test
fun `an unlabelled row is named by ICU's city`() {
val row = rows(listOf(clock(zoneId = berlin))).single()
assertThat(row.name).isEqualTo("Berlin")
}
/** §5.7 #2 */
@Test
fun `the user's label wins over the ICU city`() {
val row = rows(listOf(clock(zoneId = berlin, label = "Büro"))).single()
assertThat(row.name).isEqualTo("Büro")
}
/** §5.7 #3 */
@Test
fun `a zone with no entry falls back to its last segment`() {
val row = rows(listOf(clock(zoneId = "Asia/Ho_Chi_Minh"))).single()
assertThat(row.name).isEqualTo("Ho Chi Minh")
}
/** §5.7 #4 */
@Test
fun `the rows keep the repository's order`() {
val clocks = listOf(
clock(id = 1L, zoneId = newYork, sortOrder = 0),
clock(id = 2L, zoneId = kolkata, sortOrder = 1),
clock(id = 3L, zoneId = honolulu, sortOrder = 2),
)
val built = rows(clocks)
assertThat(built.map { it.id }).containsExactly(1L, 2L, 3L).inOrder()
}
/** §5.7 #5 */
@Test
fun `a western city reads as behind home, on the same day`() {
val row = rows(listOf(clock(zoneId = newYork))).single()
assertThat(row.time).isEqualTo(LocalTime.of(17, 13))
assertThat(row.offset).isEqualTo(OffsetLabel.Behind(6, 0))
assertThat(row.day).isEqualTo(DayLabel.TODAY)
}
/** §5.7 #6 */
@Test
fun `an eastern city reads as ahead of home, already tomorrow`() {
val row = rows(listOf(clock(zoneId = kolkata))).single()
assertThat(row.time).isEqualTo(LocalTime.of(3, 43))
assertThat(row.offset).isEqualTo(OffsetLabel.Ahead(4, 30))
assertThat(row.day).isEqualTo(DayLabel.TOMORROW)
}
/** §5.7 #7 */
@Test
fun `a city a day behind home reads as yesterday`() {
val row = rows(listOf(clock(zoneId = honolulu)), homeZone = ZoneId.of(kiritimati)).single()
assertThat(row.day).isEqualTo(DayLabel.YESTERDAY)
}
/** §5.7 #8 */
@Test
fun `home's own row reads as the same time, today`() {
val row = rows(listOf(clock(zoneId = berlin))).single()
assertThat(row.offset).isEqualTo(OffsetLabel.Same)
assertThat(row.day).isEqualTo(DayLabel.TODAY)
}
/** §5.7 #9 */
@Test
fun `a row's time is truncated to the minute, so half the ticks change nothing`() {
val clocks = listOf(clock(zoneId = newYork))
val before = rows(clocks, now = T0)
val after = rows(clocks, now = T0 + 30.seconds)
assertThat(after).isEqualTo(before)
}
/** §5.7 #10 */
@Test
fun `the truncation does not freeze the row`() {
val clocks = listOf(clock(zoneId = newYork))
val before = rows(clocks, now = T0)
val after = rows(clocks, now = T0 + 2.minutes)
assertThat(after).isNotEqualTo(before)
}
/** §5.7 #11 */
@Test
fun `a zone the device no longer knows is kept, blank and named from its id`() {
val row = rows(listOf(clock(zoneId = "Mars/Olympus"))).single()
assertThat(row.known).isFalse()
assertThat(row.time).isNull()
assertThat(row.offset).isNull()
assertThat(row.day).isNull()
assertThat(row.name).isEqualTo("Olympus")
}
/** §5.7 #12 */
@Test
fun `a known zone carries all three readings`() {
val row = rows(listOf(clock(zoneId = newYork))).single()
assertThat(row.known).isTrue()
assertThat(row.time).isNotNull()
assertThat(row.offset).isNotNull()
assertThat(row.day).isNotNull()
}
/** §5.7 #13 */
@Test
fun `the long zone name follows the map it is given`() {
val clocks = listOf(clock(zoneId = newYork))
val named = rows(clocks, zoneNames = mapOf(newYork to "Eastern Standard Time")).single()
val unnamed = rows(clocks).single()
assertThat(named.zoneName).isEqualTo("Eastern Standard Time")
assertThat(unnamed.zoneName).isNull()
}
/** §5.7 #14 */
@Test
fun `no clocks is no rows`() {
assertThat(rows(emptyList())).isEmpty()
}
/** §5.7 #15 */
@Test
fun `the offset is read at the instant, so both DST seasons are right`() {
val clocks = listOf(clock(zoneId = sydney))
val inJanuary = rows(clocks, now = january).single()
val inJuly = rows(clocks, now = july).single()
assertThat(inJanuary.offset).isEqualTo(OffsetLabel.Ahead(10, 0))
assertThat(inJuly.offset).isEqualTo(OffsetLabel.Ahead(8, 0))
}
// --- home ---
/** §5.7 #16 */
@Test
fun `the hero face is home, to the second`() {
val home = WorldClockRows.home(BERLIN, T0, entries, emptyMap())
assertThat(home.zoneId).isEqualTo(berlin)
assertThat(home.city).isEqualTo("Berlin")
assertThat(home.time).isEqualTo(LocalTime.of(23, 13, 20))
}
/** §5.7 #17 */
@Test
fun `a home zone with no entry falls back to its last segment`() {
val home = WorldClockRows.home(ZoneId.of("Asia/Ho_Chi_Minh"), T0, entries, emptyMap())
assertThat(home.city).isEqualTo("Ho Chi Minh")
assertThat(home.zoneName).isNull()
}
/** §5.7 #18 */
@Test
fun `the dial's morph carries whether it is the middle of the night there`() {
val night = WorldClockRows.home(BERLIN, T0, entries, emptyMap())
val day = WorldClockRows.home(UTC_ZONE, midday, entries, emptyMap())
assertThat(night.dayFraction).isWithin(0.001f).of(0f)
assertThat(day.dayFraction).isWithin(0.001f).of(1f)
}
}
@@ -0,0 +1,418 @@
package de.jeanlucmakiola.clockula.ui.worldclock
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.data.worldclocks.WorldClockEntity
import de.jeanlucmakiola.clockula.domain.worldclock.WorldClocks
import de.jeanlucmakiola.clockula.testing.FakeWorldClockDao
import de.jeanlucmakiola.clockula.testing.MainDispatcherExtension
import de.jeanlucmakiola.clockula.testing.WorldClockHarness
import de.jeanlucmakiola.clockula.testing.worldClockHarness
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Files
import java.nio.file.Path
import java.time.ZoneId
import kotlin.time.Duration.Companion.seconds
/**
* §5.9 — 22 cases over the **real** [WorldClockViewModel], the real source, the
* real directory, the real repository across [FakeWorldClockDao] (so the real
* `addIfAbsent` and `reorder` transactions run) and a real DataStore under a
* `@TempDir`. There is no engine to go through: every verb is a repository or
* preference write (D1).
*/
class WorldClockViewModelTest {
@JvmField
@RegisterExtension
val main = MainDispatcherExtension()
private val europeanZoneIds: List<String> =
ZoneId.getAvailableZoneIds().filter { it.startsWith("Europe/") }.sorted()
private fun storedClocks(count: Int): List<WorldClockEntity> =
europeanZoneIds.take(count).mapIndexed { index, zoneId ->
WorldClockEntity(id = index + 1L, zoneId = zoneId, label = null, sortOrder = index)
}
/** Two harnesses in one test need two DataStore files: one file, one owner. */
private fun storeDir(tempDir: Path, name: String): Path =
Files.createDirectories(tempDir.resolve(name))
private fun TestScope.collecting(harness: WorldClockHarness): WorldClockHarness = harness.also {
backgroundScope.launch { it.viewModel.state.collect {} }
}
private fun WorldClockHarness.loaded(): WorldClockUiState.Loaded =
viewModel.state.value as WorldClockUiState.Loaded
/** §5.9 #1 */
@Test
fun `the tab starts out loading, then lands empty and ready to add`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
Dispatchers.setMain(StandardTestDispatcher(testScheduler))
val harness = collecting(worldClockHarness(tempDir))
assertThat(harness.viewModel.state.value).isEqualTo(WorldClockUiState.Loading)
advanceUntilIdle()
assertThat(harness.loaded().rows).isEmpty()
assertThat(harness.loaded().canAdd).isTrue()
}
/** §5.9 #2 */
@Test
fun `with no home zone stored the face is the device's own`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
advanceUntilIdle()
assertThat(harness.loaded().home.zoneId).isEqualTo(harness.zones.current().id)
}
/** §5.9 #3 */
@Test
fun `picking a city in ADD mode stores it and shows it`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
harness.viewModel.onPickZone("Europe/Berlin")
advanceUntilIdle()
assertThat(harness.stored().map { it.zoneId }).containsExactly("Europe/Berlin")
assertThat(harness.loaded().rows.map { it.zoneId }).containsExactly("Europe/Berlin")
}
/** §5.9 #4 */
@Test
fun `picking a city the tab already has adds nothing`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val dao = FakeWorldClockDao(
listOf(WorldClockEntity(id = 1L, zoneId = "Europe/Berlin", label = null, sortOrder = 0)),
)
val harness = collecting(worldClockHarness(tempDir, dao = dao))
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
harness.viewModel.onPickZone("Europe/Berlin")
advanceUntilIdle()
assertThat(harness.stored()).hasSize(1)
}
/** §5.9 #5 */
@Test
fun `the cap is reached at twenty-four cities, not before`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val full = collecting(
worldClockHarness(
storeDir(tempDir, "full"),
dao = FakeWorldClockDao(storedClocks(WorldClocks.MAX)),
),
)
advanceUntilIdle()
assertThat(full.loaded().canAdd).isFalse()
val room = collecting(
worldClockHarness(
storeDir(tempDir, "room"),
dao = FakeWorldClockDao(storedClocks(WorldClocks.MAX - 1)),
),
)
advanceUntilIdle()
assertThat(room.loaded().canAdd).isTrue()
}
/** §5.9 #6 */
@Test
fun `a twenty-fifth city is refused rather than written`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(
worldClockHarness(tempDir, dao = FakeWorldClockDao(storedClocks(WorldClocks.MAX))),
)
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
harness.viewModel.onPickZone("Pacific/Kiritimati")
advanceUntilIdle()
assertThat(harness.stored()).hasSize(WorldClocks.MAX)
assertThat(harness.stored().map { it.zoneId }).doesNotContain("Pacific/Kiritimati")
}
/** §5.9 #7 */
@Test
fun `a blank or non-IANA id writes nothing and does not throw`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
harness.viewModel.onPickZone("")
harness.viewModel.onPickZone("Not/AZone")
advanceUntilIdle()
assertThat(harness.stored()).isEmpty()
}
/** §5.9 #8 */
@Test
fun `removing a row leaves the others in their order`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(
worldClockHarness(tempDir, dao = FakeWorldClockDao(storedClocks(3))),
)
advanceUntilIdle()
harness.viewModel.onRemove(2L)
advanceUntilIdle()
assertThat(harness.stored().map { it.id }).containsExactly(1L, 3L).inOrder()
}
/** §5.9 #9 */
@Test
fun `moving a row up rewrites the stored order and the emitted one`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(
worldClockHarness(tempDir, dao = FakeWorldClockDao(storedClocks(3))),
)
advanceUntilIdle()
harness.viewModel.onMoveUp(2L)
advanceUntilIdle()
assertThat(harness.stored().map { it.id }).containsExactly(2L, 1L, 3L).inOrder()
assertThat(harness.loaded().rows.map { it.id }).containsExactly(2L, 1L, 3L).inOrder()
}
/** §5.9 #10 */
@Test
fun `moving the first row up changes nothing`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val dao = FakeWorldClockDao(storedClocks(3))
val harness = collecting(worldClockHarness(tempDir, dao = dao))
advanceUntilIdle()
val before = dao.stored
harness.viewModel.onMoveUp(1L)
advanceUntilIdle()
assertThat(dao.stored).isEqualTo(before)
}
/** §5.9 #11 */
@Test
fun `moving the last row down changes nothing`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val dao = FakeWorldClockDao(storedClocks(3))
val harness = collecting(worldClockHarness(tempDir, dao = dao))
advanceUntilIdle()
val before = dao.stored
harness.viewModel.onMoveDown(3L)
advanceUntilIdle()
assertThat(dao.stored).isEqualTo(before)
}
/** §5.9 #12 */
@Test
fun `a drag writes the order it was given`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(
worldClockHarness(tempDir, dao = FakeWorldClockDao(storedClocks(3))),
)
advanceUntilIdle()
harness.viewModel.onReorder(listOf(3L, 1L, 2L))
advanceUntilIdle()
assertThat(harness.stored().map { it.id }).containsExactly(3L, 1L, 2L).inOrder()
}
/** §5.9 #13 */
@Test
fun `picking a city in HOME mode stores it as the home zone`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.viewModel.onOpenHomePicker()
advanceUntilIdle()
harness.viewModel.onPickZone("Asia/Tokyo")
advanceUntilIdle()
assertThat(harness.storedHomeZoneId()).isEqualTo("Asia/Tokyo")
assertThat(harness.loaded().home.zoneId).isEqualTo("Asia/Tokyo")
}
/** §5.9 #14 */
@Test
fun `picking the device zone clears the stored home zone`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.settings.setHomeZoneId("Asia/Tokyo")
harness.viewModel.onOpenHomePicker()
advanceUntilIdle()
harness.viewModel.onPickDeviceZone()
advanceUntilIdle()
assertThat(harness.storedHomeZoneId()).isNull()
assertThat(harness.loaded().home.zoneId).isEqualTo(harness.zones.current().id)
}
/** §5.9 #15 */
@Test
fun `a zone change needs no receiver, only the next tick`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
advanceUntilIdle()
harness.zones.zone = ZoneId.of("Asia/Tokyo")
harness.ticker.tick()
advanceUntilIdle()
assertThat(harness.loaded().home.zoneId).isEqualTo("Asia/Tokyo")
}
/** §5.9 #16 */
@Test
fun `a home zone the device no longer knows falls back to the device's own`(
@TempDir tempDir: Path,
) = runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.settings.setHomeZoneId("Mars/Olympus")
advanceUntilIdle()
assertThat(harness.loaded().home.zoneId).isEqualTo(harness.zones.current().id)
}
/** §5.9 #17 */
@Test
fun `the picker's query filters its rows and clearing it restores them`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.names.cities = mapOf(
"America/New_York" to "New York",
"Europe/Berlin" to "Berlin",
)
harness.zones.availableIds = setOf("America/New_York", "Europe/Berlin")
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
val all = harness.viewModel.picker.value?.rows.orEmpty().size
harness.viewModel.onQueryChange("york")
advanceUntilIdle()
val filtered = harness.viewModel.picker.value?.rows.orEmpty()
assertThat(filtered).hasSize(1)
harness.viewModel.onQueryChange("")
advanceUntilIdle()
assertThat(harness.viewModel.picker.value?.rows.orEmpty()).hasSize(all)
}
/** §5.9 #18 */
@Test
fun `dismissing the picker closes it and forgets the query`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
harness.viewModel.onOpenAddPicker()
harness.viewModel.onQueryChange("york")
advanceUntilIdle()
harness.viewModel.onDismissPicker()
advanceUntilIdle()
assertThat(harness.viewModel.picker.value).isNull()
harness.viewModel.onOpenAddPicker()
advanceUntilIdle()
assertThat(harness.viewModel.picker.value?.query).isEmpty()
}
/** §5.9 #19 */
@Test
fun `the cadence is the ticker's, never a delay at a call site`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(worldClockHarness(tempDir))
advanceUntilIdle()
assertThat(harness.ticker.requestedPeriods).contains(WorldClockDefaults.Tick)
}
/** §5.9 #20 */
@Test
fun `a second of wall clock moves the face and leaves the rows alone`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val harness = collecting(
worldClockHarness(tempDir, dao = FakeWorldClockDao(storedClocks(1))),
)
advanceUntilIdle()
val before = harness.loaded()
harness.wallClock.advance(1.seconds)
harness.ticker.tick()
advanceUntilIdle()
val after = harness.loaded()
assertThat(after.home.time).isEqualTo(before.home.time.plusSeconds(1))
assertThat(after.rows).isEqualTo(before.rows)
}
/** §5.9 #21 */
@Test
fun `one rotten zone id does not take the tab down`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val dao = FakeWorldClockDao(
listOf(WorldClockEntity(id = 1L, zoneId = "Mars/Olympus", label = null, sortOrder = 0)),
)
val harness = collecting(worldClockHarness(tempDir, dao = dao))
advanceUntilIdle()
assertThat(harness.loaded().rows.single().known).isFalse()
}
/**
* §5.9 #22 — the cities follow a per-app language change. The ViewModel and
* its upstream survive the Activity recreation AppCompat does for
* `core-locale`, so a cache above `ZoneDirectory` would keep every row in
* the old language while the ICU zone name under the face switched.
*/
@Test
fun `a language change renames the rows on the next tick`(@TempDir tempDir: Path) =
runTest(main.dispatcher) {
val dao = FakeWorldClockDao(
listOf(WorldClockEntity(id = 1L, zoneId = "Europe/Berlin", label = null, sortOrder = 0)),
)
val harness = collecting(worldClockHarness(tempDir, dao = dao))
harness.names.cities = mapOf("Europe/Berlin" to "Berlin")
advanceUntilIdle()
assertThat(harness.loaded().rows.single().name).isEqualTo("Berlin")
harness.names.localeTag = "uk"
harness.names.cities = mapOf("Europe/Berlin" to "Берлін")
harness.ticker.tick()
advanceUntilIdle()
assertThat(harness.loaded().rows.single().name).isEqualTo("Берлін")
}
}
@@ -0,0 +1,184 @@
package de.jeanlucmakiola.clockula.ui.worldclock
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.clockula.domain.worldclock.ZoneEntry
import de.jeanlucmakiola.clockula.testing.T0
import org.junit.jupiter.api.Test
import kotlin.time.Instant
/**
* §5.8 — 15 cases. One picker, two modes (D16): the device-zone row belongs to
* HOME and only while the query is blank, the check follows the mode, and the
* GMT label is read once at the instant the picker opened (D15).
*/
class ZonePickerRowsTest {
private val berlin = ZoneEntry("Europe/Berlin", "Berlin", "Germany")
private val newYork = ZoneEntry("America/New_York", "New York", "United States")
private val tokyo = ZoneEntry("Asia/Tokyo", "Tokyo", "Japan")
private val kolkata = ZoneEntry("Asia/Kolkata", "Kolkata", "India")
private val utc = ZoneEntry("UTC", "UTC", null)
private val entries = listOf(berlin, newYork, tokyo)
/** 2024-07-15T12:00:00Z — Berlin on summer time. */
private val july: Instant = Instant.fromEpochMilliseconds(1_721_044_800_000L)
private fun rows(
mode: ZonePickerMode = ZonePickerMode.ADD,
entries: List<ZoneEntry> = this.entries,
query: String = "",
now: Instant = T0,
selectedZoneIds: Set<String> = emptySet(),
deviceCity: String = "Berlin",
deviceZoneSelected: Boolean = false,
) = ZonePickerRows.from(
mode = mode,
entries = entries,
query = query,
now = now,
selectedZoneIds = selectedZoneIds,
deviceCity = deviceCity,
deviceZoneSelected = deviceZoneSelected,
)
private fun List<ZonePickerRow>.zones(): List<ZonePickerRow.Zone> =
filterIsInstance<ZonePickerRow.Zone>()
private fun List<ZonePickerRow>.deviceZone(): ZonePickerRow.DeviceZone? =
filterIsInstance<ZonePickerRow.DeviceZone>().singleOrNull()
/** §5.8 #1 */
@Test
fun `adding a city offers the cities and nothing else`() {
val built = rows(mode = ZonePickerMode.ADD)
assertThat(built.zones().map { it.zoneId })
.containsExactly(berlin.zoneId, newYork.zoneId, tokyo.zoneId).inOrder()
assertThat(built.deviceZone()).isNull()
}
/** §5.8 #2 */
@Test
fun `choosing a home zone offers the device's own first`() {
val built = rows(mode = ZonePickerMode.HOME)
assertThat(built.first()).isInstanceOf(ZonePickerRow.DeviceZone::class.java)
assertThat(built.zones()).hasSize(3)
}
/** §5.8 #3 */
@Test
fun `a search hides the device-zone row`() {
val built = rows(mode = ZonePickerMode.HOME, query = "tokyo")
assertThat(built.deviceZone()).isNull()
}
/** §5.8 #4 */
@Test
fun `whitespace is a blank query, so the device-zone row stays`() {
val built = rows(mode = ZonePickerMode.HOME, query = " ")
assertThat(built.deviceZone()).isNotNull()
}
/** §5.8 #5 */
@Test
fun `following the device is a selection of its own`() {
val built = rows(mode = ZonePickerMode.HOME, deviceZoneSelected = true)
assertThat(built.deviceZone()?.selected).isTrue()
assertThat(built.zones().filter { it.selected }).isEmpty()
}
/** §5.8 #6 */
@Test
fun `a zone already on the tab carries the check`() {
val built = rows(mode = ZonePickerMode.ADD, selectedZoneIds = setOf(berlin.zoneId))
assertThat(built.zones().filter { it.selected }.map { it.zoneId })
.containsExactly(berlin.zoneId)
}
/** §5.8 #7 */
@Test
fun `the stored home zone carries the check instead of the device row`() {
val built = rows(
mode = ZonePickerMode.HOME,
selectedZoneIds = setOf(tokyo.zoneId),
deviceZoneSelected = false,
)
assertThat(built.zones().filter { it.selected }.map { it.zoneId })
.containsExactly(tokyo.zoneId)
assertThat(built.deviceZone()?.selected).isFalse()
}
/** §5.8 #8 */
@Test
fun `the query filters the rows through the catalog's own search`() {
val built = rows(query = "york")
assertThat(built.zones().map { it.zoneId }).containsExactly(newYork.zoneId)
}
/** §5.8 #9 */
@Test
fun `a query matching nothing yields no rows at all`() {
val built = rows(query = "zzz")
assertThat(built).isEmpty()
}
/** §5.8 #10 */
@Test
fun `each row carries its GMT label`() {
val built = rows(entries = listOf(berlin, kolkata, utc)).zones().associateBy { it.zoneId }
assertThat(built.getValue(berlin.zoneId).gmt).isEqualTo("GMT+1")
assertThat(built.getValue(kolkata.zoneId).gmt).isEqualTo("GMT+5:30")
assertThat(built.getValue(utc.zoneId).gmt).isEqualTo("GMT")
}
/** §5.8 #11 */
@Test
fun `the GMT label is read at the instant the picker opened`() {
val built = rows(entries = listOf(berlin), now = july).zones().single()
assertThat(built.gmt).isEqualTo("GMT+2")
}
/** §5.8 #12 */
@Test
fun `an entry no ZoneId can parse is dropped rather than thrown on`() {
val built = rows(entries = listOf(berlin, ZoneEntry("Mars/Olympus", "Olympus", null)))
assertThat(built.zones().map { it.zoneId }).containsExactly(berlin.zoneId)
}
/** §5.8 #13 */
@Test
fun `the device-zone row is summarised with the device's current city`() {
val built = rows(mode = ZonePickerMode.HOME, deviceCity = "Berlin")
assertThat(built.deviceZone()?.city).isEqualTo("Berlin")
}
/** §5.8 #14 */
@Test
fun `a duplicated entry yields one row`() {
val built = rows(entries = listOf(berlin, berlin, newYork))
assertThat(built.zones().map { it.zoneId })
.containsExactly(berlin.zoneId, newYork.zoneId)
}
/** §5.8 #15 */
@Test
fun `a missing country stays missing, for the screen to decide`() {
val built = rows(entries = listOf(utc)).zones().single()
assertThat(built.country).isNull()
}
}