Cotton code
91 lines · 4.4 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport androidx.compose.ui.semantics.SemanticsActionsimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.text.TextLayoutResultimport androidx.compose.ui.unit.dpimport androidx.compose.foundation.layout.paddingimport com.cottondb.story.ink.Markimport com.cottondb.story.ink.MarkButtonimport com.cottondb.story.ink.TITLE_SIZEimport com.cottondb.story.ink.TITLE_LEASTimport com.cottondb.story.ink.TitleRowimport com.cottondb.story.ink.fittedTitleSizeimport com.cottondb.story.ink.fitTitleimport com.cottondb.story.ink.titleLeastimport com.cottondb.story.ink.longestTitleWordimport 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/** * RULE: a script's title breaks only between its words. Once the first scene put the Read mark beside it, "The * Lighthouse Keeper" stood as "The / Lighthous / e Keeper" on the emulator (first-run walk, 25 Nov 2026). */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w411dp-h891dp-xxhdpi", application = Application::class)class StoryTitleBreakTest {    @get:Rule val compose = createComposeRule()    @Test fun the_title_beside_read_and_more_breaks_only_between_words() = breaksOnlyBetweenWords()    /** At the phone's largest text it stood as "The / Lighthous / e / Keeper" (the store pass, emulator-5602, 26 Nov 2026). */    @Test fun at_the_largest_text_too() {        org.robolectric.RuntimeEnvironment.setFontScale(2f)        breaksOnlyBetweenWords()    }    private fun breaksOnlyBetweenWords() {        val title = "The Lighthouse Keeper"        compose.setContent {            androidx.compose.foundation.layout.Box(androidx.compose.ui.Modifier.padding(horizontal = 20.dp)) {                TitleRow(title, title, onTitle = {}) {                    MarkButton(Mark.Page, "Read", "story-home-read", size = 20.dp) {}                    MarkButton(Mark.More, "More", "story-home-more", size = 20.dp) {}                }            }        }        val results = mutableListOf<TextLayoutResult>()        compose.onNodeWithTag("story-title-words", useUnmergedTree = true).fetchSemanticsNode().config[SemanticsActions.GetTextLayoutResult].action?.invoke(results)        val layout = results.single()        val text = layout.layoutInput.text.text        val ends = (0 until layout.lineCount - 1).map { layout.getLineEnd(it) }        assertTrue("broke inside a word at $ends in \"$text\"", ends.all { text[it - 1].isWhitespace() })        assertTrue("the › left its word", ends.none { text.getOrNull(it) == '›' })    }    @Test fun a_title_that_fits_keeps_its_size_and_one_too_wide_is_drawn_smaller_but_not_below_the_least() {        assertEquals(TITLE_SIZE, fittedTitleSize(TITLE_SIZE, longest = 300f, width = 320f))        val smaller = fittedTitleSize(TITLE_SIZE, longest = 400f, width = 320f)        assertTrue("$smaller", smaller < TITLE_SIZE && smaller * 400f / TITLE_SIZE <= 320f)        assertEquals(TITLE_SIZE * 0.55f, fittedTitleSize(TITLE_SIZE, longest = 4000f, width = 320f))    }    @Test fun the_last_word_is_measured_with_its_mark() {        val asked = mutableListOf<Pair<String, Boolean>>()        longestTitleWord("The  Lighthouse Keeper", mark = true) { word, mark -> asked += word to mark; word.length.toFloat() }        assertEquals(listOf("The" to false, "Lighthouse" to false, "Keeper" to true), asked)    }    @Test fun a_size_that_grows_less_than_asked_is_stepped_down_until_the_word_fits() {        // Drawn width grows with the square root of the size asked, as large type does on Android 14: one guess is too wide.        val drawn = { at: Float -> 10f * kotlin.math.sqrt(at) * 10f }        val width = 550f        val one = fittedTitleSize(TITLE_SIZE, drawn(TITLE_SIZE), width)        assertTrue("the one-step guess fitted, so this proves nothing", drawn(one) > width)        val fitted = fitTitle(TITLE_SIZE, width, TITLE_LEAST, drawn)        assertTrue("$fitted draws ${drawn(fitted)} > $width", drawn(fitted) <= width)    }    @Test fun the_least_goes_lower_only_with_larger_text() {        assertEquals(TITLE_LEAST, titleLeast(1f))        assertEquals(TITLE_LEAST, titleLeast(0.85f))        assertEquals(TITLE_LEAST / 2f, titleLeast(2f))    }}