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
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser;
7 import android.app.AlertDialog;
8 import android.test.suitebuilder.annotation.MediumTest;
9 import android.test.suitebuilder.annotation.SmallTest;
11 import org.chromium.base.test.util.Feature;
12 import org.chromium.chrome.shell.ChromeShellTab;
13 import org.chromium.chrome.shell.ChromeShellTestBase;
14 import org.chromium.chrome.test.util.TestHttpServerClient;
15 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
17 import java.util.concurrent.TimeoutException;
20 * Integration tests verifying that form resubmission dialogs are correctly displayed and handled.
22 public class RepostFormWarningTest extends ChromeShellTestBase {
24 private ChromeShellTab mTab;
25 // Callback helper that manages waiting for pageloads to finish.
26 private TestCallbackHelperContainer mCallbackHelper;
29 public void setUp() throws Exception {
32 mTab = launchChromeShellWithBlankPage().getActiveTab();
33 mCallbackHelper = new TestCallbackHelperContainer(mTab.getContentViewCore());
35 // Wait for the initial load of about://blank to finish.
36 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(0);
39 /** Verifies that the form resubmission warning is not displayed upon first POST navigation. */
41 @Feature({"Navigation"})
42 public void testFormFirstNavigation() throws Throwable {
43 // Load the url posting data for the first time.
45 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1);
46 getInstrumentation().waitForIdleSync();
48 // Verify that the form resubmission warning was not shown.
49 assertNull("Form resubmission warning shown upon first load.",
50 RepostFormWarningDialog.getCurrentDialog());
53 /** Verifies that confirming the form reload performs the reload. */
55 @Feature({"Navigation"})
56 public void testFormResubmissionContinue() throws Throwable {
57 // Load the url posting data for the first time.
59 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1);
61 // Trigger a reload and wait for the warning to be displayed.
63 getInstrumentation().waitForIdleSync();
64 AlertDialog dialog = (AlertDialog) RepostFormWarningDialog.getCurrentDialog();
65 assertNotNull("Form resubmission warning not shown upon reload.", dialog);
67 // Click "Continue" and verify that the page is reloaded.
68 clickButton(dialog, AlertDialog.BUTTON_POSITIVE);
69 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(2);
71 // Verify that the reference to the dialog in RepostFormWarningDialog was cleared.
72 assertNull("Form resubmission warning dialog was not dismissed correctly.",
73 RepostFormWarningDialog.getCurrentDialog());
77 * Verifies that cancelling the form reload prevents it from happening. Currently the test waits
78 * after the "Cancel" button is clicked to verify that the load was not triggered, which blocks
79 * for CallbackHelper's default timeout upon each execution.
82 @Feature({"Navigation"})
83 public void testFormResubmissionCancel() throws Throwable {
84 // Load the url posting data for the first time.
86 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(1);
88 // Trigger a reload and wait for the warning to be displayed.
90 getInstrumentation().waitForIdleSync();
91 AlertDialog dialog = (AlertDialog) RepostFormWarningDialog.getCurrentDialog();
92 assertNotNull("Form resubmission warning not shown upon reload.", dialog);
94 // Click "Cancel" and verify that the page is not reloaded.
95 clickButton(dialog, AlertDialog.BUTTON_NEGATIVE);
96 boolean timedOut = false;
98 mCallbackHelper.getOnPageFinishedHelper().waitForCallback(2);
99 } catch (TimeoutException ex) {
102 assertTrue("Page was reloaded despite selecting Cancel.", timedOut);
104 // Verify that the reference to the dialog in RepostFormWarningDialog was cleared.
105 assertNull("Form resubmission warning dialog was not dismissed correctly.",
106 RepostFormWarningDialog.getCurrentDialog());
109 /** Performs a POST navigation in mTab. */
110 private void postNavigation() throws Throwable {
111 final String url = "chrome/test/data/empty.html";
112 final byte[] postData = new byte[] { 42 };
114 runTestOnUiThread(new Runnable() {
117 mTab.loadUrlWithSanitization(TestHttpServerClient.getUrl(url), postData);
123 private void reload() throws Throwable {
124 runTestOnUiThread(new Runnable() {
132 /** Clicks the given button in the given dialog. */
133 private void clickButton(final AlertDialog dialog, final int buttonId) throws Throwable {
134 runTestOnUiThread(new Runnable() {
137 dialog.getButton(buttonId).performClick();