Cotton code
87 lines · 4.4 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.assertIsNotSelectedimport androidx.compose.ui.test.assertIsSelectedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.days.book.Itemimport com.cottondb.days.pen.DaysCardLookimport com.cottondb.days.pen.penFontsimport com.cottondb.days.ui.AlertChoicesimport com.cottondb.shell.LocalCardLookimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * "No alert" did nothing (launch walk of Days, 22 Nov 2026): an event with no alert stored drew No alert chosen though * the core reminds it ten minutes before, and choosing No alert wrote the same empty alert, so it still reminded. Now * the sheet draws the choices the core answers, marks the one that stands, and No alert is written as "none". */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w411dp-h914dp-xhdpi", application = Application::class)class DaysAlertChoicesTest {    @get:Rule val compose = createComposeRule()    private val words = mapOf("change.no_alert" to "No alert", "change.alert_at" to "At the time", "change.alert_before" to "{n} min before")    private fun say(key: String, fills: Map<String, String>) = fills.entries.fold(words.getValue(key)) { line, (k, v) -> line.replace("{$k}", v) }    /** The sheet's answer for an invented event, as `days.item` gives it with [standing] on. */    private fun item(standing: String) = Item.of("event", "e", CottonCore.json.parseToJsonElement(        """{"table":"event","id":"e","whose":"yours","says":"Yours","words":"Invented dentist","day":"2026-11-24","time":"10:00","end":null,           "mark":null,"note":null,"alert":$standing,"done":false,"repeats":false,           "alerts":[{"alert":"none","on":${standing == "\"none\""}},{"alert":0,"on":${standing == "0"}},{"alert":10,"on":${standing == "10"}},                     {"alert":30,"on":${standing == "30"}},{"alert":60,"on":${standing == "60"}}],           "canChange":{"title":true,"time":true,"mark":true,"note":true,"alert":true}}""").jsonObject)    private var written: JsonPrimitive? = null    private fun show(item: Item) = compose.setContent {        CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) {            val standing = item.alerts.firstOrNull { it.on }?.alert            var chosen by remember { mutableStateOf(standing) }            AlertChoices(item.alerts, chosen, ::say) { chosen = it; written = it }        }    }    @Test fun an_event_with_no_alert_stored_shows_its_ten_minutes_chosen_not_no_alert() {        show(item("10"))        compose.onNodeWithTag("days-change-alert-10").assertIsSelected()        compose.onNodeWithTag("days-change-alert-none").assertIsNotSelected()        compose.onNodeWithText("10 min before").assertExists()        compose.onNodeWithText("At the time").assertExists()    }    @Test fun choosing_no_alert_chooses_none_and_it_shows_chosen() {        show(item("10"))        compose.onNodeWithTag("days-change-alert-none").performClick()        compose.waitForIdle()        assertEquals("No alert is written as none, never as the empty default", JsonPrimitive("none"), written)        compose.onNodeWithTag("days-change-alert-none").assertIsSelected()        compose.onNodeWithTag("days-change-alert-10").assertIsNotSelected()    }    @Test fun an_event_set_to_no_alert_shows_no_alert_chosen() {        show(item("\"none\""))        compose.onNodeWithTag("days-change-alert-none").assertIsSelected()        compose.onNodeWithTag("days-change-alert-10").assertIsNotSelected()        compose.onNodeWithTag("days-change-alert-30").performClick()        compose.waitForIdle()        assertEquals(JsonPrimitive(30), written)    }}