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.view.KeyEvent;
22import android.view.View;
23import android.widget.ListView;
24import android.widget.TextView;
25import android.widget.listview.ListWithOffScreenNextSelectable;
26
27public class ListWithOffScreenNextSelectableTest
28        extends ActivityInstrumentationTestCase<ListWithOffScreenNextSelectable> {
29    private ListView mListView;
30
31    public ListWithOffScreenNextSelectableTest() {
32        super("com.android.frameworks.coretests", ListWithOffScreenNextSelectable.class);
33    }
34
35    @Override
36    protected void setUp() throws Exception {
37        super.setUp();
38
39        mListView = getActivity().getListView();
40    }
41
42    @MediumTest
43    public void testPreconditions() {
44        assertNotNull(mListView);
45        assertEquals(5, mListView.getAdapter().getCount());
46        assertFalse(mListView.getAdapter().areAllItemsEnabled());
47        assertFalse(mListView.getAdapter().isEnabled(1));
48        assertFalse(mListView.getAdapter().isEnabled(2));
49        assertFalse(mListView.getAdapter().isEnabled(3));
50        assertEquals("only 4 children should be on screen (so that next selectable is off " +
51                "screen) for this test to be meaningful.",
52                4, mListView.getChildCount());
53        assertEquals(0, mListView.getSelectedItemPosition());
54    }
55
56    // when the next items on screen are not selectable, we pan until the next selectable item
57    // is (partially visible), then we jump to it
58    @MediumTest
59    public void testGoDownToOffScreenSelectable() {
60
61        final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
62
63        final View lastVisibleView = mListView.getChildAt(mListView.getChildCount() - 1);
64        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
65        assertEquals("expecting view to be panned to just above fading edge",
66                listBottom - mListView.getVerticalFadingEdgeLength(), lastVisibleView.getBottom());
67        assertEquals("selection should not have moved yet",
68                0, mListView.getSelectedItemPosition());
69
70        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
71        assertEquals("selection should have moved",
72                4, mListView.getSelectedItemPosition());
73        assertEquals("wrong view created when scrolling",
74                getActivity().getValueAtPosition(4), ((TextView) mListView.getSelectedView()).getText());
75        assertEquals(listBottom, mListView.getSelectedView().getBottom());
76    }
77
78    @MediumTest
79    public void testGoUpToOffScreenSelectable() {
80        final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
81        final int listTop = mListView.getListPaddingTop();
82
83        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
84        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
85
86        assertEquals(4, mListView.getSelectedItemPosition());
87        assertEquals(listBottom, mListView.getSelectedView().getBottom());
88
89        // now we have the reverse situation: the next selectable position upward is off screen
90        final View firstVisibleView = mListView.getChildAt(0);
91        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
92        assertEquals("should have panned top view just below vertical fading edge",
93                listTop + mListView.getVerticalFadingEdgeLength(), firstVisibleView.getTop());
94        assertEquals("selection should not have moved yet",
95                4, mListView.getSelectedItemPosition());
96
97        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
98        assertEquals("selection should have moved",
99                0, mListView.getSelectedItemPosition());
100        assertEquals(getActivity().getValueAtPosition(0),((TextView) mListView.getSelectedView()).getText());
101        assertEquals(listTop, mListView.getSelectedView().getTop());
102
103    }
104
105}
106