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.MediumTest;
21import android.test.suitebuilder.annotation.Suppress;
22import android.view.KeyEvent;
23import android.view.View;
24import android.widget.ListView;
25import android.widget.TextView;
26import android.widget.listview.ListOfItemsShorterThanScreen;
27
28public class ListOfItemsShorterThanScreenTest
29        extends ActivityInstrumentationTestCase<ListOfItemsShorterThanScreen> {
30    private ListView mListView;
31    private ListOfItemsShorterThanScreen mActivity;
32
33
34    public ListOfItemsShorterThanScreenTest() {
35        super("com.android.frameworks.coretests", ListOfItemsShorterThanScreen.class);
36    }
37
38    protected void setUp() throws Exception {
39        super.setUp();
40        mActivity = getActivity();
41        mListView = getActivity().getListView();
42    }
43
44    @MediumTest
45    public void testPreconditions() {
46        assertEquals(0, mListView.getSelectedItemPosition());
47        assertTrue(mListView.getChildAt(0).isSelected());
48        assertEquals(mListView.getListPaddingTop(), mListView.getSelectedView().getTop());
49    }
50
51    @MediumTest
52    public void testMoveDownToOnScreenNextItem() {
53        final View next = mListView.getChildAt(1);
54        assertFalse(next.isSelected());
55        final int secondPosition = mListView.getSelectedView().getBottom();
56        assertEquals("second item should be positioned item height pixels from top.",
57                secondPosition,
58                next.getTop());
59
60        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
61        assertEquals(1, mListView.getSelectedItemPosition());
62        assertTrue(next.isSelected());
63        assertEquals("next selected item shouldn't have moved",
64                secondPosition,
65                next.getTop());
66    }
67
68    @MediumTest
69    public void testMoveUpToOnScreenItem() {
70        // move down one, then back up
71        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
72        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
73        assertEquals(0, mListView.getSelectedItemPosition());
74    }
75
76    @MediumTest
77    @Suppress // Failing.
78    public void testMoveDownToItemRequiringScrolling() {
79        final int lastOnScreenItemIndex = mListView.getChildCount() - 1;
80        final View lastItem = mListView.getChildAt(lastOnScreenItemIndex);
81        assertTrue("last item should be partially off screen",
82                lastItem.getBottom() > mListView.getBottom());
83        assertEquals(mListView.getListPaddingTop(), mListView.getSelectedView().getTop());
84
85        for (int i = 0; i < lastOnScreenItemIndex; i++) {
86            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
87        }
88
89        assertEquals(lastOnScreenItemIndex, mListView.getSelectedItemPosition());
90        assertEquals(
91                getTopOfBottomFadingEdge(),
92                mListView.getSelectedView().getBottom());
93
94        // there should be a peeking view
95
96        // the current view isn't the last anymore...
97        assertEquals(mListView.getSelectedView(), mListView.getChildAt(mListView.getChildCount() - 2));
98
99        // peeking view is now last
100        final TextView view = (TextView) mListView.getChildAt(mListView.getChildCount() - 1);
101        assertEquals(mActivity.getValueAtPosition(lastOnScreenItemIndex + 1), view.getText());
102        assertFalse(view.isSelected());
103    }
104
105    @MediumTest
106    @Suppress // Failing.
107    public void testMoveUpToItemRequiringScrolling() {
108        // go down to one past last item, then back up to the second item.  this will
109        // require scrolling to get it back on screen, and will need a peeking edge
110
111        int numItemsOnScreen = mListView.getChildCount();
112        for (int i = 0; i < numItemsOnScreen; i++) {
113            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
114        }
115        for (int i = 0; i < numItemsOnScreen - 1; i++) {
116            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
117        }
118
119        assertEquals(1, mListView.getSelectedItemPosition());
120        assertEquals("top should be just below vertical fading edge",
121                mListView.getVerticalFadingEdgeLength() + mListView.getListPaddingTop(),
122                mListView.getSelectedView().getTop());
123    }
124
125    @MediumTest
126    public void testPressUpWhenAlreadyAtTop() {
127        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
128
129        assertEquals(0, mListView.getSelectedItemPosition());
130    }
131
132    @MediumTest
133    public void testPressDownWhenAlreadyAtBottom() {
134        final int lastItemPosition = mListView.getAdapter().getCount() - 1;
135        for (int i = 0; i < lastItemPosition; i++) {
136            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
137        }
138        assertEquals(lastItemPosition, mListView.getSelectedItemPosition());
139
140        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
141        assertEquals(lastItemPosition, mListView.getSelectedItemPosition());
142    }
143
144    @MediumTest
145    public void testNoVerticalFadingEdgeWhenMovingToBottom() {
146        final int lastItemPosition = mListView.getAdapter().getCount() - 1;
147        for (int i = 0; i < lastItemPosition; i++) {
148            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
149        }
150        assertEquals(lastItemPosition, mListView.getSelectedItemPosition());
151
152        assertEquals("bottom of last item should be just above padding; no fading edge.",
153                mListView.getHeight() - mListView.getListPaddingBottom(),
154                mListView.getSelectedView().getBottom());
155
156    }
157
158    // the top of the bottom fading edge takes into account the list padding at the bottom,
159    // and the fading edge size
160    private int getTopOfBottomFadingEdge() {
161        return mListView.getHeight() - (mListView.getVerticalFadingEdgeLength() + mListView.getListPaddingBottom());
162    }
163
164
165}
166