ReaderScrollDriverTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderScrollDriverTest.kt
1package eu.friedcutlet.yokan.ui.screens.reader23import androidx.compose.foundation.layout.Box4import androidx.compose.foundation.layout.fillMaxSize5import androidx.compose.foundation.layout.height6import androidx.compose.foundation.layout.width7import androidx.compose.foundation.lazy.LazyRow8import androidx.compose.foundation.lazy.LazyColumn9import androidx.compose.foundation.lazy.LazyListState10import androidx.compose.foundation.lazy.rememberLazyListState11import androidx.compose.runtime.mutableStateOf12import androidx.compose.ui.ImageComposeScene13import androidx.compose.ui.Modifier14import androidx.compose.ui.unit.dp15import java.util.concurrent.FutureTask16import javax.swing.SwingUtilities17import kotlinx.coroutines.ExperimentalCoroutinesApi18import kotlinx.coroutines.test.TestCoroutineScheduler19import kotlinx.coroutines.test.UnconfinedTestDispatcher20import kotlin.test.Test21import kotlin.test.assertEquals22import kotlin.test.assertTrue2324@OptIn(ExperimentalCoroutinesApi::class)25class ReaderScrollDriverTest {26 @Test fun speedUpdatesUseOneControllerAndPauseStopsTheNextFrame() = fixture { f ->27 ui { f.count.value = 20 }28 f.frames(65)29 val slow = f.position()30 assertTrue(slow in 40..60, "Saved speed should retain approximately 50 pixels/second: $slow")31 ui { f.speed.value = 60f }32 f.frames(65)33 assertTrue(f.position() - slow > 250, "Transient speed should update without resetting progress")34 ui { f.playing.value = false }35 f.frames(1)36 val stopped = f.position()37 f.frames(20)38 assertEquals(stopped, f.position())39 assertEquals(0, f.finished)40 }4142 @Test fun loadingBoundaryWaitsForExtentAndConfirmedEndFinishesOnce() = fixture { f ->43 f.frames(8)44 assertEquals(0, f.position())45 assertEquals(0, f.finished)46 ui { f.count.value = 5 }47 f.frames(65)48 assertTrue(f.position() > 30)49 ui { f.terminal.value = true; f.list.requestScrollToItem(4) }50 f.frames(8)51 assertEquals(1, f.finished)52 assertEquals(false, f.playing.value)53 f.frames(20)54 assertEquals(1, f.finished)55 }5657 @Test fun horizontalRtlScrollsForwardInLogicalOrder() = fixture(horizontalRtl = true) { f ->58 ui { f.count.value = 20 }59 f.frames(65)60 assertTrue(f.position() in 40..60)61 ui { f.speed.value = 60f }62 f.frames(65)63 assertTrue(f.position() > 300)64 assertEquals(0, f.finished)65 }6667 private fun fixture(horizontalRtl: Boolean = false, test: (Fixture) -> Unit) {68 val f = Fixture(horizontalRtl)69 try { f.frames(2); test(f) } finally { ui { f.scene.close() } }70 }71 private class Fixture(val horizontalRtl: Boolean) {72 val scheduler = TestCoroutineScheduler()73 val count = mutableStateOf(1)74 val playing = mutableStateOf(true)75 val speed = mutableStateOf(10f)76 val terminal = mutableStateOf(false)77 var finished = 078 lateinit var list: LazyListState79 val scene = ui {80 ImageComposeScene(300, 300, coroutineContext = UnconfinedTestDispatcher(scheduler)) {81 list = rememberLazyListState()82 if (horizontalRtl) LazyRow(Modifier.fillMaxSize(), state = list, reverseLayout = true) {83 items(count.value, key = { it }) { Box(Modifier.width(200.dp)) }84 } else LazyColumn(Modifier.fillMaxSize(), state = list) {85 items(count.value, key = { it }) { Box(Modifier.height(200.dp)) }86 }87 DriveAutoScroll(list, playing.value, speed.value, terminal.value) { finished++; playing.value = false }88 }89 }90 fun frames(count: Int) = ui {91 repeat(count) {92 scheduler.advanceTimeBy(16)93 scheduler.runCurrent()94 scene.render(scheduler.currentTime * 1_000_000).close()95 }96 }97 fun position() = ui { list.firstVisibleItemIndex * 200 + list.firstVisibleItemScrollOffset }98 }99 companion object {100 private fun <T> ui(block: () -> T): T {101 val task = FutureTask<T> { block() }102 SwingUtilities.invokeAndWait(task)103 return task.get()104 }105 }106}107