Cotton code
113 lines · 6.0 KB · KotlinRaw
package com.cottondb.books.readerimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.rememberimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport com.cottondb.shell.arrimport com.cottondb.shell.objimport com.cottondb.shell.LocalMaterialsimport com.cottondb.shell.Materialsimport com.cottondb.shell.ReaderFaceimport com.cottondb.shell.strimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport 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/** * THE READER'S HAND, as the emulator met it: the hand fanned Paper, Sepia and Night only, so Type size, Line height, * Margins, Typeface, Layout and Follow could not be reached. The core answers the hand as one flat tier * (`fan.hand`: the faces, then the knobs) and no longer a `fan.knobs` of its own; the hand draws that tier. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class ReaderHandTest {    @get:Rule val compose = createComposeRule()    /** `shell.reader`'s fan as the core answers it now (reader_tests.rs: the hand's ids, `fan.knobs` null). */    private val answer: JsonObject = Json.parseToJsonElement("""        {"title": "The Thousand Lanterns", "settings": {"face": "paper", "worn": "paper"},         "fan": {           "faces": {"cards": [{"id": "face-paper", "label": "Paper"}, {"id": "face-sepia", "label": "Sepia"}, {"id": "face-night", "label": "Night"}], "raised": "face-paper"},           "hand": {"cards": [{"id": "face-paper", "label": "Paper"}, {"id": "face-sepia", "label": "Sepia"}, {"id": "face-night", "label": "Night"},                              {"id": "knob-size", "label": "Type size", "words": "100%",                               "choices": {"cards": [{"id": "choice-0", "label": "85%", "value": 0.85}, {"id": "choice-1", "label": "100%", "value": 1.0}], "raised": "choice-1"}},                              {"id": "knob-line_height", "label": "Line height", "words": "Normal", "choices": {"cards": [], "raised": null}},                              {"id": "knob-margins", "label": "Margins", "words": "Even", "choices": {"cards": [], "raised": null}},                              {"id": "knob-typeface", "label": "Typeface", "words": "Publisher", "choices": {"cards": [], "raised": null}},                              {"id": "knob-layout", "label": "Layout", "words": "Pages",                               "choices": {"cards": [{"id": "choice-0", "label": "Pages", "value": "pages"}, {"id": "choice-1", "label": "Scroll", "value": "scroll"}], "raised": "choice-0"}}],                    "raised": "face-paper"},           "knobs": null}}    """).jsonObject    @Test    fun the_hand_reaches_the_knobs() {        var tapped: JsonObject? = null        compose.setContent {            val context = LocalContext.current            CompositionLocalProvider(LocalMaterials provides remember { Materials(context) }) {                ReaderChrome(                    answer, ReaderFace.of("paper"), ReaderTier.Root, faces = true, seek = null, back = null,                    say = { null }, onGo = { _, _ -> }, onReturn = {}, onContents = {}, onHand = {}, onClose = {}, onBack = {},                    onCard = { tapped = it }, onChoice = { _, _ -> },                    backLabel = "Back", handLabel = "Faces", closeLabel = "Close",                )            }        }        compose.waitForIdle()        compose.onNodeWithContentDescription("Type size, 100%").performClick()        compose.waitForIdle()        assertEquals("knob-size", tapped?.str("id"))    }    /**     * Layout, far along the hand, as the emulator met it: its two choices (Pages, Scroll) stood out of sight with only     * the hub showing, the arc kept turned as far as the finger had taken the hand. Each tier's arc stands on its own     * raised card.     */    @Test    fun a_far_knobs_choices_stand_in_sight() {        val layout = answer.obj("fan")!!.obj("hand")!!.arr("cards").first { it.str("id") == "knob-layout" }        var tier by mutableStateOf<ReaderTier>(ReaderTier.Root)        compose.setContent {            val context = LocalContext.current            CompositionLocalProvider(LocalMaterials provides remember { Materials(context) }) {                ReaderChrome(                    answer, ReaderFace.of("paper"), tier, faces = true, seek = null, back = null,                    say = { null }, onGo = { _, _ -> }, onReturn = {}, onContents = {}, onHand = {}, onClose = {}, onBack = {},                    onCard = {}, onChoice = { _, _ -> },                    backLabel = "Back", handLabel = "Faces", closeLabel = "Close",                )            }        }        compose.waitForIdle()        // A finger turns the hand along to Layout, the eighth card.        compose.onNodeWithTag("fan-tile-0").performTouchInput {            down(center)            repeat(60) { moveBy(androidx.compose.ui.geometry.Offset(-40f, 0f)) }            up()        }        compose.mainClock.advanceTimeBy(2_000); compose.waitForIdle()        compose.onNodeWithContentDescription("Layout, Pages").assertExists()        // Layout taken: its choices are the arc now.        tier = ReaderTier.Choices(layout)        compose.mainClock.advanceTimeBy(2_000); compose.waitForIdle()        compose.onNodeWithContentDescription("Pages").assertExists()        compose.onNodeWithContentDescription("Scroll").assertExists()    }}