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
This commit is contained in:
parent
63436390de
commit
34ca3bc3c3
@ -39,6 +39,7 @@ import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import androidx.media3.common.MediaItem;
|
||||
import androidx.media3.common.MediaMetadata;
|
||||
import androidx.media3.common.Player;
|
||||
import androidx.media3.common.Rating;
|
||||
@ -51,6 +52,7 @@ import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.ext.truth.os.BundleSubject;
|
||||
import androidx.test.filters.LargeTest;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import java.util.Collections;
|
||||
@ -151,10 +153,8 @@ public class MediaSessionPermissionTest {
|
||||
public void removeMediaItems() throws Exception {
|
||||
testOnCommandRequest(
|
||||
COMMAND_CHANGE_MEDIA_ITEMS,
|
||||
controller -> {
|
||||
controller.addMediaItems(MediaTestUtils.createMediaItems(/* size= */ 5));
|
||||
controller.removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ 1);
|
||||
});
|
||||
/* mediaItems= */ MediaTestUtils.createMediaItems(/* size= */ 5),
|
||||
controller -> controller.removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -180,7 +180,7 @@ public class MediaSessionPermissionTest {
|
||||
@Test
|
||||
public void setMediaUri() throws Exception {
|
||||
Uri uri = Uri.parse("media://uri");
|
||||
createSessionWithAvailableCommands(
|
||||
createSession(
|
||||
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)),
|
||||
Player.Commands.EMPTY);
|
||||
controllerTestRule
|
||||
@ -192,7 +192,7 @@ public class MediaSessionPermissionTest {
|
||||
assertThat(callback.uri).isEqualTo(uri);
|
||||
BundleSubject.assertThat(callback.extras).isEmpty();
|
||||
|
||||
createSessionWithAvailableCommands(
|
||||
createSession(
|
||||
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)),
|
||||
Player.Commands.EMPTY);
|
||||
controllerTestRule
|
||||
@ -206,7 +206,7 @@ public class MediaSessionPermissionTest {
|
||||
public void setRating() throws Exception {
|
||||
String mediaId = "testSetRating";
|
||||
Rating rating = new StarRating(5, 3.5f);
|
||||
createSessionWithAvailableCommands(
|
||||
createSession(
|
||||
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)),
|
||||
Player.Commands.EMPTY);
|
||||
controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating);
|
||||
@ -216,7 +216,7 @@ public class MediaSessionPermissionTest {
|
||||
assertThat(callback.mediaId).isEqualTo(mediaId);
|
||||
assertThat(callback.rating).isEqualTo(rating);
|
||||
|
||||
createSessionWithAvailableCommands(
|
||||
createSession(
|
||||
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)),
|
||||
Player.Commands.EMPTY);
|
||||
controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating);
|
||||
@ -228,7 +228,7 @@ public class MediaSessionPermissionTest {
|
||||
public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception {
|
||||
String mediaId = "testSetRating";
|
||||
Rating rating = new StarRating(5, 3.5f);
|
||||
createSessionWithAvailableCommands(
|
||||
createSession(
|
||||
createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)),
|
||||
Player.Commands.EMPTY);
|
||||
RemoteMediaController controller =
|
||||
@ -256,7 +256,7 @@ public class MediaSessionPermissionTest {
|
||||
Player.Commands commandsWithoutPlayPause = createPlayerCommandsWithout(playPauseCommand);
|
||||
|
||||
// Create session with play/pause command.
|
||||
createSessionWithAvailableCommands(SessionCommands.EMPTY, commandsWithPlayPause);
|
||||
createSession(SessionCommands.EMPTY, commandsWithPlayPause);
|
||||
// Create player with play/pause command.
|
||||
player.commands = commandsWithPlayPause;
|
||||
player.notifyAvailableCommandsChanged(commandsWithPlayPause);
|
||||
@ -363,12 +363,19 @@ public class MediaSessionPermissionTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void createSessionWithAvailableCommands(
|
||||
SessionCommands sessionCommands, Player.Commands playerCommands) {
|
||||
private void createSession(SessionCommands sessionCommands, Player.Commands playerCommands) {
|
||||
createSession(sessionCommands, playerCommands, /* mediaItems= */ ImmutableList.of());
|
||||
}
|
||||
|
||||
private void createSession(
|
||||
SessionCommands sessionCommands, Player.Commands playerCommands, List<MediaItem> mediaItems) {
|
||||
player =
|
||||
new MockPlayer.Builder()
|
||||
.setApplicationLooper(threadTestRule.getHandler().getLooper())
|
||||
.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 =
|
||||
new MySessionCallback() {
|
||||
@Override
|
||||
@ -392,16 +399,19 @@ public class MediaSessionPermissionTest {
|
||||
}
|
||||
|
||||
private void testOnCommandRequest(int commandCode, PermissionTestTask runnable) throws Exception {
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWith(commandCode));
|
||||
testOnCommandRequest(commandCode, /* mediaItems= */ ImmutableList.of(), runnable);
|
||||
}
|
||||
|
||||
private void testOnCommandRequest(
|
||||
int commandCode, List<MediaItem> mediaItems, PermissionTestTask runnable) throws Exception {
|
||||
createSession(SessionCommands.EMPTY, createPlayerCommandsWith(commandCode), mediaItems);
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
|
||||
assertThat(callback.onCommandRequestCalled).isTrue();
|
||||
assertThat(callback.command).isEqualTo(commandCode);
|
||||
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode));
|
||||
createSession(SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode), mediaItems);
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
|
||||
|
Loading…
x
Reference in New Issue
Block a user