Remove CountDownLatch from MockPlayer

The MockPlayer has a single CountDownLatch field and multiple boolean
flags that track if a player method was called. Upon calling the methods
the latch count. Tests set the latch count to match exactly with the
number of expected player interactions then block the test thread until
the latch reaches zero and assert the respective method flags are true.

This is subject to false positives. If the underneath implementation
changes and call more player method, then the test thread will unblock
as soon as a certain number of interactions is performed, which may be
less than what the test expected originally. However, the test may stil
pass if the player thread had enough time to update the expected method
flag.

This change removes the single CountDownLatch and the boolean flags and
instead it adds APIs to query the MockPlayer if a method has been called
and await until a method is called. Internally, the MockPlayer has a
ConditionVariable per method.

PiperOrigin-RevId: 432399077
This commit is contained in:
christosts 2022-03-04 10:35:20 +00:00 committed by Ian Baker
parent a73a9e9ca5
commit 45d512160c
10 changed files with 515 additions and 366 deletions

View File

@ -181,7 +181,6 @@ public class MediaSessionAndControllerTest {
MockPlayer player =
new MockPlayer.Builder()
.setApplicationLooper(threadTestRule.getHandler().getLooper())
.setLatchCount(1)
.build();
MediaSession session =
sessionTestRule.ensureReleaseAfterTest(
@ -190,8 +189,7 @@ public class MediaSessionAndControllerTest {
threadTestRule.getHandler().postAndSync(controller::play);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test

View File

@ -74,7 +74,6 @@ public class MediaSessionCallbackTest {
context = ApplicationProvider.getApplicationContext();
player =
new MockPlayer.Builder()
.setLatchCount(1)
.setApplicationLooper(threadTestRule.getHandler().getLooper())
.build();
}
@ -157,15 +156,14 @@ public class MediaSessionCallbackTest {
controllerTestRule.createRemoteController(session.getToken());
controller.prepare();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
assertThat(player.prepareCalled).isFalse();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PREPARE)).isFalse();
assertThat(commands).hasSize(1);
assertThat(commands.get(0)).isEqualTo(Player.COMMAND_PREPARE);
controller.play();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
assertThat(player.prepareCalled).isFalse();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PREPARE)).isFalse();
assertThat(commands).hasSize(2);
assertThat(commands.get(1)).isEqualTo(Player.COMMAND_PLAY_PAUSE);
}

View File

