package eu.friedcutlet.yokan.ui.screens.novel import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.ImageComposeScene import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.use import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.graphics.Color import eu.friedcutlet.yokan.database.DbNovelPageMode import eu.friedcutlet.yokan.database.DbNovelTextAlign import java.util.concurrent.FutureTask import javax.swing.SwingUtilities import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertTrue class NovelPagedReaderMeasureTest { @Test fun realComposeMeasurementPaginatesAndPreservesEverySourceCharacter() { listOf("A\nB", "A\n", "A\n\nB", "A\r\n\r\nB", "\nA", "\n\nA", "\n", "A\r\n", "one two three four five six seven eight nine ten").forEach { source -> val isBreakCase = source.contains('\n') || source.contains('\r') val width = if (isBreakCase) 320 else 390 val height = if (isBreakCase) 80 else 560 onUi { var invalid = false var readyPages: List? = null val scene = ImageComposeScene(width, height) { NovelPagedReader( content = NovelRichContent( blocks = listOf(NovelContentBlock.Text(AnnotatedString(source))), plainText = source, ), pageMode = DbNovelPageMode.Single, requestedPageRatio = 0.0, sourceAnchor = null, fontSize = 14, lineHeight = 1.2, textAlign = DbNovelTextAlign.Left, fontFamily = FontFamily.Serif, textColor = Color.Black, imageHeaders = emptyMap(), paragraphSpacing = 12.dp, padding = 0.dp, onPageChanged = { _, _ -> }, onComplete = { _, _ -> }, onInvalidLayout = { invalid = true }, onPagesReady = { readyPages = it }, ) } try { repeat(4) { scene.render(it * 16_000_000L).close() } assertFalse(invalid, "source should fit across pages: ${source.escapeForTest()}") val pages = assertNotNull(readyPages, "pagination should produce pages for ${source.escapeForTest()}") assertTrue(pages.isNotEmpty()) val slices = pages.flatMap { it.slices } var next = 0 slices.forEach { slice -> assertEquals(0, slice.blockIndex) assertEquals(next, slice.start, "source gap or duplicate for ${source.escapeForTest()}") assertTrue(slice.endExclusive > slice.start) next = slice.endExclusive } assertEquals(source.length, next) assertEquals(source, slices.joinToString("") { source.substring(it.start, it.endExclusive) }) if (source == "A\nB") assertEquals(2, pages.size, "one-line viewport should split at the newline") val renderedPages = pages.map { page -> page.slices.joinToString("") { slice -> renderNovelTextFragment(source.toAnnotatedString(), slice.start, slice.endExclusive).text } } when (source) { "A\nB" -> assertEquals(listOf("A", "B"), renderedPages) "A\n" -> assertEquals(listOf("A", "\u00A0"), renderedPages) "A\n\nB", "A\r\n\r\nB" -> assertEquals( listOf("A", "\u00A0", "B"), renderedPages, "${source.escapeForTest()} slices=${pages.map { it.slices }}", ) "\nA" -> assertEquals(listOf("\u00A0", "A"), renderedPages) "\n\nA" -> assertEquals(listOf("\u00A0", "\u00A0", "A"), renderedPages) "\n" -> assertEquals(listOf("\u00A0"), renderedPages) "A\r\n" -> assertEquals(listOf("A", "\u00A0"), renderedPages) } } finally { scene.close() } } } } @Test fun everyAcceptedFragmentHasMeasuredHeightWithinPageBudget() { onUi { val source = "A\nB\n\nC\n" var allFragmentsFit = false val scene = ImageComposeScene(320, 80) { MeasurementProbe(source) { result -> allFragmentsFit = result } } try { scene.render().close() assertTrue(allFragmentsFit, "all source ranges should advance with measured height within the one-line page budget") } finally { scene.close() } } } @Composable private fun MeasurementProbe(source: String, onComplete: (Boolean) -> Unit) { val density = LocalDensity.current val measurer = rememberTextMeasurer() val block = NovelContentBlock.Text(AnnotatedString(source)) val style = MaterialTheme.typography.bodyLarge.merge( TextStyle(color = Color.Black, fontSize = 14.sp, lineHeight = 17.sp, fontFamily = FontFamily.Serif), ) val width = with(density) { 280.dp.roundToPx() } val pageHeight = with(density) { 32.dp.roundToPx() } var cursor = 0 var valid = true var steps = 0 while (cursor < source.length && steps++ <= source.length) { val fit = measureNovelTextFit(measurer, block, cursor, pageHeight, width, style) if (fit == null || fit.endExclusive <= cursor || fit.heightPx !in 1..pageHeight) { valid = false break } cursor = fit.endExclusive } onComplete(valid && cursor == source.length) } private fun String.escapeForTest() = replace("\n", "\\n") private fun String.toAnnotatedString() = AnnotatedString(this) private fun onUi(block: () -> T): T { val task = FutureTask { block() } SwingUtilities.invokeAndWait(task) return task.get() } }