1 // Copyright 2012 The Chromium Authors
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/auth_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() {}
27 RepostFormWarningTest(const RepostFormWarningTest&) = delete;
28 RepostFormWarningTest& operator=(const RepostFormWarningTest&) = delete;
30 ~RepostFormWarningTest() override {}
33 void SetUpOnMainThread() override;
36 void ShowUi(const std::string& name) override;
39 content::WebContents* TryReload();
42 void RepostFormWarningTest::SetUpOnMainThread() {
43 DialogBrowserTest::SetUpOnMainThread();
44 ASSERT_TRUE(embedded_test_server()->Start());
47 ASSERT_TRUE(ui_test_utils::NavigateToURL(
48 browser(), embedded_test_server()->GetURL("/form.html")));
50 ASSERT_TRUE(ui_test_utils::NavigateToURL(
51 browser(), GURL("javascript:document.getElementById('form').submit()")));
54 void RepostFormWarningTest::ShowUi(const std::string& name) {
58 content::WebContents* RepostFormWarningTest::TryReload() {
59 // Try to reload it, checking for repost.
60 content::WebContents* web_contents =
61 browser()->tab_strip_model()->GetActiveWebContents();
62 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
66 // If becomes flaky, disable on Windows and use http://crbug.com/47228
67 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestDoubleReload) {
68 // Try to reload it twice, checking for repost.
69 content::WebContents* web_contents = TryReload();
72 // There should only be one dialog open.
73 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
74 WebContentsModalDialogManager::FromWebContents(web_contents);
75 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
77 // Navigate away from the page (this is when the test usually crashes).
78 ASSERT_TRUE(ui_test_utils::NavigateToURL(
79 browser(), embedded_test_server()->GetURL("/bar")));
81 // The dialog should've been closed.
82 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
85 // If becomes flaky, disable on Windows and use http://crbug.com/47228
86 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestLoginAfterRepost) {
87 // Try to reload it, checking for repost.
88 content::WebContents* web_contents = TryReload();
90 // Navigate to a page that requires authentication, bringing up another
92 content::NavigationController& controller = web_contents->GetController();
93 content::WindowedNotificationObserver observer(
94 chrome::NOTIFICATION_AUTH_NEEDED,
95 content::Source<content::NavigationController>(&controller));
96 browser()->OpenURL(content::OpenURLParams(
97 embedded_test_server()->GetURL("/auth-basic"), content::Referrer(),
98 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
101 // Try to reload it again.
102 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
104 // Navigate away from the page. We can't use ui_test_utils:NavigateToURL
105 // because that waits for the current page to stop loading first, which won't
106 // happen while the auth dialog is up.
107 content::TestNavigationObserver navigation_observer(web_contents);
108 browser()->OpenURL(content::OpenURLParams(
109 embedded_test_server()->GetURL("/bar"), content::Referrer(),
110 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
111 navigation_observer.Wait();
114 // Disable on Mac OS until dialogs are using toolkit-views for MacViews project.
115 // https://crbug.com/683356
116 #if !BUILDFLAG(IS_MAC)
117 IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, InvokeUi_TestRepostWarning) {