OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
| 7 import android.support.test.InstrumentationRegistry; |
7 import android.support.test.filters.MediumTest; | 8 import android.support.test.filters.MediumTest; |
8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
9 import android.support.v7.app.AlertDialog; | 10 import android.support.v7.app.AlertDialog; |
10 | 11 |
| 12 import org.junit.After; |
| 13 import org.junit.Assert; |
| 14 import org.junit.Before; |
| 15 import org.junit.Rule; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 |
11 import org.chromium.base.ThreadUtils; | 19 import org.chromium.base.ThreadUtils; |
| 20 import org.chromium.base.test.util.CommandLineFlags; |
12 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
13 import org.chromium.base.test.util.RetryOnFailure; | 22 import org.chromium.base.test.util.RetryOnFailure; |
14 import org.chromium.chrome.browser.tab.Tab; | 23 import org.chromium.chrome.browser.tab.Tab; |
15 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 24 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 25 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
16 import org.chromium.content.browser.test.util.Criteria; | 26 import org.chromium.content.browser.test.util.Criteria; |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | 27 import org.chromium.content.browser.test.util.CriteriaHelper; |
18 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 28 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
19 import org.chromium.content_public.browser.LoadUrlParams; | 29 import org.chromium.content_public.browser.LoadUrlParams; |
20 import org.chromium.net.test.EmbeddedTestServer; | 30 import org.chromium.net.test.EmbeddedTestServer; |
21 | 31 |
22 import java.util.concurrent.Callable; | 32 import java.util.concurrent.Callable; |
23 import java.util.concurrent.TimeoutException; | 33 import java.util.concurrent.TimeoutException; |
24 | 34 |
25 /** | 35 /** |
26 * Integration tests verifying that form resubmission dialogs are correctly disp
layed and handled. | 36 * Integration tests verifying that form resubmission dialogs are correctly disp
layed and handled. |
27 */ | 37 */ |
| 38 @RunWith(ChromeJUnit4ClassRunner.class) |
28 @RetryOnFailure | 39 @RetryOnFailure |
29 public class RepostFormWarningTest extends ChromeActivityTestCaseBase<ChromeActi
vity> { | 40 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 41 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) |
| 42 public class RepostFormWarningTest { |
30 // Active tab. | 43 // Active tab. |
| 44 |
| 45 @Rule |
| 46 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = |
| 47 new ChromeActivityTestRule<>(ChromeActivity.class); |
| 48 |
31 private Tab mTab; | 49 private Tab mTab; |
32 // Callback helper that manages waiting for pageloads to finish. | 50 // Callback helper that manages waiting for pageloads to finish. |
33 private TestCallbackHelperContainer mCallbackHelper; | 51 private TestCallbackHelperContainer mCallbackHelper; |
34 | 52 |
35 private EmbeddedTestServer mTestServer; | 53 private EmbeddedTestServer mTestServer; |
36 | 54 |
37 public RepostFormWarningTest() { | 55 @Before |
38 super(ChromeActivity.class); | 56 public void setUp() throws Exception { |
| 57 mActivityTestRule.startMainActivityOnBlankPage(); |
| 58 |
| 59 mTab = mActivityTestRule.getActivity().getActivityTab(); |
| 60 mCallbackHelper = new TestCallbackHelperContainer(mTab.getContentViewCor
e()); |
| 61 mTestServer = EmbeddedTestServer.createAndStartServer( |
| 62 InstrumentationRegistry.getInstrumentation().getContext()); |
39 } | 63 } |
40 | 64 |
41 @Override | 65 @After |
42 public void startMainActivity() throws InterruptedException { | |
43 startMainActivityOnBlankPage(); | |
44 } | |
45 | |
46 @Override | |
47 public void setUp() throws Exception { | |
48 super.setUp(); | |
49 | |
50 mTab = getActivity().getActivityTab(); | |
51 mCallbackHelper = new TestCallbackHelperContainer(mTab.getContentViewCor
e()); | |
52 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation
().getContext()); | |
53 } | |
54 | |
55 @Override | |
56 public void tearDown() throws Exception { | 66 public void tearDown() throws Exception { |
57 mTestServer.stopAndDestroyServer(); | 67 mTestServer.stopAndDestroyServer(); |
58 super.tearDown(); | |
59 } | 68 } |
60 | 69 |
61 /** Verifies that the form resubmission warning is not displayed upon first
POST navigation. */ | 70 /** Verifies that the form resubmission warning is not displayed upon first
POST navigation. */ |
| 71 @Test |
62 @MediumTest | 72 @MediumTest |
63 @Feature({"Navigation"}) | 73 @Feature({"Navigation"}) |
64 public void testFormFirstNavigation() throws Throwable { | 74 public void testFormFirstNavigation() throws Throwable { |
65 // Load the url posting data for the first time. | 75 // Load the url posting data for the first time. |
66 postNavigation(); | 76 postNavigation(); |
67 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); | 77 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); |
68 getInstrumentation().waitForIdleSync(); | 78 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
69 | 79 |
70 // Verify that the form resubmission warning was not shown. | 80 // Verify that the form resubmission warning was not shown. |
71 assertNull("Form resubmission warning shown upon first load.", | 81 Assert.assertNull("Form resubmission warning shown upon first load.", |
72 RepostFormWarningDialog.getCurrentDialogForTesting()); | 82 RepostFormWarningDialog.getCurrentDialogForTesting()); |
73 } | 83 } |
74 | 84 |
75 /** Verifies that confirming the form reload performs the reload. */ | 85 /** Verifies that confirming the form reload performs the reload. */ |
| 86 @Test |
76 @MediumTest | 87 @MediumTest |
77 @Feature({"Navigation"}) | 88 @Feature({"Navigation"}) |
78 public void testFormResubmissionContinue() throws Throwable { | 89 public void testFormResubmissionContinue() throws Throwable { |
79 // Load the url posting data for the first time. | 90 // Load the url posting data for the first time. |
80 postNavigation(); | 91 postNavigation(); |
81 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); | 92 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); |
82 | 93 |
83 // Trigger a reload and wait for the warning to be displayed. | 94 // Trigger a reload and wait for the warning to be displayed. |
84 reload(); | 95 reload(); |
85 AlertDialog dialog = waitForRepostFormWarningDialog(); | 96 AlertDialog dialog = waitForRepostFormWarningDialog(); |
86 | 97 |
87 // Click "Continue" and verify that the page is reloaded. | 98 // Click "Continue" and verify that the page is reloaded. |
88 clickButton(dialog, AlertDialog.BUTTON_POSITIVE); | 99 clickButton(dialog, AlertDialog.BUTTON_POSITIVE); |
89 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1); | 100 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1); |
90 | 101 |
91 // Verify that the reference to the dialog in RepostFormWarningDialog wa
s cleared. | 102 // Verify that the reference to the dialog in RepostFormWarningDialog wa
s cleared. |
92 assertNull("Form resubmission warning dialog was not dismissed correctly
.", | 103 Assert.assertNull("Form resubmission warning dialog was not dismissed co
rrectly.", |
93 RepostFormWarningDialog.getCurrentDialogForTesting()); | 104 RepostFormWarningDialog.getCurrentDialogForTesting()); |
94 } | 105 } |
95 | 106 |
96 /** | 107 /** |
97 * Verifies that cancelling the form reload prevents it from happening. Curr
ently the test waits | 108 * Verifies that cancelling the form reload prevents it from happening. Curr
ently the test waits |
98 * after the "Cancel" button is clicked to verify that the load was not trig
gered, which blocks | 109 * after the "Cancel" button is clicked to verify that the load was not trig
gered, which blocks |
99 * for CallbackHelper's default timeout upon each execution. | 110 * for CallbackHelper's default timeout upon each execution. |
100 */ | 111 */ |
| 112 @Test |
101 @SmallTest | 113 @SmallTest |
102 @Feature({"Navigation"}) | 114 @Feature({"Navigation"}) |
103 public void testFormResubmissionCancel() throws Throwable { | 115 public void testFormResubmissionCancel() throws Throwable { |
104 // Load the url posting data for the first time. | 116 // Load the url posting data for the first time. |
105 postNavigation(); | 117 postNavigation(); |
106 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); | 118 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); |
107 | 119 |
108 // Trigger a reload and wait for the warning to be displayed. | 120 // Trigger a reload and wait for the warning to be displayed. |
109 reload(); | 121 reload(); |
110 AlertDialog dialog = waitForRepostFormWarningDialog(); | 122 AlertDialog dialog = waitForRepostFormWarningDialog(); |
111 | 123 |
112 // Click "Cancel" and verify that the page is not reloaded. | 124 // Click "Cancel" and verify that the page is not reloaded. |
113 clickButton(dialog, AlertDialog.BUTTON_NEGATIVE); | 125 clickButton(dialog, AlertDialog.BUTTON_NEGATIVE); |
114 boolean timedOut = false; | 126 boolean timedOut = false; |
115 try { | 127 try { |
116 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1); | 128 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1); |
117 } catch (TimeoutException ex) { | 129 } catch (TimeoutException ex) { |
118 timedOut = true; | 130 timedOut = true; |
119 } | 131 } |
120 assertTrue("Page was reloaded despite selecting Cancel.", timedOut); | 132 Assert.assertTrue("Page was reloaded despite selecting Cancel.", timedOu
t); |
121 | 133 |
122 // Verify that the reference to the dialog in RepostFormWarningDialog wa
s cleared. | 134 // Verify that the reference to the dialog in RepostFormWarningDialog wa
s cleared. |
123 assertNull("Form resubmission warning dialog was not dismissed correctly
.", | 135 Assert.assertNull("Form resubmission warning dialog was not dismissed co
rrectly.", |
124 RepostFormWarningDialog.getCurrentDialogForTesting()); | 136 RepostFormWarningDialog.getCurrentDialogForTesting()); |
125 } | 137 } |
126 | 138 |
127 /** | 139 /** |
128 * Verifies that destroying the Tab dismisses the form resubmission dialog. | 140 * Verifies that destroying the Tab dismisses the form resubmission dialog. |
129 */ | 141 */ |
| 142 @Test |
130 @SmallTest | 143 @SmallTest |
131 @Feature({"Navigation"}) | 144 @Feature({"Navigation"}) |
132 public void testFormResubmissionTabDestroyed() throws Throwable { | 145 public void testFormResubmissionTabDestroyed() throws Throwable { |
133 // Load the url posting data for the first time. | 146 // Load the url posting data for the first time. |
134 postNavigation(); | 147 postNavigation(); |
135 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); | 148 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0); |
136 | 149 |
137 // Trigger a reload and wait for the warning to be displayed. | 150 // Trigger a reload and wait for the warning to be displayed. |
138 reload(); | 151 reload(); |
139 waitForRepostFormWarningDialog(); | 152 waitForRepostFormWarningDialog(); |
140 | 153 |
141 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 154 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
142 @Override | 155 @Override |
143 public void run() { | 156 public void run() { |
144 getActivity().getCurrentTabModel().closeTab(mTab); | 157 mActivityTestRule.getActivity().getCurrentTabModel().closeTab(mT
ab); |
145 } | 158 } |
146 }); | 159 }); |
147 | 160 |
148 CriteriaHelper.pollUiThread( | 161 CriteriaHelper.pollUiThread( |
149 new Criteria("Form resubmission dialog not dismissed correctly")
{ | 162 new Criteria("Form resubmission dialog not dismissed correctly")
{ |
150 @Override | 163 @Override |
151 public boolean isSatisfied() { | 164 public boolean isSatisfied() { |
152 return RepostFormWarningDialog.getCurrentDialogForTestin
g() == null; | 165 return RepostFormWarningDialog.getCurrentDialogForTestin
g() == null; |
153 } | 166 } |
154 }); | 167 }); |
(...skipping 13 matching lines...) Expand all Loading... |
168 return (AlertDialog) RepostFormWarningDialog.getCurrentDialogFor
Testing(); | 181 return (AlertDialog) RepostFormWarningDialog.getCurrentDialogFor
Testing(); |
169 } | 182 } |
170 }); | 183 }); |
171 } | 184 } |
172 | 185 |
173 /** Performs a POST navigation in mTab. */ | 186 /** Performs a POST navigation in mTab. */ |
174 private void postNavigation() throws Throwable { | 187 private void postNavigation() throws Throwable { |
175 final String url = "/chrome/test/data/android/test.html"; | 188 final String url = "/chrome/test/data/android/test.html"; |
176 final byte[] postData = new byte[] { 42 }; | 189 final byte[] postData = new byte[] { 42 }; |
177 | 190 |
178 runTestOnUiThread(new Runnable() { | 191 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
179 @Override | 192 @Override |
180 public void run() { | 193 public void run() { |
181 mTab.loadUrl(LoadUrlParams.createLoadHttpPostParams( | 194 mTab.loadUrl(LoadUrlParams.createLoadHttpPostParams( |
182 mTestServer.getURL(url), postData)); | 195 mTestServer.getURL(url), postData)); |
183 } | 196 } |
184 }); | 197 }); |
185 } | 198 } |
186 | 199 |
187 /** Reloads mTab. */ | 200 /** Reloads mTab. */ |
188 private void reload() throws Throwable { | 201 private void reload() throws Throwable { |
189 runTestOnUiThread(new Runnable() { | 202 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
190 @Override | 203 @Override |
191 public void run() { | 204 public void run() { |
192 mTab.reload(); | 205 mTab.reload(); |
193 } | 206 } |
194 }); | 207 }); |
195 } | 208 } |
196 | 209 |
197 /** Clicks the given button in the given dialog. */ | 210 /** Clicks the given button in the given dialog. */ |
198 private void clickButton(final AlertDialog dialog, final int buttonId) throw
s Throwable { | 211 private void clickButton(final AlertDialog dialog, final int buttonId) throw
s Throwable { |
199 runTestOnUiThread(new Runnable() { | 212 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
200 @Override | 213 @Override |
201 public void run() { | 214 public void run() { |
202 dialog.getButton(buttonId).performClick(); | 215 dialog.getButton(buttonId).performClick(); |
203 } | 216 } |
204 }); | 217 }); |
205 } | 218 } |
206 } | 219 } |
OLD | NEW |