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);
}
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();
}
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();
}
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;
}
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();
}
Aggregations