提交 4ba17bb6 编写于 作者: T tonihei 提交者: Oliver Woodman

Remove potential flakiness from ExoPlayerTest caused by multi-threading.

Some tests in ExoPlayerTest issue commands to the player from the test thread
while the player is actively playing media (playWhenReady=true). Due to the
indeterminate time taken to enqueue the commands on the playback thread, they
may arrive when the player already proceeded to another window or finished
playback.

To ensure the tests are always deterministic, this change pauses playback in
the tests where this may happen before issuing the commands.
Also, for tests where we need to wait for a new window before issuing the
next command, a new action is added which allows to play until a specified
position.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=182535096
上级 24f866e7
......@@ -15,9 +15,11 @@
*/
package com.google.android.exoplayer2.testutil;
import android.os.Handler;
import android.util.Log;
import android.view.Surface;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
......@@ -411,7 +413,7 @@ public abstract class Action {
} else {
message.setPosition(positionMs);
}
message.setHandler(new android.os.Handler());
message.setHandler(new Handler());
message.setDeleteAfterDelivery(deleteAfterDelivery);
message.send();
}
......@@ -441,6 +443,68 @@ public abstract class Action {
}
/**
* Schedules a play action to be executed, waits until the player reaches the specified position,
* and pauses the player again.
*/
public static final class PlayUntilPosition extends Action {
private final int windowIndex;
private final long positionMs;
/**
* @param tag A tag to use for logging.
* @param windowIndex The window index at which the player should be paused again.
* @param positionMs The position in that window at which the player should be paused again.
*/
public PlayUntilPosition(String tag, int windowIndex, long positionMs) {
super(tag, "PlayUntilPosition:" + windowIndex + "," + positionMs);
this.windowIndex = windowIndex;
this.positionMs = positionMs;
}
@Override
protected void doActionAndScheduleNextImpl(
final SimpleExoPlayer player,
final MappingTrackSelector trackSelector,
final Surface surface,
final HandlerWrapper handler,
final ActionNode nextAction) {
// Schedule one message on the playback thread to pause the player immediately.
player
.createMessage(
new Target() {
@Override
public void handleMessage(int messageType, Object payload)
throws ExoPlaybackException {
player.setPlayWhenReady(/* playWhenReady= */ false);
}
})
.setPosition(windowIndex, positionMs)
.send();
// Schedule another message on this test thread to continue action schedule.
player
.createMessage(
new Target() {
@Override
public void handleMessage(int messageType, Object payload)
throws ExoPlaybackException {
nextAction.schedule(player, trackSelector, surface, handler);
}
})
.setPosition(windowIndex, positionMs)
.setHandler(new Handler())
.send();
player.setPlayWhenReady(true);
}
@Override
protected void doActionImpl(
SimpleExoPlayer player, MappingTrackSelector trackSelector, Surface surface) {
// Not triggered.
}
}
/**
* Waits for {@link Player.EventListener#onTimelineChanged(Timeline, Object, int)}.
*/
......
......@@ -29,6 +29,7 @@ import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.testutil.Action.ClearVideoSurface;
import com.google.android.exoplayer2.testutil.Action.ExecuteRunnable;
import com.google.android.exoplayer2.testutil.Action.PlayUntilPosition;
import com.google.android.exoplayer2.testutil.Action.PrepareSource;
import com.google.android.exoplayer2.testutil.Action.Seek;
import com.google.android.exoplayer2.testutil.Action.SendMessages;
......@@ -229,6 +230,29 @@ public final class ActionSchedule {
return apply(new SetPlayWhenReady(tag, true));
}
/**
* Schedules a play action to be executed, waits until the player reaches the specified
* position, and pauses the player again.
*
* @param windowIndex The window index at which the player should be paused again.
* @param positionMs The position in that window at which the player should be paused again.
* @return The builder, for convenience.
*/
public Builder playUntilPosition(int windowIndex, long positionMs) {
return apply(new PlayUntilPosition(tag, windowIndex, positionMs));
}
/**
* Schedules a play action to be executed, waits until the player reaches the start of the
* specified window, and pauses the player again.
*
* @param windowIndex The window index at which the player should be paused again.
* @return The builder, for convenience.
*/
public Builder playUntilStartOfWindow(int windowIndex) {
return apply(new PlayUntilPosition(tag, windowIndex, /* positionMs= */ 0));
}
/**
* Schedules a pause action to be executed.
*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册