Cotton code
78 lines · 3.6 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.semantics.SemanticsActionsimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.text.TextLayoutResultimport androidx.compose.ui.text.font.FontFamilyimport androidx.test.core.app.ApplicationProviderimport com.cottondb.days.pen.PenThemeimport com.cottondb.days.pen.Wordsimport com.cottondb.shell.look.LookFacesimport com.cottondb.shell.look.LookStateimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonArrayimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonArrayimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNotEqualsimport org.junit.Assert.assertNotNullimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * A face chosen in Look reaches the words Days draws (owner, 23 Nov 2026: on his iPhone a font changed nothing while * size, papers and inks did). Each of Look's faces, as the kit names them, stands in turn; the day's words are then * laid out in that face's family, built from the files this build carries, and never in DM Mono. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = Application::class)class DaysLookFaceTest {    @get:Rule val compose = createComposeRule()    private val kit: JsonObject by lazy {        var dir = java.io.File("").absoluteFile        while (!java.io.File(dir, "crates/cotton-core/kits/look/look.json").isFile) dir = dir.parentFile        Json.parseToJsonElement(java.io.File(dir, "crates/cotton-core/kits/look/look.json").readText()).jsonObject    }    private fun answer(face: String): JsonObject {        val faces = kit["faces"]!!.jsonArray.filter { it.jsonObject["id"]!!.jsonPrimitive.content in LOOK_FACES }        return Json.parseToJsonElement("""{"app":"days","light":"grey","dark":"grey","ink":"blue","face":"$face","size":1.0,"sizes_are":"scale","colours":{"light":{},"dark":{}}}""")            .jsonObject.let { JsonObject(it + ("faces" to JsonArray(faces))) }    }    @Test fun every_chosen_face_reaches_the_drawn_words() {        val assets = ApplicationProvider.getApplicationContext<Application>().assets        LookState.standForTest(answer("own"))        try {            compose.setContent { PenTheme { Words("Tuesday 24", Modifier.testTag("words")) } }            val own = drawnFamily()            LOOK_FACES.forEach { face ->                val given = answer(face)                val family = LookFaces.chosen(assets, given)                assertNotNull("this build carries no file of $face", family)                compose.runOnIdle { LookState.standForTest(given) }                assertEquals("Days' words in $face", family, drawnFamily())                assertNotEquals("$face is drawn as Days' own face", own, drawnFamily())            }        } finally { LookState.standForTest(null) }    }    private fun drawnFamily(): FontFamily? {        compose.waitForIdle()        val laid = mutableListOf<TextLayoutResult>()        compose.onNodeWithTag("words", useUnmergedTree = true).fetchSemanticsNode().config[SemanticsActions.GetTextLayoutResult].action?.invoke(laid)        return laid.single().layoutInput.style.fontFamily    }    private companion object { val LOOK_FACES = listOf("serif", "typewriter", "hyperlegible", "dyslexic") }}