blob: 1819a8be1698d17d205b8a743a5c79148fa499fb [file] [log] [blame]
Jeff Brown5c225b12010-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;
Jeff Brownc5ed5912010-07-14 18:48:53 -070076 const int32_t source = AINPUT_SOURCE_KEYBOARD;
77 const int32_t action = AKEY_EVENT_ACTION_DOWN;
78 const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Jeff Brownfd035822010-06-30 16:10:35 -070079 const int32_t keyCode = AKEYCODE_ENTER;
Jeff Brown5c225b12010-06-16 01:53:36 -070080 const int32_t scanCode = 13;
Jeff Brownc5ed5912010-07-14 18:48:53 -070081 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brown5c225b12010-06-16 01:53:36 -070082 const int32_t repeatCount = 1;
83 const nsecs_t downTime = 3;
84 const nsecs_t eventTime = 4;
85
Jeff Brownc5ed5912010-07-14 18:48:53 -070086 status = mPublisher->publishKeyEvent(deviceId, source, action, flags,
Jeff Brown5c225b12010-06-16 01:53:36 -070087 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";
Jeff Brownc5ed5912010-07-14 18:48:53 -0700106 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
Jeff Brown5c225b12010-06-16 01:53:36 -0700107 << "consumer should have returned a key event";
108
109 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
110 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
Jeff Brownc5ed5912010-07-14 18:48:53 -0700111 EXPECT_EQ(source, keyEvent->getSource());
Jeff Brown5c225b12010-06-16 01:53:36 -0700112 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
Jeff Brown3915bb82010-11-05 15:02:16 -0700121 status = mConsumer->sendFinishedSignal(true);
Jeff Brown5c225b12010-06-16 01:53:36 -0700122 ASSERT_EQ(OK, status)
123 << "consumer sendFinishedSignal should return OK";
124
Jeff Brown3915bb82010-11-05 15:02:16 -0700125 bool handled = false;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800126 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brown5c225b12010-06-16 01:53:36 -0700127 ASSERT_EQ(OK, status)
128 << "publisher receiveFinishedSignal should return OK";
Jeff Brown3915bb82010-11-05 15:02:16 -0700129 ASSERT_TRUE(handled)
130 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brown5c225b12010-06-16 01:53:36 -0700131
132 status = mPublisher->reset();
133 ASSERT_EQ(OK, status)
134 << "publisher reset should return OK";
135}
136
137void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent(
138 size_t samplesToAppendBeforeDispatch, size_t samplesToAppendAfterDispatch) {
139 status_t status;
140
141 const int32_t deviceId = 1;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700142 const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
143 const int32_t action = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown85a31762010-09-01 17:01:00 -0700144 const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700145 const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
146 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brown5c225b12010-06-16 01:53:36 -0700147 const float xOffset = -10;
148 const float yOffset = -20;
149 const float xPrecision = 0.25;
150 const float yPrecision = 0.5;
151 const nsecs_t downTime = 3;
152 const size_t pointerCount = 3;
153 const int32_t pointerIds[pointerCount] = { 2, 0, 1 };
154
155 Vector<nsecs_t> sampleEventTimes;
156 Vector<PointerCoords> samplePointerCoords;
157
158 for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) {
159 sampleEventTimes.push(i + 10);
160 for (size_t j = 0; j < pointerCount; j++) {
161 samplePointerCoords.push();
Jeff Brown91c69ab2011-02-14 17:03:18 -0800162 PointerCoords& pc = samplePointerCoords.editTop();
163 pc.clear();
164 pc.setAxisValue(AINPUT_MOTION_AXIS_X, 100 * i + j);
165 pc.setAxisValue(AINPUT_MOTION_AXIS_Y, 200 * i + j);
166 pc.setAxisValue(AINPUT_MOTION_AXIS_PRESSURE, 0.5 * i + j);
167 pc.setAxisValue(AINPUT_MOTION_AXIS_SIZE, 0.7 * i + j);
168 pc.setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR, 1.5 * i + j);
169 pc.setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR, 1.7 * i + j);
170 pc.setAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR, 2.5 * i + j);
171 pc.setAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR, 2.7 * i + j);
172 pc.setAxisValue(AINPUT_MOTION_AXIS_ORIENTATION, 3.5 * i + j);
Jeff Brown5c225b12010-06-16 01:53:36 -0700173 }
174 }
175
Jeff Brown85a31762010-09-01 17:01:00 -0700176 status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags,
Jeff Brown5c225b12010-06-16 01:53:36 -0700177 metaState, xOffset, yOffset, xPrecision, yPrecision,
178 downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array());
179 ASSERT_EQ(OK, status)
180 << "publisher publishMotionEvent should return OK";
181
182 for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) {
183 size_t sampleIndex = i + 1;
184 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
185 samplePointerCoords.array() + sampleIndex * pointerCount);
186 ASSERT_EQ(OK, status)
187 << "publisher appendMotionEvent should return OK";
188 }
189
190 status = mPublisher->sendDispatchSignal();
191 ASSERT_EQ(OK, status)
192 << "publisher sendDispatchSignal should return OK";
193
194 for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) {
195 size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch;
196 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
197 samplePointerCoords.array() + sampleIndex * pointerCount);
198 ASSERT_EQ(OK, status)
199 << "publisher appendMotionEvent should return OK";
200 }
201
202 status = mConsumer->receiveDispatchSignal();
203 ASSERT_EQ(OK, status)
204 << "consumer receiveDispatchSignal should return OK";
205
206 InputEvent* event;
207 status = mConsumer->consume(& mEventFactory, & event);
208 ASSERT_EQ(OK, status)
209 << "consumer consume should return OK";
210
211 ASSERT_TRUE(event != NULL)
212 << "consumer should have returned non-NULL event";
Jeff Brownc5ed5912010-07-14 18:48:53 -0700213 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brown5c225b12010-06-16 01:53:36 -0700214 << "consumer should have returned a motion event";
215
216 size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch;
217
218 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
219 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brownc5ed5912010-07-14 18:48:53 -0700220 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brown5c225b12010-06-16 01:53:36 -0700221 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brown85a31762010-09-01 17:01:00 -0700222 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brown5c225b12010-06-16 01:53:36 -0700223 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
224 EXPECT_EQ(metaState, motionEvent->getMetaState());
225 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
226 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
227 EXPECT_EQ(downTime, motionEvent->getDownTime());
228 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
229 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
230 EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize());
231
232 for (size_t i = 0; i < pointerCount; i++) {
233 SCOPED_TRACE(i);
234 EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i));
235 }
236
237 for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) {
238 SCOPED_TRACE(sampleIndex);
239 EXPECT_EQ(sampleEventTimes[sampleIndex],
240 motionEvent->getHistoricalEventTime(sampleIndex));
241 for (size_t i = 0; i < pointerCount; i++) {
242 SCOPED_TRACE(i);
243 size_t offset = sampleIndex * pointerCount + i;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800244 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X),
Jeff Brown5c225b12010-06-16 01:53:36 -0700245 motionEvent->getHistoricalRawX(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800246 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y),
Jeff Brown5c225b12010-06-16 01:53:36 -0700247 motionEvent->getHistoricalRawY(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800248 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X) + xOffset,
Jeff Brown5c225b12010-06-16 01:53:36 -0700249 motionEvent->getHistoricalX(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800250 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y) + yOffset,
Jeff Brown5c225b12010-06-16 01:53:36 -0700251 motionEvent->getHistoricalY(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800252 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_PRESSURE),
Jeff Brown5c225b12010-06-16 01:53:36 -0700253 motionEvent->getHistoricalPressure(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800254 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_SIZE),
Jeff Brown5c225b12010-06-16 01:53:36 -0700255 motionEvent->getHistoricalSize(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800256 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR),
Jeff Brownc5ed5912010-07-14 18:48:53 -0700257 motionEvent->getHistoricalTouchMajor(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800258 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR),
Jeff Brownc5ed5912010-07-14 18:48:53 -0700259 motionEvent->getHistoricalTouchMinor(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800260 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR),
Jeff Brownc5ed5912010-07-14 18:48:53 -0700261 motionEvent->getHistoricalToolMajor(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800262 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR),
Jeff Brownc5ed5912010-07-14 18:48:53 -0700263 motionEvent->getHistoricalToolMinor(i, sampleIndex));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800264 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_ORIENTATION),
Jeff Brownc5ed5912010-07-14 18:48:53 -0700265 motionEvent->getHistoricalOrientation(i, sampleIndex));
Jeff Brown5c225b12010-06-16 01:53:36 -0700266 }
267 }
268
269 SCOPED_TRACE(lastSampleIndex);
270 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
271 for (size_t i = 0; i < pointerCount; i++) {
272 SCOPED_TRACE(i);
273 size_t offset = lastSampleIndex * pointerCount + i;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800274 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X),
275 motionEvent->getRawX(i));
276 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y),
277 motionEvent->getRawY(i));
278 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X) + xOffset,
279 motionEvent->getX(i));
280 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y) + yOffset,
281 motionEvent->getY(i));
282 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_PRESSURE),
283 motionEvent->getPressure(i));
284 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_SIZE),
285 motionEvent->getSize(i));
286 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR),
287 motionEvent->getTouchMajor(i));
288 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR),
289 motionEvent->getTouchMinor(i));
290 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR),
291 motionEvent->getToolMajor(i));
292 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR),
293 motionEvent->getToolMinor(i));
294 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_ORIENTATION),
295 motionEvent->getOrientation(i));
Jeff Brown5c225b12010-06-16 01:53:36 -0700296 }
297
Jeff Brown3915bb82010-11-05 15:02:16 -0700298 status = mConsumer->sendFinishedSignal(false);
Jeff Brown5c225b12010-06-16 01:53:36 -0700299 ASSERT_EQ(OK, status)
300 << "consumer sendFinishedSignal should return OK";
301
Jeff Brown3915bb82010-11-05 15:02:16 -0700302 bool handled = true;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800303 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brown5c225b12010-06-16 01:53:36 -0700304 ASSERT_EQ(OK, status)
305 << "publisher receiveFinishedSignal should return OK";
Jeff Brown3915bb82010-11-05 15:02:16 -0700306 ASSERT_FALSE(handled)
307 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brown5c225b12010-06-16 01:53:36 -0700308
309 status = mPublisher->reset();
310 ASSERT_EQ(OK, status)
311 << "publisher reset should return OK";
312}
313
314TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
315 ASSERT_NO_FATAL_FAILURE(Initialize());
316 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
317}
318
319TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) {
320 status_t status;
321 ASSERT_NO_FATAL_FAILURE(Initialize());
322
323 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
324 ASSERT_EQ(OK, status)
325 << "publisher publishKeyEvent should return OK first time";
326
327 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
328 ASSERT_EQ(INVALID_OPERATION, status)
329 << "publisher publishKeyEvent should return INVALID_OPERATION because "
330 "the publisher was not reset";
331}
332
333TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
334 ASSERT_NO_FATAL_FAILURE(Initialize());
335 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
336}
337
338TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) {
339 status_t status;
340 ASSERT_NO_FATAL_FAILURE(Initialize());
341
342 const size_t pointerCount = 1;
343 int32_t pointerIds[pointerCount] = { 0 };
Jeff Brown91c69ab2011-02-14 17:03:18 -0800344 PointerCoords pointerCoords[pointerCount];
345 pointerCoords[0].clear();
Jeff Brown5c225b12010-06-16 01:53:36 -0700346
Jeff Brown85a31762010-09-01 17:01:00 -0700347 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5c225b12010-06-16 01:53:36 -0700348 pointerCount, pointerIds, pointerCoords);
349 ASSERT_EQ(OK, status)
350 << "publisher publishMotionEvent should return OK";
351
Jeff Brown85a31762010-09-01 17:01:00 -0700352 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5c225b12010-06-16 01:53:36 -0700353 pointerCount, pointerIds, pointerCoords);
354 ASSERT_EQ(INVALID_OPERATION, status)
355 << "publisher publishMotionEvent should return INVALID_OPERATION because ";
356 "the publisher was not reset";
357}
358
359TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
360 status_t status;
361 ASSERT_NO_FATAL_FAILURE(Initialize());
362
363 const size_t pointerCount = 0;
364 int32_t pointerIds[pointerCount];
365 PointerCoords pointerCoords[pointerCount];
366
Jeff Brown85a31762010-09-01 17:01:00 -0700367 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5c225b12010-06-16 01:53:36 -0700368 pointerCount, pointerIds, pointerCoords);
369 ASSERT_EQ(BAD_VALUE, status)
370 << "publisher publishMotionEvent should return BAD_VALUE";
371}
372
373TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
374 status_t status;
375 ASSERT_NO_FATAL_FAILURE(Initialize());
376
377 const size_t pointerCount = MAX_POINTERS + 1;
378 int32_t pointerIds[pointerCount];
379 PointerCoords pointerCoords[pointerCount];
380
Jeff Brown85a31762010-09-01 17:01:00 -0700381 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5c225b12010-06-16 01:53:36 -0700382 pointerCount, pointerIds, pointerCoords);
383 ASSERT_EQ(BAD_VALUE, status)
384 << "publisher publishMotionEvent should return BAD_VALUE";
385}
386
387TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
388 ASSERT_NO_FATAL_FAILURE(Initialize());
389 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
390 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
391 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
392 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
393 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
394}
395
396TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) {
397 status_t status;
398 ASSERT_NO_FATAL_FAILURE(Initialize());
399 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0));
400}
401
402TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) {
403 status_t status;
404 ASSERT_NO_FATAL_FAILURE(Initialize());
405 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4));
406}
407
408TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) {
409 status_t status;
410 ASSERT_NO_FATAL_FAILURE(Initialize());
411
412 PointerCoords pointerCoords[1];
413 status = mPublisher->appendMotionSample(0, pointerCoords);
414 ASSERT_EQ(INVALID_OPERATION, status)
415 << "publisher appendMotionSample should return INVALID_OPERATION";
416}
417
418TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) {
419 status_t status;
420 ASSERT_NO_FATAL_FAILURE(Initialize());
421
422 const size_t pointerCount = MAX_POINTERS;
423 int32_t pointerIds[pointerCount];
424 PointerCoords pointerCoords[pointerCount];
425
Jeff Brownc5ed5912010-07-14 18:48:53 -0700426 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_DOWN,
Jeff Brown85a31762010-09-01 17:01:00 -0700427 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700428 ASSERT_EQ(OK, status);
429
430 status = mPublisher->appendMotionSample(0, pointerCoords);
431 ASSERT_EQ(INVALID_OPERATION, status)
432 << "publisher appendMotionSample should return INVALID_OPERATION";
433}
434
435TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) {
436 status_t status;
437 ASSERT_NO_FATAL_FAILURE(Initialize());
438
439 const size_t pointerCount = MAX_POINTERS;
440 int32_t pointerIds[pointerCount];
441 PointerCoords pointerCoords[pointerCount];
442
Jeff Brownc5ed5912010-07-14 18:48:53 -0700443 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brown85a31762010-09-01 17:01:00 -0700444 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700445 ASSERT_EQ(OK, status);
446
447 status = mPublisher->sendDispatchSignal();
448 ASSERT_EQ(OK, status);
449
450 status = mConsumer->receiveDispatchSignal();
451 ASSERT_EQ(OK, status);
452
453 InputEvent* event;
454 status = mConsumer->consume(& mEventFactory, & event);
455 ASSERT_EQ(OK, status);
456
457 status = mPublisher->appendMotionSample(0, pointerCoords);
458 ASSERT_EQ(status_t(FAILED_TRANSACTION), status)
459 << "publisher appendMotionSample should return FAILED_TRANSACTION";
460}
461
462TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) {
463 status_t status;
464 ASSERT_NO_FATAL_FAILURE(Initialize());
465
466 const size_t pointerCount = MAX_POINTERS;
467 int32_t pointerIds[pointerCount];
468 PointerCoords pointerCoords[pointerCount];
469
Jeff Brownc5ed5912010-07-14 18:48:53 -0700470 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brown85a31762010-09-01 17:01:00 -0700471 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700472 ASSERT_EQ(OK, status);
473
474 for (int count = 1;; count++) {
475 ASSERT_LT(count, 100000) << "should eventually reach OOM";
476
477 status = mPublisher->appendMotionSample(0, pointerCoords);
478 if (status != OK) {
479 ASSERT_GT(count, 12) << "should be able to add at least a dozen samples";
480 ASSERT_EQ(NO_MEMORY, status)
481 << "publisher appendMotionSample should return NO_MEMORY when buffer is full";
482 break;
483 }
484 }
485
486 status = mPublisher->appendMotionSample(0, pointerCoords);
487 ASSERT_EQ(NO_MEMORY, status)
488 << "publisher appendMotionSample should return NO_MEMORY persistently until reset";
489}
490
491} // namespace android