Skip to content

Commit 1707a2d

Browse files
committed
Add TestPillarboxRunHelper.runUntilIsPlaying to more closely match the original test implementation
1 parent 65d4da1 commit 1707a2d

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPillarboxRunHelper.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object TestPillarboxRunHelper {
3030
* Runs tasks of the main [Looper] until [Player.Listener.onEvents] matches the
3131
* expected state or a playback error occurs.
3232
*
33-
* <p>If a playback error occurs it will be thrown wrapped in an [IllegalStateException].
33+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
3434
*
3535
* @param player The [Player].
3636
* @param expectedEvents The expected [Player.Event]. If empty, waits until the first [Player.Listener.onEvents].
@@ -62,7 +62,7 @@ object TestPillarboxRunHelper {
6262
/**
6363
* Runs tasks of the main Looper until [Player.Listener.onPlaybackParametersChanged] is called or a playback error occurs.
6464
*
65-
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
65+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
6666
*
6767
* @param player The [Player].
6868
* @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded.
@@ -105,4 +105,36 @@ object TestPillarboxRunHelper {
105105
player.currentPosition >= position.inWholeMilliseconds
106106
}
107107
}
108+
109+
/**
110+
* Run and wait until [Player.isPlaying] is [isPlaying].
111+
112+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
113+
*
114+
* @param player The [Player].
115+
* @param isPlaying The expected value of [Player.isPlaying].
116+
117+
* @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded.
118+
*/
119+
@Throws(TimeoutException::class)
120+
fun runUntilIsPlaying(player: Player, isPlaying: Boolean) {
121+
verifyMainTestThread(player)
122+
if (player is ExoPlayer) {
123+
verifyPlaybackThreadIsAlive(player)
124+
}
125+
val receivedCallback = AtomicBoolean(false)
126+
val listener = object : Player.Listener {
127+
override fun onIsPlayingChanged(actual: Boolean) {
128+
if (actual == isPlaying) {
129+
receivedCallback.set(true)
130+
}
131+
}
132+
}
133+
player.addListener(listener)
134+
RobolectricUtil.runMainLooperUntil { receivedCallback.get() || player.playerError != null }
135+
player.removeListener(listener)
136+
if (player.playerError != null) {
137+
throw IllegalStateException(player.playerError)
138+
}
139+
}
108140
}

pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/IsPlayingAllTypeOfContentTest.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import android.os.Looper
99
import androidx.media3.common.MediaItem
1010
import androidx.media3.common.Player
1111
import androidx.media3.test.utils.FakeClock
12-
import androidx.media3.test.utils.robolectric.TestPlayerRunHelper
1312
import androidx.test.core.app.ApplicationProvider
13+
import ch.srgssr.pillarbox.player.test.utils.TestPillarboxRunHelper
1414
import org.junit.runner.RunWith
1515
import org.robolectric.ParameterizedRobolectricTestRunner
1616
import org.robolectric.ParameterizedRobolectricTestRunner.Parameters
@@ -24,7 +24,7 @@ import kotlin.test.assertTrue
2424

2525
@RunWith(ParameterizedRobolectricTestRunner::class)
2626
class IsPlayingAllTypeOfContentTest(
27-
private val urlToTest: String
27+
private val urlToTest: String,
2828
) {
2929
private lateinit var player: PillarboxExoPlayer
3030

@@ -51,12 +51,7 @@ class IsPlayingAllTypeOfContentTest(
5151
player.prepare()
5252
player.play()
5353

54-
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)
55-
56-
// Make test flaky because dependant of internet
57-
if (player.playerError != null) {
58-
throw IllegalStateException(player.playerError)
59-
}
54+
TestPillarboxRunHelper.runUntilIsPlaying(player, isPlaying = true)
6055

6156
assertEquals(Player.STATE_READY, player.playbackState)
6257
assertTrue(player.isPlaying)

0 commit comments

Comments
 (0)