diff options
author | Shimi Zhang <ctzsm@google.com> | 2019-02-26 16:39:18 -0800 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-02-26 16:39:18 -0800 |
commit | 26a9c05db28ddb1934f3ec7c38b42a06f89da9d3 (patch) | |
tree | 6be5a522b0982630fa39624c191561e2994234f3 | |
parent | b5c6644d68726e5b806c5c80e5976ada74453827 (diff) | |
parent | fb52c11d4ec83e68f2e328ac536bbc401ecf4085 (diff) | |
download | cts-26a9c05db28ddb1934f3ec7c38b42a06f89da9d3.tar.gz |
Merge "Merge "resolve merge conflicts of a00b291c5a4e9ba6058e2fb699ea575fce406574 to oreo-cts-dev" into oreo-cts-dev am: ba543c5ad0" into oreo-mr1-cts-dev
am: fb52c11d4e
Change-Id: If631b5a0f449827eb1260174db220fe138d02192
-rw-r--r-- | tests/tests/webkit/assets/webkit/jsunload.html | 8 | ||||
-rw-r--r-- | tests/tests/webkit/res/layout/webview_layout.xml | 6 | ||||
-rw-r--r-- | tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java | 55 |
3 files changed, 57 insertions, 12 deletions
diff --git a/tests/tests/webkit/assets/webkit/jsunload.html b/tests/tests/webkit/assets/webkit/jsunload.html index f016eb7b744..9932ae10d33 100644 --- a/tests/tests/webkit/assets/webkit/jsunload.html +++ b/tests/tests/webkit/assets/webkit/jsunload.html @@ -22,9 +22,15 @@ return "this message will be a hardcoded string in chrome."; } window.onbeforeunload = fireUnload; + window.onload = function() { + document.addEventListener("touchend", function() { + setTimeout(() => { + document.title = "touch received"; + }, 0); + }, false); + } </script> <body> javascript unload test </body> </html> - diff --git a/tests/tests/webkit/res/layout/webview_layout.xml b/tests/tests/webkit/res/layout/webview_layout.xml index 7a0ed0d1fa1..d266d21305b 100644 --- a/tests/tests/webkit/res/layout/webview_layout.xml +++ b/tests/tests/webkit/res/layout/webview_layout.xml @@ -17,9 +17,9 @@ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="match_parent"> <WebView android:id="@+id/web_page" - android:layout_width="wrap_content" - android:layout_height="wrap_content" /> + android:layout_width="match_parent" + android:layout_height="match_parent" /> </LinearLayout> diff --git a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java index 92573d873e9..60c9d25a2c8 100644 --- a/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java +++ b/tests/tests/webkit/src/android/webkit/cts/WebChromeClientTest.java @@ -31,9 +31,15 @@ import android.webkit.cts.WebViewOnUiThread.WaitForProgressClient; import com.android.compatibility.common.util.NullWebViewUtils; import com.android.compatibility.common.util.PollingCheck; +import com.google.common.util.concurrent.SettableFuture; + +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 TOUCH_RECEIVED = "touch received"; private CtsTestServer mWebServer; private WebIconDatabase mIconDb; @@ -201,11 +207,13 @@ public class WebChromeClientTest extends ActivityInstrumentationTestCase2<WebVie runWindowTest(false); } + // Note that test is still a little flaky. See b/119468441. public void testOnJsBeforeUnloadIsCalled() throws Exception { if (!NullWebViewUtils.isWebViewAvailable()) { return; } + // Use a default WebChromeClient to listen first page title change. final MockWebChromeClient webChromeClient = new MockWebChromeClient(); mOnUiThread.setWebChromeClient(webChromeClient); @@ -215,20 +223,37 @@ public class WebChromeClientTest extends ActivityInstrumentationTestCase2<WebVie assertFalse(webChromeClient.hadOnJsBeforeUnload()); - mOnUiThread.loadUrlAndWaitForCompletion(mWebServer.getAssetUrl(TestHtmlConstants.JS_UNLOAD_URL)); + mOnUiThread.loadUrlAndWaitForCompletion( + mWebServer.getAssetUrl(TestHtmlConstants.JS_UNLOAD_URL)); + + final SettableFuture<String> pageTitleFuture = SettableFuture.create(); + 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); + } + + @Override + public boolean onJsBeforeUnload( + WebView view, String url, String message, JsResult result) { + boolean ret = super.onJsBeforeUnload(view, url, message, result); + onJsBeforeUnloadFuture.set(null); + return ret; + } + }; + mOnUiThread.setWebChromeClient(webChromeClientWaitTitle); // Send a user gesture, required for unload to execute since WebView version 60. tapWebView(); + assertEquals(TOUCH_RECEIVED, waitForFuture(pageTitleFuture)); // unload should trigger when we try to navigate away - mOnUiThread.loadUrlAndWaitForCompletion(mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL)); + mOnUiThread.loadUrlAndWaitForCompletion( + mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL)); - new PollingCheck(TEST_TIMEOUT) { - @Override - protected boolean check() { - return webChromeClient.hadOnJsBeforeUnload(); - } - }.run(); + waitForFuture(onJsBeforeUnloadFuture); } public void testOnJsAlert() throws Exception { @@ -337,6 +362,20 @@ public class WebChromeClientTest extends ActivityInstrumentationTestCase2<WebVie getInstrumentation().waitForIdleSync(); } + // TODO(ctzsm): Remove this method and replace its usage when we have it in a util class. + private static <T> T waitForFuture(Future<T> future) throws Exception { + try { + return future.get(TEST_TIMEOUT, TimeUnit.MILLISECONDS); + } catch (ExecutionException e) { + Throwable cause = e.getCause(); + if (cause instanceof Error) + throw(Error) cause; + if (cause instanceof RuntimeException) + throw(RuntimeException) cause; + throw new RuntimeException(cause); + } + } + private class MockWebChromeClient extends WaitForProgressClient { private boolean mHadOnProgressChanged; private boolean mHadOnReceivedTitle; |