From 76ad0bc4ff406fc2adef579330f93d67f47dfdde Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 14 Apr 2020 10:50:27 +0100 Subject: [PATCH] Fix NPE in AdaptiveBitrateTest due to missing Looper. The new TestExoPlayer.Builder asserts that a Looper is present in the constructor, although it can be set later for cases where no such Looper exists. PiperOrigin-RevId: 306403491 --- .../exoplayer2/testutil/TestExoPlayer.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestExoPlayer.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestExoPlayer.java index d0dc1dfac4..ad36635b92 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestExoPlayer.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestExoPlayer.java @@ -41,6 +41,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** * Utilities to write unit/integration tests with a SimpleExoPlayer instance that uses fake @@ -77,7 +78,7 @@ public class TestExoPlayer { @Nullable private Renderer[] renderers; @Nullable private RenderersFactory renderersFactory; private boolean useLazyPreparation; - private Looper looper; + private @MonotonicNonNull Looper looper; public Builder(Context context) { this.context = context; @@ -85,7 +86,10 @@ public class TestExoPlayer { trackSelector = new DefaultTrackSelector(context); loadControl = new DefaultLoadControl(); bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build(); - looper = Assertions.checkNotNull(Looper.myLooper()); + @Nullable Looper myLooper = Looper.myLooper(); + if (myLooper != null) { + looper = myLooper; + } } /** @@ -234,7 +238,11 @@ public class TestExoPlayer { return this; } - /** Returns the {@link Looper} that will be used by the player. */ + /** + * Returns the {@link Looper} that will be used by the player, or null if no {@link Looper} has + * been set yet and no default is available. + */ + @Nullable public Looper getLooper() { return looper; } @@ -245,6 +253,8 @@ public class TestExoPlayer { * @return The built {@link ExoPlayerTestRunner}. */ public SimpleExoPlayer build() { + Assertions.checkNotNull( + looper, "TestExoPlayer builder run on a thread without Looper and no Looper specified."); // Do not update renderersFactory and renderers here, otherwise their getters may // return different values before and after build() is called, making them confusing. RenderersFactory playerRenderersFactory = renderersFactory;