diff --git a/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java b/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java index 2475aaeac..2a7fd5b37 100644 --- a/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java +++ b/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java @@ -18,9 +18,14 @@ import android.os.Build; import android.os.PersistableBundle; import androidx.annotation.RequiresApi; +import com.google.common.primitives.Booleans; +import com.google.common.primitives.Doubles; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; import com.google.common.truth.FailureMetadata; import com.google.common.truth.Subject; import com.google.common.truth.Truth; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -67,6 +72,18 @@ private final Map toMap(PersistableBundle bundle) { Object value = bundle.get(key); if (value instanceof PersistableBundle) { value = toMap((PersistableBundle) value); + } else if (value instanceof boolean[]) { + // Array types require special handling since arrays use reference equality by default and + // MapSubject doesn't work around that. + value = Booleans.asList((boolean[]) value); + } else if (value instanceof double[]) { + value = Doubles.asList((double[]) value); + } else if (value instanceof int[]) { + value = Ints.asList((int[]) value); + } else if (value instanceof long[]) { + value = Longs.asList((long[]) value); + } else if (value instanceof String[]) { + value = Arrays.asList((String[]) value); } map.put(key, value); } diff --git a/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java b/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java index 23963667d..5d2fa75ac 100644 --- a/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java +++ b/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java @@ -47,6 +47,7 @@ public class PersistableBundleSubjectTest { private static final long LONG_VALUE = 1L; private static final String STRING_VALUE = "bar"; + // Be weary of reference equality here. getTestBundle() uses #clone to perform a shallow copy. private static final boolean[] booleanArrayValue = new boolean[] {true, false}; private static final double[] doubleArrayValue = new double[] {1.0, 2.0, 3.0}; private static final int[] intArrayValue = new int[] {1, 2, 3}; @@ -367,11 +368,13 @@ private PersistableBundle getTestBundle() { bundle.putInt(INT_KEY, INT_VALUE); bundle.putLong(LONG_KEY, LONG_VALUE); bundle.putString(STRING_KEY, STRING_VALUE); - bundle.putBooleanArray(BOOLEAN_ARRAY_KEY, booleanArrayValue); - bundle.putDoubleArray(DOUBLE_ARRAY_KEY, doubleArrayValue); - bundle.putIntArray(INT_ARRAY_KEY, intArrayValue); - bundle.putLongArray(LONG_ARRAY_KEY, longArrayValue); - bundle.putStringArray(STRING_ARRAY_KEY, stringArrayValue); + // Perform a shallow copy with clone to avoid reference equality. The underlying types support + // content equals just fine, so we don't have to worry about them being the same reference. + bundle.putBooleanArray(BOOLEAN_ARRAY_KEY, booleanArrayValue.clone()); + bundle.putDoubleArray(DOUBLE_ARRAY_KEY, doubleArrayValue.clone()); + bundle.putIntArray(INT_ARRAY_KEY, intArrayValue.clone()); + bundle.putLongArray(LONG_ARRAY_KEY, longArrayValue.clone()); + bundle.putStringArray(STRING_ARRAY_KEY, stringArrayValue.clone()); PersistableBundle innerBundle = new PersistableBundle(); innerBundle.putString(STRING_KEY, STRING_VALUE); bundle.putPersistableBundle(PERSISTABLE_BUNDLE_KEY, innerBundle);