PlayerUiBindingTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/player/PlayerUiBindingTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class, kotlinx.coroutines.InternalCoroutinesApi::class)23package eu.friedcutlet.yokan.ui.screens.player45import eu.friedcutlet.yokan.services.media.player.MediaPlayerController6import eu.friedcutlet.yokan.services.media.player.MpvBridgeEvent7import eu.friedcutlet.yokan.services.media.player.ProfilePlayerServicesTest8import kotlinx.coroutines.CancellationException9import kotlinx.coroutines.CompletableDeferred10import kotlinx.coroutines.CoroutineName11import kotlinx.coroutines.CoroutineScope12import kotlinx.coroutines.Job13import kotlinx.coroutines.NonCancellable14import kotlinx.coroutines.SupervisorJob15import kotlinx.coroutines.async16import kotlinx.coroutines.awaitCancellation17import kotlinx.coroutines.cancelAndJoin18import kotlinx.coroutines.currentCoroutineContext19import kotlinx.coroutines.flow.Flow20import kotlinx.coroutines.flow.FlowCollector21import kotlinx.coroutines.flow.MutableSharedFlow22import kotlinx.coroutines.flow.flow23import kotlinx.coroutines.test.StandardTestDispatcher24import kotlinx.coroutines.test.UnconfinedTestDispatcher25import kotlinx.coroutines.test.runCurrent26import kotlinx.coroutines.test.runTest27import kotlinx.coroutines.withContext28import kotlin.test.Test29import kotlin.test.assertEquals30import kotlin.test.assertFailsWith31import kotlin.test.assertFalse32import kotlin.test.assertTrue3334class PlayerUiBindingTest {35 @Test fun bridgeDeliveryUsesCapturedUiContextAndExpiresOnClose() = runTest {36 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob()37 val events = MutableSharedFlow<MpvBridgeEvent>(); val delivered = mutableListOf<MpvBridgeEvent>()38 val binding = PlayerUiBinding(f.reader, controller(events), CoroutineScope(parent + StandardTestDispatcher(testScheduler) + CoroutineName("captured-ui")))39 val collect = async { binding.collectBridgeEvents {40 delivered += it41 } }42 try {43 runCurrent(); assertEquals(1, events.subscriptionCount.value)44 events.emit(Event); runCurrent(); assertEquals(listOf(Event), delivered)45 binding.close(); collect.join(); events.emit(Event)46 assertEquals(listOf(Event), delivered); assertEquals(0, events.subscriptionCount.value)47 assertTrue(parent.isActive); assertTrue(f.service.isAvailable)48 } finally { binding.close(); collect.cancelAndJoin(); parent.cancelAndJoin(); f.close() }49 }5051 @Test fun readerDeliveryRetainsCallersContextAcrossOwnedCollection() = runTest {52 val f = ProfilePlayerServicesTest.Fixture(this)53 var deliveredContext: String? = null54 val source = flow { emit(Event); awaitCancellation() }55 val collect = async(CoroutineName("captured-ui")) {56 f.reader.collectBridgeEvents(controller(source)) { deliveredContext = currentCoroutineContext()[CoroutineName]?.name }57 }58 try { runCurrent(); assertEquals("captured-ui", deliveredContext) }59 finally { collect.cancelAndJoin(); f.close() }60 }6162 @Test fun disposedBindingJoinsFinalizerAndRejectsItsLateEvent() = runTest {63 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob(); val finish = CompletableDeferred<Unit>()64 val delivered = mutableListOf<MpvBridgeEvent>(); var rejectedLate = false65 val source = object : Flow<MpvBridgeEvent> {66 override suspend fun collect(collector: FlowCollector<MpvBridgeEvent>) {67 collector.emit(Event)68 try { awaitCancellation() }69 finally { withContext(NonCancellable) {70 finish.await()71 try { collector.emit(Event.copy(value = "late")) } catch (_: CancellationException) { rejectedLate = true }72 } }73 }74 }75 val binding = PlayerUiBinding(f.reader, controller(source), CoroutineScope(parent + StandardTestDispatcher(testScheduler)))76 val collect = async { binding.collectBridgeEvents { delivered += it } }77 try {78 runCurrent(); binding.close(); runCurrent()79 assertFalse(collect.isCompleted); assertFalse(binding.active()); assertTrue(parent.isActive)80 finish.complete(Unit); collect.join()81 assertEquals(listOf(Event), delivered); assertTrue(rejectedLate)82 } finally { finish.complete(Unit); binding.close(); collect.cancelAndJoin(); parent.cancelAndJoin(); f.close() }83 }8485 @Test fun callerCancellationJoinsCollectionWithoutCancelingSiblingBinding() = runTest {86 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob(); val finish = CompletableDeferred<Unit>()87 val uiScope = CoroutineScope(parent + StandardTestDispatcher(testScheduler))88 val source = flow<MpvBridgeEvent> { try { awaitCancellation() } finally { withContext(NonCancellable) { finish.await() } } }89 val first = PlayerUiBinding(f.reader, controller(source), uiScope)90 val events = MutableSharedFlow<MpvBridgeEvent>(); val delivered = mutableListOf<MpvBridgeEvent>()91 val sibling = PlayerUiBinding(f.reader, controller(events), uiScope)92 val a = async { first.collectBridgeEvents { } }; val b = async { sibling.collectBridgeEvents { delivered += it } }93 try {94 runCurrent(); a.cancel(); runCurrent(); assertFalse(a.isCompleted)95 events.emit(Event); runCurrent(); assertEquals(listOf(Event), delivered); assertTrue(sibling.active())96 finish.complete(Unit); a.join(); assertTrue(first.active()); assertTrue(parent.isActive)97 } finally { finish.complete(Unit); first.close(); sibling.close(); a.cancelAndJoin(); b.cancelAndJoin(); parent.cancelAndJoin(); f.close() }98 }99100 @Test fun sessionRetirementJoinsBridgeFinalizerBeforeReturning() = runTest {101 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob(); val finish = CompletableDeferred<Unit>()102 val source = flow<MpvBridgeEvent> { try { awaitCancellation() } finally { withContext(NonCancellable) { finish.await() } } }103 val binding = PlayerUiBinding(f.reader, controller(source), CoroutineScope(parent + StandardTestDispatcher(testScheduler)))104 val collect = async { binding.collectBridgeEvents { } }105 try {106 runCurrent(); val close = async { f.service.closeAndJoin() }; runCurrent()107 assertFalse(close.isCompleted); assertFalse(collect.isCompleted); assertFalse(binding.active())108 finish.complete(Unit); close.await(); collect.join()109 assertTrue(parent.isActive); assertTrue(f.owner.isActive)110 } finally { finish.complete(Unit); binding.close(); collect.cancelAndJoin(); parent.cancelAndJoin(); f.close() }111 }112113 @Test fun inactiveOrExpiredBindingsCannotSubscribe() = runTest {114 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob(); var active = false115 val events = MutableSharedFlow<MpvBridgeEvent>()116 val binding = PlayerUiBinding(f.reader, controller(events), CoroutineScope(parent + StandardTestDispatcher(testScheduler))) { active }117 try {118 binding.collectBridgeEvents { error("Inactive delivery") }; assertEquals(0, events.subscriptionCount.value)119 active = true; f.reader.retire()120 binding.collectBridgeEvents { error("Expired delivery") }; assertEquals(0, events.subscriptionCount.value)121 } finally { binding.close(); parent.cancelAndJoin(); f.close() }122 }123124 @Test fun alreadyCanceledCallerCannotSubscribeIntoLiveBinding() = runTest {125 val f = ProfilePlayerServicesTest.Fixture(this); val parent = SupervisorJob()126 val events = MutableSharedFlow<MpvBridgeEvent>()127 val binding = PlayerUiBinding(f.reader, controller(events), CoroutineScope(parent + StandardTestDispatcher(testScheduler)))128 try {129 val call = async(UnconfinedTestDispatcher(testScheduler)) {130 requireNotNull(currentCoroutineContext()[Job]).cancel()131 assertFailsWith<CancellationException> { binding.collectBridgeEvents { } }132 }133 call.join(); assertEquals(0, events.subscriptionCount.value); assertTrue(binding.active())134 } finally { binding.close(); parent.cancelAndJoin(); f.close() }135 }136137 companion object {138 val Event = MpvBridgeEvent("aniyomi/switch_episode", "n")139 fun controller(events: Flow<MpvBridgeEvent>): MediaPlayerController = object : MediaPlayerController by ProfilePlayerServicesTest.Controller() {140 override val mpvBridgeEvents = events141 }142 }143}144