Fix flaky MediaBrowserListenerTest

MediaNotificationHandler tries to connect session in the same
process, so tests should be aware MediaControllers from the
MediaNotificationHandler.

PiperOrigin-RevId: 422330424
This commit is contained in:
jaewan 2022-01-17 12:03:06 +00:00 committed by Ian Baker
parent a18b64d20d
commit cd56084b3e
5 changed files with 38 additions and 34 deletions

View File

@ -28,11 +28,10 @@ interface IRemoteMediaSession {
void setSessionPositionUpdateDelayMs(String sessionId, long updateDelayMs);
void setPlayer(String sessionId, in Bundle playerBundle);
void broadcastCustomCommand(String sessionId, in Bundle command, in Bundle args);
void sendCustomCommand(
String sessionId, in Bundle controller, in Bundle command, in Bundle args);
void sendCustomCommand(String sessionId, in Bundle command, in Bundle args);
void release(String sessionId);
void setAvailableCommands(String sessionId, in Bundle controller, in Bundle sessionCommands, in Bundle playerCommands);
void setCustomLayout(String sessionId, in Bundle controller, in List<Bundle> layout);
void setAvailableCommands(String sessionId, in Bundle sessionCommands, in Bundle playerCommands);
void setCustomLayout(String sessionId, in List<Bundle> layout);
// Player Methods
void setPlayWhenReady(String sessionId, boolean playWhenReady, int reason);

View File

@ -352,13 +352,15 @@ public class MediaSessionProviderService extends Service {
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void sendCustomCommand(String sessionId, Bundle controller, Bundle command, Bundle args)
public void sendCustomCommand(String sessionId, Bundle command, Bundle args)
throws RemoteException {
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
ControllerInfo info = MediaTestUtils.getTestControllerInfo(session);
session.sendCustomCommand(info, SessionCommand.CREATOR.fromBundle(command), args);
List<ControllerInfo> controllerInfos = MediaTestUtils.getTestControllerInfos(session);
for (ControllerInfo info : controllerInfos) {
session.sendCustomCommand(info, SessionCommand.CREATOR.fromBundle(command), args);
}
});
}
@ -373,35 +375,37 @@ public class MediaSessionProviderService extends Service {
@Override
public void setAvailableCommands(
String sessionId, Bundle controller, Bundle sessionCommands, Bundle playerCommands)
throws RemoteException {
String sessionId, Bundle sessionCommands, Bundle playerCommands) throws RemoteException {
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
ControllerInfo info = MediaTestUtils.getTestControllerInfo(session);
session.setAvailableCommands(
info,
SessionCommands.CREATOR.fromBundle(sessionCommands),
Player.Commands.CREATOR.fromBundle(playerCommands));
List<ControllerInfo> controllerInfos = MediaTestUtils.getTestControllerInfos(session);
for (ControllerInfo info : controllerInfos) {
session.setAvailableCommands(
info,
SessionCommands.CREATOR.fromBundle(sessionCommands),
Player.Commands.CREATOR.fromBundle(playerCommands));
}
});
}
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void setCustomLayout(String sessionId, Bundle controller, List<Bundle> layout)
throws RemoteException {
public void setCustomLayout(String sessionId, List<Bundle> layout) throws RemoteException {
if (layout == null) {
return;
}
runOnHandler(
() -> {
MediaSession session = sessionMap.get(sessionId);
ControllerInfo info = MediaTestUtils.getTestControllerInfo(session);
List<CommandButton> buttons = new ArrayList<>();
for (Bundle bundle : layout) {
buttons.add(CommandButton.CREATOR.fromBundle(bundle));
}
session.setCustomLayout(info, buttons);
MediaSession session = sessionMap.get(sessionId);
List<ControllerInfo> controllerInfos = MediaTestUtils.getTestControllerInfos(session);
for (ControllerInfo info : controllerInfos) {
session.setCustomLayout(info, buttons);
}
});
}

View File

@ -87,17 +87,19 @@ public final class MediaTestUtils {
.build();
}
public static ControllerInfo getTestControllerInfo(MediaSession session) {
if (session == null) {
return null;
}
for (ControllerInfo info : session.getConnectedControllers()) {
if (SUPPORT_APP_PACKAGE_NAME.equals(info.getPackageName())) {
return info;
public static List<ControllerInfo> getTestControllerInfos(MediaSession session) {
List<ControllerInfo> infos = new ArrayList<>();
if (session != null) {
for (ControllerInfo info : session.getConnectedControllers()) {
if (SUPPORT_APP_PACKAGE_NAME.equals(info.getPackageName())) {
infos.add(info);
}
}
}
Log.e(TAG, "Test controller was not found in connected controllers. session=" + session);
return null;
if (infos.isEmpty()) {
Log.e(TAG, "Test controller was not found in connected controllers. session=" + session);
}
return infos;
}
/**

View File

@ -313,7 +313,7 @@ public class MockMediaLibraryService extends MediaLibraryService {
return Futures.immediateFuture(LibraryResult.ofVoid(params));
case SUBSCRIBE_ID_NOTIFY_CHILDREN_CHANGED_TO_ONE:
MockMediaLibraryService.this.session.notifyChildrenChanged(
MediaTestUtils.getTestControllerInfo(MockMediaLibraryService.this.session),
browser,
parentId,
NOTIFY_CHILDREN_CHANGED_ITEM_COUNT,
NOTIFY_CHILDREN_CHANGED_PARAMS);
@ -324,7 +324,7 @@ public class MockMediaLibraryService extends MediaLibraryService {
return Futures.immediateFuture(LibraryResult.ofVoid(params));
case SUBSCRIBE_ID_NOTIFY_CHILDREN_CHANGED_TO_ONE_WITH_NON_SUBSCRIBED_ID:
MockMediaLibraryService.this.session.notifyChildrenChanged(
MediaTestUtils.getTestControllerInfo(MockMediaLibraryService.this.session),
browser,
unsubscribedId,
NOTIFY_CHILDREN_CHANGED_ITEM_COUNT,
NOTIFY_CHILDREN_CHANGED_PARAMS);

View File

@ -178,7 +178,7 @@ public class RemoteMediaSession {
}
public void sendCustomCommand(SessionCommand command, Bundle args) throws RemoteException {
binder.sendCustomCommand(sessionId, null, command.toBundle(), args);
binder.sendCustomCommand(sessionId, command.toBundle(), args);
}
public void release() throws RemoteException {
@ -187,8 +187,7 @@ public class RemoteMediaSession {
public void setAvailableCommands(SessionCommands sessionCommands, Player.Commands playerCommands)
throws RemoteException {
binder.setAvailableCommands(
sessionId, null, sessionCommands.toBundle(), playerCommands.toBundle());
binder.setAvailableCommands(sessionId, sessionCommands.toBundle(), playerCommands.toBundle());
}
public void setCustomLayout(List<CommandButton> layout) throws RemoteException {
@ -196,7 +195,7 @@ public class RemoteMediaSession {
for (CommandButton button : layout) {
bundleList.add(button.toBundle());
}
binder.setCustomLayout(sessionId, null, bundleList);
binder.setCustomLayout(sessionId, bundleList);
}
////////////////////////////////////////////////////////////////////////////////