blob: 6c4faed2cc519c270b04e145682118be91e7d36d [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
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
29namespace android {
30
31class InputPublisherAndConsumerTest : public testing::Test {
32protected:
33 sp<InputChannel> serverChannel, clientChannel;
34 InputPublisher* mPublisher;
35 InputConsumer* mConsumer;
36 PreallocatedInputEventFactory mEventFactory;
37
38 virtual void SetUp() {
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080039 status_t result = InputChannel::openInputChannelPair("channel name",
Jeff Brown5912f952013-07-01 19:10:31 -070040 serverChannel, clientChannel);
41
42 mPublisher = new InputPublisher(serverChannel);
43 mConsumer = new InputConsumer(clientChannel);
44 }
45
46 virtual void TearDown() {
47 if (mPublisher) {
48 delete mPublisher;
49 mPublisher = NULL;
50 }
51
52 if (mConsumer) {
53 delete mConsumer;
54 mConsumer = NULL;
55 }
56
57 serverChannel.clear();
58 clientChannel.clear();
59 }
60
61 void PublishAndConsumeKeyEvent();
62 void PublishAndConsumeMotionEvent();
63};
64
65TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
66 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
67 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
68}
69
70void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
71 status_t status;
72
73 const uint32_t seq = 15;
74 const int32_t deviceId = 1;
75 const int32_t source = AINPUT_SOURCE_KEYBOARD;
76 const int32_t action = AKEY_EVENT_ACTION_DOWN;
77 const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
78 const int32_t keyCode = AKEYCODE_ENTER;
79 const int32_t scanCode = 13;
80 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
81 const int32_t repeatCount = 1;
82 const nsecs_t downTime = 3;
83 const nsecs_t eventTime = 4;
84
85 status = mPublisher->publishKeyEvent(seq, deviceId, source, action, flags,
86 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
87 ASSERT_EQ(OK, status)
88 << "publisher publishKeyEvent should return OK";
89
90 uint32_t consumeSeq;
91 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080092 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -070093 ASSERT_EQ(OK, status)
94 << "consumer consume should return OK";
95
96 ASSERT_TRUE(event != NULL)
97 << "consumer should have returned non-NULL event";
98 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
99 << "consumer should have returned a key event";
100
101 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
102 EXPECT_EQ(seq, consumeSeq);
103 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
104 EXPECT_EQ(source, keyEvent->getSource());
105 EXPECT_EQ(action, keyEvent->getAction());
106 EXPECT_EQ(flags, keyEvent->getFlags());
107 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
108 EXPECT_EQ(scanCode, keyEvent->getScanCode());
109 EXPECT_EQ(metaState, keyEvent->getMetaState());
110 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
111 EXPECT_EQ(downTime, keyEvent->getDownTime());
112 EXPECT_EQ(eventTime, keyEvent->getEventTime());
113
114 status = mConsumer->sendFinishedSignal(seq, true);
115 ASSERT_EQ(OK, status)
116 << "consumer sendFinishedSignal should return OK";
117
118 uint32_t finishedSeq = 0;
119 bool handled = false;
120 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
121 ASSERT_EQ(OK, status)
122 << "publisher receiveFinishedSignal should return OK";
123 ASSERT_EQ(seq, finishedSeq)
124 << "publisher receiveFinishedSignal should have returned the original sequence number";
125 ASSERT_TRUE(handled)
126 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
127}
128
129void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
130 status_t status;
131
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800132 constexpr uint32_t seq = 15;
133 constexpr int32_t deviceId = 1;
134 constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
135 constexpr int32_t displayId = 0;
136 constexpr int32_t action = AMOTION_EVENT_ACTION_MOVE;
137 constexpr int32_t actionButton = 0;
138 constexpr int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
139 constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
140 constexpr int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
141 constexpr int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
142 constexpr float xOffset = -10;
143 constexpr float yOffset = -20;
144 constexpr float xPrecision = 0.25;
145 constexpr float yPrecision = 0.5;
146 constexpr nsecs_t downTime = 3;
147 constexpr size_t pointerCount = 3;
148 constexpr nsecs_t eventTime = 4;
Jeff Brown5912f952013-07-01 19:10:31 -0700149 PointerProperties pointerProperties[pointerCount];
150 PointerCoords pointerCoords[pointerCount];
151 for (size_t i = 0; i < pointerCount; i++) {
152 pointerProperties[i].clear();
153 pointerProperties[i].id = (i + 2) % pointerCount;
154 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
155
156 pointerCoords[i].clear();
157 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
158 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
159 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
160 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
161 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
162 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
163 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
164 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
165 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
166 }
167
Tarandeep Singh58641502017-07-31 10:51:54 -0700168 status = mPublisher->publishMotionEvent(seq, deviceId, source, displayId, action, actionButton,
Michael Wrightb03f1032015-05-14 16:29:13 +0100169 flags, edgeFlags, metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision,
Jeff Brown5912f952013-07-01 19:10:31 -0700170 downTime, eventTime, pointerCount,
171 pointerProperties, pointerCoords);
172 ASSERT_EQ(OK, status)
173 << "publisher publishMotionEvent should return OK";
174
175 uint32_t consumeSeq;
176 InputEvent* event;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800177 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5912f952013-07-01 19:10:31 -0700178 ASSERT_EQ(OK, status)
179 << "consumer consume should return OK";
180
181 ASSERT_TRUE(event != NULL)
182 << "consumer should have returned non-NULL event";
183 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
184 << "consumer should have returned a motion event";
185
186 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
187 EXPECT_EQ(seq, consumeSeq);
188 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
189 EXPECT_EQ(source, motionEvent->getSource());
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800190 EXPECT_EQ(displayId, motionEvent->getDisplayId());
Jeff Brown5912f952013-07-01 19:10:31 -0700191 EXPECT_EQ(action, motionEvent->getAction());
192 EXPECT_EQ(flags, motionEvent->getFlags());
193 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
194 EXPECT_EQ(metaState, motionEvent->getMetaState());
195 EXPECT_EQ(buttonState, motionEvent->getButtonState());
196 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
197 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
198 EXPECT_EQ(downTime, motionEvent->getDownTime());
199 EXPECT_EQ(eventTime, motionEvent->getEventTime());
200 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
201 EXPECT_EQ(0U, motionEvent->getHistorySize());
202
203 for (size_t i = 0; i < pointerCount; i++) {
204 SCOPED_TRACE(i);
205 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
206 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
207
208 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
209 motionEvent->getRawX(i));
210 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
211 motionEvent->getRawY(i));
212 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
213 motionEvent->getX(i));
214 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
215 motionEvent->getY(i));
216 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
217 motionEvent->getPressure(i));
218 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
219 motionEvent->getSize(i));
220 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
221 motionEvent->getTouchMajor(i));
222 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
223 motionEvent->getTouchMinor(i));
224 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
225 motionEvent->getToolMajor(i));
226 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
227 motionEvent->getToolMinor(i));
228 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
229 motionEvent->getOrientation(i));
230 }
231
232 status = mConsumer->sendFinishedSignal(seq, false);
233 ASSERT_EQ(OK, status)
234 << "consumer sendFinishedSignal should return OK";
235
236 uint32_t finishedSeq = 0;
237 bool handled = true;
238 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
239 ASSERT_EQ(OK, status)
240 << "publisher receiveFinishedSignal should return OK";
241 ASSERT_EQ(seq, finishedSeq)
242 << "publisher receiveFinishedSignal should have returned the original sequence number";
243 ASSERT_FALSE(handled)
244 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
245}
246
247TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
248 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
249}
250
251TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
252 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
253}
254
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800255TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenSequenceNumberIsZero_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700256 status_t status;
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800257 const size_t pointerCount = 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700258 PointerProperties pointerProperties[pointerCount];
259 PointerCoords pointerCoords[pointerCount];
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800260 for (size_t i = 0; i < pointerCount; i++) {
261 pointerProperties[i].clear();
262 pointerCoords[i].clear();
263 }
Jeff Brown5912f952013-07-01 19:10:31 -0700264
Tarandeep Singh58641502017-07-31 10:51:54 -0700265 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5912f952013-07-01 19:10:31 -0700266 pointerCount, pointerProperties, pointerCoords);
267 ASSERT_EQ(BAD_VALUE, status)
268 << "publisher publishMotionEvent should return BAD_VALUE";
269}
270
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800271TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
272 status_t status;
273 const size_t pointerCount = 0;
274 PointerProperties pointerProperties[pointerCount];
275 PointerCoords pointerCoords[pointerCount];
276
277 status = mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278 pointerCount, pointerProperties, pointerCoords);
279 ASSERT_EQ(BAD_VALUE, status)
280 << "publisher publishMotionEvent should return BAD_VALUE";
281}
282
283TEST_F(InputPublisherAndConsumerTest,
284 PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
Jeff Brown5912f952013-07-01 19:10:31 -0700285 status_t status;
286 const size_t pointerCount = MAX_POINTERS + 1;
287 PointerProperties pointerProperties[pointerCount];
288 PointerCoords pointerCoords[pointerCount];
289 for (size_t i = 0; i < pointerCount; i++) {
290 pointerProperties[i].clear();
291 pointerCoords[i].clear();
292 }
293
Siarhei Vishniakoub0fffdd2017-11-10 20:16:56 -0800294 status = mPublisher->publishMotionEvent(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brown5912f952013-07-01 19:10:31 -0700295 pointerCount, pointerProperties, pointerCoords);
296 ASSERT_EQ(BAD_VALUE, status)
297 << "publisher publishMotionEvent should return BAD_VALUE";
298}
299
300TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
301 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
302 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
303 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
304 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
305 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
306}
307
308} // namespace android