@@ -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}
0 commit comments