@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) package eu.friedcutlet.yokan.ui.screens.novel import eu.friedcutlet.yokan.services.tts.ProfileSpeechService import eu.friedcutlet.yokan.services.tts.TextToSpeechRequest import eu.friedcutlet.yokan.services.tts.TextToSpeechService import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest class NovelTtsOwnershipTest { @Test fun pausedAndSupersededUtterancesCannotUpdateHighlight() = runTest { val f = Fixture(this) try { f.controller.start(listOf("First paragraph", "Second paragraph"), TextToSpeechRequest()) runCurrent() val first = f.native.progress.single() first(0, 5) f.controller.pause() val paused = f.controller.state.value first(7, 9) assertEquals(paused, f.controller.state.value) f.controller.resume() runCurrent() val resumed = f.native.progress.last() f.controller.skipNext() runCurrent() first(1, 2); resumed(3, 4) assertEquals(1, f.controller.state.value.paragraphIndex) assertEquals(-1, f.controller.state.value.wordStart) f.native.progress.last()(0, 6) assertEquals(6, f.controller.state.value.wordEnd) } finally { f.close() } } @Test fun retainedCommandsCannotRestartRetiredController() = runTest { val f = Fixture(this) try { f.controller.start(listOf("First", "Second"), TextToSpeechRequest()) runCurrent() val old = f.native.progress.single() f.controller.closeAndJoin() old(0, 4) assertEquals(NovelTtsUiState(), f.controller.state.value) assertFailsWith { f.controller.start(listOf("Late"), TextToSpeechRequest()) } assertFailsWith { f.controller.resume() } assertFailsWith { f.controller.pause() } assertFailsWith { f.controller.skipNext() } assertFailsWith { f.controller.skipPrevious() } assertFailsWith { f.controller.updateRequest(TextToSpeechRequest(pitch = 1.5)) } assertFailsWith { f.controller.stop() } assertEquals(listOf("First"), f.native.spoken) } finally { f.close() } } @Test fun retirementDuringRestartCleanupCannotStartReplacementParagraph() = runTest { val f = Fixture(this) val release = CompletableDeferred() f.native.stopping = { release.await() } try { f.controller.start(listOf("First", "Second"), TextToSpeechRequest()) runCurrent() val restart = async { f.controller.skipNext() } runCurrent() assertFalse(restart.isCompleted) f.controller.retire() val close = async { f.controller.closeAndJoin() } runCurrent() assertFalse(close.isCompleted) release.complete(Unit) close.await() restart.join() assertEquals(listOf("First"), f.native.spoken) assertEquals(NovelTtsUiState(), f.controller.state.value) } finally { release.complete(Unit); f.close() } } @Test fun canceledQueuedCommandCannotStartPlaybackAfterCleanup() = runTest { val f = Fixture(this) val release = CompletableDeferred() f.native.stopping = { release.await() } try { f.controller.start(listOf("First"), TextToSpeechRequest()) runCurrent() val stopping = async { f.controller.stop() } runCurrent() val queued = async { f.controller.start(listOf("Late"), TextToSpeechRequest()) } runCurrent() queued.cancel() runCurrent() release.complete(Unit) stopping.await() queued.join() runCurrent() assertEquals(listOf("First"), f.native.spoken) assertEquals(NovelTtsUiState(), f.controller.state.value) } finally { release.complete(Unit); f.close() } } private class Fixture(test: TestScope) { val owner = SupervisorJob() val scope = CoroutineScope(owner + StandardTestDispatcher(test.testScheduler)) val native = Native() val speech = ProfileSpeechService(native, scope) val reader = speech.reader() val controller = NovelTtsController(reader, scope) { reader.isAvailable } suspend fun close() { controller.closeAndJoin(); speech.closeAndJoin(); owner.cancelAndJoin() } } private class Native : TextToSpeechService { val spoken = mutableListOf() val progress = mutableListOf<(Int, Int) -> Unit>() var stopping: suspend () -> Unit = { } override suspend fun voices(language: String?): List = emptyList() override suspend fun speak(text: String, request: TextToSpeechRequest, onProgress: (Int, Int) -> Unit) { spoken += text; progress += onProgress awaitCancellation() } override suspend fun stop() { stopping() } } }