Home | History | Annotate | Download | only in listview
      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 
     17 package android.widget.listview;
     18 
     19 import android.app.Instrumentation;
     20 import android.test.ActivityInstrumentationTestCase2;
     21 import android.test.suitebuilder.annotation.LargeTest;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 import android.view.KeyEvent;
     24 import android.widget.ListView;
     25 import android.test.TouchUtils;
     26 
     27 /**
     28  * Tests restoring the scroll position in a list with a managed cursor.
     29  */
     30 public class ListManagedCursorTest extends ActivityInstrumentationTestCase2<ListManagedCursor> {
     31     private ListManagedCursor mActivity;
     32     private ListView mListView;
     33 
     34     public ListManagedCursorTest() {
     35         super(ListManagedCursor.class);
     36     }
     37 
     38     @Override
     39     protected void setUp() throws Exception {
     40         super.setUp();
     41 
     42         mActivity = getActivity();
     43         mListView = getActivity().getListView();
     44     }
     45 
     46     @MediumTest
     47     public void testPreconditions() {
     48         assertNotNull(mActivity);
     49         assertNotNull(mListView);
     50 
     51         assertEquals(0, mListView.getFirstVisiblePosition());
     52     }
     53 
     54     /**
     55      * Scroll the list using arrows, launch new activity, hit back, make sure we're still scrolled.
     56      */
     57     @LargeTest
     58     public void testKeyScrolling() {
     59         Instrumentation inst = getInstrumentation();
     60 
     61         int firstVisiblePosition = arrowScroll(inst);
     62 
     63         inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
     64         inst.waitForIdleSync();
     65 
     66         assertTrue("List changed to touch mode", !mListView.isInTouchMode());
     67         assertTrue("List did not preserve scroll position",
     68                 firstVisiblePosition == mListView.getFirstVisiblePosition());
     69     }
     70 
     71     /**
     72      * Scroll the list using arrows, launch new activity, change to touch mode, hit back, make sure
     73      * we're still scrolled.
     74      */
     75     @LargeTest
     76     public void testKeyScrollingToTouchMode() {
     77         Instrumentation inst = getInstrumentation();
     78 
     79         int firstVisiblePosition = arrowScroll(inst);
     80 
     81         TouchUtils.dragQuarterScreenUp(this, getActivity());
     82         inst.sendCharacterSync(KeyEvent.KEYCODE_BACK);
     83         inst.waitForIdleSync();
     84 
     85         assertTrue("List did not change to touch mode", mListView.isInTouchMode());
     86         assertTrue("List did not preserve scroll position",
     87                 firstVisiblePosition == mListView.getFirstVisiblePosition());
     88     }
     89 
     90     public int arrowScroll(Instrumentation inst) {
     91         int count = mListView.getChildCount();
     92 
     93         for (int i = 0; i < count * 2; i++) {
     94             inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
     95         }
     96         inst.waitForIdleSync();
     97 
     98         int firstVisiblePosition = mListView.getFirstVisiblePosition();
     99         assertTrue("Arrow scroll did not happen", firstVisiblePosition > 0);
    100         assertTrue("List still in touch mode", !mListView.isInTouchMode());
    101 
    102         inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_CENTER);
    103         inst.waitForIdleSync();
    104 
    105         try {
    106             Thread.sleep(3000);
    107         } catch (InterruptedException e) {
    108             e.printStackTrace();
    109         }
    110 
    111         return firstVisiblePosition;
    112     }
    113 }
    114