blob: c6eac25e1896501ab6421d9e213bbb37fa148d8c [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;
Jeff Brown5c1ed842010-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 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;
Jeff Brown5c1ed842010-07-14 18:48:53 -070081 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brownf4a4ec22010-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 Brown5c1ed842010-07-14 18:48:53 -070086 status = mPublisher->publishKeyEvent(deviceId, source, action, flags,
Jeff Brownf4a4ec22010-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 Brown5c1ed842010-07-14 18:48:53 -0700106 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
Jeff Brownf4a4ec22010-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 Brown5c1ed842010-07-14 18:48:53 -0700111 EXPECT_EQ(source, keyEvent->getSource());
Jeff Brownf4a4ec22010-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 Brown81499912010-11-05 15:02:16 -0700121 status = mConsumer->sendFinishedSignal(true);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700122 ASSERT_EQ(OK, status)
123 << "consumer sendFinishedSignal should return OK";
124
Jeff Brown81499912010-11-05 15:02:16 -0700125 bool handled = false;
126 status = mPublisher->receiveFinishedSignal(handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700127 ASSERT_EQ(OK, status)
128 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700129 ASSERT_TRUE(handled)
130 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-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 Brown5c1ed842010-07-14 18:48:53 -0700142 const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
143 const int32_t action = AMOTION_EVENT_ACTION_MOVE;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700144 const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
Jeff Brown5c1ed842010-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 Brownf4a4ec22010-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();
162 samplePointerCoords.editTop().x = 100 * i + j;
163 samplePointerCoords.editTop().y = 200 * i + j;
164 samplePointerCoords.editTop().pressure = 0.5 * i + j;
165 samplePointerCoords.editTop().size = 0.7 * i + j;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700166 samplePointerCoords.editTop().touchMajor = 1.5 * i + j;
167 samplePointerCoords.editTop().touchMinor = 1.7 * i + j;
168 samplePointerCoords.editTop().toolMajor = 2.5 * i + j;
169 samplePointerCoords.editTop().toolMinor = 2.7 * i + j;
170 samplePointerCoords.editTop().orientation = 3.5 * i + j;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700171 }
172 }
173
Jeff Brownaf30ff62010-09-01 17:01:00 -0700174 status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700175 metaState, xOffset, yOffset, xPrecision, yPrecision,
176 downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array());
177 ASSERT_EQ(OK, status)
178 << "publisher publishMotionEvent should return OK";
179
180 for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) {
181 size_t sampleIndex = i + 1;
182 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
183 samplePointerCoords.array() + sampleIndex * pointerCount);
184 ASSERT_EQ(OK, status)
185 << "publisher appendMotionEvent should return OK";
186 }
187
188 status = mPublisher->sendDispatchSignal();
189 ASSERT_EQ(OK, status)
190 << "publisher sendDispatchSignal should return OK";
191
192 for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) {
193 size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch;
194 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
195 samplePointerCoords.array() + sampleIndex * pointerCount);
196 ASSERT_EQ(OK, status)
197 << "publisher appendMotionEvent should return OK";
198 }
199
200 status = mConsumer->receiveDispatchSignal();
201 ASSERT_EQ(OK, status)
202 << "consumer receiveDispatchSignal should return OK";
203
204 InputEvent* event;
205 status = mConsumer->consume(& mEventFactory, & event);
206 ASSERT_EQ(OK, status)
207 << "consumer consume should return OK";
208
209 ASSERT_TRUE(event != NULL)
210 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -0700211 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700212 << "consumer should have returned a motion event";
213
214 size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch;
215
216 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
217 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700218 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700219 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brownaf30ff62010-09-01 17:01:00 -0700220 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700221 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
222 EXPECT_EQ(metaState, motionEvent->getMetaState());
223 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
224 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
225 EXPECT_EQ(downTime, motionEvent->getDownTime());
226 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
227 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
228 EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize());
229
230 for (size_t i = 0; i < pointerCount; i++) {
231 SCOPED_TRACE(i);
232 EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i));
233 }
234
235 for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) {
236 SCOPED_TRACE(sampleIndex);
237 EXPECT_EQ(sampleEventTimes[sampleIndex],
238 motionEvent->getHistoricalEventTime(sampleIndex));
239 for (size_t i = 0; i < pointerCount; i++) {
240 SCOPED_TRACE(i);
241 size_t offset = sampleIndex * pointerCount + i;
242 EXPECT_EQ(samplePointerCoords[offset].x,
243 motionEvent->getHistoricalRawX(i, sampleIndex));
244 EXPECT_EQ(samplePointerCoords[offset].y,
245 motionEvent->getHistoricalRawY(i, sampleIndex));
246 EXPECT_EQ(samplePointerCoords[offset].x + xOffset,
247 motionEvent->getHistoricalX(i, sampleIndex));
248 EXPECT_EQ(samplePointerCoords[offset].y + yOffset,
249 motionEvent->getHistoricalY(i, sampleIndex));
250 EXPECT_EQ(samplePointerCoords[offset].pressure,
251 motionEvent->getHistoricalPressure(i, sampleIndex));
252 EXPECT_EQ(samplePointerCoords[offset].size,
253 motionEvent->getHistoricalSize(i, sampleIndex));
Jeff Brown5c1ed842010-07-14 18:48:53 -0700254 EXPECT_EQ(samplePointerCoords[offset].touchMajor,
255 motionEvent->getHistoricalTouchMajor(i, sampleIndex));
256 EXPECT_EQ(samplePointerCoords[offset].touchMinor,
257 motionEvent->getHistoricalTouchMinor(i, sampleIndex));
258 EXPECT_EQ(samplePointerCoords[offset].toolMajor,
259 motionEvent->getHistoricalToolMajor(i, sampleIndex));
260 EXPECT_EQ(samplePointerCoords[offset].toolMinor,
261 motionEvent->getHistoricalToolMinor(i, sampleIndex));
262 EXPECT_EQ(samplePointerCoords[offset].orientation,
263 motionEvent->getHistoricalOrientation(i, sampleIndex));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700264 }
265 }
266
267 SCOPED_TRACE(lastSampleIndex);
268 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
269 for (size_t i = 0; i < pointerCount; i++) {
270 SCOPED_TRACE(i);
271 size_t offset = lastSampleIndex * pointerCount + i;
272 EXPECT_EQ(samplePointerCoords[offset].x, motionEvent->getRawX(i));
273 EXPECT_EQ(samplePointerCoords[offset].y, motionEvent->getRawY(i));
274 EXPECT_EQ(samplePointerCoords[offset].x + xOffset, motionEvent->getX(i));
275 EXPECT_EQ(samplePointerCoords[offset].y + yOffset, motionEvent->getY(i));
276 EXPECT_EQ(samplePointerCoords[offset].pressure, motionEvent->getPressure(i));
277 EXPECT_EQ(samplePointerCoords[offset].size, motionEvent->getSize(i));
Jeff Brown5c1ed842010-07-14 18:48:53 -0700278 EXPECT_EQ(samplePointerCoords[offset].touchMajor, motionEvent->getTouchMajor(i));
279 EXPECT_EQ(samplePointerCoords[offset].touchMinor, motionEvent->getTouchMinor(i));
280 EXPECT_EQ(samplePointerCoords[offset].toolMajor, motionEvent->getToolMajor(i));
281 EXPECT_EQ(samplePointerCoords[offset].toolMinor, motionEvent->getToolMinor(i));
282 EXPECT_EQ(samplePointerCoords[offset].orientation, motionEvent->getOrientation(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700283 }
284
Jeff Brown81499912010-11-05 15:02:16 -0700285 status = mConsumer->sendFinishedSignal(false);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700286 ASSERT_EQ(OK, status)
287 << "consumer sendFinishedSignal should return OK";
288
Jeff Brown81499912010-11-05 15:02:16 -0700289 bool handled = true;
290 status = mPublisher->receiveFinishedSignal(handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700291 ASSERT_EQ(OK, status)
292 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700293 ASSERT_FALSE(handled)
294 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700295
296 status = mPublisher->reset();
297 ASSERT_EQ(OK, status)
298 << "publisher reset should return OK";
299}
300
301TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
302 ASSERT_NO_FATAL_FAILURE(Initialize());
303 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
304}
305
306TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) {
307 status_t status;
308 ASSERT_NO_FATAL_FAILURE(Initialize());
309
310 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
311 ASSERT_EQ(OK, status)
312 << "publisher publishKeyEvent should return OK first time";
313
314 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
315 ASSERT_EQ(INVALID_OPERATION, status)
316 << "publisher publishKeyEvent should return INVALID_OPERATION because "
317 "the publisher was not reset";
318}
319
320TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
321 ASSERT_NO_FATAL_FAILURE(Initialize());
322 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
323}
324
325TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) {
326 status_t status;
327 ASSERT_NO_FATAL_FAILURE(Initialize());
328
329 const size_t pointerCount = 1;
330 int32_t pointerIds[pointerCount] = { 0 };
Jeff Brown5c1ed842010-07-14 18:48:53 -0700331 PointerCoords pointerCoords[pointerCount] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700332
Jeff Brownaf30ff62010-09-01 17:01:00 -0700333 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700334 pointerCount, pointerIds, pointerCoords);
335 ASSERT_EQ(OK, status)
336 << "publisher publishMotionEvent should return OK";
337
Jeff Brownaf30ff62010-09-01 17:01:00 -0700338 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700339 pointerCount, pointerIds, pointerCoords);
340 ASSERT_EQ(INVALID_OPERATION, status)
341 << "publisher publishMotionEvent should return INVALID_OPERATION because ";
342 "the publisher was not reset";
343}
344
345TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
346 status_t status;
347 ASSERT_NO_FATAL_FAILURE(Initialize());
348
349 const size_t pointerCount = 0;
350 int32_t pointerIds[pointerCount];
351 PointerCoords pointerCoords[pointerCount];
352
Jeff Brownaf30ff62010-09-01 17:01:00 -0700353 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700354 pointerCount, pointerIds, pointerCoords);
355 ASSERT_EQ(BAD_VALUE, status)
356 << "publisher publishMotionEvent should return BAD_VALUE";
357}
358
359TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
360 status_t status;
361 ASSERT_NO_FATAL_FAILURE(Initialize());
362
363 const size_t pointerCount = MAX_POINTERS + 1;
364 int32_t pointerIds[pointerCount];
365 PointerCoords pointerCoords[pointerCount];
366
Jeff Brownaf30ff62010-09-01 17:01:00 -0700367 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-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, PublishMultipleEvents_EndToEnd) {
374 ASSERT_NO_FATAL_FAILURE(Initialize());
375 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
376 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
377 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
378 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
379 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
380}
381
382TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) {
383 status_t status;
384 ASSERT_NO_FATAL_FAILURE(Initialize());
385 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0));
386}
387
388TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) {
389 status_t status;
390 ASSERT_NO_FATAL_FAILURE(Initialize());
391 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4));
392}
393
394TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) {
395 status_t status;
396 ASSERT_NO_FATAL_FAILURE(Initialize());
397
398 PointerCoords pointerCoords[1];
399 status = mPublisher->appendMotionSample(0, pointerCoords);
400 ASSERT_EQ(INVALID_OPERATION, status)
401 << "publisher appendMotionSample should return INVALID_OPERATION";
402}
403
404TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) {
405 status_t status;
406 ASSERT_NO_FATAL_FAILURE(Initialize());
407
408 const size_t pointerCount = MAX_POINTERS;
409 int32_t pointerIds[pointerCount];
410 PointerCoords pointerCoords[pointerCount];
411
Jeff Brown5c1ed842010-07-14 18:48:53 -0700412 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_DOWN,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700413 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700414 ASSERT_EQ(OK, status);
415
416 status = mPublisher->appendMotionSample(0, pointerCoords);
417 ASSERT_EQ(INVALID_OPERATION, status)
418 << "publisher appendMotionSample should return INVALID_OPERATION";
419}
420
421TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) {
422 status_t status;
423 ASSERT_NO_FATAL_FAILURE(Initialize());
424
425 const size_t pointerCount = MAX_POINTERS;
426 int32_t pointerIds[pointerCount];
427 PointerCoords pointerCoords[pointerCount];
428
Jeff Brown5c1ed842010-07-14 18:48:53 -0700429 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700430 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700431 ASSERT_EQ(OK, status);
432
433 status = mPublisher->sendDispatchSignal();
434 ASSERT_EQ(OK, status);
435
436 status = mConsumer->receiveDispatchSignal();
437 ASSERT_EQ(OK, status);
438
439 InputEvent* event;
440 status = mConsumer->consume(& mEventFactory, & event);
441 ASSERT_EQ(OK, status);
442
443 status = mPublisher->appendMotionSample(0, pointerCoords);
444 ASSERT_EQ(status_t(FAILED_TRANSACTION), status)
445 << "publisher appendMotionSample should return FAILED_TRANSACTION";
446}
447
448TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) {
449 status_t status;
450 ASSERT_NO_FATAL_FAILURE(Initialize());
451
452 const size_t pointerCount = MAX_POINTERS;
453 int32_t pointerIds[pointerCount];
454 PointerCoords pointerCoords[pointerCount];
455
Jeff Brown5c1ed842010-07-14 18:48:53 -0700456 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700457 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700458 ASSERT_EQ(OK, status);
459
460 for (int count = 1;; count++) {
461 ASSERT_LT(count, 100000) << "should eventually reach OOM";
462
463 status = mPublisher->appendMotionSample(0, pointerCoords);
464 if (status != OK) {
465 ASSERT_GT(count, 12) << "should be able to add at least a dozen samples";
466 ASSERT_EQ(NO_MEMORY, status)
467 << "publisher appendMotionSample should return NO_MEMORY when buffer is full";
468 break;
469 }
470 }
471
472 status = mPublisher->appendMotionSample(0, pointerCoords);
473 ASSERT_EQ(NO_MEMORY, status)
474 << "publisher appendMotionSample should return NO_MEMORY persistently until reset";
475}
476
477} // namespace android