Search in sources :

Example 21 with ActionSchedule

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

the class ExoPlayerTest method multipleListenersAndMultipleCallbacks_callbacksAreOrderedByType.

@Test
public void multipleListenersAndMultipleCallbacks_callbacksAreOrderedByType() throws Exception {
    String playWhenReadyChange1 = "playWhenReadyChange1";
    String playWhenReadyChange2 = "playWhenReadyChange2";
    String isPlayingChange1 = "isPlayingChange1";
    String isPlayingChange2 = "isPlayingChange2";
    ArrayList<String> events = new ArrayList<>();
    Player.Listener playerListener1 = new Player.Listener() {

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
            events.add(playWhenReadyChange1);
        }

        @Override
        public void onIsPlayingChanged(boolean isPlaying) {
            events.add(isPlayingChange1);
        }
    };
    Player.Listener playerListener2 = new Player.Listener() {

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
            events.add(playWhenReadyChange2);
        }

        @Override
        public void onIsPlayingChanged(boolean isPlaying) {
            events.add(isPlayingChange2);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            player.addListener(playerListener1);
            player.addListener(playerListener2);
        }
    }).waitForPlaybackState(Player.STATE_READY).play().waitForPlaybackState(Player.STATE_ENDED).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertThat(events).containsExactly(playWhenReadyChange1, playWhenReadyChange2, isPlayingChange1, isPlayingChange2, isPlayingChange1, isPlayingChange2).inOrder();
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Listener(com.google.android.exoplayer2.Player.Listener) MediaSourceEventListener(com.google.android.exoplayer2.source.MediaSourceEventListener) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) Listener(com.google.android.exoplayer2.Player.Listener) ExoPlayerTestRunner(com.google.android.exoplayer2.testutil.ExoPlayerTestRunner) Test(org.junit.Test)

Example 22 with ActionSchedule

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

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);
}
Also used : 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) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Test(org.junit.Test)

Example 23 with ActionSchedule

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

the class ExoPlayerTest method seekBeforePreparationCompletes_seeksToCorrectPosition.

@Test
public void seekBeforePreparationCompletes_seeksToCorrectPosition() throws Exception {
    CountDownLatch createPeriodCalledCountDownLatch = new CountDownLatch(1);
    FakeMediaPeriod[] fakeMediaPeriodHolder = new FakeMediaPeriod[1];
    FakeMediaSource mediaSource = new FakeMediaSource(/* timeline= */
    null, ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            // Defer completing preparation of the period until seek has been sent.
            fakeMediaPeriodHolder[0] = new FakeMediaPeriod(trackGroupArray, allocator, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            true);
            createPeriodCalledCountDownLatch.countDown();
            return fakeMediaPeriodHolder[0];
        }
    };
    AtomicLong positionWhenReady = new AtomicLong();
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_BUFFERING).delay(1).executeRunnable(() -> mediaSource.setNewSourceInfo(new FakeTimeline())).waitForTimelineChanged().executeRunnable(() -> {
        try {
            createPeriodCalledCountDownLatch.await();
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }).seek(5000).executeRunnable(() -> fakeMediaPeriodHolder[0].setPreparationComplete()).waitForPlaybackState(Player.STATE_READY).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            positionWhenReady.set(player.getCurrentPosition());
        }
    }).play().build();
    new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
    0, /* positionMs= */
    2000).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(positionWhenReady.get()).isAtLeast(5000);
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Allocator(com.google.android.exoplayer2.upstream.Allocator) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) CountDownLatch(java.util.concurrent.CountDownLatch) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod) AtomicLong(java.util.concurrent.atomic.AtomicLong) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 24 with ActionSchedule

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

the class ExoPlayerTest method moveMediaItems_unprepared_correctMaskingMediaItemIndex.

@Test
public void moveMediaItems_unprepared_correctMaskingMediaItemIndex() throws Exception {
    final int[] currentMediaItemIndices = { C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Increase current media item index.
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            player.moveMediaItem(/* currentIndex= */
            0, /* newIndex= */
            1);
            currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
        }
    }).prepare().waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(new FakeMediaSource(), new FakeMediaSource()).setActionSchedule(actionSchedule).build().start(/* doPrepare= */
    false).blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    assertArrayEquals(new int[] { 0, 1, 1 }, currentMediaItemIndices);
}
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) Test(org.junit.Test)

Example 25 with ActionSchedule

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

the class ExoPlayerTest method removeMediaItems_currentItemRemovedThatIsTheLast_correctMasking.

