Cotton code
50 lines · 2.6 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.semantics.SemanticsActionsimport androidx.compose.ui.test.getBoundsInRootimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onRootimport androidx.compose.ui.text.TextLayoutResultimport androidx.compose.ui.unit.Densityimport com.cottondb.story.ink.WritingAfterimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * RULE (DECISIONS "Write after this"): at the largest text the bar says where it writes without breaking a word, and the * way back to the end stays whole on the screen. On the emulator at font scale 2 it stood "remembe" / "r the" (22 Nov 2026). */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w332dp-h700dp-mdpi", application = Application::class)class StoryWritingAfterTest {    @get:Rule val compose = createComposeRule()    @Test fun where_it_writes_breaks_only_between_words() {        val said = "Writing after: Do you remember the cinema before it clo…"        compose.setContent {            val d = LocalDensity.current            CompositionLocalProvider(LocalDensity provides Density(d.density, fontScale = 2f)) {                WritingAfter(said, "Write at the end") {}            }        }        val results = mutableListOf<TextLayoutResult>()        compose.onNodeWithTag("story-writing-after", useUnmergedTree = true).fetchSemanticsNode().config[SemanticsActions.GetTextLayoutResult].action?.invoke(results)        val layout = results.single()        val ends = (0 until layout.lineCount - 1).map { layout.getLineEnd(it) }        assertTrue("broke inside a word at $ends", ends.all { said[it - 1] == ' ' })        val end = compose.onNodeWithTag("story-writing-end", useUnmergedTree = true).getBoundsInRoot()        val root = compose.onRoot().getBoundsInRoot()        assertTrue("the way back off the screen: $end", end.right <= root.right && end.right > end.left)        // Not squeezed: the words have most of the row, or the way back stands on its own line beneath them.        val words = compose.onNodeWithTag("story-writing-after", useUnmergedTree = true).getBoundsInRoot()        assertTrue("squeezed to $words beside $end", end.top >= words.bottom || (words.right - words.left) >= (root.right - root.left) * 0.6f)    }}