@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class) package eu.friedcutlet.yokan.services.media.player import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.ImageComposeScene import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.ui.screens.player.PlayerControllerEffect import eu.friedcutlet.yokan.ui.screens.player.rememberPlayerServices import java.util.concurrent.FutureTask import javax.swing.SwingUtilities import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue class PlayerControllerCompositionTest { @Test fun disposalSavesBeforeReleaseAndRemountAllocatesFreshController() = runTest { val f = Scene(this) try { val old = f.controllersA.single() f.ui { f.visible.value = false }; f.pump() assertTrue(old.released); assertEquals(listOf("A:10:42000"), f.saved) f.ui { f.visible.value = true }; f.pump() assertEquals(2, f.controllersA.size); assertFalse(f.controllersA.last().released) assertTrue(f.errors.isEmpty()) } finally { f.close() } } @Test fun chapterAndOwnerReplacementSaveOnlyTheirCapturedEpisodeAndController() = runTest { val f = Scene(this) try { val first = f.controllersA.single() f.ui { f.chapter.value = 20L }; f.pump() assertTrue(first.released); assertFalse(f.controllersA.last().released) f.ui { f.current.value = f.b.service }; f.pump() assertTrue(f.controllersA.last().released); assertFalse(f.controllersB.single().released) assertEquals(listOf("A:10:42000", "A:20:42000"), f.saved) assertEquals(listOf("A:10", "A:20", "B:20"), f.published) assertTrue(f.errors.isEmpty()) } finally { f.close() } } private class Scene(val test: TestScope) { val controllersA = mutableListOf() val controllersB = mutableListOf() val a = ProfilePlayerServicesTest.Fixture(test, "A", factory = { _, _ -> ProfilePlayerControllerTest.Controller().also { controllersA += it } }) val b = ProfilePlayerServicesTest.Fixture(test, "B", factory = { _, _ -> ProfilePlayerControllerTest.Controller().also { controllersB += it } }) val current = mutableStateOf(a.service) val chapter = mutableStateOf(10L) val visible = mutableStateOf(true) val saved = mutableListOf() val published = mutableListOf() val errors = mutableListOf() val scene = ui { ImageComposeScene(100, 100, coroutineContext = UnconfinedTestDispatcher(test.testScheduler)) { if (visible.value) { val owner = current.value val id = chapter.value val label = if (owner === a.service) "A" else "B" val reader = rememberPlayerServices(owner, id) val config = remember(reader) { reader.configuration(DbSettings()) } PlayerControllerEffect(reader, config, 0, onController = { if (it != null) published += "$label:$id" }, onSavePosition = { position, _ -> saved += "$label:$id:$position" }, onInitializationError = { if (it != null) errors += it }, ) } } } init { pump() } fun pump() = ui { repeat(3) { test.testScheduler.runCurrent() scene.render(test.testScheduler.currentTime * 1_000_000).close() test.testScheduler.runCurrent() } } suspend fun close() { ui { scene.close() }; a.close(); b.close() } fun ui(block: () -> T): T { if (SwingUtilities.isEventDispatchThread()) return block() val task = FutureTask(block) SwingUtilities.invokeAndWait(task) return task.get() } } }