From 36a561b4ee35911fd6594c1592aab134be22f0f7 Mon Sep 17 00:00:00 2001 From: Svetoslav Ganov Date: Wed, 25 Jan 2012 17:00:26 -0800 Subject: Fixing failing focus tests Change-Id: I3df6a72f6340cbf2e42ce4913e28471e9358088b --- .../src/android/util/InternalSelectionView.java | 28 ++++++++++++++-------- .../widget/focus/ListOfInternalSelectionViews.java | 6 ++++- .../src/android/widget/focus/RequestFocusTest.java | 2 +- .../ScrollingThroughListOfFocusablesTest.java | 27 ++++++++++++++------- 4 files changed, 42 insertions(+), 21 deletions(-) (limited to 'core/tests') diff --git a/core/tests/coretests/src/android/util/InternalSelectionView.java b/core/tests/coretests/src/android/util/InternalSelectionView.java index babf38d..a0fb0f1 100644 --- a/core/tests/coretests/src/android/util/InternalSelectionView.java +++ b/core/tests/coretests/src/android/util/InternalSelectionView.java @@ -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: diff --git a/core/tests/coretests/src/android/widget/focus/ListOfInternalSelectionViews.java b/core/tests/coretests/src/android/widget/focus/ListOfInternalSelectionViews.java index 6518341..53b866c 100644 --- a/core/tests/coretests/src/android/widget/focus/ListOfInternalSelectionViews.java +++ b/core/tests/coretests/src/android/widget/focus/ListOfInternalSelectionViews.java @@ -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) { diff --git a/core/tests/coretests/src/android/widget/focus/RequestFocusTest.java b/core/tests/coretests/src/android/widget/focus/RequestFocusTest.java index 909a8c9..a78b0c9 100644 --- a/core/tests/coretests/src/android/widget/focus/RequestFocusTest.java +++ b/core/tests/coretests/src/android/widget/focus/RequestFocusTest.java @@ -90,7 +90,7 @@ public class RequestFocusTest extends ActivityInstrumentationTestCase= 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(); + } + } } -- cgit v1.1