base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / events / x / events_x_unittest.cc
blob065e804e39b6a71d5d35116a4bc34bffe998c630
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>
6 #include <set>
8 #include <X11/extensions/XInput2.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
11 #include <X11/XKBlib.h>
13 // Generically-named #defines from Xlib that conflict with symbols in GTest.
14 #undef Bool
15 #undef None
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"
28 namespace ui {
30 namespace {
32 // Initializes the passed-in Xlib event.
33 void InitButtonEvent(XEvent* event,
34 bool is_press,
35 const gfx::Point& location,
36 int button,
37 int state) {
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,
52 XEvent* event,
53 bool is_press,
54 int keycode,
55 int state) {
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) {
70 XEvent event;
71 int x_keycode = XKeysymToKeycode(display, x_keysym);
72 // Exclude keysyms for which the server has no corresponding keycode.
73 if (x_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);
78 return true;
81 } // namespace
83 class EventsXTest : public testing::Test {
84 public:
85 EventsXTest() {}
86 ~EventsXTest() override {}
88 void SetUp() override {
89 DeviceDataManagerX11::CreateInstance();
90 ui::TouchFactory::GetInstance()->ResetForTest();
92 private:
93 DISALLOW_COPY_AND_ASSIGN(EventsXTest);
96 TEST_F(EventsXTest, ButtonEvents) {
97 XEvent event;
98 gfx::Point location(5, 10);
99 gfx::Vector2d offset;
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 |
109 ui::EF_SHIFT_DOWN,
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));
118 // Scroll up.
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());
127 // Scroll down.
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());
136 // Scroll left.
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);
145 // Scroll right.
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) {
158 XEvent event;
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) {
173 XEvent event;
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) {
198 XEvent event;
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));
226 valuators.push_back(
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.
240 valuators.clear();
241 valuators.push_back(
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.
253 valuators.clear();
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
268 // value.
269 valuators.clear();
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
281 // radius value.
282 valuators.clear();
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) {
295 int slot = 0;
296 bool success =
297 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id, &slot);
298 if (success)
299 return slot;
300 return -1;
303 TEST_F(EventsXTest, TouchEventIdRefcounting) {
304 std::vector<unsigned int> devices;
305 devices.push_back(0);
306 ui::SetUpTouchDevicesForTest(devices);
307 std::vector<Valuator> valuators;
309 const int kTrackingId0 = 5;
310 const int kTrackingId1 = 7;
312 // Increment ref count once for first touch.
313 ui::ScopedXI2Event xpress0;
314 xpress0.InitTouchEvent(
315 0, XI_TouchBegin, kTrackingId0, gfx::Point(10, 10), valuators);
316 scoped_ptr<ui::TouchEvent> upress0(new ui::TouchEvent(xpress0));
317 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId0));
319 // Increment ref count 4 times for second touch.
320 ui::ScopedXI2Event xpress1;
321 xpress1.InitTouchEvent(
322 0, XI_TouchBegin, kTrackingId1, gfx::Point(20, 20), valuators);
324 for (int i = 0; i < 4; ++i) {
325 ui::TouchEvent upress1(xpress1);
326 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1));
329 ui::ScopedXI2Event xrelease1;
330 xrelease1.InitTouchEvent(
331 0, XI_TouchEnd, kTrackingId1, gfx::Point(10, 10), valuators);
333 // Decrement ref count 3 times for second touch.
334 for (int i = 0; i < 3; ++i) {
335 ui::TouchEvent urelease1(xrelease1);
336 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1));
339 // This should clear the touch id of the second touch.
340 scoped_ptr<ui::TouchEvent> urelease1(new ui::TouchEvent(xrelease1));
341 urelease1.reset();
342 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId1));
344 // This should clear the touch id of the first touch.
345 ui::ScopedXI2Event xrelease0;
346 xrelease0.InitTouchEvent(
347 0, XI_TouchEnd, kTrackingId0, gfx::Point(10, 10), valuators);
348 scoped_ptr<ui::TouchEvent> urelease0(new ui::TouchEvent(xrelease0));
349 urelease0.reset();
350 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId0));
353 TEST_F(EventsXTest, NumpadKeyEvents) {
354 XEvent event;
355 Display* display = gfx::GetXDisplay();
357 struct {
358 bool is_numpad_key;
359 int x_keysym;
360 } keys[] = {
361 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional
362 // keysymdef.h numbering.
363 { true, XK_KP_Space },
364 { true, XK_KP_Equal },
365 // Other numpad keysyms. (This is actually exhaustive in the current list.)
366 { true, XK_KP_Tab },
367 { true, XK_KP_Enter },
368 { true, XK_KP_F1 },
369 { true, XK_KP_F2 },
370 { true, XK_KP_F3 },
371 { true, XK_KP_F4 },
372 { true, XK_KP_Home },
373 { true, XK_KP_Left },
374 { true, XK_KP_Up },
375 { true, XK_KP_Right },
376 { true, XK_KP_Down },
377 { true, XK_KP_Prior },
378 { true, XK_KP_Page_Up },
379 { true, XK_KP_Next },
380 { true, XK_KP_Page_Down },
381 { true, XK_KP_End },
382 { true, XK_KP_Begin },
383 { true, XK_KP_Insert },
384 { true, XK_KP_Delete },
385 { true, XK_KP_Multiply },
386 { true, XK_KP_Add },
387 { true, XK_KP_Separator },
388 { true, XK_KP_Subtract },
389 { true, XK_KP_Decimal },
390 { true, XK_KP_Divide },
391 { true, XK_KP_0 },
392 { true, XK_KP_1 },
393 { true, XK_KP_2 },
394 { true, XK_KP_3 },
395 { true, XK_KP_4 },
396 { true, XK_KP_5 },
397 { true, XK_KP_6 },
398 { true, XK_KP_7 },
399 { true, XK_KP_8 },
400 { true, XK_KP_9 },
401 // Largest keysym preceding XK_KP_Space.
402 { false, XK_Num_Lock },
403 // Smallest keysym following XK_KP_Equal.
404 { false, XK_F1 },
405 // Non-numpad analogues of numpad keysyms.
406 { false, XK_Tab },
407 { false, XK_Return },
408 { false, XK_F1 },
409 { false, XK_F2 },
410 { false, XK_F3 },
411 { false, XK_F4 },
412 { false, XK_Home },
413 { false, XK_Left },
414 { false, XK_Up },
415 { false, XK_Right },
416 { false, XK_Down },
417 { false, XK_Prior },
418 { false, XK_Page_Up },
419 { false, XK_Next },
420 { false, XK_Page_Down },
421 { false, XK_End },
422 { false, XK_Insert },
423 { false, XK_Delete },
424 { false, XK_multiply },
425 { false, XK_plus },
426 { false, XK_minus },
427 { false, XK_period },
428 { false, XK_slash },
429 { false, XK_0 },
430 { false, XK_1 },
431 { false, XK_2 },
432 { false, XK_3 },
433 { false, XK_4 },
434 { false, XK_5 },
435 { false, XK_6 },
436 { false, XK_7 },
437 { false, XK_8 },
438 { false, XK_9 },
439 // Miscellaneous other keysyms.
440 { false, XK_BackSpace },
441 { false, XK_Scroll_Lock },
442 { false, XK_Multi_key },
443 { false, XK_Select },
444 { false, XK_Num_Lock },
445 { false, XK_Shift_L },
446 { false, XK_space },
447 { false, XK_A },
450 for (size_t k = 0; k < arraysize(keys); ++k) {
451 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym);
452 // Exclude keysyms for which the server has no corresponding keycode.
453 if (x_keycode) {
454 InitKeyEvent(display, &event, true, x_keycode, 0);
455 // int keysym = XLookupKeysym(&event.xkey, 0);
456 // if (keysym) {
457 ui::KeyEvent ui_key_event(&event);
458 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0,
459 ui_key_event.flags() & ui::EF_NUMPAD_KEY);
464 TEST_F(EventsXTest, FunctionKeyEvents) {
465 Display* display = gfx::GetXDisplay();
467 // Min function key code minus 1.
468 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F1 - 1));
469 // All function keys.
470 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F1));
471 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F2));
472 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F3));
473 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F4));
474 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F5));
475 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F6));
476 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F7));
477 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F8));
478 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F9));
479 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F10));
480 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F11));
481 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F12));
482 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F13));
483 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F14));
484 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F15));
485 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F16));
486 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F17));
487 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F18));
488 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F19));
489 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F20));
490 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F21));
491 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F22));
492 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F23));
493 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F24));
494 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F25));
495 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F26));
496 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F27));
497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F28));
498 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F29));
499 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F30));
500 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F31));
501 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F32));
502 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F33));
503 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F34));
504 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F35));
505 // Max function key code plus 1.
506 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F35 + 1));
509 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but
510 // that an exception list of keys can still be processed.
511 TEST_F(EventsXTest, DisableKeyboard) {
512 DeviceDataManagerX11* device_data_manager =
513 static_cast<DeviceDataManagerX11*>(
514 DeviceDataManager::GetInstance());
515 unsigned int blocked_device_id = 1;
516 unsigned int other_device_id = 2;
517 unsigned int master_device_id = 3;
518 device_data_manager->DisableDevice(blocked_device_id);
520 scoped_ptr<std::set<KeyboardCode> > excepted_keys(new std::set<KeyboardCode>);
521 excepted_keys->insert(VKEY_B);
522 device_data_manager->SetDisabledKeyboardAllowedKeys(excepted_keys.Pass());
524 ScopedXI2Event xev;
525 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN.
526 xev.InitGenericKeyEvent(master_device_id,
527 blocked_device_id,
528 ui::ET_KEY_PRESSED,
529 ui::VKEY_A,
531 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev));
533 // The B key is allowed as an exception, and should return KEY_PRESSED.
534 xev.InitGenericKeyEvent(master_device_id,
535 blocked_device_id,
536 ui::ET_KEY_PRESSED,
537 ui::VKEY_B,
539 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
541 // Both A and B are allowed on an unblocked keyboard device.
542 xev.InitGenericKeyEvent(master_device_id,
543 other_device_id,
544 ui::ET_KEY_PRESSED,
545 ui::VKEY_A,
547 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
548 xev.InitGenericKeyEvent(master_device_id,
549 other_device_id,
550 ui::ET_KEY_PRESSED,
551 ui::VKEY_B,
553 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
555 device_data_manager->EnableDevice(blocked_device_id);
556 device_data_manager->SetDisabledKeyboardAllowedKeys(
557 scoped_ptr<std::set<KeyboardCode> >());
559 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled.
560 xev.InitGenericKeyEvent(master_device_id,
561 blocked_device_id,
562 ui::ET_KEY_PRESSED,
563 ui::VKEY_A,
565 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev));
568 // Verifies that the type of events from a disabled mouse is ET_UNKNOWN.
569 TEST_F(EventsXTest, DisableMouse) {
570 DeviceDataManagerX11* device_data_manager =
571 static_cast<DeviceDataManagerX11*>(
572 DeviceDataManager::GetInstance());
573 unsigned int blocked_device_id = 1;
574 unsigned int other_device_id = 2;
575 std::vector<unsigned int> device_list;
576 device_list.push_back(blocked_device_id);
577 device_list.push_back(other_device_id);
578 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list);
580 device_data_manager->DisableDevice(blocked_device_id);
582 ScopedXI2Event xev;
583 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(),
584 EF_LEFT_MOUSE_BUTTON);
585 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev));
587 xev.InitGenericButtonEvent(other_device_id, ET_MOUSE_PRESSED, gfx::Point(),
588 EF_LEFT_MOUSE_BUTTON);
589 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev));
591 device_data_manager->EnableDevice(blocked_device_id);
593 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(),
594 EF_LEFT_MOUSE_BUTTON);
595 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev));
598 #if !defined(OS_CHROMEOS)
599 TEST_F(EventsXTest, ImeFabricatedKeyEvents) {
600 Display* display = gfx::GetXDisplay();
602 unsigned int state_to_be_fabricated[] = {
603 0, ShiftMask, LockMask, ShiftMask | LockMask,
605 for (size_t i = 0; i < arraysize(state_to_be_fabricated); ++i) {
606 unsigned int state = state_to_be_fabricated[i];
607 for (int is_char = 0; is_char < 2; ++is_char) {
608 XEvent x_event;
609 InitKeyEvent(display, &x_event, true, 0, state);
610 ui::KeyEvent key_event(&x_event);
611 if (is_char) {
612 KeyEventTestApi test_event(&key_event);
613 test_event.set_is_char(true);
615 EXPECT_TRUE(key_event.flags() & ui::EF_IME_FABRICATED_KEY);
619 unsigned int state_to_be_not_fabricated[] = {
620 ControlMask, Mod1Mask, Mod2Mask, ShiftMask | ControlMask,
622 for (size_t i = 0; i < arraysize(state_to_be_not_fabricated); ++i) {
623 unsigned int state = state_to_be_not_fabricated[i];
624 for (int is_char = 0; is_char < 2; ++is_char) {
625 XEvent x_event;
626 InitKeyEvent(display, &x_event, true, 0, state);
627 ui::KeyEvent key_event(&x_event);
628 if (is_char) {
629 KeyEventTestApi test_event(&key_event);
630 test_event.set_is_char(true);
632 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY);
636 #endif
638 } // namespace ui