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 #ifndef UI_EVENTS_TEST_EVENT_GENERATOR_H_
6 #define UI_EVENTS_TEST_EVENT_GENERATOR_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/keycodes/keyboard_codes.h"
17 #include "ui/gfx/geometry/point.h"
18 #include "ui/gfx/native_widget_types.h"
36 typedef base::Callback
<void(EventType
, const gfx::Vector2dF
&)>
41 // A delegate interface for EventGenerator to abstract platform-specific event
42 // targeting and coordinate conversion.
43 class EventGeneratorDelegate
{
45 virtual ~EventGeneratorDelegate() {}
47 // Set the context of the delegate, whilst it is being used by an active
49 virtual void SetContext(EventGenerator
* owner
,
50 gfx::NativeWindow root_window
,
51 gfx::NativeWindow window
) {}
53 // The ui::EventTarget at the given |location|.
54 virtual EventTarget
* GetTargetAt(const gfx::Point
& location
) = 0;
56 // The ui::EventSource for the given |target|.
57 virtual EventSource
* GetEventSource(EventTarget
* target
) = 0;
59 // Helper functions to determine the center point of |target| or |window|.
60 virtual gfx::Point
CenterOfTarget(const EventTarget
* target
) const = 0;
61 virtual gfx::Point
CenterOfWindow(gfx::NativeWindow window
) const = 0;
63 // Convert a point between API's coordinates and |target|'s coordinates.
64 virtual void ConvertPointFromTarget(const EventTarget
* target
,
65 gfx::Point
* point
) const = 0;
66 virtual void ConvertPointToTarget(const EventTarget
* target
,
67 gfx::Point
* point
) const = 0;
69 // Convert a point from the coordinate system in the host that contains
70 // |hosted_target| into the root window's coordinate system.
71 virtual void ConvertPointFromHost(const EventTarget
* hosted_target
,
72 gfx::Point
* point
) const = 0;
75 // ui::test::EventGenerator is a tool that generates and dispatches events.
76 // Unlike |ui_controls| package in ui/base/test, this does not use platform
77 // native message loops. Instead, it sends events to the event dispatcher
80 // This class is not suited for the following cases:
82 // 1) If your test depends on native events (ui::Event::native_event()).
83 // This return is empty/NULL event with EventGenerator.
84 // 2) If your test involves nested message loop, such as
85 // menu or drag & drop. Because this class directly
86 // post an event to WindowEventDispatcher, this event will not be
87 // handled in the nested message loop.
88 // 3) Similarly, |base::MessagePumpObserver| will not be invoked.
89 // 4) Any other code that requires native message loops, such as
90 // tests for WindowTreeHostWin/WindowTreeHostX11.
92 // If one of these applies to your test, please use |ui_controls|
95 // Note: The coordinates of the points in API is determined by the
96 // EventGeneratorDelegate.
97 class EventGenerator
{
99 // Creates an EventGenerator with the mouse/touch location (0,0),
100 // which uses the |root_window|'s coordinates and the default delegate for
102 explicit EventGenerator(gfx::NativeWindow root_window
);
104 // Create an EventGenerator with EventGeneratorDelegate,
105 // which uses the coordinates conversions and targeting provided by
107 explicit EventGenerator(EventGeneratorDelegate
* delegate
);
109 // Creates an EventGenerator with the mouse/touch location
110 // at |initial_location|, which uses the |root_window|'s coordinates.
111 EventGenerator(gfx::NativeWindow root_window
,
112 const gfx::Point
& initial_location
);
114 // Creates an EventGenerator with the mouse/touch location centered over
115 // |window|. This is currently the only constructor that works on Mac, since
116 // a specific window is required (and there is no root window).
117 EventGenerator(gfx::NativeWindow root_window
, gfx::NativeWindow window
);
119 virtual ~EventGenerator();
121 // Explicitly sets the location used by mouse/touch events. This is set by the
122 // various methods that take a location but can be manipulated directly,
123 // typically for touch.
124 void set_current_location(const gfx::Point
& location
) {
125 current_location_
= location
;
127 const gfx::Point
& current_location() const { return current_location_
; }
129 void set_async(bool async
) { async_
= async
; }
130 bool async() const { return async_
; }
132 // Dispatch events through the application instead of directly to the
133 // target window. Currently only supported on Mac.
134 void set_targeting_application(bool targeting_application
) {
135 targeting_application_
= targeting_application
;
137 bool targeting_application() const { return targeting_application_
; }
139 // Resets the event flags bitmask.
140 void set_flags(int flags
) { flags_
= flags
; }
141 int flags() const { return flags_
; }
143 // Generates a left button press event.
144 void PressLeftButton();
146 // Generates a left button release event.
147 void ReleaseLeftButton();
149 // Generates events to click (press, release) left button.
150 void ClickLeftButton();
152 // Generates a double click event using the left button.
153 void DoubleClickLeftButton();
155 // Generates a right button press event.
156 void PressRightButton();
158 // Generates a right button release event.
159 void ReleaseRightButton();
161 // Moves the mouse wheel by |delta_x|, |delta_y|.
162 void MoveMouseWheel(int delta_x
, int delta_y
);
164 // Generates a mouse exit.
165 void SendMouseExit();
167 // Generates events to move mouse to be the given |point| in the
168 // |current_root_window_|'s host window coordinates.
169 void MoveMouseToInHost(const gfx::Point
& point_in_host
);
170 void MoveMouseToInHost(int x
, int y
) {
171 MoveMouseToInHost(gfx::Point(x
, y
));
174 // Generates a mouse move event at the point given in the host
175 // coordinates, with a native event with |point_for_natve|.
176 void MoveMouseToWithNative(const gfx::Point
& point_in_host
,
177 const gfx::Point
& point_for_native
);
179 // Generates events to move mouse to be the given |point| in screen
181 void MoveMouseTo(const gfx::Point
& point_in_screen
, int count
);
182 void MoveMouseTo(const gfx::Point
& point_in_screen
) {
183 MoveMouseTo(point_in_screen
, 1);
185 void MoveMouseTo(int x
, int y
) {
186 MoveMouseTo(gfx::Point(x
, y
));
189 // Generates events to move mouse to be the given |point| in |window|'s
191 void MoveMouseRelativeTo(const EventTarget
* window
, const gfx::Point
& point
);
192 void MoveMouseRelativeTo(const EventTarget
* window
, int x
, int y
) {
193 MoveMouseRelativeTo(window
, gfx::Point(x
, y
));
196 void MoveMouseBy(int x
, int y
) {
197 MoveMouseTo(current_location_
+ gfx::Vector2d(x
, y
));
200 // Generates events to drag mouse to given |point|.
201 void DragMouseTo(const gfx::Point
& point
);
203 void DragMouseTo(int x
, int y
) {
204 DragMouseTo(gfx::Point(x
, y
));
207 void DragMouseBy(int dx
, int dy
) {
208 DragMouseTo(current_location_
+ gfx::Vector2d(dx
, dy
));
211 // Generates events to move the mouse to the center of the window.
212 void MoveMouseToCenterOf(EventTarget
* window
);
214 // Generates a touch press event.
217 // Generates a touch press event with |touch_id|.
218 void PressTouchId(int touch_id
);
220 // Generates a ET_TOUCH_MOVED event to |point|.
221 void MoveTouch(const gfx::Point
& point
);
223 // Generates a ET_TOUCH_MOVED event to |point| with |touch_id|.
224 void MoveTouchId(const gfx::Point
& point
, int touch_id
);
226 // Generates a touch release event.
229 // Generates a touch release event with |touch_id|.
230 void ReleaseTouchId(int touch_id
);
232 // Generates press, move and release event to move touch
233 // to be the given |point|.
234 void PressMoveAndReleaseTouchTo(const gfx::Point
& point
);
236 void PressMoveAndReleaseTouchTo(int x
, int y
) {
237 PressMoveAndReleaseTouchTo(gfx::Point(x
, y
));
240 void PressMoveAndReleaseTouchBy(int x
, int y
) {
241 PressMoveAndReleaseTouchTo(current_location_
+ gfx::Vector2d(x
, y
));
244 // Generates press, move and release events to move touch
245 // to the center of the window.
246 void PressMoveAndReleaseTouchToCenterOf(EventTarget
* window
);
248 // Generates and dispatches a Win8 edge-swipe event (swipe up from bottom or
249 // swipe down from top). Note that it is not possible to distinguish between
250 // the two edges with this event.
251 void GestureEdgeSwipe();
253 // Generates and dispatches touch-events required to generate a TAP gesture.
254 // Note that this can generate a number of other gesture events at the same
255 // time (e.g. GESTURE_BEGIN, TAP_DOWN, END).
256 void GestureTapAt(const gfx::Point
& point
);
258 // Generates press and release touch-events to generate a TAP_DOWN event, but
259 // without generating any scroll or tap events. This can also generate a few
260 // other gesture events (e.g. GESTURE_BEGIN, END).
261 void GestureTapDownAndUp(const gfx::Point
& point
);
263 // Calculates a time duration that can be used with the given |start|, |end|,
264 // and |steps| values when calling GestureScrollSequence (or
265 // GestureScrollSequenceWithCallback) to achieve the given |velocity|.
266 base::TimeDelta
CalculateScrollDurationForFlingVelocity(
267 const gfx::Point
& start
,
268 const gfx::Point
& end
,
272 // Generates press, move, release touch-events to generate a sequence of
273 // scroll events. |duration| and |steps| affect the velocity of the scroll,
274 // and depending on these values, this may also generate FLING scroll
275 // gestures. If velocity/fling is irrelevant for the test, then any non-zero
276 // values for these should be sufficient.
277 void GestureScrollSequence(const gfx::Point
& start
,
278 const gfx::Point
& end
,
279 const base::TimeDelta
& duration
,
282 // The same as GestureScrollSequence(), with the exception that |callback| is
283 // called at each step of the scroll sequence. |callback| is called at the
284 // start of the sequence with ET_GESTURE_SCROLL_BEGIN, followed by one or more
285 // ET_GESTURE_SCROLL_UPDATE and ends with an ET_GESTURE_SCROLL_END.
286 void GestureScrollSequenceWithCallback(const gfx::Point
& start
,
287 const gfx::Point
& end
,
288 const base::TimeDelta
& duration
,
290 const ScrollStepCallback
& callback
);
292 // Generates press, move, release touch-events to generate a sequence of
293 // multi-finger scroll events. |count| specifies the number of touch-points
294 // that should generate the scroll events. |start| are the starting positions
295 // of all the touch points. |steps| and |event_separation_time_ms| are
296 // relevant when testing velocity/fling/swipe, otherwise these can be any
297 // non-zero value. |delta_x| and |delta_y| are the amount that each finger
298 // should be moved. Internally calls GestureMultiFingerScrollWithDelays
299 // with zeros as |delay_adding_finger_ms| forcing all touch down events to be
301 void GestureMultiFingerScroll(int count
,
302 const gfx::Point start
[],
303 int event_separation_time_ms
,
308 // Generates press, move, release touch-events to generate a sequence of
309 // multi-finger scroll events. |count| specifies the number of touch-points
310 // that should generate the scroll events. |start| are the starting positions
311 // of all the touch points. |delay_adding_finger_ms| are delays in ms from the
312 // starting time till touching down of each finger. |delay_adding_finger_ms|
313 // is useful when testing complex gestures that start with 1 or 2 fingers and
314 // add fingers with a delay. |steps| and |event_separation_time_ms| are
315 // relevant when testing velocity/fling/swipe, otherwise these can be any
316 // non-zero value. |delta_x| and |delta_y| are the amount that each finger
318 void GestureMultiFingerScrollWithDelays(int count
,
319 const gfx::Point start
[],
320 const int delay_adding_finger_ms
[],
321 int event_separation_time_ms
,
326 // Generates scroll sequences of a FlingCancel, Scrolls, FlingStart, with
327 // constant deltas to |x_offset| and |y_offset| in |steps|.
328 void ScrollSequence(const gfx::Point
& start
,
329 const base::TimeDelta
& step_delay
,
335 // Generates scroll sequences of a FlingCancel, Scrolls, FlingStart, sending
336 // scrolls of each of the values in |offsets|.
337 void ScrollSequence(const gfx::Point
& start
,
338 const base::TimeDelta
& step_delay
,
339 const std::vector
<gfx::PointF
>& offsets
,
342 // Generates a key press event. On platforms except Windows and X11, a key
343 // event without native_event() is generated. Note that ui::EF_ flags should
344 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>.
345 // TODO(yusukes): Support native_event() on all platforms.
346 void PressKey(KeyboardCode key_code
, int flags
);
348 // Generates a key release event. On platforms except Windows and X11, a key
349 // event without native_event() is generated. Note that ui::EF_ flags should
350 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>.
351 // TODO(yusukes): Support native_event() on all platforms.
352 void ReleaseKey(KeyboardCode key_code
, int flags
);
354 // Dispatch the event to the WindowEventDispatcher.
355 void Dispatch(Event
* event
);
357 void set_current_target(EventTarget
* target
) {
358 current_target_
= target
;
361 // Specify an alternative tick clock to be used for simulating time in tests.
362 void SetTickClock(scoped_ptr
<base::TickClock
> tick_clock
);
364 // Get the current time from the tick clock.
365 base::TimeDelta
Now();
367 // Default delegate set by a platform-specific GeneratorDelegate singleton.
368 static EventGeneratorDelegate
* default_delegate
;
371 // Set up the test context using the delegate.
372 void Init(gfx::NativeWindow root_window
, gfx::NativeWindow window_context
);
374 // Dispatch a key event to the WindowEventDispatcher.
375 void DispatchKeyEvent(bool is_press
, KeyboardCode key_code
, int flags
);
377 void UpdateCurrentDispatcher(const gfx::Point
& point
);
378 void PressButton(int flag
);
379 void ReleaseButton(int flag
);
381 gfx::Point
GetLocationInCurrentRoot() const;
382 gfx::Point
CenterOfWindow(const EventTarget
* window
) const;
384 void DispatchNextPendingEvent();
385 void DoDispatchEvent(Event
* event
, bool async
);
387 const EventGeneratorDelegate
* delegate() const;
388 EventGeneratorDelegate
* delegate();
390 scoped_ptr
<EventGeneratorDelegate
> delegate_
;
391 gfx::Point current_location_
;
392 EventTarget
* current_target_
;
395 std::list
<Event
*> pending_events_
;
396 // Set to true to cause events to be posted asynchronously.
398 bool targeting_application_
;
399 scoped_ptr
<base::TickClock
> tick_clock_
;
401 DISALLOW_COPY_AND_ASSIGN(EventGenerator
);
407 #endif // UI_EVENTS_TEST_EVENT_GENERATOR_H_