blob: 6e18a4f1abb6decdcda4bec456d25be3168ce6cc [file] [log] [blame]
Jeff Brownb2d44352011-02-17 13:01:34 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeff Brownf4a4ec22010-06-16 01:53:36 -070016
17#include <ui/InputTransport.h>
18#include <utils/Timers.h>
19#include <utils/StopWatch.h>
20#include <gtest/gtest.h>
21#include <unistd.h>
22#include <time.h>
23#include <sys/mman.h>
24#include <cutils/ashmem.h>
25
26#include "../../utils/tests/TestHelpers.h"
27
28namespace android {
29
30class InputPublisherAndConsumerTest : public testing::Test {
31protected:
32 sp<InputChannel> serverChannel, clientChannel;
33 InputPublisher* mPublisher;
34 InputConsumer* mConsumer;
35 PreallocatedInputEventFactory mEventFactory;
36
37 virtual void SetUp() {
38 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
39 serverChannel, clientChannel);
40
41 mPublisher = new InputPublisher(serverChannel);
42 mConsumer = new InputConsumer(clientChannel);
43 }
44
45 virtual void TearDown() {
46 if (mPublisher) {
47 delete mPublisher;
48 mPublisher = NULL;
49 }
50
51 if (mConsumer) {
52 delete mConsumer;
53 mConsumer = NULL;
54 }
55
56 serverChannel.clear();
57 clientChannel.clear();
58 }
59
60 void Initialize();
61 void PublishAndConsumeKeyEvent();
62 void PublishAndConsumeMotionEvent(
63 size_t samplesToAppendBeforeDispatch = 0,
64 size_t samplesToAppendAfterDispatch = 0);
65};
66
67TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
68 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
69 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
70}
71
72void InputPublisherAndConsumerTest::Initialize() {
73 status_t status;
74
75 status = mPublisher->initialize();
76 ASSERT_EQ(OK, status)
77 << "publisher initialize should return OK";
78
79 status = mConsumer->initialize();
80 ASSERT_EQ(OK, status)
81 << "consumer initialize should return OK";
82}
83
84void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
85 status_t status;
86
87 const int32_t deviceId = 1;
Jeff Brown5c1ed842010-07-14 18:48:53 -070088 const int32_t source = AINPUT_SOURCE_KEYBOARD;
89 const int32_t action = AKEY_EVENT_ACTION_DOWN;
90 const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Jeff Brown8575a872010-06-30 16:10:35 -070091 const int32_t keyCode = AKEYCODE_ENTER;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070092 const int32_t scanCode = 13;
Jeff Brown5c1ed842010-07-14 18:48:53 -070093 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070094 const int32_t repeatCount = 1;
95 const nsecs_t downTime = 3;
96 const nsecs_t eventTime = 4;
97
Jeff Brown5c1ed842010-07-14 18:48:53 -070098 status = mPublisher->publishKeyEvent(deviceId, source, action, flags,
Jeff Brownf4a4ec22010-06-16 01:53:36 -070099 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
100 ASSERT_EQ(OK, status)
101 << "publisher publishKeyEvent should return OK";
102
103 status = mPublisher->sendDispatchSignal();
104 ASSERT_EQ(OK, status)
105 << "publisher sendDispatchSignal should return OK";
106
107 status = mConsumer->receiveDispatchSignal();
108 ASSERT_EQ(OK, status)
109 << "consumer receiveDispatchSignal should return OK";
110
111 InputEvent* event;
112 status = mConsumer->consume(& mEventFactory, & event);
113 ASSERT_EQ(OK, status)
114 << "consumer consume should return OK";
115
116 ASSERT_TRUE(event != NULL)
117 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -0700118 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700119 << "consumer should have returned a key event";
120
121 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
122 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700123 EXPECT_EQ(source, keyEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700124 EXPECT_EQ(action, keyEvent->getAction());
125 EXPECT_EQ(flags, keyEvent->getFlags());
126 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
127 EXPECT_EQ(scanCode, keyEvent->getScanCode());
128 EXPECT_EQ(metaState, keyEvent->getMetaState());
129 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
130 EXPECT_EQ(downTime, keyEvent->getDownTime());
131 EXPECT_EQ(eventTime, keyEvent->getEventTime());
132
Jeff Brown81499912010-11-05 15:02:16 -0700133 status = mConsumer->sendFinishedSignal(true);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700134 ASSERT_EQ(OK, status)
135 << "consumer sendFinishedSignal should return OK";
136
Jeff Brown81499912010-11-05 15:02:16 -0700137 bool handled = false;
Jeff Brown02d85b52010-12-06 17:13:33 -0800138 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700139 ASSERT_EQ(OK, status)
140 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700141 ASSERT_TRUE(handled)
142 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700143
144 status = mPublisher->reset();
145 ASSERT_EQ(OK, status)
146 << "publisher reset should return OK";
147}
148
149void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent(
150 size_t samplesToAppendBeforeDispatch, size_t samplesToAppendAfterDispatch) {
151 status_t status;
152
153 const int32_t deviceId = 1;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700154 const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
155 const int32_t action = AMOTION_EVENT_ACTION_MOVE;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700156 const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700157 const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
158 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700159 const float xOffset = -10;
160 const float yOffset = -20;
161 const float xPrecision = 0.25;
162 const float yPrecision = 0.5;
163 const nsecs_t downTime = 3;
164 const size_t pointerCount = 3;
165 const int32_t pointerIds[pointerCount] = { 2, 0, 1 };
166
167 Vector<nsecs_t> sampleEventTimes;
168 Vector<PointerCoords> samplePointerCoords;
169
170 for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) {
171 sampleEventTimes.push(i + 10);
172 for (size_t j = 0; j < pointerCount; j++) {
173 samplePointerCoords.push();
Jeff Brown3e341462011-02-14 17:03:18 -0800174 PointerCoords& pc = samplePointerCoords.editTop();
175 pc.clear();
Jeff Brownb2d44352011-02-17 13:01:34 -0800176 pc.setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i + j);
177 pc.setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i + j);
178 pc.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i + j);
179 pc.setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i + j);
180 pc.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i + j);
181 pc.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i + j);
182 pc.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i + j);
183 pc.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i + j);
184 pc.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i + j);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700185 }
186 }
187
Jeff Brownaf30ff62010-09-01 17:01:00 -0700188 status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700189 metaState, xOffset, yOffset, xPrecision, yPrecision,
190 downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array());
191 ASSERT_EQ(OK, status)
192 << "publisher publishMotionEvent should return OK";
193
194 for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) {
195 size_t sampleIndex = i + 1;
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 = mPublisher->sendDispatchSignal();
203 ASSERT_EQ(OK, status)
204 << "publisher sendDispatchSignal should return OK";
205
206 for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) {
207 size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch;
208 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
209 samplePointerCoords.array() + sampleIndex * pointerCount);
210 ASSERT_EQ(OK, status)
211 << "publisher appendMotionEvent should return OK";
212 }
213
214 status = mConsumer->receiveDispatchSignal();
215 ASSERT_EQ(OK, status)
216 << "consumer receiveDispatchSignal should return OK";
217
218 InputEvent* event;
219 status = mConsumer->consume(& mEventFactory, & event);
220 ASSERT_EQ(OK, status)
221 << "consumer consume should return OK";
222
223 ASSERT_TRUE(event != NULL)
224 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -0700225 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700226 << "consumer should have returned a motion event";
227
228 size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch;
229
230 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
231 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700232 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700233 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brownaf30ff62010-09-01 17:01:00 -0700234 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700235 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
236 EXPECT_EQ(metaState, motionEvent->getMetaState());
237 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
238 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
239 EXPECT_EQ(downTime, motionEvent->getDownTime());
240 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
241 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
242 EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize());
243
244 for (size_t i = 0; i < pointerCount; i++) {
245 SCOPED_TRACE(i);
246 EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i));
247 }
248
249 for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) {
250 SCOPED_TRACE(sampleIndex);
251 EXPECT_EQ(sampleEventTimes[sampleIndex],
252 motionEvent->getHistoricalEventTime(sampleIndex));
253 for (size_t i = 0; i < pointerCount; i++) {
254 SCOPED_TRACE(i);
255 size_t offset = sampleIndex * pointerCount + i;
Jeff Brownb2d44352011-02-17 13:01:34 -0800256 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700257 motionEvent->getHistoricalRawX(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800258 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700259 motionEvent->getHistoricalRawY(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800260 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700261 motionEvent->getHistoricalX(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800262 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700263 motionEvent->getHistoricalY(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800264 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700265 motionEvent->getHistoricalPressure(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800266 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700267 motionEvent->getHistoricalSize(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800268 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700269 motionEvent->getHistoricalTouchMajor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800270 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700271 motionEvent->getHistoricalTouchMinor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800272 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700273 motionEvent->getHistoricalToolMajor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800274 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700275 motionEvent->getHistoricalToolMinor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800276 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700277 motionEvent->getHistoricalOrientation(i, sampleIndex));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700278 }
279 }
280
281 SCOPED_TRACE(lastSampleIndex);
282 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
283 for (size_t i = 0; i < pointerCount; i++) {
284 SCOPED_TRACE(i);
285 size_t offset = lastSampleIndex * pointerCount + i;
Jeff Brownb2d44352011-02-17 13:01:34 -0800286 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brown3e341462011-02-14 17:03:18 -0800287 motionEvent->getRawX(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800288 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brown3e341462011-02-14 17:03:18 -0800289 motionEvent->getRawY(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800290 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800291 motionEvent->getX(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800292 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800293 motionEvent->getY(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800294 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brown3e341462011-02-14 17:03:18 -0800295 motionEvent->getPressure(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800296 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brown3e341462011-02-14 17:03:18 -0800297 motionEvent->getSize(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800298 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800299 motionEvent->getTouchMajor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800300 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800301 motionEvent->getTouchMinor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800302 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800303 motionEvent->getToolMajor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800304 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800305 motionEvent->getToolMinor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800306 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown3e341462011-02-14 17:03:18 -0800307 motionEvent->getOrientation(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700308 }
309
Jeff Brown81499912010-11-05 15:02:16 -0700310 status = mConsumer->sendFinishedSignal(false);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700311 ASSERT_EQ(OK, status)
312 << "consumer sendFinishedSignal should return OK";
313
Jeff Brown81499912010-11-05 15:02:16 -0700314 bool handled = true;
Jeff Brown02d85b52010-12-06 17:13:33 -0800315 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700316 ASSERT_EQ(OK, status)
317 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700318 ASSERT_FALSE(handled)
319 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700320
321 status = mPublisher->reset();
322 ASSERT_EQ(OK, status)
323 << "publisher reset should return OK";
324}
325
326TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
327 ASSERT_NO_FATAL_FAILURE(Initialize());
328 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
329}
330
331TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) {
332 status_t status;
333 ASSERT_NO_FATAL_FAILURE(Initialize());
334
335 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
336 ASSERT_EQ(OK, status)
337 << "publisher publishKeyEvent should return OK first time";
338
339 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
340 ASSERT_EQ(INVALID_OPERATION, status)
341 << "publisher publishKeyEvent should return INVALID_OPERATION because "
342 "the publisher was not reset";
343}
344
345TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
346 ASSERT_NO_FATAL_FAILURE(Initialize());
347 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
348}
349
350TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) {
351 status_t status;
352 ASSERT_NO_FATAL_FAILURE(Initialize());
353
354 const size_t pointerCount = 1;
355 int32_t pointerIds[pointerCount] = { 0 };
Jeff Brown3e341462011-02-14 17:03:18 -0800356 PointerCoords pointerCoords[pointerCount];
357 pointerCoords[0].clear();
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700358
Jeff Brownaf30ff62010-09-01 17:01:00 -0700359 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700360 pointerCount, pointerIds, pointerCoords);
361 ASSERT_EQ(OK, status)
362 << "publisher publishMotionEvent should return OK";
363
Jeff Brownaf30ff62010-09-01 17:01:00 -0700364 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700365 pointerCount, pointerIds, pointerCoords);
366 ASSERT_EQ(INVALID_OPERATION, status)
367 << "publisher publishMotionEvent should return INVALID_OPERATION because ";
368 "the publisher was not reset";
369}
370
371TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
372 status_t status;
373 ASSERT_NO_FATAL_FAILURE(Initialize());
374
375 const size_t pointerCount = 0;
376 int32_t pointerIds[pointerCount];
377 PointerCoords pointerCoords[pointerCount];
378
Jeff Brownaf30ff62010-09-01 17:01:00 -0700379 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700380 pointerCount, pointerIds, pointerCoords);
381 ASSERT_EQ(BAD_VALUE, status)
382 << "publisher publishMotionEvent should return BAD_VALUE";
383}
384
385TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
386 status_t status;
387 ASSERT_NO_FATAL_FAILURE(Initialize());
388
389 const size_t pointerCount = MAX_POINTERS + 1;
390 int32_t pointerIds[pointerCount];
391 PointerCoords pointerCoords[pointerCount];
392
Jeff Brownaf30ff62010-09-01 17:01:00 -0700393 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700394 pointerCount, pointerIds, pointerCoords);
395 ASSERT_EQ(BAD_VALUE, status)
396 << "publisher publishMotionEvent should return BAD_VALUE";
397}
398
399TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
400 ASSERT_NO_FATAL_FAILURE(Initialize());
401 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
402 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
403 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
404 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
405 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
406}
407
408TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) {
409 status_t status;
410 ASSERT_NO_FATAL_FAILURE(Initialize());
411 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0));
412}
413
414TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) {
415 status_t status;
416 ASSERT_NO_FATAL_FAILURE(Initialize());
417 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4));
418}
419
420TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) {
421 status_t status;
422 ASSERT_NO_FATAL_FAILURE(Initialize());
423
424 PointerCoords pointerCoords[1];
425 status = mPublisher->appendMotionSample(0, pointerCoords);
426 ASSERT_EQ(INVALID_OPERATION, status)
427 << "publisher appendMotionSample should return INVALID_OPERATION";
428}
429
430TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) {
431 status_t status;
432 ASSERT_NO_FATAL_FAILURE(Initialize());
433
434 const size_t pointerCount = MAX_POINTERS;
435 int32_t pointerIds[pointerCount];
436 PointerCoords pointerCoords[pointerCount];
437
Jeff Brown5c1ed842010-07-14 18:48:53 -0700438 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_DOWN,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700439 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700440 ASSERT_EQ(OK, status);
441
442 status = mPublisher->appendMotionSample(0, pointerCoords);
443 ASSERT_EQ(INVALID_OPERATION, status)
444 << "publisher appendMotionSample should return INVALID_OPERATION";
445}
446
447TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) {
448 status_t status;
449 ASSERT_NO_FATAL_FAILURE(Initialize());
450
451 const size_t pointerCount = MAX_POINTERS;
452 int32_t pointerIds[pointerCount];
453 PointerCoords pointerCoords[pointerCount];
454
Jeff Brown5c1ed842010-07-14 18:48:53 -0700455 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700456 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700457 ASSERT_EQ(OK, status);
458
459 status = mPublisher->sendDispatchSignal();
460 ASSERT_EQ(OK, status);
461
462 status = mConsumer->receiveDispatchSignal();
463 ASSERT_EQ(OK, status);
464
465 InputEvent* event;
466 status = mConsumer->consume(& mEventFactory, & event);
467 ASSERT_EQ(OK, status);
468
469 status = mPublisher->appendMotionSample(0, pointerCoords);
470 ASSERT_EQ(status_t(FAILED_TRANSACTION), status)
471 << "publisher appendMotionSample should return FAILED_TRANSACTION";
472}
473
474TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) {
475 status_t status;
476 ASSERT_NO_FATAL_FAILURE(Initialize());
477
478 const size_t pointerCount = MAX_POINTERS;
479 int32_t pointerIds[pointerCount];
480 PointerCoords pointerCoords[pointerCount];
481
Jeff Brown5c1ed842010-07-14 18:48:53 -0700482 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700483 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700484 ASSERT_EQ(OK, status);
485
486 for (int count = 1;; count++) {
487 ASSERT_LT(count, 100000) << "should eventually reach OOM";
488
489 status = mPublisher->appendMotionSample(0, pointerCoords);
490 if (status != OK) {
491 ASSERT_GT(count, 12) << "should be able to add at least a dozen samples";
492 ASSERT_EQ(NO_MEMORY, status)
493 << "publisher appendMotionSample should return NO_MEMORY when buffer is full";
494 break;
495 }
496 }
497
498 status = mPublisher->appendMotionSample(0, pointerCoords);
499 ASSERT_EQ(NO_MEMORY, status)
500 << "publisher appendMotionSample should return NO_MEMORY persistently until reset";
501}
502
503} // namespace android