blob: 55504f2e47ed4fdfeac614bb7d857fa83bc14cd6 [file] [log] [blame]
Jeff Brownf4a4ec22010-06-16 01:53:36 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4
5#include <ui/InputTransport.h>
6#include <utils/Timers.h>
7#include <utils/StopWatch.h>
8#include <gtest/gtest.h>
9#include <unistd.h>
10#include <time.h>
11#include <sys/mman.h>
12#include <cutils/ashmem.h>
13
14#include "../../utils/tests/TestHelpers.h"
15
16namespace android {
17
18class InputPublisherAndConsumerTest : public testing::Test {
19protected:
20 sp<InputChannel> serverChannel, clientChannel;
21 InputPublisher* mPublisher;
22 InputConsumer* mConsumer;
23 PreallocatedInputEventFactory mEventFactory;
24
25 virtual void SetUp() {
26 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
27 serverChannel, clientChannel);
28
29 mPublisher = new InputPublisher(serverChannel);
30 mConsumer = new InputConsumer(clientChannel);
31 }
32
33 virtual void TearDown() {
34 if (mPublisher) {
35 delete mPublisher;
36 mPublisher = NULL;
37 }
38
39 if (mConsumer) {
40 delete mConsumer;
41 mConsumer = NULL;
42 }
43
44 serverChannel.clear();
45 clientChannel.clear();
46 }
47
48 void Initialize();
49 void PublishAndConsumeKeyEvent();
50 void PublishAndConsumeMotionEvent(
51 size_t samplesToAppendBeforeDispatch = 0,
52 size_t samplesToAppendAfterDispatch = 0);
53};
54
55TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
56 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
57 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
58}
59
60void InputPublisherAndConsumerTest::Initialize() {
61 status_t status;
62
63 status = mPublisher->initialize();
64 ASSERT_EQ(OK, status)
65 << "publisher initialize should return OK";
66
67 status = mConsumer->initialize();
68 ASSERT_EQ(OK, status)
69 << "consumer initialize should return OK";
70}
71
72void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
73 status_t status;
74
75 const int32_t deviceId = 1;
76 const int32_t nature = INPUT_EVENT_NATURE_KEY;
77 const int32_t action = KEY_EVENT_ACTION_DOWN;
78 const int32_t flags = KEY_EVENT_FLAG_FROM_SYSTEM;
Jeff Brown8575a872010-06-30 16:10:35 -070079 const int32_t keyCode = AKEYCODE_ENTER;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070080 const int32_t scanCode = 13;
81 const int32_t metaState = META_ALT_LEFT_ON | META_ALT_ON;
82 const int32_t repeatCount = 1;
83 const nsecs_t downTime = 3;
84 const nsecs_t eventTime = 4;
85
86 status = mPublisher->publishKeyEvent(deviceId, nature, action, flags,
87 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
88 ASSERT_EQ(OK, status)
89 << "publisher publishKeyEvent should return OK";
90
91 status = mPublisher->sendDispatchSignal();
92 ASSERT_EQ(OK, status)
93 << "publisher sendDispatchSignal should return OK";
94
95 status = mConsumer->receiveDispatchSignal();
96 ASSERT_EQ(OK, status)
97 << "consumer receiveDispatchSignal should return OK";
98
99 InputEvent* event;
100 status = mConsumer->consume(& mEventFactory, & event);
101 ASSERT_EQ(OK, status)
102 << "consumer consume should return OK";
103
104 ASSERT_TRUE(event != NULL)
105 << "consumer should have returned non-NULL event";
106 ASSERT_EQ(INPUT_EVENT_TYPE_KEY, event->getType())
107 << "consumer should have returned a key event";
108
109 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
110 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
111 EXPECT_EQ(nature, keyEvent->getNature());
112 EXPECT_EQ(action, keyEvent->getAction());
113 EXPECT_EQ(flags, keyEvent->getFlags());
114 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
115 EXPECT_EQ(scanCode, keyEvent->getScanCode());
116 EXPECT_EQ(metaState, keyEvent->getMetaState());
117 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
118 EXPECT_EQ(downTime, keyEvent->getDownTime());
119 EXPECT_EQ(eventTime, keyEvent->getEventTime());
120
121 status = mConsumer->sendFinishedSignal();
122 ASSERT_EQ(OK, status)
123 << "consumer sendFinishedSignal should return OK";
124
125 status = mPublisher->receiveFinishedSignal();
126 ASSERT_EQ(OK, status)
127 << "publisher receiveFinishedSignal should return OK";
128
129 status = mPublisher->reset();
130 ASSERT_EQ(OK, status)
131 << "publisher reset should return OK";
132}
133
134void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent(
135 size_t samplesToAppendBeforeDispatch, size_t samplesToAppendAfterDispatch) {
136 status_t status;
137
138 const int32_t deviceId = 1;
139 const int32_t nature = INPUT_EVENT_NATURE_TOUCH;
140 const int32_t action = MOTION_EVENT_ACTION_MOVE;
141 const int32_t edgeFlags = MOTION_EVENT_EDGE_FLAG_TOP;
142 const int32_t metaState = META_ALT_LEFT_ON | META_ALT_ON;
143 const float xOffset = -10;
144 const float yOffset = -20;
145 const float xPrecision = 0.25;
146 const float yPrecision = 0.5;
147 const nsecs_t downTime = 3;
148 const size_t pointerCount = 3;
149 const int32_t pointerIds[pointerCount] = { 2, 0, 1 };
150
151 Vector<nsecs_t> sampleEventTimes;
152 Vector<PointerCoords> samplePointerCoords;
153
154 for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) {
155 sampleEventTimes.push(i + 10);
156 for (size_t j = 0; j < pointerCount; j++) {
157 samplePointerCoords.push();
158 samplePointerCoords.editTop().x = 100 * i + j;
159 samplePointerCoords.editTop().y = 200 * i + j;
160 samplePointerCoords.editTop().pressure = 0.5 * i + j;
161 samplePointerCoords.editTop().size = 0.7 * i + j;
162 }
163 }
164
165 status = mPublisher->publishMotionEvent(deviceId, nature, action, edgeFlags,
166 metaState, xOffset, yOffset, xPrecision, yPrecision,
167 downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array());
168 ASSERT_EQ(OK, status)
169 << "publisher publishMotionEvent should return OK";
170
171 for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) {
172 size_t sampleIndex = i + 1;
173 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
174 samplePointerCoords.array() + sampleIndex * pointerCount);
175 ASSERT_EQ(OK, status)
176 << "publisher appendMotionEvent should return OK";
177 }
178
179 status = mPublisher->sendDispatchSignal();
180 ASSERT_EQ(OK, status)
181 << "publisher sendDispatchSignal should return OK";
182
183 for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) {
184 size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch;
185 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
186 samplePointerCoords.array() + sampleIndex * pointerCount);
187 ASSERT_EQ(OK, status)
188 << "publisher appendMotionEvent should return OK";
189 }
190
191 status = mConsumer->receiveDispatchSignal();
192 ASSERT_EQ(OK, status)
193 << "consumer receiveDispatchSignal should return OK";
194
195 InputEvent* event;
196 status = mConsumer->consume(& mEventFactory, & event);
197 ASSERT_EQ(OK, status)
198 << "consumer consume should return OK";
199
200 ASSERT_TRUE(event != NULL)
201 << "consumer should have returned non-NULL event";
202 ASSERT_EQ(INPUT_EVENT_TYPE_MOTION, event->getType())
203 << "consumer should have returned a motion event";
204
205 size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch;
206
207 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
208 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
209 EXPECT_EQ(nature, motionEvent->getNature());
210 EXPECT_EQ(action, motionEvent->getAction());
211 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
212 EXPECT_EQ(metaState, motionEvent->getMetaState());
213 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
214 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
215 EXPECT_EQ(downTime, motionEvent->getDownTime());
216 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
217 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
218 EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize());
219
220 for (size_t i = 0; i < pointerCount; i++) {
221 SCOPED_TRACE(i);
222 EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i));
223 }
224
225 for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) {
226 SCOPED_TRACE(sampleIndex);
227 EXPECT_EQ(sampleEventTimes[sampleIndex],
228 motionEvent->getHistoricalEventTime(sampleIndex));
229 for (size_t i = 0; i < pointerCount; i++) {
230 SCOPED_TRACE(i);
231 size_t offset = sampleIndex * pointerCount + i;
232 EXPECT_EQ(samplePointerCoords[offset].x,
233 motionEvent->getHistoricalRawX(i, sampleIndex));
234 EXPECT_EQ(samplePointerCoords[offset].y,
235 motionEvent->getHistoricalRawY(i, sampleIndex));
236 EXPECT_EQ(samplePointerCoords[offset].x + xOffset,
237 motionEvent->getHistoricalX(i, sampleIndex));
238 EXPECT_EQ(samplePointerCoords[offset].y + yOffset,
239 motionEvent->getHistoricalY(i, sampleIndex));
240 EXPECT_EQ(samplePointerCoords[offset].pressure,
241 motionEvent->getHistoricalPressure(i, sampleIndex));
242 EXPECT_EQ(samplePointerCoords[offset].size,
243 motionEvent->getHistoricalSize(i, sampleIndex));
244 }
245 }
246
247 SCOPED_TRACE(lastSampleIndex);
248 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
249 for (size_t i = 0; i < pointerCount; i++) {
250 SCOPED_TRACE(i);
251 size_t offset = lastSampleIndex * pointerCount + i;
252 EXPECT_EQ(samplePointerCoords[offset].x, motionEvent->getRawX(i));
253 EXPECT_EQ(samplePointerCoords[offset].y, motionEvent->getRawY(i));
254 EXPECT_EQ(samplePointerCoords[offset].x + xOffset, motionEvent->getX(i));
255 EXPECT_EQ(samplePointerCoords[offset].y + yOffset, motionEvent->getY(i));
256 EXPECT_EQ(samplePointerCoords[offset].pressure, motionEvent->getPressure(i));
257 EXPECT_EQ(samplePointerCoords[offset].size, motionEvent->getSize(i));
258 }
259
260 status = mConsumer->sendFinishedSignal();
261 ASSERT_EQ(OK, status)
262 << "consumer sendFinishedSignal should return OK";
263
264 status = mPublisher->receiveFinishedSignal();
265 ASSERT_EQ(OK, status)
266 << "publisher receiveFinishedSignal should return OK";
267
268 status = mPublisher->reset();
269 ASSERT_EQ(OK, status)
270 << "publisher reset should return OK";
271}
272
273TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
274 ASSERT_NO_FATAL_FAILURE(Initialize());
275 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
276}
277
278TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) {
279 status_t status;
280 ASSERT_NO_FATAL_FAILURE(Initialize());
281
282 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
283 ASSERT_EQ(OK, status)
284 << "publisher publishKeyEvent should return OK first time";
285
286 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
287 ASSERT_EQ(INVALID_OPERATION, status)
288 << "publisher publishKeyEvent should return INVALID_OPERATION because "
289 "the publisher was not reset";
290}
291
292TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
293 ASSERT_NO_FATAL_FAILURE(Initialize());
294 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
295}
296
297TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) {
298 status_t status;
299 ASSERT_NO_FATAL_FAILURE(Initialize());
300
301 const size_t pointerCount = 1;
302 int32_t pointerIds[pointerCount] = { 0 };
303 PointerCoords pointerCoords[pointerCount] = { { 0, 0, 0, 0 } };
304
305 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
306 pointerCount, pointerIds, pointerCoords);
307 ASSERT_EQ(OK, status)
308 << "publisher publishMotionEvent should return OK";
309
310 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311 pointerCount, pointerIds, pointerCoords);
312 ASSERT_EQ(INVALID_OPERATION, status)
313 << "publisher publishMotionEvent should return INVALID_OPERATION because ";
314 "the publisher was not reset";
315}
316
317TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
318 status_t status;
319 ASSERT_NO_FATAL_FAILURE(Initialize());
320
321 const size_t pointerCount = 0;
322 int32_t pointerIds[pointerCount];
323 PointerCoords pointerCoords[pointerCount];
324
325 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
326 pointerCount, pointerIds, pointerCoords);
327 ASSERT_EQ(BAD_VALUE, status)
328 << "publisher publishMotionEvent should return BAD_VALUE";
329}
330
331TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
332 status_t status;
333 ASSERT_NO_FATAL_FAILURE(Initialize());
334
335 const size_t pointerCount = MAX_POINTERS + 1;
336 int32_t pointerIds[pointerCount];
337 PointerCoords pointerCoords[pointerCount];
338
339 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
340 pointerCount, pointerIds, pointerCoords);
341 ASSERT_EQ(BAD_VALUE, status)
342 << "publisher publishMotionEvent should return BAD_VALUE";
343}
344
345TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
346 ASSERT_NO_FATAL_FAILURE(Initialize());
347 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
348 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
349 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
350 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
351 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
352}
353
354TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) {
355 status_t status;
356 ASSERT_NO_FATAL_FAILURE(Initialize());
357 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0));
358}
359
360TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) {
361 status_t status;
362 ASSERT_NO_FATAL_FAILURE(Initialize());
363 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4));
364}
365
366TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) {
367 status_t status;
368 ASSERT_NO_FATAL_FAILURE(Initialize());
369
370 PointerCoords pointerCoords[1];
371 status = mPublisher->appendMotionSample(0, pointerCoords);
372 ASSERT_EQ(INVALID_OPERATION, status)
373 << "publisher appendMotionSample should return INVALID_OPERATION";
374}
375
376TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) {
377 status_t status;
378 ASSERT_NO_FATAL_FAILURE(Initialize());
379
380 const size_t pointerCount = MAX_POINTERS;
381 int32_t pointerIds[pointerCount];
382 PointerCoords pointerCoords[pointerCount];
383
384 status = mPublisher->publishMotionEvent(0, 0, MOTION_EVENT_ACTION_DOWN,
385 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
386 ASSERT_EQ(OK, status);
387
388 status = mPublisher->appendMotionSample(0, pointerCoords);
389 ASSERT_EQ(INVALID_OPERATION, status)
390 << "publisher appendMotionSample should return INVALID_OPERATION";
391}
392
393TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) {
394 status_t status;
395 ASSERT_NO_FATAL_FAILURE(Initialize());
396
397 const size_t pointerCount = MAX_POINTERS;
398 int32_t pointerIds[pointerCount];
399 PointerCoords pointerCoords[pointerCount];
400
401 status = mPublisher->publishMotionEvent(0, 0, MOTION_EVENT_ACTION_MOVE,
402 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
403 ASSERT_EQ(OK, status);
404
405 status = mPublisher->sendDispatchSignal();
406 ASSERT_EQ(OK, status);
407
408 status = mConsumer->receiveDispatchSignal();
409 ASSERT_EQ(OK, status);
410
411 InputEvent* event;
412 status = mConsumer->consume(& mEventFactory, & event);
413 ASSERT_EQ(OK, status);
414
415 status = mPublisher->appendMotionSample(0, pointerCoords);
416 ASSERT_EQ(status_t(FAILED_TRANSACTION), status)
417 << "publisher appendMotionSample should return FAILED_TRANSACTION";
418}
419
420TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) {
421 status_t status;
422 ASSERT_NO_FATAL_FAILURE(Initialize());
423
424 const size_t pointerCount = MAX_POINTERS;
425 int32_t pointerIds[pointerCount];
426 PointerCoords pointerCoords[pointerCount];
427
428 status = mPublisher->publishMotionEvent(0, 0, MOTION_EVENT_ACTION_MOVE,
429 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
430 ASSERT_EQ(OK, status);
431
432 for (int count = 1;; count++) {
433 ASSERT_LT(count, 100000) << "should eventually reach OOM";
434
435 status = mPublisher->appendMotionSample(0, pointerCoords);
436 if (status != OK) {
437 ASSERT_GT(count, 12) << "should be able to add at least a dozen samples";
438 ASSERT_EQ(NO_MEMORY, status)
439 << "publisher appendMotionSample should return NO_MEMORY when buffer is full";
440 break;
441 }
442 }
443
444 status = mPublisher->appendMotionSample(0, pointerCoords);
445 ASSERT_EQ(NO_MEMORY, status)
446 << "publisher appendMotionSample should return NO_MEMORY persistently until reset";
447}
448
449} // namespace android