OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <cstring> | |
6 #include <set> | |
7 | |
8 #include <X11/extensions/XInput2.h> | |
9 #include <X11/XKBlib.h> | |
10 #include <X11/Xlib.h> | |
11 #include <X11/Xutil.h> | |
12 | |
13 // Generically-named #defines from Xlib that conflict with symbols in GTest. | |
14 #undef Bool | |
15 #undef None | |
16 | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "ui/events/event.h" | |
20 #include "ui/events/event_constants.h" | |
21 #include "ui/events/event_utils.h" | |
22 #include "ui/events/platform/platform_event_builder.h" | |
23 #include "ui/events/platform/platform_event_utils.h" | |
24 #include "ui/events/platform/x11/device_data_manager_x11.h" | |
25 #include "ui/events/platform/x11/touch_factory_x11.h" | |
26 #include "ui/events/test/events_test_utils.h" | |
27 #include "ui/events/test/events_test_utils_x11.h" | |
28 #include "ui/gfx/point.h" | |
29 | |
30 namespace ui { | |
31 | |
32 namespace { | |
33 | |
34 // Initializes the passed-in Xlib event. | |
35 void InitButtonEvent(XEvent* event, | |
36 bool is_press, | |
37 const gfx::Point& location, | |
38 int button, | |
39 int state) { | |
40 memset(event, 0, sizeof(*event)); | |
41 | |
42 // We don't bother setting fields that the event code doesn't use, such as | |
43 // x_root/y_root and window/root/subwindow. | |
44 XButtonEvent* button_event = &(event->xbutton); | |
45 button_event->type = is_press ? ButtonPress : ButtonRelease; | |
46 button_event->x = location.x(); | |
47 button_event->y = location.y(); | |
48 button_event->button = button; | |
49 button_event->state = state; | |
50 } | |
51 | |
52 // Initializes the passed-in Xlib event. | |
53 void InitKeyEvent(Display* display, | |
54 XEvent* event, | |
55 bool is_press, | |
56 int keycode, | |
57 int state) { | |
58 memset(event, 0, sizeof(*event)); | |
59 | |
60 // We don't bother setting fields that the event code doesn't use, such as | |
61 // x_root/y_root and window/root/subwindow. | |
62 XKeyEvent* key_event = &(event->xkey); | |
63 key_event->display = display; | |
64 key_event->type = is_press ? KeyPress : KeyRelease; | |
65 key_event->keycode = keycode; | |
66 key_event->state = state; | |
67 } | |
68 | |
69 // Returns true if the keysym maps to a KeyEvent with the EF_FUNCTION_KEY | |
70 // flag set, or the keysym maps to a zero key code. | |
71 bool HasFunctionKeyFlagSetIfSupported(Display* display, int x_keysym) { | |
72 XEvent event; | |
73 int x_keycode = XKeysymToKeycode(display, x_keysym); | |
74 // Exclude keysyms for which the server has no corresponding keycode. | |
75 if (x_keycode) { | |
76 InitKeyEvent(display, &event, true, x_keycode, 0); | |
77 ui::KeyEvent ui_key_event = PlatformEventBuilder::BuildKeyEvent(&event); | |
78 return (ui_key_event.flags() & ui::EF_FUNCTION_KEY); | |
79 } | |
80 return true; | |
81 } | |
82 | |
83 } // namespace | |
84 | |
85 class PlatformEventUtilsXTest : public testing::Test { | |
86 public: | |
87 PlatformEventUtilsXTest() {} | |
88 ~PlatformEventUtilsXTest() override {} | |
89 | |
90 void SetUp() override { | |
91 DeviceDataManagerX11::CreateInstance(); | |
92 ui::TouchFactory::GetInstance()->ResetForTest(); | |
93 } | |
94 | |
95 private: | |
96 DISALLOW_COPY_AND_ASSIGN(PlatformEventUtilsXTest); | |
97 }; | |
98 | |
99 TEST_F(PlatformEventUtilsXTest, ButtonEvents) { | |
100 XEvent event; | |
101 gfx::Point location(5, 10); | |
102 gfx::Vector2d offset; | |
103 | |
104 InitButtonEvent(&event, true, location, 1, 0); | |
105 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); | |
106 EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); | |
107 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
108 | |
109 InitButtonEvent(&event, true, location, 2, Button1Mask | ShiftMask); | |
110 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(&event)); | |
111 EXPECT_EQ( | |
112 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_SHIFT_DOWN, | |
113 ui::EventFlagsFromNative(&event)); | |
114 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
115 | |
116 InitButtonEvent(&event, false, location, 3, 0); | |
117 EXPECT_EQ(ui::ET_MOUSE_RELEASED, ui::EventTypeFromNative(&event)); | |
118 EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, ui::EventFlagsFromNative(&event)); | |
119 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
120 | |
121 // Scroll up. | |
122 InitButtonEvent(&event, true, location, 4, 0); | |
123 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); | |
124 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); | |
125 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
126 offset = ui::GetMouseWheelOffset(&event); | |
127 EXPECT_GT(offset.y(), 0); | |
128 EXPECT_EQ(0, offset.x()); | |
129 | |
130 // Scroll down. | |
131 InitButtonEvent(&event, true, location, 5, 0); | |
132 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); | |
133 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); | |
134 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
135 offset = ui::GetMouseWheelOffset(&event); | |
136 EXPECT_LT(offset.y(), 0); | |
137 EXPECT_EQ(0, offset.x()); | |
138 | |
139 // Scroll left. | |
140 InitButtonEvent(&event, true, location, 6, 0); | |
141 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); | |
142 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); | |
143 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
144 offset = ui::GetMouseWheelOffset(&event); | |
145 EXPECT_EQ(0, offset.y()); | |
146 EXPECT_GT(offset.x(), 0); | |
147 | |
148 // Scroll right. | |
149 InitButtonEvent(&event, true, location, 7, 0); | |
150 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); | |
151 EXPECT_EQ(0, ui::EventFlagsFromNative(&event)); | |
152 EXPECT_EQ(location, ui::EventLocationFromNative(&event)); | |
153 offset = ui::GetMouseWheelOffset(&event); | |
154 EXPECT_EQ(0, offset.y()); | |
155 EXPECT_LT(offset.x(), 0); | |
156 | |
157 // TODO(derat): Test XInput code. | |
158 } | |
159 | |
160 TEST_F(PlatformEventUtilsXTest, AvoidExtraEventsOnWheelRelease) { | |
161 XEvent event; | |
162 gfx::Point location(5, 10); | |
163 | |
164 InitButtonEvent(&event, true, location, 4, 0); | |
165 EXPECT_EQ(ui::ET_MOUSEWHEEL, ui::EventTypeFromNative(&event)); | |
166 | |
167 // We should return ET_UNKNOWN for the release event instead of returning | |
168 // ET_MOUSEWHEEL; otherwise we'll scroll twice for each scrollwheel step. | |
169 InitButtonEvent(&event, false, location, 4, 0); | |
170 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(&event)); | |
171 | |
172 // TODO(derat): Test XInput code. | |
173 } | |
174 | |
175 TEST_F(PlatformEventUtilsXTest, EnterLeaveEvent) { | |
176 XEvent event; | |
177 event.xcrossing.type = EnterNotify; | |
178 event.xcrossing.x = 10; | |
179 event.xcrossing.y = 20; | |
180 event.xcrossing.x_root = 110; | |
181 event.xcrossing.y_root = 120; | |
182 | |
183 // Mouse enter events are converted to mouse move events to be consistent with | |
184 // the way views handle mouse enter. See comments for EnterNotify case in | |
185 // ui::EventTypeFromNative for more details. | |
186 EXPECT_EQ(ui::ET_MOUSE_MOVED, ui::EventTypeFromNative(&event)); | |
187 EXPECT_EQ("10,20", ui::EventLocationFromNative(&event).ToString()); | |
188 EXPECT_EQ("110,120", ui::EventSystemLocationFromNative(&event).ToString()); | |
189 | |
190 event.xcrossing.type = LeaveNotify; | |
191 event.xcrossing.x = 30; | |
192 event.xcrossing.y = 40; | |
193 event.xcrossing.x_root = 230; | |
194 event.xcrossing.y_root = 240; | |
195 EXPECT_EQ(ui::ET_MOUSE_EXITED, ui::EventTypeFromNative(&event)); | |
196 EXPECT_EQ("30,40", ui::EventLocationFromNative(&event).ToString()); | |
197 EXPECT_EQ("230,240", ui::EventSystemLocationFromNative(&event).ToString()); | |
198 } | |
199 | |
200 TEST_F(PlatformEventUtilsXTest, ClickCount) { | |
201 XEvent event; | |
202 gfx::Point location(5, 10); | |
203 | |
204 for (int i = 1; i <= 3; ++i) { | |
205 InitButtonEvent(&event, true, location, 1, 0); | |
206 { | |
207 MouseEvent mouseev = PlatformEventBuilder::BuildMouseEvent(&event); | |
208 EXPECT_EQ(ui::ET_MOUSE_PRESSED, mouseev.type()); | |
209 EXPECT_EQ(i, mouseev.GetClickCount()); | |
210 } | |
211 | |
212 InitButtonEvent(&event, false, location, 1, 0); | |
213 { | |
214 MouseEvent mouseev = PlatformEventBuilder::BuildMouseEvent(&event); | |
215 EXPECT_EQ(ui::ET_MOUSE_RELEASED, mouseev.type()); | |
216 EXPECT_EQ(i, mouseev.GetClickCount()); | |
217 } | |
218 } | |
219 } | |
220 | |
221 #if defined(USE_XI2_MT) | |
222 TEST_F(PlatformEventUtilsXTest, TouchEventBasic) { | |
223 std::vector<unsigned int> devices; | |
224 devices.push_back(0); | |
225 ui::SetUpTouchDevicesForTest(devices); | |
226 std::vector<Valuator> valuators; | |
227 | |
228 // Init touch begin with tracking id 5, touch id 0. | |
229 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 20)); | |
230 valuators.push_back( | |
231 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.3f)); | |
232 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 100)); | |
233 ui::ScopedXI2Event scoped_xevent; | |
234 scoped_xevent.InitTouchEvent(0, XI_TouchBegin, 5, gfx::Point(10, 10), | |
235 valuators); | |
236 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); | |
237 EXPECT_EQ("10,10", ui::EventLocationFromNative(scoped_xevent).ToString()); | |
238 EXPECT_EQ(GetTouchId(scoped_xevent), 0); | |
239 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); | |
240 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.15f); | |
241 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); | |
242 | |
243 // Touch update, with new orientation info. | |
244 valuators.clear(); | |
245 valuators.push_back( | |
246 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.5f)); | |
247 scoped_xevent.InitTouchEvent(0, XI_TouchUpdate, 5, gfx::Point(20, 20), | |
248 valuators); | |
249 EXPECT_EQ(ui::ET_TOUCH_MOVED, ui::EventTypeFromNative(scoped_xevent)); | |
250 EXPECT_EQ("20,20", ui::EventLocationFromNative(scoped_xevent).ToString()); | |
251 EXPECT_EQ(GetTouchId(scoped_xevent), 0); | |
252 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); | |
253 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); | |
254 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.1f); | |
255 | |
256 // Another touch with tracking id 6, touch id 1. | |
257 valuators.clear(); | |
258 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 100)); | |
259 valuators.push_back( | |
260 Valuator(DeviceDataManagerX11::DT_TOUCH_ORIENTATION, 0.9f)); | |
261 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 500)); | |
262 scoped_xevent.InitTouchEvent(0, XI_TouchBegin, 6, gfx::Point(200, 200), | |
263 valuators); | |
264 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ui::EventTypeFromNative(scoped_xevent)); | |
265 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); | |
266 EXPECT_EQ(GetTouchId(scoped_xevent), 1); | |
267 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 50); | |
268 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); | |
269 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); | |
270 | |
271 // Touch with tracking id 5 should have old radius/angle value and new pressue | |
272 // value. | |
273 valuators.clear(); | |
274 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_PRESSURE, 50)); | |
275 scoped_xevent.InitTouchEvent(0, XI_TouchEnd, 5, gfx::Point(30, 30), | |
276 valuators); | |
277 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); | |
278 EXPECT_EQ("30,30", ui::EventLocationFromNative(scoped_xevent).ToString()); | |
279 EXPECT_EQ(GetTouchId(scoped_xevent), 0); | |
280 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 10); | |
281 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.25f); | |
282 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.05f); | |
283 | |
284 // Touch with tracking id 6 should have old angle/pressure value and new | |
285 // radius value. | |
286 valuators.clear(); | |
287 valuators.push_back(Valuator(DeviceDataManagerX11::DT_TOUCH_MAJOR, 50)); | |
288 scoped_xevent.InitTouchEvent(0, XI_TouchEnd, 6, gfx::Point(200, 200), | |
289 valuators); | |
290 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ui::EventTypeFromNative(scoped_xevent)); | |
291 EXPECT_EQ("200,200", ui::EventLocationFromNative(scoped_xevent).ToString()); | |
292 EXPECT_EQ(GetTouchId(scoped_xevent), 1); | |
293 EXPECT_EQ(GetTouchRadiusX(scoped_xevent), 25); | |
294 EXPECT_FLOAT_EQ(GetTouchAngle(scoped_xevent), 0.45f); | |
295 EXPECT_FLOAT_EQ(GetTouchForce(scoped_xevent), 0.5f); | |
296 } | |
297 | |
298 int GetTouchIdForTrackingId(uint32 tracking_id) { | |
299 int slot = 0; | |
300 bool success = | |
301 TouchFactory::GetInstance()->QuerySlotForTrackingID(tracking_id, &slot); | |
302 if (success) | |
303 return slot; | |
304 return -1; | |
305 } | |
306 | |
307 TEST_F(PlatformEventUtilsXTest, TouchEventIdRefcounting) { | |
308 std::vector<unsigned int> devices; | |
309 devices.push_back(0); | |
310 ui::SetUpTouchDevicesForTest(devices); | |
311 std::vector<Valuator> valuators; | |
312 | |
313 const int kTrackingId0 = 5; | |
314 const int kTrackingId1 = 7; | |
315 | |
316 // Increment ref count once for first touch. | |
317 ui::ScopedXI2Event xpress0; | |
318 xpress0.InitTouchEvent(0, XI_TouchBegin, kTrackingId0, gfx::Point(10, 10), | |
319 valuators); | |
320 scoped_ptr<ui::TouchEvent> upress0(new ui::TouchEvent(xpress0)); | |
321 EXPECT_EQ(0, GetTouchIdForTrackingId(kTrackingId0)); | |
322 | |
323 // Increment ref count 4 times for second touch. | |
324 ui::ScopedXI2Event xpress1; | |
325 xpress1.InitTouchEvent(0, XI_TouchBegin, kTrackingId1, gfx::Point(20, 20), | |
326 valuators); | |
327 | |
328 for (int i = 0; i < 4; ++i) { | |
329 ui::TouchEvent upress1(xpress1); | |
330 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1)); | |
331 } | |
332 | |
333 ui::ScopedXI2Event xrelease1; | |
334 xrelease1.InitTouchEvent(0, XI_TouchEnd, kTrackingId1, gfx::Point(10, 10), | |
335 valuators); | |
336 | |
337 // Decrement ref count 3 times for second touch. | |
338 for (int i = 0; i < 3; ++i) { | |
339 ui::TouchEvent urelease1(xrelease1); | |
340 EXPECT_EQ(1, GetTouchIdForTrackingId(kTrackingId1)); | |
341 } | |
342 | |
343 // This should clear the touch id of the second touch. | |
344 scoped_ptr<ui::TouchEvent> urelease1(new ui::TouchEvent(xrelease1)); | |
345 urelease1.reset(); | |
346 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId1)); | |
347 | |
348 // This should clear the touch id of the first touch. | |
349 ui::ScopedXI2Event xrelease0; | |
350 xrelease0.InitTouchEvent(0, XI_TouchEnd, kTrackingId0, gfx::Point(10, 10), | |
351 valuators); | |
352 scoped_ptr<ui::TouchEvent> urelease0(new ui::TouchEvent(xrelease0)); | |
353 urelease0.reset(); | |
354 EXPECT_EQ(-1, GetTouchIdForTrackingId(kTrackingId0)); | |
355 } | |
356 #endif | |
357 | |
358 TEST_F(PlatformEventUtilsXTest, NumpadKeyEvents) { | |
359 XEvent event; | |
360 Display* display = gfx::GetXDisplay(); | |
361 | |
362 struct { | |
363 bool is_numpad_key; | |
364 int x_keysym; | |
365 } keys[] = { | |
366 // XK_KP_Space and XK_KP_Equal are the extrema in the conventional | |
367 // keysymdef.h numbering. | |
368 {true, XK_KP_Space}, | |
369 {true, XK_KP_Equal}, | |
370 // Other numpad keysyms. (This is actually exhaustive in the current | |
371 // list.) | |
372 {true, XK_KP_Tab}, | |
373 {true, XK_KP_Enter}, | |
374 {true, XK_KP_F1}, | |
375 {true, XK_KP_F2}, | |
376 {true, XK_KP_F3}, | |
377 {true, XK_KP_F4}, | |
378 {true, XK_KP_Home}, | |
379 {true, XK_KP_Left}, | |
380 {true, XK_KP_Up}, | |
381 {true, XK_KP_Right}, | |
382 {true, XK_KP_Down}, | |
383 {true, XK_KP_Prior}, | |
384 {true, XK_KP_Page_Up}, | |
385 {true, XK_KP_Next}, | |
386 {true, XK_KP_Page_Down}, | |
387 {true, XK_KP_End}, | |
388 {true, XK_KP_Begin}, | |
389 {true, XK_KP_Insert}, | |
390 {true, XK_KP_Delete}, | |
391 {true, XK_KP_Multiply}, | |
392 {true, XK_KP_Add}, | |
393 {true, XK_KP_Separator}, | |
394 {true, XK_KP_Subtract}, | |
395 {true, XK_KP_Decimal}, | |
396 {true, XK_KP_Divide}, | |
397 {true, XK_KP_0}, | |
398 {true, XK_KP_1}, | |
399 {true, XK_KP_2}, | |
400 {true, XK_KP_3}, | |
401 {true, XK_KP_4}, | |
402 {true, XK_KP_5}, | |
403 {true, XK_KP_6}, | |
404 {true, XK_KP_7}, | |
405 {true, XK_KP_8}, | |
406 {true, XK_KP_9}, | |
407 // Largest keysym preceding XK_KP_Space. | |
408 {false, XK_Num_Lock}, | |
409 // Smallest keysym following XK_KP_Equal. | |
410 {false, XK_F1}, | |
411 // Non-numpad analogues of numpad keysyms. | |
412 {false, XK_Tab}, | |
413 {false, XK_Return}, | |
414 {false, XK_F1}, | |
415 {false, XK_F2}, | |
416 {false, XK_F3}, | |
417 {false, XK_F4}, | |
418 {false, XK_Home}, | |
419 {false, XK_Left}, | |
420 {false, XK_Up}, | |
421 {false, XK_Right}, | |
422 {false, XK_Down}, | |
423 {false, XK_Prior}, | |
424 {false, XK_Page_Up}, | |
425 {false, XK_Next}, | |
426 {false, XK_Page_Down}, | |
427 {false, XK_End}, | |
428 {false, XK_Insert}, | |
429 {false, XK_Delete}, | |
430 {false, XK_multiply}, | |
431 {false, XK_plus}, | |
432 {false, XK_minus}, | |
433 {false, XK_period}, | |
434 {false, XK_slash}, | |
435 {false, XK_0}, | |
436 {false, XK_1}, | |
437 {false, XK_2}, | |
438 {false, XK_3}, | |
439 {false, XK_4}, | |
440 {false, XK_5}, | |
441 {false, XK_6}, | |
442 {false, XK_7}, | |
443 {false, XK_8}, | |
444 {false, XK_9}, | |
445 // Miscellaneous other keysyms. | |
446 {false, XK_BackSpace}, | |
447 {false, XK_Scroll_Lock}, | |
448 {false, XK_Multi_key}, | |
449 {false, XK_Select}, | |
450 {false, XK_Num_Lock}, | |
451 {false, XK_Shift_L}, | |
452 {false, XK_space}, | |
453 {false, XK_A}, | |
454 }; | |
455 | |
456 for (size_t k = 0; k < arraysize(keys); ++k) { | |
457 int x_keycode = XKeysymToKeycode(display, keys[k].x_keysym); | |
458 // Exclude keysyms for which the server has no corresponding keycode. | |
459 if (x_keycode) { | |
460 InitKeyEvent(display, &event, true, x_keycode, 0); | |
461 // int keysym = XLookupKeysym(&event.xkey, 0); | |
462 // if (keysym) { | |
463 ui::KeyEvent ui_key_event = PlatformEventBuilder::BuildKeyEvent(&event); | |
464 EXPECT_EQ(keys[k].is_numpad_key ? ui::EF_NUMPAD_KEY : 0, | |
465 ui_key_event.flags() & ui::EF_NUMPAD_KEY); | |
466 } | |
467 } | |
468 } | |
469 | |
470 TEST_F(PlatformEventUtilsXTest, FunctionKeyEvents) { | |
471 Display* display = gfx::GetXDisplay(); | |
472 | |
473 // Min function key code minus 1. | |
474 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F1 - 1)); | |
475 // All function keys. | |
476 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F1)); | |
477 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F2)); | |
478 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F3)); | |
479 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F4)); | |
480 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F5)); | |
481 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F6)); | |
482 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F7)); | |
483 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F8)); | |
484 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F9)); | |
485 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F10)); | |
486 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F11)); | |
487 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F12)); | |
488 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F13)); | |
489 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F14)); | |
490 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F15)); | |
491 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F16)); | |
492 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F17)); | |
493 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F18)); | |
494 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F19)); | |
495 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F20)); | |
496 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F21)); | |
497 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F22)); | |
498 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F23)); | |
499 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F24)); | |
500 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F25)); | |
501 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F26)); | |
502 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F27)); | |
503 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F28)); | |
504 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F29)); | |
505 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F30)); | |
506 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F31)); | |
507 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F32)); | |
508 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F33)); | |
509 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F34)); | |
510 EXPECT_TRUE(HasFunctionKeyFlagSetIfSupported(display, XK_F35)); | |
511 // Max function key code plus 1. | |
512 EXPECT_FALSE(HasFunctionKeyFlagSetIfSupported(display, XK_F35 + 1)); | |
513 } | |
514 | |
515 #if defined(USE_XI2_MT) | |
516 // Verifies that the type of events from a disabled keyboard is ET_UNKNOWN, but | |
517 // that an exception list of keys can still be processed. | |
518 TEST_F(PlatformEventUtilsXTest, DisableKeyboard) { | |
519 DeviceDataManagerX11* device_data_manager = | |
520 static_cast<DeviceDataManagerX11*>(DeviceDataManager::GetInstance()); | |
521 unsigned int blocked_device_id = 1; | |
522 unsigned int other_device_id = 2; | |
523 unsigned int master_device_id = 3; | |
524 device_data_manager->DisableDevice(blocked_device_id); | |
525 | |
526 scoped_ptr<std::set<KeyboardCode>> excepted_keys(new std::set<KeyboardCode>); | |
527 excepted_keys->insert(VKEY_B); | |
528 device_data_manager->SetDisabledKeyboardAllowedKeys(excepted_keys.Pass()); | |
529 | |
530 ScopedXI2Event xev; | |
531 // A is not allowed on the blocked keyboard, and should return ET_UNKNOWN. | |
532 xev.InitGenericKeyEvent(master_device_id, blocked_device_id, | |
533 ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | |
534 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev)); | |
535 | |
536 // The B key is allowed as an exception, and should return KEY_PRESSED. | |
537 xev.InitGenericKeyEvent(master_device_id, blocked_device_id, | |
538 ui::ET_KEY_PRESSED, ui::VKEY_B, 0); | |
539 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); | |
540 | |
541 // Both A and B are allowed on an unblocked keyboard device. | |
542 xev.InitGenericKeyEvent(master_device_id, other_device_id, ui::ET_KEY_PRESSED, | |
543 ui::VKEY_A, 0); | |
544 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); | |
545 xev.InitGenericKeyEvent(master_device_id, other_device_id, ui::ET_KEY_PRESSED, | |
546 ui::VKEY_B, 0); | |
547 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); | |
548 | |
549 device_data_manager->EnableDevice(blocked_device_id); | |
550 device_data_manager->SetDisabledKeyboardAllowedKeys( | |
551 scoped_ptr<std::set<KeyboardCode>>()); | |
552 | |
553 // A key returns KEY_PRESSED as per usual now that keyboard was re-enabled. | |
554 xev.InitGenericKeyEvent(master_device_id, blocked_device_id, | |
555 ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | |
556 EXPECT_EQ(ui::ET_KEY_PRESSED, ui::EventTypeFromNative(xev)); | |
557 } | |
558 | |
559 // Verifies that the type of events from a disabled mouse is ET_UNKNOWN. | |
560 TEST_F(PlatformEventUtilsXTest, DisableMouse) { | |
561 DeviceDataManagerX11* device_data_manager = | |
562 static_cast<DeviceDataManagerX11*>(DeviceDataManager::GetInstance()); | |
563 unsigned int blocked_device_id = 1; | |
564 unsigned int other_device_id = 2; | |
565 std::vector<unsigned int> device_list; | |
566 device_list.push_back(blocked_device_id); | |
567 device_list.push_back(other_device_id); | |
568 TouchFactory::GetInstance()->SetPointerDeviceForTest(device_list); | |
569 | |
570 device_data_manager->DisableDevice(blocked_device_id); | |
571 | |
572 ScopedXI2Event xev; | |
573 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(), | |
574 EF_LEFT_MOUSE_BUTTON); | |
575 EXPECT_EQ(ui::ET_UNKNOWN, ui::EventTypeFromNative(xev)); | |
576 | |
577 xev.InitGenericButtonEvent(other_device_id, ET_MOUSE_PRESSED, gfx::Point(), | |
578 EF_LEFT_MOUSE_BUTTON); | |
579 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev)); | |
580 | |
581 device_data_manager->EnableDevice(blocked_device_id); | |
582 | |
583 xev.InitGenericButtonEvent(blocked_device_id, ET_MOUSE_PRESSED, gfx::Point(), | |
584 EF_LEFT_MOUSE_BUTTON); | |
585 EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(xev)); | |
586 } | |
587 #endif // defined(USE_XI2_MT) | |
588 | |
589 #if !defined(OS_CHROMEOS) | |
590 TEST_F(PlatformEventUtilsXTest, ImeFabricatedKeyEvents) { | |
591 Display* display = gfx::GetXDisplay(); | |
592 | |
593 unsigned int state_to_be_fabricated[] = { | |
594 0, ShiftMask, LockMask, ShiftMask | LockMask, | |
595 }; | |
596 for (size_t i = 0; i < arraysize(state_to_be_fabricated); ++i) { | |
597 unsigned int state = state_to_be_fabricated[i]; | |
598 for (int is_char = 0; is_char < 2; ++is_char) { | |
599 XEvent x_event; | |
600 InitKeyEvent(display, &x_event, true, 0, state); | |
601 ui::KeyEvent key_event = PlatformEventBuilder::BuildKeyEvent(&x_event); | |
602 if (is_char) { | |
603 KeyEventTestApi test_event(&key_event); | |
604 test_event.set_is_char(true); | |
605 } | |
606 EXPECT_TRUE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); | |
607 } | |
608 } | |
609 | |
610 unsigned int state_to_be_not_fabricated[] = { | |
611 ControlMask, Mod1Mask, Mod2Mask, ShiftMask | ControlMask, | |
612 }; | |
613 for (size_t i = 0; i < arraysize(state_to_be_not_fabricated); ++i) { | |
614 unsigned int state = state_to_be_not_fabricated[i]; | |
615 for (int is_char = 0; is_char < 2; ++is_char) { | |
616 XEvent x_event; | |
617 InitKeyEvent(display, &x_event, true, 0, state); | |
618 ui::KeyEvent key_event = PlatformEventBuilder::BuildKeyEvent(&x_event); | |
619 if (is_char) { | |
620 KeyEventTestApi test_event(&key_event); | |
621 test_event.set_is_char(true); | |
622 } | |
623 EXPECT_FALSE(key_event.flags() & ui::EF_IME_FABRICATED_KEY); | |
624 } | |
625 } | |
626 } | |
627 #endif | |
628 | |
629 } // namespace ui | |
OLD | NEW |