Avoid NPE in trimToLengthWithEllipsis().

Currently, the text paramter is annotated with @Nullable
but passing a null value will cause an NPE.

Test: atest core/tests/coretests/src/android/text/TextUtilsTest.java
Change-Id: I95a71246aa0fe3398f09cb9b4b037ca096900ad4
This commit is contained in:
Sudheer Shanka
2020-10-08 01:44:52 -07:00
parent 58686805ae
commit bfdc47752e
2 changed files with 2 additions and 1 deletions

View File

@@ -2173,7 +2173,7 @@ public class TextUtils {
public static <T extends CharSequence> T trimToLengthWithEllipsis(@Nullable T text,
@IntRange(from = 1) int size) {
T trimmed = trimToSize(text, size);
if (trimmed.length() < text.length()) {
if (text != null && trimmed.length() < text.length()) {
trimmed = (T) (trimmed.toString() + "...");
}
return trimmed;

View File

@@ -792,5 +792,6 @@ public class TextUtilsTest {
assertEquals("ABC...", TextUtils.trimToLengthWithEllipsis("ABCDEF", 3));
assertEquals("ABC", TextUtils.trimToLengthWithEllipsis("ABC", 3));
assertEquals("", TextUtils.trimToLengthWithEllipsis("", 3));
assertNull(TextUtils.trimToLengthWithEllipsis(null, 3));
}
}