@ -94,8 +94,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
public void setUp() {
context = ApplicationProvider.getApplicationContext();
handler = threadTestRule.getHandler();
player =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
@ -206,8 +205,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().play();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
@ -222,8 +221,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().pause();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
}
@Test
@ -238,8 +237,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().stop();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.stopCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_STOP, TIMEOUT_MS);
}
@Test
@ -254,8 +253,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().prepare();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
@Test
@ -271,8 +270,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
long seekPosition = 12125L;
controller.getTransportControls().seekTo(seekPosition);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO, TIMEOUT_MS);
assertThat(player.seekPositionMs).isEqualTo(seekPosition);
}
@ -289,8 +288,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
float testSpeed = 2.0f;
controller.getTransportControls().setPlaybackSpeed(testSpeed);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setPlaybackSpeedCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_PLAYBACK_SPEED, TIMEOUT_MS);
assertThat(player.playbackParameters.speed).isEqualTo(testSpeed);
}
@ -316,8 +315,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
MediaDescriptionCompat desc = new MediaDescriptionCompat.Builder().setMediaId(mediaId).build();
controller.addQueueItem(desc);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.mediaItem.mediaId).isEqualTo(mediaId);
}
@ -344,8 +342,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
MediaDescriptionCompat desc = new MediaDescriptionCompat.Builder().setMediaId(mediaId).build();
controller.addQueueItem(desc, testIndex);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemWithIndexCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM_WITH_INDEX, TIMEOUT_MS);
assertThat(player.index).isEqualTo(testIndex);
assertThat(player.mediaItem.mediaId).isEqualTo(mediaId);
}
@ -375,8 +372,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
new MediaDescriptionCompat.Builder().setMediaId(targetItem.mediaId).build();
controller.removeQueueItem(desc);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.removeMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_REMOVE_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.index).isEqualTo(targetIndex);
}
@ -392,8 +388,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().skipToPrevious();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToPreviousCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS, TIMEOUT_MS);
}
@Test
@ -408,8 +404,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().skipToNext();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
}
@Test
@ -434,8 +430,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
int targetIndex = 3;
controller.getTransportControls().skipToQueueItem(queue.get(targetIndex).getQueueId());
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToDefaultPositionWithMediaItemIndexCalled).isTrue();
player.awaitMethodCalled(
MockPlayer.METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX, TIMEOUT_MS);
assertThat(player.seekMediaItemIndex).isEqualTo(targetIndex);
}
@ -452,9 +448,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
@PlaybackStateCompat.ShuffleMode int testShuffleMode = PlaybackStateCompat.SHUFFLE_MODE_GROUP;
controller.getTransportControls().setShuffleMode(testShuffleMode);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setShuffleModeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_SHUFFLE_MODE, TIMEOUT_MS);
assertThat(player.shuffleModeEnabled).isTrue();
}
@ -471,9 +466,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
int testRepeatMode = Player.REPEAT_MODE_ALL;
controller.getTransportControls().setRepeatMode(testRepeatMode);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setRepeatModeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_REPEAT_MODE, TIMEOUT_MS);
assertThat(player.repeatMode).isEqualTo(testRepeatMode);
}
@ -488,7 +482,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
MockPlayer remotePlayer =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
handler.postAndSync(
() -> {
remotePlayer.deviceInfo =
@ -501,8 +495,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
int targetVolume = 50;
controller.setVolumeTo(targetVolume, /* flags= */ 0);
assertThat(remotePlayer.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(remotePlayer.setDeviceVolumeCalled).isTrue();
remotePlayer.awaitMethodCalled(MockPlayer.METHOD_SET_DEVICE_VOLUME, TIMEOUT_MS);
assertThat(remotePlayer.deviceVolume).isEqualTo(targetVolume);
}
@ -517,7 +510,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
MockPlayer remotePlayer =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
handler.postAndSync(
() -> {
remotePlayer.deviceInfo =
@ -529,8 +522,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller.adjustVolume(AudioManager.ADJUST_RAISE, /* flags= */ 0);
assertThat(remotePlayer.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(remotePlayer.increaseDeviceVolumeCalled).isTrue();
remotePlayer.awaitMethodCalled(MockPlayer.METHOD_INCREASE_DEVICE_VOLUME, TIMEOUT_MS);
}
@Test
@ -544,7 +536,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
MockPlayer remotePlayer =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
handler.postAndSync(
() -> {
remotePlayer.deviceInfo =
@ -556,8 +548,7 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller.adjustVolume(AudioManager.ADJUST_LOWER, /* flags= */ 0);
assertThat(remotePlayer.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(remotePlayer.decreaseDeviceVolumeCalled).isTrue();
remotePlayer.awaitMethodCalled(MockPlayer.METHOD_DECREASE_DEVICE_VOLUME, TIMEOUT_MS);
}
@Test
@ -704,7 +695,9 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.sendCommand(testCommand, testArgs, /* cb= */ null);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
@ -723,13 +716,15 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
.setId("controllerCallback_sessionRejects")
.setSessionCallback(sessionCallback)
.build();
// Session will not accept the controller's commands.
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().play();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
}
@Test
@ -757,10 +752,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().prepareFromUri(mediaUri, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
@Test
@ -788,10 +784,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().playFromUri(request, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
@ -820,10 +817,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().prepareFromMediaId(request, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
@Test
@ -852,10 +850,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().playFromMediaId(mediaId, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
@ -884,10 +883,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().prepareFromSearch(query, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
@Test
@ -916,10 +916,11 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().playFromSearch(query, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
@ -944,7 +945,6 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
}
};
handler.postAndSync(
() -> {
List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(mediaId);
@ -958,7 +958,9 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().setRating(rating);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
@ -991,16 +993,17 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.getTransportControls().pause();
assertThat(latchForPause.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
assertThat(player.pauseCalled).isFalse();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
assertThat(commands).hasSize(1);
assertThat(commands.get(0)).isEqualTo(COMMAND_PLAY_PAUSE);
controller.getTransportControls().prepare();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
assertThat(player.pauseCalled).isFalse();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
assertThat(commands).hasSize(2);
assertThat(commands.get(1)).isEqualTo(COMMAND_PREPARE);
}
@ -1056,7 +1059,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
session = null;
controller.getTransportControls().play();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
// Ensure that the controller cannot use newly create session with the same ID.
// Recreated session has different session stub, so previously created controller
@ -1068,7 +1072,8 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
.build();
controller.getTransportControls().play();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
}
private static class TestSessionCallback implements SessionCallback {

View File

@ -89,8 +89,7 @@ public class MediaSessionKeyEventTest {
Context context = ApplicationProvider.getApplicationContext();
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
handler = threadTestRule.getHandler();
player =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
sessionCallback = new TestSessionCallback();
session = new MediaSession.Builder(context, player).setSessionCallback(sessionCallback).build();
@ -143,43 +142,43 @@ public class MediaSessionKeyEventTest {
@Test
public void playKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
public void pauseKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
}
@Test
public void nextKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
}
@Test
public void previousKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToPreviousCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS, TIMEOUT_MS);
}
@Test
public void stopKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_STOP, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.stopCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_STOP, TIMEOUT_MS);
}
@Test
public void playPauseKeyEvent_play() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
@ -188,18 +187,19 @@ public class MediaSessionKeyEventTest {
() -> {
player.playWhenReady = true;
});
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
}
@Test
public void playPauseKeyEvent_doubleTapIsTranslatedToSkipToNext() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, true);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue();
assertThat(player.playCalled).isFalse();
assertThat(player.pauseCalled).isFalse();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
}
private static class TestSessionCallback implements MediaSession.SessionCallback {

View File

@ -101,7 +101,6 @@ public class MediaSessionPermissionTest {
SessionCommands sessionCommands, Player.Commands playerCommands) {
player =
new MockPlayer.Builder()
.setLatchCount(1)
.setApplicationLooper(threadTestRule.getHandler().getLooper())
.build();
callback =

View File

@ -16,10 +16,8 @@
package androidx.media3.session;
import static androidx.media3.test.session.common.CommonConstants.SUPPORT_APP_PACKAGE_NAME;
import static androidx.media3.test.session.common.TestUtils.LONG_TIMEOUT_MS;
import static androidx.media3.test.session.common.TestUtils.TIMEOUT_MS;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import androidx.media3.common.DeviceInfo;
import androidx.media3.common.MediaItem;
@ -63,7 +61,6 @@ public class MediaSessionPlayerTest {
public void setUp() throws Exception {
player =
new MockPlayer.Builder()
.setLatchCount(1)
.setApplicationLooper(threadTestRule.getHandler().getLooper())
.setMediaItems(/* itemCount= */ 5)
.build();
@ -96,53 +93,56 @@ public class MediaSessionPlayerTest {
@Test
public void play() throws Exception {
controller.play();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
@Test
public void pause() throws Exception {
controller.pause();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
}
@Test
public void prepare() throws Exception {
controller.prepare();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.prepareCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
@Test
public void stop() throws Exception {
controller.stop();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.stopCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_STOP, TIMEOUT_MS);
}
@Test
public void setPlayWhenReady() throws Exception {
boolean testPlayWhenReady = true;
controller.setPlayWhenReady(testPlayWhenReady);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setPlayWhenReadyCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_PLAY_WHEN_READY, TIMEOUT_MS);
assertThat(player.playWhenReady).isEqualTo(testPlayWhenReady);
}
@Test
public void seekToDefaultPosition() throws Exception {
controller.seekToDefaultPosition();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToDefaultPositionCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_DEFAULT_POSITION, TIMEOUT_MS);
}
@Test
public void seekToDefaultPosition_withMediaItemIndex() throws Exception {
int mediaItemIndex = 3;
controller.seekToDefaultPosition(mediaItemIndex);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToDefaultPositionWithMediaItemIndexCalled).isTrue();
player.awaitMethodCalled(
MockPlayer.METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX, TIMEOUT_MS);
assertThat(player.seekMediaItemIndex).isEqualTo(mediaItemIndex);
}
@ -150,8 +150,8 @@ public class MediaSessionPlayerTest {
public void seekTo() throws Exception {
long seekPositionMs = 12125L;
controller.seekTo(seekPositionMs);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO, TIMEOUT_MS);
assertThat(player.seekPositionMs).isEqualTo(seekPositionMs);
}
@ -159,9 +159,10 @@ public class MediaSessionPlayerTest {
public void seekTo_withMediaItemIndex() throws Exception {
int mediaItemIndex = 3;
long seekPositionMs = 12125L;
controller.seekTo(mediaItemIndex, seekPositionMs);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToWithMediaItemIndexCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_WITH_MEDIA_ITEM_INDEX, TIMEOUT_MS);
assertThat(player.seekMediaItemIndex).isEqualTo(mediaItemIndex);
assertThat(player.seekPositionMs).isEqualTo(seekPositionMs);
}
@ -169,8 +170,10 @@ public class MediaSessionPlayerTest {
@Test
public void setPlaybackSpeed() throws Exception {
float testSpeed = 1.5f;
controller.setPlaybackSpeed(testSpeed);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_PLAYBACK_SPEED, TIMEOUT_MS);
assertThat(player.playbackParameters.speed).isEqualTo(testSpeed);
}
@ -178,9 +181,10 @@ public class MediaSessionPlayerTest {
public void setPlaybackParameters() throws Exception {
PlaybackParameters testPlaybackParameters =
new PlaybackParameters(/* speed= */ 1.4f, /* pitch= */ 2.3f);
controller.setPlaybackParameters(testPlaybackParameters);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setPlaybackParametersCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_PLAYBACK_PARAMETERS, TIMEOUT_MS);
assertThat(player.playbackParameters).isEqualTo(testPlaybackParameters);
}
@ -194,8 +198,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItem(item);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.mediaItem).isEqualTo(item);
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
assertThat(player.resetPosition).isEqualTo(resetPosition);
@ -211,8 +214,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItem(item);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.mediaItem).isEqualTo(item);
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
assertThat(player.resetPosition).isEqualTo(resetPosition);
@ -228,8 +230,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItem(item);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.mediaItem).isEqualTo(item);
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
assertThat(player.resetPosition).isEqualTo(resetPosition);
@ -241,8 +242,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItems(items);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.mediaItems).isEqualTo(items);
assertThat(player.resetPosition).isFalse();
}
@ -253,8 +253,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItems(items, /* resetPosition= */ true);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemsWithResetPositionCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION, TIMEOUT_MS);
assertThat(player.mediaItems).isEqualTo(items);
assertThat(player.resetPosition).isTrue();
}
@ -267,8 +266,7 @@ public class MediaSessionPlayerTest {
controller.setMediaItems(items, startMediaItemIndex, startPositionMs);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemsWithStartIndexCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX, TIMEOUT_MS);
assertThat(player.mediaItems).isEqualTo(items);
assertThat(player.startMediaItemIndex).isEqualTo(startMediaItemIndex);
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
@ -279,9 +277,10 @@ public class MediaSessionPlayerTest {
int listSize = 4;
List<MediaItem> list = MediaTestUtils.createMediaItems(listSize);
list.set(2, list.get(1));
controller.setMediaItems(list);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.mediaItems.size()).isEqualTo(listSize);
for (int i = 0; i < listSize; i++) {
assertThat(player.mediaItems.get(i).mediaId).isEqualTo(list.get(i).mediaId);
@ -293,9 +292,8 @@ public class MediaSessionPlayerTest {
int listSize = 5000;
// Make client app to generate a long list, and call setMediaItems() with it.
controller.createAndSetFakeMediaItems(listSize);
assertThat(player.countDownLatch.await(LONG_TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.mediaItems).isNotNull();
assertThat(player.mediaItems.size()).isEqualTo(listSize);
for (int i = 0; i < listSize; i++) {
@ -310,8 +308,7 @@ public class MediaSessionPlayerTest {
controller.setPlaylistMetadata(playlistMetadata);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setPlaylistMetadataCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_PLAYLIST_METADATA, TIMEOUT_MS);
assertThat(player.playlistMetadata).isEqualTo(playlistMetadata);
}
@ -321,8 +318,7 @@ public class MediaSessionPlayerTest {
controller.addMediaItem(mediaItem);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.mediaItem).isEqualTo(mediaItem);
}
@ -333,8 +329,7 @@ public class MediaSessionPlayerTest {
controller.addMediaItem(index, mediaItem);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemWithIndexCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM_WITH_INDEX, TIMEOUT_MS);
assertThat(player.index).isEqualTo(index);
assertThat(player.mediaItem).isEqualTo(mediaItem);
}
@ -346,8 +341,7 @@ public class MediaSessionPlayerTest {
controller.addMediaItems(mediaItems);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.mediaItems).isEqualTo(mediaItems);
}
@ -359,8 +353,7 @@ public class MediaSessionPlayerTest {
controller.addMediaItems(index, mediaItems);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.addMediaItemsWithIndexCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_ADD_MEDIA_ITEMS_WITH_INDEX, TIMEOUT_MS);
assertThat(player.index).isEqualTo(index);
assertThat(player.mediaItems).isEqualTo(mediaItems);
}
@ -371,8 +364,7 @@ public class MediaSessionPlayerTest {
controller.removeMediaItem(index);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.removeMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_REMOVE_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.index).isEqualTo(index);
}
@ -383,8 +375,7 @@ public class MediaSessionPlayerTest {
controller.removeMediaItems(fromIndex, toIndex);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.removeMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_REMOVE_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.fromIndex).isEqualTo(fromIndex);
assertThat(player.toIndex).isEqualTo(toIndex);
}
@ -393,8 +384,7 @@ public class MediaSessionPlayerTest {
public void clearMediaItems() throws Exception {
controller.clearMediaItems();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.clearMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_CLEAR_MEDIA_ITEMS, TIMEOUT_MS);
}
@Test
@ -404,8 +394,7 @@ public class MediaSessionPlayerTest {
controller.moveMediaItem(index, newIndex);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.moveMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_MOVE_MEDIA_ITEM, TIMEOUT_MS);
assertThat(player.index).isEqualTo(index);
assertThat(player.newIndex).isEqualTo(newIndex);
}
@ -418,8 +407,7 @@ public class MediaSessionPlayerTest {
controller.moveMediaItems(fromIndex, toIndex, newIndex);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.moveMediaItemsCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_MOVE_MEDIA_ITEMS, TIMEOUT_MS);
assertThat(player.fromIndex).isEqualTo(fromIndex);
assertThat(player.toIndex).isEqualTo(toIndex);
assertThat(player.newIndex).isEqualTo(newIndex);
@ -428,68 +416,69 @@ public class MediaSessionPlayerTest {
@Test
public void seekToPreviousMediaItem() throws Exception {
controller.seekToPreviousMediaItem();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToPreviousMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM, TIMEOUT_MS);
}
@Test
public void seekToNextMediaItem() throws Exception {
controller.seekToNextMediaItem();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextMediaItemCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT_MEDIA_ITEM, TIMEOUT_MS);
}
@Test
public void seekToPrevious() throws Exception {
controller.seekToPrevious();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToPreviousCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS, TIMEOUT_MS);
}
@Test
public void seekToNext() throws Exception {
controller.seekToNext();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
}
@Test
public void setShuffleModeEnabled() throws Exception {
boolean testShuffleModeEnabled = true;
controller.setShuffleModeEnabled(testShuffleModeEnabled);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setShuffleModeCalled).isTrue();
controller.setShuffleModeEnabled(testShuffleModeEnabled);
player.awaitMethodCalled(MockPlayer.METHOD_SET_SHUFFLE_MODE, TIMEOUT_MS);
assertThat(player.shuffleModeEnabled).isEqualTo(testShuffleModeEnabled);
}
@Test
public void setRepeatMode() throws Exception {
int testRepeatMode = Player.REPEAT_MODE_ALL;
controller.setRepeatMode(testRepeatMode);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setRepeatModeCalled).isTrue();
controller.setRepeatMode(testRepeatMode);
player.awaitMethodCalled(MockPlayer.METHOD_SET_REPEAT_MODE, TIMEOUT_MS);
assertThat(player.repeatMode).isEqualTo(testRepeatMode);
}
@Test
public void setVolume() throws Exception {
float testVolume = .123f;
controller.setVolume(testVolume);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setVolumeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_VOLUME, TIMEOUT_MS);
assertThat(player.volume).isEqualTo(testVolume);
}
@Test
public void setDeviceVolume() throws Exception {
changePlaybackTypeToRemote();
int testVolume = 12;
controller.setDeviceVolume(testVolume);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setDeviceVolumeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_DEVICE_VOLUME, TIMEOUT_MS);
assertThat(player.deviceVolume).isEqualTo(testVolume);
}
@ -499,8 +488,7 @@ public class MediaSessionPlayerTest {
controller.increaseDeviceVolume();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.increaseDeviceVolumeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_INCREASE_DEVICE_VOLUME, TIMEOUT_MS);
}
@Test
@ -509,16 +497,16 @@ public class MediaSessionPlayerTest {
controller.decreaseDeviceVolume();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.decreaseDeviceVolumeCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_DECREASE_DEVICE_VOLUME, TIMEOUT_MS);
}
@Test
public void setDeviceMuted() throws Exception {
player.deviceMuted = false;
controller.setDeviceMuted(true);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setDeviceMutedCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_DEVICE_MUTED, TIMEOUT_MS);
assertThat(player.deviceMuted).isTrue();
}
@ -526,16 +514,14 @@ public class MediaSessionPlayerTest {
public void seekBack() throws Exception {
controller.seekBack();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekBackCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_BACK, TIMEOUT_MS);
}
@Test
public void seekForward() throws Exception {
controller.seekForward();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekForwardCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_FORWARD, TIMEOUT_MS);
}
@Test
@ -545,8 +531,7 @@ public class MediaSessionPlayerTest {
controller.setTrackSelectionParameters(trackSelectionParameters);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.setTrackSelectionParametersCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SET_TRACK_SELECTION_PARAMETERS, TIMEOUT_MS);
assertThat(player.trackSelectionParameters).isEqualTo(trackSelectionParameters);
}

