blob: 75a01a16d84faf17bc0b1b63707960e32ddbafc4 [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
28class InputPublisherAndConsumerTest : public testing::Test {
29protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050030 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
31 std::unique_ptr<InputPublisher> mPublisher;
32 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070033 PreallocatedInputEventFactory mEventFactory;
34
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050035 void SetUp() override {
36 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080037 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070038 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080039 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050040 mServerChannel = std::move(serverChannel);
41 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050043 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
44 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070045 }
46
47 void PublishAndConsumeKeyEvent();
48 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080049 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080050 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080051 void PublishAndConsumeDragEvent();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -070052 void PublishAndConsumeTouchModeEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070053};
54
55TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050056 ASSERT_NE(nullptr, mPublisher->getChannel());
57 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050058 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
59 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050060 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
61 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070062}
63
64void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
65 status_t status;
66
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010067 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080068 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010069 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060070 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010071 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060072 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
73 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
74 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010075 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
76 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
77 constexpr int32_t keyCode = AKEYCODE_ENTER;
78 constexpr int32_t scanCode = 13;
79 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
80 constexpr int32_t repeatCount = 1;
81 constexpr nsecs_t downTime = 3;
82 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100083 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070084
Garfield Tanff1f1bb2020-01-28 13:24:04 -080085 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
86 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060087 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070088 ASSERT_EQ(OK, status)
89 << "publisher publishKeyEvent should return OK";
90
91 uint32_t consumeSeq;
92 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +000093 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070094 ASSERT_EQ(OK, status)
95 << "consumer consume should return OK";
96
Yi Kong5bed83b2018-07-17 12:53:47 -070097 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -070098 << "consumer should have returned non-NULL event";
99 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
100 << "consumer should have returned a key event";
101
102 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
103 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800104 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700105 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
106 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100107 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600108 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700109 EXPECT_EQ(action, keyEvent->getAction());
110 EXPECT_EQ(flags, keyEvent->getFlags());
111 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
112 EXPECT_EQ(scanCode, keyEvent->getScanCode());
113 EXPECT_EQ(metaState, keyEvent->getMetaState());
114 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
115 EXPECT_EQ(downTime, keyEvent->getDownTime());
116 EXPECT_EQ(eventTime, keyEvent->getEventTime());
117
118 status = mConsumer->sendFinishedSignal(seq, true);
119 ASSERT_EQ(OK, status)
120 << "consumer sendFinishedSignal should return OK";
121
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000122 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
123 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
124 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
125 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
126 ASSERT_EQ(seq, finish.seq)
127 << "receiveConsumerResponse should have returned the original sequence number";
128 ASSERT_TRUE(finish.handled)
129 << "receiveConsumerResponse should have set handled to consumer's reply";
130 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000131 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700132}
133
134void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
135 status_t status;
136
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800137 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800138 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800139 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600140 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100141 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600142 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
143 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
144 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800145 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
146 constexpr int32_t actionButton = 0;
147 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
148 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
149 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
150 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800151 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600152 constexpr float xScale = 2;
153 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800154 constexpr float xOffset = -10;
155 constexpr float yOffset = -20;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700156 constexpr float rawXScale = 4;
157 constexpr float rawYScale = -5;
158 constexpr float rawXOffset = -11;
159 constexpr float rawYOffset = 42;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800160 constexpr float xPrecision = 0.25;
161 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700162 constexpr float xCursorPosition = 1.3;
163 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800164 constexpr nsecs_t downTime = 3;
165 constexpr size_t pointerCount = 3;
166 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000167 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700168 PointerProperties pointerProperties[pointerCount];
169 PointerCoords pointerCoords[pointerCount];
170 for (size_t i = 0; i < pointerCount; i++) {
171 pointerProperties[i].clear();
172 pointerProperties[i].id = (i + 2) % pointerCount;
173 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
174
175 pointerCoords[i].clear();
176 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
177 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
178 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
179 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
180 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
181 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
185 }
186
chaviw9eaa22c2020-07-01 16:21:27 -0700187 ui::Transform transform;
188 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700189 ui::Transform rawTransform;
190 rawTransform.set({rawXScale, 0, rawXOffset, 0, rawYScale, rawYOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800191 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600192 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700193 classification, transform, xPrecision, yPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700194 xCursorPosition, yCursorPosition, rawTransform,
195 downTime, eventTime, pointerCount, pointerProperties,
196 pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700197 ASSERT_EQ(OK, status)
198 << "publisher publishMotionEvent should return OK";
199
200 uint32_t consumeSeq;
201 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000202 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700203 ASSERT_EQ(OK, status)
204 << "consumer consume should return OK";
205
Yi Kong5bed83b2018-07-17 12:53:47 -0700206 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700207 << "consumer should have returned non-NULL event";
208 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
209 << "consumer should have returned a motion event";
210
211 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
212 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800213 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700214 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
215 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800216 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600217 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700218 EXPECT_EQ(action, motionEvent->getAction());
219 EXPECT_EQ(flags, motionEvent->getFlags());
220 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
221 EXPECT_EQ(metaState, motionEvent->getMetaState());
222 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800223 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700224 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600225 EXPECT_EQ(xOffset, motionEvent->getXOffset());
226 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700227 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
228 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700229 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
230 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800231 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
232 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700233 EXPECT_EQ(rawTransform, motionEvent->getRawTransform());
Jeff Brown5912f952013-07-01 19:10:31 -0700234 EXPECT_EQ(downTime, motionEvent->getDownTime());
235 EXPECT_EQ(eventTime, motionEvent->getEventTime());
236 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
237 EXPECT_EQ(0U, motionEvent->getHistorySize());
238
239 for (size_t i = 0; i < pointerCount; i++) {
240 SCOPED_TRACE(i);
241 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
242 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
243
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700244 const auto& pc = pointerCoords[i];
245 EXPECT_EQ(pc.getX() * rawXScale + rawXOffset, motionEvent->getRawX(i));
246 EXPECT_EQ(pc.getY() * rawYScale + rawYOffset, motionEvent->getRawY(i));
247 EXPECT_EQ(pc.getX() * xScale + xOffset, motionEvent->getX(i));
248 EXPECT_EQ(pc.getY() * yScale + yOffset, motionEvent->getY(i));
249 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i));
250 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i));
251 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i));
252 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i));
253 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i));
254 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i));
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700255
256 // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is
257 // "up", and the positive y direction is "down".
258 const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
259 const float x = sinf(unscaledOrientation) * xScale;
260 const float y = -cosf(unscaledOrientation) * yScale;
261 EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700262 }
263
264 status = mConsumer->sendFinishedSignal(seq, false);
265 ASSERT_EQ(OK, status)
266 << "consumer sendFinishedSignal should return OK";
267
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000268 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
269 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
270 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
271 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
272 ASSERT_EQ(seq, finish.seq)
273 << "receiveConsumerResponse should have returned the original sequence number";
274 ASSERT_FALSE(finish.handled)
275 << "receiveConsumerResponse should have set handled to consumer's reply";
276 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000277 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700278}
279
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800280void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
281 status_t status;
282
283 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800284 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800285 constexpr bool hasFocus = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000286 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800287
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700288 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus);
arthurhung7632c332020-12-30 16:58:01 +0800289 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800290
291 uint32_t consumeSeq;
292 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000293 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800294 ASSERT_EQ(OK, status) << "consumer consume should return OK";
295
296 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
297 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
298 << "consumer should have returned a focus event";
299
300 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
301 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800302 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800303 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800304
305 status = mConsumer->sendFinishedSignal(seq, true);
306 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
307
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000308 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
309 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
310 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
311 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000312
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000313 ASSERT_EQ(seq, finish.seq)
314 << "receiveConsumerResponse should have returned the original sequence number";
315 ASSERT_TRUE(finish.handled)
316 << "receiveConsumerResponse should have set handled to consumer's reply";
317 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000318 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800319}
320
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800321void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
322 status_t status;
323
324 constexpr uint32_t seq = 42;
325 int32_t eventId = InputEvent::nextId();
326 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000327 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800328
329 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800330 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800331
332 uint32_t consumeSeq;
333 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000334 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800335 ASSERT_EQ(OK, status) << "consumer consume should return OK";
336
337 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
338 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
339 << "consumer should have returned a capture event";
340
341 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
342 EXPECT_EQ(seq, consumeSeq);
343 EXPECT_EQ(eventId, captureEvent->getId());
344 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
345
346 status = mConsumer->sendFinishedSignal(seq, true);
347 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
348
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000349 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
350 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
351 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
352 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
353 ASSERT_EQ(seq, finish.seq)
354 << "receiveConsumerResponse should have returned the original sequence number";
355 ASSERT_TRUE(finish.handled)
356 << "receiveConsumerResponse should have set handled to consumer's reply";
357 ASSERT_GE(finish.consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000358 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800359}
360
arthurhung7632c332020-12-30 16:58:01 +0800361void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
362 status_t status;
363
364 constexpr uint32_t seq = 15;
365 int32_t eventId = InputEvent::nextId();
366 constexpr bool isExiting = false;
367 constexpr float x = 10;
368 constexpr float y = 15;
369 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
370
371 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
372 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
373
374 uint32_t consumeSeq;
375 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000376 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
arthurhung7632c332020-12-30 16:58:01 +0800377 ASSERT_EQ(OK, status) << "consumer consume should return OK";
378
379 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
380 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
381 << "consumer should have returned a drag event";
382
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000383 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800384 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000385 EXPECT_EQ(eventId, dragEvent.getId());
386 EXPECT_EQ(isExiting, dragEvent.isExiting());
387 EXPECT_EQ(x, dragEvent.getX());
388 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800389
390 status = mConsumer->sendFinishedSignal(seq, true);
391 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
392
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000393 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
394 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
395 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
396 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
397 ASSERT_EQ(seq, finish.seq)
398 << "receiveConsumerResponse should have returned the original sequence number";
399 ASSERT_TRUE(finish.handled)
400 << "receiveConsumerResponse should have set handled to consumer's reply";
401 ASSERT_GE(finish.consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800402 << "finished signal's consume time should be greater than publish time";
403}
404
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700405void InputPublisherAndConsumerTest::PublishAndConsumeTouchModeEvent() {
406 status_t status;
407
408 constexpr uint32_t seq = 15;
409 int32_t eventId = InputEvent::nextId();
410 constexpr bool touchModeEnabled = true;
411 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
412
413 status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled);
414 ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK";
415
416 uint32_t consumeSeq;
417 InputEvent* event;
Harry Cutts82c791c2023-03-10 17:15:07 +0000418 status = mConsumer->consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, &event);
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700419 ASSERT_EQ(OK, status) << "consumer consume should return OK";
420
421 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
422 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
423 << "consumer should have returned a touch mode event";
424
425 const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
426 EXPECT_EQ(seq, consumeSeq);
427 EXPECT_EQ(eventId, touchModeEvent.getId());
428 EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode());
429
430 status = mConsumer->sendFinishedSignal(seq, true);
431 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
432
433 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
434 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
435 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
436 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
437 ASSERT_EQ(seq, finish.seq)
438 << "receiveConsumerResponse should have returned the original sequence number";
439 ASSERT_TRUE(finish.handled)
440 << "receiveConsumerResponse should have set handled to consumer's reply";
441 ASSERT_GE(finish.consumeTime, publishTime)
442 << "finished signal's consume time should be greater than publish time";
443}
444
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000445TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
446 const int32_t inputEventId = 20;
447 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
448 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
449 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
450 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
451 ASSERT_EQ(OK, status);
452
453 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
454 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
455 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
456 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
457 ASSERT_EQ(inputEventId, timeline.inputEventId);
458 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
459}
460
Jeff Brown5912f952013-07-01 19:10:31 -0700461TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
462 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
463}
464
465TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
466 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
467}
468
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800469TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
470 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
471}
472
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800473TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
474 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
475}
476
arthurhung7632c332020-12-30 16:58:01 +0800477TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
478 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
479}
480
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700481TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) {
482 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
483}
484
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800485TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700486 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800487 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700488 PointerProperties pointerProperties[pointerCount];
489 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800490 for (size_t i = 0; i < pointerCount; i++) {
491 pointerProperties[i].clear();
492 pointerCoords[i].clear();
493 }
Jeff Brown5912f952013-07-01 19:10:31 -0700494
chaviw9eaa22c2020-07-01 16:21:27 -0700495 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700496 status =
497 mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
498 0, 0, 0, MotionClassification::NONE, identityTransform,
499 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
500 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
501 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700502 ASSERT_EQ(BAD_VALUE, status)
503 << "publisher publishMotionEvent should return BAD_VALUE";
504}
505
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800506TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
507 status_t status;
508 const size_t pointerCount = 0;
509 PointerProperties pointerProperties[pointerCount];
510 PointerCoords pointerCoords[pointerCount];
511
chaviw9eaa22c2020-07-01 16:21:27 -0700512 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700513 status =
514 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
515 0, 0, 0, MotionClassification::NONE, identityTransform,
516 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
517 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
518 0, 0, pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800519 ASSERT_EQ(BAD_VALUE, status)
520 << "publisher publishMotionEvent should return BAD_VALUE";
521}
522
523TEST_F(InputPublisherAndConsumerTest,
524 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700525 status_t status;
526 const size_t pointerCount = MAX_POINTERS + 1;
527 PointerProperties pointerProperties[pointerCount];
528 PointerCoords pointerCoords[pointerCount];
529 for (size_t i = 0; i < pointerCount; i++) {
530 pointerProperties[i].clear();
531 pointerCoords[i].clear();
532 }
533
chaviw9eaa22c2020-07-01 16:21:27 -0700534 ui::Transform identityTransform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700535 status =
536 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
537 0, 0, 0, MotionClassification::NONE, identityTransform,
538 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
539 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
540 0, 0, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700541 ASSERT_EQ(BAD_VALUE, status)
542 << "publisher publishMotionEvent should return BAD_VALUE";
543}
544
545TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
546 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
547 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
548 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800549 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700550 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
551 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800552 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800553 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800554 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
555 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700556 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700557}
558
559} // namespace android