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