Search in sources :

Example 1 with MediaLibrarySession

use of androidx.media3.session.MediaLibraryService.MediaLibrarySession in project media by androidx.

the class MediaBrowserListenerTest method onChildrenChanged_calledWhenSubscribed.

@Test
public void onChildrenChanged_calledWhenSubscribed() throws Exception {
    // This test uses MediaLibrarySession.notifyChildrenChanged().
    String expectedParentId = SUBSCRIBE_ID_NOTIFY_CHILDREN_CHANGED_TO_ALL;
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<String> parentIdRef = new AtomicReference<>();
    AtomicInteger itemCountRef = new AtomicInteger();
    AtomicReference<LibraryParams> paramsRef = new AtomicReference<>();
    MediaBrowser.Listener browserListenerProxy = new MediaBrowser.Listener() {

        @Override
        public void onChildrenChanged(MediaBrowser browser, String parentId, int itemCount, LibraryParams params) {
            parentIdRef.set(parentId);
            itemCountRef.set(itemCount);
            paramsRef.set(params);
            latch.countDown();
        }
    };
    MediaBrowser browser = createBrowser(null, browserListenerProxy);
    LibraryResult<Void> result = threadTestRule.getHandler().postAndSync(() -> browser.subscribe(expectedParentId, null)).get(TIMEOUT_MS, MILLISECONDS);
    assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
    // The MediaLibrarySession in MockMediaLibraryService is supposed to call
    // notifyChildrenChanged() in its onSubscribe() method.
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(parentIdRef.get()).isEqualTo(expectedParentId);
    assertThat(itemCountRef.get()).isEqualTo(NOTIFY_CHILDREN_CHANGED_ITEM_COUNT);
    MediaTestUtils.assertLibraryParamsEquals(paramsRef.get(), NOTIFY_CHILDREN_CHANGED_EXTRAS);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LibraryParams(androidx.media3.session.MediaLibraryService.LibraryParams) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with MediaLibrarySession

use of androidx.media3.session.MediaLibraryService.MediaLibrarySession in project media by androidx.

the class MediaLibrarySessionCallbackTest method onUnsubscribe.

@Test
public void onUnsubscribe() throws Exception {
    String testParentId = "testUnsubscribeId";
    CountDownLatch latch = new CountDownLatch(1);
    MediaLibrarySession.MediaLibrarySessionCallback sessionCallback = new MediaLibrarySession.MediaLibrarySessionCallback() {

        @Override
        public ListenableFuture<LibraryResult<Void>> onUnsubscribe(MediaLibrarySession session, ControllerInfo browser, String parentId) {
            assertThat(parentId).isEqualTo(testParentId);
            latch.countDown();
            return Futures.immediateFuture(LibraryResult.ofVoid());
        }
    };
    MockMediaLibraryService service = new MockMediaLibraryService();
    service.attachBaseContext(context);
    MediaLibrarySession session = sessionTestRule.ensureReleaseAfterTest(new MediaLibrarySession.Builder(service, player, sessionCallback).setId("testOnUnsubscribe").build());
    RemoteMediaBrowser browser = controllerTestRule.createRemoteBrowser(session.getToken());
    browser.unsubscribe(testParentId);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MediaLibrarySession(androidx.media3.session.MediaLibraryService.MediaLibrarySession) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with MediaLibrarySession

use of androidx.media3.session.MediaLibraryService.MediaLibrarySession in project media by androidx.

the class MediaLibrarySessionCallbackTest method onSubscribe.

@Test
public void onSubscribe() throws Exception {
    String testParentId = "testSubscribeId";
    LibraryParams testParams = MediaTestUtils.createLibraryParams();
    CountDownLatch latch = new CountDownLatch(1);
    MediaLibrarySession.MediaLibrarySessionCallback sessionCallback = new MediaLibrarySession.MediaLibrarySessionCallback() {

        @Override
        public ListenableFuture<LibraryResult<Void>> onSubscribe(MediaLibrarySession session, ControllerInfo browser, String parentId, @Nullable LibraryParams params) {
            assertThat(parentId).isEqualTo(testParentId);
            MediaTestUtils.assertLibraryParamsEquals(testParams, params);
            latch.countDown();
            return Futures.immediateFuture(LibraryResult.ofVoid(params));
        }
    };
    MockMediaLibraryService service = new MockMediaLibraryService();
    service.attachBaseContext(context);
    MediaLibrarySession session = sessionTestRule.ensureReleaseAfterTest(new MediaLibrarySession.Builder(service, player, sessionCallback).setId("testOnSubscribe").build());
    RemoteMediaBrowser browser = controllerTestRule.createRemoteBrowser(session.getToken());
    browser.subscribe(testParentId, testParams);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : LibraryParams(androidx.media3.session.MediaLibraryService.LibraryParams) CountDownLatch(java.util.concurrent.CountDownLatch) MediaLibrarySession(androidx.media3.session.MediaLibraryService.MediaLibrarySession) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) Nullable(androidx.annotation.Nullable) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with MediaLibrarySession

use of androidx.media3.session.MediaLibraryService.MediaLibrarySession in project media by androidx.

the class MockMediaLibraryService method onGetSession.

@Override
public MediaLibrarySession onGetSession(ControllerInfo controllerInfo) {
    TestServiceRegistry registry = TestServiceRegistry.getInstance();
    TestServiceRegistry.OnGetSessionHandler onGetSessionHandler = registry.getOnGetSessionHandler();
    if (onGetSessionHandler != null) {
        return (MediaLibrarySession) onGetSessionHandler.onGetSession(controllerInfo);
    }
    MockPlayer player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
    MediaLibrarySessionCallback callback = registry.getSessionCallback();
    session = new MediaLibrarySession.Builder(MockMediaLibraryService.this, player, callback != null ? callback : new TestLibrarySessionCallback()).setId(ID).build();
    return session;
}
Also used : MediaLibrarySessionCallback(androidx.media3.session.MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback)

Example 5 with MediaLibrarySession

use of androidx.media3.session.MediaLibraryService.MediaLibrarySession in project media by androidx.

the class MediaBrowserListenerTest method onChildrenChanged_notCalledWhenNotSubscribed2.

@Test
public void onChildrenChanged_notCalledWhenNotSubscribed2() throws Exception {
    // This test uses MediaLibrarySession.notifyChildrenChanged(ControllerInfo).
    String subscribedMediaId = SUBSCRIBE_ID_NOTIFY_CHILDREN_CHANGED_TO_ONE_WITH_NON_SUBSCRIBED_ID;
    CountDownLatch latch = new CountDownLatch(1);
    MediaBrowser.Listener browserListenerProxy = new MediaBrowser.Listener() {

        @Override
        public void onChildrenChanged(MediaBrowser browser, String parentId, int itemCount, LibraryParams params) {
            latch.countDown();
        }
    };
    MediaBrowser browser = createBrowser(null, browserListenerProxy);
    LibraryResult<Void> result = threadTestRule.getHandler().postAndSync(() -> browser.subscribe(subscribedMediaId, null)).get(TIMEOUT_MS, MILLISECONDS);
    assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
    // The MediaLibrarySession in MockMediaLibraryService is supposed to call
    // notifyChildrenChanged(ControllerInfo) in its onSubscribe() method, but
    // with a different media ID.
    // Therefore, onChildrenChanged() should not be called.
    assertThat(latch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
}
Also used : LibraryParams(androidx.media3.session.MediaLibraryService.LibraryParams) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)6 Test (org.junit.Test)6 LibraryParams (androidx.media3.session.MediaLibraryService.LibraryParams)5 LargeTest (androidx.test.filters.LargeTest)4 MediaLibrarySession (androidx.media3.session.MediaLibraryService.MediaLibrarySession)2 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)2 MediumTest (androidx.test.filters.MediumTest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nullable (androidx.annotation.Nullable)1 MediaLibrarySessionCallback (androidx.media3.session.MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback)1