Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36a561b4 authored by Svetoslav Ganov's avatar Svetoslav Ganov
Browse files

Fixing failing focus tests

Change-Id: I3df6a72f6340cbf2e42ce4913e28471e9358088b
parent 08a9e9f2
Loading
Loading
Loading
Loading
+18 −10
Original line number Diff line number Diff line
@@ -36,6 +36,11 @@ import android.util.AttributeSet;
 * entire width of the view.  The height of the view is divided evenly among
 * the rows.
 *
 * Note: If the height of the view does not divide exactly to the number of rows,
 *       the last row's height is inflated with the remainder. For example, if the
 *       view height is 22 and there are two rows, the height of the first row is
 *       10 and the second 22.
 *
 * Notice what this view does to be a good citizen w.r.t its internal selection:
 * 1) calls {@link View#requestRectangleOnScreen} each time the selection changes due to
 *    internal navigation.
@@ -138,9 +143,6 @@ public class InternalSelectionView extends View {

    @Override
    protected void onDraw(Canvas canvas) {

        int rowHeight = getRowHeight();

        int rectTop = mPaddingTop;
        int rectLeft = mPaddingLeft;
        int rectRight = getWidth() - mPaddingRight;
@@ -149,6 +151,8 @@ public class InternalSelectionView extends View {
            mPainter.setColor(Color.BLACK);
            mPainter.setAlpha(0x20);

            int rowHeight = getRowHeight(i);

            // draw background rect
            mTempRect.set(rectLeft, rectTop, rectRight, rectTop + rowHeight);
            canvas.drawRect(mTempRect, mPainter);
@@ -178,12 +182,19 @@ public class InternalSelectionView extends View {
        }
    }

    private int getRowHeight() {
        return (getHeight() - mPaddingTop - mPaddingBottom) / mNumRows;
    private int getRowHeight(int row) {
        final int availableHeight = getHeight() - mPaddingTop - mPaddingBottom;
        final int desiredRowHeight = availableHeight / mNumRows;
        if (row < mNumRows - 1) {
            return desiredRowHeight;
        } else {
            final int residualHeight = availableHeight % mNumRows;
            return desiredRowHeight + residualHeight;
        }
    }

    public void getRectForRow(Rect rect, int row) {
        final int rowHeight = getRowHeight();
        final int rowHeight = getRowHeight(row);
        final int top = mPaddingTop + row * rowHeight;
        rect.set(mPaddingLeft,
                top,
@@ -197,10 +208,7 @@ public class InternalSelectionView extends View {
        requestRectangleOnScreen(mTempRect);
    }


    /* (non-Javadoc)
    * @see android.view.KeyEvent.Callback#onKeyDown(int, android.view.KeyEvent)
    */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch(event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_UP:
+5 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.widget.focus;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
@@ -110,7 +111,10 @@ public class ListOfInternalSelectionViews extends Activity {
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mScreenHeight = getWindowManager().getDefaultDisplay().getHeight();

        Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);
        mScreenHeight = size.y;

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
+1 −1
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ public class RequestFocusTest extends ActivityInstrumentationTestCase<RequestFoc
            fail("requestFocus from wrong thread should raise exception.");
        } catch (AndroidRuntimeException e) {
            // Expected.  The actual exception is not public, so we can't catch it.
            assertEquals("android.view.ViewAncestor$CalledFromWrongThreadException",
            assertEquals("android.view.ViewRootImpl$CalledFromWrongThreadException",
                         e.getClass().getName());
        }
    }
+18 −9
Original line number Diff line number Diff line
@@ -20,10 +20,9 @@ import android.graphics.Rect;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.InternalSelectionView;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.focus.ListOfInternalSelectionViews;
import android.util.InternalSelectionView;


/**
@@ -51,6 +50,10 @@ public class ScrollingThroughListOfFocusablesTest extends InstrumentationTestCas
                    mNumRowsPerItem,      // 5 internally selectable rows per item
                    mScreenHeightFactor)); // each item is 5 / 4 screen height tall
        mListView = mActivity.getListView();
        // Make sure we have some fading edge regardless of ListView style.
        mListView.setVerticalFadingEdgeEnabled(true);
        mListView.setFadingEdgeLength(10);
        ensureNotInTouchMode();
    }

    @Override
@@ -67,12 +70,12 @@ public class ScrollingThroughListOfFocusablesTest extends InstrumentationTestCas
        assertEquals(mNumRowsPerItem, mActivity.getNumRowsPerItem());
    }

    // TODO: needs to be adjusted to pass on non-HVGA displays
    // @MediumTest
    @MediumTest
    public void testScrollingDownInFirstItem() throws Exception {

        for (int i = 0; i < mNumRowsPerItem; i++) {
            assertEquals(0, mListView.getSelectedItemPosition());
            
            InternalSelectionView view = mActivity.getSelectedView();

            assertInternallySelectedRowOnScreen(view, i);
@@ -90,13 +93,12 @@ public class ScrollingThroughListOfFocusablesTest extends InstrumentationTestCas
                    mListView.getSelectedView();

            // 1 pixel tolerance in case height / 4 is not an even number
            final int fadingEdge = mListView.getBottom() - mListView.getVerticalFadingEdgeLength();
            final int bottomFadingEdgeTop =
                mListView.getBottom() - mListView.getVerticalFadingEdgeLength();
            assertTrue("bottom of view should be just above fading edge",
                    view.getBottom() >= fadingEdge - 1 &&
                    view.getBottom() <= fadingEdge);
                    view.getBottom() == bottomFadingEdgeTop);
        }


        // make sure fading edge is the expected view
        {
            assertEquals("should be a second view visible due to the fading edge",
@@ -109,7 +111,6 @@ public class ScrollingThroughListOfFocusablesTest extends InstrumentationTestCas
        }
    }


    @MediumTest
    public void testScrollingToSecondItem() throws Exception {

@@ -223,4 +224,12 @@ public class ScrollingThroughListOfFocusablesTest extends InstrumentationTestCas
        assertTrue("bottom of row " + row + " should be on sreen",
                mTempRect.bottom < mActivity.getScreenHeight());
    }

    private void ensureNotInTouchMode() {
        // If in touch mode inject a DPAD down event to exit that mode.
        if (mListView.isInTouchMode()) {
            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
            getInstrumentation().waitForIdleSync();
        }
    }
}