Fix value type when unbundling LibraryResult without expected type
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
This commit is contained in:
parent
f1f07dc82a
commit
f28a588809
@ -387,7 +387,7 @@ public final class LibraryResult<V> implements Bundleable {
|
|||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new LibraryResult<>(resultCode, completionTimeMs, params, value, VALUE_TYPE_ITEM_LIST);
|
return new LibraryResult<>(resultCode, completionTimeMs, params, value, valueType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Documented
|
@Documented
|
||||||
|
@ -15,11 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
package androidx.media3.session;
|
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 static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
import androidx.media3.common.MediaItem;
|
import androidx.media3.common.MediaItem;
|
||||||
import androidx.media3.common.MediaMetadata;
|
import androidx.media3.common.MediaMetadata;
|
||||||
|
import androidx.media3.session.MediaLibraryService.LibraryParams;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@ -51,4 +57,74 @@ public class LibraryResultTest {
|
|||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class, () -> LibraryResult.ofItem(item, /* params= */ null));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user