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