View File

@ -78,8 +78,7 @@ public class MediaSessionTest {
public void setUp() throws Exception {
context = ApplicationProvider.getApplicationContext();
handler = threadTestRule.getHandler();
player =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
session =
sessionTestRule.ensureReleaseAfterTest(
@ -394,8 +393,7 @@ public class MediaSessionTest {
long testSeekPositionMs = 1234;
controllerCompat.getTransportControls().seekTo(testSeekPositionMs);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToCalled).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO, TIMEOUT_MS);
assertThat(player.seekPositionMs).isEqualTo(testSeekPositionMs);
}

View File

@ -44,81 +44,98 @@ public class MockPlayerTest {
@Test
public void play() {
player.play();
assertThat(player.playCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isTrue();
}
@Test
public void pause() {
player.pause();
assertThat(player.pauseCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isTrue();
}
@Test
public void prepare() {
player.prepare();
assertThat(player.prepareCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PREPARE)).isTrue();
}
@Test
public void stop() {
player.stop();
assertThat(player.stopCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_STOP)).isTrue();
}
@Test
public void release() {
player.release();
assertThat(player.releaseCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_RELEASE)).isTrue();
}
@Test
public void setPlayWhenReady() {
boolean testPlayWhenReady = false;
player.setPlayWhenReady(testPlayWhenReady);
assertThat(player.setPlayWhenReadyCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_PLAY_WHEN_READY)).isTrue();
}
@Test
public void seekTo() {
long pos = 1004L;
player.seekTo(pos);
assertThat(player.seekToCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_TO)).isTrue();
assertThat(player.seekPositionMs).isEqualTo(pos);
}
@Test
public void seekBack() {
player.seekBack();
assertThat(player.seekBackCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_BACK)).isTrue();
}
@Test
public void seekForward() {
player.seekForward();
assertThat(player.seekForwardCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_FORWARD)).isTrue();
}
@Test
public void setPlaybackParameters() {
PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */ 1.5f);
player.setPlaybackParameters(playbackParameters);
assertThat(player.setPlaybackParametersCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_PLAYBACK_PARAMETERS)).isTrue();
assertThat(player.playbackParameters).isEqualTo(playbackParameters);
}
@Test
public void setPlaybackSpeed() {
float speed = 1.5f;
player.setPlaybackSpeed(speed);
assertThat(player.setPlaybackSpeedCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_PLAYBACK_SPEED)).isTrue();
assertThat(player.playbackParameters.speed).isEqualTo(speed);
}
@Test
public void setMediaItem() {
MediaItem mediaItem = MediaTestUtils.createMediaItem("setMediaItem");
player.setMediaItem(mediaItem);
assertThat(player.setMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEM)).isTrue();
assertThat(player.mediaItem).isSameInstanceAs(mediaItem);
}
@ -126,8 +143,11 @@ public class MockPlayerTest {
public void setMediaItem_withStartPosition() {
MediaItem mediaItem = MediaTestUtils.createMediaItem("setMediaItem");
long startPositionMs = 321L;
player.setMediaItem(mediaItem, startPositionMs);
assertThat(player.setMediaItemWithStartPositionCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEM_WITH_START_POSITION))
.isTrue();
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
assertThat(player.mediaItem).isSameInstanceAs(mediaItem);
}
@ -136,8 +156,11 @@ public class MockPlayerTest {
public void setMediaItem_withResetPosition() {
MediaItem mediaItem = MediaTestUtils.createMediaItem("setMediaItem");
boolean resetPosition = true;
player.setMediaItem(mediaItem, resetPosition);
assertThat(player.setMediaItemWithResetPositionCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEM_WITH_RESET_POSITION))
.isTrue();
assertThat(player.resetPosition).isEqualTo(resetPosition);
assertThat(player.mediaItem).isEqualTo(mediaItem);
}
@ -145,8 +168,10 @@ public class MockPlayerTest {
@Test
public void setMediaItems() {
List<MediaItem> list = MediaTestUtils.createMediaItems(/* size= */ 2);
player.setMediaItems(list);
assertThat(player.setMediaItemsCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS)).isTrue();
assertThat(player.mediaItems).isSameInstanceAs(list);
}
@ -154,8 +179,11 @@ public class MockPlayerTest {
public void setMediaItems_withResetPosition() {
List<MediaItem> list = MediaTestUtils.createMediaItems(/* size= */ 2);
boolean resetPosition = true;
player.setMediaItems(list, resetPosition);
assertThat(player.setMediaItemsWithResetPositionCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION))
.isTrue();
assertThat(player.resetPosition).isEqualTo(resetPosition);
assertThat(player.mediaItems).isSameInstanceAs(list);
}
@ -165,8 +193,11 @@ public class MockPlayerTest {
List<MediaItem> list = MediaTestUtils.createMediaItems(/* size= */ 2);
int startWindowIndex = 3;
long startPositionMs = 132L;
player.setMediaItems(list, startWindowIndex, startPositionMs);
assertThat(player.setMediaItemsWithStartIndexCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX))
.isTrue();
assertThat(player.startMediaItemIndex).isEqualTo(startWindowIndex);
assertThat(player.startPositionMs).isEqualTo(startPositionMs);
assertThat(player.mediaItems).isSameInstanceAs(list);
@ -176,8 +207,10 @@ public class MockPlayerTest {
public void setMediaItems_withDuplicatedItems() {
List<MediaItem> list = MediaTestUtils.createMediaItems(/* size= */ 4);
list.set(2, list.get(1));
player.setMediaItems(list);
assertThat(player.setMediaItemsCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_MEDIA_ITEMS)).isTrue();
assertThat(player.mediaItems).isSameInstanceAs(list);
}
@ -187,7 +220,7 @@ public class MockPlayerTest {
player.setPlaylistMetadata(playlistMetadata);
assertThat(player.setPlaylistMetadataCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_PLAYLIST_METADATA)).isTrue();
assertThat(player.playlistMetadata).isSameInstanceAs(playlistMetadata);
}
@ -197,7 +230,7 @@ public class MockPlayerTest {
player.addMediaItem(mediaItem);
assertThat(player.addMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM)).isTrue();
assertThat(player.mediaItem).isSameInstanceAs(mediaItem);
}
@ -208,7 +241,7 @@ public class MockPlayerTest {
player.addMediaItem(index, mediaItem);
assertThat(player.addMediaItemWithIndexCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_ADD_MEDIA_ITEM_WITH_INDEX)).isTrue();
assertThat(player.index).isEqualTo(index);
assertThat(player.mediaItem).isSameInstanceAs(mediaItem);
}
@ -221,7 +254,7 @@ public class MockPlayerTest {
player.addMediaItems(index, mediaItems);
assertThat(player.addMediaItemsWithIndexCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_ADD_MEDIA_ITEMS_WITH_INDEX)).isTrue();
assertThat(player.index).isEqualTo(index);
assertThat(player.mediaItems).isSameInstanceAs(mediaItems);
}
@ -234,7 +267,7 @@ public class MockPlayerTest {
player.addMediaItems(index, mediaItems);
assertThat(player.addMediaItemsWithIndexCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_ADD_MEDIA_ITEMS_WITH_INDEX)).isTrue();
assertThat(player.index).isEqualTo(index);
assertThat(player.mediaItems).isSameInstanceAs(mediaItems);
}
@ -245,7 +278,7 @@ public class MockPlayerTest {
player.removeMediaItem(index);
assertThat(player.removeMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_REMOVE_MEDIA_ITEM)).isTrue();
assertThat(player.index).isEqualTo(index);
}
@ -256,7 +289,7 @@ public class MockPlayerTest {
player.removeMediaItems(fromIndex, toIndex);
assertThat(player.removeMediaItemsCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_REMOVE_MEDIA_ITEMS)).isTrue();
assertThat(player.fromIndex).isEqualTo(fromIndex);
assertThat(player.toIndex).isEqualTo(toIndex);
}
@ -265,7 +298,7 @@ public class MockPlayerTest {
public void clearMediaItems() {
player.clearMediaItems();
assertThat(player.clearMediaItemsCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_CLEAR_MEDIA_ITEMS)).isTrue();
}
@Test
@ -275,7 +308,7 @@ public class MockPlayerTest {
player.moveMediaItem(index, newIndex);
assertThat(player.moveMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_MOVE_MEDIA_ITEM)).isTrue();
assertThat(player.index).isEqualTo(index);
assertThat(player.newIndex).isEqualTo(newIndex);
}
@ -288,7 +321,7 @@ public class MockPlayerTest {
player.moveMediaItems(fromIndex, toIndex, newIndex);
assertThat(player.moveMediaItemsCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_MOVE_MEDIA_ITEMS)).isTrue();
assertThat(player.fromIndex).isEqualTo(fromIndex);
assertThat(player.toIndex).isEqualTo(toIndex);
assertThat(player.newIndex).isEqualTo(newIndex);
@ -297,76 +330,92 @@ public class MockPlayerTest {
@Test
public void seekToPreviousMediaItem() {
player.seekToPreviousMediaItem();
assertThat(player.seekToPreviousMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM)).isTrue();
}
@Test
public void seekToNextMediaItem() {
player.seekToNextMediaItem();
assertThat(player.seekToNextMediaItemCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_TO_NEXT_MEDIA_ITEM)).isTrue();
}
@Test
public void seekToPrevious() {
player.seekToPrevious();
assertThat(player.seekToPreviousCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS)).isTrue();
}
@Test
public void seekToNext() {
player.seekToNext();
assertThat(player.seekToNextCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SEEK_TO_NEXT)).isTrue();
}
@Test
public void setShuffleModeEnabled() {
boolean testShuffleModeEnabled = true;
player.setShuffleModeEnabled(testShuffleModeEnabled);
assertThat(player.setShuffleModeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_SHUFFLE_MODE)).isTrue();
assertThat(player.shuffleModeEnabled).isEqualTo(testShuffleModeEnabled);
}
@Test
public void setRepeatMode() {
int testRepeatMode = Player.REPEAT_MODE_ALL;
player.setRepeatMode(testRepeatMode);
assertThat(player.setRepeatModeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_REPEAT_MODE)).isTrue();
assertThat(player.repeatMode).isEqualTo(testRepeatMode);
}
@Test
public void setVolume() {
float testVolume = .123f;
player.setVolume(testVolume);
assertThat(player.setVolumeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_VOLUME)).isTrue();
assertThat(player.volume).isEqualTo(testVolume);
}
@Test
public void setDeviceVolume() {
int testVolume = 12;
player.setDeviceVolume(testVolume);
assertThat(player.setDeviceVolumeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_DEVICE_VOLUME)).isTrue();
assertThat(player.deviceVolume).isEqualTo(testVolume);
}
@Test
public void increaseDeviceVolume() {
player.increaseDeviceVolume();
assertThat(player.increaseDeviceVolumeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_INCREASE_DEVICE_VOLUME)).isTrue();
}
@Test
public void decreaseDeviceVolume() {
player.decreaseDeviceVolume();
assertThat(player.decreaseDeviceVolumeCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_DECREASE_DEVICE_VOLUME)).isTrue();
}
@Test
public void setDeviceMuted() {
player.deviceMuted = false;
player.setDeviceMuted(true);
assertThat(player.setDeviceMutedCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_DEVICE_MUTED)).isTrue();
assertThat(player.deviceMuted).isTrue();
}
@ -377,7 +426,8 @@ public class MockPlayerTest {
player.setTrackSelectionParameters(trackSelectionParameters);
assertThat(player.setTrackSelectionParametersCalled).isTrue();
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_SET_TRACK_SELECTION_PARAMETERS))
.isTrue();
assertThat(player.trackSelectionParameters).isSameInstanceAs(trackSelectionParameters);
}
}

