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; 18 19import android.app.Instrumentation; 20import android.test.ActivityInstrumentationTestCase; 21import android.test.suitebuilder.annotation.LargeTest; 22import android.test.suitebuilder.annotation.MediumTest; 23import android.test.TouchUtils; 24import android.view.KeyEvent; 25import android.widget.AbsListView; 26import android.widget.GridView; 27 28import android.widget.gridview.GridScrollListener; 29 30public class GridScrollListenerTest extends ActivityInstrumentationTestCase<GridScrollListener> implements 31 AbsListView.OnScrollListener { 32 private GridScrollListener mActivity; 33 private GridView mGridView; 34 private int mFirstVisibleItem = -1; 35 private int mVisibleItemCount = -1; 36 private int mTotalItemCount = -1; 37 38 public GridScrollListenerTest() { 39 super("com.android.frameworks.coretests", GridScrollListener.class); 40 } 41 42 @Override 43 protected void setUp() throws Exception { 44 super.setUp(); 45 46 mActivity = getActivity(); 47 mGridView = getActivity().getGridView(); 48 mGridView.setOnScrollListener(this); 49 } 50 51 @MediumTest 52 public void testPreconditions() { 53 assertNotNull(mActivity); 54 assertNotNull(mGridView); 55 56 assertEquals(0, mFirstVisibleItem); 57 } 58 59 @LargeTest 60 public void testKeyScrolling() { 61 Instrumentation inst = getInstrumentation(); 62 63 int firstVisibleItem = mFirstVisibleItem; 64 for (int i = 0; i < mVisibleItemCount * 2; i++) { 65 inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN); 66 } 67 inst.waitForIdleSync(); 68 69 assertTrue("Arrow scroll did not happen", mFirstVisibleItem > firstVisibleItem); 70 71 firstVisibleItem = mFirstVisibleItem; 72 inst.sendCharacterSync(KeyEvent.KEYCODE_SPACE); 73 inst.waitForIdleSync(); 74 75 assertTrue("Page scroll did not happen", mFirstVisibleItem > firstVisibleItem); 76 77 firstVisibleItem = mFirstVisibleItem; 78 KeyEvent down = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 79 KeyEvent.KEYCODE_DPAD_DOWN, 0, KeyEvent.META_ALT_ON); 80 KeyEvent up = new KeyEvent(0, 0, KeyEvent.ACTION_UP, 81 KeyEvent.KEYCODE_DPAD_DOWN, 0, KeyEvent.META_ALT_ON); 82 inst.sendKeySync(down); 83 inst.sendKeySync(up); 84 inst.waitForIdleSync(); 85 86 assertTrue("Full scroll did not happen", mFirstVisibleItem > firstVisibleItem); 87 assertEquals("Full scroll did not happen", mTotalItemCount, 88 mFirstVisibleItem + mVisibleItemCount); 89 } 90 91 @LargeTest 92 public void testTouchScrolling() { 93 Instrumentation inst = getInstrumentation(); 94 95 int firstVisibleItem = mFirstVisibleItem; 96 TouchUtils.dragQuarterScreenUp(this); 97 TouchUtils.dragQuarterScreenUp(this); 98 assertTrue("Touch scroll did not happen", mFirstVisibleItem > firstVisibleItem); 99 } 100 101 102 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 103 mFirstVisibleItem = firstVisibleItem; 104 mVisibleItemCount = visibleItemCount; 105 mTotalItemCount = totalItemCount; 106 } 107 108 public void onScrollStateChanged(AbsListView view, int scrollState) { 109 } 110} 111