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/root_window.h" | 8 #include "ui/aura/root_window.h" |
9 #include "ui/base/event.h" | 9 #include "ui/base/event.h" |
10 | 10 |
11 #if defined(USE_X11) | 11 #if defined(USE_X11) |
12 #include <X11/Xlib.h> | 12 #include <X11/Xlib.h> |
13 #include "ui/base/x/x11_util.h" | 13 #include "ui/base/x/x11_util.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class TestKeyEvent : public ui::KeyEvent { | 18 class TestKeyEvent : public ui::KeyEvent { |
19 public: | 19 public: |
20 TestKeyEvent(const base::NativeEvent& native_event, int flags) | 20 TestKeyEvent(const base::NativeEvent& native_event, int flags) |
21 : KeyEvent(native_event, false /* is_char */) { | 21 : KeyEvent(native_event, false /* is_char */) { |
22 set_flags(flags); | 22 set_flags(flags); |
23 } | 23 } |
24 }; | 24 }; |
25 | 25 |
26 class TestTouchEvent : public ui::TouchEventImpl { | 26 class TestTouchEvent : public ui::TouchEvent { |
27 public: | 27 public: |
28 TestTouchEvent(ui::EventType type, | 28 TestTouchEvent(ui::EventType type, |
29 const gfx::Point& root_location, | 29 const gfx::Point& root_location, |
30 int flags) | 30 int flags) |
31 : TouchEventImpl(type, root_location, 0, | 31 : TouchEvent(type, root_location, 0, |
32 base::Time::NowFromSystemTime() - base::Time()) { | 32 base::Time::NowFromSystemTime() - base::Time()) { |
33 set_flags(flags); | 33 set_flags(flags); |
34 } | 34 } |
35 | 35 |
36 private: | 36 private: |
37 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); | 37 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); |
38 }; | 38 }; |
39 | 39 |
40 gfx::Point CenterOfWindowInRootWindowCoordinate(aura::RootWindow* root_window, | 40 gfx::Point CenterOfWindowInRootWindowCoordinate(aura::RootWindow* root_window, |
41 aura::Window* window) { | 41 aura::Window* window) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 ReleaseTouch(); | 155 ReleaseTouch(); |
156 } | 156 } |
157 | 157 |
158 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { | 158 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { |
159 PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(root_window_, | 159 PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(root_window_, |
160 window)); | 160 window)); |
161 } | 161 } |
162 | 162 |
163 void EventGenerator::GestureTapAt(const gfx::Point& location) { | 163 void EventGenerator::GestureTapAt(const gfx::Point& location) { |
164 const int kTouchId = 2; | 164 const int kTouchId = 2; |
165 ui::TouchEventImpl press(ui::ET_TOUCH_PRESSED, location, kTouchId, | 165 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, location, kTouchId, |
166 base::Time::NowFromSystemTime() - base::Time()); | 166 base::Time::NowFromSystemTime() - base::Time()); |
167 Dispatch(press); | 167 Dispatch(press); |
168 | 168 |
169 ui::TouchEventImpl release( | 169 ui::TouchEvent release( |
170 ui::ET_TOUCH_RELEASED, location, kTouchId, | 170 ui::ET_TOUCH_RELEASED, location, kTouchId, |
171 press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); | 171 press.time_stamp() + base::TimeDelta::FromMilliseconds(50)); |
172 Dispatch(release); | 172 Dispatch(release); |
173 } | 173 } |
174 | 174 |
175 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { | 175 void EventGenerator::GestureTapDownAndUp(const gfx::Point& location) { |
176 const int kTouchId = 3; | 176 const int kTouchId = 3; |
177 ui::TouchEventImpl press( | 177 ui::TouchEvent press( |
178 ui::ET_TOUCH_PRESSED, location, kTouchId, | 178 ui::ET_TOUCH_PRESSED, location, kTouchId, |
179 base::Time::NowFromSystemTime() - base::Time()); | 179 base::Time::NowFromSystemTime() - base::Time()); |
180 Dispatch(press); | 180 Dispatch(press); |
181 | 181 |
182 ui::TouchEventImpl release( | 182 ui::TouchEvent release( |
183 ui::ET_TOUCH_RELEASED, location, kTouchId, | 183 ui::ET_TOUCH_RELEASED, location, kTouchId, |
184 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); | 184 press.time_stamp() + base::TimeDelta::FromMilliseconds(1000)); |
185 Dispatch(release); | 185 Dispatch(release); |
186 } | 186 } |
187 | 187 |
188 void EventGenerator::GestureScrollSequence(const gfx::Point& start, | 188 void EventGenerator::GestureScrollSequence(const gfx::Point& start, |
189 const gfx::Point& end, | 189 const gfx::Point& end, |
190 const base::TimeDelta& step_delay, | 190 const base::TimeDelta& step_delay, |
191 int steps) { | 191 int steps) { |
192 const int kTouchId = 5; | 192 const int kTouchId = 5; |
193 base::TimeDelta timestamp = base::Time::NowFromSystemTime() - base::Time(); | 193 base::TimeDelta timestamp = base::Time::NowFromSystemTime() - base::Time(); |
194 ui::TouchEventImpl press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); | 194 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, kTouchId, timestamp); |
195 Dispatch(press); | 195 Dispatch(press); |
196 | 196 |
197 int dx = (end.x() - start.x()) / steps; | 197 int dx = (end.x() - start.x()) / steps; |
198 int dy = (end.y() - start.y()) / steps; | 198 int dy = (end.y() - start.y()) / steps; |
199 gfx::Point location = start; | 199 gfx::Point location = start; |
200 for (int i = 0; i < steps; ++i) { | 200 for (int i = 0; i < steps; ++i) { |
201 location.Offset(dx, dy); | 201 location.Offset(dx, dy); |
202 timestamp += step_delay; | 202 timestamp += step_delay; |
203 ui::TouchEventImpl move(ui::ET_TOUCH_MOVED, location, kTouchId, timestamp); | 203 ui::TouchEvent move(ui::ET_TOUCH_MOVED, location, kTouchId, timestamp); |
204 Dispatch(move); | 204 Dispatch(move); |
205 } | 205 } |
206 | 206 |
207 ui::TouchEventImpl release(ui::ET_TOUCH_RELEASED, end, kTouchId, timestamp); | 207 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, end, kTouchId, timestamp); |
208 Dispatch(release); | 208 Dispatch(release); |
209 } | 209 } |
210 | 210 |
211 void EventGenerator::GestureMultiFingerScroll(int count, | 211 void EventGenerator::GestureMultiFingerScroll(int count, |
212 const gfx::Point* start, | 212 const gfx::Point* start, |
213 int event_separation_time_ms, | 213 int event_separation_time_ms, |
214 int steps, | 214 int steps, |
215 int move_x, | 215 int move_x, |
216 int move_y) { | 216 int move_y) { |
217 const int kMaxTouchPoints = 10; | 217 const int kMaxTouchPoints = 10; |
218 gfx::Point points[kMaxTouchPoints]; | 218 gfx::Point points[kMaxTouchPoints]; |
219 CHECK_LE(count, kMaxTouchPoints); | 219 CHECK_LE(count, kMaxTouchPoints); |
220 CHECK_GT(steps, 0); | 220 CHECK_GT(steps, 0); |
221 | 221 |
222 int delta_x = move_x / steps; | 222 int delta_x = move_x / steps; |
223 int delta_y = move_y / steps; | 223 int delta_y = move_y / steps; |
224 | 224 |
225 base::TimeDelta press_time = base::Time::NowFromSystemTime() - base::Time(); | 225 base::TimeDelta press_time = base::Time::NowFromSystemTime() - base::Time(); |
226 for (int i = 0; i < count; ++i) { | 226 for (int i = 0; i < count; ++i) { |
227 points[i] = start[i]; | 227 points[i] = start[i]; |
228 ui::TouchEventImpl press(ui::ET_TOUCH_PRESSED, points[i], i, press_time); | 228 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, points[i], i, press_time); |
229 Dispatch(press); | 229 Dispatch(press); |
230 } | 230 } |
231 | 231 |
232 for (int step = 0; step < steps; ++step) { | 232 for (int step = 0; step < steps; ++step) { |
233 base::TimeDelta move_time = press_time + | 233 base::TimeDelta move_time = press_time + |
234 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step); | 234 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step); |
235 for (int i = 0; i < count; ++i) { | 235 for (int i = 0; i < count; ++i) { |
236 points[i].Offset(delta_x, delta_y); | 236 points[i].Offset(delta_x, delta_y); |
237 ui::TouchEventImpl move(ui::ET_TOUCH_MOVED, points[i], i, move_time); | 237 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time); |
238 Dispatch(move); | 238 Dispatch(move); |
239 } | 239 } |
240 } | 240 } |
241 | 241 |
242 base::TimeDelta release_time = press_time + | 242 base::TimeDelta release_time = press_time + |
243 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps); | 243 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps); |
244 for (int i = 0; i < count; ++i) { | 244 for (int i = 0; i < count; ++i) { |
245 ui::TouchEventImpl release( | 245 ui::TouchEvent release( |
246 ui::ET_TOUCH_RELEASED, points[i], i, release_time); | 246 ui::ET_TOUCH_RELEASED, points[i], i, release_time); |
247 Dispatch(release); | 247 Dispatch(release); |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { | 251 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { |
252 DispatchKeyEvent(true, key_code, flags); | 252 DispatchKeyEvent(true, key_code, flags); |
253 } | 253 } |
254 | 254 |
255 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { | 255 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { |
(...skipping 16 matching lines...) Expand all Loading... |
272 case ui::ET_MOUSEWHEEL: | 272 case ui::ET_MOUSEWHEEL: |
273 root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( | 273 root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent( |
274 static_cast<ui::MouseEvent*>(&event)); | 274 static_cast<ui::MouseEvent*>(&event)); |
275 break; | 275 break; |
276 case ui::ET_TOUCH_RELEASED: | 276 case ui::ET_TOUCH_RELEASED: |
277 case ui::ET_TOUCH_PRESSED: | 277 case ui::ET_TOUCH_PRESSED: |
278 case ui::ET_TOUCH_MOVED: | 278 case ui::ET_TOUCH_MOVED: |
279 case ui::ET_TOUCH_STATIONARY: | 279 case ui::ET_TOUCH_STATIONARY: |
280 case ui::ET_TOUCH_CANCELLED: | 280 case ui::ET_TOUCH_CANCELLED: |
281 root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( | 281 root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent( |
282 static_cast<ui::TouchEventImpl*>(&event)); | 282 static_cast<ui::TouchEvent*>(&event)); |
283 break; | 283 break; |
284 default: | 284 default: |
285 NOTIMPLEMENTED(); | 285 NOTIMPLEMENTED(); |
286 break; | 286 break; |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 void EventGenerator::DispatchKeyEvent(bool is_press, | 290 void EventGenerator::DispatchKeyEvent(bool is_press, |
291 ui::KeyboardCode key_code, | 291 ui::KeyboardCode key_code, |
292 int flags) { | 292 int flags) { |
293 #if defined(OS_WIN) | 293 #if defined(OS_WIN) |
294 MSG native_event = | 294 MSG native_event = |
295 { NULL, (is_press ? WM_KEYDOWN : WM_KEYUP), key_code, 0 }; | 295 { NULL, (is_press ? WM_KEYDOWN : WM_KEYUP), key_code, 0 }; |
296 TestKeyEvent keyev(native_event, flags); | 296 TestKeyEvent keyev(native_event, flags); |
297 #else | 297 #else |
298 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; | 298 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; |
299 #if defined(USE_X11) | 299 #if defined(USE_X11) |
300 scoped_ptr<XEvent> native_event(new XEvent); | 300 scoped_ptr<XEvent> native_event(new XEvent); |
301 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); | 301 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); |
302 TestKeyEvent keyev(native_event.get(), flags); | 302 TestKeyEvent keyev(native_event.get(), flags); |
303 #else | 303 #else |
304 ui::KeyEvent keyev(type, key_code, flags); | 304 ui::KeyEvent keyev(type, key_code, flags); |
305 #endif // USE_X11 | 305 #endif // USE_X11 |
306 #endif // OS_WIN | 306 #endif // OS_WIN |
307 Dispatch(keyev); | 307 Dispatch(keyev); |
308 } | 308 } |
309 | 309 |
310 } // namespace test | 310 } // namespace test |
311 } // namespace aura | 311 } // namespace aura |
OLD | NEW |