Skip to content
Open
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
2 changes: 2 additions & 0 deletions ext/truth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

**Breaking Changes**
Expand Down
2 changes: 2 additions & 0 deletions ext/truth/java/androidx/test/ext/truth/api/current_public.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ package androidx.test.ext.truth.os {
method public final com.google.common.truth.PrimitiveIntArraySubject! intArray(String!);
method public final com.google.common.truth.IntegerSubject! integer(String!);
method public final void isEmpty();
method public void isEqualTo(android.os.PersistableBundle!);
method public final void isNotEmpty();
method public void isNotEqualTo(android.os.PersistableBundle!);
method public final com.google.common.truth.PrimitiveLongArraySubject! longArray(String!);
method public final com.google.common.truth.LongSubject! longInt(String!);
method public androidx.test.ext.truth.os.PersistableBundleSubject! persistableBundle(String!);
Expand Down
29 changes: 29 additions & 0 deletions ext/truth/java/androidx/test/ext/truth/os/BaseBundleSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.truth.PrimitiveLongArraySubject;
import com.google.common.truth.StringSubject;
import com.google.common.truth.Subject;
import java.util.Arrays;

/**
* Subject for making assertions about {@link BaseBundle}s.
Expand Down Expand Up @@ -118,4 +119,32 @@ public final PrimitiveDoubleArraySubject doubleArray(String key) {
public final ObjectArraySubject<String> stringArray(String key) {
return check("getStringArray(%s)", key).that(actual.getStringArray(key));
}

/** Checks if two bundle values are equal. */
protected final boolean isBaseBundleValueEqual(Object actual, Object expected) {
if ((actual == null) != (expected == null)) {
return false;
} else if (actual == null) {
return true;
}

// We need to iterate through each possible array type, since .equals() would just be
// referential equality.
if (actual instanceof boolean[] actualArray && expected instanceof boolean[] expectedArray) {
return Arrays.equals(actualArray, expectedArray);
} else if (actual instanceof double[] actualArray
&& expected instanceof double[] expectedArray) {
return Arrays.equals(actualArray, expectedArray);
} else if (actual instanceof int[] actualArray && expected instanceof int[] expectedArray) {
return Arrays.equals(actualArray, expectedArray);
} else if (actual instanceof long[] actualArray && expected instanceof long[] expectedArray) {
return Arrays.equals(actualArray, expectedArray);
} else if (actual instanceof String[] actualArray
&& expected instanceof String[] expectedArray) {
return Arrays.equals(actualArray, expectedArray);
} else {
// If types don't match above, or they're simply a non-array type, use normal equality.
return actual.equals(expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package androidx.test.ext.truth.os;

import static com.google.common.truth.Fact.simpleFact;

import android.os.Build;
import android.os.PersistableBundle;
import androidx.annotation.RequiresApi;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;
import java.util.Set;

/**
* Subject for making assertions about {@link PersistableBundle}s.
Expand All @@ -34,6 +37,56 @@ public static PersistableBundleSubject assertThat(PersistableBundle persistableB
return Truth.assertAbout(persistableBundles()).that(persistableBundle);
}

public final void isEqualTo(PersistableBundle other) {
if (!arePersistableBundlesEqual(actual, other)) {
failWithActual(simpleFact("expected to be equal to " + other));
}
}

public final void isNotEqualTo(PersistableBundle other) {
if (arePersistableBundlesEqual(actual, other)) {
failWithActual(simpleFact("expected to not be equal to " + other));
}
}

private final boolean arePersistableBundlesEqual(
PersistableBundle actual, PersistableBundle expected) {
if ((actual == null) != (expected == null)) {
return false;
} else if (actual == null) {
return true;
}

if (actual.size() != expected.size()) {
return false;
}

Set<String> actualKeys = actual.keySet();
Set<String> expectedKeys = expected.keySet();
if (!actualKeys.equals(expectedKeys)) {
return false;
}

for (String key : actualKeys) {
Object actualValue = actual.get(key);
Object expectedValue = expected.get(key);
boolean actualHasPersistableBundleValue = actualValue instanceof PersistableBundle;
boolean expectedHasPersistableBundleValue = expectedValue instanceof PersistableBundle;

if (actualHasPersistableBundleValue != expectedHasPersistableBundleValue) {
return false;
} else if (actualHasPersistableBundleValue) {
if (!arePersistableBundlesEqual(
actual.getPersistableBundle(key), expected.getPersistableBundle(key))) {
return false;
}
} else if (!isBaseBundleValueEqual(actualValue, expectedValue)) {
return false;
}
}
return true;
}

public static Subject.Factory<PersistableBundleSubject, PersistableBundle> persistableBundles() {
return PersistableBundleSubject::new;
}
Expand Down
Loading
Loading