OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "ui/aura/test/event_generator.h" | 5 #include "ui/aura/test/event_generator.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop_proxy.h" | |
10 #include "base/time/default_tick_clock.h" | |
11 #include "ui/aura/client/screen_position_client.h" | 7 #include "ui/aura/client/screen_position_client.h" |
12 #include "ui/aura/window_event_dispatcher.h" | 8 #include "ui/aura/window_event_dispatcher.h" |
13 #include "ui/aura/window_tree_host.h" | 9 #include "ui/aura/window_tree_host.h" |
14 #include "ui/events/event.h" | |
15 #include "ui/events/event_source.h" | |
16 #include "ui/events/event_utils.h" | |
17 #include "ui/events/test/events_test_utils.h" | |
18 #include "ui/gfx/vector2d_conversions.h" | |
19 | |
20 #if defined(USE_X11) | |
21 #include <X11/Xlib.h> | |
22 #include "ui/base/x/x11_util.h" | |
23 #include "ui/events/event_utils.h" | |
24 #include "ui/events/test/events_test_utils_x11.h" | |
25 #endif | |
26 | |
27 #if defined(OS_WIN) | |
28 #include "ui/events/keycodes/keyboard_code_conversion.h" | |
29 #endif | |
30 | 10 |
31 namespace aura { | 11 namespace aura { |
32 namespace test { | 12 namespace test { |
33 namespace { | 13 namespace { |
34 | 14 |
35 void DummyCallback(ui::EventType, const gfx::Vector2dF&) { | 15 class DefaultEventGeneratorDelegate : public EventGeneratorDelegateAura { |
36 } | |
37 | |
38 class DefaultEventGeneratorDelegate : public EventGeneratorDelegate { | |
39 public: | 16 public: |
40 explicit DefaultEventGeneratorDelegate(Window* root_window) | 17 explicit DefaultEventGeneratorDelegate(Window* root_window) |
41 : root_window_(root_window) {} | 18 : root_window_(root_window) {} |
42 virtual ~DefaultEventGeneratorDelegate() {} | 19 virtual ~DefaultEventGeneratorDelegate() {} |
43 | 20 |
44 // EventGeneratorDelegate overrides: | 21 // EventGeneratorDelegateAura overrides: |
45 virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const OVERRIDE { | 22 virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const OVERRIDE { |
46 return root_window_->GetHost(); | 23 return root_window_->GetHost(); |
47 } | 24 } |
48 | 25 |
49 virtual client::ScreenPositionClient* GetScreenPositionClient( | 26 virtual client::ScreenPositionClient* GetScreenPositionClient( |
50 const aura::Window* window) const OVERRIDE { | 27 const aura::Window* window) const OVERRIDE { |
51 return NULL; | 28 return NULL; |
52 } | 29 } |
53 | 30 |
54 private: | 31 private: |
55 Window* root_window_; | 32 Window* root_window_; |
56 | 33 |
57 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate); | 34 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate); |
58 }; | 35 }; |
59 | 36 |
60 class TestKeyEvent : public ui::KeyEvent { | 37 const Window* WindowFromTarget(const ui::EventTarget* event_target) { |
61 public: | 38 return static_cast<const Window*>(event_target); |
62 TestKeyEvent(const base::NativeEvent& native_event, int flags, bool is_char) | 39 } |
63 : KeyEvent(native_event, is_char) { | |
64 set_flags(flags); | |
65 } | |
66 }; | |
67 | |
68 class TestTouchEvent : public ui::TouchEvent { | |
69 public: | |
70 TestTouchEvent(ui::EventType type, | |
71 const gfx::Point& root_location, | |
72 int touch_id, | |
73 int flags, | |
74 base::TimeDelta timestamp) | |
75 : TouchEvent(type, root_location, flags, touch_id, timestamp, | |
76 1.0f, 1.0f, 1.0f, 1.0f) { | |
77 } | |
78 | |
79 private: | |
80 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); | |
81 }; | |
82 | |
83 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON; | |
84 | 40 |
85 } // namespace | 41 } // namespace |
86 | 42 |
87 EventGenerator::EventGenerator(Window* root_window) | 43 EventGeneratorDelegateAura::EventGeneratorDelegateAura() { |
88 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | |
89 current_host_(delegate_->GetHostAt(current_location_)), | |
90 flags_(0), | |
91 grab_(false), | |
92 async_(false), | |
93 tick_clock_(new base::DefaultTickClock()) { | |
94 } | 44 } |
95 | 45 |
96 EventGenerator::EventGenerator(Window* root_window, const gfx::Point& point) | 46 EventGeneratorDelegateAura::~EventGeneratorDelegateAura() { |
97 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | |
98 current_location_(point), | |
99 current_host_(delegate_->GetHostAt(current_location_)), | |
100 flags_(0), | |
101 grab_(false), | |
102 async_(false), | |
103 tick_clock_(new base::DefaultTickClock()) { | |
104 } | 47 } |
105 | 48 |
106 EventGenerator::EventGenerator(Window* root_window, Window* window) | 49 ui::EventTarget* EventGeneratorDelegateAura::GetTargetAt( |
107 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | 50 const gfx::Point& location) { |
108 current_location_(CenterOfWindow(window)), | 51 return GetHostAt(location)->window(); |
109 current_host_(delegate_->GetHostAt(current_location_)), | |
110 flags_(0), | |
111 grab_(false), | |
112 async_(false), | |
113 tick_clock_(new base::DefaultTickClock()) { | |
114 } | 52 } |
115 | 53 |
116 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate) | 54 ui::EventSource* EventGeneratorDelegateAura::GetEventSource( |
117 : delegate_(delegate), | 55 ui::EventTarget* target) { |
118 current_host_(delegate_->GetHostAt(current_location_)), | 56 return static_cast<Window*>(target)->GetHost()->GetEventSource(); |
119 flags_(0), | |
120 grab_(false), | |
121 async_(false), | |
122 tick_clock_(new base::DefaultTickClock()) { | |
123 } | 57 } |
124 | 58 |
125 EventGenerator::~EventGenerator() { | 59 gfx::Point EventGeneratorDelegateAura::CenterOfTarget( |
126 for (std::list<ui::Event*>::iterator i = pending_events_.begin(); | 60 const ui::EventTarget* target) const { |
127 i != pending_events_.end(); ++i) | 61 gfx::Point center = |
128 delete *i; | 62 gfx::Rect(WindowFromTarget(target)->bounds().size()).CenterPoint(); |
129 pending_events_.clear(); | 63 ConvertPointFromTarget(target, ¢er); |
| 64 return center; |
130 } | 65 } |
131 | 66 |
132 void EventGenerator::PressLeftButton() { | 67 gfx::Point EventGeneratorDelegateAura::CenterOfWindow( |
133 PressButton(ui::EF_LEFT_MOUSE_BUTTON); | 68 gfx::NativeWindow window) const { |
| 69 return CenterOfTarget(window); |
134 } | 70 } |
135 | 71 |
136 void EventGenerator::ReleaseLeftButton() { | 72 void EventGeneratorDelegateAura::ConvertPointFromTarget( |
137 ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON); | 73 const ui::EventTarget* event_target, |
138 } | 74 gfx::Point* point) const { |
139 | |
140 void EventGenerator::ClickLeftButton() { | |
141 PressLeftButton(); | |
142 ReleaseLeftButton(); | |
143 } | |
144 | |
145 void EventGenerator::DoubleClickLeftButton() { | |
146 flags_ |= ui::EF_IS_DOUBLE_CLICK; | |
147 PressLeftButton(); | |
148 flags_ ^= ui::EF_IS_DOUBLE_CLICK; | |
149 ReleaseLeftButton(); | |
150 } | |
151 | |
152 void EventGenerator::PressRightButton() { | |
153 PressButton(ui::EF_RIGHT_MOUSE_BUTTON); | |
154 } | |
155 | |
156 void EventGenerator::ReleaseRightButton() { | |
157 ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON); | |
158 } | |
159 | |
160 void EventGenerator::MoveMouseWheel(int delta_x, int delta_y) { | |
161 gfx::Point location = GetLocationInCurrentRoot(); | |
162 ui::MouseEvent mouseev(ui::ET_MOUSEWHEEL, location, location, flags_, 0); | |
163 ui::MouseWheelEvent wheelev(mouseev, delta_x, delta_y); | |
164 Dispatch(&wheelev); | |
165 } | |
166 | |
167 void EventGenerator::SendMouseExit() { | |
168 gfx::Point exit_location(current_location_); | |
169 ConvertPointToTarget(current_host_->window(), &exit_location); | |
170 ui::MouseEvent mouseev(ui::ET_MOUSE_EXITED, exit_location, exit_location, | |
171 flags_, 0); | |
172 Dispatch(&mouseev); | |
173 } | |
174 | |
175 void EventGenerator::MoveMouseToInHost(const gfx::Point& point_in_host) { | |
176 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ? | |
177 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; | |
178 ui::MouseEvent mouseev(event_type, point_in_host, point_in_host, flags_, 0); | |
179 Dispatch(&mouseev); | |
180 | |
181 current_location_ = point_in_host; | |
182 current_host_->ConvertPointFromHost(¤t_location_); | |
183 } | |
184 | |
185 void EventGenerator::MoveMouseTo(const gfx::Point& point_in_screen, | |
186 int count) { | |
187 DCHECK_GT(count, 0); | |
188 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ? | |
189 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; | |
190 | |
191 gfx::Vector2dF diff(point_in_screen - current_location_); | |
192 for (float i = 1; i <= count; i++) { | |
193 gfx::Vector2dF step(diff); | |
194 step.Scale(i / count); | |
195 gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step); | |
196 if (!grab_) | |
197 UpdateCurrentDispatcher(move_point); | |
198 ConvertPointToTarget(current_host_->window(), &move_point); | |
199 ui::MouseEvent mouseev(event_type, move_point, move_point, flags_, 0); | |
200 Dispatch(&mouseev); | |
201 } | |
202 current_location_ = point_in_screen; | |
203 } | |
204 | |
205 void EventGenerator::MoveMouseRelativeTo(const Window* window, | |
206 const gfx::Point& point_in_parent) { | |
207 gfx::Point point(point_in_parent); | |
208 ConvertPointFromTarget(window, &point); | |
209 MoveMouseTo(point); | |
210 } | |
211 | |
212 void EventGenerator::DragMouseTo(const gfx::Point& point) { | |
213 PressLeftButton(); | |
214 MoveMouseTo(point); | |
215 ReleaseLeftButton(); | |
216 } | |
217 | |
218 void EventGenerator::MoveMouseToCenterOf(Window* window) { | |
219 MoveMouseTo(CenterOfWindow(window)); | |
220 } | |
221 | |
222 void EventGenerator::PressTouch() { | |
223 PressTouchId(0); | |
224 } | |
225 | |
226 void EventGenerator::PressTouchId(int touch_id) { | |
227 TestTouchEvent touchev( | |
228 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags_, | |
229 Now()); | |
230 Dispatch(&touchev); | |
231 } | |
232 | |
233 void EventGenerator::MoveTouch(const gfx::Point& point) { | |
234 MoveTouchId(point, 0); | |
235 } | |
236 | |
237 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) { | |
238 current_location_ = point; | |
239 TestTouchEvent touchev( | |
240 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags_, | |
241 Now()); | |
242 Dispatch(&touchev); | |
243 | |
244 if (!grab_) | |
245 UpdateCurrentDispatcher(point); | |
246 } | |
247 | |
248 void EventGenerator::ReleaseTouch() { | |
249 ReleaseTouchId(0); | |
250 } | |
251 | |
252 void EventGenerator::ReleaseTouchId(int touch_id) { | |
253 TestTouchEvent touchev( | |
254 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags_, | |
255 Now()); | |
256 Dispatch(&touchev); | |
257 } | |
258 | |
259 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { | |
260 PressTouch(); | |
261 MoveTouch(point); | |
262 ReleaseTouch(); | |
263 } | |
264 | |
265 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { | |
266 PressMoveAndReleaseTouchTo(CenterOfWindow(window)); | |
267 } | |
268 | |
269 void EventGenerator::GestureEdgeSwipe() { | |
270 ui::GestureEvent gesture( | |
271 0, | |
272 0, | |
273 0, | |
274 Now(), | |
275 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE, 0, 0)); | |
276 Dispatch(&gesture); | |
277 } | |
278 | |
279 void EventGenerator::GestureTapAt(const gfx::Point& location) { | |
280 const int kTouchId = 2; | |
281 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | |
282 location, | |
283 kTouchId, | |
284 Now()); | |
285 Dispatch(&press); | |
286 | |
287 ui::TouchEvent release( | |
288 ui::ET_TOUCH_RELEASED, location, kTouchId, | |
289 press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); | |
290 Dispatch(&release); | |
291 } | |
292 | |
293 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { | |
294 const int kTouchId = 3; | |
295 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | |
296 location, | |
297 kTouchId, | |
298 Now()); | |
299 Dispatch(&press); | |
300 | |
301 ui::TouchEvent release( | |
302 ui::ET_TOUCH_RELEASED, location, kTouchId, | |
303 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); | |
304 Dispatch(&release); | |
305 } | |
306 | |
307 void EventGenerator::GestureScrollSequence(const gfx::Point& start, | |
308 const gfx::Point& end, | |
309 const base::TimeDelta& step_delay, | |
310 int steps) { | |
311 GestureScrollSequenceWithCallback(start, end, step_delay, steps, | |
312 base::Bind(&DummyCallback)); | |
313 } | |
314 | |
315 void EventGenerator::GestureScrollSequenceWithCallback( | |
316 const gfx::Point& start, | |
317 const gfx::Point& end, | |
318 const base::TimeDelta& step_delay, | |
319 int steps, | |
320 const ScrollStepCallback& callback) { | |
321 const int kTouchId = 5; | |
322 base::TimeDelta timestamp = Now(); | |
323 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); | |
324 Dispatch(&press); | |
325 | |
326 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF()); | |
327 | |
328 int dx = (end.x() - start.x()) / steps; | |
329 int dy = (end.y() - start.y()) / steps; | |
330 gfx::Point location = start; | |
331 for (int i = 0; i < steps; ++i) { | |
332 location.Offset(dx, dy); | |
333 timestamp += step_delay; | |
334 ui::TouchEvent move(ui::ET_TOUCH_MOVED, location, kTouchId, timestamp); | |
335 Dispatch(&move); | |
336 callback.Run(ui::ET_GESTURE_SCROLL_UPDATE, gfx::Vector2dF(dx, dy)); | |
337 } | |
338 | |
339 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, end, kTouchId, timestamp); | |
340 Dispatch(&release); | |
341 | |
342 callback.Run(ui::ET_GESTURE_SCROLL_END, gfx::Vector2dF()); | |
343 } | |
344 | |
345 void EventGenerator::GestureMultiFingerScroll(int count, | |
346 const gfx::Point start[], | |
347 int event_separation_time_ms, | |
348 int steps, | |
349 int move_x, | |
350 int move_y) { | |
351 const int kMaxTouchPoints = 10; | |
352 int delays[kMaxTouchPoints] = { 0 }; | |
353 GestureMultiFingerScrollWithDelays( | |
354 count, start, delays, event_separation_time_ms, steps, move_x, move_y); | |
355 } | |
356 | |
357 void EventGenerator::GestureMultiFingerScrollWithDelays( | |
358 int count, | |
359 const gfx::Point start[], | |
360 const int delay_adding_finger_ms[], | |
361 int event_separation_time_ms, | |
362 int steps, | |
363 int move_x, | |
364 int move_y) { | |
365 const int kMaxTouchPoints = 10; | |
366 gfx::Point points[kMaxTouchPoints]; | |
367 CHECK_LE(count, kMaxTouchPoints); | |
368 CHECK_GT(steps, 0); | |
369 | |
370 int delta_x = move_x / steps; | |
371 int delta_y = move_y / steps; | |
372 | |
373 for (int i = 0; i < count; ++i) { | |
374 points[i] = start[i]; | |
375 } | |
376 | |
377 base::TimeDelta press_time_first = Now(); | |
378 base::TimeDelta press_time[kMaxTouchPoints]; | |
379 bool pressed[kMaxTouchPoints]; | |
380 for (int i = 0; i < count; ++i) { | |
381 pressed[i] = false; | |
382 press_time[i] = press_time_first + | |
383 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]); | |
384 } | |
385 | |
386 int last_id = 0; | |
387 for (int step = 0; step < steps; ++step) { | |
388 base::TimeDelta move_time = press_time_first + | |
389 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step); | |
390 | |
391 while (last_id < count && | |
392 !pressed[last_id] && | |
393 move_time >= press_time[last_id]) { | |
394 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | |
395 points[last_id], | |
396 last_id, | |
397 press_time[last_id]); | |
398 Dispatch(&press); | |
399 pressed[last_id] = true; | |
400 last_id++; | |
401 } | |
402 | |
403 for (int i = 0; i < count; ++i) { | |
404 points[i].Offset(delta_x, delta_y); | |
405 if (i >= last_id) | |
406 continue; | |
407 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time); | |
408 Dispatch(&move); | |
409 } | |
410 } | |
411 | |
412 base::TimeDelta release_time = press_time_first + | |
413 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps); | |
414 for (int i = 0; i < last_id; ++i) { | |
415 ui::TouchEvent release( | |
416 ui::ET_TOUCH_RELEASED, points[i], i, release_time); | |
417 Dispatch(&release); | |
418 } | |
419 } | |
420 | |
421 void EventGenerator::ScrollSequence(const gfx::Point& start, | |
422 const base::TimeDelta& step_delay, | |
423 float x_offset, | |
424 float y_offset, | |
425 int steps, | |
426 int num_fingers) { | |
427 base::TimeDelta timestamp = Now(); | |
428 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, | |
429 start, | |
430 timestamp, | |
431 0, | |
432 0, 0, | |
433 0, 0, | |
434 num_fingers); | |
435 Dispatch(&fling_cancel); | |
436 | |
437 float dx = x_offset / steps; | |
438 float dy = y_offset / steps; | |
439 for (int i = 0; i < steps; ++i) { | |
440 timestamp += step_delay; | |
441 ui::ScrollEvent move(ui::ET_SCROLL, | |
442 start, | |
443 timestamp, | |
444 0, | |
445 dx, dy, | |
446 dx, dy, | |
447 num_fingers); | |
448 Dispatch(&move); | |
449 } | |
450 | |
451 ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START, | |
452 start, | |
453 timestamp, | |
454 0, | |
455 x_offset, y_offset, | |
456 x_offset, y_offset, | |
457 num_fingers); | |
458 Dispatch(&fling_start); | |
459 } | |
460 | |
461 void EventGenerator::ScrollSequence(const gfx::Point& start, | |
462 const base::TimeDelta& step_delay, | |
463 const std::vector<gfx::Point>& offsets, | |
464 int num_fingers) { | |
465 int steps = offsets.size(); | |
466 base::TimeDelta timestamp = Now(); | |
467 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, | |
468 start, | |
469 timestamp, | |
470 0, | |
471 0, 0, | |
472 0, 0, | |
473 num_fingers); | |
474 Dispatch(&fling_cancel); | |
475 | |
476 for (int i = 0; i < steps; ++i) { | |
477 timestamp += step_delay; | |
478 ui::ScrollEvent scroll(ui::ET_SCROLL, | |
479 start, | |
480 timestamp, | |
481 0, | |
482 offsets[i].x(), offsets[i].y(), | |
483 offsets[i].x(), offsets[i].y(), | |
484 num_fingers); | |
485 Dispatch(&scroll); | |
486 } | |
487 | |
488 ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START, | |
489 start, | |
490 timestamp, | |
491 0, | |
492 offsets[steps - 1].x(), offsets[steps - 1].y(), | |
493 offsets[steps - 1].x(), offsets[steps - 1].y(), | |
494 num_fingers); | |
495 Dispatch(&fling_start); | |
496 } | |
497 | |
498 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { | |
499 DispatchKeyEvent(true, key_code, flags); | |
500 } | |
501 | |
502 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { | |
503 DispatchKeyEvent(false, key_code, flags); | |
504 } | |
505 | |
506 void EventGenerator::Dispatch(ui::Event* event) { | |
507 DoDispatchEvent(event, async_); | |
508 } | |
509 | |
510 void EventGenerator::SetTickClock(scoped_ptr<base::TickClock> tick_clock) { | |
511 tick_clock_ = tick_clock.Pass(); | |
512 } | |
513 | |
514 base::TimeDelta EventGenerator::Now() { | |
515 // This is the same as what EventTimeForNow() does, but here we do it | |
516 // with a tick clock that can be replaced with a simulated clock for tests. | |
517 return base::TimeDelta::FromInternalValue( | |
518 tick_clock_->NowTicks().ToInternalValue()); | |
519 } | |
520 | |
521 void EventGenerator::DispatchKeyEvent(bool is_press, | |
522 ui::KeyboardCode key_code, | |
523 int flags) { | |
524 #if defined(OS_WIN) | |
525 UINT key_press = WM_KEYDOWN; | |
526 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); | |
527 if (is_press && character) { | |
528 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; | |
529 TestKeyEvent keyev(native_event, flags, false); | |
530 Dispatch(&keyev); | |
531 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character | |
532 // if the key event cooresponds to a real character. | |
533 key_press = WM_CHAR; | |
534 key_code = static_cast<ui::KeyboardCode>(character); | |
535 } | |
536 MSG native_event = | |
537 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 }; | |
538 TestKeyEvent keyev(native_event, flags, key_press == WM_CHAR); | |
539 #elif defined(USE_X11) | |
540 ui::ScopedXI2Event xevent; | |
541 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, | |
542 key_code, | |
543 flags); | |
544 ui::KeyEvent keyev(xevent, false); | |
545 #else | |
546 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; | |
547 ui::KeyEvent keyev(type, key_code, flags, false); | |
548 #endif // OS_WIN | |
549 Dispatch(&keyev); | |
550 } | |
551 | |
552 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { | |
553 current_host_ = delegate_->GetHostAt(point); | |
554 } | |
555 | |
556 void EventGenerator::PressButton(int flag) { | |
557 if (!(flags_ & flag)) { | |
558 flags_ |= flag; | |
559 grab_ = flags_ & kAllButtonMask; | |
560 gfx::Point location = GetLocationInCurrentRoot(); | |
561 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_, | |
562 flag); | |
563 Dispatch(&mouseev); | |
564 } | |
565 } | |
566 | |
567 void EventGenerator::ReleaseButton(int flag) { | |
568 if (flags_ & flag) { | |
569 gfx::Point location = GetLocationInCurrentRoot(); | |
570 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location, | |
571 location, flags_, flag); | |
572 Dispatch(&mouseev); | |
573 flags_ ^= flag; | |
574 } | |
575 grab_ = flags_ & kAllButtonMask; | |
576 } | |
577 | |
578 void EventGenerator::ConvertPointFromTarget(const aura::Window* target, | |
579 gfx::Point* point) const { | |
580 DCHECK(point); | 75 DCHECK(point); |
581 aura::client::ScreenPositionClient* client = | 76 const Window* target = WindowFromTarget(event_target); |
582 delegate_->GetScreenPositionClient(target); | 77 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target); |
583 if (client) | 78 if (client) |
584 client->ConvertPointToScreen(target, point); | 79 client->ConvertPointToScreen(target, point); |
585 else | 80 else |
586 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point); | 81 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point); |
587 } | 82 } |
588 | 83 |
589 void EventGenerator::ConvertPointToTarget(const aura::Window* target, | 84 void EventGeneratorDelegateAura::ConvertPointToTarget( |
590 gfx::Point* point) const { | 85 const ui::EventTarget* event_target, |
| 86 gfx::Point* point) const { |
591 DCHECK(point); | 87 DCHECK(point); |
592 aura::client::ScreenPositionClient* client = | 88 const Window* target = WindowFromTarget(event_target); |
593 delegate_->GetScreenPositionClient(target); | 89 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target); |
594 if (client) | 90 if (client) |
595 client->ConvertPointFromScreen(target, point); | 91 client->ConvertPointFromScreen(target, point); |
596 else | 92 else |
597 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point); | 93 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point); |
598 } | 94 } |
599 | 95 |
600 gfx::Point EventGenerator::GetLocationInCurrentRoot() const { | 96 void EventGeneratorDelegateAura::ConvertPointFromHost( |
601 gfx::Point p(current_location_); | 97 const ui::EventTarget* hosted_target, |
602 ConvertPointToTarget(current_host_->window(), &p); | 98 gfx::Point* point) const { |
603 return p; | 99 const Window* window = WindowFromTarget(hosted_target); |
604 } | 100 window->GetHost()->ConvertPointFromHost(point); |
605 | |
606 gfx::Point EventGenerator::CenterOfWindow(const Window* window) const { | |
607 gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint(); | |
608 ConvertPointFromTarget(window, ¢er); | |
609 return center; | |
610 } | |
611 | |
612 void EventGenerator::DoDispatchEvent(ui::Event* event, bool async) { | |
613 if (async) { | |
614 ui::Event* pending_event; | |
615 if (event->IsKeyEvent()) { | |
616 pending_event = new ui::KeyEvent(*static_cast<ui::KeyEvent*>(event)); | |
617 } else if (event->IsMouseEvent()) { | |
618 pending_event = new ui::MouseEvent(*static_cast<ui::MouseEvent*>(event)); | |
619 } else if (event->IsTouchEvent()) { | |
620 pending_event = new ui::TouchEvent(*static_cast<ui::TouchEvent*>(event)); | |
621 } else if (event->IsScrollEvent()) { | |
622 pending_event = | |
623 new ui::ScrollEvent(*static_cast<ui::ScrollEvent*>(event)); | |
624 } else { | |
625 NOTREACHED() << "Invalid event type"; | |
626 return; | |
627 } | |
628 if (pending_events_.empty()) { | |
629 base::MessageLoopProxy::current()->PostTask( | |
630 FROM_HERE, | |
631 base::Bind(&EventGenerator::DispatchNextPendingEvent, | |
632 base::Unretained(this))); | |
633 } | |
634 pending_events_.push_back(pending_event); | |
635 } else { | |
636 ui::EventSource* event_source = current_host_->GetEventSource(); | |
637 ui::EventSourceTestApi event_source_test(event_source); | |
638 ui::EventDispatchDetails details = | |
639 event_source_test.SendEventToProcessor(event); | |
640 CHECK(!details.dispatcher_destroyed); | |
641 } | |
642 } | |
643 | |
644 void EventGenerator::DispatchNextPendingEvent() { | |
645 DCHECK(!pending_events_.empty()); | |
646 ui::Event* event = pending_events_.front(); | |
647 DoDispatchEvent(event, false); | |
648 pending_events_.pop_front(); | |
649 delete event; | |
650 if (!pending_events_.empty()) { | |
651 base::MessageLoopProxy::current()->PostTask( | |
652 FROM_HERE, | |
653 base::Bind(&EventGenerator::DispatchNextPendingEvent, | |
654 base::Unretained(this))); | |
655 } | |
656 } | 101 } |
657 | 102 |
658 } // namespace test | 103 } // namespace test |
659 } // namespace aura | 104 } // namespace aura |
| 105 |
| 106 namespace ui { |
| 107 namespace test { |
| 108 |
| 109 // static |
| 110 EventGeneratorDelegate* EventGenerator::CreateDefaultPlatformDelegate( |
| 111 EventGenerator* owner, |
| 112 gfx::NativeWindow root_window, |
| 113 gfx::NativeWindow window) { |
| 114 return new aura::test::DefaultEventGeneratorDelegate(root_window); |
| 115 } |
| 116 |
| 117 } // namespace ui |
| 118 } // namespace test |
OLD | NEW |