Skip to content

Commit

Permalink
Throw exception if a released player is passed to TestPlayerRunHelper
Browse files Browse the repository at this point in the history
I considered moving this enforcement inside the ExoPlayerImpl
implementation, but it might lead to app crashes in cases that apps
(incorrectly) call a released player, but it wasn't actually causing a
problem.

PiperOrigin-RevId: 489233917
  • Loading branch information
icbaker authored and microkatz committed Nov 22, 2022
1 parent 0388631 commit cba65c8
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package androidx.media3.test.utils.robolectric;

import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.test.utils.robolectric.RobolectricUtil.runMainLooperUntil;

import android.os.Looper;
Expand Down Expand Up @@ -55,6 +56,9 @@ private TestPlayerRunHelper() {}
public static void runUntilPlaybackState(Player player, @Player.State int expectedState)
throws TimeoutException {
verifyMainTestThread(player);
if (player instanceof ExoPlayer) {
verifyPlaybackThreadIsAlive((ExoPlayer) player);
}
runMainLooperUntil(
() -> player.getPlaybackState() == expectedState || player.getPlayerError() != null);
if (player.getPlayerError() != null) {
Expand All @@ -76,6 +80,9 @@ public static void runUntilPlaybackState(Player player, @Player.State int expect
public static void runUntilPlayWhenReady(Player player, boolean expectedPlayWhenReady)
throws TimeoutException {
verifyMainTestThread(player);
if (player instanceof ExoPlayer) {
verifyPlaybackThreadIsAlive((ExoPlayer) player);
}
runMainLooperUntil(
() ->
player.getPlayWhenReady() == expectedPlayWhenReady || player.getPlayerError() != null);
Expand All @@ -98,6 +105,9 @@ public static void runUntilPlayWhenReady(Player player, boolean expectedPlayWhen
public static void runUntilTimelineChanged(Player player, Timeline expectedTimeline)
throws TimeoutException {
verifyMainTestThread(player);
if (player instanceof ExoPlayer) {
verifyPlaybackThreadIsAlive((ExoPlayer) player);
}
runMainLooperUntil(
() ->
expectedTimeline.equals(player.getCurrentTimeline())
Expand Down Expand Up @@ -151,6 +161,9 @@ public void onTimelineChanged(Timeline timeline, int reason) {
public static void runUntilPositionDiscontinuity(
Player player, @Player.DiscontinuityReason int expectedReason) throws TimeoutException {
verifyMainTestThread(player);
if (player instanceof ExoPlayer) {
verifyPlaybackThreadIsAlive((ExoPlayer) player);
}
AtomicBoolean receivedCallback = new AtomicBoolean(false);
Player.Listener listener =
new Player.Listener() {
Expand Down Expand Up @@ -180,6 +193,8 @@ public void onPositionDiscontinuity(
*/
public static ExoPlaybackException runUntilError(ExoPlayer player) throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);

runMainLooperUntil(() -> player.getPlayerError() != null);
return checkNotNull(player.getPlayerError());
}
Expand All @@ -199,6 +214,8 @@ public static ExoPlaybackException runUntilError(ExoPlayer player) throws Timeou
public static void runUntilSleepingForOffload(ExoPlayer player, boolean expectedSleepForOffload)
throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);

AtomicBoolean receiverCallback = new AtomicBoolean(false);
ExoPlayer.AudioOffloadListener listener =
new ExoPlayer.AudioOffloadListener() {
Expand Down Expand Up @@ -228,6 +245,8 @@ public void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload)
*/
public static void runUntilRenderedFirstFrame(ExoPlayer player) throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);

AtomicBoolean receivedCallback = new AtomicBoolean(false);
Player.Listener listener =
new Player.Listener() {
Expand Down Expand Up @@ -259,6 +278,7 @@ public void onRenderedFirstFrame() {
public static void playUntilPosition(ExoPlayer player, int mediaItemIndex, long positionMs)
throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);
Looper applicationLooper = Util.getCurrentOrMainLooper();
AtomicBoolean messageHandled = new AtomicBoolean(false);
player
Expand Down Expand Up @@ -319,6 +339,8 @@ public static void playUntilStartOfMediaItem(ExoPlayer player, int mediaItemInde
public static void runUntilPendingCommandsAreFullyHandled(ExoPlayer player)
throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);

// Send message to player that will arrive after all other pending commands. Thus, the message
// execution on the app thread will also happen after all other pending command
// acknowledgements have arrived back on the app thread.
Expand All @@ -336,4 +358,10 @@ private static void verifyMainTestThread(Player player) {
throw new IllegalStateException();
}
}

private static void verifyPlaybackThreadIsAlive(ExoPlayer player) {
checkState(
player.getPlaybackLooper().getThread().isAlive(),
"Playback thread is not alive, has the player been released?");
}
}

0 comments on commit cba65c8

Please sign in to comment.