OLD | NEW |
1 // Copyright 2014 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 #include "ui/events/test/event_generator.h" | 5 #include "ui/events/test/event_generator.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/time/default_tick_clock.h" | 12 #include "base/time/default_tick_clock.h" |
| 13 #include "base/time/tick_clock.h" |
13 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
14 #include "ui/events/event_source.h" | 15 #include "ui/events/event_source.h" |
15 #include "ui/events/event_utils.h" | 16 #include "ui/events/event_utils.h" |
16 #include "ui/events/test/events_test_utils.h" | 17 #include "ui/events/test/events_test_utils.h" |
17 #include "ui/gfx/geometry/vector2d_conversions.h" | 18 #include "ui/gfx/geometry/vector2d_conversions.h" |
18 | 19 |
19 #if defined(USE_X11) | 20 #if defined(USE_X11) |
20 #include <X11/Xlib.h> | 21 #include <X11/Xlib.h> |
21 #include "ui/events/test/events_test_utils_x11.h" | 22 #include "ui/events/test/events_test_utils_x11.h" |
22 #endif | 23 #endif |
23 | 24 |
24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
25 #include "ui/events/keycodes/keyboard_code_conversion.h" | 26 #include "ui/events/keycodes/keyboard_code_conversion.h" |
26 #endif | 27 #endif |
27 | 28 |
28 namespace ui { | 29 namespace ui { |
29 namespace test { | 30 namespace test { |
30 namespace { | 31 namespace { |
31 | 32 |
32 void DummyCallback(EventType, const gfx::Vector2dF&) { | 33 void DummyCallback(EventType, const gfx::Vector2dF&) { |
33 } | 34 } |
34 | 35 |
| 36 class TestTickClock : public base::TickClock { |
| 37 public: |
| 38 // Starts off with a clock set to TimeTicks(). |
| 39 TestTickClock() {} |
| 40 |
| 41 base::TimeTicks NowTicks() override { |
| 42 return base::TimeTicks::FromInternalValue(ticks_++ * 1000); |
| 43 } |
| 44 |
| 45 private: |
| 46 int64 ticks_ = 1; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(TestTickClock); |
| 49 }; |
| 50 |
35 class TestKeyEvent : public ui::KeyEvent { | 51 class TestKeyEvent : public ui::KeyEvent { |
36 public: | 52 public: |
37 TestKeyEvent(const base::NativeEvent& native_event, int flags) | 53 TestKeyEvent(const base::NativeEvent& native_event, int flags) |
38 : KeyEvent(native_event) { | 54 : KeyEvent(native_event) { |
39 set_flags(flags); | 55 set_flags(flags); |
40 } | 56 } |
41 }; | 57 }; |
42 | 58 |
43 class TestTouchEvent : public ui::TouchEvent { | 59 class TestTouchEvent : public ui::TouchEvent { |
44 public: | 60 public: |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 return base::TimeDelta::FromInternalValue( | 527 return base::TimeDelta::FromInternalValue( |
512 tick_clock_->NowTicks().ToInternalValue()); | 528 tick_clock_->NowTicks().ToInternalValue()); |
513 } | 529 } |
514 | 530 |
515 void EventGenerator::Init(gfx::NativeWindow root_window, | 531 void EventGenerator::Init(gfx::NativeWindow root_window, |
516 gfx::NativeWindow window_context) { | 532 gfx::NativeWindow window_context) { |
517 delegate()->SetContext(this, root_window, window_context); | 533 delegate()->SetContext(this, root_window, window_context); |
518 if (window_context) | 534 if (window_context) |
519 current_location_ = delegate()->CenterOfWindow(window_context); | 535 current_location_ = delegate()->CenterOfWindow(window_context); |
520 current_target_ = delegate()->GetTargetAt(current_location_); | 536 current_target_ = delegate()->GetTargetAt(current_location_); |
| 537 |
| 538 scoped_ptr<base::TickClock> clock(new TestTickClock); |
| 539 SetTickClock(clock.Pass()); |
521 } | 540 } |
522 | 541 |
523 void EventGenerator::DispatchKeyEvent(bool is_press, | 542 void EventGenerator::DispatchKeyEvent(bool is_press, |
524 ui::KeyboardCode key_code, | 543 ui::KeyboardCode key_code, |
525 int flags) { | 544 int flags) { |
526 #if defined(OS_WIN) | 545 #if defined(OS_WIN) |
527 UINT key_press = WM_KEYDOWN; | 546 UINT key_press = WM_KEYDOWN; |
528 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); | 547 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); |
529 if (is_press && character) { | 548 if (is_press && character) { |
530 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; | 549 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; |
531 TestKeyEvent keyev(native_event, flags); | 550 TestKeyEvent keyev(native_event, flags); |
532 Dispatch(&keyev); | 551 Dispatch(&keyev); |
533 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character | 552 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character |
534 // if the key event cooresponds to a real character. | 553 // if the key event cooresponds to a real character. |
535 key_press = WM_CHAR; | 554 key_press = WM_CHAR; |
536 key_code = static_cast<ui::KeyboardCode>(character); | 555 key_code = static_cast<ui::KeyboardCode>(character); |
537 } | 556 } |
538 MSG native_event = | 557 MSG native_event = |
539 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 }; | 558 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 }; |
| 559 native_event.time = Now().InMicroseconds(); |
540 TestKeyEvent keyev(native_event, flags); | 560 TestKeyEvent keyev(native_event, flags); |
541 #elif defined(USE_X11) | 561 #elif defined(USE_X11) |
542 ui::ScopedXI2Event xevent; | 562 ui::ScopedXI2Event xevent; |
543 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, | 563 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, |
544 key_code, | 564 key_code, |
545 flags); | 565 flags); |
| 566 static_cast<XEvent*>(xevent)->xkey.time = Now().InMicroseconds(); |
546 ui::KeyEvent keyev(xevent); | 567 ui::KeyEvent keyev(xevent); |
547 #else | 568 #else |
548 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; | 569 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; |
549 ui::KeyEvent keyev(type, key_code, flags); | 570 ui::KeyEvent keyev(type, key_code, flags); |
550 #endif // OS_WIN | 571 #endif // OS_WIN |
551 Dispatch(&keyev); | 572 Dispatch(&keyev); |
552 } | 573 } |
553 | 574 |
554 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { | 575 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { |
555 current_target_ = delegate()->GetTargetAt(point); | 576 current_target_ = delegate()->GetTargetAt(point); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 return default_delegate; | 662 return default_delegate; |
642 } | 663 } |
643 | 664 |
644 EventGeneratorDelegate* EventGenerator::delegate() { | 665 EventGeneratorDelegate* EventGenerator::delegate() { |
645 return const_cast<EventGeneratorDelegate*>( | 666 return const_cast<EventGeneratorDelegate*>( |
646 const_cast<const EventGenerator*>(this)->delegate()); | 667 const_cast<const EventGenerator*>(this)->delegate()); |
647 } | 668 } |
648 | 669 |
649 } // namespace test | 670 } // namespace test |
650 } // namespace ui | 671 } // namespace ui |
OLD | NEW |