Skip to content

Commit 7d04225

Browse files
BEGIN_PUBLIC
Define isEqualTo for `PersistableBundleSubject` Defines a brute force `isEqualTo` API for `PersistableBundle` by converting it to a map and taking advantage of the existing `MapSubject`. `PersistableBundle` has historically been very painful to test since they are lazy containers and do not implement equals. While the existing per value subjects are useful, they do not allow for generic comparison in parametrized tests which limits flexibility and results in more boilerplate in unit tests. END_PUBLIC PiperOrigin-RevId: 816295120
1 parent 43475af commit 7d04225

File tree

4 files changed

+303
-31
lines changed

4 files changed

+303
-31
lines changed

ext/truth/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
* `BundleSubject` and `PersistableBundleSubject` now share `BaseBundle` support,
1212
including additional array type assertions
13+
* `PersistableBundleSubject` now supports `isEqualTo` and `isNotEqualTo` for
14+
direct comparison between two `PersistableBundle`'s.
1315
* Added `integerArrayList` method to `BundleSubject`.
1416

1517
**Breaking Changes**

ext/truth/java/androidx/test/ext/truth/api/current_public.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ package androidx.test.ext.truth.os {
149149
method public final com.google.common.truth.PrimitiveIntArraySubject! intArray(String!);
150150
method public final com.google.common.truth.IntegerSubject! integer(String!);
151151
method public final void isEmpty();
152+
method public void isEqualTo(android.os.PersistableBundle!);
152153
method public final void isNotEmpty();
154+
method public void isNotEqualTo(android.os.PersistableBundle!);
153155
method public final com.google.common.truth.PrimitiveLongArraySubject! longArray(String!);
154156
method public final com.google.common.truth.LongSubject! longInt(String!);
155157
method public androidx.test.ext.truth.os.PersistableBundleSubject! persistableBundle(String!);

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.common.truth.FailureMetadata;
2222
import com.google.common.truth.Subject;
2323
import com.google.common.truth.Truth;
24+
import java.util.HashMap;
25+
import java.util.Map;
2426

2527
/**
2628
* Subject for making assertions about {@link PersistableBundle}s.
@@ -34,6 +36,43 @@ public static PersistableBundleSubject assertThat(PersistableBundle persistableB
3436
return Truth.assertAbout(persistableBundles()).that(persistableBundle);
3537
}
3638

39+
public final void isEqualTo(PersistableBundle other) {
40+
// If either are null, just fall back to an equals() comparison.
41+
if (actual == null || other == null) {
42+
super.isEqualTo(other);
43+
return;
44+
}
45+
46+
// PersistableBundle doesn't implement equals() so we convert it to a Map so we can use the
47+
// existing Truth implementation.
48+
Truth.assertThat(toMap(actual)).isEqualTo(toMap(other));
49+
}
50+
51+
public final void isNotEqualTo(PersistableBundle other) {
52+
// If either are null, just fall back to an equals() comparison.
53+
if (actual == null || other == null) {
54+
super.isNotEqualTo(other);
55+
return;
56+
}
57+
58+
// PersistableBundle doesn't implement equals() so we convert it to a Map so we can use the
59+
// existing Truth implementation.
60+
Truth.assertThat(toMap(actual)).isNotEqualTo(toMap(other));
61+
}
62+
63+
/** Converts the {@link PersistableBundle} to a {@link Map} for comparison. */
64+
private final Map<String, Object> toMap(PersistableBundle bundle) {
65+
Map<String, Object> map = new HashMap<>();
66+
for (String key : bundle.keySet()) {
67+
Object value = bundle.get(key);
68+
if (value instanceof PersistableBundle) {
69+
value = toMap((PersistableBundle) value);
70+
}
71+
map.put(key, value);
72+
}
73+
return map;
74+
}
75+
3776
public static Subject.Factory<PersistableBundleSubject, PersistableBundle> persistableBundles() {
3877
return PersistableBundleSubject::new;
3978
}

0 commit comments

Comments
 (0)