ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / events / ozone / evdev / event_converter_evdev_impl_unittest.cc
blobb0de29dca8a569cd4fdc69c926f895ec945f053f
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>
7 #include "base/bind.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/device/device_manager.h"
15 #include "ui/events/ozone/evdev/cursor_delegate_evdev.h"
16 #include "ui/events/ozone/evdev/event_converter_evdev_impl.h"
17 #include "ui/events/ozone/evdev/event_converter_test_util.h"
18 #include "ui/events/ozone/evdev/event_factory_evdev.h"
19 #include "ui/events/ozone/evdev/keyboard_evdev.h"
20 #include "ui/events/ozone/layout/keyboard_layout_engine_manager.h"
22 namespace ui {
24 const char kTestDevicePath[] = "/dev/input/test-device";
26 class MockEventConverterEvdevImpl : public EventConverterEvdevImpl {
27 public:
28 MockEventConverterEvdevImpl(int fd,
29 CursorDelegateEvdev* cursor,
30 DeviceEventDispatcherEvdev* dispatcher)
31 : EventConverterEvdevImpl(fd,
32 base::FilePath(kTestDevicePath),
34 INPUT_DEVICE_UNKNOWN,
35 EventDeviceInfo(),
36 cursor,
37 dispatcher) {
38 Start();
40 ~MockEventConverterEvdevImpl() override {}
42 private:
43 DISALLOW_COPY_AND_ASSIGN(MockEventConverterEvdevImpl);
46 class MockCursorEvdev : public CursorDelegateEvdev {
47 public:
48 MockCursorEvdev() {}
49 ~MockCursorEvdev() override {}
51 // CursorDelegateEvdev:
52 void MoveCursorTo(gfx::AcceleratedWidget widget,
53 const gfx::PointF& location) override {
54 cursor_location_ = location;
56 void MoveCursorTo(const gfx::PointF& location) override {
57 cursor_location_ = location;
59 void MoveCursor(const gfx::Vector2dF& delta) override {
60 cursor_location_ = gfx::PointF(delta.x(), delta.y());
62 bool IsCursorVisible() override { return 1; }
63 gfx::Rect GetCursorConfinedBounds() override {
64 NOTIMPLEMENTED();
65 return gfx::Rect();
67 gfx::PointF GetLocation() override { return cursor_location_; }
69 private:
70 // The location of the mock cursor.
71 gfx::PointF cursor_location_;
73 DISALLOW_COPY_AND_ASSIGN(MockCursorEvdev);
76 } // namespace ui
78 // Test fixture.
79 class EventConverterEvdevImplTest : public testing::Test {
80 public:
81 EventConverterEvdevImplTest() {}
83 // Overridden from testing::Test:
84 void SetUp() override {
85 // Set up pipe to satisfy message pump (unused).
86 int evdev_io[2];
87 if (pipe(evdev_io))
88 PLOG(FATAL) << "failed pipe";
89 events_in_ = evdev_io[0];
90 events_out_ = evdev_io[1];
92 cursor_.reset(new ui::MockCursorEvdev());
94 device_manager_ = ui::CreateDeviceManagerForTest();
95 event_factory_ = ui::CreateEventFactoryEvdevForTest(
96 cursor_.get(), device_manager_.get(),
97 ui::KeyboardLayoutEngineManager::GetKeyboardLayoutEngine(),
98 base::Bind(&EventConverterEvdevImplTest::DispatchEventForTest,
99 base::Unretained(this)));
100 dispatcher_ =
101 ui::CreateDeviceEventDispatcherEvdevForTest(event_factory_.get());
102 device_.reset(new ui::MockEventConverterEvdevImpl(events_in_, cursor_.get(),
103 dispatcher_.get()));
106 void TearDown() override {
107 device_.reset();
108 cursor_.reset();
109 close(events_in_);
110 close(events_out_);
113 ui::MockCursorEvdev* cursor() { return cursor_.get(); }
114 ui::MockEventConverterEvdevImpl* device() { return device_.get(); }
116 unsigned size() { return dispatched_events_.size(); }
117 ui::KeyEvent* dispatched_event(unsigned index) {
118 DCHECK_GT(dispatched_events_.size(), index);
119 ui::Event* ev = dispatched_events_[index];
120 DCHECK(ev->IsKeyEvent());
121 return static_cast<ui::KeyEvent*>(ev);
123 ui::MouseEvent* dispatched_mouse_event(unsigned index) {
124 DCHECK_GT(dispatched_events_.size(), index);
125 ui::Event* ev = dispatched_events_[index];
126 DCHECK(ev->IsMouseEvent());
127 return static_cast<ui::MouseEvent*>(ev);
130 private:
131 void DispatchEventForTest(ui::Event* event) {
132 scoped_ptr<ui::Event> cloned_event = ui::Event::Clone(*event);
133 dispatched_events_.push_back(cloned_event.release());
136 base::MessageLoopForUI ui_loop_;
138 scoped_ptr<ui::MockCursorEvdev> cursor_;
139 scoped_ptr<ui::DeviceManager> device_manager_;
140 scoped_ptr<ui::EventFactoryEvdev> event_factory_;
141 scoped_ptr<ui::DeviceEventDispatcherEvdev> dispatcher_;
142 scoped_ptr<ui::MockEventConverterEvdevImpl> device_;
144 ScopedVector<ui::Event> dispatched_events_;
146 int events_out_;
147 int events_in_;
149 DISALLOW_COPY_AND_ASSIGN(EventConverterEvdevImplTest);
152 TEST_F(EventConverterEvdevImplTest, KeyPress) {
153 ui::MockEventConverterEvdevImpl* dev = device();
155 struct input_event mock_kernel_queue[] = {
156 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
157 {{0, 0}, EV_KEY, KEY_BACKSPACE, 1},
158 {{0, 0}, EV_SYN, SYN_REPORT, 0},
160 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
161 {{0, 0}, EV_KEY, KEY_BACKSPACE, 0},
162 {{0, 0}, EV_SYN, SYN_REPORT, 0},
165 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
166 EXPECT_EQ(2u, size());
168 ui::KeyEvent* event;
170 event = dispatched_event(0);
171 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
172 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
173 EXPECT_EQ(0, event->flags());
175 event = dispatched_event(1);
176 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
177 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
178 EXPECT_EQ(0, event->flags());
181 TEST_F(EventConverterEvdevImplTest, KeyRepeat) {
182 ui::MockEventConverterEvdevImpl* dev = device();
184 struct input_event mock_kernel_queue[] = {
185 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
186 {{0, 0}, EV_KEY, KEY_BACKSPACE, 1},
187 {{0, 0}, EV_SYN, SYN_REPORT, 0},
189 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
190 {{0, 0}, EV_KEY, KEY_BACKSPACE, 2},
191 {{0, 0}, EV_SYN, SYN_REPORT, 0},
193 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
194 {{0, 0}, EV_KEY, KEY_BACKSPACE, 2},
195 {{0, 0}, EV_SYN, SYN_REPORT, 0},
197 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
198 {{0, 0}, EV_KEY, KEY_BACKSPACE, 0},
199 {{0, 0}, EV_SYN, SYN_REPORT, 0},
202 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
203 EXPECT_EQ(2u, size());
205 ui::KeyEvent* event;
207 event = dispatched_event(0);
208 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
209 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
210 EXPECT_EQ(0, event->flags());
212 event = dispatched_event(1);
213 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
214 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
215 EXPECT_EQ(0, event->flags());
218 TEST_F(EventConverterEvdevImplTest, NoEvents) {
219 ui::MockEventConverterEvdevImpl* dev = device();
220 dev->ProcessEvents(NULL, 0);
221 EXPECT_EQ(0u, size());
224 TEST_F(EventConverterEvdevImplTest, KeyWithModifier) {
225 ui::MockEventConverterEvdevImpl* dev = device();
227 struct input_event mock_kernel_queue[] = {
228 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
229 {{0, 0}, EV_KEY, KEY_LEFTSHIFT, 1},
230 {{0, 0}, EV_SYN, SYN_REPORT, 0},
232 {{0, 0}, EV_MSC, MSC_SCAN, 0x70004},
233 {{0, 0}, EV_KEY, KEY_A, 1},
234 {{0, 0}, EV_SYN, SYN_REPORT, 0},
236 {{0, 0}, EV_MSC, MSC_SCAN, 0x70004},
237 {{0, 0}, EV_KEY, KEY_A, 0},
238 {{0, 0}, EV_SYN, SYN_REPORT, 0},
240 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
241 {{0, 0}, EV_KEY, KEY_LEFTSHIFT, 0},
242 {{0, 0}, EV_SYN, SYN_REPORT, 0},
245 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
246 EXPECT_EQ(4u, size());
248 ui::KeyEvent* event;
250 event = dispatched_event(0);
251 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
252 EXPECT_EQ(ui::VKEY_SHIFT, event->key_code());
253 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
255 event = dispatched_event(1);
256 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
257 EXPECT_EQ(ui::VKEY_A, event->key_code());
258 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
260 event = dispatched_event(2);
261 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
262 EXPECT_EQ(ui::VKEY_A, event->key_code());
263 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
265 event = dispatched_event(3);
266 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
267 EXPECT_EQ(ui::VKEY_SHIFT, event->key_code());
268 EXPECT_EQ(0, event->flags());
271 TEST_F(EventConverterEvdevImplTest, KeyWithDuplicateModifier) {
272 ui::MockEventConverterEvdevImpl* dev = device();
274 struct input_event mock_kernel_queue[] = {
275 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
276 {{0, 0}, EV_KEY, KEY_LEFTCTRL, 1},
277 {{0, 0}, EV_SYN, SYN_REPORT, 0},
279 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e5},
280 {{0, 0}, EV_KEY, KEY_RIGHTCTRL, 1},
281 {{0, 0}, EV_SYN, SYN_REPORT, 0},
283 {{0, 0}, EV_MSC, MSC_SCAN, 0x7001d},
284 {{0, 0}, EV_KEY, KEY_Z, 1},
285 {{0, 0}, EV_SYN, SYN_REPORT, 0},
287 {{0, 0}, EV_MSC, MSC_SCAN, 0x7001d},
288 {{0, 0}, EV_KEY, KEY_Z, 0},
289 {{0, 0}, EV_SYN, SYN_REPORT, 0},
291 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
292 {{0, 0}, EV_KEY, KEY_LEFTCTRL, 0},
293 {{0, 0}, EV_SYN, SYN_REPORT, 0},
295 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e5},
296 {{0, 0}, EV_KEY, KEY_RIGHTCTRL, 0},
297 {{0, 0}, EV_SYN, SYN_REPORT, 0},
300 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
301 EXPECT_EQ(6u, size());
303 ui::KeyEvent* event;
305 event = dispatched_event(0);
306 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
307 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
308 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
310 event = dispatched_event(1);
311 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
312 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
313 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
315 event = dispatched_event(2);
316 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
317 EXPECT_EQ(ui::VKEY_Z, event->key_code());
318 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
320 event = dispatched_event(3);
321 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
322 EXPECT_EQ(ui::VKEY_Z, event->key_code());
323 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
325 event = dispatched_event(4);
326 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
327 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
328 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
330 event = dispatched_event(5);
331 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
332 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
333 EXPECT_EQ(0, event->flags());
336 TEST_F(EventConverterEvdevImplTest, KeyWithLock) {
337 ui::MockEventConverterEvdevImpl* dev = device();
339 struct input_event mock_kernel_queue[] = {
340 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
341 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 1},
342 {{0, 0}, EV_SYN, SYN_REPORT, 0},
344 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
345 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 0},
346 {{0, 0}, EV_SYN, SYN_REPORT, 0},
349 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
350 EXPECT_EQ(2u, size());
352 ui::KeyEvent* event;
354 event = dispatched_event(0);
355 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
356 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
357 EXPECT_EQ(ui::EF_MOD3_DOWN, event->flags());
359 event = dispatched_event(1);
360 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
361 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
362 EXPECT_EQ(ui::EF_NONE, event->flags());
365 TEST_F(EventConverterEvdevImplTest, MouseButton) {
366 ui::MockEventConverterEvdevImpl* dev = device();
368 struct input_event mock_kernel_queue[] = {
369 {{0, 0}, EV_KEY, BTN_LEFT, 1},
370 {{0, 0}, EV_SYN, SYN_REPORT, 0},
372 {{0, 0}, EV_KEY, BTN_LEFT, 0},
373 {{0, 0}, EV_SYN, SYN_REPORT, 0},
376 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
377 EXPECT_EQ(2u, size());
379 ui::MouseEvent* event;
381 event = dispatched_mouse_event(0);
382 EXPECT_EQ(ui::ET_MOUSE_PRESSED, event->type());
383 EXPECT_EQ(true, event->IsLeftMouseButton());
385 event = dispatched_mouse_event(1);
386 EXPECT_EQ(ui::ET_MOUSE_RELEASED, event->type());
387 EXPECT_EQ(true, event->IsLeftMouseButton());
390 TEST_F(EventConverterEvdevImplTest, MouseMove) {
391 ui::MockEventConverterEvdevImpl* dev = device();
393 struct input_event mock_kernel_queue[] = {
394 {{0, 0}, EV_REL, REL_X, 4},
395 {{0, 0}, EV_REL, REL_Y, 2},
396 {{0, 0}, EV_SYN, SYN_REPORT, 0},
399 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
400 EXPECT_EQ(1u, size());
402 ui::MouseEvent* event;
404 event = dispatched_mouse_event(0);
405 EXPECT_EQ(ui::ET_MOUSE_MOVED, event->type());
406 EXPECT_EQ(cursor()->GetLocation(), gfx::PointF(4, 2));
409 TEST_F(EventConverterEvdevImplTest, UnmappedKeyPress) {
410 ui::MockEventConverterEvdevImpl* dev = device();
412 struct input_event mock_kernel_queue[] = {
413 {{0, 0}, EV_KEY, BTN_TOUCH, 1},
414 {{0, 0}, EV_SYN, SYN_REPORT, 0},
416 {{0, 0}, EV_KEY, BTN_TOUCH, 0},
417 {{0, 0}, EV_SYN, SYN_REPORT, 0},
420 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
421 EXPECT_EQ(0u, size());