Fix Flakey View Tests

Bug 6419859 6419189 6419703 6419437 6419385 6380848

Change-Id: I96f123f8a3a2bf67f779f29d3e676f8c24e78808
diff --git a/tests/tests/view/src/android/view/cts/ViewTest.java b/tests/tests/view/src/android/view/cts/ViewTest.java
index 2bcb4e7..94aed3f 100644
--- a/tests/tests/view/src/android/view/cts/ViewTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTest.java
@@ -16,14 +16,9 @@
 
 package android.view.cts;
 
-import com.google.android.collect.Lists;
-
 import com.android.cts.stub.R;
 import com.android.internal.view.menu.ContextMenuBuilder;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import com.google.android.collect.Lists;
 
 import android.app.Activity;
 import android.content.Context;
@@ -80,6 +75,10 @@
 import android.widget.ListView;
 import android.widget.cts.StubActivity;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 /**
  * Test {@link View}.
  */
@@ -212,7 +211,12 @@
         });
         getInstrumentation().waitForIdleSync();
 
-        assertTrue(view.hasCalledOnAnimationStart());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return view.hasCalledOnAnimationStart();
+            }
+        }.run();
 
         // check whether it has ended after duration, and alpha changed during this time.
         new PollingCheck(duration + TIMEOUT_DELTA) {
@@ -2268,7 +2272,12 @@
             }
         });
         getInstrumentation().waitForIdleSync();
-        assertTrue(view.hasCalledOnDraw());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return view.hasCalledOnDraw();
+            }
+        }.run();
 
         view.reset();
         runTestOnUiThread(new Runnable() {
@@ -2300,7 +2309,12 @@
             }
         });
         getInstrumentation().waitForIdleSync();
-        assertTrue(view.hasCalledOnDraw());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return view.hasCalledOnDraw();
+            }
+        }.run();
 
         view.reset();
         runTestOnUiThread(new Runnable() {
@@ -2326,7 +2340,12 @@
             }
         });
         getInstrumentation().waitForIdleSync();
-        assertTrue(view.hasCalledOnDraw());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return view.hasCalledOnDraw();
+            }
+        }.run();
 
         view.reset();
         runTestOnUiThread(new Runnable() {
@@ -2352,7 +2371,12 @@
             }
         });
         getInstrumentation().waitForIdleSync();
-        assertTrue(view.hasCalledOnDraw());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return view.hasCalledOnDraw();
+            }
+        }.run();
 
         view.reset();
         runTestOnUiThread(new Runnable() {
@@ -3059,7 +3083,14 @@
         }.run();
 
         imm.showSoftInput(editText, 0);
-        assertTrue(editText.hasCalledOnCreateInputConnection());
+
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return editText.hasCalledOnCreateInputConnection();
+            }
+        }.run();
+
         assertTrue(editText.hasCalledOnCheckIsTextEditor());
         assertTrue(imm.isActive(editText));
 
diff --git a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
index 1dee2b3..5837290 100644
--- a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
@@ -21,6 +21,7 @@
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.app.cts.MockActivity;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.TouchUtils;
 import android.view.View;
@@ -118,7 +119,7 @@
 
         mViewTreeObserver = b.getViewTreeObserver();
 
-        MockOnTouchModeChangeListener listener = new MockOnTouchModeChangeListener();
+        final MockOnTouchModeChangeListener listener = new MockOnTouchModeChangeListener();
         assertFalse(listener.hasCalledOnTouchModeChanged());
         mViewTreeObserver.addOnTouchModeChangeListener(listener);
 
@@ -130,7 +131,12 @@
         });
         mInstrumentation.waitForIdleSync();
 
-        assertTrue(listener.hasCalledOnTouchModeChanged());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return listener.hasCalledOnTouchModeChanged();
+            }
+        }.run();
     }
 
     public void testAddOnComputeInternalInsetsListener() {
@@ -282,7 +288,7 @@
 
         mViewTreeObserver = scrollView.getViewTreeObserver();
 
-        MockOnScrollChangedListener listener = new MockOnScrollChangedListener();
+        final MockOnScrollChangedListener listener = new MockOnScrollChangedListener();
         assertFalse(listener.hasCalledOnScrollChanged());
         mViewTreeObserver.addOnScrollChangedListener(listener);
 
@@ -293,7 +299,12 @@
             }
         });
         mInstrumentation.waitForIdleSync();
-        assertTrue(listener.hasCalledOnScrollChanged());
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return listener.hasCalledOnScrollChanged();
+            }
+        }.run();
 
         listener.reset();
         assertFalse(listener.hasCalledOnScrollChanged());