Search in sources :

Example 1 with StarRating

use of androidx.media3.common.StarRating in project media by androidx.

the class MediaSessionCallbackTest method onSetRatingWithoutMediaId.

@Test
public void onSetRatingWithoutMediaId() throws Exception {
    float ratingValue = 3.5f;
    Rating testRating = new StarRating(5, ratingValue);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Rating> ratingRef = new AtomicReference<>();
    MediaSession.SessionCallback callback = new MediaSession.SessionCallback() {

        @Override
        public ListenableFuture<SessionResult> onSetRating(MediaSession session, ControllerInfo controller, Rating rating) {
            if (!TextUtils.equals(controller.getPackageName(), SUPPORT_APP_PACKAGE_NAME)) {
                return Futures.immediateFuture(new SessionResult(RESULT_INFO_SKIPPED));
            }
            ratingRef.set(rating);
            latch.countDown();
            return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
        }
    };
    MediaSession session = sessionTestRule.ensureReleaseAfterTest(new MediaSession.Builder(context, player).setSessionCallback(callback).setId("testOnSetRating").build());
    RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
    SessionResult result = controller.setRating(testRating);
    assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(ratingRef.get()).isEqualTo(testRating);
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) AtomicReference(java.util.concurrent.atomic.AtomicReference) StarRating(androidx.media3.common.StarRating) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with StarRating

use of androidx.media3.common.StarRating in project media by androidx.

the class MediaSessionCompatCallbackWithMediaControllerTest method setRatingWithMediaId.

@Test
public void setRatingWithMediaId() throws Exception {
    float ratingValue = 3.5f;
    Rating rating = new StarRating(5, ratingValue);
    String mediaId = "media_id";
    MediaMetadataCompat metadata = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, mediaId).build();
    session.setMetadata(metadata);
    RemoteMediaController controller = createControllerAndWaitConnection();
    sessionCallback.reset(1);
    controller.setRating(mediaId, rating);
    assertThat(sessionCallback.await(TIMEOUT_MS)).isTrue();
    assertThat(sessionCallback.onSetRatingCalled).isTrue();
    assertThat(MediaUtils.convertToRating(sessionCallback.rating)).isEqualTo(rating);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) StarRating(androidx.media3.common.StarRating) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 3 with StarRating

use of androidx.media3.common.StarRating in project media by androidx.

the class MediaSessionCallbackTest method onSetRatingWithMediaId.

@Test
public void onSetRatingWithMediaId() throws Exception {
    float ratingValue = 3.5f;
    Rating testRating = new StarRating(5, ratingValue);
    String testMediaId = "media_id";
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<String> mediaIdRef = new AtomicReference<>();
    AtomicReference<Rating> ratingRef = new AtomicReference<>();
    MediaSession.SessionCallback callback = new MediaSession.SessionCallback() {

        @Override
        public ListenableFuture<SessionResult> onSetRating(MediaSession session, ControllerInfo controller, String mediaId, Rating rating) {
            if (!TextUtils.equals(controller.getPackageName(), SUPPORT_APP_PACKAGE_NAME)) {
                return Futures.immediateFuture(new SessionResult(RESULT_INFO_SKIPPED));
            }
            mediaIdRef.set(mediaId);
            ratingRef.set(rating);
            latch.countDown();
            return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
        }
    };
    MediaSession session = sessionTestRule.ensureReleaseAfterTest(new MediaSession.Builder(context, player).setSessionCallback(callback).setId("testOnSetRating").build());
    RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
    SessionResult result = controller.setRating(testMediaId, testRating);
    assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(mediaIdRef.get()).isEqualTo(testMediaId);
    assertThat(ratingRef.get()).isEqualTo(testRating);
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) AtomicReference(java.util.concurrent.atomic.AtomicReference) StarRating(androidx.media3.common.StarRating) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 4 with StarRating

use of androidx.media3.common.StarRating in project media by androidx.

the class MediaSessionPermissionTest method changingPermissionForSessionCommandWithSetAvailableCommands.

@Test
public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception {
    String mediaId = "testSetRating";
    Rating rating = new StarRating(5, 3.5f);
    createSessionWithAvailableCommands(createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), Player.Commands.EMPTY);
    RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
    controller.setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(callback.onSetRatingCalled).isTrue();
    callback.reset();
    // Change allowed commands.
    session.setAvailableCommands(getTestControllerInfo(), createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)), Player.Commands.EMPTY);
    controller.setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) StarRating(androidx.media3.common.StarRating) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with StarRating

use of androidx.media3.common.StarRating in project media by androidx.

the class StarRating method toBundle.

@UnstableApi
@Override
public Bundle toBundle() {
    Bundle bundle = new Bundle();
    bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE);
    bundle.putInt(keyForField(FIELD_MAX_STARS), maxStars);
    bundle.putFloat(keyForField(FIELD_STAR_RATING), starRating);
    return bundle;
}
Also used : Bundle(android.os.Bundle) UnstableApi(androidx.media3.common.util.UnstableApi)

Aggregations

StarRating (androidx.media3.common.StarRating)8 Test (org.junit.Test)7 Rating (androidx.media3.common.Rating)6 LargeTest (androidx.test.filters.LargeTest)5 Bundle (android.os.Bundle)2 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)2 SmallTest (androidx.test.filters.SmallTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 HeartRating (androidx.media3.common.HeartRating)1 MediaMetadata (androidx.media3.common.MediaMetadata)1 PercentageRating (androidx.media3.common.PercentageRating)1 ThumbRating (androidx.media3.common.ThumbRating)1 UnstableApi (androidx.media3.common.util.UnstableApi)1