use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method removeMediaItems_removePreviousItemWithMultiplePeriods.
@Test
public void removeMediaItems_removePreviousItemWithMultiplePeriods() throws Exception {
int initialMediaItemIndex = 1;
int initialWindowFirstPeriodIndex = 2;
int initialWindowLastPeriodIndex = 4;
int initialPeriodIndex = 3;
int testFromIndex = 0;
int testToIndex = 1;
int testMediaItemCount = 2;
int prevWindowPeriodSize = 2;
int testCurrentMediaItemIndex = 0;
int testCurrentPeriodIndex = initialPeriodIndex - prevWindowPeriodSize;
int testCurrentWindowFirstPeriodIndex = initialWindowFirstPeriodIndex - prevWindowPeriodSize;
int testCurrentWindowLastPeriodIndex = initialWindowLastPeriodIndex - prevWindowPeriodSize;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimelineWithPeriodSizes(new int[] { prevWindowPeriodSize, 3, 2 })).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialPeriodIndex).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 currentPeriodIndexRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.removeMediaItems(/* fromIndex= */
testFromIndex, /* toIndex= */
testToIndex);
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimeline(newTimelineRef.get(), testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
testCurrentWindowFirstPeriodIndex, /* testLastPeriodIndex= */
testCurrentWindowLastPeriodIndex);
assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method setRepeatMode.
@Test
public void setRepeatMode() throws Exception {
int testRepeatMode = REPEAT_MODE_ALL;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setRepeatMode(REPEAT_MODE_ONE).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicInteger repeatModeFromCallbackRef = new AtomicInteger();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onRepeatModeChanged(int repeatMode) {
repeatModeFromCallbackRef.set(repeatMode);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
onEventsRef.set(events);
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicInteger repeatModeFromGetterRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.setRepeatMode(testRepeatMode);
repeatModeFromGetterRef.set(controller.getRepeatMode());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(repeatModeFromCallbackRef.get()).isEqualTo(testRepeatMode);
assertThat(onEventsRef.get().contains(Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
assertThat(repeatModeFromGetterRef.get()).isEqualTo(testRepeatMode);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method assertMoveMediaItems_whenMovingBetweenWindowsWithMultiplePeriods.
private void assertMoveMediaItems_whenMovingBetweenWindowsWithMultiplePeriods(int initialMediaItemCount, int testFromIndex, int testToIndex, int testNewIndex, int testCurrentMediaItemIndex, int testCurrentWindowFirstPeriodIndex, int testCurrentWindowLastPeriodIndex, int testCurrentPeriodIndex) throws Exception {
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 currentPeriodIndexRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.moveMediaItems(testFromIndex, testToIndex, testNewIndex);
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimeline(newTimelineRef.get(), initialMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
testCurrentWindowFirstPeriodIndex, /* testLastPeriodIndex= */
testCurrentWindowLastPeriodIndex);
assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
use of androidx.media3.common.Player.Events 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);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method addMediaItems_toEndOfTimeline.
@Test
public void addMediaItems_toEndOfTimeline() throws Exception {
int initialMediaItemCount = 3;
int initialMediaItemIndex = 2;
int testMediaItemCount = 2;
int testCurrentMediaItemIndex = initialMediaItemIndex;
int testNextMediaItemIndex = testCurrentMediaItemIndex + 1;
int testPreviousMediaItemIndex = testCurrentMediaItemIndex - 1;
int testCurrentPeriodIndex = initialMediaItemIndex;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(initialMediaItemCount)).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();
AtomicInteger currentPeriodIndexRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.addMediaItems(createMediaItems(testMediaItemCount));
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimeline(newTimelineRef.get(), initialMediaItemCount + testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
testCurrentPeriodIndex, /* testLastPeriodIndex= */
testCurrentPeriodIndex);
assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
Aggregations