blob: 965fda73b4d9df7ad7bac2baf994dac3080a271c [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
chaviw09c8d2d2020-08-24 15:48:26 -070019#include <attestation/HmacKeyManager.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050021#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000024using android::base::Result;
25
Jeff Brown5912f952013-07-01 19:10:31 -070026namespace android {
27
Prabir Pradhan00e029d2023-03-09 20:11:09 +000028constexpr static float EPSILON = MotionEvent::ROUNDING_PRECISION;
29
Jeff Brown5912f952013-07-01 19:10:31 -070030class InputPublisherAndConsumerTest : public testing::Test {
31protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050032 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
33 std::unique_ptr<InputPublisher> mPublisher;
34 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070035 PreallocatedInputEventFactory mEventFactory;
36
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050037 void SetUp() override {
38 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080039 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070040 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080041 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050042 mServerChannel = std::move(serverChannel);
43 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070044
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050045 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
46 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070047 }
48
49 void PublishAndConsumeKeyEvent();
50 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080051 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080052 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080053 void PublishAndConsumeDragEvent();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -070054 void PublishAndConsumeTouchModeEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070055};
56
57TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050058 ASSERT_NE(nullptr, mPublisher->getChannel());
59 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050060 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
61 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050062 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
63 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070064}
65
66void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
67 status_t status;
68
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010069 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080070 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010071 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060072 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010073 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060074 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
75 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
76 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010077 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
78 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
79 constexpr int32_t keyCode = AKEYCODE_ENTER;
80 constexpr int32_t scanCode = 13;
81 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
82 constexpr int32_t repeatCount = 1;
83 constexpr nsecs_t downTime = 3;
84 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100085 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070086
Garfield Tanff1f1bb2020-01-28 13:24:04 -080087 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
88 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060089 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070090 ASSERT_EQ(OK, status)
91 << "publisher publishKeyEvent should return OK";
92
93 uint32_t consumeSeq;
94 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +000095 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070096 ASSERT_EQ(OK, status)
97 << "consumer consume should return OK";
98
Yi Kong5bed83b2018-07-17 12:53:47 -070099 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700100 << "consumer should have returned non-NULL event";
101 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
102 << "consumer should have returned a key event";
103
104 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
105 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800106 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700107 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
108 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100109 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600110 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700111 EXPECT_EQ(action, keyEvent->getAction());
112 EXPECT_EQ(flags, keyEvent->getFlags());
113 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
114 EXPECT_EQ(scanCode, keyEvent->getScanCode());
115 EXPECT_EQ(metaState, keyEvent->getMetaState());
116 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
117 EXPECT_EQ(downTime, keyEvent->getDownTime());
118 EXPECT_EQ(eventTime, keyEvent->getEventTime());
119
120 status = mConsumer->sendFinishedSignal(seq, true);
121 ASSERT_EQ(OK, status)
122 << "consumer sendFinishedSignal should return OK";
123
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000124 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
125 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
126 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
127 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
128 ASSERT_EQ(seq, finish.seq)
129 << "receiveConsumerResponse should have returned the original sequence number";
130 ASSERT_TRUE(finish.handled)
131 << "receiveConsumerResponse should have set handled to consumer's reply";
132 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000133 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700134}
135
136void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
137 status_t status;
138
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800139 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800140 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800141 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600142 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100143 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600144 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
145 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
146 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800147 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
148 constexpr int32_t actionButton = 0;
149 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
150 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
151 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
152 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800153 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600154 constexpr float xScale = 2;
155 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800156 constexpr float xOffset = -10;
157 constexpr float yOffset = -20;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700158 constexpr float rawXScale = 4;
159 constexpr float rawYScale = -5;
160 constexpr float rawXOffset = -11;
161 constexpr float rawYOffset = 42;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800162 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;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800166 constexpr nsecs_t downTime = 3;
167 constexpr size_t pointerCount = 3;
168 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000169 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700170 PointerProperties pointerProperties[pointerCount];
171 PointerCoords pointerCoords[pointerCount];
172 for (size_t i = 0; i < pointerCount; i++) {
173 pointerProperties[i].clear();
174 pointerProperties[i].id = (i + 2) % pointerCount;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700175 pointerProperties[i].toolType = ToolType::FINGER;
Jeff Brown5912f952013-07-01 19:10:31 -0700176
177 pointerCoords[i].clear();
178 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
179 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
180 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
181 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
186 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
187 }
188
chaviw9eaa22c2020-07-01 16:21:27 -0700189 ui::Transform transform;
190 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700191 ui::Transform rawTransform;
192 rawTransform.set({rawXScale, 0, rawXOffset, 0, rawYScale, rawYOffset, 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,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700196 xCursorPosition, yCursorPosition, rawTransform,
197 downTime, eventTime, pointerCount, pointerProperties,
198 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;
Harry Cutts82c791c2023-03-10 17:15:07 +0000204 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -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());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000231 EXPECT_NEAR(xCursorPosition, motionEvent->getRawXCursorPosition(), EPSILON);
232 EXPECT_NEAR(yCursorPosition, motionEvent->getRawYCursorPosition(), EPSILON);
233 EXPECT_NEAR(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition(), EPSILON);
234 EXPECT_NEAR(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition(), EPSILON);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700235 EXPECT_EQ(rawTransform, motionEvent->getRawTransform());
Jeff Brown5912f952013-07-01 19:10:31 -0700236 EXPECT_EQ(downTime, motionEvent->getDownTime());
237 EXPECT_EQ(eventTime, motionEvent->getEventTime());
238 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
239 EXPECT_EQ(0U, motionEvent->getHistorySize());
240
241 for (size_t i = 0; i < pointerCount; i++) {
242 SCOPED_TRACE(i);
243 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
244 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
245
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700246 const auto& pc = pointerCoords[i];
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000247 EXPECT_EQ(pc, motionEvent->getSamplePointerCoords()[i]);
248
249 EXPECT_NEAR(pc.getX() * rawXScale + rawXOffset, motionEvent->getRawX(i), EPSILON);
250 EXPECT_NEAR(pc.getY() * rawYScale + rawYOffset, motionEvent->getRawY(i), EPSILON);
251 EXPECT_NEAR(pc.getX() * xScale + xOffset, motionEvent->getX(i), EPSILON);
252 EXPECT_NEAR(pc.getY() * yScale + yOffset, motionEvent->getY(i), EPSILON);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700253 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i));
254 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i));
255 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i));
256 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i));
257 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i));
258 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i));
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700259
260 // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is
261 // "up", and the positive y direction is "down".
262 const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
263 const float x = sinf(unscaledOrientation) * xScale;
264 const float y = -cosf(unscaledOrientation) * yScale;
265 EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700266 }
267
268 status = mConsumer->sendFinishedSignal(seq, false);
269 ASSERT_EQ(OK, status)
270 << "consumer sendFinishedSignal should return OK";
271
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000272 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
273 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
274 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
275 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
276 ASSERT_EQ(seq, finish.seq)
277 << "receiveConsumerResponse should have returned the original sequence number";
278 ASSERT_FALSE(finish.handled)
279 << "receiveConsumerResponse should have set handled to consumer's reply";
280 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000281 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700282}
283
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800284void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
285 status_t status;
286
287 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800288 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800289 constexpr bool hasFocus = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000290 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800291
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700292 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus);
arthurhung7632c332020-12-30 16:58:01 +0800293 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800294
295 uint32_t consumeSeq;
296 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000297 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800298 ASSERT_EQ(OK, status) << "consumer consume should return OK";
299
300 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
301 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
302 << "consumer should have returned a focus event";
303
304 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
305 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800306 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800307 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800308
309 status = mConsumer->sendFinishedSignal(seq, true);
310 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
311
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000312 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
313 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
314 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
315 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000316
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000317 ASSERT_EQ(seq, finish.seq)
318 << "receiveConsumerResponse should have returned the original sequence number";
319 ASSERT_TRUE(finish.handled)
320 << "receiveConsumerResponse should have set handled to consumer's reply";
321 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000322 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800323}
324
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800325void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
326 status_t status;
327
328 constexpr uint32_t seq = 42;
329 int32_t eventId = InputEvent::nextId();
330 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000331 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800332
333 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800334 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800335
336 uint32_t consumeSeq;
337 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000338 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800339 ASSERT_EQ(OK, status) << "consumer consume should return OK";
340
341 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
342 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
343 << "consumer should have returned a capture event";
344
345 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
346 EXPECT_EQ(seq, consumeSeq);
347 EXPECT_EQ(eventId, captureEvent->getId());
348 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
349
350 status = mConsumer->sendFinishedSignal(seq, true);
351 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
352
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000353 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
354 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
355 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
356 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
357 ASSERT_EQ(seq, finish.seq)
358 << "receiveConsumerResponse should have returned the original sequence number";
359 ASSERT_TRUE(finish.handled)
360 << "receiveConsumerResponse should have set handled to consumer's reply";
361 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000362 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800363}
364
arthurhung7632c332020-12-30 16:58:01 +0800365void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
366 status_t status;
367
368 constexpr uint32_t seq = 15;
369 int32_t eventId = InputEvent::nextId();
370 constexpr bool isExiting = false;
371 constexpr float x = 10;
372 constexpr float y = 15;
373 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
374
375 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
376 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
377
378 uint32_t consumeSeq;
379 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000380 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
arthurhung7632c332020-12-30 16:58:01 +0800381 ASSERT_EQ(OK, status) << "consumer consume should return OK";
382
383 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
384 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
385 << "consumer should have returned a drag event";
386
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000387 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800388 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000389 EXPECT_EQ(eventId, dragEvent.getId());
390 EXPECT_EQ(isExiting, dragEvent.isExiting());
391 EXPECT_EQ(x, dragEvent.getX());
392 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800393
394 status = mConsumer->sendFinishedSignal(seq, true);
395 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
396
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000397 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
398 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
399 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
400 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
401 ASSERT_EQ(seq, finish.seq)
402 << "receiveConsumerResponse should have returned the original sequence number";
403 ASSERT_TRUE(finish.handled)
404 << "receiveConsumerResponse should have set handled to consumer's reply";
405 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800406 << "finished signal's consume time should be greater than publish time";
407}
408
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700409void InputPublisherAndConsumerTest::PublishAndConsumeTouchModeEvent() {
410 status_t status;
411
412 constexpr uint32_t seq = 15;
413 int32_t eventId = InputEvent::nextId();
414 constexpr bool touchModeEnabled = true;
415 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
416
417 status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled);
418 ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK";
419
420 uint32_t consumeSeq;
421 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000422 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700423 ASSERT_EQ(OK, status) << "consumer consume should return OK";
424
425 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
426 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
427 << "consumer should have returned a touch mode event";
428
429 const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
430 EXPECT_EQ(seq, consumeSeq);
431 EXPECT_EQ(eventId, touchModeEvent.getId());
432 EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode());
433
434 status = mConsumer->sendFinishedSignal(seq, true);
435 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
436
437 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
438 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
439 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
440 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
441 ASSERT_EQ(seq, finish.seq)
442 << "receiveConsumerResponse should have returned the original sequence number";
443 ASSERT_TRUE(finish.handled)
444 << "receiveConsumerResponse should have set handled to consumer's reply";
445 ASSERT_GE(finish.consumeTime, publishTime)
446 << "finished signal's consume time should be greater than publish time";
447}
448
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000449TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
450 const int32_t inputEventId = 20;
451 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
452 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
453 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
454 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
455 ASSERT_EQ(OK, status);
456
457 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
458 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
459 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
460 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
461 ASSERT_EQ(inputEventId, timeline.inputEventId);
462 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
463}
464
Jeff Brown5912f952013-07-01 19:10:31 -0700465TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
466 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
467}
468
469TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
470 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
471}
472
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800473TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
474 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
475}
476
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800477TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
478 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
479}
480
arthurhung7632c332020-12-30 16:58:01 +0800481TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
482 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
483}
484
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700485TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) {
486 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
487}
488
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800489TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700490 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800491 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700492 PointerProperties pointerProperties[pointerCount];
493 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800494 for (size_t i = 0; i < pointerCount; i++) {
495 pointerProperties[i].clear();
496 pointerCoords[i].clear();
497 }
Jeff Brown5912f952013-07-01 19:10:31 -0700498
chaviw9eaa22c2020-07-01 16:21:27 -0700499 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700500 status =
501 mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
502 0, 0, 0, MotionClassification::NONE, identityTransform,
503 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
504 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
505 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700506 ASSERT_EQ(BAD_VALUE, status)
507 << "publisher publishMotionEvent should return BAD_VALUE";
508}
509
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800510TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
511 status_t status;
512 const size_t pointerCount = 0;
513 PointerProperties pointerProperties[pointerCount];
514 PointerCoords pointerCoords[pointerCount];
515
chaviw9eaa22c2020-07-01 16:21:27 -0700516 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700517 status =
518 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
519 0, 0, 0, MotionClassification::NONE, identityTransform,
520 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
521 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
522 0, 0, pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800523 ASSERT_EQ(BAD_VALUE, status)
524 << "publisher publishMotionEvent should return BAD_VALUE";
525}
526
527TEST_F(InputPublisherAndConsumerTest,
528 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700529 status_t status;
530 const size_t pointerCount = MAX_POINTERS + 1;
531 PointerProperties pointerProperties[pointerCount];
532 PointerCoords pointerCoords[pointerCount];
533 for (size_t i = 0; i < pointerCount; i++) {
534 pointerProperties[i].clear();
535 pointerCoords[i].clear();
536 }
537
chaviw9eaa22c2020-07-01 16:21:27 -0700538 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700539 status =
540 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
541 0, 0, 0, MotionClassification::NONE, identityTransform,
542 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
543 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
544 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700545 ASSERT_EQ(BAD_VALUE, status)
546 << "publisher publishMotionEvent should return BAD_VALUE";
547}
548
549TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
550 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
551 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
552 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800553 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700554 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
555 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800556 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800557 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800558 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
559 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700560 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700561}
562
563} // namespace android