MpvNativeSurfaceBindingTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/media/player/MpvNativeSurfaceBindingTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)2package eu.friedcutlet.yokan.services.media.player34import io.github.kmpmpv.MpvRenderTarget5import kotlinx.coroutines.*6import kotlinx.coroutines.test.*7import kotlin.test.*89class MpvNativeSurfaceBindingTest {10 @Test fun destructionSupersedesQueuedAttachmentAndResize() = runTest {11 val f = Fixture(this)12 try {13 val binding = checkNotNull(f.bind())14 binding.attach(MpvRenderTarget(1L), 100, 100); binding.resize(200, 200); binding.detach()15 runCurrent(); assertTrue(f.events.isEmpty())16 binding.attach(MpvRenderTarget(2L), 300, 300); runCurrent()17 assertEquals(listOf("attach:2", "resize:300x300"), f.events)18 binding.closeAndJoin(); assertEquals("detach", f.events.last())19 assertFalse(binding.attach(MpvRenderTarget(3L), 1, 1))20 } finally { f.controller.releaseAndJoin() }21 }22 @Test fun oldCleanupDoesNotDetachAReplacementTarget() = runTest {23 val f = Fixture(this)24 try {25 val old = checkNotNull(f.bind()); old.attach(MpvRenderTarget(1L), 10, 10); runCurrent()26 val fresh = checkNotNull(f.bind()); fresh.attach(MpvRenderTarget(2L), 20, 20); runCurrent()27 old.closeAndJoin()28 assertEquals(listOf("attach:1", "resize:10x10", "attach:2", "resize:20x20"), f.events)29 fresh.closeAndJoin(); assertEquals("detach", f.events.last())30 } finally { f.controller.releaseAndJoin() }31 }32 @Test fun retirementJoinsNativeAttachmentBeforeDetachmentAndPlayerRelease() = runTest {33 val f = Fixture(this); val entered = CompletableDeferred<Unit>(); val finish = CompletableDeferred<Unit>()34 f.player.onAttach = { f.events += "attach:${it.nativeWindowHandle}"; withContext(NonCancellable) { entered.complete(Unit); finish.await() } }35 val binding = checkNotNull(f.bind()); binding.attach(MpvRenderTarget(1L), 10, 10); entered.await()36 val close = async { f.controller.releaseAndJoin() }; runCurrent()37 assertFalse(close.isCompleted); assertEquals(0, f.player.releaseCount)38 finish.complete(Unit); close.await()39 assertEquals(listOf("attach:1", "detach", "release"), f.events)40 assertNull(f.bind())41 }42 @Test fun failedDetachmentRetriesBeforeNativePlayerRelease() = runTest {43 val f = Fixture(this); var attempts = 0; var fail = true44 f.player.onDetach = { attempts++; if (fail) error("detach failed") }45 val binding = checkNotNull(f.bind()); binding.attach(MpvRenderTarget(1L), 10, 10); runCurrent()46 assertFailsWith<IllegalStateException> { f.controller.releaseAndJoin() }47 assertEquals(0, f.player.releaseCount)48 fail = false; f.controller.releaseAndJoin(); assertTrue(attempts >= 2); assertEquals(1, f.player.releaseCount)49 }50 @Test fun partialAttachmentFailureStillOwnsFinalDetachment() = runTest {51 val f = Fixture(this); f.player.onAttach = { f.events += "attach:${it.nativeWindowHandle}"; error("partial native attachment") }52 val binding = checkNotNull(f.bind()); binding.attach(MpvRenderTarget(1L), 10, 10); runCurrent()53 binding.closeAndJoin(); assertEquals(listOf("attach:1", "detach"), f.events)54 f.controller.releaseAndJoin()55 }56 private class Fixture(test: TestScope) {57 val events = mutableListOf<String>()58 var removals = 059 val player = MediaPlayerInitializationTest.RecordingMpvPlayer().apply {60 onAttach = { this@Fixture.events += "attach:${it.nativeWindowHandle}" }61 onResize = { width, height -> this@Fixture.events += "resize:${width}x$height" }62 onDetach = { this@Fixture.events += "detach" }63 onRelease = { this@Fixture.events += "release" }64 }65 val controller = MpvMediaPlayerController(player, NoopMediaStreamProxy, StandardTestDispatcher(test.testScheduler))66 private val context = StandardTestDispatcher(test.testScheduler)67 fun bind() = controller.bindNativeSurface(context) { removals++ }68 }69}70