mirror of
https://github.com/androidx/media.git
synced 2025-05-06 23:20:42 +08:00
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:
parent
f4dfbdbc37
commit
94e97fe5db
@ -38,6 +38,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import android.content.Context;
|
||||
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;
|
||||
@ -49,6 +50,7 @@ import androidx.media3.test.session.common.MainLooperTestRule;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
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;
|
||||
@ -155,10 +157,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
|
||||
@ -185,7 +185,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);
|
||||
@ -205,7 +205,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 =
|
||||
@ -231,7 +231,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);
|
||||
@ -324,12 +324,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
|
||||
@ -353,16 +360,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