blob: f45774ba8c85f8a6fbc0d5ed205c8e539bce1142 [file] [log] [blame]
Jeff Brownebbd5d12011-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 Brown5c225b12010-06-16 01:53:36 -070016
Mathias Agopianb93a03f82012-02-17 15:34:57 -080017#include <androidfw/InputTransport.h>
Jeff Brown5c225b12010-06-16 01:53:36 -070018#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
Mathias Agopian08965ec2012-03-05 16:16:58 -080026#include "TestHelpers.h"
Jeff Brown5c225b12010-06-16 01:53:36 -070027
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 Brown5c225b12010-06-16 01:53:36 -070060 void PublishAndConsumeKeyEvent();
Jeff Browncbee6d62012-02-03 20:11:27 -080061 void PublishAndConsumeMotionEvent();
Jeff Brown5c225b12010-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 Brown5c225b12010-06-16 01:53:36 -070069void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
70 status_t status;
71
Jeff Brown072ec962012-02-07 14:46:57 -080072 const uint32_t seq = 15;
Jeff Brown5c225b12010-06-16 01:53:36 -070073 const int32_t deviceId = 1;
Jeff Brownc5ed5912010-07-14 18:48:53 -070074 const int32_t source = AINPUT_SOURCE_KEYBOARD;
75 const int32_t action = AKEY_EVENT_ACTION_DOWN;
76 const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM;
Jeff Brownfd035822010-06-30 16:10:35 -070077 const int32_t keyCode = AKEYCODE_ENTER;
Jeff Brown5c225b12010-06-16 01:53:36 -070078 const int32_t scanCode = 13;
Jeff Brownc5ed5912010-07-14 18:48:53 -070079 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brown5c225b12010-06-16 01:53:36 -070080 const int32_t repeatCount = 1;
81 const nsecs_t downTime = 3;
82 const nsecs_t eventTime = 4;
83
Jeff Brown072ec962012-02-07 14:46:57 -080084 status = mPublisher->publishKeyEvent(seq, deviceId, source, action, flags,
Jeff Brown5c225b12010-06-16 01:53:36 -070085 keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
86 ASSERT_EQ(OK, status)
87 << "publisher publishKeyEvent should return OK";
88
Jeff Brown072ec962012-02-07 14:46:57 -080089 uint32_t consumeSeq;
Jeff Brown5c225b12010-06-16 01:53:36 -070090 InputEvent* event;
Jeff Brown771526c2012-04-27 15:13:25 -070091 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5c225b12010-06-16 01:53:36 -070092 ASSERT_EQ(OK, status)
93 << "consumer consume should return OK";
94
95 ASSERT_TRUE(event != NULL)
96 << "consumer should have returned non-NULL event";
Jeff Brownc5ed5912010-07-14 18:48:53 -070097 ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType())
Jeff Brown5c225b12010-06-16 01:53:36 -070098 << "consumer should have returned a key event";
99
100 KeyEvent* keyEvent = static_cast<KeyEvent*>(event);
Jeff Brown072ec962012-02-07 14:46:57 -0800101 EXPECT_EQ(seq, consumeSeq);
Jeff Brown5c225b12010-06-16 01:53:36 -0700102 EXPECT_EQ(deviceId, keyEvent->getDeviceId());
Jeff Brownc5ed5912010-07-14 18:48:53 -0700103 EXPECT_EQ(source, keyEvent->getSource());
Jeff Brown5c225b12010-06-16 01:53:36 -0700104 EXPECT_EQ(action, keyEvent->getAction());
105 EXPECT_EQ(flags, keyEvent->getFlags());
106 EXPECT_EQ(keyCode, keyEvent->getKeyCode());
107 EXPECT_EQ(scanCode, keyEvent->getScanCode());
108 EXPECT_EQ(metaState, keyEvent->getMetaState());
109 EXPECT_EQ(repeatCount, keyEvent->getRepeatCount());
110 EXPECT_EQ(downTime, keyEvent->getDownTime());
111 EXPECT_EQ(eventTime, keyEvent->getEventTime());
112
Jeff Brown072ec962012-02-07 14:46:57 -0800113 status = mConsumer->sendFinishedSignal(seq, true);
Jeff Brown5c225b12010-06-16 01:53:36 -0700114 ASSERT_EQ(OK, status)
115 << "consumer sendFinishedSignal should return OK";
116
Jeff Brown072ec962012-02-07 14:46:57 -0800117 uint32_t finishedSeq = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700118 bool handled = false;
Jeff Brown072ec962012-02-07 14:46:57 -0800119 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
Jeff Brown5c225b12010-06-16 01:53:36 -0700120 ASSERT_EQ(OK, status)
121 << "publisher receiveFinishedSignal should return OK";
Jeff Brown072ec962012-02-07 14:46:57 -0800122 ASSERT_EQ(seq, finishedSeq)
123 << "publisher receiveFinishedSignal should have returned the original sequence number";
Jeff Brown3915bb82010-11-05 15:02:16 -0700124 ASSERT_TRUE(handled)
125 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brown5c225b12010-06-16 01:53:36 -0700126}
127
Jeff Browncbee6d62012-02-03 20:11:27 -0800128void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700129 status_t status;
130
Jeff Brown072ec962012-02-07 14:46:57 -0800131 const uint32_t seq = 15;
Jeff Brown5c225b12010-06-16 01:53:36 -0700132 const int32_t deviceId = 1;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700133 const int32_t source = AINPUT_SOURCE_TOUCHSCREEN;
134 const int32_t action = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown85a31762010-09-01 17:01:00 -0700135 const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700136 const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP;
137 const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700138 const int32_t buttonState = AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brown5c225b12010-06-16 01:53:36 -0700139 const float xOffset = -10;
140 const float yOffset = -20;
141 const float xPrecision = 0.25;
142 const float yPrecision = 0.5;
143 const nsecs_t downTime = 3;
144 const size_t pointerCount = 3;
Jeff Browncbee6d62012-02-03 20:11:27 -0800145 const nsecs_t eventTime = 4;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700146 PointerProperties pointerProperties[pointerCount];
Jeff Browncbee6d62012-02-03 20:11:27 -0800147 PointerCoords pointerCoords[pointerCount];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700148 for (size_t i = 0; i < pointerCount; i++) {
149 pointerProperties[i].clear();
150 pointerProperties[i].id = (i + 2) % pointerCount;
151 pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown5c225b12010-06-16 01:53:36 -0700152
Jeff Browncbee6d62012-02-03 20:11:27 -0800153 pointerCoords[i].clear();
154 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, 100 * i);
155 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, 200 * i);
156 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.5 * i);
157 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_SIZE, 0.7 * i);
158 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, 1.5 * i);
159 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, 1.7 * i);
160 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.5 * i);
161 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, 2.7 * i);
162 pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, 3.5 * i);
Jeff Brown5c225b12010-06-16 01:53:36 -0700163 }
164
Jeff Brown072ec962012-02-07 14:46:57 -0800165 status = mPublisher->publishMotionEvent(seq, deviceId, source, action, flags, edgeFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700166 metaState, buttonState, xOffset, yOffset, xPrecision, yPrecision,
Jeff Browncbee6d62012-02-03 20:11:27 -0800167 downTime, eventTime, pointerCount,
168 pointerProperties, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700169 ASSERT_EQ(OK, status)
170 << "publisher publishMotionEvent should return OK";
171
Jeff Brown072ec962012-02-07 14:46:57 -0800172 uint32_t consumeSeq;
Jeff Brown5c225b12010-06-16 01:53:36 -0700173 InputEvent* event;
Jeff Brown771526c2012-04-27 15:13:25 -0700174 status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, &event);
Jeff Brown5c225b12010-06-16 01:53:36 -0700175 ASSERT_EQ(OK, status)
176 << "consumer consume should return OK";
177
178 ASSERT_TRUE(event != NULL)
179 << "consumer should have returned non-NULL event";
Jeff Brownc5ed5912010-07-14 18:48:53 -0700180 ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType())
Jeff Brown5c225b12010-06-16 01:53:36 -0700181 << "consumer should have returned a motion event";
182
Jeff Brown5c225b12010-06-16 01:53:36 -0700183 MotionEvent* motionEvent = static_cast<MotionEvent*>(event);
Jeff Brown072ec962012-02-07 14:46:57 -0800184 EXPECT_EQ(seq, consumeSeq);
Jeff Brown5c225b12010-06-16 01:53:36 -0700185 EXPECT_EQ(deviceId, motionEvent->getDeviceId());
Jeff Brownc5ed5912010-07-14 18:48:53 -0700186 EXPECT_EQ(source, motionEvent->getSource());
Jeff Brown5c225b12010-06-16 01:53:36 -0700187 EXPECT_EQ(action, motionEvent->getAction());
Jeff Brown85a31762010-09-01 17:01:00 -0700188 EXPECT_EQ(flags, motionEvent->getFlags());
Jeff Brown5c225b12010-06-16 01:53:36 -0700189 EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags());
190 EXPECT_EQ(metaState, motionEvent->getMetaState());
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700191 EXPECT_EQ(buttonState, motionEvent->getButtonState());
Jeff Brown5c225b12010-06-16 01:53:36 -0700192 EXPECT_EQ(xPrecision, motionEvent->getXPrecision());
193 EXPECT_EQ(yPrecision, motionEvent->getYPrecision());
194 EXPECT_EQ(downTime, motionEvent->getDownTime());
Jeff Browncbee6d62012-02-03 20:11:27 -0800195 EXPECT_EQ(eventTime, motionEvent->getEventTime());
Jeff Brown5c225b12010-06-16 01:53:36 -0700196 EXPECT_EQ(pointerCount, motionEvent->getPointerCount());
Jeff Browncbee6d62012-02-03 20:11:27 -0800197 EXPECT_EQ(0U, motionEvent->getHistorySize());
Jeff Brown5c225b12010-06-16 01:53:36 -0700198
199 for (size_t i = 0; i < pointerCount; i++) {
200 SCOPED_TRACE(i);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700201 EXPECT_EQ(pointerProperties[i].id, motionEvent->getPointerId(i));
202 EXPECT_EQ(pointerProperties[i].toolType, motionEvent->getToolType(i));
Jeff Brown5c225b12010-06-16 01:53:36 -0700203
Jeff Browncbee6d62012-02-03 20:11:27 -0800204 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800205 motionEvent->getRawX(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800206 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800207 motionEvent->getRawY(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800208 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800209 motionEvent->getX(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800210 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800211 motionEvent->getY(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800212 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800213 motionEvent->getPressure(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800214 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800215 motionEvent->getSize(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800216 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800217 motionEvent->getTouchMajor(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800218 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800219 motionEvent->getTouchMinor(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800220 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800221 motionEvent->getToolMajor(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800222 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800223 motionEvent->getToolMinor(i));
Jeff Browncbee6d62012-02-03 20:11:27 -0800224 EXPECT_EQ(pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown91c69ab2011-02-14 17:03:18 -0800225 motionEvent->getOrientation(i));
Jeff Brown5c225b12010-06-16 01:53:36 -0700226 }
227
Jeff Brown072ec962012-02-07 14:46:57 -0800228 status = mConsumer->sendFinishedSignal(seq, false);
Jeff Brown5c225b12010-06-16 01:53:36 -0700229 ASSERT_EQ(OK, status)
230 << "consumer sendFinishedSignal should return OK";
231
Jeff Brown072ec962012-02-07 14:46:57 -0800232 uint32_t finishedSeq = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700233 bool handled = true;
Jeff Brown072ec962012-02-07 14:46:57 -0800234 status = mPublisher->receiveFinishedSignal(&finishedSeq, &handled);
Jeff Brown5c225b12010-06-16 01:53:36 -0700235 ASSERT_EQ(OK, status)
236 << "publisher receiveFinishedSignal should return OK";
Jeff Brown072ec962012-02-07 14:46:57 -0800237 ASSERT_EQ(seq, finishedSeq)
238 << "publisher receiveFinishedSignal should have returned the original sequence number";
Jeff Brown3915bb82010-11-05 15:02:16 -0700239 ASSERT_FALSE(handled)
240 << "publisher receiveFinishedSignal should have set handled to consumer's reply";
Jeff Brown5c225b12010-06-16 01:53:36 -0700241}
242
243TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700244 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
245}
246
Jeff Brown5c225b12010-06-16 01:53:36 -0700247TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700248 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
249}
250
Jeff Brown5c225b12010-06-16 01:53:36 -0700251TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) {
252 status_t status;
Jeff Brown5c225b12010-06-16 01:53:36 -0700253 const size_t pointerCount = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700254 PointerProperties pointerProperties[pointerCount];
Jeff Brown5c225b12010-06-16 01:53:36 -0700255 PointerCoords pointerCoords[pointerCount];
256
Jeff Brown072ec962012-02-07 14:46:57 -0800257 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700258 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700259 ASSERT_EQ(BAD_VALUE, status)
260 << "publisher publishMotionEvent should return BAD_VALUE";
261}
262
263TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) {
264 status_t status;
Jeff Brown5c225b12010-06-16 01:53:36 -0700265 const size_t pointerCount = MAX_POINTERS + 1;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700266 PointerProperties pointerProperties[pointerCount];
Jeff Brown5c225b12010-06-16 01:53:36 -0700267 PointerCoords pointerCoords[pointerCount];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700268 for (size_t i = 0; i < pointerCount; i++) {
269 pointerProperties[i].clear();
270 pointerCoords[i].clear();
271 }
Jeff Brown5c225b12010-06-16 01:53:36 -0700272
Jeff Brown072ec962012-02-07 14:46:57 -0800273 status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700274 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -0700275 ASSERT_EQ(BAD_VALUE, status)
276 << "publisher publishMotionEvent should return BAD_VALUE";
277}
278
279TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700280 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
281 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
282 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
283 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent());
284 ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent());
285}
286
Jeff Brown5c225b12010-06-16 01:53:36 -0700287} // namespace android