feat(ring): the ring screen, over the lock screen
Shows over the lock screen, turns it on, keeps it on, and swallows back for the whole lifetime of the window — including while the session is still loading, which is where a back press used to finish the activity and leave the alarm sounding with nothing on screen to stop it. The optional dismiss challenge cannot strand the user: the standing wrong answer clears the moment they start typing the next one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wmy1BpCKi8KeSjaWhYuCPV
This commit is contained in:
1 parent
83c5e9fe3f
commit
959eb599ff
8 files changed
+1183
-100
No files matched your search
@@ -0,0 +1,464 @@
|
||||
package de.jeanlucmakiola.clockula.ui.ring
|
||||
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.alarm.AlarmEngine
|
||||
import de.jeanlucmakiola.clockula.data.alarms.AlarmMapper
|
||||
import de.jeanlucmakiola.clockula.data.alarms.AlarmRepositoryImpl
|
||||
import de.jeanlucmakiola.clockula.data.alarms.AlarmStateRepositoryImpl
|
||||
import de.jeanlucmakiola.clockula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.clockula.domain.DismissChallenge
|
||||
import de.jeanlucmakiola.clockula.domain.TimeOfDay
|
||||
import de.jeanlucmakiola.clockula.domain.alarm.ChallengeGate
|
||||
import de.jeanlucmakiola.clockula.testing.BERLIN
|
||||
import de.jeanlucmakiola.clockula.testing.FakeAlarmDao
|
||||
import de.jeanlucmakiola.clockula.testing.FakeAlarmNotifier
|
||||
import de.jeanlucmakiola.clockula.testing.FakeAlarmScheduler
|
||||
import de.jeanlucmakiola.clockula.testing.FakeAlarmStateDao
|
||||
import de.jeanlucmakiola.clockula.testing.FakeRingCoordinator
|
||||
import de.jeanlucmakiola.clockula.testing.FakeTicker
|
||||
import de.jeanlucmakiola.clockula.testing.FakeWallClock
|
||||
import de.jeanlucmakiola.clockula.testing.FakeZoneProvider
|
||||
import de.jeanlucmakiola.clockula.testing.MainDispatcherExtension
|
||||
import de.jeanlucmakiola.clockula.testing.RecordingRebootRepair
|
||||
import de.jeanlucmakiola.clockula.testing.alarmAt
|
||||
import de.jeanlucmakiola.clockula.testing.instantAt
|
||||
import de.jeanlucmakiola.clockula.testing.offsetInstant
|
||||
import de.jeanlucmakiola.floret.prefs.PrefStore
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
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.Path
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
/**
|
||||
* The ring screen's behaviour over the **real** alarm engine: what a ringing
|
||||
* alarm looks like, what the challenge gate lets through, and the two ways out
|
||||
* that mean a half-awake person is never stranded with a screaming phone.
|
||||
*/
|
||||
class AlarmRingViewModelTest {
|
||||
|
||||
@JvmField
|
||||
@RegisterExtension
|
||||
val main = MainDispatcherExtension()
|
||||
|
||||
private val alarmDao = FakeAlarmDao()
|
||||
private val stateDao = FakeAlarmStateDao(alarms = alarmDao)
|
||||
private val wallClock = FakeWallClock(instantAt(BERLIN, "2026-06-10T05:00"))
|
||||
private val zones = FakeZoneProvider(BERLIN)
|
||||
private val scheduler = FakeAlarmScheduler()
|
||||
private val ring = FakeRingCoordinator()
|
||||
private val notifier = FakeAlarmNotifier()
|
||||
private val rebootRepair = RecordingRebootRepair()
|
||||
private val ticker = FakeTicker()
|
||||
|
||||
private val alarms = AlarmRepositoryImpl(alarmDao, wallClock)
|
||||
private val states = AlarmStateRepositoryImpl(stateDao)
|
||||
|
||||
private val sevenToday = offsetInstant("2026-06-10T07:00+02:00")
|
||||
|
||||
private fun TestScope.engine(tempDir: Path): AlarmEngine = AlarmEngine(
|
||||
alarms = alarms,
|
||||
states = states,
|
||||
settings = SettingsPrefs(
|
||||
PrefStore(
|
||||
PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
|
||||
produceFile = { tempDir.resolve("clockula_prefs_test.preferences_pb").toFile() },
|
||||
),
|
||||
),
|
||||
),
|
||||
scheduler = scheduler,
|
||||
ring = ring,
|
||||
notifier = notifier,
|
||||
rebootRepair = rebootRepair,
|
||||
wallClock = wallClock,
|
||||
zones = zones,
|
||||
)
|
||||
|
||||
private suspend fun givenRinging(
|
||||
engine: AlarmEngine,
|
||||
label: String = "Work",
|
||||
challenge: DismissChallenge? = null,
|
||||
snoozeLimit: Int? = null,
|
||||
) {
|
||||
val alarm = alarmAt(id = 1L, hour = 7, minute = 0, label = label, snoozeLimit = snoozeLimit)
|
||||
.copy(dismissChallenge = challenge)
|
||||
alarmDao.insert(AlarmMapper.toEntity(alarm))
|
||||
wallClock.instant = sevenToday
|
||||
engine.onFire(1L, sevenToday)
|
||||
}
|
||||
|
||||
private fun AlarmRingViewModel.ringing(): RingUiState.Ringing = state.value as RingUiState.Ringing
|
||||
|
||||
private fun AlarmRingViewModel.math(): ChallengeUiState.Math =
|
||||
ringing().challenge as ChallengeUiState.Math
|
||||
|
||||
private fun viewModel(engine: AlarmEngine) = AlarmRingViewModel(engine, ring, wallClock, ticker)
|
||||
|
||||
@Test
|
||||
fun `nothing ringing settles on finished`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val viewModel = viewModel(engine(tempDir))
|
||||
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.state.value).isEqualTo(RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a ringing alarm shows its time and its label`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, label = "Work")
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.ringing().time to viewModel.ringing().label)
|
||||
.isEqualTo(TimeOfDay(7, 0) to "Work")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a blank label is left blank for the screen to fall back on`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, label = "")
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.ringing().label).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an alarm at its snooze limit cannot be snoozed`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, snoozeLimit = 0)
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.ringing().canSnooze).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `no challenge means dismiss is one tap`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.NONE)
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.ringing().challenge).isEqualTo(ChallengeUiState.Direct)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a maths challenge arrives with a problem and an empty answer`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
val math = viewModel.math()
|
||||
assertThat(listOf(math.prompt.isNotBlank(), math.typed.isEmpty(), math.wrongAttempts == 0))
|
||||
.containsExactly(true, true, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a hold challenge arrives at zero progress`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.HOLD)
|
||||
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.ringing().challenge)
|
||||
.isEqualTo(ChallengeUiState.Hold(progress = 0f, escapeAvailable = false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `snooze is never gated by the challenge`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onSnooze()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).snoozedUntil).isNotNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `snoozing finishes the screen`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onSnooze()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.state.value).isEqualTo(RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an ungated dismiss stops the ring and finishes the screen`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.NONE)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onDismissRequested()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince to viewModel.state.value)
|
||||
.isEqualTo(null to RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `dismiss does nothing while the maths gate is closed`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onDismissRequested()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince).isNotNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `typing an answer changes nothing but the answer`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
val before = viewModel.math()
|
||||
|
||||
viewModel.onAnswerChanged("8")
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.math()).isEqualTo(before.copy(typed = "8"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the right answer is itself the dismissal`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
viewModel.onAnswerChanged(answerTo(viewModel.math().prompt))
|
||||
|
||||
viewModel.onSubmitAnswer()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince to viewModel.state.value)
|
||||
.isEqualTo(null to RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a wrong answer replaces the problem and keeps the alarm ringing`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
val before = viewModel.math()
|
||||
viewModel.onAnswerChanged("-1")
|
||||
|
||||
viewModel.onSubmitAnswer()
|
||||
advanceUntilIdle()
|
||||
|
||||
val after = viewModel.math()
|
||||
assertThat(
|
||||
listOf(
|
||||
after.typed,
|
||||
after.wrongAttempts,
|
||||
after.showedWrong,
|
||||
after.prompt != before.prompt,
|
||||
states.state(1L).ringingSince != null,
|
||||
),
|
||||
).containsExactly("", 1, true, true, true).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `typing an answer to the new problem clears the standing wrong answer`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
viewModel.onAnswerChanged("-1")
|
||||
viewModel.onSubmitAnswer()
|
||||
advanceUntilIdle()
|
||||
assertThat(viewModel.math().showedWrong).isTrue()
|
||||
|
||||
viewModel.onAnswerChanged("4")
|
||||
advanceUntilIdle()
|
||||
|
||||
// "Not quite" belongs to the problem that was answered, not to the
|
||||
// one being typed: left standing it reads as a fresh rejection.
|
||||
val after = viewModel.math()
|
||||
assertThat(after.showedWrong to after.typed).isEqualTo(false to "4")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a completed hold is itself the dismissal`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.HOLD)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onHoldProgress(1f)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince to viewModel.state.value)
|
||||
.isEqualTo(null to RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unfinished hold keeps the alarm ringing`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.HOLD)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onHoldProgress(0.99f)
|
||||
advanceUntilIdle()
|
||||
|
||||
val hold = viewModel.ringing().challenge as ChallengeUiState.Hold
|
||||
assertThat(hold.progress to (states.state(1L).ringingSince != null))
|
||||
.isEqualTo(0.99f to true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `escaping before the escape is offered does nothing`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onEscape()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince).isNotNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `three wrong answers earn a way out`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
repeat(ChallengeGate.MAX_WRONG_ATTEMPTS) {
|
||||
viewModel.onAnswerChanged("-1")
|
||||
viewModel.onSubmitAnswer()
|
||||
advanceUntilIdle()
|
||||
}
|
||||
|
||||
viewModel.onEscape()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince to viewModel.state.value)
|
||||
.isEqualTo(null to RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a minute of ringing earns a way out`(@TempDir tempDir: Path) = runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, challenge = DismissChallenge.MATH)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
wallClock.instant = sevenToday + ChallengeGate.ESCAPE_AFTER + 1.seconds
|
||||
ticker.tick()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.math().escapeAvailable).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a dismissal from elsewhere finishes the screen`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
engine.dismiss(1L)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.state.value).isEqualTo(RingUiState.Finished)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a coordinator that has forgotten the ring does not end it`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
ring.stopRinging(1L)
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(viewModel.state.value).isInstanceOf(RingUiState.Ringing::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a refused snooze dismisses rather than stranding the alarm`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine, snoozeLimit = 0)
|
||||
val viewModel = viewModel(engine)
|
||||
advanceUntilIdle()
|
||||
|
||||
viewModel.onSnooze()
|
||||
advanceUntilIdle()
|
||||
|
||||
val state = states.state(1L)
|
||||
assertThat(listOf(state.ringingSince, state.snoozedUntil, viewModel.state.value))
|
||||
.containsExactly(null, null, RingUiState.Finished)
|
||||
.inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every action is ignored until the session has loaded`(@TempDir tempDir: Path) =
|
||||
runTest(main.dispatcher) {
|
||||
Dispatchers.setMain(StandardTestDispatcher(testScheduler))
|
||||
val engine = engine(tempDir)
|
||||
givenRinging(engine)
|
||||
advanceUntilIdle()
|
||||
val viewModel = viewModel(engine)
|
||||
check(viewModel.state.value == RingUiState.Loading)
|
||||
|
||||
viewModel.onDismissRequested()
|
||||
viewModel.onSubmitAnswer()
|
||||
viewModel.onHoldProgress(1f)
|
||||
viewModel.onEscape()
|
||||
viewModel.onSnooze()
|
||||
advanceUntilIdle()
|
||||
|
||||
assertThat(states.state(1L).ringingSince).isNotNull()
|
||||
}
|
||||
|
||||
/** The prompt is `"47 + 38"`; the answer is what it says. */
|
||||
private fun answerTo(prompt: String): String =
|
||||
prompt.split("+").sumOf { it.trim().toInt() }.toString()
|
||||
}
|
||||
Reference in new issue
Block a user