From 22ea9ed6875384800bcd86772709ee68c2ebdf67 Mon Sep 17 00:00:00 2001 From: tonihei Date: Wed, 2 Aug 2017 05:58:45 -0700 Subject: [PATCH] Clean-up ExoHostedTest/HostActivity. This change clearly seperates the hosting of a test (HostActivity) from the internals of hosting an ExoPlayer lifecycle (ExoHostedTest). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163967960 --- .../exoplayer2/testutil/ExoHostedTest.java | 36 ++++++++---- .../exoplayer2/testutil/HostActivity.java | 56 ++++++++----------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoHostedTest.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoHostedTest.java index 4aba23d691..77e197515b 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoHostedTest.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExoHostedTest.java @@ -73,7 +73,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener, private final long expectedPlayingTimeMs; private final DecoderCounters videoDecoderCounters; private final DecoderCounters audioDecoderCounters; - private final ConditionVariable playerFinished; + private final ConditionVariable testFinished; private ActionSchedule pendingSchedule; private Handler actionHandler; @@ -116,7 +116,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener, this.tag = tag; this.expectedPlayingTimeMs = expectedPlayingTimeMs; this.failOnPlayerError = failOnPlayerError; - this.playerFinished = new ConditionVariable(); + this.testFinished = new ConditionVariable(); this.videoDecoderCounters = new DecoderCounters(); this.audioDecoderCounters = new DecoderCounters(); } @@ -172,16 +172,13 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener, } @Override - public final boolean blockUntilEnded(long timeoutMs) { - return playerFinished.block(timeoutMs); + public final boolean blockUntilStopped(long timeoutMs) { + return testFinished.block(timeoutMs); } @Override - public final void onStop() { - actionHandler.removeCallbacksAndMessages(null); - sourceDurationMs = player.getDuration(); - player.release(); - player = null; + public final boolean forceStop() { + return stopTest(); } @Override @@ -222,7 +219,7 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener, playerWasPrepared |= playbackState != Player.STATE_IDLE; if (playbackState == Player.STATE_ENDED || (playbackState == Player.STATE_IDLE && playerWasPrepared)) { - playerFinished.open(); + stopTest(); } boolean playing = playWhenReady && playbackState == Player.STATE_READY; if (!this.playing && playing) { @@ -337,6 +334,25 @@ public abstract class ExoHostedTest implements HostedTest, Player.EventListener, // Internal logic + private boolean stopTest() { + if (player == null) { + return false; + } + actionHandler.removeCallbacksAndMessages(null); + sourceDurationMs = player.getDuration(); + player.release(); + player = null; + // We post opening of the finished condition so that any events posted to the main thread as a + // result of player.release() are guaranteed to be handled before the test returns. + actionHandler.post(new Runnable() { + @Override + public void run() { + testFinished.open(); + } + }); + return true; + } + protected DrmSessionManager buildDrmSessionManager(String userAgent) { // Do nothing. Interested subclasses may override. return null; diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/HostActivity.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/HostActivity.java index e1f7eae379..8e4b9001dd 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/HostActivity.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/HostActivity.java @@ -56,21 +56,20 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba void onStart(HostActivity host, Surface surface); /** - * Called on the main thread to block until the test has stopped or {@link #onStop()} is called. + * Called on the main thread to block until the test has stopped or {@link #forceStop()} is + * called. * * @param timeoutMs The maximum time to block in milliseconds. * @return Whether the test has stopped successful. */ - boolean blockUntilEnded(long timeoutMs); + boolean blockUntilStopped(long timeoutMs); /** - * Called on the main thread when the test is stopped. - *

- * The test will be stopped when {@link #blockUntilEnded(long)} returns, if the - * {@link HostActivity} has been paused, or if the {@link HostActivity}'s {@link Surface} has - * been destroyed. + * Called on the main thread to force stop the test (if it is not stopped already). + * + * @return Whether the test was forced stopped. */ - void onStop(); + boolean forceStop(); /** * Called on the test thread after the test has finished and been stopped. @@ -89,7 +88,8 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba private HostedTest hostedTest; private boolean hostedTestStarted; - private boolean forcedFinished; + private ConditionVariable hostedTestStartedCondition; + private boolean forcedStopped; /** * Executes a {@link HostedTest} inside the host. @@ -109,34 +109,31 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba * @param timeoutMs The number of milliseconds to wait for the test to finish. * @param failOnTimeout Whether the test fails when the timeout is exceeded. */ - public void runTest(HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) { + public void runTest(final HostedTest hostedTest, long timeoutMs, boolean failOnTimeout) { Assertions.checkArgument(timeoutMs > 0); Assertions.checkState(Thread.currentThread() != getMainLooper().getThread()); - Assertions.checkState(this.hostedTest == null); - this.hostedTest = Assertions.checkNotNull(hostedTest); + Assertions.checkNotNull(hostedTest); + hostedTestStartedCondition = new ConditionVariable(); + forcedStopped = false; hostedTestStarted = false; - forcedFinished = false; - final ConditionVariable testStarted = new ConditionVariable(); runOnUiThread(new Runnable() { @Override public void run() { + HostActivity.this.hostedTest = hostedTest; maybeStartHostedTest(); - testStarted.open(); } }); - testStarted.block(); + hostedTestStartedCondition.block(); - if (hostedTest.blockUntilEnded(timeoutMs)) { - hostedTest.onStop(); - if (!forcedFinished) { - Log.d(TAG, "Test finished. Checking pass conditions."); + if (hostedTest.blockUntilStopped(timeoutMs)) { + if (!forcedStopped) { + Log.d(TAG, "Checking test pass conditions."); hostedTest.onFinished(); - this.hostedTest = null; Log.d(TAG, "Pass conditions checked."); } else { - String message = "Test released before it finished. Activity may have been paused whilst " + String message = "Test force stopped. Activity may have been paused whilst " + "test was in progress."; Log.e(TAG, message); fail(message); @@ -147,8 +144,8 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba if (failOnTimeout) { fail(message); } - maybeStopHostedTest(); } + this.hostedTest = null; } // Activity lifecycle @@ -175,12 +172,6 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba super.onStart(); } - @Override - public void onResume() { - super.onResume(); - maybeStartHostedTest(); - } - @Override public void onPause() { super.onPause(); @@ -224,14 +215,13 @@ public final class HostActivity extends Activity implements SurfaceHolder.Callba hostedTestStarted = true; Log.d(TAG, "Starting test."); hostedTest.onStart(this, surface); + hostedTestStartedCondition.open(); } } private void maybeStopHostedTest() { - if (hostedTest != null && hostedTestStarted) { - forcedFinished = true; - hostedTest.onStop(); - hostedTest = null; + if (hostedTest != null && hostedTestStarted && !forcedStopped) { + forcedStopped = hostedTest.forceStop(); } }