blob: 8db5bf128902f1b6efb0ff85fcb7a772a2777d7c [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
chaviw09c8d2d2020-08-24 15:48:26 -070023#include <attestation/HmacKeyManager.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024#include <cutils/ashmem.h>
25#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050026#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <utils/StopWatch.h>
chaviw09c8d2d2020-08-24 15:48:26 -070029#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000031using android::base::Result;
32
Jeff Brown5912f952013-07-01 19:10:31 -070033namespace android {
34
35class InputPublisherAndConsumerTest : public testing::Test {
36protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050037 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
38 std::unique_ptr<InputPublisher> mPublisher;
39 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070040 PreallocatedInputEventFactory mEventFactory;
41
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050042 void SetUp() override {
43 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080044 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070045 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080046 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050047 mServerChannel = std::move(serverChannel);
48 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070049
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050050 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
51 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070052 }
53
54 void PublishAndConsumeKeyEvent();
55 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080056 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080057 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080058 void PublishAndConsumeDragEvent();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -070059 void PublishAndConsumeTouchModeEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070060};
61
62TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050063 ASSERT_NE(nullptr, mPublisher->getChannel());
64 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050065 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
66 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050067 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
68 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070069}
70
71void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
72 status_t status;
73
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010074 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080075 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010076 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060077 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010078 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060079 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
80 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
81 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010082 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
83 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
84 constexpr int32_t keyCode = AKEYCODE_ENTER;
85 constexpr int32_t scanCode = 13;
86 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
87 constexpr int32_t repeatCount = 1;
88 constexpr nsecs_t downTime = 3;
89 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100090 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070091
Garfield Tanff1f1bb2020-01-28 13:24:04 -080092 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
93 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060094 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070095 ASSERT_EQ(OK, status)
96 << "publisher publishKeyEvent should return OK";
97
98 uint32_t consumeSeq;
99 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800100 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700101 ASSERT_EQ(OK, status)
102 << "consumer consume should return OK";
103
Yi Kong5bed83b2018-07-17 12:53:47 -0700104 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700105 << "consumer should have returned non-NULL event";
106 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
107 << "consumer should have returned a key event";
108
109 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
110 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800111 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700112 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
113 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100114 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600115 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700116 EXPECT_EQ(action, keyEvent->getAction());
117 EXPECT_EQ(flags, keyEvent->getFlags());
118 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
119 EXPECT_EQ(scanCode, keyEvent->getScanCode());
120 EXPECT_EQ(metaState, keyEvent->getMetaState());
121 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
122 EXPECT_EQ(downTime, keyEvent->getDownTime());
123 EXPECT_EQ(eventTime, keyEvent->getEventTime());
124
125 status = mConsumer->sendFinishedSignal(seq, true);
126 ASSERT_EQ(OK, status)
127 << "consumer sendFinishedSignal should return OK";
128
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000129 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
130 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
131 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
132 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
133 ASSERT_EQ(seq, finish.seq)
134 << "receiveConsumerResponse should have returned the original sequence number";
135 ASSERT_TRUE(finish.handled)
136 << "receiveConsumerResponse should have set handled to consumer's reply";
137 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000138 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700139}
140
141void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
142 status_t status;
143
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800144 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800145 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800146 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600147 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100148 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600149 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
150 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
151 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800152 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
153 constexpr int32_t actionButton = 0;
154 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
155 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
156 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
157 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800158 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600159 constexpr float xScale = 2;
160 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800161 constexpr float xOffset = -10;
162 constexpr float yOffset = -20;
163 constexpr float xPrecision = 0.25;
164 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700165 constexpr float xCursorPosition = 1.3;
166 constexpr float yCursorPosition = 50.6;
Evan Rosky09576692021-07-01 12:22:09 -0700167 constexpr uint32_t displayOrientation = ui::Transform::ROT_0;
Evan Rosky84f07f02021-04-16 10:42:42 -0700168 constexpr int32_t displayWidth = 1000;
169 constexpr int32_t displayHeight = 2000;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800170 constexpr nsecs_t downTime = 3;
171 constexpr size_t pointerCount = 3;
172 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000173 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700174 PointerProperties pointerProperties[pointerCount];
175 PointerCoords pointerCoords[pointerCount];
176 for (size_t i = 0; i < pointerCount; i++) {
177 pointerProperties[i].clear();
178 pointerProperties[i].id = (i + 2) % pointerCount;
179 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
180
181 pointerCoords[i].clear();
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
186 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
187 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
188 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
189 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
190 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
191 }
192
chaviw9eaa22c2020-07-01 16:21:27 -0700193 ui::Transform transform;
194 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800195 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600196 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700197 classification, transform, xPrecision, yPrecision,
Evan Rosky09576692021-07-01 12:22:09 -0700198 xCursorPosition, yCursorPosition, displayOrientation,
199 displayWidth, displayHeight, downTime, eventTime,
200 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700201 ASSERT_EQ(OK, status)
202 << "publisher publishMotionEvent should return OK";
203
204 uint32_t consumeSeq;
205 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800206 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700207 ASSERT_EQ(OK, status)
208 << "consumer consume should return OK";
209
Yi Kong5bed83b2018-07-17 12:53:47 -0700210 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700211 << "consumer should have returned non-NULL event";
212 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
213 << "consumer should have returned a motion event";
214
215 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
216 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800217 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700218 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
219 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800220 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600221 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700222 EXPECT_EQ(action, motionEvent->getAction());
223 EXPECT_EQ(flags, motionEvent->getFlags());
224 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
225 EXPECT_EQ(metaState, motionEvent->getMetaState());
226 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800227 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700228 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600229 EXPECT_EQ(xOffset, motionEvent->getXOffset());
230 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700231 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
232 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700233 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
234 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800235 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
236 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Evan Rosky09576692021-07-01 12:22:09 -0700237 EXPECT_EQ(displayOrientation, motionEvent->getDisplayOrientation());
Evan Rosky84f07f02021-04-16 10:42:42 -0700238 EXPECT_EQ(displayWidth, motionEvent->getDisplaySize().x);
239 EXPECT_EQ(displayHeight, motionEvent->getDisplaySize().y);
Jeff Brown5912f952013-07-01 19:10:31 -0700240 EXPECT_EQ(downTime, motionEvent->getDownTime());
241 EXPECT_EQ(eventTime, motionEvent->getEventTime());
242 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
243 EXPECT_EQ(0U, motionEvent->getHistorySize());
244
245 for (size_t i = 0; i < pointerCount; i++) {
246 SCOPED_TRACE(i);
247 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
248 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
249
250 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
251 motionEvent->getRawX(i));
252 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
253 motionEvent->getRawY(i));
chaviw82357092020-01-28 13:13:06 -0800254 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
255 motionEvent->getX(i));
256 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
257 motionEvent->getY(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700258 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
259 motionEvent->getPressure(i));
260 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
261 motionEvent->getSize(i));
262 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
263 motionEvent->getTouchMajor(i));
264 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
265 motionEvent->getTouchMinor(i));
266 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
267 motionEvent->getToolMajor(i));
268 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
269 motionEvent->getToolMinor(i));
270 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
271 motionEvent->getOrientation(i));
272 }
273
274 status = mConsumer->sendFinishedSignal(seq, false);
275 ASSERT_EQ(OK, status)
276 << "consumer sendFinishedSignal should return OK";
277
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000278 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
279 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
280 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
281 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
282 ASSERT_EQ(seq, finish.seq)
283 << "receiveConsumerResponse should have returned the original sequence number";
284 ASSERT_FALSE(finish.handled)
285 << "receiveConsumerResponse should have set handled to consumer's reply";
286 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000287 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700288}
289
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800290void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
291 status_t status;
292
293 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800294 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800295 constexpr bool hasFocus = true;
296 constexpr bool inTouchMode = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000297 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800298
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800299 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
arthurhung7632c332020-12-30 16:58:01 +0800300 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800301
302 uint32_t consumeSeq;
303 InputEvent* event;
304 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
305 ASSERT_EQ(OK, status) << "consumer consume should return OK";
306
307 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
308 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
309 << "consumer should have returned a focus event";
310
311 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
312 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800313 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800314 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
315 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
316
317 status = mConsumer->sendFinishedSignal(seq, true);
318 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
319
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000320 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
321 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
322 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
323 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000324
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000325 ASSERT_EQ(seq, finish.seq)
326 << "receiveConsumerResponse should have returned the original sequence number";
327 ASSERT_TRUE(finish.handled)
328 << "receiveConsumerResponse should have set handled to consumer's reply";
329 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000330 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800331}
332
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800333void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
334 status_t status;
335
336 constexpr uint32_t seq = 42;
337 int32_t eventId = InputEvent::nextId();
338 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000339 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800340
341 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800342 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800343
344 uint32_t consumeSeq;
345 InputEvent* event;
346 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
347 ASSERT_EQ(OK, status) << "consumer consume should return OK";
348
349 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
350 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
351 << "consumer should have returned a capture event";
352
353 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
354 EXPECT_EQ(seq, consumeSeq);
355 EXPECT_EQ(eventId, captureEvent->getId());
356 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
357
358 status = mConsumer->sendFinishedSignal(seq, true);
359 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
360
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000361 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
362 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
363 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
364 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
365 ASSERT_EQ(seq, finish.seq)
366 << "receiveConsumerResponse should have returned the original sequence number";
367 ASSERT_TRUE(finish.handled)
368 << "receiveConsumerResponse should have set handled to consumer's reply";
369 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000370 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800371}
372
arthurhung7632c332020-12-30 16:58:01 +0800373void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
374 status_t status;
375
376 constexpr uint32_t seq = 15;
377 int32_t eventId = InputEvent::nextId();
378 constexpr bool isExiting = false;
379 constexpr float x = 10;
380 constexpr float y = 15;
381 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
382
383 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
384 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
385
386 uint32_t consumeSeq;
387 InputEvent* event;
388 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
389 ASSERT_EQ(OK, status) << "consumer consume should return OK";
390
391 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
392 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
393 << "consumer should have returned a drag event";
394
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000395 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800396 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000397 EXPECT_EQ(eventId, dragEvent.getId());
398 EXPECT_EQ(isExiting, dragEvent.isExiting());
399 EXPECT_EQ(x, dragEvent.getX());
400 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800401
402 status = mConsumer->sendFinishedSignal(seq, true);
403 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
404
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000405 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
406 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
407 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
408 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
409 ASSERT_EQ(seq, finish.seq)
410 << "receiveConsumerResponse should have returned the original sequence number";
411 ASSERT_TRUE(finish.handled)
412 << "receiveConsumerResponse should have set handled to consumer's reply";
413 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800414 << "finished signal's consume time should be greater than publish time";
415}
416
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700417void InputPublisherAndConsumerTest::PublishAndConsumeTouchModeEvent() {
418 status_t status;
419
420 constexpr uint32_t seq = 15;
421 int32_t eventId = InputEvent::nextId();
422 constexpr bool touchModeEnabled = true;
423 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
424
425 status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled);
426 ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK";
427
428 uint32_t consumeSeq;
429 InputEvent* event;
430 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
431 ASSERT_EQ(OK, status) << "consumer consume should return OK";
432
433 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
434 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
435 << "consumer should have returned a touch mode event";
436
437 const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
438 EXPECT_EQ(seq, consumeSeq);
439 EXPECT_EQ(eventId, touchModeEvent.getId());
440 EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode());
441
442 status = mConsumer->sendFinishedSignal(seq, true);
443 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
444
445 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
446 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
447 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
448 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
449 ASSERT_EQ(seq, finish.seq)
450 << "receiveConsumerResponse should have returned the original sequence number";
451 ASSERT_TRUE(finish.handled)
452 << "receiveConsumerResponse should have set handled to consumer's reply";
453 ASSERT_GE(finish.consumeTime, publishTime)
454 << "finished signal's consume time should be greater than publish time";
455}
456
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000457TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
458 const int32_t inputEventId = 20;
459 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
460 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
461 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
462 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
463 ASSERT_EQ(OK, status);
464
465 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
466 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
467 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
468 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
469 ASSERT_EQ(inputEventId, timeline.inputEventId);
470 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
471}
472
Jeff Brown5912f952013-07-01 19:10:31 -0700473TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
474 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
475}
476
477TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
478 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
479}
480
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800481TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
482 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
483}
484
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800485TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
486 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
487}
488
arthurhung7632c332020-12-30 16:58:01 +0800489TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
490 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
491}
492
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700493TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) {
494 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
495}
496
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800497TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700498 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800499 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700500 PointerProperties pointerProperties[pointerCount];
501 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800502 for (size_t i = 0; i < pointerCount; i++) {
503 pointerProperties[i].clear();
504 pointerCoords[i].clear();
505 }
Jeff Brown5912f952013-07-01 19:10:31 -0700506
chaviw9eaa22c2020-07-01 16:21:27 -0700507 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800508 status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700509 0, 0, 0, MotionClassification::NONE, identityTransform,
510 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700511 AMOTION_EVENT_INVALID_CURSOR_POSITION,
512 ui::Transform::ROT_0, 0, 0, 0, 0, pointerCount,
513 pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700514 ASSERT_EQ(BAD_VALUE, status)
515 << "publisher publishMotionEvent should return BAD_VALUE";
516}
517
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800518TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
519 status_t status;
520 const size_t pointerCount = 0;
521 PointerProperties pointerProperties[pointerCount];
522 PointerCoords pointerCoords[pointerCount];
523
chaviw9eaa22c2020-07-01 16:21:27 -0700524 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800525 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700526 0, 0, 0, MotionClassification::NONE, identityTransform,
527 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700528 AMOTION_EVENT_INVALID_CURSOR_POSITION,
529 ui::Transform::ROT_0, 0, 0, 0, 0, pointerCount,
530 pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800531 ASSERT_EQ(BAD_VALUE, status)
532 << "publisher publishMotionEvent should return BAD_VALUE";
533}
534
535TEST_F(InputPublisherAndConsumerTest,
536 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700537 status_t status;
538 const size_t pointerCount = MAX_POINTERS + 1;
539 PointerProperties pointerProperties[pointerCount];
540 PointerCoords pointerCoords[pointerCount];
541 for (size_t i = 0; i < pointerCount; i++) {
542 pointerProperties[i].clear();
543 pointerCoords[i].clear();
544 }
545
chaviw9eaa22c2020-07-01 16:21:27 -0700546 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800547 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700548 0, 0, 0, MotionClassification::NONE, identityTransform,
549 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky09576692021-07-01 12:22:09 -0700550 AMOTION_EVENT_INVALID_CURSOR_POSITION,
551 ui::Transform::ROT_0, 0, 0, 0, 0, pointerCount,
552 pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700553 ASSERT_EQ(BAD_VALUE, status)
554 << "publisher publishMotionEvent should return BAD_VALUE";
555}
556
557TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
558 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
559 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
560 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800561 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700562 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
563 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800564 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800565 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800566 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
567 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700568 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700569}
570
571} // namespace android