blob: 727862ab0ec9f2819112aa304e2b54aece57317f [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]5973b962012-04-09 21:17:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Allen Bauera20daa82018-01-08 20:10:245#include "build/build_config.h"
[email protected]5973b962012-04-09 21:17:186#include "chrome/app/chrome_command_ids.h"
[email protected]fdf40f3e2013-07-11 23:55:467#include "chrome/browser/chrome_notification_types.h"
[email protected]5973b962012-04-09 21:17:188#include "chrome/browser/ui/browser.h"
[email protected]cc872372013-01-28 21:57:079#include "chrome/browser/ui/tabs/tab_strip_model.h"
Allen Bauera20daa82018-01-08 20:10:2410#include "chrome/browser/ui/test/test_browser_dialog.h"
[email protected]5973b962012-04-09 21:17:1811#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"
[email protected]e41d0082013-05-16 04:37:5414#include "components/web_modal/web_contents_modal_dialog_manager.h"
[email protected]5973b962012-04-09 21:17:1815#include "content/public/browser/navigation_controller.h"
16#include "content/public/browser/web_contents.h"
Peter Kasting919ce652020-05-07 10:22:3617#include "content/public/test/browser_test.h"
[email protected]5b8ff1c2012-06-02 20:42:2018#include "content/public/test/test_navigation_observer.h"
svaldeza01f7d92015-11-18 17:47:5619#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]5973b962012-04-09 21:17:1820
[email protected]e41d0082013-05-16 04:37:5421using web_modal::WebContentsModalDialogManager;
22
Allen Bauera20daa82018-01-08 20:10:2423class RepostFormWarningTest : public DialogBrowserTest {
24 public:
25 RepostFormWarningTest() {}
Peter Boström53c6c5952021-09-17 09:41:2626
27 RepostFormWarningTest(const RepostFormWarningTest&) = delete;
28 RepostFormWarningTest& operator=(const RepostFormWarningTest&) = delete;
29
Allen Bauera20daa82018-01-08 20:10:2430 ~RepostFormWarningTest() override {}
[email protected]5973b962012-04-09 21:17:1831
Allen Bauera20daa82018-01-08 20:10:2432 // BrowserTestBase:
33 void SetUpOnMainThread() override;
34
35 // DialogBrowserTest:
36 void ShowUi(const std::string& name) override;
37
38 protected:
39 content::WebContents* TryReload();
Allen Bauera20daa82018-01-08 20:10:2440};
41
42void RepostFormWarningTest::SetUpOnMainThread() {
43 DialogBrowserTest::SetUpOnMainThread();
svaldeza01f7d92015-11-18 17:47:5644 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]5973b962012-04-09 21:17:1845
46 // Load a form.
Lukasz Anforowiczb78290c2021-09-08 04:31:3847 ASSERT_TRUE(ui_test_utils::NavigateToURL(
48 browser(), embedded_test_server()->GetURL("/form.html")));
[email protected]5973b962012-04-09 21:17:1849 // Submit it.
Lukasz Anforowiczb78290c2021-09-08 04:31:3850 ASSERT_TRUE(ui_test_utils::NavigateToURL(
51 browser(), GURL("javascript:document.getElementById('form').submit()")));
Allen Bauera20daa82018-01-08 20:10:2452}
[email protected]5973b962012-04-09 21:17:1853
Allen Bauera20daa82018-01-08 20:10:2454void RepostFormWarningTest::ShowUi(const std::string& name) {
55 TryReload();
56}
57
58content::WebContents* RepostFormWarningTest::TryReload() {
59 // Try to reload it, checking for repost.
[email protected]cc872372013-01-28 21:57:0760 content::WebContents* web_contents =
61 browser()->tab_strip_model()->GetActiveWebContents();
toyoshim6142d96f2016-12-19 09:07:2562 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
Allen Bauera20daa82018-01-08 20:10:2463 return web_contents;
64}
65
66// If becomes flaky, disable on Windows and use http://crbug.com/47228
67IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestDoubleReload) {
68 // Try to reload it twice, checking for repost.
69 content::WebContents* web_contents = TryReload();
70 TryReload();
[email protected]5973b962012-04-09 21:17:1871
72 // There should only be one dialog open.
[email protected]6473adc042013-01-12 16:56:4173 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
[email protected]d2b16572013-01-03 00:41:5874 WebContentsModalDialogManager::FromWebContents(web_contents);
[email protected]93a38c4e2013-09-10 17:19:1275 EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive());
[email protected]5973b962012-04-09 21:17:1876
77 // Navigate away from the page (this is when the test usually crashes).
Lukasz Anforowiczb78290c2021-09-08 04:31:3878 ASSERT_TRUE(ui_test_utils::NavigateToURL(
79 browser(), embedded_test_server()->GetURL("/bar")));
[email protected]5973b962012-04-09 21:17:1880
81 // The dialog should've been closed.
[email protected]93a38c4e2013-09-10 17:19:1282 EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive());
[email protected]5973b962012-04-09 21:17:1883}
84
85// If becomes flaky, disable on Windows and use http://crbug.com/47228
86IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestLoginAfterRepost) {
[email protected]5973b962012-04-09 21:17:1887 // Try to reload it, checking for repost.
Allen Bauera20daa82018-01-08 20:10:2488 content::WebContents* web_contents = TryReload();
[email protected]5973b962012-04-09 21:17:1889
90 // Navigate to a page that requires authentication, bringing up another
91 // tab-modal sheet.
92 content::NavigationController& controller = web_contents->GetController();
[email protected]a7fe9112012-07-20 02:34:4593 content::WindowedNotificationObserver observer(
[email protected]5973b962012-04-09 21:17:1894 chrome::NOTIFICATION_AUTH_NEEDED,
95 content::Source<content::NavigationController>(&controller));
96 browser()->OpenURL(content::OpenURLParams(
svaldeza01f7d92015-11-18 17:47:5697 embedded_test_server()->GetURL("/auth-basic"), content::Referrer(),
nick3b04f322016-08-31 19:29:1998 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
[email protected]5973b962012-04-09 21:17:1899 observer.Wait();
100
101 // Try to reload it again.
toyoshim6142d96f2016-12-19 09:07:25102 web_contents->GetController().Reload(content::ReloadType::NORMAL, true);
[email protected]5973b962012-04-09 21:17:18103
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.
[email protected]cbb1ef592013-06-05 19:49:46107 content::TestNavigationObserver navigation_observer(web_contents);
[email protected]5973b962012-04-09 21:17:18108 browser()->OpenURL(content::OpenURLParams(
nick3b04f322016-08-31 19:29:19109 embedded_test_server()->GetURL("/bar"), content::Referrer(),
110 WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false));
[email protected]5973b962012-04-09 21:17:18111 navigation_observer.Wait();
112}
Allen Bauera20daa82018-01-08 20:10:24113
114// Disable on Mac OS until dialogs are using toolkit-views for MacViews project.
115// https://crbug.com/683356
Xiaohan Wang55ae2c012022-01-20 21:49:11116#if !BUILDFLAG(IS_MAC)
Allen Bauera20daa82018-01-08 20:10:24117IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, InvokeUi_TestRepostWarning) {
118 ShowAndVerifyUi();
119}
120#endif