Skip to content

Commit

Permalink
Fix MonitoringTest by using PillarboxExoPlayer directly (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 authored Sep 18, 2024
1 parent 231c650 commit 6d7053c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class CommandersActStreaming(
private val commandersAct: CommandersAct,
private val player: ExoPlayer,
var currentData: CommandersActTracker.Data,
private val coroutineContext: CoroutineContext,
coroutineContext: CoroutineContext,
) : AnalyticsListener {

private enum class State {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package ch.srgssr.pillarbox.core.business.tracker.commandersact

import android.content.Context
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import androidx.media3.common.C
Expand All @@ -13,6 +14,7 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.TrackGroup
import androidx.media3.common.Tracks
import androidx.media3.exoplayer.ExoPlayer
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.srgssr.pillarbox.analytics.commandersact.CommandersAct
import ch.srgssr.pillarbox.analytics.commandersact.MediaEventType
Expand Down Expand Up @@ -262,6 +264,7 @@ class CommandersActStreamingTest {
): ExoPlayer {
return mockk<ExoPlayer> {
val player = this
val looper = ApplicationProvider.getApplicationContext<Context>().mainLooper

every { player.playWhenReady } returns true
every { player.isPlaying } returns isPlaying
Expand All @@ -272,6 +275,7 @@ class CommandersActStreamingTest {
every { player.deviceVolume } returns deviceVolume
every { player.duration } returns duration
every { player.currentTracks } returns currentTracks
every { player.applicationLooper } returns looper
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ch.srgssr.pillarbox.core.business.tracker.comscore

import android.content.Context
import android.os.Looper
import android.view.SurfaceView
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
Expand All @@ -25,12 +26,15 @@ import com.comscore.streaming.AssetMetadata
import com.comscore.streaming.StreamingAnalytics
import io.mockk.Called
import io.mockk.MockKVerificationScope
import io.mockk.clearAllMocks
import io.mockk.confirmVerified
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifyOrder
import org.junit.runner.RunWith
import org.robolectric.Shadows.shadowOf
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Ignore
import kotlin.test.Test
Expand Down Expand Up @@ -69,6 +73,13 @@ class ComScoreTrackerIntegrationTest {
)
}

@AfterTest
fun tearDown() {
clearAllMocks()
player.release()
shadowOf(Looper.getMainLooper()).idle()
}

@Test
fun `player unprepared`() {
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_IDLE)
Expand All @@ -95,16 +106,14 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifyEndEvent()
verifyPlayerInformation()
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
}
Expand Down Expand Up @@ -181,8 +190,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifyPauseEvent()
}
Expand All @@ -202,8 +210,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
}
confirmVerified(streamingAnalytics)
}
Expand All @@ -222,8 +229,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
}
confirmVerified(streamingAnalytics)
Expand All @@ -248,8 +254,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifyPauseEvent()
}
Expand Down Expand Up @@ -280,8 +285,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifyPauseEvent()
verifyPlayEvent()
Expand All @@ -308,8 +312,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifyEndEvent()
}
Expand All @@ -334,12 +337,10 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
verifySeekStart()
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifyPlayEvent()
}
confirmVerified(streamingAnalytics)
Expand All @@ -353,13 +354,13 @@ class ComScoreTrackerIntegrationTest {

TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

verifyLiveInformation(atLeast = 0)
verifyOrder {
verifyPlayerInformation()
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
}
confirmVerified(streamingAnalytics)
}
Expand Down Expand Up @@ -389,8 +390,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
}
confirmVerified(streamingAnalytics)
}
Expand All @@ -408,8 +408,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
}
Expand All @@ -430,8 +429,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 2f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
}
Expand All @@ -454,8 +452,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
verifyPlaybackRate(playbackRate = 2f)
Expand All @@ -481,8 +478,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
verifyPauseEvent()
Expand Down Expand Up @@ -513,8 +509,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
verifyPauseEvent()
Expand Down Expand Up @@ -542,8 +537,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
verifyEndEvent()
Expand All @@ -568,14 +562,12 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(0L)
verifyPlayEvent()
verifySeekStart()
verifySeekEvent(30_000L)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
verifySeekEvent(30_000L)
verifyPlayEvent()
}
Expand All @@ -595,8 +587,7 @@ class ComScoreTrackerIntegrationTest {
verifyCreatePlaybackSession()
verifyMetadata()
verifyPlaybackRate(playbackRate = 1f)
verifyBufferStartEvent()
verifyBufferStopEvent()
verifyBufferEvents()
}
confirmVerified(streamingAnalytics)
}
Expand Down Expand Up @@ -653,12 +644,8 @@ class ComScoreTrackerIntegrationTest {
}

@Suppress("UnusedReceiverParameter")
private fun MockKVerificationScope.verifyBufferStartEvent() {
private fun MockKVerificationScope.verifyBufferEvents() {
streamingAnalytics.notifyBufferStart()
}

@Suppress("UnusedReceiverParameter")
private fun MockKVerificationScope.verifyBufferStopEvent() {
streamingAnalytics.notifyBufferStop()
}

Expand All @@ -672,8 +659,8 @@ class ComScoreTrackerIntegrationTest {
streamingAnalytics.notifySeekStart()
}

private fun verifyLiveInformation() {
verify {
private fun verifyLiveInformation(atLeast: Int = 1) {
verify(atLeast = atLeast) {
streamingAnalytics.setDvrWindowLength(any())
streamingAnalytics.startFromDvrWindowOffset(any())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class PillarboxExoPlayer internal constructor(
internal val sessionManager = PlaybackSessionManager()
private val window = Window()

@VisibleForTesting
internal val monitoring = Monitoring(
context = context,
player = this,
metricsCollector = metricsCollector,
messageHandler = monitoringMessageHandler,
sessionManager = sessionManager,
coroutineContext = coroutineContext,
)

override var smoothSeekingEnabled: Boolean = false
set(value) {
if (value != field) {
Expand Down Expand Up @@ -138,14 +148,6 @@ class PillarboxExoPlayer internal constructor(
init {
sessionManager.setPlayer(this)
metricsCollector.setPlayer(this)
Monitoring(
context = context,
player = this,
metricsCollector = metricsCollector,
messageHandler = monitoringMessageHandler,
sessionManager = sessionManager,
coroutineContext = coroutineContext,
)
addListener(analyticsCollector)
exoPlayer.addListener(ComponentListener())
itemPillarboxDataTracker.addCallback(timeRangeTracker)
Expand Down
Loading

0 comments on commit 6d7053c

Please sign in to comment.