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 "../InputReader.h" 18 19 #include <inttypes.h> 20 #include <utils/List.h> 21 #include <gtest/gtest.h> 22 #include <math.h> 23 24 namespace android { 25 26 // An arbitrary time value. 27 static const nsecs_t ARBITRARY_TIME = 1234; 28 29 // Arbitrary display properties. 30 static const int32_t DISPLAY_ID = 0; 31 static const int32_t DISPLAY_WIDTH = 480; 32 static const int32_t DISPLAY_HEIGHT = 800; 33 static const int32_t VIRTUAL_DISPLAY_ID = 1; 34 static const int32_t VIRTUAL_DISPLAY_WIDTH = 400; 35 static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500; 36 static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "Vr-display-unique-ID"; 37 38 // Error tolerance for floating point assertions. 39 static const float EPSILON = 0.001f; 40 41 template<typename T> 42 static inline T min(T a, T b) { 43 return a < b ? a : b; 44 } 45 46 static inline float avg(float x, float y) { 47 return (x + y) / 2; 48 } 49 50 51 // --- FakePointerController --- 52 53 class FakePointerController : public PointerControllerInterface { 54 bool mHaveBounds; 55 float mMinX, mMinY, mMaxX, mMaxY; 56 float mX, mY; 57 int32_t mButtonState; 58 59 protected: 60 virtual ~FakePointerController() { } 61 62 public: 63 FakePointerController() : 64 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0), 65 mButtonState(0) { 66 } 67 68 void setBounds(float minX, float minY, float maxX, float maxY) { 69 mHaveBounds = true; 70 mMinX = minX; 71 mMinY = minY; 72 mMaxX = maxX; 73 mMaxY = maxY; 74 } 75 76 virtual void setPosition(float x, float y) { 77 mX = x; 78 mY = y; 79 } 80 81 virtual void setButtonState(int32_t buttonState) { 82 mButtonState = buttonState; 83 } 84 85 virtual int32_t getButtonState() const { 86 return mButtonState; 87 } 88 89 virtual void getPosition(float* outX, float* outY) const { 90 *outX = mX; 91 *outY = mY; 92 } 93 94 private: 95 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const { 96 *outMinX = mMinX; 97 *outMinY = mMinY; 98 *outMaxX = mMaxX; 99 *outMaxY = mMaxY; 100 return mHaveBounds; 101 } 102 103 virtual void move(float deltaX, float deltaY) { 104 mX += deltaX; 105 if (mX < mMinX) mX = mMinX; 106 if (mX > mMaxX) mX = mMaxX; 107 mY += deltaY; 108 if (mY < mMinY) mY = mMinY; 109 if (mY > mMaxY) mY = mMaxY; 110 } 111 112 virtual void fade(Transition) { 113 } 114 115 virtual void unfade(Transition) { 116 } 117 118 virtual void setPresentation(Presentation) { 119 } 120 121 virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) { 122 } 123 124 virtual void clearSpots() { 125 } 126 }; 127 128 129 // --- FakeInputReaderPolicy --- 130 131 class FakeInputReaderPolicy : public InputReaderPolicyInterface { 132 InputReaderConfiguration mConfig; 133 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers; 134 Vector<InputDeviceInfo> mInputDevices; 135 TouchAffineTransformation transform; 136 137 protected: 138 virtual ~FakeInputReaderPolicy() { } 139 140 public: 141 FakeInputReaderPolicy() { 142 } 143 144 void setDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation, 145 const String8& uniqueId) { 146 DisplayViewport v = createDisplayViewport(displayId, width, height, orientation, uniqueId); 147 // Set the size of both the internal and external display at the same time. 148 mConfig.setPhysicalDisplayViewport(ViewportType::VIEWPORT_INTERNAL, v); 149 mConfig.setPhysicalDisplayViewport(ViewportType::VIEWPORT_EXTERNAL, v); 150 } 151 152 void setVirtualDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation, 153 const String8& uniqueId) { 154 Vector<DisplayViewport> viewports; 155 viewports.push_back(createDisplayViewport(displayId, width, height, orientation, uniqueId)); 156 mConfig.setVirtualDisplayViewports(viewports); 157 } 158 159 void addExcludedDeviceName(const String8& deviceName) { 160 mConfig.excludedDeviceNames.push(deviceName); 161 } 162 163 void addDisabledDevice(int32_t deviceId) { 164 ssize_t index = mConfig.disabledDevices.indexOf(deviceId); 165 bool currentlyEnabled = index < 0; 166 if (currentlyEnabled) { 167 mConfig.disabledDevices.add(deviceId); 168 } 169 } 170 171 void removeDisabledDevice(int32_t deviceId) { 172 ssize_t index = mConfig.disabledDevices.indexOf(deviceId); 173 bool currentlyEnabled = index < 0; 174 if (!currentlyEnabled) { 175 mConfig.disabledDevices.remove(deviceId); 176 } 177 } 178 179 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) { 180 mPointerControllers.add(deviceId, controller); 181 } 182 183 const InputReaderConfiguration* getReaderConfiguration() const { 184 return &mConfig; 185 } 186 187 const Vector<InputDeviceInfo>& getInputDevices() const { 188 return mInputDevices; 189 } 190 191 TouchAffineTransformation getTouchAffineTransformation(const String8& inputDeviceDescriptor, 192 int32_t surfaceRotation) { 193 return transform; 194 } 195 196 void setTouchAffineTransformation(const TouchAffineTransformation t) { 197 transform = t; 198 } 199 200 void setPointerCapture(bool enabled) { 201 mConfig.pointerCapture = enabled; 202 } 203 204 private: 205 DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height, 206 int32_t orientation, const String8& uniqueId) { 207 bool isRotated = (orientation == DISPLAY_ORIENTATION_90 208 || orientation == DISPLAY_ORIENTATION_270); 209 DisplayViewport v; 210 v.displayId = displayId; 211 v.orientation = orientation; 212 v.logicalLeft = 0; 213 v.logicalTop = 0; 214 v.logicalRight = isRotated ? height : width; 215 v.logicalBottom = isRotated ? width : height; 216 v.physicalLeft = 0; 217 v.physicalTop = 0; 218 v.physicalRight = isRotated ? height : width; 219 v.physicalBottom = isRotated ? width : height; 220 v.deviceWidth = isRotated ? height : width; 221 v.deviceHeight = isRotated ? width : height; 222 v.uniqueId = uniqueId; 223 return v; 224 } 225 226 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) { 227 *outConfig = mConfig; 228 } 229 230 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) { 231 return mPointerControllers.valueFor(deviceId); 232 } 233 234 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) { 235 mInputDevices = inputDevices; 236 } 237 238 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) { 239 return NULL; 240 } 241 242 virtual String8 getDeviceAlias(const InputDeviceIdentifier&) { 243 return String8::empty(); 244 } 245 }; 246 247 248 // --- FakeInputListener --- 249 250 class FakeInputListener : public InputListenerInterface { 251 private: 252 List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue; 253 List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue; 254 List<NotifyKeyArgs> mNotifyKeyArgsQueue; 255 List<NotifyMotionArgs> mNotifyMotionArgsQueue; 256 List<NotifySwitchArgs> mNotifySwitchArgsQueue; 257 258 protected: 259 virtual ~FakeInputListener() { } 260 261 public: 262 FakeInputListener() { 263 } 264 265 void assertNotifyConfigurationChangedWasCalled( 266 NotifyConfigurationChangedArgs* outEventArgs = NULL) { 267 ASSERT_FALSE(mNotifyConfigurationChangedArgsQueue.empty()) 268 << "Expected notifyConfigurationChanged() to have been called."; 269 if (outEventArgs) { 270 *outEventArgs = *mNotifyConfigurationChangedArgsQueue.begin(); 271 } 272 mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin()); 273 } 274 275 void assertNotifyConfigurationChangedWasNotCalled() { 276 ASSERT_TRUE(mNotifyConfigurationChangedArgsQueue.empty()) 277 << "Expected notifyConfigurationChanged() to not have been called."; 278 } 279 280 void assertNotifyDeviceResetWasCalled( 281 NotifyDeviceResetArgs* outEventArgs = NULL) { 282 ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty()) 283 << "Expected notifyDeviceReset() to have been called."; 284 if (outEventArgs) { 285 *outEventArgs = *mNotifyDeviceResetArgsQueue.begin(); 286 } 287 mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin()); 288 } 289 290 void assertNotifyDeviceResetWasNotCalled() { 291 ASSERT_TRUE(mNotifyDeviceResetArgsQueue.empty()) 292 << "Expected notifyDeviceReset() to not have been called."; 293 } 294 295 void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) { 296 ASSERT_FALSE(mNotifyKeyArgsQueue.empty()) 297 << "Expected notifyKey() to have been called."; 298 if (outEventArgs) { 299 *outEventArgs = *mNotifyKeyArgsQueue.begin(); 300 } 301 mNotifyKeyArgsQueue.erase(mNotifyKeyArgsQueue.begin()); 302 } 303 304 void assertNotifyKeyWasNotCalled() { 305 ASSERT_TRUE(mNotifyKeyArgsQueue.empty()) 306 << "Expected notifyKey() to not have been called."; 307 } 308 309 void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = NULL) { 310 ASSERT_FALSE(mNotifyMotionArgsQueue.empty()) 311 << "Expected notifyMotion() to have been called."; 312 if (outEventArgs) { 313 *outEventArgs = *mNotifyMotionArgsQueue.begin(); 314 } 315 mNotifyMotionArgsQueue.erase(mNotifyMotionArgsQueue.begin()); 316 } 317 318 void assertNotifyMotionWasNotCalled() { 319 ASSERT_TRUE(mNotifyMotionArgsQueue.empty()) 320 << "Expected notifyMotion() to not have been called."; 321 } 322 323 void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = NULL) { 324 ASSERT_FALSE(mNotifySwitchArgsQueue.empty()) 325 << "Expected notifySwitch() to have been called."; 326 if (outEventArgs) { 327 *outEventArgs = *mNotifySwitchArgsQueue.begin(); 328 } 329 mNotifySwitchArgsQueue.erase(mNotifySwitchArgsQueue.begin()); 330 } 331 332 private: 333 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { 334 mNotifyConfigurationChangedArgsQueue.push_back(*args); 335 } 336 337 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) { 338 mNotifyDeviceResetArgsQueue.push_back(*args); 339 } 340 341 virtual void notifyKey(const NotifyKeyArgs* args) { 342 mNotifyKeyArgsQueue.push_back(*args); 343 } 344 345 virtual void notifyMotion(const NotifyMotionArgs* args) { 346 mNotifyMotionArgsQueue.push_back(*args); 347 } 348 349 virtual void notifySwitch(const NotifySwitchArgs* args) { 350 mNotifySwitchArgsQueue.push_back(*args); 351 } 352 }; 353 354 355 // --- FakeEventHub --- 356 357 class FakeEventHub : public EventHubInterface { 358 struct KeyInfo { 359 int32_t keyCode; 360 uint32_t flags; 361 }; 362 363 struct Device { 364 InputDeviceIdentifier identifier; 365 uint32_t classes; 366 PropertyMap configuration; 367 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes; 368 KeyedVector<int, bool> relativeAxes; 369 KeyedVector<int32_t, int32_t> keyCodeStates; 370 KeyedVector<int32_t, int32_t> scanCodeStates; 371 KeyedVector<int32_t, int32_t> switchStates; 372 KeyedVector<int32_t, int32_t> absoluteAxisValue; 373 KeyedVector<int32_t, KeyInfo> keysByScanCode; 374 KeyedVector<int32_t, KeyInfo> keysByUsageCode; 375 KeyedVector<int32_t, bool> leds; 376 Vector<VirtualKeyDefinition> virtualKeys; 377 bool enabled; 378 379 status_t enable() { 380 enabled = true; 381 return OK; 382 } 383 384 status_t disable() { 385 enabled = false; 386 return OK; 387 } 388 389 explicit Device(uint32_t classes) : 390 classes(classes), enabled(true) { 391 } 392 }; 393 394 KeyedVector<int32_t, Device*> mDevices; 395 Vector<String8> mExcludedDevices; 396 List<RawEvent> mEvents; 397 398 protected: 399 virtual ~FakeEventHub() { 400 for (size_t i = 0; i < mDevices.size(); i++) { 401 delete mDevices.valueAt(i); 402 } 403 } 404 405 public: 406 FakeEventHub() { } 407 408 void addDevice(int32_t deviceId, const String8& name, uint32_t classes) { 409 Device* device = new Device(classes); 410 device->identifier.name = name; 411 mDevices.add(deviceId, device); 412 413 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0); 414 } 415 416 void removeDevice(int32_t deviceId) { 417 delete mDevices.valueFor(deviceId); 418 mDevices.removeItem(deviceId); 419 420 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0); 421 } 422 423 bool isDeviceEnabled(int32_t deviceId) { 424 Device* device = getDevice(deviceId); 425 if (device == NULL) { 426 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); 427 return false; 428 } 429 return device->enabled; 430 } 431 432 status_t enableDevice(int32_t deviceId) { 433 status_t result; 434 Device* device = getDevice(deviceId); 435 if (device == NULL) { 436 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); 437 return BAD_VALUE; 438 } 439 if (device->enabled) { 440 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId); 441 return OK; 442 } 443 result = device->enable(); 444 return result; 445 } 446 447 status_t disableDevice(int32_t deviceId) { 448 Device* device = getDevice(deviceId); 449 if (device == NULL) { 450 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__); 451 return BAD_VALUE; 452 } 453 if (!device->enabled) { 454 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId); 455 return OK; 456 } 457 return device->disable(); 458 } 459 460 void finishDeviceScan() { 461 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0); 462 } 463 464 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) { 465 Device* device = getDevice(deviceId); 466 device->configuration.addProperty(key, value); 467 } 468 469 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) { 470 Device* device = getDevice(deviceId); 471 device->configuration.addAll(configuration); 472 } 473 474 void addAbsoluteAxis(int32_t deviceId, int axis, 475 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) { 476 Device* device = getDevice(deviceId); 477 478 RawAbsoluteAxisInfo info; 479 info.valid = true; 480 info.minValue = minValue; 481 info.maxValue = maxValue; 482 info.flat = flat; 483 info.fuzz = fuzz; 484 info.resolution = resolution; 485 device->absoluteAxes.add(axis, info); 486 } 487 488 void addRelativeAxis(int32_t deviceId, int32_t axis) { 489 Device* device = getDevice(deviceId); 490 device->relativeAxes.add(axis, true); 491 } 492 493 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) { 494 Device* device = getDevice(deviceId); 495 device->keyCodeStates.replaceValueFor(keyCode, state); 496 } 497 498 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) { 499 Device* device = getDevice(deviceId); 500 device->scanCodeStates.replaceValueFor(scanCode, state); 501 } 502 503 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) { 504 Device* device = getDevice(deviceId); 505 device->switchStates.replaceValueFor(switchCode, state); 506 } 507 508 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) { 509 Device* device = getDevice(deviceId); 510 device->absoluteAxisValue.replaceValueFor(axis, value); 511 } 512 513 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, 514 int32_t keyCode, uint32_t flags) { 515 Device* device = getDevice(deviceId); 516 KeyInfo info; 517 info.keyCode = keyCode; 518 info.flags = flags; 519 if (scanCode) { 520 device->keysByScanCode.add(scanCode, info); 521 } 522 if (usageCode) { 523 device->keysByUsageCode.add(usageCode, info); 524 } 525 } 526 527 void addLed(int32_t deviceId, int32_t led, bool initialState) { 528 Device* device = getDevice(deviceId); 529 device->leds.add(led, initialState); 530 } 531 532 bool getLedState(int32_t deviceId, int32_t led) { 533 Device* device = getDevice(deviceId); 534 return device->leds.valueFor(led); 535 } 536 537 Vector<String8>& getExcludedDevices() { 538 return mExcludedDevices; 539 } 540 541 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) { 542 Device* device = getDevice(deviceId); 543 device->virtualKeys.push(definition); 544 } 545 546 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type, 547 int32_t code, int32_t value) { 548 RawEvent event; 549 event.when = when; 550 event.deviceId = deviceId; 551 event.type = type; 552 event.code = code; 553 event.value = value; 554 mEvents.push_back(event); 555 556 if (type == EV_ABS) { 557 setAbsoluteAxisValue(deviceId, code, value); 558 } 559 } 560 561 void assertQueueIsEmpty() { 562 ASSERT_EQ(size_t(0), mEvents.size()) 563 << "Expected the event queue to be empty (fully consumed)."; 564 } 565 566 private: 567 Device* getDevice(int32_t deviceId) const { 568 ssize_t index = mDevices.indexOfKey(deviceId); 569 return index >= 0 ? mDevices.valueAt(index) : NULL; 570 } 571 572 virtual uint32_t getDeviceClasses(int32_t deviceId) const { 573 Device* device = getDevice(deviceId); 574 return device ? device->classes : 0; 575 } 576 577 virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const { 578 Device* device = getDevice(deviceId); 579 return device ? device->identifier : InputDeviceIdentifier(); 580 } 581 582 virtual int32_t getDeviceControllerNumber(int32_t) const { 583 return 0; 584 } 585 586 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const { 587 Device* device = getDevice(deviceId); 588 if (device) { 589 *outConfiguration = device->configuration; 590 } 591 } 592 593 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, 594 RawAbsoluteAxisInfo* outAxisInfo) const { 595 Device* device = getDevice(deviceId); 596 if (device) { 597 ssize_t index = device->absoluteAxes.indexOfKey(axis); 598 if (index >= 0) { 599 *outAxisInfo = device->absoluteAxes.valueAt(index); 600 return OK; 601 } 602 } 603 outAxisInfo->clear(); 604 return -1; 605 } 606 607 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const { 608 Device* device = getDevice(deviceId); 609 if (device) { 610 return device->relativeAxes.indexOfKey(axis) >= 0; 611 } 612 return false; 613 } 614 615 virtual bool hasInputProperty(int32_t, int) const { 616 return false; 617 } 618 619 virtual status_t mapKey(int32_t deviceId, 620 int32_t scanCode, int32_t usageCode, int32_t metaState, 621 int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const { 622 Device* device = getDevice(deviceId); 623 if (device) { 624 const KeyInfo* key = getKey(device, scanCode, usageCode); 625 if (key) { 626 if (outKeycode) { 627 *outKeycode = key->keyCode; 628 } 629 if (outFlags) { 630 *outFlags = key->flags; 631 } 632 if (outMetaState) { 633 *outMetaState = metaState; 634 } 635 return OK; 636 } 637 } 638 return NAME_NOT_FOUND; 639 } 640 641 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const { 642 if (usageCode) { 643 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode); 644 if (index >= 0) { 645 return &device->keysByUsageCode.valueAt(index); 646 } 647 } 648 if (scanCode) { 649 ssize_t index = device->keysByScanCode.indexOfKey(scanCode); 650 if (index >= 0) { 651 return &device->keysByScanCode.valueAt(index); 652 } 653 } 654 return NULL; 655 } 656 657 virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const { 658 return NAME_NOT_FOUND; 659 } 660 661 virtual void setExcludedDevices(const Vector<String8>& devices) { 662 mExcludedDevices = devices; 663 } 664 665 virtual size_t getEvents(int, RawEvent* buffer, size_t) { 666 if (mEvents.empty()) { 667 return 0; 668 } 669 670 *buffer = *mEvents.begin(); 671 mEvents.erase(mEvents.begin()); 672 return 1; 673 } 674 675 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const { 676 Device* device = getDevice(deviceId); 677 if (device) { 678 ssize_t index = device->scanCodeStates.indexOfKey(scanCode); 679 if (index >= 0) { 680 return device->scanCodeStates.valueAt(index); 681 } 682 } 683 return AKEY_STATE_UNKNOWN; 684 } 685 686 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const { 687 Device* device = getDevice(deviceId); 688 if (device) { 689 ssize_t index = device->keyCodeStates.indexOfKey(keyCode); 690 if (index >= 0) { 691 return device->keyCodeStates.valueAt(index); 692 } 693 } 694 return AKEY_STATE_UNKNOWN; 695 } 696 697 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const { 698 Device* device = getDevice(deviceId); 699 if (device) { 700 ssize_t index = device->switchStates.indexOfKey(sw); 701 if (index >= 0) { 702 return device->switchStates.valueAt(index); 703 } 704 } 705 return AKEY_STATE_UNKNOWN; 706 } 707 708 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, 709 int32_t* outValue) const { 710 Device* device = getDevice(deviceId); 711 if (device) { 712 ssize_t index = device->absoluteAxisValue.indexOfKey(axis); 713 if (index >= 0) { 714 *outValue = device->absoluteAxisValue.valueAt(index); 715 return OK; 716 } 717 } 718 *outValue = 0; 719 return -1; 720 } 721 722 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, 723 uint8_t* outFlags) const { 724 bool result = false; 725 Device* device = getDevice(deviceId); 726 if (device) { 727 for (size_t i = 0; i < numCodes; i++) { 728 for (size_t j = 0; j < device->keysByScanCode.size(); j++) { 729 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) { 730 outFlags[i] = 1; 731 result = true; 732 } 733 } 734 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) { 735 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) { 736 outFlags[i] = 1; 737 result = true; 738 } 739 } 740 } 741 } 742 return result; 743 } 744 745 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const { 746 Device* device = getDevice(deviceId); 747 if (device) { 748 ssize_t index = device->keysByScanCode.indexOfKey(scanCode); 749 return index >= 0; 750 } 751 return false; 752 } 753 754 virtual bool hasLed(int32_t deviceId, int32_t led) const { 755 Device* device = getDevice(deviceId); 756 return device && device->leds.indexOfKey(led) >= 0; 757 } 758 759 virtual void setLedState(int32_t deviceId, int32_t led, bool on) { 760 Device* device = getDevice(deviceId); 761 if (device) { 762 ssize_t index = device->leds.indexOfKey(led); 763 if (index >= 0) { 764 device->leds.replaceValueAt(led, on); 765 } else { 766 ADD_FAILURE() 767 << "Attempted to set the state of an LED that the EventHub declared " 768 "was not present. led=" << led; 769 } 770 } 771 } 772 773 virtual void getVirtualKeyDefinitions(int32_t deviceId, 774 Vector<VirtualKeyDefinition>& outVirtualKeys) const { 775 outVirtualKeys.clear(); 776 777 Device* device = getDevice(deviceId); 778 if (device) { 779 outVirtualKeys.appendVector(device->virtualKeys); 780 } 781 } 782 783 virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const { 784 return NULL; 785 } 786 787 virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) { 788 return false; 789 } 790 791 virtual void vibrate(int32_t, nsecs_t) { 792 } 793 794 virtual void cancelVibrate(int32_t) { 795 } 796 797 virtual bool isExternal(int32_t) const { 798 return false; 799 } 800 801 virtual void dump(String8&) { 802 } 803 804 virtual void monitor() { 805 } 806 807 virtual void requestReopenDevices() { 808 } 809 810 virtual void wake() { 811 } 812 }; 813 814 815 // --- FakeInputReaderContext --- 816 817 class FakeInputReaderContext : public InputReaderContext { 818 sp<EventHubInterface> mEventHub; 819 sp<InputReaderPolicyInterface> mPolicy; 820 sp<InputListenerInterface> mListener; 821 int32_t mGlobalMetaState; 822 bool mUpdateGlobalMetaStateWasCalled; 823 int32_t mGeneration; 824 825 public: 826 FakeInputReaderContext(const sp<EventHubInterface>& eventHub, 827 const sp<InputReaderPolicyInterface>& policy, 828 const sp<InputListenerInterface>& listener) : 829 mEventHub(eventHub), mPolicy(policy), mListener(listener), 830 mGlobalMetaState(0) { 831 } 832 833 virtual ~FakeInputReaderContext() { } 834 835 void assertUpdateGlobalMetaStateWasCalled() { 836 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled) 837 << "Expected updateGlobalMetaState() to have been called."; 838 mUpdateGlobalMetaStateWasCalled = false; 839 } 840 841 void setGlobalMetaState(int32_t state) { 842 mGlobalMetaState = state; 843 } 844 845 uint32_t getGeneration() { 846 return mGeneration; 847 } 848 849 private: 850 virtual void updateGlobalMetaState() { 851 mUpdateGlobalMetaStateWasCalled = true; 852 } 853 854 virtual int32_t getGlobalMetaState() { 855 return mGlobalMetaState; 856 } 857 858 virtual EventHubInterface* getEventHub() { 859 return mEventHub.get(); 860 } 861 862 virtual InputReaderPolicyInterface* getPolicy() { 863 return mPolicy.get(); 864 } 865 866 virtual InputListenerInterface* getListener() { 867 return mListener.get(); 868 } 869 870 virtual void disableVirtualKeysUntil(nsecs_t) { 871 } 872 873 virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) { 874 return false; 875 } 876 877 virtual void fadePointer() { 878 } 879 880 virtual void requestTimeoutAtTime(nsecs_t) { 881 } 882 883 virtual int32_t bumpGeneration() { 884 return ++mGeneration; 885 } 886 887 virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) { 888 889 } 890 891 virtual void dispatchExternalStylusState(const StylusState&) { 892 893 } 894 }; 895 896 897 // --- FakeInputMapper --- 898 899 class FakeInputMapper : public InputMapper { 900 uint32_t mSources; 901 int32_t mKeyboardType; 902 int32_t mMetaState; 903 KeyedVector<int32_t, int32_t> mKeyCodeStates; 904 KeyedVector<int32_t, int32_t> mScanCodeStates; 905 KeyedVector<int32_t, int32_t> mSwitchStates; 906 Vector<int32_t> mSupportedKeyCodes; 907 RawEvent mLastEvent; 908 909 bool mConfigureWasCalled; 910 bool mResetWasCalled; 911 bool mProcessWasCalled; 912 913 public: 914 FakeInputMapper(InputDevice* device, uint32_t sources) : 915 InputMapper(device), 916 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE), 917 mMetaState(0), 918 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) { 919 } 920 921 virtual ~FakeInputMapper() { } 922 923 void setKeyboardType(int32_t keyboardType) { 924 mKeyboardType = keyboardType; 925 } 926 927 void setMetaState(int32_t metaState) { 928 mMetaState = metaState; 929 } 930 931 void assertConfigureWasCalled() { 932 ASSERT_TRUE(mConfigureWasCalled) 933 << "Expected configure() to have been called."; 934 mConfigureWasCalled = false; 935 } 936 937 void assertResetWasCalled() { 938 ASSERT_TRUE(mResetWasCalled) 939 << "Expected reset() to have been called."; 940 mResetWasCalled = false; 941 } 942 943 void assertProcessWasCalled(RawEvent* outLastEvent = NULL) { 944 ASSERT_TRUE(mProcessWasCalled) 945 << "Expected process() to have been called."; 946 if (outLastEvent) { 947 *outLastEvent = mLastEvent; 948 } 949 mProcessWasCalled = false; 950 } 951 952 void setKeyCodeState(int32_t keyCode, int32_t state) { 953 mKeyCodeStates.replaceValueFor(keyCode, state); 954 } 955 956 void setScanCodeState(int32_t scanCode, int32_t state) { 957 mScanCodeStates.replaceValueFor(scanCode, state); 958 } 959 960 void setSwitchState(int32_t switchCode, int32_t state) { 961 mSwitchStates.replaceValueFor(switchCode, state); 962 } 963 964 void addSupportedKeyCode(int32_t keyCode) { 965 mSupportedKeyCodes.add(keyCode); 966 } 967 968 private: 969 virtual uint32_t getSources() { 970 return mSources; 971 } 972 973 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) { 974 InputMapper::populateDeviceInfo(deviceInfo); 975 976 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) { 977 deviceInfo->setKeyboardType(mKeyboardType); 978 } 979 } 980 981 virtual void configure(nsecs_t, const InputReaderConfiguration*, uint32_t) { 982 mConfigureWasCalled = true; 983 } 984 985 virtual void reset(nsecs_t) { 986 mResetWasCalled = true; 987 } 988 989 virtual void process(const RawEvent* rawEvent) { 990 mLastEvent = *rawEvent; 991 mProcessWasCalled = true; 992 } 993 994 virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) { 995 ssize_t index = mKeyCodeStates.indexOfKey(keyCode); 996 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN; 997 } 998 999 virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) { 1000 ssize_t index = mScanCodeStates.indexOfKey(scanCode); 1001 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN; 1002 } 1003 1004 virtual int32_t getSwitchState(uint32_t, int32_t switchCode) { 1005 ssize_t index = mSwitchStates.indexOfKey(switchCode); 1006 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN; 1007 } 1008 1009 virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes, 1010 const int32_t* keyCodes, uint8_t* outFlags) { 1011 bool result = false; 1012 for (size_t i = 0; i < numCodes; i++) { 1013 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) { 1014 if (keyCodes[i] == mSupportedKeyCodes[j]) { 1015 outFlags[i] = 1; 1016 result = true; 1017 } 1018 } 1019 } 1020 return result; 1021 } 1022 1023 virtual int32_t getMetaState() { 1024 return mMetaState; 1025 } 1026 1027 virtual void fadePointer() { 1028 } 1029 }; 1030 1031 1032 // --- InstrumentedInputReader --- 1033 1034 class InstrumentedInputReader : public InputReader { 1035 InputDevice* mNextDevice; 1036 1037 public: 1038 InstrumentedInputReader(const sp<EventHubInterface>& eventHub, 1039 const sp<InputReaderPolicyInterface>& policy, 1040 const sp<InputListenerInterface>& listener) : 1041 InputReader(eventHub, policy, listener), 1042 mNextDevice(NULL) { 1043 } 1044 1045 virtual ~InstrumentedInputReader() { 1046 if (mNextDevice) { 1047 delete mNextDevice; 1048 } 1049 } 1050 1051 void setNextDevice(InputDevice* device) { 1052 mNextDevice = device; 1053 } 1054 1055 InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const String8& name, 1056 uint32_t classes) { 1057 InputDeviceIdentifier identifier; 1058 identifier.name = name; 1059 int32_t generation = deviceId + 1; 1060 return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier, 1061 classes); 1062 } 1063 1064 protected: 1065 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber, 1066 const InputDeviceIdentifier& identifier, uint32_t classes) { 1067 if (mNextDevice) { 1068 InputDevice* device = mNextDevice; 1069 mNextDevice = NULL; 1070 return device; 1071 } 1072 return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes); 1073 } 1074 1075 friend class InputReaderTest; 1076 }; 1077 1078 1079 // --- InputReaderTest --- 1080 1081 class InputReaderTest : public testing::Test { 1082 protected: 1083 sp<FakeInputListener> mFakeListener; 1084 sp<FakeInputReaderPolicy> mFakePolicy; 1085 sp<FakeEventHub> mFakeEventHub; 1086 sp<InstrumentedInputReader> mReader; 1087 1088 virtual void SetUp() { 1089 mFakeEventHub = new FakeEventHub(); 1090 mFakePolicy = new FakeInputReaderPolicy(); 1091 mFakeListener = new FakeInputListener(); 1092 1093 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener); 1094 } 1095 1096 virtual void TearDown() { 1097 mReader.clear(); 1098 1099 mFakeListener.clear(); 1100 mFakePolicy.clear(); 1101 mFakeEventHub.clear(); 1102 } 1103 1104 void addDevice(int32_t deviceId, const String8& name, uint32_t classes, 1105 const PropertyMap* configuration) { 1106 mFakeEventHub->addDevice(deviceId, name, classes); 1107 1108 if (configuration) { 1109 mFakeEventHub->addConfigurationMap(deviceId, configuration); 1110 } 1111 mFakeEventHub->finishDeviceScan(); 1112 mReader->loopOnce(); 1113 mReader->loopOnce(); 1114 mFakeEventHub->assertQueueIsEmpty(); 1115 } 1116 1117 void disableDevice(int32_t deviceId, InputDevice* device) { 1118 mFakePolicy->addDisabledDevice(deviceId); 1119 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device); 1120 } 1121 1122 void enableDevice(int32_t deviceId, InputDevice* device) { 1123 mFakePolicy->removeDisabledDevice(deviceId); 1124 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device); 1125 } 1126 1127 void configureDevice(uint32_t changes, InputDevice* device) { 1128 device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes); 1129 } 1130 1131 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber, 1132 const String8& name, uint32_t classes, uint32_t sources, 1133 const PropertyMap* configuration) { 1134 InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes); 1135 FakeInputMapper* mapper = new FakeInputMapper(device, sources); 1136 device->addMapper(mapper); 1137 mReader->setNextDevice(device); 1138 addDevice(deviceId, name, classes, configuration); 1139 return mapper; 1140 } 1141 }; 1142 1143 TEST_F(InputReaderTest, GetInputDevices) { 1144 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"), 1145 INPUT_DEVICE_CLASS_KEYBOARD, NULL)); 1146 ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("ignored"), 1147 0, NULL)); // no classes so device will be ignored 1148 1149 Vector<InputDeviceInfo> inputDevices; 1150 mReader->getInputDevices(inputDevices); 1151 1152 ASSERT_EQ(1U, inputDevices.size()); 1153 ASSERT_EQ(1, inputDevices[0].getId()); 1154 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string()); 1155 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType()); 1156 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources()); 1157 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size()); 1158 1159 // Should also have received a notification describing the new input devices. 1160 inputDevices = mFakePolicy->getInputDevices(); 1161 ASSERT_EQ(1U, inputDevices.size()); 1162 ASSERT_EQ(1, inputDevices[0].getId()); 1163 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string()); 1164 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType()); 1165 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources()); 1166 ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size()); 1167 } 1168 1169 TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) { 1170 constexpr int32_t deviceId = 1; 1171 constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD; 1172 InputDevice* device = mReader->newDevice(deviceId, 0, String8("fake"), deviceClass); 1173 // Must add at least one mapper or the device will be ignored! 1174 FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD); 1175 device->addMapper(mapper); 1176 mReader->setNextDevice(device); 1177 addDevice(deviceId, String8("fake"), deviceClass, NULL); 1178 1179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(NULL)); 1180 1181 NotifyDeviceResetArgs resetArgs; 1182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs)); 1183 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 1184 ASSERT_EQ(deviceId, resetArgs.deviceId); 1185 1186 ASSERT_EQ(device->isEnabled(), true); 1187 disableDevice(deviceId, device); 1188 mReader->loopOnce(); 1189 1190 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs); 1191 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 1192 ASSERT_EQ(deviceId, resetArgs.deviceId); 1193 ASSERT_EQ(device->isEnabled(), false); 1194 1195 disableDevice(deviceId, device); 1196 mReader->loopOnce(); 1197 mFakeListener->assertNotifyDeviceResetWasNotCalled(); 1198 mFakeListener->assertNotifyConfigurationChangedWasNotCalled(); 1199 ASSERT_EQ(device->isEnabled(), false); 1200 1201 enableDevice(deviceId, device); 1202 mReader->loopOnce(); 1203 mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs); 1204 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 1205 ASSERT_EQ(deviceId, resetArgs.deviceId); 1206 ASSERT_EQ(device->isEnabled(), true); 1207 } 1208 1209 TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) { 1210 FakeInputMapper* mapper = NULL; 1211 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"), 1212 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL)); 1213 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN); 1214 1215 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0, 1216 AINPUT_SOURCE_ANY, AKEYCODE_A)) 1217 << "Should return unknown when the device id is >= 0 but unknown."; 1218 1219 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1, 1220 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1221 << "Should return unknown when the device id is valid but the sources are not supported by the device."; 1222 1223 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1, 1224 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1225 << "Should return value provided by mapper when device id is valid and the device supports some of the sources."; 1226 1227 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1, 1228 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1229 << "Should return unknown when the device id is < 0 but the sources are not supported by any device."; 1230 1231 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1, 1232 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1233 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources."; 1234 } 1235 1236 TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) { 1237 FakeInputMapper* mapper = NULL; 1238 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"), 1239 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL)); 1240 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN); 1241 1242 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0, 1243 AINPUT_SOURCE_ANY, KEY_A)) 1244 << "Should return unknown when the device id is >= 0 but unknown."; 1245 1246 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1, 1247 AINPUT_SOURCE_TRACKBALL, KEY_A)) 1248 << "Should return unknown when the device id is valid but the sources are not supported by the device."; 1249 1250 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1, 1251 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A)) 1252 << "Should return value provided by mapper when device id is valid and the device supports some of the sources."; 1253 1254 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1, 1255 AINPUT_SOURCE_TRACKBALL, KEY_A)) 1256 << "Should return unknown when the device id is < 0 but the sources are not supported by any device."; 1257 1258 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1, 1259 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A)) 1260 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources."; 1261 } 1262 1263 TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) { 1264 FakeInputMapper* mapper = NULL; 1265 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"), 1266 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL)); 1267 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN); 1268 1269 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0, 1270 AINPUT_SOURCE_ANY, SW_LID)) 1271 << "Should return unknown when the device id is >= 0 but unknown."; 1272 1273 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1, 1274 AINPUT_SOURCE_TRACKBALL, SW_LID)) 1275 << "Should return unknown when the device id is valid but the sources are not supported by the device."; 1276 1277 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1, 1278 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID)) 1279 << "Should return value provided by mapper when device id is valid and the device supports some of the sources."; 1280 1281 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1, 1282 AINPUT_SOURCE_TRACKBALL, SW_LID)) 1283 << "Should return unknown when the device id is < 0 but the sources are not supported by any device."; 1284 1285 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1, 1286 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID)) 1287 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources."; 1288 } 1289 1290 TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) { 1291 FakeInputMapper* mapper = NULL; 1292 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"), 1293 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL)); 1294 mapper->addSupportedKeyCode(AKEYCODE_A); 1295 mapper->addSupportedKeyCode(AKEYCODE_B); 1296 1297 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 }; 1298 uint8_t flags[4] = { 0, 0, 0, 1 }; 1299 1300 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags)) 1301 << "Should return false when device id is >= 0 but unknown."; 1302 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]); 1303 1304 flags[3] = 1; 1305 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags)) 1306 << "Should return false when device id is valid but the sources are not supported by the device."; 1307 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]); 1308 1309 flags[3] = 1; 1310 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags)) 1311 << "Should return value provided by mapper when device id is valid and the device supports some of the sources."; 1312 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]); 1313 1314 flags[3] = 1; 1315 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags)) 1316 << "Should return false when the device id is < 0 but the sources are not supported by any device."; 1317 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]); 1318 1319 flags[3] = 1; 1320 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags)) 1321 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources."; 1322 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]); 1323 } 1324 1325 TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) { 1326 addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL); 1327 1328 NotifyConfigurationChangedArgs args; 1329 1330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args)); 1331 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 1332 } 1333 1334 TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) { 1335 FakeInputMapper* mapper = NULL; 1336 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"), 1337 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL)); 1338 1339 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1); 1340 mReader->loopOnce(); 1341 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty()); 1342 1343 RawEvent event; 1344 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event)); 1345 ASSERT_EQ(0, event.when); 1346 ASSERT_EQ(1, event.deviceId); 1347 ASSERT_EQ(EV_KEY, event.type); 1348 ASSERT_EQ(KEY_A, event.code); 1349 ASSERT_EQ(1, event.value); 1350 } 1351 1352 1353 // --- InputDeviceTest --- 1354 1355 class InputDeviceTest : public testing::Test { 1356 protected: 1357 static const char* DEVICE_NAME; 1358 static const int32_t DEVICE_ID; 1359 static const int32_t DEVICE_GENERATION; 1360 static const int32_t DEVICE_CONTROLLER_NUMBER; 1361 static const uint32_t DEVICE_CLASSES; 1362 1363 sp<FakeEventHub> mFakeEventHub; 1364 sp<FakeInputReaderPolicy> mFakePolicy; 1365 sp<FakeInputListener> mFakeListener; 1366 FakeInputReaderContext* mFakeContext; 1367 1368 InputDevice* mDevice; 1369 1370 virtual void SetUp() { 1371 mFakeEventHub = new FakeEventHub(); 1372 mFakePolicy = new FakeInputReaderPolicy(); 1373 mFakeListener = new FakeInputListener(); 1374 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener); 1375 1376 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0); 1377 InputDeviceIdentifier identifier; 1378 identifier.name = DEVICE_NAME; 1379 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, 1380 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES); 1381 } 1382 1383 virtual void TearDown() { 1384 delete mDevice; 1385 1386 delete mFakeContext; 1387 mFakeListener.clear(); 1388 mFakePolicy.clear(); 1389 mFakeEventHub.clear(); 1390 } 1391 }; 1392 1393 const char* InputDeviceTest::DEVICE_NAME = "device"; 1394 const int32_t InputDeviceTest::DEVICE_ID = 1; 1395 const int32_t InputDeviceTest::DEVICE_GENERATION = 2; 1396 const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0; 1397 const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD 1398 | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK; 1399 1400 TEST_F(InputDeviceTest, ImmutableProperties) { 1401 ASSERT_EQ(DEVICE_ID, mDevice->getId()); 1402 ASSERT_STREQ(DEVICE_NAME, mDevice->getName()); 1403 ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses()); 1404 } 1405 1406 TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) { 1407 ASSERT_EQ(mDevice->isEnabled(), true); 1408 } 1409 1410 TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) { 1411 // Configuration. 1412 InputReaderConfiguration config; 1413 mDevice->configure(ARBITRARY_TIME, &config, 0); 1414 1415 // Reset. 1416 mDevice->reset(ARBITRARY_TIME); 1417 1418 NotifyDeviceResetArgs resetArgs; 1419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs)); 1420 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 1421 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId); 1422 1423 // Metadata. 1424 ASSERT_TRUE(mDevice->isIgnored()); 1425 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources()); 1426 1427 InputDeviceInfo info; 1428 mDevice->getDeviceInfo(&info); 1429 ASSERT_EQ(DEVICE_ID, info.getId()); 1430 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string()); 1431 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType()); 1432 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources()); 1433 1434 // State queries. 1435 ASSERT_EQ(0, mDevice->getMetaState()); 1436 1437 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0)) 1438 << "Ignored device should return unknown key code state."; 1439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0)) 1440 << "Ignored device should return unknown scan code state."; 1441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0)) 1442 << "Ignored device should return unknown switch state."; 1443 1444 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B }; 1445 uint8_t flags[2] = { 0, 1 }; 1446 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags)) 1447 << "Ignored device should never mark any key codes."; 1448 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged."; 1449 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged."; 1450 } 1451 1452 TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) { 1453 // Configuration. 1454 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value")); 1455 1456 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD); 1457 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1458 mapper1->setMetaState(AMETA_ALT_ON); 1459 mapper1->addSupportedKeyCode(AKEYCODE_A); 1460 mapper1->addSupportedKeyCode(AKEYCODE_B); 1461 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN); 1462 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP); 1463 mapper1->setScanCodeState(2, AKEY_STATE_DOWN); 1464 mapper1->setScanCodeState(3, AKEY_STATE_UP); 1465 mapper1->setSwitchState(4, AKEY_STATE_DOWN); 1466 mDevice->addMapper(mapper1); 1467 1468 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN); 1469 mapper2->setMetaState(AMETA_SHIFT_ON); 1470 mDevice->addMapper(mapper2); 1471 1472 InputReaderConfiguration config; 1473 mDevice->configure(ARBITRARY_TIME, &config, 0); 1474 1475 String8 propertyValue; 1476 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue)) 1477 << "Device should have read configuration during configuration phase."; 1478 ASSERT_STREQ("value", propertyValue.string()); 1479 1480 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled()); 1481 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled()); 1482 1483 // Reset 1484 mDevice->reset(ARBITRARY_TIME); 1485 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled()); 1486 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled()); 1487 1488 NotifyDeviceResetArgs resetArgs; 1489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs)); 1490 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 1491 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId); 1492 1493 // Metadata. 1494 ASSERT_FALSE(mDevice->isIgnored()); 1495 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources()); 1496 1497 InputDeviceInfo info; 1498 mDevice->getDeviceInfo(&info); 1499 ASSERT_EQ(DEVICE_ID, info.getId()); 1500 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string()); 1501 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType()); 1502 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources()); 1503 1504 // State queries. 1505 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState()) 1506 << "Should query mappers and combine meta states."; 1507 1508 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1509 << "Should return unknown key code state when source not supported."; 1510 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1511 << "Should return unknown scan code state when source not supported."; 1512 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A)) 1513 << "Should return unknown switch state when source not supported."; 1514 1515 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A)) 1516 << "Should query mapper when source is supported."; 1517 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3)) 1518 << "Should query mapper when source is supported."; 1519 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4)) 1520 << "Should query mapper when source is supported."; 1521 1522 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 }; 1523 uint8_t flags[4] = { 0, 0, 0, 1 }; 1524 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags)) 1525 << "Should do nothing when source is unsupported."; 1526 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported."; 1527 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported."; 1528 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported."; 1529 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported."; 1530 1531 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags)) 1532 << "Should query mapper when source is supported."; 1533 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set."; 1534 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set."; 1535 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged."; 1536 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged."; 1537 1538 // Event handling. 1539 RawEvent event; 1540 mDevice->process(&event, 1); 1541 1542 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled()); 1543 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled()); 1544 } 1545 1546 1547 // --- InputMapperTest --- 1548 1549 class InputMapperTest : public testing::Test { 1550 protected: 1551 static const char* DEVICE_NAME; 1552 static const int32_t DEVICE_ID; 1553 static const int32_t DEVICE_GENERATION; 1554 static const int32_t DEVICE_CONTROLLER_NUMBER; 1555 static const uint32_t DEVICE_CLASSES; 1556 1557 sp<FakeEventHub> mFakeEventHub; 1558 sp<FakeInputReaderPolicy> mFakePolicy; 1559 sp<FakeInputListener> mFakeListener; 1560 FakeInputReaderContext* mFakeContext; 1561 InputDevice* mDevice; 1562 1563 virtual void SetUp() { 1564 mFakeEventHub = new FakeEventHub(); 1565 mFakePolicy = new FakeInputReaderPolicy(); 1566 mFakeListener = new FakeInputListener(); 1567 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener); 1568 InputDeviceIdentifier identifier; 1569 identifier.name = DEVICE_NAME; 1570 mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION, 1571 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES); 1572 1573 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0); 1574 } 1575 1576 virtual void TearDown() { 1577 delete mDevice; 1578 delete mFakeContext; 1579 mFakeListener.clear(); 1580 mFakePolicy.clear(); 1581 mFakeEventHub.clear(); 1582 } 1583 1584 void addConfigurationProperty(const char* key, const char* value) { 1585 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value)); 1586 } 1587 1588 void configureDevice(uint32_t changes) { 1589 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes); 1590 } 1591 1592 void addMapperAndConfigure(InputMapper* mapper) { 1593 mDevice->addMapper(mapper); 1594 configureDevice(0); 1595 mDevice->reset(ARBITRARY_TIME); 1596 } 1597 1598 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height, 1599 int32_t orientation) { 1600 mFakePolicy->setDisplayViewport(displayId, width, height, orientation, String8::empty()); 1601 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO); 1602 } 1603 1604 void setVirtualDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height, 1605 int32_t orientation, const String8& uniqueId) { 1606 mFakePolicy->setVirtualDisplayViewport(displayId, width, height, orientation, uniqueId); 1607 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO); 1608 } 1609 1610 static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type, 1611 int32_t code, int32_t value) { 1612 RawEvent event; 1613 event.when = when; 1614 event.deviceId = deviceId; 1615 event.type = type; 1616 event.code = code; 1617 event.value = value; 1618 mapper->process(&event); 1619 } 1620 1621 static void assertMotionRange(const InputDeviceInfo& info, 1622 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) { 1623 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source); 1624 ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source; 1625 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source; 1626 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source; 1627 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source; 1628 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source; 1629 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source; 1630 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source; 1631 } 1632 1633 static void assertPointerCoords(const PointerCoords& coords, 1634 float x, float y, float pressure, float size, 1635 float touchMajor, float touchMinor, float toolMajor, float toolMinor, 1636 float orientation, float distance) { 1637 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1); 1638 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 1639 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON); 1640 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON); 1641 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1); 1642 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1); 1643 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1); 1644 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1); 1645 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON); 1646 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON); 1647 } 1648 1649 static void assertPosition(const sp<FakePointerController>& controller, float x, float y) { 1650 float actualX, actualY; 1651 controller->getPosition(&actualX, &actualY); 1652 ASSERT_NEAR(x, actualX, 1); 1653 ASSERT_NEAR(y, actualY, 1); 1654 } 1655 }; 1656 1657 const char* InputMapperTest::DEVICE_NAME = "device"; 1658 const int32_t InputMapperTest::DEVICE_ID = 1; 1659 const int32_t InputMapperTest::DEVICE_GENERATION = 2; 1660 const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0; 1661 const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests 1662 1663 1664 // --- SwitchInputMapperTest --- 1665 1666 class SwitchInputMapperTest : public InputMapperTest { 1667 protected: 1668 }; 1669 1670 TEST_F(SwitchInputMapperTest, GetSources) { 1671 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice); 1672 addMapperAndConfigure(mapper); 1673 1674 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources()); 1675 } 1676 1677 TEST_F(SwitchInputMapperTest, GetSwitchState) { 1678 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice); 1679 addMapperAndConfigure(mapper); 1680 1681 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1); 1682 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID)); 1683 1684 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0); 1685 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID)); 1686 } 1687 1688 TEST_F(SwitchInputMapperTest, Process) { 1689 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice); 1690 addMapperAndConfigure(mapper); 1691 1692 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 1); 1693 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_JACK_PHYSICAL_INSERT, 1); 1694 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_HEADPHONE_INSERT, 0); 1695 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 1696 1697 NotifySwitchArgs args; 1698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args)); 1699 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 1700 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues); 1701 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT), 1702 args.switchMask); 1703 ASSERT_EQ(uint32_t(0), args.policyFlags); 1704 } 1705 1706 1707 // --- KeyboardInputMapperTest --- 1708 1709 class KeyboardInputMapperTest : public InputMapperTest { 1710 protected: 1711 void testDPadKeyRotation(KeyboardInputMapper* mapper, 1712 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode); 1713 }; 1714 1715 void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper, 1716 int32_t originalScanCode, int32_t, int32_t rotatedKeyCode) { 1717 NotifyKeyArgs args; 1718 1719 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 1); 1720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1721 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 1722 ASSERT_EQ(originalScanCode, args.scanCode); 1723 ASSERT_EQ(rotatedKeyCode, args.keyCode); 1724 1725 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 0); 1726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1727 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 1728 ASSERT_EQ(originalScanCode, args.scanCode); 1729 ASSERT_EQ(rotatedKeyCode, args.keyCode); 1730 } 1731 1732 1733 TEST_F(KeyboardInputMapperTest, GetSources) { 1734 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 1735 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1736 addMapperAndConfigure(mapper); 1737 1738 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources()); 1739 } 1740 1741 TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) { 1742 const int32_t USAGE_A = 0x070004; 1743 const int32_t USAGE_UNKNOWN = 0x07ffff; 1744 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE); 1745 mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE); 1746 1747 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 1748 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1749 addMapperAndConfigure(mapper); 1750 1751 // Key down by scan code. 1752 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1753 EV_KEY, KEY_HOME, 1); 1754 NotifyKeyArgs args; 1755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1756 ASSERT_EQ(DEVICE_ID, args.deviceId); 1757 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1758 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 1759 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 1760 ASSERT_EQ(AKEYCODE_HOME, args.keyCode); 1761 ASSERT_EQ(KEY_HOME, args.scanCode); 1762 ASSERT_EQ(AMETA_NONE, args.metaState); 1763 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1764 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags); 1765 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1766 1767 // Key up by scan code. 1768 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, 1769 EV_KEY, KEY_HOME, 0); 1770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1771 ASSERT_EQ(DEVICE_ID, args.deviceId); 1772 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1773 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime); 1774 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 1775 ASSERT_EQ(AKEYCODE_HOME, args.keyCode); 1776 ASSERT_EQ(KEY_HOME, args.scanCode); 1777 ASSERT_EQ(AMETA_NONE, args.metaState); 1778 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1779 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags); 1780 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1781 1782 // Key down by usage code. 1783 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1784 EV_MSC, MSC_SCAN, USAGE_A); 1785 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1786 EV_KEY, 0, 1); 1787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1788 ASSERT_EQ(DEVICE_ID, args.deviceId); 1789 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1790 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 1791 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 1792 ASSERT_EQ(AKEYCODE_A, args.keyCode); 1793 ASSERT_EQ(0, args.scanCode); 1794 ASSERT_EQ(AMETA_NONE, args.metaState); 1795 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1796 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags); 1797 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1798 1799 // Key up by usage code. 1800 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1801 EV_MSC, MSC_SCAN, USAGE_A); 1802 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, 1803 EV_KEY, 0, 0); 1804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1805 ASSERT_EQ(DEVICE_ID, args.deviceId); 1806 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1807 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime); 1808 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 1809 ASSERT_EQ(AKEYCODE_A, args.keyCode); 1810 ASSERT_EQ(0, args.scanCode); 1811 ASSERT_EQ(AMETA_NONE, args.metaState); 1812 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags); 1814 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1815 1816 // Key down with unknown scan code or usage code. 1817 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1818 EV_MSC, MSC_SCAN, USAGE_UNKNOWN); 1819 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1820 EV_KEY, KEY_UNKNOWN, 1); 1821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1822 ASSERT_EQ(DEVICE_ID, args.deviceId); 1823 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1824 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 1825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 1826 ASSERT_EQ(0, args.keyCode); 1827 ASSERT_EQ(KEY_UNKNOWN, args.scanCode); 1828 ASSERT_EQ(AMETA_NONE, args.metaState); 1829 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1830 ASSERT_EQ(0U, args.policyFlags); 1831 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1832 1833 // Key up with unknown scan code or usage code. 1834 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1835 EV_MSC, MSC_SCAN, USAGE_UNKNOWN); 1836 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, 1837 EV_KEY, KEY_UNKNOWN, 0); 1838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1839 ASSERT_EQ(DEVICE_ID, args.deviceId); 1840 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 1841 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime); 1842 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 1843 ASSERT_EQ(0, args.keyCode); 1844 ASSERT_EQ(KEY_UNKNOWN, args.scanCode); 1845 ASSERT_EQ(AMETA_NONE, args.metaState); 1846 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags); 1847 ASSERT_EQ(0U, args.policyFlags); 1848 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 1849 } 1850 1851 TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) { 1852 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0); 1853 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0); 1854 1855 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 1856 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1857 addMapperAndConfigure(mapper); 1858 1859 // Initial metastate. 1860 ASSERT_EQ(AMETA_NONE, mapper->getMetaState()); 1861 1862 // Metakey down. 1863 process(mapper, ARBITRARY_TIME, DEVICE_ID, 1864 EV_KEY, KEY_LEFTSHIFT, 1); 1865 NotifyKeyArgs args; 1866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 1868 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState()); 1869 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled()); 1870 1871 // Key down. 1872 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, 1873 EV_KEY, KEY_A, 1); 1874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1875 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 1876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState()); 1877 1878 // Key up. 1879 process(mapper, ARBITRARY_TIME + 2, DEVICE_ID, 1880 EV_KEY, KEY_A, 0); 1881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1882 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 1883 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState()); 1884 1885 // Metakey up. 1886 process(mapper, ARBITRARY_TIME + 3, DEVICE_ID, 1887 EV_KEY, KEY_LEFTSHIFT, 0); 1888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1889 ASSERT_EQ(AMETA_NONE, args.metaState); 1890 ASSERT_EQ(AMETA_NONE, mapper->getMetaState()); 1891 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled()); 1892 } 1893 1894 TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) { 1895 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0); 1896 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0); 1897 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0); 1898 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0); 1899 1900 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 1901 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1902 addMapperAndConfigure(mapper); 1903 1904 setDisplayInfoAndReconfigure(DISPLAY_ID, 1905 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1906 DISPLAY_ORIENTATION_90); 1907 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1908 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP)); 1909 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1910 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT)); 1911 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1912 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN)); 1913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1914 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT)); 1915 } 1916 1917 TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) { 1918 mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0); 1919 mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0); 1920 mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0); 1921 mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0); 1922 1923 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 1924 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 1925 addConfigurationProperty("keyboard.orientationAware", "1"); 1926 addMapperAndConfigure(mapper); 1927 1928 setDisplayInfoAndReconfigure(DISPLAY_ID, 1929 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1930 DISPLAY_ORIENTATION_0); 1931 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1932 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP)); 1933 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1934 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT)); 1935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1936 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN)); 1937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1938 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT)); 1939 1940 setDisplayInfoAndReconfigure(DISPLAY_ID, 1941 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1942 DISPLAY_ORIENTATION_90); 1943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1944 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT)); 1945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1946 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP)); 1947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1948 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT)); 1949 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1950 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN)); 1951 1952 setDisplayInfoAndReconfigure(DISPLAY_ID, 1953 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1954 DISPLAY_ORIENTATION_180); 1955 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1956 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN)); 1957 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1958 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT)); 1959 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1960 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP)); 1961 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1962 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT)); 1963 1964 setDisplayInfoAndReconfigure(DISPLAY_ID, 1965 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1966 DISPLAY_ORIENTATION_270); 1967 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1968 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT)); 1969 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1970 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN)); 1971 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1972 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT)); 1973 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, 1974 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP)); 1975 1976 // Special case: if orientation changes while key is down, we still emit the same keycode 1977 // in the key up as we did in the key down. 1978 NotifyKeyArgs args; 1979 1980 setDisplayInfoAndReconfigure(DISPLAY_ID, 1981 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1982 DISPLAY_ORIENTATION_270); 1983 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 1); 1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1985 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 1986 ASSERT_EQ(KEY_UP, args.scanCode); 1987 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode); 1988 1989 setDisplayInfoAndReconfigure(DISPLAY_ID, 1990 DISPLAY_WIDTH, DISPLAY_HEIGHT, 1991 DISPLAY_ORIENTATION_180); 1992 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 0); 1993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 1994 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 1995 ASSERT_EQ(KEY_UP, args.scanCode); 1996 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode); 1997 } 1998 1999 TEST_F(KeyboardInputMapperTest, GetKeyCodeState) { 2000 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 2001 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 2002 addMapperAndConfigure(mapper); 2003 2004 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1); 2005 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A)); 2006 2007 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0); 2008 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A)); 2009 } 2010 2011 TEST_F(KeyboardInputMapperTest, GetScanCodeState) { 2012 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 2013 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 2014 addMapperAndConfigure(mapper); 2015 2016 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1); 2017 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A)); 2018 2019 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0); 2020 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A)); 2021 } 2022 2023 TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) { 2024 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 2025 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 2026 addMapperAndConfigure(mapper); 2027 2028 mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0); 2029 2030 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B }; 2031 uint8_t flags[2] = { 0, 0 }; 2032 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags)); 2033 ASSERT_TRUE(flags[0]); 2034 ASSERT_FALSE(flags[1]); 2035 } 2036 2037 TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) { 2038 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/); 2039 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/); 2040 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/); 2041 mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0); 2042 mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0); 2043 mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0); 2044 2045 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, 2046 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC); 2047 addMapperAndConfigure(mapper); 2048 2049 // Initialization should have turned all of the lights off. 2050 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2051 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2052 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2053 2054 // Toggle caps lock on. 2055 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2056 EV_KEY, KEY_CAPSLOCK, 1); 2057 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2058 EV_KEY, KEY_CAPSLOCK, 0); 2059 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2060 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2061 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2062 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState()); 2063 2064 // Toggle num lock on. 2065 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2066 EV_KEY, KEY_NUMLOCK, 1); 2067 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2068 EV_KEY, KEY_NUMLOCK, 0); 2069 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2070 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2071 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2072 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState()); 2073 2074 // Toggle caps lock off. 2075 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2076 EV_KEY, KEY_CAPSLOCK, 1); 2077 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2078 EV_KEY, KEY_CAPSLOCK, 0); 2079 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2080 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2081 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2082 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState()); 2083 2084 // Toggle scroll lock on. 2085 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2086 EV_KEY, KEY_SCROLLLOCK, 1); 2087 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2088 EV_KEY, KEY_SCROLLLOCK, 0); 2089 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2090 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2091 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2092 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState()); 2093 2094 // Toggle num lock off. 2095 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2096 EV_KEY, KEY_NUMLOCK, 1); 2097 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2098 EV_KEY, KEY_NUMLOCK, 0); 2099 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2100 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2101 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2102 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState()); 2103 2104 // Toggle scroll lock off. 2105 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2106 EV_KEY, KEY_SCROLLLOCK, 1); 2107 process(mapper, ARBITRARY_TIME, DEVICE_ID, 2108 EV_KEY, KEY_SCROLLLOCK, 0); 2109 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL)); 2110 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML)); 2111 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL)); 2112 ASSERT_EQ(AMETA_NONE, mapper->getMetaState()); 2113 } 2114 2115 2116 // --- CursorInputMapperTest --- 2117 2118 class CursorInputMapperTest : public InputMapperTest { 2119 protected: 2120 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD; 2121 2122 sp<FakePointerController> mFakePointerController; 2123 2124 virtual void SetUp() { 2125 InputMapperTest::SetUp(); 2126 2127 mFakePointerController = new FakePointerController(); 2128 mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController); 2129 } 2130 2131 void testMotionRotation(CursorInputMapper* mapper, 2132 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY); 2133 }; 2134 2135 const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6; 2136 2137 void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper, 2138 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) { 2139 NotifyMotionArgs args; 2140 2141 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, originalX); 2142 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, originalY); 2143 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2147 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD, 2148 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 2149 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2150 } 2151 2152 TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) { 2153 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2154 addConfigurationProperty("cursor.mode", "pointer"); 2155 addMapperAndConfigure(mapper); 2156 2157 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources()); 2158 } 2159 2160 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) { 2161 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2162 addConfigurationProperty("cursor.mode", "navigation"); 2163 addMapperAndConfigure(mapper); 2164 2165 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources()); 2166 } 2167 2168 TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) { 2169 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2170 addConfigurationProperty("cursor.mode", "pointer"); 2171 addMapperAndConfigure(mapper); 2172 2173 InputDeviceInfo info; 2174 mapper->populateDeviceInfo(&info); 2175 2176 // Initially there may not be a valid motion range. 2177 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE)); 2178 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE)); 2179 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, 2180 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f)); 2181 2182 // When the bounds are set, then there should be a valid motion range. 2183 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1); 2184 2185 InputDeviceInfo info2; 2186 mapper->populateDeviceInfo(&info2); 2187 2188 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, 2189 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE, 2190 1, 800 - 1, 0.0f, 0.0f)); 2191 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, 2192 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE, 2193 2, 480 - 1, 0.0f, 0.0f)); 2194 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, 2195 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 2196 0.0f, 1.0f, 0.0f, 0.0f)); 2197 } 2198 2199 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) { 2200 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2201 addConfigurationProperty("cursor.mode", "navigation"); 2202 addMapperAndConfigure(mapper); 2203 2204 InputDeviceInfo info; 2205 mapper->populateDeviceInfo(&info); 2206 2207 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, 2208 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL, 2209 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD)); 2210 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, 2211 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL, 2212 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD)); 2213 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, 2214 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL, 2215 0.0f, 1.0f, 0.0f, 0.0f)); 2216 } 2217 2218 TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) { 2219 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2220 addConfigurationProperty("cursor.mode", "navigation"); 2221 addMapperAndConfigure(mapper); 2222 2223 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 2224 2225 NotifyMotionArgs args; 2226 2227 // Button press. 2228 // Mostly testing non x/y behavior here so we don't need to check again elsewhere. 2229 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1); 2230 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2232 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 2233 ASSERT_EQ(DEVICE_ID, args.deviceId); 2234 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source); 2235 ASSERT_EQ(uint32_t(0), args.policyFlags); 2236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action); 2237 ASSERT_EQ(0, args.flags); 2238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 2239 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState); 2240 ASSERT_EQ(0, args.edgeFlags); 2241 ASSERT_EQ(uint32_t(1), args.pointerCount); 2242 ASSERT_EQ(0, args.pointerProperties[0].id); 2243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType); 2244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2245 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2246 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision); 2247 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision); 2248 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 2249 2250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2251 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 2252 ASSERT_EQ(DEVICE_ID, args.deviceId); 2253 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source); 2254 ASSERT_EQ(uint32_t(0), args.policyFlags); 2255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action); 2256 ASSERT_EQ(0, args.flags); 2257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 2258 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState); 2259 ASSERT_EQ(0, args.edgeFlags); 2260 ASSERT_EQ(uint32_t(1), args.pointerCount); 2261 ASSERT_EQ(0, args.pointerProperties[0].id); 2262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType); 2263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2264 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2265 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision); 2266 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision); 2267 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 2268 2269 // Button release. Should have same down time. 2270 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0); 2271 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2273 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime); 2274 ASSERT_EQ(DEVICE_ID, args.deviceId); 2275 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source); 2276 ASSERT_EQ(uint32_t(0), args.policyFlags); 2277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action); 2278 ASSERT_EQ(0, args.flags); 2279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 2280 ASSERT_EQ(0, args.buttonState); 2281 ASSERT_EQ(0, args.edgeFlags); 2282 ASSERT_EQ(uint32_t(1), args.pointerCount); 2283 ASSERT_EQ(0, args.pointerProperties[0].id); 2284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType); 2285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2286 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2287 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision); 2288 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision); 2289 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 2290 2291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2292 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime); 2293 ASSERT_EQ(DEVICE_ID, args.deviceId); 2294 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source); 2295 ASSERT_EQ(uint32_t(0), args.policyFlags); 2296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action); 2297 ASSERT_EQ(0, args.flags); 2298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 2299 ASSERT_EQ(0, args.buttonState); 2300 ASSERT_EQ(0, args.edgeFlags); 2301 ASSERT_EQ(uint32_t(1), args.pointerCount); 2302 ASSERT_EQ(0, args.pointerProperties[0].id); 2303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType); 2304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2305 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2306 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision); 2307 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision); 2308 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 2309 } 2310 2311 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) { 2312 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2313 addConfigurationProperty("cursor.mode", "navigation"); 2314 addMapperAndConfigure(mapper); 2315 2316 NotifyMotionArgs args; 2317 2318 // Motion in X but not Y. 2319 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1); 2320 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2324 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2325 2326 // Motion in Y but not X. 2327 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2); 2328 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2332 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2333 } 2334 2335 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) { 2336 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2337 addConfigurationProperty("cursor.mode", "navigation"); 2338 addMapperAndConfigure(mapper); 2339 2340 NotifyMotionArgs args; 2341 2342 // Button press. 2343 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1); 2344 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action); 2347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2348 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2349 2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action); 2352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2353 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2354 2355 // Button release. 2356 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0); 2357 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action); 2360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2361 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2362 2363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action); 2365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2366 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2367 } 2368 2369 TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) { 2370 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2371 addConfigurationProperty("cursor.mode", "navigation"); 2372 addMapperAndConfigure(mapper); 2373 2374 NotifyMotionArgs args; 2375 2376 // Combined X, Y and Button. 2377 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1); 2378 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2); 2379 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1); 2380 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action); 2383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2384 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 2385 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2386 2387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action); 2389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2390 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 2391 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2392 2393 // Move X, Y a bit while pressed. 2394 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 2); 2395 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 1); 2396 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2400 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 2401 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2402 2403 // Release Button. 2404 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0); 2405 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action); 2408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2409 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2410 2411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action); 2413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2414 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2415 } 2416 2417 TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) { 2418 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2419 addConfigurationProperty("cursor.mode", "navigation"); 2420 addMapperAndConfigure(mapper); 2421 2422 setDisplayInfoAndReconfigure(DISPLAY_ID, 2423 DISPLAY_WIDTH, DISPLAY_HEIGHT, 2424 DISPLAY_ORIENTATION_90); 2425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1)); 2426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1)); 2427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0)); 2428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1)); 2429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1)); 2430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1)); 2431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0)); 2432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1)); 2433 } 2434 2435 TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) { 2436 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2437 addConfigurationProperty("cursor.mode", "navigation"); 2438 addConfigurationProperty("cursor.orientationAware", "1"); 2439 addMapperAndConfigure(mapper); 2440 2441 setDisplayInfoAndReconfigure(DISPLAY_ID, 2442 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0); 2443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1)); 2444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1)); 2445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0)); 2446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1)); 2447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1)); 2448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1)); 2449 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0)); 2450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1)); 2451 2452 setDisplayInfoAndReconfigure(DISPLAY_ID, 2453 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90); 2454 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0)); 2455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1)); 2456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1)); 2457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1)); 2458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0)); 2459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1)); 2460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1)); 2461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1)); 2462 2463 setDisplayInfoAndReconfigure(DISPLAY_ID, 2464 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180); 2465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1)); 2466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1)); 2467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0)); 2468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1)); 2469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1)); 2470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1)); 2471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0)); 2472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1)); 2473 2474 setDisplayInfoAndReconfigure(DISPLAY_ID, 2475 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270); 2476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0)); 2477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1)); 2478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1)); 2479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1)); 2480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0)); 2481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1)); 2482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1)); 2483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1)); 2484 } 2485 2486 TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) { 2487 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2488 addConfigurationProperty("cursor.mode", "pointer"); 2489 addMapperAndConfigure(mapper); 2490 2491 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1); 2492 mFakePointerController->setPosition(100, 200); 2493 mFakePointerController->setButtonState(0); 2494 2495 NotifyMotionArgs motionArgs; 2496 NotifyKeyArgs keyArgs; 2497 2498 // press BTN_LEFT, release BTN_LEFT 2499 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 1); 2500 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2502 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 2503 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 2504 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState()); 2505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2506 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2507 2508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2510 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 2511 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState()); 2512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2513 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2514 2515 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 0); 2516 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2519 ASSERT_EQ(0, motionArgs.buttonState); 2520 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2522 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2523 2524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 2526 ASSERT_EQ(0, motionArgs.buttonState); 2527 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2529 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2530 2531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2533 ASSERT_EQ(0, motionArgs.buttonState); 2534 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2536 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2537 2538 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE 2539 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 1); 2540 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 1); 2541 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 2544 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 2545 motionArgs.buttonState); 2546 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 2547 mFakePointerController->getButtonState()); 2548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2549 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2550 2551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2553 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 2554 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 2555 mFakePointerController->getButtonState()); 2556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2557 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2558 2559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2561 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 2562 motionArgs.buttonState); 2563 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 2564 mFakePointerController->getButtonState()); 2565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2566 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2567 2568 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 0); 2569 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2572 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 2573 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState()); 2574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2575 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2576 2577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 2579 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 2580 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState()); 2581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2582 100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2583 2584 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0); 2585 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2588 ASSERT_EQ(0, motionArgs.buttonState); 2589 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2591 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2592 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0); 2593 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2594 2595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2596 ASSERT_EQ(0, motionArgs.buttonState); 2597 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 2599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2600 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2601 2602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2603 ASSERT_EQ(0, motionArgs.buttonState); 2604 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2607 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2608 2609 // press BTN_BACK, release BTN_BACK 2610 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 1); 2611 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2613 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 2614 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 2615 2616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2618 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 2619 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState()); 2620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2621 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2622 2623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2625 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 2626 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState()); 2627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2628 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2629 2630 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 0); 2631 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2634 ASSERT_EQ(0, motionArgs.buttonState); 2635 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2637 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2638 2639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2641 ASSERT_EQ(0, motionArgs.buttonState); 2642 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2643 2644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2645 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2647 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 2648 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 2649 2650 // press BTN_SIDE, release BTN_SIDE 2651 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 1); 2652 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2654 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 2655 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 2656 2657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 2660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState()); 2661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2662 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2663 2664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 2667 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState()); 2668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2669 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2670 2671 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 0); 2672 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2675 ASSERT_EQ(0, motionArgs.buttonState); 2676 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2678 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2679 2680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2682 ASSERT_EQ(0, motionArgs.buttonState); 2683 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2685 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2686 2687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 2689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 2690 2691 // press BTN_FORWARD, release BTN_FORWARD 2692 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 1); 2693 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 2696 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 2697 2698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2700 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 2701 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState()); 2702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2703 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2704 2705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2707 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 2708 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState()); 2709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2710 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2711 2712 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 0); 2713 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2716 ASSERT_EQ(0, motionArgs.buttonState); 2717 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2719 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2720 2721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2723 ASSERT_EQ(0, motionArgs.buttonState); 2724 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2726 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2727 2728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 2730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 2731 2732 // press BTN_EXTRA, release BTN_EXTRA 2733 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 1); 2734 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 2737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 2738 2739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 2742 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState()); 2743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2744 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2745 2746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 2748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 2749 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState()); 2750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2751 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2752 2753 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 0); 2754 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 2757 ASSERT_EQ(0, motionArgs.buttonState); 2758 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2760 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2761 2762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 2763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 2764 ASSERT_EQ(0, motionArgs.buttonState); 2765 ASSERT_EQ(0, mFakePointerController->getButtonState()); 2766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 2767 100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2768 2769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 2770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 2771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 2772 } 2773 2774 TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) { 2775 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2776 addConfigurationProperty("cursor.mode", "pointer"); 2777 addMapperAndConfigure(mapper); 2778 2779 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1); 2780 mFakePointerController->setPosition(100, 200); 2781 mFakePointerController->setButtonState(0); 2782 2783 NotifyMotionArgs args; 2784 2785 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10); 2786 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20); 2787 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2789 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source); 2790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action); 2791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2792 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2793 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f)); 2794 } 2795 2796 TEST_F(CursorInputMapperTest, Process_PointerCapture) { 2797 CursorInputMapper* mapper = new CursorInputMapper(mDevice); 2798 addConfigurationProperty("cursor.mode", "pointer"); 2799 mFakePolicy->setPointerCapture(true); 2800 addMapperAndConfigure(mapper); 2801 2802 NotifyDeviceResetArgs resetArgs; 2803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs)); 2804 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 2805 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId); 2806 2807 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1); 2808 mFakePointerController->setPosition(100, 200); 2809 mFakePointerController->setButtonState(0); 2810 2811 NotifyMotionArgs args; 2812 2813 // Move. 2814 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10); 2815 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20); 2816 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2818 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2821 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2822 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f)); 2823 2824 // Button press. 2825 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1); 2826 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2828 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2829 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action); 2830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2831 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2833 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action); 2835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2836 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2837 2838 // Button release. 2839 process(mapper, ARBITRARY_TIME + 2, DEVICE_ID, EV_KEY, BTN_MOUSE, 0); 2840 process(mapper, ARBITRARY_TIME + 2, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2842 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action); 2844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2845 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2847 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action); 2849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2850 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2851 2852 // Another move. 2853 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 30); 2854 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 40); 2855 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2857 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source); 2858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); 2859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2860 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2861 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f)); 2862 2863 // Disable pointer capture and check that the device generation got bumped 2864 // and events are generated the usual way. 2865 const uint32_t generation = mFakeContext->getGeneration(); 2866 mFakePolicy->setPointerCapture(false); 2867 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE); 2868 ASSERT_TRUE(mFakeContext->getGeneration() != generation); 2869 2870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs)); 2871 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime); 2872 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId); 2873 2874 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10); 2875 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20); 2876 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 2877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 2878 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source); 2879 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action); 2880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 2881 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); 2882 ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f)); 2883 } 2884 2885 2886 // --- TouchInputMapperTest --- 2887 2888 class TouchInputMapperTest : public InputMapperTest { 2889 protected: 2890 static const int32_t RAW_X_MIN; 2891 static const int32_t RAW_X_MAX; 2892 static const int32_t RAW_Y_MIN; 2893 static const int32_t RAW_Y_MAX; 2894 static const int32_t RAW_TOUCH_MIN; 2895 static const int32_t RAW_TOUCH_MAX; 2896 static const int32_t RAW_TOOL_MIN; 2897 static const int32_t RAW_TOOL_MAX; 2898 static const int32_t RAW_PRESSURE_MIN; 2899 static const int32_t RAW_PRESSURE_MAX; 2900 static const int32_t RAW_ORIENTATION_MIN; 2901 static const int32_t RAW_ORIENTATION_MAX; 2902 static const int32_t RAW_DISTANCE_MIN; 2903 static const int32_t RAW_DISTANCE_MAX; 2904 static const int32_t RAW_TILT_MIN; 2905 static const int32_t RAW_TILT_MAX; 2906 static const int32_t RAW_ID_MIN; 2907 static const int32_t RAW_ID_MAX; 2908 static const int32_t RAW_SLOT_MIN; 2909 static const int32_t RAW_SLOT_MAX; 2910 static const float X_PRECISION; 2911 static const float Y_PRECISION; 2912 static const float X_PRECISION_VIRTUAL; 2913 static const float Y_PRECISION_VIRTUAL; 2914 2915 static const float GEOMETRIC_SCALE; 2916 static const TouchAffineTransformation AFFINE_TRANSFORM; 2917 2918 static const VirtualKeyDefinition VIRTUAL_KEYS[2]; 2919 2920 enum Axes { 2921 POSITION = 1 << 0, 2922 TOUCH = 1 << 1, 2923 TOOL = 1 << 2, 2924 PRESSURE = 1 << 3, 2925 ORIENTATION = 1 << 4, 2926 MINOR = 1 << 5, 2927 ID = 1 << 6, 2928 DISTANCE = 1 << 7, 2929 TILT = 1 << 8, 2930 SLOT = 1 << 9, 2931 TOOL_TYPE = 1 << 10, 2932 }; 2933 2934 void prepareDisplay(int32_t orientation); 2935 void prepareVirtualDisplay(int32_t orientation); 2936 void prepareVirtualKeys(); 2937 void prepareLocationCalibration(); 2938 int32_t toRawX(float displayX); 2939 int32_t toRawY(float displayY); 2940 float toCookedX(float rawX, float rawY); 2941 float toCookedY(float rawX, float rawY); 2942 float toDisplayX(int32_t rawX); 2943 float toDisplayX(int32_t rawX, int32_t displayWidth); 2944 float toDisplayY(int32_t rawY); 2945 float toDisplayY(int32_t rawY, int32_t displayHeight); 2946 2947 }; 2948 2949 const int32_t TouchInputMapperTest::RAW_X_MIN = 25; 2950 const int32_t TouchInputMapperTest::RAW_X_MAX = 1019; 2951 const int32_t TouchInputMapperTest::RAW_Y_MIN = 30; 2952 const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009; 2953 const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0; 2954 const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31; 2955 const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0; 2956 const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15; 2957 const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN; 2958 const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX; 2959 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7; 2960 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7; 2961 const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0; 2962 const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7; 2963 const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0; 2964 const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150; 2965 const int32_t TouchInputMapperTest::RAW_ID_MIN = 0; 2966 const int32_t TouchInputMapperTest::RAW_ID_MAX = 9; 2967 const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0; 2968 const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9; 2969 const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH; 2970 const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT; 2971 const float TouchInputMapperTest::X_PRECISION_VIRTUAL = 2972 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH; 2973 const float TouchInputMapperTest::Y_PRECISION_VIRTUAL = 2974 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT; 2975 const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM = 2976 TouchAffineTransformation(1, -2, 3, -4, 5, -6); 2977 2978 const float TouchInputMapperTest::GEOMETRIC_SCALE = 2979 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1), 2980 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1)); 2981 2982 const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = { 2983 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 }, 2984 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 }, 2985 }; 2986 2987 void TouchInputMapperTest::prepareDisplay(int32_t orientation) { 2988 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation); 2989 } 2990 2991 void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) { 2992 setVirtualDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, 2993 VIRTUAL_DISPLAY_HEIGHT, orientation, String8(VIRTUAL_DISPLAY_UNIQUE_ID)); 2994 } 2995 2996 void TouchInputMapperTest::prepareVirtualKeys() { 2997 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]); 2998 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]); 2999 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE); 3000 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE); 3001 } 3002 3003 void TouchInputMapperTest::prepareLocationCalibration() { 3004 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM); 3005 } 3006 3007 int32_t TouchInputMapperTest::toRawX(float displayX) { 3008 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN); 3009 } 3010 3011 int32_t TouchInputMapperTest::toRawY(float displayY) { 3012 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN); 3013 } 3014 3015 float TouchInputMapperTest::toCookedX(float rawX, float rawY) { 3016 AFFINE_TRANSFORM.applyTo(rawX, rawY); 3017 return rawX; 3018 } 3019 3020 float TouchInputMapperTest::toCookedY(float rawX, float rawY) { 3021 AFFINE_TRANSFORM.applyTo(rawX, rawY); 3022 return rawY; 3023 } 3024 3025 float TouchInputMapperTest::toDisplayX(int32_t rawX) { 3026 return toDisplayX(rawX, DISPLAY_WIDTH); 3027 } 3028 3029 float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) { 3030 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1); 3031 } 3032 3033 float TouchInputMapperTest::toDisplayY(int32_t rawY) { 3034 return toDisplayY(rawY, DISPLAY_HEIGHT); 3035 } 3036 3037 float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) { 3038 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1); 3039 } 3040 3041 3042 // --- SingleTouchInputMapperTest --- 3043 3044 class SingleTouchInputMapperTest : public TouchInputMapperTest { 3045 protected: 3046 void prepareButtons(); 3047 void prepareAxes(int axes); 3048 3049 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y); 3050 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y); 3051 void processUp(SingleTouchInputMapper* mappery); 3052 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure); 3053 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor); 3054 void processDistance(SingleTouchInputMapper* mapper, int32_t distance); 3055 void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY); 3056 void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value); 3057 void processSync(SingleTouchInputMapper* mapper); 3058 }; 3059 3060 void SingleTouchInputMapperTest::prepareButtons() { 3061 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0); 3062 } 3063 3064 void SingleTouchInputMapperTest::prepareAxes(int axes) { 3065 if (axes & POSITION) { 3066 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X, 3067 RAW_X_MIN, RAW_X_MAX, 0, 0); 3068 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y, 3069 RAW_Y_MIN, RAW_Y_MAX, 0, 0); 3070 } 3071 if (axes & PRESSURE) { 3072 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE, 3073 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0); 3074 } 3075 if (axes & TOOL) { 3076 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH, 3077 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0); 3078 } 3079 if (axes & DISTANCE) { 3080 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE, 3081 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0); 3082 } 3083 if (axes & TILT) { 3084 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X, 3085 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0); 3086 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y, 3087 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0); 3088 } 3089 } 3090 3091 void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) { 3092 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 1); 3093 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x); 3094 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y); 3095 } 3096 3097 void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) { 3098 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x); 3099 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y); 3100 } 3101 3102 void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) { 3103 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0); 3104 } 3105 3106 void SingleTouchInputMapperTest::processPressure( 3107 SingleTouchInputMapper* mapper, int32_t pressure) { 3108 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, pressure); 3109 } 3110 3111 void SingleTouchInputMapperTest::processToolMajor( 3112 SingleTouchInputMapper* mapper, int32_t toolMajor) { 3113 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, toolMajor); 3114 } 3115 3116 void SingleTouchInputMapperTest::processDistance( 3117 SingleTouchInputMapper* mapper, int32_t distance) { 3118 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, distance); 3119 } 3120 3121 void SingleTouchInputMapperTest::processTilt( 3122 SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) { 3123 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_X, tiltX); 3124 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_Y, tiltY); 3125 } 3126 3127 void SingleTouchInputMapperTest::processKey( 3128 SingleTouchInputMapper* mapper, int32_t code, int32_t value) { 3129 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value); 3130 } 3131 3132 void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) { 3133 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 3134 } 3135 3136 3137 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) { 3138 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3139 prepareButtons(); 3140 prepareAxes(POSITION); 3141 addMapperAndConfigure(mapper); 3142 3143 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources()); 3144 } 3145 3146 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) { 3147 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3148 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X); 3149 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y); 3150 prepareButtons(); 3151 prepareAxes(POSITION); 3152 addMapperAndConfigure(mapper); 3153 3154 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources()); 3155 } 3156 3157 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) { 3158 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3159 prepareButtons(); 3160 prepareAxes(POSITION); 3161 addConfigurationProperty("touch.deviceType", "touchPad"); 3162 addMapperAndConfigure(mapper); 3163 3164 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources()); 3165 } 3166 3167 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) { 3168 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3169 prepareButtons(); 3170 prepareAxes(POSITION); 3171 addConfigurationProperty("touch.deviceType", "touchScreen"); 3172 addMapperAndConfigure(mapper); 3173 3174 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources()); 3175 } 3176 3177 TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) { 3178 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3179 addConfigurationProperty("touch.deviceType", "touchScreen"); 3180 prepareDisplay(DISPLAY_ORIENTATION_0); 3181 prepareButtons(); 3182 prepareAxes(POSITION); 3183 prepareVirtualKeys(); 3184 addMapperAndConfigure(mapper); 3185 3186 // Unknown key. 3187 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A)); 3188 3189 // Virtual key is down. 3190 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX); 3191 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY); 3192 processDown(mapper, x, y); 3193 processSync(mapper); 3194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled()); 3195 3196 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME)); 3197 3198 // Virtual key is up. 3199 processUp(mapper); 3200 processSync(mapper); 3201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled()); 3202 3203 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME)); 3204 } 3205 3206 TEST_F(SingleTouchInputMapperTest, GetScanCodeState) { 3207 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3208 addConfigurationProperty("touch.deviceType", "touchScreen"); 3209 prepareDisplay(DISPLAY_ORIENTATION_0); 3210 prepareButtons(); 3211 prepareAxes(POSITION); 3212 prepareVirtualKeys(); 3213 addMapperAndConfigure(mapper); 3214 3215 // Unknown key. 3216 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A)); 3217 3218 // Virtual key is down. 3219 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX); 3220 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY); 3221 processDown(mapper, x, y); 3222 processSync(mapper); 3223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled()); 3224 3225 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME)); 3226 3227 // Virtual key is up. 3228 processUp(mapper); 3229 processSync(mapper); 3230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled()); 3231 3232 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME)); 3233 } 3234 3235 TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) { 3236 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3237 addConfigurationProperty("touch.deviceType", "touchScreen"); 3238 prepareDisplay(DISPLAY_ORIENTATION_0); 3239 prepareButtons(); 3240 prepareAxes(POSITION); 3241 prepareVirtualKeys(); 3242 addMapperAndConfigure(mapper); 3243 3244 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A }; 3245 uint8_t flags[2] = { 0, 0 }; 3246 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags)); 3247 ASSERT_TRUE(flags[0]); 3248 ASSERT_FALSE(flags[1]); 3249 } 3250 3251 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) { 3252 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3253 addConfigurationProperty("touch.deviceType", "touchScreen"); 3254 prepareDisplay(DISPLAY_ORIENTATION_0); 3255 prepareButtons(); 3256 prepareAxes(POSITION); 3257 prepareVirtualKeys(); 3258 addMapperAndConfigure(mapper); 3259 3260 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 3261 3262 NotifyKeyArgs args; 3263 3264 // Press virtual key. 3265 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX); 3266 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY); 3267 processDown(mapper, x, y); 3268 processSync(mapper); 3269 3270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 3271 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 3272 ASSERT_EQ(DEVICE_ID, args.deviceId); 3273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 3274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags); 3275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action); 3276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags); 3277 ASSERT_EQ(AKEYCODE_HOME, args.keyCode); 3278 ASSERT_EQ(KEY_HOME, args.scanCode); 3279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 3280 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 3281 3282 // Release virtual key. 3283 processUp(mapper); 3284 processSync(mapper); 3285 3286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args)); 3287 ASSERT_EQ(ARBITRARY_TIME, args.eventTime); 3288 ASSERT_EQ(DEVICE_ID, args.deviceId); 3289 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source); 3290 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags); 3291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action); 3292 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags); 3293 ASSERT_EQ(AKEYCODE_HOME, args.keyCode); 3294 ASSERT_EQ(KEY_HOME, args.scanCode); 3295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState); 3296 ASSERT_EQ(ARBITRARY_TIME, args.downTime); 3297 3298 // Should not have sent any motions. 3299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 3300 } 3301 3302 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) { 3303 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3304 addConfigurationProperty("touch.deviceType", "touchScreen"); 3305 prepareDisplay(DISPLAY_ORIENTATION_0); 3306 prepareButtons(); 3307 prepareAxes(POSITION); 3308 prepareVirtualKeys(); 3309 addMapperAndConfigure(mapper); 3310 3311 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 3312 3313 NotifyKeyArgs keyArgs; 3314 3315 // Press virtual key. 3316 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX); 3317 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY); 3318 processDown(mapper, x, y); 3319 processSync(mapper); 3320 3321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3322 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime); 3323 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId); 3324 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source); 3325 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags); 3326 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 3327 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags); 3328 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode); 3329 ASSERT_EQ(KEY_HOME, keyArgs.scanCode); 3330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState); 3331 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime); 3332 3333 // Move out of bounds. This should generate a cancel and a pointer down since we moved 3334 // into the display area. 3335 y -= 100; 3336 processMove(mapper, x, y); 3337 processSync(mapper); 3338 3339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3340 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime); 3341 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId); 3342 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source); 3343 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags); 3344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 3345 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY 3346 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags); 3347 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode); 3348 ASSERT_EQ(KEY_HOME, keyArgs.scanCode); 3349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState); 3350 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime); 3351 3352 NotifyMotionArgs motionArgs; 3353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3355 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3356 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3357 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3358 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 3359 ASSERT_EQ(0, motionArgs.flags); 3360 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3361 ASSERT_EQ(0, motionArgs.buttonState); 3362 ASSERT_EQ(0, motionArgs.edgeFlags); 3363 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3367 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3368 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3369 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3371 3372 // Keep moving out of bounds. Should generate a pointer move. 3373 y -= 50; 3374 processMove(mapper, x, y); 3375 processSync(mapper); 3376 3377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3383 ASSERT_EQ(0, motionArgs.flags); 3384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3385 ASSERT_EQ(0, motionArgs.buttonState); 3386 ASSERT_EQ(0, motionArgs.edgeFlags); 3387 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3391 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3395 3396 // Release out of bounds. Should generate a pointer up. 3397 processUp(mapper); 3398 processSync(mapper); 3399 3400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 3406 ASSERT_EQ(0, motionArgs.flags); 3407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3408 ASSERT_EQ(0, motionArgs.buttonState); 3409 ASSERT_EQ(0, motionArgs.edgeFlags); 3410 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3414 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3418 3419 // Should not have sent any more keys or motions. 3420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 3421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 3422 } 3423 3424 TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) { 3425 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3426 addConfigurationProperty("touch.deviceType", "touchScreen"); 3427 prepareDisplay(DISPLAY_ORIENTATION_0); 3428 prepareButtons(); 3429 prepareAxes(POSITION); 3430 prepareVirtualKeys(); 3431 addMapperAndConfigure(mapper); 3432 3433 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 3434 3435 NotifyMotionArgs motionArgs; 3436 3437 // Initially go down out of bounds. 3438 int32_t x = -10; 3439 int32_t y = -10; 3440 processDown(mapper, x, y); 3441 processSync(mapper); 3442 3443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 3444 3445 // Move into the display area. Should generate a pointer down. 3446 x = 50; 3447 y = 75; 3448 processMove(mapper, x, y); 3449 processSync(mapper); 3450 3451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3456 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 3457 ASSERT_EQ(0, motionArgs.flags); 3458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3459 ASSERT_EQ(0, motionArgs.buttonState); 3460 ASSERT_EQ(0, motionArgs.edgeFlags); 3461 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3465 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3469 3470 // Release. Should generate a pointer up. 3471 processUp(mapper); 3472 processSync(mapper); 3473 3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3479 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 3480 ASSERT_EQ(0, motionArgs.flags); 3481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3482 ASSERT_EQ(0, motionArgs.buttonState); 3483 ASSERT_EQ(0, motionArgs.edgeFlags); 3484 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3488 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3492 3493 // Should not have sent any more keys or motions. 3494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 3495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 3496 } 3497 3498 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) { 3499 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3500 addConfigurationProperty("touch.deviceType", "touchScreen"); 3501 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID); 3502 3503 prepareVirtualDisplay(DISPLAY_ORIENTATION_0); 3504 prepareButtons(); 3505 prepareAxes(POSITION); 3506 prepareVirtualKeys(); 3507 addMapperAndConfigure(mapper); 3508 3509 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 3510 3511 NotifyMotionArgs motionArgs; 3512 3513 // Down. 3514 int32_t x = 100; 3515 int32_t y = 125; 3516 processDown(mapper, x, y); 3517 processSync(mapper); 3518 3519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3520 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3521 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3522 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId); 3523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 3526 ASSERT_EQ(0, motionArgs.flags); 3527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3528 ASSERT_EQ(0, motionArgs.buttonState); 3529 ASSERT_EQ(0, motionArgs.edgeFlags); 3530 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3534 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT), 3535 1, 0, 0, 0, 0, 0, 0, 0)); 3536 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON); 3537 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON); 3538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3539 3540 // Move. 3541 x += 50; 3542 y += 75; 3543 processMove(mapper, x, y); 3544 processSync(mapper); 3545 3546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3548 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3549 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId); 3550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3551 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3553 ASSERT_EQ(0, motionArgs.flags); 3554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3555 ASSERT_EQ(0, motionArgs.buttonState); 3556 ASSERT_EQ(0, motionArgs.edgeFlags); 3557 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3561 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT), 3562 1, 0, 0, 0, 0, 0, 0, 0)); 3563 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON); 3564 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON); 3565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3566 3567 // Up. 3568 processUp(mapper); 3569 processSync(mapper); 3570 3571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3573 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3574 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId); 3575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3576 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 3578 ASSERT_EQ(0, motionArgs.flags); 3579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3580 ASSERT_EQ(0, motionArgs.buttonState); 3581 ASSERT_EQ(0, motionArgs.edgeFlags); 3582 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3586 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT), 3587 1, 0, 0, 0, 0, 0, 0, 0)); 3588 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON); 3589 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON); 3590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3591 3592 // Should not have sent any more keys or motions. 3593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 3594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 3595 } 3596 3597 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) { 3598 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3599 addConfigurationProperty("touch.deviceType", "touchScreen"); 3600 prepareDisplay(DISPLAY_ORIENTATION_0); 3601 prepareButtons(); 3602 prepareAxes(POSITION); 3603 prepareVirtualKeys(); 3604 addMapperAndConfigure(mapper); 3605 3606 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 3607 3608 NotifyMotionArgs motionArgs; 3609 3610 // Down. 3611 int32_t x = 100; 3612 int32_t y = 125; 3613 processDown(mapper, x, y); 3614 processSync(mapper); 3615 3616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3618 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3619 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3620 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3621 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 3622 ASSERT_EQ(0, motionArgs.flags); 3623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3624 ASSERT_EQ(0, motionArgs.buttonState); 3625 ASSERT_EQ(0, motionArgs.edgeFlags); 3626 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3630 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3634 3635 // Move. 3636 x += 50; 3637 y += 75; 3638 processMove(mapper, x, y); 3639 processSync(mapper); 3640 3641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3647 ASSERT_EQ(0, motionArgs.flags); 3648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3649 ASSERT_EQ(0, motionArgs.buttonState); 3650 ASSERT_EQ(0, motionArgs.edgeFlags); 3651 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3659 3660 // Up. 3661 processUp(mapper); 3662 processSync(mapper); 3663 3664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 3666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 3667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 3668 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 3669 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 3670 ASSERT_EQ(0, motionArgs.flags); 3671 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 3672 ASSERT_EQ(0, motionArgs.buttonState); 3673 ASSERT_EQ(0, motionArgs.edgeFlags); 3674 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 3675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 3676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 3677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 3678 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0)); 3679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 3680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 3681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 3682 3683 // Should not have sent any more keys or motions. 3684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 3685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 3686 } 3687 3688 TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) { 3689 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3690 addConfigurationProperty("touch.deviceType", "touchScreen"); 3691 prepareButtons(); 3692 prepareAxes(POSITION); 3693 addConfigurationProperty("touch.orientationAware", "0"); 3694 addMapperAndConfigure(mapper); 3695 3696 NotifyMotionArgs args; 3697 3698 // Rotation 90. 3699 prepareDisplay(DISPLAY_ORIENTATION_90); 3700 processDown(mapper, toRawX(50), toRawY(75)); 3701 processSync(mapper); 3702 3703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3704 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1); 3705 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 3706 3707 processUp(mapper); 3708 processSync(mapper); 3709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled()); 3710 } 3711 3712 TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) { 3713 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3714 addConfigurationProperty("touch.deviceType", "touchScreen"); 3715 prepareButtons(); 3716 prepareAxes(POSITION); 3717 addMapperAndConfigure(mapper); 3718 3719 NotifyMotionArgs args; 3720 3721 // Rotation 0. 3722 prepareDisplay(DISPLAY_ORIENTATION_0); 3723 processDown(mapper, toRawX(50), toRawY(75)); 3724 processSync(mapper); 3725 3726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3727 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1); 3728 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 3729 3730 processUp(mapper); 3731 processSync(mapper); 3732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled()); 3733 3734 // Rotation 90. 3735 prepareDisplay(DISPLAY_ORIENTATION_90); 3736 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50)); 3737 processSync(mapper); 3738 3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3740 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1); 3741 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 3742 3743 processUp(mapper); 3744 processSync(mapper); 3745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled()); 3746 3747 // Rotation 180. 3748 prepareDisplay(DISPLAY_ORIENTATION_180); 3749 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN); 3750 processSync(mapper); 3751 3752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3753 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1); 3754 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 3755 3756 processUp(mapper); 3757 processSync(mapper); 3758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled()); 3759 3760 // Rotation 270. 3761 prepareDisplay(DISPLAY_ORIENTATION_270); 3762 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN); 3763 processSync(mapper); 3764 3765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3766 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1); 3767 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1); 3768 3769 processUp(mapper); 3770 processSync(mapper); 3771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled()); 3772 } 3773 3774 TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) { 3775 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3776 addConfigurationProperty("touch.deviceType", "touchScreen"); 3777 prepareDisplay(DISPLAY_ORIENTATION_0); 3778 prepareButtons(); 3779 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT); 3780 addMapperAndConfigure(mapper); 3781 3782 // These calculations are based on the input device calibration documentation. 3783 int32_t rawX = 100; 3784 int32_t rawY = 200; 3785 int32_t rawPressure = 10; 3786 int32_t rawToolMajor = 12; 3787 int32_t rawDistance = 2; 3788 int32_t rawTiltX = 30; 3789 int32_t rawTiltY = 110; 3790 3791 float x = toDisplayX(rawX); 3792 float y = toDisplayY(rawY); 3793 float pressure = float(rawPressure) / RAW_PRESSURE_MAX; 3794 float size = float(rawToolMajor) / RAW_TOOL_MAX; 3795 float tool = float(rawToolMajor) * GEOMETRIC_SCALE; 3796 float distance = float(rawDistance); 3797 3798 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f; 3799 float tiltScale = M_PI / 180; 3800 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale; 3801 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale; 3802 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); 3803 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); 3804 3805 processDown(mapper, rawX, rawY); 3806 processPressure(mapper, rawPressure); 3807 processToolMajor(mapper, rawToolMajor); 3808 processDistance(mapper, rawDistance); 3809 processTilt(mapper, rawTiltX, rawTiltY); 3810 processSync(mapper); 3811 3812 NotifyMotionArgs args; 3813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 3815 x, y, pressure, size, tool, tool, tool, tool, orientation, distance)); 3816 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT)); 3817 } 3818 3819 TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) { 3820 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3821 addConfigurationProperty("touch.deviceType", "touchScreen"); 3822 prepareDisplay(DISPLAY_ORIENTATION_0); 3823 prepareLocationCalibration(); 3824 prepareButtons(); 3825 prepareAxes(POSITION); 3826 addMapperAndConfigure(mapper); 3827 3828 int32_t rawX = 100; 3829 int32_t rawY = 200; 3830 3831 float x = toDisplayX(toCookedX(rawX, rawY)); 3832 float y = toDisplayY(toCookedY(rawX, rawY)); 3833 3834 processDown(mapper, rawX, rawY); 3835 processSync(mapper); 3836 3837 NotifyMotionArgs args; 3838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 3839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 3840 x, y, 1, 0, 0, 0, 0, 0, 0, 0)); 3841 } 3842 3843 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) { 3844 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 3845 addConfigurationProperty("touch.deviceType", "touchScreen"); 3846 prepareDisplay(DISPLAY_ORIENTATION_0); 3847 prepareButtons(); 3848 prepareAxes(POSITION); 3849 addMapperAndConfigure(mapper); 3850 3851 NotifyMotionArgs motionArgs; 3852 NotifyKeyArgs keyArgs; 3853 3854 processDown(mapper, 100, 200); 3855 processSync(mapper); 3856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 3858 ASSERT_EQ(0, motionArgs.buttonState); 3859 3860 // press BTN_LEFT, release BTN_LEFT 3861 processKey(mapper, BTN_LEFT, 1); 3862 processSync(mapper); 3863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3865 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 3866 3867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3869 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 3870 3871 processKey(mapper, BTN_LEFT, 0); 3872 processSync(mapper); 3873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3875 ASSERT_EQ(0, motionArgs.buttonState); 3876 3877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3879 ASSERT_EQ(0, motionArgs.buttonState); 3880 3881 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE 3882 processKey(mapper, BTN_RIGHT, 1); 3883 processKey(mapper, BTN_MIDDLE, 1); 3884 processSync(mapper); 3885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3887 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 3888 motionArgs.buttonState); 3889 3890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3892 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 3893 3894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3896 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 3897 motionArgs.buttonState); 3898 3899 processKey(mapper, BTN_RIGHT, 0); 3900 processSync(mapper); 3901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3903 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 3904 3905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3907 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 3908 3909 processKey(mapper, BTN_MIDDLE, 0); 3910 processSync(mapper); 3911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3913 ASSERT_EQ(0, motionArgs.buttonState); 3914 3915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3917 ASSERT_EQ(0, motionArgs.buttonState); 3918 3919 // press BTN_BACK, release BTN_BACK 3920 processKey(mapper, BTN_BACK, 1); 3921 processSync(mapper); 3922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 3924 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 3925 3926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3928 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 3929 3930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3931 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3932 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 3933 3934 processKey(mapper, BTN_BACK, 0); 3935 processSync(mapper); 3936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3938 ASSERT_EQ(0, motionArgs.buttonState); 3939 3940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3942 ASSERT_EQ(0, motionArgs.buttonState); 3943 3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3945 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 3946 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 3947 3948 // press BTN_SIDE, release BTN_SIDE 3949 processKey(mapper, BTN_SIDE, 1); 3950 processSync(mapper); 3951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 3953 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 3954 3955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3957 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 3958 3959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3961 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 3962 3963 processKey(mapper, BTN_SIDE, 0); 3964 processSync(mapper); 3965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3967 ASSERT_EQ(0, motionArgs.buttonState); 3968 3969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3971 ASSERT_EQ(0, motionArgs.buttonState); 3972 3973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3974 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 3975 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 3976 3977 // press BTN_FORWARD, release BTN_FORWARD 3978 processKey(mapper, BTN_FORWARD, 1); 3979 processSync(mapper); 3980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 3981 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 3982 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 3983 3984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 3986 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 3987 3988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 3990 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 3991 3992 processKey(mapper, BTN_FORWARD, 0); 3993 processSync(mapper); 3994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 3996 ASSERT_EQ(0, motionArgs.buttonState); 3997 3998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 3999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4000 ASSERT_EQ(0, motionArgs.buttonState); 4001 4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 4003 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 4004 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 4005 4006 // press BTN_EXTRA, release BTN_EXTRA 4007 processKey(mapper, BTN_EXTRA, 1); 4008 processSync(mapper); 4009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 4010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 4011 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 4012 4013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4015 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 4016 4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 4019 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 4020 4021 processKey(mapper, BTN_EXTRA, 0); 4022 processSync(mapper); 4023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 4025 ASSERT_EQ(0, motionArgs.buttonState); 4026 4027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4029 ASSERT_EQ(0, motionArgs.buttonState); 4030 4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 4032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 4033 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 4034 4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 4036 4037 // press BTN_STYLUS, release BTN_STYLUS 4038 processKey(mapper, BTN_STYLUS, 1); 4039 processSync(mapper); 4040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4042 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState); 4043 4044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 4046 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState); 4047 4048 processKey(mapper, BTN_STYLUS, 0); 4049 processSync(mapper); 4050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 4052 ASSERT_EQ(0, motionArgs.buttonState); 4053 4054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4056 ASSERT_EQ(0, motionArgs.buttonState); 4057 4058 // press BTN_STYLUS2, release BTN_STYLUS2 4059 processKey(mapper, BTN_STYLUS2, 1); 4060 processSync(mapper); 4061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4063 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState); 4064 4065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 4067 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState); 4068 4069 processKey(mapper, BTN_STYLUS2, 0); 4070 processSync(mapper); 4071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 4073 ASSERT_EQ(0, motionArgs.buttonState); 4074 4075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4077 ASSERT_EQ(0, motionArgs.buttonState); 4078 4079 // release touch 4080 processUp(mapper); 4081 processSync(mapper); 4082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4083 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 4084 ASSERT_EQ(0, motionArgs.buttonState); 4085 } 4086 4087 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) { 4088 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 4089 addConfigurationProperty("touch.deviceType", "touchScreen"); 4090 prepareDisplay(DISPLAY_ORIENTATION_0); 4091 prepareButtons(); 4092 prepareAxes(POSITION); 4093 addMapperAndConfigure(mapper); 4094 4095 NotifyMotionArgs motionArgs; 4096 4097 // default tool type is finger 4098 processDown(mapper, 100, 200); 4099 processSync(mapper); 4100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4103 4104 // eraser 4105 processKey(mapper, BTN_TOOL_RUBBER, 1); 4106 processSync(mapper); 4107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType); 4110 4111 // stylus 4112 processKey(mapper, BTN_TOOL_RUBBER, 0); 4113 processKey(mapper, BTN_TOOL_PEN, 1); 4114 processSync(mapper); 4115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 4118 4119 // brush 4120 processKey(mapper, BTN_TOOL_PEN, 0); 4121 processKey(mapper, BTN_TOOL_BRUSH, 1); 4122 processSync(mapper); 4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 4126 4127 // pencil 4128 processKey(mapper, BTN_TOOL_BRUSH, 0); 4129 processKey(mapper, BTN_TOOL_PENCIL, 1); 4130 processSync(mapper); 4131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 4134 4135 // airbrush 4136 processKey(mapper, BTN_TOOL_PENCIL, 0); 4137 processKey(mapper, BTN_TOOL_AIRBRUSH, 1); 4138 processSync(mapper); 4139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 4142 4143 // mouse 4144 processKey(mapper, BTN_TOOL_AIRBRUSH, 0); 4145 processKey(mapper, BTN_TOOL_MOUSE, 1); 4146 processSync(mapper); 4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 4150 4151 // lens 4152 processKey(mapper, BTN_TOOL_MOUSE, 0); 4153 processKey(mapper, BTN_TOOL_LENS, 1); 4154 processSync(mapper); 4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 4158 4159 // double-tap 4160 processKey(mapper, BTN_TOOL_LENS, 0); 4161 processKey(mapper, BTN_TOOL_DOUBLETAP, 1); 4162 processSync(mapper); 4163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4166 4167 // triple-tap 4168 processKey(mapper, BTN_TOOL_DOUBLETAP, 0); 4169 processKey(mapper, BTN_TOOL_TRIPLETAP, 1); 4170 processSync(mapper); 4171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4174 4175 // quad-tap 4176 processKey(mapper, BTN_TOOL_TRIPLETAP, 0); 4177 processKey(mapper, BTN_TOOL_QUADTAP, 1); 4178 processSync(mapper); 4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4182 4183 // finger 4184 processKey(mapper, BTN_TOOL_QUADTAP, 0); 4185 processKey(mapper, BTN_TOOL_FINGER, 1); 4186 processSync(mapper); 4187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4190 4191 // stylus trumps finger 4192 processKey(mapper, BTN_TOOL_PEN, 1); 4193 processSync(mapper); 4194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 4197 4198 // eraser trumps stylus 4199 processKey(mapper, BTN_TOOL_RUBBER, 1); 4200 processSync(mapper); 4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType); 4204 4205 // mouse trumps eraser 4206 processKey(mapper, BTN_TOOL_MOUSE, 1); 4207 processSync(mapper); 4208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 4211 4212 // back to default tool type 4213 processKey(mapper, BTN_TOOL_MOUSE, 0); 4214 processKey(mapper, BTN_TOOL_RUBBER, 0); 4215 processKey(mapper, BTN_TOOL_PEN, 0); 4216 processKey(mapper, BTN_TOOL_FINGER, 0); 4217 processSync(mapper); 4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4221 } 4222 4223 TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) { 4224 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 4225 addConfigurationProperty("touch.deviceType", "touchScreen"); 4226 prepareDisplay(DISPLAY_ORIENTATION_0); 4227 prepareButtons(); 4228 prepareAxes(POSITION); 4229 mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0); 4230 addMapperAndConfigure(mapper); 4231 4232 NotifyMotionArgs motionArgs; 4233 4234 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0 4235 processKey(mapper, BTN_TOOL_FINGER, 1); 4236 processMove(mapper, 100, 200); 4237 processSync(mapper); 4238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 4240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4241 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 4242 4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4246 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 4247 4248 // move a little 4249 processMove(mapper, 150, 250); 4250 processSync(mapper); 4251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4254 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4255 4256 // down when BTN_TOUCH is pressed, pressure defaults to 1 4257 processKey(mapper, BTN_TOUCH, 1); 4258 processSync(mapper); 4259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 4261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4262 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4263 4264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4267 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 4268 4269 // up when BTN_TOUCH is released, hover restored 4270 processKey(mapper, BTN_TOUCH, 0); 4271 processSync(mapper); 4272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4273 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 4274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4275 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 4276 4277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 4279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4280 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4281 4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4286 4287 // exit hover when pointer goes away 4288 processKey(mapper, BTN_TOOL_FINGER, 0); 4289 processSync(mapper); 4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 4292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4293 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4294 } 4295 4296 TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) { 4297 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice); 4298 addConfigurationProperty("touch.deviceType", "touchScreen"); 4299 prepareDisplay(DISPLAY_ORIENTATION_0); 4300 prepareButtons(); 4301 prepareAxes(POSITION | PRESSURE); 4302 addMapperAndConfigure(mapper); 4303 4304 NotifyMotionArgs motionArgs; 4305 4306 // initially hovering because pressure is 0 4307 processDown(mapper, 100, 200); 4308 processPressure(mapper, 0); 4309 processSync(mapper); 4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 4312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4313 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 4314 4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4318 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 4319 4320 // move a little 4321 processMove(mapper, 150, 250); 4322 processSync(mapper); 4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4326 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4327 4328 // down when pressure is non-zero 4329 processPressure(mapper, RAW_PRESSURE_MAX); 4330 processSync(mapper); 4331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 4333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4334 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4335 4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4339 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 4340 4341 // up when pressure becomes 0, hover restored 4342 processPressure(mapper, 0); 4343 processSync(mapper); 4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4345 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 4346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4347 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 4348 4349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 4351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4352 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4353 4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 4356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4357 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4358 4359 // exit hover when pointer goes away 4360 processUp(mapper); 4361 processSync(mapper); 4362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 4364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4365 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 4366 } 4367 4368 4369 // --- MultiTouchInputMapperTest --- 4370 4371 class MultiTouchInputMapperTest : public TouchInputMapperTest { 4372 protected: 4373 void prepareAxes(int axes); 4374 4375 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y); 4376 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor); 4377 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor); 4378 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor); 4379 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor); 4380 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation); 4381 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure); 4382 void processDistance(MultiTouchInputMapper* mapper, int32_t distance); 4383 void processId(MultiTouchInputMapper* mapper, int32_t id); 4384 void processSlot(MultiTouchInputMapper* mapper, int32_t slot); 4385 void processToolType(MultiTouchInputMapper* mapper, int32_t toolType); 4386 void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value); 4387 void processMTSync(MultiTouchInputMapper* mapper); 4388 void processSync(MultiTouchInputMapper* mapper); 4389 }; 4390 4391 void MultiTouchInputMapperTest::prepareAxes(int axes) { 4392 if (axes & POSITION) { 4393 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X, 4394 RAW_X_MIN, RAW_X_MAX, 0, 0); 4395 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y, 4396 RAW_Y_MIN, RAW_Y_MAX, 0, 0); 4397 } 4398 if (axes & TOUCH) { 4399 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR, 4400 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0); 4401 if (axes & MINOR) { 4402 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR, 4403 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0); 4404 } 4405 } 4406 if (axes & TOOL) { 4407 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR, 4408 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0); 4409 if (axes & MINOR) { 4410 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR, 4411 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0); 4412 } 4413 } 4414 if (axes & ORIENTATION) { 4415 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION, 4416 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0); 4417 } 4418 if (axes & PRESSURE) { 4419 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE, 4420 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0); 4421 } 4422 if (axes & DISTANCE) { 4423 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE, 4424 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0); 4425 } 4426 if (axes & ID) { 4427 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID, 4428 RAW_ID_MIN, RAW_ID_MAX, 0, 0); 4429 } 4430 if (axes & SLOT) { 4431 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT, 4432 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0); 4433 mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0); 4434 } 4435 if (axes & TOOL_TYPE) { 4436 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE, 4437 0, MT_TOOL_MAX, 0, 0); 4438 } 4439 } 4440 4441 void MultiTouchInputMapperTest::processPosition( 4442 MultiTouchInputMapper* mapper, int32_t x, int32_t y) { 4443 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, x); 4444 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, y); 4445 } 4446 4447 void MultiTouchInputMapperTest::processTouchMajor( 4448 MultiTouchInputMapper* mapper, int32_t touchMajor) { 4449 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor); 4450 } 4451 4452 void MultiTouchInputMapperTest::processTouchMinor( 4453 MultiTouchInputMapper* mapper, int32_t touchMinor) { 4454 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor); 4455 } 4456 4457 void MultiTouchInputMapperTest::processToolMajor( 4458 MultiTouchInputMapper* mapper, int32_t toolMajor) { 4459 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor); 4460 } 4461 4462 void MultiTouchInputMapperTest::processToolMinor( 4463 MultiTouchInputMapper* mapper, int32_t toolMinor) { 4464 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor); 4465 } 4466 4467 void MultiTouchInputMapperTest::processOrientation( 4468 MultiTouchInputMapper* mapper, int32_t orientation) { 4469 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, orientation); 4470 } 4471 4472 void MultiTouchInputMapperTest::processPressure( 4473 MultiTouchInputMapper* mapper, int32_t pressure) { 4474 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, pressure); 4475 } 4476 4477 void MultiTouchInputMapperTest::processDistance( 4478 MultiTouchInputMapper* mapper, int32_t distance) { 4479 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_DISTANCE, distance); 4480 } 4481 4482 void MultiTouchInputMapperTest::processId( 4483 MultiTouchInputMapper* mapper, int32_t id) { 4484 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, id); 4485 } 4486 4487 void MultiTouchInputMapperTest::processSlot( 4488 MultiTouchInputMapper* mapper, int32_t slot) { 4489 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_SLOT, slot); 4490 } 4491 4492 void MultiTouchInputMapperTest::processToolType( 4493 MultiTouchInputMapper* mapper, int32_t toolType) { 4494 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOOL_TYPE, toolType); 4495 } 4496 4497 void MultiTouchInputMapperTest::processKey( 4498 MultiTouchInputMapper* mapper, int32_t code, int32_t value) { 4499 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value); 4500 } 4501 4502 void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) { 4503 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0); 4504 } 4505 4506 void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) { 4507 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0); 4508 } 4509 4510 4511 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) { 4512 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 4513 addConfigurationProperty("touch.deviceType", "touchScreen"); 4514 prepareDisplay(DISPLAY_ORIENTATION_0); 4515 prepareAxes(POSITION); 4516 prepareVirtualKeys(); 4517 addMapperAndConfigure(mapper); 4518 4519 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 4520 4521 NotifyMotionArgs motionArgs; 4522 4523 // Two fingers down at once. 4524 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500; 4525 processPosition(mapper, x1, y1); 4526 processMTSync(mapper); 4527 processPosition(mapper, x2, y2); 4528 processMTSync(mapper); 4529 processSync(mapper); 4530 4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4537 ASSERT_EQ(0, motionArgs.flags); 4538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4539 ASSERT_EQ(0, motionArgs.buttonState); 4540 ASSERT_EQ(0, motionArgs.edgeFlags); 4541 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4545 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4546 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4547 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4549 4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4552 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4553 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4554 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4555 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4556 motionArgs.action); 4557 ASSERT_EQ(0, motionArgs.flags); 4558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4559 ASSERT_EQ(0, motionArgs.buttonState); 4560 ASSERT_EQ(0, motionArgs.edgeFlags); 4561 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4564 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4569 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4570 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4571 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4573 4574 // Move. 4575 x1 += 10; y1 += 15; x2 += 5; y2 -= 10; 4576 processPosition(mapper, x1, y1); 4577 processMTSync(mapper); 4578 processPosition(mapper, x2, y2); 4579 processMTSync(mapper); 4580 processSync(mapper); 4581 4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4583 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4584 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4585 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4586 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4587 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4588 ASSERT_EQ(0, motionArgs.flags); 4589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4590 ASSERT_EQ(0, motionArgs.buttonState); 4591 ASSERT_EQ(0, motionArgs.edgeFlags); 4592 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4595 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4598 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4601 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4602 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4603 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4604 4605 // First finger up. 4606 x2 += 15; y2 -= 20; 4607 processPosition(mapper, x2, y2); 4608 processMTSync(mapper); 4609 processSync(mapper); 4610 4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4613 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4615 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4616 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4617 motionArgs.action); 4618 ASSERT_EQ(0, motionArgs.flags); 4619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4620 ASSERT_EQ(0, motionArgs.buttonState); 4621 ASSERT_EQ(0, motionArgs.edgeFlags); 4622 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4625 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4628 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4634 4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4637 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4639 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4641 ASSERT_EQ(0, motionArgs.flags); 4642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4643 ASSERT_EQ(0, motionArgs.buttonState); 4644 ASSERT_EQ(0, motionArgs.edgeFlags); 4645 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4646 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 4647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4650 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4651 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4653 4654 // Move. 4655 x2 += 20; y2 -= 25; 4656 processPosition(mapper, x2, y2); 4657 processMTSync(mapper); 4658 processSync(mapper); 4659 4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4666 ASSERT_EQ(0, motionArgs.flags); 4667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4668 ASSERT_EQ(0, motionArgs.buttonState); 4669 ASSERT_EQ(0, motionArgs.edgeFlags); 4670 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4671 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 4672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4674 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4675 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4676 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4678 4679 // New finger down. 4680 int32_t x3 = 700, y3 = 300; 4681 processPosition(mapper, x2, y2); 4682 processMTSync(mapper); 4683 processPosition(mapper, x3, y3); 4684 processMTSync(mapper); 4685 processSync(mapper); 4686 4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4692 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4693 motionArgs.action); 4694 ASSERT_EQ(0, motionArgs.flags); 4695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4696 ASSERT_EQ(0, motionArgs.buttonState); 4697 ASSERT_EQ(0, motionArgs.edgeFlags); 4698 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4701 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4704 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4707 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4708 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4710 4711 // Second finger up. 4712 x3 += 30; y3 -= 20; 4713 processPosition(mapper, x3, y3); 4714 processMTSync(mapper); 4715 processSync(mapper); 4716 4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4719 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4720 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4721 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4722 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4723 motionArgs.action); 4724 ASSERT_EQ(0, motionArgs.flags); 4725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4726 ASSERT_EQ(0, motionArgs.buttonState); 4727 ASSERT_EQ(0, motionArgs.edgeFlags); 4728 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4731 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4734 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4736 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4737 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4738 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4740 4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4742 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4743 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4744 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4745 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4747 ASSERT_EQ(0, motionArgs.flags); 4748 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4749 ASSERT_EQ(0, motionArgs.buttonState); 4750 ASSERT_EQ(0, motionArgs.edgeFlags); 4751 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4755 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4756 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4757 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4759 4760 // Last finger up. 4761 processMTSync(mapper); 4762 processSync(mapper); 4763 4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime); 4766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId); 4767 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source); 4768 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags); 4769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 4770 ASSERT_EQ(0, motionArgs.flags); 4771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState); 4772 ASSERT_EQ(0, motionArgs.buttonState); 4773 ASSERT_EQ(0, motionArgs.edgeFlags); 4774 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4778 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON); 4780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON); 4781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime); 4782 4783 // Should not have sent any more keys or motions. 4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 4786 } 4787 4788 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) { 4789 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 4790 addConfigurationProperty("touch.deviceType", "touchScreen"); 4791 prepareDisplay(DISPLAY_ORIENTATION_0); 4792 prepareAxes(POSITION | ID); 4793 prepareVirtualKeys(); 4794 addMapperAndConfigure(mapper); 4795 4796 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 4797 4798 NotifyMotionArgs motionArgs; 4799 4800 // Two fingers down at once. 4801 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500; 4802 processPosition(mapper, x1, y1); 4803 processId(mapper, 1); 4804 processMTSync(mapper); 4805 processPosition(mapper, x2, y2); 4806 processId(mapper, 2); 4807 processMTSync(mapper); 4808 processSync(mapper); 4809 4810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4812 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4816 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4817 4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4819 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4820 motionArgs.action); 4821 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4824 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4827 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4829 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4830 4831 // Move. 4832 x1 += 10; y1 += 15; x2 += 5; y2 -= 10; 4833 processPosition(mapper, x1, y1); 4834 processId(mapper, 1); 4835 processMTSync(mapper); 4836 processPosition(mapper, x2, y2); 4837 processId(mapper, 2); 4838 processMTSync(mapper); 4839 processSync(mapper); 4840 4841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4843 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4846 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4849 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4851 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4852 4853 // First finger up. 4854 x2 += 15; y2 -= 20; 4855 processPosition(mapper, x2, y2); 4856 processId(mapper, 2); 4857 processMTSync(mapper); 4858 processSync(mapper); 4859 4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4861 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4862 motionArgs.action); 4863 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4866 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4869 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4872 4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4875 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4876 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 4877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4879 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4880 4881 // Move. 4882 x2 += 20; y2 -= 25; 4883 processPosition(mapper, x2, y2); 4884 processId(mapper, 2); 4885 processMTSync(mapper); 4886 processSync(mapper); 4887 4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4890 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4891 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 4892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4894 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4895 4896 // New finger down. 4897 int32_t x3 = 700, y3 = 300; 4898 processPosition(mapper, x2, y2); 4899 processId(mapper, 2); 4900 processMTSync(mapper); 4901 processPosition(mapper, x3, y3); 4902 processId(mapper, 3); 4903 processMTSync(mapper); 4904 processSync(mapper); 4905 4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4907 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4908 motionArgs.action); 4909 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4910 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4912 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4915 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4917 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4918 4919 // Second finger up. 4920 x3 += 30; y3 -= 20; 4921 processPosition(mapper, x3, y3); 4922 processId(mapper, 3); 4923 processMTSync(mapper); 4924 processSync(mapper); 4925 4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4927 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4928 motionArgs.action); 4929 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4932 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 4933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 4934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4935 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 4937 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 4938 4939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 4941 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4945 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4946 4947 // Last finger up. 4948 processMTSync(mapper); 4949 processSync(mapper); 4950 4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4952 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 4953 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4957 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 4958 4959 // Should not have sent any more keys or motions. 4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 4961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 4962 } 4963 4964 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) { 4965 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 4966 addConfigurationProperty("touch.deviceType", "touchScreen"); 4967 prepareDisplay(DISPLAY_ORIENTATION_0); 4968 prepareAxes(POSITION | ID | SLOT); 4969 prepareVirtualKeys(); 4970 addMapperAndConfigure(mapper); 4971 4972 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON); 4973 4974 NotifyMotionArgs motionArgs; 4975 4976 // Two fingers down at once. 4977 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500; 4978 processPosition(mapper, x1, y1); 4979 processId(mapper, 1); 4980 processSlot(mapper, 1); 4981 processPosition(mapper, x2, y2); 4982 processId(mapper, 2); 4983 processSync(mapper); 4984 4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 4987 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 4988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 4991 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 4992 4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 4994 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 4995 motionArgs.action); 4996 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 4997 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 4998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 4999 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 5000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 5001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5002 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 5003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 5004 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5005 5006 // Move. 5007 x1 += 10; y1 += 15; x2 += 5; y2 -= 10; 5008 processSlot(mapper, 0); 5009 processPosition(mapper, x1, y1); 5010 processSlot(mapper, 1); 5011 processPosition(mapper, x2, y2); 5012 processSync(mapper); 5013 5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5016 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 5017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5019 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 5020 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 5021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5022 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 5023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 5024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5025 5026 // First finger up. 5027 x2 += 15; y2 -= 20; 5028 processSlot(mapper, 0); 5029 processId(mapper, -1); 5030 processSlot(mapper, 1); 5031 processPosition(mapper, x2, y2); 5032 processSync(mapper); 5033 5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5035 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 5036 motionArgs.action); 5037 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 5038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5040 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 5041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 5042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5043 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0)); 5044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 5045 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5046 5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5049 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 5050 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 5051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5053 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5054 5055 // Move. 5056 x2 += 20; y2 -= 25; 5057 processPosition(mapper, x2, y2); 5058 processSync(mapper); 5059 5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5062 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 5063 ASSERT_EQ(1, motionArgs.pointerProperties[0].id); 5064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5067 5068 // New finger down. 5069 int32_t x3 = 700, y3 = 300; 5070 processPosition(mapper, x2, y2); 5071 processSlot(mapper, 0); 5072 processId(mapper, 3); 5073 processPosition(mapper, x3, y3); 5074 processSync(mapper); 5075 5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5077 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 5078 motionArgs.action); 5079 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 5080 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5082 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 5083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 5084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5085 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 5086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 5087 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5088 5089 // Second finger up. 5090 x3 += 30; y3 -= 20; 5091 processSlot(mapper, 1); 5092 processId(mapper, -1); 5093 processSlot(mapper, 0); 5094 processPosition(mapper, x3, y3); 5095 processSync(mapper); 5096 5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5098 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 5099 motionArgs.action); 5100 ASSERT_EQ(size_t(2), motionArgs.pointerCount); 5101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5103 ASSERT_EQ(1, motionArgs.pointerProperties[1].id); 5104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType); 5105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5106 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 5107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], 5108 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0)); 5109 5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5112 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 5113 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5116 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 5117 5118 // Last finger up. 5119 processId(mapper, -1); 5120 processSync(mapper); 5121 5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 5124 ASSERT_EQ(size_t(1), motionArgs.pointerCount); 5125 ASSERT_EQ(0, motionArgs.pointerProperties[0].id); 5126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5128 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0)); 5129 5130 // Should not have sent any more keys or motions. 5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled()); 5133 } 5134 5135 TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) { 5136 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5137 addConfigurationProperty("touch.deviceType", "touchScreen"); 5138 prepareDisplay(DISPLAY_ORIENTATION_0); 5139 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE); 5140 addMapperAndConfigure(mapper); 5141 5142 // These calculations are based on the input device calibration documentation. 5143 int32_t rawX = 100; 5144 int32_t rawY = 200; 5145 int32_t rawTouchMajor = 7; 5146 int32_t rawTouchMinor = 6; 5147 int32_t rawToolMajor = 9; 5148 int32_t rawToolMinor = 8; 5149 int32_t rawPressure = 11; 5150 int32_t rawDistance = 0; 5151 int32_t rawOrientation = 3; 5152 int32_t id = 5; 5153 5154 float x = toDisplayX(rawX); 5155 float y = toDisplayY(rawY); 5156 float pressure = float(rawPressure) / RAW_PRESSURE_MAX; 5157 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX; 5158 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE; 5159 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE; 5160 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE; 5161 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE; 5162 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2; 5163 float distance = float(rawDistance); 5164 5165 processPosition(mapper, rawX, rawY); 5166 processTouchMajor(mapper, rawTouchMajor); 5167 processTouchMinor(mapper, rawTouchMinor); 5168 processToolMajor(mapper, rawToolMajor); 5169 processToolMinor(mapper, rawToolMinor); 5170 processPressure(mapper, rawPressure); 5171 processOrientation(mapper, rawOrientation); 5172 processDistance(mapper, rawDistance); 5173 processId(mapper, id); 5174 processMTSync(mapper); 5175 processSync(mapper); 5176 5177 NotifyMotionArgs args; 5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5179 ASSERT_EQ(0, args.pointerProperties[0].id); 5180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 5181 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, 5182 orientation, distance)); 5183 } 5184 5185 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) { 5186 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5187 addConfigurationProperty("touch.deviceType", "touchScreen"); 5188 prepareDisplay(DISPLAY_ORIENTATION_0); 5189 prepareAxes(POSITION | TOUCH | TOOL | MINOR); 5190 addConfigurationProperty("touch.size.calibration", "geometric"); 5191 addMapperAndConfigure(mapper); 5192 5193 // These calculations are based on the input device calibration documentation. 5194 int32_t rawX = 100; 5195 int32_t rawY = 200; 5196 int32_t rawTouchMajor = 140; 5197 int32_t rawTouchMinor = 120; 5198 int32_t rawToolMajor = 180; 5199 int32_t rawToolMinor = 160; 5200 5201 float x = toDisplayX(rawX); 5202 float y = toDisplayY(rawY); 5203 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX; 5204 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE; 5205 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE; 5206 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE; 5207 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE; 5208 5209 processPosition(mapper, rawX, rawY); 5210 processTouchMajor(mapper, rawTouchMajor); 5211 processTouchMinor(mapper, rawTouchMinor); 5212 processToolMajor(mapper, rawToolMajor); 5213 processToolMinor(mapper, rawToolMinor); 5214 processMTSync(mapper); 5215 processSync(mapper); 5216 5217 NotifyMotionArgs args; 5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 5220 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0)); 5221 } 5222 5223 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) { 5224 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5225 addConfigurationProperty("touch.deviceType", "touchScreen"); 5226 prepareDisplay(DISPLAY_ORIENTATION_0); 5227 prepareAxes(POSITION | TOUCH | TOOL); 5228 addConfigurationProperty("touch.size.calibration", "diameter"); 5229 addConfigurationProperty("touch.size.scale", "10"); 5230 addConfigurationProperty("touch.size.bias", "160"); 5231 addConfigurationProperty("touch.size.isSummed", "1"); 5232 addMapperAndConfigure(mapper); 5233 5234 // These calculations are based on the input device calibration documentation. 5235 // Note: We only provide a single common touch/tool value because the device is assumed 5236 // not to emit separate values for each pointer (isSummed = 1). 5237 int32_t rawX = 100; 5238 int32_t rawY = 200; 5239 int32_t rawX2 = 150; 5240 int32_t rawY2 = 250; 5241 int32_t rawTouchMajor = 5; 5242 int32_t rawToolMajor = 8; 5243 5244 float x = toDisplayX(rawX); 5245 float y = toDisplayY(rawY); 5246 float x2 = toDisplayX(rawX2); 5247 float y2 = toDisplayY(rawY2); 5248 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX; 5249 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f; 5250 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f; 5251 5252 processPosition(mapper, rawX, rawY); 5253 processTouchMajor(mapper, rawTouchMajor); 5254 processToolMajor(mapper, rawToolMajor); 5255 processMTSync(mapper); 5256 processPosition(mapper, rawX2, rawY2); 5257 processTouchMajor(mapper, rawTouchMajor); 5258 processToolMajor(mapper, rawToolMajor); 5259 processMTSync(mapper); 5260 processSync(mapper); 5261 5262 NotifyMotionArgs args; 5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action); 5265 5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5267 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), 5268 args.action); 5269 ASSERT_EQ(size_t(2), args.pointerCount); 5270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 5271 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0)); 5272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1], 5273 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0)); 5274 } 5275 5276 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) { 5277 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5278 addConfigurationProperty("touch.deviceType", "touchScreen"); 5279 prepareDisplay(DISPLAY_ORIENTATION_0); 5280 prepareAxes(POSITION | TOUCH | TOOL); 5281 addConfigurationProperty("touch.size.calibration", "area"); 5282 addConfigurationProperty("touch.size.scale", "43"); 5283 addConfigurationProperty("touch.size.bias", "3"); 5284 addMapperAndConfigure(mapper); 5285 5286 // These calculations are based on the input device calibration documentation. 5287 int32_t rawX = 100; 5288 int32_t rawY = 200; 5289 int32_t rawTouchMajor = 5; 5290 int32_t rawToolMajor = 8; 5291 5292 float x = toDisplayX(rawX); 5293 float y = toDisplayY(rawY); 5294 float size = float(rawTouchMajor) / RAW_TOUCH_MAX; 5295 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f; 5296 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f; 5297 5298 processPosition(mapper, rawX, rawY); 5299 processTouchMajor(mapper, rawTouchMajor); 5300 processToolMajor(mapper, rawToolMajor); 5301 processMTSync(mapper); 5302 processSync(mapper); 5303 5304 NotifyMotionArgs args; 5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 5307 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0)); 5308 } 5309 5310 TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) { 5311 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5312 addConfigurationProperty("touch.deviceType", "touchScreen"); 5313 prepareDisplay(DISPLAY_ORIENTATION_0); 5314 prepareAxes(POSITION | PRESSURE); 5315 addConfigurationProperty("touch.pressure.calibration", "amplitude"); 5316 addConfigurationProperty("touch.pressure.scale", "0.01"); 5317 addMapperAndConfigure(mapper); 5318 5319 // These calculations are based on the input device calibration documentation. 5320 int32_t rawX = 100; 5321 int32_t rawY = 200; 5322 int32_t rawPressure = 60; 5323 5324 float x = toDisplayX(rawX); 5325 float y = toDisplayY(rawY); 5326 float pressure = float(rawPressure) * 0.01f; 5327 5328 processPosition(mapper, rawX, rawY); 5329 processPressure(mapper, rawPressure); 5330 processMTSync(mapper); 5331 processSync(mapper); 5332 5333 NotifyMotionArgs args; 5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args)); 5335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 5336 x, y, pressure, 0, 0, 0, 0, 0, 0, 0)); 5337 } 5338 5339 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) { 5340 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5341 addConfigurationProperty("touch.deviceType", "touchScreen"); 5342 prepareDisplay(DISPLAY_ORIENTATION_0); 5343 prepareAxes(POSITION | ID | SLOT); 5344 addMapperAndConfigure(mapper); 5345 5346 NotifyMotionArgs motionArgs; 5347 NotifyKeyArgs keyArgs; 5348 5349 processId(mapper, 1); 5350 processPosition(mapper, 100, 200); 5351 processSync(mapper); 5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 5354 ASSERT_EQ(0, motionArgs.buttonState); 5355 5356 // press BTN_LEFT, release BTN_LEFT 5357 processKey(mapper, BTN_LEFT, 1); 5358 processSync(mapper); 5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5361 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 5362 5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5365 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState); 5366 5367 processKey(mapper, BTN_LEFT, 0); 5368 processSync(mapper); 5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5371 ASSERT_EQ(0, motionArgs.buttonState); 5372 5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5375 ASSERT_EQ(0, motionArgs.buttonState); 5376 5377 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE 5378 processKey(mapper, BTN_RIGHT, 1); 5379 processKey(mapper, BTN_MIDDLE, 1); 5380 processSync(mapper); 5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5383 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 5384 motionArgs.buttonState); 5385 5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5388 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 5389 5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5392 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY, 5393 motionArgs.buttonState); 5394 5395 processKey(mapper, BTN_RIGHT, 0); 5396 processSync(mapper); 5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5399 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 5400 5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5403 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState); 5404 5405 processKey(mapper, BTN_MIDDLE, 0); 5406 processSync(mapper); 5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5409 ASSERT_EQ(0, motionArgs.buttonState); 5410 5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5413 ASSERT_EQ(0, motionArgs.buttonState); 5414 5415 // press BTN_BACK, release BTN_BACK 5416 processKey(mapper, BTN_BACK, 1); 5417 processSync(mapper); 5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5419 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 5420 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 5421 5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5424 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 5425 5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5428 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 5429 5430 processKey(mapper, BTN_BACK, 0); 5431 processSync(mapper); 5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5434 ASSERT_EQ(0, motionArgs.buttonState); 5435 5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5438 ASSERT_EQ(0, motionArgs.buttonState); 5439 5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5441 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 5442 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 5443 5444 // press BTN_SIDE, release BTN_SIDE 5445 processKey(mapper, BTN_SIDE, 1); 5446 processSync(mapper); 5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 5449 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 5450 5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5453 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 5454 5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState); 5458 5459 processKey(mapper, BTN_SIDE, 0); 5460 processSync(mapper); 5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5463 ASSERT_EQ(0, motionArgs.buttonState); 5464 5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5467 ASSERT_EQ(0, motionArgs.buttonState); 5468 5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 5471 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode); 5472 5473 // press BTN_FORWARD, release BTN_FORWARD 5474 processKey(mapper, BTN_FORWARD, 1); 5475 processSync(mapper); 5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 5478 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 5479 5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 5483 5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5485 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5486 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 5487 5488 processKey(mapper, BTN_FORWARD, 0); 5489 processSync(mapper); 5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5492 ASSERT_EQ(0, motionArgs.buttonState); 5493 5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5496 ASSERT_EQ(0, motionArgs.buttonState); 5497 5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5499 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 5500 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 5501 5502 // press BTN_EXTRA, release BTN_EXTRA 5503 processKey(mapper, BTN_EXTRA, 1); 5504 processSync(mapper); 5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5506 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action); 5507 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 5508 5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5511 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 5512 5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5515 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState); 5516 5517 processKey(mapper, BTN_EXTRA, 0); 5518 processSync(mapper); 5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5521 ASSERT_EQ(0, motionArgs.buttonState); 5522 5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5525 ASSERT_EQ(0, motionArgs.buttonState); 5526 5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs)); 5528 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action); 5529 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode); 5530 5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled()); 5532 5533 // press BTN_STYLUS, release BTN_STYLUS 5534 processKey(mapper, BTN_STYLUS, 1); 5535 processSync(mapper); 5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5538 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState); 5539 5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5542 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState); 5543 5544 processKey(mapper, BTN_STYLUS, 0); 5545 processSync(mapper); 5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5548 ASSERT_EQ(0, motionArgs.buttonState); 5549 5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5552 ASSERT_EQ(0, motionArgs.buttonState); 5553 5554 // press BTN_STYLUS2, release BTN_STYLUS2 5555 processKey(mapper, BTN_STYLUS2, 1); 5556 processSync(mapper); 5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5559 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState); 5560 5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action); 5563 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState); 5564 5565 processKey(mapper, BTN_STYLUS2, 0); 5566 processSync(mapper); 5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5568 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action); 5569 ASSERT_EQ(0, motionArgs.buttonState); 5570 5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5573 ASSERT_EQ(0, motionArgs.buttonState); 5574 5575 // release touch 5576 processId(mapper, -1); 5577 processSync(mapper); 5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 5580 ASSERT_EQ(0, motionArgs.buttonState); 5581 } 5582 5583 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) { 5584 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5585 addConfigurationProperty("touch.deviceType", "touchScreen"); 5586 prepareDisplay(DISPLAY_ORIENTATION_0); 5587 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE); 5588 addMapperAndConfigure(mapper); 5589 5590 NotifyMotionArgs motionArgs; 5591 5592 // default tool type is finger 5593 processId(mapper, 1); 5594 processPosition(mapper, 100, 200); 5595 processSync(mapper); 5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 5598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5599 5600 // eraser 5601 processKey(mapper, BTN_TOOL_RUBBER, 1); 5602 processSync(mapper); 5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType); 5606 5607 // stylus 5608 processKey(mapper, BTN_TOOL_RUBBER, 0); 5609 processKey(mapper, BTN_TOOL_PEN, 1); 5610 processSync(mapper); 5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5614 5615 // brush 5616 processKey(mapper, BTN_TOOL_PEN, 0); 5617 processKey(mapper, BTN_TOOL_BRUSH, 1); 5618 processSync(mapper); 5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5622 5623 // pencil 5624 processKey(mapper, BTN_TOOL_BRUSH, 0); 5625 processKey(mapper, BTN_TOOL_PENCIL, 1); 5626 processSync(mapper); 5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5630 5631 // airbrush 5632 processKey(mapper, BTN_TOOL_PENCIL, 0); 5633 processKey(mapper, BTN_TOOL_AIRBRUSH, 1); 5634 processSync(mapper); 5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5638 5639 // mouse 5640 processKey(mapper, BTN_TOOL_AIRBRUSH, 0); 5641 processKey(mapper, BTN_TOOL_MOUSE, 1); 5642 processSync(mapper); 5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 5646 5647 // lens 5648 processKey(mapper, BTN_TOOL_MOUSE, 0); 5649 processKey(mapper, BTN_TOOL_LENS, 1); 5650 processSync(mapper); 5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 5654 5655 // double-tap 5656 processKey(mapper, BTN_TOOL_LENS, 0); 5657 processKey(mapper, BTN_TOOL_DOUBLETAP, 1); 5658 processSync(mapper); 5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5662 5663 // triple-tap 5664 processKey(mapper, BTN_TOOL_DOUBLETAP, 0); 5665 processKey(mapper, BTN_TOOL_TRIPLETAP, 1); 5666 processSync(mapper); 5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5670 5671 // quad-tap 5672 processKey(mapper, BTN_TOOL_TRIPLETAP, 0); 5673 processKey(mapper, BTN_TOOL_QUADTAP, 1); 5674 processSync(mapper); 5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5678 5679 // finger 5680 processKey(mapper, BTN_TOOL_QUADTAP, 0); 5681 processKey(mapper, BTN_TOOL_FINGER, 1); 5682 processSync(mapper); 5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5686 5687 // stylus trumps finger 5688 processKey(mapper, BTN_TOOL_PEN, 1); 5689 processSync(mapper); 5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5693 5694 // eraser trumps stylus 5695 processKey(mapper, BTN_TOOL_RUBBER, 1); 5696 processSync(mapper); 5697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType); 5700 5701 // mouse trumps eraser 5702 processKey(mapper, BTN_TOOL_MOUSE, 1); 5703 processSync(mapper); 5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType); 5707 5708 // MT tool type trumps BTN tool types: MT_TOOL_FINGER 5709 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE 5710 processSync(mapper); 5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5714 5715 // MT tool type trumps BTN tool types: MT_TOOL_PEN 5716 processToolType(mapper, MT_TOOL_PEN); 5717 processSync(mapper); 5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType); 5721 5722 // back to default tool type 5723 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing 5724 processKey(mapper, BTN_TOOL_MOUSE, 0); 5725 processKey(mapper, BTN_TOOL_RUBBER, 0); 5726 processKey(mapper, BTN_TOOL_PEN, 0); 5727 processKey(mapper, BTN_TOOL_FINGER, 0); 5728 processSync(mapper); 5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action); 5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType); 5732 } 5733 5734 TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) { 5735 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5736 addConfigurationProperty("touch.deviceType", "touchScreen"); 5737 prepareDisplay(DISPLAY_ORIENTATION_0); 5738 prepareAxes(POSITION | ID | SLOT); 5739 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0); 5740 addMapperAndConfigure(mapper); 5741 5742 NotifyMotionArgs motionArgs; 5743 5744 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0 5745 processId(mapper, 1); 5746 processPosition(mapper, 100, 200); 5747 processSync(mapper); 5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5749 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 5750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5751 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 5752 5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5754 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5756 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 5757 5758 // move a little 5759 processPosition(mapper, 150, 250); 5760 processSync(mapper); 5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5764 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5765 5766 // down when BTN_TOUCH is pressed, pressure defaults to 1 5767 processKey(mapper, BTN_TOUCH, 1); 5768 processSync(mapper); 5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 5771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5773 5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 5776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5777 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 5778 5779 // up when BTN_TOUCH is released, hover restored 5780 processKey(mapper, BTN_TOUCH, 0); 5781 processSync(mapper); 5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5783 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5785 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 5786 5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 5789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5790 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5791 5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5795 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5796 5797 // exit hover when pointer goes away 5798 processId(mapper, -1); 5799 processSync(mapper); 5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 5802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5803 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5804 } 5805 5806 TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) { 5807 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice); 5808 addConfigurationProperty("touch.deviceType", "touchScreen"); 5809 prepareDisplay(DISPLAY_ORIENTATION_0); 5810 prepareAxes(POSITION | ID | SLOT | PRESSURE); 5811 addMapperAndConfigure(mapper); 5812 5813 NotifyMotionArgs motionArgs; 5814 5815 // initially hovering because pressure is 0 5816 processId(mapper, 1); 5817 processPosition(mapper, 100, 200); 5818 processPressure(mapper, 0); 5819 processSync(mapper); 5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5821 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 5822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5823 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 5824 5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5828 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0)); 5829 5830 // move a little 5831 processPosition(mapper, 150, 250); 5832 processSync(mapper); 5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5836 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5837 5838 // down when pressure becomes non-zero 5839 processPressure(mapper, RAW_PRESSURE_MAX); 5840 processSync(mapper); 5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 5843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5844 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5845 5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5847 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action); 5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5849 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 5850 5851 // up when pressure becomes 0, hover restored 5852 processPressure(mapper, 0); 5853 processSync(mapper); 5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action); 5856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5857 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0)); 5858 5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action); 5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5862 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5863 5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action); 5866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5867 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5868 5869 // exit hover when pointer goes away 5870 processId(mapper, -1); 5871 processSync(mapper); 5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs)); 5873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action); 5874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 5875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0)); 5876 } 5877 5878 5879 } // namespace android 5880