blob: 088e00b59c8ce54c9dcecc0650ff3b0a4dfbb169 [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>
26#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/StopWatch.h>
chaviw09c8d2d2020-08-24 15:48:26 -070028#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000030using android::base::Result;
31
Jeff Brown5912f952013-07-01 19:10:31 -070032namespace android {
33
34class InputPublisherAndConsumerTest : public testing::Test {
35protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050036 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
37 std::unique_ptr<InputPublisher> mPublisher;
38 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070039 PreallocatedInputEventFactory mEventFactory;
40
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050041 void SetUp() override {
42 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080043 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070044 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080045 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050046 mServerChannel = std::move(serverChannel);
47 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070048
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050049 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
50 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070051 }
52
53 void PublishAndConsumeKeyEvent();
54 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080055 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080056 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080057 void PublishAndConsumeDragEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070058};
59
60TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050061 ASSERT_NE(nullptr, mPublisher->getChannel());
62 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050063 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
64 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050065 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
66 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070067}
68
69void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
70 status_t status;
71
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010072 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080073 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010074 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060075 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010076 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060077 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
78 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
79 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010080 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
81 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
82 constexpr int32_t keyCode = AKEYCODE_ENTER;
83 constexpr int32_t scanCode = 13;
84 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
85 constexpr int32_t repeatCount = 1;
86 constexpr nsecs_t downTime = 3;
87 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100088 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070089
Garfield Tanff1f1bb2020-01-28 13:24:04 -080090 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
91 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060092 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070093 ASSERT_EQ(OK, status)
94 << "publisher publishKeyEvent should return OK";
95
96 uint32_t consumeSeq;
97 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080098 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070099 ASSERT_EQ(OK, status)
100 << "consumer consume should return OK";
101
Yi Kong5bed83b2018-07-17 12:53:47 -0700102 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700103 << "consumer should have returned non-NULL event";
104 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
105 << "consumer should have returned a key event";
106
107 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
108 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800109 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700110 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
111 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100112 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600113 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700114 EXPECT_EQ(action, keyEvent->getAction());
115 EXPECT_EQ(flags, keyEvent->getFlags());
116 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
117 EXPECT_EQ(scanCode, keyEvent->getScanCode());
118 EXPECT_EQ(metaState, keyEvent->getMetaState());
119 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
120 EXPECT_EQ(downTime, keyEvent->getDownTime());
121 EXPECT_EQ(eventTime, keyEvent->getEventTime());
122
123 status = mConsumer->sendFinishedSignal(seq, true);
124 ASSERT_EQ(OK, status)
125 << "consumer sendFinishedSignal should return OK";
126
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000127 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
128 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
129 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
130 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
131 ASSERT_EQ(seq, finish.seq)
132 << "receiveConsumerResponse should have returned the original sequence number";
133 ASSERT_TRUE(finish.handled)
134 << "receiveConsumerResponse should have set handled to consumer's reply";
135 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000136 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700137}
138
139void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
140 status_t status;
141
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800142 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800143 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800144 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600145 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100146 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600147 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
148 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
149 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800150 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
151 constexpr int32_t actionButton = 0;
152 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
153 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
154 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
155 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800156 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600157 constexpr float xScale = 2;
158 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800159 constexpr float xOffset = -10;
160 constexpr float yOffset = -20;
161 constexpr float xPrecision = 0.25;
162 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700163 constexpr float xCursorPosition = 1.3;
164 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800165 constexpr nsecs_t downTime = 3;
166 constexpr size_t pointerCount = 3;
167 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000168 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700169 PointerProperties pointerProperties[pointerCount];
170 PointerCoords pointerCoords[pointerCount];
171 for (size_t i = 0; i < pointerCount; i++) {
172 pointerProperties[i].clear();
173 pointerProperties[i].id = (i + 2) % pointerCount;
174 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
175
176 pointerCoords[i].clear();
177 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
178 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
179 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
180 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
181 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
186 }
187
chaviw9eaa22c2020-07-01 16:21:27 -0700188 ui::Transform transform;
189 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800190 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600191 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700192 classification, transform, xPrecision, yPrecision,
193 xCursorPosition, yCursorPosition, downTime, eventTime,
194 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700195 ASSERT_EQ(OK, status)
196 << "publisher publishMotionEvent should return OK";
197
198 uint32_t consumeSeq;
199 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800200 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700201 ASSERT_EQ(OK, status)
202 << "consumer consume should return OK";
203
Yi Kong5bed83b2018-07-17 12:53:47 -0700204 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700205 << "consumer should have returned non-NULL event";
206 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
207 << "consumer should have returned a motion event";
208
209 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
210 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800211 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700212 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
213 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800214 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600215 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700216 EXPECT_EQ(action, motionEvent->getAction());
217 EXPECT_EQ(flags, motionEvent->getFlags());
218 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
219 EXPECT_EQ(metaState, motionEvent->getMetaState());
220 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800221 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700222 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600223 EXPECT_EQ(xOffset, motionEvent->getXOffset());
224 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700225 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
226 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700227 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
228 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800229 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
230 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Jeff Brown5912f952013-07-01 19:10:31 -0700231 EXPECT_EQ(downTime, motionEvent->getDownTime());
232 EXPECT_EQ(eventTime, motionEvent->getEventTime());
233 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
234 EXPECT_EQ(0U, motionEvent->getHistorySize());
235
236 for (size_t i = 0; i < pointerCount; i++) {
237 SCOPED_TRACE(i);
238 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
239 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
240
241 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
242 motionEvent->getRawX(i));
243 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
244 motionEvent->getRawY(i));
chaviw82357092020-01-28 13:13:06 -0800245 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
246 motionEvent->getX(i));
247 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
248 motionEvent->getY(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700249 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
250 motionEvent->getPressure(i));
251 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
252 motionEvent->getSize(i));
253 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
254 motionEvent->getTouchMajor(i));
255 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
256 motionEvent->getTouchMinor(i));
257 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
258 motionEvent->getToolMajor(i));
259 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
260 motionEvent->getToolMinor(i));
261 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
262 motionEvent->getOrientation(i));
263 }
264
265 status = mConsumer->sendFinishedSignal(seq, false);
266 ASSERT_EQ(OK, status)
267 << "consumer sendFinishedSignal should return OK";
268
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000269 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
270 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
271 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
272 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
273 ASSERT_EQ(seq, finish.seq)
274 << "receiveConsumerResponse should have returned the original sequence number";
275 ASSERT_FALSE(finish.handled)
276 << "receiveConsumerResponse should have set handled to consumer's reply";
277 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000278 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700279}
280
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800281void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
282 status_t status;
283
284 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800285 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800286 constexpr bool hasFocus = true;
287 constexpr bool inTouchMode = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000288 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800289
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800290 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
arthurhung7632c332020-12-30 16:58:01 +0800291 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800292
293 uint32_t consumeSeq;
294 InputEvent* event;
295 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
296 ASSERT_EQ(OK, status) << "consumer consume should return OK";
297
298 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
299 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
300 << "consumer should have returned a focus event";
301
302 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
303 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800304 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800305 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
306 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
307
308 status = mConsumer->sendFinishedSignal(seq, true);
309 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
310
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000311 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
312 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
313 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
314 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000315
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000316 ASSERT_EQ(seq, finish.seq)
317 << "receiveConsumerResponse should have returned the original sequence number";
318 ASSERT_TRUE(finish.handled)
319 << "receiveConsumerResponse should have set handled to consumer's reply";
320 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000321 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800322}
323
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800324void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
325 status_t status;
326
327 constexpr uint32_t seq = 42;
328 int32_t eventId = InputEvent::nextId();
329 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000330 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800331
332 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800333 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800334
335 uint32_t consumeSeq;
336 InputEvent* event;
337 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
338 ASSERT_EQ(OK, status) << "consumer consume should return OK";
339
340 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
341 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
342 << "consumer should have returned a capture event";
343
344 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
345 EXPECT_EQ(seq, consumeSeq);
346 EXPECT_EQ(eventId, captureEvent->getId());
347 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
348
349 status = mConsumer->sendFinishedSignal(seq, true);
350 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
351
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000352 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
353 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
354 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
355 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
356 ASSERT_EQ(seq, finish.seq)
357 << "receiveConsumerResponse should have returned the original sequence number";
358 ASSERT_TRUE(finish.handled)
359 << "receiveConsumerResponse should have set handled to consumer's reply";
360 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000361 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800362}
363
arthurhung7632c332020-12-30 16:58:01 +0800364void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
365 status_t status;
366
367 constexpr uint32_t seq = 15;
368 int32_t eventId = InputEvent::nextId();
369 constexpr bool isExiting = false;
370 constexpr float x = 10;
371 constexpr float y = 15;
372 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
373
374 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
375 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
376
377 uint32_t consumeSeq;
378 InputEvent* event;
379 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
380 ASSERT_EQ(OK, status) << "consumer consume should return OK";
381
382 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
383 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
384 << "consumer should have returned a drag event";
385
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000386 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800387 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000388 EXPECT_EQ(eventId, dragEvent.getId());
389 EXPECT_EQ(isExiting, dragEvent.isExiting());
390 EXPECT_EQ(x, dragEvent.getX());
391 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800392
393 status = mConsumer->sendFinishedSignal(seq, true);
394 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
395
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000396 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
397 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
398 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
399 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
400 ASSERT_EQ(seq, finish.seq)
401 << "receiveConsumerResponse should have returned the original sequence number";
402 ASSERT_TRUE(finish.handled)
403 << "receiveConsumerResponse should have set handled to consumer's reply";
404 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800405 << "finished signal's consume time should be greater than publish time";
406}
407
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000408TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
409 const int32_t inputEventId = 20;
410 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
411 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
412 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
413 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
414 ASSERT_EQ(OK, status);
415
416 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
417 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
418 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
419 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
420 ASSERT_EQ(inputEventId, timeline.inputEventId);
421 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
422}
423
Jeff Brown5912f952013-07-01 19:10:31 -0700424TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
425 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
426}
427
428TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
429 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
430}
431
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800432TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
433 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
434}
435
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800436TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
437 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
438}
439
arthurhung7632c332020-12-30 16:58:01 +0800440TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
441 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
442}
443
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800444TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700445 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800446 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 PointerProperties pointerProperties[pointerCount];
448 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800449 for (size_t i = 0; i < pointerCount; i++) {
450 pointerProperties[i].clear();
451 pointerCoords[i].clear();
452 }
Jeff Brown5912f952013-07-01 19:10:31 -0700453
chaviw9eaa22c2020-07-01 16:21:27 -0700454 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800455 status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700456 0, 0, 0, MotionClassification::NONE, identityTransform,
457 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600458 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
459 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700460 ASSERT_EQ(BAD_VALUE, status)
461 << "publisher publishMotionEvent should return BAD_VALUE";
462}
463
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800464TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
465 status_t status;
466 const size_t pointerCount = 0;
467 PointerProperties pointerProperties[pointerCount];
468 PointerCoords pointerCoords[pointerCount];
469
chaviw9eaa22c2020-07-01 16:21:27 -0700470 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800471 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700472 0, 0, 0, MotionClassification::NONE, identityTransform,
473 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600474 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
475 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800476 ASSERT_EQ(BAD_VALUE, status)
477 << "publisher publishMotionEvent should return BAD_VALUE";
478}
479
480TEST_F(InputPublisherAndConsumerTest,
481 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700482 status_t status;
483 const size_t pointerCount = MAX_POINTERS + 1;
484 PointerProperties pointerProperties[pointerCount];
485 PointerCoords pointerCoords[pointerCount];
486 for (size_t i = 0; i < pointerCount; i++) {
487 pointerProperties[i].clear();
488 pointerCoords[i].clear();
489 }
490
chaviw9eaa22c2020-07-01 16:21:27 -0700491 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800492 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700493 0, 0, 0, MotionClassification::NONE, identityTransform,
494 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600495 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
496 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700497 ASSERT_EQ(BAD_VALUE, status)
498 << "publisher publishMotionEvent should return BAD_VALUE";
499}
500
501TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
502 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
503 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
504 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800505 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700506 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
507 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800508 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800509 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800510 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
511 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700512}
513
514} // namespace android