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 | package org.chromium.android_webview.test; |
6 | |
7 | import android.content.Context; |
8 | import android.os.Message; |
9 | import android.test.suitebuilder.annotation.SmallTest; |
10 | |
11 | import org.apache.http.util.EncodingUtils; |
12 | import org.chromium.android_webview.AwContents; |
13 | import org.chromium.base.test.util.DisabledTest; |
14 | import org.chromium.base.test.util.Feature; |
15 | import org.chromium.content.browser.ContentViewCore; |
16 | import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
17 | import org.chromium.net.test.util.TestWebServer; |
18 | |
19 | import java.util.concurrent.atomic.AtomicReference; |
20 | import java.util.concurrent.TimeUnit; |
21 | import java.util.concurrent.TimeoutException; |
22 | |
23 | /** |
24 | * Tests if resubmission of post data is handled properly. |
25 | */ |
26 | public class AwContentsClientOnFormResubmissionTest extends AwTestBase { |
27 | |
28 | private static class TestAwContentsClient |
29 | extends org.chromium.android_webview.test.TestAwContentsClient { |
30 | |
31 | // Number of times onFormResubmit is called. |
32 | private int mResubmissions = 0; |
33 | // Whether to resubmit Post data on reload. |
34 | private boolean mResubmit = false; |
35 | |
36 | public int getResubmissions() { |
37 | return mResubmissions; |
38 | } |
39 | public void setResubmit(boolean resubmit) { |
40 | mResubmit = resubmit; |
41 | } |
42 | @Override |
43 | public void onFormResubmission(Message dontResend, Message resend) { |
44 | mResubmissions++; |
45 | if (mResubmit) { |
46 | resend.sendToTarget(); |
47 | } else { |
48 | dontResend.sendToTarget(); |
49 | } |
50 | } |
51 | } |
52 | |
53 | // Server responses for load and reload of posts. |
54 | private static final String LOAD_RESPONSE = |
55 | "<html><head><title>Load</title></head><body>HELLO</body></html>"; |
56 | private static final String RELOAD_RESPONSE = |
57 | "<html><head><title>Reload</title></head><body>HELLO</body></html>"; |
58 | |
59 | // Server timeout in seconds. Used to detect dontResend case. |
60 | private static final int TIMEOUT = 3; |
61 | |
62 | // The web server. |
63 | private TestWebServer mServer; |
64 | // The mock client. |
65 | private TestAwContentsClient mContentsClient; |
66 | private AwContents mAwContents; |
67 | |
68 | @Override |
69 | public void setUp() throws Exception { |
70 | super.setUp(); |
71 | mServer = new TestWebServer(false); |
72 | mContentsClient = new TestAwContentsClient(); |
73 | final AwTestContainerView testContainerView = |
74 | createAwTestContainerViewOnMainSync(mContentsClient); |
75 | mAwContents = testContainerView.getAwContents(); |
76 | } |
77 | |
78 | @Override |
79 | public void tearDown() throws Exception { |
80 | mServer.shutdown(); |
81 | super.tearDown(); |
82 | } |
83 | |
84 | /* |
85 | @SmallTest |
86 | @Feature({"AndroidWebView", "Navigation"}) |
87 | */ |
88 | @DisabledTest |
89 | public void testResend() throws Throwable { |
90 | mContentsClient.setResubmit(true); |
91 | doReload(); |
92 | assertEquals(1, mContentsClient.getResubmissions()); |
93 | assertEquals("Reload", getTitleOnUiThread(mAwContents)); |
94 | } |
95 | |
96 | @SmallTest |
97 | @Feature({"AndroidWebView", "Navigation"}) |
98 | public void testDontResend() throws Throwable { |
99 | mContentsClient.setResubmit(false); |
100 | doReload(); |
101 | assertEquals(1, mContentsClient.getResubmissions()); |
102 | assertEquals("Load", getTitleOnUiThread(mAwContents)); |
103 | } |
104 | |
105 | protected void doReload() throws Throwable { |
106 | String url = mServer.setResponse("/form", LOAD_RESPONSE, null); |
107 | String postData = "content=blabla"; |
108 | byte[] data = EncodingUtils.getBytes(postData, "BASE64"); |
109 | postUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url, data); |
110 | assertEquals(0, mContentsClient.getResubmissions()); |
111 | assertEquals("Load", getTitleOnUiThread(mAwContents)); |
112 | // Verify reload works as expected. |
113 | mServer.setResponse("/form", RELOAD_RESPONSE, null); |
114 | TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
115 | mContentsClient.getOnPageFinishedHelper(); |
116 | int callCount = onPageFinishedHelper.getCallCount(); |
117 | // Run reload on UI thread. |
118 | getInstrumentation().runOnMainSync(new Runnable() { |
119 | @Override |
120 | public void run() { |
121 | mAwContents.getContentViewCore().reload(); |
122 | } |
123 | }); |
124 | try { |
125 | // Wait for page finished callback, or a timeout. A timeout is necessary |
126 | // to detect a dontResend response. |
127 | onPageFinishedHelper.waitForCallback(callCount, 1, TIMEOUT, TimeUnit.SECONDS); |
128 | } catch (TimeoutException e) { |
129 | } |
130 | } |
131 | } |