File: mouse_events_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (201 lines) | stat: -rw-r--r-- 7,737 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/javascript_dialogs/tab_modal_dialog_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/isolated_world_ids.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/base/test/ui_controls.h"

namespace {

// Integration test of browser event forwarding and web content event handling.
class MouseEventsTest : public InProcessBrowserTest {
 public:
  MouseEventsTest() = default;

  MouseEventsTest(const MouseEventsTest&) = delete;
  MouseEventsTest& operator=(const MouseEventsTest&) = delete;

  // InProcessBrowserTest:
  void SetUpOnMainThread() override {
    ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
  }

  content::WebContents* GetActiveWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  // Wait for the active web contents title to match |title|.
  void WaitForTitle(const std::string& title) {
    // Logging added temporarily to track down flakiness cited below.
    LOG(INFO) << "Waiting for title: " << title;
    const std::u16string expected_title(base::ASCIIToUTF16(title));
    content::TitleWatcher title_watcher(GetActiveWebContents(), expected_title);
    ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle());
  }

  // Load the test page and wait for onmouseover to be called.
  void NavigateAndWaitForMouseOver() {
    ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));

    // Move the mouse 2px above the web contents; allows onmouseover after load.
    const gfx::Rect bounds = GetActiveWebContents()->GetContainerBounds();
    ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() - 2);

    // Navigate to the test page and wait for onload to be called.
    const GURL url = ui_test_utils::GetTestUrl(
        base::FilePath(),
        base::FilePath(FILE_PATH_LITERAL("mouse_events_test.html")));
    ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
    WaitForTitle("onload");

    // Move the mouse over the div and wait for onmouseover to be called.
    ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() + 10);
    WaitForTitle("onmouseover");
  }

  // Load the test page and wait for onmouseover then onmouseout to be called.
  void NavigateAndWaitForMouseOverThenMouseOut() {
    EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver());

    // Moving the mouse outside the div should trigger onmouseout.
    const gfx::Rect bounds = GetActiveWebContents()->GetContainerBounds();
    ui_controls::SendMouseMove(bounds.CenterPoint().x(), bounds.y() - 10);
    WaitForTitle("onmouseout");
  }
};

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
// Flaky; http://crbug.com/133361.
#define MAYBE_MouseOver DISABLED_MouseOver
#else
#define MAYBE_MouseOver MouseOver
#endif

IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_MouseOver) {
  NavigateAndWaitForMouseOver();
}

#if BUILDFLAG(IS_MAC)
// Flaky; http://crbug.com/133361.
#define MAYBE_ClickAndDoubleClick DISABLED_ClickAndDoubleClick
#else
#define MAYBE_ClickAndDoubleClick ClickAndDoubleClick
#endif

IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ClickAndDoubleClick) {
  NavigateAndWaitForMouseOver();

  ui_controls::SendMouseClick(ui_controls::LEFT);
  WaitForTitle("onclick");

  ui_controls::SendMouseClick(ui_controls::LEFT);
  WaitForTitle("ondblclick");
}

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
    BUILDFLAG(IS_WIN)
// Flaky; http://crbug.com/133361.
#define MAYBE_TestOnMouseOut DISABLED_TestOnMouseOut
#else
#define MAYBE_TestOnMouseOut TestOnMouseOut
#endif

IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_TestOnMouseOut) {
  NavigateAndWaitForMouseOverThenMouseOut();
}

#if BUILDFLAG(IS_WIN)
// Mac/Linux are flaky; http://crbug.com/133361.
IN_PROC_BROWSER_TEST_F(MouseEventsTest, MouseDownOnBrowserCaption) {
  gfx::Rect browser_bounds = browser()->window()->GetBounds();
  ui_controls::SendMouseMove(browser_bounds.x() + 200, browser_bounds.y() + 10);
  ui_controls::SendMouseClick(ui_controls::LEFT);

  NavigateAndWaitForMouseOverThenMouseOut();
}
#endif

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE)
// Test that a mouseleave is not triggered when showing the context menu.
// If the test is failed, it means that Blink gets the mouseleave event
// when showing the context menu and it could make the unexpecting
// content behavior such as clearing the hover status.
// Please refer to the below issue for understanding what happens .
// Flaky; See http://crbug.com/656101.
#define MAYBE_ContextMenu DISABLED_ContextMenu
#else
#define MAYBE_ContextMenu ContextMenu
#endif

IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ContextMenu) {
  EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver());

  ContextMenuWaiter menu_observer;
  ui_controls::SendMouseClick(ui_controls::RIGHT);
  // Wait until the context menu is opened and closed.
  menu_observer.WaitForMenuOpenAndClose();

  content::WebContents* tab = GetActiveWebContents();
  tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
      u"done()", base::NullCallback(), content::ISOLATED_WORLD_ID_GLOBAL);
  const std::u16string success_title = u"without mouseleave";
  const std::u16string failure_title = u"with mouseleave";
  content::TitleWatcher done_title_watcher(tab, success_title);
  done_title_watcher.AlsoWaitForTitle(failure_title);
  EXPECT_EQ(success_title, done_title_watcher.WaitAndGetTitle());
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
// Test that a mouseleave is not triggered when showing a modal dialog.
// Sample regression: crbug.com/394672
// Flaky; http://crbug.com/838120
#define MAYBE_ModalDialog DISABLED_ModalDialog
#else
#define MAYBE_ModalDialog ModalDialog
#endif

IN_PROC_BROWSER_TEST_F(MouseEventsTest, MAYBE_ModalDialog) {
  EXPECT_NO_FATAL_FAILURE(NavigateAndWaitForMouseOver());

  content::WebContents* tab = GetActiveWebContents();
  auto* js_dialog_manager =
      javascript_dialogs::TabModalDialogManager::FromWebContents(tab);
  base::RunLoop dialog_wait;
  js_dialog_manager->SetDialogShownCallbackForTesting(
      dialog_wait.QuitClosure());
  tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
      u"alert()", base::NullCallback(), content::ISOLATED_WORLD_ID_GLOBAL);
  dialog_wait.Run();

  // Cancel the dialog.
  js_dialog_manager->HandleJavaScriptDialog(tab, false, nullptr);

  tab->GetPrimaryMainFrame()->ExecuteJavaScriptForTests(
      u"done()", base::NullCallback(), content::ISOLATED_WORLD_ID_GLOBAL);
  const std::u16string success_title = u"without mouseleave";
  const std::u16string failure_title = u"with mouseleave";
  content::TitleWatcher done_title_watcher(tab, success_title);
  done_title_watcher.AlsoWaitForTitle(failure_title);
  EXPECT_EQ(success_title, done_title_watcher.WaitAndGetTitle());
}

}  // namespace