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.
7 #include <X11/extensions/XInput2.h>
10 #include <X11/XKBlib.h>
12 // Generically-named #defines from Xlib that conflict with symbols in GTest.
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/events/event.h"
18 #include "ui/events/event_constants.h"
19 #include "ui/events/event_utils.h"
20 #include "ui/events/test/events_test_utils_x11.h"
21 #include "ui/gfx/point.h"
27 // Initializes the passed-in Xlib event.
28 void InitButtonEvent(XEvent
* event
,
30 const gfx::Point
& location
,
33 memset(event
, 0, sizeof(*event
));
35 // We don't bother setting fields that the event code doesn't use, such as
36 // x_root/y_root and window/root/subwindow.
37 XButtonEvent
* button_event
= &(event
->xbutton
);
38 button_event
->type
= is_press
? ButtonPress
: ButtonRelease
;
39 button_event
->x
= location
.x();
40 button_event
->y
= location
.y();
41 button_event
->button
= button
;
42 button_event
->state
= state
;
45 // Initializes the passed-in Xlib event.
46 void InitKeyEvent(Display
* display
,
51 memset(event
, 0, sizeof(*event
));
53 // We don't bother setting fields that the event code doesn't use, such as
54 // x_root/y_root and window/root/subwindow.
55 XKeyEvent
* key_event
= &(event
->xkey
);
56 key_event
->display
= display
;
57 key_event
->type
= is_press
? KeyPress
: KeyRelease
;
58 key_event
->keycode
= keycode
;
59 key_event
->state
= state
;
64 TEST(EventsXTest
, ButtonEvents
) {
66 gfx::Point
location(5, 10);
69 InitButtonEvent(&event
, true, location
, 1, 0);
70 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(&event
));
71 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
, ui::EventFlagsFromNative(&event
));
72 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
74 InitButtonEvent(&event
, true, location
, 2, Button1Mask
| ShiftMask
);
75 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, ui::EventTypeFromNative(&event
));
76 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_MIDDLE_MOUSE_BUTTON
|
78 ui::EventFlagsFromNative(&event
));
79 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
81 InitButtonEvent(&event
, false, location
, 3, 0);
82 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, ui::EventTypeFromNative(&event
));
83 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON
, ui::EventFlagsFromNative(&event
));
84 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
87 InitButtonEvent(&event
, true, location
, 4, 0);
88 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
89 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
90 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
91 offset
= ui::GetMouseWheelOffset(&event
);
92 EXPECT_GT(offset
.y(), 0);
93 EXPECT_EQ(0, offset
.x());
96 InitButtonEvent(&event
, true, location
, 5, 0);
97 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
98 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
99 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
100 offset
= ui::GetMouseWheelOffset(&event
);
101 EXPECT_LT(offset
.y(), 0);
102 EXPECT_EQ(0, offset
.x());
105 InitButtonEvent(&event
, true, location
, 6, 0);
106 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
107 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
108 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
109 offset
= ui::GetMouseWheelOffset(&event
);
110 EXPECT_EQ(0, offset
.y());
111 EXPECT_GT(offset
.x(), 0);
114 InitButtonEvent(&event
, true, location
, 7, 0);
115 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
116 EXPECT_EQ(0, ui::EventFlagsFromNative(&event
));
117 EXPECT_EQ(location
, ui::EventLocationFromNative(&event
));
118 offset
= ui::GetMouseWheelOffset(&event
);
119 EXPECT_EQ(0, offset
.y());
120 EXPECT_LT(offset
.x(), 0);
122 // TODO(derat): Test XInput code.
125 TEST(EventsXTest
, AvoidExtraEventsOnWheelRelease
) {
127 gfx::Point
location(5, 10);
129 InitButtonEvent(&event
, true, location
, 4, 0);
130 EXPECT_EQ(ui::ET_MOUSEWHEEL
, ui::EventTypeFromNative(&event
));
132 // We should return ET_UNKNOWN for the release event instead of returning
133 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step.
134 InitButtonEvent(&event
, false, location
, 4, 0);
135 EXPECT_EQ(ui::ET_UNKNOWN
, ui::EventTypeFromNative(&event
));
137 // TODO(derat): Test XInput code.
140 TEST(EventsXTest
, EnterLeaveEvent
) {
142 event
.xcrossing
.type
= EnterNotify
;
143 event
.xcrossing
.x
= 10;
144 event
.xcrossing
.y
= 20;
145 event
.xcrossing
.x_root
= 110;
146 event
.xcrossing
.y_root
= 120;
148 // Mouse enter events are converted to mouse move events to be consistent with
149 // the way views handle mouse enter. See comments for EnterNotify case in
150 // ui::EventTypeFromNative for more details.
151 EXPECT_EQ(ui::ET_MOUSE_MOVED
, ui::EventTypeFromNative(&event
));
152 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event
).ToString());
153 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event
).ToString());
155 event
.xcrossing
.type
= LeaveNotify
;
156 event
.xcrossing
.x
= 30;
157 event
.xcrossing
.y
= 40;
158 event
.xcrossing
.x_root
= 230;
159 event
.xcrossing
.y_root
= 240;
160 EXPECT_EQ(ui::ET_MOUSE_EXITED
, ui::EventTypeFromNative(&event
));
161 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event
).ToString());
162 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event
).ToString());
165 TEST(EventsXTest
, ClickCount
) {
167 gfx::Point
location(5, 10);
169 for (int i
= 1; i
<= 3; ++i
) {
170 InitButtonEvent(&event
, true, location
, 1, 0);
172 MouseEvent
mouseev(&event
);
173 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, mouseev
.type());
174 EXPECT_EQ(i
, mouseev
.GetClickCount());
177 InitButtonEvent(&event
, false, location
, 1, 0);
179 MouseEvent
mouseev(&event
);
180 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, mouseev
.type());
181 EXPECT_EQ(i
, mouseev
.GetClickCount());
186 #if defined(USE_XI2_MT)
187 TEST(EventsXTest
, TouchEventBasic
) {
188 std::vector
<unsigned int> devices
;
189 devices
.push_back(0);
190 ui::SetUpTouchDevicesForTest(devices
);
191 std::vector
<Valuator
> valuators
;
193 // Init touch begin with tracking id 5, touch id 0.
194 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR
, 20));
195 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION
, 0.3f
));
196 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE
, 100));
197 ui::ScopedXI2Event scoped_xevent
;
198 scoped_xevent
.InitTouchEvent(
199 0, XI_TouchBegin
, 5, gfx::Point(10, 10), valuators
);
200 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
201 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent
).ToString());
202 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
203 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
204 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.15f
);
205 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
207 // Touch update, with new orientation info.
209 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION
, 0.5f
));
210 scoped_xevent
.InitTouchEvent(
211 0, XI_TouchUpdate
, 5, gfx::Point(20, 20), valuators
);
212 EXPECT_EQ(ui::ET_TOUCH_MOVED
, ui::EventTypeFromNative(scoped_xevent
));
213 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent
).ToString());
214 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
215 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
216 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
217 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.1f
);
219 // Another touch with tracking id 6, touch id 1.
221 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR
, 100));
222 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_ORIENTATION
, 0.9f
));
223 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE
, 500));
224 scoped_xevent
.InitTouchEvent(
225 0, XI_TouchBegin
, 6, gfx::Point(200, 200), valuators
);
226 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, ui::EventTypeFromNative(scoped_xevent
));
227 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
228 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
229 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 50);
230 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
231 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
233 // Touch with tracking id 5 should have old radius/angle value and new pressue
236 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_PRESSURE
, 50));
237 scoped_xevent
.InitTouchEvent(
238 0, XI_TouchEnd
, 5, gfx::Point(30, 30), valuators
);
239 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
240 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent
).ToString());
241 EXPECT_EQ(GetTouchId(scoped_xevent
), 0);
242 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 10);
243 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.25f
);
244 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.05f
);
246 // Touch with tracking id 6 should have old angle/pressure value and new
249 valuators
.push_back(Valuator(DeviceDataManager::DT_TOUCH_MAJOR
, 50));
250 scoped_xevent
.InitTouchEvent(
251 0, XI_TouchEnd
, 6, gfx::Point(200, 200), valuators
);
252 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, ui::EventTypeFromNative(scoped_xevent
));
253 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent
).ToString());
254 EXPECT_EQ(GetTouchId(scoped_xevent
), 1);
255 EXPECT_EQ(GetTouchRadiusX(scoped_xevent
), 25);
256 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent
), 0.45f
);
257 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent
), 0.5f
);
261 TEST(EventsXTest
, NumpadKeyEvents
) {
263 Display
* display
= gfx::GetXDisplay();
268 ui::KeyboardCode ui_keycode
;
270 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional
271 // keysymdef.h numbering.
272 { true, XK_KP_Space
},
273 { true, XK_KP_Equal
},
274 // Other numpad keysyms. (This is actually exhaustive in the current list.)
276 { true, XK_KP_Enter
},
281 { true, XK_KP_Home
},
282 { true, XK_KP_Left
},
284 { true, XK_KP_Right
},
285 { true, XK_KP_Down
},
286 { true, XK_KP_Prior
},
287 { true, XK_KP_Page_Up
},
288 { true, XK_KP_Next
},
289 { true, XK_KP_Page_Down
},
291 { true, XK_KP_Begin
},
292 { true, XK_KP_Insert
},
293 { true, XK_KP_Delete
},
294 { true, XK_KP_Multiply
},
296 { true, XK_KP_Separator
},
297 { true, XK_KP_Subtract
},
298 { true, XK_KP_Decimal
},
299 { true, XK_KP_Divide
},
310 // Largest keysym preceding XK_KP_Space.
311 { false, XK_Num_Lock
},
312 // Smallest keysym following XK_KP_Equal.
314 // Non-numpad analogues of numpad keysyms.
316 { false, XK_Return
},
327 { false, XK_Page_Up
},
329 { false, XK_Page_Down
},
331 { false, XK_Insert
},
332 { false, XK_Delete
},
333 { false, XK_multiply
},
336 { false, XK_period
},
348 // Miscellaneous other keysyms.
349 { false, XK_BackSpace
},
350 { false, XK_Scroll_Lock
},
351 { false, XK_Multi_key
},
352 { false, XK_Select
},
353 { false, XK_Num_Lock
},
354 { false, XK_Shift_L
},
359 for (size_t k
= 0; k
< ARRAYSIZE_UNSAFE(keys
); ++k
) {
360 int x_keycode
= XKeysymToKeycode(display
, keys
[k
].x_keysym
);
361 // Exclude keysyms for which the server has no corresponding keycode.
363 InitKeyEvent(display
, &event
, true, x_keycode
, 0);
364 // int keysym = XLookupKeysym(&event.xkey, 0);
366 ui::KeyEvent
ui_key_event(&event
, false);
367 EXPECT_EQ(keys
[k
].is_numpad_key
? ui::EF_NUMPAD_KEY
: 0,
368 ui_key_event
.flags() & ui::EF_NUMPAD_KEY
);