Home | History | Annotate | Download | only in arrowscroll
      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.arrowscroll;
     18 
     19 import android.test.ActivityInstrumentationTestCase;
     20 import android.test.suitebuilder.annotation.LargeTest;
     21 import android.test.suitebuilder.annotation.MediumTest;
     22 import android.test.suitebuilder.annotation.Suppress;
     23 import android.view.KeyEvent;
     24 import android.view.View;
     25 import android.widget.ListView;
     26 import android.widget.listview.ListOfThinItems;
     27 
     28 public class ListOfThinItemsTest extends ActivityInstrumentationTestCase<ListOfThinItems> {
     29     private ListView mListView;
     30 
     31     public ListOfThinItemsTest() {
     32         super("com.android.frameworks.coretests", ListOfThinItems.class);
     33     }
     34 
     35     @Override
     36     protected void setUp() throws Exception{
     37         super.setUp();
     38         mListView = getActivity().getListView();
     39     }
     40 
     41     @MediumTest
     42     @Suppress // Failing.
     43     public void testPreconditions() {
     44         assertNotNull(mListView);
     45         assertTrue("need item height less than fading edge length",
     46                 mListView.getSelectedView().getHeight() < mListView.getVerticalFadingEdgeLength());
     47         assertTrue("need items off screen",
     48                 mListView.getChildCount() < mListView.getAdapter().getCount());
     49     }
     50 
     51     @LargeTest
     52     public void testScrollToBottom() {
     53         final int numItems = mListView.getAdapter().getCount();
     54         final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
     55         for (int i = 0; i < numItems; i++) {
     56             assertEquals("wrong selection at position " + i,
     57                     i, mListView.getSelectedItemPosition());
     58             final int bottomFadingEdge = listBottom - mListView.getVerticalFadingEdgeLength();
     59             final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
     60             final int lastVisiblePosition = lastChild.getId();
     61 
     62 
     63             int bottomThreshold = (lastVisiblePosition < mListView.getAdapter().getCount() - 1) ?
     64                     bottomFadingEdge : listBottom;
     65 
     66             String prefix = "after " + i + " down presses, ";
     67 
     68             assertTrue(prefix + "selected item is below bottom threshold (fading edge or bottom as " +
     69                     "appropriate)",
     70                     mListView.getSelectedView().getBottom() <= bottomThreshold);
     71             assertTrue(prefix + "first item in list must be at very top or just above",
     72                     mListView.getChildAt(0).getTop() <= 0);
     73             assertTrue(prefix + "last item in list should be at very bottom or just below",
     74                     lastChild.getBottom() >= listBottom);
     75             sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
     76         }
     77     }
     78 
     79     @LargeTest
     80     public void testScrollToTop() {
     81         final int numItems = mListView.getAdapter().getCount();
     82 
     83         for (int i = 0; i < numItems - 1; i++) {
     84             sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
     85         }
     86         assertEquals("should have moved to last position",
     87                 numItems - 1, mListView.getSelectedItemPosition());
     88 
     89         int listTop = mListView.getListPaddingTop();
     90         final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
     91 
     92         for (int i = 0; i < numItems; i++) {
     93             int expectedPostion = numItems - (i + 1);
     94             assertEquals("wrong selection at position " + expectedPostion,
     95                     expectedPostion, mListView.getSelectedItemPosition());
     96             final int topFadingEdge = listTop + mListView.getVerticalFadingEdgeLength();
     97             final View firstChild = mListView.getChildAt(0);
     98             final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
     99             final int firstVisiblePosition = firstChild.getId();
    100 
    101 
    102             int topThreshold = (firstVisiblePosition > 0) ?
    103                     topFadingEdge : listTop;
    104 
    105             String prefix = "after " + i + " up presses, ";
    106 
    107             assertTrue(prefix + "selected item is above top threshold (fading edge or top as " +
    108                     "appropriate)",
    109                     mListView.getSelectedView().getTop() >= topThreshold);
    110             assertTrue(prefix + "first item in list must be at very top or just above",
    111                     firstChild.getTop() <= 0);
    112             assertTrue(prefix + "last item in list should be at very bottom or just below",
    113                     lastChild.getBottom() >= listBottom);
    114             sendKeys(KeyEvent.KEYCODE_DPAD_UP);
    115         }
    116     }
    117 }
    118