Home | History | Annotate | Download | only in util
      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 
     17 package android.util;
     18 
     19 import android.app.Instrumentation;
     20 import android.view.KeyEvent;
     21 import android.widget.ListView;
     22 
     23 
     24 /**
     25  * Various useful stuff for instrumentation testing listview.
     26  */
     27 public class ListUtil {
     28 
     29 
     30     private final ListView mListView;
     31     private final Instrumentation mInstrumentation;
     32 
     33     /**
     34      * @param listView The listview to act on
     35      * @param instrumentation The instrumentation to use.
     36      */
     37     public ListUtil(ListView listView, Instrumentation instrumentation) {
     38         mListView = listView;
     39         mInstrumentation = instrumentation;
     40     }
     41 
     42     /**
     43      * Set the selected position of the list view.
     44      * @param pos The desired position.
     45      */
     46     public final void setSelectedPosition(final int pos) {
     47         mListView.post(new Runnable() {
     48             public void run() {
     49                 mListView.setSelection(pos);
     50             }
     51         });
     52         mInstrumentation.waitForIdleSync();
     53     }
     54 
     55     /**
     56      * Get the top of the list.
     57      */
     58     public final int getListTop() {
     59         return mListView.getListPaddingTop();
     60     }
     61 
     62     /**
     63      * Get the bottom of the list.
     64      */
     65     public final int getListBottom() {
     66         return mListView.getHeight() - mListView.getListPaddingBottom();
     67     }
     68 
     69     /**
     70      * Arrow (up or down as appropriate) to the desired position in the list.
     71      * @param desiredPos The desired position
     72      * @throws IllegalStateException if the position can't be reached within 20 presses.
     73      */
     74     public final void arrowScrollToSelectedPosition(int desiredPos) {
     75         if (desiredPos > mListView.getSelectedItemPosition()) {
     76             arrowDownToSelectedPosition(desiredPos);
     77         } else {
     78             arrowUpToSelectedPosition(desiredPos);
     79         }
     80     }
     81 
     82     private void arrowDownToSelectedPosition(int position) {
     83         int maxDowns = 20;
     84         while(mListView.getSelectedItemPosition() < position && --maxDowns > 0) {
     85             mInstrumentation.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
     86         }
     87         if (position != mListView.getSelectedItemPosition()) {
     88             throw new IllegalStateException("couldn't get to item after 20 downs");
     89         }
     90 
     91     }
     92 
     93     private void arrowUpToSelectedPosition(int position) {
     94         int maxUps = 20;
     95         while(mListView.getSelectedItemPosition() > position && --maxUps > 0) {
     96             mInstrumentation.sendCharacterSync(KeyEvent.KEYCODE_DPAD_UP);
     97         }
     98         if (position != mListView.getSelectedItemPosition()) {
     99             throw new IllegalStateException("couldn't get to item after 20 ups");
    100         }
    101     }
    102 
    103 }
    104