blob: fc317156681e48717fe86d654be823cb6aae86f0 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "TestHelpers.h"
18
19#include <unistd.h>
20#include <sys/mman.h>
21#include <time.h>
22
chaviw09c8d2d2020-08-24 15:48:26 -070023#include <attestation/HmacKeyManager.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024#include <cutils/ashmem.h>
25#include <gtest/gtest.h>
26#include <input/InputTransport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <utils/StopWatch.h>
chaviw09c8d2d2020-08-24 15:48:26 -070028#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +000030using android::base::Result;
31
Jeff Brown5912f952013-07-01 19:10:31 -070032namespace android {
33
34class InputPublisherAndConsumerTest : public testing::Test {
35protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050036 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
37 std::unique_ptr<InputPublisher> mPublisher;
38 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070039 PreallocatedInputEventFactory mEventFactory;
40
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050041 void SetUp() override {
42 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080043 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070044 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080045 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050046 mServerChannel = std::move(serverChannel);
47 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070048
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050049 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
50 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070051 }
52
53 void PublishAndConsumeKeyEvent();
54 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080055 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080056 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080057 void PublishAndConsumeDragEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070058};
59
60TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050061 ASSERT_NE(nullptr, mPublisher->getChannel());
62 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050063 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
64 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050065 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
66 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070067}
68
69void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
70 status_t status;
71
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010072 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080073 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010074 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060075 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010076 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060077 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
78 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
79 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010080 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
81 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
82 constexpr int32_t keyCode = AKEYCODE_ENTER;
83 constexpr int32_t scanCode = 13;
84 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
85 constexpr int32_t repeatCount = 1;
86 constexpr nsecs_t downTime = 3;
87 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100088 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070089
Garfield Tanff1f1bb2020-01-28 13:24:04 -080090 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
91 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060092 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070093 ASSERT_EQ(OK, status)
94 << "publisher publishKeyEvent should return OK";
95
96 uint32_t consumeSeq;
97 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080098 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070099 ASSERT_EQ(OK, status)
100 << "consumer consume should return OK";
101
Yi Kong5bed83b2018-07-17 12:53:47 -0700102 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700103 << "consumer should have returned non-NULL event";
104 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
105 << "consumer should have returned a key event";
106
107 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
108 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800109 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700110 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
111 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100112 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600113 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700114 EXPECT_EQ(action, keyEvent->getAction());
115 EXPECT_EQ(flags, keyEvent->getFlags());
116 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
117 EXPECT_EQ(scanCode, keyEvent->getScanCode());
118 EXPECT_EQ(metaState, keyEvent->getMetaState());
119 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
120 EXPECT_EQ(downTime, keyEvent->getDownTime());
121 EXPECT_EQ(eventTime, keyEvent->getEventTime());
122
123 status = mConsumer->sendFinishedSignal(seq, true);
124 ASSERT_EQ(OK, status)
125 << "consumer sendFinishedSignal should return OK";
126
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000127 Result<InputPublisher::Finished> result = mPublisher->receiveFinishedSignal();
128 ASSERT_TRUE(result.ok()) << "publisher receiveFinishedSignal should return OK";
129 ASSERT_EQ(seq, result->seq)
130 << "receiveFinishedSignal should have returned the original sequence number";
131 ASSERT_TRUE(result->handled)
132 << "receiveFinishedSignal should have set handled to consumer's reply";
133 ASSERT_GE(result->consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000134 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700135}
136
137void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
138 status_t status;
139
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800140 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800141 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800142 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600143 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100144 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600145 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
146 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
147 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800148 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
149 constexpr int32_t actionButton = 0;
150 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
151 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
152 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
153 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800154 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600155 constexpr float xScale = 2;
156 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800157 constexpr float xOffset = -10;
158 constexpr float yOffset = -20;
159 constexpr float xPrecision = 0.25;
160 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700161 constexpr float xCursorPosition = 1.3;
162 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800163 constexpr nsecs_t downTime = 3;
164 constexpr size_t pointerCount = 3;
165 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000166 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700167 PointerProperties pointerProperties[pointerCount];
168 PointerCoords pointerCoords[pointerCount];
169 for (size_t i = 0; i < pointerCount; i++) {
170 pointerProperties[i].clear();
171 pointerProperties[i].id = (i + 2) % pointerCount;
172 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
173
174 pointerCoords[i].clear();
175 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
176 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
177 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
178 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
179 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
180 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
181 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
182 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
184 }
185
chaviw9eaa22c2020-07-01 16:21:27 -0700186 ui::Transform transform;
187 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800188 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600189 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700190 classification, transform, xPrecision, yPrecision,
191 xCursorPosition, yCursorPosition, downTime, eventTime,
192 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700193 ASSERT_EQ(OK, status)
194 << "publisher publishMotionEvent should return OK";
195
196 uint32_t consumeSeq;
197 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800198 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700199 ASSERT_EQ(OK, status)
200 << "consumer consume should return OK";
201
Yi Kong5bed83b2018-07-17 12:53:47 -0700202 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700203 << "consumer should have returned non-NULL event";
204 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
205 << "consumer should have returned a motion event";
206
207 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
208 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800209 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700210 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
211 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800212 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600213 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700214 EXPECT_EQ(action, motionEvent->getAction());
215 EXPECT_EQ(flags, motionEvent->getFlags());
216 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
217 EXPECT_EQ(metaState, motionEvent->getMetaState());
218 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800219 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700220 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600221 EXPECT_EQ(xOffset, motionEvent->getXOffset());
222 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700223 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
224 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700225 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
226 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800227 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
228 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Jeff Brown5912f952013-07-01 19:10:31 -0700229 EXPECT_EQ(downTime, motionEvent->getDownTime());
230 EXPECT_EQ(eventTime, motionEvent->getEventTime());
231 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
232 EXPECT_EQ(0U, motionEvent->getHistorySize());
233
234 for (size_t i = 0; i < pointerCount; i++) {
235 SCOPED_TRACE(i);
236 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
237 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
238
239 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
240 motionEvent->getRawX(i));
241 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
242 motionEvent->getRawY(i));
chaviw82357092020-01-28 13:13:06 -0800243 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
244 motionEvent->getX(i));
245 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
246 motionEvent->getY(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700247 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
248 motionEvent->getPressure(i));
249 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
250 motionEvent->getSize(i));
251 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
252 motionEvent->getTouchMajor(i));
253 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
254 motionEvent->getTouchMinor(i));
255 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
256 motionEvent->getToolMajor(i));
257 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
258 motionEvent->getToolMinor(i));
259 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
260 motionEvent->getOrientation(i));
261 }
262
263 status = mConsumer->sendFinishedSignal(seq, false);
264 ASSERT_EQ(OK, status)
265 << "consumer sendFinishedSignal should return OK";
266
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000267 Result<InputPublisher::Finished> result = mPublisher->receiveFinishedSignal();
268 ASSERT_TRUE(result.ok()) << "receiveFinishedSignal should return OK";
269 ASSERT_EQ(seq, result->seq)
270 << "receiveFinishedSignal should have returned the original sequence number";
271 ASSERT_FALSE(result->handled)
272 << "receiveFinishedSignal should have set handled to consumer's reply";
273 ASSERT_GE(result->consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000274 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700275}
276
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800277void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
278 status_t status;
279
280 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800281 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800282 constexpr bool hasFocus = true;
283 constexpr bool inTouchMode = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000284 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800285
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800286 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
arthurhung7632c332020-12-30 16:58:01 +0800287 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800288
289 uint32_t consumeSeq;
290 InputEvent* event;
291 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
292 ASSERT_EQ(OK, status) << "consumer consume should return OK";
293
294 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
295 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
296 << "consumer should have returned a focus event";
297
298 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
299 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800300 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800301 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
302 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
303
304 status = mConsumer->sendFinishedSignal(seq, true);
305 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
306
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000307 Result<InputPublisher::Finished> result = mPublisher->receiveFinishedSignal();
308
309 ASSERT_TRUE(result.ok()) << "receiveFinishedSignal should return OK";
310 ASSERT_EQ(seq, result->seq)
311 << "receiveFinishedSignal should have returned the original sequence number";
312 ASSERT_TRUE(result->handled)
313 << "receiveFinishedSignal should have set handled to consumer's reply";
314 ASSERT_GE(result->consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000315 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800316}
317
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800318void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
319 status_t status;
320
321 constexpr uint32_t seq = 42;
322 int32_t eventId = InputEvent::nextId();
323 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000324 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800325
326 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800327 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800328
329 uint32_t consumeSeq;
330 InputEvent* event;
331 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
332 ASSERT_EQ(OK, status) << "consumer consume should return OK";
333
334 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
335 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
336 << "consumer should have returned a capture event";
337
338 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
339 EXPECT_EQ(seq, consumeSeq);
340 EXPECT_EQ(eventId, captureEvent->getId());
341 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
342
343 status = mConsumer->sendFinishedSignal(seq, true);
344 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
345
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000346 android::base::Result<InputPublisher::Finished> result = mPublisher->receiveFinishedSignal();
347 ASSERT_TRUE(result.ok()) << "publisher receiveFinishedSignal should return OK";
348 ASSERT_EQ(seq, result->seq)
349 << "receiveFinishedSignal should have returned the original sequence number";
350 ASSERT_TRUE(result->handled)
351 << "receiveFinishedSignal should have set handled to consumer's reply";
352 ASSERT_GE(result->consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000353 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800354}
355
arthurhung7632c332020-12-30 16:58:01 +0800356void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
357 status_t status;
358
359 constexpr uint32_t seq = 15;
360 int32_t eventId = InputEvent::nextId();
361 constexpr bool isExiting = false;
362 constexpr float x = 10;
363 constexpr float y = 15;
364 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
365
366 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
367 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
368
369 uint32_t consumeSeq;
370 InputEvent* event;
371 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
372 ASSERT_EQ(OK, status) << "consumer consume should return OK";
373
374 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
375 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
376 << "consumer should have returned a drag event";
377
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000378 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
arthurhung7632c332020-12-30 16:58:01 +0800379 EXPECT_EQ(seq, consumeSeq);
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000380 EXPECT_EQ(eventId, dragEvent.getId());
381 EXPECT_EQ(isExiting, dragEvent.isExiting());
382 EXPECT_EQ(x, dragEvent.getX());
383 EXPECT_EQ(y, dragEvent.getY());
arthurhung7632c332020-12-30 16:58:01 +0800384
385 status = mConsumer->sendFinishedSignal(seq, true);
386 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
387
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000388 android::base::Result<InputPublisher::Finished> result = mPublisher->receiveFinishedSignal();
389 ASSERT_TRUE(result.ok()) << "publisher receiveFinishedSignal should return OK";
390 ASSERT_EQ(seq, result->seq)
arthurhung7632c332020-12-30 16:58:01 +0800391 << "publisher receiveFinishedSignal should have returned the original sequence number";
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000392 ASSERT_TRUE(result->handled)
arthurhung7632c332020-12-30 16:58:01 +0800393 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000394 ASSERT_GE(result->consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800395 << "finished signal's consume time should be greater than publish time";
396}
397
Jeff Brown5912f952013-07-01 19:10:31 -0700398TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
399 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
400}
401
402TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
403 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
404}
405
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800406TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
407 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
408}
409
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800410TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
411 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
412}
413
arthurhung7632c332020-12-30 16:58:01 +0800414TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
415 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
416}
417
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800418TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700419 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800420 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700421 PointerProperties pointerProperties[pointerCount];
422 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800423 for (size_t i = 0; i < pointerCount; i++) {
424 pointerProperties[i].clear();
425 pointerCoords[i].clear();
426 }
Jeff Brown5912f952013-07-01 19:10:31 -0700427
chaviw9eaa22c2020-07-01 16:21:27 -0700428 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800429 status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700430 0, 0, 0, MotionClassification::NONE, identityTransform,
431 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600432 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
433 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700434 ASSERT_EQ(BAD_VALUE, status)
435 << "publisher publishMotionEvent should return BAD_VALUE";
436}
437
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800438TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
439 status_t status;
440 const size_t pointerCount = 0;
441 PointerProperties pointerProperties[pointerCount];
442 PointerCoords pointerCoords[pointerCount];
443
chaviw9eaa22c2020-07-01 16:21:27 -0700444 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800445 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700446 0, 0, 0, MotionClassification::NONE, identityTransform,
447 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600448 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
449 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800450 ASSERT_EQ(BAD_VALUE, status)
451 << "publisher publishMotionEvent should return BAD_VALUE";
452}
453
454TEST_F(InputPublisherAndConsumerTest,
455 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700456 status_t status;
457 const size_t pointerCount = MAX_POINTERS + 1;
458 PointerProperties pointerProperties[pointerCount];
459 PointerCoords pointerCoords[pointerCount];
460 for (size_t i = 0; i < pointerCount; i++) {
461 pointerProperties[i].clear();
462 pointerCoords[i].clear();
463 }
464
chaviw9eaa22c2020-07-01 16:21:27 -0700465 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800466 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700467 0, 0, 0, MotionClassification::NONE, identityTransform,
468 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600469 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
470 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700471 ASSERT_EQ(BAD_VALUE, status)
472 << "publisher publishMotionEvent should return BAD_VALUE";
473}
474
475TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
476 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
477 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
478 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800479 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700480 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
481 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800482 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800483 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800484 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
485 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700486}
487
488} // namespace android