Skip to content

Commit 90bf6df

Browse files
committed
Add TestPillarboxRunHelper.runUntilIsPlaying to more closely match the original test implementation
1 parent 07215c2 commit 90bf6df

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
@@ -29,7 +29,7 @@ object TestPillarboxRunHelper {
2929
* Runs tasks of the main Looper until [Player.Listener.onEvents] matches the
3030
* expected state or a playback error occurs.
3131
*
32-
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
32+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
3333
*
3434
* @param player The [Player].
3535
* @param expectedEvent The expected [Player.Event] if null wait until first [Player.Listener.onEvents].
@@ -60,7 +60,7 @@ object TestPillarboxRunHelper {
6060
/**
6161
* Runs tasks of the main Looper until [Player.Listener.onPlaybackParametersChanged] is called or a playback error occurs.
6262
*
63-
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
63+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
6464
*
6565
* @param player The [Player].
6666
* @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded.
@@ -103,4 +103,36 @@ object TestPillarboxRunHelper {
103103
player.currentPosition >= position.inWholeMilliseconds
104104
}
105105
}
106+
107+
/**
108+
* Run and wait until [Player.isPlaying] is [isPlaying].
109+
110+
* If a playback error occurs, it will be thrown wrapped in an [IllegalStateException].
111+
*
112+
* @param player The [Player].
113+
* @param isPlaying The expected value of [Player.isPlaying].
114+
115+
* @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded.
116+
*/
117+
@Throws(TimeoutException::class)
118+
fun runUntilIsPlaying(player: Player, isPlaying: Boolean) {
119+
verifyMainTestThread(player)
120+
if (player is ExoPlayer) {
121+
verifyPlaybackThreadIsAlive(player)
122+
}
123+
val receivedCallback = AtomicBoolean(false)
124+
val listener = object : Player.Listener {
125+
override fun onIsPlayingChanged(actual: Boolean) {
126+
if (actual == isPlaying) {
127+
receivedCallback.set(true)
128+
}
129+
}
130+
}
131+
player.addListener(listener)
132+
RobolectricUtil.runMainLooperUntil { receivedCallback.get() || player.playerError != null }
133+
player.removeListener(listener)
134+
if (player.playerError != null) {
135+
throw IllegalStateException(player.playerError)
136+
}
137+
}
106138
}

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

@@ -49,12 +49,7 @@ class IsPlayingAllTypeOfContentTest(
4949
player.prepare()
5050
player.play()
5151

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

5954
assertEquals(Player.STATE_READY, player.playbackState)
6055
assertTrue(player.isPlaying)

0 commit comments

Comments
 (0)