blob: a0ee94b63463e26be5d72a3368634e80b185a336 [file] [log] [blame]
Jeff Brownb2d44352011-02-17 13:01:34 -08001/*
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 */
Jeff Brownf4a4ec22010-06-16 01:53:36 -070016
17#include <ui/InputTransport.h>
18#include <utils/Timers.h>
19#include <utils/StopWatch.h>
20#include <gtest/gtest.h>
21#include <unistd.h>
22#include <time.h>
23#include <sys/mman.h>
24#include <cutils/ashmem.h>
25
26#include "../../utils/tests/TestHelpers.h"
27
28namespace android {
29
30class InputPublisherAndConsumerTest : public testing::Test {
31protected:
32 sp<InputChannel> serverChannel, clientChannel;
33 InputPublisher* mPublisher;
34 InputConsumer* mConsumer;
35 PreallocatedInputEventFactory mEventFactory;
36
37 virtual void SetUp() {
38 status_t result = InputChannel::openInputChannelPair(String8("channel name"),
39 serverChannel, clientChannel);
40
41 mPublisher = new InputPublisher(serverChannel);
42 mConsumer = new InputConsumer(clientChannel);
43 }
44
45 virtual void TearDown() {
46 if (mPublisher) {
47 delete mPublisher;
48 mPublisher = NULL;
49 }
50
51 if (mConsumer) {
52 delete mConsumer;
53 mConsumer = NULL;
54 }
55
56 serverChannel.clear();
57 clientChannel.clear();
58 }
59
Jeff Brownf4a4ec22010-06-16 01:53:36 -070060 void PublishAndConsumeKeyEvent();
Jeff Brown6cdee982012-02-03 20:11:27 -080061 void PublishAndConsumeMotionEvent();
Jeff Brownf4a4ec22010-06-16 01:53:36 -070062};
63
64TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
65 EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
66 EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
67}
68
Jeff Brownf4a4ec22010-06-16 01:53:36 -070069void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
70 status_t status;
71
72 const int32_t deviceId = 1;
Jeff Brown5c1ed842010-07-14 18:48:53 -070073 const int32_t source = AINPUT_SOURCE_KEYBOARD;
74 const int32_t action = AKEY_EVENT_ACTION_DOWN;
75 const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Jeff Brown8575a872010-06-30 16:10:35 -070076 const int32_t keyCode = AKEYCODE_ENTER;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070077 const int32_t scanCode = 13;
Jeff Brown5c1ed842010-07-14 18:48:53 -070078 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brownf4a4ec22010-06-16 01:53:36 -070079 const int32_t repeatCount = 1;
80 const nsecs_t downTime = 3;
81 const nsecs_t eventTime = 4;
82
Jeff Brown5c1ed842010-07-14 18:48:53 -070083 status = mPublisher->publishKeyEvent(deviceId, source, action, flags,
Jeff Brownf4a4ec22010-06-16 01:53:36 -070084 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
85 ASSERT_EQ(OK, status)
86 << "publisher publishKeyEvent should return OK";
87
Jeff Brownf4a4ec22010-06-16 01:53:36 -070088 InputEvent* event;
89 status = mConsumer->consume(& mEventFactory, & event);
90 ASSERT_EQ(OK, status)
91 << "consumer consume should return OK";
92
93 ASSERT_TRUE(event != NULL)
94 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -070095 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -070096 << "consumer should have returned a key event";
97
98 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
99 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700100 EXPECT_EQ(source, keyEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700101 EXPECT_EQ(action, keyEvent->getAction());
102 EXPECT_EQ(flags, keyEvent->getFlags());
103 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
104 EXPECT_EQ(scanCode, keyEvent->getScanCode());
105 EXPECT_EQ(metaState, keyEvent->getMetaState());
106 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
107 EXPECT_EQ(downTime, keyEvent->getDownTime());
108 EXPECT_EQ(eventTime, keyEvent->getEventTime());
109
Jeff Brown81499912010-11-05 15:02:16 -0700110 status = mConsumer->sendFinishedSignal(true);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700111 ASSERT_EQ(OK, status)
112 << "consumer sendFinishedSignal should return OK";
113
Jeff Brown81499912010-11-05 15:02:16 -0700114 bool handled = false;
Jeff Brown02d85b52010-12-06 17:13:33 -0800115 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700116 ASSERT_EQ(OK, status)
117 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700118 ASSERT_TRUE(handled)
119 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700120}
121
Jeff Brown6cdee982012-02-03 20:11:27 -0800122void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700123 status_t status;
124
125 const int32_t deviceId = 1;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700126 const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
127 const int32_t action = AMOTION_EVENT_ACTION_MOVE;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700128 const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700129 const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
130 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Browne959ed22011-05-06 18:20:01 -0700131 const int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700132 const float xOffset = -10;
133 const float yOffset = -20;
134 const float xPrecision = 0.25;
135 const float yPrecision = 0.5;
136 const nsecs_t downTime = 3;
137 const size_t pointerCount = 3;
Jeff Brown6cdee982012-02-03 20:11:27 -0800138 const nsecs_t eventTime = 4;
Jeff Browne959ed22011-05-06 18:20:01 -0700139 PointerProperties pointerProperties[pointerCount];
Jeff Brown6cdee982012-02-03 20:11:27 -0800140 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700141 for (size_t i = 0; i < pointerCount; i++) {
142 pointerProperties[i].clear();
143 pointerProperties[i].id = (i + 2) % pointerCount;
144 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700145
Jeff Brown6cdee982012-02-03 20:11:27 -0800146 pointerCoords[i].clear();
147 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
148 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
149 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
150 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
151 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
152 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
153 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
154 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
155 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700156 }
157
Jeff Brownaf30ff62010-09-01 17:01:00 -0700158 status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags,
Jeff Browne959ed22011-05-06 18:20:01 -0700159 metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision,
Jeff Brown6cdee982012-02-03 20:11:27 -0800160 downTime, eventTime, pointerCount,
161 pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700162 ASSERT_EQ(OK, status)
163 << "publisher publishMotionEvent should return OK";
164
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700165 InputEvent* event;
166 status = mConsumer->consume(& mEventFactory, & event);
167 ASSERT_EQ(OK, status)
168 << "consumer consume should return OK";
169
170 ASSERT_TRUE(event != NULL)
171 << "consumer should have returned non-NULL event";
Jeff Brown5c1ed842010-07-14 18:48:53 -0700172 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700173 << "consumer should have returned a motion event";
174
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700175 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
176 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brown5c1ed842010-07-14 18:48:53 -0700177 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700178 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brownaf30ff62010-09-01 17:01:00 -0700179 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700180 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
181 EXPECT_EQ(metaState, motionEvent->getMetaState());
Jeff Browne959ed22011-05-06 18:20:01 -0700182 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700183 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
184 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
185 EXPECT_EQ(downTime, motionEvent->getDownTime());
Jeff Brown6cdee982012-02-03 20:11:27 -0800186 EXPECT_EQ(eventTime, motionEvent->getEventTime());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700187 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
Jeff Brown6cdee982012-02-03 20:11:27 -0800188 EXPECT_EQ(0U, motionEvent->getHistorySize());
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700189
190 for (size_t i = 0; i < pointerCount; i++) {
191 SCOPED_TRACE(i);
Jeff Browne959ed22011-05-06 18:20:01 -0700192 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
193 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700194
Jeff Brown6cdee982012-02-03 20:11:27 -0800195 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brown3e341462011-02-14 17:03:18 -0800196 motionEvent->getRawX(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800197 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brown3e341462011-02-14 17:03:18 -0800198 motionEvent->getRawY(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800199 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800200 motionEvent->getX(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800201 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brown3e341462011-02-14 17:03:18 -0800202 motionEvent->getY(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800203 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brown3e341462011-02-14 17:03:18 -0800204 motionEvent->getPressure(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800205 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brown3e341462011-02-14 17:03:18 -0800206 motionEvent->getSize(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800207 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800208 motionEvent->getTouchMajor(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800209 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800210 motionEvent->getTouchMinor(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800211 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800212 motionEvent->getToolMajor(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800213 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown3e341462011-02-14 17:03:18 -0800214 motionEvent->getToolMinor(i));
Jeff Brown6cdee982012-02-03 20:11:27 -0800215 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown3e341462011-02-14 17:03:18 -0800216 motionEvent->getOrientation(i));
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700217 }
218
Jeff Brown81499912010-11-05 15:02:16 -0700219 status = mConsumer->sendFinishedSignal(false);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700220 ASSERT_EQ(OK, status)
221 << "consumer sendFinishedSignal should return OK";
222
Jeff Brown81499912010-11-05 15:02:16 -0700223 bool handled = true;
Jeff Brown02d85b52010-12-06 17:13:33 -0800224 status = mPublisher->receiveFinishedSignal(&handled);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700225 ASSERT_EQ(OK, status)
226 << "publisher receiveFinishedSignal should return OK";
Jeff Brown81499912010-11-05 15:02:16 -0700227 ASSERT_FALSE(handled)
228 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700229}
230
231TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700232 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
233}
234
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700235TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700236 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
237}
238
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700239TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
240 status_t status;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700241 const size_t pointerCount = 0;
Jeff Browne959ed22011-05-06 18:20:01 -0700242 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700243 PointerCoords pointerCoords[pointerCount];
244
Jeff Browne959ed22011-05-06 18:20:01 -0700245 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
246 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700247 ASSERT_EQ(BAD_VALUE, status)
248 << "publisher publishMotionEvent should return BAD_VALUE";
249}
250
251TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
252 status_t status;
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700253 const size_t pointerCount = MAX_POINTERS + 1;
Jeff Browne959ed22011-05-06 18:20:01 -0700254 PointerProperties pointerProperties[pointerCount];
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700255 PointerCoords pointerCoords[pointerCount];
Jeff Browne959ed22011-05-06 18:20:01 -0700256 for (size_t i = 0; i < pointerCount; i++) {
257 pointerProperties[i].clear();
258 pointerCoords[i].clear();
259 }
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700260
Jeff Browne959ed22011-05-06 18:20:01 -0700261 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262 pointerCount, pointerProperties, pointerCoords);
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700263 ASSERT_EQ(BAD_VALUE, status)
264 << "publisher publishMotionEvent should return BAD_VALUE";
265}
266
267TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700268 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
269 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
270 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
271 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
272 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
273}
274
Jeff Brownf4a4ec22010-06-16 01:53:36 -0700275} // namespace android