Fix WebChromeClientTest#testOnJsBeforeUnloadIsCalled CTS test.
Fix WebChromeClientTest#testOnJsBeforeUnloadIsCalled which is
failing for WebView 60+.
This patch addresses the requirement that has been introduced
in WebView 60: there should be a user gesture in order for
beforeunload to work.
Bug: 64152248
Crbug: 757007
Test: testOnJsBeforeUnloadIsCalled passes
cts-tradefed run cts -m CtsWebkitTestCases --test android.webkit.cts.WebChromeClientTest#testOnJsBeforeUnloadIsCalled
Change-Id: Icaf0e4d97f931737f7a67809ddd4599b31bdbe9a
(cherry picked from commit 7358a7582bf8a1c58d9fb0f687e862a39d89c593)
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
index 424d856..d141eda 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java
@@ -20,7 +20,9 @@
import android.cts.util.PollingCheck;
import android.graphics.Bitmap;
import android.os.Message;
+import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
+import android.view.MotionEvent;
import android.view.ViewGroup;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
@@ -202,6 +204,7 @@
if (!NullWebViewUtils.isWebViewAvailable()) {
return;
}
+
final MockWebChromeClient webChromeClient = new MockWebChromeClient();
mOnUiThread.setWebChromeClient(webChromeClient);
@@ -212,6 +215,10 @@
assertFalse(webChromeClient.hadOnJsBeforeUnload());
mOnUiThread.loadUrlAndWaitForCompletion(mWebServer.getAssetUrl(TestHtmlConstants.JS_UNLOAD_URL));
+
+ // Send a user gesture, required for unload to execute since WebView version 60.
+ tapWebView();
+
// unload should trigger when we try to navigate away
mOnUiThread.loadUrlAndWaitForCompletion(mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL));
@@ -307,6 +314,28 @@
assertEquals(webChromeClient.getMessage(), "testOnJsPrompt");
}
+ /**
+ * Taps in the the center of a webview.
+ */
+ private void tapWebView() {
+ int[] location = mOnUiThread.getLocationOnScreen();
+ int middleX = location[0] + mOnUiThread.getWebView().getWidth() / 2;
+ int middleY = location[1] + mOnUiThread.getWebView().getHeight() / 2;
+
+ long timeDown = SystemClock.uptimeMillis();
+ getInstrumentation().sendPointerSync(
+ MotionEvent.obtain(timeDown, timeDown, MotionEvent.ACTION_DOWN,
+ middleX, middleY, 0));
+
+ long timeUp = SystemClock.uptimeMillis();
+ getInstrumentation().sendPointerSync(
+ MotionEvent.obtain(timeUp, timeUp, MotionEvent.ACTION_UP,
+ middleX, middleY, 0));
+
+ // Wait for the system to process all events in the queue
+ getInstrumentation().waitForIdleSync();
+ }
+
private class MockWebChromeClient extends WaitForProgressClient {
private boolean mHadOnProgressChanged;
private boolean mHadOnReceivedTitle;