Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 5 | #include "build/build_config.h" |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 6 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 7 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 8 | #include "chrome/browser/ui/browser.h" |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 9 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 10 | #include "chrome/browser/ui/test/test_browser_dialog.h" |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 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" |
[email protected] | e41d008 | 2013-05-16 04:37:54 | [diff] [blame] | 14 | #include "components/web_modal/web_contents_modal_dialog_manager.h" |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 15 | #include "content/public/browser/navigation_controller.h" |
| 16 | #include "content/public/browser/web_contents.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 17 | #include "content/public/test/browser_test.h" |
[email protected] | 5b8ff1c | 2012-06-02 20:42:20 | [diff] [blame] | 18 | #include "content/public/test/test_navigation_observer.h" |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 19 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 20 | |
[email protected] | e41d008 | 2013-05-16 04:37:54 | [diff] [blame] | 21 | using web_modal::WebContentsModalDialogManager; |
| 22 | |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 23 | class RepostFormWarningTest : public DialogBrowserTest { |
| 24 | public: |
| 25 | RepostFormWarningTest() {} |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 26 | |
| 27 | RepostFormWarningTest(const RepostFormWarningTest&) = delete; |
| 28 | RepostFormWarningTest& operator=(const RepostFormWarningTest&) = delete; |
| 29 | |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 30 | ~RepostFormWarningTest() override {} |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 31 | |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 32 | // BrowserTestBase: |
| 33 | void SetUpOnMainThread() override; |
| 34 | |
| 35 | // DialogBrowserTest: |
| 36 | void ShowUi(const std::string& name) override; |
| 37 | |
| 38 | protected: |
| 39 | content::WebContents* TryReload(); |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | void RepostFormWarningTest::SetUpOnMainThread() { |
| 43 | DialogBrowserTest::SetUpOnMainThread(); |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 44 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 45 | |
| 46 | // Load a form. |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 47 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 48 | browser(), embedded_test_server()->GetURL("/form.html"))); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 49 | // Submit it. |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 50 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 51 | browser(), GURL("javascript:document.getElementById('form').submit()"))); |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 52 | } |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 53 | |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 54 | void RepostFormWarningTest::ShowUi(const std::string& name) { |
| 55 | TryReload(); |
| 56 | } |
| 57 | |
| 58 | content::WebContents* RepostFormWarningTest::TryReload() { |
| 59 | // Try to reload it, checking for repost. |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 60 | content::WebContents* web_contents = |
| 61 | browser()->tab_strip_model()->GetActiveWebContents(); |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 62 | web_contents->GetController().Reload(content::ReloadType::NORMAL, true); |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 63 | return web_contents; |
| 64 | } |
| 65 | |
| 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(); |
| 70 | TryReload(); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 71 | |
| 72 | // There should only be one dialog open. |
[email protected] | 6473adc04 | 2013-01-12 16:56:41 | [diff] [blame] | 73 | WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
[email protected] | d2b1657 | 2013-01-03 00:41:58 | [diff] [blame] | 74 | WebContentsModalDialogManager::FromWebContents(web_contents); |
[email protected] | 93a38c4e | 2013-09-10 17:19:12 | [diff] [blame] | 75 | EXPECT_TRUE(web_contents_modal_dialog_manager->IsDialogActive()); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 76 | |
| 77 | // Navigate away from the page (this is when the test usually crashes). |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 78 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 79 | browser(), embedded_test_server()->GetURL("/bar"))); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 80 | |
| 81 | // The dialog should've been closed. |
[email protected] | 93a38c4e | 2013-09-10 17:19:12 | [diff] [blame] | 82 | EXPECT_FALSE(web_contents_modal_dialog_manager->IsDialogActive()); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // If becomes flaky, disable on Windows and use http://crbug.com/47228 |
| 86 | IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, TestLoginAfterRepost) { |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 87 | // Try to reload it, checking for repost. |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 88 | content::WebContents* web_contents = TryReload(); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 89 | |
| 90 | // Navigate to a page that requires authentication, bringing up another |
| 91 | // tab-modal sheet. |
| 92 | content::NavigationController& controller = web_contents->GetController(); |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 93 | content::WindowedNotificationObserver observer( |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 94 | chrome::NOTIFICATION_AUTH_NEEDED, |
| 95 | content::Source<content::NavigationController>(&controller)); |
| 96 | browser()->OpenURL(content::OpenURLParams( |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 97 | embedded_test_server()->GetURL("/auth-basic"), content::Referrer(), |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 98 | WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 99 | observer.Wait(); |
| 100 | |
| 101 | // Try to reload it again. |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 102 | web_contents->GetController().Reload(content::ReloadType::NORMAL, true); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 103 | |
| 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] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 107 | content::TestNavigationObserver navigation_observer(web_contents); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 108 | browser()->OpenURL(content::OpenURLParams( |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 109 | embedded_test_server()->GetURL("/bar"), content::Referrer(), |
| 110 | WindowOpenDisposition::CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 5973b96 | 2012-04-09 21:17:18 | [diff] [blame] | 111 | navigation_observer.Wait(); |
| 112 | } |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 113 | |
| 114 | // Disable on Mac OS until dialogs are using toolkit-views for MacViews project. |
| 115 | // https://crbug.com/683356 |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 116 | #if !BUILDFLAG(IS_MAC) |
Allen Bauer | a20daa8 | 2018-01-08 20:10:24 | [diff] [blame] | 117 | IN_PROC_BROWSER_TEST_F(RepostFormWarningTest, InvokeUi_TestRepostWarning) { |
| 118 | ShowAndVerifyUi(); |
| 119 | } |
| 120 | #endif |