Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [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 | |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 5 | #include "base/callback_helpers.h" |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 7 | #include "base/run_loop.h" |
| 8 | #include "base/strings/utf_string_conversions.h" |
| 9 | #include "build/build_config.h" |
| 10 | #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 11 | #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h" |
| 12 | #include "chrome/browser/ui/browser.h" |
| 13 | #include "chrome/browser/ui/browser_window.h" |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 | #include "chrome/test/base/in_process_browser_test.h" |
| 16 | #include "chrome/test/base/interactive_test_utils.h" |
| 17 | #include "chrome/test/base/ui_test_utils.h" |
Evan Stade | 4b55d202 | 2020-02-06 17:29:06 | [diff] [blame] | 18 | #include "components/javascript_dialogs/tab_modal_dialog_manager.h" |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 19 | #include "content/public/browser/render_frame_host.h" |
| 20 | #include "content/public/browser/web_contents.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 21 | #include "content/public/test/browser_test.h" |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 22 | #include "content/public/test/browser_test_utils.h" |
| 23 | #include "ui/base/test/ui_controls.h" |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // Integration test of browser event forwarding and web content event handling. |
| 28 | class MouseEventsTest : public InProcessBrowserTest { |
| 29 | public: |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 30 | MouseEventsTest() = default; |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 31 | |
Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 32 | MouseEventsTest(const MouseEventsTest&) = delete; |
| 33 | MouseEventsTest& operator=(const MouseEventsTest&) = delete; |
| 34 | |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 35 | // InProcessBrowserTest: |
| 36 | void SetUpOnMainThread() override { |
| 37 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 38 | } |
| 39 | |
| 40 | content::WebContents* GetActiveWebContents() { |
| 41 | return browser()->tab_strip_model()->GetActiveWebContents(); |
| 42 | } |
| 43 | |
| 44 | // Wait for the active web contents title to match |title|. |
| 45 | void WaitForTitle(const std::string& title) { |
| 46 | // Logging added temporarily to track down flakiness cited below. |
| 47 | LOG(INFO) << "Waiting for title: " << title; |
Jan Wilken Dörrie | dec9912 | 2021-03-11 18:02:30 | [diff] [blame] | 48 | const std::u16string expected_title(base::ASCIIToUTF16(title)); |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 49 | content::TitleWatcher title_watcher(GetActiveWebContents(), expected_title); |
| 50 | ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 51 | } |
| 52 | |
| 53 | // Load the test page and wait for onmouseover to be called. |
| 54 | void NavigateAndWaitForMouseOver() { |
| 55 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 56 | |
| 57 | // Move the mouse 2px above the web contents; allows onmouseover after load. |
| 58 | const gfx::Rect bounds = GetActiveWebContents()->GetContainerBounds(); |
| 59 | ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() - 2); |
| 60 | |
| 61 | // Navigate to the test page and wait for onload to be called. |
| 62 | const GURL url = ui_test_utils::GetTestUrl( |
| 63 | base::FilePath(), |
| 64 | base::FilePath(FILE_PATH_LITERAL("mouse_events_test.html"))); |
Lukasz Anforowicz | b78290c | 2021-09-08 04:31:38 | [diff] [blame] | 65 | ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 66 | WaitForTitle("onload"); |
| 67 | |
| 68 | // Move the mouse over the div and wait for onmouseover to be called. |
| 69 | ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() + 10); |
| 70 | WaitForTitle("onmouseover"); |
| 71 | } |
| 72 | |
| 73 | // Load the test page and wait for onmouseover then onmouseout to be called. |
| 74 | void NavigateAndWaitForMouseOverThenMouseOut() { |
| 75 | EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver()); |
| 76 | |
| 77 | // Moving the mouse outside the div should trigger onmouseout. |
| 78 | const gfx::Rect bounds = GetActiveWebContents()->GetContainerBounds(); |
| 79 | ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() - 10); |
| 80 | WaitForTitle("onmouseout"); |
| 81 | } |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 82 | }; |
| 83 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 84 | #if BUILDFLAG(IS_MAC) |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 85 | // Flaky; http://crbug.com/133361. |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 86 | #define MAYBE_MouseOver DISABLED_MouseOver |
| 87 | #else |
| 88 | #define MAYBE_MouseOver MouseOver |
| 89 | #endif |
| 90 | |
| 91 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_MouseOver) { |
| 92 | NavigateAndWaitForMouseOver(); |
| 93 | } |
| 94 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 95 | #if BUILDFLAG(IS_MAC) |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 96 | // Flaky; http://crbug.com/133361. |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 97 | #define MAYBE_ClickAndDoubleClick DISABLED_ClickAndDoubleClick |
| 98 | #else |
| 99 | #define MAYBE_ClickAndDoubleClick ClickAndDoubleClick |
| 100 | #endif |
| 101 | |
| 102 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ClickAndDoubleClick) { |
| 103 | NavigateAndWaitForMouseOver(); |
| 104 | |
| 105 | ui_controls::SendMouseClick(ui_controls::LEFT); |
| 106 | WaitForTitle("onclick"); |
| 107 | |
| 108 | ui_controls::SendMouseClick(ui_controls::LEFT); |
| 109 | WaitForTitle("ondblclick"); |
| 110 | } |
| 111 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 112 | #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \ |
| 113 | BUILDFLAG(IS_WIN) |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 114 | // Flaky; http://crbug.com/133361. |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 115 | #define MAYBE_TestOnMouseOut DISABLED_TestOnMouseOut |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 116 | #else |
| 117 | #define MAYBE_TestOnMouseOut TestOnMouseOut |
| 118 | #endif |
| 119 | |
| 120 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_TestOnMouseOut) { |
| 121 | NavigateAndWaitForMouseOverThenMouseOut(); |
| 122 | } |
| 123 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 124 | #if BUILDFLAG(IS_WIN) |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 125 | // Mac/Linux are flaky; http://crbug.com/133361. |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 126 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MouseDownOnBrowserCaption) { |
| 127 | gfx::Rect browser_bounds = browser()->window()->GetBounds(); |
| 128 | ui_controls::SendMouseMove(browser_bounds.x() + 200, browser_bounds.y() + 10); |
| 129 | ui_controls::SendMouseClick(ui_controls::LEFT); |
| 130 | |
| 131 | NavigateAndWaitForMouseOverThenMouseOut(); |
| 132 | } |
| 133 | #endif |
| 134 | |
kylechar | 29fa971 | 2022-11-04 00:47:47 | [diff] [blame] | 135 | #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE) |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 136 | // Test that a mouseleave is not triggered when showing the context menu. |
| 137 | // If the test is failed, it means that Blink gets the mouseleave event |
| 138 | // when showing the context menu and it could make the unexpecting |
| 139 | // content behavior such as clearing the hover status. |
| 140 | // Please refer to the below issue for understanding what happens . |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 141 | // Flaky; See http://crbug.com/656101. |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 142 | #define MAYBE_ContextMenu DISABLED_ContextMenu |
| 143 | #else |
| 144 | #define MAYBE_ContextMenu ContextMenu |
| 145 | #endif |
| 146 | |
| 147 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ContextMenu) { |
| 148 | EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver()); |
| 149 | |
Avi Drissman | 6b53a1d4 | 2018-04-09 18:19:43 | [diff] [blame] | 150 | ContextMenuWaiter menu_observer; |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 151 | ui_controls::SendMouseClick(ui_controls::RIGHT); |
| 152 | // Wait until the context menu is opened and closed. |
| 153 | menu_observer.WaitForMenuOpenAndClose(); |
| 154 | |
| 155 | content::WebContents* tab = GetActiveWebContents(); |
Dave Tapuska | 6aea6e2 | 2022-06-06 21:21:51 | [diff] [blame] | 156 | tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(u"done()", |
| 157 | base::NullCallback()); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 158 | const std::u16string success_title = u"without mouseleave"; |
| 159 | const std::u16string failure_title = u"with mouseleave"; |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 160 | content::TitleWatcher done_title_watcher(tab, success_title); |
| 161 | done_title_watcher.AlsoWaitForTitle(failure_title); |
| 162 | EXPECT_EQ(success_title, done_title_watcher.WaitAndGetTitle()); |
| 163 | } |
| 164 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 165 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \ |
| 166 | BUILDFLAG(IS_CHROMEOS) |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 167 | // Test that a mouseleave is not triggered when showing a modal dialog. |
| 168 | // Sample regression: crbug.com/394672 |
Avi Drissman | 487275f | 2021-11-02 18:46:00 | [diff] [blame] | 169 | // Flaky; http://crbug.com/838120 |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 170 | #define MAYBE_ModalDialog DISABLED_ModalDialog |
| 171 | #else |
| 172 | #define MAYBE_ModalDialog ModalDialog |
| 173 | #endif |
| 174 | |
| 175 | IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ModalDialog) { |
| 176 | EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver()); |
| 177 | |
| 178 | content::WebContents* tab = GetActiveWebContents(); |
Evan Stade | 4b55d202 | 2020-02-06 17:29:06 | [diff] [blame] | 179 | auto* js_dialog_manager = |
| 180 | javascript_dialogs::TabModalDialogManager::FromWebContents(tab); |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 181 | base::RunLoop dialog_wait; |
Evan Stade | 4b55d202 | 2020-02-06 17:29:06 | [diff] [blame] | 182 | js_dialog_manager->SetDialogShownCallbackForTesting( |
| 183 | dialog_wait.QuitClosure()); |
Dave Tapuska | 6aea6e2 | 2022-06-06 21:21:51 | [diff] [blame] | 184 | tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(u"alert()", |
| 185 | base::NullCallback()); |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 186 | dialog_wait.Run(); |
| 187 | |
| 188 | // Cancel the dialog. |
Evan Stade | 4b55d202 | 2020-02-06 17:29:06 | [diff] [blame] | 189 | js_dialog_manager->HandleJavaScriptDialog(tab, false, nullptr); |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 190 | |
Dave Tapuska | 6aea6e2 | 2022-06-06 21:21:51 | [diff] [blame] | 191 | tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(u"done()", |
| 192 | base::NullCallback()); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 193 | const std::u16string success_title = u"without mouseleave"; |
| 194 | const std::u16string failure_title = u"with mouseleave"; |
Mike Wasserman | 07e6c46 | 2018-03-31 01:15:50 | [diff] [blame] | 195 | content::TitleWatcher done_title_watcher(tab, success_title); |
| 196 | done_title_watcher.AlsoWaitForTitle(failure_title); |
| 197 | EXPECT_EQ(success_title, done_title_watcher.WaitAndGetTitle()); |
| 198 | } |
| 199 | |
| 200 | } // namespace |