ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / events / x / events_x_unittest.cc
blob554590a04b9ee04648f2f52a48d3d6c360f33570
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.
5 #include <cstring>
6 #include <set>
8 #include <X11/extensions/XInput2.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include <X11/XKBlib.h>
13 // Generically-named #defines from Xlib that conflict with symbols in GTest.
14 #undef Bool
15 #undef None
17 #include "base/memory/scoped_ptr.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/events/devices/x11/device_data_manager_x11.h"
20 #include "ui/events/devices/x11/touch_factory_x11.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/test/events_test_utils.h"
25 #include "ui/events/test/events_test_utils_x11.h"
26 #include "ui/gfx/geometry/point.h"
28 namespace ui {
30 namespace {
32 // Initializes the passed-in Xlib event.
33 void InitButtonEvent(XEvent* event,
34 bool is_press,
35 const gfx::Point& location,
36 int button,
37 int state) {
38 memset(event, 0, sizeof(*event));
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;
50 // Initializes the passed-in Xlib event.
51 void InitKeyEvent(Display* display,
52 XEvent* event,
53 bool is_press,
54 int keycode,
55 int state) {
56 memset(event, 0, sizeof(*event));
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;
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.
69 bool 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);
78 return true;
81 } // namespace
83 class EventsXTest : public testing::Test {
84 public:
85 EventsXTest() {}
86 ~EventsXTest() override {}
88 void SetUp() override {
89 DeviceDataManagerX11::CreateInstance();
90 ui::TouchFactory::GetInstance()->ResetForTest();
92 private:
93 DISALLOW_COPY_AND_ASSIGN(EventsXTest);
96 TEST_F(EventsXTest, ButtonEvents) {
97 XEvent event;
98 gfx::Point location(5, 10);
99 gfx::Vector2d offset;
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));
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));
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));
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());
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());
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);
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);
154 // TODO(derat): Test XInput code.
157 TEST_F(EventsXTest, AvoidExtraEventsOnWheelRelease) {
158 XEvent event;
159 gfx::Point location(5, 10);
161 InitButtonEvent(&event, true, location, 4, 0);
162 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event));
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));
169 // TODO(derat): Test XInput code.
172 TEST_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;
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());
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());
197 TEST_F(EventsXTest, ClickCount) {
198 XEvent event;
199 gfx::Point location(5, 10);
201 for (int i = 1; i <= 3; ++i) {
202 InitButtonEvent(&event, true, location, 1, 0);
204 MouseEvent mouseev(&event);
205 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type());
206 EXPECT_EQ(i, mouseev.GetClickCount());
209 InitButtonEvent(&event, false, location, 1, 0);
211 MouseEvent mouseev(&event);
212 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type());
213 EXPECT_EQ(i, mouseev.GetClickCount());
218 TEST_F(EventsXTest, TouchEventBasic) {
219 std::vector<unsigned int> devices;
220 devices.push_back(0);
221 ui::SetUpTouchDevicesForTest(devices);
222 std::vector<Valuator> valuators;
224 // Init touch begin with tracking id 5, touch id 0.
225 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 20));
226 valuators.push_back(
227 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.3f));
228 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 100));
229 ui::ScopedXI2Event scoped_xevent;
230 scoped_xevent.InitTouchEvent(
231 0, XI_TouchBegin, 5, gfx::Point(10, 10), valuators);
232 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
233 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString());
234 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
235 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
236 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f);
237 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
239 // Touch update, with new orientation info.
240 valuators.clear();
241 valuators.push_back(
242 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.5f));
243 scoped_xevent.InitTouchEvent(
244 0, XI_TouchUpdate, 5, gfx::Point(20, 20), valuators);
245 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent));
246 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString());
247 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
248 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
249 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
250 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f);
252 // Another touch with tracking id 6, touch id 1.
253 valuators.clear();
254 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 100));
255 valuators.push_back(Valuator(
256 DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.9f));
257 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 500));
258 scoped_xevent.InitTouchEvent(
259 0, XI_TouchBegin, 6, gfx::Point(200, 200), valuators);
260 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent));
261 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
262 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
263 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50);
264 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
265 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);
267 // Touch with tracking id 5 should have old radius/angle value and new pressue
268 // value.
269 valuators.clear();
270 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 50));
271 scoped_xevent.InitTouchEvent(
272 0, XI_TouchEnd, 5, gfx::Point(30, 30), valuators);
273 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
274 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString());
275 EXPECT_EQ(GetTouchId(scoped_xevent), 0);
276 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10);
277 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f);
278 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f);
280 // Touch with tracking id 6 should have old angle/pressure value and new
281 // radius value.
282 valuators.clear();
283 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 50));
284 scoped_xevent.InitTouchEvent(
285 0, XI_TouchEnd, 6, gfx::Point(200, 200), valuators);
286 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent));
287 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString());
288 EXPECT_EQ(GetTouchId(scoped_xevent), 1);
289 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25);
290 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f);
291 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f);
294 int GetTouchIdForTrackingId(uint32 tracking_id) {
295 int slot = 0;
296 bool success =
297 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id, &slot);
298 if (success)
299 return slot;
300 return -1;
303 TEST_F(EventsXTest, TouchEventNotRemovingFromNativeMapping) {
304 std::vector<unsigned int> devices;
305 devices.push_back(0);
306 ui::SetUpTouchDevicesForTest(devices);
307 std::vector<Valuator> valuators;
309 const int kTrackingId = 5;
311 // Two touch presses with the same tracking id.
312 ui::ScopedXI2Event xpress0;
313 xpress0.InitTouchEvent(
314 0, XI_TouchBegin, kTrackingId, gfx::Point(10, 10), valuators);
315 scoped_ptr<ui::TouchEvent> upress0(new ui::TouchEvent(xpress0));
316 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId));
318 ui::ScopedXI2Event xpress1;
319 xpress1.InitTouchEvent(
320 0, XI_TouchBegin, kTrackingId, gfx::Point(20, 20), valuators);
321 ui::TouchEvent upress1(xpress1);
322 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId));
324 // The first touch release shouldn't clear the mapping from the
325 // tracking id.
326 ui::ScopedXI2Event xrelease0;
327 xrelease0.InitTouchEvent(
328 0, XI_TouchEnd, kTrackingId, gfx::Point(10, 10), valuators);
330 ui::TouchEvent urelease0(xrelease0);
331 urelease0.set_should_remove_native_touch_id_mapping(false);
333 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId));
335 // The second touch release should clear the mapping from the
336 // tracking id.
337 ui::ScopedXI2Event xrelease1;
338 xrelease1.InitTouchEvent(
339 0, XI_TouchEnd, kTrackingId, gfx::Point(10, 10), valuators);
341 ui::TouchEvent urelease1(xrelease1);
343 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId));
346 TEST_F(EventsXTest, NumpadKeyEvents) {
347 XEvent event;
348 Display* display = gfx::GetXDisplay();
350 struct {
351 bool is_numpad_key;
352 int x_keysym;
353 } keys[] = {
354 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional
355 // keysymdef.h numbering.
356 { true, XK_KP_Space },
357 { true, XK_KP_Equal },
358 // Other numpad keysyms. (This is actually exhaustive in the current list.)
359 { true, XK_KP_Tab },
360 { true, XK_KP_Enter },
361 { true, XK_KP_F1 },
362 { true, XK_KP_F2 },
363 { true, XK_KP_F3 },
364 { true, XK_KP_F4 },
365 { true, XK_KP_Home },
366 { true, XK_KP_Left },
367 { true, XK_KP_Up },
368 { true, XK_KP_Right },
369 { true, XK_KP_Down },
370 { true, XK_KP_Prior },
371 { true, XK_KP_Page_Up },
372 { true, XK_KP_Next },
373 { true, XK_KP_Page_Down },
374 { true, XK_KP_End },
375 { true, XK_KP_Begin },
376 { true, XK_KP_Insert },
377 { true, XK_KP_Delete },
378 { true, XK_KP_Multiply },
379 { true, XK_KP_Add },
380 { true, XK_KP_Separator },
381 { true, XK_KP_Subtract },
382 { true, XK_KP_Decimal },
383 { true, XK_KP_Divide },
384 { true, XK_KP_0 },
385 { true, XK_KP_1 },
386 { true, XK_KP_2 },
387 { true, XK_KP_3 },
388 { true, XK_KP_4 },
389 { true, XK_KP_5 },
390 { true, XK_KP_6 },
391 { true, XK_KP_7 },
392 { true, XK_KP_8 },
393 { true, XK_KP_9 },
394 // Largest keysym preceding XK_KP_Space.
395 { false, XK_Num_Lock },
396 // Smallest keysym following XK_KP_Equal.
397 { false, XK_F1 },
398 // Non-numpad analogues of numpad keysyms.
399 { false, XK_Tab },
400 { false, XK_Return },
401 { false, XK_F1 },
402 { false, XK_F2 },
403 { false, XK_F3 },
404 { false, XK_F4 },
405 { false, XK_Home },
406 { false, XK_Left },
407 { false, XK_Up },
408 { false, XK_Right },
409 { false, XK_Down },
410 { false, XK_Prior },
411 { false, XK_Page_Up },
412 { false, XK_Next },
413 { false, XK_Page_Down },
414 { false, XK_End },
415 { false, XK_Insert },
416 { false, XK_Delete },
417 { false, XK_multiply },
418 { false, XK_plus },
419 { false, XK_minus },
420 { false, XK_period },
421 { false, XK_slash },
422 { false, XK_0 },
423 { false, XK_1 },
424 { false, XK_2 },
425 { false, XK_3 },
426 { false, XK_4 },
427 { false, XK_5 },
428 { false, XK_6 },
429 { false, XK_7 },
430 { false, XK_8 },
431 { false, XK_9 },
432 // Miscellaneous other keysyms.
433 { false, XK_BackSpace },
434 { false, XK_Scroll_Lock },
435 { false, XK_Multi_key },
436 { false, XK_Select },
437 { false, XK_Num_Lock },
438 { false, XK_Shift_L },
439 { false, XK_space },
440 { false, XK_A },
443 for (size_t k = 0; k < arraysize(keys); ++k) {
444 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym);
445 // Exclude keysyms for which the server has no corresponding keycode.
446 if (x_keycode) {
447 InitKeyEvent(display, &event, true, x_keycode, 0);
448 // int keysym = XLookupKeysym(&event.xkey, 0);
449 // if (keysym) {
450 ui::KeyEvent ui_key_event(&event);
451 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0,
452 ui_key_event.flags() & ui::EF_NUMPAD_KEY);
457 TEST_F(EventsXTest, FunctionKeyEvents) {
458 Display* display = gfx::GetXDisplay();
460 // Min function key code minus 1.
461 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F1 - 1));
462 // All function keys.
463 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F1));
464 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F2));
465 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F3));
466 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F4));
467 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F5));
468 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F6));
469 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F7));
470 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F8));
471 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F9));
472 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F10));
473 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F11));
474 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F12));
475 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F13));
476 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F14));
477 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F15));
478 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F16));
479 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F17));
480 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F18));
481 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F19));
482 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F20));
483 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F21));
484 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F22));
485 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F23));
486 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F24));
487 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F25));
488 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F26));
489 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F27));
490 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F28));
491 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F29));
492 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F30));
493 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F31));
494 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F32));
495 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F33));
496 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F34));
497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F35));
498 // Max function key code plus 1.
499 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F35 + 1));
502 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but
503 // that an exception list of keys can still be processed.
504 TEST_F(EventsXTest, DisableKeyboard) {
505 DeviceDataManagerX11* device_data_manager =
506 static_cast<DeviceDataManagerX11*>(
507 DeviceDataManager::GetInstance());
508 unsigned int blocked_device_id = 1;
509 unsigned int other_device_id = 2;
510 unsigned int master_device_id = 3;
511 device_data_manager->DisableDevice(blocked_device_id);
513 scoped_ptr<std::set<KeyboardCode> > excepted_keys(new std::set<KeyboardCode>);
514 excepted_keys->insert(VKEY_B);
515 device_data_manager->SetDisabledKeyboardAllowedKeys(excepted_keys.Pass());
517 ScopedXI2Event xev;
518 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN.
519 xev.InitGenericKeyEvent(master_device_id,
520 blocked_device_id,
521 ui::ET_KEY_PRESSED,
522 ui::VKEY_A,
524 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev));
526 // The B key is allowed as an exception, and should return KEY_PRESSED.
527 xev.InitGenericKeyEvent(master_device_id,
528 blocked_device_id,
529 ui::ET_KEY_PRESSED,
530 ui::VKEY_B,
532 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
534 // Both A and B are allowed on an unblocked keyboard device.
535 xev.InitGenericKeyEvent(master_device_id,
536 other_device_id,
537 ui::ET_KEY_PRESSED,
538 ui::VKEY_A,
540 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
541 xev.InitGenericKeyEvent(master_device_id,
542 other_device_id,
543 ui::ET_KEY_PRESSED,
544 ui::VKEY_B,
546 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
548 device_data_manager->EnableDevice(blocked_device_id);
549 device_data_manager->SetDisabledKeyboardAllowedKeys(nullptr);
551 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled.
552 xev.InitGenericKeyEvent(master_device_id,
553 blocked_device_id,
554 ui::ET_KEY_PRESSED,
555 ui::VKEY_A,
557 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
560 // Verifies that the type of events from a disabled mouse is ET_UNKNOWN.
561 TEST_F(EventsXTest, DisableMouse) {
562 DeviceDataManagerX11* device_data_manager =
563 static_cast<DeviceDataManagerX11*>(
564 DeviceDataManager::GetInstance());
565 unsigned int blocked_device_id = 1;
566 unsigned int other_device_id = 2;
567 std::vector<unsigned int> device_list;
568 device_list.push_back(blocked_device_id);
569 device_list.push_back(other_device_id);
570 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list);
572 device_data_manager->DisableDevice(blocked_device_id);
574 ScopedXI2Event xev;
575 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(),
576 EF_LEFT_MOUSE_BUTTON);
577 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev));
579 xev.InitGenericButtonEvent(other_device_id, ET_MOUSE_PRESSED, gfx::Point(),
580 EF_LEFT_MOUSE_BUTTON);
581 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev));
583 device_data_manager->EnableDevice(blocked_device_id);
585 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(),
586 EF_LEFT_MOUSE_BUTTON);
587 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev));
590 #if !defined(OS_CHROMEOS)
591 TEST_F(EventsXTest, ImeFabricatedKeyEvents) {
592 Display* display = gfx::GetXDisplay();
594 unsigned int state_to_be_fabricated[] = {
595 0, ShiftMask, LockMask, ShiftMask | LockMask,
597 for (size_t i = 0; i < arraysize(state_to_be_fabricated); ++i) {
598 unsigned int state = state_to_be_fabricated[i];
599 for (int is_char = 0; is_char < 2; ++is_char) {
600 XEvent x_event;
601 InitKeyEvent(display, &x_event, true, 0, state);
602 ui::KeyEvent key_event(&x_event);
603 if (is_char) {
604 KeyEventTestApi test_event(&key_event);
605 test_event.set_is_char(true);
607 EXPECT_TRUE(key_event.flags() & ui::EF_IME_FABRICATED_KEY);
611 unsigned int state_to_be_not_fabricated[] = {
612 ControlMask, Mod1Mask, Mod2Mask, ShiftMask | ControlMask,
614 for (size_t i = 0; i < arraysize(state_to_be_not_fabricated); ++i) {
615 unsigned int state = state_to_be_not_fabricated[i];
616 for (int is_char = 0; is_char < 2; ++is_char) {
617 XEvent x_event;
618 InitKeyEvent(display, &x_event, true, 0, state);
619 ui::KeyEvent key_event(&x_event);
620 if (is_char) {
621 KeyEventTestApi test_event(&key_event);
622 test_event.set_is_char(true);
624 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY);
628 #endif
630 } // namespace ui