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.
5 #include "build/build_config.h"
6 #include "chrome/app/chrome_command_ids.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/test/test_browser_dialog.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/browser_test.h"
18 #include "content/public/test/test_navigation_observer.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
21 using web_modal::WebContentsModalDialogManager;
23 class RepostFormWarningTest : public DialogBrowserTest {
25 RepostFormWarningTest() {}
26 ~RepostFormWarningTest() override {}
29 void SetUpOnMainThread() override;
32 void ShowUi(const std::string& name) override;
35 content::WebContents* TryReload();
38 DISALLOW_COPY_AND_ASSIGN(RepostFormWarningTest);
41 void RepostFormWarningTest::SetUpOnMainThread() {
42 DialogBrowserTest::SetUpOnMainThread();
43 ASSERT_TRUE(embedded_test_server()->Start());
46 ui_test_utils::NavigateToURL(browser(),
47 embedded_test_server()->GetURL("/form.html"));
49 ui_test_utils::NavigateToURL(
50 browser(), GURL("javascript:document.getElementById('form').submit()"));
53 void RepostFormWarningTest::ShowUi(const std::string& name) {
57 content::WebContents* RepostFormWarningTest::TryReload() {
58 // Try to reload it, checking for repost.
59 content::WebContents* web_contents =
60 browser()->tab_strip_model()->GetActiveWebContents();
61 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
65 // If becomes flaky, disable on Windows and use http://crbug.com/47228
66 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestDoubleReload) {
67 // Try to reload it twice, checking for repost.
68 content::WebContents* web_contents = TryReload();
71 // There should only be one dialog open.
72 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
73 WebContentsModalDialogManager::FromWebContents(web_contents);
74 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
76 // Navigate away from the page (this is when the test usually crashes).
77 ui_test_utils::NavigateToURL(browser(),
78 embedded_test_server()->GetURL("/bar"));
80 // The dialog should've been closed.
81 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
84 // If becomes flaky, disable on Windows and use http://crbug.com/47228
85 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestLoginAfterRepost) {
86 // Try to reload it, checking for repost.
87 content::WebContents* web_contents = TryReload();
89 // Navigate to a page that requires authentication, bringing up another
91 content::NavigationController& controller = web_contents->GetController();
92 content::WindowedNotificationObserver observer(
93 chrome::NOTIFICATION_AUTH_NEEDED,
94 content::Source<content::NavigationController>(&controller));
95 browser()->OpenURL(content::OpenURLParams(
96 embedded_test_server()->GetURL("/auth-basic"), content::Referrer(),
97 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
100 // Try to reload it again.
101 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
103 // Navigate away from the page. We can't use ui_test_utils:NavigateToURL
104 // because that waits for the current page to stop loading first, which won't
105 // happen while the auth dialog is up.
106 content::TestNavigationObserver navigation_observer(web_contents);
107 browser()->OpenURL(content::OpenURLParams(
108 embedded_test_server()->GetURL("/bar"), content::Referrer(),
109 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
110 navigation_observer.Wait();
113 // Disable on Mac OS until dialogs are using toolkit-views for MacViews project.
114 // https://crbug.com/683356
115 #if !defined(OS_MACOSX)
116 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, InvokeUi_TestRepostWarning) {