Clean up WebView.addJavascriptInterface() CTS test

Cleans up unused code regarding updating the page title and uses
wait/notify to avoid a busy wait. Also uses 'new Object' for the
injected object where possible.

This will simplify later refactoring to call all WebView methods on
the UI thread.

Bug: 4340864
Change-Id: Ie88765351dfc711b22a661a3677bd4c08e6b0ad4
diff --git a/tests/assets/webkit/test_jsInterface.html b/tests/assets/webkit/test_jsInterface.html
index b7d7150..662dfe0 100644
--- a/tests/assets/webkit/test_jsInterface.html
+++ b/tests/assets/webkit/test_jsInterface.html
@@ -15,11 +15,7 @@
 
 <html>
     <title>Original title</title>
-    <script type="text/javascript">
-        /* This function is invoked by the activity */
-        function changeTitle(title) { document.title = title; }
-    </script>
     <!-- Calls into the javascript interface for the activity -->
-    <body onload='window.dummy.onLoad(document.title)'>
+    <body onload='window.dummy.provideResult(document.title)'>
     </body>
 </html>
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
index c973c5d..5b7a515 100755
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
@@ -66,6 +66,8 @@
 import java.io.File;
 import java.io.FileInputStream;
 
+import junit.framework.Assert;
+
 @TestTargetClass(android.webkit.WebView.class)
 public class WebViewTest extends ActivityInstrumentationTestCase2<WebViewStubActivity> {
     private static final String LOGTAG = "WebViewTest";
@@ -525,19 +527,43 @@
         settings.setJavaScriptEnabled(true);
         settings.setJavaScriptCanOpenWindowsAutomatically(true);
 
+        final class DummyJavaScriptInterface {
+            private boolean mWasProvideResultCalled;
+            private String mResult;
+
+            private synchronized String waitForResult() {
+                while (!mWasProvideResultCalled) {
+                    try {
+                        wait(TEST_TIMEOUT);
+                    } catch (InterruptedException e) {
+                        continue;
+                    }
+                    if (!mWasProvideResultCalled) {
+                        Assert.fail("Unexpected timeout");
+                    }
+                }
+                return mResult;
+            }
+
+            public synchronized boolean wasProvideResultCalled() {
+                return mWasProvideResultCalled;
+            }
+
+            public synchronized void provideResult(String result) {
+                mWasProvideResultCalled = true;
+                mResult = result;
+                notify();
+            }
+        }
+
         final DummyJavaScriptInterface obj = new DummyJavaScriptInterface();
         mWebView.addJavascriptInterface(obj, "dummy");
-        assertFalse(obj.hasChangedTitle());
+        assertFalse(obj.wasProvideResultCalled());
 
         startWebServer(false);
         String url = mWebServer.getAssetUrl(TestHtmlConstants.ADD_JAVA_SCRIPT_INTERFACE_URL);
         assertLoadUrlSuccessfully(url);
-        new DelayedCheck() {
-            @Override
-            protected boolean check() {
-                return obj.hasChangedTitle();
-            }
-        }.run();
+        assertEquals("Original title", obj.waitForResult());
     }
 
     @TestTargetNew(
@@ -563,7 +589,7 @@
         assertEquals("undefined", mWebView.getTitle());
 
         // Test that adding an object gives an object type.
-        final DummyJavaScriptInterface obj = new DummyJavaScriptInterface();
+        final Object obj = new Object();
         mWebView.addJavascriptInterface(obj, "injectedObject");
         mWebView.loadData(setTitleToPropertyTypeHtml, "text/html", "UTF-8");
         waitForLoadComplete();
@@ -591,7 +617,7 @@
     public void testAddJavascriptInterfaceOddName() throws Exception {
         WebSettings settings = mWebView.getSettings();
         settings.setJavaScriptEnabled(true);
-        final DummyJavaScriptInterface obj = new DummyJavaScriptInterface();
+        final Object obj = new Object();
 
         // We should be able to use any character other than a single quote.
         // TODO: We currently fail when the name contains '#', '\', '\n' or '\r'.
@@ -629,7 +655,7 @@
                 "<body onload=\"document.title = typeof window.injectedObject;\"></body></html>";
 
         // Test that adding an object gives an object type.
-        final DummyJavaScriptInterface obj = new DummyJavaScriptInterface();
+        final Object obj = new Object();
         mWebView.addJavascriptInterface(obj, "injectedObject");
         mWebView.loadData(setTitleToPropertyTypeHtml, "text/html", "UTF-8");
         waitForLoadComplete();
@@ -2226,23 +2252,6 @@
         }
     }
 
-    private final class DummyJavaScriptInterface {
-        private boolean mTitleChanged;
-
-        private boolean hasChangedTitle() {
-            return mTitleChanged;
-        }
-
-        public void onLoad(String oldTitle) {
-            mWebView.getHandler().post(new Runnable() {
-                public void run() {
-                    mWebView.loadUrl("javascript:changeTitle(\"new title\")");
-                    mTitleChanged = true;
-                }
-            });
-        }
-    }
-
     private final class MyDownloadListener implements DownloadListener {
         public String url;
         public String mimeType;