1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Test suite for displaying and functioning of modal dialogs.
9 package org.chromium.android_webview.test;
11 import android.test.suitebuilder.annotation.MediumTest;
12 import android.test.suitebuilder.annotation.SmallTest;
14 import org.chromium.android_webview.AwContents;
15 import org.chromium.android_webview.JsPromptResultReceiver;
16 import org.chromium.android_webview.JsResultReceiver;
17 import org.chromium.base.test.util.Feature;
18 import org.chromium.content.browser.test.util.CallbackHelper;
20 import java.util.concurrent.atomic.AtomicBoolean;
22 public class WebViewModalDialogOverrideTest extends AwTestBase {
23 private static final String EMPTY_PAGE =
25 "<title>Modal Dialog Test</title><p>Testcase.</p>";
26 private static final String BEFORE_UNLOAD_URL =
28 "<head><script>window.onbeforeunload=function() {" +
29 "return 'Are you sure?';" +
30 "};</script></head></body>";
33 * Verify that when the AwContentsClient calls handleJsAlert.
36 @Feature({"AndroidWebView"})
37 public void testOverrideAlertHandling() throws Throwable {
38 final String alertText = "Hello World!";
40 final AtomicBoolean callbackCalled = new AtomicBoolean(false);
41 // Returning true from the callback should not show a dialog.
42 TestAwContentsClient client = new TestAwContentsClient() {
44 public void handleJsAlert(String url, String message, JsResultReceiver res) {
45 callbackCalled.set(true);
47 assertEquals(alertText, message);
50 AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
51 final AwContents awContents = view.getAwContents();
53 enableJavaScriptOnUiThread(awContents);
54 loadDataSync(awContents, client.getOnPageFinishedHelper(),
55 EMPTY_PAGE, "text/html", false);
56 executeJavaScriptAndWaitForResult(awContents, client,
57 "alert('" + alertText + "')");
58 assertTrue(callbackCalled.get());
62 * Verify that when the AwContentsClient calls handleJsPrompt.
65 @Feature({"AndroidWebView"})
66 public void testOverridePromptHandling() throws Throwable {
67 final String promtText = "How do you like your eggs in the morning?";
68 final String promptDefault = "Scrambled";
69 final String promptResult = "I like mine with a kiss";
71 final AtomicBoolean called = new AtomicBoolean(false);
72 // Returning true from the callback should not show a dialog.
73 final TestAwContentsClient client = new TestAwContentsClient() {
75 public void handleJsPrompt(String url, String message, String defaultValue,
76 JsPromptResultReceiver res) {
77 assertEquals(promtText, message);
78 assertEquals(promptDefault, defaultValue);
79 res.confirm(promptResult);
83 AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
84 final AwContents awContents = view.getAwContents();
86 enableJavaScriptOnUiThread(awContents);
87 loadDataSync(awContents, client.getOnPageFinishedHelper(),
88 EMPTY_PAGE, "text/html", false);
89 String result = executeJavaScriptAndWaitForResult(awContents, client,
90 "prompt('" + promtText + "','" + promptDefault + "')");
91 assertTrue(called.get());
92 assertEquals("\"" + promptResult + "\"", result);
96 * Verify that when the AwContentsClient calls handleJsConfirm and the client confirms.
99 @Feature({"AndroidWebView"})
100 public void testOverrideConfirmHandlingConfirmed() throws Throwable {
101 final String confirmText = "Would you like a cookie?";
103 final AtomicBoolean called = new AtomicBoolean(false);
104 // Returning true from the callback should not show a dialog.
105 TestAwContentsClient client = new TestAwContentsClient() {
107 public void handleJsConfirm(String url, String message, JsResultReceiver res) {
108 assertEquals(confirmText, message);
113 AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
114 final AwContents awContents = view.getAwContents();
115 enableJavaScriptOnUiThread(awContents);
117 loadDataSync(awContents, client.getOnPageFinishedHelper(),
118 EMPTY_PAGE, "text/html", false);
119 String result = executeJavaScriptAndWaitForResult(awContents, client,
120 "confirm('" + confirmText + "')");
121 assertTrue(called.get());
122 assertEquals("true", result);
126 * Verify that when the AwContentsClient calls handleJsConfirm and the client cancels.
129 @Feature({"AndroidWebView"})
130 public void testOverrideConfirmHandlingCancelled() throws Throwable {
131 final String confirmText = "Would you like a cookie?";
133 final AtomicBoolean called = new AtomicBoolean(false);
134 // Returning true from the callback should not show a dialog.
135 TestAwContentsClient client = new TestAwContentsClient() {
137 public void handleJsConfirm(String url, String message, JsResultReceiver res) {
138 assertEquals(confirmText, message);
143 AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
144 final AwContents awContents = view.getAwContents();
145 enableJavaScriptOnUiThread(awContents);
147 loadDataSync(awContents, client.getOnPageFinishedHelper(),
148 EMPTY_PAGE, "text/html", false);
149 String result = executeJavaScriptAndWaitForResult(awContents, client,
150 "confirm('" + confirmText + "')");
151 assertTrue(called.get());
152 assertEquals("false", result);
156 * Verify that when the AwContentsClient calls handleJsBeforeUnload
159 @Feature({"AndroidWebView"})
160 public void testOverrideBeforeUnloadHandling() throws Throwable {
161 final CallbackHelper jsBeforeUnloadHelper = new CallbackHelper();
162 TestAwContentsClient client = new TestAwContentsClient() {
164 public void handleJsBeforeUnload(String url, String message, JsResultReceiver res) {
166 jsBeforeUnloadHelper.notifyCalled();
169 AwTestContainerView view = createAwTestContainerViewOnMainSync(client);
170 final AwContents awContents = view.getAwContents();
171 enableJavaScriptOnUiThread(awContents);
173 loadDataSync(awContents, client.getOnPageFinishedHelper(), BEFORE_UNLOAD_URL,
175 enableJavaScriptOnUiThread(awContents);
177 // Don't wait synchronously because we don't leave the page.
178 int currentCallCount = jsBeforeUnloadHelper.getCallCount();
179 loadDataAsync(awContents, EMPTY_PAGE, "text/html", false);
180 jsBeforeUnloadHelper.waitForCallback(currentCallCount);