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" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "ui/aura/client/screen_position_client.h" | 10 #include "ui/aura/client/screen_position_client.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 : KeyEvent(native_event, is_char) { | 62 : KeyEvent(native_event, is_char) { |
63 set_flags(flags); | 63 set_flags(flags); |
64 } | 64 } |
65 }; | 65 }; |
66 | 66 |
67 class TestTouchEvent : public ui::TouchEvent { | 67 class TestTouchEvent : public ui::TouchEvent { |
68 public: | 68 public: |
69 TestTouchEvent(ui::EventType type, | 69 TestTouchEvent(ui::EventType type, |
70 const gfx::Point& root_location, | 70 const gfx::Point& root_location, |
71 int touch_id, | 71 int touch_id, |
72 int flags) | 72 int flags, |
73 : TouchEvent(type, root_location, flags, touch_id, ui::EventTimeForNow(), | 73 base::TimeDelta timestamp) |
| 74 : TouchEvent(type, root_location, flags, touch_id, timestamp, |
74 1.0f, 1.0f, 1.0f, 1.0f) { | 75 1.0f, 1.0f, 1.0f, 1.0f) { |
75 } | 76 } |
76 | 77 |
77 private: | 78 private: |
78 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); | 79 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); |
79 }; | 80 }; |
80 | 81 |
81 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON; | 82 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON; |
82 | 83 |
83 } // namespace | 84 } // namespace |
84 | 85 |
85 EventGenerator::EventGenerator(Window* root_window) | 86 EventGenerator::EventGenerator(Window* root_window) |
86 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | 87 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
87 current_host_(delegate_->GetHostAt(current_location_)), | 88 current_host_(delegate_->GetHostAt(current_location_)), |
88 flags_(0), | 89 flags_(0), |
89 grab_(false), | 90 grab_(false), |
90 async_(false) { | 91 async_(false), |
| 92 use_simulated_time_(false) { |
91 } | 93 } |
92 | 94 |
93 EventGenerator::EventGenerator(Window* root_window, const gfx::Point& point) | 95 EventGenerator::EventGenerator(Window* root_window, const gfx::Point& point) |
94 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | 96 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
95 current_location_(point), | 97 current_location_(point), |
96 current_host_(delegate_->GetHostAt(current_location_)), | 98 current_host_(delegate_->GetHostAt(current_location_)), |
97 flags_(0), | 99 flags_(0), |
98 grab_(false), | 100 grab_(false), |
99 async_(false) { | 101 async_(false), |
| 102 use_simulated_time_(false) { |
100 } | 103 } |
101 | 104 |
102 EventGenerator::EventGenerator(Window* root_window, Window* window) | 105 EventGenerator::EventGenerator(Window* root_window, Window* window) |
103 : delegate_(new DefaultEventGeneratorDelegate(root_window)), | 106 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
104 current_location_(CenterOfWindow(window)), | 107 current_location_(CenterOfWindow(window)), |
105 current_host_(delegate_->GetHostAt(current_location_)), | 108 current_host_(delegate_->GetHostAt(current_location_)), |
106 flags_(0), | 109 flags_(0), |
107 grab_(false), | 110 grab_(false), |
108 async_(false) { | 111 async_(false), |
| 112 use_simulated_time_(false) { |
109 } | 113 } |
110 | 114 |
111 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate) | 115 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate) |
112 : delegate_(delegate), | 116 : delegate_(delegate), |
113 current_host_(delegate_->GetHostAt(current_location_)), | 117 current_host_(delegate_->GetHostAt(current_location_)), |
114 flags_(0), | 118 flags_(0), |
115 grab_(false), | 119 grab_(false), |
116 async_(false) { | 120 async_(false), |
| 121 use_simulated_time_(false) { |
117 } | 122 } |
118 | 123 |
119 EventGenerator::~EventGenerator() { | 124 EventGenerator::~EventGenerator() { |
120 for (std::list<ui::Event*>::iterator i = pending_events_.begin(); | 125 for (std::list<ui::Event*>::iterator i = pending_events_.begin(); |
121 i != pending_events_.end(); ++i) | 126 i != pending_events_.end(); ++i) |
122 delete *i; | 127 delete *i; |
123 pending_events_.clear(); | 128 pending_events_.clear(); |
124 } | 129 } |
125 | 130 |
126 void EventGenerator::PressLeftButton() { | 131 void EventGenerator::PressLeftButton() { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 void EventGenerator::MoveMouseToCenterOf(Window* window) { | 217 void EventGenerator::MoveMouseToCenterOf(Window* window) { |
213 MoveMouseTo(CenterOfWindow(window)); | 218 MoveMouseTo(CenterOfWindow(window)); |
214 } | 219 } |
215 | 220 |
216 void EventGenerator::PressTouch() { | 221 void EventGenerator::PressTouch() { |
217 PressTouchId(0); | 222 PressTouchId(0); |
218 } | 223 } |
219 | 224 |
220 void EventGenerator::PressTouchId(int touch_id) { | 225 void EventGenerator::PressTouchId(int touch_id) { |
221 TestTouchEvent touchev( | 226 TestTouchEvent touchev( |
222 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags_); | 227 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags_, |
| 228 Now()); |
223 Dispatch(&touchev); | 229 Dispatch(&touchev); |
224 } | 230 } |
225 | 231 |
226 void EventGenerator::MoveTouch(const gfx::Point& point) { | 232 void EventGenerator::MoveTouch(const gfx::Point& point) { |
227 MoveTouchId(point, 0); | 233 MoveTouchId(point, 0); |
228 } | 234 } |
229 | 235 |
230 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) { | 236 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) { |
231 current_location_ = point; | 237 current_location_ = point; |
232 TestTouchEvent touchev( | 238 TestTouchEvent touchev( |
233 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags_); | 239 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags_, Now()); |
234 Dispatch(&touchev); | 240 Dispatch(&touchev); |
235 | 241 |
236 if (!grab_) | 242 if (!grab_) |
237 UpdateCurrentDispatcher(point); | 243 UpdateCurrentDispatcher(point); |
238 } | 244 } |
239 | 245 |
240 void EventGenerator::ReleaseTouch() { | 246 void EventGenerator::ReleaseTouch() { |
241 ReleaseTouchId(0); | 247 ReleaseTouchId(0); |
242 } | 248 } |
243 | 249 |
244 void EventGenerator::ReleaseTouchId(int touch_id) { | 250 void EventGenerator::ReleaseTouchId(int touch_id) { |
245 TestTouchEvent touchev( | 251 TestTouchEvent touchev( |
246 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags_); | 252 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags_, |
| 253 Now()); |
247 Dispatch(&touchev); | 254 Dispatch(&touchev); |
248 } | 255 } |
249 | 256 |
250 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { | 257 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { |
251 PressTouch(); | 258 PressTouch(); |
252 MoveTouch(point); | 259 MoveTouch(point); |
253 ReleaseTouch(); | 260 ReleaseTouch(); |
254 } | 261 } |
255 | 262 |
256 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { | 263 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { |
257 PressMoveAndReleaseTouchTo(CenterOfWindow(window)); | 264 PressMoveAndReleaseTouchTo(CenterOfWindow(window)); |
258 } | 265 } |
259 | 266 |
260 void EventGenerator::GestureEdgeSwipe() { | 267 void EventGenerator::GestureEdgeSwipe() { |
261 ui::GestureEvent gesture( | 268 ui::GestureEvent gesture( |
262 ui::ET_GESTURE_WIN8_EDGE_SWIPE, | 269 ui::ET_GESTURE_WIN8_EDGE_SWIPE, |
263 0, | 270 0, |
264 0, | 271 0, |
265 0, | 272 0, |
266 ui::EventTimeForNow(), | 273 Now(), |
267 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE, 0, 0), | 274 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE, 0, 0), |
268 0); | 275 0); |
269 Dispatch(&gesture); | 276 Dispatch(&gesture); |
270 } | 277 } |
271 | 278 |
272 void EventGenerator::GestureTapAt(const gfx::Point& location) { | 279 void EventGenerator::GestureTapAt(const gfx::Point& location) { |
273 const int kTouchId = 2; | 280 const int kTouchId = 2; |
274 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | 281 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, |
275 location, | 282 location, |
276 kTouchId, | 283 kTouchId, |
277 ui::EventTimeForNow()); | 284 Now()); |
278 Dispatch(&press); | 285 Dispatch(&press); |
279 | 286 |
280 ui::TouchEvent release( | 287 ui::TouchEvent release( |
281 ui::ET_TOUCH_RELEASED, location, kTouchId, | 288 ui::ET_TOUCH_RELEASED, location, kTouchId, |
282 press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); | 289 press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); |
283 Dispatch(&release); | 290 Dispatch(&release); |
284 } | 291 } |
285 | 292 |
286 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { | 293 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { |
287 const int kTouchId = 3; | 294 const int kTouchId = 3; |
288 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | 295 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, |
289 location, | 296 location, |
290 kTouchId, | 297 kTouchId, |
291 ui::EventTimeForNow()); | 298 Now()); |
292 Dispatch(&press); | 299 Dispatch(&press); |
293 | 300 |
294 ui::TouchEvent release( | 301 ui::TouchEvent release( |
295 ui::ET_TOUCH_RELEASED, location, kTouchId, | 302 ui::ET_TOUCH_RELEASED, location, kTouchId, |
296 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); | 303 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); |
297 Dispatch(&release); | 304 Dispatch(&release); |
298 } | 305 } |
299 | 306 |
300 void EventGenerator::GestureScrollSequence(const gfx::Point& start, | 307 void EventGenerator::GestureScrollSequence(const gfx::Point& start, |
301 const gfx::Point& end, | 308 const gfx::Point& end, |
302 const base::TimeDelta& step_delay, | 309 const base::TimeDelta& step_delay, |
303 int steps) { | 310 int steps) { |
304 GestureScrollSequenceWithCallback(start, end, step_delay, steps, | 311 GestureScrollSequenceWithCallback(start, end, step_delay, steps, |
305 base::Bind(&DummyCallback)); | 312 base::Bind(&DummyCallback)); |
306 } | 313 } |
307 | 314 |
308 void EventGenerator::GestureScrollSequenceWithCallback( | 315 void EventGenerator::GestureScrollSequenceWithCallback( |
309 const gfx::Point& start, | 316 const gfx::Point& start, |
310 const gfx::Point& end, | 317 const gfx::Point& end, |
311 const base::TimeDelta& step_delay, | 318 const base::TimeDelta& step_delay, |
312 int steps, | 319 int steps, |
313 const ScrollStepCallback& callback) { | 320 const ScrollStepCallback& callback) { |
314 const int kTouchId = 5; | 321 const int kTouchId = 5; |
315 base::TimeDelta timestamp = ui::EventTimeForNow(); | 322 base::TimeDelta timestamp = Now(); |
316 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); | 323 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); |
317 Dispatch(&press); | 324 Dispatch(&press); |
318 | 325 |
319 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF()); | 326 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF()); |
320 | 327 |
321 int dx = (end.x() - start.x()) / steps; | 328 int dx = (end.x() - start.x()) / steps; |
322 int dy = (end.y() - start.y()) / steps; | 329 int dy = (end.y() - start.y()) / steps; |
323 gfx::Point location = start; | 330 gfx::Point location = start; |
324 for (int i = 0; i < steps; ++i) { | 331 for (int i = 0; i < steps; ++i) { |
325 location.Offset(dx, dy); | 332 location.Offset(dx, dy); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 CHECK_LE(count, kMaxTouchPoints); | 367 CHECK_LE(count, kMaxTouchPoints); |
361 CHECK_GT(steps, 0); | 368 CHECK_GT(steps, 0); |
362 | 369 |
363 int delta_x = move_x / steps; | 370 int delta_x = move_x / steps; |
364 int delta_y = move_y / steps; | 371 int delta_y = move_y / steps; |
365 | 372 |
366 for (int i = 0; i < count; ++i) { | 373 for (int i = 0; i < count; ++i) { |
367 points[i] = start[i]; | 374 points[i] = start[i]; |
368 } | 375 } |
369 | 376 |
370 base::TimeDelta press_time_first = ui::EventTimeForNow(); | 377 base::TimeDelta press_time_first = Now(); |
371 base::TimeDelta press_time[kMaxTouchPoints]; | 378 base::TimeDelta press_time[kMaxTouchPoints]; |
372 bool pressed[kMaxTouchPoints]; | 379 bool pressed[kMaxTouchPoints]; |
373 for (int i = 0; i < count; ++i) { | 380 for (int i = 0; i < count; ++i) { |
374 pressed[i] = false; | 381 pressed[i] = false; |
375 press_time[i] = press_time_first + | 382 press_time[i] = press_time_first + |
376 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]); | 383 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]); |
377 } | 384 } |
378 | 385 |
379 int last_id = 0; | 386 int last_id = 0; |
380 for (int step = 0; step < steps; ++step) { | 387 for (int step = 0; step < steps; ++step) { |
(...skipping 29 matching lines...) Expand all Loading... |
410 Dispatch(&release); | 417 Dispatch(&release); |
411 } | 418 } |
412 } | 419 } |
413 | 420 |
414 void EventGenerator::ScrollSequence(const gfx::Point& start, | 421 void EventGenerator::ScrollSequence(const gfx::Point& start, |
415 const base::TimeDelta& step_delay, | 422 const base::TimeDelta& step_delay, |
416 float x_offset, | 423 float x_offset, |
417 float y_offset, | 424 float y_offset, |
418 int steps, | 425 int steps, |
419 int num_fingers) { | 426 int num_fingers) { |
420 base::TimeDelta timestamp = base::TimeDelta::FromInternalValue( | 427 base::TimeDelta timestamp = Now(); |
421 base::TimeTicks::Now().ToInternalValue()); | |
422 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, | 428 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, |
423 start, | 429 start, |
424 timestamp, | 430 timestamp, |
425 0, | 431 0, |
426 0, 0, | 432 0, 0, |
427 0, 0, | 433 0, 0, |
428 num_fingers); | 434 num_fingers); |
429 Dispatch(&fling_cancel); | 435 Dispatch(&fling_cancel); |
430 | 436 |
431 float dx = x_offset / steps; | 437 float dx = x_offset / steps; |
(...skipping 18 matching lines...) Expand all Loading... |
450 x_offset, y_offset, | 456 x_offset, y_offset, |
451 num_fingers); | 457 num_fingers); |
452 Dispatch(&fling_start); | 458 Dispatch(&fling_start); |
453 } | 459 } |
454 | 460 |
455 void EventGenerator::ScrollSequence(const gfx::Point& start, | 461 void EventGenerator::ScrollSequence(const gfx::Point& start, |
456 const base::TimeDelta& step_delay, | 462 const base::TimeDelta& step_delay, |
457 const std::vector<gfx::Point>& offsets, | 463 const std::vector<gfx::Point>& offsets, |
458 int num_fingers) { | 464 int num_fingers) { |
459 int steps = offsets.size(); | 465 int steps = offsets.size(); |
460 base::TimeDelta timestamp = ui::EventTimeForNow(); | 466 base::TimeDelta timestamp = Now(); |
461 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, | 467 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, |
462 start, | 468 start, |
463 timestamp, | 469 timestamp, |
464 0, | 470 0, |
465 0, 0, | 471 0, 0, |
466 0, 0, | 472 0, 0, |
467 num_fingers); | 473 num_fingers); |
468 Dispatch(&fling_cancel); | 474 Dispatch(&fling_cancel); |
469 | 475 |
470 for (int i = 0; i < steps; ++i) { | 476 for (int i = 0; i < steps; ++i) { |
(...skipping 23 matching lines...) Expand all Loading... |
494 } | 500 } |
495 | 501 |
496 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { | 502 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { |
497 DispatchKeyEvent(false, key_code, flags); | 503 DispatchKeyEvent(false, key_code, flags); |
498 } | 504 } |
499 | 505 |
500 void EventGenerator::Dispatch(ui::Event* event) { | 506 void EventGenerator::Dispatch(ui::Event* event) { |
501 DoDispatchEvent(event, async_); | 507 DoDispatchEvent(event, async_); |
502 } | 508 } |
503 | 509 |
| 510 void EventGenerator::UseSimulatedTime() { |
| 511 use_simulated_time_ = true; |
| 512 } |
| 513 |
| 514 void EventGenerator::AdvanceSimulatedTimeBy(base::TimeDelta delta) { |
| 515 simulated_time_ += delta; |
| 516 } |
| 517 |
| 518 base::TimeDelta EventGenerator::Now() { |
| 519 if (use_simulated_time_) |
| 520 return simulated_time_; |
| 521 return ui::EventTimeForNow(); |
| 522 } |
| 523 |
504 void EventGenerator::DispatchKeyEvent(bool is_press, | 524 void EventGenerator::DispatchKeyEvent(bool is_press, |
505 ui::KeyboardCode key_code, | 525 ui::KeyboardCode key_code, |
506 int flags) { | 526 int flags) { |
507 #if defined(OS_WIN) | 527 #if defined(OS_WIN) |
508 UINT key_press = WM_KEYDOWN; | 528 UINT key_press = WM_KEYDOWN; |
509 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); | 529 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); |
510 if (is_press && character) { | 530 if (is_press && character) { |
511 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; | 531 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; |
512 TestKeyEvent keyev(native_event, flags, false); | 532 TestKeyEvent keyev(native_event, flags, false); |
513 Dispatch(&keyev); | 533 Dispatch(&keyev); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 pending_events_.pop_front(); | 651 pending_events_.pop_front(); |
632 delete event; | 652 delete event; |
633 if (!pending_events_.empty()) { | 653 if (!pending_events_.empty()) { |
634 base::MessageLoopProxy::current()->PostTask( | 654 base::MessageLoopProxy::current()->PostTask( |
635 FROM_HERE, | 655 FROM_HERE, |
636 base::Bind(&EventGenerator::DispatchNextPendingEvent, | 656 base::Bind(&EventGenerator::DispatchNextPendingEvent, |
637 base::Unretained(this))); | 657 base::Unretained(this))); |
638 } | 658 } |
639 } | 659 } |
640 | 660 |
641 | |
642 } // namespace test | 661 } // namespace test |
643 } // namespace aura | 662 } // namespace aura |
OLD | NEW |