ListTouchBottomGravityManyTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
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
17package android.widget.listview.touch;
18
19import android.test.ActivityInstrumentationTestCase;
20import android.test.suitebuilder.annotation.LargeTest;
21import android.test.suitebuilder.annotation.MediumTest;
22import android.test.TouchUtils;
23import android.view.Gravity;
24import android.view.View;
25import android.view.ViewConfiguration;
26import android.widget.ListView;
27
28import android.widget.listview.ListBottomGravityMany;
29
30/**
31 * Touch tests for a list where all of the items do not fit on the screen, and the list
32 * stacks from the bottom.
33 */
34public class ListTouchBottomGravityManyTest extends ActivityInstrumentationTestCase<ListBottomGravityMany> {
35    private ListBottomGravityMany mActivity;
36    private ListView mListView;
37
38    public ListTouchBottomGravityManyTest() {
39        super("com.android.frameworks.coretests", ListBottomGravityMany.class);
40    }
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45
46        mActivity = getActivity();
47        mListView = getActivity().getListView();
48    }
49
50    @MediumTest
51    public void testPreconditions() {
52        assertNotNull(mActivity);
53        assertNotNull(mListView);
54
55        // Last item should be selected
56        assertEquals(mListView.getAdapter().getCount() - 1, mListView.getSelectedItemPosition());
57    }
58
59    @LargeTest
60    public void testPullDown() {
61        int originalCount = mListView.getChildCount();
62
63        TouchUtils.scrollToTop(this, mListView);
64
65        // Nothing should be selected
66        assertEquals("Selection still available after touch", -1,
67                mListView.getSelectedItemPosition());
68
69        View firstChild = mListView.getChildAt(0);
70
71        assertEquals("Item zero not the first child in the list", 0, firstChild.getId());
72
73        assertEquals("Item zero not at the top of the list", mListView.getListPaddingTop(),
74                firstChild.getTop());
75
76        assertTrue(String.format("Too many children created: %d expected no more than %d",
77                mListView.getChildCount(), originalCount + 1),
78                mListView.getChildCount() <= originalCount + 1);
79    }
80
81    @MediumTest
82    public void testPushUp() {
83        TouchUtils.scrollToBottom(this, mListView);
84
85        // Nothing should be selected
86        assertEquals("Selection still available after touch", -1,
87                mListView.getSelectedItemPosition());
88
89        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
90
91        assertEquals("List is not scrolled to the bottom", mListView.getAdapter().getCount() - 1,
92                lastChild.getId());
93
94        assertEquals("Last item is not touching the bottom edge",
95                mListView.getHeight() - mListView.getListPaddingBottom(), lastChild.getBottom());
96    }
97
98    @MediumTest
99    public void testNoScroll() {
100        View firstChild = mListView.getChildAt(0);
101        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
102
103        int lastTop = lastChild.getTop();
104
105        TouchUtils.dragViewBy(this, firstChild, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
106                0, ViewConfiguration.getTouchSlop());
107
108        View newLastChild = mListView.getChildAt(mListView.getChildCount() - 1);
109
110        assertEquals("View scrolled too early", lastTop, newLastChild.getTop());
111        assertEquals("Wrong view in last position", mListView.getAdapter().getCount() - 1,
112                newLastChild.getId());
113    }
114
115    // TODO: needs to be adjusted to pass on non-HVGA displays
116    // @LargeTest
117    public void testShortScroll() {
118        View firstChild = mListView.getChildAt(0);
119        if (firstChild.getTop() < this.mListView.getListPaddingTop()) {
120            firstChild = mListView.getChildAt(1);
121        }
122
123        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
124
125        int lastTop = lastChild.getTop();
126
127        TouchUtils.dragViewBy(this, firstChild, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,
128                0, ViewConfiguration.getTouchSlop() + 1 + 10);
129
130        View newLastChild = mListView.getChildAt(mListView.getChildCount() - 1);
131
132        assertEquals("View scrolled to wrong position", lastTop, newLastChild.getTop() - 10);
133        assertEquals("Wrong view in last position", mListView.getAdapter().getCount() - 1,
134                newLastChild.getId());
135    }
136
137    // TODO: needs to be adjusted to pass on non-HVGA displays
138    // @LargeTest
139    public void testLongScroll() {
140        View firstChild = mListView.getChildAt(0);
141        if (firstChild.getTop() < mListView.getListPaddingTop()) {
142            firstChild = mListView.getChildAt(1);
143        }
144
145        int firstTop = firstChild.getTop();
146
147        int distance = TouchUtils.dragViewBy(this, firstChild,
148                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
149                (int)(mActivity.getWindowManager().getDefaultDisplay().getHeight() * 0.75f));
150
151        assertEquals("View scrolled to wrong position", firstTop
152                + (distance - ViewConfiguration.getTouchSlop() - 1), firstChild.getTop());
153    }
154
155}
156