Skip to content

Commit

Permalink
Add TestPillarboxRunHelper.runUntilIsPlaying to more closely match …
Browse files Browse the repository at this point in the history
…the original test implementation
  • Loading branch information
MGaetan89 committed Jul 31, 2024
1 parent f686a03 commit 2063a86
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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].
Expand Down Expand Up @@ -60,7 +60,7 @@ object TestPillarboxRunHelper {
/**
* Runs tasks of the main Looper until [Player.Listener.onPlaybackParametersChanged] is called or a playback error occurs.
*
* <p>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.
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 2063a86

Please sign in to comment.