Skip to content

Commit 4807b1e

Browse files
Implement full support for BundleSubject and PersistableBundleSubject assertions from BaseBundle
This creates a library-internal `BaseBundleSubject` to house support for the common implementation and adds support for the missing array types that it accepts. Now that minSdk = 23, we are no longer subject to SDK 21 + 22 limitations that historically complicated this sharing. PiperOrigin-RevId: 789381337
1 parent 0fc3552 commit 4807b1e

File tree

7 files changed

+228
-164
lines changed

7 files changed

+228
-164
lines changed

ext/truth/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
**New Features**
1010

11+
* `BundleSubject` and `PersistableBundleSubject` now share `BaseBundle` support,
12+
including additional array type assertions
13+
1114
**Breaking Changes**
1215

1316
**API Changes**

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,27 @@ package androidx.test.ext.truth.os {
105105

106106
public final class BundleSubject extends com.google.common.truth.Subject {
107107
method public static androidx.test.ext.truth.os.BundleSubject! assertThat(android.os.Bundle!);
108-
method public com.google.common.truth.BooleanSubject! bool(String!);
108+
method public final com.google.common.truth.BooleanSubject! bool(String!);
109+
method public final com.google.common.truth.PrimitiveBooleanArraySubject! booleanArray(String!);
109110
method public static com.google.common.truth.Subject.Factory<androidx.test.ext.truth.os.BundleSubject!,android.os.Bundle!>! bundles();
110111
method public com.google.common.truth.PrimitiveByteArraySubject! byteArray(String!);
111-
method public void containsKey(String!);
112-
method public void doesNotContainKey(String!);
113-
method public com.google.common.truth.DoubleSubject doubleFloat(String);
114-
method public void hasSize(int);
115-
method public com.google.common.truth.IntegerSubject! integer(String!);
116-
method public void isEmpty();
117-
method public void isNotEmpty();
118-
method public com.google.common.truth.LongSubject! longInt(String!);
112+
method public final void containsKey(String!);
113+
method public final void doesNotContainKey(String!);
114+
method public final com.google.common.truth.PrimitiveDoubleArraySubject! doubleArray(String!);
115+
method public final com.google.common.truth.DoubleSubject! doubleFloat(String!);
116+
method public final void hasSize(int);
117+
method public final com.google.common.truth.PrimitiveIntArraySubject! intArray(String!);
118+
method public final com.google.common.truth.IntegerSubject! integer(String!);
119+
method public final void isEmpty();
120+
method public final void isNotEmpty();
121+
method public final com.google.common.truth.PrimitiveLongArraySubject! longArray(String!);
122+
method public final com.google.common.truth.LongSubject! longInt(String!);
119123
method public <T extends android.os.Parcelable> androidx.test.ext.truth.os.ParcelableSubject<T!>! parcelable(String!);
120124
method public com.google.common.truth.IterableSubject! parcelableArrayList(String!);
121125
method public <T extends android.os.Parcelable, SubjectT extends com.google.common.truth.Subject> SubjectT! parcelableAsType(String!, com.google.common.truth.Subject.Factory<SubjectT!,T!>!);
122126
method public com.google.common.truth.Subject! serializable(String!);
123-
method public com.google.common.truth.StringSubject! string(String!);
124-
method public com.google.common.truth.ObjectArraySubject<java.lang.String!> stringArray(String);
127+
method public final com.google.common.truth.StringSubject! string(String!);
128+
method public final com.google.common.truth.ObjectArraySubject<java.lang.String!>! stringArray(String!);
125129
method public com.google.common.truth.IterableSubject! stringArrayList(String!);
126130
}
127131

@@ -134,19 +138,23 @@ package androidx.test.ext.truth.os {
134138

135139
@RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP) public final class PersistableBundleSubject extends com.google.common.truth.Subject {
136140
method public static androidx.test.ext.truth.os.PersistableBundleSubject! assertThat(android.os.PersistableBundle!);
137-
method @RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP_MR1) public com.google.common.truth.BooleanSubject! bool(String!);
138-
method public void containsKey(String!);
139-
method public void doesNotContainKey(String!);
140-
method public com.google.common.truth.DoubleSubject doubleFloat(String);
141-
method public void hasSize(int);
142-
method public com.google.common.truth.IntegerSubject! integer(String!);
143-
method public void isEmpty();
144-
method public void isNotEmpty();
145-
method public com.google.common.truth.LongSubject! longInt(String!);
141+
method public final com.google.common.truth.BooleanSubject! bool(String!);
142+
method public final com.google.common.truth.PrimitiveBooleanArraySubject! booleanArray(String!);
143+
method public final void containsKey(String!);
144+
method public final void doesNotContainKey(String!);
145+
method public final com.google.common.truth.PrimitiveDoubleArraySubject! doubleArray(String!);
146+
method public final com.google.common.truth.DoubleSubject! doubleFloat(String!);
147+
method public final void hasSize(int);
148+
method public final com.google.common.truth.PrimitiveIntArraySubject! intArray(String!);
149+
method public final com.google.common.truth.IntegerSubject! integer(String!);
150+
method public final void isEmpty();
151+
method public final void isNotEmpty();
152+
method public final com.google.common.truth.PrimitiveLongArraySubject! longArray(String!);
153+
method public final com.google.common.truth.LongSubject! longInt(String!);
146154
method public androidx.test.ext.truth.os.PersistableBundleSubject! persistableBundle(String!);
147155
method public static com.google.common.truth.Subject.Factory<androidx.test.ext.truth.os.PersistableBundleSubject!,android.os.PersistableBundle!>! persistableBundles();
148-
method public com.google.common.truth.StringSubject! string(String!);
149-
method public com.google.common.truth.ObjectArraySubject<java.lang.String!> stringArray(String);
156+
method public final com.google.common.truth.StringSubject! string(String!);
157+
method public final com.google.common.truth.ObjectArraySubject<java.lang.String!>! stringArray(String!);
150158
}
151159

