Home | History | Annotate | Download | only in scroll
      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 
     17 package android.widget.scroll;
     18 
     19 import android.widget.scroll.RequestRectangleVisible;
     20 import com.android.frameworks.coretests.R;
     21 
     22 import android.test.ActivityInstrumentationTestCase;
     23 import android.test.suitebuilder.annotation.LargeTest;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.test.ViewAsserts;
     26 import android.widget.Button;
     27 import android.widget.ScrollView;
     28 import android.widget.TextView;
     29 import android.view.View;
     30 import android.view.KeyEvent;
     31 
     32 /**
     33  * {@link RequestRectangleVisible} is set up to exercise the cases of moving a
     34  * rectangle that is either off screen or not entirely on the screen onto the screen.
     35  */
     36 public class RequestRectangleVisibleTest extends ActivityInstrumentationTestCase<RequestRectangleVisible> {
     37 
     38     private ScrollView mScrollView;
     39 
     40     private Button mClickToScrollFromAbove;
     41     private Button mClickToScrollToUpperBlob;
     42     private TextView mTopBlob;
     43 
     44     private View mChildToScrollTo;
     45 
     46     private TextView mBottomBlob;
     47     private Button mClickToScrollToBlobLowerBlob;
     48     private Button mClickToScrollFromBelow;
     49 
     50     public RequestRectangleVisibleTest() {
     51         super("com.android.frameworks.coretests", RequestRectangleVisible.class);
     52     }
     53 
     54     @Override
     55     protected void setUp() throws Exception {
     56         super.setUp();
     57 
     58         RequestRectangleVisible a = getActivity();
     59 
     60         mScrollView = (ScrollView) a.findViewById(R.id.scrollView);
     61         mClickToScrollFromAbove = (Button) a.findViewById(R.id.scrollToRectFromTop);
     62         mClickToScrollToUpperBlob = (Button) a.findViewById(R.id.scrollToRectFromTop2);
     63         mTopBlob = (TextView) a.findViewById(R.id.topBlob);
     64         mChildToScrollTo = a.findViewById(R.id.childToMakeVisible);
     65         mBottomBlob = (TextView) a.findViewById(R.id.bottomBlob);
     66         mClickToScrollToBlobLowerBlob = (Button) a.findViewById(R.id.scrollToRectFromBottom2);
     67         mClickToScrollFromBelow = (Button) a.findViewById(R.id.scrollToRectFromBottom);
     68 
     69 
     70     }
     71 
     72 
     73     @MediumTest
     74     public void testPreconditions() {
     75         assertNotNull(mScrollView);
     76         assertNotNull(mClickToScrollFromAbove);
     77         assertNotNull(mClickToScrollToUpperBlob);
     78         assertNotNull(mTopBlob);
     79         assertNotNull(mChildToScrollTo);
     80         assertNotNull(mBottomBlob);
     81         assertNotNull(mClickToScrollToBlobLowerBlob);
     82         assertNotNull(mClickToScrollFromBelow);
     83 
     84         assertTrue("top blob needs to be taller than the screen for many of the "
     85                 + "tests below to work.",
     86                 mTopBlob.getHeight() > mScrollView.getHeight());
     87 
     88         assertTrue("bottom blob needs to be taller than the screen for many of the "
     89                 + "tests below to work.",
     90                 mBottomBlob.getHeight() > mScrollView.getHeight());
     91 
     92         assertTrue("top blob needs to be lower than the fading edge region",
     93                 mTopBlob.getTop() > mScrollView.getVerticalFadingEdgeLength());
     94     }
     95 
     96     @MediumTest
     97     public void testScrollToOffScreenRectangleFromTop() {
     98         // view is off screen
     99         assertTrue(mClickToScrollFromAbove.hasFocus());
    100         ViewAsserts.assertOffScreenBelow(mScrollView, mChildToScrollTo);
    101 
    102         // click
    103         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    104         getInstrumentation().waitForIdleSync();  // wait for scrolling to finish
    105 
    106         // should be on screen, positioned at the bottom (with room for
    107         // fading edge)
    108         ViewAsserts.assertOnScreen(mScrollView, mChildToScrollTo);
    109         ViewAsserts.assertHasScreenCoordinates(
    110                 mScrollView, mChildToScrollTo,
    111                 0,
    112                 mScrollView.getHeight()
    113                         - mChildToScrollTo.getHeight()
    114                         - mScrollView.getVerticalFadingEdgeLength());
    115     }
    116 
    117     @MediumTest
    118     public void testScrollToPartiallyOffScreenRectFromTop() {
    119         pressDownUntilViewInFocus(mClickToScrollToUpperBlob, 4);
    120 
    121         // make sure the blob is indeed partially on screen below
    122         assertOnBottomEdgeOfScreen(mScrollView, mTopBlob);
    123 
    124         // click
    125         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    126         getInstrumentation().waitForIdleSync();  // wait for scrolling to finish
    127 
    128         // blob should have moved so top of it is at top of screen (with
    129         // room for the vertical fading edge
    130         ViewAsserts.assertHasScreenCoordinates(
    131                 mScrollView, mTopBlob, 0, mScrollView.getVerticalFadingEdgeLength());
    132     }
    133 
    134     @LargeTest
    135     public void testScrollToOffScreenRectangleFromBottom() {
    136         // go to bottom button
    137         pressDownUntilViewInFocus(mClickToScrollFromBelow, 10);
    138 
    139         // view is off screen above
    140         assertTrue(mClickToScrollFromBelow.hasFocus());
    141         ViewAsserts.assertOffScreenAbove(mScrollView, mChildToScrollTo);
    142 
    143         // click
    144         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    145         getInstrumentation().waitForIdleSync();  // wait for scrolling to finish
    146 
    147         // on screen, positioned at top (with room for fading edge)
    148         ViewAsserts.assertOnScreen(mScrollView, mChildToScrollTo);
    149         ViewAsserts.assertHasScreenCoordinates(
    150                 mScrollView, mChildToScrollTo, 0, mScrollView.getVerticalFadingEdgeLength());
    151     }
    152 
    153 
    154     @LargeTest
    155     public void testScrollToPartiallyOffScreenRectFromBottom() {
    156         pressDownUntilViewInFocus(mClickToScrollToBlobLowerBlob, 10);
    157 
    158         // make sure the blob is indeed partially on screen above
    159         assertOnTopEdgeOfScreen(mScrollView, mBottomBlob);
    160 
    161         // click
    162         sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
    163         getInstrumentation().waitForIdleSync();  // wait for scrolling to finish
    164 
    165         // blob should have moved so bottom of it is at bottom of screen
    166         // with room for vertical fading edge
    167         ViewAsserts.assertHasScreenCoordinates(
    168                 mScrollView, mBottomBlob,
    169                 0,
    170                 mScrollView.getHeight() - mBottomBlob.getHeight()
    171                     - mScrollView.getVerticalFadingEdgeLength());
    172     }
    173 
    174 
    175     /**
    176      * Press the down key until a particular view is in focus
    177      * @param view The view to get in focus.
    178      * @param maxKeyPress The maximum times to press down before failing.
    179      */
    180     private void pressDownUntilViewInFocus(View view, int maxKeyPress) {
    181         int count = 0;
    182         while(!view.hasFocus()) {
    183             sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    184             getInstrumentation().waitForIdleSync();
    185 
    186             // just in case...
    187             if (++count > maxKeyPress) {
    188                 fail("couldn't move down to bottom button within "
    189                         + maxKeyPress + " key presses.");
    190             }
    191         }
    192     }
    193 
    194     /**
    195      * Assert that view overlaps the bottom edge of the screen
    196      * @param origin The root view of the screen.
    197      * @param view The view
    198      */
    199     static public void assertOnBottomEdgeOfScreen(View origin, View view) {
    200         int[] xy = new int[2];
    201         view.getLocationOnScreen(xy);
    202 
    203         int[] xyRoot = new int[2];
    204         origin.getLocationOnScreen(xyRoot);
    205 
    206         int bottom = xy[1] + view.getHeight();
    207         int bottomOfRoot = xyRoot[1] + origin.getHeight();
    208 
    209         assertTrue(bottom > bottomOfRoot);
    210 
    211         assertTrue(xy[1] < bottomOfRoot);
    212         assertTrue(bottom > bottomOfRoot);
    213     }
    214 
    215     /**
    216      * Assert that view overlaps the bottom edge of the screen
    217      * @param origin The root view of the screen.
    218      * @param view The view
    219      */
    220     static public void assertOnTopEdgeOfScreen(View origin, View view) {
    221         int[] xy = new int[2];
    222         view.getLocationOnScreen(xy);
    223 
    224         int[] xyRoot = new int[2];
    225         origin.getLocationOnScreen(xyRoot);
    226 
    227         int bottom = xy[1] + view.getHeight();
    228         int bottomOfRoot = xyRoot[1] + origin.getHeight();
    229 
    230         assertTrue(bottom < bottomOfRoot);
    231         assertTrue(bottom > xyRoot[1]);
    232 
    233         assertTrue(xy[1] < xyRoot[1]);
    234     }
    235 
    236 
    237 }
    238