Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MF-171: Add PlayLog test 13 #46

Open
wants to merge 5 commits into
base: jantonio/playlog_test_12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.tidal.sdk.player.common

class PlaybackEngineUsageAfterReleaseException :
IllegalStateException("Attempted to use a released PlaybackEngine")
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ internal class ExoPlayerPlaybackEngine(
}

override fun release() {
val positionInSeconds = if (forwardingMediaProduct?.productType == ProductType.BROADCAST) {
extendedExoPlayer.currentPositionSinceEpochMs
} else {
extendedExoPlayer.currentPositionMs
}.toDouble() / MS_IN_SECOND
reportEnd(EndReason.OTHER, endPositionSeconds = positionInSeconds)
extendedExoPlayer.release()
coroutineScope.launch {
eventSink.emit(Event.Release)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tidal.sdk.player.playbackengine

import android.os.Handler
import com.tidal.sdk.player.common.PlaybackEngineUsageAfterReleaseException
import com.tidal.sdk.player.common.model.AudioQuality
import com.tidal.sdk.player.common.model.LoudnessNormalizationMode
import com.tidal.sdk.player.common.model.MediaProduct
Expand Down Expand Up @@ -78,8 +79,7 @@ internal class SingleHandlerPlaybackEngine(

private fun postOrThrow(runnable: Runnable) {
if (!handler.post(runnable)) {
val className = SingleHandlerPlaybackEngine::class.simpleName
error("Attempt to use a released instance of $className")
throw PlaybackEngineUsageAfterReleaseException()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ internal class ExoPlayerPlaybackEngineTest {

playbackEngine.release()

verify(initialExtendedExoPlayer).currentPositionMs
verify(initialExtendedExoPlayer).release()
verifyNoMoreInteractions(initialExtendedExoPlayer, looper)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tidal.sdk.player.playlog

import assertk.Assert
import assertk.assertions.isCloseTo

internal fun Assert<Double>.isAssetPositionEqualTo(targetPosition: Double) =
isCloseTo(targetPosition, 0.5)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package com.tidal.sdk.player.playlog

import android.app.Application
import androidx.test.platform.app.InstrumentationRegistry
import assertk.Assert
import assertk.assertThat
import assertk.assertions.isBetween
import assertk.assertions.isCloseTo
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import com.google.gson.Gson
Expand All @@ -18,6 +16,7 @@ import com.tidal.sdk.common.TidalMessage
import com.tidal.sdk.eventproducer.EventSender
import com.tidal.sdk.eventproducer.model.ConsentCategory
import com.tidal.sdk.player.Player
import com.tidal.sdk.player.common.PlaybackEngineUsageAfterReleaseException
import com.tidal.sdk.player.common.model.MediaProduct
import com.tidal.sdk.player.common.model.ProductType
import com.tidal.sdk.player.events.EventReporterModuleRoot
Expand Down Expand Up @@ -142,10 +141,18 @@ 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 (throwable: Throwable) {
if (throwable !is PlaybackEngineUsageAfterReleaseException) {
throw throwable
}
}
verify(eventSender, atMost(Int.MAX_VALUE))
.sendEvent(
argThat { !contentEquals("playback_session") },
Expand Down Expand Up @@ -699,8 +706,72 @@ internal class SingleMediaProductPlayLogTest {
)
}

private fun Assert<Double>.isAssetPositionEqualTo(targetPosition: Double) = run {
isCloseTo(targetPosition, 0.5)
@Test
fun playAndStop() = 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.reset()
}

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()),
)
}

@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()),
)
}
}

Expand Down
Loading