OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/input/EventHandler.h" | 5 #include "core/input/EventHandler.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/Range.h" | 8 #include "core/dom/Range.h" |
9 #include "core/editing/Editor.h" | 9 #include "core/editing/Editor.h" |
10 #include "core/editing/FrameSelection.h" | 10 #include "core/editing/FrameSelection.h" |
11 #include "core/frame/FrameView.h" | 11 #include "core/frame/FrameView.h" |
12 #include "core/frame/LocalFrame.h" | 12 #include "core/frame/LocalFrame.h" |
13 #include "core/frame/Settings.h" | 13 #include "core/frame/Settings.h" |
14 #include "core/page/AutoscrollController.h" | 14 #include "core/page/AutoscrollController.h" |
15 #include "core/page/Page.h" | 15 #include "core/page/Page.h" |
16 #include "core/testing/DummyPageHolder.h" | 16 #include "core/testing/DummyPageHolder.h" |
17 #include "platform/PlatformMouseEvent.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 #include <memory> | 18 #include <memory> |
20 | 19 |
21 namespace blink { | 20 namespace blink { |
22 | 21 |
23 class EventHandlerTest : public ::testing::Test { | 22 class EventHandlerTest : public ::testing::Test { |
24 protected: | 23 protected: |
25 void SetUp() override; | 24 void SetUp() override; |
26 | 25 |
27 Page& page() const { return m_dummyPageHolder->page(); } | 26 Page& page() const { return m_dummyPageHolder->page(); } |
(...skipping 28 matching lines...) Expand all Loading... |
56 m_type = WebInputEvent::GestureLongPress; | 55 m_type = WebInputEvent::GestureLongPress; |
57 x = globalX = position.x(); | 56 x = globalX = position.x(); |
58 y = globalY = position.y(); | 57 y = globalY = position.y(); |
59 sourceDevice = WebGestureDeviceTouchscreen; | 58 sourceDevice = WebGestureDeviceTouchscreen; |
60 data.longPress.width = 5; | 59 data.longPress.width = 5; |
61 data.longPress.height = 5; | 60 data.longPress.height = 5; |
62 m_frameScale = 1; | 61 m_frameScale = 1; |
63 } | 62 } |
64 }; | 63 }; |
65 | 64 |
66 class MousePressEventBuilder : public PlatformMouseEvent { | 65 class MousePressEventBuilder : public WebMouseEvent { |
67 public: | 66 public: |
68 MousePressEventBuilder(IntPoint position, | 67 MousePressEventBuilder(IntPoint position, |
69 int clickCount, | 68 int clickCountParam, |
70 WebMouseEvent::Button button) | 69 WebMouseEvent::Button buttonParam) |
71 : PlatformMouseEvent(position, | 70 : WebMouseEvent(WebInputEvent::MouseDown, |
72 position, | 71 WebInputEvent::NoModifiers, |
73 button, | 72 TimeTicks::Now().InSeconds()) { |
74 PlatformEvent::MousePressed, | 73 clickCount = clickCountParam; |
75 clickCount, | 74 button = buttonParam; |
76 static_cast<PlatformEvent::Modifiers>(0), | 75 x = globalX = position.x(); |
77 TimeTicks::Now()) {} | 76 y = globalY = position.y(); |
| 77 m_frameScale = 1; |
| 78 } |
78 }; | 79 }; |
79 | 80 |
80 void EventHandlerTest::SetUp() { | 81 void EventHandlerTest::SetUp() { |
81 m_dummyPageHolder = DummyPageHolder::create(IntSize(300, 400)); | 82 m_dummyPageHolder = DummyPageHolder::create(IntSize(300, 400)); |
82 } | 83 } |
83 | 84 |
84 void EventHandlerTest::setHtmlInnerHTML(const char* htmlContent) { | 85 void EventHandlerTest::setHtmlInnerHTML(const char* htmlContent) { |
85 document().documentElement()->setInnerHTML(String::fromUTF8(htmlContent)); | 86 document().documentElement()->setInnerHTML(String::fromUTF8(htmlContent)); |
86 document().view()->updateAllLifecyclePhases(); | 87 document().view()->updateAllLifecyclePhases(); |
87 } | 88 } |
(...skipping 10 matching lines...) Expand all Loading... |
98 "class='line'>Line 5</span>" | 99 "class='line'>Line 5</span>" |
99 "<span class='line'>Line 6</span><span class='line'>Line 7</span><span " | 100 "<span class='line'>Line 6</span><span class='line'>Line 7</span><span " |
100 "class='line'>Line 8</span><span class='line'>Line 9</span><span " | 101 "class='line'>Line 8</span><span class='line'>Line 9</span><span " |
101 "class='line'>Line 10</span>" | 102 "class='line'>Line 10</span>" |
102 "</div>"); | 103 "</div>"); |
103 | 104 |
104 FrameView* frameView = document().view(); | 105 FrameView* frameView = document().view(); |
105 frameView->layoutViewportScrollableArea()->setScrollOffset( | 106 frameView->layoutViewportScrollableArea()->setScrollOffset( |
106 ScrollOffset(0, 400), ProgrammaticScroll); | 107 ScrollOffset(0, 400), ProgrammaticScroll); |
107 | 108 |
108 PlatformMouseEvent mouseDownEvent( | 109 WebMouseEvent mouseDownEvent(WebInputEvent::MouseDown, WebFloatPoint(0, 0), |
109 IntPoint(0, 0), IntPoint(100, 200), WebPointerProperties::Button::Left, | 110 WebFloatPoint(100, 200), |
110 PlatformEvent::MousePressed, 1, PlatformEvent::Modifiers::LeftButtonDown, | 111 WebPointerProperties::Button::Left, 1, |
111 TimeTicks::Now()); | 112 WebInputEvent::Modifiers::LeftButtonDown, |
| 113 WebInputEvent::TimeStampForTesting); |
| 114 mouseDownEvent.setFrameScale(1); |
112 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); | 115 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); |
113 | 116 |
114 PlatformMouseEvent mouseMoveEvent( | 117 WebMouseEvent mouseMoveEvent(WebInputEvent::MouseMove, WebFloatPoint(100, 50), |
115 IntPoint(100, 50), IntPoint(200, 250), WebPointerProperties::Button::Left, | 118 WebFloatPoint(200, 250), |
116 PlatformEvent::MouseMoved, 1, PlatformEvent::Modifiers::LeftButtonDown, | 119 WebPointerProperties::Button::Left, 1, |
117 TimeTicks::Now()); | 120 WebInputEvent::Modifiers::LeftButtonDown, |
| 121 WebInputEvent::TimeStampForTesting); |
| 122 mouseMoveEvent.setFrameScale(1); |
118 document().frame()->eventHandler().handleMouseMoveEvent( | 123 document().frame()->eventHandler().handleMouseMoveEvent( |
119 mouseMoveEvent, Vector<PlatformMouseEvent>()); | 124 mouseMoveEvent, Vector<WebMouseEvent>()); |
120 | 125 |
121 page().autoscrollController().animate(WTF::monotonicallyIncreasingTime()); | 126 page().autoscrollController().animate(WTF::monotonicallyIncreasingTime()); |
122 page().animator().serviceScriptedAnimations( | 127 page().animator().serviceScriptedAnimations( |
123 WTF::monotonicallyIncreasingTime()); | 128 WTF::monotonicallyIncreasingTime()); |
124 | 129 |
125 PlatformMouseEvent mouseUpEvent( | 130 WebMouseEvent mouseUpEvent( |
126 IntPoint(100, 50), IntPoint(200, 250), WebPointerProperties::Button::Left, | 131 WebMouseEvent::MouseUp, WebFloatPoint(100, 50), WebFloatPoint(200, 250), |
127 PlatformEvent::MouseReleased, 1, static_cast<PlatformEvent::Modifiers>(0), | 132 WebPointerProperties::Button::Left, 1, WebInputEvent::NoModifiers, |
128 TimeTicks::Now()); | 133 WebInputEvent::TimeStampForTesting); |
| 134 mouseUpEvent.setFrameScale(1); |
129 document().frame()->eventHandler().handleMouseReleaseEvent(mouseUpEvent); | 135 document().frame()->eventHandler().handleMouseReleaseEvent(mouseUpEvent); |
130 | 136 |
131 ASSERT_TRUE(selection().isRange()); | 137 ASSERT_TRUE(selection().isRange()); |
132 Range* range = | 138 Range* range = |
133 createRange(selection().selection().toNormalizedEphemeralRange()); | 139 createRange(selection().selection().toNormalizedEphemeralRange()); |
134 ASSERT_TRUE(range); | 140 ASSERT_TRUE(range); |
135 EXPECT_EQ("Line 1\nLine 2", range->text()); | 141 EXPECT_EQ("Line 1\nLine 2", range->text()); |
136 } | 142 } |
137 | 143 |
138 TEST_F(EventHandlerTest, multiClickSelectionFromTap) { | 144 TEST_F(EventHandlerTest, multiClickSelectionFromTap) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 TEST_F(EventHandlerTest, draggedInlinePositionTest) { | 205 TEST_F(EventHandlerTest, draggedInlinePositionTest) { |
200 setHtmlInnerHTML( | 206 setHtmlInnerHTML( |
201 "<style>" | 207 "<style>" |
202 "body { margin: 0px; }" | 208 "body { margin: 0px; }" |
203 ".line { font-family: sans-serif; background: blue; width: 300px; " | 209 ".line { font-family: sans-serif; background: blue; width: 300px; " |
204 "height: 30px; font-size: 40px; margin-left: 250px; }" | 210 "height: 30px; font-size: 40px; margin-left: 250px; }" |
205 "</style>" | 211 "</style>" |
206 "<div style='width: 300px; height: 100px;'>" | 212 "<div style='width: 300px; height: 100px;'>" |
207 "<span class='line' draggable='true'>abcd</span>" | 213 "<span class='line' draggable='true'>abcd</span>" |
208 "</div>"); | 214 "</div>"); |
209 PlatformMouseEvent mouseDownEvent( | 215 WebMouseEvent mouseDownEvent(WebMouseEvent::MouseDown, WebFloatPoint(262, 29), |
210 IntPoint(262, 29), IntPoint(329, 67), WebPointerProperties::Button::Left, | 216 WebFloatPoint(329, 67), |
211 PlatformEvent::MousePressed, 1, PlatformEvent::Modifiers::LeftButtonDown, | 217 WebPointerProperties::Button::Left, 1, |
212 TimeTicks::Now()); | 218 WebInputEvent::Modifiers::LeftButtonDown, |
| 219 WebInputEvent::TimeStampForTesting); |
| 220 mouseDownEvent.setFrameScale(1); |
213 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); | 221 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); |
214 | 222 |
215 PlatformMouseEvent mouseMoveEvent( | 223 WebMouseEvent mouseMoveEvent(WebMouseEvent::MouseMove, |
216 IntPoint(618, 298), IntPoint(685, 436), | 224 WebFloatPoint(618, 298), WebFloatPoint(685, 436), |
217 WebPointerProperties::Button::Left, PlatformEvent::MouseMoved, 1, | 225 WebPointerProperties::Button::Left, 1, |
218 PlatformEvent::Modifiers::LeftButtonDown, TimeTicks::Now()); | 226 WebInputEvent::Modifiers::LeftButtonDown, |
| 227 WebInputEvent::TimeStampForTesting); |
| 228 mouseMoveEvent.setFrameScale(1); |
219 document().frame()->eventHandler().handleMouseMoveEvent( | 229 document().frame()->eventHandler().handleMouseMoveEvent( |
220 mouseMoveEvent, Vector<PlatformMouseEvent>()); | 230 mouseMoveEvent, Vector<WebMouseEvent>()); |
221 | 231 |
222 EXPECT_EQ( | 232 EXPECT_EQ( |
223 IntPoint(12, 29), | 233 IntPoint(12, 29), |
224 document().frame()->eventHandler().dragDataTransferLocationForTesting()); | 234 document().frame()->eventHandler().dragDataTransferLocationForTesting()); |
225 } | 235 } |
226 | 236 |
227 TEST_F(EventHandlerTest, draggedSVGImagePositionTest) { | 237 TEST_F(EventHandlerTest, draggedSVGImagePositionTest) { |
228 setHtmlInnerHTML( | 238 setHtmlInnerHTML( |
229 "<style>" | 239 "<style>" |
230 "body { margin: 0px; }" | 240 "body { margin: 0px; }" |
231 "[draggable] {" | 241 "[draggable] {" |
232 "-webkit-user-select: none; user-select: none; -webkit-user-drag: " | 242 "-webkit-user-select: none; user-select: none; -webkit-user-drag: " |
233 "element; }" | 243 "element; }" |
234 "</style>" | 244 "</style>" |
235 "<div style='width: 300px; height: 100px;'>" | 245 "<div style='width: 300px; height: 100px;'>" |
236 "<svg width='500' height='500'>" | 246 "<svg width='500' height='500'>" |
237 "<rect x='100' y='100' width='100px' height='100px' fill='blue' " | 247 "<rect x='100' y='100' width='100px' height='100px' fill='blue' " |
238 "draggable='true'/>" | 248 "draggable='true'/>" |
239 "</svg>" | 249 "</svg>" |
240 "</div>"); | 250 "</div>"); |
241 PlatformMouseEvent mouseDownEvent( | 251 WebMouseEvent mouseDownEvent(WebMouseEvent::MouseDown, |
242 IntPoint(145, 144), IntPoint(212, 282), | 252 WebFloatPoint(145, 144), WebFloatPoint(212, 282), |
243 WebPointerProperties::Button::Left, PlatformEvent::MousePressed, 1, | 253 WebPointerProperties::Button::Left, 1, |
244 PlatformEvent::Modifiers::LeftButtonDown, TimeTicks::Now()); | 254 WebInputEvent::Modifiers::LeftButtonDown, |
| 255 WebInputEvent::TimeStampForTesting); |
| 256 mouseDownEvent.setFrameScale(1); |
245 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); | 257 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); |
246 | 258 |
247 PlatformMouseEvent mouseMoveEvent( | 259 WebMouseEvent mouseMoveEvent(WebMouseEvent::MouseMove, |
248 IntPoint(618, 298), IntPoint(685, 436), | 260 WebFloatPoint(618, 298), WebFloatPoint(685, 436), |
249 WebPointerProperties::Button::Left, PlatformEvent::MouseMoved, 1, | 261 WebPointerProperties::Button::Left, 1, |
250 PlatformEvent::Modifiers::LeftButtonDown, TimeTicks::Now()); | 262 WebInputEvent::Modifiers::LeftButtonDown, |
| 263 WebInputEvent::TimeStampForTesting); |
| 264 mouseMoveEvent.setFrameScale(1); |
251 document().frame()->eventHandler().handleMouseMoveEvent( | 265 document().frame()->eventHandler().handleMouseMoveEvent( |
252 mouseMoveEvent, Vector<PlatformMouseEvent>()); | 266 mouseMoveEvent, Vector<WebMouseEvent>()); |
253 | 267 |
254 EXPECT_EQ( | 268 EXPECT_EQ( |
255 IntPoint(45, 44), | 269 IntPoint(45, 44), |
256 document().frame()->eventHandler().dragDataTransferLocationForTesting()); | 270 document().frame()->eventHandler().dragDataTransferLocationForTesting()); |
257 } | 271 } |
258 | 272 |
259 // Regression test for http://crbug.com/641403 to verify we use up-to-date | 273 // Regression test for http://crbug.com/641403 to verify we use up-to-date |
260 // layout tree for dispatching "contextmenu" event. | 274 // layout tree for dispatching "contextmenu" event. |
261 TEST_F(EventHandlerTest, sendContextMenuEventWithHover) { | 275 TEST_F(EventHandlerTest, sendContextMenuEventWithHover) { |
262 setHtmlInnerHTML( | 276 setHtmlInnerHTML( |
263 "<style>*:hover { color: red; }</style>" | 277 "<style>*:hover { color: red; }</style>" |
264 "<div>foo</div>"); | 278 "<div>foo</div>"); |
265 document().settings()->setScriptEnabled(true); | 279 document().settings()->setScriptEnabled(true); |
266 Element* script = document().createElement("script"); | 280 Element* script = document().createElement("script"); |
267 script->setInnerHTML( | 281 script->setInnerHTML( |
268 "document.addEventListener('contextmenu', event => " | 282 "document.addEventListener('contextmenu', event => " |
269 "event.preventDefault());"); | 283 "event.preventDefault());"); |
270 document().body()->appendChild(script); | 284 document().body()->appendChild(script); |
271 document().updateStyleAndLayout(); | 285 document().updateStyleAndLayout(); |
272 document().frame()->selection().setSelection( | 286 document().frame()->selection().setSelection( |
273 SelectionInDOMTree::Builder() | 287 SelectionInDOMTree::Builder() |
274 .collapse(Position(document().body(), 0)) | 288 .collapse(Position(document().body(), 0)) |
275 .build()); | 289 .build()); |
276 PlatformMouseEvent mouseDownEvent( | 290 WebMouseEvent mouseDownEvent( |
277 IntPoint(0, 0), IntPoint(100, 200), WebPointerProperties::Button::Right, | 291 WebMouseEvent::MouseDown, WebFloatPoint(0, 0), WebFloatPoint(100, 200), |
278 PlatformEvent::MousePressed, 1, PlatformEvent::Modifiers::RightButtonDown, | 292 WebPointerProperties::Button::Right, 1, |
279 TimeTicks::Now()); | 293 PlatformEvent::Modifiers::RightButtonDown, TimeTicks::Now().InSeconds()); |
| 294 mouseDownEvent.setFrameScale(1); |
280 EXPECT_EQ( | 295 EXPECT_EQ( |
281 WebInputEventResult::HandledApplication, | 296 WebInputEventResult::HandledApplication, |
282 document().frame()->eventHandler().sendContextMenuEvent(mouseDownEvent)); | 297 document().frame()->eventHandler().sendContextMenuEvent(mouseDownEvent)); |
283 } | 298 } |
284 | 299 |
285 TEST_F(EventHandlerTest, EmptyTextfieldInsertionOnTap) { | 300 TEST_F(EventHandlerTest, EmptyTextfieldInsertionOnTap) { |
286 setHtmlInnerHTML("<textarea cols=50 rows=50></textarea>"); | 301 setHtmlInnerHTML("<textarea cols=50 rows=50></textarea>"); |
287 | 302 |
288 TapEventBuilder singleTapEvent(IntPoint(200, 200), 1); | 303 TapEventBuilder singleTapEvent(IntPoint(200, 200), 1); |
289 document().frame()->eventHandler().handleGestureEvent(singleTapEvent); | 304 document().frame()->eventHandler().handleGestureEvent(singleTapEvent); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 | 395 |
381 ASSERT_TRUE(selection().isRange()); | 396 ASSERT_TRUE(selection().isRange()); |
382 ASSERT_FALSE(selection().isHandleVisible()); | 397 ASSERT_FALSE(selection().isHandleVisible()); |
383 } | 398 } |
384 | 399 |
385 TEST_F(EventHandlerTest, dragEndInNewDrag) { | 400 TEST_F(EventHandlerTest, dragEndInNewDrag) { |
386 setHtmlInnerHTML( | 401 setHtmlInnerHTML( |
387 "<style>.box { width: 100px; height: 100px; display: block; }</style>" | 402 "<style>.box { width: 100px; height: 100px; display: block; }</style>" |
388 "<a class='box' href=''>Drag me</a>"); | 403 "<a class='box' href=''>Drag me</a>"); |
389 | 404 |
390 PlatformMouseEvent mouseDownEvent( | 405 WebMouseEvent mouseDownEvent( |
391 IntPoint(50, 50), IntPoint(50, 50), WebPointerProperties::Button::Left, | 406 WebInputEvent::MouseDown, WebFloatPoint(50, 50), WebFloatPoint(50, 50), |
392 PlatformEvent::MousePressed, 1, PlatformEvent::Modifiers::LeftButtonDown, | 407 WebPointerProperties::Button::Left, 1, |
393 TimeTicks::Now()); | 408 PlatformEvent::Modifiers::LeftButtonDown, TimeTicks::Now().InSeconds()); |
| 409 mouseDownEvent.setFrameScale(1); |
394 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); | 410 document().frame()->eventHandler().handleMousePressEvent(mouseDownEvent); |
395 | 411 |
396 PlatformMouseEvent mouseMoveEvent( | 412 WebMouseEvent mouseMoveEvent( |
397 IntPoint(51, 50), IntPoint(51, 50), WebPointerProperties::Button::Left, | 413 WebInputEvent::MouseMove, WebFloatPoint(51, 50), WebFloatPoint(51, 50), |
398 PlatformEvent::MouseMoved, 1, PlatformEvent::Modifiers::LeftButtonDown, | 414 WebPointerProperties::Button::Left, 1, |
399 TimeTicks::Now()); | 415 PlatformEvent::Modifiers::LeftButtonDown, TimeTicks::Now().InSeconds()); |
| 416 mouseMoveEvent.setFrameScale(1); |
400 document().frame()->eventHandler().handleMouseMoveEvent( | 417 document().frame()->eventHandler().handleMouseMoveEvent( |
401 mouseMoveEvent, Vector<PlatformMouseEvent>()); | 418 mouseMoveEvent, Vector<WebMouseEvent>()); |
402 | 419 |
403 // This reproduces what might be the conditions of http://crbug.com/677916 | 420 // This reproduces what might be the conditions of http://crbug.com/677916 |
404 // | 421 // |
405 // TODO(crbug.com/682047): The call sequence below should not occur outside | 422 // TODO(crbug.com/682047): The call sequence below should not occur outside |
406 // this contrived test. Given the current code, it is unclear how the | 423 // this contrived test. Given the current code, it is unclear how the |
407 // dragSourceEndedAt() call could occur before a drag operation is started. | 424 // dragSourceEndedAt() call could occur before a drag operation is started. |
408 | 425 |
409 PlatformMouseEvent mouseUpEvent( | 426 WebMouseEvent mouseUpEvent( |
410 IntPoint(100, 50), IntPoint(200, 250), WebPointerProperties::Button::Left, | 427 WebInputEvent::MouseUp, WebFloatPoint(100, 50), WebFloatPoint(200, 250), |
411 PlatformEvent::MouseReleased, 1, static_cast<PlatformEvent::Modifiers>(0), | 428 WebPointerProperties::Button::Left, 1, |
412 TimeTicks::Now()); | 429 static_cast<PlatformEvent::Modifiers>(0), TimeTicks::Now().InSeconds()); |
| 430 mouseUpEvent.setFrameScale(1); |
413 document().frame()->eventHandler().dragSourceEndedAt(mouseUpEvent, | 431 document().frame()->eventHandler().dragSourceEndedAt(mouseUpEvent, |
414 DragOperationNone); | 432 DragOperationNone); |
415 | 433 |
416 // This test passes if it doesn't crash. | 434 // This test passes if it doesn't crash. |
417 } | 435 } |
418 | 436 |
419 } // namespace blink | 437 } // namespace blink |
OLD | NEW |