| Index: android_webview/javatests/src/org/chromium/android_webview/test/WebViewModalDialogOverrideTest.java
|
| diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/WebViewModalDialogOverrideTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/WebViewModalDialogOverrideTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..473f8f1c0b68d49637fd070761ecb28d895db5c2
|
| --- /dev/null
|
| +++ b/android_webview/javatests/src/org/chromium/android_webview/test/WebViewModalDialogOverrideTest.java
|
| @@ -0,0 +1,185 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +/**
|
| + * Test suite for displaying and functioning of modal dialogs.
|
| + */
|
| +
|
| +package org.chromium.android_webview.test;
|
| +
|
| +import android.test.suitebuilder.annotation.MediumTest;
|
| +import android.test.suitebuilder.annotation.SmallTest;
|
| +
|
| +import junit.framework.Assert;
|
| +
|
| +import org.chromium.android_webview.JsPromptResultReceiver;
|
| +import org.chromium.android_webview.JsResultReceiver;
|
| +import org.chromium.base.test.Feature;
|
| +import org.chromium.content.browser.ContentViewCore;
|
| +import org.chromium.content.browser.util.CallbackHelper;
|
| +
|
| +import java.util.concurrent.atomic.AtomicBoolean;
|
| +import java.util.concurrent.atomic.AtomicInteger;
|
| +
|
| +public class WebViewModalDialogOverrideTest extends AndroidWebViewTestBase {
|
| + private final static String EMPTY_PAGE =
|
| + "<!doctype html>" +
|
| + "<title>Modal Dialog Test</title><p>Testcase.</p>";
|
| + private final static String BEFORE_UNLOAD_URL =
|
| + "<!doctype html>" +
|
| + "<head><script>window.onbeforeunload=function() {" +
|
| + "return 'Are you sure?';" +
|
| + "};</script></head></body>";
|
| +
|
| + /*
|
| + * Verify that when the AwContentsClient calls handleJsAlert.
|
| + */
|
| + @SmallTest
|
| + @Feature({"Android-WebView"})
|
| + public void testOverrideAlertHandling() throws Throwable {
|
| + final String ALERT_TEXT = "Hello World!";
|
| +
|
| + final AtomicBoolean callbackCalled = new AtomicBoolean(false);
|
| + // Returning true from the callback should not show a dialog.
|
| + TestAwContentsClient client = new TestAwContentsClient() {
|
| + @Override
|
| + public void handleJsAlert(String url, String message, JsResultReceiver res) {
|
| + callbackCalled.set(true);
|
| + res.confirm();
|
| + assertEquals(ALERT_TEXT, message);
|
| + }
|
| + };
|
| + AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
|
| + final ContentViewCore contentViewCore = view.getContentViewCore();
|
| +
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| + loadDataSync(contentViewCore, client.getOnPageFinishedHelper(),
|
| + EMPTY_PAGE, "text/html", false);
|
| + executeJavaScriptAndWaitForResult(contentViewCore, client,
|
| + "alert('" + ALERT_TEXT + "')");
|
| + assertTrue(callbackCalled.get());
|
| + }
|
| +
|
| + /*
|
| + * Verify that when the AwContentsClient calls handleJsPrompt.
|
| + */
|
| + @SmallTest
|
| + @Feature({"Android-WebView"})
|
| + public void testOverridePromptHandling() throws Throwable {
|
| + final String PROMPT_TEXT = "How do you like your eggs in the morning?";
|
| + final String PROMPT_DEFAULT = "Scrambled";
|
| + final String PROMPT_RESULT = "I like mine with a kiss";
|
| +
|
| + final AtomicBoolean called = new AtomicBoolean(false);
|
| + // Returning true from the callback should not show a dialog.
|
| + final TestAwContentsClient client = new TestAwContentsClient() {
|
| + @Override
|
| + public void handleJsPrompt(String url, String message, String defaultValue,
|
| + JsPromptResultReceiver res) {
|
| + assertEquals(PROMPT_TEXT, message);
|
| + assertEquals(PROMPT_DEFAULT, defaultValue);
|
| + res.confirm(PROMPT_RESULT);
|
| + called.set(true);
|
| + }
|
| + };
|
| + AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
|
| + ContentViewCore contentViewCore = view.getContentViewCore();
|
| +
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| + loadDataSync(contentViewCore, client.getOnPageFinishedHelper(),
|
| + EMPTY_PAGE, "text/html", false);
|
| + String result = executeJavaScriptAndWaitForResult(contentViewCore, client,
|
| + "prompt('" + PROMPT_TEXT + "','" + PROMPT_DEFAULT + "')");
|
| + assertTrue(called.get());
|
| + assertEquals("\"" + PROMPT_RESULT + "\"", result);
|
| + }
|
| +
|
| + /*
|
| + * Verify that when the AwContentsClient calls handleJsConfirm and the client confirms.
|
| + */
|
| + @SmallTest
|
| + @Feature({"Android-WebView"})
|
| + public void testOverrideConfirmHandlingConfirmed() throws Throwable {
|
| + final String CONFIRM_TEXT = "Would you like a cookie?";
|
| +
|
| + final AtomicBoolean called = new AtomicBoolean(false);
|
| + // Returning true from the callback should not show a dialog.
|
| + TestAwContentsClient client = new TestAwContentsClient() {
|
| + @Override
|
| + public void handleJsConfirm(String url, String message, JsResultReceiver res) {
|
| + assertEquals(CONFIRM_TEXT, message);
|
| + res.confirm();
|
| + called.set(true);
|
| + }
|
| + };
|
| + AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
|
| + ContentViewCore contentViewCore = view.getContentViewCore();
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| +
|
| + loadDataSync(contentViewCore, client.getOnPageFinishedHelper(),
|
| + EMPTY_PAGE, "text/html", false);
|
| + String result = executeJavaScriptAndWaitForResult(contentViewCore, client,
|
| + "confirm('" + CONFIRM_TEXT + "')");
|
| + assertTrue(called.get());
|
| + assertEquals("true", result);
|
| + }
|
| +
|
| + /*
|
| + * Verify that when the AwContentsClient calls handleJsConfirm and the client cancels.
|
| + */
|
| + @SmallTest
|
| + @Feature({"Android-WebView"})
|
| + public void testOverrideConfirmHandlingCancelled() throws Throwable {
|
| + final String CONFIRM_TEXT = "Would you like a cookie?";
|
| +
|
| + final AtomicBoolean called = new AtomicBoolean(false);
|
| + // Returning true from the callback should not show a dialog.
|
| + TestAwContentsClient client = new TestAwContentsClient() {
|
| + @Override
|
| + public void handleJsConfirm(String url, String message, JsResultReceiver res) {
|
| + assertEquals(CONFIRM_TEXT, message);
|
| + res.cancel();
|
| + called.set(true);
|
| + }
|
| + };
|
| + AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
|
| + ContentViewCore contentViewCore = view.getContentViewCore();
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| +
|
| + loadDataSync(contentViewCore, client.getOnPageFinishedHelper(),
|
| + EMPTY_PAGE, "text/html", false);
|
| + String result = executeJavaScriptAndWaitForResult(contentViewCore, client,
|
| + "confirm('" + CONFIRM_TEXT + "')");
|
| + assertTrue(called.get());
|
| + assertEquals("false", result);
|
| + }
|
| +
|
| + /*
|
| + * Verify that when the AwContentsClient calls handleJsBeforeUnload
|
| + */
|
| + @MediumTest
|
| + @Feature({"Android-WebView"})
|
| + public void testOverrideBeforeUnloadHandling() throws Throwable {
|
| + final CallbackHelper jsBeforeUnloadHelper = new CallbackHelper();
|
| + TestAwContentsClient client = new TestAwContentsClient() {
|
| + @Override
|
| + public void handleJsBeforeUnload(String url, String message, JsResultReceiver res) {
|
| + res.cancel();
|
| + jsBeforeUnloadHelper.notifyCalled();
|
| + }
|
| + };
|
| + AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
|
| + ContentViewCore contentViewCore = view.getContentViewCore();
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| +
|
| + loadDataSync(contentViewCore, client.getOnPageFinishedHelper(), BEFORE_UNLOAD_URL,
|
| + "text/html", false);
|
| + enableJavaScriptOnUiThread(contentViewCore);
|
| +
|
| + // Don't wait synchronously because we don't leave the page.
|
| + int currentCallCount = jsBeforeUnloadHelper.getCallCount();
|
| + loadDataAsync(contentViewCore, EMPTY_PAGE, "text/html", false);
|
| + jsBeforeUnloadHelper.waitForCallback(currentCallCount);
|
| + }
|
| +}
|
|
|