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.
8 #include <X11/extensions/XInput2.h>
10 #include <X11/Xutil.h>
11 #include <X11/XKBlib.h>
13 // Generically-named #defines from Xlib that conflict with symbols in GTest.
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"
32 // Initializes the passed-in Xlib event.
33 void InitButtonEvent(XEvent
* event
,
35 const gfx::Point
& location
,
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
,
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
) {
71 int x_keycode
= XKeysymToKeycode(display
, x_keysym
);
72 // Exclude keysyms for which the server has no corresponding 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
);
83 class EventsXTest
: public testing::Test
{
86 ~EventsXTest() override
{}
88 void SetUp() override
{
89 DeviceDataManagerX11::CreateInstance();
90 ui::TouchFactory::GetInstance()->ResetForTest();
93 DISALLOW_COPY_AND_ASSIGN(EventsXTest
);
96 TEST_F(EventsXTest
, ButtonEvents
) {
98 gfx::Point
location(5, 10);
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(ui::EF_LEFT_MOUSE_BUTTON
,
105 ui::GetChangedMouseButtonFlagsFromNative(&event
));
106 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
108 InitButtonEvent(&event
, true, location
, 2, Button1Mask
| ShiftMask
);
109 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(&event
));
110 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_MIDDLE_MOUSE_BUTTON
|
112 ui::EventFlagsFromNative(&event
));
113 EXPECT_EQ(ui::EF_MIDDLE_MOUSE_BUTTON
,
114 ui::GetChangedMouseButtonFlagsFromNative(&event
));
115 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
117 InitButtonEvent(&event
, false, location
, 3, 0);
118 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, ui::EventTypeFromNative(&event
));
119 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON
, ui::EventFlagsFromNative(&event
));
120 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON
,
121 ui::GetChangedMouseButtonFlagsFromNative(&event
));
122 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
125 InitButtonEvent(&event
, true, location
, 4, 0);
126 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
127 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
128 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
129 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
130 offset
= ui::GetMouseWheelOffset(&event
);
131 EXPECT_GT(offset
.y(), 0);
132 EXPECT_EQ(0, offset
.x());
135 InitButtonEvent(&event
, true, location
, 5, 0);
136 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
137 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
138 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
139 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
140 offset
= ui::GetMouseWheelOffset(&event
);
141 EXPECT_LT(offset
.y(), 0);
142 EXPECT_EQ(0, offset
.x());
145 InitButtonEvent(&event
, true, location
, 6, 0);
146 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
147 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
148 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
149 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
150 offset
= ui::GetMouseWheelOffset(&event
);
151 EXPECT_EQ(0, offset
.y());
152 EXPECT_GT(offset
.x(), 0);
155 InitButtonEvent(&event
, true, location
, 7, 0);
156 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
157 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
158 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
159 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
160 offset
= ui::GetMouseWheelOffset(&event
);
161 EXPECT_EQ(0, offset
.y());
162 EXPECT_LT(offset
.x(), 0);
164 // TODO(derat): Test XInput code.
167 TEST_F(EventsXTest
, AvoidExtraEventsOnWheelRelease
) {
169 gfx::Point
location(5, 10);
171 InitButtonEvent(&event
, true, location
, 4, 0);
172 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
174 // We should return ET_UNKNOWN for the release event instead of returning
175 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step.
176 InitButtonEvent(&event
, false, location
, 4, 0);
177 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(&event
));
179 // TODO(derat): Test XInput code.
182 TEST_F(EventsXTest
, EnterLeaveEvent
) {
184 event
.xcrossing
.type
= EnterNotify
;
185 event
.xcrossing
.x
= 10;
186 event
.xcrossing
.y
= 20;
187 event
.xcrossing
.x_root
= 110;
188 event
.xcrossing
.y_root
= 120;
190 // Mouse enter events are converted to mouse move events to be consistent with
191 // the way views handle mouse enter. See comments for EnterNotify case in
192 // ui::EventTypeFromNative for more details.
193 EXPECT_EQ(ui::ET_MOUSE_MOVED
, ui::EventTypeFromNative(&event
));
194 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event
).ToString());
195 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event
).ToString());
197 event
.xcrossing
.type
= LeaveNotify
;
198 event
.xcrossing
.x
= 30;
199 event
.xcrossing
.y
= 40;
200 event
.xcrossing
.x_root
= 230;
201 event
.xcrossing
.y_root
= 240;
202 EXPECT_EQ(ui::ET_MOUSE_EXITED
, ui::EventTypeFromNative(&event
));
203 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event
).ToString());
204 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event
).ToString());
207 TEST_F(EventsXTest
, ClickCount
) {
209 gfx::Point
location(5, 10);
211 base::TimeDelta time_stamp
= base::TimeDelta::FromMilliseconds(1);
212 for (int i
= 1; i
<= 3; ++i
) {
213 InitButtonEvent(&event
, true, location
, 1, 0);
215 event
.xbutton
.time
= time_stamp
.InMilliseconds();
216 MouseEvent
mouseev(&event
);
217 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, mouseev
.type());
218 EXPECT_EQ(i
, mouseev
.GetClickCount());
221 InitButtonEvent(&event
, false, location
, 1, 0);
223 event
.xbutton
.time
= time_stamp
.InMilliseconds();
224 MouseEvent
mouseev(&event
);
225 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, mouseev
.type());
226 EXPECT_EQ(i
, mouseev
.GetClickCount());
228 time_stamp
+= base::TimeDelta::FromMilliseconds(1);
232 TEST_F(EventsXTest
, TouchEventBasic
) {
233 std::vector
<int> devices
;
234 devices
.push_back(0);
235 ui::SetUpTouchDevicesForTest(devices
);
236 std::vector
<Valuator
> valuators
;
238 // Init touch begin with tracking id 5, touch id 0.
239 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 20));
241 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.3f
));
242 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 100));
243 ui::ScopedXI2Event scoped_xevent
;
244 scoped_xevent
.InitTouchEvent(
245 0, XI_TouchBegin
, 5, gfx::Point(10, 10), valuators
);
246 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
247 EXPECT_EQ("10,10", 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.15f
);
251 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
253 // Touch update, with new orientation info.
256 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.5f
));
257 scoped_xevent
.InitTouchEvent(
258 0, XI_TouchUpdate
, 5, gfx::Point(20, 20), valuators
);
259 EXPECT_EQ(ui::ET_TOUCH_MOVED
, ui::EventTypeFromNative(scoped_xevent
));
260 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent
).ToString());
261 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
262 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
263 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
264 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
266 // Another touch with tracking id 6, touch id 1.
268 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 100));
269 valuators
.push_back(Valuator(
270 DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.9f
));
271 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 500));
272 scoped_xevent
.InitTouchEvent(
273 0, XI_TouchBegin
, 6, gfx::Point(200, 200), valuators
);
274 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
275 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
276 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
277 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 50);
278 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
279 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
281 // Touch with tracking id 5 should have old radius/angle value and new pressue
284 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 50));
285 scoped_xevent
.InitTouchEvent(
286 0, XI_TouchEnd
, 5, gfx::Point(30, 30), valuators
);
287 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
288 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent
).ToString());
289 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
290 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
291 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
292 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.05f
);
294 // Touch with tracking id 6 should have old angle/pressure value and new
297 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 50));
298 scoped_xevent
.InitTouchEvent(
299 0, XI_TouchEnd
, 6, gfx::Point(200, 200), valuators
);
300 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
301 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
302 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
303 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 25);
304 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
305 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
308 int GetTouchIdForTrackingId(uint32 tracking_id
) {
311 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id
, &slot
);
317 TEST_F(EventsXTest
, TouchEventNotRemovingFromNativeMapping
) {
318 std::vector
<int> devices
;
319 devices
.push_back(0);
320 ui::SetUpTouchDevicesForTest(devices
);
321 std::vector
<Valuator
> valuators
;
323 const int kTrackingId
= 5;
325 // Two touch presses with the same tracking id.
326 ui::ScopedXI2Event xpress0
;
327 xpress0
.InitTouchEvent(
328 0, XI_TouchBegin
, kTrackingId
, gfx::Point(10, 10), valuators
);
329 scoped_ptr
<ui::TouchEvent
> upress0(new ui::TouchEvent(xpress0
));
330 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
332 ui::ScopedXI2Event xpress1
;
333 xpress1
.InitTouchEvent(
334 0, XI_TouchBegin
, kTrackingId
, gfx::Point(20, 20), valuators
);
335 ui::TouchEvent
upress1(xpress1
);
336 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
338 // The first touch release shouldn't clear the mapping from the
340 ui::ScopedXI2Event xrelease0
;
341 xrelease0
.InitTouchEvent(
342 0, XI_TouchEnd
, kTrackingId
, gfx::Point(10, 10), valuators
);
344 ui::TouchEvent
urelease0(xrelease0
);
345 urelease0
.set_should_remove_native_touch_id_mapping(false);
347 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
349 // The second touch release should clear the mapping from the
351 ui::ScopedXI2Event xrelease1
;
352 xrelease1
.InitTouchEvent(
353 0, XI_TouchEnd
, kTrackingId
, gfx::Point(10, 10), valuators
);
355 ui::TouchEvent
urelease1(xrelease1
);
357 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId
));
360 // Copied events should not remove native touch id mappings, as this causes a
361 // crash (crbug.com/467102). Copied events do not contain a proper
362 // base::NativeEvent and should not attempt to access it.
363 TEST_F(EventsXTest
, CopiedTouchEventNotRemovingFromNativeMapping
) {
364 std::vector
<int> devices
;
365 devices
.push_back(0);
366 ui::SetUpTouchDevicesForTest(devices
);
367 std::vector
<Valuator
> valuators
;
369 // Create a release event which has a native touch id mapping.
370 ui::ScopedXI2Event xrelease0
;
371 xrelease0
.InitTouchEvent(0, XI_TouchEnd
, 0, gfx::Point(10, 10), valuators
);
372 ui::TouchEvent
urelease0(xrelease0
);
374 // When the copy is destructed it should not attempt to remove the mapping.
375 // Exiting this scope should not cause a crash.
376 ui::TouchEvent copy
= urelease0
;
380 TEST_F(EventsXTest
, NumpadKeyEvents
) {
382 Display
* display
= gfx::GetXDisplay();
388 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional
389 // keysymdef.h numbering.
390 { true, XK_KP_Space
},
391 { true, XK_KP_Equal
},
392 // Other numpad keysyms. (This is actually exhaustive in the current list.)
394 { true, XK_KP_Enter
},
399 { true, XK_KP_Home
},
400 { true, XK_KP_Left
},
402 { true, XK_KP_Right
},
403 { true, XK_KP_Down
},
404 { true, XK_KP_Prior
},
405 { true, XK_KP_Page_Up
},
406 { true, XK_KP_Next
},
407 { true, XK_KP_Page_Down
},
409 { true, XK_KP_Begin
},
410 { true, XK_KP_Insert
},
411 { true, XK_KP_Delete
},
412 { true, XK_KP_Multiply
},
414 { true, XK_KP_Separator
},
415 { true, XK_KP_Subtract
},
416 { true, XK_KP_Decimal
},
417 { true, XK_KP_Divide
},
428 // Largest keysym preceding XK_KP_Space.
429 { false, XK_Num_Lock
},
430 // Smallest keysym following XK_KP_Equal.
432 // Non-numpad analogues of numpad keysyms.
434 { false, XK_Return
},
445 { false, XK_Page_Up
},
447 { false, XK_Page_Down
},
449 { false, XK_Insert
},
450 { false, XK_Delete
},
451 { false, XK_multiply
},
454 { false, XK_period
},
466 // Miscellaneous other keysyms.
467 { false, XK_BackSpace
},
468 { false, XK_Scroll_Lock
},
469 { false, XK_Multi_key
},
470 { false, XK_Select
},
471 { false, XK_Num_Lock
},
472 { false, XK_Shift_L
},
477 for (size_t k
= 0; k
< arraysize(keys
); ++k
) {
478 int x_keycode
= XKeysymToKeycode(display
, keys
[k
].x_keysym
);
479 // Exclude keysyms for which the server has no corresponding keycode.
481 InitKeyEvent(display
, &event
, true, x_keycode
, 0);
482 // int keysym = XLookupKeysym(&event.xkey, 0);
484 ui::KeyEvent
ui_key_event(&event
);
485 EXPECT_EQ(keys
[k
].is_numpad_key
? ui::EF_NUMPAD_KEY
: 0,
486 ui_key_event
.flags() & ui::EF_NUMPAD_KEY
);
491 TEST_F(EventsXTest
, FunctionKeyEvents
) {
492 Display
* display
= gfx::GetXDisplay();
494 // Min function key code minus 1.
495 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display
, XK_F1
- 1));
496 // All function keys.
497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F1
));
498 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F2
));
499 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F3
));
500 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F4
));
501 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F5
));
502 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F6
));
503 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F7
));
504 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F8
));
505 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F9
));
506 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F10
));
507 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F11
));
508 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F12
));
509 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F13
));
510 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F14
));
511 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F15
));
512 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F16
));
513 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F17
));
514 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F18
));
515 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F19
));
516 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F20
));
517 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F21
));
518 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F22
));
519 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F23
));
520 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F24
));
521 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F25
));
522 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F26
));
523 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F27
));
524 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F28
));
525 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F29
));
526 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F30
));
527 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F31
));
528 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F32
));
529 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F33
));
530 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F34
));
531 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display
, XK_F35
));
532 // Max function key code plus 1.
533 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display
, XK_F35
+ 1));
536 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but
537 // that an exception list of keys can still be processed.
538 TEST_F(EventsXTest
, DisableKeyboard
) {
539 DeviceDataManagerX11
* device_data_manager
=
540 static_cast<DeviceDataManagerX11
*>(
541 DeviceDataManager::GetInstance());
542 int blocked_device_id
= 1;
543 int other_device_id
= 2;
544 int master_device_id
= 3;
545 device_data_manager
->DisableDevice(blocked_device_id
);
547 scoped_ptr
<std::set
<KeyboardCode
> > excepted_keys(new std::set
<KeyboardCode
>);
548 excepted_keys
->insert(VKEY_B
);
549 device_data_manager
->SetDisabledKeyboardAllowedKeys(excepted_keys
.Pass());
552 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN.
553 xev
.InitGenericKeyEvent(master_device_id
,
558 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(xev
));
560 // The B key is allowed as an exception, and should return KEY_PRESSED.
561 xev
.InitGenericKeyEvent(master_device_id
,
566 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
568 // Both A and B are allowed on an unblocked keyboard device.
569 xev
.InitGenericKeyEvent(master_device_id
,
574 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
575 xev
.InitGenericKeyEvent(master_device_id
,
580 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
582 device_data_manager
->EnableDevice(blocked_device_id
);
583 device_data_manager
->SetDisabledKeyboardAllowedKeys(nullptr);
585 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled.
586 xev
.InitGenericKeyEvent(master_device_id
,
591 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
594 // Verifies that the type of events from a disabled mouse is ET_UNKNOWN.
595 TEST_F(EventsXTest
, DisableMouse
) {
596 DeviceDataManagerX11
* device_data_manager
=
597 static_cast<DeviceDataManagerX11
*>(
598 DeviceDataManager::GetInstance());
599 int blocked_device_id
= 1;
600 int other_device_id
= 2;
601 std::vector
<int> device_list
;
602 device_list
.push_back(blocked_device_id
);
603 device_list
.push_back(other_device_id
);
604 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list
);
606 device_data_manager
->DisableDevice(blocked_device_id
);
609 xev
.InitGenericButtonEvent(blocked_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
610 EF_LEFT_MOUSE_BUTTON
);
611 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(xev
));
613 xev
.InitGenericButtonEvent(other_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
614 EF_LEFT_MOUSE_BUTTON
);
615 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(xev
));
617 device_data_manager
->EnableDevice(blocked_device_id
);
619 xev
.InitGenericButtonEvent(blocked_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
620 EF_LEFT_MOUSE_BUTTON
);
621 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(xev
));
624 #if !defined(OS_CHROMEOS)
625 TEST_F(EventsXTest
, ImeFabricatedKeyEvents
) {
626 Display
* display
= gfx::GetXDisplay();
628 unsigned int state_to_be_fabricated
[] = {
629 0, ShiftMask
, LockMask
, ShiftMask
| LockMask
,
631 for (size_t i
= 0; i
< arraysize(state_to_be_fabricated
); ++i
) {
632 unsigned int state
= state_to_be_fabricated
[i
];
633 for (int is_char
= 0; is_char
< 2; ++is_char
) {
635 InitKeyEvent(display
, &x_event
, true, 0, state
);
636 ui::KeyEvent
key_event(&x_event
);
638 KeyEventTestApi
test_event(&key_event
);
639 test_event
.set_is_char(true);
641 EXPECT_TRUE(key_event
.flags() & ui::EF_IME_FABRICATED_KEY
);
645 unsigned int state_to_be_not_fabricated
[] = {
646 ControlMask
, Mod1Mask
, Mod2Mask
, ShiftMask
| ControlMask
,
648 for (size_t i
= 0; i
< arraysize(state_to_be_not_fabricated
); ++i
) {
649 unsigned int state
= state_to_be_not_fabricated
[i
];
650 for (int is_char
= 0; is_char
< 2; ++is_char
) {
652 InitKeyEvent(display
, &x_event
, true, 0, state
);
653 ui::KeyEvent
key_event(&x_event
);
655 KeyEventTestApi
test_event(&key_event
);
656 test_event
.set_is_char(true);
658 EXPECT_FALSE(key_event
.flags() & ui::EF_IME_FABRICATED_KEY
);