blob: d0c337c3a61b6e6aa17035e41e43b2737fa62f8d [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();
Jeff Brown5912f952013-07-01 19:10:31 -070059};
60
61TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050062 ASSERT_NE(nullptr, mPublisher->getChannel());
63 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050064 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
65 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050066 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
67 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070068}
69
70void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
71 status_t status;
72
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010073 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080074 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010075 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060076 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010077 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060078 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
79 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
80 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010081 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
82 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
83 constexpr int32_t keyCode = AKEYCODE_ENTER;
84 constexpr int32_t scanCode = 13;
85 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
86 constexpr int32_t repeatCount = 1;
87 constexpr nsecs_t downTime = 3;
88 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100089 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070090
Garfield Tanff1f1bb2020-01-28 13:24:04 -080091 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
92 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060093 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070094 ASSERT_EQ(OK, status)
95 << "publisher publishKeyEvent should return OK";
96
97 uint32_t consumeSeq;
98 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080099 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700100 ASSERT_EQ(OK, status)
101 << "consumer consume should return OK";
102
Yi Kong5bed83b2018-07-17 12:53:47 -0700103 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700104 << "consumer should have returned non-NULL event";
105 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
106 << "consumer should have returned a key event";
107
108 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
109 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800110 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700111 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
112 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100113 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600114 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700115 EXPECT_EQ(action, keyEvent->getAction());
116 EXPECT_EQ(flags, keyEvent->getFlags());
117 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
118 EXPECT_EQ(scanCode, keyEvent->getScanCode());
119 EXPECT_EQ(metaState, keyEvent->getMetaState());
120 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
121 EXPECT_EQ(downTime, keyEvent->getDownTime());
122 EXPECT_EQ(eventTime, keyEvent->getEventTime());
123
124 status = mConsumer->sendFinishedSignal(seq, true);
125 ASSERT_EQ(OK, status)
126 << "consumer sendFinishedSignal should return OK";
127
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000128 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
129 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
130 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
131 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
132 ASSERT_EQ(seq, finish.seq)
133 << "receiveConsumerResponse should have returned the original sequence number";
134 ASSERT_TRUE(finish.handled)
135 << "receiveConsumerResponse should have set handled to consumer's reply";
136 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000137 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700138}
139
140void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
141 status_t status;
142
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800143 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800144 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800145 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600146 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100147 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600148 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
149 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
150 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800151 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
152 constexpr int32_t actionButton = 0;
153 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
154 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
155 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
156 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800157 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600158 constexpr float xScale = 2;
159 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800160 constexpr float xOffset = -10;
161 constexpr float yOffset = -20;
162 constexpr float xPrecision = 0.25;
163 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700164 constexpr float xCursorPosition = 1.3;
165 constexpr float yCursorPosition = 50.6;
Evan Rosky84f07f02021-04-16 10:42:42 -0700166 constexpr int32_t displayWidth = 1000;
167 constexpr int32_t displayHeight = 2000;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800168 constexpr nsecs_t downTime = 3;
169 constexpr size_t pointerCount = 3;
170 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000171 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700172 PointerProperties pointerProperties[pointerCount];
173 PointerCoords pointerCoords[pointerCount];
174 for (size_t i = 0; i < pointerCount; i++) {
175 pointerProperties[i].clear();
176 pointerProperties[i].id = (i + 2) % pointerCount;
177 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
178
179 pointerCoords[i].clear();
180 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
181 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
186 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
187 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
188 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
189 }
190
chaviw9eaa22c2020-07-01 16:21:27 -0700191 ui::Transform transform;
192 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800193 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600194 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700195 classification, transform, xPrecision, yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700196 xCursorPosition, yCursorPosition, displayWidth,
197 displayHeight, downTime, eventTime, pointerCount,
198 pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700199 ASSERT_EQ(OK, status)
200 << "publisher publishMotionEvent should return OK";
201
202 uint32_t consumeSeq;
203 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800204 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700205 ASSERT_EQ(OK, status)
206 << "consumer consume should return OK";
207
Yi Kong5bed83b2018-07-17 12:53:47 -0700208 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700209 << "consumer should have returned non-NULL event";
210 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
211 << "consumer should have returned a motion event";
212
213 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
214 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800215 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700216 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
217 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800218 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600219 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700220 EXPECT_EQ(action, motionEvent->getAction());
221 EXPECT_EQ(flags, motionEvent->getFlags());
222 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
223 EXPECT_EQ(metaState, motionEvent->getMetaState());
224 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800225 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700226 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600227 EXPECT_EQ(xOffset, motionEvent->getXOffset());
228 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700229 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
230 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700231 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
232 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800233 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
234 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Evan Rosky84f07f02021-04-16 10:42:42 -0700235 EXPECT_EQ(displayWidth, motionEvent->getDisplaySize().x);
236 EXPECT_EQ(displayHeight, motionEvent->getDisplaySize().y);
Jeff Brown5912f952013-07-01 19:10:31 -0700237 EXPECT_EQ(downTime, motionEvent->getDownTime());
238 EXPECT_EQ(eventTime, motionEvent->getEventTime());
239 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
240 EXPECT_EQ(0U, motionEvent->getHistorySize());
241
242 for (size_t i = 0; i < pointerCount; i++) {
243 SCOPED_TRACE(i);
244 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
245 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
246
247 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
248 motionEvent->getRawX(i));
249 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
250 motionEvent->getRawY(i));
chaviw82357092020-01-28 13:13:06 -0800251 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
252 motionEvent->getX(i));
253 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
254 motionEvent->getY(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700255 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
256 motionEvent->getPressure(i));
257 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
258 motionEvent->getSize(i));
259 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
260 motionEvent->getTouchMajor(i));
261 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
262 motionEvent->getTouchMinor(i));
263 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
264 motionEvent->getToolMajor(i));
265 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
266 motionEvent->getToolMinor(i));
267 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
268 motionEvent->getOrientation(i));
269 }
270
271 status = mConsumer->sendFinishedSignal(seq, false);
272 ASSERT_EQ(OK, status)
273 << "consumer sendFinishedSignal should return OK";
274
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000275 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
276 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
277 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
278 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
279 ASSERT_EQ(seq, finish.seq)
280 << "receiveConsumerResponse should have returned the original sequence number";
281 ASSERT_FALSE(finish.handled)
282 << "receiveConsumerResponse should have set handled to consumer's reply";
283 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000284 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700285}
286
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800287void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
288 status_t status;
289
290 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800291 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800292 constexpr bool hasFocus = true;
293 constexpr bool inTouchMode = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000294 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800295
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800296 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
arthurhung7632c332020-12-30 16:58:01 +0800297 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800298
299 uint32_t consumeSeq;
300 InputEvent* event;
301 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
302 ASSERT_EQ(OK, status) << "consumer consume should return OK";
303
304 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
305 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
306 << "consumer should have returned a focus event";
307
308 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
309 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800310 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800311 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
312 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
313
314 status = mConsumer->sendFinishedSignal(seq, true);
315 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
316
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000317 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
318 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
319 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
320 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000321
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000322 ASSERT_EQ(seq, finish.seq)
323 << "receiveConsumerResponse should have returned the original sequence number";
324 ASSERT_TRUE(finish.handled)
325 << "receiveConsumerResponse should have set handled to consumer's reply";
326 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000327 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800328}
329
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800330void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
331 status_t status;
332
333 constexpr uint32_t seq = 42;
334 int32_t eventId = InputEvent::nextId();
335 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000336 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800337
338 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800339 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800340
341 uint32_t consumeSeq;
342 InputEvent* event;
343 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
344 ASSERT_EQ(OK, status) << "consumer consume should return OK";
345
346 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
347 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
348 << "consumer should have returned a capture event";
349
350 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
351 EXPECT_EQ(seq, consumeSeq);
352 EXPECT_EQ(eventId, captureEvent->getId());
353 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
354
355 status = mConsumer->sendFinishedSignal(seq, true);
356 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
357
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000358 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
359 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
360 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
361 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
362 ASSERT_EQ(seq, finish.seq)
363 << "receiveConsumerResponse should have returned the original sequence number";
364 ASSERT_TRUE(finish.handled)
365 << "receiveConsumerResponse should have set handled to consumer's reply";
366 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000367 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800368}
369
arthurhung7632c332020-12-30 16:58:01 +0800370void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
371 status_t status;
372
373 constexpr uint32_t seq = 15;
374 int32_t eventId = InputEvent::nextId();
375 constexpr bool isExiting = false;
376 constexpr float x = 10;
377 constexpr float y = 15;
378 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
379
380 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
381 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
382
383 uint32_t consumeSeq;
384 InputEvent* event;
385 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
386 ASSERT_EQ(OK, status) << "consumer consume should return OK";
387
388 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
389 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
390 << "consumer should have returned a drag event";
391
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000392 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800393 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000394 EXPECT_EQ(eventId, dragEvent.getId());
395 EXPECT_EQ(isExiting, dragEvent.isExiting());
396 EXPECT_EQ(x, dragEvent.getX());
397 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800398
399 status = mConsumer->sendFinishedSignal(seq, true);
400 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
401
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000402 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
403 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
404 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
405 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
406 ASSERT_EQ(seq, finish.seq)
407 << "receiveConsumerResponse should have returned the original sequence number";
408 ASSERT_TRUE(finish.handled)
409 << "receiveConsumerResponse should have set handled to consumer's reply";
410 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800411 << "finished signal's consume time should be greater than publish time";
412}
413
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000414TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
415 const int32_t inputEventId = 20;
416 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
417 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
418 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
419 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
420 ASSERT_EQ(OK, status);
421
422 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
423 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
424 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
425 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
426 ASSERT_EQ(inputEventId, timeline.inputEventId);
427 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
428}
429
Jeff Brown5912f952013-07-01 19:10:31 -0700430TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
431 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
432}
433
434TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
435 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
436}
437
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800438TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
439 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
440}
441
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800442TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
443 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
444}
445
arthurhung7632c332020-12-30 16:58:01 +0800446TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
447 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
448}
449
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800450TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700451 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800452 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700453 PointerProperties pointerProperties[pointerCount];
454 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800455 for (size_t i = 0; i < pointerCount; i++) {
456 pointerProperties[i].clear();
457 pointerCoords[i].clear();
458 }
Jeff Brown5912f952013-07-01 19:10:31 -0700459
chaviw9eaa22c2020-07-01 16:21:27 -0700460 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800461 status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700462 0, 0, 0, MotionClassification::NONE, identityTransform,
463 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700464 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0, 0, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600465 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700466 ASSERT_EQ(BAD_VALUE, status)
467 << "publisher publishMotionEvent should return BAD_VALUE";
468}
469
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800470TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
471 status_t status;
472 const size_t pointerCount = 0;
473 PointerProperties pointerProperties[pointerCount];
474 PointerCoords pointerCoords[pointerCount];
475
chaviw9eaa22c2020-07-01 16:21:27 -0700476 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800477 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700478 0, 0, 0, MotionClassification::NONE, identityTransform,
479 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700480 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0, 0, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600481 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800482 ASSERT_EQ(BAD_VALUE, status)
483 << "publisher publishMotionEvent should return BAD_VALUE";
484}
485
486TEST_F(InputPublisherAndConsumerTest,
487 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700488 status_t status;
489 const size_t pointerCount = MAX_POINTERS + 1;
490 PointerProperties pointerProperties[pointerCount];
491 PointerCoords pointerCoords[pointerCount];
492 for (size_t i = 0; i < pointerCount; i++) {
493 pointerProperties[i].clear();
494 pointerCoords[i].clear();
495 }
496
chaviw9eaa22c2020-07-01 16:21:27 -0700497 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800498 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700499 0, 0, 0, MotionClassification::NONE, identityTransform,
500 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700501 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0, 0, 0,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600502 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700503 ASSERT_EQ(BAD_VALUE, status)
504 << "publisher publishMotionEvent should return BAD_VALUE";
505}
506
507TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
508 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
509 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
510 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800511 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700512 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
513 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800514 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800515 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800516 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
517 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700518}
519
520} // namespace android