Index: ui/aura/test/event_generator.cc |
diff --git a/ui/aura/test/event_generator.cc b/ui/aura/test/event_generator.cc |
index 42deba8e3723b3b401bd14471b05fabc72f40f81..0da5cb50da59821c9c777e5582154de3531ece22 100644 |
--- a/ui/aura/test/event_generator.cc |
+++ b/ui/aura/test/event_generator.cc |
@@ -4,7 +4,9 @@ |
#include "ui/aura/test/event_generator.h" |
+#include "base/bind.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop_proxy.h" |
#include "ui/aura/client/screen_position_client.h" |
#include "ui/aura/root_window.h" |
#include "ui/base/events/event.h" |
@@ -75,7 +77,8 @@ EventGenerator::EventGenerator(RootWindow* root_window) |
: delegate_(new DefaultEventGeneratorDelegate(root_window)), |
current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
flags_(0), |
- grab_(false) { |
+ grab_(false), |
+ post_events_(false) { |
} |
EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point) |
@@ -83,7 +86,8 @@ EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point) |
current_location_(point), |
current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
flags_(0), |
- grab_(false) { |
+ grab_(false), |
+ post_events_(false) { |
} |
EventGenerator::EventGenerator(RootWindow* root_window, Window* window) |
@@ -91,14 +95,16 @@ EventGenerator::EventGenerator(RootWindow* root_window, Window* window) |
current_location_(CenterOfWindow(window)), |
current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
flags_(0), |
- grab_(false) { |
+ grab_(false), |
+ post_events_(false) { |
} |
EventGenerator::EventGenerator(EventGeneratorDelegate* delegate) |
: delegate_(delegate), |
current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
flags_(0), |
- grab_(false) { |
+ grab_(false), |
+ post_events_(false) { |
} |
EventGenerator::~EventGenerator() { |
@@ -146,7 +152,7 @@ void EventGenerator::MoveMouseTo(const gfx::Point& point, int count) { |
UpdateCurrentRootWindow(move_point); |
ConvertPointToTarget(current_root_window_, &move_point); |
ui::MouseEvent mouseev(event_type, move_point, move_point, flags_); |
- Dispatch(mouseev); |
+ DispatchMouseEvent(mouseev); |
} |
current_location_ = point; |
} |
@@ -171,12 +177,12 @@ void EventGenerator::MoveMouseToCenterOf(Window* window) { |
void EventGenerator::PressTouch() { |
TestTouchEvent touchev( |
ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), flags_); |
- Dispatch(touchev); |
+ DispatchTouchEvent(touchev); |
} |
void EventGenerator::MoveTouch(const gfx::Point& point) { |
TestTouchEvent touchev(ui::ET_TOUCH_MOVED, point, flags_); |
- Dispatch(touchev); |
+ DispatchTouchEvent(touchev); |
current_location_ = point; |
if (!grab_) |
@@ -186,7 +192,7 @@ void EventGenerator::MoveTouch(const gfx::Point& point) { |
void EventGenerator::ReleaseTouch() { |
TestTouchEvent touchev( |
ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), flags_); |
- Dispatch(touchev); |
+ DispatchTouchEvent(touchev); |
} |
void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { |
@@ -205,12 +211,12 @@ void EventGenerator::GestureTapAt(const gfx::Point& location) { |
location, |
kTouchId, |
ui::EventTimeForNow()); |
- Dispatch(press); |
+ DispatchTouchEvent(press); |
ui::TouchEvent release( |
ui::ET_TOUCH_RELEASED, location, kTouchId, |
press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); |
- Dispatch(release); |
+ DispatchTouchEvent(release); |
} |
void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { |
@@ -219,12 +225,12 @@ void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { |
location, |
kTouchId, |
ui::EventTimeForNow()); |
- Dispatch(press); |
+ DispatchTouchEvent(press); |
ui::TouchEvent release( |
ui::ET_TOUCH_RELEASED, location, kTouchId, |
press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); |
- Dispatch(release); |
+ DispatchTouchEvent(release); |
} |
void EventGenerator::GestureScrollSequence(const gfx::Point& start, |
@@ -234,7 +240,7 @@ void EventGenerator::GestureScrollSequence(const gfx::Point& start, |
const int kTouchId = 5; |
base::TimeDelta timestamp = ui::EventTimeForNow(); |
ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); |
- Dispatch(press); |
+ DispatchTouchEvent(press); |
int dx = (end.x() - start.x()) / steps; |
int dy = (end.y() - start.y()) / steps; |
@@ -243,11 +249,11 @@ void EventGenerator::GestureScrollSequence(const gfx::Point& start, |
location.Offset(dx, dy); |
timestamp += step_delay; |
ui::TouchEvent move(ui::ET_TOUCH_MOVED, location, kTouchId, timestamp); |
- Dispatch(move); |
+ DispatchTouchEvent(move); |
} |
ui::TouchEvent release(ui::ET_TOUCH_RELEASED, end, kTouchId, timestamp); |
- Dispatch(release); |
+ DispatchTouchEvent(release); |
} |
void EventGenerator::GestureMultiFingerScroll(int count, |
@@ -268,7 +274,7 @@ void EventGenerator::GestureMultiFingerScroll(int count, |
for (int i = 0; i < count; ++i) { |
points[i] = start[i]; |
ui::TouchEvent press(ui::ET_TOUCH_PRESSED, points[i], i, press_time); |
- Dispatch(press); |
+ DispatchTouchEvent(press); |
} |
for (int step = 0; step < steps; ++step) { |
@@ -277,7 +283,7 @@ void EventGenerator::GestureMultiFingerScroll(int count, |
for (int i = 0; i < count; ++i) { |
points[i].Offset(delta_x, delta_y); |
ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time); |
- Dispatch(move); |
+ DispatchTouchEvent(move); |
} |
} |
@@ -286,10 +292,89 @@ void EventGenerator::GestureMultiFingerScroll(int count, |
for (int i = 0; i < count; ++i) { |
ui::TouchEvent release( |
ui::ET_TOUCH_RELEASED, points[i], i, release_time); |
- Dispatch(release); |
+ DispatchTouchEvent(release); |
} |
} |
+void EventGenerator::ScrollSequence(const gfx::Point& start, |
+ const base::TimeDelta& step_delay, |
+ float x_offset, |
+ float y_offset, |
+ int steps, |
+ int num_fingers) { |
+ base::TimeDelta timestamp = base::TimeDelta::FromInternalValue( |
sky
2013/01/22 18:05:01
Should this use ui::EventTimeForNow ?
DaveMoore
2013/01/27 21:21:54
Done.
|
+ base::TimeTicks::Now().ToInternalValue()); |
+ ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, |
+ start, |
+ timestamp, |
+ 0, |
+ 0, |
+ 0, |
+ num_fingers); |
+ DispatchScrollEvent(fling_cancel); |
+ |
+ float dx = x_offset / steps; |
+ float dy = y_offset / steps; |
+ for (int i = 0; i < steps; ++i) { |
+ timestamp += step_delay; |
+ ui::ScrollEvent move(ui::ET_SCROLL, |
+ start, |
+ timestamp, |
+ 0, |
+ dx, |
+ dy, |
+ num_fingers); |
+ DispatchScrollEvent(move); |
+ } |
+ |
+ ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START, |
+ start, |
+ timestamp, |
+ 0, |
+ x_offset, |
+ y_offset, |
+ num_fingers); |
+ DispatchScrollEvent(fling_start); |
+} |
+ |
+void EventGenerator::ScrollSequence(const gfx::Point& start, |
+ const base::TimeDelta& step_delay, |
+ const std::vector<gfx::Point> offsets, |
+ int num_fingers) { |
+ int steps = offsets.size(); |
+ base::TimeDelta timestamp = base::TimeDelta::FromInternalValue( |
+ base::TimeTicks::Now().ToInternalValue()); |
+ ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, |
+ start, |
+ timestamp, |
+ 0, |
+ 0, |
+ 0, |
+ num_fingers); |
+ DispatchScrollEvent(fling_cancel); |
+ |
+ for (int i = 0; i < steps; ++i) { |
+ timestamp += step_delay; |
+ ui::ScrollEvent scroll(ui::ET_SCROLL, |
+ start, |
+ timestamp, |
+ 0, |
+ offsets[i].x(), |
+ offsets[i].y(), |
+ num_fingers); |
+ DispatchScrollEvent(scroll); |
+ } |
+ |
+ ui::ScrollEvent fling_start(ui::ET_SCROLL_FLING_START, |
+ start, |
+ timestamp, |
+ 0, |
+ offsets[steps - 1].x(), |
+ offsets[steps - 1].y(), |
+ num_fingers); |
+ DispatchScrollEvent(fling_start); |
+} |
+ |
void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { |
DispatchKeyEvent(true, key_code, flags); |
} |
@@ -298,34 +383,63 @@ void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { |
DispatchKeyEvent(false, key_code, flags); |
} |
-void EventGenerator::Dispatch(ui::Event& event) { |
- switch (event.type()) { |
- case ui::ET_KEY_PRESSED: |
- case ui::ET_KEY_RELEASED: |
- current_root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent( |
- static_cast<ui::KeyEvent*>(&event)); |
- break; |
- case ui::ET_MOUSE_PRESSED: |
- case ui::ET_MOUSE_DRAGGED: |
- case ui::ET_MOUSE_RELEASED: |
- case ui::ET_MOUSE_MOVED: |
- case ui::ET_MOUSE_ENTERED: |
- case ui::ET_MOUSE_EXITED: |
- case ui::ET_MOUSEWHEEL: |
- current_root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( |
- static_cast<ui::MouseEvent*>(&event)); |
- break; |
- case ui::ET_TOUCH_RELEASED: |
- case ui::ET_TOUCH_PRESSED: |
- case ui::ET_TOUCH_MOVED: |
- case ui::ET_TOUCH_STATIONARY: |
- case ui::ET_TOUCH_CANCELLED: |
- current_root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( |
- static_cast<ui::TouchEvent*>(&event)); |
- break; |
- default: |
- NOTIMPLEMENTED(); |
- break; |
+void EventGenerator::DispatchKeyEvent(ui::KeyEvent& key_event) { |
+ if (post_events_) { |
+ if (pending_key_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchKeyEvent, |
+ base::Unretained(this))); |
+ } |
+ pending_key_events_.push_back(key_event); |
+ } else { |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent( |
+ &key_event); |
+ } |
+} |
+ |
+void EventGenerator::DispatchMouseEvent(ui::MouseEvent& mouse_event) { |
+ if (post_events_) { |
+ if (pending_mouse_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchMouseEvent, |
+ base::Unretained(this))); |
+ } |
+ pending_mouse_events_.push_back(mouse_event); |
+ } else { |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( |
+ &mouse_event); |
+ } |
+} |
+ |
+void EventGenerator::DispatchTouchEvent(ui::TouchEvent& touch_event) { |
+ if (post_events_) { |
+ if (pending_touch_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchTouchEvent, |
+ base::Unretained(this))); |
+ } |
+ pending_touch_events_.push_back(touch_event); |
+ } else { |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( |
+ &touch_event); |
+ } |
+} |
+ |
+void EventGenerator::DispatchScrollEvent(ui::ScrollEvent& scroll_event) { |
+ if (post_events_) { |
+ if (pending_scroll_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchScrollEvent, |
+ base::Unretained(this))); |
+ } |
+ pending_scroll_events_.push_back(scroll_event); |
+ } else { |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostScrollEvent( |
+ &scroll_event); |
} |
} |
@@ -338,7 +452,7 @@ void EventGenerator::DispatchKeyEvent(bool is_press, |
if (is_press && character) { |
MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; |
TestKeyEvent keyev(native_event, flags, false); |
- Dispatch(keyev); |
+ DispatchKeyEvent(keyev); |
// On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character |
// if the key event cooresponds to a real character. |
key_press = WM_CHAR; |
@@ -357,7 +471,7 @@ void EventGenerator::DispatchKeyEvent(bool is_press, |
ui::KeyEvent keyev(type, key_code, flags, false); |
#endif // USE_X11 |
#endif // OS_WIN |
- Dispatch(keyev); |
+ DispatchKeyEvent(keyev); |
} |
void EventGenerator::UpdateCurrentRootWindow(const gfx::Point& point) { |
@@ -370,7 +484,7 @@ void EventGenerator::PressButton(int flag) { |
grab_ = flags_ & kAllButtonMask; |
gfx::Point location = GetLocationInCurrentRoot(); |
ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_); |
- Dispatch(mouseev); |
+ DispatchMouseEvent(mouseev); |
} |
} |
@@ -379,7 +493,7 @@ void EventGenerator::ReleaseButton(int flag) { |
gfx::Point location = GetLocationInCurrentRoot(); |
ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location, |
location, flags_); |
- Dispatch(mouseev); |
+ DispatchMouseEvent(mouseev); |
flags_ ^= flag; |
} |
grab_ = flags_ & kAllButtonMask; |
@@ -419,5 +533,60 @@ gfx::Point EventGenerator::CenterOfWindow(const Window* window) const { |
return center; |
} |
+void EventGenerator::DoDispatchKeyEvent() { |
sky
2013/01/22 18:05:01
How about a single member for std::list<Event*> wi
DaveMoore
2013/01/27 21:21:54
rewritten to minimize copied code.
On 2013/01/22 1
|
+ DCHECK(!pending_key_events_.empty()); |
+ ui::KeyEvent key_event = pending_key_events_.front(); |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent(&key_event); |
+ pending_key_events_.pop_front(); |
+ if (!pending_key_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchKeyEvent, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
+void EventGenerator::DoDispatchMouseEvent() { |
+ DCHECK(!pending_mouse_events_.empty()); |
+ ui::MouseEvent mouse_event = pending_mouse_events_.front(); |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( |
+ &mouse_event); |
+ pending_mouse_events_.pop_front(); |
+ if (!pending_mouse_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchMouseEvent, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
+void EventGenerator::DoDispatchTouchEvent() { |
+ DCHECK(!pending_touch_events_.empty()); |
+ ui::TouchEvent touch_event = pending_touch_events_.front(); |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( |
+ &touch_event); |
+ pending_touch_events_.pop_front(); |
+ if (!pending_touch_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchTouchEvent, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
+void EventGenerator::DoDispatchScrollEvent() { |
+ DCHECK(!pending_scroll_events_.empty()); |
+ ui::ScrollEvent scroll_event = pending_scroll_events_.front(); |
+ current_root_window_->AsRootWindowHostDelegate()->OnHostScrollEvent( |
+ &scroll_event); |
+ pending_scroll_events_.pop_front(); |
+ if (!pending_scroll_events_.empty()) { |
+ base::MessageLoopProxy::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&EventGenerator::DoDispatchScrollEvent, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
} // namespace test |
} // namespace aura |