package com.cottondb.story import android.app.Application import androidx.compose.ui.semantics.SemanticsActions import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.text.TextLayoutResult import androidx.compose.ui.unit.dp import androidx.compose.foundation.layout.padding import com.cottondb.story.ink.Mark import com.cottondb.story.ink.MarkButton import com.cottondb.story.ink.TITLE_SIZE import com.cottondb.story.ink.TITLE_LEAST import com.cottondb.story.ink.TitleRow import com.cottondb.story.ink.fittedTitleSize import com.cottondb.story.ink.fitTitle import com.cottondb.story.ink.titleLeast import com.cottondb.story.ink.longestTitleWord 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 /** * 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() 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>() 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)) } }