package com.cottondb.story import android.app.Application import android.view.ViewGroup import androidx.activity.ComponentActivity import androidx.compose.material3.Text import androidx.compose.ui.test.getBoundsInRoot import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.unit.dp import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.size import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.onAllNodesWithTag import com.cottondb.story.ink.InkField import com.cottondb.story.ink.InkPageFrame import com.cottondb.story.ink.InkSheet import com.cottondb.story.ink.SceneFrame import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() private fun keyboard(height: Int) { compose.runOnUiThread { val view = compose.activity.findViewById(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() } }