blob: 2fc77e97a044a9a4a0034605bdf0b60f44e38f56 [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();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080063 void PublishAndConsumeFocusEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070064};
65
66TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
67 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
68 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
69}
70
71void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
72 status_t status;
73
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010074 constexpr uint32_t seq = 15;
75 constexpr int32_t deviceId = 1;
76 constexpr int32_t source = AINPUT_SOURCE_KEYBOARD;
77 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
78 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
79 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
80 constexpr int32_t keyCode = AKEYCODE_ENTER;
81 constexpr int32_t scanCode = 13;
82 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
83 constexpr int32_t repeatCount = 1;
84 constexpr nsecs_t downTime = 3;
85 constexpr nsecs_t eventTime = 4;
Jeff Brown5912f952013-07-01 19:10:31 -070086
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010087 status = mPublisher->publishKeyEvent(seq, deviceId, source, displayId, action, flags,
Jeff Brown5912f952013-07-01 19:10:31 -070088 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
89 ASSERT_EQ(OK, status)
90 << "publisher publishKeyEvent should return OK";
91
92 uint32_t consumeSeq;
93 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080094 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070095 ASSERT_EQ(OK, status)
96 << "consumer consume should return OK";
97
Yi Kong5bed83b2018-07-17 12:53:47 -070098 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -070099 << "consumer should have returned non-NULL event";
100 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
101 << "consumer should have returned a key event";
102
103 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
104 EXPECT_EQ(seq, consumeSeq);
105 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
106 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100107 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Jeff Brown5912f952013-07-01 19:10:31 -0700108 EXPECT_EQ(action, keyEvent->getAction());
109 EXPECT_EQ(flags, keyEvent->getFlags());
110 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
111 EXPECT_EQ(scanCode, keyEvent->getScanCode());
112 EXPECT_EQ(metaState, keyEvent->getMetaState());
113 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
114 EXPECT_EQ(downTime, keyEvent->getDownTime());
115 EXPECT_EQ(eventTime, keyEvent->getEventTime());
116
117 status = mConsumer->sendFinishedSignal(seq, true);
118 ASSERT_EQ(OK, status)
119 << "consumer sendFinishedSignal should return OK";
120
121 uint32_t finishedSeq = 0;
122 bool handled = false;
123 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
124 ASSERT_EQ(OK, status)
125 << "publisher receiveFinishedSignal should return OK";
126 ASSERT_EQ(seq, finishedSeq)
127 << "publisher receiveFinishedSignal should have returned the original sequence number";
128 ASSERT_TRUE(handled)
129 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
130}
131
132void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
133 status_t status;
134
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800135 constexpr uint32_t seq = 15;
136 constexpr int32_t deviceId = 1;
137 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100138 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800139 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
140 constexpr int32_t actionButton = 0;
141 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
142 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
143 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
144 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800145 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800146 constexpr float xOffset = -10;
147 constexpr float yOffset = -20;
148 constexpr float xPrecision = 0.25;
149 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700150 constexpr float xCursorPosition = 1.3;
151 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800152 constexpr nsecs_t downTime = 3;
153 constexpr size_t pointerCount = 3;
154 constexpr nsecs_t eventTime = 4;
Jeff Brown5912f952013-07-01 19:10:31 -0700155 PointerProperties pointerProperties[pointerCount];
156 PointerCoords pointerCoords[pointerCount];
157 for (size_t i = 0; i < pointerCount; i++) {
158 pointerProperties[i].clear();
159 pointerProperties[i].id = (i + 2) % pointerCount;
160 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
161
162 pointerCoords[i].clear();
163 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
164 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
165 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
166 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
167 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
168 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
169 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
170 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
171 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
172 }
173
Garfield Tan00f511d2019-06-12 16:55:40 -0700174 status =
175 mPublisher->publishMotionEvent(seq, deviceId, source, displayId, action, actionButton,
176 flags, edgeFlags, metaState, buttonState, classification,
177 xOffset, yOffset, xPrecision, yPrecision,
178 xCursorPosition, yCursorPosition, downTime, eventTime,
179 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700180 ASSERT_EQ(OK, status)
181 << "publisher publishMotionEvent should return OK";
182
183 uint32_t consumeSeq;
184 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800185 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700186 ASSERT_EQ(OK, status)
187 << "consumer consume should return OK";
188
Yi Kong5bed83b2018-07-17 12:53:47 -0700189 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700190 << "consumer should have returned non-NULL event";
191 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
192 << "consumer should have returned a motion event";
193
194 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
195 EXPECT_EQ(seq, consumeSeq);
196 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
197 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800198 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Jeff Brown5912f952013-07-01 19:10:31 -0700199 EXPECT_EQ(action, motionEvent->getAction());
200 EXPECT_EQ(flags, motionEvent->getFlags());
201 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
202 EXPECT_EQ(metaState, motionEvent->getMetaState());
203 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800204 EXPECT_EQ(classification, motionEvent->getClassification());
Jeff Brown5912f952013-07-01 19:10:31 -0700205 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
206 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700207 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
208 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
209 EXPECT_EQ(xCursorPosition + xOffset, motionEvent->getXCursorPosition());
210 EXPECT_EQ(yCursorPosition + yOffset, motionEvent->getYCursorPosition());
Jeff Brown5912f952013-07-01 19:10:31 -0700211 EXPECT_EQ(downTime, motionEvent->getDownTime());
212 EXPECT_EQ(eventTime, motionEvent->getEventTime());
213 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
214 EXPECT_EQ(0U, motionEvent->getHistorySize());
215
216 for (size_t i = 0; i < pointerCount; i++) {
217 SCOPED_TRACE(i);
218 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
219 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
220
221 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
222 motionEvent->getRawX(i));
223 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
224 motionEvent->getRawY(i));
225 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
226 motionEvent->getX(i));
227 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
228 motionEvent->getY(i));
229 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
230 motionEvent->getPressure(i));
231 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
232 motionEvent->getSize(i));
233 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
234 motionEvent->getTouchMajor(i));
235 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
236 motionEvent->getTouchMinor(i));
237 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
238 motionEvent->getToolMajor(i));
239 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
240 motionEvent->getToolMinor(i));
241 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
242 motionEvent->getOrientation(i));
243 }
244
245 status = mConsumer->sendFinishedSignal(seq, false);
246 ASSERT_EQ(OK, status)
247 << "consumer sendFinishedSignal should return OK";
248
249 uint32_t finishedSeq = 0;
250 bool handled = true;
251 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
252 ASSERT_EQ(OK, status)
253 << "publisher receiveFinishedSignal should return OK";
254 ASSERT_EQ(seq, finishedSeq)
255 << "publisher receiveFinishedSignal should have returned the original sequence number";
256 ASSERT_FALSE(handled)
257 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
258}
259
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800260void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
261 status_t status;
262
263 constexpr uint32_t seq = 15;
264 constexpr bool hasFocus = true;
265 constexpr bool inTouchMode = true;
266
267 status = mPublisher->publishFocusEvent(seq, hasFocus, inTouchMode);
268 ASSERT_EQ(OK, status) << "publisher publishKeyEvent should return OK";
269
270 uint32_t consumeSeq;
271 InputEvent* event;
272 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
273 ASSERT_EQ(OK, status) << "consumer consume should return OK";
274
275 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
276 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
277 << "consumer should have returned a focus event";
278
279 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
280 EXPECT_EQ(seq, consumeSeq);
281 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
282 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
283
284 status = mConsumer->sendFinishedSignal(seq, true);
285 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
286
287 uint32_t finishedSeq = 0;
288 bool handled = false;
289 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
290 ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK";
291 ASSERT_EQ(seq, finishedSeq)
292 << "publisher receiveFinishedSignal should have returned the original sequence number";
293 ASSERT_TRUE(handled)
294 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
295}
296
Jeff Brown5912f952013-07-01 19:10:31 -0700297TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
298 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
299}
300
301TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
302 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
303}
304
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800305TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
306 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
307}
308
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800309TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700310 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800311 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700312 PointerProperties pointerProperties[pointerCount];
313 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800314 for (size_t i = 0; i < pointerCount; i++) {
315 pointerProperties[i].clear();
316 pointerCoords[i].clear();
317 }
Jeff Brown5912f952013-07-01 19:10:31 -0700318
Garfield Tan00f511d2019-06-12 16:55:40 -0700319 status =
320 mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
321 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
322 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
323 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700324 ASSERT_EQ(BAD_VALUE, status)
325 << "publisher publishMotionEvent should return BAD_VALUE";
326}
327
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800328TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
329 status_t status;
330 const size_t pointerCount = 0;
331 PointerProperties pointerProperties[pointerCount];
332 PointerCoords pointerCoords[pointerCount];
333
Garfield Tan00f511d2019-06-12 16:55:40 -0700334 status =
335 mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
336 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
337 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
338 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800339 ASSERT_EQ(BAD_VALUE, status)
340 << "publisher publishMotionEvent should return BAD_VALUE";
341}
342
343TEST_F(InputPublisherAndConsumerTest,
344 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700345 status_t status;
346 const size_t pointerCount = MAX_POINTERS + 1;
347 PointerProperties pointerProperties[pointerCount];
348 PointerCoords pointerCoords[pointerCount];
349 for (size_t i = 0; i < pointerCount; i++) {
350 pointerProperties[i].clear();
351 pointerCoords[i].clear();
352 }
353
Garfield Tan00f511d2019-06-12 16:55:40 -0700354 status =
355 mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, MotionClassification::NONE,
356 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
357 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
358 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700359 ASSERT_EQ(BAD_VALUE, status)
360 << "publisher publishMotionEvent should return BAD_VALUE";
361}
362
363TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
364 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
365 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
366 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800367 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700368 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
369 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
370}
371
372} // namespace android