use of androidx.media3.test.utils.ActionSchedule in project media by androidx.
the class ExoPlayerTest method recursivePlayerChangesAreReportedInCorrectOrder.
@Test
public void recursivePlayerChangesAreReportedInCorrectOrder() throws Exception {
// The listener stops the player as soon as it's ready (which should report a timeline and state
// change) and sets playWhenReady to false when the timeline callback is received.
final AtomicReference<Player> playerReference = new AtomicReference<>();
final List<Boolean> playerListenerPlayWhenReady = new ArrayList<>();
final List<Integer> playerListenerStates = new ArrayList<>();
List<Integer> sequence = new ArrayList<>();
final Player.Listener playerListener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
playerListenerStates.add(playbackState);
if (playbackState == Player.STATE_READY) {
playerReference.get().stop(/* reset= */
true);
sequence.add(0);
}
}
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
if (timeline.isEmpty()) {
playerReference.get().pause();
sequence.add(1);
}
}
@Override
public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
playerListenerPlayWhenReady.add(playWhenReady);
sequence.add(2);
}
};
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
playerReference.set(player);
player.addListener(playerListener);
}
}).build();
new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
assertThat(playerListenerStates).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
assertThat(playerListenerPlayWhenReady).containsExactly(false).inOrder();
assertThat(sequence).containsExactly(0, 1, 2).inOrder();
}
use of androidx.media3.test.utils.ActionSchedule in project media by androidx.
the class ExoPlayerTest method clippedLoopedPeriodsArePlayedFully.
@Test
public void clippedLoopedPeriodsArePlayedFully() throws Exception {
long startPositionUs = 300_000;
long expectedDurationUs = 700_000;
MediaSource mediaSource = new ClippingMediaSource(new FakeMediaSource(), startPositionUs, startPositionUs + expectedDurationUs);
Clock clock = new FakeClock(/* isAutoAdvancing= */
true);
AtomicReference<Player> playerReference = new AtomicReference<>();
AtomicLong positionAtDiscontinuityMs = new AtomicLong(C.TIME_UNSET);
AtomicLong clockAtStartMs = new AtomicLong(C.TIME_UNSET);
AtomicLong clockAtDiscontinuityMs = new AtomicLong(C.TIME_UNSET);
Player.Listener playerListener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
if (playbackState == Player.STATE_READY && clockAtStartMs.get() == C.TIME_UNSET) {
clockAtStartMs.set(clock.elapsedRealtime());
}
}
@Override
public void onPositionDiscontinuity(@DiscontinuityReason int reason) {
if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
positionAtDiscontinuityMs.set(playerReference.get().getCurrentPosition());
clockAtDiscontinuityMs.set(clock.elapsedRealtime());
}
}
};
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
playerReference.set(player);
player.addListener(playerListener);
}
}).pause().setRepeatMode(Player.REPEAT_MODE_ALL).waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
0, /* positionMs= */
1).playUntilStartOfMediaItem(/* mediaItemIndex= */
0).setRepeatMode(Player.REPEAT_MODE_OFF).play().build();
new ExoPlayerTestRunner.Builder(context).setClock(clock).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
assertThat(positionAtDiscontinuityMs.get()).isAtLeast(0L);
assertThat(clockAtDiscontinuityMs.get() - clockAtStartMs.get()).isAtLeast(Util.usToMs(expectedDurationUs));
}
use of androidx.media3.test.utils.ActionSchedule in project media by androidx.
the class ExoPlayerTest method pauseAtEndOfMediaItems_pausesPlaybackWhenEnded.
@Test
public void pauseAtEndOfMediaItems_pausesPlaybackWhenEnded() throws Exception {
TimelineWindowDefinition timelineWindowDefinition = new TimelineWindowDefinition(/* isSeekable= */
true, /* isDynamic= */
false, /* durationUs= */
10 * C.MICROS_PER_SECOND);
MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(timelineWindowDefinition));
AtomicInteger playbackStateAfterPause = new AtomicInteger(C.INDEX_UNSET);
AtomicLong positionAfterPause = new AtomicLong(C.TIME_UNSET);
AtomicInteger mediaItemIndexAfterPause = new AtomicInteger(C.INDEX_UNSET);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlayWhenReady(true).waitForPlayWhenReady(false).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
playbackStateAfterPause.set(player.getPlaybackState());
mediaItemIndexAfterPause.set(player.getCurrentMediaItemIndex());
positionAfterPause.set(player.getContentPosition());
}
}).build();
new ExoPlayerTestRunner.Builder(context).setPauseAtEndOfMediaItems(true).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
assertThat(playbackStateAfterPause.get()).isEqualTo(Player.STATE_ENDED);
assertThat(mediaItemIndexAfterPause.get()).isEqualTo(0);
assertThat(positionAfterPause.get()).isEqualTo(10_000);
}
use of androidx.media3.test.utils.ActionSchedule in project media by androidx.
the class ExoPlayerTest method setMediaSources_whenIdle_invalidSeek_correctMaskingPlaybackState.
@Test
public void setMediaSources_whenIdle_invalidSeek_correctMaskingPlaybackState() throws Exception {
final int[] maskingPlaybackStates = new int[1];
Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPositionDiscontinuity().waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
// Set a media item with an implicit seek to the current position which is
// invalid in the new timeline.
player.setMediaSource(new FakeMediaSource(new FakeTimeline(/* windowCount= */
1, 1L)));
maskingPlaybackStates[0] = player.getPlaybackState();
}
}).prepare().waitForPlaybackState(Player.STATE_READY).build();
ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
1, /* positionMs= */
C.TIME_UNSET).setMediaSources(new ConcatenatingMediaSource()).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
// Expect reset of masking to first media item.
exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED);
assertArrayEquals(new int[] { Player.STATE_IDLE }, maskingPlaybackStates);
exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}
use of androidx.media3.test.utils.ActionSchedule in project media by androidx.
the class ExoPlayerTest method seekToCurrentPosition_inEndedState_switchesToBufferingStateAndContinuesPlayback.
@Test
public void seekToCurrentPosition_inEndedState_switchesToBufferingStateAndContinuesPlayback() throws Exception {
MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(/* windowCount = */
1));
AtomicInteger mediaItemIndexAfterFinalEndedState = new AtomicInteger();
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_ENDED).addMediaSources(mediaSource).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
player.seekTo(player.getCurrentPosition());
}
}).waitForPlaybackState(Player.STATE_READY).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
mediaItemIndexAfterFinalEndedState.set(player.getCurrentMediaItemIndex());
}
}).build();
new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
assertThat(mediaItemIndexAfterFinalEndedState.get()).isEqualTo(1);
}
Aggregations