blob: 8393e99d650658123c6571b53722638247df459f [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
Prabir Pradhan0909dc12023-03-09 20:11:09 +000035constexpr static float EPSILON = MotionEvent::ROUNDING_PRECISION;
36
Jeff Brown5912f952013-07-01 19:10:31 -070037class InputPublisherAndConsumerTest : public testing::Test {
38protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050039 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
40 std::unique_ptr<InputPublisher> mPublisher;
41 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070042 PreallocatedInputEventFactory mEventFactory;
43
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050044 void SetUp() override {
45 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080046 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070047 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080048 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050049 mServerChannel = std::move(serverChannel);
50 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070051
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050052 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
53 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070054 }
55
56 void PublishAndConsumeKeyEvent();
57 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080058 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080059 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080060 void PublishAndConsumeDragEvent();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -070061 void PublishAndConsumeTouchModeEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070062};
63
64TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050065 ASSERT_NE(nullptr, mPublisher->getChannel());
66 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050067 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
68 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050069 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
70 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070071}
72
73void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
74 status_t status;
75
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010076 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080077 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010078 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060079 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010080 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060081 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
82 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
83 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010084 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
85 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
86 constexpr int32_t keyCode = AKEYCODE_ENTER;
87 constexpr int32_t scanCode = 13;
88 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
89 constexpr int32_t repeatCount = 1;
90 constexpr nsecs_t downTime = 3;
91 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100092 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070093
Garfield Tanff1f1bb2020-01-28 13:24:04 -080094 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
95 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060096 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070097 ASSERT_EQ(OK, status)
98 << "publisher publishKeyEvent should return OK";
99
100 uint32_t consumeSeq;
101 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800102 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700103 ASSERT_EQ(OK, status)
104 << "consumer consume should return OK";
105
Yi Kong5bed83b2018-07-17 12:53:47 -0700106 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700107 << "consumer should have returned non-NULL event";
108 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
109 << "consumer should have returned a key event";
110
111 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
112 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800113 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700114 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
115 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100116 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600117 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700118 EXPECT_EQ(action, keyEvent->getAction());
119 EXPECT_EQ(flags, keyEvent->getFlags());
120 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
121 EXPECT_EQ(scanCode, keyEvent->getScanCode());
122 EXPECT_EQ(metaState, keyEvent->getMetaState());
123 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
124 EXPECT_EQ(downTime, keyEvent->getDownTime());
125 EXPECT_EQ(eventTime, keyEvent->getEventTime());
126
127 status = mConsumer->sendFinishedSignal(seq, true);
128 ASSERT_EQ(OK, status)
129 << "consumer sendFinishedSignal should return OK";
130
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000131 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
132 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
133 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
134 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
135 ASSERT_EQ(seq, finish.seq)
136 << "receiveConsumerResponse should have returned the original sequence number";
137 ASSERT_TRUE(finish.handled)
138 << "receiveConsumerResponse should have set handled to consumer's reply";
139 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000140 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700141}
142
143void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
144 status_t status;
145
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800146 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800147 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800148 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600149 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100150 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600151 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
152 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
153 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800154 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
155 constexpr int32_t actionButton = 0;
156 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
157 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
158 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
159 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800160 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600161 constexpr float xScale = 2;
162 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800163 constexpr float xOffset = -10;
164 constexpr float yOffset = -20;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700165 constexpr float rawXScale = 4;
166 constexpr float rawYScale = -5;
167 constexpr float rawXOffset = -11;
168 constexpr float rawYOffset = 42;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800169 constexpr float xPrecision = 0.25;
170 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700171 constexpr float xCursorPosition = 1.3;
172 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800173 constexpr nsecs_t downTime = 3;
174 constexpr size_t pointerCount = 3;
175 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000176 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700177 PointerProperties pointerProperties[pointerCount];
178 PointerCoords pointerCoords[pointerCount];
179 for (size_t i = 0; i < pointerCount; i++) {
180 pointerProperties[i].clear();
181 pointerProperties[i].id = (i + 2) % pointerCount;
182 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
183
184 pointerCoords[i].clear();
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
186 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
187 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
188 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
189 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
190 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
191 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
192 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
193 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
194 }
195
chaviw9eaa22c2020-07-01 16:21:27 -0700196 ui::Transform transform;
197 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700198 ui::Transform rawTransform;
199 rawTransform.set({rawXScale, 0, rawXOffset, 0, rawYScale, rawYOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800200 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600201 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700202 classification, transform, xPrecision, yPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700203 xCursorPosition, yCursorPosition, rawTransform,
204 downTime, eventTime, pointerCount, pointerProperties,
205 pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700206 ASSERT_EQ(OK, status)
207 << "publisher publishMotionEvent should return OK";
208
209 uint32_t consumeSeq;
210 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800211 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700212 ASSERT_EQ(OK, status)
213 << "consumer consume should return OK";
214
Yi Kong5bed83b2018-07-17 12:53:47 -0700215 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700216 << "consumer should have returned non-NULL event";
217 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
218 << "consumer should have returned a motion event";
219
220 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
221 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800222 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700223 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
224 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800225 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600226 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700227 EXPECT_EQ(action, motionEvent->getAction());
228 EXPECT_EQ(flags, motionEvent->getFlags());
229 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
230 EXPECT_EQ(metaState, motionEvent->getMetaState());
231 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800232 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700233 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600234 EXPECT_EQ(xOffset, motionEvent->getXOffset());
235 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700236 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
237 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000238 EXPECT_NEAR(xCursorPosition, motionEvent->getRawXCursorPosition(), EPSILON);
239 EXPECT_NEAR(yCursorPosition, motionEvent->getRawYCursorPosition(), EPSILON);
240 EXPECT_NEAR(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition(), EPSILON);
241 EXPECT_NEAR(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition(), EPSILON);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700242 EXPECT_EQ(rawTransform, motionEvent->getRawTransform());
Jeff Brown5912f952013-07-01 19:10:31 -0700243 EXPECT_EQ(downTime, motionEvent->getDownTime());
244 EXPECT_EQ(eventTime, motionEvent->getEventTime());
245 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
246 EXPECT_EQ(0U, motionEvent->getHistorySize());
247
248 for (size_t i = 0; i < pointerCount; i++) {
249 SCOPED_TRACE(i);
250 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
251 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
252
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700253 const auto& pc = pointerCoords[i];
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000254 EXPECT_EQ(pc, motionEvent->getSamplePointerCoords()[i]);
255
256 EXPECT_NEAR(pc.getX() * rawXScale + rawXOffset, motionEvent->getRawX(i), EPSILON);
257 EXPECT_NEAR(pc.getY() * rawYScale + rawYOffset, motionEvent->getRawY(i), EPSILON);
258 EXPECT_NEAR(pc.getX() * xScale + xOffset, motionEvent->getX(i), EPSILON);
259 EXPECT_NEAR(pc.getY() * yScale + yOffset, motionEvent->getY(i), EPSILON);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700260 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i));
261 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i));
262 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i));
263 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i));
264 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i));
265 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i));
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700266
267 // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is
268 // "up", and the positive y direction is "down".
269 const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
270 const float x = sinf(unscaledOrientation) * xScale;
271 const float y = -cosf(unscaledOrientation) * yScale;
272 EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700273 }
274
275 status = mConsumer->sendFinishedSignal(seq, false);
276 ASSERT_EQ(OK, status)
277 << "consumer sendFinishedSignal should return OK";
278
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000279 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
280 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
281 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
282 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
283 ASSERT_EQ(seq, finish.seq)
284 << "receiveConsumerResponse should have returned the original sequence number";
285 ASSERT_FALSE(finish.handled)
286 << "receiveConsumerResponse should have set handled to consumer's reply";
287 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000288 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700289}
290
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800291void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
292 status_t status;
293
294 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800295 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800296 constexpr bool hasFocus = 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
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700299 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus);
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());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800315
316 status = mConsumer->sendFinishedSignal(seq, true);
317 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
318
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000319 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
320 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
321 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
322 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000323
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000324 ASSERT_EQ(seq, finish.seq)
325 << "receiveConsumerResponse should have returned the original sequence number";
326 ASSERT_TRUE(finish.handled)
327 << "receiveConsumerResponse should have set handled to consumer's reply";
328 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000329 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800330}
331
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800332void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
333 status_t status;
334
335 constexpr uint32_t seq = 42;
336 int32_t eventId = InputEvent::nextId();
337 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000338 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800339
340 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800341 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800342
343 uint32_t consumeSeq;
344 InputEvent* event;
345 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
346 ASSERT_EQ(OK, status) << "consumer consume should return OK";
347
348 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
349 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
350 << "consumer should have returned a capture event";
351
352 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
353 EXPECT_EQ(seq, consumeSeq);
354 EXPECT_EQ(eventId, captureEvent->getId());
355 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
356
357 status = mConsumer->sendFinishedSignal(seq, true);
358 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
359
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000360 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
361 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
362 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
363 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
364 ASSERT_EQ(seq, finish.seq)
365 << "receiveConsumerResponse should have returned the original sequence number";
366 ASSERT_TRUE(finish.handled)
367 << "receiveConsumerResponse should have set handled to consumer's reply";
368 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000369 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800370}
371
arthurhung7632c332020-12-30 16:58:01 +0800372void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
373 status_t status;
374
375 constexpr uint32_t seq = 15;
376 int32_t eventId = InputEvent::nextId();
377 constexpr bool isExiting = false;
378 constexpr float x = 10;
379 constexpr float y = 15;
380 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
381
382 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
383 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
384
385 uint32_t consumeSeq;
386 InputEvent* event;
387 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
388 ASSERT_EQ(OK, status) << "consumer consume should return OK";
389
390 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
391 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
392 << "consumer should have returned a drag event";
393
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000394 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800395 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000396 EXPECT_EQ(eventId, dragEvent.getId());
397 EXPECT_EQ(isExiting, dragEvent.isExiting());
398 EXPECT_EQ(x, dragEvent.getX());
399 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800400
401 status = mConsumer->sendFinishedSignal(seq, true);
402 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
403
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000404 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
405 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
406 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
407 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
408 ASSERT_EQ(seq, finish.seq)
409 << "receiveConsumerResponse should have returned the original sequence number";
410 ASSERT_TRUE(finish.handled)
411 << "receiveConsumerResponse should have set handled to consumer's reply";
412 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800413 << "finished signal's consume time should be greater than publish time";
414}
415
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700416void InputPublisherAndConsumerTest::PublishAndConsumeTouchModeEvent() {
417 status_t status;
418
419 constexpr uint32_t seq = 15;
420 int32_t eventId = InputEvent::nextId();
421 constexpr bool touchModeEnabled = true;
422 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
423
424 status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled);
425 ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK";
426
427 uint32_t consumeSeq;
428 InputEvent* event;
429 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
430 ASSERT_EQ(OK, status) << "consumer consume should return OK";
431
432 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
433 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
434 << "consumer should have returned a touch mode event";
435
436 const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
437 EXPECT_EQ(seq, consumeSeq);
438 EXPECT_EQ(eventId, touchModeEvent.getId());
439 EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode());
440
441 status = mConsumer->sendFinishedSignal(seq, true);
442 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
443
444 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
445 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
446 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
447 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
448 ASSERT_EQ(seq, finish.seq)
449 << "receiveConsumerResponse should have returned the original sequence number";
450 ASSERT_TRUE(finish.handled)
451 << "receiveConsumerResponse should have set handled to consumer's reply";
452 ASSERT_GE(finish.consumeTime, publishTime)
453 << "finished signal's consume time should be greater than publish time";
454}
455
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000456TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
457 const int32_t inputEventId = 20;
458 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
459 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
460 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
461 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
462 ASSERT_EQ(OK, status);
463
464 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
465 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
466 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
467 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
468 ASSERT_EQ(inputEventId, timeline.inputEventId);
469 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
470}
471
Jeff Brown5912f952013-07-01 19:10:31 -0700472TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
473 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
474}
475
476TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
477 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
478}
479
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800480TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
481 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
482}
483
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800484TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
485 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
486}
487
arthurhung7632c332020-12-30 16:58:01 +0800488TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
489 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
490}
491
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700492TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) {
493 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
494}
495
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800496TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700497 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800498 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700499 PointerProperties pointerProperties[pointerCount];
500 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800501 for (size_t i = 0; i < pointerCount; i++) {
502 pointerProperties[i].clear();
503 pointerCoords[i].clear();
504 }
Jeff Brown5912f952013-07-01 19:10:31 -0700505
chaviw9eaa22c2020-07-01 16:21:27 -0700506 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700507 status =
508 mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
509 0, 0, 0, MotionClassification::NONE, identityTransform,
510 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
511 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
512 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700513 ASSERT_EQ(BAD_VALUE, status)
514 << "publisher publishMotionEvent should return BAD_VALUE";
515}
516
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800517TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
518 status_t status;
519 const size_t pointerCount = 0;
520 PointerProperties pointerProperties[pointerCount];
521 PointerCoords pointerCoords[pointerCount];
522
chaviw9eaa22c2020-07-01 16:21:27 -0700523 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700524 status =
525 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
526 0, 0, 0, MotionClassification::NONE, identityTransform,
527 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
528 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
529 0, 0, pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800530 ASSERT_EQ(BAD_VALUE, status)
531 << "publisher publishMotionEvent should return BAD_VALUE";
532}
533
534TEST_F(InputPublisherAndConsumerTest,
535 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700536 status_t status;
537 const size_t pointerCount = MAX_POINTERS + 1;
538 PointerProperties pointerProperties[pointerCount];
539 PointerCoords pointerCoords[pointerCount];
540 for (size_t i = 0; i < pointerCount; i++) {
541 pointerProperties[i].clear();
542 pointerCoords[i].clear();
543 }
544
chaviw9eaa22c2020-07-01 16:21:27 -0700545 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700546 status =
547 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
548 0, 0, 0, MotionClassification::NONE, identityTransform,
549 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
550 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
551 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700552 ASSERT_EQ(BAD_VALUE, status)
553 << "publisher publishMotionEvent should return BAD_VALUE";
554}
555
556TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
557 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
558 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
559 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800560 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700561 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
562 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800563 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800564 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800565 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
566 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700567 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700568}
569
570} // namespace android