Fix testPageScroll by not using waitForIdleSync.

BUG: 20113398

Change-Id: Ib873b96862fa3dfcc47587c6fc58e1543d8af10d
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
index 1e22acc..b381d72 100755
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
@@ -1557,6 +1557,37 @@
         assertEquals(1, handler.getMsgArg1());
     }
 
+    private static void waitForFlingDone(WebViewOnUiThread webview) {
+        class ScrollDiffPollingCheck extends PollingCheck {
+            private static final long TIME_SLICE = 50;
+            WebViewOnUiThread mWebView;
+            private int mScrollX;
+            private int mScrollY;
+
+            ScrollDiffPollingCheck(WebViewOnUiThread webview) {
+                mWebView = webview;
+                mScrollX = mWebView.getScrollX();
+                mScrollY = mWebView.getScrollY();
+            }
+
+            @Override
+            protected boolean check() {
+                try {
+                    Thread.sleep(TIME_SLICE);
+                } catch (InterruptedException e) {
+                    // Intentionally ignored.
+                }
+                int newScrollX = mWebView.getScrollX();
+                int newScrollY = mWebView.getScrollY();
+                boolean flingDone = newScrollX == mScrollX && newScrollY == mScrollY;
+                mScrollX = newScrollX;
+                mScrollY = newScrollY;
+                return flingDone;
+            }
+        }
+        new ScrollDiffPollingCheck(webview).run();
+    }
+
     public void testPageScroll() throws Throwable {
         if (!NullWebViewUtils.isWebViewAvailable()) {
             return;
@@ -1578,29 +1609,29 @@
         }.run();
 
         do {
-            getInstrumentation().waitForIdleSync();
+            waitForFlingDone(mOnUiThread);
         } while (mOnUiThread.pageDown(false));
 
-        getInstrumentation().waitForIdleSync();
+        waitForFlingDone(mOnUiThread);
         int bottomScrollY = mOnUiThread.getScrollY();
 
         assertTrue(mOnUiThread.pageUp(false));
 
         do {
-            getInstrumentation().waitForIdleSync();
+            waitForFlingDone(mOnUiThread);
         } while (mOnUiThread.pageUp(false));
 
-        getInstrumentation().waitForIdleSync();
+        waitForFlingDone(mOnUiThread);
         int topScrollY = mOnUiThread.getScrollY();
 
         // jump to the bottom
         assertTrue(mOnUiThread.pageDown(true));
-        getInstrumentation().waitForIdleSync();
+        waitForFlingDone(mOnUiThread);
         assertEquals(bottomScrollY, mOnUiThread.getScrollY());
 
         // jump to the top
         assertTrue(mOnUiThread.pageUp(true));
-        getInstrumentation().waitForIdleSync();
+        waitForFlingDone(mOnUiThread);
         assertEquals(topScrollY, mOnUiThread.getScrollY());
     }