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 3// found in the LICENSE file. 4 5#include <cstring> 6#include <set> 7 8#include <X11/extensions/XInput2.h> 9#include <X11/Xlib.h> 10#include <X11/Xutil.h> 11#include <X11/XKBlib.h> 12 13// Generically-named #defines from Xlib that conflict with symbols in GTest. 14#undef Bool 15#undef None 16 17#include "base/memory/scoped_ptr.h" 18#include "testing/gtest/include/gtest/gtest.h" 19#include "ui/events/event.h" 20#include "ui/events/event_constants.h" 21#include "ui/events/event_utils.h" 22#include "ui/events/test/events_test_utils.h" 23#include "ui/events/test/events_test_utils_x11.h" 24#include "ui/events/x/device_data_manager_x11.h" 25#include "ui/events/x/touch_factory_x11.h" 26#include "ui/gfx/point.h" 27 28namespace ui { 29 30namespace { 31 32// Initializes the passed-in Xlib event. 33void InitButtonEvent(XEvent* event, 34 bool is_press, 35 const gfx::Point& location, 36 int button, 37 int state) { 38 memset(event, 0, sizeof(*event)); 39 40 // We don't bother setting fields that the event code doesn't use, such as 41 // x_root/y_root and window/root/subwindow. 42 XButtonEvent* button_event = &(event->xbutton); 43 button_event->type = is_press ? ButtonPress : ButtonRelease; 44 button_event->x = location.x(); 45 button_event->y = location.y(); 46 button_event->button = button; 47 button_event->state = state; 48} 49 50// Initializes the passed-in Xlib event. 51void InitKeyEvent(Display* display, 52 XEvent* event, 53 bool is_press, 54 int keycode, 55 int state) { 56 memset(event, 0, sizeof(*event)); 57 58 // We don't bother setting fields that the event code doesn't use, such as 59 // x_root/y_root and window/root/subwindow. 60 XKeyEvent* key_event = &(event->xkey); 61 key_event->display = display; 62 key_event->type = is_press ? KeyPress : KeyRelease; 63 key_event->keycode = keycode; 64 key_event->state = state; 65} 66 67// Returns true if the keysym maps to a KeyEvent with the EF_FUNCTION_KEY 68// flag set, or the keysym maps to a zero key code. 69bool HasFunctionKeyFlagSetIfSupported(Display* display, int x_keysym) { 70 XEvent event; 71 int x_keycode = XKeysymToKeycode(display, x_keysym); 72 // Exclude keysyms for which the server has no corresponding keycode. 73 if (x_keycode) { 74 InitKeyEvent(display, &event, true, x_keycode, 0); 75 ui::KeyEvent ui_key_event(&event); 76 return (ui_key_event.flags() & ui::EF_FUNCTION_KEY); 77 } 78 return true; 79} 80 81} // namespace 82 83class EventsXTest : public testing::Test { 84 public: 85 EventsXTest() {} 86 virtual ~EventsXTest() {} 87 88 virtual void SetUp() OVERRIDE { 89 DeviceDataManagerX11::CreateInstance(); 90 ui::TouchFactory::GetInstance()->ResetForTest(); 91 } 92 private: 93 DISALLOW_COPY_AND_ASSIGN(EventsXTest); 94}; 95 96TEST_F(EventsXTest, ButtonEvents) { 97 XEvent event; 98 gfx::Point location(5, 10); 99 gfx::Vector2d offset; 100 101 InitButtonEvent(&event, true, location, 1, 0); 102 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); 103 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); 104 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 105 106 InitButtonEvent(&event, true, location, 2, Button1Mask | ShiftMask); 107 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); 108 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON | 109 ui::EF_SHIFT_DOWN, 110 ui::EventFlagsFromNative(&event)); 111 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 112 113 InitButtonEvent(&event, false, location, 3, 0); 114 EXPECT_EQ(ui::ET_MOUSE_RELEASED, ui::EventTypeFromNative(&event)); 115 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); 116 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 117 118 // Scroll up. 119 InitButtonEvent(&event, true, location, 4, 0); 120 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 121 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); 122 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 123 offset = ui::GetMouseWheelOffset(&event); 124 EXPECT_GT(offset.y(), 0); 125 EXPECT_EQ(0, offset.x()); 126 127 // Scroll down. 128 InitButtonEvent(&event, true, location, 5, 0); 129 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 130 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); 131 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 132 offset = ui::GetMouseWheelOffset(&event); 133 EXPECT_LT(offset.y(), 0); 134 EXPECT_EQ(0, offset.x()); 135 136 // Scroll left. 137 InitButtonEvent(&event, true, location, 6, 0); 138 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 139 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); 140 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 141 offset = ui::GetMouseWheelOffset(&event); 142 EXPECT_EQ(0, offset.y()); 143 EXPECT_GT(offset.x(), 0); 144 145 // Scroll right. 146 InitButtonEvent(&event, true, location, 7, 0); 147 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 148 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); 149 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); 150 offset = ui::GetMouseWheelOffset(&event); 151 EXPECT_EQ(0, offset.y()); 152 EXPECT_LT(offset.x(), 0); 153 154 // TODO(derat): Test XInput code. 155} 156 157TEST_F(EventsXTest, AvoidExtraEventsOnWheelRelease) { 158 XEvent event; 159 gfx::Point location(5, 10); 160 161 InitButtonEvent(&event, true, location, 4, 0); 162 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); 163 164 // We should return ET_UNKNOWN for the release event instead of returning 165 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step. 166 InitButtonEvent(&event, false, location, 4, 0); 167 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(&event)); 168 169 // TODO(derat): Test XInput code. 170} 171 172TEST_F(EventsXTest, EnterLeaveEvent) { 173 XEvent event; 174 event.xcrossing.type = EnterNotify; 175 event.xcrossing.x = 10; 176 event.xcrossing.y = 20; 177 event.xcrossing.x_root = 110; 178 event.xcrossing.y_root = 120; 179 180 // Mouse enter events are converted to mouse move events to be consistent with 181 // the way views handle mouse enter. See comments for EnterNotify case in 182 // ui::EventTypeFromNative for more details. 183 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(&event)); 184 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event).ToString()); 185 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event).ToString()); 186 187 event.xcrossing.type = LeaveNotify; 188 event.xcrossing.x = 30; 189 event.xcrossing.y = 40; 190 event.xcrossing.x_root = 230; 191 event.xcrossing.y_root = 240; 192 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(&event)); 193 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event).ToString()); 194 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event).ToString()); 195} 196 197TEST_F(EventsXTest, ClickCount) { 198 XEvent event; 199 gfx::Point location(5, 10); 200 201 for (int i = 1; i <= 3; ++i) { 202 InitButtonEvent(&event, true, location, 1, 0); 203 { 204 MouseEvent mouseev(&event); 205 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type()); 206 EXPECT_EQ(i, mouseev.GetClickCount()); 207 } 208 209 InitButtonEvent(&event, false, location, 1, 0); 210 { 211 MouseEvent mouseev(&event); 212 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type()); 213 EXPECT_EQ(i, mouseev.GetClickCount()); 214 } 215 } 216} 217 218#if defined(USE_XI2_MT) 219TEST_F(EventsXTest, TouchEventBasic) { 220 std::vector<unsigned int> devices; 221 devices.push_back(0); 222 ui::SetUpTouchDevicesForTest(devices); 223 std::vector<Valuator> valuators; 224 225 // Init touch begin with tracking id 5, touch id 0. 226 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 20)); 227 valuators.push_back( 228 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.3f)); 229 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 100)); 230 ui::ScopedXI2Event scoped_xevent; 231 scoped_xevent.InitTouchEvent( 232 0, XI_TouchBegin, 5, gfx::Point(10, 10), valuators); 233 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); 234 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString()); 235 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 236 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 237 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f); 238 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); 239 240 // Touch update, with new orientation info. 241 valuators.clear(); 242 valuators.push_back( 243 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.5f)); 244 scoped_xevent.InitTouchEvent( 245 0, XI_TouchUpdate, 5, gfx::Point(20, 20), valuators); 246 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent)); 247 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString()); 248 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 249 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 250 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); 251 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); 252 253 // Another touch with tracking id 6, touch id 1. 254 valuators.clear(); 255 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 100)); 256 valuators.push_back(Valuator( 257 DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.9f)); 258 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 500)); 259 scoped_xevent.InitTouchEvent( 260 0, XI_TouchBegin, 6, gfx::Point(200, 200), valuators); 261 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); 262 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); 263 EXPECT_EQ(GetTouchId(scoped_xevent), 1); 264 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50); 265 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); 266 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); 267 268 // Touch with tracking id 5 should have old radius/angle value and new pressue 269 // value. 270 valuators.clear(); 271 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 50)); 272 scoped_xevent.InitTouchEvent( 273 0, XI_TouchEnd, 5, gfx::Point(30, 30), valuators); 274 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); 275 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString()); 276 EXPECT_EQ(GetTouchId(scoped_xevent), 0); 277 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); 278 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); 279 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f); 280 281 // Touch with tracking id 6 should have old angle/pressure value and new 282 // radius value. 283 valuators.clear(); 284 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 50)); 285 scoped_xevent.InitTouchEvent( 286 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators); 287 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); 288 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); 289 EXPECT_EQ(GetTouchId(scoped_xevent), 1); 290 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25); 291 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); 292 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); 293} 294 295int GetTouchIdForTrackingId(uint32 tracking_id) { 296 int slot = 0; 297 bool success = 298 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id, &slot); 299 if (success) 300 return slot; 301 return -1; 302} 303 304TEST_F(EventsXTest, TouchEventIdRefcounting) { 305 std::vector<unsigned int> devices; 306 devices.push_back(0); 307 ui::SetUpTouchDevicesForTest(devices); 308 std::vector<Valuator> valuators; 309 310 const int kTrackingId0 = 5; 311 const int kTrackingId1 = 7; 312 313 // Increment ref count once for first touch. 314 ui::ScopedXI2Event xpress0; 315 xpress0.InitTouchEvent( 316 0, XI_TouchBegin, kTrackingId0, gfx::Point(10, 10), valuators); 317 scoped_ptr<ui::TouchEvent> upress0(new ui::TouchEvent(xpress0)); 318 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId0)); 319 320 // Increment ref count 4 times for second touch. 321 ui::ScopedXI2Event xpress1; 322 xpress1.InitTouchEvent( 323 0, XI_TouchBegin, kTrackingId1, gfx::Point(20, 20), valuators); 324 325 for (int i = 0; i < 4; ++i) { 326 ui::TouchEvent upress1(xpress1); 327 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1)); 328 } 329 330 ui::ScopedXI2Event xrelease1; 331 xrelease1.InitTouchEvent( 332 0, XI_TouchEnd, kTrackingId1, gfx::Point(10, 10), valuators); 333 334 // Decrement ref count 3 times for second touch. 335 for (int i = 0; i < 3; ++i) { 336 ui::TouchEvent urelease1(xrelease1); 337 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1)); 338 } 339 340 // This should clear the touch id of the second touch. 341 scoped_ptr<ui::TouchEvent> urelease1(new ui::TouchEvent(xrelease1)); 342 urelease1.reset(); 343 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId1)); 344 345 // This should clear the touch id of the first touch. 346 ui::ScopedXI2Event xrelease0; 347 xrelease0.InitTouchEvent( 348 0, XI_TouchEnd, kTrackingId0, gfx::Point(10, 10), valuators); 349 scoped_ptr<ui::TouchEvent> urelease0(new ui::TouchEvent(xrelease0)); 350 urelease0.reset(); 351 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId0)); 352} 353#endif 354 355TEST_F(EventsXTest, NumpadKeyEvents) { 356 XEvent event; 357 Display* display = gfx::GetXDisplay(); 358 359 struct { 360 bool is_numpad_key; 361 int x_keysym; 362 } keys[] = { 363 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional 364 // keysymdef.h numbering. 365 { true, XK_KP_Space }, 366 { true, XK_KP_Equal }, 367 // Other numpad keysyms. (This is actually exhaustive in the current list.) 368 { true, XK_KP_Tab }, 369 { true, XK_KP_Enter }, 370 { true, XK_KP_F1 }, 371 { true, XK_KP_F2 }, 372 { true, XK_KP_F3 }, 373 { true, XK_KP_F4 }, 374 { true, XK_KP_Home }, 375 { true, XK_KP_Left }, 376 { true, XK_KP_Up }, 377 { true, XK_KP_Right }, 378 { true, XK_KP_Down }, 379 { true, XK_KP_Prior }, 380 { true, XK_KP_Page_Up }, 381 { true, XK_KP_Next }, 382 { true, XK_KP_Page_Down }, 383 { true, XK_KP_End }, 384 { true, XK_KP_Begin }, 385 { true, XK_KP_Insert }, 386 { true, XK_KP_Delete }, 387 { true, XK_KP_Multiply }, 388 { true, XK_KP_Add }, 389 { true, XK_KP_Separator }, 390 { true, XK_KP_Subtract }, 391 { true, XK_KP_Decimal }, 392 { true, XK_KP_Divide }, 393 { true, XK_KP_0 }, 394 { true, XK_KP_1 }, 395 { true, XK_KP_2 }, 396 { true, XK_KP_3 }, 397 { true, XK_KP_4 }, 398 { true, XK_KP_5 }, 399 { true, XK_KP_6 }, 400 { true, XK_KP_7 }, 401 { true, XK_KP_8 }, 402 { true, XK_KP_9 }, 403 // Largest keysym preceding XK_KP_Space. 404 { false, XK_Num_Lock }, 405 // Smallest keysym following XK_KP_Equal. 406 { false, XK_F1 }, 407 // Non-numpad analogues of numpad keysyms. 408 { false, XK_Tab }, 409 { false, XK_Return }, 410 { false, XK_F1 }, 411 { false, XK_F2 }, 412 { false, XK_F3 }, 413 { false, XK_F4 }, 414 { false, XK_Home }, 415 { false, XK_Left }, 416 { false, XK_Up }, 417 { false, XK_Right }, 418 { false, XK_Down }, 419 { false, XK_Prior }, 420 { false, XK_Page_Up }, 421 { false, XK_Next }, 422 { false, XK_Page_Down }, 423 { false, XK_End }, 424 { false, XK_Insert }, 425 { false, XK_Delete }, 426 { false, XK_multiply }, 427 { false, XK_plus }, 428 { false, XK_minus }, 429 { false, XK_period }, 430 { false, XK_slash }, 431 { false, XK_0 }, 432 { false, XK_1 }, 433 { false, XK_2 }, 434 { false, XK_3 }, 435 { false, XK_4 }, 436 { false, XK_5 }, 437 { false, XK_6 }, 438 { false, XK_7 }, 439 { false, XK_8 }, 440 { false, XK_9 }, 441 // Miscellaneous other keysyms. 442 { false, XK_BackSpace }, 443 { false, XK_Scroll_Lock }, 444 { false, XK_Multi_key }, 445 { false, XK_Select }, 446 { false, XK_Num_Lock }, 447 { false, XK_Shift_L }, 448 { false, XK_space }, 449 { false, XK_A }, 450 }; 451 452 for (size_t k = 0; k < ARRAYSIZE_UNSAFE(keys); ++k) { 453 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym); 454 // Exclude keysyms for which the server has no corresponding keycode. 455 if (x_keycode) { 456 InitKeyEvent(display, &event, true, x_keycode, 0); 457 // int keysym = XLookupKeysym(&event.xkey, 0); 458 // if (keysym) { 459 ui::KeyEvent ui_key_event(&event); 460 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0, 461 ui_key_event.flags() & ui::EF_NUMPAD_KEY); 462 } 463 } 464} 465 466TEST_F(EventsXTest, FunctionKeyEvents) { 467 Display* display = gfx::GetXDisplay(); 468 469 // Min function key code minus 1. 470 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F1 - 1)); 471 // All function keys. 472 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F1)); 473 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F2)); 474 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F3)); 475 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F4)); 476 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F5)); 477 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F6)); 478 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F7)); 479 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F8)); 480 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F9)); 481 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F10)); 482 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F11)); 483 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F12)); 484 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F13)); 485 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F14)); 486 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F15)); 487 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F16)); 488 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F17)); 489 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F18)); 490 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F19)); 491 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F20)); 492 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F21)); 493 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F22)); 494 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F23)); 495 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F24)); 496 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F25)); 497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F26)); 498 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F27)); 499 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F28)); 500 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F29)); 501 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F30)); 502 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F31)); 503 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F32)); 504 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F33)); 505 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F34)); 506 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F35)); 507 // Max function key code plus 1. 508 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F35 + 1)); 509} 510 511#if defined(USE_XI2_MT) 512// Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but 513// that an exception list of keys can still be processed. 514TEST_F(EventsXTest, DisableKeyboard) { 515 DeviceDataManagerX11* device_data_manager = 516 static_cast<DeviceDataManagerX11*>( 517 DeviceDataManager::GetInstance()); 518 unsigned int blocked_device_id = 1; 519 unsigned int other_device_id = 2; 520 unsigned int master_device_id = 3; 521 device_data_manager->DisableDevice(blocked_device_id); 522 523 scoped_ptr<std::set<KeyboardCode> > excepted_keys(new std::set<KeyboardCode>); 524 excepted_keys->insert(VKEY_B); 525 device_data_manager->SetDisabledKeyboardAllowedKeys(excepted_keys.Pass()); 526 527 ScopedXI2Event xev; 528 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN. 529 xev.InitGenericKeyEvent(master_device_id, 530 blocked_device_id, 531 ui::ET_KEY_PRESSED, 532 ui::VKEY_A, 533 0); 534 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev)); 535 536 // The B key is allowed as an exception, and should return KEY_PRESSED. 537 xev.InitGenericKeyEvent(master_device_id, 538 blocked_device_id, 539 ui::ET_KEY_PRESSED, 540 ui::VKEY_B, 541 0); 542 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); 543 544 // Both A and B are allowed on an unblocked keyboard device. 545 xev.InitGenericKeyEvent(master_device_id, 546 other_device_id, 547 ui::ET_KEY_PRESSED, 548 ui::VKEY_A, 549 0); 550 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); 551 xev.InitGenericKeyEvent(master_device_id, 552 other_device_id, 553 ui::ET_KEY_PRESSED, 554 ui::VKEY_B, 555 0); 556 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); 557 558 device_data_manager->EnableDevice(blocked_device_id); 559 device_data_manager->SetDisabledKeyboardAllowedKeys( 560 scoped_ptr<std::set<KeyboardCode> >()); 561 562 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled. 563 xev.InitGenericKeyEvent(master_device_id, 564 blocked_device_id, 565 ui::ET_KEY_PRESSED, 566 ui::VKEY_A, 567 0); 568 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); 569} 570 571// Verifies that the type of events from a disabled mouse is ET_UNKNOWN. 572TEST_F(EventsXTest, DisableMouse) { 573 DeviceDataManagerX11* device_data_manager = 574 static_cast<DeviceDataManagerX11*>( 575 DeviceDataManager::GetInstance()); 576 unsigned int blocked_device_id = 1; 577 unsigned int other_device_id = 2; 578 std::vector<unsigned int> device_list; 579 device_list.push_back(blocked_device_id); 580 device_list.push_back(other_device_id); 581 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); 582 583 device_data_manager->DisableDevice(blocked_device_id); 584 585 ScopedXI2Event xev; 586 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(), 587 EF_LEFT_MOUSE_BUTTON); 588 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev)); 589 590 xev.InitGenericButtonEvent(other_device_id, ET_MOUSE_PRESSED, gfx::Point(), 591 EF_LEFT_MOUSE_BUTTON); 592 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev)); 593 594 device_data_manager->EnableDevice(blocked_device_id); 595 596 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(), 597 EF_LEFT_MOUSE_BUTTON); 598 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev)); 599} 600#endif // defined(USE_XI2_MT) 601 602#if !defined(OS_CHROMEOS) 603TEST_F(EventsXTest, ImeFabricatedKeyEvents) { 604 Display* display = gfx::GetXDisplay(); 605 606 unsigned int state_to_be_fabricated[] = { 607 0, ShiftMask, LockMask, ShiftMask | LockMask, 608 }; 609 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(state_to_be_fabricated); ++i) { 610 unsigned int state = state_to_be_fabricated[i]; 611 for (int is_char = 0; is_char < 2; ++is_char) { 612 XEvent x_event; 613 InitKeyEvent(display, &x_event, true, 0, state); 614 ui::KeyEvent key_event(&x_event); 615 if (is_char) { 616 KeyEventTestApi test_event(&key_event); 617 test_event.set_is_char(true); 618 } 619 EXPECT_TRUE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); 620 } 621 } 622 623 unsigned int state_to_be_not_fabricated[] = { 624 ControlMask, Mod1Mask, Mod2Mask, ShiftMask | ControlMask, 625 }; 626 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(state_to_be_not_fabricated); ++i) { 627 unsigned int state = state_to_be_not_fabricated[i]; 628 for (int is_char = 0; is_char < 2; ++is_char) { 629 XEvent x_event; 630 InitKeyEvent(display, &x_event, true, 0, state); 631 ui::KeyEvent key_event(&x_event); 632 if (is_char) { 633 KeyEventTestApi test_event(&key_event); 634 test_event.set_is_char(true); 635 } 636 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); 637 } 638 } 639} 640#endif 641 642} // namespace ui 643