1/*
2 * Copyright (C) 2008 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.scroll.arrowscroll;
18
19import android.widget.scroll.ButtonsWithTallTextViewInBetween;
20
21import android.graphics.Rect;
22import android.test.ActivityInstrumentationTestCase;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.view.KeyEvent;
25import android.view.View;
26import android.widget.Button;
27import android.widget.ScrollView;
28import android.widget.TextView;
29
30public class ButtonsWithTallTextViewInBetweenTest
31        extends ActivityInstrumentationTestCase<ButtonsWithTallTextViewInBetween> {
32
33    private ScrollView mScrollView;
34    private Button mTopButton;
35    private TextView mMiddleFiller;
36    private TextView mBottomButton;
37
38    public ButtonsWithTallTextViewInBetweenTest() {
39        super("com.android.frameworks.coretests", ButtonsWithTallTextViewInBetween.class);
40    }
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45
46        mScrollView = getActivity().getScrollView();
47        mTopButton = getActivity().getTopButton();
48        mMiddleFiller = getActivity().getMiddleFiller();
49        mBottomButton = getActivity().getBottomButton();
50    }
51
52    private Rect mTempRect = new Rect();
53
54    private int getTopWithinScrollView(View descendant) {
55        descendant.getDrawingRect(mTempRect);
56        mScrollView.offsetDescendantRectToMyCoords(descendant, mTempRect);
57        return mTempRect.top;
58    }
59
60    private int getBottomWithinScrollView(View descendant) {
61        descendant.getDrawingRect(mTempRect);
62        mScrollView.offsetDescendantRectToMyCoords(descendant, mTempRect);
63        return mTempRect.bottom;
64    }
65
66    @MediumTest
67    public void testPreconditions() {
68        assertTrue("top button should be shorter than max scroll amount",
69                mTopButton.getHeight() <
70                mScrollView.getMaxScrollAmount());
71        assertTrue("bottom button should be further than max scroll amount off screen",
72                getTopWithinScrollView(mBottomButton)- mScrollView.getBottom() > mScrollView.getMaxScrollAmount());
73    }
74
75    @MediumTest
76    public void testPanTopButtonOffScreenLosesFocus() {
77        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
78
79        assertEquals("scroll view should be scrolled by the max amount for one "
80                + "arrow navigation",
81                mScrollView.getMaxScrollAmount(),
82                mScrollView.getScrollY());
83
84        assertTrue("top button should be off screen",
85                getBottomWithinScrollView(mTopButton) < mScrollView.getScrollY());
86
87        assertFalse("top button should have lost focus",
88                mTopButton.isFocused());
89
90        assertTrue("scroll view should be focused", mScrollView.isFocused());
91    }
92
93    @MediumTest
94    public void testScrollDownToBottomButton() throws Exception {
95        final int screenBottom = mScrollView.getScrollY() + mScrollView.getHeight();
96        final int numDownsToButtonButton =
97                ((getBottomWithinScrollView(mBottomButton) - screenBottom)) / mScrollView.getMaxScrollAmount() + 1;
98
99        for (int i = 0; i < numDownsToButtonButton; i++) {
100            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
101        }
102
103        assertTrue("bottombutton.isFocused", mBottomButton.isFocused());
104
105        assertEquals("should be fully scrolled to bottom",
106                getActivity().getLinearLayout().getHeight() - mScrollView.getHeight(),
107                mScrollView.getScrollY());
108    }
109
110    @MediumTest
111    public void testPanBottomButtonOffScreenLosesFocus() throws Exception {
112        mBottomButton.post(new Runnable() {
113            public void run() {
114                mBottomButton.requestFocus();
115            }
116        });
117
118        getInstrumentation().waitForIdleSync();
119
120        assertTrue("bottombutton.isFocused", mBottomButton.isFocused());
121        final int maxScroll = getActivity().getLinearLayout().getHeight()
122                - mScrollView.getHeight();
123        assertEquals("should be fully scrolled to bottom",
124                maxScroll,
125                mScrollView.getScrollY());
126
127        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
128
129        assertEquals("scroll view should have scrolled by the max amount for one "
130                + "arrow navigation",
131                maxScroll - mScrollView.getMaxScrollAmount(),
132                mScrollView.getScrollY());
133
134        assertTrue("bottom button should be off screen",
135                getTopWithinScrollView(mBottomButton) > mScrollView.getScrollY() + mScrollView.getHeight());
136
137        assertFalse("bottom button should have lost focus",
138                mBottomButton.isFocused());
139
140        assertTrue("scroll view should be focused", mScrollView.isFocused());
141    }
142}
143