[SuwLib] Add test for multiline spans for LinkAccessibilityHelper

Bug: 31312797
Test: ./gradlew connectedAndroidTest
Change-Id: Id181b65ea110b4e5e42ef3646463636a6e78a731
This commit is contained in:
Maurice Lam 2016-08-30 15:58:00 -07:00
parent 0e7f4375e9
commit 7c7aa7d0d3

View file

@ -18,6 +18,7 @@ package com.android.setupwizardlib.test;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.text.BidiFormatter;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v4.widget.ExploreByTouchHelper;
import android.test.AndroidTestCase;
@ -25,6 +26,7 @@ import android.test.suitebuilder.annotation.SmallTest;
import android.text.SpannableStringBuilder;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.TextView;
@ -37,36 +39,23 @@ import java.util.List;
public class LinkAccessibilityHelperTest extends AndroidTestCase {
private static final LinkSpan LINK_SPAN = new LinkSpan("foobar");
private TextView mTextView;
private TestLinkAccessibilityHelper mHelper;
private LinkSpan mSpan;
private DisplayMetrics mDisplayMetrics;
@Override
protected void setUp() throws Exception {
super.setUp();
mSpan = new LinkSpan("foobar");
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
ssb.setSpan(mSpan, 1, 2, 0 /* flags */);
mTextView = new TextView(getContext());
mTextView.setText(ssb);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
mHelper = new TestLinkAccessibilityHelper(mTextView);
mTextView.measure(dp2Px(500), dp2Px(500));
mTextView.layout(dp2Px(0), dp2Px(0), dp2Px(500), dp2Px(500));
}
@SmallTest
public void testGetVirtualViewAt() {
initTextView();
final int virtualViewId = mHelper.getVirtualViewAt(dp2Px(15), dp2Px(10));
assertEquals("Virtual view ID should be 1", 1, virtualViewId);
}
@SmallTest
public void testGetVirtualViewAtHost() {
initTextView();
final int virtualViewId = mHelper.getVirtualViewAt(dp2Px(100), dp2Px(100));
assertEquals("Virtual view ID should be INVALID_ID",
ExploreByTouchHelper.INVALID_ID, virtualViewId);
@ -74,6 +63,7 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testGetVisibleVirtualViews() {
initTextView();
List<Integer> virtualViewIds = new ArrayList<>();
mHelper.getVisibleVirtualViews(virtualViewIds);
@ -83,6 +73,7 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testOnPopulateEventForVirtualView() {
initTextView();
AccessibilityEvent event = AccessibilityEvent.obtain();
mHelper.onPopulateEventForVirtualView(1, event);
@ -95,6 +86,7 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testOnPopulateEventForVirtualViewHost() {
initTextView();
AccessibilityEvent event = AccessibilityEvent.obtain();
mHelper.onPopulateEventForVirtualView(ExploreByTouchHelper.INVALID_ID, event);
@ -106,6 +98,7 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testOnPopulateNodeForVirtualView() {
initTextView();
AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
mHelper.onPopulateNodeForVirtualView(1, info);
@ -123,6 +116,7 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testNullLayout() {
initTextView();
// Setting the padding will cause the layout to be null-ed out.
mTextView.setPadding(1, 1, 1, 1);
@ -139,35 +133,114 @@ public class LinkAccessibilityHelperTest extends AndroidTestCase {
@SmallTest
public void testRtlLayout() {
// Redo setUp with a Hebrew (RTL) string.
mSpan = new LinkSpan("foobar");
SpannableStringBuilder ssb = new SpannableStringBuilder("מכונה בתרגום");
ssb.setSpan(mSpan, 1, 2, 0 /* flags */);
mTextView = new TextView(getContext());
mTextView.setText(ssb);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
mHelper = new TestLinkAccessibilityHelper(mTextView);
mTextView.measure(dp2Px(500), dp2Px(500));
mTextView.layout(dp2Px(0), dp2Px(0), dp2Px(500), dp2Px(500));
// End redo setup
ssb.setSpan(LINK_SPAN, 1, 2, 0 /* flags */);
initTextView(ssb);
AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
mHelper.onPopulateNodeForVirtualView(1, info);
assertEquals("LinkSpan description should be \"כ\"",
"כ", info.getContentDescription().toString());
assertTrue("LinkSpan should be focusable", info.isFocusable());
assertTrue("LinkSpan should be clickable", info.isClickable());
Rect bounds = new Rect();
info.getBoundsInParent(bounds);
assertEquals("LinkSpan bounds should be (70.5dp, 0dp, 78.5dp, 20.5dp)",
new Rect(dp2Px(70.5f), dp2Px(0f), dp2Px(78.5f), dp2Px(20.5f)), bounds);
assertEquals("LinkSpan bounds should be (481.5dp, 0dp, 489.5dp, 20.5dp)",
new Rect(dp2Px(481.5f), dp2Px(0f), dp2Px(489.5f), dp2Px(20.5f)), bounds);
info.recycle();
}
@SmallTest
public void testMultilineLink() {
SpannableStringBuilder ssb = new SpannableStringBuilder(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+ "Praesent accumsan efficitur eros eu porttitor.");
ssb.setSpan(LINK_SPAN, 51, 74, 0 /* flags */);
initTextView(ssb);
AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
mHelper.onPopulateNodeForVirtualView(51, info);
assertEquals("LinkSpan description should match the span",
"elit. Praesent accumsan", info.getContentDescription().toString());
Rect bounds = new Rect();
info.getBoundsInParent(bounds);
assertEquals("LinkSpan bounds should match first line of the span",
new Rect(dp2Px(343f), dp2Px(0f), dp2Px(500f), dp2Px(19.5f)), bounds);
info.recycle();
}
@SmallTest
public void testRtlMultilineLink() {
String iwLoremIpsum = "אחר על רביעי אקטואליה. לוח דת אחרות המקובל רומנית, מיזמים מועמדים "
+ "האנציקלופדיה בה צ'ט. מתן מה שנורו לערוך ייִדיש, בקר או החול אנתרופולוגיה, עוד "
+ "דפים המחשב מיזמים ב.";
SpannableStringBuilder ssb = new SpannableStringBuilder(iwLoremIpsum);
ssb.setSpan(LINK_SPAN, 50, 100, 0 /* flags */);
initTextView(ssb);
AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
mHelper.onPopulateNodeForVirtualView(50, info);
assertEquals("LinkSpan description should match the span",
iwLoremIpsum.substring(50, 100),
info.getContentDescription().toString());
Rect bounds = new Rect();
info.getBoundsInParent(bounds);
assertEquals("LinkSpan bounds should match the first line of the span",
new Rect(dp2Px(0f), dp2Px(0f), dp2Px(150f), dp2Px(19.5f)), bounds);
info.recycle();
}
@SmallTest
public void testBidiMultilineLink() {
String iwLoremIpsum = "אחר על רביעי אקטואליה. לוח דת אחרות המקובל רומנית, מיזמים מועמדים "
+ "האנציקלופדיה בה צ'ט. מתן מה שנורו לערוך ייִדיש, בקר או החול אנתרופולוגיה, עוד "
+ "דפים המחשב מיזמים ב.";
BidiFormatter formatter = BidiFormatter.getInstance(false /* rtlContext */);
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append("hello ").append(formatter.unicodeWrap(iwLoremIpsum)).append(" world");
ssb.setSpan(LINK_SPAN,
"hello ".length() + 2, // Add two for the characters added by BidiFormatter
"hello ".length() + 2 + iwLoremIpsum.length(),
0 /* flags */);
initTextView(ssb);
AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
mHelper.onPopulateNodeForVirtualView("hello ".length() + 2, info);
assertEquals("LinkSpan description should match the span",
iwLoremIpsum,
info.getContentDescription().toString());
Rect bounds = new Rect();
info.getBoundsInParent(bounds);
assertEquals("LinkSpan bounds should match the first line of the span",
new Rect(dp2Px(491.5f), dp2Px(0f), dp2Px(500f), dp2Px(19.5f)), bounds);
info.recycle();
}
private void initTextView() {
SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
ssb.setSpan(LINK_SPAN, 1, 2, 0 /* flags */);
initTextView(ssb);
}
private void initTextView(CharSequence text) {
mTextView = new TextView(getContext());
mTextView.setSingleLine(false);
mTextView.setText(text);
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
mHelper = new TestLinkAccessibilityHelper(mTextView);
int measureExactly500dp = View.MeasureSpec.makeMeasureSpec(dp2Px(500),
View.MeasureSpec.EXACTLY);
mTextView.measure(measureExactly500dp, measureExactly500dp);
mTextView.layout(dp2Px(0), dp2Px(0), dp2Px(500), dp2Px(500));
}
private int dp2Px(float dp) {
if (mDisplayMetrics == null) {
mDisplayMetrics = getContext().getResources().getDisplayMetrics();