blob: fcc4cadb71ce2e5baa959d754fc03624eb235aef [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 Browne959ed22011-05-06 18:20:01 -0700159 const int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700160 const float xOffset = -10;
161 const float yOffset = -20;
162 const float xPrecision = 0.25;
163 const float yPrecision = 0.5;
164 const nsecs_t downTime = 3;
165 const size_t pointerCount = 3;
Jeff Browne959ed22011-05-06 18:20:01 -0700166 PointerProperties pointerProperties[pointerCount];
167 for (size_t i = 0; i < pointerCount; i++) {
168 pointerProperties[i].clear();
169 pointerProperties[i].id = (i + 2) % pointerCount;
170 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
171 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700172
173 Vector<nsecs_t> sampleEventTimes;
174 Vector<PointerCoords> samplePointerCoords;
175
176 for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) {
177 sampleEventTimes.push(i + 10);
178 for (size_t j = 0; j < pointerCount; j++) {
179 samplePointerCoords.push();
Jeff Brown3e341462011-02-14 17:03:18 -0800180 PointerCoords& pc = samplePointerCoords.editTop();
181 pc.clear();
Jeff Brownb2d44352011-02-17 13:01:34 -0800182 pc.setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i + j);
183 pc.setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i + j);
184 pc.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i + j);
185 pc.setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i + j);
186 pc.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i + j);
187 pc.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i + j);
188 pc.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i + j);
189 pc.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i + j);
190 pc.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i + j);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700191 }
192 }
193
Jeff Brownaf30ff62010-09-01 17:01:00 -0700194 status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags,
Jeff Browne959ed22011-05-06 18:20:01 -0700195 metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision,
196 downTime, sampleEventTimes[0], pointerCount,
197 pointerProperties, samplePointerCoords.array());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700198 ASSERT_EQ(OK, status)
199 << "publisher publishMotionEvent should return OK";
200
201 for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) {
202 size_t sampleIndex = i + 1;
203 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
204 samplePointerCoords.array() + sampleIndex * pointerCount);
205 ASSERT_EQ(OK, status)
206 << "publisher appendMotionEvent should return OK";
207 }
208
209 status = mPublisher->sendDispatchSignal();
210 ASSERT_EQ(OK, status)
211 << "publisher sendDispatchSignal should return OK";
212
213 for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) {
214 size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch;
215 status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex],
216 samplePointerCoords.array() + sampleIndex * pointerCount);
217 ASSERT_EQ(OK, status)
218 << "publisher appendMotionEvent should return OK";
219 }
220
221 status = mConsumer->receiveDispatchSignal();
222 ASSERT_EQ(OK, status)
223 << "consumer receiveDispatchSignal should return OK";
224
225 InputEvent* event;
226 status = mConsumer->consume(& mEventFactory, & event);
227 ASSERT_EQ(OK, status)
228 << "consumer consume should return OK";
229
230 ASSERT_TRUE(event != NULL)
231 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -0700232 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700233 << "consumer should have returned a motion event";
234
235 size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch;
236
237 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
238 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700239 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700240 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brownaf30ff62010-09-01 17:01:00 -0700241 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700242 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
243 EXPECT_EQ(metaState, motionEvent->getMetaState());
Jeff Browne959ed22011-05-06 18:20:01 -0700244 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700245 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
246 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
247 EXPECT_EQ(downTime, motionEvent->getDownTime());
248 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
249 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
250 EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize());
251
252 for (size_t i = 0; i < pointerCount; i++) {
253 SCOPED_TRACE(i);
Jeff Browne959ed22011-05-06 18:20:01 -0700254 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
255 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700256 }
257
258 for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) {
259 SCOPED_TRACE(sampleIndex);
260 EXPECT_EQ(sampleEventTimes[sampleIndex],
261 motionEvent->getHistoricalEventTime(sampleIndex));
262 for (size_t i = 0; i < pointerCount; i++) {
263 SCOPED_TRACE(i);
264 size_t offset = sampleIndex * pointerCount + i;
Jeff Brownb2d44352011-02-17 13:01:34 -0800265 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700266 motionEvent->getHistoricalRawX(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800267 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700268 motionEvent->getHistoricalRawY(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800269 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700270 motionEvent->getHistoricalX(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800271 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700272 motionEvent->getHistoricalY(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800273 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700274 motionEvent->getHistoricalPressure(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800275 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700276 motionEvent->getHistoricalSize(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800277 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700278 motionEvent->getHistoricalTouchMajor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800279 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700280 motionEvent->getHistoricalTouchMinor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800281 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700282 motionEvent->getHistoricalToolMajor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800283 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700284 motionEvent->getHistoricalToolMinor(i, sampleIndex));
Jeff Brownb2d44352011-02-17 13:01:34 -0800285 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown5c1ed842010-07-14 18:48:53 -0700286 motionEvent->getHistoricalOrientation(i, sampleIndex));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700287 }
288 }
289
290 SCOPED_TRACE(lastSampleIndex);
291 EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime());
292 for (size_t i = 0; i < pointerCount; i++) {
293 SCOPED_TRACE(i);
294 size_t offset = lastSampleIndex * pointerCount + i;
Jeff Brownb2d44352011-02-17 13:01:34 -0800295 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brown3e341462011-02-14 17:03:18 -0800296 motionEvent->getRawX(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800297 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brown3e341462011-02-14 17:03:18 -0800298 motionEvent->getRawY(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800299 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800300 motionEvent->getX(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800301 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800302 motionEvent->getY(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800303 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brown3e341462011-02-14 17:03:18 -0800304 motionEvent->getPressure(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800305 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brown3e341462011-02-14 17:03:18 -0800306 motionEvent->getSize(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800307 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800308 motionEvent->getTouchMajor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800309 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800310 motionEvent->getTouchMinor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800311 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800312 motionEvent->getToolMajor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800313 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800314 motionEvent->getToolMinor(i));
Jeff Brownb2d44352011-02-17 13:01:34 -0800315 EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown3e341462011-02-14 17:03:18 -0800316 motionEvent->getOrientation(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700317 }
318
Jeff Brown81499912010-11-05 15:02:16 -0700319 status = mConsumer->sendFinishedSignal(false);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700320 ASSERT_EQ(OK, status)
321 << "consumer sendFinishedSignal should return OK";
322
Jeff Brown81499912010-11-05 15:02:16 -0700323 bool handled = true;
Jeff Brown02d85b52010-12-06 17:13:33 -0800324 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700325 ASSERT_EQ(OK, status)
326 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700327 ASSERT_FALSE(handled)
328 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700329
330 status = mPublisher->reset();
331 ASSERT_EQ(OK, status)
332 << "publisher reset should return OK";
333}
334
335TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
336 ASSERT_NO_FATAL_FAILURE(Initialize());
337 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
338}
339
340TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) {
341 status_t status;
342 ASSERT_NO_FATAL_FAILURE(Initialize());
343
344 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
345 ASSERT_EQ(OK, status)
346 << "publisher publishKeyEvent should return OK first time";
347
348 status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
349 ASSERT_EQ(INVALID_OPERATION, status)
350 << "publisher publishKeyEvent should return INVALID_OPERATION because "
351 "the publisher was not reset";
352}
353
354TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
355 ASSERT_NO_FATAL_FAILURE(Initialize());
356 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
357}
358
359TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) {
360 status_t status;
361 ASSERT_NO_FATAL_FAILURE(Initialize());
362
363 const size_t pointerCount = 1;
Jeff Browne959ed22011-05-06 18:20:01 -0700364 PointerProperties pointerProperties[pointerCount];
Jeff Brown3e341462011-02-14 17:03:18 -0800365 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700366 for (size_t i = 0; i < pointerCount; i++) {
367 pointerProperties[i].clear();
368 pointerCoords[i].clear();
369 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700370
Jeff Browne959ed22011-05-06 18:20:01 -0700371 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
372 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700373 ASSERT_EQ(OK, status)
374 << "publisher publishMotionEvent should return OK";
375
Jeff Browne959ed22011-05-06 18:20:01 -0700376 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
377 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700378 ASSERT_EQ(INVALID_OPERATION, status)
379 << "publisher publishMotionEvent should return INVALID_OPERATION because ";
380 "the publisher was not reset";
381}
382
383TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
384 status_t status;
385 ASSERT_NO_FATAL_FAILURE(Initialize());
386
387 const size_t pointerCount = 0;
Jeff Browne959ed22011-05-06 18:20:01 -0700388 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700389 PointerCoords pointerCoords[pointerCount];
390
Jeff Browne959ed22011-05-06 18:20:01 -0700391 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
392 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700393 ASSERT_EQ(BAD_VALUE, status)
394 << "publisher publishMotionEvent should return BAD_VALUE";
395}
396
397TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
398 status_t status;
399 ASSERT_NO_FATAL_FAILURE(Initialize());
400
401 const size_t pointerCount = MAX_POINTERS + 1;
Jeff Browne959ed22011-05-06 18:20:01 -0700402 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700403 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700404 for (size_t i = 0; i < pointerCount; i++) {
405 pointerProperties[i].clear();
406 pointerCoords[i].clear();
407 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700408
Jeff Browne959ed22011-05-06 18:20:01 -0700409 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
410 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700411 ASSERT_EQ(BAD_VALUE, status)
412 << "publisher publishMotionEvent should return BAD_VALUE";
413}
414
415TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
416 ASSERT_NO_FATAL_FAILURE(Initialize());
417 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
418 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
419 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
420 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
421 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
422}
423
424TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) {
425 status_t status;
426 ASSERT_NO_FATAL_FAILURE(Initialize());
427 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0));
428}
429
430TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) {
431 status_t status;
432 ASSERT_NO_FATAL_FAILURE(Initialize());
433 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4));
434}
435
436TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) {
437 status_t status;
438 ASSERT_NO_FATAL_FAILURE(Initialize());
439
440 PointerCoords pointerCoords[1];
441 status = mPublisher->appendMotionSample(0, pointerCoords);
442 ASSERT_EQ(INVALID_OPERATION, status)
443 << "publisher appendMotionSample should return INVALID_OPERATION";
444}
445
446TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) {
447 status_t status;
448 ASSERT_NO_FATAL_FAILURE(Initialize());
449
450 const size_t pointerCount = MAX_POINTERS;
Jeff Browne959ed22011-05-06 18:20:01 -0700451 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700452 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700453 for (size_t i = 0; i < pointerCount; i++) {
454 pointerProperties[i].clear();
455 pointerCoords[i].clear();
456 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700457
Jeff Brown5c1ed842010-07-14 18:48:53 -0700458 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_DOWN,
Jeff Browne959ed22011-05-06 18:20:01 -0700459 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700460 ASSERT_EQ(OK, status);
461
462 status = mPublisher->appendMotionSample(0, pointerCoords);
463 ASSERT_EQ(INVALID_OPERATION, status)
464 << "publisher appendMotionSample should return INVALID_OPERATION";
465}
466
467TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) {
468 status_t status;
469 ASSERT_NO_FATAL_FAILURE(Initialize());
470
471 const size_t pointerCount = MAX_POINTERS;
Jeff Browne959ed22011-05-06 18:20:01 -0700472 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700473 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700474 for (size_t i = 0; i < pointerCount; i++) {
475 pointerProperties[i].clear();
476 pointerCoords[i].clear();
477 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700478
Jeff Brown5c1ed842010-07-14 18:48:53 -0700479 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Browne959ed22011-05-06 18:20:01 -0700480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700481 ASSERT_EQ(OK, status);
482
483 status = mPublisher->sendDispatchSignal();
484 ASSERT_EQ(OK, status);
485
486 status = mConsumer->receiveDispatchSignal();
487 ASSERT_EQ(OK, status);
488
489 InputEvent* event;
490 status = mConsumer->consume(& mEventFactory, & event);
491 ASSERT_EQ(OK, status);
492
493 status = mPublisher->appendMotionSample(0, pointerCoords);
494 ASSERT_EQ(status_t(FAILED_TRANSACTION), status)
495 << "publisher appendMotionSample should return FAILED_TRANSACTION";
496}
497
498TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) {
499 status_t status;
500 ASSERT_NO_FATAL_FAILURE(Initialize());
501
502 const size_t pointerCount = MAX_POINTERS;
Jeff Browne959ed22011-05-06 18:20:01 -0700503 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700504 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700505 for (size_t i = 0; i < pointerCount; i++) {
506 pointerProperties[i].clear();
507 pointerCoords[i].clear();
508 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700509
Jeff Brown5c1ed842010-07-14 18:48:53 -0700510 status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE,
Jeff Browne959ed22011-05-06 18:20:01 -0700511 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700512 ASSERT_EQ(OK, status);
513
514 for (int count = 1;; count++) {
515 ASSERT_LT(count, 100000) << "should eventually reach OOM";
516
517 status = mPublisher->appendMotionSample(0, pointerCoords);
518 if (status != OK) {
519 ASSERT_GT(count, 12) << "should be able to add at least a dozen samples";
520 ASSERT_EQ(NO_MEMORY, status)
521 << "publisher appendMotionSample should return NO_MEMORY when buffer is full";
522 break;
523 }
524 }
525
526 status = mPublisher->appendMotionSample(0, pointerCoords);
527 ASSERT_EQ(NO_MEMORY, status)
528 << "publisher appendMotionSample should return NO_MEMORY persistently until reset";
529}
530
531} // namespace android