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 23 #57

Draft
wants to merge 3 commits into
base: jantonio/playlog_test_21
Choose a base branch
from
Draft
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
Expand Up @@ -204,7 +204,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
Expand Down Expand Up @@ -264,7 +264,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
Expand Down Expand Up @@ -318,7 +318,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(3.seconds)
Expand Down Expand Up @@ -372,7 +372,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
Expand Down Expand Up @@ -428,7 +428,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(3.seconds)
Expand Down Expand Up @@ -485,7 +485,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
Expand Down Expand Up @@ -569,7 +569,7 @@ internal class SingleMediaProductPlayLogTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(4.seconds) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
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.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -515,4 +517,203 @@ internal class TwoMediaProductsPlayLogTest {
},
)
}

@Suppress("CyclomaticComplexMethod", "LongMethod")
@Test
fun playAndDoALot() = runTest {
val gson = Gson()
val payloadCaptor = argumentCaptor<String>()

player.playbackEngine.load(mediaProduct1)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
delay(10.milliseconds)
}
player.playbackEngine.pause()
player.playbackEngine.seek(3_000F)
player.playbackEngine.play()
player.playbackEngine.setNext(mediaProduct2)
player.playbackEngine.pause()
player.playbackEngine.play()
delay(1.seconds)
while (player.playbackEngine.assetPosition < 4) {
delay(10.milliseconds)
}
withTimeout(8.seconds) {
val waitJob = async {
player.playbackEngine.events
.filter { it is Event.MediaProductTransition }
.first()
}
player.playbackEngine.skipToNext()
waitJob.await()
}
player.playbackEngine.seek(58_000F)
player.playbackEngine.setRepeatOne(true)
withTimeout(10.seconds) {
player.playbackEngine
.events
.filterIsInstance<Event.MediaProductTransition>()
.first()
}
delay(1.seconds)
while (player.playbackEngine.assetPosition < 1) {
delay(10.milliseconds)
}
player.playbackEngine.reset()
}

eventReporterCoroutineScope.advanceUntilIdle()
verify(eventSender, times(3)).sendEvent(
eq("playback_session"),
eq(ConsentCategory.NECESSARY),
payloadCaptor.capture(),
eq(emptyMap()),
)
payloadCaptor.allValues.map {
gson.fromJson(it, JsonObject::class.java)["payload"].asJsonObject
}.combinedPassAllOf(
1 to {
assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0)
assertThat(get("endAssetPosition").asDouble).isAssetPositionEqualTo(4.0)
assertThat(get("actualProductId")?.asString).isEqualTo(mediaProduct1.productId)
assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct1.sourceType)
assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct1.sourceId)
with(get("actions").asJsonArray) {
val firstStopAction =
gson.fromJson(this[0], PlaybackSession.Payload.Action::class.java)
val firstStartAction =
gson.fromJson(this[1], PlaybackSession.Payload.Action::class.java)
val perfectFirstResumeTimestamp = firstStopAction.timestamp
val secondStopAction =
gson.fromJson(this[2], PlaybackSession.Payload.Action::class.java)
val secondStartAction =
gson.fromJson(this[3], PlaybackSession.Payload.Action::class.java)
val perfectSecondResumeTimestamp = secondStopAction.timestamp
assertThat(firstStopAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_STOP)
assertThat(firstStopAction.assetPositionSeconds)
.isAssetPositionEqualTo(2.0)
assertThat(firstStartAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_START)
assertThat(firstStartAction.assetPositionSeconds)
.isAssetPositionEqualTo(3.0)
assertThat(firstStartAction.timestamp)
.isBetween(
perfectFirstResumeTimestamp - 500,
perfectFirstResumeTimestamp + 500,
)
assertThat(secondStopAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_STOP)
assertThat(secondStopAction.assetPositionSeconds)
.isAssetPositionEqualTo(3.0)
assertThat(secondStartAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_START)
assertThat(secondStartAction.assetPositionSeconds)
.isAssetPositionEqualTo(3.0)
assertThat(firstStartAction.timestamp)
.isBetween(
perfectSecondResumeTimestamp - 500,
perfectSecondResumeTimestamp + 500,
)
}
},
1 to {
assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0)
assertThat(get("endAssetPosition").asDouble)
.isAssetPositionEqualTo(MEDIA_PRODUCT_2_DURATION_SECONDS)
assertThat(get("actualProductId")?.asString).isEqualTo(mediaProduct2.productId)
assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct2.sourceType)
assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct2.sourceId)
with(get("actions").asJsonArray) {
val stopAction =
gson.fromJson(
this[0],
PlaybackSession.Payload.Action::class.java,
)
val startAction =
gson.fromJson(
this[1],
PlaybackSession.Payload.Action::class.java,
)
val perfectResumeTimestamp = stopAction.timestamp
assertThat(stopAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_STOP)
assertThat(stopAction.assetPositionSeconds).isAssetPositionEqualTo(0.0)
assertThat(startAction.actionType)
.isEqualTo(PlaybackSession.Payload.Action.Type.PLAYBACK_START)
assertThat(startAction.assetPositionSeconds).isAssetPositionEqualTo(58.0)
assertThat(startAction.timestamp)
.isBetween(perfectResumeTimestamp - 500, perfectResumeTimestamp + 500)
}
},
1 to {
assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0)
assertThat(get("endAssetPosition").asDouble).isAssetPositionEqualTo(1.0)
assertThat(get("actualProductId")?.asString).isEqualTo(mediaProduct2.productId)
assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct2.sourceType)
assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct2.sourceId)
assertThat(get("actions").asJsonArray).isEmpty()
},
)
}

@Suppress("CyclomaticComplexMethod")
@Test
fun playSequentiallyWithoutRepeatOne() = runTest {
val gson = Gson()
val payloadCaptor = argumentCaptor<String>()

player.playbackEngine.load(mediaProduct1)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(8.seconds) {
player.playbackEngine.events.filter { it is Event.MediaProductTransition }.first()
}
delay(2.seconds)
while (player.playbackEngine.assetPosition < 2) {
delay(10.milliseconds)
}
player.playbackEngine.load(mediaProduct2)
player.playbackEngine.play()
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, times(2)).sendEvent(
eq("playback_session"),
eq(ConsentCategory.NECESSARY),
payloadCaptor.capture(),
eq(emptyMap()),
)
payloadCaptor.allValues.map {
gson.fromJson(it, JsonObject::class.java)["payload"].asJsonObject
}.combinedPassAllOf(
1 to {
assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0)
assertThat(get("endAssetPosition").asDouble).isAssetPositionEqualTo(2.0)
assertThat(get("actualProductId")?.asString).isEqualTo(mediaProduct1.productId)
assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct1.sourceType)
assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct1.sourceId)
assertThat(get("actions").asJsonArray).isEmpty()
},
1 to {
assertThat(get("startAssetPosition").asDouble).isAssetPositionEqualTo(0.0)
assertThat(get("endAssetPosition").asDouble).isAssetPositionEqualTo(1.0)
assertThat(get("actualProductId")?.asString).isEqualTo(mediaProduct2.productId)
assertThat(get("sourceType")?.asString).isEqualTo(mediaProduct2.sourceType)
assertThat(get("sourceId")?.asString).isEqualTo(mediaProduct2.sourceId)
assertThat(get("actions").asJsonArray).isEmpty()
},
)
}
}