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.
6 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/event.h"
11 #include "ui/events/keycodes/dom/dom_code.h"
12 #include "ui/events/keycodes/keyboard_codes.h"
13 #include "ui/events/ozone/device/device_manager.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/event_converter_test_util.h"
17 #include "ui/events/ozone/evdev/event_factory_evdev.h"
18 #include "ui/events/ozone/evdev/keyboard_evdev.h"
19 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
21 #include <linux/input.h>
25 const char kTestDevicePath
[] = "/dev/input/test-device";
27 class MockEventConverterEvdevImpl
: public EventConverterEvdevImpl
{
29 MockEventConverterEvdevImpl(int fd
,
30 CursorDelegateEvdev
* cursor
,
31 DeviceEventDispatcherEvdev
* dispatcher
)
32 : EventConverterEvdevImpl(fd
,
33 base::FilePath(kTestDevicePath
),
40 ~MockEventConverterEvdevImpl() override
{ SetEnabled(false); }
42 // EventConverterEvdevImpl:
43 bool HasKeyboard() const override
{ return true; }
44 bool HasTouchpad() const override
{ return true; }
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
GetCursorConfinedBounds() 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());
98 device_manager_
= ui::CreateDeviceManagerForTest();
99 event_factory_
= ui::CreateEventFactoryEvdevForTest(
100 cursor_
.get(), device_manager_
.get(),
101 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(),
102 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest
,
103 base::Unretained(this)));
105 ui::CreateDeviceEventDispatcherEvdevForTest(event_factory_
.get());
106 device_
.reset(new ui::MockEventConverterEvdevImpl(events_in_
, cursor_
.get(),
110 void TearDown() override
{
117 ui::MockCursorEvdev
* cursor() { return cursor_
.get(); }
118 ui::MockEventConverterEvdevImpl
* device() { return device_
.get(); }
120 unsigned size() { return dispatched_events_
.size(); }
121 ui::KeyEvent
* dispatched_event(unsigned index
) {
122 DCHECK_GT(dispatched_events_
.size(), index
);
123 ui::Event
* ev
= dispatched_events_
[index
];
124 DCHECK(ev
->IsKeyEvent());
125 return static_cast<ui::KeyEvent
*>(ev
);
127 ui::MouseEvent
* dispatched_mouse_event(unsigned index
) {
128 DCHECK_GT(dispatched_events_
.size(), index
);
129 ui::Event
* ev
= dispatched_events_
[index
];
130 DCHECK(ev
->IsMouseEvent());
131 return static_cast<ui::MouseEvent
*>(ev
);
134 void ClearDispatchedEvents() {
135 dispatched_events_
.clear();
138 void DestroyDevice() { device_
.reset(); }
141 void DispatchEventForTest(ui::Event
* event
) {
142 scoped_ptr
<ui::Event
> cloned_event
= ui::Event::Clone(*event
);
143 dispatched_events_
.push_back(cloned_event
.Pass());
146 base::MessageLoopForUI ui_loop_
;
148 scoped_ptr
<ui::MockCursorEvdev
> cursor_
;
149 scoped_ptr
<ui::DeviceManager
> device_manager_
;
150 scoped_ptr
<ui::EventFactoryEvdev
> event_factory_
;
151 scoped_ptr
<ui::DeviceEventDispatcherEvdev
> dispatcher_
;
152 scoped_ptr
<ui::MockEventConverterEvdevImpl
> device_
;
154 ScopedVector
<ui::Event
> dispatched_events_
;
159 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest
);
162 TEST_F(EventConverterEvdevImplTest
, KeyPress
) {
163 ui::MockEventConverterEvdevImpl
* dev
= device();
165 struct input_event mock_kernel_queue
[] = {
166 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
167 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
168 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
170 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
171 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
172 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
175 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
176 EXPECT_EQ(2u, size());
180 event
= dispatched_event(0);
181 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
182 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
183 EXPECT_EQ(0, event
->flags());
185 event
= dispatched_event(1);
186 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
187 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
188 EXPECT_EQ(0, event
->flags());
191 TEST_F(EventConverterEvdevImplTest
, KeyRepeat
) {
192 ui::MockEventConverterEvdevImpl
* dev
= device();
194 struct input_event mock_kernel_queue
[] = {
195 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
196 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
197 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
199 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
200 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
201 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
203 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
204 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
205 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
207 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
208 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
209 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
212 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
213 EXPECT_EQ(2u, size());
217 event
= dispatched_event(0);
218 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
219 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
220 EXPECT_EQ(0, event
->flags());
222 event
= dispatched_event(1);
223 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
224 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
225 EXPECT_EQ(0, event
->flags());
228 TEST_F(EventConverterEvdevImplTest
, NoEvents
) {
229 ui::MockEventConverterEvdevImpl
* dev
= device();
230 dev
->ProcessEvents(NULL
, 0);
231 EXPECT_EQ(0u, size());
234 TEST_F(EventConverterEvdevImplTest
, KeyWithModifier
) {
235 ui::MockEventConverterEvdevImpl
* dev
= device();
237 struct input_event mock_kernel_queue
[] = {
238 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
239 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 1},
240 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
242 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
243 {{0, 0}, EV_KEY
, KEY_A
, 1},
244 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
246 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
247 {{0, 0}, EV_KEY
, KEY_A
, 0},
248 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
250 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
251 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 0},
252 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
255 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
256 EXPECT_EQ(4u, size());
260 event
= dispatched_event(0);
261 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
262 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
263 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
265 event
= dispatched_event(1);
266 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
267 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
268 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
270 event
= dispatched_event(2);
271 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
272 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
273 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
275 event
= dispatched_event(3);
276 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
277 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
278 EXPECT_EQ(0, event
->flags());
281 TEST_F(EventConverterEvdevImplTest
, KeyWithDuplicateModifier
) {
282 ui::MockEventConverterEvdevImpl
* dev
= device();
284 struct input_event mock_kernel_queue
[] = {
285 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
286 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 1},
287 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
289 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
290 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 1},
291 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
293 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
294 {{0, 0}, EV_KEY
, KEY_Z
, 1},
295 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
297 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
298 {{0, 0}, EV_KEY
, KEY_Z
, 0},
299 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
301 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
302 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 0},
303 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
305 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
306 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 0},
307 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
310 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
311 EXPECT_EQ(6u, size());
315 event
= dispatched_event(0);
316 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
317 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
318 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
320 event
= dispatched_event(1);
321 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
322 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
323 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
325 event
= dispatched_event(2);
326 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
327 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
328 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
330 event
= dispatched_event(3);
331 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
332 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
333 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
335 event
= dispatched_event(4);
336 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
337 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
338 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
340 event
= dispatched_event(5);
341 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
342 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
343 EXPECT_EQ(0, event
->flags());
346 TEST_F(EventConverterEvdevImplTest
, KeyWithLock
) {
347 ui::MockEventConverterEvdevImpl
* dev
= device();
349 struct input_event mock_kernel_queue
[] = {
350 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
351 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 1},
352 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
354 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
355 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 0},
356 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
359 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
360 EXPECT_EQ(2u, size());
364 event
= dispatched_event(0);
365 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
366 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
367 EXPECT_EQ(ui::EF_MOD3_DOWN
, event
->flags());
369 event
= dispatched_event(1);
370 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
371 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
372 EXPECT_EQ(ui::EF_NONE
, event
->flags());
375 TEST_F(EventConverterEvdevImplTest
, MouseButton
) {
376 ui::MockEventConverterEvdevImpl
* dev
= device();
378 struct input_event mock_kernel_queue
[] = {
379 {{0, 0}, EV_KEY
, BTN_LEFT
, 1},
380 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
382 {{0, 0}, EV_KEY
, BTN_LEFT
, 0},
383 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
386 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
387 EXPECT_EQ(2u, size());
389 ui::MouseEvent
* event
;
391 event
= dispatched_mouse_event(0);
392 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
393 EXPECT_EQ(true, event
->IsLeftMouseButton());
395 event
= dispatched_mouse_event(1);
396 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
397 EXPECT_EQ(true, event
->IsLeftMouseButton());
400 // Test that BTN_BACK and BTN_SIDE are treated as the same button.
401 TEST_F(EventConverterEvdevImplTest
, MouseBackButton
) {
402 ui::MockEventConverterEvdevImpl
* dev
= device();
404 struct input_event mock_kernel_queue
[] = {
405 {{0, 0}, EV_KEY
, BTN_SIDE
, 1},
406 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
408 {{0, 0}, EV_KEY
, BTN_BACK
, 1},
409 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
411 {{0, 0}, EV_KEY
, BTN_SIDE
, 0},
412 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
414 {{0, 0}, EV_KEY
, BTN_BACK
, 0},
415 {{0, 0}, EV_SYN
, SYN_REPORT
, 0}
418 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
419 EXPECT_EQ(2u, size());
421 ui::MouseEvent
* event
= nullptr;
423 event
= dispatched_mouse_event(0);
424 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
425 EXPECT_EQ(ui::EF_BACK_MOUSE_BUTTON
, event
->changed_button_flags());
427 event
= dispatched_mouse_event(1);
428 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
429 EXPECT_EQ(ui::EF_BACK_MOUSE_BUTTON
, event
->changed_button_flags());
432 // Test that BTN_FORWARD and BTN_EXTRA are treated as the same button.
433 TEST_F(EventConverterEvdevImplTest
, MouseForwardButton
) {
434 ui::MockEventConverterEvdevImpl
* dev
= device();
436 struct input_event mock_kernel_queue
[] = {
437 {{0, 0}, EV_KEY
, BTN_FORWARD
, 1},
438 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
440 {{0, 0}, EV_KEY
, BTN_EXTRA
, 1},
441 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
443 {{0, 0}, EV_KEY
, BTN_EXTRA
, 0},
444 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
446 {{0, 0}, EV_KEY
, BTN_FORWARD
, 0},
447 {{0, 0}, EV_SYN
, SYN_REPORT
, 0}
450 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
451 EXPECT_EQ(2u, size());
453 ui::MouseEvent
* event
= nullptr;
455 event
= dispatched_mouse_event(0);
456 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
457 EXPECT_EQ(ui::EF_FORWARD_MOUSE_BUTTON
, event
->changed_button_flags());
459 event
= dispatched_mouse_event(1);
460 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
461 EXPECT_EQ(ui::EF_FORWARD_MOUSE_BUTTON
, event
->changed_button_flags());
464 TEST_F(EventConverterEvdevImplTest
, MouseMove
) {
465 ui::MockEventConverterEvdevImpl
* dev
= device();
467 struct input_event mock_kernel_queue
[] = {
468 {{0, 0}, EV_REL
, REL_X
, 4},
469 {{0, 0}, EV_REL
, REL_Y
, 2},
470 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
473 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
474 EXPECT_EQ(1u, size());
476 ui::MouseEvent
* event
;
478 event
= dispatched_mouse_event(0);
479 EXPECT_EQ(ui::ET_MOUSE_MOVED
, event
->type());
480 EXPECT_EQ(cursor()->GetLocation(), gfx::PointF(4, 2));
483 TEST_F(EventConverterEvdevImplTest
, UnmappedKeyPress
) {
484 ui::MockEventConverterEvdevImpl
* dev
= device();
486 struct input_event mock_kernel_queue
[] = {
487 {{0, 0}, EV_KEY
, BTN_TOUCH
, 1},
488 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
490 {{0, 0}, EV_KEY
, BTN_TOUCH
, 0},
491 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
494 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
495 EXPECT_EQ(0u, size());
498 TEST_F(EventConverterEvdevImplTest
, ShouldReleaseKeysOnUnplug
) {
499 ui::MockEventConverterEvdevImpl
* dev
= device();
501 struct input_event mock_kernel_queue
[] = {
502 {{0, 0}, EV_KEY
, KEY_A
, 1},
503 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
506 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
507 EXPECT_EQ(1u, size());
510 EXPECT_EQ(2u, size());
512 ui::KeyEvent
* event
= dispatched_event(0);
513 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
514 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
516 event
= dispatched_event(1);
517 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
518 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
521 TEST_F(EventConverterEvdevImplTest
, ShouldReleaseKeysOnSynDropped
) {
522 ui::MockEventConverterEvdevImpl
* dev
= device();
524 struct input_event mock_kernel_queue
[] = {
525 {{0, 0}, EV_KEY
, KEY_A
, 1},
526 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
528 {{0, 0}, EV_SYN
, SYN_DROPPED
, 0},
531 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
532 EXPECT_EQ(2u, size());
534 ui::KeyEvent
* event
= dispatched_event(0);
535 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
536 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
538 event
= dispatched_event(1);
539 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
540 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
543 TEST_F(EventConverterEvdevImplTest
, ShouldReleaseKeysOnDisable
) {
544 ui::MockEventConverterEvdevImpl
* dev
= device();
546 struct input_event mock_kernel_queue
[] = {
547 {{0, 0}, EV_KEY
, KEY_A
, 1},
548 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
551 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
552 EXPECT_EQ(1u, size());
554 dev
->SetEnabled(false);
555 EXPECT_EQ(2u, size());
557 ui::KeyEvent
* event
= dispatched_event(0);
558 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
559 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
561 event
= dispatched_event(1);
562 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
563 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
567 // Test that SetAllowedKeys() causes events for non-allowed keys to be dropped.
568 TEST_F(EventConverterEvdevImplTest
, SetAllowedKeys
) {
569 ui::MockEventConverterEvdevImpl
* dev
= device();
570 struct input_event mock_kernel_queue
[] = {
571 {{0, 0}, EV_KEY
, KEY_A
, 1},
572 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
574 {{0, 0}, EV_KEY
, KEY_A
, 0},
575 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
577 {{0, 0}, EV_KEY
, KEY_POWER
, 1},
578 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
580 {{0, 0}, EV_KEY
, KEY_POWER
, 0},
581 {{0, 0}, EV_KEY
, SYN_REPORT
, 0},
584 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
586 ASSERT_EQ(4u, size());
587 ui::KeyEvent
* event
= dispatched_event(0);
588 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
589 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
590 event
= dispatched_event(1);
591 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
592 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
593 event
= dispatched_event(2);
594 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
595 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
596 event
= dispatched_event(3);
597 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
598 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
600 ClearDispatchedEvents();
601 std::vector
<ui::DomCode
> allowed_keys
;
602 allowed_keys
.push_back(ui::DomCode::POWER
);
603 dev
->SetKeyFilter(true /* enable_filter */, allowed_keys
);
604 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
606 ASSERT_EQ(2u, size());
607 event
= dispatched_event(0);
608 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
609 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
610 event
= dispatched_event(1);
611 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
612 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
614 ClearDispatchedEvents();
615 dev
->SetKeyFilter(false /* enable_filter */, std::vector
<ui::DomCode
>());
616 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
618 event
= dispatched_event(0);
619 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
620 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
621 event
= dispatched_event(1);
622 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
623 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
624 event
= dispatched_event(2);
625 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
626 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
627 event
= dispatched_event(3);
628 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
629 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
632 // Test that if a non-allowed key is pressed when SetAllowedKeys() is called
633 // that the non-allowed key is released.
634 TEST_F(EventConverterEvdevImplTest
, SetAllowedKeysBlockedKeyPressed
) {
635 ui::MockEventConverterEvdevImpl
* dev
= device();
637 struct input_event key_press
[] = {
638 {{0, 0}, EV_KEY
, KEY_A
, 1},
639 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
641 struct input_event key_release
[] = {
642 {{0, 0}, EV_KEY
, KEY_A
, 0},
643 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
646 dev
->ProcessEvents(key_press
, arraysize(key_press
));
647 ASSERT_EQ(1u, size());
648 ui::KeyEvent
* event
= dispatched_event(0);
649 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
651 // Block all key events. Calling SetAllowKeys() should dispatch a synthetic
652 // key release for VKEY_A.
653 ClearDispatchedEvents();
654 std::vector
<ui::DomCode
> allowed_keys
;
655 dev
->SetKeyFilter(true /* enable_filter */, allowed_keys
);
656 ASSERT_EQ(1u, size());
657 event
= dispatched_event(0);
658 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
660 // The real key release should be dropped, whenever it comes.
661 ClearDispatchedEvents();
662 dev
->ProcessEvents(key_release
, arraysize(key_release
));
663 ASSERT_EQ(0u, size());