Fix Flakey Widget Tests

Bug 6419880 6420509 6420511 6420513 6419883

Change-Id: I418b748f68b22e0b04fd174e1a66c44549484aec
diff --git a/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java b/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
index c8a184a..6846d38 100644
--- a/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
@@ -18,13 +18,13 @@
 
 import com.android.cts.stub.R;
 
-
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
+import android.cts.util.PollingCheck;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Rect;
@@ -304,12 +304,17 @@
 
         Drawable drawable = mListView.getSelector();
         assertNotNull(drawable);
-        Rect r = drawable.getBounds();
+        final Rect r = drawable.getBounds();
 
-        TextView v = (TextView) mListView.getSelectedView();
+        final TextView v = (TextView) mListView.getSelectedView();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return v.getRight() == r.right;
+            }
+        }.run();
         assertEquals(v.getLeft(), r.left);
         assertEquals(v.getTop(), r.top);
-        assertEquals(v.getRight(), r.right);
         assertEquals(v.getBottom(), r.bottom);
     }
 
@@ -518,10 +523,10 @@
         });
         mInstrumentation.waitForIdleSync();
 
-        TextView v = (TextView) listView.getSelectedView();
+        final TextView v = (TextView) listView.getSelectedView();
         assertNull(listView.getContextMenuInfo());
 
-        MockOnItemLongClickListener listener = new MockOnItemLongClickListener();
+        final MockOnItemLongClickListener listener = new MockOnItemLongClickListener();
         listView.setOnItemLongClickListener(listener);
 
         assertNull(listener.getParent());
@@ -531,8 +536,14 @@
 
         TouchUtils.longClickView(this, v);
 
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return v == listener.getView();
+            }
+        }.run();
+
         assertSame(listView, listener.getParent());
-        assertSame(v, listener.getView());
         assertEquals(2, listener.getPosition());
         assertEquals(listView.getItemIdAtPosition(2), listener.getID());
 
diff --git a/tests/tests/widget/src/android/widget/cts/FrameLayoutTest.java b/tests/tests/widget/src/android/widget/cts/FrameLayoutTest.java
index b8c3992..5b225e8 100755
--- a/tests/tests/widget/src/android/widget/cts/FrameLayoutTest.java
+++ b/tests/tests/widget/src/android/widget/cts/FrameLayoutTest.java
@@ -18,13 +18,13 @@
 
 import com.android.cts.stub.R;
 
-
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
+import android.cts.util.PollingCheck;
 import android.graphics.Rect;
 import android.graphics.Region;
 import android.graphics.drawable.BitmapDrawable;
@@ -37,9 +37,9 @@
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.FrameLayout;
+import android.widget.FrameLayout.LayoutParams;
 import android.widget.LinearLayout;
 import android.widget.TextView;
-import android.widget.FrameLayout.LayoutParams;
 
 import java.io.IOException;
 
@@ -90,9 +90,14 @@
         assertSame(foreground, mFrameLayout.getForeground());
         // check the default gravity FILL, it completely fills its container
         assertTrue(foreground.isVisible());
-        Rect rect = foreground.getBounds();
+        final Rect rect = foreground.getBounds();
         // foreground has been stretched
-        assertEquals(mFrameLayout.getHeight(), rect.bottom - rect.top);
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return mFrameLayout.getHeight() == rect.bottom - rect.top;
+            }
+        }.run();
         assertEquals(mFrameLayout.getWidth(), rect.right - rect.left);
 
         // should get a new foreground again, because former foreground has been stretched
@@ -112,10 +117,10 @@
         mInstrumentation.waitForIdleSync();
         assertSame(newForeground, mFrameLayout.getForeground());
         assertTrue(newForeground.isVisible());
-        rect = newForeground.getBounds();
+        Rect rect2 = newForeground.getBounds();
         // not changing its size
-        assertEquals(foreground.getIntrinsicHeight(), rect.bottom - rect.top);
-        assertEquals(foreground.getIntrinsicWidth(), rect.right - rect.left);
+        assertEquals(foreground.getIntrinsicHeight(), rect2.bottom - rect2.top);
+        assertEquals(foreground.getIntrinsicWidth(), rect2.right - rect2.left);
         assertCenterAligned(mFrameLayout, newForeground);
     }
 
diff --git a/tests/tests/widget/src/android/widget/cts/ListViewTest.java b/tests/tests/widget/src/android/widget/cts/ListViewTest.java
index 50a2f16..d2bd4f4 100644
--- a/tests/tests/widget/src/android/widget/cts/ListViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/ListViewTest.java
@@ -19,12 +19,12 @@
 import com.android.cts.stub.R;
 import com.google.android.collect.Lists;
 
-
 import org.xmlpull.v1.XmlPullParser;
 
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
+import android.cts.util.PollingCheck;
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
@@ -39,10 +39,10 @@
 import android.view.ViewGroup;
 import android.view.animation.LayoutAnimationController;
 import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
-import android.widget.AdapterView.OnItemClickListener;
 
 import java.util.List;
 
@@ -142,8 +142,13 @@
         mInstrumentation.waitForIdleSync();
 
         Drawable d = mListView.getDivider();
-        Rect r = d.getBounds();
-        assertTrue(r.bottom - r.top > 0);
+        final Rect r = d.getBounds();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return r.bottom - r.top > 0;
+            }
+        }.run();
 
         mInstrumentation.runOnMainSync(new Runnable() {
             public void run() {
@@ -362,11 +367,17 @@
         mInstrumentation.waitForIdleSync();
 
         Drawable defaultDrawable = mListView.getDivider();
-        Rect r = defaultDrawable.getBounds();
-        assertTrue(r.bottom - r.top > 0);
+        final Rect r = defaultDrawable.getBounds();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return r.bottom - r.top > 0;
+            }
+        }.run();
 
         final Drawable d = mActivity.getResources().getDrawable(R.drawable.scenery);
-        r = d.getBounds();
+
+        Rect r2 = d.getBounds();
         mInstrumentation.runOnMainSync(new Runnable() {
             public void run() {
                 mListView.setDivider(d);
@@ -374,7 +385,7 @@
         });
         mInstrumentation.waitForIdleSync();
         assertSame(d, mListView.getDivider());
-        assertEquals(r.bottom - r.top, mListView.getDividerHeight());
+        assertEquals(r2.bottom - r2.top, mListView.getDividerHeight());
 
         mInstrumentation.runOnMainSync(new Runnable() {
             public void run() {
@@ -383,7 +394,7 @@
         });
         mInstrumentation.waitForIdleSync();
         assertEquals(10, mListView.getDividerHeight());
-        assertEquals(10, r.bottom - r.top);
+        assertEquals(10, r2.bottom - r2.top);
     }
 
     public void testSetSelection() {