@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) package eu.friedcutlet.yokan.services.media.player import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.async import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest import kotlinx.coroutines.withContext import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue class ProfilePlayerScreenshotTest { @Test fun screenshotPathIsPublishedOnlyAfterNativeCaptureCompletes() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) val controller = ProfilePlayerServicesTest.Controller() val finish = CompletableDeferred() controller.onAwaitedCommand = { args -> finish.await() f.files.bytes[args[2]] = byteArrayOf(1, 2, 3) true } val request = async { f.reader.requestScreenshot(controller, "Book", "Episode") } try { runCurrent() assertFalse(request.isCompleted) assertEquals(0, f.files.bytes.values.single().size) finish.complete(Unit) val path = requireNotNull(request.await()) assertEquals(listOf("sync", "screenshot-to-file", path, "video"), controller.commands.single()) assertEquals(listOf(1, 2, 3), f.files.bytes.getValue(path).toList()) assertTrue(f.files.deletions.isEmpty()) } finally { finish.complete(Unit); request.cancelAndJoin(); f.close() } } @Test fun canceledCaptureJoinsLateNativeWriteBeforeRemovingItsFile() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) val controller = ProfilePlayerServicesTest.Controller() val finish = CompletableDeferred() controller.onAwaitedCommand = { args -> try { awaitCancellation() } finally { withContext(NonCancellable) { finish.await() assertTrue(f.files.deletions.isEmpty()) f.files.bytes[args[2]] = byteArrayOf(1) } } } val request = async { f.reader.requestScreenshot(controller, "Book", "Episode") } try { runCurrent(); request.cancel(); runCurrent() assertFalse(request.isCompleted) assertTrue(f.files.deletions.isEmpty()) finish.complete(Unit); request.join() assertEquals(f.files.writes, f.files.deletions) assertTrue(f.files.bytes.isEmpty()) assertTrue(f.service.isAvailable) } finally { finish.complete(Unit); request.cancelAndJoin(); f.close() } } @Test fun serviceRetirementJoinsCaptureAndPreservesAnotherOwnersFiles() = runTest { val a = ProfilePlayerServicesTest.Fixture(this, "A") val b = ProfilePlayerServicesTest.Fixture(this, "B") val controller = ProfilePlayerServicesTest.Controller() val finish = CompletableDeferred() controller.onAwaitedCommand = { try { awaitCancellation() } finally { withContext(NonCancellable) { finish.await() } } } val request = async { a.reader.requestScreenshot(controller, "Book", "Episode") } try { runCurrent() val close = async { a.service.closeAndJoin() } runCurrent() assertFalse(close.isCompleted); assertFalse(request.isCompleted) val otherPath = requireNotNull(b.reader.requestScreenshot(ProfilePlayerServicesTest.Controller(), "Other", "Episode")) finish.complete(Unit); close.await() assertFailsWith { request.await() } assertTrue(a.files.bytes.isEmpty()); assertTrue(a.owner.isActive) assertTrue(b.files.bytes.containsKey(otherPath)); assertTrue(b.service.isAvailable) assertTrue(b.files.deletions.isEmpty()) } finally { finish.complete(Unit); request.cancelAndJoin(); a.close(); b.close() } } @Test fun failedCaptureRemovesOnlyItsOwnPartialFile() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) val controller = ProfilePlayerServicesTest.Controller() try { val prior = requireNotNull(f.reader.requestScreenshot(controller, "Book", "Episode")) controller.onAwaitedCommand = { args -> f.files.bytes[args[2]] = byteArrayOf(1); false } assertEquals(null, f.reader.requestScreenshot(controller, "Book", "Episode")) assertEquals(listOf(f.files.writes.last()), f.files.deletions) assertTrue(f.files.bytes.containsKey(prior)) } finally { f.close() } } @Test fun thrownCaptureFailureRemovesItsSeedWithoutRetiringOwner() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) val controller = ProfilePlayerServicesTest.Controller() controller.onAwaitedCommand = { throw IllegalStateException("Capture failed") } try { assertFailsWith { f.reader.requestScreenshot(controller, "Book", "Episode") } assertEquals(f.files.writes, f.files.deletions) assertTrue(f.files.bytes.isEmpty()); assertTrue(f.service.isAvailable) } finally { f.close() } } @Test fun expiryAfterNativeSuccessCannotPublishOrKeepCapturedFile() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) val controller = ProfilePlayerServicesTest.Controller() controller.onAwaitedCommand = { args -> f.files.bytes[args[2]] = byteArrayOf(1) f.reader.retire() true } try { assertFailsWith { f.reader.requestScreenshot(controller, "Book", "Episode") } assertEquals(f.files.writes, f.files.deletions) assertTrue(f.files.bytes.isEmpty()) } finally { f.close() } } @Test fun queueAdmissionAloneCannotReportCaptureSuccess() = runTest { val f = ProfilePlayerServicesTest.Fixture(this) var queued = false val fallback = createComposeMediaPlayerController(NoopMediaStreamProxy) val queueOnly = object : MediaPlayerController by fallback { override val isMpvBackend = true override fun executeMpvCommand(vararg arguments: String): Boolean { queued = true; return true } } try { assertEquals(null, f.reader.requestScreenshot(queueOnly, "Book", "Episode")) assertFalse(queued) assertEquals(f.files.writes, f.files.deletions) } finally { fallback.releaseAndJoin(); f.close() } } }