Search in sources :

Example 1 with MediaTestUtils.createTimeline

use of androidx.media3.session.MediaTestUtils.createTimeline in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method playlistChange.

@Test
public void playlistChange() throws Exception {
    AtomicReference<List<QueueItem>> queueRef = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onQueueChanged(List<QueueItem> queue) {
            queueRef.set(queue);
            latch.countDown();
        }
    };
    controllerCompat.registerCallback(callback, handler);
    Timeline timeline = MediaTestUtils.createTimeline(/* windowCount= */
    5);
    session.getMockPlayer().setTimeline(timeline);
    session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    List<QueueItem> queueFromParam = queueRef.get();
    List<QueueItem> queueFromGetter = controllerCompat.getQueue();
    assertThat(queueFromParam).hasSize(timeline.getWindowCount());
    assertThat(queueFromGetter).hasSize(timeline.getWindowCount());
    Timeline.Window window = new Timeline.Window();
    for (int i = 0; i < timeline.getWindowCount(); i++) {
        assertThat(queueFromParam.get(i).getDescription().getMediaId()).isEqualTo(timeline.getWindow(i, window).mediaItem.mediaId);
        assertThat(queueFromGetter.get(i).getDescription().getMediaId()).isEqualTo(timeline.getWindow(i, window).mediaItem.mediaId);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Timeline(androidx.media3.common.Timeline) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with MediaTestUtils.createTimeline

use of androidx.media3.session.MediaTestUtils.createTimeline in project media by androidx.

the class MediaControllerStateMaskingTest method seekToPreviousMediaItem.

@Test
public void seekToPreviousMediaItem() throws Exception {
    int initialMediaItemIndex = 1;
    String firstMediaId = "firstMediaId";
    String secondMediaId = "secondMediaId";
    String thirdMediaId = "thirdMediaId";
    String testCurrentMediaId = firstMediaId;
    int testMediaItemIndex = 0;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(MediaTestUtils.createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(3);
    AtomicReference<MediaItem> newMediaItemRef = new AtomicReference<>();
    AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
    AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
            newMediaItemRef.set(mediaItem);
            latch.countDown();
        }

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
            oldPositionInfoRef.set(oldPosition);
            newPositionInfoRef.set(newPosition);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.seekToPreviousMediaItem();
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(newMediaItemRef.get().mediaId).isEqualTo(testCurrentMediaId);
    assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialMediaItemIndex);
    assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
    assertThat(onEventsRef.get().contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(onEventsRef.get().contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testMediaItemIndex);
}
Also used : Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(androidx.media3.common.MediaItem) PositionInfo(androidx.media3.common.Player.PositionInfo) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 3 with MediaTestUtils.createTimeline

use of androidx.media3.session.MediaTestUtils.createTimeline in project media by androidx.

the class MediaControllerStateMaskingTest method stop.

@Test
public void stop() throws Exception {
    long duration = 6000L;
    long initialCurrentPosition = 3000L;
    long initialBufferedPosition = duration;
    int initialBufferedPercentage = 100;
    long initialTotalBufferedDuration = initialBufferedPosition - initialCurrentPosition;
    int testPlaybackState = Player.STATE_IDLE;
    long testCurrentPosition = 3000L;
    long testBufferedPosition = testCurrentPosition;
    int testBufferedPercentage = 50;
    long testTotalBufferedDuration = testBufferedPosition - testCurrentPosition;
    Timeline testTimeline = MediaTestUtils.createTimeline(3);
    PlaybackException testPlaybackException = new PlaybackException(/* message= */
    "test", /* cause= */
    null, PlaybackException.ERROR_CODE_REMOTE_ERROR);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDuration(duration).setPlaybackState(Player.STATE_READY).setPlayerError(testPlaybackException).setTimeline(testTimeline).setCurrentPosition(initialCurrentPosition).setBufferedPosition(initialBufferedPosition).setBufferedPercentage(initialBufferedPercentage).setTotalBufferedDuration(initialTotalBufferedDuration).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger playbackStateFromCallbackRef = new AtomicInteger();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(int playbackState) {
            playbackStateFromCallbackRef.set(playbackState);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger playbackStateFromGetterRef = new AtomicInteger();
    AtomicReference<PlaybackException> playerErrorFromGetterRef = new AtomicReference<>();
    AtomicReference<Timeline> timelineFromGetterRef = new AtomicReference<>();
    AtomicLong currentPositionFromGetterRef = new AtomicLong();
    AtomicLong bufferedPositionFromGetterRef = new AtomicLong();
    AtomicInteger bufferedPercentageFromGetterRef = new AtomicInteger();
    AtomicLong totalBufferedDurationFromGetterRef = new AtomicLong();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.stop();
        playbackStateFromGetterRef.set(controller.getPlaybackState());
        playerErrorFromGetterRef.set(controller.getPlayerError());
        timelineFromGetterRef.set(controller.getCurrentTimeline());
        currentPositionFromGetterRef.set(controller.getCurrentPosition());
        bufferedPositionFromGetterRef.set(controller.getBufferedPosition());
        bufferedPercentageFromGetterRef.set(controller.getBufferedPercentage());
        totalBufferedDurationFromGetterRef.set(controller.getTotalBufferedDuration());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(playbackStateFromCallbackRef.get()).isEqualTo(testPlaybackState);
    assertThat(onEventsRef.get().contains(Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
    assertThat(playbackStateFromGetterRef.get()).isEqualTo(testPlaybackState);
    assertThat(playerErrorFromGetterRef.get().errorInfoEquals(testPlaybackException)).isTrue();
    assertThat(timelineFromGetterRef.get().getWindowCount()).isEqualTo(testTimeline.getWindowCount());
    assertThat(currentPositionFromGetterRef.get()).isEqualTo(testCurrentPosition);
    assertThat(bufferedPositionFromGetterRef.get()).isEqualTo(testBufferedPosition);
    assertThat(bufferedPercentageFromGetterRef.get()).isEqualTo(testBufferedPercentage);
    assertThat(totalBufferedDurationFromGetterRef.get()).isEqualTo(testTotalBufferedDuration);
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 4 with MediaTestUtils.createTimeline

use of androidx.media3.session.MediaTestUtils.createTimeline in project media by androidx.

the class MediaControllerListenerTest method onMediaItemTransition_withNullMediaItem.

@Test
public void onMediaItemTransition_withNullMediaItem() throws Exception {
    Timeline timeline = MediaTestUtils.createTimeline(/* windowCount= */
    1);
    remoteSession.getMockPlayer().setTimeline(timeline);
    remoteSession.getMockPlayer().setCurrentMediaItemIndex(0);
    remoteSession.getMockPlayer().notifyMediaItemTransition(0, Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
    AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(new Player.Listener() {

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, @Player.MediaItemTransitionReason int reason) {
            mediaItemRef.set(mediaItem);
            latch.countDown();
        }
    }));
    remoteSession.getMockPlayer().setTimeline(Timeline.EMPTY);
    remoteSession.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
    remoteSession.getMockPlayer().notifyMediaItemTransition(C.INDEX_UNSET, Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(mediaItemRef.get()).isNull();
}
Also used : MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) MediaItem(androidx.media3.common.MediaItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with MediaTestUtils.createTimeline

use of androidx.media3.session.MediaTestUtils.createTimeline in project media by androidx.

the class MediaControllerStateMaskingTest method removeMediaItems_currentItemNotRemoved.

@Test
public void removeMediaItems_currentItemNotRemoved() throws Exception {
    int initialMediaItemIndex = 1;
    String firstMediaId = "firstMediaId";
    String secondMediaId = "secondMediaId";
    String thirdMediaId = "thirdMediaId";
    String testCurrentMediaId = secondMediaId;
    int testFromIndex = 2;
    int testToIndex = 3;
    int testMediaItemCount = 2;
    int testCurrentMediaItemIndex = initialMediaItemIndex;
    int testNextMediaItemIndex = C.INDEX_UNSET;
    int testPreviousMediaItemIndex = testCurrentMediaItemIndex - 1;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            newTimelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicInteger nextMediaItemIndexRef = new AtomicInteger();
    AtomicInteger previousMediaItemIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.removeMediaItems(/* fromIndex= */
        testFromIndex, /* toIndex= */
        testToIndex);
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
        previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimeline(newTimelineRef.get(), testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
    testCurrentMediaItemIndex, /* testLastPeriodIndex= */
    testCurrentMediaItemIndex);
    Window window = new Window();
    assertThat(newTimelineRef.get().getWindow(testCurrentMediaItemIndex, window).mediaItem.mediaId).isEqualTo(testCurrentMediaId);
    assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
    assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
    assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
}
Also used : Window(androidx.media3.common.Timeline.Window) Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)32 LargeTest (androidx.test.filters.LargeTest)30 Timeline (androidx.media3.common.Timeline)29 Bundle (android.os.Bundle)24 CountDownLatch (java.util.concurrent.CountDownLatch)21 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 Player (androidx.media3.common.Player)16 MediaTestUtils.createTimeline (androidx.media3.session.MediaTestUtils.createTimeline)15 MediaItem (androidx.media3.common.MediaItem)11 Nullable (androidx.annotation.Nullable)8 PositionInfo (androidx.media3.common.Player.PositionInfo)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 QueueItem (android.support.v4.media.session.MediaSessionCompat.QueueItem)3 RepeatMode (androidx.media3.common.Player.RepeatMode)3 Window (androidx.media3.common.Timeline.Window)2 RemoteMockPlayer (androidx.media3.session.RemoteMediaSession.RemoteMockPlayer)2 MediumTest (androidx.test.filters.MediumTest)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RatingCompat (android.support.v4.media.RatingCompat)1