package com.cottondb.books.reader import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.onNodeWithTag import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import com.cottondb.shell.arr import com.cottondb.shell.obj import com.cottondb.shell.LocalMaterials import com.cottondb.shell.Materials import com.cottondb.shell.ReaderFace import com.cottondb.shell.str import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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.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() } }