package eu.friedcutlet.yokan.ui.screens.reader import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.ImageComposeScene import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import java.util.concurrent.FutureTask import javax.swing.SwingUtilities import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestCoroutineScheduler import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @OptIn(ExperimentalCoroutinesApi::class) class ReaderScrollDriverTest { @Test fun speedUpdatesUseOneControllerAndPauseStopsTheNextFrame() = fixture { f -> ui { f.count.value = 20 } f.frames(65) val slow = f.position() assertTrue(slow in 40..60, "Saved speed should retain approximately 50 pixels/second: $slow") ui { f.speed.value = 60f } f.frames(65) assertTrue(f.position() - slow > 250, "Transient speed should update without resetting progress") ui { f.playing.value = false } f.frames(1) val stopped = f.position() f.frames(20) assertEquals(stopped, f.position()) assertEquals(0, f.finished) } @Test fun loadingBoundaryWaitsForExtentAndConfirmedEndFinishesOnce() = fixture { f -> f.frames(8) assertEquals(0, f.position()) assertEquals(0, f.finished) ui { f.count.value = 5 } f.frames(65) assertTrue(f.position() > 30) ui { f.terminal.value = true; f.list.requestScrollToItem(4) } f.frames(8) assertEquals(1, f.finished) assertEquals(false, f.playing.value) f.frames(20) assertEquals(1, f.finished) } @Test fun horizontalRtlScrollsForwardInLogicalOrder() = fixture(horizontalRtl = true) { f -> ui { f.count.value = 20 } f.frames(65) assertTrue(f.position() in 40..60) ui { f.speed.value = 60f } f.frames(65) assertTrue(f.position() > 300) assertEquals(0, f.finished) } private fun fixture(horizontalRtl: Boolean = false, test: (Fixture) -> Unit) { val f = Fixture(horizontalRtl) try { f.frames(2); test(f) } finally { ui { f.scene.close() } } } private class Fixture(val horizontalRtl: Boolean) { val scheduler = TestCoroutineScheduler() val count = mutableStateOf(1) val playing = mutableStateOf(true) val speed = mutableStateOf(10f) val terminal = mutableStateOf(false) var finished = 0 lateinit var list: LazyListState val scene = ui { ImageComposeScene(300, 300, coroutineContext = UnconfinedTestDispatcher(scheduler)) { list = rememberLazyListState() if (horizontalRtl) LazyRow(Modifier.fillMaxSize(), state = list, reverseLayout = true) { items(count.value, key = { it }) { Box(Modifier.width(200.dp)) } } else LazyColumn(Modifier.fillMaxSize(), state = list) { items(count.value, key = { it }) { Box(Modifier.height(200.dp)) } } DriveAutoScroll(list, playing.value, speed.value, terminal.value) { finished++; playing.value = false } } } fun frames(count: Int) = ui { repeat(count) { scheduler.advanceTimeBy(16) scheduler.runCurrent() scene.render(scheduler.currentTime * 1_000_000).close() } } fun position() = ui { list.firstVisibleItemIndex * 200 + list.firstVisibleItemScrollOffset } } companion object { private fun ui(block: () -> T): T { val task = FutureTask { block() } SwingUtilities.invokeAndWait(task) return task.get() } } }