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.ListTopGravityMany;
29
30/**
31 * Touch tests for a list where all of the items do not fit on the screen.
32 */
33public class ListTouchManyTest extends ActivityInstrumentationTestCase<ListTopGravityMany> {
34    private ListTopGravityMany mActivity;
35    private ListView mListView;
36
37    public ListTouchManyTest() {
38        super("com.android.frameworks.coretests", ListTopGravityMany.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        // First item should be selected
55        assertEquals(0, mListView.getSelectedItemPosition());
56    }
57
58    @MediumTest
59    public void testPullDown() {
60        TouchUtils.scrollToTop(this, mListView);
61
62        // Nothing should be selected
63        assertEquals("Selection still available after touch", -1,
64                mListView.getSelectedItemPosition());
65
66        View firstChild = mListView.getChildAt(0);
67
68        assertEquals("Item zero not the first child in the list", 0, firstChild.getId());
69
70        assertEquals("Item zero not at the top of the list", mListView.getListPaddingTop(),
71                firstChild.getTop());
72    }
73
74    @LargeTest
75    public void testPushUp() {
76        int originalCount = mListView.getChildCount();
77
78        TouchUtils.scrollToBottom(this, mListView);
79
80        // Nothing should be selected
81        assertEquals("Selection still available after touch", -1,
82                mListView.getSelectedItemPosition());
83
84        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
85
86        assertEquals("List is not scrolled to the bottom", mListView.getAdapter().getCount() - 1,
87                lastChild.getId());
88
89        assertEquals("Last item is not touching the bottom edge",
90                mListView.getHeight() - mListView.getListPaddingBottom(), lastChild.getBottom());
91
92        assertTrue(String.format("Too many children created: %d expected no more than %d",
93                mListView.getChildCount(), originalCount + 1),
94                mListView.getChildCount() <= originalCount + 1);
95    }
96
97    @LargeTest
98    public void testPress() {
99        int i;
100        int count = mListView.getChildCount();
101        mActivity.setClickedPosition(-1);
102        mActivity.setLongClickedPosition(-1);
103
104        for (i = 0; i < count; i++) {
105            View child = mListView.getChildAt(i);
106            if ((child.getTop() >= mListView.getListPaddingTop())
107                    && (child.getBottom() <=
108                        mListView.getHeight() - mListView.getListPaddingBottom())) {
109                TouchUtils.clickView(this, child);
110
111                assertEquals("Incorrect view position reported being clicked", i,
112                        mActivity.getClickedPosition());
113                assertEquals("View falsely reported being long clicked", -1,
114                        mActivity.getLongClickedPosition());
115                try {
116                    Thread.sleep((long)(ViewConfiguration.getLongPressTimeout() * 1.25f));
117                } catch (InterruptedException e) {
118                    e.printStackTrace();
119                }
120            }
121        }
122    }
123
124    @LargeTest
125    public void testLongPress() {
126        int i;
127        int count = mListView.getChildCount();
128        mActivity.enableLongPress();
129        mActivity.setClickedPosition(-1);
130        mActivity.setLongClickedPosition(-1);
131
132        for (i = 0; i < count; i++) {
133            View child = mListView.getChildAt(i);
134            if ((child.getTop() >= mListView.getListPaddingTop())
135                    && (child.getBottom() <=
136                        mListView.getHeight() - mListView.getListPaddingBottom())) {
137                TouchUtils.longClickView(this, child);
138                assertEquals("Incorrect view position reported being long clicked", i,
139                        mActivity.getLongClickedPosition());
140                assertEquals("View falsely reported being clicked", -1,
141                        mActivity.getClickedPosition());
142            }
143        }
144    }
145
146    @MediumTest
147    public void testNoScroll() {
148        View firstChild = mListView.getChildAt(0);
149        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
150
151        int firstTop = firstChild.getTop();
152
153        TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT,
154                0, -(ViewConfiguration.getTouchSlop()));
155
156        View newFirstChild = mListView.getChildAt(0);
157
158        assertEquals("View scrolled too early", firstTop, newFirstChild.getTop());
159        assertEquals("Wrong view in first position", 0, newFirstChild.getId());
160    }
161
162    // TODO: needs to be adjusted to pass on non-HVGA displays
163    // @LargeTest
164    public void testShortScroll() {
165        View firstChild = mListView.getChildAt(0);
166        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
167
168        int firstTop = firstChild.getTop();
169
170        TouchUtils.dragViewBy(this, lastChild, Gravity.TOP | Gravity.LEFT,
171                0, -(ViewConfiguration.getTouchSlop() + 1 + 10));
172
173        View newFirstChild = mListView.getChildAt(0);
174
175        assertEquals("View scrolled too early", firstTop, newFirstChild.getTop() + 10);
176        assertEquals("Wrong view in first position", 0, newFirstChild.getId());
177    }
178
179    // TODO: needs to be adjusted to pass on non-HVGA displays
180    // @LargeTest
181    public void testLongScroll() {
182        View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
183
184        int lastTop = lastChild.getTop();
185
186        int distance = TouchUtils.dragViewToY(this, lastChild,
187                Gravity.TOP | Gravity.LEFT, mListView.getTop());
188
189        assertEquals("View scrolled to wrong position",
190                lastTop - (distance - ViewConfiguration.getTouchSlop() - 1), lastChild.getTop());
191    }
192
193
194}
195