1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/test/event_generator.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/tick_clock.h"
13 #include "ui/events/event.h"
14 #include "ui/events/event_source.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/events/test/events_test_utils.h"
17 #include "ui/gfx/geometry/vector2d_conversions.h"
21 #include "ui/events/test/events_test_utils_x11.h"
25 #include "ui/events/keycodes/keyboard_code_conversion.h"
32 void DummyCallback(EventType
, const gfx::Vector2dF
&) {
35 class TestTickClock
: public base::TickClock
{
37 // Starts off with a clock set to TimeTicks().
40 base::TimeTicks
NowTicks() override
{
41 return base::TimeTicks::FromInternalValue(ticks_
++ * 1000);
47 DISALLOW_COPY_AND_ASSIGN(TestTickClock
);
50 class TestKeyEvent
: public ui::KeyEvent
{
52 TestKeyEvent(const base::NativeEvent
& native_event
, int flags
)
53 : KeyEvent(native_event
) {
58 class TestTouchEvent
: public ui::TouchEvent
{
60 TestTouchEvent(ui::EventType type
,
61 const gfx::Point
& root_location
,
64 base::TimeDelta timestamp
)
65 : TouchEvent(type
, root_location
, flags
, touch_id
, timestamp
,
66 1.0f
, 1.0f
, 0.0f
, 0.0f
) {
70 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent
);
73 const int kAllButtonMask
= ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
;
77 EventGeneratorDelegate
* EventGenerator::default_delegate
= NULL
;
79 EventGenerator::EventGenerator(gfx::NativeWindow root_window
)
80 : current_target_(NULL
),
84 targeting_application_(false),
85 tick_clock_(new TestTickClock()) {
86 Init(root_window
, NULL
);
89 EventGenerator::EventGenerator(gfx::NativeWindow root_window
,
90 const gfx::Point
& point
)
91 : current_location_(point
),
92 current_target_(NULL
),
96 targeting_application_(false),
97 tick_clock_(new TestTickClock()) {
98 Init(root_window
, NULL
);
101 EventGenerator::EventGenerator(gfx::NativeWindow root_window
,
102 gfx::NativeWindow window
)
103 : current_target_(NULL
),
107 targeting_application_(false),
108 tick_clock_(new TestTickClock()) {
109 Init(root_window
, window
);
112 EventGenerator::EventGenerator(EventGeneratorDelegate
* delegate
)
113 : delegate_(delegate
),
114 current_target_(NULL
),
118 targeting_application_(false),
119 tick_clock_(new TestTickClock()) {
123 EventGenerator::~EventGenerator() {
124 for (std::list
<ui::Event
*>::iterator i
= pending_events_
.begin();
125 i
!= pending_events_
.end(); ++i
)
127 pending_events_
.clear();
128 delegate()->SetContext(NULL
, NULL
, NULL
);
131 void EventGenerator::PressLeftButton() {
132 PressButton(ui::EF_LEFT_MOUSE_BUTTON
);
135 void EventGenerator::ReleaseLeftButton() {
136 ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON
);
139 void EventGenerator::ClickLeftButton() {
144 void EventGenerator::DoubleClickLeftButton() {
145 flags_
&= ~ui::EF_IS_DOUBLE_CLICK
;
147 flags_
|= ui::EF_IS_DOUBLE_CLICK
;
149 flags_
&= ~ui::EF_IS_DOUBLE_CLICK
;
152 void EventGenerator::PressRightButton() {
153 PressButton(ui::EF_RIGHT_MOUSE_BUTTON
);
156 void EventGenerator::ReleaseRightButton() {
157 ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON
);
160 void EventGenerator::MoveMouseWheel(int delta_x
, int delta_y
) {
161 gfx::Point location
= GetLocationInCurrentRoot();
162 ui::MouseEvent
mouseev(ui::ET_MOUSEWHEEL
, location
, location
,
163 ui::EventTimeForNow(), flags_
, 0);
164 ui::MouseWheelEvent
wheelev(mouseev
, delta_x
, delta_y
);
168 void EventGenerator::SendMouseExit() {
169 gfx::Point
exit_location(current_location_
);
170 delegate()->ConvertPointToTarget(current_target_
, &exit_location
);
171 ui::MouseEvent
mouseev(ui::ET_MOUSE_EXITED
, exit_location
, exit_location
,
172 ui::EventTimeForNow(), flags_
, 0);
176 void EventGenerator::MoveMouseToInHost(const gfx::Point
& point_in_host
) {
177 const ui::EventType event_type
= (flags_
& ui::EF_LEFT_MOUSE_BUTTON
) ?
178 ui::ET_MOUSE_DRAGGED
: ui::ET_MOUSE_MOVED
;
179 ui::MouseEvent
mouseev(event_type
, point_in_host
, point_in_host
,
180 ui::EventTimeForNow(), flags_
, 0);
183 current_location_
= point_in_host
;
184 delegate()->ConvertPointFromHost(current_target_
, ¤t_location_
);
187 void EventGenerator::MoveMouseTo(const gfx::Point
& point_in_screen
,
190 const ui::EventType event_type
= (flags_
& ui::EF_LEFT_MOUSE_BUTTON
) ?
191 ui::ET_MOUSE_DRAGGED
: ui::ET_MOUSE_MOVED
;
193 gfx::Vector2dF
diff(point_in_screen
- current_location_
);
194 for (float i
= 1; i
<= count
; i
++) {
195 gfx::Vector2dF
step(diff
);
196 step
.Scale(i
/ count
);
197 gfx::Point move_point
= current_location_
+ gfx::ToRoundedVector2d(step
);
199 UpdateCurrentDispatcher(move_point
);
200 delegate()->ConvertPointToTarget(current_target_
, &move_point
);
201 ui::MouseEvent
mouseev(event_type
, move_point
, move_point
,
202 ui::EventTimeForNow(), flags_
, 0);
205 current_location_
= point_in_screen
;
208 void EventGenerator::MoveMouseRelativeTo(const EventTarget
* window
,
209 const gfx::Point
& point_in_parent
) {
210 gfx::Point
point(point_in_parent
);
211 delegate()->ConvertPointFromTarget(window
, &point
);
215 void EventGenerator::DragMouseTo(const gfx::Point
& point
) {
221 void EventGenerator::MoveMouseToCenterOf(EventTarget
* window
) {
222 MoveMouseTo(CenterOfWindow(window
));
225 void EventGenerator::PressTouch() {
229 void EventGenerator::PressTouchId(int touch_id
) {
230 TestTouchEvent
touchev(
231 ui::ET_TOUCH_PRESSED
, GetLocationInCurrentRoot(), touch_id
, flags_
,
236 void EventGenerator::MoveTouch(const gfx::Point
& point
) {
237 MoveTouchId(point
, 0);
240 void EventGenerator::MoveTouchId(const gfx::Point
& point
, int touch_id
) {
241 current_location_
= point
;
242 TestTouchEvent
touchev(
243 ui::ET_TOUCH_MOVED
, GetLocationInCurrentRoot(), touch_id
, flags_
,
248 UpdateCurrentDispatcher(point
);
251 void EventGenerator::ReleaseTouch() {
255 void EventGenerator::ReleaseTouchId(int touch_id
) {
256 TestTouchEvent
touchev(
257 ui::ET_TOUCH_RELEASED
, GetLocationInCurrentRoot(), touch_id
, flags_
,
262 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point
& point
) {
268 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(EventTarget
* window
) {
269 PressMoveAndReleaseTouchTo(CenterOfWindow(window
));
272 void EventGenerator::GestureEdgeSwipe() {
273 ui::GestureEvent
gesture(
274 0, 0, 0, Now(), ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE
));
278 void EventGenerator::GestureTapAt(const gfx::Point
& location
) {
279 const int kTouchId
= 2;
280 ui::TouchEvent
press(ui::ET_TOUCH_PRESSED
,
286 ui::TouchEvent
release(
287 ui::ET_TOUCH_RELEASED
, location
, kTouchId
,
288 press
.time_stamp() + base::TimeDelta::FromMilliseconds(50));
292 void EventGenerator::GestureTapDownAndUp(const gfx::Point
& location
) {
293 const int kTouchId
= 3;
294 ui::TouchEvent
press(ui::ET_TOUCH_PRESSED
,
300 ui::TouchEvent
release(
301 ui::ET_TOUCH_RELEASED
, location
, kTouchId
,
302 press
.time_stamp() + base::TimeDelta::FromMilliseconds(1000));
306 base::TimeDelta
EventGenerator::CalculateScrollDurationForFlingVelocity(
307 const gfx::Point
& start
,
308 const gfx::Point
& end
,
311 const float kGestureDistance
= (start
- end
).Length();
312 const float kFlingStepDelay
= (kGestureDistance
/ velocity
) / steps
* 1000000;
313 return base::TimeDelta::FromMicroseconds(kFlingStepDelay
);
316 void EventGenerator::GestureScrollSequence(const gfx::Point
& start
,
317 const gfx::Point
& end
,
318 const base::TimeDelta
& step_delay
,
320 GestureScrollSequenceWithCallback(start
, end
, step_delay
, steps
,
321 base::Bind(&DummyCallback
));
324 void EventGenerator::GestureScrollSequenceWithCallback(
325 const gfx::Point
& start
,
326 const gfx::Point
& end
,
327 const base::TimeDelta
& step_delay
,
329 const ScrollStepCallback
& callback
) {
330 const int kTouchId
= 5;
331 base::TimeDelta timestamp
= Now();
332 ui::TouchEvent
press(ui::ET_TOUCH_PRESSED
, start
, kTouchId
, timestamp
);
335 callback
.Run(ui::ET_GESTURE_SCROLL_BEGIN
, gfx::Vector2dF());
337 float dx
= static_cast<float>(end
.x() - start
.x()) / steps
;
338 float dy
= static_cast<float>(end
.y() - start
.y()) / steps
;
339 gfx::PointF location
= start
;
340 for (int i
= 0; i
< steps
; ++i
) {
341 location
.Offset(dx
, dy
);
342 timestamp
+= step_delay
;
343 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
, location
, kTouchId
, timestamp
);
345 callback
.Run(ui::ET_GESTURE_SCROLL_UPDATE
, gfx::Vector2dF(dx
, dy
));
348 ui::TouchEvent
release(ui::ET_TOUCH_RELEASED
, end
, kTouchId
, timestamp
);
351 callback
.Run(ui::ET_GESTURE_SCROLL_END
, gfx::Vector2dF());
354 void EventGenerator::GestureMultiFingerScroll(int count
,
355 const gfx::Point start
[],
356 int event_separation_time_ms
,
360 const int kMaxTouchPoints
= 10;
361 int delays
[kMaxTouchPoints
] = { 0 };
362 GestureMultiFingerScrollWithDelays(
363 count
, start
, delays
, event_separation_time_ms
, steps
, move_x
, move_y
);
366 void EventGenerator::GestureMultiFingerScrollWithDelays(
368 const gfx::Point start
[],
369 const int delay_adding_finger_ms
[],
370 int event_separation_time_ms
,
374 const int kMaxTouchPoints
= 10;
375 gfx::Point points
[kMaxTouchPoints
];
376 CHECK_LE(count
, kMaxTouchPoints
);
379 int delta_x
= move_x
/ steps
;
380 int delta_y
= move_y
/ steps
;
382 for (int i
= 0; i
< count
; ++i
) {
383 points
[i
] = start
[i
];
386 base::TimeDelta press_time_first
= Now();
387 base::TimeDelta press_time
[kMaxTouchPoints
];
388 bool pressed
[kMaxTouchPoints
];
389 for (int i
= 0; i
< count
; ++i
) {
391 press_time
[i
] = press_time_first
+
392 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms
[i
]);
396 for (int step
= 0; step
< steps
; ++step
) {
397 base::TimeDelta move_time
= press_time_first
+
398 base::TimeDelta::FromMilliseconds(event_separation_time_ms
* step
);
400 while (last_id
< count
&&
402 move_time
>= press_time
[last_id
]) {
403 ui::TouchEvent
press(ui::ET_TOUCH_PRESSED
,
406 press_time
[last_id
]);
408 pressed
[last_id
] = true;
412 for (int i
= 0; i
< count
; ++i
) {
413 points
[i
].Offset(delta_x
, delta_y
);
416 ui::TouchEvent
move(ui::ET_TOUCH_MOVED
, points
[i
], i
, move_time
);
421 base::TimeDelta release_time
= press_time_first
+
422 base::TimeDelta::FromMilliseconds(event_separation_time_ms
* steps
);
423 for (int i
= 0; i
< last_id
; ++i
) {
424 ui::TouchEvent
release(
425 ui::ET_TOUCH_RELEASED
, points
[i
], i
, release_time
);
430 void EventGenerator::ScrollSequence(const gfx::Point
& start
,
431 const base::TimeDelta
& step_delay
,
436 base::TimeDelta timestamp
= Now();
437 ui::ScrollEvent
fling_cancel(ui::ET_SCROLL_FLING_CANCEL
,
444 Dispatch(&fling_cancel
);
446 float dx
= x_offset
/ steps
;
447 float dy
= y_offset
/ steps
;
448 for (int i
= 0; i
< steps
; ++i
) {
449 timestamp
+= step_delay
;
450 ui::ScrollEvent
move(ui::ET_SCROLL
,
460 ui::ScrollEvent
fling_start(ui::ET_SCROLL_FLING_START
,
467 Dispatch(&fling_start
);
470 void EventGenerator::ScrollSequence(const gfx::Point
& start
,
471 const base::TimeDelta
& step_delay
,
472 const std::vector
<gfx::PointF
>& offsets
,
474 size_t steps
= offsets
.size();
475 base::TimeDelta timestamp
= Now();
476 ui::ScrollEvent
fling_cancel(ui::ET_SCROLL_FLING_CANCEL
,
483 Dispatch(&fling_cancel
);
485 for (size_t i
= 0; i
< steps
; ++i
) {
486 timestamp
+= step_delay
;
487 ui::ScrollEvent
scroll(ui::ET_SCROLL
,
491 offsets
[i
].x(), offsets
[i
].y(),
492 offsets
[i
].x(), offsets
[i
].y(),
497 ui::ScrollEvent
fling_start(ui::ET_SCROLL_FLING_START
,
501 offsets
[steps
- 1].x(), offsets
[steps
- 1].y(),
502 offsets
[steps
- 1].x(), offsets
[steps
- 1].y(),
504 Dispatch(&fling_start
);
507 void EventGenerator::PressKey(ui::KeyboardCode key_code
, int flags
) {
508 DispatchKeyEvent(true, key_code
, flags
);
511 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code
, int flags
) {
512 DispatchKeyEvent(false, key_code
, flags
);
515 void EventGenerator::Dispatch(ui::Event
* event
) {
516 DoDispatchEvent(event
, async_
);
519 void EventGenerator::SetTickClock(scoped_ptr
<base::TickClock
> tick_clock
) {
520 tick_clock_
= tick_clock
.Pass();
523 base::TimeDelta
EventGenerator::Now() {
524 // This is the same as what EventTimeForNow() does, but here we do it
525 // with a tick clock that can be replaced with a simulated clock for tests.
526 return base::TimeDelta::FromInternalValue(
527 tick_clock_
->NowTicks().ToInternalValue());
530 void EventGenerator::Init(gfx::NativeWindow root_window
,
531 gfx::NativeWindow window_context
) {
532 delegate()->SetContext(this, root_window
, window_context
);
534 current_location_
= delegate()->CenterOfWindow(window_context
);
535 current_target_
= delegate()->GetTargetAt(current_location_
);
538 void EventGenerator::DispatchKeyEvent(bool is_press
,
539 ui::KeyboardCode key_code
,
542 UINT key_press
= WM_KEYDOWN
;
543 uint16 character
= ui::GetCharacterFromKeyCode(key_code
, flags
);
544 if (is_press
&& character
) {
545 MSG native_event
= { NULL
, WM_KEYDOWN
, key_code
, 0 };
546 TestKeyEvent
keyev(native_event
, flags
);
548 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character
549 // if the key event cooresponds to a real character.
551 key_code
= static_cast<ui::KeyboardCode
>(character
);
554 { NULL
, (is_press
? key_press
: WM_KEYUP
), key_code
, 0 };
555 native_event
.time
= Now().InMicroseconds();
556 TestKeyEvent
keyev(native_event
, flags
);
557 #elif defined(USE_X11)
558 ui::ScopedXI2Event xevent
;
559 xevent
.InitKeyEvent(is_press
? ui::ET_KEY_PRESSED
: ui::ET_KEY_RELEASED
,
562 static_cast<XEvent
*>(xevent
)->xkey
.time
= Now().InMicroseconds();
563 ui::KeyEvent
keyev(xevent
);
565 ui::EventType type
= is_press
? ui::ET_KEY_PRESSED
: ui::ET_KEY_RELEASED
;
566 ui::KeyEvent
keyev(type
, key_code
, flags
);
571 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point
& point
) {
572 current_target_
= delegate()->GetTargetAt(point
);
575 void EventGenerator::PressButton(int flag
) {
576 if (!(flags_
& flag
)) {
578 grab_
= (flags_
& kAllButtonMask
) != 0;
579 gfx::Point location
= GetLocationInCurrentRoot();
580 ui::MouseEvent
mouseev(ui::ET_MOUSE_PRESSED
, location
, location
,
581 ui::EventTimeForNow(), flags_
, flag
);
586 void EventGenerator::ReleaseButton(int flag
) {
588 gfx::Point location
= GetLocationInCurrentRoot();
589 ui::MouseEvent
mouseev(ui::ET_MOUSE_RELEASED
, location
, location
,
590 ui::EventTimeForNow(), flags_
, flag
);
594 grab_
= (flags_
& kAllButtonMask
) != 0;
597 gfx::Point
EventGenerator::GetLocationInCurrentRoot() const {
598 gfx::Point
p(current_location_
);
599 delegate()->ConvertPointToTarget(current_target_
, &p
);
603 gfx::Point
EventGenerator::CenterOfWindow(const EventTarget
* window
) const {
604 return delegate()->CenterOfTarget(window
);
607 void EventGenerator::DoDispatchEvent(ui::Event
* event
, bool async
) {
609 ui::Event
* pending_event
;
610 if (event
->IsKeyEvent()) {
611 pending_event
= new ui::KeyEvent(*static_cast<ui::KeyEvent
*>(event
));
612 } else if (event
->IsMouseEvent()) {
613 pending_event
= new ui::MouseEvent(*static_cast<ui::MouseEvent
*>(event
));
614 } else if (event
->IsTouchEvent()) {
615 pending_event
= new ui::TouchEvent(*static_cast<ui::TouchEvent
*>(event
));
616 } else if (event
->IsScrollEvent()) {
618 new ui::ScrollEvent(*static_cast<ui::ScrollEvent
*>(event
));
620 NOTREACHED() << "Invalid event type";
623 if (pending_events_
.empty()) {
624 base::ThreadTaskRunnerHandle::Get()->PostTask(
626 base::Bind(&EventGenerator::DispatchNextPendingEvent
,
627 base::Unretained(this)));
629 pending_events_
.push_back(pending_event
);
631 ui::EventSource
* event_source
= delegate()->GetEventSource(current_target_
);
632 ui::EventSourceTestApi
event_source_test(event_source
);
633 ui::EventDispatchDetails details
=
634 event_source_test
.SendEventToProcessor(event
);
635 CHECK(!details
.dispatcher_destroyed
);
639 void EventGenerator::DispatchNextPendingEvent() {
640 DCHECK(!pending_events_
.empty());
641 ui::Event
* event
= pending_events_
.front();
642 DoDispatchEvent(event
, false);
643 pending_events_
.pop_front();
645 if (!pending_events_
.empty()) {
646 base::ThreadTaskRunnerHandle::Get()->PostTask(
648 base::Bind(&EventGenerator::DispatchNextPendingEvent
,
649 base::Unretained(this)));
653 const EventGeneratorDelegate
* EventGenerator::delegate() const {
655 return delegate_
.get();
657 DCHECK(default_delegate
);
658 return default_delegate
;
661 EventGeneratorDelegate
* EventGenerator::delegate() {
662 return const_cast<EventGeneratorDelegate
*>(
663 const_cast<const EventGenerator
*>(this)->delegate());