blob: a362f3281dc2ea057e7198dab0bdbe0761d29693 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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 */
16
17#include "TestHelpers.h"
18
19#include <unistd.h>
20#include <sys/mman.h>
21#include <time.h>
22
23#include <cutils/ashmem.h>
24#include <gtest/gtest.h>
25#include <input/InputTransport.h>
26#include <utils/Timers.h>
27#include <utils/StopWatch.h>
28
29namespace android {
30
31class InputPublisherAndConsumerTest : public testing::Test {
32protected:
33 sp<InputChannel> serverChannel, clientChannel;
34 InputPublisher* mPublisher;
35 InputConsumer* mConsumer;
36 PreallocatedInputEventFactory mEventFactory;
37
38 virtual void SetUp() {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080039 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070040 serverChannel, clientChannel);
41
42 mPublisher = new InputPublisher(serverChannel);
43 mConsumer = new InputConsumer(clientChannel);
44 }
45
46 virtual void TearDown() {
47 if (mPublisher) {
48 delete mPublisher;
Yi Kong5bed83b2018-07-17 12:53:47 -070049 mPublisher = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -070050 }
51
52 if (mConsumer) {
53 delete mConsumer;
Yi Kong5bed83b2018-07-17 12:53:47 -070054 mConsumer = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -070055 }
56
57 serverChannel.clear();
58 clientChannel.clear();
59 }
60
61 void PublishAndConsumeKeyEvent();
62 void PublishAndConsumeMotionEvent();
63};
64
65TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
66 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
67 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
68}
69
70void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
71 status_t status;
72
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010073 constexpr uint32_t seq = 15;
74 constexpr int32_t deviceId = 1;
75 constexpr int32_t source = AINPUT_SOURCE_KEYBOARD;
76 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
77 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
78 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
79 constexpr int32_t keyCode = AKEYCODE_ENTER;
80 constexpr int32_t scanCode = 13;
81 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
82 constexpr int32_t repeatCount = 1;
83 constexpr nsecs_t downTime = 3;
84 constexpr nsecs_t eventTime = 4;
Jeff Brown5912f952013-07-01 19:10:31 -070085
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010086 status = mPublisher->publishKeyEvent(seq, deviceId, source, displayId, action, flags,
Jeff Brown5912f952013-07-01 19:10:31 -070087 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
88 ASSERT_EQ(OK, status)
89 << "publisher publishKeyEvent should return OK";
90
91 uint32_t consumeSeq;
92 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080093 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070094 ASSERT_EQ(OK, status)
95 << "consumer consume should return OK";
96
Yi Kong5bed83b2018-07-17 12:53:47 -070097 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -070098 << "consumer should have returned non-NULL event";
99 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
100 << "consumer should have returned a key event";
101
102 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
103 EXPECT_EQ(seq, consumeSeq);
104 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
105 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100106 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Jeff Brown5912f952013-07-01 19:10:31 -0700107 EXPECT_EQ(action, keyEvent->getAction());
108 EXPECT_EQ(flags, keyEvent->getFlags());
109 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
110 EXPECT_EQ(scanCode, keyEvent->getScanCode());
111 EXPECT_EQ(metaState, keyEvent->getMetaState());
112 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
113 EXPECT_EQ(downTime, keyEvent->getDownTime());
114 EXPECT_EQ(eventTime, keyEvent->getEventTime());
115
116 status = mConsumer->sendFinishedSignal(seq, true);
117 ASSERT_EQ(OK, status)
118 << "consumer sendFinishedSignal should return OK";
119
120 uint32_t finishedSeq = 0;
121 bool handled = false;
122 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
123 ASSERT_EQ(OK, status)
124 << "publisher receiveFinishedSignal should return OK";
125 ASSERT_EQ(seq, finishedSeq)
126 << "publisher receiveFinishedSignal should have returned the original sequence number";
127 ASSERT_TRUE(handled)
128 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
129}
130
131void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
132 status_t status;
133
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800134 constexpr uint32_t seq = 15;
135 constexpr int32_t deviceId = 1;
136 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100137 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800138 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
139 constexpr int32_t actionButton = 0;
140 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
141 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
142 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
143 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800144 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800145 constexpr float xOffset = -10;
146 constexpr float yOffset = -20;
147 constexpr float xPrecision = 0.25;
148 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700149 constexpr float xCursorPosition = 1.3;
150 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800151 constexpr nsecs_t downTime = 3;
152 constexpr size_t pointerCount = 3;
153 constexpr nsecs_t eventTime = 4;
Jeff Brown5912f952013-07-01 19:10:31 -0700154 PointerProperties pointerProperties[pointerCount];
155 PointerCoords pointerCoords[pointerCount];
156 for (size_t i = 0; i < pointerCount; i++) {
157 pointerProperties[i].clear();
158 pointerProperties[i].id = (i + 2) % pointerCount;
159 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
160
161 pointerCoords[i].clear();
162 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
163 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
164 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
165 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
166 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
167 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
168 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
169 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
170 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
171 }
172
Garfield Tan00f511d2019-06-12 16:55:40 -0700173 status =
174 mPublisher->publishMotionEvent(seq, deviceId, source, displayId, action, actionButton,
175 flags, edgeFlags, metaState, buttonState, classification,
176 xOffset, yOffset, xPrecision, yPrecision,
177 xCursorPosition, yCursorPosition, downTime, eventTime,
178 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700179 ASSERT_EQ(OK, status)
180 << "publisher publishMotionEvent should return OK";
181
182 uint32_t consumeSeq;
183 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800184 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700185 ASSERT_EQ(OK, status)
186 << "consumer consume should return OK";
187
Yi Kong5bed83b2018-07-17 12:53:47 -0700188 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700189 << "consumer should have returned non-NULL event";
190 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
191 << "consumer should have returned a motion event";
192
193 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
194 EXPECT_EQ(seq, consumeSeq);
195 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
196 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800197 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Jeff Brown5912f952013-07-01 19:10:31 -0700198 EXPECT_EQ(action, motionEvent->getAction());
199 EXPECT_EQ(flags, motionEvent->getFlags());
200 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
201 EXPECT_EQ(metaState, motionEvent->getMetaState());
202 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800203 EXPECT_EQ(classification, motionEvent->getClassification());
Jeff Brown5912f952013-07-01 19:10:31 -0700204 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
205 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700206 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
207 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
208 EXPECT_EQ(xCursorPosition + xOffset, motionEvent->getXCursorPosition());
209 EXPECT_EQ(yCursorPosition + yOffset, motionEvent->getYCursorPosition());
Jeff Brown5912f952013-07-01 19:10:31 -0700210 EXPECT_EQ(downTime, motionEvent->getDownTime());
211 EXPECT_EQ(eventTime, motionEvent->getEventTime());
212 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
213 EXPECT_EQ(0U, motionEvent->getHistorySize());
214
215 for (size_t i = 0; i < pointerCount; i++) {
216 SCOPED_TRACE(i);
217 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
218 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
219
220 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
221 motionEvent->getRawX(i));
222 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
223 motionEvent->getRawY(i));
224 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
225 motionEvent->getX(i));
226 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
227 motionEvent->getY(i));
228 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
229 motionEvent->getPressure(i));
230 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
231 motionEvent->getSize(i));
232 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
233 motionEvent->getTouchMajor(i));
234 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
235 motionEvent->getTouchMinor(i));
236 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
237 motionEvent->getToolMajor(i));
238 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
239 motionEvent->getToolMinor(i));
240 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
241 motionEvent->getOrientation(i));
242 }
243
244 status = mConsumer->sendFinishedSignal(seq, false);
245 ASSERT_EQ(OK, status)
246 << "consumer sendFinishedSignal should return OK";
247
248 uint32_t finishedSeq = 0;
249 bool handled = true;
250 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
251 ASSERT_EQ(OK, status)
252 << "publisher receiveFinishedSignal should return OK";
253 ASSERT_EQ(seq, finishedSeq)
254 << "publisher receiveFinishedSignal should have returned the original sequence number";
255 ASSERT_FALSE(handled)
256 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
257}
258
259TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
260 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
261}
262
263TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
264 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
265}
266
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800267TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700268 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800269 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700270 PointerProperties pointerProperties[pointerCount];
271 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800272 for (size_t i = 0; i < pointerCount; i++) {
273 pointerProperties[i].clear();
274 pointerCoords[i].clear();
275 }
Jeff Brown5912f952013-07-01 19:10:31 -0700276
Garfield Tan00f511d2019-06-12 16:55:40 -0700277 status =
278 mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
279 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
280 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
281 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700282 ASSERT_EQ(BAD_VALUE, status)
283 << "publisher publishMotionEvent should return BAD_VALUE";
284}
285
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800286TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
287 status_t status;
288 const size_t pointerCount = 0;
289 PointerProperties pointerProperties[pointerCount];
290 PointerCoords pointerCoords[pointerCount];
291
Garfield Tan00f511d2019-06-12 16:55:40 -0700292 status =
293 mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
294 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
295 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
296 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800297 ASSERT_EQ(BAD_VALUE, status)
298 << "publisher publishMotionEvent should return BAD_VALUE";
299}
300
301TEST_F(InputPublisherAndConsumerTest,
302 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700303 status_t status;
304 const size_t pointerCount = MAX_POINTERS + 1;
305 PointerProperties pointerProperties[pointerCount];
306 PointerCoords pointerCoords[pointerCount];
307 for (size_t i = 0; i < pointerCount; i++) {
308 pointerProperties[i].clear();
309 pointerCoords[i].clear();
310 }
311
Garfield Tan00f511d2019-06-12 16:55:40 -0700312 status =
313 mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
314 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
315 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
316 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700317 ASSERT_EQ(BAD_VALUE, status)
318 << "publisher publishMotionEvent should return BAD_VALUE";
319}
320
321TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
322 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
323 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
324 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
325 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
326 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
327}
328
329} // namespace android