Fix nullability issues for testutil.truth

PiperOrigin-RevId: 289658098
This commit is contained in:
olly 2020-01-14 16:35:16 +00:00 committed by Oliver Woodman
parent 4c74f3cffd
commit 3feb263cfe
3 changed files with 39 additions and 4 deletions

View File

@ -36,6 +36,8 @@ dependencies {
api 'androidx.test.ext:junit:' + androidxTestJUnitVersion
api 'junit:junit:' + junitVersion
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 project(modulePrefix + 'library-core')
testImplementation project(modulePrefix + 'testutils')

View File

@ -45,6 +45,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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. */
// 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() {
if (actual == null) {
failWithoutActual(simpleFact("Spanned must not be null"));
return;
}
Object[] spans = actual.getSpans(0, actual.length(), Object.class);
if (spans.length > 0) {
failWithoutActual(
@ -599,7 +606,8 @@ public final class SpannedSubject extends Subject {
failWithoutActual(simpleFact("Spanned must not be null"));
return;
}
Object[] matchingSpans = actual.getSpans(start, end, spanClazz);
@NullableType Object[] matchingSpans = actual.getSpans(start, end, spanClazz);
if (matchingSpans.length != 0) {
failWithoutActual(
simpleFact(
@ -612,15 +620,21 @@ public final class SpannedSubject extends Subject {
}
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<>();
for (T span : actual.getSpans(startIndex, endIndex, spanClazz)) {
if (actual.getSpanStart(span) == startIndex && actual.getSpanEnd(span) == endIndex) {
spans.add(span);
}
}
return spans;
return Collections.unmodifiableList(spans);
}
@RequiresNonNull("actual")
private void failWithExpectedSpan(
int start, int end, Class<?> spanType, String spannedSubstring) {
failWithoutActual(
@ -887,7 +901,7 @@ public final class SpannedSubject extends Subject {
@Override
public AndSpanFlags withFamily(String fontFamily) {
List<Integer> matchingSpanFlags = new ArrayList<>();
List<String> spanFontFamilies = new ArrayList<>();
List<@NullableType String> spanFontFamilies = new ArrayList<>();
for (TypefaceSpan span : actualSpans) {
spanFontFamilies.add(span.getFamily());
@ -1059,7 +1073,7 @@ public final class SpannedSubject extends Subject {
}
@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}

View File

@ -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;