diff --git a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPillarboxRunHelper.kt b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPillarboxRunHelper.kt index c8c463a9b..41e30e5c2 100644 --- a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPillarboxRunHelper.kt +++ b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPillarboxRunHelper.kt @@ -29,7 +29,7 @@ object TestPillarboxRunHelper { * Runs tasks of the main Looper until [Player.Listener.onEvents] matches the * expected state or a playback error occurs. * - *
If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}. + * If a playback error occurs, it will be thrown wrapped in an [IllegalStateException]. * * @param player The [Player]. * @param expectedEvent The expected [Player.Event] if null wait until first [Player.Listener.onEvents]. @@ -60,7 +60,7 @@ object TestPillarboxRunHelper { /** * Runs tasks of the main Looper until [Player.Listener.onPlaybackParametersChanged] is called or a playback error occurs. * - *
If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}. + * If a playback error occurs, it will be thrown wrapped in an [IllegalStateException]. * * @param player The [Player]. * @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded. @@ -103,4 +103,36 @@ object TestPillarboxRunHelper { player.currentPosition >= position.inWholeMilliseconds } } + + /** + * Run and wait until [Player.isPlaying] is [isPlaying]. + + * If a playback error occurs, it will be thrown wrapped in an [IllegalStateException]. + * + * @param player The [Player]. + * @param isPlaying The expected value of [Player.isPlaying]. + + * @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded. + */ + @Throws(TimeoutException::class) + fun runUntilIsPlaying(player: Player, isPlaying: Boolean) { + verifyMainTestThread(player) + if (player is ExoPlayer) { + verifyPlaybackThreadIsAlive(player) + } + val receivedCallback = AtomicBoolean(false) + val listener = object : Player.Listener { + override fun onIsPlayingChanged(actual: Boolean) { + if (actual == isPlaying) { + receivedCallback.set(true) + } + } + } + player.addListener(listener) + RobolectricUtil.runMainLooperUntil { receivedCallback.get() || player.playerError != null } + player.removeListener(listener) + if (player.playerError != null) { + throw IllegalStateException(player.playerError) + } + } } diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/IsPlayingAllTypeOfContentTest.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/IsPlayingAllTypeOfContentTest.kt index edb635fd2..b8ef3d9cb 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/IsPlayingAllTypeOfContentTest.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/IsPlayingAllTypeOfContentTest.kt @@ -9,8 +9,8 @@ import android.os.Looper 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 ch.srgssr.pillarbox.player.test.utils.TestPillarboxRunHelper import org.junit.runner.RunWith import org.robolectric.ParameterizedRobolectricTestRunner import org.robolectric.ParameterizedRobolectricTestRunner.Parameters @@ -24,7 +24,7 @@ import kotlin.test.assertTrue @RunWith(ParameterizedRobolectricTestRunner::class) class IsPlayingAllTypeOfContentTest( - private val urlToTest: String + private val urlToTest: String, ) { private lateinit var player: PillarboxExoPlayer @@ -49,12 +49,7 @@ class IsPlayingAllTypeOfContentTest( player.prepare() player.play() - TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY) - - // Make test flaky because dependant of internet - if (player.playerError != null) { - throw IllegalStateException(player.playerError) - } + TestPillarboxRunHelper.runUntilIsPlaying(player, isPlaying = true) assertEquals(Player.STATE_READY, player.playbackState) assertTrue(player.isPlaying)