diff --git a/ext/truth/CHANGELOG.md b/ext/truth/CHANGELOG.md index 83ec4994a..28322cd03 100644 --- a/ext/truth/CHANGELOG.md +++ b/ext/truth/CHANGELOG.md @@ -8,6 +8,9 @@ **New Features** +* `BundleSubject` and `PersistableBundleSubject` now share `BaseBundle` support, + including additional array type assertions + **Breaking Changes** **API Changes** diff --git a/ext/truth/java/androidx/test/ext/truth/api/current_public.txt b/ext/truth/java/androidx/test/ext/truth/api/current_public.txt index 0dc74d0ba..e8411dfa8 100644 --- a/ext/truth/java/androidx/test/ext/truth/api/current_public.txt +++ b/ext/truth/java/androidx/test/ext/truth/api/current_public.txt @@ -105,23 +105,27 @@ package androidx.test.ext.truth.os { public final class BundleSubject extends com.google.common.truth.Subject { method public static androidx.test.ext.truth.os.BundleSubject! assertThat(android.os.Bundle!); - method public com.google.common.truth.BooleanSubject! bool(String!); + method public final com.google.common.truth.BooleanSubject! bool(String!); + method public final com.google.common.truth.PrimitiveBooleanArraySubject! booleanArray(String!); method public static com.google.common.truth.Subject.Factory! bundles(); method public com.google.common.truth.PrimitiveByteArraySubject! byteArray(String!); - method public void containsKey(String!); - method public void doesNotContainKey(String!); - method public com.google.common.truth.DoubleSubject doubleFloat(String); - method public void hasSize(int); - method public com.google.common.truth.IntegerSubject! integer(String!); - method public void isEmpty(); - method public void isNotEmpty(); - method public com.google.common.truth.LongSubject! longInt(String!); + method public final void containsKey(String!); + method public final void doesNotContainKey(String!); + method public final com.google.common.truth.PrimitiveDoubleArraySubject! doubleArray(String!); + method public final com.google.common.truth.DoubleSubject! doubleFloat(String!); + method public final void hasSize(int); + 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 final void isNotEmpty(); + 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.ParcelableSubject! parcelable(String!); method public com.google.common.truth.IterableSubject! parcelableArrayList(String!); method public SubjectT! parcelableAsType(String!, com.google.common.truth.Subject.Factory!); method public com.google.common.truth.Subject! serializable(String!); - method public com.google.common.truth.StringSubject! string(String!); - method public com.google.common.truth.ObjectArraySubject stringArray(String); + method public final com.google.common.truth.StringSubject! string(String!); + method public final com.google.common.truth.ObjectArraySubject! stringArray(String!); method public com.google.common.truth.IterableSubject! stringArrayList(String!); } @@ -134,19 +138,23 @@ package androidx.test.ext.truth.os { @RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP) public final class PersistableBundleSubject extends com.google.common.truth.Subject { method public static androidx.test.ext.truth.os.PersistableBundleSubject! assertThat(android.os.PersistableBundle!); - method @RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP_MR1) public com.google.common.truth.BooleanSubject! bool(String!); - method public void containsKey(String!); - method public void doesNotContainKey(String!); - method public com.google.common.truth.DoubleSubject doubleFloat(String); - method public void hasSize(int); - method public com.google.common.truth.IntegerSubject! integer(String!); - method public void isEmpty(); - method public void isNotEmpty(); - method public com.google.common.truth.LongSubject! longInt(String!); + method public final com.google.common.truth.BooleanSubject! bool(String!); + method public final com.google.common.truth.PrimitiveBooleanArraySubject! booleanArray(String!); + method public final void containsKey(String!); + method public final void doesNotContainKey(String!); + method public final com.google.common.truth.PrimitiveDoubleArraySubject! doubleArray(String!); + method public final com.google.common.truth.DoubleSubject! doubleFloat(String!); + method public final void hasSize(int); + 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 final void isNotEmpty(); + 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!); method public static com.google.common.truth.Subject.Factory! persistableBundles(); - method public com.google.common.truth.StringSubject! string(String!); - method public com.google.common.truth.ObjectArraySubject stringArray(String); + method public final com.google.common.truth.StringSubject! string(String!); + method public final com.google.common.truth.ObjectArraySubject! stringArray(String!); } } diff --git a/ext/truth/java/androidx/test/ext/truth/os/BaseBundleSubject.java b/ext/truth/java/androidx/test/ext/truth/os/BaseBundleSubject.java new file mode 100644 index 000000000..d98c3ec29 --- /dev/null +++ b/ext/truth/java/androidx/test/ext/truth/os/BaseBundleSubject.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.test.ext.truth.os; + +import static com.google.common.truth.Fact.simpleFact; + +import android.os.BaseBundle; +import androidx.annotation.RestrictTo; +import androidx.annotation.RestrictTo.Scope; +import com.google.common.truth.BooleanSubject; +import com.google.common.truth.DoubleSubject; +import com.google.common.truth.FailureMetadata; +import com.google.common.truth.IntegerSubject; +import com.google.common.truth.LongSubject; +import com.google.common.truth.ObjectArraySubject; +import com.google.common.truth.PrimitiveBooleanArraySubject; +import com.google.common.truth.PrimitiveDoubleArraySubject; +import com.google.common.truth.PrimitiveIntArraySubject; +import com.google.common.truth.PrimitiveLongArraySubject; +import com.google.common.truth.StringSubject; +import com.google.common.truth.Subject; + +/** + * Subject for making assertions about {@link BaseBundle}s. + * + *

Concrete implementations for external users are {@link BundleSubject} and {@link + * PersistableBundleSubject}. + * + * @hide + */ +@RestrictTo(Scope.LIBRARY) +public abstract class BaseBundleSubject extends Subject { + + private final BaseBundle actual; + + BaseBundleSubject(FailureMetadata failureMetadata, BaseBundle subject) { + super(failureMetadata, subject); + this.actual = subject; + } + + public final void isEmpty() { + if (!actual.isEmpty()) { + failWithActual(simpleFact("expected to be empty")); + } + } + + public final void isNotEmpty() { + if (actual.isEmpty()) { + failWithActual(simpleFact("expected to be non-empty")); + } + } + + public final void hasSize(int size) { + check("size()").that(actual.size()).isEqualTo(size); + } + + public final void containsKey(String key) { + if (!actual.containsKey(key)) { + failWithActual(simpleFact("expected to contain key " + key)); + } + } + + public final void doesNotContainKey(String key) { + if (actual.containsKey(key)) { + failWithActual(simpleFact("expected to not contain key " + key)); + } + } + + public final BooleanSubject bool(String key) { + return check("getBoolean(%s)", key).that(actual.getBoolean(key)); + } + + public final IntegerSubject integer(String key) { + return check("getInt(%s)", key).that(actual.getInt(key)); + } + + public final LongSubject longInt(String key) { + return check("getLong(%s)", key).that(actual.getLong(key)); + } + + public final DoubleSubject doubleFloat(String key) { + return check("getDouble(%s)", key).that(actual.getDouble(key)); + } + + public final StringSubject string(String key) { + return check("getString(%s)", key).that(actual.getString(key)); + } + + public final PrimitiveBooleanArraySubject booleanArray(String key) { + return check("getBooleanArray(%s)", key).that(actual.getBooleanArray(key)); + } + + public final PrimitiveIntArraySubject intArray(String key) { + return check("getIntArray(%s)", key).that(actual.getIntArray(key)); + } + + public final PrimitiveLongArraySubject longArray(String key) { + return check("getLongArray(%s)", key).that(actual.getLongArray(key)); + } + + public final PrimitiveDoubleArraySubject doubleArray(String key) { + return check("getDoubleArray(%s)", key).that(actual.getDoubleArray(key)); + } + + public final ObjectArraySubject stringArray(String key) { + return check("getStringArray(%s)", key).that(actual.getStringArray(key)); + } +} diff --git a/ext/truth/java/androidx/test/ext/truth/os/BundleSubject.java b/ext/truth/java/androidx/test/ext/truth/os/BundleSubject.java index b2cfc344c..7579d7341 100644 --- a/ext/truth/java/androidx/test/ext/truth/os/BundleSubject.java +++ b/ext/truth/java/androidx/test/ext/truth/os/BundleSubject.java @@ -15,32 +15,20 @@ */ package androidx.test.ext.truth.os; -import static com.google.common.truth.Fact.simpleFact; - import android.os.Bundle; import android.os.Parcelable; -import androidx.annotation.NonNull; -import com.google.common.truth.BooleanSubject; -import com.google.common.truth.DoubleSubject; import com.google.common.truth.FailureMetadata; -import com.google.common.truth.IntegerSubject; import com.google.common.truth.IterableSubject; -import com.google.common.truth.LongSubject; -import com.google.common.truth.ObjectArraySubject; import com.google.common.truth.PrimitiveByteArraySubject; -import com.google.common.truth.StringSubject; import com.google.common.truth.Subject; import com.google.common.truth.Truth; -// LINT.IfChange -// TODO(b/308978831) once minSdkVersion >= 21, unify most methods with PersistableBundleSubject -// through a (library-internal) BaseBundleSubject superclass. /** * Subject for making assertions about {@link Bundle}s. * *

