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