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.widget.ListView;
23import android.view.KeyEvent;
24import android.widget.listview.ListItemsExpandOnSelection;
25
26public class ListItemsExpandOnSelectionTest extends ActivityInstrumentationTestCase<ListItemsExpandOnSelection> {
27    private ListView mListView;
28    private int mListTop;
29    private int mListBottom;
30    private int mExpandedHeight;
31    private int mNormalHeight;
32
33    public ListItemsExpandOnSelectionTest() {
34        super("com.android.frameworks.coretests",
35                ListItemsExpandOnSelection.class);
36    }
37
38    @Override
39    protected void setUp() throws Exception {
40        super.setUp();
41        mListView = getActivity().getListView();
42        mListTop = mListView.getListPaddingTop();
43        mListBottom = mListView.getHeight() - mListView.getListPaddingBottom();
44        mExpandedHeight = mListView.getChildAt(0).getHeight();
45        mNormalHeight = mListView.getChildAt(1).getHeight();
46    }
47
48    @MediumTest
49    public void testPreconditions() {
50        assertEquals(0, mListView.getSelectedItemPosition());
51        assertEquals("selected item should be 1.5 times taller than the others",
52                mExpandedHeight, (int) (mNormalHeight * 1.5));
53    }
54
55    @MediumTest
56    public void testMoveSelectionDownNotRequiringScroll() {
57
58        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
59
60        assertEquals(1, mListView.getSelectedItemPosition());
61        assertEquals("first item's top should not have shifted",
62                mListTop, mListView.getChildAt(0).getTop());
63
64    }
65
66    @MediumTest
67    public void testMoveSelectionUpNotRequiringScroll() {
68        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
69
70        assertEquals(1, mListView.getSelectedItemPosition());
71        final int oldBottom = mListView.getSelectedView().getBottom();
72
73        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
74
75        assertEquals("bottom of 2nd itme should have stayed the same when " +
76                "moving back up",
77                oldBottom,
78                mListView.getChildAt(1).getBottom());
79    }
80
81    @MediumTest
82    public void testMoveSelectionDownRequiringScroll() {
83        int lastItemIndex = mListView.getChildCount() - 1;
84
85        for(int i = 0; i < lastItemIndex; i++) {
86            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
87        }
88        getInstrumentation().waitForIdleSync();
89
90        assertEquals("list position", lastItemIndex, mListView.getSelectedItemPosition());
91        assertEquals("expanded height", mExpandedHeight, mListView.getSelectedView().getHeight());
92        assertEquals("should be above bottom fading edge",
93                mListBottom - mListView.getVerticalFadingEdgeLength(),
94                mListView.getSelectedView().getBottom());
95    }
96
97    @LargeTest
98    public void testMoveSelectionUpRequiringScroll() {
99        int childrenPerScreen = mListView.getChildCount();
100
101        // go down past last child on screen
102        for(int i = 0; i < childrenPerScreen; i++) {
103            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
104        }
105
106        // go back up to second to last
107        for(int i = 0; i < childrenPerScreen - 1; i++) {
108            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
109        }
110        getInstrumentation().waitForIdleSync();
111
112        assertEquals("list position", 1, mListView.getSelectedItemPosition());
113        assertEquals("expanded height", mExpandedHeight, mListView.getSelectedView().getHeight());
114        assertEquals("should be below top fading edge",
115                mListTop + mListView.getVerticalFadingEdgeLength(),
116                mListView.getSelectedView().getTop());
117    }
118}
119