1 // Copyright 2013 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 <linux/input.h>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_vector.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/event.h"
11 #include "ui/events/keycodes/keyboard_codes.h"
12 #include "ui/events/ozone/evdev/key_event_converter.h"
13
14 namespace ui {
15
16 const int kInvalidFileDescriptor = -1;
17 const int kTestDeviceId = 0;
18
19 class MockKeyEventConverterEvdev : public KeyEventConverterEvdev {
20 public:
MockKeyEventConverterEvdev(EventModifiersEvdev * modifiers)21 MockKeyEventConverterEvdev(EventModifiersEvdev* modifiers)
22 : KeyEventConverterEvdev(kInvalidFileDescriptor,
23 kTestDeviceId,
24 modifiers) {}
~MockKeyEventConverterEvdev()25 virtual ~MockKeyEventConverterEvdev() {};
26
size()27 unsigned size() { return dispatched_events_.size(); }
event(unsigned index)28 KeyEvent* event(unsigned index) { return dispatched_events_[index]; }
29
30 virtual void DispatchEvent(scoped_ptr<Event> event) OVERRIDE;
31
32 private:
33 ScopedVector<KeyEvent> dispatched_events_;
34
35 DISALLOW_COPY_AND_ASSIGN(MockKeyEventConverterEvdev);
36 };
37
DispatchEvent(scoped_ptr<Event> event)38 void MockKeyEventConverterEvdev::DispatchEvent(scoped_ptr<Event> event) {
39 dispatched_events_.push_back(static_cast<KeyEvent*>(event.release()));
40 }
41
42 } // namespace ui
43
44 // Test fixture.
45 class KeyEventConverterEvdevTest : public testing::Test {
46 public:
KeyEventConverterEvdevTest()47 KeyEventConverterEvdevTest() {}
48
49 // Overridden from testing::Test:
SetUp()50 virtual void SetUp() OVERRIDE {
51 modifiers_ = new ui::EventModifiersEvdev();
52 device_ = new ui::MockKeyEventConverterEvdev(modifiers_);
53 }
TearDown()54 virtual void TearDown() OVERRIDE {
55 delete device_;
56 delete modifiers_;
57 }
58
device()59 ui::MockKeyEventConverterEvdev* device() { return device_; }
modifiers()60 ui::EventModifiersEvdev* modifiers() { return modifiers_; }
61
62 private:
63 ui::EventModifiersEvdev* modifiers_;
64 ui::MockKeyEventConverterEvdev* device_;
65 DISALLOW_COPY_AND_ASSIGN(KeyEventConverterEvdevTest);
66 };
67
TEST_F(KeyEventConverterEvdevTest,KeyPress)68 TEST_F(KeyEventConverterEvdevTest, KeyPress) {
69 ui::MockKeyEventConverterEvdev* dev = device();
70
71 struct input_event mock_kernel_queue[] = {
72 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
73 {{0, 0}, EV_KEY, KEY_BACKSPACE, 1},
74 {{0, 0}, EV_SYN, SYN_REPORT, 0},
75
76 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
77 {{0, 0}, EV_KEY, KEY_BACKSPACE, 0},
78 {{0, 0}, EV_SYN, SYN_REPORT, 0},
79 };
80
81 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
82 EXPECT_EQ(2u, dev->size());
83
84 ui::KeyEvent* event;
85
86 event = dev->event(0);
87 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
88 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
89 EXPECT_EQ(0, event->flags());
90
91 event = dev->event(1);
92 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
93 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
94 EXPECT_EQ(0, event->flags());
95 }
96
TEST_F(KeyEventConverterEvdevTest,KeyRepeat)97 TEST_F(KeyEventConverterEvdevTest, KeyRepeat) {
98 ui::MockKeyEventConverterEvdev* dev = device();
99
100 struct input_event mock_kernel_queue[] = {
101 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
102 {{0, 0}, EV_KEY, KEY_BACKSPACE, 1},
103 {{0, 0}, EV_SYN, SYN_REPORT, 0},
104
105 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
106 {{0, 0}, EV_KEY, KEY_BACKSPACE, 2},
107 {{0, 0}, EV_SYN, SYN_REPORT, 0},
108
109 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
110 {{0, 0}, EV_KEY, KEY_BACKSPACE, 2},
111 {{0, 0}, EV_SYN, SYN_REPORT, 0},
112
113 {{0, 0}, EV_MSC, MSC_SCAN, 0x7002a},
114 {{0, 0}, EV_KEY, KEY_BACKSPACE, 0},
115 {{0, 0}, EV_SYN, SYN_REPORT, 0},
116 };
117
118 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
119 EXPECT_EQ(4u, dev->size());
120
121 ui::KeyEvent* event;
122
123 event = dev->event(0);
124 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
125 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
126 EXPECT_EQ(0, event->flags());
127
128 event = dev->event(1);
129 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
130 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
131 EXPECT_EQ(0, event->flags());
132
133 event = dev->event(2);
134 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
135 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
136 EXPECT_EQ(0, event->flags());
137
138 event = dev->event(3);
139 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
140 EXPECT_EQ(ui::VKEY_BACK, event->key_code());
141 EXPECT_EQ(0, event->flags());
142 }
143
TEST_F(KeyEventConverterEvdevTest,NoEvents)144 TEST_F(KeyEventConverterEvdevTest, NoEvents) {
145 ui::MockKeyEventConverterEvdev* dev = device();
146 dev->ProcessEvents(NULL, 0);
147 EXPECT_EQ(0u, dev->size());
148 }
149
TEST_F(KeyEventConverterEvdevTest,KeyWithModifier)150 TEST_F(KeyEventConverterEvdevTest, KeyWithModifier) {
151 ui::MockKeyEventConverterEvdev* dev = device();
152
153 struct input_event mock_kernel_queue[] = {
154 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
155 {{0, 0}, EV_KEY, KEY_LEFTSHIFT, 1},
156 {{0, 0}, EV_SYN, SYN_REPORT, 0},
157
158 {{0, 0}, EV_MSC, MSC_SCAN, 0x70004},
159 {{0, 0}, EV_KEY, KEY_A, 1},
160 {{0, 0}, EV_SYN, SYN_REPORT, 0},
161
162 {{0, 0}, EV_MSC, MSC_SCAN, 0x70004},
163 {{0, 0}, EV_KEY, KEY_A, 0},
164 {{0, 0}, EV_SYN, SYN_REPORT, 0},
165
166 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
167 {{0, 0}, EV_KEY, KEY_LEFTSHIFT, 0},
168 {{0, 0}, EV_SYN, SYN_REPORT, 0},
169 };
170
171 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
172 EXPECT_EQ(4u, dev->size());
173
174 ui::KeyEvent* event;
175
176 event = dev->event(0);
177 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
178 EXPECT_EQ(ui::VKEY_SHIFT, event->key_code());
179 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
180
181 event = dev->event(1);
182 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
183 EXPECT_EQ(ui::VKEY_A, event->key_code());
184 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
185
186 event = dev->event(2);
187 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
188 EXPECT_EQ(ui::VKEY_A, event->key_code());
189 EXPECT_EQ(ui::EF_SHIFT_DOWN, event->flags());
190
191 event = dev->event(3);
192 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
193 EXPECT_EQ(ui::VKEY_SHIFT, event->key_code());
194 EXPECT_EQ(0, event->flags());
195 }
196
TEST_F(KeyEventConverterEvdevTest,KeyWithDuplicateModifier)197 TEST_F(KeyEventConverterEvdevTest, KeyWithDuplicateModifier) {
198 ui::MockKeyEventConverterEvdev* dev = device();
199
200 struct input_event mock_kernel_queue[] = {
201 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
202 {{0, 0}, EV_KEY, KEY_LEFTCTRL, 1},
203 {{0, 0}, EV_SYN, SYN_REPORT, 0},
204
205 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e5},
206 {{0, 0}, EV_KEY, KEY_RIGHTCTRL, 1},
207 {{0, 0}, EV_SYN, SYN_REPORT, 0},
208
209 {{0, 0}, EV_MSC, MSC_SCAN, 0x7001d},
210 {{0, 0}, EV_KEY, KEY_Z, 1},
211 {{0, 0}, EV_SYN, SYN_REPORT, 0},
212
213 {{0, 0}, EV_MSC, MSC_SCAN, 0x7001d},
214 {{0, 0}, EV_KEY, KEY_Z, 0},
215 {{0, 0}, EV_SYN, SYN_REPORT, 0},
216
217 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e1},
218 {{0, 0}, EV_KEY, KEY_LEFTCTRL, 0},
219 {{0, 0}, EV_SYN, SYN_REPORT, 0},
220
221 {{0, 0}, EV_MSC, MSC_SCAN, 0x700e5},
222 {{0, 0}, EV_KEY, KEY_RIGHTCTRL, 0},
223 {{0, 0}, EV_SYN, SYN_REPORT, 0},
224 };
225
226 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
227 EXPECT_EQ(6u, dev->size());
228
229 ui::KeyEvent* event;
230
231 event = dev->event(0);
232 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
233 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
234 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
235
236 event = dev->event(1);
237 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
238 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
239 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
240
241 event = dev->event(2);
242 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
243 EXPECT_EQ(ui::VKEY_Z, event->key_code());
244 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
245
246 event = dev->event(3);
247 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
248 EXPECT_EQ(ui::VKEY_Z, event->key_code());
249 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
250
251 event = dev->event(4);
252 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
253 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
254 EXPECT_EQ(ui::EF_CONTROL_DOWN, event->flags());
255
256 event = dev->event(5);
257 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
258 EXPECT_EQ(ui::VKEY_CONTROL, event->key_code());
259 EXPECT_EQ(0, event->flags());
260 }
261
TEST_F(KeyEventConverterEvdevTest,KeyWithLock)262 TEST_F(KeyEventConverterEvdevTest, KeyWithLock) {
263 ui::MockKeyEventConverterEvdev* dev = device();
264
265 struct input_event mock_kernel_queue[] = {
266 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
267 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 1},
268 {{0, 0}, EV_SYN, SYN_REPORT, 0},
269
270 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
271 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 0},
272 {{0, 0}, EV_SYN, SYN_REPORT, 0},
273
274 {{0, 0}, EV_MSC, MSC_SCAN, 0x70014},
275 {{0, 0}, EV_KEY, KEY_Q, 1},
276 {{0, 0}, EV_SYN, SYN_REPORT, 0},
277
278 {{0, 0}, EV_MSC, MSC_SCAN, 0x70014},
279 {{0, 0}, EV_KEY, KEY_Q, 0},
280 {{0, 0}, EV_SYN, SYN_REPORT, 0},
281
282 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
283 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 1},
284 {{0, 0}, EV_SYN, SYN_REPORT, 0},
285
286 {{0, 0}, EV_MSC, MSC_SCAN, 0x70039},
287 {{0, 0}, EV_KEY, KEY_CAPSLOCK, 0},
288 {{0, 0}, EV_SYN, SYN_REPORT, 0},
289 };
290
291 dev->ProcessEvents(mock_kernel_queue, arraysize(mock_kernel_queue));
292 EXPECT_EQ(6u, dev->size());
293
294 ui::KeyEvent* event;
295
296 event = dev->event(0);
297 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
298 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
299 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN, event->flags());
300
301 event = dev->event(1);
302 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
303 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
304 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN, event->flags());
305
306 event = dev->event(2);
307 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
308 EXPECT_EQ(ui::VKEY_Q, event->key_code());
309 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN, event->flags());
310
311 event = dev->event(3);
312 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
313 EXPECT_EQ(ui::VKEY_Q, event->key_code());
314 EXPECT_EQ(ui::EF_CAPS_LOCK_DOWN, event->flags());
315
316 event = dev->event(4);
317 EXPECT_EQ(ui::ET_KEY_PRESSED, event->type());
318 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
319 EXPECT_EQ(0, event->flags());
320
321 event = dev->event(5);
322 EXPECT_EQ(ui::ET_KEY_RELEASED, event->type());
323 EXPECT_EQ(ui::VKEY_CAPITAL, event->key_code());
324 EXPECT_EQ(0, event->flags());
325 }
326