OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <linux/input.h> | 7 #include <linux/input.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
20 #include "ui/events/ozone/evdev/touch_event_converter_ozone.h" | 20 #include "ui/events/ozone/evdev/touch_event_converter.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 static int SetNonBlocking(int fd) { | 24 static int SetNonBlocking(int fd) { |
25 int flags = fcntl(fd, F_GETFL, 0); | 25 int flags = fcntl(fd, F_GETFL, 0); |
26 if (flags == -1) | 26 if (flags == -1) |
27 flags = 0; | 27 flags = 0; |
28 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); | 28 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
29 } | 29 } |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 namespace ui { | 33 namespace ui { |
34 | 34 |
35 class MockTouchEventConverterOzone : public TouchEventConverterOzone, | 35 class MockTouchEventConverterEvdev : public TouchEventConverterEvdev, |
36 public base::MessageLoop::Dispatcher { | 36 public base::MessageLoop::Dispatcher { |
37 public: | 37 public: |
38 MockTouchEventConverterOzone(int a, int b); | 38 MockTouchEventConverterEvdev(int a, int b); |
39 virtual ~MockTouchEventConverterOzone() {}; | 39 virtual ~MockTouchEventConverterEvdev() {}; |
40 | 40 |
41 void ConfigureReadMock(struct input_event* queue, | 41 void ConfigureReadMock(struct input_event* queue, |
42 long read_this_many, | 42 long read_this_many, |
43 long queue_index); | 43 long queue_index); |
44 | 44 |
45 unsigned size() { return dispatched_events_.size(); } | 45 unsigned size() { return dispatched_events_.size(); } |
46 TouchEvent* event(unsigned index) { return dispatched_events_[index]; } | 46 TouchEvent* event(unsigned index) { return dispatched_events_[index]; } |
47 | 47 |
48 // Actually dispatch the event reader code. | 48 // Actually dispatch the event reader code. |
49 void ReadNow() { | 49 void ReadNow() { |
50 OnFileCanReadWithoutBlocking(read_pipe_); | 50 OnFileCanReadWithoutBlocking(read_pipe_); |
51 base::RunLoop().RunUntilIdle(); | 51 base::RunLoop().RunUntilIdle(); |
52 } | 52 } |
53 | 53 |
54 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE; | 54 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE; |
55 | 55 |
56 private: | 56 private: |
57 int read_pipe_; | 57 int read_pipe_; |
58 int write_pipe_; | 58 int write_pipe_; |
59 | 59 |
60 ScopedVector<TouchEvent> dispatched_events_; | 60 ScopedVector<TouchEvent> dispatched_events_; |
61 | 61 |
62 DISALLOW_COPY_AND_ASSIGN(MockTouchEventConverterOzone); | 62 DISALLOW_COPY_AND_ASSIGN(MockTouchEventConverterEvdev); |
63 }; | 63 }; |
64 | 64 |
65 MockTouchEventConverterOzone::MockTouchEventConverterOzone(int a, int b) | 65 MockTouchEventConverterEvdev::MockTouchEventConverterEvdev(int a, int b) |
66 : TouchEventConverterOzone(a, b) { | 66 : TouchEventConverterEvdev(a, b) { |
67 pressure_min_ = 30; | 67 pressure_min_ = 30; |
68 pressure_max_ = 60; | 68 pressure_max_ = 60; |
69 | 69 |
70 int fds[2]; | 70 int fds[2]; |
71 | 71 |
72 DCHECK(pipe(fds) >= 0) << "pipe() failed, errno: " << errno; | 72 DCHECK(pipe(fds) >= 0) << "pipe() failed, errno: " << errno; |
73 DCHECK(SetNonBlocking(fds[0]) == 0) | 73 DCHECK(SetNonBlocking(fds[0]) == 0) |
74 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 74 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
75 DCHECK(SetNonBlocking(fds[1]) == 0) | 75 DCHECK(SetNonBlocking(fds[1]) == 0) |
76 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; | 76 << "SetNonBlocking for pipe fd[0] failed, errno: " << errno; |
77 read_pipe_ = fds[0]; | 77 read_pipe_ = fds[0]; |
78 write_pipe_ = fds[1]; | 78 write_pipe_ = fds[1]; |
79 } | 79 } |
80 | 80 |
81 bool MockTouchEventConverterOzone::Dispatch(const base::NativeEvent& event) { | 81 bool MockTouchEventConverterEvdev::Dispatch(const base::NativeEvent& event) { |
82 ui::TouchEvent* ev = new ui::TouchEvent(event); | 82 ui::TouchEvent* ev = new ui::TouchEvent(event); |
83 dispatched_events_.push_back(ev); | 83 dispatched_events_.push_back(ev); |
84 return true; | 84 return true; |
85 } | 85 } |
86 | 86 |
87 void MockTouchEventConverterOzone::ConfigureReadMock(struct input_event* queue, | 87 void MockTouchEventConverterEvdev::ConfigureReadMock(struct input_event* queue, |
88 long read_this_many, | 88 long read_this_many, |
89 long queue_index) { | 89 long queue_index) { |
90 int nwrite = HANDLE_EINTR(write(write_pipe_, | 90 int nwrite = HANDLE_EINTR(write(write_pipe_, |
91 queue + queue_index, | 91 queue + queue_index, |
92 sizeof(struct input_event) * read_this_many)); | 92 sizeof(struct input_event) * read_this_many)); |
93 DCHECK(nwrite == | 93 DCHECK(nwrite == |
94 static_cast<int>(sizeof(struct input_event) * read_this_many)) | 94 static_cast<int>(sizeof(struct input_event) * read_this_many)) |
95 << "write() failed, errno: " << errno; | 95 << "write() failed, errno: " << errno; |
96 } | 96 } |
97 | 97 |
98 } // namespace ui | 98 } // namespace ui |
99 | 99 |
100 // Test fixture. | 100 // Test fixture. |
101 class TouchEventConverterOzoneTest : public testing::Test { | 101 class TouchEventConverterEvdevTest : public testing::Test { |
102 public: | 102 public: |
103 TouchEventConverterOzoneTest() {} | 103 TouchEventConverterEvdevTest() {} |
104 | 104 |
105 // Overridden from testing::Test: | 105 // Overridden from testing::Test: |
106 virtual void SetUp() OVERRIDE { | 106 virtual void SetUp() OVERRIDE { |
107 loop_ = new base::MessageLoop(base::MessageLoop::TYPE_UI); | 107 loop_ = new base::MessageLoop(base::MessageLoop::TYPE_UI); |
108 device_ = new ui::MockTouchEventConverterOzone(-1, 2); | 108 device_ = new ui::MockTouchEventConverterEvdev(-1, 2); |
109 base::MessagePumpOzone::Current()->AddDispatcherForRootWindow(device_); | 109 base::MessagePumpOzone::Current()->AddDispatcherForRootWindow(device_); |
110 } | 110 } |
111 virtual void TearDown() OVERRIDE { | 111 virtual void TearDown() OVERRIDE { |
112 delete device_; | 112 delete device_; |
113 delete loop_; | 113 delete loop_; |
114 } | 114 } |
115 | 115 |
116 ui::MockTouchEventConverterOzone* device() { return device_; } | 116 ui::MockTouchEventConverterEvdev* device() { return device_; } |
117 | 117 |
118 private: | 118 private: |
119 base::MessageLoop* loop_; | 119 base::MessageLoop* loop_; |
120 ui::MockTouchEventConverterOzone* device_; | 120 ui::MockTouchEventConverterEvdev* device_; |
121 DISALLOW_COPY_AND_ASSIGN(TouchEventConverterOzoneTest); | 121 DISALLOW_COPY_AND_ASSIGN(TouchEventConverterEvdevTest); |
122 }; | 122 }; |
123 | 123 |
124 // TODO(rjkroege): Test for valid handling of time stamps. | 124 // TODO(rjkroege): Test for valid handling of time stamps. |
125 TEST_F(TouchEventConverterOzoneTest, TouchDown) { | 125 TEST_F(TouchEventConverterEvdevTest, TouchDown) { |
126 ui::MockTouchEventConverterOzone* dev = device(); | 126 ui::MockTouchEventConverterEvdev* dev = device(); |
127 | 127 |
128 struct input_event mock_kernel_queue[] = { | 128 struct input_event mock_kernel_queue[] = { |
129 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, | 129 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, |
130 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, | 130 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, |
131 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, | 131 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, |
132 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, | 132 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, |
133 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} | 133 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} |
134 }; | 134 }; |
135 | 135 |
136 dev->ConfigureReadMock(mock_kernel_queue, 1, 0); | 136 dev->ConfigureReadMock(mock_kernel_queue, 1, 0); |
(...skipping 13 matching lines...) Expand all Loading... |
150 | 150 |
151 EXPECT_EQ(ui::ET_TOUCH_PRESSED, event->type()); | 151 EXPECT_EQ(ui::ET_TOUCH_PRESSED, event->type()); |
152 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); | 152 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); |
153 EXPECT_EQ(42, event->x()); | 153 EXPECT_EQ(42, event->x()); |
154 EXPECT_EQ(51, event->y()); | 154 EXPECT_EQ(51, event->y()); |
155 EXPECT_EQ(0, event->touch_id()); | 155 EXPECT_EQ(0, event->touch_id()); |
156 EXPECT_FLOAT_EQ(.5f, event->force()); | 156 EXPECT_FLOAT_EQ(.5f, event->force()); |
157 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); | 157 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); |
158 } | 158 } |
159 | 159 |
160 TEST_F(TouchEventConverterOzoneTest, NoEvents) { | 160 TEST_F(TouchEventConverterEvdevTest, NoEvents) { |
161 ui::MockTouchEventConverterOzone* dev = device(); | 161 ui::MockTouchEventConverterEvdev* dev = device(); |
162 dev->ConfigureReadMock(NULL, 0, 0); | 162 dev->ConfigureReadMock(NULL, 0, 0); |
163 EXPECT_EQ(0u, dev->size()); | 163 EXPECT_EQ(0u, dev->size()); |
164 } | 164 } |
165 | 165 |
166 TEST_F(TouchEventConverterOzoneTest, TouchMove) { | 166 TEST_F(TouchEventConverterEvdevTest, TouchMove) { |
167 ui::MockTouchEventConverterOzone* dev = device(); | 167 ui::MockTouchEventConverterEvdev* dev = device(); |
168 | 168 |
169 struct input_event mock_kernel_queue_press[] = { | 169 struct input_event mock_kernel_queue_press[] = { |
170 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, | 170 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, |
171 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, | 171 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, |
172 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, | 172 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, |
173 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, | 173 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, |
174 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} | 174 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} |
175 }; | 175 }; |
176 | 176 |
177 struct input_event mock_kernel_queue_move1[] = { | 177 struct input_event mock_kernel_queue_move1[] = { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 211 |
212 EXPECT_EQ(ui::ET_TOUCH_MOVED, event->type()); | 212 EXPECT_EQ(ui::ET_TOUCH_MOVED, event->type()); |
213 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); | 213 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); |
214 EXPECT_EQ(42, event->x()); | 214 EXPECT_EQ(42, event->x()); |
215 EXPECT_EQ(42, event->y()); | 215 EXPECT_EQ(42, event->y()); |
216 EXPECT_EQ(0, event->touch_id()); | 216 EXPECT_EQ(0, event->touch_id()); |
217 EXPECT_FLOAT_EQ(2.f / 3.f, event->force()); | 217 EXPECT_FLOAT_EQ(2.f / 3.f, event->force()); |
218 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); | 218 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); |
219 } | 219 } |
220 | 220 |
221 TEST_F(TouchEventConverterOzoneTest, TouchRelease) { | 221 TEST_F(TouchEventConverterEvdevTest, TouchRelease) { |
222 ui::MockTouchEventConverterOzone* dev = device(); | 222 ui::MockTouchEventConverterEvdev* dev = device(); |
223 | 223 |
224 struct input_event mock_kernel_queue_press[] = { | 224 struct input_event mock_kernel_queue_press[] = { |
225 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, | 225 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, |
226 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, | 226 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, |
227 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, | 227 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, |
228 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, | 228 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, |
229 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} | 229 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} |
230 }; | 230 }; |
231 | 231 |
232 struct input_event mock_kernel_queue_release[] = { | 232 struct input_event mock_kernel_queue_release[] = { |
(...skipping 15 matching lines...) Expand all Loading... |
248 | 248 |
249 EXPECT_EQ(ui::ET_TOUCH_RELEASED, event->type()); | 249 EXPECT_EQ(ui::ET_TOUCH_RELEASED, event->type()); |
250 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); | 250 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), event->time_stamp()); |
251 EXPECT_EQ(42, event->x()); | 251 EXPECT_EQ(42, event->x()); |
252 EXPECT_EQ(51, event->y()); | 252 EXPECT_EQ(51, event->y()); |
253 EXPECT_EQ(0, event->touch_id()); | 253 EXPECT_EQ(0, event->touch_id()); |
254 EXPECT_FLOAT_EQ(.5f, event->force()); | 254 EXPECT_FLOAT_EQ(.5f, event->force()); |
255 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); | 255 EXPECT_FLOAT_EQ(0.f, event->rotation_angle()); |
256 } | 256 } |
257 | 257 |
258 TEST_F(TouchEventConverterOzoneTest, TwoFingerGesture) { | 258 TEST_F(TouchEventConverterEvdevTest, TwoFingerGesture) { |
259 ui::MockTouchEventConverterOzone* dev = device(); | 259 ui::MockTouchEventConverterEvdev* dev = device(); |
260 | 260 |
261 ui::TouchEvent* ev0; | 261 ui::TouchEvent* ev0; |
262 ui::TouchEvent* ev1; | 262 ui::TouchEvent* ev1; |
263 | 263 |
264 struct input_event mock_kernel_queue_press0[] = { | 264 struct input_event mock_kernel_queue_press0[] = { |
265 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, | 265 {{0, 0}, EV_ABS, ABS_MT_TRACKING_ID, 684}, |
266 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, | 266 {{0, 0}, EV_ABS, ABS_MT_TOUCH_MAJOR, 3}, |
267 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, | 267 {{0, 0}, EV_ABS, ABS_MT_PRESSURE, 45}, |
268 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, | 268 {{0, 0}, EV_ABS, ABS_MT_POSITION_X, 42}, |
269 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} | 269 {{0, 0}, EV_ABS, ABS_MT_POSITION_Y, 51}, {{0, 0}, EV_SYN, SYN_REPORT, 0} |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 ev1 = dev->event(8); | 387 ev1 = dev->event(8); |
388 | 388 |
389 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ev1->type()); | 389 EXPECT_EQ(ui::ET_TOUCH_RELEASED, ev1->type()); |
390 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp()); | 390 EXPECT_EQ(base::TimeDelta::FromMicroseconds(0), ev1->time_stamp()); |
391 EXPECT_EQ(38, ev1->x()); | 391 EXPECT_EQ(38, ev1->x()); |
392 EXPECT_EQ(102, ev1->y()); | 392 EXPECT_EQ(102, ev1->y()); |
393 EXPECT_EQ(1, ev1->touch_id()); | 393 EXPECT_EQ(1, ev1->touch_id()); |
394 EXPECT_FLOAT_EQ(.5f, ev1->force()); | 394 EXPECT_FLOAT_EQ(.5f, ev1->force()); |
395 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle()); | 395 EXPECT_FLOAT_EQ(0.f, ev1->rotation_angle()); |
396 } | 396 } |
OLD | NEW |