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