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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "ui/aura/client/screen_position_client.h" |
8 #include "ui/aura/root_window.h" | 9 #include "ui/aura/root_window.h" |
9 #include "ui/base/events/event.h" | 10 #include "ui/base/events/event.h" |
10 #include "ui/base/events/event_utils.h" | 11 #include "ui/base/events/event_utils.h" |
11 #include "ui/gfx/vector2d_conversions.h" | 12 #include "ui/gfx/vector2d_conversions.h" |
12 | 13 |
13 #if defined(USE_X11) | 14 #if defined(USE_X11) |
14 #include <X11/Xlib.h> | 15 #include <X11/Xlib.h> |
15 #include "ui/base/x/x11_util.h" | 16 #include "ui/base/x/x11_util.h" |
16 #endif | 17 #endif |
17 | 18 |
18 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
19 #include "ui/base/keycodes/keyboard_code_conversion.h" | 20 #include "ui/base/keycodes/keyboard_code_conversion.h" |
20 #endif | 21 #endif |
21 | 22 |
| 23 namespace aura { |
| 24 namespace test { |
22 namespace { | 25 namespace { |
23 | 26 |
| 27 class DefaultEventGeneratorDelegate : public EventGeneratorDelegate { |
| 28 public: |
| 29 explicit DefaultEventGeneratorDelegate(RootWindow* root_window) |
| 30 : root_window_(root_window) {} |
| 31 virtual ~DefaultEventGeneratorDelegate() {} |
| 32 |
| 33 // EventGeneratorDelegate overrides: |
| 34 RootWindow* GetRootWindowAt(const gfx::Point& point) const OVERRIDE { |
| 35 return root_window_; |
| 36 } |
| 37 |
| 38 client::ScreenPositionClient* GetScreenPositionClient( |
| 39 const aura::Window* window) const OVERRIDE { |
| 40 return NULL; |
| 41 } |
| 42 |
| 43 private: |
| 44 RootWindow* root_window_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate); |
| 47 }; |
| 48 |
24 class TestKeyEvent : public ui::KeyEvent { | 49 class TestKeyEvent : public ui::KeyEvent { |
25 public: | 50 public: |
26 TestKeyEvent(const base::NativeEvent& native_event, int flags, bool is_char) | 51 TestKeyEvent(const base::NativeEvent& native_event, int flags, bool is_char) |
27 : KeyEvent(native_event, is_char) { | 52 : KeyEvent(native_event, is_char) { |
28 set_flags(flags); | 53 set_flags(flags); |
29 } | 54 } |
30 }; | 55 }; |
31 | 56 |
32 class TestTouchEvent : public ui::TouchEvent { | 57 class TestTouchEvent : public ui::TouchEvent { |
33 public: | 58 public: |
34 TestTouchEvent(ui::EventType type, | 59 TestTouchEvent(ui::EventType type, |
35 const gfx::Point& root_location, | 60 const gfx::Point& root_location, |
36 int flags) | 61 int flags) |
37 : TouchEvent(type, root_location, 0, ui::EventTimeForNow()) { | 62 : TouchEvent(type, root_location, 0, ui::EventTimeForNow()) { |
38 set_flags(flags); | 63 set_flags(flags); |
39 } | 64 } |
40 | 65 |
41 private: | 66 private: |
42 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); | 67 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); |
43 }; | 68 }; |
44 | 69 |
45 gfx::Point CenterOfWindowInRootWindowCoordinate(aura::RootWindow* root_window, | 70 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON; |
46 aura::Window* window) { | |
47 gfx::Point center = window->bounds().CenterPoint(); | |
48 aura::Window::ConvertPointToTarget(window->parent(), root_window, ¢er); | |
49 return center; | |
50 } | |
51 | 71 |
52 } // namespace | 72 } // namespace |
53 | 73 |
54 namespace aura { | |
55 namespace test { | |
56 | |
57 EventGenerator::EventGenerator(RootWindow* root_window) | 74 EventGenerator::EventGenerator(RootWindow* root_window) |
58 : root_window_(root_window), | 75 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
59 flags_(0) { | 76 current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
| 77 flags_(0), |
| 78 grab_(false) { |
60 } | 79 } |
61 | 80 |
62 EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point) | 81 EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point) |
63 : root_window_(root_window), | 82 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
| 83 current_location_(point), |
| 84 current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
64 flags_(0), | 85 flags_(0), |
65 current_location_(point) { | 86 grab_(false) { |
66 } | 87 } |
67 | 88 |
68 EventGenerator::EventGenerator(RootWindow* root_window, Window* window) | 89 EventGenerator::EventGenerator(RootWindow* root_window, Window* window) |
69 : root_window_(root_window), | 90 : delegate_(new DefaultEventGeneratorDelegate(root_window)), |
| 91 current_location_(CenterOfWindow(window)), |
| 92 current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
70 flags_(0), | 93 flags_(0), |
71 current_location_(CenterOfWindowInRootWindowCoordinate(root_window, | 94 grab_(false) { |
72 window)) { | 95 } |
| 96 |
| 97 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate) |
| 98 : delegate_(delegate), |
| 99 current_root_window_(delegate_->GetRootWindowAt(current_location_)), |
| 100 flags_(0), |
| 101 grab_(false) { |
73 } | 102 } |
74 | 103 |
75 EventGenerator::~EventGenerator() { | 104 EventGenerator::~EventGenerator() { |
76 } | 105 } |
77 | 106 |
78 void EventGenerator::PressLeftButton() { | 107 void EventGenerator::PressLeftButton() { |
79 if ((flags_ & ui::EF_LEFT_MOUSE_BUTTON) == 0) { | 108 PressButton(ui::EF_LEFT_MOUSE_BUTTON); |
80 flags_ |= ui::EF_LEFT_MOUSE_BUTTON; | |
81 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, current_location_, | |
82 current_location_, flags_); | |
83 Dispatch(mouseev); | |
84 } | |
85 } | 109 } |
86 | 110 |
87 void EventGenerator::ReleaseLeftButton() { | 111 void EventGenerator::ReleaseLeftButton() { |
88 if (flags_ & ui::EF_LEFT_MOUSE_BUTTON) { | 112 ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON); |
89 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, current_location_, | |
90 current_location_, flags_); | |
91 Dispatch(mouseev); | |
92 flags_ ^= ui::EF_LEFT_MOUSE_BUTTON; | |
93 } | |
94 } | 113 } |
95 | 114 |
96 void EventGenerator::ClickLeftButton() { | 115 void EventGenerator::ClickLeftButton() { |
97 PressLeftButton(); | 116 PressLeftButton(); |
98 ReleaseLeftButton(); | 117 ReleaseLeftButton(); |
99 } | 118 } |
100 | 119 |
101 void EventGenerator::DoubleClickLeftButton() { | 120 void EventGenerator::DoubleClickLeftButton() { |
102 flags_ |= ui::EF_IS_DOUBLE_CLICK; | 121 flags_ |= ui::EF_IS_DOUBLE_CLICK; |
103 PressLeftButton(); | 122 PressLeftButton(); |
104 flags_ ^= ui::EF_IS_DOUBLE_CLICK; | 123 flags_ ^= ui::EF_IS_DOUBLE_CLICK; |
105 ReleaseLeftButton(); | 124 ReleaseLeftButton(); |
106 } | 125 } |
107 | 126 |
108 void EventGenerator::PressRightButton() { | 127 void EventGenerator::PressRightButton() { |
109 if ((flags_ & ui::EF_RIGHT_MOUSE_BUTTON) == 0) { | 128 PressButton(ui::EF_RIGHT_MOUSE_BUTTON); |
110 flags_ |= ui::EF_RIGHT_MOUSE_BUTTON; | |
111 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, current_location_, | |
112 current_location_, flags_); | |
113 Dispatch(mouseev); | |
114 } | |
115 } | 129 } |
116 | 130 |
117 void EventGenerator::ReleaseRightButton() { | 131 void EventGenerator::ReleaseRightButton() { |
118 if (flags_ & ui::EF_RIGHT_MOUSE_BUTTON) { | 132 ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON); |
119 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, current_location_, | |
120 current_location_, flags_); | |
121 Dispatch(mouseev); | |
122 flags_ ^= ui::EF_RIGHT_MOUSE_BUTTON; | |
123 } | |
124 } | 133 } |
125 | 134 |
126 void EventGenerator::MoveMouseTo(const gfx::Point& point, int count) { | 135 void EventGenerator::MoveMouseTo(const gfx::Point& point, int count) { |
127 DCHECK_GT(count, 0); | 136 DCHECK_GT(count, 0); |
128 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ? | 137 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ? |
129 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; | 138 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; |
| 139 |
130 gfx::Vector2dF diff(point - current_location_); | 140 gfx::Vector2dF diff(point - current_location_); |
131 for (float i = 1; i <= count; i++) { | 141 for (float i = 1; i <= count; i++) { |
132 gfx::Vector2dF step(diff); | 142 gfx::Vector2dF step(diff); |
133 step.Scale(i / count); | 143 step.Scale(i / count); |
134 gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step); | 144 gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step); |
| 145 if (!grab_) |
| 146 UpdateCurrentRootWindow(move_point); |
| 147 ConvertPointToTarget(current_root_window_, &move_point); |
135 ui::MouseEvent mouseev(event_type, move_point, move_point, flags_); | 148 ui::MouseEvent mouseev(event_type, move_point, move_point, flags_); |
136 Dispatch(mouseev); | 149 Dispatch(mouseev); |
137 } | 150 } |
138 current_location_ = point; | 151 current_location_ = point; |
139 } | 152 } |
140 | 153 |
141 void EventGenerator::MoveMouseRelativeTo(const Window* window, | 154 void EventGenerator::MoveMouseRelativeTo(const Window* window, |
142 const gfx::Point& point) { | 155 const gfx::Point& point_in_parent) { |
143 gfx::Point root_point(point); | 156 gfx::Point point(point_in_parent); |
144 Window::ConvertPointToTarget(window, root_window_, &root_point); | 157 ConvertPointFromTarget(window, &point); |
145 | 158 MoveMouseTo(point); |
146 MoveMouseTo(root_point); | |
147 } | 159 } |
148 | 160 |
149 void EventGenerator::DragMouseTo(const gfx::Point& point) { | 161 void EventGenerator::DragMouseTo(const gfx::Point& point) { |
150 PressLeftButton(); | 162 PressLeftButton(); |
151 MoveMouseTo(point); | 163 MoveMouseTo(point); |
152 ReleaseLeftButton(); | 164 ReleaseLeftButton(); |
153 } | 165 } |
154 | 166 |
155 void EventGenerator::MoveMouseToCenterOf(Window* window) { | 167 void EventGenerator::MoveMouseToCenterOf(Window* window) { |
156 MoveMouseTo(CenterOfWindowInRootWindowCoordinate(root_window_, window)); | 168 MoveMouseTo(CenterOfWindow(window)); |
157 } | 169 } |
158 | 170 |
159 void EventGenerator::PressTouch() { | 171 void EventGenerator::PressTouch() { |
160 TestTouchEvent touchev(ui::ET_TOUCH_PRESSED, current_location_, flags_); | 172 TestTouchEvent touchev( |
| 173 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), flags_); |
161 Dispatch(touchev); | 174 Dispatch(touchev); |
162 } | 175 } |
163 | 176 |
164 void EventGenerator::MoveTouch(const gfx::Point& point) { | 177 void EventGenerator::MoveTouch(const gfx::Point& point) { |
165 TestTouchEvent touchev(ui::ET_TOUCH_MOVED, point, flags_); | 178 TestTouchEvent touchev(ui::ET_TOUCH_MOVED, point, flags_); |
166 Dispatch(touchev); | 179 Dispatch(touchev); |
167 | 180 |
168 current_location_ = point; | 181 current_location_ = point; |
| 182 if (!grab_) |
| 183 UpdateCurrentRootWindow(point); |
169 } | 184 } |
170 | 185 |
171 void EventGenerator::ReleaseTouch() { | 186 void EventGenerator::ReleaseTouch() { |
172 TestTouchEvent touchev(ui::ET_TOUCH_RELEASED, current_location_, flags_); | 187 TestTouchEvent touchev( |
| 188 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), flags_); |
173 Dispatch(touchev); | 189 Dispatch(touchev); |
174 } | 190 } |
175 | 191 |
176 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { | 192 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { |
177 PressTouch(); | 193 PressTouch(); |
178 MoveTouch(point); | 194 MoveTouch(point); |
179 ReleaseTouch(); | 195 ReleaseTouch(); |
180 } | 196 } |
181 | 197 |
182 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { | 198 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { |
183 PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(root_window_, | 199 PressMoveAndReleaseTouchTo(CenterOfWindow(window)); |
184 window)); | |
185 } | 200 } |
186 | 201 |
187 void EventGenerator::GestureTapAt(const gfx::Point& location) { | 202 void EventGenerator::GestureTapAt(const gfx::Point& location) { |
188 const int kTouchId = 2; | 203 const int kTouchId = 2; |
189 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, | 204 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, |
190 location, | 205 location, |
191 kTouchId, | 206 kTouchId, |
192 ui::EventTimeForNow()); | 207 ui::EventTimeForNow()); |
193 Dispatch(press); | 208 Dispatch(press); |
194 | 209 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 295 } |
281 | 296 |
282 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { | 297 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { |
283 DispatchKeyEvent(false, key_code, flags); | 298 DispatchKeyEvent(false, key_code, flags); |
284 } | 299 } |
285 | 300 |
286 void EventGenerator::Dispatch(ui::Event& event) { | 301 void EventGenerator::Dispatch(ui::Event& event) { |
287 switch (event.type()) { | 302 switch (event.type()) { |
288 case ui::ET_KEY_PRESSED: | 303 case ui::ET_KEY_PRESSED: |
289 case ui::ET_KEY_RELEASED: | 304 case ui::ET_KEY_RELEASED: |
290 root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent( | 305 current_root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent( |
291 static_cast<ui::KeyEvent*>(&event)); | 306 static_cast<ui::KeyEvent*>(&event)); |
292 break; | 307 break; |
293 case ui::ET_MOUSE_PRESSED: | 308 case ui::ET_MOUSE_PRESSED: |
294 case ui::ET_MOUSE_DRAGGED: | 309 case ui::ET_MOUSE_DRAGGED: |
295 case ui::ET_MOUSE_RELEASED: | 310 case ui::ET_MOUSE_RELEASED: |
296 case ui::ET_MOUSE_MOVED: | 311 case ui::ET_MOUSE_MOVED: |
297 case ui::ET_MOUSE_ENTERED: | 312 case ui::ET_MOUSE_ENTERED: |
298 case ui::ET_MOUSE_EXITED: | 313 case ui::ET_MOUSE_EXITED: |
299 case ui::ET_MOUSEWHEEL: | 314 case ui::ET_MOUSEWHEEL: |
300 root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( | 315 current_root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( |
301 static_cast<ui::MouseEvent*>(&event)); | 316 static_cast<ui::MouseEvent*>(&event)); |
302 break; | 317 break; |
303 case ui::ET_TOUCH_RELEASED: | 318 case ui::ET_TOUCH_RELEASED: |
304 case ui::ET_TOUCH_PRESSED: | 319 case ui::ET_TOUCH_PRESSED: |
305 case ui::ET_TOUCH_MOVED: | 320 case ui::ET_TOUCH_MOVED: |
306 case ui::ET_TOUCH_STATIONARY: | 321 case ui::ET_TOUCH_STATIONARY: |
307 case ui::ET_TOUCH_CANCELLED: | 322 case ui::ET_TOUCH_CANCELLED: |
308 root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( | 323 current_root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( |
309 static_cast<ui::TouchEvent*>(&event)); | 324 static_cast<ui::TouchEvent*>(&event)); |
310 break; | 325 break; |
311 default: | 326 default: |
312 NOTIMPLEMENTED(); | 327 NOTIMPLEMENTED(); |
313 break; | 328 break; |
314 } | 329 } |
315 } | 330 } |
316 | 331 |
317 void EventGenerator::DispatchKeyEvent(bool is_press, | 332 void EventGenerator::DispatchKeyEvent(bool is_press, |
318 ui::KeyboardCode key_code, | 333 ui::KeyboardCode key_code, |
(...skipping 19 matching lines...) Expand all Loading... |
338 scoped_ptr<XEvent> native_event(new XEvent); | 353 scoped_ptr<XEvent> native_event(new XEvent); |
339 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); | 354 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); |
340 TestKeyEvent keyev(native_event.get(), flags, false); | 355 TestKeyEvent keyev(native_event.get(), flags, false); |
341 #else | 356 #else |
342 ui::KeyEvent keyev(type, key_code, flags, false); | 357 ui::KeyEvent keyev(type, key_code, flags, false); |
343 #endif // USE_X11 | 358 #endif // USE_X11 |
344 #endif // OS_WIN | 359 #endif // OS_WIN |
345 Dispatch(keyev); | 360 Dispatch(keyev); |
346 } | 361 } |
347 | 362 |
| 363 void EventGenerator::UpdateCurrentRootWindow(const gfx::Point& point) { |
| 364 current_root_window_ = delegate_->GetRootWindowAt(point); |
| 365 } |
| 366 |
| 367 void EventGenerator::PressButton(int flag) { |
| 368 if (!(flags_ & flag)) { |
| 369 flags_ |= flag; |
| 370 grab_ = flags_ & kAllButtonMask; |
| 371 gfx::Point location = GetLocationInCurrentRoot(); |
| 372 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_); |
| 373 Dispatch(mouseev); |
| 374 } |
| 375 } |
| 376 |
| 377 void EventGenerator::ReleaseButton(int flag) { |
| 378 if (flags_ & flag) { |
| 379 gfx::Point location = GetLocationInCurrentRoot(); |
| 380 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location, |
| 381 location, flags_); |
| 382 Dispatch(mouseev); |
| 383 flags_ ^= flag; |
| 384 } |
| 385 grab_ = flags_ & kAllButtonMask; |
| 386 } |
| 387 |
| 388 void EventGenerator::ConvertPointFromTarget(const aura::Window* target, |
| 389 gfx::Point* point) const { |
| 390 DCHECK(point); |
| 391 aura::client::ScreenPositionClient* client = |
| 392 delegate_->GetScreenPositionClient(target); |
| 393 if (client) |
| 394 client->ConvertPointToScreen(target, point); |
| 395 else |
| 396 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point); |
| 397 } |
| 398 |
| 399 void EventGenerator::ConvertPointToTarget(const aura::Window* target, |
| 400 gfx::Point* point) const { |
| 401 DCHECK(point); |
| 402 aura::client::ScreenPositionClient* client = |
| 403 delegate_->GetScreenPositionClient(target); |
| 404 if (client) |
| 405 client->ConvertPointFromScreen(target, point); |
| 406 else |
| 407 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point); |
| 408 } |
| 409 |
| 410 gfx::Point EventGenerator::GetLocationInCurrentRoot() const { |
| 411 gfx::Point p(current_location_); |
| 412 ConvertPointToTarget(current_root_window_, &p); |
| 413 return p; |
| 414 } |
| 415 |
| 416 gfx::Point EventGenerator::CenterOfWindow(const Window* window) const { |
| 417 gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint(); |
| 418 ConvertPointFromTarget(window, ¢er); |
| 419 return center; |
| 420 } |
| 421 |
348 } // namespace test | 422 } // namespace test |
349 } // namespace aura | 423 } // namespace aura |
OLD | NEW |