Home | History | Annotate | Download | only in touch
      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.widget.listview.touch;
     18 
     19 import android.test.ActivityInstrumentationTestCase;
     20 import android.test.suitebuilder.annotation.LargeTest;
     21 import android.test.suitebuilder.annotation.MediumTest;
     22 import android.test.TouchUtils;
     23 import android.test.suitebuilder.annotation.Suppress;
     24 import android.view.View;
     25 import android.widget.ListView;
     26 
     27 import android.widget.listview.ListSimple;
     28 
     29 /**
     30  * Tests setting the selection in touch mode
     31  */
     32 @Suppress // Flaky.
     33 public class ListSetSelectionTest extends ActivityInstrumentationTestCase<ListSimple> {
     34     private ListSimple mActivity;
     35     private ListView mListView;
     36 
     37     public ListSetSelectionTest() {
     38         super("com.android.frameworks.coretests", ListSimple.class);
     39     }
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         super.setUp();
     44 
     45         mActivity = getActivity();
     46         mListView = getActivity().getListView();
     47     }
     48 
     49     @MediumTest
     50     public void testPreconditions() {
     51         assertNotNull(mActivity);
     52         assertNotNull(mListView);
     53     }
     54 
     55     @LargeTest
     56     public void testSetSelection() {
     57         TouchUtils.dragQuarterScreenDown(this);
     58         TouchUtils.dragQuarterScreenUp(this);
     59 
     60         // Nothing should be selected
     61         assertEquals("Selection still available after touch", -1,
     62                 mListView.getSelectedItemPosition());
     63 
     64         final int targetPosition = mListView.getAdapter().getCount() / 2;
     65 
     66         mActivity.runOnUiThread(new Runnable() {
     67             public void run() {
     68                 mListView.setSelection(targetPosition);
     69             }
     70         });
     71         getInstrumentation().waitForIdleSync();
     72 
     73         boolean found = false;
     74         int childCount = mListView.getChildCount();
     75         for (int i=0; i<childCount; i++) {
     76             View child = mListView.getChildAt(i);
     77             if (child.getId() == targetPosition) {
     78                 found = true;
     79                 break;
     80             }
     81         }
     82         assertTrue("Selected item not visible in list", found);
     83     }
     84 
     85     @LargeTest
     86     public void testSetSelectionFromTop() {
     87         TouchUtils.dragQuarterScreenDown(this);
     88         TouchUtils.dragQuarterScreenUp(this);
     89 
     90         // Nothing should be selected
     91         assertEquals("Selection still available after touch", -1,
     92                 mListView.getSelectedItemPosition());
     93 
     94         final int targetPosition = mListView.getAdapter().getCount() / 2;
     95 
     96         mActivity.runOnUiThread(new Runnable() {
     97             public void run() {
     98                 mListView.setSelectionFromTop(targetPosition, 100);
     99             }
    100         });
    101         getInstrumentation().waitForIdleSync();
    102 
    103         View target = null;
    104         boolean found = false;
    105         int childCount = mListView.getChildCount();
    106         for (int i=0; i<childCount; i++) {
    107             View child = mListView.getChildAt(i);
    108             if (child.getId() == targetPosition) {
    109                 target = child;
    110                 found = true;
    111                 break;
    112             }
    113         }
    114         assertTrue("Selected item not visible in list", found);
    115 
    116         if (target != null) {
    117             assertEquals("Selection not at correct location", 100 + mListView.getPaddingTop(),
    118                     target.getTop());
    119         }
    120     }
    121 
    122     @LargeTest
    123     public void testSetSelection0() {
    124         TouchUtils.dragQuarterScreenDown(this);
    125         TouchUtils.dragQuarterScreenDown(this);
    126         TouchUtils.dragQuarterScreenDown(this);
    127 
    128         // Nothing should be selected
    129         assertEquals("Selection still available after touch", -1,
    130                 mListView.getSelectedItemPosition());
    131 
    132         mActivity.runOnUiThread(new Runnable() {
    133             public void run() {
    134                 mListView.setSelection(0);
    135             }
    136         });
    137         getInstrumentation().waitForIdleSync();
    138 
    139         boolean found = false;
    140         int childCount = mListView.getChildCount();
    141         for (int i=0; i<childCount; i++) {
    142             View child = mListView.getChildAt(i);
    143             if (child.getId() == 0 && i == 0) {
    144                 found = true;
    145                 break;
    146             }
    147         }
    148         assertTrue("Selected item not visible in list", found);
    149     }
    150 }
    151