OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef UI_AURA_TEST_EVENT_GENERATOR_H_ | 5 #ifndef UI_EVENTS_TEST_EVENT_GENERATOR_H_ |
6 #define UI_AURA_TEST_EVENT_GENERATOR_H_ | 6 #define UI_EVENTS_TEST_EVENT_GENERATOR_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "ui/events/event_constants.h" | 15 #include "ui/events/event_constants.h" |
16 #include "ui/events/keycodes/keyboard_codes.h" | 16 #include "ui/events/keycodes/keyboard_codes.h" |
| 17 #include "ui/gfx/native_widget_types.h" |
17 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class TickClock; | 21 class TickClock; |
21 } | 22 } |
22 | 23 |
23 namespace ui { | 24 namespace ui { |
24 class Event; | 25 class Event; |
25 class EventProcessor; | 26 class EventProcessor; |
| 27 class EventSource; |
| 28 class EventTarget; |
26 class KeyEvent; | 29 class KeyEvent; |
27 class MouseEvent; | 30 class MouseEvent; |
28 class ScrollEvent; | 31 class ScrollEvent; |
29 class TouchEvent; | 32 class TouchEvent; |
30 } | |
31 | |
32 namespace aura { | |
33 class Window; | |
34 class WindowTreeHost; | |
35 | |
36 namespace client { | |
37 class ScreenPositionClient; | |
38 } | |
39 | 33 |
40 namespace test { | 34 namespace test { |
41 | 35 |
42 typedef base::Callback<void(ui::EventType, const gfx::Vector2dF&)> | 36 typedef base::Callback<void(EventType, const gfx::Vector2dF&)> |
43 ScrollStepCallback; | 37 ScrollStepCallback; |
44 | 38 |
45 // A delegate interface for EventGenerator that provides a way to | 39 // A delegate interface for EventGenerator to abstract platform-specific event |
46 // locate aura root window for given point. | 40 // targeting and coordinate conversion. |
47 class EventGeneratorDelegate { | 41 class EventGeneratorDelegate { |
48 public: | 42 public: |
49 virtual ~EventGeneratorDelegate() {} | 43 virtual ~EventGeneratorDelegate() {} |
50 | 44 |
51 // Returns the host for given point. | 45 // The ui::EventTarget at the given |location|. |
52 virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const = 0; | 46 virtual EventTarget* GetTargetAt(const gfx::Point& location) = 0; |
53 | 47 |
54 // Returns the screen position client that determines the | 48 // The ui::EventSource for the given |target|. |
55 // coordinates used in EventGenerator. EventGenerator uses | 49 virtual EventSource* GetEventSource(EventTarget* target) = 0; |
56 // root Window's coordinate if this returns NULL. | 50 |
57 virtual client::ScreenPositionClient* GetScreenPositionClient( | 51 // Helper functions to determine the center point of |target| or |window|. |
58 const aura::Window* window) const = 0; | 52 virtual gfx::Point CenterOfTarget(const EventTarget* target) const = 0; |
| 53 virtual gfx::Point CenterOfWindow(gfx::NativeWindow window) const = 0; |
| 54 |
| 55 // Convert a point between API's coordinates and |target|'s coordinates. |
| 56 virtual void ConvertPointFromTarget(const EventTarget* target, |
| 57 gfx::Point* point) const = 0; |
| 58 virtual void ConvertPointToTarget(const EventTarget* target, |
| 59 gfx::Point* point) const = 0; |
| 60 |
| 61 // Convert a point from the coordinate system in the host that contains |
| 62 // |hosted_target| into the root window's coordinate system. |
| 63 virtual void ConvertPointFromHost(const EventTarget* hosted_target, |
| 64 gfx::Point* point) const = 0; |
59 }; | 65 }; |
60 | 66 |
61 // EventGenerator is a tool that generates and dispatch events. | 67 // ui::test::EventGenerator is a tool that generates and dispatches events. |
62 // Unlike |ui_controls| package in ui/base/test, this does not generate platform | 68 // Unlike |ui_controls| package in ui/base/test, this does not use platform |
63 // native events. Instead, it sends events to |aura::WindowEventDispatcher| | 69 // native message loops. Instead, it sends events to the event dispatcher |
64 // synchronously. | 70 // synchronously. |
65 // | 71 // |
66 // This class is not suited for the following cases: | 72 // This class is not suited for the following cases: |
67 // | 73 // |
68 // 1) If your test depends on native events (ui::Event::native_event()). | 74 // 1) If your test depends on native events (ui::Event::native_event()). |
69 // This return is empty/NULL event with EventGenerator. | 75 // This return is empty/NULL event with EventGenerator. |
70 // 2) If your test involves nested message loop, such as | 76 // 2) If your test involves nested message loop, such as |
71 // menu or drag & drop. Because this class directly | 77 // menu or drag & drop. Because this class directly |
72 // post an event to WindowEventDispatcher, this event will not be | 78 // post an event to WindowEventDispatcher, this event will not be |
73 // handled in the nested message loop. | 79 // handled in the nested message loop. |
74 // 3) Similarly, |base::MessagePumpObserver| will not be invoked. | 80 // 3) Similarly, |base::MessagePumpObserver| will not be invoked. |
75 // 4) Any other code that requires native events, such as | 81 // 4) Any other code that requires native message loops, such as |
76 // tests for WindowTreeHostWin/WindowTreeHostX11. | 82 // tests for WindowTreeHostWin/WindowTreeHostX11. |
77 // | 83 // |
78 // If one of these applies to your test, please use |ui_controls| | 84 // If one of these applies to your test, please use |ui_controls| |
79 // package instead. | 85 // package instead. |
80 // | 86 // |
81 // Note: The coordinates of the points in API is determined by the | 87 // Note: The coordinates of the points in API is determined by the |
82 // EventGeneratorDelegate. | 88 // EventGeneratorDelegate. |
83 class EventGenerator { | 89 class EventGenerator { |
84 public: | 90 public: |
85 // Creates an EventGenerator with the mouse/touch location (0,0), | 91 // Creates an EventGenerator with the mouse/touch location (0,0), |
86 // which uses the |root_window|'s coordinates. | 92 // which uses the |root_window|'s coordinates and the default delegate for |
87 explicit EventGenerator(Window* root_window); | 93 // this platform. |
| 94 explicit EventGenerator(gfx::NativeWindow root_window); |
88 | 95 |
89 // Create an EventGenerator with EventGeneratorDelegate, | 96 // Create an EventGenerator with EventGeneratorDelegate, |
90 // which uses the coordinates used by |delegate|. | 97 // which uses the coordinates conversions and targeting provided by |
| 98 // |delegate|. |
91 explicit EventGenerator(EventGeneratorDelegate* delegate); | 99 explicit EventGenerator(EventGeneratorDelegate* delegate); |
92 | 100 |
93 // Creates an EventGenerator with the mouse/touch location | 101 // Creates an EventGenerator with the mouse/touch location |
94 // at |initial_location|, which uses the |root_window|'s coordinates. | 102 // at |initial_location|, which uses the |root_window|'s coordinates. |
95 EventGenerator(Window* root_window, const gfx::Point& initial_location); | 103 EventGenerator(gfx::NativeWindow root_window, |
| 104 const gfx::Point& initial_location); |
96 | 105 |
97 // Creates an EventGenerator with the mouse/touch location | 106 // Creates an EventGenerator with the mouse/touch location centered over |
98 // centered over |window|, which uses the |root_window|'s coordinates. | 107 // |window|. This is currently the only constructor that works on Mac, since |
99 EventGenerator(Window* root_window, Window* window); | 108 // a specific window is required (and there is no root window). |
| 109 EventGenerator(gfx::NativeWindow root_window, gfx::NativeWindow window); |
100 | 110 |
101 virtual ~EventGenerator(); | 111 virtual ~EventGenerator(); |
102 | 112 |
103 // Explicitly sets the location used by mouse/touch events. This is set by the | 113 // Explicitly sets the location used by mouse/touch events. This is set by the |
104 // various methods that take a location but can be manipulated directly, | 114 // various methods that take a location but can be manipulated directly, |
105 // typically for touch. | 115 // typically for touch. |
106 void set_current_location(const gfx::Point& location) { | 116 void set_current_location(const gfx::Point& location) { |
107 current_location_ = location; | 117 current_location_ = location; |
108 } | 118 } |
109 const gfx::Point& current_location() const { return current_location_; } | 119 const gfx::Point& current_location() const { return current_location_; } |
110 | 120 |
111 void set_async(bool async) { async_ = async; } | 121 void set_async(bool async) { async_ = async; } |
112 bool async() const { return async_; } | 122 bool async() const { return async_; } |
113 | 123 |
114 // Resets the event flags bitmask. | 124 // Resets the event flags bitmask. |
115 void set_flags(int flags) { flags_ = flags; } | 125 void set_flags(int flags) { flags_ = flags; } |
| 126 int flags() const { return flags_; } |
116 | 127 |
117 // Generates a left button press event. | 128 // Generates a left button press event. |
118 void PressLeftButton(); | 129 void PressLeftButton(); |
119 | 130 |
120 // Generates a left button release event. | 131 // Generates a left button release event. |
121 void ReleaseLeftButton(); | 132 void ReleaseLeftButton(); |
122 | 133 |
123 // Generates events to click (press, release) left button. | 134 // Generates events to click (press, release) left button. |
124 void ClickLeftButton(); | 135 void ClickLeftButton(); |
125 | 136 |
(...skipping 24 matching lines...) Expand all Loading... |
150 void MoveMouseTo(const gfx::Point& point_in_screen, int count); | 161 void MoveMouseTo(const gfx::Point& point_in_screen, int count); |
151 void MoveMouseTo(const gfx::Point& point_in_screen) { | 162 void MoveMouseTo(const gfx::Point& point_in_screen) { |
152 MoveMouseTo(point_in_screen, 1); | 163 MoveMouseTo(point_in_screen, 1); |
153 } | 164 } |
154 void MoveMouseTo(int x, int y) { | 165 void MoveMouseTo(int x, int y) { |
155 MoveMouseTo(gfx::Point(x, y)); | 166 MoveMouseTo(gfx::Point(x, y)); |
156 } | 167 } |
157 | 168 |
158 // Generates events to move mouse to be the given |point| in |window|'s | 169 // Generates events to move mouse to be the given |point| in |window|'s |
159 // coordinates. | 170 // coordinates. |
160 void MoveMouseRelativeTo(const Window* window, const gfx::Point& point); | 171 void MoveMouseRelativeTo(const EventTarget* window, const gfx::Point& point); |
161 void MoveMouseRelativeTo(const Window* window, int x, int y) { | 172 void MoveMouseRelativeTo(const EventTarget* window, int x, int y) { |
162 MoveMouseRelativeTo(window, gfx::Point(x, y)); | 173 MoveMouseRelativeTo(window, gfx::Point(x, y)); |
163 } | 174 } |
164 | 175 |
165 void MoveMouseBy(int x, int y) { | 176 void MoveMouseBy(int x, int y) { |
166 MoveMouseTo(current_location_ + gfx::Vector2d(x, y)); | 177 MoveMouseTo(current_location_ + gfx::Vector2d(x, y)); |
167 } | 178 } |
168 | 179 |
169 // Generates events to drag mouse to given |point|. | 180 // Generates events to drag mouse to given |point|. |
170 void DragMouseTo(const gfx::Point& point); | 181 void DragMouseTo(const gfx::Point& point); |
171 | 182 |
172 void DragMouseTo(int x, int y) { | 183 void DragMouseTo(int x, int y) { |
173 DragMouseTo(gfx::Point(x, y)); | 184 DragMouseTo(gfx::Point(x, y)); |
174 } | 185 } |
175 | 186 |
176 void DragMouseBy(int dx, int dy) { | 187 void DragMouseBy(int dx, int dy) { |
177 DragMouseTo(current_location_ + gfx::Vector2d(dx, dy)); | 188 DragMouseTo(current_location_ + gfx::Vector2d(dx, dy)); |
178 } | 189 } |
179 | 190 |
180 // Generates events to move the mouse to the center of the window. | 191 // Generates events to move the mouse to the center of the window. |
181 void MoveMouseToCenterOf(Window* window); | 192 void MoveMouseToCenterOf(EventTarget* window); |
182 | 193 |
183 // Generates a touch press event. | 194 // Generates a touch press event. |
184 void PressTouch(); | 195 void PressTouch(); |
185 | 196 |
186 // Generates a touch press event with |touch_id|. | 197 // Generates a touch press event with |touch_id|. |
187 void PressTouchId(int touch_id); | 198 void PressTouchId(int touch_id); |
188 | 199 |
189 // Generates a ET_TOUCH_MOVED event to |point|. | 200 // Generates a ET_TOUCH_MOVED event to |point|. |
190 void MoveTouch(const gfx::Point& point); | 201 void MoveTouch(const gfx::Point& point); |
191 | 202 |
(...skipping 13 matching lines...) Expand all Loading... |
205 void PressMoveAndReleaseTouchTo(int x, int y) { | 216 void PressMoveAndReleaseTouchTo(int x, int y) { |
206 PressMoveAndReleaseTouchTo(gfx::Point(x, y)); | 217 PressMoveAndReleaseTouchTo(gfx::Point(x, y)); |
207 } | 218 } |
208 | 219 |
209 void PressMoveAndReleaseTouchBy(int x, int y) { | 220 void PressMoveAndReleaseTouchBy(int x, int y) { |
210 PressMoveAndReleaseTouchTo(current_location_ + gfx::Vector2d(x, y)); | 221 PressMoveAndReleaseTouchTo(current_location_ + gfx::Vector2d(x, y)); |
211 } | 222 } |
212 | 223 |
213 // Generates press, move and release events to move touch | 224 // Generates press, move and release events to move touch |
214 // to the center of the window. | 225 // to the center of the window. |
215 void PressMoveAndReleaseTouchToCenterOf(Window* window); | 226 void PressMoveAndReleaseTouchToCenterOf(EventTarget* window); |
216 | 227 |
217 // Generates and dispatches a Win8 edge-swipe event (swipe up from bottom or | 228 // Generates and dispatches a Win8 edge-swipe event (swipe up from bottom or |
218 // swipe down from top). Note that it is not possible to distinguish between | 229 // swipe down from top). Note that it is not possible to distinguish between |
219 // the two edges with this event. | 230 // the two edges with this event. |
220 void GestureEdgeSwipe(); | 231 void GestureEdgeSwipe(); |
221 | 232 |
222 // Generates and dispatches touch-events required to generate a TAP gesture. | 233 // Generates and dispatches touch-events required to generate a TAP gesture. |
223 // Note that this can generate a number of other gesture events at the same | 234 // Note that this can generate a number of other gesture events at the same |
224 // time (e.g. GESTURE_BEGIN, TAP_DOWN, END). | 235 // time (e.g. GESTURE_BEGIN, TAP_DOWN, END). |
225 void GestureTapAt(const gfx::Point& point); | 236 void GestureTapAt(const gfx::Point& point); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 // scrolls of each of the values in |offsets|. | 307 // scrolls of each of the values in |offsets|. |
297 void ScrollSequence(const gfx::Point& start, | 308 void ScrollSequence(const gfx::Point& start, |
298 const base::TimeDelta& step_delay, | 309 const base::TimeDelta& step_delay, |
299 const std::vector<gfx::Point>& offsets, | 310 const std::vector<gfx::Point>& offsets, |
300 int num_fingers); | 311 int num_fingers); |
301 | 312 |
302 // Generates a key press event. On platforms except Windows and X11, a key | 313 // Generates a key press event. On platforms except Windows and X11, a key |
303 // event without native_event() is generated. Note that ui::EF_ flags should | 314 // event without native_event() is generated. Note that ui::EF_ flags should |
304 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. | 315 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. |
305 // TODO(yusukes): Support native_event() on all platforms. | 316 // TODO(yusukes): Support native_event() on all platforms. |
306 void PressKey(ui::KeyboardCode key_code, int flags); | 317 void PressKey(KeyboardCode key_code, int flags); |
307 | 318 |
308 // Generates a key release event. On platforms except Windows and X11, a key | 319 // Generates a key release event. On platforms except Windows and X11, a key |
309 // event without native_event() is generated. Note that ui::EF_ flags should | 320 // event without native_event() is generated. Note that ui::EF_ flags should |
310 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. | 321 // be passed as |flags|, not the native ones like 'ShiftMask' in <X11/X.h>. |
311 // TODO(yusukes): Support native_event() on all platforms. | 322 // TODO(yusukes): Support native_event() on all platforms. |
312 void ReleaseKey(ui::KeyboardCode key_code, int flags); | 323 void ReleaseKey(KeyboardCode key_code, int flags); |
313 | 324 |
314 // Dispatch the event to the WindowEventDispatcher. | 325 // Dispatch the event to the WindowEventDispatcher. |
315 void Dispatch(ui::Event* event); | 326 void Dispatch(Event* event); |
316 | 327 |
317 void set_current_host(WindowTreeHost* host) { | 328 void set_current_target(EventTarget* target) { |
318 current_host_ = host; | 329 current_target_ = target; |
319 } | 330 } |
320 | 331 |
321 // Specify an alternative tick clock to be used for simulating time in tests. | 332 // Specify an alternative tick clock to be used for simulating time in tests. |
322 void SetTickClock(scoped_ptr<base::TickClock> tick_clock); | 333 void SetTickClock(scoped_ptr<base::TickClock> tick_clock); |
323 | 334 |
324 // Get the current time from the tick clock. | 335 // Get the current time from the tick clock. |
325 base::TimeDelta Now(); | 336 base::TimeDelta Now(); |
326 | 337 |
327 private: | 338 private: |
| 339 // Implemented per-platform to create the default EventGeneratorDelegate. |
| 340 static EventGeneratorDelegate* CreateDefaultPlatformDelegate( |
| 341 EventGenerator* owner, |
| 342 gfx::NativeWindow root_window, |
| 343 gfx::NativeWindow window); |
| 344 |
328 // Dispatch a key event to the WindowEventDispatcher. | 345 // Dispatch a key event to the WindowEventDispatcher. |
329 void DispatchKeyEvent(bool is_press, ui::KeyboardCode key_code, int flags); | 346 void DispatchKeyEvent(bool is_press, KeyboardCode key_code, int flags); |
330 | 347 |
331 void UpdateCurrentDispatcher(const gfx::Point& point); | 348 void UpdateCurrentDispatcher(const gfx::Point& point); |
332 void PressButton(int flag); | 349 void PressButton(int flag); |
333 void ReleaseButton(int flag); | 350 void ReleaseButton(int flag); |
334 | 351 |
335 // Convert a point between API's coordinates and | |
336 // |target|'s coordinates. | |
337 void ConvertPointFromTarget(const aura::Window* target, | |
338 gfx::Point* point) const; | |
339 void ConvertPointToTarget(const aura::Window* target, | |
340 gfx::Point* point) const; | |
341 | |
342 gfx::Point GetLocationInCurrentRoot() const; | 352 gfx::Point GetLocationInCurrentRoot() const; |
343 gfx::Point CenterOfWindow(const Window* window) const; | 353 gfx::Point CenterOfWindow(const EventTarget* window) const; |
344 | 354 |
345 void DispatchNextPendingEvent(); | 355 void DispatchNextPendingEvent(); |
346 void DoDispatchEvent(ui::Event* event, bool async); | 356 void DoDispatchEvent(Event* event, bool async); |
347 | 357 |
348 scoped_ptr<EventGeneratorDelegate> delegate_; | 358 scoped_ptr<EventGeneratorDelegate> delegate_; |
349 gfx::Point current_location_; | 359 gfx::Point current_location_; |
350 WindowTreeHost* current_host_; | 360 EventTarget* current_target_; |
351 int flags_; | 361 int flags_; |
352 bool grab_; | 362 bool grab_; |
353 std::list<ui::Event*> pending_events_; | 363 std::list<Event*> pending_events_; |
354 // Set to true to cause events to be posted asynchronously. | 364 // Set to true to cause events to be posted asynchronously. |
355 bool async_; | 365 bool async_; |
356 scoped_ptr<base::TickClock> tick_clock_; | 366 scoped_ptr<base::TickClock> tick_clock_; |
357 | 367 |
358 DISALLOW_COPY_AND_ASSIGN(EventGenerator); | 368 DISALLOW_COPY_AND_ASSIGN(EventGenerator); |
359 }; | 369 }; |
360 | 370 |
361 } // namespace test | 371 } // namespace test |
362 } // namespace aura | 372 } // namespace ui |
363 | 373 |
364 #endif // UI_AURA_TEST_EVENT_GENERATOR_H_ | 374 #endif // UI_EVENTS_TEST_EVENT_GENERATOR_H_ |
OLD | NEW |