Skip to content

Commit

Permalink
Add tests to CommandersActTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Feb 14, 2024
1 parent ca69d02 commit 2770bb7
Showing 1 changed file with 296 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.core.business.tracker.commandersact

import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.test.utils.FakeClock
import androidx.media3.test.utils.robolectric.TestPlayerRunHelper
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.srgssr.pillarbox.analytics.commandersact.CommandersAct
import ch.srgssr.pillarbox.core.business.DefaultPillarbox
import ch.srgssr.pillarbox.core.business.MediaCompositionMediaItemSource
import ch.srgssr.pillarbox.core.business.MediaItemUrn
import ch.srgssr.pillarbox.core.business.integrationlayer.data.isValidMediaUrn
import ch.srgssr.pillarbox.core.business.integrationlayer.service.DefaultMediaCompositionDataSource
import ch.srgssr.pillarbox.core.business.tracker.DefaultMediaItemTrackerRepository
import ch.srgssr.pillarbox.core.business.tracker.comscore.ComScoreTracker
import ch.srgssr.pillarbox.player.data.MediaItemSource
import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerRepository
import io.mockk.Called
import io.mockk.confirmVerified
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifyOrder
import org.junit.runner.RunWith
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@RunWith(AndroidJUnit4::class)
class CommandersActTrackerIntegrationTest {
private lateinit var clock: FakeClock
private lateinit var commandersAct: CommandersAct
private lateinit var player: Player

@BeforeTest
fun setup() {
clock = FakeClock(true)
commandersAct = mockk(relaxed = true)

val mediaItemTrackerRepository = DefaultMediaItemTrackerRepository(
trackerRepository = MediaItemTrackerRepository(),
commandersAct = commandersAct,
)
mediaItemTrackerRepository.registerFactory(ComScoreTracker::class.java) {
mockk<ComScoreTracker>(relaxed = true)
}

val urnMediaItemSource = MediaCompositionMediaItemSource(
mediaCompositionDataSource = DefaultMediaCompositionDataSource()
)
val mediaItemSource = object : MediaItemSource {
override suspend fun loadMediaItem(mediaItem: MediaItem): MediaItem {
return if (mediaItem.mediaId.isValidMediaUrn()) {
urnMediaItemSource.loadMediaItem(mediaItem)
} else {
mediaItem
}
}
}

player = DefaultPillarbox(
context = ApplicationProvider.getApplicationContext(),
mediaItemTrackerRepository = mediaItemTrackerRepository,
mediaItemSource = mediaItemSource,
clock = clock,
)
}

@Test
fun `player unprepared`() {
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_IDLE)

verify { commandersAct wasNot Called }
}

@Test
fun `player prepared and playing, changing media item`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

player.setMediaItem(MediaItemUrn(URN_NOT_LIVE_VIDEO))
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `audio URN don't send any analytics`() {
player.setMediaItem(MediaItemUrn(URN_AUDIO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `URL don't send any analytics`() {
player.setMediaItem(MediaItem.fromUri(URL))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verify { commandersAct wasNot Called }
}

@Test
fun `player prepared but not playing`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared and playing`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared and playing, change playback speed`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true
player.setPlaybackSpeed(2f)

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared and playing, change playback speed while playing`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

clock.advanceTime(5.minutes.inWholeMilliseconds)
player.setPlaybackSpeed(2f)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared, playing and paused`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

clock.advanceTime(2.minutes.inWholeMilliseconds)
player.pause()

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared, playing, paused, playing again`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

clock.advanceTime(2.minutes.inWholeMilliseconds)
player.pause()

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

clock.advanceTime(4.minutes.inWholeMilliseconds)
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared, playing and stopped`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

clock.advanceTime(2.minutes.inWholeMilliseconds)
player.stop()

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_IDLE)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared, playing and seeking`() {
player.setMediaItem(MediaItemUrn(URN_NOT_LIVE_VIDEO))
player.prepare()
player.playWhenReady = true

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

player.seekTo(30.seconds.inWholeMilliseconds)

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared and seek`() {
player.setMediaItem(MediaItemUrn(URN_NOT_LIVE_VIDEO))
player.prepare()
player.seekTo(3.minutes.inWholeMilliseconds)

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyOrder {
commandersAct.enableRunningInBackground()
}
confirmVerified(commandersAct)
}

@Test
fun `player prepared and stopped`() {
player.setMediaItem(MediaItemUrn(URN_LIVE_VIDEO))
player.prepare()
player.stop()

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_IDLE)

verify { commandersAct wasNot Called }
}

private companion object {
private const val URL = "https://swi-vod.akamaized.net/videoJson/47603186/master.m3u8"
private const val URN_AUDIO = "urn:rts:audio:13598743"
private const val URN_LIVE_VIDEO = "urn:rts:video:8841634"
private const val URN_NOT_LIVE_VIDEO = "urn:rsi:video:15916771"
}
}

0 comments on commit 2770bb7

Please sign in to comment.