@Test
public void removeMediaItems_currentItemRemovedThatIsTheLast_correctMasking() throws Exception {
    Timeline firstTimeline = new FakeTimeline(/* windowCount= */
    1, 1L);
    MediaSource firstMediaSource = new FakeMediaSource(firstTimeline);
    Timeline secondTimeline = new FakeTimeline(/* windowCount= */
    1, 2L);
    MediaSource secondMediaSource = new FakeMediaSource(secondTimeline);
    Timeline thirdTimeline = new FakeTimeline(/* windowCount= */
    1, 3L);
    MediaSource thirdMediaSource = new FakeMediaSource(thirdTimeline);
    Timeline fourthTimeline = new FakeTimeline(/* windowCount= */
    1, 3L);
    MediaSource fourthMediaSource = new FakeMediaSource(fourthTimeline);
    final int[] currentMediaItemIndices = new int[9];
    Arrays.fill(currentMediaItemIndices, C.INDEX_UNSET);
    final int[] maskingPlaybackStates = new int[4];
    Arrays.fill(maskingPlaybackStates, C.INDEX_UNSET);
    final long[] currentPositions = new long[3];
    Arrays.fill(currentPositions, C.TIME_UNSET);
    final long[] bufferedPositions = new long[3];
    Arrays.fill(bufferedPositions, C.TIME_UNSET);
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_READY).waitForPendingPlayerCommands().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Expect the current media item index to be 2 after seek.
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            currentPositions[0] = player.getCurrentPosition();
            bufferedPositions[0] = player.getBufferedPosition();
            player.removeMediaItem(/* index= */
            2);
            // Expect the current media item index to be 0
            // (default position of timeline after not finding subsequent period).
            currentMediaItemIndices[1] = player.getCurrentMediaItemIndex();
            // Transition to ENDED.
            maskingPlaybackStates[0] = player.getPlaybackState();
            currentPositions[1] = player.getCurrentPosition();
            bufferedPositions[1] = player.getBufferedPosition();
        }
    }).waitForPlaybackState(Player.STATE_ENDED).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Expects the current media item index still on 0.
            currentMediaItemIndices[2] = player.getCurrentMediaItemIndex();
            // Insert an item at begin when the playlist is not empty.
            player.addMediaSource(/* index= */
            0, thirdMediaSource);
            // Expects the current media item index to be (0 + 1) after insertion at begin.
            currentMediaItemIndices[3] = player.getCurrentMediaItemIndex();
            // Remains in ENDED.
            maskingPlaybackStates[1] = player.getPlaybackState();
            currentPositions[2] = player.getCurrentPosition();
            bufferedPositions[2] = player.getBufferedPosition();
        }
    }).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[4] = player.getCurrentMediaItemIndex();
            // Implicit seek to the current media item index, which is out of bounds in new
            // timeline.
            player.setMediaSource(fourthMediaSource, /* resetPosition= */
            false);
            // 0 after reset.
            currentMediaItemIndices[5] = player.getCurrentMediaItemIndex();
            // Invalid seek, so we remain in ENDED.
            maskingPlaybackStates[2] = player.getPlaybackState();
        }
    }).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[6] = player.getCurrentMediaItemIndex();
            // Explicit seek to (0, C.TIME_UNSET). Player transitions to BUFFERING.
            player.setMediaSource(fourthMediaSource, /* startPositionMs= */
            5000);
            // 0 after explicit seek.
            currentMediaItemIndices[7] = player.getCurrentMediaItemIndex();
            // Transitions from ENDED to BUFFERING after explicit seek.
            maskingPlaybackStates[3] = player.getPlaybackState();
        }
    }).waitForTimelineChanged().executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            // Check whether actual media item index is equal masking index from above.
            currentMediaItemIndices[8] = player.getCurrentMediaItemIndex();
        }
    }).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).initialSeek(/* mediaItemIndex= */
    2, /* positionMs= */
    C.TIME_UNSET).setExpectedPlayerEndedCount(2).setMediaSources(firstMediaSource, secondMediaSource, thirdMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    // Expect reset of masking to first media item.
    exoPlayerTestRunner.assertPlaybackStatesEqual(Player.STATE_BUFFERING, // Ready after initial prepare.
    Player.STATE_READY, // ended after removing current media item index
    Player.STATE_ENDED, // buffers after set items with seek
    Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_ENDED);
    assertArrayEquals(new int[] { // ended after removing current media item index
    Player.STATE_ENDED, // adding items does not change state
    Player.STATE_ENDED, // set items with seek to current position.
    Player.STATE_ENDED, Player.STATE_BUFFERING }, // buffers after set items with seek
    maskingPlaybackStates);
    assertArrayEquals(new int[] { 2, 0, 0, 1, 1, 0, 0, 0, 0 }, currentMediaItemIndices);
    assertThat(currentPositions[0]).isEqualTo(0);
    assertThat(currentPositions[1]).isEqualTo(0);
    assertThat(currentPositions[2]).isEqualTo(0);
    assertThat(bufferedPositions[0]).isGreaterThan(0);
    assertThat(bufferedPositions[1]).isEqualTo(0);
    assertThat(bufferedPositions[2]).isEqualTo(0);
}
Also used : NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) 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) 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)

Aggregations

ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)143 Test (org.junit.Test)142 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)103 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)97 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)96 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)84 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)79 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)59 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)59 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)55 MediaSource (com.google.android.exoplayer2.source.MediaSource)40 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)36 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)30 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)30 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)30 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)30 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)30 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)19 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)17 ArrayList (java.util.ArrayList)16