Fix the calls to WebView from non-UI thread in test app

WebView is complaining that some calls used in test app is not from
UI thread.

Change-Id: I7ef61694e3988a663eeedf03463580a93e57fe46
diff --git a/tests/src/com/android/browser/PopularUrlsTest.java b/tests/src/com/android/browser/PopularUrlsTest.java
index f1ab71b..85dc4a3 100644
--- a/tests/src/com/android/browser/PopularUrlsTest.java
+++ b/tests/src/com/android/browser/PopularUrlsTest.java
@@ -458,7 +458,7 @@
 
         while (mStatus.getIteration() < loopCount) {
             if (clearCache) {
-                webView.clearCache(true);
+                clearCacheUiThread(webView, true);
             }
             while(iterator.hasNext()) {
                 page = iterator.next();
@@ -480,7 +480,7 @@
                 waitForLoad();
                 long stopTime = System.currentTimeMillis();
 
-                String url = webView.getUrl();
+                String url = getUrlUiThread(webView);
                 Log.i(TAG, "finish: " + url);
 
                 if (writer != null) {
@@ -534,4 +534,44 @@
             }
         }
     }
+
+    private void clearCacheUiThread(final WebView webView, final boolean includeDiskFiles) {
+        Runnable runner = new Runnable() {
+
+            @Override
+            public void run() {
+                webView.clearCache(includeDiskFiles);
+            }
+        };
+        getInstrumentation().runOnMainSync(runner);
+    }
+
+    private String getUrlUiThread(final WebView webView) {
+        WebViewUrlGetter urlGetter = new WebViewUrlGetter(webView);
+        getInstrumentation().runOnMainSync(urlGetter);
+        return urlGetter.getUrl();
+    }
+
+    private class WebViewUrlGetter implements Runnable {
+
+        private WebView mWebView;
+        private String mUrl;
+
+        public WebViewUrlGetter(WebView webView) {
+            mWebView = webView;
+        }
+
+        @Override
+        public void run() {
+                mUrl = null;
+                mUrl = mWebView.getUrl();
+        }
+
+        public String getUrl() {
+            if (mUrl != null) {
+                return mUrl;
+            } else
+                throw new IllegalStateException("url has not been fetched yet");
+        }
+    }
 }