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/default_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/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 TestKeyEvent : public ui::KeyEvent {
37 TestKeyEvent(const base::NativeEvent& native_event, int flags)
38 : KeyEvent(native_event) {
43 class TestTouchEvent : public ui::TouchEvent {
45 TestTouchEvent(ui::EventType type,
46 const gfx::Point& root_location,
49 base::TimeDelta timestamp)
50 : TouchEvent(type, root_location, flags, touch_id, timestamp,
51 1.0f, 1.0f, 0.0f, 0.0f) {
55 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent);
58 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON;
62 EventGeneratorDelegate* EventGenerator::default_delegate = NULL;
64 EventGenerator::EventGenerator(gfx::NativeWindow root_window)
65 : current_target_(NULL),
69 tick_clock_(new base::DefaultTickClock()) {
70 Init(root_window, NULL);
73 EventGenerator::EventGenerator(gfx::NativeWindow root_window,
74 const gfx::Point& point)
75 : current_location_(point),
76 current_target_(NULL),
80 tick_clock_(new base::DefaultTickClock()) {
81 Init(root_window, NULL);
84 EventGenerator::EventGenerator(gfx::NativeWindow root_window,
85 gfx::NativeWindow window)
86 : current_target_(NULL),
90 tick_clock_(new base::DefaultTickClock()) {
91 Init(root_window, window);
94 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate)
95 : delegate_(delegate),
96 current_target_(NULL),
100 tick_clock_(new base::DefaultTickClock()) {
104 EventGenerator::~EventGenerator() {
105 for (std::list<ui::Event*>::iterator i = pending_events_.begin();
106 i != pending_events_.end(); ++i)
108 pending_events_.clear();
109 delegate()->SetContext(NULL, NULL, NULL);
112 void EventGenerator::PressLeftButton() {
113 PressButton(ui::EF_LEFT_MOUSE_BUTTON);
116 void EventGenerator::ReleaseLeftButton() {
117 ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON);
120 void EventGenerator::ClickLeftButton() {
125 void EventGenerator::DoubleClickLeftButton() {
126 flags_ |= ui::EF_IS_DOUBLE_CLICK;
128 flags_ ^= ui::EF_IS_DOUBLE_CLICK;
132 void EventGenerator::PressRightButton() {
133 PressButton(ui::EF_RIGHT_MOUSE_BUTTON);
136 void EventGenerator::ReleaseRightButton() {
137 ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON);
140 void EventGenerator::MoveMouseWheel(int delta_x, int delta_y) {
141 gfx::Point location = GetLocationInCurrentRoot();
142 ui::MouseEvent mouseev(ui::ET_MOUSEWHEEL, location, location, flags_, 0);
143 ui::MouseWheelEvent wheelev(mouseev, delta_x, delta_y);
147 void EventGenerator::SendMouseExit() {
148 gfx::Point exit_location(current_location_);
149 delegate()->ConvertPointToTarget(current_target_, &exit_location);
150 ui::MouseEvent mouseev(ui::ET_MOUSE_EXITED, exit_location, exit_location,
155 void EventGenerator::MoveMouseToInHost(const gfx::Point& point_in_host) {
156 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ?
157 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
158 ui::MouseEvent mouseev(event_type, point_in_host, point_in_host, flags_, 0);
161 current_location_ = point_in_host;
162 delegate()->ConvertPointFromHost(current_target_, ¤t_location_);
165 void EventGenerator::MoveMouseTo(const gfx::Point& point_in_screen,
168 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ?
169 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
171 gfx::Vector2dF diff(point_in_screen - current_location_);
172 for (float i = 1; i <= count; i++) {
173 gfx::Vector2dF step(diff);
174 step.Scale(i / count);
175 gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step);
177 UpdateCurrentDispatcher(move_point);
178 delegate()->ConvertPointToTarget(current_target_, &move_point);
179 ui::MouseEvent mouseev(event_type, move_point, move_point, flags_, 0);
182 current_location_ = point_in_screen;
185 void EventGenerator::MoveMouseRelativeTo(const EventTarget* window,
186 const gfx::Point& point_in_parent) {
187 gfx::Point point(point_in_parent);
188 delegate()->ConvertPointFromTarget(window, &point);
192 void EventGenerator::DragMouseTo(const gfx::Point& point) {
198 void EventGenerator::MoveMouseToCenterOf(EventTarget* window) {
199 MoveMouseTo(CenterOfWindow(window));
202 void EventGenerator::PressTouch() {
206 void EventGenerator::PressTouchId(int touch_id) {
207 TestTouchEvent touchev(
208 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags_,
213 void EventGenerator::MoveTouch(const gfx::Point& point) {
214 MoveTouchId(point, 0);
217 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) {
218 current_location_ = point;
219 TestTouchEvent touchev(
220 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags_,
225 UpdateCurrentDispatcher(point);
228 void EventGenerator::ReleaseTouch() {
232 void EventGenerator::ReleaseTouchId(int touch_id) {
233 TestTouchEvent touchev(
234 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags_,
239 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) {
245 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(EventTarget* window) {
246 PressMoveAndReleaseTouchTo(CenterOfWindow(window));
249 void EventGenerator::GestureEdgeSwipe() {
250 ui::GestureEvent gesture(
251 0, 0, 0, Now(), ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE));
255 void EventGenerator::GestureTapAt(const gfx::Point& location) {
256 const int kTouchId = 2;
257 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
263 ui::TouchEvent release(
264 ui::ET_TOUCH_RELEASED, location, kTouchId,
265 press.time_stamp() + base::TimeDelta::FromMilliseconds(50));
269 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) {
270 const int kTouchId = 3;
271 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
277 ui::TouchEvent release(
278 ui::ET_TOUCH_RELEASED, location, kTouchId,
279 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000));
283 void EventGenerator::GestureScrollSequence(const gfx::Point& start,
284 const gfx::Point& end,
285 const base::TimeDelta& step_delay,
287 GestureScrollSequenceWithCallback(start, end, step_delay, steps,
288 base::Bind(&DummyCallback));
291 void EventGenerator::GestureScrollSequenceWithCallback(
292 const gfx::Point& start,
293 const gfx::Point& end,
294 const base::TimeDelta& step_delay,
296 const ScrollStepCallback& callback) {
297 const int kTouchId = 5;
298 base::TimeDelta timestamp = Now();
299 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp);
302 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF());
304 int dx = (end.x() - start.x()) / steps;
305 int dy = (end.y() - start.y()) / steps;
306 gfx::Point location = start;
307 for (int i = 0; i < steps; ++i) {
308 location.Offset(dx, dy);
309 timestamp += step_delay;
310 ui::TouchEvent move(ui::ET_TOUCH_MOVED, location, kTouchId, timestamp);
312 callback.Run(ui::ET_GESTURE_SCROLL_UPDATE, gfx::Vector2dF(dx, dy));
315 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, end, kTouchId, timestamp);
318 callback.Run(ui::ET_GESTURE_SCROLL_END, gfx::Vector2dF());
321 void EventGenerator::GestureMultiFingerScroll(int count,
322 const gfx::Point start[],
323 int event_separation_time_ms,
327 const int kMaxTouchPoints = 10;
328 int delays[kMaxTouchPoints] = { 0 };
329 GestureMultiFingerScrollWithDelays(
330 count, start, delays, event_separation_time_ms, steps, move_x, move_y);
333 void EventGenerator::GestureMultiFingerScrollWithDelays(
335 const gfx::Point start[],
336 const int delay_adding_finger_ms[],
337 int event_separation_time_ms,
341 const int kMaxTouchPoints = 10;
342 gfx::Point points[kMaxTouchPoints];
343 CHECK_LE(count, kMaxTouchPoints);
346 int delta_x = move_x / steps;
347 int delta_y = move_y / steps;
349 for (int i = 0; i < count; ++i) {
350 points[i] = start[i];
353 base::TimeDelta press_time_first = Now();
354 base::TimeDelta press_time[kMaxTouchPoints];
355 bool pressed[kMaxTouchPoints];
356 for (int i = 0; i < count; ++i) {
358 press_time[i] = press_time_first +
359 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]);
363 for (int step = 0; step < steps; ++step) {
364 base::TimeDelta move_time = press_time_first +
365 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step);
367 while (last_id < count &&
369 move_time >= press_time[last_id]) {
370 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
373 press_time[last_id]);
375 pressed[last_id] = true;
379 for (int i = 0; i < count; ++i) {
380 points[i].Offset(delta_x, delta_y);
383 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time);
388 base::TimeDelta release_time = press_time_first +
389 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps);
390 for (int i = 0; i < last_id; ++i) {
391 ui::TouchEvent release(
392 ui::ET_TOUCH_RELEASED, points[i], i, release_time);
397 void EventGenerator::ScrollSequence(const gfx::Point& start,
398 const base::TimeDelta& step_delay,
403 base::TimeDelta timestamp = Now();
404 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
411 Dispatch(&fling_cancel);
413 float dx = x_offset / steps;
414 float dy = y_offset / steps;
415 for (int i = 0; i < steps; ++i) {
416 timestamp += step_delay;
417 ui::ScrollEvent move(ui::ET_SCROLL,
427 ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START,
434 Dispatch(&fling_start);
437 void EventGenerator::ScrollSequence(const gfx::Point& start,
438 const base::TimeDelta& step_delay,
439 const std::vector<gfx::Point>& offsets,
441 size_t steps = offsets.size();
442 base::TimeDelta timestamp = Now();
443 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
450 Dispatch(&fling_cancel);
452 for (size_t i = 0; i < steps; ++i) {
453 timestamp += step_delay;
454 ui::ScrollEvent scroll(ui::ET_SCROLL,
458 offsets[i].x(), offsets[i].y(),
459 offsets[i].x(), offsets[i].y(),
464 ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START,
468 offsets[steps - 1].x(), offsets[steps - 1].y(),
469 offsets[steps - 1].x(), offsets[steps - 1].y(),
471 Dispatch(&fling_start);
474 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) {
475 DispatchKeyEvent(true, key_code, flags);
478 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) {
479 DispatchKeyEvent(false, key_code, flags);
482 void EventGenerator::Dispatch(ui::Event* event) {
483 DoDispatchEvent(event, async_);
486 void EventGenerator::SetTickClock(scoped_ptr<base::TickClock> tick_clock) {
487 tick_clock_ = tick_clock.Pass();
490 base::TimeDelta EventGenerator::Now() {
491 // This is the same as what EventTimeForNow() does, but here we do it
492 // with a tick clock that can be replaced with a simulated clock for tests.
493 return base::TimeDelta::FromInternalValue(
494 tick_clock_->NowTicks().ToInternalValue());
497 void EventGenerator::Init(gfx::NativeWindow root_window,
498 gfx::NativeWindow window_context) {
499 delegate()->SetContext(this, root_window, window_context);
501 current_location_ = delegate()->CenterOfWindow(window_context);
502 current_target_ = delegate()->GetTargetAt(current_location_);
505 void EventGenerator::DispatchKeyEvent(bool is_press,
506 ui::KeyboardCode key_code,
509 UINT key_press = WM_KEYDOWN;
510 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags);
511 if (is_press && character) {
512 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 };
513 TestKeyEvent keyev(native_event, flags);
515 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character
516 // if the key event cooresponds to a real character.
518 key_code = static_cast<ui::KeyboardCode>(character);
521 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 };
522 TestKeyEvent keyev(native_event, flags);
523 #elif defined(USE_X11)
524 ui::ScopedXI2Event xevent;
525 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED,
528 ui::KeyEvent keyev(xevent);
530 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
531 ui::KeyEvent keyev(type, key_code, flags);
536 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) {
537 current_target_ = delegate()->GetTargetAt(point);
540 void EventGenerator::PressButton(int flag) {
541 if (!(flags_ & flag)) {
543 grab_ = (flags_ & kAllButtonMask) != 0;
544 gfx::Point location = GetLocationInCurrentRoot();
545 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_,
551 void EventGenerator::ReleaseButton(int flag) {
553 gfx::Point location = GetLocationInCurrentRoot();
554 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location,
555 location, flags_, flag);
559 grab_ = (flags_ & kAllButtonMask) != 0;
562 gfx::Point EventGenerator::GetLocationInCurrentRoot() const {
563 gfx::Point p(current_location_);
564 delegate()->ConvertPointToTarget(current_target_, &p);
568 gfx::Point EventGenerator::CenterOfWindow(const EventTarget* window) const {
569 return delegate()->CenterOfTarget(window);
572 void EventGenerator::DoDispatchEvent(ui::Event* event, bool async) {
574 ui::Event* pending_event;
575 if (event->IsKeyEvent()) {
576 pending_event = new ui::KeyEvent(*static_cast<ui::KeyEvent*>(event));
577 } else if (event->IsMouseEvent()) {
578 pending_event = new ui::MouseEvent(*static_cast<ui::MouseEvent*>(event));
579 } else if (event->IsTouchEvent()) {
580 pending_event = new ui::TouchEvent(*static_cast<ui::TouchEvent*>(event));
581 } else if (event->IsScrollEvent()) {
583 new ui::ScrollEvent(*static_cast<ui::ScrollEvent*>(event));
585 NOTREACHED() << "Invalid event type";
588 if (pending_events_.empty()) {
589 base::ThreadTaskRunnerHandle::Get()->PostTask(
591 base::Bind(&EventGenerator::DispatchNextPendingEvent,
592 base::Unretained(this)));
594 pending_events_.push_back(pending_event);
596 ui::EventSource* event_source = delegate()->GetEventSource(current_target_);
597 ui::EventSourceTestApi event_source_test(event_source);
598 ui::EventDispatchDetails details =
599 event_source_test.SendEventToProcessor(event);
600 CHECK(!details.dispatcher_destroyed);
604 void EventGenerator::DispatchNextPendingEvent() {
605 DCHECK(!pending_events_.empty());
606 ui::Event* event = pending_events_.front();
607 DoDispatchEvent(event, false);
608 pending_events_.pop_front();
610 if (!pending_events_.empty()) {
611 base::ThreadTaskRunnerHandle::Get()->PostTask(
613 base::Bind(&EventGenerator::DispatchNextPendingEvent,
614 base::Unretained(this)));
618 const EventGeneratorDelegate* EventGenerator::delegate() const {
620 return delegate_.get();
622 DCHECK(default_delegate);
623 return default_delegate;
626 EventGeneratorDelegate* EventGenerator::delegate() {
627 return const_cast<EventGeneratorDelegate*>(
628 const_cast<const EventGenerator*>(this)->delegate());