Remove stopTest workaround in ExoHostedTest

The workaround to post a message was needed to ensure we
receive any final onIsPlayingChanged event before the test
is finished (to record the correct playing time).

Using onEvents allows us to do this synchronously as the
callback guarantees that there is no other pending event.

#exofixit

PiperOrigin-RevId: 344436180
This commit is contained in:
tonihei 2020-11-26 17:59:21 +00:00 committed by Andrew Lewis
parent 9d8a41712d
commit e66f0032aa

View File

@ -15,7 +15,7 @@
*/ */
package com.google.android.exoplayer2.testutil; package com.google.android.exoplayer2.testutil;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull; import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import android.os.ConditionVariable; import android.os.ConditionVariable;
@ -71,7 +71,6 @@ public abstract class ExoHostedTest implements AnalyticsListener, HostedTest {
private @MonotonicNonNull DefaultTrackSelector trackSelector; private @MonotonicNonNull DefaultTrackSelector trackSelector;
private @MonotonicNonNull Surface surface; private @MonotonicNonNull Surface surface;
private @MonotonicNonNull ExoPlaybackException playerError; private @MonotonicNonNull ExoPlaybackException playerError;
private boolean playerWasPrepared;
private long totalPlayingTimeMs; private long totalPlayingTimeMs;
private long lastPlayingStartTimeMs; private long lastPlayingStartTimeMs;
@ -181,30 +180,24 @@ public abstract class ExoHostedTest implements AnalyticsListener, HostedTest {
// AnalyticsListener // AnalyticsListener
@Override @Override
public final void onPlaybackStateChanged(EventTime eventTime, @Player.State int playbackState) { public void onEvents(Player player, Events events) {
playerWasPrepared |= playbackState != Player.STATE_IDLE; if (events.contains(EVENT_IS_PLAYING_CHANGED)) {
if (playbackState == Player.STATE_ENDED if (player.isPlaying()) {
|| (playbackState == Player.STATE_IDLE && playerWasPrepared)) { lastPlayingStartTimeMs = SystemClock.elapsedRealtime();
// Post stopTest to ensure all currently pending events (e.g. onIsPlayingChanged or } else {
// onPlayerError) are still delivered before the player is released. totalPlayingTimeMs += SystemClock.elapsedRealtime() - lastPlayingStartTimeMs;
checkStateNotNull(actionHandler).post(this::stopTest); }
} }
} if (events.contains(EVENT_PLAYER_ERROR)) {
playerError = checkNotNull(player.getPlayerError());
@Override onPlayerErrorInternal(playerError);
public void onIsPlayingChanged(EventTime eventTime, boolean playing) { }
if (playing) { if (events.contains(EVENT_PLAYBACK_STATE_CHANGED)) {
lastPlayingStartTimeMs = SystemClock.elapsedRealtime(); @Player.State int playbackState = player.getPlaybackState();
} else { if (playbackState == Player.STATE_ENDED || playbackState == Player.STATE_IDLE) {
totalPlayingTimeMs += SystemClock.elapsedRealtime() - lastPlayingStartTimeMs; stopTest();
}
} }
}
@Override
public final void onPlayerError(EventTime eventTime, ExoPlaybackException error) {
playerWasPrepared = true;
playerError = error;
onPlayerErrorInternal(error);
} }
@Override @Override