Cotton code
136 lines · 6.4 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport android.view.ViewGroupimport androidx.activity.ComponentActivityimport androidx.compose.material3.Textimport androidx.compose.ui.test.getBoundsInRootimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.unit.dpimport androidx.core.graphics.Insetsimport androidx.core.view.ViewCompatimport androidx.core.view.WindowCompatimport androidx.core.view.WindowInsetsCompatimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.sizeimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.onAllNodesWithTagimport com.cottondb.story.ink.InkFieldimport com.cottondb.story.ink.InkPageFrameimport com.cottondb.story.ink.InkSheetimport com.cottondb.story.ink.SceneFrameimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * A page's rows stand above the keyboard, so the field being typed in can be scrolled into sight: About's "In one line" * sat under the keyboard in landscape on the emulator (21 Nov 2026). The keyboard here is 300 of a 700 dp screen. A * scene's writing bar is always whole above the keyboard: in landscape its speaker chip, switches, mic and Send were * under it, the heading taking the room. A field typed in at a sheet's foot comes up whole, its underline with it (Where * and when's one line, at the largest text, stood with its foot under the keyboard). */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class)class StoryKeyboardTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private fun keyboard(height: Int) {        compose.runOnUiThread {            val view = compose.activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0)            val insets = WindowInsetsCompat.Builder()                .setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, height))                .setVisible(WindowInsetsCompat.Type.ime(), true)                .build()            ViewCompat.dispatchApplyWindowInsets(view, insets)        }        compose.waitForIdle()    }    @Test fun a_page_stands_above_the_keyboard() {        compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) }        compose.setContent {            InkPageFrame("Back", "More", "About this script", "page", onBack = {}) { (1..12).forEach { Text("Fact $it") } }        }        keyboard(300)        val bottom = compose.onNodeWithTag("page").getBoundsInRoot().bottom        assertTrue("the page reaches under the keyboard: its foot at $bottom", bottom <= 400.dp)    }    /** A scene as SceneFrame lays it in [height] dp: a 64 dp heading, the lines, a 180 dp writing bar. */    private fun scene(height: Int) {        compose.setContent {            Box(Modifier.size(400.dp, height.dp)) {                SceneFrame(                    Modifier.fillMaxWidth().height(height.dp),                    heading = { Box(Modifier.fillMaxWidth().height(64.dp).testTag("heading")) },                    lines = { Box(Modifier.fillMaxWidth().testTag("lines")) },                    bar = { Box(Modifier.fillMaxWidth().height(180.dp).testTag("bar")) },                )            }        }    }    @Test fun the_writing_bar_is_whole_when_the_room_is_short() {        scene(height = 250)        val bar = compose.onNodeWithTag("bar").getBoundsInRoot()        assertEquals(250.dp, bar.bottom)        assertEquals(70.dp, bar.top)        assertTrue("the heading took the bar's room", compose.onAllNodesWithTag("heading").fetchSemanticsNodes(false).none { it.layoutInfo.isPlaced })    }    @Test fun with_room_the_heading_stands_over_the_lines() {        scene(height = 700)        compose.onNodeWithTag("heading").assertIsDisplayed()        val lines = compose.onNodeWithTag("lines").getBoundsInRoot()        assertEquals(64.dp, lines.top)        assertEquals(520.dp, lines.bottom)        assertEquals(700.dp, compose.onNodeWithTag("bar").getBoundsInRoot().bottom)    }    @Test fun a_field_at_a_sheets_foot_comes_up_whole_above_the_keyboard() {        compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) }        val focus = androidx.compose.ui.focus.FocusRequester()        compose.setContent {            InkSheet("Where and when", "Close", "sheet", onClose = {}, typing = true) {                (1..30).forEach { Text("Train Platform $it") }                InkField("", {}, "What happens here?", "summary", Modifier.testTag("whole"), focus = focus)            }        }        compose.runOnUiThread { focus.requestFocus() }        compose.waitForIdle()        keyboard(300)        val whole = compose.onNodeWithTag("whole").getBoundsInRoot()        assertTrue("the field's foot under the keyboard: $whole", whole.bottom <= 400.dp)        assertTrue("the field above the sheet's top: $whole", whole.top >= 0.dp)    }    /**     * A sheet that fits over the keyboard stands there whole, its Done in sight with no scroll: the keyboard lifts the     * sheet, and the sheet's height cap is its own (walk, 24 Nov 2026: Where and when's Done was cut by the keyboard,     * the keyboard's room counted inside the cap). Here 900 dp tall, the keyboard 300, the sheet's rows about 520.     */    @Config(qualifiers = "w400dp-h900dp-mdpi")    @Test fun a_sheet_that_fits_over_the_keyboard_shows_its_done_whole() {        compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) }        compose.setContent {            InkSheet("Where and when", "Close", "sheet", onClose = {}, typing = true) {                Box(Modifier.fillMaxWidth().height(380.dp))                Box(Modifier.fillMaxWidth().height(48.dp).testTag("done"))            }        }        keyboard(300)        val done = compose.onNodeWithTag("done").getBoundsInRoot()        assertTrue("Done under the keyboard: $done", done.bottom <= 600.dp)        compose.onNodeWithTag("done").assertIsDisplayed()    }}