152160
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2018 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package androidx.test.ext.truth.os;
17+
18+
import static com.google.common.truth.Fact.simpleFact;
19+
20+
import android.os.BaseBundle;
21+
import androidx.annotation.RestrictTo;
22+
import androidx.annotation.RestrictTo.Scope;
23+
import com.google.common.truth.BooleanSubject;
24+
import com.google.common.truth.DoubleSubject;
25+
import com.google.common.truth.FailureMetadata;
26+
import com.google.common.truth.IntegerSubject;
27+
import com.google.common.truth.LongSubject;
28+
import com.google.common.truth.ObjectArraySubject;
29+
import com.google.common.truth.PrimitiveBooleanArraySubject;
30+
import com.google.common.truth.PrimitiveDoubleArraySubject;
31+
import com.google.common.truth.PrimitiveIntArraySubject;
32+
import com.google.common.truth.PrimitiveLongArraySubject;
33+
import com.google.common.truth.StringSubject;
34+
import com.google.common.truth.Subject;
35+
36+
/**
37+
* Subject for making assertions about {@link BaseBundle}s.
38+
*
39+
* <p>Concrete implementations for external users are {@link BundleSubject} and {@link
40+
* PersistableBundleSubject}.
41+
*
42+
* @hide
43+
*/
44+
@RestrictTo(Scope.LIBRARY)
45+
public abstract class BaseBundleSubject extends Subject {
46+
47+
private final BaseBundle actual;
48+
49+
BaseBundleSubject(FailureMetadata failureMetadata, BaseBundle subject) {
50+
super(failureMetadata, subject);
51+
this.actual = subject;
52+
}
53+
54+
public final void isEmpty() {
55+
if (!actual.isEmpty()) {
56+
failWithActual(simpleFact("expected to be empty"));
57+
}
58+
}
59+
60+
public final void isNotEmpty() {
61+
if (actual.isEmpty()) {
62+
failWithActual(simpleFact("expected to be non-empty"));
63+
}
64+
}
65+
66+
public final void hasSize(int size) {
67+
check("size()").that(actual.size()).isEqualTo(size);
68+
}
69+
70+
public final void containsKey(String key) {
71+
if (!actual.containsKey(key)) {
72+
failWithActual(simpleFact("expected to contain key " + key));
73+
}
74+
}
75+
76+
public final void doesNotContainKey(String key) {
77+
if (actual.containsKey(key)) {
78+
failWithActual(simpleFact("expected to not contain key " + key));
79+
}
80+
}
81+
82+
public final BooleanSubject bool(String key) {
83+
return check("getBoolean(%s)", key).that(actual.getBoolean(key));
84+
}
85+
86+
public final IntegerSubject integer(String key) {
87+
return check("getInt(%s)", key).that(actual.getInt(key));
88+
}
89+
90+
public final LongSubject longInt(String key) {
91+
return check("getLong(%s)", key).that(actual.getLong(key));
92+
}
93+
94+
public final DoubleSubject doubleFloat(String key) {
95+
return check("getDouble(%s)", key).that(actual.getDouble(key));
96+
}
97+
98+
public final StringSubject string(String key) {
99+
return check("getString(%s)", key).that(actual.getString(key));
100+
}
101+
102+
public final PrimitiveBooleanArraySubject booleanArray(String key) {
103+
return check("getBooleanArray(%s)", key).that(actual.getBooleanArray(key));
104+
}
105+
106+
public final PrimitiveIntArraySubject intArray(String key) {
107+
return check("getIntArray(%s)", key).that(actual.getIntArray(key));
108+
}
109+
110+
public final PrimitiveLongArraySubject longArray(String key) {
111+
return check("getLongArray(%s)", key).that(actual.getLongArray(key));
112+
}
113+
114+
public final PrimitiveDoubleArraySubject doubleArray(String key) {
115+
return check("getDoubleArray(%s)", key).that(actual.getDoubleArray(key));
116+
}
117+
118+
public final ObjectArraySubject<String> stringArray(String key) {
119+
return check("getStringArray(%s)", key).that(actual.getStringArray(key));
120+
}
121+
}

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

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,20 @@
1515
*/
1616
package androidx.test.ext.truth.os;
1717

