PlayerUiCompositionTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/player/PlayerUiCompositionTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class)23package eu.friedcutlet.yokan.ui.screens.player45import androidx.compose.runtime.CompositionLocalProvider6import androidx.compose.runtime.MutableState7import androidx.compose.runtime.SideEffect8import androidx.compose.runtime.mutableStateOf9import androidx.compose.ui.ImageComposeScene10import androidx.lifecycle.Lifecycle11import androidx.lifecycle.LifecycleOwner12import androidx.lifecycle.LifecycleRegistry13import androidx.lifecycle.compose.LocalLifecycleOwner14import eu.friedcutlet.yokan.services.media.player.MediaPlayerController15import eu.friedcutlet.yokan.services.media.player.MpvBridgeEvent16import eu.friedcutlet.yokan.services.media.player.PictureInPicturePlaybackOwners17import eu.friedcutlet.yokan.services.media.player.ProfilePlayerServicesTest18import java.util.concurrent.FutureTask19import javax.swing.SwingUtilities20import kotlinx.coroutines.flow.MutableSharedFlow21import kotlinx.coroutines.test.TestScope22import kotlinx.coroutines.test.UnconfinedTestDispatcher23import kotlinx.coroutines.test.runTest24import kotlin.test.Test25import kotlin.test.assertEquals26import kotlin.test.assertFalse27import kotlin.test.assertNotSame28import kotlin.test.assertSame29import kotlin.test.assertTrue3031class PlayerUiCompositionTest {32 @Test fun disposalExpiresBridgeAndPipAndRemountGetsFreshCapabilities() = runTest {33 val f = Scene(this)34 try {35 val old = f.binding; val request = f.request36 assertTrue(f.ui { request() }); f.emit(f.eventsA)37 f.ui { f.visible.value = false }; f.pump()38 assertFalse(old.active()); assertFalse(f.ui { request() }); assertEquals(false to false, f.states.last())39 f.emit(f.eventsA); assertEquals(listOf("A:10:first"), f.delivered)40 f.ui { f.visible.value = true }; f.pump()41 assertNotSame(old, f.binding); assertTrue(f.binding.active()); assertTrue(f.ui { f.request() })42 f.emit(f.eventsA); assertEquals(listOf("A:10:first", "A:10:first"), f.delivered)43 } finally { f.close() }44 }4546 @Test fun controllerChapterAndServiceReplacementExpireOnlyCapturedBindings() = runTest {47 val f = Scene(this)48 try {49 val first = f.binding; val oldRequest = f.request50 f.emit(f.eventsA)51 f.ui { f.controller.value = f.controllerB }; f.pump()52 assertFalse(first.active()); assertFalse(f.ui { oldRequest() })53 f.emit(f.eventsA); assertEquals(1, f.delivered.size)54 f.emit(f.eventsB)55 val second = f.binding56 f.ui { f.chapter.value = 20L }; f.pump(); assertFalse(second.active()); f.emit(f.eventsB)57 val third = f.binding58 f.ui { f.current.value = f.b.service }; f.pump(); assertFalse(third.active()); f.emit(f.eventsB)59 assertEquals(listOf("A:10:first", "A:10:first", "A:20:first", "B:20:first"), f.delivered)60 assertEquals(0, f.eventsA.subscriptionCount.value); assertEquals(1, f.eventsB.subscriptionCount.value)61 assertTrue(f.a.owner.isActive); assertTrue(f.b.owner.isActive); assertTrue(f.ui { f.request() })62 } finally { f.close() }63 }6465 @Test fun stopAndResumeBeforeRecompositionCannotReviveRetainedCallbacks() = runTest {66 val f = Scene(this)67 try {68 val old = f.binding; val request = f.request69 f.ui {70 f.lifecycle.value.registry.currentState = Lifecycle.State.CREATED71 assertFalse(old.active()); assertFalse(request()); assertFalse(f.admitted())72 f.lifecycle.value.registry.currentState = Lifecycle.State.RESUMED73 assertFalse(old.active()); assertFalse(request())74 }75 f.pump()76 assertNotSame(old, f.binding); assertTrue(f.binding.active()); assertTrue(f.ui { f.request() })77 f.emit(f.eventsA); assertEquals(listOf("A:10:first"), f.delivered)78 } finally { f.close() }79 }8081 @Test fun lifecycleOwnerReplacementRemovesOldObserverAndKeepsFreshBindingLive() = runTest {82 val f = Scene(this)83 try {84 val old = f.binding; val oldLifecycle = f.lifecycle.value85 f.ui { f.lifecycle.value = Owner() }; f.pump()86 val fresh = f.binding87 assertFalse(old.active()); assertTrue(fresh.active())88 f.ui { oldLifecycle.registry.currentState = Lifecycle.State.CREATED }; f.pump()89 assertSame(fresh, f.binding); assertTrue(fresh.active()); f.emit(f.eventsA)90 assertEquals(listOf("A:10:first"), f.delivered)91 } finally { f.close() }92 }9394 @Test fun latestHandlerAndPlayingUpdatesDoNotRecreateLiveBinding() = runTest {95 val f = Scene(this)96 try {97 val old = f.binding98 f.ui { f.handler.value = "updated"; f.playing.value = false }; f.pump()99 assertSame(old, f.binding); assertEquals(true to false, f.states.last())100 f.ui { f.lifecycle.value.registry.currentState = Lifecycle.State.STARTED }; f.pump()101 assertSame(old, f.binding); assertTrue(old.active())102 f.emit(f.eventsA); assertEquals(listOf("A:10:updated"), f.delivered)103 } finally { f.close() }104 }105106 private class Owner : LifecycleOwner {107 val registry = LifecycleRegistry.createUnsafe(this).also { it.currentState = Lifecycle.State.RESUMED }108 override val lifecycle: Lifecycle get() = registry109 }110111 private class Scene(val test: TestScope) {112 val a = ProfilePlayerServicesTest.Fixture(test, "A")113 val b = ProfilePlayerServicesTest.Fixture(test, "B")114 val current = mutableStateOf(a.service)115 val chapter = mutableStateOf(10L)116 val visible = mutableStateOf(true)117 val lifecycle = mutableStateOf(Owner())118 val eventsA = MutableSharedFlow<MpvBridgeEvent>()119 val eventsB = MutableSharedFlow<MpvBridgeEvent>()120 val controllerA = PlayerUiBindingTest.controller(eventsA)121 val controllerB = PlayerUiBindingTest.controller(eventsB)122 val controller: MutableState<MediaPlayerController> = mutableStateOf(controllerA)123 val handler = mutableStateOf("first")124 val playing = mutableStateOf(true)125 val delivered = mutableListOf<String>()126 val states = mutableListOf<Pair<Boolean, Boolean>>()127 var admitted: () -> Boolean = { false }128 val owners = PictureInPicturePlaybackOwners(129 publish = { active, eligible, callback -> states += active to eligible; admitted = callback },130 request = { true },131 )132 lateinit var binding: PlayerUiBinding133 lateinit var request: () -> Boolean134 val scene = ui {135 ImageComposeScene(100, 100, coroutineContext = UnconfinedTestDispatcher(test.testScheduler)) {136 CompositionLocalProvider(LocalLifecycleOwner provides lifecycle.value) {137 if (visible.value) {138 val owner = current.value139 val id = chapter.value140 val label = if (owner === a.service) "A" else "B"141 val reader = rememberPlayerServices(owner, id)142 val callbacks = rememberPlayerUiBinding(reader, controller.value)143 val pip = rememberPlayerPictureInPicture(callbacks, playing.value, owners)144 val currentHandler = handler.value145 PlayerBridgeEffect(callbacks) { delivered += "$label:$id:$currentHandler" }146 SideEffect { binding = callbacks; request = pip }147 }148 }149 }150 }151 init { pump() }152 fun pump() = ui { repeat(3) {153 test.testScheduler.runCurrent()154 scene.render(test.testScheduler.currentTime * 1_000_000).close()155 test.testScheduler.runCurrent()156 } }157 suspend fun emit(events: MutableSharedFlow<MpvBridgeEvent>) {158 events.emit(PlayerUiBindingTest.Event); pump()159 }160 suspend fun close() { ui { scene.close() }; a.close(); b.close() }161 fun <T> ui(block: () -> T): T {162 if (SwingUtilities.isEventDispatchThread()) return block()163 val task = FutureTask(block); SwingUtilities.invokeAndWait(task); return task.get()164 }165 }166}167