[webview] fix testOnJsBeforeUnloadIsCalled flakes

The flakiness appears to have two causes. Firstly, it was possible to
miss the initial page title update because the WebChromeClient was set
after the title change. Secondly, sometimes the synthesised touch events
get turned into a click rather than a touchend.

Fixed so as to:
* Guarantee ordering by setting the WebChromeClient before page load,
  and observing the sequence title changes.
* Trigger on click rather than touchend.

Fixes: 123043623
Test: WebChromeClientTest#testOnJsBeforeUnloadIsCalled
Change-Id: I5d66ebed8b32be54b7267c1e947efc99cb94e04f
diff --git a/tests/tests/webkit/assets/webkit/jsunload.html b/tests/tests/webkit/assets/webkit/jsunload.html
index 9932ae1..593ecc9 100644
--- a/tests/tests/webkit/assets/webkit/jsunload.html
+++ b/tests/tests/webkit/assets/webkit/jsunload.html
@@ -23,11 +23,8 @@
         }
         window.onbeforeunload = fireUnload;
         window.onload = function() {
-            document.addEventListener("touchend", function() {
-                setTimeout(() => {
-                    document.title = "touch received";
-                }, 0);
-            }, false);
+            document.addEventListener("click", () => document.title = "touch received", false);
+            document.title = "listener added";
         }
     </script>
     <body>
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
index 94ebe66..5e133ea 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
@@ -33,12 +33,16 @@
 
 import com.google.common.util.concurrent.SettableFuture;
 
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 public class WebChromeClientTest extends ActivityInstrumentationTestCase2<WebViewCtsActivity> {
     private static final long TEST_TIMEOUT = 5000L;
+    private static final String JAVASCRIPT_UNLOAD = "javascript unload";
+    private static final String LISTENER_ADDED = "listener added";
     private static final String TOUCH_RECEIVED = "touch received";
 
     private CtsTestServer mWebServer;
@@ -213,26 +217,17 @@
             return;
         }
 
-        // Use a default WebChromeClient to listen first page title change.
-        final MockWebChromeClient webChromeClient = new MockWebChromeClient();
-        mOnUiThread.setWebChromeClient(webChromeClient);
-
         final WebSettings settings = mOnUiThread.getSettings();
         settings.setJavaScriptEnabled(true);
         settings.setJavaScriptCanOpenWindowsAutomatically(true);
 
-        assertFalse(webChromeClient.hadOnJsBeforeUnload());
-
-        mOnUiThread.loadUrlAndWaitForCompletion(
-            mWebServer.getAssetUrl(TestHtmlConstants.JS_UNLOAD_URL));
-
-        final SettableFuture<String> pageTitleFuture = SettableFuture.create();
+        final BlockingQueue<String> pageTitleQueue = new ArrayBlockingQueue<>(3);
         final SettableFuture<Void> onJsBeforeUnloadFuture = SettableFuture.create();
         final MockWebChromeClient webChromeClientWaitTitle = new MockWebChromeClient() {
             @Override
             public void onReceivedTitle(WebView view, String title) {
                 super.onReceivedTitle(view, title);
-                pageTitleFuture.set(title);
+                pageTitleQueue.add(title);
             }
 
             @Override
@@ -245,9 +240,14 @@
         };
         mOnUiThread.setWebChromeClient(webChromeClientWaitTitle);
 
+        mOnUiThread.loadUrlAndWaitForCompletion(
+            mWebServer.getAssetUrl(TestHtmlConstants.JS_UNLOAD_URL));
+
+        assertEquals(JAVASCRIPT_UNLOAD, pageTitleQueue.poll(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
+        assertEquals(LISTENER_ADDED, pageTitleQueue.poll(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
         // Send a user gesture, required for unload to execute since WebView version 60.
         tapWebView();
-        assertEquals(TOUCH_RECEIVED, waitForFuture(pageTitleFuture));
+        assertEquals(TOUCH_RECEIVED, pageTitleQueue.poll(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
 
         // unload should trigger when we try to navigate away
         mOnUiThread.loadUrlAndWaitForCompletion(