LinkAccessibilityHelperTest.java revision d832154e333a3a45b5faecd518b664ddd297183c
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.setupwizardlib.test;
18
19import android.graphics.Rect;
20import android.os.Bundle;
21import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
22import android.support.v4.widget.ExploreByTouchHelper;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.text.SpannableStringBuilder;
26import android.view.accessibility.AccessibilityEvent;
27import android.widget.TextView;
28
29import com.android.setupwizardlib.span.LinkSpan;
30import com.android.setupwizardlib.util.LinkAccessibilityHelper;
31
32import java.util.ArrayList;
33import java.util.Collections;
34import java.util.List;
35
36public class LinkAccessibilityHelperTest extends AndroidTestCase {
37
38    private TextView mTextView;
39    private TestLinkAccessibilityHelper mHelper;
40    private LinkSpan mSpan;
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45        mSpan = new LinkSpan("foobar");
46        SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world");
47        ssb.setSpan(mSpan, 1, 2, 0 /* flags */);
48        mTextView = new TextView(getContext());
49        mTextView.setText(ssb);
50        mHelper = new TestLinkAccessibilityHelper(mTextView);
51
52        mTextView.measure(500, 500);
53        mTextView.layout(0, 0, 500, 500);
54    }
55
56    @SmallTest
57    public void testGetVirtualViewAt() {
58        final int virtualViewId = mHelper.getVirtualViewAt(15, 10);
59        assertEquals("Virtual view ID should be 1", 1, virtualViewId);
60    }
61
62    @SmallTest
63    public void testGetVirtualViewAtHost() {
64        final int virtualViewId = mHelper.getVirtualViewAt(100, 100);
65        assertEquals("Virtual view ID should be INVALID_ID",
66                ExploreByTouchHelper.INVALID_ID, virtualViewId);
67    }
68
69    @SmallTest
70    public void testGetVisibleVirtualViews() {
71        List<Integer> virtualViewIds = new ArrayList<>();
72        mHelper.getVisibleVirtualViews(virtualViewIds);
73
74        assertEquals("VisibleVirtualViews should be [1]",
75                Collections.singletonList(1), virtualViewIds);
76    }
77
78    @SmallTest
79    public void testOnPopulateEventForVirtualView() {
80        AccessibilityEvent event = AccessibilityEvent.obtain();
81        mHelper.onPopulateEventForVirtualView(1, event);
82
83        // LinkSpan is set on substring(1, 2) of "Hello world" --> "e"
84        assertEquals("LinkSpan description should be \"e\"",
85                "e", event.getContentDescription().toString());
86
87        event.recycle();
88    }
89
90    @SmallTest
91    public void testOnPopulateEventForVirtualViewHost() {
92        AccessibilityEvent event = AccessibilityEvent.obtain();
93        mHelper.onPopulateEventForVirtualView(ExploreByTouchHelper.INVALID_ID, event);
94
95        assertEquals("Host view description should be \"Hello world\"", "Hello world",
96                event.getContentDescription().toString());
97
98        event.recycle();
99    }
100
101    @SmallTest
102    public void testOnPopulateNodeForVirtualView() {
103        AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
104        mHelper.onPopulateNodeForVirtualView(1, info);
105
106        assertEquals("LinkSpan description should be \"e\"",
107                "e", info.getContentDescription().toString());
108        assertTrue("LinkSpan should be focusable", info.isFocusable());
109        assertTrue("LinkSpan should be clickable", info.isClickable());
110        Rect bounds = new Rect();
111        info.getBoundsInParent(bounds);
112        assertEquals("LinkSpan bounds should be (20, 0, 35, 38)", new Rect(20, 0, 35, 38), bounds);
113
114        info.recycle();
115    }
116
117    private static class TestLinkAccessibilityHelper extends LinkAccessibilityHelper {
118
119        public TestLinkAccessibilityHelper(TextView view) {
120            super(view);
121        }
122
123        @Override
124        public int getVirtualViewAt(float x, float y) {
125            return super.getVirtualViewAt(x, y);
126        }
127
128        @Override
129        public void getVisibleVirtualViews(List<Integer> virtualViewIds) {
130            super.getVisibleVirtualViews(virtualViewIds);
131        }
132
133        @Override
134        public void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
135            super.onPopulateEventForVirtualView(virtualViewId, event);
136        }
137
138        @Override
139        public void onPopulateNodeForVirtualView(int virtualViewId,
140                AccessibilityNodeInfoCompat info) {
141            super.onPopulateNodeForVirtualView(virtualViewId, info);
142        }
143
144        @Override
145        public boolean onPerformActionForVirtualView(int virtualViewId, int action,
146                Bundle arguments) {
147            return super.onPerformActionForVirtualView(virtualViewId, action, arguments);
148        }
149    }
150}
151