1/* 2 * Copyright (C) 2008 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.listview; 18 19import android.app.Instrumentation; 20import android.test.ActivityInstrumentationTestCase; 21import android.test.FlakyTest; 22import android.test.suitebuilder.annotation.LargeTest; 23import android.test.suitebuilder.annotation.MediumTest; 24import android.view.KeyEvent; 25import android.widget.ListView; 26import android.test.TouchUtils; 27 28/** 29 * Tests restoring the scroll position in a list with a managed cursor. 30 */ 31public class ListManagedCursorTest extends ActivityInstrumentationTestCase<ListManagedCursor> { 32 private ListManagedCursor mActivity; 33 private ListView mListView; 34 35 public ListManagedCursorTest() { 36 super("com.android.frameworks.coretests", ListManagedCursor.class); 37 } 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 43 mActivity = getActivity(); 44 mListView = getActivity().getListView(); 45 } 46 47 @MediumTest 48 public void testPreconditions() { 49 assertNotNull(mActivity); 50 assertNotNull(mListView); 51 52 assertEquals(0, mListView.getFirstVisiblePosition()); 53 } 54 55 /** 56 * Scroll the list using arrows, launch new activity, hit back, make sure we're still scrolled. 57 */ 58 @LargeTest 59 public void testKeyScrolling() { 60 Instrumentation inst = getInstrumentation(); 61 62 int firstVisiblePosition = arrowScroll(inst); 63 64 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK); 65 inst.waitForIdleSync(); 66 67 assertTrue("List changed to touch mode", !mListView.isInTouchMode()); 68 assertTrue("List did not preserve scroll position", 69 firstVisiblePosition == mListView.getFirstVisiblePosition()); 70 } 71 72 /** 73 * Scroll the list using touch, launch new activity, hit back, make sure we're still scrolled. 74 */ 75 @LargeTest 76 public void testTouchScrolling() { 77 Instrumentation inst = getInstrumentation(); 78 79 int firstVisiblePosition = touchScroll(inst); 80 81 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK); 82 inst.waitForIdleSync(); 83 84 assertTrue("List not in touch mode", mListView.isInTouchMode()); 85 assertTrue("List did not preserve scroll position", 86 firstVisiblePosition == mListView.getFirstVisiblePosition()); 87 } 88 89 /** 90 * Scroll the list using arrows, launch new activity, change to touch mode, hit back, make sure 91 * we're still scrolled. 92 */ 93 @LargeTest 94 public void testKeyScrollingToTouchMode() { 95 Instrumentation inst = getInstrumentation(); 96 97 int firstVisiblePosition = arrowScroll(inst); 98 99 TouchUtils.dragQuarterScreenUp(this); 100 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK); 101 inst.waitForIdleSync(); 102 103 assertTrue("List did not change to touch mode", mListView.isInTouchMode()); 104 assertTrue("List did not preserve scroll position", 105 firstVisiblePosition == mListView.getFirstVisiblePosition()); 106 } 107 108 109 /** 110 * Scroll the list using touch, launch new activity, change to trackball mode, hit back, make 111 * sure we're still scrolled. 112 */ 113 @FlakyTest(tolerance=3) 114 @LargeTest 115 public void testTouchScrollingToTrackballMode() { 116 Instrumentation inst = getInstrumentation(); 117 118 int firstVisiblePosition = touchScroll(inst); 119 120 inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN); 121 inst.waitForIdleSync(); 122 inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN); 123 inst.waitForIdleSync(); 124 inst.sendCharacterSync(KeyEvent.KEYCODE_BACK); 125 inst.waitForIdleSync(); 126 assertTrue("List not in trackball mode", !mListView.isInTouchMode()); 127 assertTrue("List did not preserve scroll position", firstVisiblePosition == mListView 128 .getFirstVisiblePosition()); 129 } 130 131 public int arrowScroll(Instrumentation inst) { 132 int count = mListView.getChildCount(); 133 134 for (int i = 0; i < count * 2; i++) { 135 inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN); 136 } 137 inst.waitForIdleSync(); 138 139 int firstVisiblePosition = mListView.getFirstVisiblePosition(); 140 assertTrue("Arrow scroll did not happen", firstVisiblePosition > 0); 141 assertTrue("List still in touch mode", !mListView.isInTouchMode()); 142 143 inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_CENTER); 144 inst.waitForIdleSync(); 145 146 try { 147 Thread.sleep(3000); 148 } catch (InterruptedException e) { 149 e.printStackTrace(); 150 } 151 152 return firstVisiblePosition; 153 } 154 155 public int touchScroll(Instrumentation inst) { 156 TouchUtils.dragQuarterScreenUp(this); 157 inst.waitForIdleSync(); 158 TouchUtils.dragQuarterScreenUp(this); 159 inst.waitForIdleSync(); 160 TouchUtils.dragQuarterScreenUp(this); 161 inst.waitForIdleSync(); 162 TouchUtils.dragQuarterScreenUp(this); 163 inst.waitForIdleSync(); 164 165 int firstVisiblePosition = mListView.getFirstVisiblePosition(); 166 assertTrue("Touch scroll did not happen", firstVisiblePosition > 0); 167 assertTrue("List not in touch mode", mListView.isInTouchMode()); 168 169 TouchUtils.clickView(this, mListView.getChildAt(mListView.getChildCount() - 1)); 170 inst.waitForIdleSync(); 171 172 try { 173 Thread.sleep(3000); 174 } catch (InterruptedException e) { 175 e.printStackTrace(); 176 } 177 178 return firstVisiblePosition; 179 } 180} 181