LinkAccessibilityHelperTest.java revision 04eecdd42f0bc9751825f9f21131a59852256278
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
14004eecdd42f0bc9751825f9f21131a59852256278Maurice Lam    @SmallTest
14104eecdd42f0bc9751825f9f21131a59852256278Maurice Lam    public void testRtlLayout() {
14204eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        // Redo setUp with a Hebrew (RTL) string.
14304eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mSpan = new LinkSpan("foobar");
14404eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        SpannableStringBuilder ssb = new SpannableStringBuilder("מכונה בתרגום");
14504eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        ssb.setSpan(mSpan, 1, 2, 0 /* flags */);
14604eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
14704eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mTextView = new TextView(getContext());
14804eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mTextView.setText(ssb);
14904eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
15004eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mHelper = new TestLinkAccessibilityHelper(mTextView);
15104eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
15204eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mTextView.measure(dp2Px(500), dp2Px(500));
15304eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mTextView.layout(dp2Px(0), dp2Px(0), dp2Px(500), dp2Px(500));
15404eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        // End redo setup
15504eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
15604eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
15704eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        mHelper.onPopulateNodeForVirtualView(1, info);
15804eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
15904eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        assertEquals("LinkSpan description should be \"כ\"",
16004eecdd42f0bc9751825f9f21131a59852256278Maurice Lam                "כ", info.getContentDescription().toString());
16104eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        assertTrue("LinkSpan should be focusable", info.isFocusable());
16204eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        assertTrue("LinkSpan should be clickable", info.isClickable());
16304eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        Rect bounds = new Rect();
16404eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        info.getBoundsInParent(bounds);
16504eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        assertEquals("LinkSpan bounds should be (70.5dp, 0dp, 78.5dp, 20.5dp)",
16604eecdd42f0bc9751825f9f21131a59852256278Maurice Lam                new Rect(dp2Px(70.5f), dp2Px(0f), dp2Px(78.5f), dp2Px(20.5f)), bounds);
16704eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
16804eecdd42f0bc9751825f9f21131a59852256278Maurice Lam        info.recycle();
16904eecdd42f0bc9751825f9f21131a59852256278Maurice Lam    }
17004eecdd42f0bc9751825f9f21131a59852256278Maurice Lam
171059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam    private int dp2Px(float dp) {
172059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam        if (mDisplayMetrics == null) {
173059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam            mDisplayMetrics = getContext().getResources().getDisplayMetrics();
174059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam        }
175059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mDisplayMetrics);
176059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam    }
177059321ff28f79a2cbe630b4d814a96ff9254643aMaurice Lam
178d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam    private static class TestLinkAccessibilityHelper extends LinkAccessibilityHelper {
179d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
180d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public TestLinkAccessibilityHelper(TextView view) {
181d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            super(view);
182d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
183d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
184d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        @Override
185d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public int getVirtualViewAt(float x, float y) {
186d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            return super.getVirtualViewAt(x, y);
187d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
188d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
189d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        @Override
190d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public void getVisibleVirtualViews(List<Integer> virtualViewIds) {
191d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            super.getVisibleVirtualViews(virtualViewIds);
192d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
193d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
194d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        @Override
195d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
196d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            super.onPopulateEventForVirtualView(virtualViewId, event);
197d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
198d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
199d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        @Override
200d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public void onPopulateNodeForVirtualView(int virtualViewId,
201d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam                AccessibilityNodeInfoCompat info) {
202d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            super.onPopulateNodeForVirtualView(virtualViewId, info);
203d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
204d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam
205d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        @Override
206d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        public boolean onPerformActionForVirtualView(int virtualViewId, int action,
207d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam                Bundle arguments) {
208d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam            return super.onPerformActionForVirtualView(virtualViewId, action, arguments);
209d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam        }
210d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam    }
211d832154e333a3a45b5faecd518b664ddd297183cMaurice Lam}
212