From 0a98f7f251a2f90593c25a30bc654ca5a9713484 Mon Sep 17 00:00:00 2001 From: Jorge Antonio Diaz-Benito Soriano Date: Wed, 26 Jun 2024 11:08:45 +0200 Subject: [PATCH] Add PlayLog test 12 --- .../playlog/SingleMediaProductPlayLogTest.kt | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/player/src/androidTest/kotlin/com/tidal/sdk/player/playlog/SingleMediaProductPlayLogTest.kt b/player/src/androidTest/kotlin/com/tidal/sdk/player/playlog/SingleMediaProductPlayLogTest.kt index 4155e889..340160ca 100644 --- a/player/src/androidTest/kotlin/com/tidal/sdk/player/playlog/SingleMediaProductPlayLogTest.kt +++ b/player/src/androidTest/kotlin/com/tidal/sdk/player/playlog/SingleMediaProductPlayLogTest.kt @@ -142,10 +142,17 @@ internal class SingleMediaProductPlayLogTest { } @After - fun afterEach() = runBlocking { - val job = launch { player.playbackEngine.events.first { it is Event.Release } } - player.release() - job.join() + fun afterEach() { + try { + runBlocking { + val job = launch { player.playbackEngine.events.first { it is Event.Release } } + player.release() + job.join() + } + } catch (alreadyReleasedException: IllegalStateException) { + assertThat(alreadyReleasedException.message) + .isEqualTo("Attempt to use a released instance of SingleHandlerPlaybackEngine") + } verify(eventSender, atMost(Int.MAX_VALUE)) .sendEvent( argThat { !contentEquals("playback_session") }, @@ -733,6 +740,40 @@ internal class SingleMediaProductPlayLogTest { ) } + @Test + fun playWithRelease() = runTest { + player.playbackEngine.load(mediaProduct) + player.playbackEngine.play() + withContext(Dispatchers.Default.limitedParallelism(1)) { + withTimeout(8.seconds) { + player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first() + } + delay(1.seconds) + while (player.playbackEngine.assetPosition < 1) { + delay(10.milliseconds) + } + player.playbackEngine.release() + } + + eventReporterCoroutineScope.advanceUntilIdle() + verify(eventSender).sendEvent( + eq("playback_session"), + eq(ConsentCategory.NECESSARY), + argThat { + with(Gson().fromJson(this, JsonObject::class.java)["payload"].asJsonObject) { + assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0) + assertThat(get("endAssetPosition").asDouble).isAssetPositionEqualTo(1.0) + assertThat(get("actualProductId").asString).isEqualTo(mediaProduct.productId) + assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct.sourceType) + assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct.sourceId) + assertThat(get("actions").asJsonArray).isEmpty() + } + true + }, + eq(emptyMap()), + ) + } + private fun Assert.isAssetPositionEqualTo(targetPosition: Double) = run { isCloseTo(targetPosition, 0.5) }