1 | // Copyright (c) 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. |
4 | |
5 | /** |
6 | * Test suite for displaying and functioning of modal dialogs. |
7 | */ |
8 | |
9 | package org.chromium.android_webview.test; |
10 | |
11 | import android.test.suitebuilder.annotation.MediumTest; |
12 | import android.test.suitebuilder.annotation.SmallTest; |
13 | |
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; |
19 | |
20 | import java.util.concurrent.atomic.AtomicBoolean; |
21 | |
22 | public class WebViewModalDialogOverrideTest extends AwTestBase { |
23 | private final static String EMPTY_PAGE = |
24 | "<!doctype html>" + |
25 | "<title>Modal Dialog Test</title><p>Testcase.</p>"; |
26 | private final static String BEFORE_UNLOAD_URL = |
27 | "<!doctype html>" + |
28 | "<head><script>window.onbeforeunload=function() {" + |
29 | "return 'Are you sure?';" + |
30 | "};</script></head></body>"; |
31 | |
32 | /* |
33 | * Verify that when the AwContentsClient calls handleJsAlert. |
34 | */ |
35 | @SmallTest |
36 | @Feature({"AndroidWebView"}) |
37 | public void testOverrideAlertHandling() throws Throwable { |
38 | final String ALERT_TEXT = "Hello World!"; |
39 | |
40 | final AtomicBoolean callbackCalled = new AtomicBoolean(false); |
41 | // Returning true from the callback should not show a dialog. |
42 | TestAwContentsClient client = new TestAwContentsClient() { |
43 | @Override |
44 | public void handleJsAlert(String url, String message, JsResultReceiver res) { |
45 | callbackCalled.set(true); |
46 | res.confirm(); |
47 | assertEquals(ALERT_TEXT, message); |
48 | } |
49 | }; |
50 | AwTestContainerView view = createAwTestContainerViewOnMainSync(client); |
51 | final AwContents awContents = view.getAwContents(); |
52 | |
53 | enableJavaScriptOnUiThread(awContents); |
54 | loadDataSync(awContents, client.getOnPageFinishedHelper(), |
55 | EMPTY_PAGE, "text/html", false); |
56 | executeJavaScriptAndWaitForResult(awContents, client, |
57 | "alert('" + ALERT_TEXT + "')"); |
58 | assertTrue(callbackCalled.get()); |
59 | } |
60 | |
61 | /* |
62 | * Verify that when the AwContentsClient calls handleJsPrompt. |
63 | */ |
64 | @SmallTest |
65 | @Feature({"AndroidWebView"}) |
66 | public void testOverridePromptHandling() throws Throwable { |
67 | final String PROMPT_TEXT = "How do you like your eggs in the morning?"; |
68 | final String PROMPT_DEFAULT = "Scrambled"; |
69 | final String PROMPT_RESULT = "I like mine with a kiss"; |
70 | |
71 | final AtomicBoolean called = new AtomicBoolean(false); |
72 | // Returning true from the callback should not show a dialog. |
73 | final TestAwContentsClient client = new TestAwContentsClient() { |
74 | @Override |
75 | public void handleJsPrompt(String url, String message, String defaultValue, |
76 | JsPromptResultReceiver res) { |
77 | assertEquals(PROMPT_TEXT, message); |
78 | assertEquals(PROMPT_DEFAULT, defaultValue); |
79 | res.confirm(PROMPT_RESULT); |
80 | called.set(true); |
81 | } |
82 | }; |
83 | AwTestContainerView view = createAwTestContainerViewOnMainSync(client); |
84 | final AwContents awContents = view.getAwContents(); |
85 | |
86 | enableJavaScriptOnUiThread(awContents); |
87 | loadDataSync(awContents, client.getOnPageFinishedHelper(), |
88 | EMPTY_PAGE, "text/html", false); |
89 | String result = executeJavaScriptAndWaitForResult(awContents, client, |
90 | "prompt('" + PROMPT_TEXT + "','" + PROMPT_DEFAULT + "')"); |
91 | assertTrue(called.get()); |
92 | assertEquals("\"" + PROMPT_RESULT + "\"", result); |
93 | } |
94 | |
95 | /* |
96 | * Verify that when the AwContentsClient calls handleJsConfirm and the client confirms. |
97 | */ |
98 | @SmallTest |
99 | @Feature({"AndroidWebView"}) |
100 | public void testOverrideConfirmHandlingConfirmed() throws Throwable { |
101 | final String CONFIRM_TEXT = "Would you like a cookie?"; |
102 | |
103 | final AtomicBoolean called = new AtomicBoolean(false); |
104 | // Returning true from the callback should not show a dialog. |
105 | TestAwContentsClient client = new TestAwContentsClient() { |
106 | @Override |
107 | public void handleJsConfirm(String url, String message, JsResultReceiver res) { |
108 | assertEquals(CONFIRM_TEXT, message); |
109 | res.confirm(); |
110 | called.set(true); |
111 | } |
112 | }; |
113 | AwTestContainerView view = createAwTestContainerViewOnMainSync(client); |
114 | final AwContents awContents = view.getAwContents(); |
115 | enableJavaScriptOnUiThread(awContents); |
116 | |
117 | loadDataSync(awContents, client.getOnPageFinishedHelper(), |
118 | EMPTY_PAGE, "text/html", false); |
119 | String result = executeJavaScriptAndWaitForResult(awContents, client, |
120 | "confirm('" + CONFIRM_TEXT + "')"); |
121 | assertTrue(called.get()); |
122 | assertEquals("true", result); |
123 | } |
124 | |
125 | /* |
126 | * Verify that when the AwContentsClient calls handleJsConfirm and the client cancels. |
127 | */ |
128 | @SmallTest |
129 | @Feature({"AndroidWebView"}) |
130 | public void testOverrideConfirmHandlingCancelled() throws Throwable { |
131 | final String CONFIRM_TEXT = "Would you like a cookie?"; |
132 | |
133 | final AtomicBoolean called = new AtomicBoolean(false); |
134 | // Returning true from the callback should not show a dialog. |
135 | TestAwContentsClient client = new TestAwContentsClient() { |
136 | @Override |
137 | public void handleJsConfirm(String url, String message, JsResultReceiver res) { |
138 | assertEquals(CONFIRM_TEXT, message); |
139 | res.cancel(); |
140 | called.set(true); |
141 | } |
142 | }; |
143 | AwTestContainerView view = createAwTestContainerViewOnMainSync(client); |
144 | final AwContents awContents = view.getAwContents(); |
145 | enableJavaScriptOnUiThread(awContents); |
146 | |
147 | loadDataSync(awContents, client.getOnPageFinishedHelper(), |
148 | EMPTY_PAGE, "text/html", false); |
149 | String result = executeJavaScriptAndWaitForResult(awContents, client, |
150 | "confirm('" + CONFIRM_TEXT + "')"); |
151 | assertTrue(called.get()); |
152 | assertEquals("false", result); |
153 | } |
154 | |
155 | /* |
156 | * Verify that when the AwContentsClient calls handleJsBeforeUnload |
157 | */ |
158 | @MediumTest |
159 | @Feature({"AndroidWebView"}) |
160 | public void testOverrideBeforeUnloadHandling() throws Throwable { |
161 | final CallbackHelper jsBeforeUnloadHelper = new CallbackHelper(); |
162 | TestAwContentsClient client = new TestAwContentsClient() { |
163 | @Override |
164 | public void handleJsBeforeUnload(String url, String message, JsResultReceiver res) { |
165 | res.cancel(); |
166 | jsBeforeUnloadHelper.notifyCalled(); |
167 | } |
168 | }; |
169 | AwTestContainerView view = createAwTestContainerViewOnMainSync(client); |
170 | final AwContents awContents = view.getAwContents(); |
171 | enableJavaScriptOnUiThread(awContents); |
172 | |
173 | loadDataSync(awContents, client.getOnPageFinishedHelper(), BEFORE_UNLOAD_URL, |
174 | "text/html", false); |
175 | enableJavaScriptOnUiThread(awContents); |
176 | |
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); |
181 | } |
182 | } |