Simple Cache: remove experiment code for flush intervals.
[chromium-blink-merge.git] / ui / events / x / events_x_unittest.cc
blob368682cbb1bd5fec0bb896ada960ac5c5ea5aa92
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>
7 #include <X11/extensions/XInput2.h>
8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h>
10 #include <X11/XKBlib.h>
12 // Generically-named #defines from Xlib that conflict with symbols in GTest.
13 #undef Bool
14 #undef None
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"
23 namespace ui {
25 namespace {
27 // Initializes the passed-in Xlib event.
28 void InitButtonEvent(XEvent* event,
29 bool is_press,
30 const gfx::Point& location,
31 int button,
32 int state) {
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,
47 XEvent* event,
48 bool is_press,
49 int keycode,
50 int state) {
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;
62 } // namespace
64 TEST(EventsXTest, ButtonEvents) {
65 XEvent event;
66 gfx::Point location(5, 10);
67 gfx::Vector2d offset;
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 |
77 ui::EF_SHIFT_DOWN,
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));
86 // Scroll up.
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());
95 // Scroll down.
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());
104 // Scroll left.
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);
113 // Scroll right.
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) {
126 XEvent event;
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) {
141 XEvent event;
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) {
166 XEvent event;
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.
208 valuators.clear();
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.
220 valuators.clear();
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
234 // value.
235 valuators.clear();
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
247 // radius value.
248 valuators.clear();
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);
259 #endif
261 TEST(EventsXTest, NumpadKeyEvents) {
262 XEvent event;
263 Display* display = gfx::GetXDisplay();
265 struct {
266 bool is_numpad_key;
267 int x_keysym;
268 ui::KeyboardCode ui_keycode;
269 } keys[] = {
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.)
275 { true, XK_KP_Tab },
276 { true, XK_KP_Enter },
277 { true, XK_KP_F1 },
278 { true, XK_KP_F2 },
279 { true, XK_KP_F3 },
280 { true, XK_KP_F4 },
281 { true, XK_KP_Home },
282 { true, XK_KP_Left },
283 { true, XK_KP_Up },
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 },
290 { true, XK_KP_End },
291 { true, XK_KP_Begin },
292 { true, XK_KP_Insert },
293 { true, XK_KP_Delete },
294 { true, XK_KP_Multiply },
295 { true, XK_KP_Add },
296 { true, XK_KP_Separator },
297 { true, XK_KP_Subtract },
298 { true, XK_KP_Decimal },
299 { true, XK_KP_Divide },
300 { true, XK_KP_0 },
301 { true, XK_KP_1 },
302 { true, XK_KP_2 },
303 { true, XK_KP_3 },
304 { true, XK_KP_4 },
305 { true, XK_KP_5 },
306 { true, XK_KP_6 },
307 { true, XK_KP_7 },
308 { true, XK_KP_8 },
309 { true, XK_KP_9 },
310 // Largest keysym preceding XK_KP_Space.
311 { false, XK_Num_Lock },
312 // Smallest keysym following XK_KP_Equal.
313 { false, XK_F1 },
314 // Non-numpad analogues of numpad keysyms.
315 { false, XK_Tab },
316 { false, XK_Return },
317 { false, XK_F1 },
318 { false, XK_F2 },
319 { false, XK_F3 },
320 { false, XK_F4 },
321 { false, XK_Home },
322 { false, XK_Left },
323 { false, XK_Up },
324 { false, XK_Right },
325 { false, XK_Down },
326 { false, XK_Prior },
327 { false, XK_Page_Up },
328 { false, XK_Next },
329 { false, XK_Page_Down },
330 { false, XK_End },
331 { false, XK_Insert },
332 { false, XK_Delete },
333 { false, XK_multiply },
334 { false, XK_plus },
335 { false, XK_minus },
336 { false, XK_period },
337 { false, XK_slash },
338 { false, XK_0 },
339 { false, XK_1 },
340 { false, XK_2 },
341 { false, XK_3 },
342 { false, XK_4 },
343 { false, XK_5 },
344 { false, XK_6 },
345 { false, XK_7 },
346 { false, XK_8 },
347 { false, XK_9 },
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 },
355 { false, XK_space },
356 { false, XK_A },
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.
362 if (x_keycode) {
363 InitKeyEvent(display, &event, true, x_keycode, 0);
364 // int keysym = XLookupKeysym(&event.xkey, 0);
365 // if (keysym) {
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);
373 } // namespace ui