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 <cstring> | 5 #include <cstring> |
6 | 6 |
7 #include <X11/extensions/XInput2.h> | 7 #include <X11/extensions/XInput2.h> |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
| 9 #include <X11/Xutil.h> |
| 10 #include <X11/XKBlib.h> |
9 | 11 |
10 // Generically-named #defines from Xlib that conflict with symbols in GTest. | 12 // Generically-named #defines from Xlib that conflict with symbols in GTest. |
11 #undef Bool | 13 #undef Bool |
12 #undef None | 14 #undef None |
13 | 15 |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
16 #include "ui/events/event_constants.h" | 18 #include "ui/events/event_constants.h" |
17 #include "ui/events/event_utils.h" | 19 #include "ui/events/event_utils.h" |
18 #include "ui/events/test/events_test_utils_x11.h" | 20 #include "ui/events/test/events_test_utils_x11.h" |
(...skipping 14 matching lines...) Expand all Loading... |
33 // We don't bother setting fields that the event code doesn't use, such as | 35 // We don't bother setting fields that the event code doesn't use, such as |
34 // x_root/y_root and window/root/subwindow. | 36 // x_root/y_root and window/root/subwindow. |
35 XButtonEvent* button_event = &(event->xbutton); | 37 XButtonEvent* button_event = &(event->xbutton); |
36 button_event->type = is_press ? ButtonPress : ButtonRelease; | 38 button_event->type = is_press ? ButtonPress : ButtonRelease; |
37 button_event->x = location.x(); | 39 button_event->x = location.x(); |
38 button_event->y = location.y(); | 40 button_event->y = location.y(); |
39 button_event->button = button; | 41 button_event->button = button; |
40 button_event->state = state; | 42 button_event->state = state; |
41 } | 43 } |
42 | 44 |
| 45 // Initializes the passed-in Xlib event. |
| 46 void InitKeyEvent(Display* display, |
| 47 XEvent* event, |
| 48 bool is_press, |
| 49 int keycode, |
| 50 int state) { |
| 51 memset(event, 0, sizeof(*event)); |
| 52 |
| 53 // We don't bother setting fields that the event code doesn't use, such as |
| 54 // x_root/y_root and window/root/subwindow. |
| 55 XKeyEvent* key_event = &(event->xkey); |
| 56 key_event->display = display; |
| 57 key_event->type = is_press ? KeyPress : KeyRelease; |
| 58 key_event->keycode = keycode; |
| 59 key_event->state = state; |
| 60 } |
| 61 |
43 } // namespace | 62 } // namespace |
44 | 63 |
45 TEST(EventsXTest, ButtonEvents) { | 64 TEST(EventsXTest, ButtonEvents) { |
46 XEvent event; | 65 XEvent event; |
47 gfx::Point location(5, 10); | 66 gfx::Point location(5, 10); |
48 gfx::Vector2d offset; | 67 gfx::Vector2d offset; |
49 | 68 |
50 InitButtonEvent(&event, true, location, 1, 0); | 69 InitButtonEvent(&event, true, location, 1, 0); |
51 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); | 70 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); |
52 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); | 71 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 scoped_xevent.InitTouchEvent( | 250 scoped_xevent.InitTouchEvent( |
232 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators); | 251 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators); |
233 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); | 252 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); |
234 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); | 253 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); |
235 EXPECT_EQ(GetTouchId(scoped_xevent), 1); | 254 EXPECT_EQ(GetTouchId(scoped_xevent), 1); |
236 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25); | 255 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25); |
237 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); | 256 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); |
238 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); | 257 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); |
239 } | 258 } |
240 #endif | 259 #endif |
| 260 |
| 261 TEST(EventsXTest, NumpadKeyEvents) { |
| 262 XEvent event; |
| 263 Display* display = gfx::GetXDisplay(); |
| 264 |
| 265 struct { |
| 266 bool is_numpad_key; |
| 267 int x_keysym; |
| 268 ui::KeyboardCode ui_keycode; |
| 269 } keys[] = { |
| 270 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional |
| 271 // keysymdef.h numbering. |
| 272 { true, XK_KP_Space }, |
| 273 { true, XK_KP_Equal }, |
| 274 // Other numpad keysyms. (This is actually exhaustive in the current list.) |
| 275 { true, XK_KP_Tab }, |
| 276 { true, XK_KP_Enter }, |
| 277 { true, XK_KP_F1 }, |
| 278 { true, XK_KP_F2 }, |
| 279 { true, XK_KP_F3 }, |
| 280 { true, XK_KP_F4 }, |
| 281 { true, XK_KP_Home }, |
| 282 { true, XK_KP_Left }, |
| 283 { true, XK_KP_Up }, |
| 284 { true, XK_KP_Right }, |
| 285 { true, XK_KP_Down }, |
| 286 { true, XK_KP_Prior }, |
| 287 { true, XK_KP_Page_Up }, |
| 288 { true, XK_KP_Next }, |
| 289 { true, XK_KP_Page_Down }, |
| 290 { true, XK_KP_End }, |
| 291 { true, XK_KP_Begin }, |
| 292 { true, XK_KP_Insert }, |
| 293 { true, XK_KP_Delete }, |
| 294 { true, XK_KP_Multiply }, |
| 295 { true, XK_KP_Add }, |
| 296 { true, XK_KP_Separator }, |
| 297 { true, XK_KP_Subtract }, |
| 298 { true, XK_KP_Decimal }, |
| 299 { true, XK_KP_Divide }, |
| 300 { true, XK_KP_0 }, |
| 301 { true, XK_KP_1 }, |
| 302 { true, XK_KP_2 }, |
| 303 { true, XK_KP_3 }, |
| 304 { true, XK_KP_4 }, |
| 305 { true, XK_KP_5 }, |
| 306 { true, XK_KP_6 }, |
| 307 { true, XK_KP_7 }, |
| 308 { true, XK_KP_8 }, |
| 309 { true, XK_KP_9 }, |
| 310 // Largest keysym preceding XK_KP_Space. |
| 311 { false, XK_Num_Lock }, |
| 312 // Smallest keysym following XK_KP_Equal. |
| 313 { false, XK_F1 }, |
| 314 // Non-numpad analogues of numpad keysyms. |
| 315 { false, XK_Tab }, |
| 316 { false, XK_Return }, |
| 317 { false, XK_F1 }, |
| 318 { false, XK_F2 }, |
| 319 { false, XK_F3 }, |
| 320 { false, XK_F4 }, |
| 321 { false, XK_Home }, |
| 322 { false, XK_Left }, |
| 323 { false, XK_Up }, |
| 324 { false, XK_Right }, |
| 325 { false, XK_Down }, |
| 326 { false, XK_Prior }, |
| 327 { false, XK_Page_Up }, |
| 328 { false, XK_Next }, |
| 329 { false, XK_Page_Down }, |
| 330 { false, XK_End }, |
| 331 { false, XK_Insert }, |
| 332 { false, XK_Delete }, |
| 333 { false, XK_multiply }, |
| 334 { false, XK_plus }, |
| 335 { false, XK_minus }, |
| 336 { false, XK_period }, |
| 337 { false, XK_slash }, |
| 338 { false, XK_0 }, |
| 339 { false, XK_1 }, |
| 340 { false, XK_2 }, |
| 341 { false, XK_3 }, |
| 342 { false, XK_4 }, |
| 343 { false, XK_5 }, |
| 344 { false, XK_6 }, |
| 345 { false, XK_7 }, |
| 346 { false, XK_8 }, |
| 347 { false, XK_9 }, |
| 348 // Miscellaneous other keysyms. |
| 349 { false, XK_BackSpace }, |
| 350 { false, XK_Scroll_Lock }, |
| 351 { false, XK_Multi_key }, |
| 352 { false, XK_Select }, |
| 353 { false, XK_Num_Lock }, |
| 354 { false, XK_Shift_L }, |
| 355 { false, XK_space }, |
| 356 { false, XK_A }, |
| 357 }; |
| 358 |
| 359 for (size_t k = 0; k < ARRAYSIZE_UNSAFE(keys); ++k) { |
| 360 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym); |
| 361 // Exclude keysyms for which the server has no corresponding keycode. |
| 362 if (x_keycode) { |
| 363 InitKeyEvent(display, &event, true, x_keycode, 0); |
| 364 // int keysym = XLookupKeysym(&event.xkey, 0); |
| 365 // if (keysym) { |
| 366 ui::KeyEvent ui_key_event(&event, false); |
| 367 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0, |
| 368 ui_key_event.flags() & ui::EF_NUMPAD_KEY); |
| 369 } |
| 370 } |
| 371 } |
| 372 |
241 } // namespace ui | 373 } // namespace ui |
OLD | NEW |