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