Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: ui/base/ozone/touch_event_converter_ozone_unittest.cc

Issue 16466003: Event handling support for ozone. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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 <errno.h>
6 #include <fcntl.h>
7 #include <linux/input.h>
8 #include <unistd.h>
9
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/message_loop.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/run_loop.h"
17 #include "base/time.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/base/events/event.h"
20 #include "ui/base/ozone/touch_event_converter_ozone.h"
21
22
23 namespace {
24
25 static int SetNonBlocking(int fd) {
26 int flags = fcntl(fd, F_GETFL, 0);
27 if (flags == -1)
28 flags = 0;
29 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
30 }
31
32 } // namespace
33
34 namespace ui {
35
36 class MockTouchEventConverterOzone : public TouchEventConverterOzone,
37 public MessageLoop::Dispatcher {
38 public:
39 MockTouchEventConverterOzone(int a, int b);
40 ~MockTouchEventConverterOzone() {};
41
42 void ConfigureReadMock(struct input_event* queue,
43 long read_this_many,
44 long queue_index);
45
46 unsigned size() { return dispatched_events_.size(); }
47 TouchEvent* event(unsigned index) { return dispatched_events_[index]; }
48
49 // Actually dispatch the event reader code.
50 void ReadNow() {
51 OnFileCanReadWithoutBlocking(read_pipe_);
52 base::RunLoop().RunUntilIdle();
53 }
54
55 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE;
56
57 private:
58 int read_pipe_;
59 int write_pipe_;
60
61 ScopedVector<TouchEvent> dispatched_events_;
62
63 DISALLOW_COPY_AND_ASSIGN(MockTouchEventConverterOzone);
64 };
65
66 MockTouchEventConverterOzone::MockTouchEventConverterOzone(int a, int b)
67 : TouchEventConverterOzone(a, b) {
68 pressure_min_ = 30;
69 pressure_max_ = 60;
70
71 int fds[2];
72
73 DCHECK(pipe(fds) >= 0) << "pipe() failed, errno: " << errno;
74 DCHECK(SetNonBlocking(fds[0]) == 0)
75 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno;
76 DCHECK(SetNonBlocking(fds[1]) == 0)
77 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno;
78 read_pipe_ = fds[0];
79 write_pipe_ = fds[1];
80 }
81
82 bool MockTouchEventConverterOzone::Dispatch(const base::NativeEvent& event) {
83 ui::TouchEvent* ev = new ui::TouchEvent(event);
84 dispatched_events_.push_back(ev);
85 return true;
86 }
87
88 void MockTouchEventConverterOzone::ConfigureReadMock(struct input_event* queue,
89 long read_this_many,
90 long queue_index) {
91 int nwrite = HANDLE_EINTR(write(write_pipe_,
92 queue + queue_index,
93 sizeof(struct input_event) * read_this_many));
94 DCHECK(nwrite ==
95 static_cast<int>(sizeof(struct input_event) * read_this_many))
96 << "write() failed, errno: " << errno;
97 }
98
99 } // namespace ui
100
101 // Test fixture.
102 class TouchEventConverterOzoneTest : public testing::Test {
103 public:
104 TouchEventConverterOzoneTest() {}
105
106 // Overridden from testing::Test:
107 virtual void SetUp() OVERRIDE {
108 loop_ = new MessageLoop(MessageLoop::TYPE_UI);
109 device_ = new ui::MockTouchEventConverterOzone(-1, 2);
110 base::MessagePumpOzone::Current()->AddDispatcherForRootWindow(device_);
111 }
112 virtual void TearDown() OVERRIDE {
113 delete device_;
114 delete loop_;
115 }
116
117 ui::MockTouchEventConverterOzone* device() { return device_; }
118
119 private:
120 base::MessageLoop* loop_;
121 ui::MockTouchEventConverterOzone* device_;
122 DISALLOW_COPY_AND_ASSIGN(TouchEventConverterOzoneTest);
123 };
124
125 // TODO(rjkroege): Test for valid handling of time stamps.
126 TEST_F(TouchEventConverterOzoneTest, TouchDown) {
127 ui::MockTouchEventConverterOzone* dev = device();
128
129 struct input_event mock_kernel_queue[] = {
130 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
131 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3},
132 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45},
133 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
134 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
135 };
136
137 dev->ConfigureReadMock(mock_kernel_queue, 1, 0);
138 dev->ReadNow();
139 EXPECT_EQ(0u, dev->size());
140
141 dev->ConfigureReadMock(mock_kernel_queue, 2, 1);
142 dev->ReadNow();
143 EXPECT_EQ(0u, dev->size());
144
145 dev->ConfigureReadMock(mock_kernel_queue, 3, 3);
146 dev->ReadNow();
147 EXPECT_EQ(1u, dev->size());
148
149 ui::TouchEvent* event = dev->event(0);
150 EXPECT_FALSE(event == NULL);
151
152 EXPECT_EQ(ui::ET_TOUCH_PRESSED, event->type());
153 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp());
154 EXPECT_EQ(42, event->x());
155 EXPECT_EQ(51, event->y());
156 EXPECT_EQ(0, event->touch_id());
157 EXPECT_FLOAT_EQ(.5f, event->force());
158 EXPECT_FLOAT_EQ(0.f, event->rotation_angle());
159 }
160
161 TEST_F(TouchEventConverterOzoneTest, NoEvents) {
162 ui::MockTouchEventConverterOzone* dev = device();
163 dev->ConfigureReadMock(NULL, 0, 0);
164 EXPECT_EQ(0u, dev->size());
165 }
166
167 TEST_F(TouchEventConverterOzoneTest, TouchMove) {
168 ui::MockTouchEventConverterOzone* dev = device();
169
170 struct input_event mock_kernel_queue_press[] = {
171 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
172 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3},
173 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45},
174 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
175 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
176 };
177
178 struct input_event mock_kernel_queue_move1[] = {
179 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 50},
180 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
181 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 43}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
182 };
183
184 struct input_event mock_kernel_queue_move2[] = {
185 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 42}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
186 };
187
188 // Setup and discard a press.
189 dev->ConfigureReadMock(mock_kernel_queue_press, 6, 0);
190 dev->ReadNow();
191 EXPECT_EQ(1u, dev->size());
192
193 dev->ConfigureReadMock(mock_kernel_queue_move1, 4, 0);
194 dev->ReadNow();
195 EXPECT_EQ(2u, dev->size());
196 ui::TouchEvent* event = dev->event(1);
197 EXPECT_FALSE(event == NULL);
198
199 EXPECT_EQ(ui::ET_TOUCH_MOVED, event->type());
200 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp());
201 EXPECT_EQ(42, event->x());
202 EXPECT_EQ(43, event->y());
203 EXPECT_EQ(0, event->touch_id());
204 EXPECT_FLOAT_EQ(2.f / 3.f, event->force());
205 EXPECT_FLOAT_EQ(0.f, event->rotation_angle());
206
207 dev->ConfigureReadMock(mock_kernel_queue_move2, 2, 0);
208 dev->ReadNow();
209 EXPECT_EQ(3u, dev->size());
210 event = dev->event(2);
211 EXPECT_FALSE(event == NULL);
212
213 EXPECT_EQ(ui::ET_TOUCH_MOVED, event->type());
214 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp());
215 EXPECT_EQ(42, event->x());
216 EXPECT_EQ(42, event->y());
217 EXPECT_EQ(0, event->touch_id());
218 EXPECT_FLOAT_EQ(2.f / 3.f, event->force());
219 EXPECT_FLOAT_EQ(0.f, event->rotation_angle());
220 }
221
222 TEST_F(TouchEventConverterOzoneTest, TouchRelease) {
223 ui::MockTouchEventConverterOzone* dev = device();
224
225 struct input_event mock_kernel_queue_press[] = {
226 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
227 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3},
228 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45},
229 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
230 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
231 };
232
233 struct input_event mock_kernel_queue_release[] = {
234 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
235 };
236
237 // Setup and discard a press.
238 dev->ConfigureReadMock(mock_kernel_queue_press, 6, 0);
239 dev->ReadNow();
240 EXPECT_EQ(1u, dev->size());
241 ui::TouchEvent* event = dev->event(0);
242 EXPECT_FALSE(event == NULL);
243
244 dev->ConfigureReadMock(mock_kernel_queue_release, 2, 0);
245 dev->ReadNow();
246 EXPECT_EQ(2u, dev->size());
247 event = dev->event(1);
248 EXPECT_FALSE(event == NULL);
249
250 EXPECT_EQ(ui::ET_TOUCH_RELEASED, event->type());
251 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp());
252 EXPECT_EQ(42, event->x());
253 EXPECT_EQ(51, event->y());
254 EXPECT_EQ(0, event->touch_id());
255 EXPECT_FLOAT_EQ(.5f, event->force());
256 EXPECT_FLOAT_EQ(0.f, event->rotation_angle());
257 }
258
259 TEST_F(TouchEventConverterOzoneTest, TwoFingerGesture) {
260 ui::MockTouchEventConverterOzone* dev = device();
261
262 ui::TouchEvent* ev0;
263 ui::TouchEvent* ev1;
264
265 struct input_event mock_kernel_queue_press0[] = {
266 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684},
267 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3},
268 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45},
269 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42},
270 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
271 };
272 // Setup and discard a press.
273 dev->ConfigureReadMock(mock_kernel_queue_press0, 6, 0);
274 dev->ReadNow();
275 EXPECT_EQ(1u, dev->size());
276
277 struct input_event mock_kernel_queue_move0[] = {
278 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
279 };
280 // Setup and discard a move.
281 dev->ConfigureReadMock(mock_kernel_queue_move0, 2, 0);
282 dev->ReadNow();
283 EXPECT_EQ(2u, dev->size());
284
285 struct input_event mock_kernel_queue_move0press1[] = {
286 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40}, {{0, 0}, EV_SYN, SYN_REPORT, 0},
287 {{0, 0}, EV_ABS, ABS_MT_SLOT, 1}, {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 686},
288 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3},
289 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45},
290 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 101},
291 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 102}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
292 };
293 // Move on 0, press on 1.
294 dev->ConfigureReadMock(mock_kernel_queue_move0press1, 9, 0);
295 dev->ReadNow();
296 EXPECT_EQ(4u, dev->size());
297 ev0 = dev->event(2);
298 ev1 = dev->event(3);
299
300 // Move
301 EXPECT_EQ(ui::ET_TOUCH_MOVED, ev0->type());
302 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev0->time_stamp());
303 EXPECT_EQ(40, ev0->x());
304 EXPECT_EQ(51, ev0->y());
305 EXPECT_EQ(0, ev0->touch_id());
306 EXPECT_FLOAT_EQ(.5f, ev0->force());
307 EXPECT_FLOAT_EQ(0.f, ev0->rotation_angle());
308
309 // Press
310 EXPECT_EQ(ui::ET_TOUCH_PRESSED, ev1->type());
311 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp());
312 EXPECT_EQ(101, ev1->x());
313 EXPECT_EQ(102, ev1->y());
314 EXPECT_EQ(1, ev1->touch_id());
315 EXPECT_FLOAT_EQ(.5f, ev1->force());
316 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle());
317
318 // Stationary 0, Moves 1.
319 struct input_event mock_kernel_queue_stationary0_move1[] = {
320 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 40}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
321 };
322 dev->ConfigureReadMock(mock_kernel_queue_stationary0_move1, 2, 0);
323 dev->ReadNow();
324 EXPECT_EQ(5u, dev->size());
325 ev1 = dev->event(4);
326
327 EXPECT_EQ(ui::ET_TOUCH_MOVED, ev1->type());
328 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp());
329 EXPECT_EQ(40, ev1->x());
330 EXPECT_EQ(102, ev1->y());
331 EXPECT_EQ(1, ev1->touch_id());
332
333 EXPECT_FLOAT_EQ(.5f, ev1->force());
334 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle());
335
336 // Move 0, stationary 1.
337 struct input_event mock_kernel_queue_move0_stationary1[] = {
338 {{0, 0}, EV_ABS, ABS_MT_SLOT, 0}, {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 39},
339 {{0, 0}, EV_SYN, SYN_REPORT, 0}
340 };
341 dev->ConfigureReadMock(mock_kernel_queue_move0_stationary1, 3, 0);
342 dev->ReadNow();
343 EXPECT_EQ(6u, dev->size());
344 ev0 = dev->event(5);
345
346 EXPECT_EQ(ui::ET_TOUCH_MOVED, ev0->type());
347 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev0->time_stamp());
348 EXPECT_EQ(39, ev0->x());
349 EXPECT_EQ(51, ev0->y());
350 EXPECT_EQ(0, ev0->touch_id());
351 EXPECT_FLOAT_EQ(.5f, ev0->force());
352 EXPECT_FLOAT_EQ(0.f, ev0->rotation_angle());
353
354 // Release 0, move 1.
355 struct input_event mock_kernel_queue_release0_move1[] = {
356 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1}, {{0, 0}, EV_ABS, ABS_MT_SLOT, 1},
357 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 38}, {{0, 0}, EV_SYN, SYN_REPORT, 0}
358 };
359 dev->ConfigureReadMock(mock_kernel_queue_release0_move1, 4, 0);
360 dev->ReadNow();
361 EXPECT_EQ(8u, dev->size());
362 ev0 = dev->event(6);
363 ev1 = dev->event(7);
364
365 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ev0->type());
366 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev0->time_stamp());
367 EXPECT_EQ(39, ev0->x());
368 EXPECT_EQ(51, ev0->y());
369 EXPECT_EQ(0, ev0->touch_id());
370 EXPECT_FLOAT_EQ(.5f, ev0->force());
371 EXPECT_FLOAT_EQ(0.f, ev0->rotation_angle());
372
373 EXPECT_EQ(ui::ET_TOUCH_MOVED, ev1->type());
374 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp());
375 EXPECT_EQ(38, ev1->x());
376 EXPECT_EQ(102, ev1->y());
377 EXPECT_EQ(1, ev1->touch_id());
378 EXPECT_FLOAT_EQ(.5f, ev1->force());
379 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle());
380
381 // Release 1.
382 struct input_event mock_kernel_queue_release1[] = {
383 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, -1}, {{0, 0}, EV_SYN, SYN_REPORT, 0},
384 };
385 dev->ConfigureReadMock(mock_kernel_queue_release1, 2, 0);
386 dev->ReadNow();
387 EXPECT_EQ(9u, dev->size());
388 ev1 = dev->event(8);
389
390 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ev1->type());
391 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp());
392 EXPECT_EQ(38, ev1->x());
393 EXPECT_EQ(102, ev1->y());
394 EXPECT_EQ(1, ev1->touch_id());
395 EXPECT_FLOAT_EQ(.5f, ev1->force());
396 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle());
397 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698