View File

@ -134,8 +134,7 @@ public class MockMediaLibraryService extends MediaLibraryService {
return (MediaLibrarySession) onGetSessionHandler.onGetSession(controllerInfo);
}
MockPlayer player =
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
MockPlayer player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
MediaLibrarySessionCallback callback = registry.getSessionCallback();
session =

View File

@ -15,11 +15,15 @@
*/
package androidx.media3.session;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static java.lang.annotation.ElementType.TYPE_USE;
import android.os.Looper;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import androidx.media3.common.AudioAttributes;
@ -35,20 +39,162 @@ import androidx.media3.common.TrackSelectionParameters;
import androidx.media3.common.TracksInfo;
import androidx.media3.common.VideoSize;
import androidx.media3.common.text.Cue;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;
/** A mock implementation of {@link Player} for testing. */
@UnstableApi
public class MockPlayer implements Player {
public final CountDownLatch countDownLatch;
/** Player methods. */
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
METHOD_ADD_MEDIA_ITEM,
METHOD_ADD_MEDIA_ITEMS,
METHOD_ADD_MEDIA_ITEM_WITH_INDEX,
METHOD_ADD_MEDIA_ITEMS_WITH_INDEX,
METHOD_CLEAR_MEDIA_ITEMS,
METHOD_DECREASE_DEVICE_VOLUME,
METHOD_INCREASE_DEVICE_VOLUME,
METHOD_MOVE_MEDIA_ITEM,
METHOD_MOVE_MEDIA_ITEMS,
METHOD_PAUSE,
METHOD_PLAY,
METHOD_PREPARE,
METHOD_RELEASE,
METHOD_REMOVE_MEDIA_ITEM,
METHOD_REMOVE_MEDIA_ITEMS,
METHOD_SEEK_BACK,
METHOD_SEEK_FORWARD,
METHOD_SEEK_TO,
METHOD_SEEK_TO_DEFAULT_POSITION,
METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX,
METHOD_SEEK_TO_NEXT,
METHOD_SEEK_TO_NEXT_MEDIA_ITEM,
METHOD_SEEK_TO_PREVIOUS,
METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM,
METHOD_SEEK_TO_WITH_MEDIA_ITEM_INDEX,
METHOD_SET_DEVICE_MUTED,
METHOD_SET_DEVICE_VOLUME,
METHOD_SET_MEDIA_ITEM,
METHOD_SET_MEDIA_ITEM_WITH_RESET_POSITION,
METHOD_SET_MEDIA_ITEM_WITH_START_POSITION,
METHOD_SET_MEDIA_ITEMS,
METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION,
METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX,
METHOD_SET_PLAY_WHEN_READY,
METHOD_SET_PLAYBACK_PARAMETERS,
METHOD_SET_PLAYBACK_SPEED,
METHOD_SET_PLAYLIST_METADATA,
METHOD_SET_REPEAT_MODE,
METHOD_SET_SHUFFLE_MODE,
METHOD_SET_TRACK_SELECTION_PARAMETERS,
METHOD_SET_VOLUME,
METHOD_STOP
})
public @interface Method {}
/** Maps to {@link Player#addMediaItem(MediaItem)}. */
public static final int METHOD_ADD_MEDIA_ITEM = 0;
/** Maps to {@link Player#addMediaItems(List)}. */
public static final int METHOD_ADD_MEDIA_ITEMS = 1;
/** Maps to {@link Player#addMediaItem(int, MediaItem)}. */
public static final int METHOD_ADD_MEDIA_ITEM_WITH_INDEX = 2;
/** Maps to {@link Player#addMediaItems(int, List)}. */
public static final int METHOD_ADD_MEDIA_ITEMS_WITH_INDEX = 3;
/** Maps to {@link Player#clearMediaItems()}. */
public static final int METHOD_CLEAR_MEDIA_ITEMS = 4;
/** Maps to {@link Player#decreaseDeviceVolume()}. */
public static final int METHOD_DECREASE_DEVICE_VOLUME = 5;
/** Maps to {@link Player#increaseDeviceVolume()}. */
public static final int METHOD_INCREASE_DEVICE_VOLUME = 6;
/** Maps to {@link Player#moveMediaItem(int, int)}. */
public static final int METHOD_MOVE_MEDIA_ITEM = 7;
/** Maps to {@link Player#moveMediaItems(int, int, int)}. */
public static final int METHOD_MOVE_MEDIA_ITEMS = 8;
/** Maps to {@link Player#pause()}. */
public static final int METHOD_PAUSE = 9;
/** Maps to {@link Player#play()}. */
public static final int METHOD_PLAY = 10;
/** Maps to {@link Player#prepare()}. */
public static final int METHOD_PREPARE = 11;
/** Maps to {@link Player#release()}. */
public static final int METHOD_RELEASE = 12;
/** Maps to {@link Player#removeMediaItem(int)}. */
public static final int METHOD_REMOVE_MEDIA_ITEM = 13;
/** Maps to {@link Player#removeMediaItems(int, int)}. */
public static final int METHOD_REMOVE_MEDIA_ITEMS = 14;
/** Maps to {@link Player#seekBack()}. */
public static final int METHOD_SEEK_BACK = 15;
/** Maps to {@link Player#seekForward()}. */
public static final int METHOD_SEEK_FORWARD = 16;
/** Maps to {@link Player#seekTo(long)}. */
public static final int METHOD_SEEK_TO = 17;
/** Maps to {@link Player#seekToDefaultPosition()}. */
public static final int METHOD_SEEK_TO_DEFAULT_POSITION = 18;
/** Maps to {@link Player#seekToDefaultPosition(int)}. */
public static final int METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX = 19;
/** Maps to {@link Player#seekToNext()}. */
public static final int METHOD_SEEK_TO_NEXT = 20;
/** Maps to {@link Player#seekToNextMediaItem()}. */
public static final int METHOD_SEEK_TO_NEXT_MEDIA_ITEM = 21;
/** Maps to {@link Player#seekToPrevious()}. */
public static final int METHOD_SEEK_TO_PREVIOUS = 22;
/** Maps to {@link Player#seekToPreviousMediaItem()}. */
public static final int METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM = 23;
/** Maps to {@link Player#seekTo(int, long)}. */
public static final int METHOD_SEEK_TO_WITH_MEDIA_ITEM_INDEX = 24;
/** Maps to {@link Player#setDeviceMuted(boolean)}. */
public static final int METHOD_SET_DEVICE_MUTED = 25;
/** Maps to {@link Player#setDeviceVolume(int)}. */
public static final int METHOD_SET_DEVICE_VOLUME = 26;
/** Maps to {@link Player#setMediaItem(MediaItem)}. */
public static final int METHOD_SET_MEDIA_ITEM = 27;
/** Maps to {@link Player#setMediaItem(MediaItem, boolean)}. */
public static final int METHOD_SET_MEDIA_ITEM_WITH_RESET_POSITION = 28;
/** Maps to {@link Player#setMediaItem(MediaItem, long)}. */
public static final int METHOD_SET_MEDIA_ITEM_WITH_START_POSITION = 29;
/** Maps to {@link Player#setMediaItems(List)}. */
public static final int METHOD_SET_MEDIA_ITEMS = 30;
/** Maps to {@link Player#setMediaItems(List, boolean)}. */
public static final int METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION = 31;
/** Maps to {@link Player#setMediaItems(List, int, long)}. */
public static final int METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX = 32;
/** Maps to {@link Player#setPlayWhenReady(boolean)}. */
public static final int METHOD_SET_PLAY_WHEN_READY = 33;
/** Maps to {@link Player#setPlaybackParameters(PlaybackParameters)}. */
public static final int METHOD_SET_PLAYBACK_PARAMETERS = 34;
/** Maps to {@link Player#setPlaybackSpeed(float)}. */
public static final int METHOD_SET_PLAYBACK_SPEED = 35;
/** Maps to {@link Player#setPlaylistMetadata(MediaMetadata)}. */
public static final int METHOD_SET_PLAYLIST_METADATA = 36;
/** Maps to {@link Player#setRepeatMode(int)}. */
public static final int METHOD_SET_REPEAT_MODE = 37;
/** Maps to {@link Player#setShuffleModeEnabled(boolean)}. */
public static final int METHOD_SET_SHUFFLE_MODE = 38;
/** Maps to {@link Player#setTrackSelectionParameters(TrackSelectionParameters)}. */
public static final int METHOD_SET_TRACK_SELECTION_PARAMETERS = 39;
/** Maps to {@link Player#setVolume(float)}. */
public static final int METHOD_SET_VOLUME = 40;
/** Maps to {@link Player#stop()}. */
public static final int METHOD_STOP = 41;
private final boolean changePlayerStateWithTransportControl;
private final Looper applicationLooper;
private final ArraySet<Listener> listeners = new ArraySet<>();
private final ImmutableMap<@Method Integer, ConditionVariable> conditionVariables =
createMethodConditionVariables();
@Nullable PlaybackException playerError;
public AudioAttributes audioAttributes;
@ -104,51 +250,7 @@ public class MockPlayer implements Player {
public long maxSeekToPreviousPositionMs;
public TrackSelectionParameters trackSelectionParameters;
public boolean playCalled;
public boolean pauseCalled;
public boolean prepareCalled;
public boolean stopCalled;
public boolean releaseCalled;
public boolean seekToDefaultPositionCalled;
public boolean seekToDefaultPositionWithMediaItemIndexCalled;
public boolean seekToCalled;
public boolean seekToWithMediaItemIndexCalled;
public boolean setPlaybackSpeedCalled;
public boolean setPlaybackParametersCalled;
public boolean setMediaItemCalled;
public boolean setMediaItemWithStartPositionCalled;
public boolean setMediaItemWithResetPositionCalled;
public boolean setMediaItemsCalled;
public boolean setMediaItemsWithResetPositionCalled;
public boolean setMediaItemsWithStartIndexCalled;
public boolean setPlaylistMetadataCalled;
public boolean addMediaItemCalled;
public boolean addMediaItemWithIndexCalled;
public boolean addMediaItemsCalled;
public boolean addMediaItemsWithIndexCalled;
public boolean removeMediaItemCalled;
public boolean removeMediaItemsCalled;
public boolean clearMediaItemsCalled;
public boolean moveMediaItemCalled;
public boolean moveMediaItemsCalled;
public boolean seekToPreviousMediaItemCalled;
public boolean seekToNextMediaItemCalled;
public boolean seekToPreviousCalled;
public boolean seekToNextCalled;
public boolean setRepeatModeCalled;
public boolean setShuffleModeCalled;
public boolean setVolumeCalled;
public boolean setDeviceVolumeCalled;
public boolean increaseDeviceVolumeCalled;
public boolean decreaseDeviceVolumeCalled;
public boolean setDeviceMutedCalled;
public boolean setPlayWhenReadyCalled;
public boolean seekBackCalled;
public boolean seekForwardCalled;
public boolean setTrackSelectionParametersCalled;
private MockPlayer(Builder builder) {
countDownLatch = new CountDownLatch(builder.latchCount);
changePlayerStateWithTransportControl = builder.changePlayerStateWithTransportControl;
applicationLooper = builder.applicationLooper;
@ -201,13 +303,12 @@ public class MockPlayer implements Player {
@Override
public void release() {
releaseCalled = true;
checkNotNull(conditionVariables.get(METHOD_RELEASE)).open();
}
@Override
public void stop() {
stopCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_STOP)).open();
}
@Deprecated
@ -234,8 +335,7 @@ public class MockPlayer implements Player {
@Override
public void play() {
playCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_PLAY)).open();
if (changePlayerStateWithTransportControl) {
notifyPlayWhenReadyChanged(
/* playWhenReady= */ true, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST);
@ -244,8 +344,7 @@ public class MockPlayer implements Player {
@Override
public void pause() {
pauseCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_PAUSE)).open();
if (changePlayerStateWithTransportControl) {
notifyPlayWhenReadyChanged(
/* playWhenReady= */ false, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST);
@ -254,8 +353,7 @@ public class MockPlayer implements Player {
@Override
public void prepare() {
prepareCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_PREPARE)).open();
if (changePlayerStateWithTransportControl) {
notifyPlaybackStateChanged(Player.STATE_READY);
}
@ -263,30 +361,27 @@ public class MockPlayer implements Player {
@Override
public void seekToDefaultPosition() {
seekToDefaultPositionCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_DEFAULT_POSITION)).open();
}
@Override
public void seekToDefaultPosition(int mediaItemIndex) {
seekToDefaultPositionWithMediaItemIndexCalled = true;
seekMediaItemIndex = mediaItemIndex;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX))
.open();
}
@Override
public void seekTo(long positionMs) {
seekToCalled = true;
seekPositionMs = positionMs;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO)).open();
}
@Override
public void seekTo(int mediaItemIndex, long positionMs) {
seekToWithMediaItemIndexCalled = true;
seekMediaItemIndex = mediaItemIndex;
seekPositionMs = positionMs;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_WITH_MEDIA_ITEM_INDEX)).open();
}
@Override
@ -296,8 +391,7 @@ public class MockPlayer implements Player {
@Override
public void seekBack() {
seekBackCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_BACK)).open();
}
@Override
@ -307,8 +401,7 @@ public class MockPlayer implements Player {
@Override
public void seekForward() {
seekForwardCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_FORWARD)).open();
}
@Override
@ -506,16 +599,14 @@ public class MockPlayer implements Player {
@Override
public void setPlaybackParameters(PlaybackParameters playbackParameters) {
setPlaybackParametersCalled = true;
this.playbackParameters = playbackParameters;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_PLAYBACK_PARAMETERS)).open();
}
@Override
public void setPlaybackSpeed(float speed) {
setPlaybackSpeedCalled = true;
playbackParameters = new PlaybackParameters(speed);
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_PLAYBACK_SPEED)).open();
}
@Override
@ -525,9 +616,8 @@ public class MockPlayer implements Player {
@Override
public void setVolume(float volume) {
setVolumeCalled = true;
this.volume = volume;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_VOLUME)).open();
}
@Override
@ -552,35 +642,30 @@ public class MockPlayer implements Player {
@Override
public void setDeviceVolume(int volume) {
setDeviceVolumeCalled = true;
deviceVolume = volume;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_DEVICE_VOLUME)).open();
}
@Override
public void increaseDeviceVolume() {
increaseDeviceVolumeCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_INCREASE_DEVICE_VOLUME)).open();
}
@Override
public void decreaseDeviceVolume() {
decreaseDeviceVolumeCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_DECREASE_DEVICE_VOLUME)).open();
}
@Override
public void setDeviceMuted(boolean muted) {
setDeviceMutedCalled = true;
deviceMuted = muted;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_DEVICE_MUTED)).open();
}
@Override
public void setPlayWhenReady(boolean playWhenReady) {
this.setPlayWhenReadyCalled = true;
this.playWhenReady = playWhenReady;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_PLAY_WHEN_READY)).open();
}
@Override
@ -624,49 +709,43 @@ public class MockPlayer implements Player {
@Override
public void setMediaItem(MediaItem mediaItem) {
setMediaItemCalled = true;
this.mediaItem = mediaItem;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEM)).open();
}
@Override
public void setMediaItem(MediaItem mediaItem, long startPositionMs) {
setMediaItemWithStartPositionCalled = true;
this.mediaItem = mediaItem;
this.startPositionMs = startPositionMs;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEM_WITH_START_POSITION)).open();
}
@Override
public void setMediaItem(MediaItem mediaItem, boolean resetPosition) {
setMediaItemWithResetPositionCalled = true;
this.mediaItem = mediaItem;
this.resetPosition = resetPosition;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEM_WITH_RESET_POSITION)).open();
}
@Override
public void setMediaItems(List<MediaItem> mediaItems) {
setMediaItemsCalled = true;
this.mediaItems = mediaItems;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEMS)).open();
}
@Override
public void setMediaItems(List<MediaItem> mediaItems, boolean resetPosition) {
setMediaItemsWithResetPositionCalled = true;
this.mediaItems = mediaItems;
this.resetPosition = resetPosition;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION)).open();
}
@Override
public void setMediaItems(List<MediaItem> mediaItems, int startIndex, long startPositionMs) {
setMediaItemsWithStartIndexCalled = true;
this.mediaItems = mediaItems;
this.startMediaItemIndex = startIndex;
this.startPositionMs = startPositionMs;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX)).open();
}
@Override
@ -676,9 +755,8 @@ public class MockPlayer implements Player {
@Override
public void setPlaylistMetadata(MediaMetadata playlistMetadata) {
setPlaylistMetadataCalled = true;
this.playlistMetadata = playlistMetadata;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_PLAYLIST_METADATA)).open();
}
@Deprecated
@ -773,70 +851,61 @@ public class MockPlayer implements Player {
@Override
public void addMediaItem(MediaItem mediaItem) {
addMediaItemCalled = true;
this.mediaItem = mediaItem;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_ADD_MEDIA_ITEM)).open();
}
@Override
public void addMediaItem(int index, MediaItem mediaItem) {
addMediaItemWithIndexCalled = true;
this.index = index;
this.mediaItem = mediaItem;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_ADD_MEDIA_ITEM_WITH_INDEX)).open();
}
@Override
public void addMediaItems(List<MediaItem> mediaItems) {
addMediaItemsCalled = true;
this.mediaItems = mediaItems;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_ADD_MEDIA_ITEMS)).open();
}
@Override
public void addMediaItems(int index, List<MediaItem> mediaItems) {
addMediaItemsWithIndexCalled = true;
this.index = index;
this.mediaItems = mediaItems;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_ADD_MEDIA_ITEMS_WITH_INDEX)).open();
}
@Override
public void removeMediaItem(int index) {
removeMediaItemCalled = true;
this.index = index;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_REMOVE_MEDIA_ITEM)).open();
}
@Override
public void removeMediaItems(int fromIndex, int toIndex) {
removeMediaItemsCalled = true;
this.fromIndex = fromIndex;
this.toIndex = toIndex;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_REMOVE_MEDIA_ITEMS)).open();
}
@Override
public void clearMediaItems() {
clearMediaItemsCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_CLEAR_MEDIA_ITEMS)).open();
}
@Override
public void moveMediaItem(int currentIndex, int newIndex) {
moveMediaItemCalled = true;
this.index = currentIndex;
this.newIndex = newIndex;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_MOVE_MEDIA_ITEM)).open();
}
@Override
public void moveMediaItems(int fromIndex, int toIndex, int newIndex) {
moveMediaItemsCalled = true;
this.fromIndex = fromIndex;
this.toIndex = toIndex;
this.newIndex = newIndex;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_MOVE_MEDIA_ITEMS)).open();
}
@Deprecated
@ -899,20 +968,17 @@ public class MockPlayer implements Player {
@Override
public void seekToPreviousMediaItem() {
seekToPreviousMediaItemCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM)).open();
}
@Override
public void seekToNextMediaItem() {
seekToNextMediaItemCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_NEXT_MEDIA_ITEM)).open();
}
@Override
public void seekToPrevious() {
seekToPreviousCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_PREVIOUS)).open();
}
@Override
@ -922,8 +988,7 @@ public class MockPlayer implements Player {
@Override
public void seekToNext() {
seekToNextCalled = true;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SEEK_TO_NEXT)).open();
}
@Override
@ -933,9 +998,8 @@ public class MockPlayer implements Player {
@Override
public void setRepeatMode(int repeatMode) {
setRepeatModeCalled = true;
this.repeatMode = repeatMode;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_REPEAT_MODE)).open();
}
@Override
@ -945,9 +1009,8 @@ public class MockPlayer implements Player {
@Override
public void setShuffleModeEnabled(boolean shuffleModeEnabled) {
setShuffleModeCalled = true;
this.shuffleModeEnabled = shuffleModeEnabled;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_SHUFFLE_MODE)).open();
}
@Override
@ -999,9 +1062,6 @@ public class MockPlayer implements Player {
@Override
public VideoSize getVideoSize() {
if (videoSize == null) {
videoSize = VideoSize.UNKNOWN;
}
return videoSize;
}
@ -1122,9 +1182,8 @@ public class MockPlayer implements Player {
@Override
public void setTrackSelectionParameters(TrackSelectionParameters parameters) {
setTrackSelectionParametersCalled = true;
trackSelectionParameters = parameters;
countDownLatch.countDown();
checkNotNull(conditionVariables.get(METHOD_SET_TRACK_SELECTION_PARAMETERS)).open();
}
@Override
@ -1132,10 +1191,73 @@ public class MockPlayer implements Player {
return applicationLooper;
}
/** Returns whether {@code method} has been called at least once. */
public boolean hasMethodBeenCalled(@Method int method) {
return checkNotNull(conditionVariables.get(method)).isOpen();
}
/**
* Awaits up to {@code timeOutMs} until {@code method} is called, otherwise throws a {@link
* TimeoutException}.
*/
public void awaitMethodCalled(@Method int method, long timeOutMs)
throws TimeoutException, InterruptedException {
if (!checkNotNull(conditionVariables.get(method)).block(timeOutMs)) {
throw new TimeoutException(
Util.formatInvariant("Method %d not called after %f ms", method, timeOutMs));
}
}
private static ImmutableMap<@Method Integer, ConditionVariable> createMethodConditionVariables() {
return new ImmutableMap.Builder<@Method Integer, ConditionVariable>()
.put(METHOD_ADD_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_ADD_MEDIA_ITEMS, new ConditionVariable())
.put(METHOD_ADD_MEDIA_ITEM_WITH_INDEX, new ConditionVariable())
.put(METHOD_ADD_MEDIA_ITEMS_WITH_INDEX, new ConditionVariable())
.put(METHOD_CLEAR_MEDIA_ITEMS, new ConditionVariable())
.put(METHOD_DECREASE_DEVICE_VOLUME, new ConditionVariable())
.put(METHOD_INCREASE_DEVICE_VOLUME, new ConditionVariable())
.put(METHOD_MOVE_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_MOVE_MEDIA_ITEMS, new ConditionVariable())
.put(METHOD_PAUSE, new ConditionVariable())
.put(METHOD_PLAY, new ConditionVariable())
.put(METHOD_PREPARE, new ConditionVariable())
.put(METHOD_RELEASE, new ConditionVariable())
.put(METHOD_REMOVE_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_REMOVE_MEDIA_ITEMS, new ConditionVariable())
.put(METHOD_SEEK_BACK, new ConditionVariable())
.put(METHOD_SEEK_FORWARD, new ConditionVariable())
.put(METHOD_SEEK_TO, new ConditionVariable())
.put(METHOD_SEEK_TO_DEFAULT_POSITION, new ConditionVariable())
.put(METHOD_SEEK_TO_DEFAULT_POSITION_WITH_MEDIA_ITEM_INDEX, new ConditionVariable())
.put(METHOD_SEEK_TO_NEXT, new ConditionVariable())
.put(METHOD_SEEK_TO_NEXT_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_SEEK_TO_PREVIOUS, new ConditionVariable())
.put(METHOD_SEEK_TO_PREVIOUS_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_SEEK_TO_WITH_MEDIA_ITEM_INDEX, new ConditionVariable())
.put(METHOD_SET_DEVICE_MUTED, new ConditionVariable())
.put(METHOD_SET_DEVICE_VOLUME, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEM, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEM_WITH_RESET_POSITION, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEM_WITH_START_POSITION, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEMS, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEMS_WITH_RESET_POSITION, new ConditionVariable())
.put(METHOD_SET_MEDIA_ITEMS_WITH_START_INDEX, new ConditionVariable())
.put(METHOD_SET_PLAY_WHEN_READY, new ConditionVariable())
.put(METHOD_SET_PLAYBACK_PARAMETERS, new ConditionVariable())
.put(METHOD_SET_PLAYBACK_SPEED, new ConditionVariable())
.put(METHOD_SET_PLAYLIST_METADATA, new ConditionVariable())
.put(METHOD_SET_REPEAT_MODE, new ConditionVariable())
.put(METHOD_SET_SHUFFLE_MODE, new ConditionVariable())
.put(METHOD_SET_TRACK_SELECTION_PARAMETERS, new ConditionVariable())
.put(METHOD_SET_VOLUME, new ConditionVariable())
.put(METHOD_STOP, new ConditionVariable())
.buildOrThrow();
}
/** Builder for {@link MockPlayer}. */
public static final class Builder {
private int latchCount;
private boolean changePlayerStateWithTransportControl;
private Looper applicationLooper;
private int itemCount;
@ -1144,11 +1266,6 @@ public class MockPlayer implements Player {
applicationLooper = Util.getCurrentOrMainLooper();
}
public Builder setLatchCount(int latchCount) {
this.latchCount = latchCount;
return this;
}
public Builder setChangePlayerStateWithTransportControl(
boolean changePlayerStateWithTransportControl) {
this.changePlayerStateWithTransportControl = changePlayerStateWithTransportControl;