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