Fix nullability issues for testutil.truth
PiperOrigin-RevId: 289658098
This commit is contained in:
parent
4c74f3cffd
commit
3feb263cfe
@ -36,6 +36,8 @@ dependencies {
|
|||||||
api 'androidx.test.ext:junit:' + androidxTestJUnitVersion
|
api 'androidx.test.ext:junit:' + androidxTestJUnitVersion
|
||||||
api 'junit:junit:' + junitVersion
|
api 'junit:junit:' + junitVersion
|
||||||
api 'com.google.truth:truth:' + truthVersion
|
api 'com.google.truth:truth:' + truthVersion
|
||||||
|
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
|
||||||
|
compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion
|
||||||
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
|
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
|
||||||
implementation project(modulePrefix + 'library-core')
|
implementation project(modulePrefix + 'library-core')
|
||||||
testImplementation project(modulePrefix + 'testutils')
|
testImplementation project(modulePrefix + 'testutils')
|
||||||
|
@ -45,6 +45,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.checkerframework.checker.nullness.compatqual.NullableType;
|
||||||
|
import org.checkerframework.checker.nullness.qual.RequiresNonNull;
|
||||||
|
|
||||||
/** A Truth {@link Subject} for assertions on {@link Spanned} instances containing text styling. */
|
/** A Truth {@link Subject} for assertions on {@link Spanned} instances containing text styling. */
|
||||||
// TODO: add support for more Spans i.e. all those used in com.google.android.exoplayer2.text.
|
// TODO: add support for more Spans i.e. all those used in com.google.android.exoplayer2.text.
|
||||||
@ -74,6 +76,11 @@ public final class SpannedSubject extends Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void hasNoSpans() {
|
public void hasNoSpans() {
|
||||||
|
if (actual == null) {
|
||||||
|
failWithoutActual(simpleFact("Spanned must not be null"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Object[] spans = actual.getSpans(0, actual.length(), Object.class);
|
Object[] spans = actual.getSpans(0, actual.length(), Object.class);
|
||||||
if (spans.length > 0) {
|
if (spans.length > 0) {
|
||||||
failWithoutActual(
|
failWithoutActual(
|
||||||
@ -599,7 +606,8 @@ public final class SpannedSubject extends Subject {
|
|||||||
failWithoutActual(simpleFact("Spanned must not be null"));
|
failWithoutActual(simpleFact("Spanned must not be null"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Object[] matchingSpans = actual.getSpans(start, end, spanClazz);
|
|
||||||
|
@NullableType Object[] matchingSpans = actual.getSpans(start, end, spanClazz);
|
||||||
if (matchingSpans.length != 0) {
|
if (matchingSpans.length != 0) {
|
||||||
failWithoutActual(
|
failWithoutActual(
|
||||||
simpleFact(
|
simpleFact(
|
||||||
@ -612,15 +620,21 @@ public final class SpannedSubject extends Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T> List<T> findMatchingSpans(int startIndex, int endIndex, Class<T> spanClazz) {
|
private <T> List<T> findMatchingSpans(int startIndex, int endIndex, Class<T> spanClazz) {
|
||||||
|
if (actual == null) {
|
||||||
|
failWithoutActual(simpleFact("Spanned must not be null"));
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
List<T> spans = new ArrayList<>();
|
List<T> spans = new ArrayList<>();
|
||||||
for (T span : actual.getSpans(startIndex, endIndex, spanClazz)) {
|
for (T span : actual.getSpans(startIndex, endIndex, spanClazz)) {
|
||||||
if (actual.getSpanStart(span) == startIndex && actual.getSpanEnd(span) == endIndex) {
|
if (actual.getSpanStart(span) == startIndex && actual.getSpanEnd(span) == endIndex) {
|
||||||
spans.add(span);
|
spans.add(span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return spans;
|
return Collections.unmodifiableList(spans);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresNonNull("actual")
|
||||||
private void failWithExpectedSpan(
|
private void failWithExpectedSpan(
|
||||||
int start, int end, Class<?> spanType, String spannedSubstring) {
|
int start, int end, Class<?> spanType, String spannedSubstring) {
|
||||||
failWithoutActual(
|
failWithoutActual(
|
||||||
@ -887,7 +901,7 @@ public final class SpannedSubject extends Subject {
|
|||||||
@Override
|
@Override
|
||||||
public AndSpanFlags withFamily(String fontFamily) {
|
public AndSpanFlags withFamily(String fontFamily) {
|
||||||
List<Integer> matchingSpanFlags = new ArrayList<>();
|
List<Integer> matchingSpanFlags = new ArrayList<>();
|
||||||
List<String> spanFontFamilies = new ArrayList<>();
|
List<@NullableType String> spanFontFamilies = new ArrayList<>();
|
||||||
|
|
||||||
for (TypefaceSpan span : actualSpans) {
|
for (TypefaceSpan span : actualSpans) {
|
||||||
spanFontFamilies.add(span.getFamily());
|
spanFontFamilies.add(span.getFamily());
|
||||||
@ -1059,7 +1073,7 @@ public final class SpannedSubject extends Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(@Nullable Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2019 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.
|
||||||
|
*/
|
||||||
|
@NonNullApi
|
||||||
|
package com.google.android.exoplayer2.testutil.truth;
|
||||||
|
|
||||||
|
import com.google.android.exoplayer2.util.NonNullApi;
|
Loading…
x
Reference in New Issue
Block a user