From 32e6cf55092b514bfcf21c9834891f71ca641877 Mon Sep 17 00:00:00 2001 From: tonihei Date: Mon, 22 Jan 2018 08:48:09 -0800 Subject: [PATCH] Remove further potential flakiness from ExoPlayerTest. These were caused by two issues: 1. The FakeMediaSource can be updated with a new timeline. The setNewSourceInfo is called from a different thread than prepareSource and both access local variables without synchronization. 2. For multi-window playback, the FakeRenderer claims that isReady and isEnded are both set to false if it read the end of the stream. However isReady should be true because it is able to "render" its data until the end of the stream. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=182785169 --- .../android/exoplayer2/ExoPlayerTest.java | 2 +- .../DynamicConcatenatingMediaSourceTest.java | 4 +-- .../exoplayer2/testutil/FakeMediaSource.java | 29 ++++++++++++++----- .../exoplayer2/testutil/FakeRenderer.java | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/ExoPlayerTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/ExoPlayerTest.java index 4fea634ec3..ba5733b445 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/ExoPlayerTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/ExoPlayerTest.java @@ -1077,7 +1077,7 @@ public final class ExoPlayerTest extends TestCase { new Runnable() { @Override public void run() { - mediaSource.setNewSourceInfo(timeline, /* manifest= */ null); + mediaSource.setNewSourceInfo(timeline, /* newManifest= */ null); } }) .waitForPlaybackState(Player.STATE_IDLE) diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/source/DynamicConcatenatingMediaSourceTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/source/DynamicConcatenatingMediaSourceTest.java index 5fa158725d..2a8709273e 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/source/DynamicConcatenatingMediaSourceTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/source/DynamicConcatenatingMediaSourceTest.java @@ -234,7 +234,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase { lazySources[1].setNewSourceInfo(createFakeTimeline(8), null); } }); - timeline = testRunner.assertTimelineChange(); + timeline = testRunner.assertTimelineChangeBlocking(); TimelineAsserts.assertPeriodCounts(timeline, 1, 9); TimelineAsserts.assertWindowIds(timeline, 111, 999); TimelineAsserts.assertWindowIsDynamic(timeline, false, false); @@ -272,7 +272,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase { lazySources[3].setNewSourceInfo(createFakeTimeline(7), null); } }); - timeline = testRunner.assertTimelineChange(); + timeline = testRunner.assertTimelineChangeBlocking(); TimelineAsserts.assertPeriodCounts(timeline, 8, 1, 2, 9); TimelineAsserts.assertWindowIds(timeline, 888, 111, 222, 999); TimelineAsserts.assertWindowIsDynamic(timeline, false, false, false, false); diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java index f4c8435801..fba82ca079 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeMediaSource.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.testutil; +import android.os.Handler; import android.support.annotation.Nullable; import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.Format; @@ -35,15 +36,16 @@ import junit.framework.Assert; */ public class FakeMediaSource implements MediaSource { - private final Object manifest; private final TrackGroupArray trackGroupArray; private final ArrayList activeMediaPeriods; private final ArrayList createdMediaPeriods; protected Timeline timeline; + private Object manifest; private boolean preparedSource; private boolean releasedSource; private Listener listener; + private Handler sourceInfoRefreshHandler; /** * Creates a {@link FakeMediaSource}. This media source creates {@link FakeMediaPeriod}s with a @@ -71,10 +73,12 @@ public class FakeMediaSource implements MediaSource { } @Override - public void prepareSource(ExoPlayer player, boolean isTopLevelSource, Listener listener) { + public synchronized void prepareSource( + ExoPlayer player, boolean isTopLevelSource, Listener listener) { Assert.assertFalse(preparedSource); preparedSource = true; this.listener = listener; + sourceInfoRefreshHandler = new Handler(); if (timeline != null) { listener.onSourceInfoRefreshed(this, timeline, manifest); } @@ -117,11 +121,22 @@ public class FakeMediaSource implements MediaSource { * Sets a new timeline and manifest. If the source is already prepared, this triggers a source * info refresh message being sent to the listener. */ - public void setNewSourceInfo(Timeline newTimeline, Object manifest) { - Assert.assertFalse(releasedSource); - this.timeline = newTimeline; - if (preparedSource) { - listener.onSourceInfoRefreshed(this, timeline, manifest); + public synchronized void setNewSourceInfo(final Timeline newTimeline, final Object newManifest) { + if (sourceInfoRefreshHandler != null) { + sourceInfoRefreshHandler.post( + new Runnable() { + @Override + public void run() { + Assert.assertFalse(releasedSource); + Assert.assertTrue(preparedSource); + timeline = newTimeline; + manifest = newManifest; + listener.onSourceInfoRefreshed(FakeMediaSource.this, timeline, manifest); + } + }); + } else { + timeline = newTimeline; + manifest = newManifest; } } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeRenderer.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeRenderer.java index 75adcf9018..e500ae7b60 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeRenderer.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeRenderer.java @@ -73,7 +73,7 @@ public class FakeRenderer extends BaseRenderer { } } } - isReady = buffer.timeUs >= positionUs; + isReady = buffer.timeUs >= positionUs || hasReadStreamToEnd(); } @Override