Search in sources :

Example 21 with PlayerRunnable

use of com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable in project ExoPlayer by google.

the class ExoPlayerTest method resetPlaylistWithPreviousPosition.

@Test
public void resetPlaylistWithPreviousPosition() throws Exception {
    Object firstWindowId = new Object();
    Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    firstWindowId));
    Timeline firstExpectedMaskingTimeline = new MaskingMediaSource.PlaceholderTimeline(FakeTimeline.FAKE_MEDIA_ITEM.buildUpon().setTag(firstWindowId).build());
    Object secondWindowId = new Object();
    Timeline secondTimeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    secondWindowId));
    Timeline secondExpectedMaskingTimeline = new MaskingMediaSource.PlaceholderTimeline(FakeTimeline.FAKE_MEDIA_ITEM.buildUpon().setTag(secondWindowId).build());
    MediaSource secondSource = new FakeMediaSource(secondTimeline);
    AtomicLong positionAfterReprepare = new AtomicLong();
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    0, /* positionMs= */
    2000).setMediaSources(/* mediaItemIndex= */
    0, /* positionMs= */
    2000, secondSource).waitForTimelineChanged(secondTimeline, /* expectedReason */
    Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            positionAfterReprepare.set(player.getCurrentPosition());
        }
    }).play().build();
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setTimeline(timeline).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    testRunner.assertTimelinesSame(firstExpectedMaskingTimeline, timeline, secondExpectedMaskingTimeline, secondTimeline);
    testRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
    assertThat(positionAfterReprepare.get()).isAtLeast(2000L);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) AtomicLong(java.util.concurrent.atomic.AtomicLong) ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) CompositeMediaSource(com.google.android.exoplayer2.source.CompositeMediaSource) ClippingMediaSource(com.google.android.exoplayer2.source.ClippingMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 22 with PlayerRunnable

use of com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable in project ExoPlayer by google.

the class ExoPlayerTest method stop_withoutReset_doesNotResetPosition_correctMasking.

@Test
public void stop_withoutReset_doesNotResetPosition_correctMasking() throws Exception {
    int[] currentMediaItemIndex = { C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET };
    long[] currentPosition = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    long[] bufferedPosition = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    long[] totalBufferedDuration = { C.TIME_UNSET, C.TIME_UNSET, C.TIME_UNSET };
    final FakeMediaSource mediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().seek(/* mediaItemIndex= */
    1, /* positionMs= */
    1000).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndex[0] = player.getCurrentMediaItemIndex();
            currentPosition[0] = player.getCurrentPosition();
            bufferedPosition[0] = player.getBufferedPosition();
            totalBufferedDuration[0] = player.getTotalBufferedDuration();
            player.stop(/* reset= */
            false);
            currentMediaItemIndex[1] = player.getCurrentMediaItemIndex();
            currentPosition[1] = player.getCurrentPosition();
            bufferedPosition[1] = player.getBufferedPosition();
            totalBufferedDuration[1] = player.getTotalBufferedDuration();
        }
    }).waitForPlaybackState(Player.STATE_IDLE).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndex[2] = player.getCurrentMediaItemIndex();
            currentPosition[2] = player.getCurrentPosition();
            bufferedPosition[2] = player.getBufferedPosition();
            totalBufferedDuration[2] = player.getTotalBufferedDuration();
        }
    }).build();
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource, mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    testRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
    testRunner.assertPositionDiscontinuityReasonsEqual(Player.DISCONTINUITY_REASON_SEEK);
    assertThat(currentMediaItemIndex[0]).isEqualTo(1);
    assertThat(currentPosition[0]).isEqualTo(1000);
    assertThat(bufferedPosition[0]).isEqualTo(10000);
    assertThat(totalBufferedDuration[0]).isEqualTo(9000);
    assertThat(currentMediaItemIndex[1]).isEqualTo(1);
    assertThat(currentPosition[1]).isEqualTo(1000);
    assertThat(bufferedPosition[1]).isEqualTo(1000);
    assertThat(totalBufferedDuration[1]).isEqualTo(0);
    assertThat(currentMediaItemIndex[2]).isEqualTo(1);
    assertThat(currentPosition[2]).isEqualTo(1000);
    assertThat(bufferedPosition[2]).isEqualTo(1000);
    assertThat(totalBufferedDuration[2]).isEqualTo(0);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 23 with PlayerRunnable

