Add CTS test for underlines not affecting width
We measure some text with and without an UnderlineSpan with a
boundary where the text would be kerned. The widths should be the
same.
Bug: 32907446
Test: cts-tradefed run cts-dev --module CtsTextTestCases
Change-Id: Ib0cfeeb72cc45c3bb44ccc29327f590ab62e387a
diff --git a/tests/tests/text/src/android/text/style/cts/UnderlineSpanTest.java b/tests/tests/text/src/android/text/style/cts/UnderlineSpanTest.java
index 0661833..c93dd39 100644
--- a/tests/tests/text/src/android/text/style/cts/UnderlineSpanTest.java
+++ b/tests/tests/text/src/android/text/style/cts/UnderlineSpanTest.java
@@ -16,12 +16,16 @@
package android.text.style.cts;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.os.Parcel;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
+import android.text.SpannableString;
+import android.text.Spanned;
+import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.style.UnderlineSpan;
@@ -85,4 +89,26 @@
p.recycle();
}
}
+
+ @Test
+ public void testDoesntAffectWidth() {
+ // Roboto kerns between "P" and "."
+ final SpannableString text = new SpannableString("P. T");
+
+ final TextPaint tp = new TextPaint();
+ tp.setTextSize(100.0f); // Large enough so that the difference in kerning is visible.
+ final int largeWidth = 10000; // Enough width so the whole text fits in one line.
+ final StaticLayout origLayout = StaticLayout.Builder.obtain(
+ text, 0, text.length(), tp, largeWidth).build();
+ final float origLineWidth = origLayout.getLineWidth(0);
+
+ // Underline just the "P".
+ text.setSpan(new UnderlineSpan(), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+
+ final StaticLayout underlinedLayout = StaticLayout.Builder.obtain(
+ text, 0, text.length(), tp, largeWidth).build();
+ final float underlinedLineWidth = underlinedLayout.getLineWidth(0);
+
+ assertEquals(origLineWidth, underlinedLineWidth, 0.0f);
+ }
}