Skip to content

Commit 54e9296

Browse files
Update PersistableBundleSubject#isEqual to handle arrays
I originally thought MapSubject was handling `Array` types, but the PersistableBundles in the test cases were using the same array reference meaning the tests were incorrect. Updates to just convert the arrays to a list and updates the tests to perform a clone in `getTestBundle`. We could also convert them to a function instead but a shallow copy is sufficient here. PiperOrigin-RevId: 827559640
1 parent 7a50fec commit 54e9296

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
import android.os.Build;
1919
import android.os.PersistableBundle;
2020
import androidx.annotation.RequiresApi;
21+
import com.google.common.primitives.Booleans;
22+
import com.google.common.primitives.Doubles;
23+
import com.google.common.primitives.Ints;
24+
import com.google.common.primitives.Longs;
2125
import com.google.common.truth.FailureMetadata;
2226
import com.google.common.truth.Subject;
2327
import com.google.common.truth.Truth;
28+
import java.util.Arrays;
2429
import java.util.HashMap;
2530
import java.util.Map;
2631

@@ -67,6 +72,18 @@ private final Map<String, Object> toMap(PersistableBundle bundle) {
6772
Object value = bundle.get(key);
6873
if (value instanceof PersistableBundle) {
6974
value = toMap((PersistableBundle) value);
75+
} else if (value instanceof boolean[]) {
76+
// Array types require special handling since arrays use reference equality by default and
77+
// MapSubject doesn't work around that.
78+
value = Booleans.asList((boolean[]) value);
79+
} else if (value instanceof double[]) {
80+
value = Doubles.asList((double[]) value);
81+
} else if (value instanceof int[]) {
82+
value = Ints.asList((int[]) value);
83+
} else if (value instanceof long[]) {
84+
value = Longs.asList((long[]) value);
85+
} else if (value instanceof String[]) {
86+
value = Arrays.asList((String[]) value);
7087
}
7188
map.put(key, value);
7289
}

ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class PersistableBundleSubjectTest {
4747
private static final long LONG_VALUE = 1L;
4848
private static final String STRING_VALUE = "bar";
4949

50+
// Be weary of reference equality here. getTestBundle() uses #clone to perform a shallow copy.
5051
private static final boolean[] booleanArrayValue = new boolean[] {true, false};
5152
private static final double[] doubleArrayValue = new double[] {1.0, 2.0, 3.0};
5253
private static final int[] intArrayValue = new int[] {1, 2, 3};
@@ -367,11 +368,13 @@ private PersistableBundle getTestBundle() {
367368
bundle.putInt(INT_KEY, INT_VALUE);
368369
bundle.putLong(LONG_KEY, LONG_VALUE);
369370
bundle.putString(STRING_KEY, STRING_VALUE);
370-
bundle.putBooleanArray(BOOLEAN_ARRAY_KEY, booleanArrayValue);
371-
bundle.putDoubleArray(DOUBLE_ARRAY_KEY, doubleArrayValue);
372-
bundle.putIntArray(INT_ARRAY_KEY, intArrayValue);
373-
bundle.putLongArray(LONG_ARRAY_KEY, longArrayValue);
374-
bundle.putStringArray(STRING_ARRAY_KEY, stringArrayValue);
371+
// Perform a shallow copy with clone to avoid reference equality. The underlying types support
372+
// content equals just fine, so we don't have to worry about them being the same reference.
373+
bundle.putBooleanArray(BOOLEAN_ARRAY_KEY, booleanArrayValue.clone());
374+
bundle.putDoubleArray(DOUBLE_ARRAY_KEY, doubleArrayValue.clone());
375+
bundle.putIntArray(INT_ARRAY_KEY, intArrayValue.clone());
376+
bundle.putLongArray(LONG_ARRAY_KEY, longArrayValue.clone());
377+
bundle.putStringArray(STRING_ARRAY_KEY, stringArrayValue.clone());
375378
PersistableBundle innerBundle = new PersistableBundle();
376379
innerBundle.putString(STRING_KEY, STRING_VALUE);
377380
bundle.putPersistableBundle(PERSISTABLE_BUNDLE_KEY, innerBundle);

0 commit comments

Comments
 (0)