Split test methods more clearly in arrange, act, assert.

The tests in MediaControllerCompatCallbackWithMediaSessionTest
don't follow this pattern very consistently at the moment.

PiperOrigin-RevId: 487501913
(cherry picked from commit c6a0ba25f996b77adf6bf0dd9921f429d5950437)
This commit is contained in:
tonihei 2022-11-10 12:47:12 +00:00 committed by microkatz
parent 58d670d941
commit a9eb121169

View File

@ -227,7 +227,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
@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 =
@ -242,6 +241,7 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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);
@ -251,7 +251,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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 =
@ -266,6 +265,7 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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);
@ -284,6 +284,7 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
controllerCompat.registerCallback(callback, handler);
session.release();
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
@ -312,7 +313,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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<>();
@ -361,7 +361,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
controllerCompat.registerCallback(callback, handler);
Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setPlaybackState(testState)
@ -376,13 +375,13 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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);
@ -393,7 +392,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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());
@ -402,7 +400,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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();
@ -415,7 +412,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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() {
@ -429,12 +425,12 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
controllerCompat.registerCallback(callback, handler);
Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setDeviceInfo(deviceInfo)
.setDeviceVolume(deviceVolume)
.build();
session.setPlayer(playerConfig);
assertThat(playbackInfoNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
@ -451,7 +447,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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;
@ -459,7 +454,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
AudioAttributesCompat attrsCompat =
new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);
CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
@ -472,12 +466,12 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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.
@ -506,7 +500,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
AudioAttributesCompat attrsCompat =
new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);
CountDownLatch playbackInfoNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
new MediaControllerCompat.Callback() {
@ -519,12 +512,12 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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.
@ -554,12 +547,10 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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() {
@ -573,12 +564,12 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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.
@ -620,6 +611,7 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
controllerCompat.registerCallback(callback, handler);
session.getMockPlayer().notifyPlaybackParametersChanged(playbackParameters);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getPlaybackSpeed()).isEqualTo(playbackParameters.speed);
assertThat(
@ -645,7 +637,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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 =
@ -662,8 +653,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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(
@ -696,7 +687,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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 =
@ -708,12 +698,13 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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);
@ -746,7 +737,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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 =
@ -760,8 +750,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
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);
@ -795,7 +785,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
session
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ true, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
@ -813,8 +802,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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);
@ -848,7 +837,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
session
.getMockPlayer()
.setPlayWhenReady(/* playWhenReady= */ false, Player.PLAYBACK_SUPPRESSION_REASON_NONE);
AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
@ -865,8 +853,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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);
@ -890,7 +878,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
@Test
public void playbackStateChange_positionDiscontinuityNotifies_updatesPosition() throws Exception {
long testSeekPosition = 1300;
AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback =
@ -902,14 +889,15 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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);
@ -1004,7 +992,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.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);
@ -1061,8 +1048,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
controllerCompat.registerCallback(callback, handler);
Timeline timeline = MediaTestUtils.createTimeline(/* windowCount= */ 5);
session.getMockPlayer().setTimeline(timeline);
session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
@ -1093,8 +1080,8 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
controllerCompat.registerCallback(callback, handler);
int listSize = 5_000;
session.getMockPlayer().createAndSetFakeTimeline(listSize);
session.getMockPlayer().notifyTimelineChanged(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
@ -1131,13 +1118,13 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
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);
@ -1169,9 +1156,9 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
}
};
controllerCompat.registerCallback(callback, handler);
String playlistTitle = "playlistTitle";
MediaMetadata playlistMetadata = new MediaMetadata.Builder().setTitle(playlistTitle).build();
session.getMockPlayer().setPlaylistMetadata(playlistMetadata);
session.getMockPlayer().notifyPlaylistMetadataChanged();
@ -1189,7 +1176,6 @@ public class MediaControllerCompatCallbackWithMediaSessionTest {
.setDeviceVolume(1)
.build();
session.setPlayer(playerConfig);
int targetVolume = 3;
CountDownLatch targetVolumeNotified = new CountDownLatch(1);
MediaControllerCompat.Callback callback =