Search in sources :

Example 1 with SessionCallback

use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method onDisconnected_afterTimeout_isCalled.

@Test
public void onDisconnected_afterTimeout_isCalled() throws Exception {
    CountDownLatch disconnectedLatch = new CountDownLatch(1);
    session = new MediaSession.Builder(context, player).setId("onDisconnected_afterTimeout_isCalled").setSessionCallback(new SessionCallback() {

        private ControllerInfo connectedController;

        @Override
        public MediaSession.ConnectionResult onConnect(MediaSession session, ControllerInfo controller) {
            if (EXPECTED_CONTROLLER_PACKAGE_NAME.equals(controller.getPackageName())) {
                connectedController = controller;
                return SessionCallback.super.onConnect(session, controller);
            }
            return MediaSession.ConnectionResult.reject();
        }

        @Override
        public void onDisconnected(MediaSession session, ControllerInfo controller) {
            if (Util.areEqual(connectedController, controller)) {
                disconnectedLatch.countDown();
            }
        }
    }).build();
    // Make onDisconnected() to be called immediately after the connection.
    session.setLegacyControllerConnectionTimeoutMs(0);
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    // Invoke any command for session to recognize the controller compat.
    controller.getTransportControls().seekTo(111);
    assertThat(disconnectedLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with SessionCallback

use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method prepareFromSearch.

@Test
public void prepareFromSearch() throws Exception {
    String query = "test_query";
    Bundle bundle = new Bundle();
    bundle.putString("key", "value");
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
            assertThat(uri.toString()).isEqualTo("androidx://media3-session/prepareFromSearch?query=" + query);
            assertThat(TestUtils.equals(bundle, extras)).isTrue();
            latch.countDown();
            return RESULT_SUCCESS;
        }
    };
    session = new MediaSession.Builder(context, player).setId("prepareFromSearch").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().prepareFromSearch(query, bundle);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
Also used : Bundle(android.os.Bundle) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 3 with SessionCallback

use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method prepareFromMediaUri.

@Test
public void prepareFromMediaUri() throws Exception {
    Uri mediaUri = Uri.parse("foo://bar");
    Bundle bundle = new Bundle();
    bundle.putString("key", "value");
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
            assertThat(uri).isEqualTo(mediaUri);
            assertThat(TestUtils.equals(bundle, extras)).isTrue();
            latch.countDown();
            return RESULT_SUCCESS;
        }
    };
    session = new MediaSession.Builder(context, player).setId("prepareFromMediaUri").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().prepareFromUri(mediaUri, bundle);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
Also used : Bundle(android.os.Bundle) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 4 with SessionCallback

use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method onCommandRequest.

@Test
public void onCommandRequest() throws Exception {
    ArrayList<Integer> commands = new ArrayList<>();
    CountDownLatch latchForPause = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public int onPlayerCommandRequest(MediaSession session, ControllerInfo controllerInfo, @Player.Command int command) {
            assertThat(controllerInfo.isTrusted()).isFalse();
            commands.add(command);
            if (command == COMMAND_PLAY_PAUSE) {
                latchForPause.countDown();
                return RESULT_ERROR_INVALID_STATE;
            }
            return RESULT_SUCCESS;
        }
    };
    session = new MediaSession.Builder(context, player).setId("onPlayerCommandRequest").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().pause();
    assertThat(latchForPause.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
    assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
    assertThat(commands).hasSize(1);
    assertThat(commands.get(0)).isEqualTo(COMMAND_PLAY_PAUSE);
    controller.getTransportControls().prepare();
    player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
    assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
    assertThat(commands).hasSize(2);
    assertThat(commands.get(1)).isEqualTo(COMMAND_PREPARE);
}
Also used : ArrayList(java.util.ArrayList) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with SessionCallback

use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method playFromMediaUri.

@Test
public void playFromMediaUri() throws Exception {
    Uri request = Uri.parse("foo://bar");
    Bundle bundle = new Bundle();
    bundle.putString("key", "value");
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
            assertThat(uri).isEqualTo(request);
            assertThat(TestUtils.equals(bundle, extras)).isTrue();
            latch.countDown();
            return RESULT_SUCCESS;
        }
    };
    session = new MediaSession.Builder(context, player).setId("playFromMediaUri").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().playFromUri(request, bundle);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
Also used : Bundle(android.os.Bundle) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)15 SessionCallback (androidx.media3.session.MediaSession.SessionCallback)15 LargeTest (androidx.test.filters.LargeTest)15 Bundle (android.os.Bundle)7 Uri (android.net.Uri)6 MediaLibrarySession (androidx.media3.session.MediaLibraryService.MediaLibrarySession)2 MediumTest (androidx.test.filters.MediumTest)2 ComponentName (android.content.ComponentName)1 RatingCompat (android.support.v4.media.RatingCompat)1 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1 Nullable (androidx.annotation.Nullable)1 MediaItem (androidx.media3.common.MediaItem)1 Rating (androidx.media3.common.Rating)1 LibraryParams (androidx.media3.session.MediaLibraryService.LibraryParams)1 MediaLibrarySessionCallback (androidx.media3.session.MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1