Skip to content

Commit

Permalink
Split test methods more clearly in arrange, act, assert.
Browse files Browse the repository at this point in the history
The tests in MediaControllerCompatCallbackWithMediaSessionTest
don't follow this pattern very consistently at the moment.

PiperOrigin-RevId: 487501913
(cherry picked from commit c6a0ba2)
  • Loading branch information
tonihei authored and microkatz committed Nov 10, 2022
1 parent 58d670d commit a9eb121
Showing 1 changed file with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ public void onPlaybackStateChanged(PlaybackStateCompat state) {
@Test
public void repeatModeChange() throws Exception {
@PlaybackStateCompat.RepeatMode int testRepeatMode = PlaybackStateCompat.REPEAT_MODE_ALL;

CountDownLatch latch = new CountDownLatch(1);
AtomicInteger repeatModeRef = new AtomicInteger();
MediaControllerCompat.Callback callback =
Expand All @@ -242,6 +241,7 @@ public void onRepeatModeChanged(@PlaybackStateCompat.RepeatMode int repeatMode)

session.getMockPlayer().setRepeatMode(Player.REPEAT_MODE_ALL);
session.getMockPlayer().notifyRepeatModeChanged();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(repeatModeRef.get()).isEqualTo(testRepeatMode);
assertThat(controllerCompat.getRepeatMode()).isEqualTo(testRepeatMode);
Expand All @@ -251,7 +251,6 @@ public void onRepeatModeChanged(@PlaybackStateCompat.RepeatMode int repeatMode)
public void shuffleModeChange() throws Exception {
@PlaybackStateCompat.ShuffleMode
int testShuffleModeEnabled = PlaybackStateCompat.SHUFFLE_MODE_ALL;

CountDownLatch latch = new CountDownLatch(1);
AtomicInteger shuffleModeRef = new AtomicInteger();
MediaControllerCompat.Callback callback =
Expand All @@ -266,6 +265,7 @@ public void onShuffleModeChanged(@PlaybackStateCompat.ShuffleMode int shuffleMod

session.getMockPlayer().setShuffleModeEnabled(/* shuffleModeEnabled= */ true);
session.getMockPlayer().notifyShuffleModeEnabledChanged();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(shuffleModeRef.get()).isEqualTo(testShuffleModeEnabled);
assertThat(controllerCompat.getShuffleMode()).isEqualTo(testShuffleModeEnabled);
Expand All @@ -284,6 +284,7 @@ public void onSessionDestroyed() {
controllerCompat.registerCallback(callback, handler);

session.release();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}

Expand Down Expand Up @@ -312,7 +313,6 @@ public void setPlayer_isNotified() throws Exception {
new MediaMetadata.Builder().setTitle(testPlaylistTitle).build();
boolean testShuffleModeEnabled = true;
@RepeatMode int testRepeatMode = Player.REPEAT_MODE_ONE;

AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
AtomicReference<MediaMetadataCompat> metadataRef = new AtomicReference<>();
AtomicReference<CharSequence> queueTitleRef = new AtomicReference<>();
Expand Down Expand Up @@ -361,7 +361,6 @@ public void onShuffleModeChanged(int shuffleMode) {
}
};
controllerCompat.registerCallback(callback, handler);

Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setPlaybackState(testState)
Expand All @@ -376,13 +375,13 @@ public void onShuffleModeChanged(int shuffleMode) {
.setShuffleModeEnabled(testShuffleModeEnabled)
.setRepeatMode(testRepeatMode)
.build();

session.setPlayer(playerConfig);

assertThat(latchForPlaybackState.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getBufferedPosition()).isEqualTo(testBufferedPositionMs);
assertThat(playbackStateRef.get().getPosition()).isEqualTo(testCurrentPositionMs);
assertThat(playbackStateRef.get().getPlaybackSpeed()).isEqualTo(playbackParameters.speed);

assertThat(latchForMetadata.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(metadataRef.get().getString(METADATA_KEY_MEDIA_ID))
.isEqualTo(testMediaItems.get(testItemIndex).mediaId);
Expand All @@ -393,7 +392,6 @@ public void onShuffleModeChanged(int shuffleMode) {
playbackStateRef.get(), metadataRef.get(), /* timeDiffMs= */ C.TIME_UNSET);
assertThat(playbackStateFromControllerCompat).isEqualTo(testState);
assertThat(metadataRef.get().getRating(METADATA_KEY_USER_RATING).hasHeart()).isTrue();

assertThat(latchForQueue.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
List<QueueItem> queue = controllerCompat.getQueue();
assertThat(queue).hasSize(testTimeline.getWindowCount());
Expand All @@ -402,7 +400,6 @@ public void onShuffleModeChanged(int shuffleMode) {
.isEqualTo(testMediaItems.get(i).mediaId);
}
assertThat(queueTitleRef.get().toString()).isEqualTo(testPlaylistTitle);

assertThat(latchForShuffleMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
assertThat(shuffleModeRef.get()).isEqualTo(PlaybackStateCompat.SHUFFLE_MODE_ALL);
assertThat(latchForRepeatMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
Expand All @@ -415,7 +412,6 @@ public void setPlayer_playbackTypeChangedToRemote() throws Exception {
new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */ 0, /* maxVolume= */ 25);
int legacyPlaybackType = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_REMOTE;
int deviceVolume = 10;

CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
Expand All @@ -429,12 +425,12 @@ public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
}
};
controllerCompat.registerCallback(callback, handler);

Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setDeviceInfo(deviceInfo)
.setDeviceVolume(deviceVolume)
.build();

session.setPlayer(playerConfig);

assertThat(playbackInfoNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
Expand All @@ -451,15 +447,13 @@ public void setPlayer_playbackTypeChangedToLocal() throws Exception {
Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfo).build();
session.setPlayer(playerConfig);

DeviceInfo deviceInfoToUpdate =
new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_LOCAL, /* minVolume= */ 0, /* maxVolume= */ 10);
int legacyPlaybackTypeToUpdate = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_LOCAL;
int legacyStream = AudioManager.STREAM_RING;
AudioAttributesCompat attrsCompat =
new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);

CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
Expand All @@ -472,12 +466,12 @@ public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
}
};
controllerCompat.registerCallback(callback, handler);

Bundle playerConfigToUpdate =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setDeviceInfo(deviceInfoToUpdate)
.setAudioAttributes(attrs)
.build();

session.setPlayer(playerConfigToUpdate);

// In API 21 and 22, onAudioInfoChanged is not called when playback is changed to local.
Expand Down Expand Up @@ -506,7 +500,6 @@ public void setPlayer_playbackTypeNotChanged_local() throws Exception {
AudioAttributesCompat attrsCompat =
new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);

CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
Expand All @@ -519,12 +512,12 @@ public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
}
};
controllerCompat.registerCallback(callback, handler);

Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setDeviceInfo(deviceInfo)
.setAudioAttributes(attrs)
.build();

session.setPlayer(playerConfig);

// In API 21+, onAudioInfoChanged() is not called when playbackType is not changed.
Expand Down Expand Up @@ -554,12 +547,10 @@ public void setPlayer_playbackTypeNotChanged_remote() throws Exception {
.setDeviceVolume(1)
.build();
session.setPlayer(playerConfig);

DeviceInfo deviceInfoToUpdate =
new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */ 0, /* maxVolume= */ 25);
int legacyPlaybackTypeToUpdate = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_REMOTE;
int deviceVolumeToUpdate = 10;

CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
Expand All @@ -573,12 +564,12 @@ public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
}
};
controllerCompat.registerCallback(callback, handler);

