FileDocCategorySizeDatePackage
RequestRectangleVisibleTest.javaAPI DocAndroid 5.1 API8538Thu Mar 12 22:22:12 GMT 2015android.widget.scroll

RequestRectangleVisibleTest

public class RequestRectangleVisibleTest extends android.test.ActivityInstrumentationTestCase
{@link RequestRectangleVisible} is set up to exercise the cases of moving a rectangle that is either off screen or not entirely on the screen onto the screen.

Fields Summary
private android.widget.ScrollView
mScrollView
private android.widget.Button
mClickToScrollFromAbove
private android.widget.Button
mClickToScrollToUpperBlob
private android.widget.TextView
mTopBlob
private android.view.View
mChildToScrollTo
private android.widget.TextView
mBottomBlob
private android.widget.Button
mClickToScrollToBlobLowerBlob
private android.widget.Button
mClickToScrollFromBelow
Constructors Summary
public RequestRectangleVisibleTest()

        super("com.android.frameworks.coretests", RequestRectangleVisible.class);
    
Methods Summary
public static voidassertOnBottomEdgeOfScreen(android.view.View origin, android.view.View view)
Assert that view overlaps the bottom edge of the screen

param
origin The root view of the screen.
param
view The view

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int bottom = xy[1] + view.getHeight();
        int bottomOfRoot = xyRoot[1] + origin.getHeight();

        assertTrue(bottom > bottomOfRoot);

        assertTrue(xy[1] < bottomOfRoot);
        assertTrue(bottom > bottomOfRoot);
    
public static voidassertOnTopEdgeOfScreen(android.view.View origin, android.view.View view)
Assert that view overlaps the bottom edge of the screen

param
origin The root view of the screen.
param
view The view

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int bottom = xy[1] + view.getHeight();
        int bottomOfRoot = xyRoot[1] + origin.getHeight();

        assertTrue(bottom < bottomOfRoot);
        assertTrue(bottom > xyRoot[1]);

        assertTrue(xy[1] < xyRoot[1]);
    
private voidpressDownUntilViewInFocus(android.view.View view, int maxKeyPress)
Press the down key until a particular view is in focus

param
view The view to get in focus.
param
maxKeyPress The maximum times to press down before failing.

        int count = 0;
        while(!view.hasFocus()) {
            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
            getInstrumentation().waitForIdleSync();

            // just in case...
            if (++count > maxKeyPress) {
                fail("couldn't move down to bottom button within "
                        + maxKeyPress + " key presses.");
            }
        }
    
protected voidsetUp()

        super.setUp();

        RequestRectangleVisible a = getActivity();

        mScrollView = (ScrollView) a.findViewById(R.id.scrollView);
        mClickToScrollFromAbove = (Button) a.findViewById(R.id.scrollToRectFromTop);
        mClickToScrollToUpperBlob = (Button) a.findViewById(R.id.scrollToRectFromTop2);
        mTopBlob = (TextView) a.findViewById(R.id.topBlob);
        mChildToScrollTo = a.findViewById(R.id.childToMakeVisible);
        mBottomBlob = (TextView) a.findViewById(R.id.bottomBlob);
        mClickToScrollToBlobLowerBlob = (Button) a.findViewById(R.id.scrollToRectFromBottom2);
        mClickToScrollFromBelow = (Button) a.findViewById(R.id.scrollToRectFromBottom);

        
    
public voidtestPreconditions()

        assertNotNull(mScrollView);
        assertNotNull(mClickToScrollFromAbove);
        assertNotNull(mClickToScrollToUpperBlob);
        assertNotNull(mTopBlob);
        assertNotNull(mChildToScrollTo);
        assertNotNull(mBottomBlob);
        assertNotNull(mClickToScrollToBlobLowerBlob);
        assertNotNull(mClickToScrollFromBelow);

        assertTrue("top blob needs to be taller than the screen for many of the "
                + "tests below to work.",
                mTopBlob.getHeight() > mScrollView.getHeight());

        assertTrue("bottom blob needs to be taller than the screen for many of the "
                + "tests below to work.",
                mBottomBlob.getHeight() > mScrollView.getHeight());

        assertTrue("top blob needs to be lower than the fading edge region",
                mTopBlob.getTop() > mScrollView.getVerticalFadingEdgeLength());
    
public voidtestScrollToOffScreenRectangleFromBottom()

        // go to bottom button
        pressDownUntilViewInFocus(mClickToScrollFromBelow, 10);

        // view is off screen above
        assertTrue(mClickToScrollFromBelow.hasFocus());
        ViewAsserts.assertOffScreenAbove(mScrollView, mChildToScrollTo);

        // click
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();  // wait for scrolling to finish

        // on screen, positioned at top (with room for fading edge)
        ViewAsserts.assertOnScreen(mScrollView, mChildToScrollTo);
        ViewAsserts.assertHasScreenCoordinates(
                mScrollView, mChildToScrollTo, 0, mScrollView.getVerticalFadingEdgeLength());
    
public voidtestScrollToOffScreenRectangleFromTop()

        // view is off screen
        assertTrue(mClickToScrollFromAbove.hasFocus());
        ViewAsserts.assertOffScreenBelow(mScrollView, mChildToScrollTo);

        // click
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();  // wait for scrolling to finish

        // should be on screen, positioned at the bottom (with room for
        // fading edge)
        ViewAsserts.assertOnScreen(mScrollView, mChildToScrollTo);
        ViewAsserts.assertHasScreenCoordinates(
                mScrollView, mChildToScrollTo,
                0,
                mScrollView.getHeight()
                        - mChildToScrollTo.getHeight()
                        - mScrollView.getVerticalFadingEdgeLength());
    
public voidtestScrollToPartiallyOffScreenRectFromBottom()

        pressDownUntilViewInFocus(mClickToScrollToBlobLowerBlob, 10);

        // make sure the blob is indeed partially on screen above
        assertOnTopEdgeOfScreen(mScrollView, mBottomBlob);

        // click
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();  // wait for scrolling to finish

        // blob should have moved so bottom of it is at bottom of screen
        // with room for vertical fading edge
        ViewAsserts.assertHasScreenCoordinates(
                mScrollView, mBottomBlob,
                0,
                mScrollView.getHeight() - mBottomBlob.getHeight()
                    - mScrollView.getVerticalFadingEdgeLength());
    
public voidtestScrollToPartiallyOffScreenRectFromTop()

        pressDownUntilViewInFocus(mClickToScrollToUpperBlob, 4);

        // make sure the blob is indeed partially on screen below
        assertOnBottomEdgeOfScreen(mScrollView, mTopBlob);

        // click
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();  // wait for scrolling to finish

        // blob should have moved so top of it is at top of screen (with
        // room for the vertical fading edge
        ViewAsserts.assertHasScreenCoordinates(
                mScrollView, mTopBlob, 0, mScrollView.getVerticalFadingEdgeLength());