18-
import static com.google.common.truth.Fact.simpleFact;
19-
2018
import android.os.Bundle;
2119
import android.os.Parcelable;
22-
import androidx.annotation.NonNull;
23-
import com.google.common.truth.BooleanSubject;
24-
import com.google.common.truth.DoubleSubject;
2520
import com.google.common.truth.FailureMetadata;
26-
import com.google.common.truth.IntegerSubject;
2721
import com.google.common.truth.IterableSubject;
28-
import com.google.common.truth.LongSubject;
29-
import com.google.common.truth.ObjectArraySubject;
3022
import com.google.common.truth.PrimitiveByteArraySubject;
31-
import com.google.common.truth.StringSubject;
3223
import com.google.common.truth.Subject;
3324
import com.google.common.truth.Truth;
3425

35-
// LINT.IfChange
36-
// TODO(b/308978831) once minSdkVersion >= 21, unify most methods with PersistableBundleSubject
37-
// through a (library-internal) BaseBundleSubject superclass.
3826
/**
3927
* Subject for making assertions about {@link Bundle}s.
4028
*
4129
* <p>To assert about {@link android.os.PersistableBundle}s, use {@link PersistableBundleSubject}.
4230
*/
43-
public final class BundleSubject extends Subject {
31+
public final class BundleSubject extends BaseBundleSubject {
4432

4533
public static BundleSubject assertThat(Bundle bundle) {
4634
return Truth.assertAbout(bundles()).that(bundle);
@@ -57,43 +45,6 @@ public static Subject.Factory<BundleSubject, Bundle> bundles() {
5745
this.actual = subject;
5846
}
5947

60-
public void hasSize(int size) {
61-
check("size()").that(actual.size()).isEqualTo(size);
62-
}
63-
64-
public void isEmpty() {
65-
if (!actual.isEmpty()) {
66-
failWithActual(simpleFact("expected to be empty"));
67-
}
68-
}
69-
70-
public void isNotEmpty() {
71-
if (actual.isEmpty()) {
72-
failWithActual(simpleFact("expected to be non-empty"));
73-
}
74-
}
75-
76-
public StringSubject string(String key) {
77-
return check("getString(%s)", key).that(actual.getString(key));
78-
}
79-
80-
public IntegerSubject integer(String key) {
81-
return check("getInt(%s)", key).that(actual.getInt(key));
82-
}
83-
84-
public LongSubject longInt(String key) {
85-
return check("getLong(%s)", key).that(actual.getLong(key));
86-
}
87-
88-
@NonNull
89-
public DoubleSubject doubleFloat(@NonNull String key) {
90-
return check("getDouble(%s)", key).that(actual.getDouble(key));
91-
}
92-
93-
public BooleanSubject bool(String key) {
94-
return check("getBoolean(%s)", key).that(actual.getBoolean(key));
95-
}
96-
9748
public PrimitiveByteArraySubject byteArray(String key) {
9849
return check("getByteArray(%s)", key).that(actual.getByteArray(key));
9950
}
@@ -109,11 +60,6 @@ public <T extends Parcelable, SubjectT extends Subject> SubjectT parcelableAsTyp
10960
return check("getParcelable(%s)", key).about(subjectFactory).that(actual.<T>getParcelable(key));
11061
}
11162

112-
@NonNull
113-
public ObjectArraySubject<String> stringArray(@NonNull String key) {
114-
return check("getStringArray(%s)", key).that(actual.getStringArray(key));
115-
}
116-
11763
public IterableSubject stringArrayList(String key) {
11864
return check("getStringArrayList(%s)", key).that(actual.getStringArrayList(key));
11965
}
@@ -126,17 +72,4 @@ public IterableSubject parcelableArrayList(String key) {
12672
public Subject serializable(String key) {
12773
return check("getSerializable(%s)", key).that(actual.getSerializable(key));
12874
}
129-
130-
public void containsKey(String key) {
131-
if (!actual.containsKey(key)) {
132-
failWithActual(simpleFact("expected to contain key " + key));
133-
}
134-
}
135-
136-
public void doesNotContainKey(String key) {
137-
if (actual.containsKey(key)) {
138-
failWithActual(simpleFact("expected to not contain key " + key));
139-
}
140-
}
14175
}
142-
// LINT.ThenChange(PersistableBundleSubject.java)

0 commit comments

Comments
 (0)