Bundle playerConfigToUpdate =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setDeviceInfo(deviceInfoToUpdate)
.setDeviceVolume(deviceVolumeToUpdate)
.build();

session.setPlayer(playerConfigToUpdate);

// In API 21+, onAudioInfoChanged() is not called when playbackType is not changed.
Expand Down Expand Up @@ -620,6 +611,7 @@ public void onPlaybackStateChanged(PlaybackStateCompat state) {
controllerCompat.registerCallback(callback, handler);

session.getMockPlayer().notifyPlaybackParametersChanged(playbackParameters);

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getPlaybackSpeed()).isEqualTo(playbackParameters.speed);
assertThat(
Expand All @@ -645,7 +637,6 @@ public void playbackStateChange_playWhenReadyBecomesFalseWhenReady_notifiesPause
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
session.getMockPlayer().notifyPlaybackStateChanged(STATE_READY);

AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -662,8 +653,8 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
.getMockPlayer()
.notifyPlayWhenReadyChanged(
/* playWhenReady= */ false, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateCompatRef.get().getState()).isEqualTo(PlaybackStateCompat.STATE_PAUSED);
assertThat(playbackStateCompatRef.get().getPlaybackSpeed()).isEqualTo(0f);
assertThat(
Expand Down Expand Up @@ -696,7 +687,6 @@ public void playbackStateChange_playWhenReadyBecomesTrueWhenBuffering_notifiesBu
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ false, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
session.getMockPlayer().notifyPlaybackStateChanged(Player.STATE_BUFFERING);

AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -708,12 +698,13 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
}
};
controllerCompat.registerCallback(callback, handler);

session
.getMockPlayer()
.notifyPlayWhenReadyChanged(
/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateCompatRef.get().getState())
.isEqualTo(PlaybackStateCompat.STATE_BUFFERING);
assertThat(playbackStateCompatRef.get().getPlaybackSpeed()).isEqualTo(0f);
Expand Down Expand Up @@ -746,7 +737,6 @@ public void playbackStateChange_playbackStateBecomesEnded_notifiesStopped() thro
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
session.getMockPlayer().notifyPlaybackStateChanged(STATE_READY);

AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -760,8 +750,8 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
controllerCompat.registerCallback(callback, handler);

session.getMockPlayer().notifyPlaybackStateChanged(STATE_ENDED);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateCompatRef.get().getState())
.isEqualTo(PlaybackStateCompat.STATE_STOPPED);
assertThat(playbackStateCompatRef.get().getPlaybackSpeed()).isEqualTo(0f);
Expand Down Expand Up @@ -795,7 +785,6 @@ public void playbackStateChange_withPlaybackSuppression_notifiesPlayingWithSpeed
session
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);

AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -813,8 +802,8 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
.notifyPlayWhenReadyChanged(
/* playWhenReady= */ true,
Player.PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateCompatRef.get().getState())
.isEqualTo(PlaybackStateCompat.STATE_PLAYING);
assertThat(playbackStateCompatRef.get().getPlaybackSpeed()).isEqualTo(0f);
Expand Down Expand Up @@ -848,7 +837,6 @@ public void playbackStateChange_playWhenReadyBecomesTrueWhenReady_notifiesPlayin
session
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ false, Player.PLAYBACK_SUPPRESSION_REASON_NONE);

AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -865,8 +853,8 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
.getMockPlayer()
.notifyPlayWhenReadyChanged(
/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateCompatRef.get().getState())
.isEqualTo(PlaybackStateCompat.STATE_PLAYING);
assertThat(playbackStateCompatRef.get().getPlaybackSpeed()).isEqualTo(1f);
Expand All @@ -890,7 +878,6 @@ public void onPlaybackStateChanged(PlaybackStateCompat playbackStateCompat) {
@Test
public void playbackStateChange_positionDiscontinuityNotifies_updatesPosition() throws Exception {
long testSeekPosition = 1300;

AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand All @@ -902,14 +889,15 @@ public void onPlaybackStateChanged(PlaybackStateCompat state) {
}
};
controllerCompat.registerCallback(callback, handler);

session.getMockPlayer().setCurrentPosition(testSeekPosition);

session
.getMockPlayer()
.notifyPositionDiscontinuity(
/* oldPosition= */ SessionPositionInfo.DEFAULT_POSITION_INFO,
/* newPosition= */ SessionPositionInfo.DEFAULT_POSITION_INFO,
Player.DISCONTINUITY_REASON_SEEK);

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getPosition()).isEqualTo(testSeekPosition);
assertThat(controllerCompat.getPlaybackState().getPosition()).isEqualTo(testSeekPosition);
Expand Down Expand Up @@ -1004,7 +992,6 @@ public void currentMediaItemChange() throws Exception {
.build());
Timeline timeline = new PlaylistTimeline(testMediaItems);
session.getMockPlayer().setTimeline(timeline);

AtomicReference<MediaMetadataCompat> metadataRef = new AtomicReference<>();
AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
CountDownLatch latchForMetadata = new CountDownLatch(1);
Expand Down Expand Up @@ -1061,8 +1048,8 @@ public void onQueueChanged(List<QueueItem> queue) {
}
};
controllerCompat.registerCallback(callback, handler);

Timeline timeline = MediaTestUtils.createTimeline(/* windowCount= */ 5);

session.getMockPlayer().setTimeline(timeline);
session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);

Expand Down Expand Up @@ -1093,8 +1080,8 @@ public void onQueueChanged(List<QueueItem> queue) {
}
};
controllerCompat.registerCallback(callback, handler);

int listSize = 5_000;

session.getMockPlayer().createAndSetFakeTimeline(listSize);
session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);

Expand Down Expand Up @@ -1131,13 +1118,13 @@ public void onQueueChanged(List<QueueItem> queue) {
}
};
controllerCompat.registerCallback(callback, handler);

MediaItem mediaItem =
new MediaItem.Builder()
.setMediaId("mediaItem_withSampleMediaMetadata")
.setMediaMetadata(MediaTestUtils.createMediaMetadata())
.build();
Timeline timeline = new PlaylistTimeline(ImmutableList.of(mediaItem));

session.getMockPlayer().setTimeline(timeline);
session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);

Expand Down Expand Up @@ -1169,9 +1156,9 @@ public void onQueueTitleChanged(CharSequence title) {
}
};
controllerCompat.registerCallback(callback, handler);

String playlistTitle = "playlistTitle";
MediaMetadata playlistMetadata = new MediaMetadata.Builder().setTitle(playlistTitle).build();

session.getMockPlayer().setPlaylistMetadata(playlistMetadata);
session.getMockPlayer().notifyPlaylistMetadataChanged();

Expand All @@ -1189,7 +1176,6 @@ public void onAudioInfoChanged_isCalledByVolumeChange() throws Exception {
.setDeviceVolume(1)
.build();
session.setPlayer(playerConfig);

int targetVolume = 3;
CountDownLatch targetVolumeNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
Expand Down

0 comments on commit a9eb121

Please sign in to comment.