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.
5 package org.chromium.android_webview.test;
7 import android.os.Message;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
12 import org.apache.http.util.EncodingUtils;
13 import org.chromium.android_webview.AwContents;
14 import org.chromium.base.test.util.DisabledTest;
15 import org.chromium.base.test.util.Feature;
16 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
17 import org.chromium.net.test.util.TestWebServer;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
23 * Tests if resubmission of post data is handled properly.
25 public class AwContentsClientOnFormResubmissionTest extends AwTestBase {
27 private static class TestAwContentsClient
28 extends org.chromium.android_webview.test.TestAwContentsClient {
30 // Number of times onFormResubmit is called.
31 private int mResubmissions = 0;
32 // Whether to resubmit Post data on reload.
33 private boolean mResubmit = false;
35 public int getResubmissions() {
36 return mResubmissions;
38 public void setResubmit(boolean resubmit) {
42 public void onFormResubmission(Message dontResend, Message resend) {
45 resend.sendToTarget();
47 dontResend.sendToTarget();
52 // Server responses for load and reload of posts.
53 private static final String LOAD_RESPONSE =
54 "<html><head><title>Load</title></head><body>HELLO</body></html>";
55 private static final String RELOAD_RESPONSE =
56 "<html><head><title>Reload</title></head><body>HELLO</body></html>";
58 // Server timeout in seconds. Used to detect dontResend case.
59 private static final long TIMEOUT = scaleTimeout(3);
62 private TestWebServer mServer;
64 private TestAwContentsClient mContentsClient;
65 private AwContents mAwContents;
68 public void setUp() throws Exception {
70 mServer = TestWebServer.start();
71 mContentsClient = new TestAwContentsClient();
72 final AwTestContainerView testContainerView =
73 createAwTestContainerViewOnMainSync(mContentsClient);
74 mAwContents = testContainerView.getAwContents();
78 public void tearDown() throws Exception {
85 @Feature({"AndroidWebView", "Navigation"})
88 public void testResend() throws Throwable {
89 mContentsClient.setResubmit(true);
91 assertEquals(1, mContentsClient.getResubmissions());
92 assertEquals("Reload", getTitleOnUiThread(mAwContents));
96 @Feature({"AndroidWebView", "Navigation"})
97 public void testDontResend() throws Throwable {
98 mContentsClient.setResubmit(false);
100 assertEquals(1, mContentsClient.getResubmissions());
101 assertEquals("Load", getTitleOnUiThread(mAwContents));
104 protected void doReload() throws Throwable {
105 String url = mServer.setResponse("/form", LOAD_RESPONSE, null);
106 String postData = "content=blabla";
107 byte[] data = EncodingUtils.getBytes(postData, "BASE64");
108 postUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url, data);
109 assertEquals(0, mContentsClient.getResubmissions());
110 assertEquals("Load", getTitleOnUiThread(mAwContents));
111 // Verify reload works as expected.
112 mServer.setResponse("/form", RELOAD_RESPONSE, null);
113 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
114 mContentsClient.getOnPageFinishedHelper();
115 int callCount = onPageFinishedHelper.getCallCount();
116 // Run reload on UI thread.
117 getInstrumentation().runOnMainSync(new Runnable() {
120 mAwContents.getNavigationController().reload(true);
124 // Wait for page finished callback, or a timeout. A timeout is necessary
125 // to detect a dontResend response.
126 onPageFinishedHelper.waitForCallback(callCount, 1, TIMEOUT, TimeUnit.SECONDS);
127 } catch (TimeoutException e) {
128 // Exception expected from testDontResend case.