diff --git a/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionServiceTest.java b/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionServiceTest.java index fb7af3c86d..eab260a59b 100644 --- a/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionServiceTest.java +++ b/libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionServiceTest.java @@ -101,6 +101,7 @@ public class MediaSessionServiceTest { List playbackCommandControllerInfos = new ArrayList<>(); List onDisconnectedCommandControllerInfos = new ArrayList<>(); AtomicReference session = new AtomicReference<>(); + ConditionVariable disconnected = new ConditionVariable(); testServiceRegistry.setOnGetSessionHandler( controllerInfo -> { // The controllerInfo passed to the onGetSession of the service. @@ -136,8 +137,8 @@ public class MediaSessionServiceTest { if (!session.isMediaNotificationController(controller)) { // The controllerInfo when disconnecting. onDisconnectedCommandControllerInfos.add(controller); + disconnected.open(); } - MediaSession.Callback.super.onDisconnected(session, controller); } }) .build()); @@ -150,6 +151,8 @@ public class MediaSessionServiceTest { // Get the started service instance after creation. MockMediaSessionService service = (MockMediaSessionService) testServiceRegistry.getServiceInstance(); + // TestServiceRegistry is taken care of and cleaned up @After the test. + service.setCleanupServiceRegistryOnDestroy(false); controller.setRepeatMode(Player.REPEAT_MODE_ONE); List connectedControllerManagerControllerInfos = new ArrayList<>(); for (ControllerInfo controllerInfo : session.get().getConnectedControllers()) { @@ -159,9 +162,12 @@ public class MediaSessionServiceTest { } } + // The controller that was bound to the service unbinds when released. Because the service was + // never started (as in `onStartCommand()` was never called), the service is immediately + // terminated by the system when the last bound client unbinds. controller.release(); - service.blockUntilAllControllersUnbind(TIMEOUT_MS); + assertThat(disconnected.block(TIMEOUT_MS)).isTrue(); assertThat(onGetSessionControllerInfos).hasSize(1); assertThat(onGetSessionControllerInfos).isEqualTo(onConnectControllerInfos); assertThat(onGetSessionControllerInfos).isEqualTo(playbackCommandControllerInfos); diff --git a/libraries/test_session_current/src/main/java/androidx/media3/session/MockMediaSessionService.java b/libraries/test_session_current/src/main/java/androidx/media3/session/MockMediaSessionService.java index 22f4801549..7e0690b105 100644 --- a/libraries/test_session_current/src/main/java/androidx/media3/session/MockMediaSessionService.java +++ b/libraries/test_session_current/src/main/java/androidx/media3/session/MockMediaSessionService.java @@ -38,11 +38,24 @@ public class MockMediaSessionService extends MediaSessionService { @Nullable public MediaSession session; @Nullable private HandlerThread handlerThread; + private boolean cleanupServiceRegistryOnDestroy; public MockMediaSessionService() { boundControllerCount = new AtomicInteger(/* initialValue= */ 0); allControllersUnbound = new ConditionVariable(); allControllersUnbound.open(); + cleanupServiceRegistryOnDestroy = true; + } + + /** + * Whether the service should clean up the service registry {@link #onDestroy()} by calling {@link + * TestServiceRegistry#cleanUp()} on {@link TestServiceRegistry#getInstance()}. + * + *

The cleanup will release all sessions of the service. A test can clean up when tearing down + * the test, to prevent the sessions to be released by the service. + */ + public void setCleanupServiceRegistryOnDestroy(boolean cleanupServiceRegistryOnDestroy) { + this.cleanupServiceRegistryOnDestroy = cleanupServiceRegistryOnDestroy; } /** Returns whether at least one controller is bound to this service. */ @@ -90,7 +103,9 @@ public class MockMediaSessionService extends MediaSessionService { @Override public void onDestroy() { super.onDestroy(); - TestServiceRegistry.getInstance().cleanUp(); + if (cleanupServiceRegistryOnDestroy) { + TestServiceRegistry.getInstance().cleanUp(); + } handlerThread.quitSafely(); }