Skip to content

Commit

Permalink
Include nullness of MediaMetadata.extras in equals method
Browse files Browse the repository at this point in the history
This ensures MediaMetadata with just non-null extras is not
considered equal to MediaMetadata.EMPTY. This makes sure the
contents are bundled when a controller sets the extras in a
new MediaItem

Issue: androidx/media#1176

#minor-release

PiperOrigin-RevId: 618876642
  • Loading branch information
tonihei authored and Copybara-Service committed Mar 25, 2024
1 parent fad84e8 commit 8a4254d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Expand Up @@ -2218,8 +2218,9 @@ public RequestMetadata build() {
/**
* Optional extras {@link Bundle}.
*
* <p>Given the complexities of checking the equality of two {@link Bundle}s, the contents of
* these extras are not considered in the {@link #equals(Object)} or {@link #hashCode()}.
* <p>Given the complexities of checking the equality of two {@link Bundle} instances, the
* contents of these extras are not considered in the {@link #equals(Object)} or {@link
* #hashCode()} implementation.
*/
@Nullable public final Bundle extras;

Expand Down
Expand Up @@ -1086,8 +1086,9 @@ public MediaMetadata build() {
/**
* Optional extras {@link Bundle}.
*
* <p>Given the complexities of checking the equality of two {@link Bundle}s, this is not
* considered in the {@link #equals(Object)} or {@link #hashCode()}.
* <p>Given the complexities of checking the equality of two {@link Bundle} instances, the
* contents of these extras are not considered in the {@link #equals(Object)} and {@link
* #hashCode()} implementation.
*/
@Nullable public final Bundle extras;

Expand Down Expand Up @@ -1192,7 +1193,8 @@ public boolean equals(@Nullable Object obj) {
&& Util.areEqual(genre, that.genre)
&& Util.areEqual(compilation, that.compilation)
&& Util.areEqual(station, that.station)
&& Util.areEqual(mediaType, that.mediaType);
&& Util.areEqual(mediaType, that.mediaType)
&& ((extras == null) == (that.extras == null));
}

@SuppressWarnings("deprecation") // Hashing deprecated fields.
Expand Down Expand Up @@ -1230,7 +1232,8 @@ public int hashCode() {
genre,
compilation,
station,
mediaType);
mediaType,
extras == null);
}

// Bundleable implementation.
Expand Down
Expand Up @@ -163,6 +163,20 @@ public void createFullyPopulatedMediaMetadata_roundTripViaBundle_yieldsEqualInst
assertThat(mediaMetadataFromBundle.extras.getString(EXTRAS_KEY)).isEqualTo(EXTRAS_VALUE);
}

/** Regression test for https://github.com/androidx/media/issues/1176. */
@Test
public void roundTripViaBundle_withJustNonNullExtras_restoresAllData() {
Bundle extras = new Bundle();
extras.putString("key", "value");
MediaMetadata mediaMetadata = new MediaMetadata.Builder().setExtras(extras).build();

MediaMetadata restoredMetadata = MediaMetadata.fromBundle(mediaMetadata.toBundle());

assertThat(restoredMetadata).isEqualTo(mediaMetadata);
assertThat(restoredMetadata.extras).isNotNull();
assertThat(restoredMetadata.extras.get("key")).isEqualTo("value");
}

@SuppressWarnings("deprecation") // Testing deprecated setter.
@Test
public void builderSetFolderType_toNone_setsIsBrowsableToFalse() {
Expand Down

0 comments on commit 8a4254d

Please sign in to comment.