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 setSessionPositionUpdateDelayMs(String sessionId, long updateDelayMs);
void setPlayer(String sessionId, in Bundle playerBundle); void setPlayer(String sessionId, in Bundle playerBundle);
void broadcastCustomCommand(String sessionId, in Bundle command, in Bundle args); void broadcastCustomCommand(String sessionId, in Bundle command, in Bundle args);
void sendCustomCommand( void sendCustomCommand(String sessionId, in Bundle command, in Bundle args);
String sessionId, in Bundle controller, in Bundle command, in Bundle args);
void release(String sessionId); void release(String sessionId);
void setAvailableCommands(String sessionId, in Bundle controller, in Bundle sessionCommands, in Bundle playerCommands); void setAvailableCommands(String sessionId, in Bundle sessionCommands, in Bundle playerCommands);
void setCustomLayout(String sessionId, in Bundle controller, in List<Bundle> layout); void setCustomLayout(String sessionId, in List<Bundle> layout);
// Player Methods // Player Methods
void setPlayWhenReady(String sessionId, boolean playWhenReady, int reason); void setPlayWhenReady(String sessionId, boolean playWhenReady, int reason);

View File

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

View File

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

View File

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

View File

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