InteractiveBackGestureTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/navigation/InteractiveBackGestureTest.kt
1package eu.friedcutlet.yokan.ui.navigation23import androidx.compose.runtime.BroadcastFrameClock4import kotlinx.coroutines.CoroutineScope5import kotlinx.coroutines.ExperimentalCoroutinesApi6import kotlinx.coroutines.test.runTest7import kotlin.test.Test8import kotlin.test.assertEquals9import kotlin.test.assertFalse10import kotlin.test.assertTrue1112@OptIn(ExperimentalCoroutinesApi::class)13class InteractiveBackGestureTest {14 @Test15 fun releaseUsesVelocityDirectionAndFallsBackToDistance() {16 assertTrue(shouldCompleteBack(0.15f, 2f))17 assertFalse(shouldCompleteBack(0.8f, -2f))18 assertFalse(shouldCompleteBack(0.49f, 0.1f))19 assertTrue(shouldCompleteBack(0.51f, -0.1f))20 }2122 @Test23 fun completionKeepsOutgoingPageOffscreenAndOnlyPopsOnce() = runTest {24 val clock = BroadcastFrameClock()25 val gesture = InteractiveBackGesture(CoroutineScope(backgroundScope.coroutineContext + clock))26 var pops = 027 assertTrue(gesture.begin())28 gesture.drag(240f, 400f)29 gesture.finish(2f, false) { pops++ }30 gesture.finish(2f, false) { pops++ }31 assertFalse(gesture.begin())32 repeat(120) { testScheduler.runCurrent(); clock.sendFrame(it * 16_000_000L) }33 testScheduler.runCurrent()34 assertEquals(1, pops)35 assertEquals(1f, gesture.progress)36 assertTrue(gesture.active)37 gesture.dispose()38 }3940 @Test41 fun cancellationReturnsToRestAndDisposedGestureCannotPop() = runTest {42 val clock = BroadcastFrameClock()43 val gesture = InteractiveBackGesture(CoroutineScope(backgroundScope.coroutineContext + clock))44 var pops = 045 gesture.begin()46 gesture.drag(300f, 400f)47 gesture.finish(3f, true) { pops++ }48 repeat(120) { testScheduler.runCurrent(); clock.sendFrame(it * 16_000_000L) }49 testScheduler.runCurrent()50 assertEquals(0, pops)51 assertEquals(0f, gesture.progress)52 assertFalse(gesture.active)53 assertTrue(gesture.begin())54 gesture.drag(300f, 400f)55 gesture.finish(3f, false) { pops++ }56 gesture.dispose()57 repeat(120) { testScheduler.runCurrent(); clock.sendFrame((it + 120) * 16_000_000L) }58 assertEquals(0, pops)59 assertFalse(gesture.begin())60 }61}62