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 <cutils/ashmem.h>
24 #include <gtest/gtest.h>
25 #include <input/InputTransport.h>
26 #include <utils/Timers.h>
27 #include <utils/StopWatch.h>
28 
29 namespace android {
30 
31 class InputPublisherAndConsumerTest : public testing::Test {
32 protected:
33     sp<InputChannel> serverChannel, clientChannel;
34     InputPublisher* mPublisher;
35     InputConsumer* mConsumer;
36     PreallocatedInputEventFactory mEventFactory;
37 
SetUp()38     virtual void SetUp() {
39         status_t result = InputChannel::openInputChannelPair("channel name",
40                 serverChannel, clientChannel);
41         ASSERT_EQ(OK, result);
42 
43         mPublisher = new InputPublisher(serverChannel);
44         mConsumer = new InputConsumer(clientChannel);
45     }
46 
TearDown()47     virtual void TearDown() {
48         if (mPublisher) {
49             delete mPublisher;
50             mPublisher = nullptr;
51         }
52 
53         if (mConsumer) {
54             delete mConsumer;
55             mConsumer = nullptr;
56         }
57 
58         serverChannel.clear();
59         clientChannel.clear();
60     }
61 
62     void PublishAndConsumeKeyEvent();
63     void PublishAndConsumeMotionEvent();
64     void PublishAndConsumeFocusEvent();
65 };
66 
TEST_F(InputPublisherAndConsumerTest,GetChannel_ReturnsTheChannel)67 TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
68     EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
69     EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
70 }
71 
PublishAndConsumeKeyEvent()72 void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
73     status_t status;
74 
75     constexpr uint32_t seq = 15;
76     int32_t eventId = InputEvent::nextId();
77     constexpr int32_t deviceId = 1;
78     constexpr uint32_t source = AINPUT_SOURCE_KEYBOARD;
79     constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
80     constexpr std::array<uint8_t, 32> hmac = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,
81                                               20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,
82                                               9,  8,  7,  6,  5,  4,  3,  2,  1,  0};
83     constexpr int32_t action = AKEY_EVENT_ACTION_DOWN;
84     constexpr int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
85     constexpr int32_t keyCode = AKEYCODE_ENTER;
86     constexpr int32_t scanCode = 13;
87     constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
88     constexpr int32_t repeatCount = 1;
89     constexpr nsecs_t downTime = 3;
90     constexpr nsecs_t eventTime = 4;
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     uint32_t finishedSeq = 0;
130     bool handled = false;
131     status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
132     ASSERT_EQ(OK, status)
133             << "publisher receiveFinishedSignal should return OK";
134     ASSERT_EQ(seq, finishedSeq)
135             << "publisher receiveFinishedSignal should have returned the original sequence number";
136     ASSERT_TRUE(handled)
137             << "publisher receiveFinishedSignal should have set handled to consumer's reply";
138 }
139 
PublishAndConsumeMotionEvent()140 void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
141     status_t status;
142 
143     constexpr uint32_t seq = 15;
144     int32_t eventId = InputEvent::nextId();
145     constexpr int32_t deviceId = 1;
146     constexpr uint32_t source = AINPUT_SOURCE_TOUCHSCREEN;
147     constexpr int32_t displayId = ADISPLAY_ID_DEFAULT;
148     constexpr std::array<uint8_t, 32> hmac = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
149                                               11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
150                                               22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
151     constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
152     constexpr int32_t actionButton = 0;
153     constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
154     constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
155     constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
156     constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
157     constexpr MotionClassification classification = MotionClassification::AMBIGUOUS_GESTURE;
158     constexpr float xScale = 2;
159     constexpr float yScale = 3;
160     constexpr float xOffset = -10;
161     constexpr float yOffset = -20;
162     constexpr float xPrecision = 0.25;
163     constexpr float yPrecision = 0.5;
164     constexpr float xCursorPosition = 1.3;
165     constexpr float yCursorPosition = 50.6;
166     constexpr nsecs_t downTime = 3;
167     constexpr size_t pointerCount = 3;
168     constexpr nsecs_t eventTime = 4;
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 = AMOTION_EVENT_TOOL_TYPE_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     status = mPublisher->publishMotionEvent(seq, eventId, deviceId, source, displayId, hmac, action,
189                                             actionButton, flags, edgeFlags, metaState, buttonState,
190                                             classification, xScale, yScale, xOffset, yOffset,
191                                             xPrecision, yPrecision, xCursorPosition,
192                                             yCursorPosition, downTime, eventTime, pointerCount,
193                                             pointerProperties, pointerCoords);
194     ASSERT_EQ(OK, status)
195             << "publisher publishMotionEvent should return OK";
196 
197     uint32_t consumeSeq;
198     InputEvent* event;
199     status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
200     ASSERT_EQ(OK, status)
201             << "consumer consume should return OK";
202 
203     ASSERT_TRUE(event != nullptr)
204             << "consumer should have returned non-NULL event";
205     ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
206             << "consumer should have returned a motion event";
207 
208     MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
209     EXPECT_EQ(seq, consumeSeq);
210     EXPECT_EQ(eventId, motionEvent->getId());
211     EXPECT_EQ(deviceId, motionEvent->getDeviceId());
212     EXPECT_EQ(source, motionEvent->getSource());
213     EXPECT_EQ(displayId, motionEvent->getDisplayId());
214     EXPECT_EQ(hmac, motionEvent->getHmac());
215     EXPECT_EQ(action, motionEvent->getAction());
216     EXPECT_EQ(flags, motionEvent->getFlags());
217     EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
218     EXPECT_EQ(metaState, motionEvent->getMetaState());
219     EXPECT_EQ(buttonState, motionEvent->getButtonState());
220     EXPECT_EQ(classification, motionEvent->getClassification());
221     EXPECT_EQ(xScale, motionEvent->getXScale());
222     EXPECT_EQ(yScale, motionEvent->getYScale());
223     EXPECT_EQ(xOffset, motionEvent->getXOffset());
224     EXPECT_EQ(yOffset, motionEvent->getYOffset());
225     EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
226     EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
227     EXPECT_EQ(xCursorPosition, motionEvent->getRawXCursorPosition());
228     EXPECT_EQ(yCursorPosition, motionEvent->getRawYCursorPosition());
229     EXPECT_EQ(xCursorPosition * xScale + xOffset, motionEvent->getXCursorPosition());
230     EXPECT_EQ(yCursorPosition * yScale + yOffset, motionEvent->getYCursorPosition());
231     EXPECT_EQ(downTime, motionEvent->getDownTime());
232     EXPECT_EQ(eventTime, motionEvent->getEventTime());
233     EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
234     EXPECT_EQ(0U, motionEvent->getHistorySize());
235 
236     for (size_t i = 0; i < pointerCount; i++) {
237         SCOPED_TRACE(i);
238         EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
239         EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
240 
241         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
242                 motionEvent->getRawX(i));
243         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
244                 motionEvent->getRawY(i));
245         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) * xScale + xOffset,
246                   motionEvent->getX(i));
247         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) * yScale + yOffset,
248                   motionEvent->getY(i));
249         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
250                 motionEvent->getPressure(i));
251         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
252                 motionEvent->getSize(i));
253         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
254                 motionEvent->getTouchMajor(i));
255         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
256                 motionEvent->getTouchMinor(i));
257         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
258                 motionEvent->getToolMajor(i));
259         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
260                 motionEvent->getToolMinor(i));
261         EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
262                 motionEvent->getOrientation(i));
263     }
264 
265     status = mConsumer->sendFinishedSignal(seq, false);
266     ASSERT_EQ(OK, status)
267             << "consumer sendFinishedSignal should return OK";
268 
269     uint32_t finishedSeq = 0;
270     bool handled = true;
271     status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
272     ASSERT_EQ(OK, status)
273             << "publisher receiveFinishedSignal should return OK";
274     ASSERT_EQ(seq, finishedSeq)
275             << "publisher receiveFinishedSignal should have returned the original sequence number";
276     ASSERT_FALSE(handled)
277             << "publisher receiveFinishedSignal should have set handled to consumer's reply";
278 }
279 
PublishAndConsumeFocusEvent()280 void InputPublisherAndConsumerTest::PublishAndConsumeFocusEvent() {
281     status_t status;
282 
283     constexpr uint32_t seq = 15;
284     int32_t eventId = InputEvent::nextId();
285     constexpr bool hasFocus = true;
286     constexpr bool inTouchMode = true;
287 
288     status = mPublisher->publishFocusEvent(seq, eventId, hasFocus, inTouchMode);
289     ASSERT_EQ(OK, status) << "publisher publishKeyEvent should return OK";
290 
291     uint32_t consumeSeq;
292     InputEvent* event;
293     status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
294     ASSERT_EQ(OK, status) << "consumer consume should return OK";
295 
296     ASSERT_TRUE(event != nullptr) << "consumer should have returned non-NULL event";
297     ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType())
298             << "consumer should have returned a focus event";
299 
300     FocusEvent* focusEvent = static_cast<FocusEvent*>(event);
301     EXPECT_EQ(seq, consumeSeq);
302     EXPECT_EQ(eventId, focusEvent->getId());
303     EXPECT_EQ(hasFocus, focusEvent->getHasFocus());
304     EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode());
305 
306     status = mConsumer->sendFinishedSignal(seq, true);
307     ASSERT_EQ(OK, status) << "consumer sendFinishedSignal should return OK";
308 
309     uint32_t finishedSeq = 0;
310     bool handled = false;
311     status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
312     ASSERT_EQ(OK, status) << "publisher receiveFinishedSignal should return OK";
313     ASSERT_EQ(seq, finishedSeq)
314             << "publisher receiveFinishedSignal should have returned the original sequence number";
315     ASSERT_TRUE(handled)
316             << "publisher receiveFinishedSignal should have set handled to consumer's reply";
317 }
318 
TEST_F(InputPublisherAndConsumerTest,PublishKeyEvent_EndToEnd)319 TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
320     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
321 }
322 
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_EndToEnd)323 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
324     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
325 }
326 
TEST_F(InputPublisherAndConsumerTest,PublishFocusEvent_EndToEnd)327 TEST_F(InputPublisherAndConsumerTest, PublishFocusEvent_EndToEnd) {
328     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
329 }
330 
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError)331 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
332     status_t status;
333     const size_t pointerCount = 1;
334     PointerProperties pointerProperties[pointerCount];
335     PointerCoords pointerCoords[pointerCount];
336     for (size_t i = 0; i < pointerCount; i++) {
337         pointerProperties[i].clear();
338         pointerCoords[i].clear();
339     }
340 
341     status = mPublisher->publishMotionEvent(0, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
342                                             0, 0, 0, MotionClassification::NONE, 1 /* xScale */,
343                                             1 /* yScale */, 0, 0, 0, 0,
344                                             AMOTION_EVENT_INVALID_CURSOR_POSITION,
345                                             AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
346                                             pointerCount, pointerProperties, pointerCoords);
347     ASSERT_EQ(BAD_VALUE, status)
348             << "publisher publishMotionEvent should return BAD_VALUE";
349 }
350 
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError)351 TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
352     status_t status;
353     const size_t pointerCount = 0;
354     PointerProperties pointerProperties[pointerCount];
355     PointerCoords pointerCoords[pointerCount];
356 
357     status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
358                                             0, 0, 0, MotionClassification::NONE, 1 /* xScale */,
359                                             1 /* yScale */, 0, 0, 0, 0,
360                                             AMOTION_EVENT_INVALID_CURSOR_POSITION,
361                                             AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
362                                             pointerCount, pointerProperties, pointerCoords);
363     ASSERT_EQ(BAD_VALUE, status)
364             << "publisher publishMotionEvent should return BAD_VALUE";
365 }
366 
TEST_F(InputPublisherAndConsumerTest,PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError)367 TEST_F(InputPublisherAndConsumerTest,
368         PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
369     status_t status;
370     const size_t pointerCount = MAX_POINTERS + 1;
371     PointerProperties pointerProperties[pointerCount];
372     PointerCoords pointerCoords[pointerCount];
373     for (size_t i = 0; i < pointerCount; i++) {
374         pointerProperties[i].clear();
375         pointerCoords[i].clear();
376     }
377 
378     status = mPublisher->publishMotionEvent(1, InputEvent::nextId(), 0, 0, 0, INVALID_HMAC, 0, 0, 0,
379                                             0, 0, 0, MotionClassification::NONE, 1 /* xScale */,
380                                             1 /* yScale */, 0, 0, 0, 0,
381                                             AMOTION_EVENT_INVALID_CURSOR_POSITION,
382                                             AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, 0,
383                                             pointerCount, pointerProperties, pointerCoords);
384     ASSERT_EQ(BAD_VALUE, status)
385             << "publisher publishMotionEvent should return BAD_VALUE";
386 }
387 
TEST_F(InputPublisherAndConsumerTest,PublishMultipleEvents_EndToEnd)388 TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
389     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
390     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
391     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
392     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeFocusEvent());
393     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
394     ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
395 }
396 
397 } // namespace android
398