De-flake test in MediaSessionPermissionTest

This change makes MediaSessionPermissionTest.removeMediaItems()
non-flaky. This is a quick fix though it'd be good to refactor this
test file.

PiperOrigin-RevId: 453162764
(cherry picked from commit 34ca3bc3c3b52b8dcfc531de76bc0b34f7a89c8c)
This commit is contained in:
christosts 2022-06-06 10:42:08 +00:00 committed by microkatz
parent f4dfbdbc37
commit 94e97fe5db

View File

@ -38,6 +38,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata; import androidx.media3.common.MediaMetadata;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.media3.common.Rating; import androidx.media3.common.Rating;
@ -49,6 +50,7 @@ import androidx.media3.test.session.common.MainLooperTestRule;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import java.util.Collections; import java.util.Collections;
@ -155,10 +157,8 @@ public class MediaSessionPermissionTest {
public void removeMediaItems() throws Exception { public void removeMediaItems() throws Exception {
testOnCommandRequest( testOnCommandRequest(
COMMAND_CHANGE_MEDIA_ITEMS, COMMAND_CHANGE_MEDIA_ITEMS,
controller -> { /* mediaItems= */ MediaTestUtils.createMediaItems(/* size= */ 5),
controller.addMediaItems(MediaTestUtils.createMediaItems(/* size= */ 5)); controller -> controller.removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ 1));
controller.removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ 1);
});
} }
@Test @Test
@ -185,7 +185,7 @@ public class MediaSessionPermissionTest {
public void setRating() throws Exception { public void setRating() throws Exception {
String mediaId = "testSetRating"; String mediaId = "testSetRating";
Rating rating = new StarRating(5, 3.5f); Rating rating = new StarRating(5, 3.5f);
createSessionWithAvailableCommands( createSession(
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)),
Player.Commands.EMPTY); Player.Commands.EMPTY);
controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating); controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating);
@ -205,7 +205,7 @@ public class MediaSessionPermissionTest {
public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception { public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception {
String mediaId = "testSetRating"; String mediaId = "testSetRating";
Rating rating = new StarRating(5, 3.5f); Rating rating = new StarRating(5, 3.5f);
createSessionWithAvailableCommands( createSession(
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)),
Player.Commands.EMPTY); Player.Commands.EMPTY);
RemoteMediaController controller = RemoteMediaController controller =
@ -231,7 +231,7 @@ public class MediaSessionPermissionTest {
Player.Commands commandsWithoutPlayPause = createPlayerCommandsWithout(playPauseCommand); Player.Commands commandsWithoutPlayPause = createPlayerCommandsWithout(playPauseCommand);
// Create session with play/pause command. // Create session with play/pause command.
createSessionWithAvailableCommands(SessionCommands.EMPTY, commandsWithPlayPause); createSession(SessionCommands.EMPTY, commandsWithPlayPause);
// Create player with play/pause command. // Create player with play/pause command.
player.commands = commandsWithPlayPause; player.commands = commandsWithPlayPause;
player.notifyAvailableCommandsChanged(commandsWithPlayPause); player.notifyAvailableCommandsChanged(commandsWithPlayPause);
@ -324,12 +324,19 @@ public class MediaSessionPermissionTest {
} }
} }
private void createSessionWithAvailableCommands( private void createSession(SessionCommands sessionCommands, Player.Commands playerCommands) {
SessionCommands sessionCommands, Player.Commands playerCommands) { createSession(sessionCommands, playerCommands, /* mediaItems= */ ImmutableList.of());
}
private void createSession(
SessionCommands sessionCommands, Player.Commands playerCommands, List<MediaItem> mediaItems) {
player = player =
new MockPlayer.Builder() new MockPlayer.Builder()
.setApplicationLooper(threadTestRule.getHandler().getLooper()) .setApplicationLooper(threadTestRule.getHandler().getLooper())
.build(); .build();
// Add media items directly on the mock player's list so that the player's interaction state
// does not change.
player.mediaItems.addAll(mediaItems);
callback = callback =
new MySessionCallback() { new MySessionCallback() {
@Override @Override
@ -353,16 +360,19 @@ public class MediaSessionPermissionTest {
} }
private void testOnCommandRequest(int commandCode, PermissionTestTask runnable) throws Exception { private void testOnCommandRequest(int commandCode, PermissionTestTask runnable) throws Exception {
createSessionWithAvailableCommands( testOnCommandRequest(commandCode, /* mediaItems= */ ImmutableList.of(), runnable);
SessionCommands.EMPTY, createPlayerCommandsWith(commandCode)); }
private void testOnCommandRequest(
int commandCode, List<MediaItem> mediaItems, PermissionTestTask runnable) throws Exception {
createSession(SessionCommands.EMPTY, createPlayerCommandsWith(commandCode), mediaItems);
runnable.run(controllerTestRule.createRemoteController(session.getToken())); runnable.run(controllerTestRule.createRemoteController(session.getToken()));
assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(callback.onCommandRequestCalled).isTrue(); assertThat(callback.onCommandRequestCalled).isTrue();
assertThat(callback.command).isEqualTo(commandCode); assertThat(callback.command).isEqualTo(commandCode);
createSessionWithAvailableCommands( createSession(SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode), mediaItems);
SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode));
runnable.run(controllerTestRule.createRemoteController(session.getToken())); runnable.run(controllerTestRule.createRemoteController(session.getToken()));
assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse(); assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();