LinkAccessibilityHelperTest.java revision cf90658b5c16018c9f3db7fd1d872025cff5d1dc
1d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam/* 2d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * Copyright (C) 2016 The Android Open Source Project 3d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * 4d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License"); 5d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * you may not use this file except in compliance with the License. 6d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * You may obtain a copy of the License at 7d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * 8d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * http://www.apache.org/licenses/LICENSE-2.0 9d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * 10d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * Unless required by applicable law or agreed to in writing, software 11d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS, 12d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * See the License for the specific language governing permissions and 14d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam * limitations under the License. 15d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam */ 16d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 17d832154e333a3a45b5faecd518b664ddd297183cMaurice Lampackage com.android.setupwizardlib.test; 18d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 19d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.graphics.Rect; 20d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.os.Bundle; 21d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; 22d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.support.v4.widget.ExploreByTouchHelper; 23d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.test.AndroidTestCase; 24d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.test.suitebuilder.annotation.SmallTest; 25d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.text.SpannableStringBuilder; 26059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lamimport android.util.DisplayMetrics; 27059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lamimport android.util.TypedValue; 28d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.view.accessibility.AccessibilityEvent; 29d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport android.widget.TextView; 30d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 31d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport com.android.setupwizardlib.span.LinkSpan; 32d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport com.android.setupwizardlib.util.LinkAccessibilityHelper; 33d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 34d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport java.util.ArrayList; 35d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport java.util.Collections; 36d832154e333a3a45b5faecd518b664ddd297183cMaurice Lamimport java.util.List; 37d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 38d832154e333a3a45b5faecd518b664ddd297183cMaurice Lampublic class LinkAccessibilityHelperTest extends AndroidTestCase { 39d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 40d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam private TextView mTextView; 41d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam private TestLinkAccessibilityHelper mHelper; 42d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam private LinkSpan mSpan; 43d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 44059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam private DisplayMetrics mDisplayMetrics; 45059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam 46d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 47d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam protected void setUp() throws Exception { 48d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam super.setUp(); 49d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mSpan = new LinkSpan("foobar"); 50d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); 51d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam ssb.setSpan(mSpan, 1, 2, 0 /* flags */); 52059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam 53d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mTextView = new TextView(getContext()); 54d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mTextView.setText(ssb); 55059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15); 56d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mHelper = new TestLinkAccessibilityHelper(mTextView); 57d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 58059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam mTextView.measure(dp2Px(500), dp2Px(500)); 59059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam mTextView.layout(dp2Px(0), dp2Px(0), dp2Px(500), dp2Px(500)); 60d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 61d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 62d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 63d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testGetVirtualViewAt() { 64059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam final int virtualViewId = mHelper.getVirtualViewAt(dp2Px(15), dp2Px(10)); 65d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("Virtual view ID should be 1", 1, virtualViewId); 66d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 67d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 68d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 69d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testGetVirtualViewAtHost() { 70059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam final int virtualViewId = mHelper.getVirtualViewAt(dp2Px(100), dp2Px(100)); 71d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("Virtual view ID should be INVALID_ID", 72d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam ExploreByTouchHelper.INVALID_ID, virtualViewId); 73d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 74d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 75d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 76d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testGetVisibleVirtualViews() { 77d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam List<Integer> virtualViewIds = new ArrayList<>(); 78d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mHelper.getVisibleVirtualViews(virtualViewIds); 79d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 80d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("VisibleVirtualViews should be [1]", 81d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam Collections.singletonList(1), virtualViewIds); 82d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 83d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 84d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 85d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testOnPopulateEventForVirtualView() { 86d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam AccessibilityEvent event = AccessibilityEvent.obtain(); 87d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mHelper.onPopulateEventForVirtualView(1, event); 88d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 89d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam // LinkSpan is set on substring(1, 2) of "Hello world" --> "e" 90d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("LinkSpan description should be \"e\"", 91d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam "e", event.getContentDescription().toString()); 92d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 93d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam event.recycle(); 94d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 95d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 96d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 97d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testOnPopulateEventForVirtualViewHost() { 98d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam AccessibilityEvent event = AccessibilityEvent.obtain(); 99d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mHelper.onPopulateEventForVirtualView(ExploreByTouchHelper.INVALID_ID, event); 100d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 101d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("Host view description should be \"Hello world\"", "Hello world", 102d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam event.getContentDescription().toString()); 103d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 104d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam event.recycle(); 105d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 106d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 107d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @SmallTest 108d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void testOnPopulateNodeForVirtualView() { 109d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(); 110d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam mHelper.onPopulateNodeForVirtualView(1, info); 111d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 112d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertEquals("LinkSpan description should be \"e\"", 113d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam "e", info.getContentDescription().toString()); 114d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertTrue("LinkSpan should be focusable", info.isFocusable()); 115d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam assertTrue("LinkSpan should be clickable", info.isClickable()); 116d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam Rect bounds = new Rect(); 117d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam info.getBoundsInParent(bounds); 118059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam assertEquals("LinkSpan bounds should be (10.5dp, 0dp, 18.5dp, 20.5dp)", 119059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam new Rect(dp2Px(10.5f), dp2Px(0f), dp2Px(18.5f), dp2Px(20.5f)), bounds); 120d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 121d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam info.recycle(); 122d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 123d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 124cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam @SmallTest 125cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam public void testNullLayout() { 126cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam // Setting the padding will cause the layout to be null-ed out. 127cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam mTextView.setPadding(1, 1, 1, 1); 128cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam 129cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain(); 130cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam mHelper.onPopulateNodeForVirtualView(0, info); 131cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam 132cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam Rect bounds = new Rect(); 133cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam info.getBoundsInParent(bounds); 134cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam assertEquals("LinkSpan bounds should be (0, 0, 1, 1)", 135cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam new Rect(0, 0, 1, 1), bounds); 136cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam 137cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam info.recycle(); 138cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam } 139cf90658b5c16018c9f3db7fd1d872025cff5d1dcMaurice Lam 140059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam private int dp2Px(float dp) { 141059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam if (mDisplayMetrics == null) { 142059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam mDisplayMetrics = getContext().getResources().getDisplayMetrics(); 143059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam } 144059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mDisplayMetrics); 145059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam } 146059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam 147d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam private static class TestLinkAccessibilityHelper extends LinkAccessibilityHelper { 148d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 149d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public TestLinkAccessibilityHelper(TextView view) { 150d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam super(view); 151d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 152d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 153d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 154d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public int getVirtualViewAt(float x, float y) { 155d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam return super.getVirtualViewAt(x, y); 156d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 157d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 158d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 159d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void getVisibleVirtualViews(List<Integer> virtualViewIds) { 160d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam super.getVisibleVirtualViews(virtualViewIds); 161d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 162d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 163d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 164d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) { 165d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam super.onPopulateEventForVirtualView(virtualViewId, event); 166d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 167d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 168d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 169d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public void onPopulateNodeForVirtualView(int virtualViewId, 170d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam AccessibilityNodeInfoCompat info) { 171d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam super.onPopulateNodeForVirtualView(virtualViewId, info); 172d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 173d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam 174d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam @Override 175d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam public boolean onPerformActionForVirtualView(int virtualViewId, int action, 176d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam Bundle arguments) { 177d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam return super.onPerformActionForVirtualView(virtualViewId, action, arguments); 178d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 179d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam } 180d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam} 181