Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,6 +72,18 @@ private final Map<String, Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down
Loading