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/dom3/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
),
41 ~MockEventConverterEvdevImpl() override
{}
43 // EventConverterEvdevImpl:
44 bool HasKeyboard() const override
{ return true; }
45 bool HasTouchpad() const override
{ return true; }
48 DISALLOW_COPY_AND_ASSIGN(MockEventConverterEvdevImpl
);
51 class MockCursorEvdev
: public CursorDelegateEvdev
{
54 ~MockCursorEvdev() override
{}
56 // CursorDelegateEvdev:
57 void MoveCursorTo(gfx::AcceleratedWidget widget
,
58 const gfx::PointF
& location
) override
{
59 cursor_location_
= location
;
61 void MoveCursorTo(const gfx::PointF
& location
) override
{
62 cursor_location_
= location
;
64 void MoveCursor(const gfx::Vector2dF
& delta
) override
{
65 cursor_location_
= gfx::PointF(delta
.x(), delta
.y());
67 bool IsCursorVisible() override
{ return 1; }
68 gfx::Rect
GetCursorConfinedBounds() override
{
72 gfx::PointF
GetLocation() override
{ return cursor_location_
; }
75 // The location of the mock cursor.
76 gfx::PointF cursor_location_
;
78 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev
);
84 class EventConverterEvdevImplTest
: public testing::Test
{
86 EventConverterEvdevImplTest() {}
88 // Overridden from testing::Test:
89 void SetUp() override
{
90 // Set up pipe to satisfy message pump (unused).
93 PLOG(FATAL
) << "failed pipe";
94 events_in_
= evdev_io
[0];
95 events_out_
= evdev_io
[1];
97 cursor_
.reset(new ui::MockCursorEvdev());
99 device_manager_
= ui::CreateDeviceManagerForTest();
100 event_factory_
= ui::CreateEventFactoryEvdevForTest(
101 cursor_
.get(), device_manager_
.get(),
102 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(),
103 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest
,
104 base::Unretained(this)));
106 ui::CreateDeviceEventDispatcherEvdevForTest(event_factory_
.get());
107 device_
.reset(new ui::MockEventConverterEvdevImpl(events_in_
, cursor_
.get(),
111 void TearDown() override
{
118 ui::MockCursorEvdev
* cursor() { return cursor_
.get(); }
119 ui::MockEventConverterEvdevImpl
* device() { return device_
.get(); }
121 unsigned size() { return dispatched_events_
.size(); }
122 ui::KeyEvent
* dispatched_event(unsigned index
) {
123 DCHECK_GT(dispatched_events_
.size(), index
);
124 ui::Event
* ev
= dispatched_events_
[index
];
125 DCHECK(ev
->IsKeyEvent());
126 return static_cast<ui::KeyEvent
*>(ev
);
128 ui::MouseEvent
* dispatched_mouse_event(unsigned index
) {
129 DCHECK_GT(dispatched_events_
.size(), index
);
130 ui::Event
* ev
= dispatched_events_
[index
];
131 DCHECK(ev
->IsMouseEvent());
132 return static_cast<ui::MouseEvent
*>(ev
);
135 void ClearDispatchedEvents() {
136 dispatched_events_
.clear();
139 void DestroyDevice() { device_
.reset(); }
142 void DispatchEventForTest(ui::Event
* event
) {
143 scoped_ptr
<ui::Event
> cloned_event
= ui::Event::Clone(*event
);
144 dispatched_events_
.push_back(cloned_event
.release());
147 base::MessageLoopForUI ui_loop_
;
149 scoped_ptr
<ui::MockCursorEvdev
> cursor_
;
150 scoped_ptr
<ui::DeviceManager
> device_manager_
;
151 scoped_ptr
<ui::EventFactoryEvdev
> event_factory_
;
152 scoped_ptr
<ui::DeviceEventDispatcherEvdev
> dispatcher_
;
153 scoped_ptr
<ui::MockEventConverterEvdevImpl
> device_
;
155 ScopedVector
<ui::Event
> dispatched_events_
;
160 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest
);
163 TEST_F(EventConverterEvdevImplTest
, KeyPress
) {
164 ui::MockEventConverterEvdevImpl
* dev
= device();
166 struct input_event mock_kernel_queue
[] = {
167 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
168 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
169 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
171 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
172 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
173 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
176 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
177 EXPECT_EQ(2u, size());
181 event
= dispatched_event(0);
182 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
183 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
184 EXPECT_EQ(0, event
->flags());
186 event
= dispatched_event(1);
187 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
188 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
189 EXPECT_EQ(0, event
->flags());
192 TEST_F(EventConverterEvdevImplTest
, KeyRepeat
) {
193 ui::MockEventConverterEvdevImpl
* dev
= device();
195 struct input_event mock_kernel_queue
[] = {
196 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
197 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 1},
198 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
200 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
201 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
202 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
204 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
205 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 2},
206 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
208 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7002a},
209 {{0, 0}, EV_KEY
, KEY_BACKSPACE
, 0},
210 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
213 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
214 EXPECT_EQ(2u, size());
218 event
= dispatched_event(0);
219 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
220 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
221 EXPECT_EQ(0, event
->flags());
223 event
= dispatched_event(1);
224 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
225 EXPECT_EQ(ui::VKEY_BACK
, event
->key_code());
226 EXPECT_EQ(0, event
->flags());
229 TEST_F(EventConverterEvdevImplTest
, NoEvents
) {
230 ui::MockEventConverterEvdevImpl
* dev
= device();
231 dev
->ProcessEvents(NULL
, 0);
232 EXPECT_EQ(0u, size());
235 TEST_F(EventConverterEvdevImplTest
, KeyWithModifier
) {
236 ui::MockEventConverterEvdevImpl
* dev
= device();
238 struct input_event mock_kernel_queue
[] = {
239 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
240 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 1},
241 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
243 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
244 {{0, 0}, EV_KEY
, KEY_A
, 1},
245 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
247 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70004},
248 {{0, 0}, EV_KEY
, KEY_A
, 0},
249 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
251 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
252 {{0, 0}, EV_KEY
, KEY_LEFTSHIFT
, 0},
253 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
256 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
257 EXPECT_EQ(4u, size());
261 event
= dispatched_event(0);
262 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
263 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
264 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
266 event
= dispatched_event(1);
267 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
268 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
269 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
271 event
= dispatched_event(2);
272 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
273 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
274 EXPECT_EQ(ui::EF_SHIFT_DOWN
, event
->flags());
276 event
= dispatched_event(3);
277 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
278 EXPECT_EQ(ui::VKEY_SHIFT
, event
->key_code());
279 EXPECT_EQ(0, event
->flags());
282 TEST_F(EventConverterEvdevImplTest
, KeyWithDuplicateModifier
) {
283 ui::MockEventConverterEvdevImpl
* dev
= device();
285 struct input_event mock_kernel_queue
[] = {
286 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
287 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 1},
288 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
290 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
291 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 1},
292 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
294 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
295 {{0, 0}, EV_KEY
, KEY_Z
, 1},
296 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
298 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x7001d},
299 {{0, 0}, EV_KEY
, KEY_Z
, 0},
300 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
302 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e1},
303 {{0, 0}, EV_KEY
, KEY_LEFTCTRL
, 0},
304 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
306 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x700e5},
307 {{0, 0}, EV_KEY
, KEY_RIGHTCTRL
, 0},
308 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
311 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
312 EXPECT_EQ(6u, size());
316 event
= dispatched_event(0);
317 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
318 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
319 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
321 event
= dispatched_event(1);
322 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
323 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
324 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
326 event
= dispatched_event(2);
327 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
328 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
329 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
331 event
= dispatched_event(3);
332 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
333 EXPECT_EQ(ui::VKEY_Z
, event
->key_code());
334 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
336 event
= dispatched_event(4);
337 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
338 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
339 EXPECT_EQ(ui::EF_CONTROL_DOWN
, event
->flags());
341 event
= dispatched_event(5);
342 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
343 EXPECT_EQ(ui::VKEY_CONTROL
, event
->key_code());
344 EXPECT_EQ(0, event
->flags());
347 TEST_F(EventConverterEvdevImplTest
, KeyWithLock
) {
348 ui::MockEventConverterEvdevImpl
* dev
= device();
350 struct input_event mock_kernel_queue
[] = {
351 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
352 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 1},
353 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
355 {{0, 0}, EV_MSC
, MSC_SCAN
, 0x70039},
356 {{0, 0}, EV_KEY
, KEY_CAPSLOCK
, 0},
357 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
360 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
361 EXPECT_EQ(2u, size());
365 event
= dispatched_event(0);
366 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
367 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
368 EXPECT_EQ(ui::EF_MOD3_DOWN
, event
->flags());
370 event
= dispatched_event(1);
371 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
372 EXPECT_EQ(ui::VKEY_CAPITAL
, event
->key_code());
373 EXPECT_EQ(ui::EF_NONE
, event
->flags());
376 TEST_F(EventConverterEvdevImplTest
, MouseButton
) {
377 ui::MockEventConverterEvdevImpl
* dev
= device();
379 struct input_event mock_kernel_queue
[] = {
380 {{0, 0}, EV_KEY
, BTN_LEFT
, 1},
381 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
383 {{0, 0}, EV_KEY
, BTN_LEFT
, 0},
384 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
387 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
388 EXPECT_EQ(2u, size());
390 ui::MouseEvent
* event
;
392 event
= dispatched_mouse_event(0);
393 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
394 EXPECT_EQ(true, event
->IsLeftMouseButton());
396 event
= dispatched_mouse_event(1);
397 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
398 EXPECT_EQ(true, event
->IsLeftMouseButton());
401 // Test that BTN_BACK and BTN_SIDE are treated as the same button.
402 TEST_F(EventConverterEvdevImplTest
, MouseBackButton
) {
403 ui::MockEventConverterEvdevImpl
* dev
= device();
405 struct input_event mock_kernel_queue
[] = {
406 {{0, 0}, EV_KEY
, BTN_SIDE
, 1},
407 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
409 {{0, 0}, EV_KEY
, BTN_BACK
, 1},
410 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
412 {{0, 0}, EV_KEY
, BTN_SIDE
, 0},
413 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
415 {{0, 0}, EV_KEY
, BTN_BACK
, 0},
416 {{0, 0}, EV_SYN
, SYN_REPORT
, 0}
419 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
420 EXPECT_EQ(2u, size());
422 ui::MouseEvent
* event
= nullptr;
424 event
= dispatched_mouse_event(0);
425 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
426 EXPECT_EQ(ui::EF_BACK_MOUSE_BUTTON
, event
->changed_button_flags());
428 event
= dispatched_mouse_event(1);
429 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
430 EXPECT_EQ(ui::EF_BACK_MOUSE_BUTTON
, event
->changed_button_flags());
433 // Test that BTN_FORWARD and BTN_EXTRA are treated as the same button.
434 TEST_F(EventConverterEvdevImplTest
, MouseForwardButton
) {
435 ui::MockEventConverterEvdevImpl
* dev
= device();
437 struct input_event mock_kernel_queue
[] = {
438 {{0, 0}, EV_KEY
, BTN_FORWARD
, 1},
439 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
441 {{0, 0}, EV_KEY
, BTN_EXTRA
, 1},
442 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
444 {{0, 0}, EV_KEY
, BTN_EXTRA
, 0},
445 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
447 {{0, 0}, EV_KEY
, BTN_FORWARD
, 0},
448 {{0, 0}, EV_SYN
, SYN_REPORT
, 0}
451 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
452 EXPECT_EQ(2u, size());
454 ui::MouseEvent
* event
= nullptr;
456 event
= dispatched_mouse_event(0);
457 EXPECT_EQ(ui::ET_MOUSE_PRESSED
, event
->type());
458 EXPECT_EQ(ui::EF_FORWARD_MOUSE_BUTTON
, event
->changed_button_flags());
460 event
= dispatched_mouse_event(1);
461 EXPECT_EQ(ui::ET_MOUSE_RELEASED
, event
->type());
462 EXPECT_EQ(ui::EF_FORWARD_MOUSE_BUTTON
, event
->changed_button_flags());
465 TEST_F(EventConverterEvdevImplTest
, MouseMove
) {
466 ui::MockEventConverterEvdevImpl
* dev
= device();
468 struct input_event mock_kernel_queue
[] = {
469 {{0, 0}, EV_REL
, REL_X
, 4},
470 {{0, 0}, EV_REL
, REL_Y
, 2},
471 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
474 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
475 EXPECT_EQ(1u, size());
477 ui::MouseEvent
* event
;
479 event
= dispatched_mouse_event(0);
480 EXPECT_EQ(ui::ET_MOUSE_MOVED
, event
->type());
481 EXPECT_EQ(cursor()->GetLocation(), gfx::PointF(4, 2));
484 TEST_F(EventConverterEvdevImplTest
, UnmappedKeyPress
) {
485 ui::MockEventConverterEvdevImpl
* dev
= device();
487 struct input_event mock_kernel_queue
[] = {
488 {{0, 0}, EV_KEY
, BTN_TOUCH
, 1},
489 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
491 {{0, 0}, EV_KEY
, BTN_TOUCH
, 0},
492 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
495 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
496 EXPECT_EQ(0u, size());
499 TEST_F(EventConverterEvdevImplTest
, ShouldReleaseKeysOnUnplug
) {
500 ui::MockEventConverterEvdevImpl
* dev
= device();
502 struct input_event mock_kernel_queue
[] = {
503 {{0, 0}, EV_KEY
, KEY_A
, 1},
504 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
507 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
508 EXPECT_EQ(1u, size());
511 EXPECT_EQ(2u, size());
513 ui::KeyEvent
* event
= dispatched_event(0);
514 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
515 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
517 event
= dispatched_event(1);
518 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
519 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
522 TEST_F(EventConverterEvdevImplTest
, ShouldReleaseKeysOnSynDropped
) {
523 ui::MockEventConverterEvdevImpl
* dev
= device();
525 struct input_event mock_kernel_queue
[] = {
526 {{0, 0}, EV_KEY
, KEY_A
, 1},
527 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
529 {{0, 0}, EV_SYN
, SYN_DROPPED
, 0},
532 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
533 EXPECT_EQ(2u, size());
535 ui::KeyEvent
* event
= dispatched_event(0);
536 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
537 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
539 event
= dispatched_event(1);
540 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
541 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
544 // Test that SetAllowedKeys() causes events for non-allowed keys to be dropped.
545 TEST_F(EventConverterEvdevImplTest
, SetAllowedKeys
) {
546 ui::MockEventConverterEvdevImpl
* dev
= device();
547 struct input_event mock_kernel_queue
[] = {
548 {{0, 0}, EV_KEY
, KEY_A
, 1},
549 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
551 {{0, 0}, EV_KEY
, KEY_A
, 0},
552 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
554 {{0, 0}, EV_KEY
, KEY_POWER
, 1},
555 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
557 {{0, 0}, EV_KEY
, KEY_POWER
, 0},
558 {{0, 0}, EV_KEY
, SYN_REPORT
, 0},
561 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
563 ASSERT_EQ(4u, size());
564 ui::KeyEvent
* event
= dispatched_event(0);
565 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
566 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
567 event
= dispatched_event(1);
568 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
569 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
570 event
= dispatched_event(2);
571 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
572 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
573 event
= dispatched_event(3);
574 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
575 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
577 ClearDispatchedEvents();
578 scoped_ptr
<std::set
<ui::DomCode
>> allowed_keys(new std::set
<ui::DomCode
>);
579 allowed_keys
->insert(ui::DomCode::POWER
);
580 dev
->SetAllowedKeys(allowed_keys
.Pass());
581 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
583 ASSERT_EQ(2u, size());
584 event
= dispatched_event(0);
585 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
586 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
587 event
= dispatched_event(1);
588 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
589 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
591 ClearDispatchedEvents();
593 dev
->ProcessEvents(mock_kernel_queue
, arraysize(mock_kernel_queue
));
595 event
= dispatched_event(0);
596 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
597 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
598 event
= dispatched_event(1);
599 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
600 EXPECT_EQ(ui::VKEY_A
, event
->key_code());
601 event
= dispatched_event(2);
602 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
603 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
604 event
= dispatched_event(3);
605 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
606 EXPECT_EQ(ui::VKEY_POWER
, event
->key_code());
609 // Test that if a non-allowed key is pressed when SetAllowedKeys() is called
610 // that the non-allowed key is released.
611 TEST_F(EventConverterEvdevImplTest
, SetAllowedKeysBlockedKeyPressed
) {
612 ui::MockEventConverterEvdevImpl
* dev
= device();
614 struct input_event key_press
[] = {
615 {{0, 0}, EV_KEY
, KEY_A
, 1},
616 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
618 struct input_event key_release
[] = {
619 {{0, 0}, EV_KEY
, KEY_A
, 0},
620 {{0, 0}, EV_SYN
, SYN_REPORT
, 0},
623 dev
->ProcessEvents(key_press
, arraysize(key_press
));
624 ASSERT_EQ(1u, size());
625 ui::KeyEvent
* event
= dispatched_event(0);
626 EXPECT_EQ(ui::ET_KEY_PRESSED
, event
->type());
628 // Block all key events. Calling SetAllowKeys() should dispatch a synthetic
629 // key release for VKEY_A.
630 ClearDispatchedEvents();
631 scoped_ptr
<std::set
<ui::DomCode
>> allowed_keys(new std::set
<ui::DomCode
>);
632 dev
->SetAllowedKeys(allowed_keys
.Pass());
633 ASSERT_EQ(1u, size());
634 event
= dispatched_event(0);
635 EXPECT_EQ(ui::ET_KEY_RELEASED
, event
->type());
637 // The real key release should be dropped, whenever it comes.
638 ClearDispatchedEvents();
639 dev
->ProcessEvents(key_release
, arraysize(key_release
));
640 ASSERT_EQ(0u, size());