From b1ba2cea68ac52a1c37bed3b67e6aa86aad42034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Wed, 18 Dec 2024 10:39:21 +0100 Subject: [PATCH] Add an helper method to create an SRG test player --- .../core/business/PillarboxTestPlayer.kt | 36 +++++++++++++++++++ .../CommandersActTrackerIntegrationTest.kt | 16 ++------- .../ComScoreTrackerIntegrationTest.kt | 13 ++----- 3 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/PillarboxTestPlayer.kt diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/PillarboxTestPlayer.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/PillarboxTestPlayer.kt new file mode 100644 index 000000000..d338f0c47 --- /dev/null +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/PillarboxTestPlayer.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.core.business + +import android.content.Context +import androidx.media3.common.C +import androidx.media3.exoplayer.DefaultLoadControl +import androidx.media3.test.utils.FakeClock +import androidx.test.core.app.ApplicationProvider +import ch.srgssr.pillarbox.player.PillarboxDsl +import ch.srgssr.pillarbox.player.PillarboxExoPlayer +import kotlin.coroutines.EmptyCoroutineContext + +/** + * Pillarbox ExoPlayer + * + * @param context The [Context], by default [ApplicationProvider.getApplicationContext] + * @param block The block to further configure the [PillarboxExoPlayer]. + * @return [PillarboxExoPlayer] configured for tests. + */ +@PillarboxDsl +fun PillarboxExoPlayer(context: Context = ApplicationProvider.getApplicationContext(), block: SRG.Builder.() -> Unit = {}): PillarboxExoPlayer { + return PillarboxExoPlayer(context, SRG) { + loadControl(DefaultLoadControl()) + clock(FakeClock(true)) + coroutineContext(EmptyCoroutineContext) + block() + }.apply { + // FIXME Investigate why we need to disable the image track in tests + trackSelectionParameters = trackSelectionParameters.buildUpon() + .setTrackTypeDisabled(C.TRACK_TYPE_IMAGE, true) + .build() + } +} diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt index 4944b9696..b32e2030a 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt @@ -6,7 +6,6 @@ package ch.srgssr.pillarbox.core.business.tracker.commandersact import android.content.Context import android.os.Looper -import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.Player import androidx.media3.exoplayer.ExoPlayer @@ -42,7 +41,6 @@ import kotlinx.coroutines.test.advanceTimeBy import kotlinx.coroutines.test.runTest import org.junit.runner.RunWith import org.robolectric.Shadows.shadowOf -import kotlin.coroutines.EmptyCoroutineContext import kotlin.math.abs import kotlin.test.AfterTest import kotlin.test.BeforeTest @@ -70,21 +68,13 @@ class CommandersActTrackerIntegrationTest { testDispatcher = UnconfinedTestDispatcher() val context = ApplicationProvider.getApplicationContext() - val mediaCompositionWithFallbackService = LocalMediaCompositionWithFallbackService(context) - player = PillarboxExoPlayer(context) { + player = PillarboxExoPlayer { + clock(clock) srgAssetLoader(context) { - mediaCompositionService(mediaCompositionWithFallbackService) + mediaCompositionService(LocalMediaCompositionWithFallbackService(context)) commanderActTrackerFactory(CommandersActTracker.Factory(commandersAct = commandersAct, coroutineContext = testDispatcher)) comscoreTrackerFactory(mockk(relaxed = true)) } - clock(clock) - // Use other CoroutineContext to avoid infinite loop because Heartbeat is also running in Pillarbox. - coroutineContext(EmptyCoroutineContext) - }.apply { - // FIXME Investigate why we need to disable the image track in tests - trackSelectionParameters = trackSelectionParameters.buildUpon() - .setTrackTypeDisabled(C.TRACK_TYPE_IMAGE, true) - .build() } } diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTrackerIntegrationTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTrackerIntegrationTest.kt index 8709c23fd..a20d35174 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTrackerIntegrationTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/comscore/ComScoreTrackerIntegrationTest.kt @@ -9,7 +9,6 @@ import android.os.Looper import android.view.SurfaceView import android.view.ViewGroup import androidx.core.view.updateLayoutParams -import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.Player import androidx.media3.test.utils.FakeClock @@ -33,7 +32,6 @@ 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 @@ -57,20 +55,13 @@ class ComScoreTrackerIntegrationTest { ComScoreTracker(streamingAnalytics) } val context = ApplicationProvider.getApplicationContext() - val mediaCompositionWithFallbackService = LocalMediaCompositionWithFallbackService(context) - player = PillarboxExoPlayer(context) { + player = PillarboxExoPlayer { clock(clock) - coroutineContext(EmptyCoroutineContext) srgAssetLoader(context) { - mediaCompositionService(mediaCompositionWithFallbackService) + mediaCompositionService(LocalMediaCompositionWithFallbackService(context)) comscoreTrackerFactory(comScoreFactory) commanderActTrackerFactory(mockk(relaxed = true)) } - }.apply { - // FIXME Investigate why we need to disable the image track in tests - trackSelectionParameters = trackSelectionParameters.buildUpon() - .setTrackTypeDisabled(C.TRACK_TYPE_IMAGE, true) - .build() } }