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);
}
}
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);
}
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);
}
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();
}
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);
}
Aggregations