ExploreByTouchHelperTest.java revision ff22d81f6561f6cdd2a91eb63238c41079927a22
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 android.support.v4.widget; 18 19import android.graphics.Rect; 20import android.os.Build; 21import android.os.Bundle; 22import android.support.coreui.test.R; 23import android.support.test.annotation.UiThreadTest; 24import android.support.v4.BaseInstrumentationTestCase; 25import android.support.v4.view.ViewCompat; 26import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; 27import android.test.suitebuilder.annotation.SmallTest; 28import android.view.View; 29import org.junit.Before; 30import org.junit.Test; 31 32import java.util.List; 33 34import static junit.framework.Assert.assertFalse; 35import static org.junit.Assert.assertEquals; 36import static org.junit.Assert.assertNotNull; 37import static org.junit.Assume.assumeTrue; 38 39@SmallTest 40public class ExploreByTouchHelperTest extends BaseInstrumentationTestCase<ExploreByTouchHelperTestActivity> { 41 private View mHost; 42 43 public ExploreByTouchHelperTest() { 44 super(ExploreByTouchHelperTestActivity.class); 45 } 46 47 @Before 48 public void setUp() { 49 // Accessibility delegates are only supported on API 14+. 50 assumeTrue(Build.VERSION.SDK_INT >= 14); 51 mHost = mActivityTestRule.getActivity().findViewById(R.id.host_view); 52 } 53 54 @Test 55 @UiThreadTest 56 public void testBoundsInScreen() { 57 final ExploreByTouchHelper helper = new ParentBoundsHelper(mHost); 58 ViewCompat.setAccessibilityDelegate(mHost, helper); 59 60 final AccessibilityNodeInfoCompat node = 61 helper.getAccessibilityNodeProvider(mHost).createAccessibilityNodeInfo(1); 62 assertNotNull(node); 63 64 final Rect hostBounds = new Rect(); 65 mHost.getLocalVisibleRect(hostBounds); 66 assertFalse("Host has not been laid out", hostBounds.isEmpty()); 67 68 final Rect nodeBoundsInParent = new Rect(); 69 node.getBoundsInParent(nodeBoundsInParent); 70 assertEquals("Wrong bounds in parent", hostBounds, nodeBoundsInParent); 71 72 final Rect hostBoundsOnScreen = getBoundsOnScreen(mHost); 73 final Rect nodeBoundsInScreen = new Rect(); 74 node.getBoundsInScreen(nodeBoundsInScreen); 75 assertEquals("Wrong bounds in screen", hostBoundsOnScreen, nodeBoundsInScreen); 76 77 final int scrollX = 100; 78 final int scrollY = 50; 79 mHost.scrollTo(scrollX, scrollY); 80 81 // Generate a node for the new position. 82 final AccessibilityNodeInfoCompat scrolledNode = 83 helper.getAccessibilityNodeProvider(mHost).createAccessibilityNodeInfo(1); 84 assertNotNull(scrolledNode); 85 86 mHost.getLocalVisibleRect(hostBounds); 87 hostBounds.intersect(nodeBoundsInParent); 88 final Rect scrolledNodeBoundsInParent = new Rect(); 89 scrolledNode.getBoundsInParent(scrolledNodeBoundsInParent); 90 assertEquals("Wrong bounds in parent after scrolling", 91 hostBounds, scrolledNodeBoundsInParent); 92 93 final Rect expectedBoundsInScreen = new Rect(hostBoundsOnScreen); 94 expectedBoundsInScreen.offset(-scrollX, -scrollY); 95 expectedBoundsInScreen.intersect(hostBoundsOnScreen); 96 scrolledNode.getBoundsInScreen(nodeBoundsInScreen); 97 assertEquals("Wrong bounds in screen after scrolling", 98 expectedBoundsInScreen, nodeBoundsInScreen); 99 100 ViewCompat.setAccessibilityDelegate(mHost, null); 101 } 102 103 private static Rect getBoundsOnScreen(View v) { 104 final int[] tempLocation = new int[2]; 105 final Rect hostBoundsOnScreen = new Rect(0, 0, v.getWidth(), v.getHeight()); 106 v.getLocationOnScreen(tempLocation); 107 hostBoundsOnScreen.offset(tempLocation[0], tempLocation[1]); 108 return hostBoundsOnScreen; 109 } 110 111 /** 112 * An extension of ExploreByTouchHelper that contains a single virtual view 113 * whose bounds match the host view. 114 */ 115 private static class ParentBoundsHelper extends ExploreByTouchHelper { 116 private final View mHost; 117 118 public ParentBoundsHelper(View host) { 119 super(host); 120 121 mHost = host; 122 } 123 124 @Override 125 protected int getVirtualViewAt(float x, float y) { 126 return 1; 127 } 128 129 @Override 130 protected void getVisibleVirtualViews(List<Integer> virtualViewIds) { 131 virtualViewIds.add(1); 132 } 133 134 @Override 135 protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) { 136 if (virtualViewId == 1) { 137 node.setContentDescription("test"); 138 139 final Rect hostBounds = new Rect(0, 0, mHost.getWidth(), mHost.getHeight()); 140 node.setBoundsInParent(hostBounds); 141 } 142 } 143 144 @Override 145 protected boolean onPerformActionForVirtualView(int virtualViewId, int action, Bundle arguments) { 146 return false; 147 } 148 } 149} 150