From 3feb263cfe82602b7b9c7990babb7320b4d37dbc Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 14 Jan 2020 16:35:16 +0000 Subject: [PATCH] Fix nullability issues for testutil.truth PiperOrigin-RevId: 289658098 --- testutils/build.gradle | 2 ++ .../testutil/truth/SpannedSubject.java | 22 +++++++++++++++---- .../testutil/truth/package-info.java | 19 ++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/package-info.java diff --git a/testutils/build.gradle b/testutils/build.gradle index 83537bcef1..9309c23bcd 100644 --- a/testutils/build.gradle +++ b/testutils/build.gradle @@ -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') diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java index db76208ce9..f5506de649 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java @@ -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 List findMatchingSpans(int startIndex, int endIndex, Class spanClazz) { + if (actual == null) { + failWithoutActual(simpleFact("Spanned must not be null")); + return Collections.emptyList(); + } + List 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 matchingSpanFlags = new ArrayList<>(); - List 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; } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/package-info.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/package-info.java new file mode 100644 index 0000000000..003abfc059 --- /dev/null +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/package-info.java @@ -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;