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.test.suitebuilder.annotation.Suppress;
23import android.view.KeyEvent;
24import android.view.View;
25import android.widget.ListView;
26import android.widget.listview.ListOfThinItems;
27
28public 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        // focus the listview
54        getActivity().runOnUiThread(() -> mListView.requestFocus());
55        getInstrumentation().waitForIdleSync();
56
57        final int numItems = mListView.getAdapter().getCount();
58        final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
59        for (int i = 0; i < numItems; i++) {
60            assertEquals("wrong selection at position " + i,
61                    i, mListView.getSelectedItemPosition());
62            final int bottomFadingEdge = listBottom - mListView.getVerticalFadingEdgeLength();
63            final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
64            final int lastVisiblePosition = lastChild.getId();
65
66
67            int bottomThreshold = (lastVisiblePosition < mListView.getAdapter().getCount() - 1) ?
68                    bottomFadingEdge : listBottom;
69
70            String prefix = "after " + i + " down presses, ";
71
72            assertTrue(prefix + "selected item is below bottom threshold (fading edge or bottom as " +
73                    "appropriate)",
74                    mListView.getSelectedView().getBottom() <= bottomThreshold);
75            assertTrue(prefix + "first item in list must be at very top or just above",
76                    mListView.getChildAt(0).getTop() <= 0);
77            assertTrue(prefix + "last item in list should be at very bottom or just below",
78                    lastChild.getBottom() >= listBottom);
79            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
80        }
81    }
82
83    @LargeTest
84    public void testScrollToTop() {
85        // focus the listview
86        getActivity().runOnUiThread(() -> mListView.requestFocus());
87        getInstrumentation().waitForIdleSync();
88
89        final int numItems = mListView.getAdapter().getCount();
90
91        for (int i = 0; i < numItems - 1; i++) {
92            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
93        }
94        assertEquals("should have moved to last position",
95                numItems - 1, mListView.getSelectedItemPosition());
96
97        int listTop = mListView.getListPaddingTop();
98        final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
99
100        for (int i = 0; i < numItems; i++) {
101            int expectedPostion = numItems - (i + 1);
102            assertEquals("wrong selection at position " + expectedPostion,
103                    expectedPostion, mListView.getSelectedItemPosition());
104            final int topFadingEdge = listTop + mListView.getVerticalFadingEdgeLength();
105            final View firstChild = mListView.getChildAt(0);
106            final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
107            final int firstVisiblePosition = firstChild.getId();
108
109
110            int topThreshold = (firstVisiblePosition > 0) ?
111                    topFadingEdge : listTop;
112
113            String prefix = "after " + i + " up presses, ";
114
115            assertTrue(prefix + "selected item is above top threshold (fading edge or top as " +
116                    "appropriate)",
117                    mListView.getSelectedView().getTop() >= topThreshold);
118            assertTrue(prefix + "first item in list must be at very top or just above",
119                    firstChild.getTop() <= 0);
120            assertTrue(prefix + "last item in list should be at very bottom or just below",
121                    lastChild.getBottom() >= listBottom);
122            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
123        }
124    }
125}
126