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 #if !defined(OS_CHROMEOS)
51 // Initializes the passed-in Xlib event.
52 void InitKeyEvent(Display
* display
,
57 memset(event
, 0, sizeof(*event
));
59 // We don't bother setting fields that the event code doesn't use, such as
60 // x_root/y_root and window/root/subwindow.
61 XKeyEvent
* key_event
= &(event
->xkey
);
62 key_event
->display
= display
;
63 key_event
->type
= is_press
? KeyPress
: KeyRelease
;
64 key_event
->keycode
= keycode
;
65 key_event
->state
= state
;
71 class EventsXTest
: public testing::Test
{
74 ~EventsXTest() override
{}
76 void SetUp() override
{
77 DeviceDataManagerX11::CreateInstance();
78 ui::TouchFactory::GetInstance()->ResetForTest();
81 DISALLOW_COPY_AND_ASSIGN(EventsXTest
);
84 TEST_F(EventsXTest
, ButtonEvents
) {
86 gfx::Point
location(5, 10);
89 InitButtonEvent(&event
, true, location
, 1, 0);
90 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(&event
));
91 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
, ui::EventFlagsFromNative(&event
));
92 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
,
93 ui::GetChangedMouseButtonFlagsFromNative(&event
));
94 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
96 InitButtonEvent(&event
, true, location
, 2, Button1Mask
| ShiftMask
);
97 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(&event
));
98 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_MIDDLE_MOUSE_BUTTON
|
100 ui::EventFlagsFromNative(&event
));
101 EXPECT_EQ(ui::EF_MIDDLE_MOUSE_BUTTON
,
102 ui::GetChangedMouseButtonFlagsFromNative(&event
));
103 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
105 InitButtonEvent(&event
, false, location
, 3, 0);
106 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, ui::EventTypeFromNative(&event
));
107 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON
, ui::EventFlagsFromNative(&event
));
108 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON
,
109 ui::GetChangedMouseButtonFlagsFromNative(&event
));
110 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
113 InitButtonEvent(&event
, true, location
, 4, 0);
114 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
115 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
116 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
117 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
118 offset
= ui::GetMouseWheelOffset(&event
);
119 EXPECT_GT(offset
.y(), 0);
120 EXPECT_EQ(0, offset
.x());
123 InitButtonEvent(&event
, true, location
, 5, 0);
124 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
125 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
126 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
127 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
128 offset
= ui::GetMouseWheelOffset(&event
);
129 EXPECT_LT(offset
.y(), 0);
130 EXPECT_EQ(0, offset
.x());
133 InitButtonEvent(&event
, true, location
, 6, 0);
134 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
135 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
136 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
137 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
138 offset
= ui::GetMouseWheelOffset(&event
);
139 EXPECT_EQ(0, offset
.y());
140 EXPECT_GT(offset
.x(), 0);
143 InitButtonEvent(&event
, true, location
, 7, 0);
144 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
145 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
146 EXPECT_EQ(ui::EF_NONE
, ui::GetChangedMouseButtonFlagsFromNative(&event
));
147 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
148 offset
= ui::GetMouseWheelOffset(&event
);
149 EXPECT_EQ(0, offset
.y());
150 EXPECT_LT(offset
.x(), 0);
152 // TODO(derat): Test XInput code.
155 TEST_F(EventsXTest
, AvoidExtraEventsOnWheelRelease
) {
157 gfx::Point
location(5, 10);
159 InitButtonEvent(&event
, true, location
, 4, 0);
160 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
162 // We should return ET_UNKNOWN for the release event instead of returning
163 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step.
164 InitButtonEvent(&event
, false, location
, 4, 0);
165 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(&event
));
167 // TODO(derat): Test XInput code.
170 TEST_F(EventsXTest
, EnterLeaveEvent
) {
172 event
.xcrossing
.type
= EnterNotify
;
173 event
.xcrossing
.x
= 10;
174 event
.xcrossing
.y
= 20;
175 event
.xcrossing
.x_root
= 110;
176 event
.xcrossing
.y_root
= 120;
178 // Mouse enter events are converted to mouse move events to be consistent with
179 // the way views handle mouse enter. See comments for EnterNotify case in
180 // ui::EventTypeFromNative for more details.
181 EXPECT_EQ(ui::ET_MOUSE_MOVED
, ui::EventTypeFromNative(&event
));
182 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event
).ToString());
183 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event
).ToString());
185 event
.xcrossing
.type
= LeaveNotify
;
186 event
.xcrossing
.x
= 30;
187 event
.xcrossing
.y
= 40;
188 event
.xcrossing
.x_root
= 230;
189 event
.xcrossing
.y_root
= 240;
190 EXPECT_EQ(ui::ET_MOUSE_EXITED
, ui::EventTypeFromNative(&event
));
191 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event
).ToString());
192 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event
).ToString());
195 TEST_F(EventsXTest
, ClickCount
) {
197 gfx::Point
location(5, 10);
199 base::TimeDelta time_stamp
= base::TimeDelta::FromMilliseconds(1);
200 for (int i
= 1; i
<= 3; ++i
) {
201 InitButtonEvent(&event
, true, location
, 1, 0);
203 event
.xbutton
.time
= time_stamp
.InMilliseconds();
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 event
.xbutton
.time
= time_stamp
.InMilliseconds();
212 MouseEvent
mouseev(&event
);
213 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, mouseev
.type());
214 EXPECT_EQ(i
, mouseev
.GetClickCount());
216 time_stamp
+= base::TimeDelta::FromMilliseconds(1);
220 TEST_F(EventsXTest
, TouchEventBasic
) {
221 std::vector
<int> devices
;
222 devices
.push_back(0);
223 ui::SetUpTouchDevicesForTest(devices
);
224 std::vector
<Valuator
> valuators
;
226 // Init touch begin with tracking id 5, touch id 0.
227 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 20));
229 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.3f
));
230 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 100));
231 ui::ScopedXI2Event scoped_xevent
;
232 scoped_xevent
.InitTouchEvent(
233 0, XI_TouchBegin
, 5, gfx::Point(10, 10), valuators
);
234 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
235 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent
).ToString());
236 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
237 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
238 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.15f
);
239 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
241 // Touch update, with new orientation info.
244 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.5f
));
245 scoped_xevent
.InitTouchEvent(
246 0, XI_TouchUpdate
, 5, gfx::Point(20, 20), valuators
);
247 EXPECT_EQ(ui::ET_TOUCH_MOVED
, ui::EventTypeFromNative(scoped_xevent
));
248 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent
).ToString());
249 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
250 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
251 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
252 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
254 // Another touch with tracking id 6, touch id 1.
256 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 100));
257 valuators
.push_back(Valuator(
258 DeviceDataManagerX11::DT_TOUCH_ORIENTATION
, 0.9f
));
259 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 500));
260 scoped_xevent
.InitTouchEvent(
261 0, XI_TouchBegin
, 6, gfx::Point(200, 200), valuators
);
262 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
263 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
264 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
265 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 50);
266 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
267 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
269 // Touch with tracking id 5 should have old radius/angle value and new pressue
272 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE
, 50));
273 scoped_xevent
.InitTouchEvent(
274 0, XI_TouchEnd
, 5, gfx::Point(30, 30), valuators
);
275 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
276 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent
).ToString());
277 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
278 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
279 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
280 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.05f
);
282 // Touch with tracking id 6 should have old angle/pressure value and new
285 valuators
.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR
, 50));
286 scoped_xevent
.InitTouchEvent(
287 0, XI_TouchEnd
, 6, gfx::Point(200, 200), valuators
);
288 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
289 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
290 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
291 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 25);
292 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
293 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
296 int GetTouchIdForTrackingId(uint32 tracking_id
) {
299 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id
, &slot
);
305 TEST_F(EventsXTest
, TouchEventNotRemovingFromNativeMapping
) {
306 std::vector
<int> devices
;
307 devices
.push_back(0);
308 ui::SetUpTouchDevicesForTest(devices
);
309 std::vector
<Valuator
> valuators
;
311 const int kTrackingId
= 5;
313 // Two touch presses with the same tracking id.
314 ui::ScopedXI2Event xpress0
;
315 xpress0
.InitTouchEvent(
316 0, XI_TouchBegin
, kTrackingId
, gfx::Point(10, 10), valuators
);
317 scoped_ptr
<ui::TouchEvent
> upress0(new ui::TouchEvent(xpress0
));
318 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
320 ui::ScopedXI2Event xpress1
;
321 xpress1
.InitTouchEvent(
322 0, XI_TouchBegin
, kTrackingId
, gfx::Point(20, 20), valuators
);
323 ui::TouchEvent
upress1(xpress1
);
324 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
326 // The first touch release shouldn't clear the mapping from the
328 ui::ScopedXI2Event xrelease0
;
329 xrelease0
.InitTouchEvent(
330 0, XI_TouchEnd
, kTrackingId
, gfx::Point(10, 10), valuators
);
332 ui::TouchEvent
urelease0(xrelease0
);
333 urelease0
.set_should_remove_native_touch_id_mapping(false);
335 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId
));
337 // The second touch release should clear the mapping from the
339 ui::ScopedXI2Event xrelease1
;
340 xrelease1
.InitTouchEvent(
341 0, XI_TouchEnd
, kTrackingId
, gfx::Point(10, 10), valuators
);
343 ui::TouchEvent
urelease1(xrelease1
);
345 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId
));
348 // Copied events should not remove native touch id mappings, as this causes a
349 // crash (crbug.com/467102). Copied events do not contain a proper
350 // base::NativeEvent and should not attempt to access it.
351 TEST_F(EventsXTest
, CopiedTouchEventNotRemovingFromNativeMapping
) {
352 std::vector
<int> devices
;
353 devices
.push_back(0);
354 ui::SetUpTouchDevicesForTest(devices
);
355 std::vector
<Valuator
> valuators
;
357 // Create a release event which has a native touch id mapping.
358 ui::ScopedXI2Event xrelease0
;
359 xrelease0
.InitTouchEvent(0, XI_TouchEnd
, 0, gfx::Point(10, 10), valuators
);
360 ui::TouchEvent
urelease0(xrelease0
);
362 // When the copy is destructed it should not attempt to remove the mapping.
363 // Exiting this scope should not cause a crash.
364 ui::TouchEvent copy
= urelease0
;
368 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but
369 // that an exception list of keys can still be processed.
370 TEST_F(EventsXTest
, DisableKeyboard
) {
371 DeviceDataManagerX11
* device_data_manager
=
372 static_cast<DeviceDataManagerX11
*>(
373 DeviceDataManager::GetInstance());
374 int blocked_device_id
= 1;
375 int other_device_id
= 2;
376 int master_device_id
= 3;
377 device_data_manager
->DisableDevice(blocked_device_id
);
379 scoped_ptr
<std::set
<KeyboardCode
> > excepted_keys(new std::set
<KeyboardCode
>);
380 excepted_keys
->insert(VKEY_B
);
381 device_data_manager
->SetDisabledKeyboardAllowedKeys(excepted_keys
.Pass());
384 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN.
385 xev
.InitGenericKeyEvent(master_device_id
,
390 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(xev
));
392 // The B key is allowed as an exception, and should return KEY_PRESSED.
393 xev
.InitGenericKeyEvent(master_device_id
,
398 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
400 // Both A and B are allowed on an unblocked keyboard device.
401 xev
.InitGenericKeyEvent(master_device_id
,
406 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
407 xev
.InitGenericKeyEvent(master_device_id
,
412 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
414 device_data_manager
->EnableDevice(blocked_device_id
);
415 device_data_manager
->SetDisabledKeyboardAllowedKeys(nullptr);
417 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled.
418 xev
.InitGenericKeyEvent(master_device_id
,
423 EXPECT_EQ(ui::ET_KEY_PRESSED
, ui::EventTypeFromNative(xev
));
426 // Verifies that the type of events from a disabled mouse is ET_UNKNOWN.
427 TEST_F(EventsXTest
, DisableMouse
) {
428 DeviceDataManagerX11
* device_data_manager
=
429 static_cast<DeviceDataManagerX11
*>(
430 DeviceDataManager::GetInstance());
431 int blocked_device_id
= 1;
432 int other_device_id
= 2;
433 std::vector
<int> device_list
;
434 device_list
.push_back(blocked_device_id
);
435 device_list
.push_back(other_device_id
);
436 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list
);
438 device_data_manager
->DisableDevice(blocked_device_id
);
441 xev
.InitGenericButtonEvent(blocked_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
442 EF_LEFT_MOUSE_BUTTON
);
443 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(xev
));
445 xev
.InitGenericButtonEvent(other_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
446 EF_LEFT_MOUSE_BUTTON
);
447 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(xev
));
449 device_data_manager
->EnableDevice(blocked_device_id
);
451 xev
.InitGenericButtonEvent(blocked_device_id
, ET_MOUSE_PRESSED
, gfx::Point(),
452 EF_LEFT_MOUSE_BUTTON
);
453 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(xev
));
456 #if !defined(OS_CHROMEOS)
457 TEST_F(EventsXTest
, ImeFabricatedKeyEvents
) {
458 Display
* display
= gfx::GetXDisplay();
460 unsigned int state_to_be_fabricated
[] = {
461 0, ShiftMask
, LockMask
, ShiftMask
| LockMask
,
463 for (size_t i
= 0; i
< arraysize(state_to_be_fabricated
); ++i
) {
464 unsigned int state
= state_to_be_fabricated
[i
];
465 for (int is_char
= 0; is_char
< 2; ++is_char
) {
467 InitKeyEvent(display
, &x_event
, true, 0, state
);
468 ui::KeyEvent
key_event(&x_event
);
470 KeyEventTestApi
test_event(&key_event
);
471 test_event
.set_is_char(true);
473 EXPECT_TRUE(key_event
.flags() & ui::EF_IME_FABRICATED_KEY
);
477 unsigned int state_to_be_not_fabricated
[] = {
478 ControlMask
, Mod1Mask
, Mod2Mask
, ShiftMask
| ControlMask
,
480 for (size_t i
= 0; i
< arraysize(state_to_be_not_fabricated
); ++i
) {
481 unsigned int state
= state_to_be_not_fabricated
[i
];
482 for (int is_char
= 0; is_char
< 2; ++is_char
) {
484 InitKeyEvent(display
, &x_event
, true, 0, state
);
485 ui::KeyEvent
key_event(&x_event
);
487 KeyEventTestApi
test_event(&key_event
);
488 test_event
.set_is_char(true);
490 EXPECT_FALSE(key_event
.flags() & ui::EF_IME_FABRICATED_KEY
);
496 TEST_F(EventsXTest
, IgnoresMotionEventForMouseWheelScroll
) {
498 std::vector
<int> devices
;
499 devices
.push_back(device_id
);
500 ui::SetUpPointerDevicesForTest(devices
);
503 xev
.InitScrollEvent(device_id
, 1, 2, 3, 4, 1);
504 // We shouldn't produce a mouse move event on a mouse wheel
505 // scroll. These events are only produced for some mice.
506 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(xev
));