blob: b5ed8d7ba7f5b23fd3373dfe2b7328737d7da2f2 [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
30namespace android {
31
32class InputPublisherAndConsumerTest : public testing::Test {
33protected:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050034 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
35 std::unique_ptr<InputPublisher> mPublisher;
36 std::unique_ptr<InputConsumer> mConsumer;
Jeff Brown5912f952013-07-01 19:10:31 -070037 PreallocatedInputEventFactory mEventFactory;
38
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050039 void SetUp() override {
40 std::unique_ptr<InputChannel> serverChannel, clientChannel;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080041 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070042 serverChannel, clientChannel);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080043 ASSERT_EQ(OK, result);
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050044 mServerChannel = std::move(serverChannel);
45 mClientChannel = std::move(clientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070046
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050047 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
48 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -070049 }
50
51 void PublishAndConsumeKeyEvent();
52 void PublishAndConsumeMotionEvent();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080053 void PublishAndConsumeFocusEvent();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080054 void PublishAndConsumeCaptureEvent();
arthurhung7632c332020-12-30 16:58:01 +080055 void PublishAndConsumeDragEvent();
Jeff Brown5912f952013-07-01 19:10:31 -070056};
57
58TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050059 ASSERT_NE(nullptr, mPublisher->getChannel());
60 ASSERT_NE(nullptr, mConsumer->getChannel());
Siarhei Vishniakoud2588272020-07-10 11:15:40 -050061 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
62 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -050063 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
64 mConsumer->getChannel()->getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -070065}
66
67void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
68 status_t status;
69
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010070 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080071 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010072 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060073 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010074 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060075 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
76 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
77 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010078 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
79 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
80 constexpr int32_t keyCode = AKEYCODE_ENTER;
81 constexpr int32_t scanCode = 13;
82 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
83 constexpr int32_t repeatCount = 1;
84 constexpr nsecs_t downTime = 3;
85 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100086 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -070087
Garfield Tanff1f1bb2020-01-28 13:24:04 -080088 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
89 flags, keyCode, scanCode, metaState, repeatCount, downTime,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060090 eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -070091 ASSERT_EQ(OK, status)
92 << "publisher publishKeyEvent should return OK";
93
94 uint32_t consumeSeq;
95 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080096 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070097 ASSERT_EQ(OK, status)
98 << "consumer consume should return OK";
99
Yi Kong5bed83b2018-07-17 12:53:47 -0700100 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700101 << "consumer should have returned non-NULL event";
102 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
103 << "consumer should have returned a key event";
104
105 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
106 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800107 EXPECT_EQ(eventId, keyEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700108 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
109 EXPECT_EQ(source, keyEvent->getSource());
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100110 EXPECT_EQ(displayId, keyEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600111 EXPECT_EQ(hmac, keyEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700112 EXPECT_EQ(action, keyEvent->getAction());
113 EXPECT_EQ(flags, keyEvent->getFlags());
114 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
115 EXPECT_EQ(scanCode, keyEvent->getScanCode());
116 EXPECT_EQ(metaState, keyEvent->getMetaState());
117 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
118 EXPECT_EQ(downTime, keyEvent->getDownTime());
119 EXPECT_EQ(eventTime, keyEvent->getEventTime());
120
121 status = mConsumer->sendFinishedSignal(seq, true);
122 ASSERT_EQ(OK, status)
123 << "consumer sendFinishedSignal should return OK";
124
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000125 uint32_t finishedSeq = 0;
126 bool handled = false;
127 nsecs_t consumeTime;
128 status = mPublisher->receiveFinishedSignal(
129 [&finishedSeq, &handled, &consumeTime](uint32_t inSeq, bool inHandled,
130 nsecs_t inConsumeTime) -> void {
131 finishedSeq = inSeq;
132 handled = inHandled;
133 consumeTime = inConsumeTime;
134 });
135 ASSERT_EQ(OK, status)
136 << "publisher receiveFinishedSignal should return OK";
137 ASSERT_EQ(seq, finishedSeq)
138 << "publisher receiveFinishedSignal should have returned the original sequence number";
139 ASSERT_TRUE(handled)
140 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
141 ASSERT_GE(consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000142 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700143}
144
145void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
146 status_t status;
147
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800148 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800149 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800150 constexpr int32_t deviceId = 1;
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600151 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100152 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600153 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
154 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
155 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800156 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
157 constexpr int32_t actionButton = 0;
158 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
159 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
160 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
161 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800162 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600163 constexpr float xScale = 2;
164 constexpr float yScale = 3;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800165 constexpr float xOffset = -10;
166 constexpr float yOffset = -20;
167 constexpr float xPrecision = 0.25;
168 constexpr float yPrecision = 0.5;
Garfield Tan00f511d2019-06-12 16:55:40 -0700169 constexpr float xCursorPosition = 1.3;
170 constexpr float yCursorPosition = 50.6;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800171 constexpr nsecs_t downTime = 3;
172 constexpr size_t pointerCount = 3;
173 constexpr nsecs_t eventTime = 4;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000174 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown5912f952013-07-01 19:10:31 -0700175 PointerProperties pointerProperties[pointerCount];
176 PointerCoords pointerCoords[pointerCount];
177 for (size_t i = 0; i < pointerCount; i++) {
178 pointerProperties[i].clear();
179 pointerProperties[i].id = (i + 2) % pointerCount;
180 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
181
182 pointerCoords[i].clear();
183 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
184 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
185 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
186 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
187 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
188 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
189 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
190 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
191 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
192 }
193
chaviw9eaa22c2020-07-01 16:21:27 -0700194 ui::Transform transform;
195 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800196 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 actionButton, flags, edgeFlags, metaState, buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700198 classification, transform, xPrecision, yPrecision,
199 xCursorPosition, yCursorPosition, downTime, eventTime,
200 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700201 ASSERT_EQ(OK, status)
202 << "publisher publishMotionEvent should return OK";
203
204 uint32_t consumeSeq;
205 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800206 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700207 ASSERT_EQ(OK, status)
208 << "consumer consume should return OK";
209
Yi Kong5bed83b2018-07-17 12:53:47 -0700210 ASSERT_TRUE(event != nullptr)
Jeff Brown5912f952013-07-01 19:10:31 -0700211 << "consumer should have returned non-NULL event";
212 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
213 << "consumer should have returned a motion event";
214
215 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
216 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800217 EXPECT_EQ(eventId, motionEvent->getId());
Jeff Brown5912f952013-07-01 19:10:31 -0700218 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
219 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800220 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600221 EXPECT_EQ(hmac, motionEvent->getHmac());
Jeff Brown5912f952013-07-01 19:10:31 -0700222 EXPECT_EQ(action, motionEvent->getAction());
223 EXPECT_EQ(flags, motionEvent->getFlags());
224 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
225 EXPECT_EQ(metaState, motionEvent->getMetaState());
226 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800227 EXPECT_EQ(classification, motionEvent->getClassification());
chaviw9eaa22c2020-07-01 16:21:27 -0700228 EXPECT_EQ(transform, motionEvent->getTransform());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600229 EXPECT_EQ(xOffset, motionEvent->getXOffset());
230 EXPECT_EQ(yOffset, motionEvent->getYOffset());
Jeff Brown5912f952013-07-01 19:10:31 -0700231 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
232 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
Garfield Tan00f511d2019-06-12 16:55:40 -0700233 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
234 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
chaviw82357092020-01-28 13:13:06 -0800235 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
236 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
Jeff Brown5912f952013-07-01 19:10:31 -0700237 EXPECT_EQ(downTime, motionEvent->getDownTime());
238 EXPECT_EQ(eventTime, motionEvent->getEventTime());
239 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
240 EXPECT_EQ(0U, motionEvent->getHistorySize());
241
242 for (size_t i = 0; i < pointerCount; i++) {
243 SCOPED_TRACE(i);
244 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
245 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
246
247 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
248 motionEvent->getRawX(i));
249 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
250 motionEvent->getRawY(i));
chaviw82357092020-01-28 13:13:06 -0800251 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
252 motionEvent->getX(i));
253 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
254 motionEvent->getY(i));
Jeff Brown5912f952013-07-01 19:10:31 -0700255 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
256 motionEvent->getPressure(i));
257 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
258 motionEvent->getSize(i));
259 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
260 motionEvent->getTouchMajor(i));
261 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
262 motionEvent->getTouchMinor(i));
263 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
264 motionEvent->getToolMajor(i));
265 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
266 motionEvent->getToolMinor(i));
267 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
268 motionEvent->getOrientation(i));
269 }
270
271 status = mConsumer->sendFinishedSignal(seq, false);
272 ASSERT_EQ(OK, status)
273 << "consumer sendFinishedSignal should return OK";
274
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000275 uint32_t finishedSeq = 0;
276 bool handled = true;
277 nsecs_t consumeTime;
278 status = mPublisher->receiveFinishedSignal(
279 [&finishedSeq, &handled, &consumeTime](uint32_t inSeq, bool inHandled,
280 nsecs_t inConsumeTime) -> void {
281 finishedSeq = inSeq;
282 handled = inHandled;
283 consumeTime = inConsumeTime;
284 });
285 ASSERT_EQ(OK, status)
286 << "publisher receiveFinishedSignal should return OK";
287 ASSERT_EQ(seq, finishedSeq)
288 << "publisher receiveFinishedSignal should have returned the original sequence number";
289 ASSERT_FALSE(handled)
290 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
291 ASSERT_GE(consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000292 << "finished signal's consume time should be greater than publish time";
Jeff Brown5912f952013-07-01 19:10:31 -0700293}
294
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800295void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
296 status_t status;
297
298 constexpr uint32_t seq = 15;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800299 int32_t eventId = InputEvent::nextId();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800300 constexpr bool hasFocus = true;
301 constexpr bool inTouchMode = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000302 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800303
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800304 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
arthurhung7632c332020-12-30 16:58:01 +0800305 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800306
307 uint32_t consumeSeq;
308 InputEvent* event;
309 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
310 ASSERT_EQ(OK, status) << "consumer consume should return OK";
311
312 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
313 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
314 << "consumer should have returned a focus event";
315
316 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
317 EXPECT_EQ(seq, consumeSeq);
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800318 EXPECT_EQ(eventId, focusEvent->getId());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800319 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
320 EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
321
322 status = mConsumer->sendFinishedSignal(seq, true);
323 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
324
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000325 uint32_t finishedSeq = 0;
326 bool handled = false;
327 nsecs_t consumeTime;
328 status = mPublisher->receiveFinishedSignal(
329 [&finishedSeq, &handled, &consumeTime](uint32_t inSeq, bool inHandled,
330 nsecs_t inConsumeTime) -> void {
331 finishedSeq = inSeq;
332 handled = inHandled;
333 consumeTime = inConsumeTime;
334 });
335 ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK";
336 ASSERT_EQ(seq, finishedSeq)
337 << "publisher receiveFinishedSignal should have returned the original sequence number";
338 ASSERT_TRUE(handled)
339 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
340 ASSERT_GE(consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000341 << "finished signal's consume time should be greater than publish time";
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800342}
343
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800344void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
345 status_t status;
346
347 constexpr uint32_t seq = 42;
348 int32_t eventId = InputEvent::nextId();
349 constexpr bool captureEnabled = true;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000350 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800351
352 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
arthurhung7632c332020-12-30 16:58:01 +0800353 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800354
355 uint32_t consumeSeq;
356 InputEvent* event;
357 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
358 ASSERT_EQ(OK, status) << "consumer consume should return OK";
359
360 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
361 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
362 << "consumer should have returned a capture event";
363
364 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
365 EXPECT_EQ(seq, consumeSeq);
366 EXPECT_EQ(eventId, captureEvent->getId());
367 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
368
369 status = mConsumer->sendFinishedSignal(seq, true);
370 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
371
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000372 uint32_t finishedSeq = 0;
373 bool handled = false;
374 nsecs_t consumeTime;
375 status = mPublisher->receiveFinishedSignal(
376 [&finishedSeq, &handled, &consumeTime](uint32_t inSeq, bool inHandled,
377 nsecs_t inConsumeTime) -> void {
378 finishedSeq = inSeq;
379 handled = inHandled;
380 consumeTime = inConsumeTime;
381 });
382 ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK";
383 ASSERT_EQ(seq, finishedSeq)
384 << "publisher receiveFinishedSignal should have returned the original sequence number";
385 ASSERT_TRUE(handled)
386 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
387 ASSERT_GE(consumeTime, publishTime)
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000388 << "finished signal's consume time should be greater than publish time";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800389}
390
arthurhung7632c332020-12-30 16:58:01 +0800391void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
392 status_t status;
393
394 constexpr uint32_t seq = 15;
395 int32_t eventId = InputEvent::nextId();
396 constexpr bool isExiting = false;
397 constexpr float x = 10;
398 constexpr float y = 15;
399 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
400
401 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
402 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
403
404 uint32_t consumeSeq;
405 InputEvent* event;
406 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
407 ASSERT_EQ(OK, status) << "consumer consume should return OK";
408
409 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
410 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
411 << "consumer should have returned a drag event";
412
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000413 DragEvent* dragEvent = static_cast<DragEvent*>(event);
arthurhung7632c332020-12-30 16:58:01 +0800414 EXPECT_EQ(seq, consumeSeq);
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000415 EXPECT_EQ(eventId, dragEvent->getId());
416 EXPECT_EQ(isExiting, dragEvent->isExiting());
417 EXPECT_EQ(x, dragEvent->getX());
418 EXPECT_EQ(y, dragEvent->getY());
arthurhung7632c332020-12-30 16:58:01 +0800419
420 status = mConsumer->sendFinishedSignal(seq, true);
421 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
422
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000423 uint32_t finishedSeq = 0;
424 bool handled = false;
425 nsecs_t consumeTime;
426 status = mPublisher->receiveFinishedSignal(
427 [&finishedSeq, &handled, &consumeTime](uint32_t inSeq, bool inHandled,
428 nsecs_t inConsumeTime) -> void {
429 finishedSeq = inSeq;
430 handled = inHandled;
431 consumeTime = inConsumeTime;
432 });
433 ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK";
434 ASSERT_EQ(seq, finishedSeq)
arthurhung7632c332020-12-30 16:58:01 +0800435 << "publisher receiveFinishedSignal should have returned the original sequence number";
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000436 ASSERT_TRUE(handled)
arthurhung7632c332020-12-30 16:58:01 +0800437 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Yohei Yukawa7cce0ad2021-03-12 04:18:38 +0000438 ASSERT_GE(consumeTime, publishTime)
arthurhung7632c332020-12-30 16:58:01 +0800439 << "finished signal's consume time should be greater than publish time";
440}
441
Jeff Brown5912f952013-07-01 19:10:31 -0700442TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
443 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
444}
445
446TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
447 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
448}
449
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800450TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
451 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
452}
453
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800454TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
455 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
456}
457
arthurhung7632c332020-12-30 16:58:01 +0800458TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
459 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
460}
461
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800462TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700463 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800464 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700465 PointerProperties pointerProperties[pointerCount];
466 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800467 for (size_t i = 0; i < pointerCount; i++) {
468 pointerProperties[i].clear();
469 pointerCoords[i].clear();
470 }
Jeff Brown5912f952013-07-01 19:10:31 -0700471
chaviw9eaa22c2020-07-01 16:21:27 -0700472 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800473 status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700474 0, 0, 0, MotionClassification::NONE, identityTransform,
475 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600476 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
477 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700478 ASSERT_EQ(BAD_VALUE, status)
479 << "publisher publishMotionEvent should return BAD_VALUE";
480}
481
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800482TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
483 status_t status;
484 const size_t pointerCount = 0;
485 PointerProperties pointerProperties[pointerCount];
486 PointerCoords pointerCoords[pointerCount];
487
chaviw9eaa22c2020-07-01 16:21:27 -0700488 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800489 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700490 0, 0, 0, MotionClassification::NONE, identityTransform,
491 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600492 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
493 pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800494 ASSERT_EQ(BAD_VALUE, status)
495 << "publisher publishMotionEvent should return BAD_VALUE";
496}
497
498TEST_F(InputPublisherAndConsumerTest,
499 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700500 status_t status;
501 const size_t pointerCount = MAX_POINTERS + 1;
502 PointerProperties pointerProperties[pointerCount];
503 PointerCoords pointerCoords[pointerCount];
504 for (size_t i = 0; i < pointerCount; i++) {
505 pointerProperties[i].clear();
506 pointerCoords[i].clear();
507 }
508
chaviw9eaa22c2020-07-01 16:21:27 -0700509 ui::Transform identityTransform;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800510 status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
chaviw9eaa22c2020-07-01 16:21:27 -0700511 0, 0, 0, MotionClassification::NONE, identityTransform,
512 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600513 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
514 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700515 ASSERT_EQ(BAD_VALUE, status)
516 << "publisher publishMotionEvent should return BAD_VALUE";
517}
518
519TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
520 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
521 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
522 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800523 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700524 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
525 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800526 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
arthurhung7632c332020-12-30 16:58:01 +0800527 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800528 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
529 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
Jeff Brown5912f952013-07-01 19:10:31 -0700530}
531
532} // namespace android