1 // Copyright 2014 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 <linux/input.h>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/events/event.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h"
15 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h"
16 #include "ui/events/ozone/evdev/keyboard_evdev.h"
17 #include "ui/events/ozone/evdev/mouse_button_map_evdev.h"
18 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
22 const char kTestDevicePath
[] = "/dev/input/test-device";
24 class MockEventConverterEvdevImpl
: public EventConverterEvdevImpl
{
26 MockEventConverterEvdevImpl(int fd
,
27 EventModifiersEvdev
* modifiers
,
28 MouseButtonMapEvdev
* button_map
,
29 CursorDelegateEvdev
* cursor
,
30 KeyboardEvdev
* keyboard
,
31 const EventDispatchCallback
& callback
)
32 : EventConverterEvdevImpl(fd
,
33 base::FilePath(kTestDevicePath
),
44 ~MockEventConverterEvdevImpl() override
{}
47 DISALLOW_COPY_AND_ASSIGN(MockEventConverterEvdevImpl
);
50 class MockCursorEvdev
: public CursorDelegateEvdev
{
53 ~MockCursorEvdev() override
{}
55 // CursorDelegateEvdev:
56 void MoveCursorTo(gfx::AcceleratedWidget widget
,
57 const gfx::PointF
& location
) override
{
58 cursor_location_
= location
;
60 void MoveCursorTo(const gfx::PointF
& location
) override
{
61 cursor_location_
= location
;
63 void MoveCursor(const gfx::Vector2dF
& delta
) override
{
64 cursor_location_
= gfx::PointF(delta
.x(), delta
.y());
66 bool IsCursorVisible() override
{ return 1; }
67 gfx::Rect
GetCursorDisplayBounds() override
{
71 gfx::PointF
GetLocation() override
{ return cursor_location_
; }
74 // The location of the mock cursor.
75 gfx::PointF cursor_location_
;
77 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev
);
83 class EventConverterEvdevImplTest
: public testing::Test
{
85 EventConverterEvdevImplTest() {}
87 // Overridden from testing::Test:
88 void SetUp() override
{
89 // Set up pipe to satisfy message pump (unused).
92 PLOG(FATAL
) << "failed pipe";
93 events_in_
= evdev_io
[0];
94 events_out_
= evdev_io
[1];
96 cursor_
.reset(new ui::MockCursorEvdev());
97 modifiers_
.reset(new ui::EventModifiersEvdev());
98 button_map_
.reset(new ui::MouseButtonMapEvdev());
100 const ui::EventDispatchCallback callback
=
101 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest
,
102 base::Unretained(this));
103 keyboard_
.reset(new ui::KeyboardEvdev(
105 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(), callback
));
106 device_
.reset(new ui::MockEventConverterEvdevImpl(
107 events_in_
, modifiers_
.get(), button_map_
.get(), cursor_
.get(),
108 keyboard_
.get(), callback
));
110 void TearDown() override
{
120 ui::MockCursorEvdev
* cursor() { return cursor_
.get(); }
121 ui::MockEventConverterEvdevImpl
* device() { return device_
.get(); }
122 ui::EventModifiersEvdev
* modifiers() { return modifiers_
.get(); }
123 ui::MouseButtonMapEvdev
* button_map() { return button_map_
.get(); }
125 unsigned size() { return dispatched_events_
.size(); }
126 ui::KeyEvent
* dispatched_event(unsigned index
) {
127 DCHECK_GT(dispatched_events_
.size(), index
);
128 ui::Event
* ev
= dispatched_events_
[index
];
129 DCHECK(ev
->IsKeyEvent());
130 return static_cast<ui::KeyEvent
*>(ev
);
132 ui::MouseEvent
* dispatched_mouse_event(unsigned index
) {
133 DCHECK_GT(dispatched_events_
.size(), index
);
134 ui::Event
* ev
= dispatched_events_
[index
];
135 DCHECK(ev
->IsMouseEvent());
136 return static_cast<ui::MouseEvent
*>(ev
);
140 void DispatchEventForTest(scoped_ptr
<ui::Event
> event
) {
141 dispatched_events_
.push_back(event
.release());
144 base::MessageLoopForUI ui_loop_
;
146 scoped_ptr
<ui::MockCursorEvdev
> cursor_
;
147 scoped_ptr
<ui::EventModifiersEvdev
> modifiers_
;
148 scoped_ptr
<ui::MouseButtonMapEvdev
> button_map_
;
149 scoped_ptr
<ui::KeyboardEvdev
> keyboard_
;
150 scoped_ptr
<ui::MockEventConverterEvdevImpl
> device_
;
152 ScopedVector
<ui::Event
> dispatched_events_
;
157 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest
);
160 TEST_F(EventConverterEvdevImplTest
, KeyPress
) {
161 ui::MockEventConverterEvdevImpl
* dev
= device();
163 struct input_event mock_kernel_queue
[] = {
164 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
165 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
166 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
168 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
169 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
170 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
173 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
174 EXPECT_EQ(2u, size());
178 event
= dispatched_event(0);
179 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
180 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
181 EXPECT_EQ(0, event
->flags());
183 event
= dispatched_event(1);
184 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
185 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
186 EXPECT_EQ(0, event
->flags());
189 TEST_F(EventConverterEvdevImplTest
, KeyRepeat
) {
190 ui::MockEventConverterEvdevImpl
* dev
= device();
192 struct input_event mock_kernel_queue
[] = {
193 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
194 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
195 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
197 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
198 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
199 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
201 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
202 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
203 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
205 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
206 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
207 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
210 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
211 EXPECT_EQ(2u, size());
215 event
= dispatched_event(0);
216 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
217 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
218 EXPECT_EQ(0, event
->flags());
220 event
= dispatched_event(1);
221 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
222 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
223 EXPECT_EQ(0, event
->flags());
226 TEST_F(EventConverterEvdevImplTest
, NoEvents
) {
227 ui::MockEventConverterEvdevImpl
* dev
= device();
228 dev
->ProcessEvents(NULL
, 0);
229 EXPECT_EQ(0u, size());
232 TEST_F(EventConverterEvdevImplTest
, KeyWithModifier
) {
233 ui::MockEventConverterEvdevImpl
* dev
= device();
235 struct input_event mock_kernel_queue
[] = {
236 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
237 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 1},
238 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
240 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
241 {{0, 0}, EV_KEY
, KEY_A
, 1},
242 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
244 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
245 {{0, 0}, EV_KEY
, KEY_A
, 0},
246 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
248 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
249 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 0},
250 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
253 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
254 EXPECT_EQ(4u, size());
258 event
= dispatched_event(0);
259 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
260 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
261 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
263 event
= dispatched_event(1);
264 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
265 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
266 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
268 event
= dispatched_event(2);
269 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
270 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
271 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
273 event
= dispatched_event(3);
274 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
275 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
276 EXPECT_EQ(0, event
->flags());
279 TEST_F(EventConverterEvdevImplTest
, KeyWithDuplicateModifier
) {
280 ui::MockEventConverterEvdevImpl
* dev
= device();
282 struct input_event mock_kernel_queue
[] = {
283 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
284 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 1},
285 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
287 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
288 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 1},
289 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
291 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
292 {{0, 0}, EV_KEY
, KEY_Z
, 1},
293 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
295 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
296 {{0, 0}, EV_KEY
, KEY_Z
, 0},
297 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
299 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
300 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 0},
301 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
303 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
304 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 0},
305 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
308 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
309 EXPECT_EQ(6u, size());
313 event
= dispatched_event(0);
314 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
315 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
316 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
318 event
= dispatched_event(1);
319 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
320 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
321 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
323 event
= dispatched_event(2);
324 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
325 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
326 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
328 event
= dispatched_event(3);
329 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
330 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
331 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
333 event
= dispatched_event(4);
334 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
335 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
336 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
338 event
= dispatched_event(5);
339 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
340 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
341 EXPECT_EQ(0, event
->flags());
344 TEST_F(EventConverterEvdevImplTest
, KeyWithLock
) {
345 ui::MockEventConverterEvdevImpl
* dev
= device();
347 struct input_event mock_kernel_queue
[] = {
348 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
349 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 1},
350 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
352 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
353 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 0},
354 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
356 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70014},
357 {{0, 0}, EV_KEY
, KEY_Q
, 1},
358 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
360 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70014},
361 {{0, 0}, EV_KEY
, KEY_Q
, 0},
362 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
364 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
365 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 1},
366 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
368 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
369 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 0},
370 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
373 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
374 EXPECT_EQ(6u, size());
378 event
= dispatched_event(0);
379 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
380 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
381 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN
, event
->flags());
383 event
= dispatched_event(1);
384 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
385 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
386 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN
, event
->flags());
388 event
= dispatched_event(2);
389 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
390 EXPECT_EQ(ui::VKEY_Q
, event
->key_code());
391 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN
, event
->flags());
393 event
= dispatched_event(3);
394 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
395 EXPECT_EQ(ui::VKEY_Q
, event
->key_code());
396 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN
, event
->flags());
398 event
= dispatched_event(4);
399 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
400 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
401 EXPECT_EQ(0, event
->flags());
403 event
= dispatched_event(5);
404 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
405 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
406 EXPECT_EQ(0, event
->flags());
409 TEST_F(EventConverterEvdevImplTest
, MouseButton
) {
410 ui::MockEventConverterEvdevImpl
* dev
= device();
412 struct input_event mock_kernel_queue
[] = {
413 {{0, 0}, EV_KEY
, BTN_LEFT
, 1},
414 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
416 {{0, 0}, EV_KEY
, BTN_LEFT
, 0},
417 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
420 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
421 EXPECT_EQ(2u, size());
423 ui::MouseEvent
* event
;
425 event
= dispatched_mouse_event(0);
426 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
427 EXPECT_EQ(true, event
->IsLeftMouseButton());
429 event
= dispatched_mouse_event(1);
430 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
431 EXPECT_EQ(true, event
->IsLeftMouseButton());
434 TEST_F(EventConverterEvdevImplTest
, MouseMove
) {
435 ui::MockEventConverterEvdevImpl
* dev
= device();
437 struct input_event mock_kernel_queue
[] = {
438 {{0, 0}, EV_REL
, REL_X
, 4},
439 {{0, 0}, EV_REL
, REL_Y
, 2},
440 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
443 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
444 EXPECT_EQ(1u, size());
446 ui::MouseEvent
* event
;
448 event
= dispatched_mouse_event(0);
449 EXPECT_EQ(ui::ET_MOUSE_MOVED
, event
->type());
450 EXPECT_EQ(cursor()->GetLocation(), gfx::PointF(4, 2));
453 TEST_F(EventConverterEvdevImplTest
, UnmappedKeyPress
) {
454 ui::MockEventConverterEvdevImpl
* dev
= device();
456 struct input_event mock_kernel_queue
[] = {
457 {{0, 0}, EV_KEY
, BTN_TOUCH
, 1},
458 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
460 {{0, 0}, EV_KEY
, BTN_TOUCH
, 0},
461 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
464 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
465 EXPECT_EQ(0u, size());