Cotton code
99 lines · 5.5 KB · KotlinRaw
package com.cottondb.reportimport androidx.compose.foundation.text.input.rememberTextFieldStateimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.semantics.SemanticsNodeimport androidx.compose.ui.semantics.SemanticsPropertiesimport androidx.compose.ui.semantics.getOrNullimport androidx.compose.ui.test.junit4.ComposeContentTestRuleimport androidx.compose.ui.test.onAllNodesWithTagimport androidx.compose.ui.test.assertCountEqualsimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport com.cottondb.shell.CardLookimport com.cottondb.shell.LocalCardLookimport com.cottondb.shell.ReportAskimport com.cottondb.shell.ReportWordsimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrue/** * "What looks wrong?" in a redesigned app's look (OLD-LOOK item 2, 22 Nov 2026): the sheet says the question, the * field's hint, the one line and Send, and nothing else (the way out's name aside); no kicker, no picture of the * screen, no project, no category, no Cancel; and it stands in the app's own sheet ([frame]: a tag only that app's * sheet frame draws). Send goes once there are words. */object ReportSheetCheck {    val words = ReportWords(        question = "What looks wrong?", hint = "One sentence is enough.",        goesWith = "The screen you were on and the app's recent log go with it.", send = "Send", sending = "Sending", close = "Close",    )    fun check(compose: ComposeContentTestRule, look: @androidx.compose.runtime.Composable () -> CardLook, frame: String) {        var sent = 0        compose.setContent {            CompositionLocalProvider(LocalCardLook provides look()) {                ReportAsk(words, rememberTextFieldState(), sending = false, onClose = {}) { sent++ }            }        }        val said = said(compose.onRoot(useUnmergedTree = true).fetchSemanticsNode())        val asked = setOf(words.question, words.hint, words.goesWith, words.send)        assertTrue("the sheet says ${asked - said} missing from $said", said.containsAll(asked))        assertEquals("the sheet says only the question, the hint, the line and Send", emptySet<String>(), said - asked - words.close)        listOf("Report a problem", "Cancel", "Which project is this about? A report can stay unfiled.", "The screen you were on").forEach {            assertTrue("$it is on the sheet", it !in said)        }        compose.onAllNodesWithTag("report-snapshot").assertCountEquals(0)        compose.onAllNodesWithTag("report-cancel").assertCountEquals(0)        compose.onNodeWithTag(frame, useUnmergedTree = true).assertExists()        compose.onNodeWithTag("report-send").performClick()        compose.runOnIdle { assertEquals("Send waits for words", 0, sent) }        compose.onNodeWithTag("report-note").performTextInput("The list jumps")        compose.onNodeWithTag("report-send").performClick()        compose.runOnIdle { assertEquals(1, sent) }    }    /**     * Send stands wholly in sight when the sheet has less room than it needs ([check]'s window made short, the words     * doubled: the room left above a keyboard at the largest text), as on emulator-5558, where Story's sheet opened     * with Send under the keyboard. Its bounds in the window, clipped by the sheet's scroll, are its whole height.     */    fun sendInSight(compose: ComposeContentTestRule, look: @androidx.compose.runtime.Composable () -> CardLook) {        compose.setContent {            val density = androidx.compose.ui.platform.LocalDensity.current            CompositionLocalProvider(                androidx.compose.ui.platform.LocalDensity provides androidx.compose.ui.unit.Density(density.density, 2f),                LocalCardLook provides look(),            ) {                ReportAsk(words, rememberTextFieldState(), sending = false, onClose = {}) {}            }        }        // The sheet waits for the keyboard to settle before it scrolls Send into sight.        compose.mainClock.advanceTimeBy(1_000)        compose.waitForIdle()        val root = compose.onRoot().fetchSemanticsNode().boundsInWindow        val send = compose.onNodeWithTag("report-send").fetchSemanticsNode()        val needed = listOf(            compose.onNodeWithText(words.question).fetchSemanticsNode(),            compose.onNodeWithTag("report-note").fetchSemanticsNode(),            compose.onNodeWithTag("report-goes-with").fetchSemanticsNode(),            send,        ).sumOf { it.size.height }        assertTrue("the sheet's parts ($needed px) need more room than the window (${root.height}), or nothing is tested", needed > root.height)        val seen = send.boundsInWindow        assertEquals("Send is wholly in sight (seen $seen of ${send.size} in $root)", send.size.height.toFloat(), seen.height, 1f)        assertTrue("Send stands inside the window ($seen in $root)", seen.bottom <= root.bottom + 0.5f)    }    /** Every word the tree says: texts and names, the field's typed words left out. */    fun said(node: SemanticsNode): Set<String> {        val here = buildSet {            if (node.config.getOrNull(SemanticsProperties.EditableText) == null) node.config.getOrNull(SemanticsProperties.Text)?.forEach { add(it.text) }            node.config.getOrNull(SemanticsProperties.ContentDescription)?.forEach { add(it) }        }        return here + node.children.flatMap { said(it) }    }}