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 static junit.framework.Assert.assertFalse; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertNotNull; 23import static org.junit.Assume.assumeTrue; 24 25import android.graphics.Rect; 26import android.os.Build; 27import android.os.Bundle; 28import android.support.coreui.test.R; 29import android.support.test.annotation.UiThreadTest; 30import android.support.test.filters.SmallTest; 31import android.support.v4.BaseInstrumentationTestCase; 32import android.support.v4.view.ViewCompat; 33import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; 34import android.view.View; 35 36import org.junit.Before; 37import org.junit.Test; 38 39import java.util.List; 40 41@SmallTest 42public class ExploreByTouchHelperTest extends BaseInstrumentationTestCase<ExploreByTouchHelperTestActivity> { 43 private View mHost; 44 45 public ExploreByTouchHelperTest() { 46 super(ExploreByTouchHelperTestActivity.class); 47 } 48 49 @Before 50 public void setUp() { 51 // Accessibility delegates are only supported on API 14+. 52 assumeTrue(Build.VERSION.SDK_INT >= 14); 53 mHost = mActivityTestRule.getActivity().findViewById(R.id.host_view); 54 } 55 56 @Test 57 @UiThreadTest 58 public void testBoundsInScreen() { 59 final ExploreByTouchHelper helper = new ParentBoundsHelper(mHost); 60 ViewCompat.setAccessibilityDelegate(mHost, helper); 61 62 final AccessibilityNodeInfoCompat node = 63 helper.getAccessibilityNodeProvider(mHost).createAccessibilityNodeInfo(1); 64 assertNotNull(node); 65 66 final Rect hostBounds = new Rect(); 67 mHost.getLocalVisibleRect(hostBounds); 68 assertFalse("Host has not been laid out", hostBounds.isEmpty()); 69 70 final Rect nodeBoundsInParent = new Rect(); 71 node.getBoundsInParent(nodeBoundsInParent); 72 assertEquals("Wrong bounds in parent", hostBounds, nodeBoundsInParent); 73 74 final Rect hostBoundsOnScreen = getBoundsOnScreen(mHost); 75 final Rect nodeBoundsInScreen = new Rect(); 76 node.getBoundsInScreen(nodeBoundsInScreen); 77 assertEquals("Wrong bounds in screen", hostBoundsOnScreen, nodeBoundsInScreen); 78 79 final int scrollX = 100; 80 final int scrollY = 50; 81 mHost.scrollTo(scrollX, scrollY); 82 83 // Generate a node for the new position. 84 final AccessibilityNodeInfoCompat scrolledNode = 85 helper.getAccessibilityNodeProvider(mHost).createAccessibilityNodeInfo(1); 86 assertNotNull(scrolledNode); 87 88 // Bounds in parent should not be affected by visibility. 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