To assert about {@link android.os.PersistableBundle}s, use {@link PersistableBundleSubject}. */ -public final class BundleSubject extends Subject { +public final class BundleSubject extends BaseBundleSubject { public static BundleSubject assertThat(Bundle bundle) { return Truth.assertAbout(bundles()).that(bundle); @@ -57,43 +45,6 @@ public static Subject.Factory bundles() { this.actual = subject; } - public void hasSize(int size) { - check("size()").that(actual.size()).isEqualTo(size); - } - - public void isEmpty() { - if (!actual.isEmpty()) { - failWithActual(simpleFact("expected to be empty")); - } - } - - public void isNotEmpty() { - if (actual.isEmpty()) { - failWithActual(simpleFact("expected to be non-empty")); - } - } - - public StringSubject string(String key) { - return check("getString(%s)", key).that(actual.getString(key)); - } - - public IntegerSubject integer(String key) { - return check("getInt(%s)", key).that(actual.getInt(key)); - } - - public LongSubject longInt(String key) { - return check("getLong(%s)", key).that(actual.getLong(key)); - } - - @NonNull - public DoubleSubject doubleFloat(@NonNull String key) { - return check("getDouble(%s)", key).that(actual.getDouble(key)); - } - - public BooleanSubject bool(String key) { - return check("getBoolean(%s)", key).that(actual.getBoolean(key)); - } - public PrimitiveByteArraySubject byteArray(String key) { return check("getByteArray(%s)", key).that(actual.getByteArray(key)); } @@ -109,11 +60,6 @@ public SubjectT parcelableAsTyp return check("getParcelable(%s)", key).about(subjectFactory).that(actual.getParcelable(key)); } - @NonNull - public ObjectArraySubject stringArray(@NonNull String key) { - return check("getStringArray(%s)", key).that(actual.getStringArray(key)); - } - public IterableSubject stringArrayList(String key) { return check("getStringArrayList(%s)", key).that(actual.getStringArrayList(key)); } @@ -126,17 +72,4 @@ public IterableSubject parcelableArrayList(String key) { public Subject serializable(String key) { return check("getSerializable(%s)", key).that(actual.getSerializable(key)); } - - public void containsKey(String key) { - if (!actual.containsKey(key)) { - failWithActual(simpleFact("expected to contain key " + key)); - } - } - - public void doesNotContainKey(String key) { - if (actual.containsKey(key)) { - failWithActual(simpleFact("expected to not contain key " + key)); - } - } } -// LINT.ThenChange(PersistableBundleSubject.java) 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 cae703f3d..284cceb11 100644 --- a/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java +++ b/ext/truth/java/androidx/test/ext/truth/os/PersistableBundleSubject.java @@ -15,32 +15,20 @@ */ 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.NonNull; import androidx.annotation.RequiresApi; -import com.google.common.truth.BooleanSubject; -import com.google.common.truth.DoubleSubject; import com.google.common.truth.FailureMetadata; -import com.google.common.truth.IntegerSubject; -import com.google.common.truth.LongSubject; -import com.google.common.truth.ObjectArraySubject; -import com.google.common.truth.StringSubject; import com.google.common.truth.Subject; import com.google.common.truth.Truth; -// LINT.IfChange -// TODO(b/308978831) once minSdkVersion >= 21, unify most methods with BundleSubject through a -// (library-internal) BaseBundleSubject superclass. /** * Subject for making assertions about {@link PersistableBundle}s. * *

To assert about "regular" {@link android.os.Bundle}s, use {@link BundleSubject}. */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -public final class PersistableBundleSubject extends Subject { +public final class PersistableBundleSubject extends BaseBundleSubject { public static PersistableBundleSubject assertThat(PersistableBundle persistableBundle) { return Truth.assertAbout(persistableBundles()).that(persistableBundle); @@ -57,65 +45,9 @@ public static Subject.Factory persi this.actual = subject; } - public void hasSize(int size) { - check("size()").that(actual.size()).isEqualTo(size); - } - - public void isEmpty() { - if (!actual.isEmpty()) { - failWithActual(simpleFact("expected to be empty")); - } - } - - public void isNotEmpty() { - if (actual.isEmpty()) { - failWithActual(simpleFact("expected to be non-empty")); - } - } - - public StringSubject string(String key) { - return check("getString(%s)", key).that(actual.getString(key)); - } - - public IntegerSubject integer(String key) { - return check("getInt(%s)", key).that(actual.getInt(key)); - } - - public LongSubject longInt(String key) { - return check("getLong(%s)", key).that(actual.getLong(key)); - } - - @NonNull - public DoubleSubject doubleFloat(@NonNull String key) { - return check("getDouble(%s)", key).that(actual.getDouble(key)); - } - - @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1) - public BooleanSubject bool(String key) { - return check("getBoolean(%s)", key).that(actual.getBoolean(key)); - } - - @NonNull - public ObjectArraySubject stringArray(@NonNull String key) { - return check("getStringArray(%s)", key).that(actual.getStringArray(key)); - } - public PersistableBundleSubject persistableBundle(String key) { return check("getPersistableBundle(%s)", key) .about(persistableBundles()) .that(actual.getPersistableBundle(key)); } - - public void containsKey(String key) { - if (!actual.containsKey(key)) { - failWithActual(simpleFact("expected to contain key " + key)); - } - } - - public void doesNotContainKey(String key) { - if (actual.containsKey(key)) { - failWithActual(simpleFact("expected to not contain key " + key)); - } - } } -// LINT.ThenChange(BundleSubject.java) diff --git a/ext/truth/javatests/androidx/test/ext/truth/os/BundleSubjectTest.java b/ext/truth/javatests/androidx/test/ext/truth/os/BundleSubjectTest.java index 685491c7e..409413b73 100644 --- a/ext/truth/javatests/androidx/test/ext/truth/os/BundleSubjectTest.java +++ b/ext/truth/javatests/androidx/test/ext/truth/os/BundleSubjectTest.java @@ -122,6 +122,42 @@ public void parcelableAsType() { assertThat(bundle).parcelableAsType("foo", IntentSubject.intents()).hasAction("bar"); } + @Test + public void booleanArray() { + Bundle bundle = new Bundle(); + bundle.putBooleanArray("foo", new boolean[] {true, false}); + + assertThat(bundle).booleanArray("foo").asList().containsExactly(true, false).inOrder(); + } + + @Test + public void intArray() { + Bundle bundle = new Bundle(); + bundle.putIntArray("foo", new int[] {1, 2, 3}); + + assertThat(bundle).intArray("foo").asList().containsExactly(1, 2, 3).inOrder(); + } + + @Test + public void longArray() { + Bundle bundle = new Bundle(); + bundle.putLongArray("foo", new long[] {1L, 2L, 3L}); + + assertThat(bundle).longArray("foo").asList().containsExactly(1L, 2L, 3L).inOrder(); + } + + @Test + public void doubleArray() { + Bundle bundle = new Bundle(); + bundle.putDoubleArray("foo", new double[] {1.0, 2.0, 3.0}); + + assertThat(bundle) + .doubleArray("foo") + .usingExactEquality() + .containsExactly(1.0, 2.0, 3.0) + .inOrder(); + } + @Test public void stringArray() { Bundle bundle = new Bundle(); 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 7819b1934..bca3e868b 100644 --- a/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java +++ b/ext/truth/javatests/androidx/test/ext/truth/os/PersistableBundleSubjectTest.java @@ -16,9 +16,7 @@ package androidx.test.ext.truth.os; import static androidx.test.ext.truth.os.PersistableBundleSubject.assertThat; -import static org.junit.Assume.assumeTrue; -import android.os.Build; import android.os.PersistableBundle; import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Test; @@ -88,15 +86,48 @@ public void doubleFloat() { @Test public void bool() { - // PersistableBundle (and BaseBundle) didn't get boolean support until 22 - assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1); - PersistableBundle bundle = new PersistableBundle(); bundle.putBoolean("foo", true); assertThat(bundle).bool("foo").isTrue(); } + @Test + public void booleanArray() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putBooleanArray("foo", new boolean[] {true, false}); + + assertThat(bundle).booleanArray("foo").asList().containsExactly(true, false).inOrder(); + } + + @Test + public void intArray() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putIntArray("foo", new int[] {1, 2, 3}); + + assertThat(bundle).intArray("foo").asList().containsExactly(1, 2, 3).inOrder(); + } + + @Test + public void longArray() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putLongArray("foo", new long[] {1L, 2L, 3L}); + + assertThat(bundle).longArray("foo").asList().containsExactly(1L, 2L, 3L).inOrder(); + } + + @Test + public void doubleArray() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putDoubleArray("foo", new double[] {1.0, 2.0, 3.0}); + + assertThat(bundle) + .doubleArray("foo") + .usingExactEquality() + .containsExactly(1.0, 2.0, 3.0) + .inOrder(); + } + @Test public void stringArray() { PersistableBundle bundle = new PersistableBundle();