1/* 2 * Copyright (C) 2007 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.widget.gridview.touch; 18 19import android.test.ActivityInstrumentationTestCase; 20import android.test.suitebuilder.annotation.LargeTest; 21import android.test.suitebuilder.annotation.MediumTest; 22import android.test.TouchUtils; 23import android.view.Gravity; 24import android.view.View; 25import android.view.ViewConfiguration; 26import android.widget.GridView; 27 28import android.widget.gridview.GridVerticalSpacing; 29 30public class GridTouchVerticalSpacingTest extends ActivityInstrumentationTestCase<GridVerticalSpacing> { 31 private GridVerticalSpacing mActivity; 32 private GridView mGridView; 33 34 public GridTouchVerticalSpacingTest() { 35 super("com.android.frameworks.coretests", GridVerticalSpacing.class); 36 } 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 42 mActivity = getActivity(); 43 mGridView = getActivity().getGridView(); 44 } 45 46 @MediumTest 47 public void testPreconditions() { 48 assertNotNull(mActivity); 49 assertNotNull(mGridView); 50 51 assertEquals(0, mGridView.getSelectedItemPosition()); 52 } 53 54 @MediumTest 55 public void testNoScroll() { 56 View firstChild = mGridView.getChildAt(0); 57 View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1); 58 59 int firstTop = firstChild.getTop(); 60 61 TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT, 62 0, -(ViewConfiguration.getTouchSlop())); 63 64 View newFirstChild = mGridView.getChildAt(0); 65 66 assertEquals("View scrolled too early", firstTop, newFirstChild.getTop()); 67 assertEquals("Wrong view in first position", 0, newFirstChild.getId()); 68 } 69 70 // TODO: needs to be adjusted to pass on non-HVGA displays 71 // @LargeTest 72 public void testShortScroll() { 73 View firstChild = mGridView.getChildAt(0); 74 View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1); 75 76 int firstTop = firstChild.getTop(); 77 78 TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT, 79 0, -(ViewConfiguration.getTouchSlop() + 1 + 10)); 80 81 View newFirstChild = mGridView.getChildAt(0); 82 83 assertEquals("View scrolled to wrong position", firstTop, newFirstChild.getTop() + 10); 84 assertEquals("Wrong view in first position", 0, newFirstChild.getId()); 85 } 86 87 // TODO: needs to be adjusted to pass on non-HVGA displays 88 // @LargeTest 89 public void testLongScroll() { 90 View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1); 91 92 int lastTop = lastChild.getTop(); 93 94 int distance = TouchUtils.dragViewToY(this, lastChild, Gravity.TOP | Gravity.LEFT, 95 mGridView.getTop()); 96 97 assertEquals("View scrolled to wrong position", 98 lastTop - (distance - ViewConfiguration.getTouchSlop() - 1), lastChild.getTop()); 99 } 100 101 @LargeTest 102 public void testManyScrolls() { 103 int originalCount = mGridView.getChildCount(); 104 105 View firstChild; 106 int firstId = Integer.MIN_VALUE; 107 int firstTop = Integer.MIN_VALUE; 108 int prevId; 109 int prevTop; 110 do { 111 prevId = firstId; 112 prevTop = firstTop; 113 TouchUtils.dragQuarterScreenUp(this); 114 assertTrue(String.format("Too many children created: %d expected no more than %d", 115 mGridView.getChildCount(), originalCount + 4), 116 mGridView.getChildCount() <= originalCount + 4); 117 firstChild = mGridView.getChildAt(0); 118 firstId = firstChild.getId(); 119 firstTop = firstChild.getTop(); 120 } while ((prevId != firstId) || (prevTop != firstTop)); 121 122 123 View lastChild = mGridView.getChildAt(mGridView.getChildCount() - 1); 124 assertEquals("Grid is not scrolled to the bottom", mGridView.getAdapter().getCount() - 1, 125 lastChild.getId()); 126 127 firstId = Integer.MIN_VALUE; 128 firstTop = Integer.MIN_VALUE; 129 do { 130 prevId = firstId; 131 prevTop = firstTop; 132 TouchUtils.dragQuarterScreenDown(this); 133 assertTrue(String.format("Too many children created: %d expected no more than %d", 134 mGridView.getChildCount(), originalCount + 4), 135 mGridView.getChildCount() <= originalCount + 4); 136 firstChild = mGridView.getChildAt(0); 137 firstId = firstChild.getId(); 138 firstTop = firstChild.getTop(); 139 } while ((prevId != firstId) || (prevTop != firstTop)); 140 141 firstChild = mGridView.getChildAt(0); 142 assertEquals("Grid is not scrolled to the top", 0, firstChild.getId()); 143 } 144} 145