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;
18
19import android.graphics.Rect;
20import android.test.ActivityInstrumentationTestCase;
21import android.test.suitebuilder.annotation.LargeTest;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.view.View;
24import android.view.KeyEvent;
25import android.widget.ListView;
26import android.widget.listview.ListOfThinItems;
27
28public class ListItemRequestRectAboveThinFirstItemTest
29        extends ActivityInstrumentationTestCase<ListOfThinItems> {
30    private ListView mListView;
31
32    public ListItemRequestRectAboveThinFirstItemTest() {
33        super("com.android.frameworks.coretests", ListOfThinItems.class);
34    }
35
36    protected void setUp() throws Exception {
37        super.setUp();
38        mListView = getActivity().getListView();
39    }
40
41    @MediumTest
42    public void testPreconditions() {
43
44        assertTrue("first child needs to be within fading edge height",
45                mListView.getChildAt(0).getBottom() < mListView.getVerticalFadingEdgeLength());
46        assertTrue("should be at least two visible children",
47                mListView.getChildCount() >= 2);
48    }
49
50    // reproduce bug 998501: when first item fits within fading edge,
51    // having the second item call requestRectangleOnScreen with a rect above
52    // the bounds of the list, it was scrolling too far
53    @MediumTest
54    public void testSecondItemRequestRectAboveTop() throws Exception {
55
56        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
57        assertEquals("selected position", 1, mListView.getSelectedItemPosition());
58
59        final View second = mListView.getChildAt(1);
60        final Rect rect = new Rect();
61        second.getDrawingRect(rect);
62        rect.offset(0, -2 * second.getBottom());
63
64        getActivity().requestRectangleOnScreen(1, rect);
65        getInstrumentation().waitForIdleSync();
66
67        assertEquals("top of first item",
68                mListView.getListPaddingTop(), mListView.getChildAt(0).getTop());
69
70    }
71
72    // same thing, but at bottom
73    @LargeTest
74    public void testSecondToLastItemRequestRectBelowBottom() throws Exception {
75
76        final int secondToLastPos = mListView.getCount() - 2;
77
78        while (mListView.getSelectedItemPosition() < secondToLastPos) {
79            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
80        }
81        assertEquals("selected position", secondToLastPos,
82                mListView.getSelectedItemPosition());
83
84        final View secondToLast = mListView.getSelectedView();
85        final Rect rect = new Rect();
86        secondToLast.getDrawingRect(rect);
87        rect.offset(0,
88                2 * (mListView.getBottom() - secondToLast.getTop()));
89
90        final int secondToLastIndex = mListView.getChildCount() - 2;
91        getActivity().requestRectangleOnScreen(secondToLastIndex, rect);
92        getInstrumentation().waitForIdleSync();
93
94        int listBottom = mListView.getHeight() - mListView.getPaddingBottom();
95        assertEquals("bottom of last item should be at bottom of list",
96                listBottom,
97                mListView.getChildAt(mListView.getChildCount() - 1).getBottom());
98    }
99
100
101
102}
103