Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010 The Android Open Source Project |
| 3 | // |
| 4 | |
| 5 | #include <ui/InputTransport.h> |
| 6 | #include <utils/Timers.h> |
| 7 | #include <utils/StopWatch.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | #include <unistd.h> |
| 10 | #include <time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <cutils/ashmem.h> |
| 13 | |
| 14 | #include "../../utils/tests/TestHelpers.h" |
| 15 | |
| 16 | namespace android { |
| 17 | |
| 18 | class InputPublisherAndConsumerTest : public testing::Test { |
| 19 | protected: |
| 20 | sp<InputChannel> serverChannel, clientChannel; |
| 21 | InputPublisher* mPublisher; |
| 22 | InputConsumer* mConsumer; |
| 23 | PreallocatedInputEventFactory mEventFactory; |
| 24 | |
| 25 | virtual void SetUp() { |
| 26 | status_t result = InputChannel::openInputChannelPair(String8("channel name"), |
| 27 | serverChannel, clientChannel); |
| 28 | |
| 29 | mPublisher = new InputPublisher(serverChannel); |
| 30 | mConsumer = new InputConsumer(clientChannel); |
| 31 | } |
| 32 | |
| 33 | virtual void TearDown() { |
| 34 | if (mPublisher) { |
| 35 | delete mPublisher; |
| 36 | mPublisher = NULL; |
| 37 | } |
| 38 | |
| 39 | if (mConsumer) { |
| 40 | delete mConsumer; |
| 41 | mConsumer = NULL; |
| 42 | } |
| 43 | |
| 44 | serverChannel.clear(); |
| 45 | clientChannel.clear(); |
| 46 | } |
| 47 | |
| 48 | void Initialize(); |
| 49 | void PublishAndConsumeKeyEvent(); |
| 50 | void PublishAndConsumeMotionEvent( |
| 51 | size_t samplesToAppendBeforeDispatch = 0, |
| 52 | size_t samplesToAppendAfterDispatch = 0); |
| 53 | }; |
| 54 | |
| 55 | TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) { |
| 56 | EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get()); |
| 57 | EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get()); |
| 58 | } |
| 59 | |
| 60 | void InputPublisherAndConsumerTest::Initialize() { |
| 61 | status_t status; |
| 62 | |
| 63 | status = mPublisher->initialize(); |
| 64 | ASSERT_EQ(OK, status) |
| 65 | << "publisher initialize should return OK"; |
| 66 | |
| 67 | status = mConsumer->initialize(); |
| 68 | ASSERT_EQ(OK, status) |
| 69 | << "consumer initialize should return OK"; |
| 70 | } |
| 71 | |
| 72 | void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() { |
| 73 | status_t status; |
| 74 | |
| 75 | const int32_t deviceId = 1; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 76 | const int32_t source = AINPUT_SOURCE_KEYBOARD; |
| 77 | const int32_t action = AKEY_EVENT_ACTION_DOWN; |
| 78 | const int32_t flags = AKEY_EVENT_FLAG_FROM_SYSTEM; |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 79 | const int32_t keyCode = AKEYCODE_ENTER; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 80 | const int32_t scanCode = 13; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 81 | const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 82 | const int32_t repeatCount = 1; |
| 83 | const nsecs_t downTime = 3; |
| 84 | const nsecs_t eventTime = 4; |
| 85 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 86 | status = mPublisher->publishKeyEvent(deviceId, source, action, flags, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 87 | keyCode, scanCode, metaState, repeatCount, downTime, eventTime); |
| 88 | ASSERT_EQ(OK, status) |
| 89 | << "publisher publishKeyEvent should return OK"; |
| 90 | |
| 91 | status = mPublisher->sendDispatchSignal(); |
| 92 | ASSERT_EQ(OK, status) |
| 93 | << "publisher sendDispatchSignal should return OK"; |
| 94 | |
| 95 | status = mConsumer->receiveDispatchSignal(); |
| 96 | ASSERT_EQ(OK, status) |
| 97 | << "consumer receiveDispatchSignal should return OK"; |
| 98 | |
| 99 | InputEvent* event; |
| 100 | status = mConsumer->consume(& mEventFactory, & event); |
| 101 | ASSERT_EQ(OK, status) |
| 102 | << "consumer consume should return OK"; |
| 103 | |
| 104 | ASSERT_TRUE(event != NULL) |
| 105 | << "consumer should have returned non-NULL event"; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 106 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, event->getType()) |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 107 | << "consumer should have returned a key event"; |
| 108 | |
| 109 | KeyEvent* keyEvent = static_cast<KeyEvent*>(event); |
| 110 | EXPECT_EQ(deviceId, keyEvent->getDeviceId()); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 111 | EXPECT_EQ(source, keyEvent->getSource()); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 112 | EXPECT_EQ(action, keyEvent->getAction()); |
| 113 | EXPECT_EQ(flags, keyEvent->getFlags()); |
| 114 | EXPECT_EQ(keyCode, keyEvent->getKeyCode()); |
| 115 | EXPECT_EQ(scanCode, keyEvent->getScanCode()); |
| 116 | EXPECT_EQ(metaState, keyEvent->getMetaState()); |
| 117 | EXPECT_EQ(repeatCount, keyEvent->getRepeatCount()); |
| 118 | EXPECT_EQ(downTime, keyEvent->getDownTime()); |
| 119 | EXPECT_EQ(eventTime, keyEvent->getEventTime()); |
| 120 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 121 | status = mConsumer->sendFinishedSignal(true); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 122 | ASSERT_EQ(OK, status) |
| 123 | << "consumer sendFinishedSignal should return OK"; |
| 124 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 125 | bool handled = false; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 126 | status = mPublisher->receiveFinishedSignal(&handled); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 127 | ASSERT_EQ(OK, status) |
| 128 | << "publisher receiveFinishedSignal should return OK"; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 129 | ASSERT_TRUE(handled) |
| 130 | << "publisher receiveFinishedSignal should have set handled to consumer's reply"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 131 | |
| 132 | status = mPublisher->reset(); |
| 133 | ASSERT_EQ(OK, status) |
| 134 | << "publisher reset should return OK"; |
| 135 | } |
| 136 | |
| 137 | void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent( |
| 138 | size_t samplesToAppendBeforeDispatch, size_t samplesToAppendAfterDispatch) { |
| 139 | status_t status; |
| 140 | |
| 141 | const int32_t deviceId = 1; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 142 | const int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 143 | const int32_t action = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 144 | const int32_t flags = AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 145 | const int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_TOP; |
| 146 | const int32_t metaState = AMETA_ALT_LEFT_ON | AMETA_ALT_ON; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 147 | const float xOffset = -10; |
| 148 | const float yOffset = -20; |
| 149 | const float xPrecision = 0.25; |
| 150 | const float yPrecision = 0.5; |
| 151 | const nsecs_t downTime = 3; |
| 152 | const size_t pointerCount = 3; |
| 153 | const int32_t pointerIds[pointerCount] = { 2, 0, 1 }; |
| 154 | |
| 155 | Vector<nsecs_t> sampleEventTimes; |
| 156 | Vector<PointerCoords> samplePointerCoords; |
| 157 | |
| 158 | for (size_t i = 0; i <= samplesToAppendAfterDispatch + samplesToAppendBeforeDispatch; i++) { |
| 159 | sampleEventTimes.push(i + 10); |
| 160 | for (size_t j = 0; j < pointerCount; j++) { |
| 161 | samplePointerCoords.push(); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 162 | PointerCoords& pc = samplePointerCoords.editTop(); |
| 163 | pc.clear(); |
| 164 | pc.setAxisValue(AINPUT_MOTION_AXIS_X, 100 * i + j); |
| 165 | pc.setAxisValue(AINPUT_MOTION_AXIS_Y, 200 * i + j); |
| 166 | pc.setAxisValue(AINPUT_MOTION_AXIS_PRESSURE, 0.5 * i + j); |
| 167 | pc.setAxisValue(AINPUT_MOTION_AXIS_SIZE, 0.7 * i + j); |
| 168 | pc.setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR, 1.5 * i + j); |
| 169 | pc.setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR, 1.7 * i + j); |
| 170 | pc.setAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR, 2.5 * i + j); |
| 171 | pc.setAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR, 2.7 * i + j); |
| 172 | pc.setAxisValue(AINPUT_MOTION_AXIS_ORIENTATION, 3.5 * i + j); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 176 | status = mPublisher->publishMotionEvent(deviceId, source, action, flags, edgeFlags, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 177 | metaState, xOffset, yOffset, xPrecision, yPrecision, |
| 178 | downTime, sampleEventTimes[0], pointerCount, pointerIds, samplePointerCoords.array()); |
| 179 | ASSERT_EQ(OK, status) |
| 180 | << "publisher publishMotionEvent should return OK"; |
| 181 | |
| 182 | for (size_t i = 0; i < samplesToAppendBeforeDispatch; i++) { |
| 183 | size_t sampleIndex = i + 1; |
| 184 | status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex], |
| 185 | samplePointerCoords.array() + sampleIndex * pointerCount); |
| 186 | ASSERT_EQ(OK, status) |
| 187 | << "publisher appendMotionEvent should return OK"; |
| 188 | } |
| 189 | |
| 190 | status = mPublisher->sendDispatchSignal(); |
| 191 | ASSERT_EQ(OK, status) |
| 192 | << "publisher sendDispatchSignal should return OK"; |
| 193 | |
| 194 | for (size_t i = 0; i < samplesToAppendAfterDispatch; i++) { |
| 195 | size_t sampleIndex = i + 1 + samplesToAppendBeforeDispatch; |
| 196 | status = mPublisher->appendMotionSample(sampleEventTimes[sampleIndex], |
| 197 | samplePointerCoords.array() + sampleIndex * pointerCount); |
| 198 | ASSERT_EQ(OK, status) |
| 199 | << "publisher appendMotionEvent should return OK"; |
| 200 | } |
| 201 | |
| 202 | status = mConsumer->receiveDispatchSignal(); |
| 203 | ASSERT_EQ(OK, status) |
| 204 | << "consumer receiveDispatchSignal should return OK"; |
| 205 | |
| 206 | InputEvent* event; |
| 207 | status = mConsumer->consume(& mEventFactory, & event); |
| 208 | ASSERT_EQ(OK, status) |
| 209 | << "consumer consume should return OK"; |
| 210 | |
| 211 | ASSERT_TRUE(event != NULL) |
| 212 | << "consumer should have returned non-NULL event"; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 213 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 214 | << "consumer should have returned a motion event"; |
| 215 | |
| 216 | size_t lastSampleIndex = samplesToAppendBeforeDispatch + samplesToAppendAfterDispatch; |
| 217 | |
| 218 | MotionEvent* motionEvent = static_cast<MotionEvent*>(event); |
| 219 | EXPECT_EQ(deviceId, motionEvent->getDeviceId()); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 220 | EXPECT_EQ(source, motionEvent->getSource()); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 221 | EXPECT_EQ(action, motionEvent->getAction()); |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 222 | EXPECT_EQ(flags, motionEvent->getFlags()); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 223 | EXPECT_EQ(edgeFlags, motionEvent->getEdgeFlags()); |
| 224 | EXPECT_EQ(metaState, motionEvent->getMetaState()); |
| 225 | EXPECT_EQ(xPrecision, motionEvent->getXPrecision()); |
| 226 | EXPECT_EQ(yPrecision, motionEvent->getYPrecision()); |
| 227 | EXPECT_EQ(downTime, motionEvent->getDownTime()); |
| 228 | EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime()); |
| 229 | EXPECT_EQ(pointerCount, motionEvent->getPointerCount()); |
| 230 | EXPECT_EQ(lastSampleIndex, motionEvent->getHistorySize()); |
| 231 | |
| 232 | for (size_t i = 0; i < pointerCount; i++) { |
| 233 | SCOPED_TRACE(i); |
| 234 | EXPECT_EQ(pointerIds[i], motionEvent->getPointerId(i)); |
| 235 | } |
| 236 | |
| 237 | for (size_t sampleIndex = 0; sampleIndex < lastSampleIndex; sampleIndex++) { |
| 238 | SCOPED_TRACE(sampleIndex); |
| 239 | EXPECT_EQ(sampleEventTimes[sampleIndex], |
| 240 | motionEvent->getHistoricalEventTime(sampleIndex)); |
| 241 | for (size_t i = 0; i < pointerCount; i++) { |
| 242 | SCOPED_TRACE(i); |
| 243 | size_t offset = sampleIndex * pointerCount + i; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 244 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X), |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 245 | motionEvent->getHistoricalRawX(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 246 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y), |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 247 | motionEvent->getHistoricalRawY(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 248 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X) + xOffset, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 249 | motionEvent->getHistoricalX(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 250 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y) + yOffset, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 251 | motionEvent->getHistoricalY(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 252 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_PRESSURE), |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 253 | motionEvent->getHistoricalPressure(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 254 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_SIZE), |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 255 | motionEvent->getHistoricalSize(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 256 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 257 | motionEvent->getHistoricalTouchMajor(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 258 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 259 | motionEvent->getHistoricalTouchMinor(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 260 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 261 | motionEvent->getHistoricalToolMajor(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 262 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 263 | motionEvent->getHistoricalToolMinor(i, sampleIndex)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 264 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_ORIENTATION), |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 265 | motionEvent->getHistoricalOrientation(i, sampleIndex)); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | SCOPED_TRACE(lastSampleIndex); |
| 270 | EXPECT_EQ(sampleEventTimes[lastSampleIndex], motionEvent->getEventTime()); |
| 271 | for (size_t i = 0; i < pointerCount; i++) { |
| 272 | SCOPED_TRACE(i); |
| 273 | size_t offset = lastSampleIndex * pointerCount + i; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 274 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X), |
| 275 | motionEvent->getRawX(i)); |
| 276 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y), |
| 277 | motionEvent->getRawY(i)); |
| 278 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_X) + xOffset, |
| 279 | motionEvent->getX(i)); |
| 280 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_Y) + yOffset, |
| 281 | motionEvent->getY(i)); |
| 282 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_PRESSURE), |
| 283 | motionEvent->getPressure(i)); |
| 284 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_SIZE), |
| 285 | motionEvent->getSize(i)); |
| 286 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR), |
| 287 | motionEvent->getTouchMajor(i)); |
| 288 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR), |
| 289 | motionEvent->getTouchMinor(i)); |
| 290 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR), |
| 291 | motionEvent->getToolMajor(i)); |
| 292 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR), |
| 293 | motionEvent->getToolMinor(i)); |
| 294 | EXPECT_EQ(samplePointerCoords[offset].getAxisValue(AINPUT_MOTION_AXIS_ORIENTATION), |
| 295 | motionEvent->getOrientation(i)); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 298 | status = mConsumer->sendFinishedSignal(false); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 299 | ASSERT_EQ(OK, status) |
| 300 | << "consumer sendFinishedSignal should return OK"; |
| 301 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 302 | bool handled = true; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 303 | status = mPublisher->receiveFinishedSignal(&handled); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 304 | ASSERT_EQ(OK, status) |
| 305 | << "publisher receiveFinishedSignal should return OK"; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 306 | ASSERT_FALSE(handled) |
| 307 | << "publisher receiveFinishedSignal should have set handled to consumer's reply"; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 308 | |
| 309 | status = mPublisher->reset(); |
| 310 | ASSERT_EQ(OK, status) |
| 311 | << "publisher reset should return OK"; |
| 312 | } |
| 313 | |
| 314 | TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_EndToEnd) { |
| 315 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 316 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent()); |
| 317 | } |
| 318 | |
| 319 | TEST_F(InputPublisherAndConsumerTest, PublishKeyEvent_WhenNotReset_ReturnsError) { |
| 320 | status_t status; |
| 321 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 322 | |
| 323 | status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 324 | ASSERT_EQ(OK, status) |
| 325 | << "publisher publishKeyEvent should return OK first time"; |
| 326 | |
| 327 | status = mPublisher->publishKeyEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 328 | ASSERT_EQ(INVALID_OPERATION, status) |
| 329 | << "publisher publishKeyEvent should return INVALID_OPERATION because " |
| 330 | "the publisher was not reset"; |
| 331 | } |
| 332 | |
| 333 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_EndToEnd) { |
| 334 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 335 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent()); |
| 336 | } |
| 337 | |
| 338 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenNotReset_ReturnsError) { |
| 339 | status_t status; |
| 340 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 341 | |
| 342 | const size_t pointerCount = 1; |
| 343 | int32_t pointerIds[pointerCount] = { 0 }; |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame^] | 344 | PointerCoords pointerCoords[pointerCount]; |
| 345 | pointerCoords[0].clear(); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 346 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 347 | status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 348 | pointerCount, pointerIds, pointerCoords); |
| 349 | ASSERT_EQ(OK, status) |
| 350 | << "publisher publishMotionEvent should return OK"; |
| 351 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 352 | status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 353 | pointerCount, pointerIds, pointerCoords); |
| 354 | ASSERT_EQ(INVALID_OPERATION, status) |
| 355 | << "publisher publishMotionEvent should return INVALID_OPERATION because "; |
| 356 | "the publisher was not reset"; |
| 357 | } |
| 358 | |
| 359 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountLessThan1_ReturnsError) { |
| 360 | status_t status; |
| 361 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 362 | |
| 363 | const size_t pointerCount = 0; |
| 364 | int32_t pointerIds[pointerCount]; |
| 365 | PointerCoords pointerCoords[pointerCount]; |
| 366 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 367 | status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 368 | pointerCount, pointerIds, pointerCoords); |
| 369 | ASSERT_EQ(BAD_VALUE, status) |
| 370 | << "publisher publishMotionEvent should return BAD_VALUE"; |
| 371 | } |
| 372 | |
| 373 | TEST_F(InputPublisherAndConsumerTest, PublishMotionEvent_WhenPointerCountGreaterThanMax_ReturnsError) { |
| 374 | status_t status; |
| 375 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 376 | |
| 377 | const size_t pointerCount = MAX_POINTERS + 1; |
| 378 | int32_t pointerIds[pointerCount]; |
| 379 | PointerCoords pointerCoords[pointerCount]; |
| 380 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 381 | status = mPublisher->publishMotionEvent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 382 | pointerCount, pointerIds, pointerCoords); |
| 383 | ASSERT_EQ(BAD_VALUE, status) |
| 384 | << "publisher publishMotionEvent should return BAD_VALUE"; |
| 385 | } |
| 386 | |
| 387 | TEST_F(InputPublisherAndConsumerTest, PublishMultipleEvents_EndToEnd) { |
| 388 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 389 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent()); |
| 390 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent()); |
| 391 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent()); |
| 392 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent()); |
| 393 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeKeyEvent()); |
| 394 | } |
| 395 | |
| 396 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledBeforeDispatchSignal_AppendsSamples) { |
| 397 | status_t status; |
| 398 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 399 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(3, 0)); |
| 400 | } |
| 401 | |
| 402 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenCalledAfterDispatchSignalAndNotConsumed_AppendsSamples) { |
| 403 | status_t status; |
| 404 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 405 | ASSERT_NO_FATAL_FAILURE(PublishAndConsumeMotionEvent(0, 4)); |
| 406 | } |
| 407 | |
| 408 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenNoMotionEventPublished_ReturnsError) { |
| 409 | status_t status; |
| 410 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 411 | |
| 412 | PointerCoords pointerCoords[1]; |
| 413 | status = mPublisher->appendMotionSample(0, pointerCoords); |
| 414 | ASSERT_EQ(INVALID_OPERATION, status) |
| 415 | << "publisher appendMotionSample should return INVALID_OPERATION"; |
| 416 | } |
| 417 | |
| 418 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenPublishedMotionEventIsNotAMove_ReturnsError) { |
| 419 | status_t status; |
| 420 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 421 | |
| 422 | const size_t pointerCount = MAX_POINTERS; |
| 423 | int32_t pointerIds[pointerCount]; |
| 424 | PointerCoords pointerCoords[pointerCount]; |
| 425 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 426 | status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_DOWN, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 427 | 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 428 | ASSERT_EQ(OK, status); |
| 429 | |
| 430 | status = mPublisher->appendMotionSample(0, pointerCoords); |
| 431 | ASSERT_EQ(INVALID_OPERATION, status) |
| 432 | << "publisher appendMotionSample should return INVALID_OPERATION"; |
| 433 | } |
| 434 | |
| 435 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenAlreadyConsumed_ReturnsError) { |
| 436 | status_t status; |
| 437 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 438 | |
| 439 | const size_t pointerCount = MAX_POINTERS; |
| 440 | int32_t pointerIds[pointerCount]; |
| 441 | PointerCoords pointerCoords[pointerCount]; |
| 442 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 443 | status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 444 | 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 445 | ASSERT_EQ(OK, status); |
| 446 | |
| 447 | status = mPublisher->sendDispatchSignal(); |
| 448 | ASSERT_EQ(OK, status); |
| 449 | |
| 450 | status = mConsumer->receiveDispatchSignal(); |
| 451 | ASSERT_EQ(OK, status); |
| 452 | |
| 453 | InputEvent* event; |
| 454 | status = mConsumer->consume(& mEventFactory, & event); |
| 455 | ASSERT_EQ(OK, status); |
| 456 | |
| 457 | status = mPublisher->appendMotionSample(0, pointerCoords); |
| 458 | ASSERT_EQ(status_t(FAILED_TRANSACTION), status) |
| 459 | << "publisher appendMotionSample should return FAILED_TRANSACTION"; |
| 460 | } |
| 461 | |
| 462 | TEST_F(InputPublisherAndConsumerTest, AppendMotionSample_WhenBufferFull_ReturnsError) { |
| 463 | status_t status; |
| 464 | ASSERT_NO_FATAL_FAILURE(Initialize()); |
| 465 | |
| 466 | const size_t pointerCount = MAX_POINTERS; |
| 467 | int32_t pointerIds[pointerCount]; |
| 468 | PointerCoords pointerCoords[pointerCount]; |
| 469 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 470 | status = mPublisher->publishMotionEvent(0, 0, AMOTION_EVENT_ACTION_MOVE, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 471 | 0, 0, 0, 0, 0, 0, 0, 0, 0, pointerCount, pointerIds, pointerCoords); |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 472 | ASSERT_EQ(OK, status); |
| 473 | |
| 474 | for (int count = 1;; count++) { |
| 475 | ASSERT_LT(count, 100000) << "should eventually reach OOM"; |
| 476 | |
| 477 | status = mPublisher->appendMotionSample(0, pointerCoords); |
| 478 | if (status != OK) { |
| 479 | ASSERT_GT(count, 12) << "should be able to add at least a dozen samples"; |
| 480 | ASSERT_EQ(NO_MEMORY, status) |
| 481 | << "publisher appendMotionSample should return NO_MEMORY when buffer is full"; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | status = mPublisher->appendMotionSample(0, pointerCoords); |
| 487 | ASSERT_EQ(NO_MEMORY, status) |
| 488 | << "publisher appendMotionSample should return NO_MEMORY persistently until reset"; |
| 489 | } |
| 490 | |
| 491 | } // namespace android |