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