use of com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable in project ExoPlayer by google.

the class ExoPlayerTest method setMediaSources_whenIdle_noSeekEmpty_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenIdle_noSeekEmpty_correctMaskingPlaybackState() throws Exception {
    final int[] maskingPlaybackStates = new int[1];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set an empty media item with no seek.
            player.setMediaSource(new ConcatenatingMediaSource());
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).setMediaSources(new FakeMediaSource()).prepare().waitForPlaybackState(Player.STATE_READY).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).skipSettingMediaSources().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);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 24 with PlayerRunnable

use of com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable in project ExoPlayer by google.

the class ExoPlayerTest method setMediaSources_whenIdle_correctMaskingPlaybackState.

@Test
public void setMediaSources_whenIdle_correctMaskingPlaybackState() throws Exception {
    final int[] maskingPlaybackStates = new int[4];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with no seek.
            player.setMediaSource(new ConcatenatingMediaSource());
            maskingPlaybackStates[0] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an implicit seek to the current position.
            player.setMediaSource(new FakeMediaSource());
            maskingPlaybackStates[1] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set media item with an explicit seek.
            player.setMediaSource(new FakeMediaSource(), /* startPositionMs= */
            C.TIME_UNSET);
            maskingPlaybackStates[2] = player.getPlaybackState();
        }
    }).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Set empty media item with an explicit seek.
            player.setMediaSource(new ConcatenatingMediaSource(), /* startPositionMs= */
            C.TIME_UNSET);
            maskingPlaybackStates[3] = player.getPlaybackState();
        }
    }).prepare().build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).skipSettingMediaSources().setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_ENDED);
    assertArrayEquals(new int[] { Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE, Player.STATE_IDLE }, maskingPlaybackStates);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ConcatenatingMediaSource(com.google.android.exoplayer2.source.ConcatenatingMediaSource) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 25 with PlayerRunnable

use of com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable in project ExoPlayer by google.

the class ExoPlayerTest method playbackErrorAndReprepareDoesNotResetPosition.

@Test
public void playbackErrorAndReprepareDoesNotResetPosition() throws Exception {
    final Timeline timeline = new FakeTimeline(/* windowCount= */
    2);
    final long[] positionHolder = new long[3];
    final int[] mediaItemIndexHolder = new int[3];
    final FakeMediaSource firstMediaSource = new FakeMediaSource(timeline);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).playUntilPosition(/* mediaItemIndex= */
    1, /* positionMs= */
    500).throwPlaybackException(ExoPlaybackException.createForSource(new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED)).waitForPlaybackState(Player.STATE_IDLE).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position while in error state
            positionHolder[0] = player.getCurrentPosition();
            mediaItemIndexHolder[0] = player.getCurrentMediaItemIndex();
        }
    }).prepare().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position while repreparing.
            positionHolder[1] = player.getCurrentPosition();
            mediaItemIndexHolder[1] = player.getCurrentMediaItemIndex();
        }
    }).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Position after repreparation finished.
            positionHolder[2] = player.getCurrentPosition();
            mediaItemIndexHolder[2] = player.getCurrentMediaItemIndex();
        }
    }).play().build();
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(firstMediaSource).setActionSchedule(actionSchedule).build();
    try {
        testRunner.start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
        fail();
    } catch (ExoPlaybackException e) {
    // Expected exception.
    }
    assertThat(positionHolder[0]).isAtLeast(500L);
    assertThat(positionHolder[1]).isEqualTo(positionHolder[0]);
    assertThat(positionHolder[2]).isEqualTo(positionHolder[0]);
    assertThat(mediaItemIndexHolder[0]).isEqualTo(1);
    assertThat(mediaItemIndexHolder[1]).isEqualTo(1);
    assertThat(mediaItemIndexHolder[2]).isEqualTo(1);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) IOException(java.io.IOException) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Aggregations

PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)93 Test (org.junit.Test)91 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)76 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)74 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)56 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)51 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)43 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)34 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)28 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)28 MediaSource (com.google.android.exoplayer2.source.MediaSource)21 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)20 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)20 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)20 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)20 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)20 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)18 AtomicLong (java.util.concurrent.atomic.AtomicLong)15 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11