package eu.friedcutlet.yokan.ui.navigation import androidx.compose.runtime.BroadcastFrameClock import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @OptIn(ExperimentalCoroutinesApi::class) class InteractiveBackGestureTest { @Test fun releaseUsesVelocityDirectionAndFallsBackToDistance() { assertTrue(shouldCompleteBack(0.15f, 2f)) assertFalse(shouldCompleteBack(0.8f, -2f)) assertFalse(shouldCompleteBack(0.49f, 0.1f)) assertTrue(shouldCompleteBack(0.51f, -0.1f)) } @Test fun completionKeepsOutgoingPageOffscreenAndOnlyPopsOnce() = runTest { val clock = BroadcastFrameClock() val gesture = InteractiveBackGesture(CoroutineScope(backgroundScope.coroutineContext + clock)) var pops = 0 assertTrue(gesture.begin()) gesture.drag(240f, 400f) gesture.finish(2f, false) { pops++ } gesture.finish(2f, false) { pops++ } assertFalse(gesture.begin()) repeat(120) { testScheduler.runCurrent(); clock.sendFrame(it * 16_000_000L) } testScheduler.runCurrent() assertEquals(1, pops) assertEquals(1f, gesture.progress) assertTrue(gesture.active) gesture.dispose() } @Test fun cancellationReturnsToRestAndDisposedGestureCannotPop() = runTest { val clock = BroadcastFrameClock() val gesture = InteractiveBackGesture(CoroutineScope(backgroundScope.coroutineContext + clock)) var pops = 0 gesture.begin() gesture.drag(300f, 400f) gesture.finish(3f, true) { pops++ } repeat(120) { testScheduler.runCurrent(); clock.sendFrame(it * 16_000_000L) } testScheduler.runCurrent() assertEquals(0, pops) assertEquals(0f, gesture.progress) assertFalse(gesture.active) assertTrue(gesture.begin()) gesture.drag(300f, 400f) gesture.finish(3f, false) { pops++ } gesture.dispose() repeat(120) { testScheduler.runCurrent(); clock.sendFrame((it + 120) * 16_000_000L) } assertEquals(0, pops) assertFalse(gesture.begin()) } }