1 /*
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
23 #include <attestation/HmacKeyManager.h>
24 #include <cutils/ashmem.h>
25 #include <gtest/gtest.h>
26 #include <gui/constants.h>
27 #include <input/InputTransport.h>
28 #include <utils/StopWatch.h>
29 #include <utils/Timers.h>
30
31 using android::base::Result;
32
33 namespace android {
34
35 class InputPublisherAndConsumerTest : public testing::Test {
36 protected:
37 std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
38 std::unique_ptr<InputPublisher> mPublisher;
39 std::unique_ptr<InputConsumer> mConsumer;
40 PreallocatedInputEventFactory mEventFactory;
41
SetUp()42 void SetUp() override {
43 std::unique_ptr<InputChannel> serverChannel, clientChannel;
44 status_t result = InputChannel::openInputChannelPair("channel name",
45 serverChannel, clientChannel);
46 ASSERT_EQ(OK, result);
47 mServerChannel = std::move(serverChannel);
48 mClientChannel = std::move(clientChannel);
49
50 mPublisher = std::make_unique<InputPublisher>(mServerChannel);
51 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
52 }
53
54 void PublishAndConsumeKeyEvent();
55 void PublishAndConsumeMotionEvent();
56 void PublishAndConsumeFocusEvent();
57 void PublishAndConsumeCaptureEvent();
58 void PublishAndConsumeDragEvent();
59 void PublishAndConsumeTouchModeEvent();
60 };
61
TEST_F(InputPublisherAndConsumerTest,GetChannel_ReturnsTheChannel)62 TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
63 ASSERT_NE(nullptr, mPublisher->getChannel());
64 ASSERT_NE(nullptr, mConsumer->getChannel());
65 EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
66 EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
67 ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
68 mConsumer->getChannel()->getConnectionToken());
69 }
70
PublishAndConsumeKeyEvent()71 void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
72 status_t status;
73
74 constexpr uint32_t seq = 15;
75 int32_t eventId = InputEvent::nextId();
76 constexpr int32_t deviceId = 1;
77 constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
78 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
79 constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
80 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
81 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
82 constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
83 constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
84 constexpr int32_t keyCode = AKEYCODE_ENTER;
85 constexpr int32_t scanCode = 13;
86 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
87 constexpr int32_t repeatCount = 1;
88 constexpr nsecs_t downTime = 3;
89 constexpr nsecs_t eventTime = 4;
90 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
91
92 status = mPublisher->publishKeyEvent(seq, eventId, deviceId, source, displayId, hmac, action,
93 flags, keyCode, scanCode, metaState, repeatCount, downTime,
94 eventTime);
95 ASSERT_EQ(OK, status)
96 << "publisher publishKeyEvent should return OK";
97
98 uint32_t consumeSeq;
99 InputEvent* event;
100 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
101 ASSERT_EQ(OK, status)
102 << "consumer consume should return OK";
103
104 ASSERT_TRUE(event != nullptr)
105 << "consumer should have returned non-NULL event";
106 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
107 << "consumer should have returned a key event";
108
109 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
110 EXPECT_EQ(seq, consumeSeq);
111 EXPECT_EQ(eventId, keyEvent->getId());
112 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
113 EXPECT_EQ(source, keyEvent->getSource());
114 EXPECT_EQ(displayId, keyEvent->getDisplayId());
115 EXPECT_EQ(hmac, keyEvent->getHmac());
116 EXPECT_EQ(action, keyEvent->getAction());
117 EXPECT_EQ(flags, keyEvent->getFlags());
118 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
119 EXPECT_EQ(scanCode, keyEvent->getScanCode());
120 EXPECT_EQ(metaState, keyEvent->getMetaState());
121 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
122 EXPECT_EQ(downTime, keyEvent->getDownTime());
123 EXPECT_EQ(eventTime, keyEvent->getEventTime());
124
125 status = mConsumer->sendFinishedSignal(seq, true);
126 ASSERT_EQ(OK, status)
127 << "consumer sendFinishedSignal should return OK";
128
129 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
130 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
131 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
132 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
133 ASSERT_EQ(seq, finish.seq)
134 << "receiveConsumerResponse should have returned the original sequence number";
135 ASSERT_TRUE(finish.handled)
136 << "receiveConsumerResponse should have set handled to consumer's reply";
137 ASSERT_GE(finish.consumeTime, publishTime)
138 << "finished signal's consume time should be greater than publish time";
139 }
140
PublishAndConsumeMotionEvent()141 void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
142 status_t status;
143
144 constexpr uint32_t seq = 15;
145 int32_t eventId = InputEvent::nextId();
146 constexpr int32_t deviceId = 1;
147 constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
148 constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
149 constexpr std::array<uint8_t, 32> hmac = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
150 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
151 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
152 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
153 constexpr int32_t actionButton = 0;
154 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
155 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
156 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
157 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
158 constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
159 constexpr float xScale = 2;
160 constexpr float yScale = 3;
161 constexpr float xOffset = -10;
162 constexpr float yOffset = -20;
163 constexpr float rawXScale = 4;
164 constexpr float rawYScale = -5;
165 constexpr float rawXOffset = -11;
166 constexpr float rawYOffset = 42;
167 constexpr float xPrecision = 0.25;
168 constexpr float yPrecision = 0.5;
169 constexpr float xCursorPosition = 1.3;
170 constexpr float yCursorPosition = 50.6;
171 constexpr nsecs_t downTime = 3;
172 constexpr size_t pointerCount = 3;
173 constexpr nsecs_t eventTime = 4;
174 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
175 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
194 ui::Transform transform;
195 transform.set({xScale, 0, xOffset, 0, yScale, yOffset, 0, 0, 1});
196 ui::Transform rawTransform;
197 rawTransform.set({rawXScale, 0, rawXOffset, 0, rawYScale, rawYOffset, 0, 0, 1});
198 status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
199 actionButton, flags, edgeFlags, metaState, buttonState,
200 classification, transform, xPrecision, yPrecision,
201 xCursorPosition, yCursorPosition, rawTransform,
202 downTime, eventTime, pointerCount, pointerProperties,
203 pointerCoords);
204 ASSERT_EQ(OK, status)
205 << "publisher publishMotionEvent should return OK";
206
207 uint32_t consumeSeq;
208 InputEvent* event;
209 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
210 ASSERT_EQ(OK, status)
211 << "consumer consume should return OK";
212
213 ASSERT_TRUE(event != nullptr)
214 << "consumer should have returned non-NULL event";
215 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
216 << "consumer should have returned a motion event";
217
218 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
219 EXPECT_EQ(seq, consumeSeq);
220 EXPECT_EQ(eventId, motionEvent->getId());
221 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
222 EXPECT_EQ(source, motionEvent->getSource());
223 EXPECT_EQ(displayId, motionEvent->getDisplayId());
224 EXPECT_EQ(hmac, motionEvent->getHmac());
225 EXPECT_EQ(action, motionEvent->getAction());
226 EXPECT_EQ(flags, motionEvent->getFlags());
227 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
228 EXPECT_EQ(metaState, motionEvent->getMetaState());
229 EXPECT_EQ(buttonState, motionEvent->getButtonState());
230 EXPECT_EQ(classification, motionEvent->getClassification());
231 EXPECT_EQ(transform, motionEvent->getTransform());
232 EXPECT_EQ(xOffset, motionEvent->getXOffset());
233 EXPECT_EQ(yOffset, motionEvent->getYOffset());
234 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
235 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
236 EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
237 EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
238 EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
239 EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
240 EXPECT_EQ(rawTransform, motionEvent->getRawTransform());
241 EXPECT_EQ(downTime, motionEvent->getDownTime());
242 EXPECT_EQ(eventTime, motionEvent->getEventTime());
243 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
244 EXPECT_EQ(0U, motionEvent->getHistorySize());
245
246 for (size_t i = 0; i < pointerCount; i++) {
247 SCOPED_TRACE(i);
248 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
249 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
250
251 const auto& pc = pointerCoords[i];
252 EXPECT_EQ(pc.getX() * rawXScale + rawXOffset, motionEvent->getRawX(i));
253 EXPECT_EQ(pc.getY() * rawYScale + rawYOffset, motionEvent->getRawY(i));
254 EXPECT_EQ(pc.getX() * xScale + xOffset, motionEvent->getX(i));
255 EXPECT_EQ(pc.getY() * yScale + yOffset, motionEvent->getY(i));
256 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), motionEvent->getPressure(i));
257 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_SIZE), motionEvent->getSize(i));
258 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), motionEvent->getTouchMajor(i));
259 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i));
260 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i));
261 EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i));
262
263 // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is
264 // "up", and the positive y direction is "down".
265 const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
266 const float x = sinf(unscaledOrientation) * xScale;
267 const float y = -cosf(unscaledOrientation) * yScale;
268 EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i));
269 }
270
271 status = mConsumer->sendFinishedSignal(seq, false);
272 ASSERT_EQ(OK, status)
273 << "consumer sendFinishedSignal should return OK";
274
275 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
276 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
277 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
278 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
279 ASSERT_EQ(seq, finish.seq)
280 << "receiveConsumerResponse should have returned the original sequence number";
281 ASSERT_FALSE(finish.handled)
282 << "receiveConsumerResponse should have set handled to consumer's reply";
283 ASSERT_GE(finish.consumeTime, publishTime)
284 << "finished signal's consume time should be greater than publish time";
285 }
286
PublishAndConsumeFocusEvent()287 void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
288 status_t status;
289
290 constexpr uint32_t seq = 15;
291 int32_t eventId = InputEvent::nextId();
292 constexpr bool hasFocus = true;
293 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
294
295 status = mPublisher->publishFocusEvent(seq, eventId, hasFocus);
296 ASSERT_EQ(OK, status) << "publisher publishFocusEvent should return OK";
297
298 uint32_t consumeSeq;
299 InputEvent* event;
300 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
301 ASSERT_EQ(OK, status) << "consumer consume should return OK";
302
303 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
304 ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
305 << "consumer should have returned a focus event";
306
307 FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
308 EXPECT_EQ(seq, consumeSeq);
309 EXPECT_EQ(eventId, focusEvent->getId());
310 EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
311
312 status = mConsumer->sendFinishedSignal(seq, true);
313 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
314
315 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
316 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
317 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
318 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
319
320 ASSERT_EQ(seq, finish.seq)
321 << "receiveConsumerResponse should have returned the original sequence number";
322 ASSERT_TRUE(finish.handled)
323 << "receiveConsumerResponse should have set handled to consumer's reply";
324 ASSERT_GE(finish.consumeTime, publishTime)
325 << "finished signal's consume time should be greater than publish time";
326 }
327
PublishAndConsumeCaptureEvent()328 void InputPublisherAndConsumerTest::PublishAndConsumeCaptureEvent() {
329 status_t status;
330
331 constexpr uint32_t seq = 42;
332 int32_t eventId = InputEvent::nextId();
333 constexpr bool captureEnabled = true;
334 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
335
336 status = mPublisher->publishCaptureEvent(seq, eventId, captureEnabled);
337 ASSERT_EQ(OK, status) << "publisher publishCaptureEvent should return OK";
338
339 uint32_t consumeSeq;
340 InputEvent* event;
341 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
342 ASSERT_EQ(OK, status) << "consumer consume should return OK";
343
344 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
345 ASSERT_EQ(AINPUT_EVENT_TYPE_CAPTURE, event->getType())
346 << "consumer should have returned a capture event";
347
348 const CaptureEvent* captureEvent = static_cast<CaptureEvent*>(event);
349 EXPECT_EQ(seq, consumeSeq);
350 EXPECT_EQ(eventId, captureEvent->getId());
351 EXPECT_EQ(captureEnabled, captureEvent->getPointerCaptureEnabled());
352
353 status = mConsumer->sendFinishedSignal(seq, true);
354 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
355
356 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
357 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
358 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
359 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
360 ASSERT_EQ(seq, finish.seq)
361 << "receiveConsumerResponse should have returned the original sequence number";
362 ASSERT_TRUE(finish.handled)
363 << "receiveConsumerResponse should have set handled to consumer's reply";
364 ASSERT_GE(finish.consumeTime, publishTime)
365 << "finished signal's consume time should be greater than publish time";
366 }
367
PublishAndConsumeDragEvent()368 void InputPublisherAndConsumerTest::PublishAndConsumeDragEvent() {
369 status_t status;
370
371 constexpr uint32_t seq = 15;
372 int32_t eventId = InputEvent::nextId();
373 constexpr bool isExiting = false;
374 constexpr float x = 10;
375 constexpr float y = 15;
376 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
377
378 status = mPublisher->publishDragEvent(seq, eventId, x, y, isExiting);
379 ASSERT_EQ(OK, status) << "publisher publishDragEvent should return OK";
380
381 uint32_t consumeSeq;
382 InputEvent* event;
383 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
384 ASSERT_EQ(OK, status) << "consumer consume should return OK";
385
386 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
387 ASSERT_EQ(AINPUT_EVENT_TYPE_DRAG, event->getType())
388 << "consumer should have returned a drag event";
389
390 const DragEvent& dragEvent = static_cast<const DragEvent&>(*event);
391 EXPECT_EQ(seq, consumeSeq);
392 EXPECT_EQ(eventId, dragEvent.getId());
393 EXPECT_EQ(isExiting, dragEvent.isExiting());
394 EXPECT_EQ(x, dragEvent.getX());
395 EXPECT_EQ(y, dragEvent.getY());
396
397 status = mConsumer->sendFinishedSignal(seq, true);
398 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
399
400 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
401 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
402 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
403 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
404 ASSERT_EQ(seq, finish.seq)
405 << "receiveConsumerResponse should have returned the original sequence number";
406 ASSERT_TRUE(finish.handled)
407 << "receiveConsumerResponse should have set handled to consumer's reply";
408 ASSERT_GE(finish.consumeTime, publishTime)
409 << "finished signal's consume time should be greater than publish time";
410 }
411
PublishAndConsumeTouchModeEvent()412 void InputPublisherAndConsumerTest::PublishAndConsumeTouchModeEvent() {
413 status_t status;
414
415 constexpr uint32_t seq = 15;
416 int32_t eventId = InputEvent::nextId();
417 constexpr bool touchModeEnabled = true;
418 const nsecs_t publishTime = systemTime(SYSTEM_TIME_MONOTONIC);
419
420 status = mPublisher->publishTouchModeEvent(seq, eventId, touchModeEnabled);
421 ASSERT_EQ(OK, status) << "publisher publishTouchModeEvent should return OK";
422
423 uint32_t consumeSeq;
424 InputEvent* event;
425 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
426 ASSERT_EQ(OK, status) << "consumer consume should return OK";
427
428 ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
429 ASSERT_EQ(AINPUT_EVENT_TYPE_TOUCH_MODE, event->getType())
430 << "consumer should have returned a touch mode event";
431
432 const TouchModeEvent& touchModeEvent = static_cast<const TouchModeEvent&>(*event);
433 EXPECT_EQ(seq, consumeSeq);
434 EXPECT_EQ(eventId, touchModeEvent.getId());
435 EXPECT_EQ(touchModeEnabled, touchModeEvent.isInTouchMode());
436
437 status = mConsumer->sendFinishedSignal(seq, true);
438 ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
439
440 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
441 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
442 ASSERT_TRUE(std::holds_alternative<InputPublisher::Finished>(*result));
443 const InputPublisher::Finished& finish = std::get<InputPublisher::Finished>(*result);
444 ASSERT_EQ(seq, finish.seq)
445 << "receiveConsumerResponse should have returned the original sequence number";
446 ASSERT_TRUE(finish.handled)
447 << "receiveConsumerResponse should have set handled to consumer's reply";
448 ASSERT_GE(finish.consumeTime, publishTime)
449 << "finished signal's consume time should be greater than publish time";
450 }
451
TEST_F(InputPublisherAndConsumerTest,SendTimeline)452 TEST_F(InputPublisherAndConsumerTest, SendTimeline) {
453 const int32_t inputEventId = 20;
454 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
455 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 30;
456 graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 40;
457 status_t status = mConsumer->sendTimeline(inputEventId, graphicsTimeline);
458 ASSERT_EQ(OK, status);
459
460 Result<InputPublisher::ConsumerResponse> result = mPublisher->receiveConsumerResponse();
461 ASSERT_TRUE(result.ok()) << "receiveConsumerResponse should return OK";
462 ASSERT_TRUE(std::holds_alternative<InputPublisher::Timeline>(*result));
463 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(*result);
464 ASSERT_EQ(inputEventId, timeline.inputEventId);
465 ASSERT_EQ(graphicsTimeline, timeline.graphicsTimeline);
466 }
467
TEST_F(InputPublisherAndConsumerTest,PublishKeyEvent_EndToEnd)468 TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
469 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
470 }
471
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_EndToEnd)472 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
473 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
474 }
475
TEST_F(InputPublisherAndConsumerTest,PublishFocusEvent_EndToEnd)476 TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
477 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
478 }
479
TEST_F(InputPublisherAndConsumerTest,PublishCaptureEvent_EndToEnd)480 TEST_F(InputPublisherAndConsumerTest, PublishCaptureEvent_EndToEnd) {
481 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
482 }
483
TEST_F(InputPublisherAndConsumerTest,PublishDragEvent_EndToEnd)484 TEST_F(InputPublisherAndConsumerTest, PublishDragEvent_EndToEnd) {
485 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
486 }
487
TEST_F(InputPublisherAndConsumerTest,PublishTouchModeEvent_EndToEnd)488 TEST_F(InputPublisherAndConsumerTest, PublishTouchModeEvent_EndToEnd) {
489 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
490 }
491
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError)492 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
493 status_t status;
494 const size_t pointerCount = 1;
495 PointerProperties pointerProperties[pointerCount];
496 PointerCoords pointerCoords[pointerCount];
497 for (size_t i = 0; i < pointerCount; i++) {
498 pointerProperties[i].clear();
499 pointerCoords[i].clear();
500 }
501
502 ui::Transform identityTransform;
503 status =
504 mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
505 0, 0, 0, MotionClassification::NONE, identityTransform,
506 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
507 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
508 0, 0, pointerCount, pointerProperties, pointerCoords);
509 ASSERT_EQ(BAD_VALUE, status)
510 << "publisher publishMotionEvent should return BAD_VALUE";
511 }
512
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError)513 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
514 status_t status;
515 const size_t pointerCount = 0;
516 PointerProperties pointerProperties[pointerCount];
517 PointerCoords pointerCoords[pointerCount];
518
519 ui::Transform identityTransform;
520 status =
521 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
522 0, 0, 0, MotionClassification::NONE, identityTransform,
523 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
524 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
525 0, 0, pointerCount, pointerProperties, pointerCoords);
526 ASSERT_EQ(BAD_VALUE, status)
527 << "publisher publishMotionEvent should return BAD_VALUE";
528 }
529
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError)530 TEST_F(InputPublisherAndConsumerTest,
531 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
532 status_t status;
533 const size_t pointerCount = MAX_POINTERS + 1;
534 PointerProperties pointerProperties[pointerCount];
535 PointerCoords pointerCoords[pointerCount];
536 for (size_t i = 0; i < pointerCount; i++) {
537 pointerProperties[i].clear();
538 pointerCoords[i].clear();
539 }
540
541 ui::Transform identityTransform;
542 status =
543 mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
544 0, 0, 0, MotionClassification::NONE, identityTransform,
545 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
546 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform,
547 0, 0, pointerCount, pointerProperties, pointerCoords);
548 ASSERT_EQ(BAD_VALUE, status)
549 << "publisher publishMotionEvent should return BAD_VALUE";
550 }
551
TEST_F(InputPublisherAndConsumerTest,PublishMultipleEvents_EndToEnd)552 TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
553 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
554 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
555 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
556 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
557 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
558 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
559 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeCaptureEvent());
560 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeDragEvent());
561 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
562 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
563 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeTouchModeEvent());
564 }
565
566 } // namespace android
567