FileDocCategorySizeDatePackage
ListTouchManyTest.javaAPI DocAndroid 5.1 API7213Thu Mar 12 22:22:12 GMT 2015android.widget.listview.touch

ListTouchManyTest

public class ListTouchManyTest extends android.test.ActivityInstrumentationTestCase
Touch tests for a list where all of the items do not fit on the screen.

Fields Summary
private android.widget.listview.ListTopGravityMany
mActivity
private android.widget.ListView
mListView
Constructors Summary
public ListTouchManyTest()

        super("com.android.frameworks.coretests", ListTopGravityMany.class);
    
Methods Summary
protected voidsetUp()

        super.setUp();

        mActivity = getActivity();
        mListView = getActivity().getListView();
    
public voidtestLongPress()

        int i;
        int count = mListView.getChildCount();
        mActivity.enableLongPress();
        mActivity.setClickedPosition(-1);
        mActivity.setLongClickedPosition(-1);
        
        for (i = 0; i < count; i++) {
            View child = mListView.getChildAt(i);
            if ((child.getTop() >= mListView.getListPaddingTop())
                    && (child.getBottom() <= 
                        mListView.getHeight() - mListView.getListPaddingBottom())) {
                TouchUtils.longClickView(this, child);
                assertEquals("Incorrect view position reported being long clicked", i, 
                        mActivity.getLongClickedPosition());
                assertEquals("View falsely reported being clicked", -1, 
                        mActivity.getClickedPosition());
            }
        }
    
public voidtestLongScroll()

        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
        
        int lastTop = lastChild.getTop();
        
        int distance = TouchUtils.dragViewToY(this, lastChild, 
                Gravity.TOP | Gravity.LEFT, mListView.getTop());
        
        assertEquals("View scrolled to wrong position", 
                lastTop - (distance - ViewConfiguration.getTouchSlop() - 1), lastChild.getTop());
    
public voidtestNoScroll()

        View firstChild = mListView.getChildAt(0);
        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
        
        int firstTop = firstChild.getTop();
        
        TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT, 
                0, -(ViewConfiguration.getTouchSlop()));
        
        View newFirstChild = mListView.getChildAt(0);
        
        assertEquals("View scrolled too early", firstTop, newFirstChild.getTop());
        assertEquals("Wrong view in first position", 0, newFirstChild.getId());
    
public voidtestPreconditions()

        assertNotNull(mActivity);
        assertNotNull(mListView);
        
        // First item should be selected
        assertEquals(0, mListView.getSelectedItemPosition());
    
public voidtestPress()

        int i;
        int count = mListView.getChildCount();
        mActivity.setClickedPosition(-1);
        mActivity.setLongClickedPosition(-1);
        
        for (i = 0; i < count; i++) {
            View child = mListView.getChildAt(i);
            if ((child.getTop() >= mListView.getListPaddingTop())
                    && (child.getBottom() <= 
                        mListView.getHeight() - mListView.getListPaddingBottom())) {
                TouchUtils.clickView(this, child);
                
                assertEquals("Incorrect view position reported being clicked", i, 
                        mActivity.getClickedPosition());
                assertEquals("View falsely reported being long clicked", -1, 
                        mActivity.getLongClickedPosition());
                try {
                    Thread.sleep((long)(ViewConfiguration.getLongPressTimeout() * 1.25f));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
public voidtestPullDown()

        TouchUtils.scrollToTop(this, mListView);
        
        // Nothing should be selected
        assertEquals("Selection still available after touch", -1, 
                mListView.getSelectedItemPosition());
        
        View firstChild = mListView.getChildAt(0);
        
        assertEquals("Item zero not the first child in the list", 0, firstChild.getId());
        
        assertEquals("Item zero not at the top of the list", mListView.getListPaddingTop(),
                firstChild.getTop());
    
public voidtestPushUp()

        int originalCount = mListView.getChildCount();
        
        TouchUtils.scrollToBottom(this, mListView);

        // Nothing should be selected
        assertEquals("Selection still available after touch", -1, 
                mListView.getSelectedItemPosition());

        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);

        assertEquals("List is not scrolled to the bottom", mListView.getAdapter().getCount() - 1,
                lastChild.getId());

        assertEquals("Last item is not touching the bottom edge",  
                mListView.getHeight() - mListView.getListPaddingBottom(), lastChild.getBottom());
        
        assertTrue(String.format("Too many children created: %d expected no more than %d", 
                mListView.getChildCount(), originalCount + 1), 
                mListView.getChildCount() <= originalCount + 1);
    
public voidtestShortScroll()

        View firstChild = mListView.getChildAt(0);
        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
        
        int firstTop = firstChild.getTop();
        
        TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT,
                0, -(ViewConfiguration.getTouchSlop() + 1 + 10));
        
        View newFirstChild = mListView.getChildAt(0);
        
        assertEquals("View scrolled too early", firstTop, newFirstChild.getTop() + 10);
        assertEquals("Wrong view in first position", 0, newFirstChild.getId());