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(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
|
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
));
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());
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());
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);
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
) {
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
) {
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
) {
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));
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.
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.
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
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
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
) {
297 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id
, &slot
);
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
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
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
) {
348 Display
* display
= gfx::GetXDisplay();
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.)
360 { true, XK_KP_Enter
},
365 { true, XK_KP_Home
},
366 { true, XK_KP_Left
},
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
},
375 { true, XK_KP_Begin
},
376 { true, XK_KP_Insert
},
377 { true, XK_KP_Delete
},
378 { true, XK_KP_Multiply
},
380 { true, XK_KP_Separator
},
381 { true, XK_KP_Subtract
},
382 { true, XK_KP_Decimal
},
383 { true, XK_KP_Divide
},
394 // Largest keysym preceding XK_KP_Space.
395 { false, XK_Num_Lock
},
396 // Smallest keysym following XK_KP_Equal.
398 // Non-numpad analogues of numpad keysyms.
400 { false, XK_Return
},
411 { false, XK_Page_Up
},
413 { false, XK_Page_Down
},
415 { false, XK_Insert
},
416 { false, XK_Delete
},
417 { false, XK_multiply
},
420 { false, XK_period
},
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
},
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.
447 InitKeyEvent(display
, &event
, true, x_keycode
, 0);
448 // int keysym = XLookupKeysym(&event.xkey, 0);
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());
518 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN.
519 xev
.InitGenericKeyEvent(master_device_id
,
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
,
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
,
540 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
541 xev
.InitGenericKeyEvent(master_device_id
,
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
,
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
);
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
) {
601 InitKeyEvent(display
, &x_event
, true, 0, state
);
602 ui::KeyEvent
key_event(&x_event
);
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
) {
618 InitKeyEvent(display
, &x_event
, true, 0, state
);
619 ui::KeyEvent
key_event(&x_event
);
621 KeyEventTestApi
test_event(&key_event
);
622 test_event
.set_is_char(true);
624 EXPECT_FALSE(key_event
.flags() & ui::EF_IME_FABRICATED_KEY
);