Commit d99a6677 by ibaker Committed by christosts

Throw exception if a released player is passed to TestPlayerRunHelper

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
(cherry picked from commit d4c9199a)
parent 0d11d551
......@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.robolectric;
import static com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import android.os.Looper;
import com.google.android.exoplayer2.ExoPlaybackException;
......@@ -53,6 +54,9 @@ public class 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) {
......@@ -74,6 +78,9 @@ public class TestPlayerRunHelper {
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);
......@@ -96,6 +103,9 @@ public class TestPlayerRunHelper {
public static void runUntilTimelineChanged(Player player, Timeline expectedTimeline)
throws TimeoutException {
verifyMainTestThread(player);
if (player instanceof ExoPlayer) {
verifyPlaybackThreadIsAlive((ExoPlayer) player);
}
runMainLooperUntil(
() ->
expectedTimeline.equals(player.getCurrentTimeline())
......@@ -149,6 +159,9 @@ public class TestPlayerRunHelper {
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() {
......@@ -178,6 +191,8 @@ public class TestPlayerRunHelper {
*/
public static ExoPlaybackException runUntilError(ExoPlayer player) throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);
runMainLooperUntil(() -> player.getPlayerError() != null);
return checkNotNull(player.getPlayerError());
}
......@@ -197,6 +212,8 @@ public class TestPlayerRunHelper {
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() {
......@@ -226,6 +243,8 @@ public class TestPlayerRunHelper {
*/
public static void runUntilRenderedFirstFrame(ExoPlayer player) throws TimeoutException {
verifyMainTestThread(player);
verifyPlaybackThreadIsAlive(player);
AtomicBoolean receivedCallback = new AtomicBoolean(false);
Player.Listener listener =
new Player.Listener() {
......@@ -257,6 +276,7 @@ public class TestPlayerRunHelper {
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
......@@ -317,6 +337,8 @@ public class TestPlayerRunHelper {
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.
......@@ -334,4 +356,10 @@ public class TestPlayerRunHelper {
throw new IllegalStateException();
}
}
private static void verifyPlaybackThreadIsAlive(ExoPlayer player) {
checkState(
player.getPlaybackLooper().getThread().isAlive(),
"Playback thread is not alive, has the player been released?");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment