Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PlayLog test 10
Browse files Browse the repository at this point in the history
stoyicker committed Jun 27, 2024
1 parent 61e760b commit 4321798
Showing 1 changed file with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -25,17 +25,20 @@ import com.tidal.sdk.player.events.di.DefaultEventReporterComponent
import com.tidal.sdk.player.events.model.PlaybackSession
import com.tidal.sdk.player.events.playlogtest.PlayLogTestDefaultEventReporterComponentFactory
import com.tidal.sdk.player.events.reflectionComponentFactoryF
import com.tidal.sdk.player.playbackengine.model.Event
import com.tidal.sdk.player.playbackengine.model.Event.MediaProductEnded
import com.tidal.sdk.player.playbackengine.model.Event.MediaProductTransition
import com.tidal.sdk.player.playbackengine.model.Event.Release
import com.tidal.sdk.player.setBodyFromFile
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
@@ -58,6 +61,7 @@ import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.mockito.Mockito.atMost
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verifyNoMoreInteractions
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.argThat
@@ -153,7 +157,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd

@After
fun afterEach() = runBlocking {
val job = launch { player.playbackEngine.events.first { it is Event.Release } }
val job = launch { player.playbackEngine.events.first { it is Release } }
player.release()
job.join()
verify(eventSender, atMost(Int.MAX_VALUE))
@@ -206,7 +210,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
@@ -265,7 +269,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
@@ -319,7 +323,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(3.seconds)
while (player.playbackEngine.assetPosition < 3) {
@@ -373,7 +377,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
@@ -429,7 +433,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(3.seconds)
while (player.playbackEngine.assetPosition < 3) {
@@ -486,7 +490,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
@@ -570,7 +574,7 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
player.playbackEngine.events.filter { it is MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
@@ -672,6 +676,45 @@ internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProd
)
}

@Test
fun playWithRepeatOne() = runTest {
val gson = Gson()

player.playbackEngine.load(mediaProduct)
player.playbackEngine.setRepeatOne(true)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is MediaProductTransition }
.take(2)
.collect()
}
player.playbackEngine.setRepeatOne(false)
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is MediaProductEnded }.first()
}
}

eventReporterCoroutineScope.advanceUntilIdle()
verify(eventSender, times(2)).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(MEDIA_PRODUCT_DURATION_SECONDS)
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<Double>.isAssetPositionEqualTo(targetPosition: Double) = run {
isCloseTo(targetPosition, 0.5)
}

0 comments on commit 4321798

Please sign in to comment.