Skip to content

Commit

Permalink
Fix value type when unbundling LibraryResult without expected type
Browse files Browse the repository at this point in the history
Calling LibraryResult.toBundle() could have caused a CastClassException.
This was because when unbundled with UNKNOWN_TYPE_CREATOR.fromBundle(Bundle),
the valueType was set to VALUE_TYPE_ITEM_LIST for all types and the MediaItem
was attempted to be casted to a list.

PiperOrigin-RevId: 529717688
  • Loading branch information
marcbaechinger authored and tof-tof committed May 5, 2023
1 parent f1f07dc commit f28a588
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private static LibraryResult<?> fromBundle(
throw new IllegalStateException();
}

return new LibraryResult<>(resultCode, completionTimeMs, params, value, VALUE_TYPE_ITEM_LIST);
return new LibraryResult<>(resultCode, completionTimeMs, params, value, valueType);
}

@Documented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
*/
package androidx.media3.session;

import static androidx.media3.session.LibraryResult.RESULT_ERROR_NOT_SUPPORTED;
import static androidx.media3.session.LibraryResult.UNKNOWN_TYPE_CREATOR;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import android.os.Bundle;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.session.MediaLibraryService.LibraryParams;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -51,4 +57,74 @@ public void constructor_mediaItemWithoutIsPlayable_throwsIAE() {
assertThrows(
IllegalArgumentException.class, () -> LibraryResult.ofItem(item, /* params= */ null));
}

@Test
public void toBundle_mediaItemLibraryResultThatWasUnbundledAsAnUnknownType_noException() {
MediaItem mediaItem =
new MediaItem.Builder()
.setMediaId("rootMediaId")
.setMediaMetadata(
new MediaMetadata.Builder().setIsPlayable(false).setIsBrowsable(true).build())
.build();
LibraryParams params = new LibraryParams.Builder().build();
LibraryResult<MediaItem> libraryResult = LibraryResult.ofItem(mediaItem, params);
Bundle libraryResultBundle = libraryResult.toBundle();
LibraryResult<?> libraryResultFromUntyped =
UNKNOWN_TYPE_CREATOR.fromBundle(libraryResultBundle);

Bundle bundleOfUntyped = libraryResultFromUntyped.toBundle();

assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).value).isEqualTo(mediaItem);
}

@Test
public void toBundle_mediaItemListLibraryResultThatWasUnbundledAsAnUnknownType_noException() {
MediaItem mediaItem =
new MediaItem.Builder()
.setMediaId("rootMediaId")
.setMediaMetadata(
new MediaMetadata.Builder().setIsPlayable(false).setIsBrowsable(true).build())
.build();
LibraryParams params = new LibraryParams.Builder().build();
LibraryResult<ImmutableList<MediaItem>> libraryResult =
LibraryResult.ofItemList(ImmutableList.of(mediaItem), params);
Bundle libraryResultBundle = libraryResult.toBundle();
LibraryResult<?> mediaItemLibraryResultFromUntyped =
UNKNOWN_TYPE_CREATOR.fromBundle(libraryResultBundle);

Bundle bundleOfUntyped = mediaItemLibraryResultFromUntyped.toBundle();

assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).value)
.isEqualTo(ImmutableList.of(mediaItem));
}

@Test
public void toBundle_errorResultThatWasUnbundledAsAnUnknownType_noException() {
LibraryResult<ImmutableList<Error>> libraryResult =
LibraryResult.ofError(LibraryResult.RESULT_ERROR_NOT_SUPPORTED);
Bundle errorLibraryResultBundle = libraryResult.toBundle();
LibraryResult<?> libraryResultFromUntyped =
UNKNOWN_TYPE_CREATOR.fromBundle(errorLibraryResultBundle);

Bundle bundleOfUntyped = libraryResultFromUntyped.toBundle();

assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).value).isNull();
assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).resultCode)
.isEqualTo(RESULT_ERROR_NOT_SUPPORTED);
}

@Test
public void toBundle_voidResultThatWasUnbundledAsAnUnknownType_noException() {
LibraryResult<ImmutableList<Error>> libraryResult =
LibraryResult.ofError(LibraryResult.RESULT_ERROR_NOT_SUPPORTED);
Bundle errorLibraryResultBundle = libraryResult.toBundle();
LibraryResult<?> libraryResultFromUntyped =
UNKNOWN_TYPE_CREATOR.fromBundle(errorLibraryResultBundle);

Bundle bundleOfUntyped = libraryResultFromUntyped.toBundle();

assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).value).isNull();
assertThat(UNKNOWN_TYPE_CREATOR.fromBundle(bundleOfUntyped).resultCode)
.isEqualTo(RESULT_ERROR_NOT_SUPPORTED);
}
}

0 comments on commit f28a588

Please sign in to comment.