InputReader_test.cpp revision d02c5b6aace05d9fd938e2d03705ac4f60f8da19
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
23namespace android {
24
25// An arbitrary time value.
26static const nsecs_t ARBITRARY_TIME = 1234;
27
28// Arbitrary display properties.
29static const int32_t DISPLAY_ID = 0;
30static const int32_t DISPLAY_WIDTH = 480;
31static const int32_t DISPLAY_HEIGHT = 800;
32
33// Error tolerance for floating point assertions.
34static const float EPSILON = 0.001f;
35
36template<typename T>
37static inline T min(T a, T b) {
38    return a < b ? a : b;
39}
40
41static inline float avg(float x, float y) {
42    return (x + y) / 2;
43}
44
45
46// --- FakePointerController ---
47
48class FakePointerController : public PointerControllerInterface {
49    bool mHaveBounds;
50    float mMinX, mMinY, mMaxX, mMaxY;
51    float mX, mY;
52    int32_t mButtonState;
53
54protected:
55    virtual ~FakePointerController() { }
56
57public:
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
89private:
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
127class FakeInputReaderPolicy : public InputReaderPolicyInterface {
128    InputReaderConfiguration mConfig;
129    KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
130    Vector<InputDeviceInfo> mInputDevices;
131
132protected:
133    virtual ~FakeInputReaderPolicy() { }
134
135public:
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
176private:
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 InputDeviceIdentifier& identifier) {
190        return NULL;
191    }
192
193    virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) {
194        return String8::empty();
195    }
196};
197
198
199// --- FakeInputListener ---
200
201class FakeInputListener : public InputListenerInterface {
202private:
203    List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
204    List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
205    List<NotifyKeyArgs> mNotifyKeyArgsQueue;
206    List<NotifyMotionArgs> mNotifyMotionArgsQueue;
207    List<NotifySwitchArgs> mNotifySwitchArgsQueue;
208
209protected:
210    virtual ~FakeInputListener() { }
211
212public:
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
273private:
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
298class 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
328protected:
329    virtual ~FakeEventHub() {
330        for (size_t i = 0; i < mDevices.size(); i++) {
331            delete mDevices.valueAt(i);
332        }
333    }
334
335public:
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
459private:
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
707class 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
715public:
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
735private:
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
778class 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
792public:
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
847private:
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
914class InstrumentedInputReader : public InputReader {
915    InputDevice* mNextDevice;
916
917public:
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
944protected:
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
961class InputReaderTest : public testing::Test {
962protected:
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
1009TEST_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
1035TEST_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
1062TEST_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
1089TEST_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
1116TEST_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
1151TEST_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
1160TEST_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
1181class InputDeviceTest : public testing::Test {
1182protected:
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
1219const char* InputDeviceTest::DEVICE_NAME = "device";
1220const int32_t InputDeviceTest::DEVICE_ID = 1;
1221const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1222const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1223const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1224        | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1225
1226TEST_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
1232TEST_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
1274TEST_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
1371class InputMapperTest : public testing::Test {
1372protected:
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
1470const char* InputMapperTest::DEVICE_NAME = "device";
1471const int32_t InputMapperTest::DEVICE_ID = 1;
1472const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1473const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1474const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1475
1476
1477// --- SwitchInputMapperTest ---
1478
1479class SwitchInputMapperTest : public InputMapperTest {
1480protected:
1481};
1482
1483TEST_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
1490TEST_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
1501TEST_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
1522class KeyboardInputMapperTest : public InputMapperTest {
1523protected:
1524    void testDPadKeyRotation(KeyboardInputMapper* mapper,
1525            int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1526};
1527
1528void 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
1546TEST_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
1554TEST_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
1664TEST_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
1707TEST_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
1730TEST_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
1812TEST_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
1824TEST_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
1836TEST_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
1850TEST_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
1931class CursorInputMapperTest : public InputMapperTest {
1932protected:
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
1948const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
1949
1950void 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
1965TEST_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
1973TEST_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
1981TEST_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
2012TEST_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
2031TEST_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
2086TEST_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
2110TEST_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
2134TEST_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
2171TEST_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
2189TEST_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
2240TEST_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
2416TEST_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
2440class TouchInputMapperTest : public InputMapperTest {
2441protected:
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
2491const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
2492const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
2493const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
2494const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
2495const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2496const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2497const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2498const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2499const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
2500const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
2501const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2502const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2503const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
2504const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
2505const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
2506const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
2507const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2508const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
2509const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
2510const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
2511const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2512const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
2513
2514const 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
2518const 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
2523void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2524    setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2525}
2526
2527void 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
2534int32_t TouchInputMapperTest::toRawX(float displayX) {
2535    return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
2536}
2537
2538int32_t TouchInputMapperTest::toRawY(float displayY) {
2539    return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
2540}
2541
2542float TouchInputMapperTest::toDisplayX(int32_t rawX) {
2543    return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
2544}
2545
2546float 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
2553class SingleTouchInputMapperTest : public TouchInputMapperTest {
2554protected:
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
2569void SingleTouchInputMapperTest::prepareButtons() {
2570    mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
2571}
2572
2573void 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
2600void 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
2606void 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
2611void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
2612    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0);
2613}
2614
2615void SingleTouchInputMapperTest::processPressure(
2616        SingleTouchInputMapper* mapper, int32_t pressure) {
2617    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, pressure);
2618}
2619
2620void SingleTouchInputMapperTest::processToolMajor(
2621        SingleTouchInputMapper* mapper, int32_t toolMajor) {
2622    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
2623}
2624
2625void SingleTouchInputMapperTest::processDistance(
2626        SingleTouchInputMapper* mapper, int32_t distance) {
2627    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, distance);
2628}
2629
2630void 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
2636void SingleTouchInputMapperTest::processKey(
2637        SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
2638    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
2639}
2640
2641void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
2642    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2643}
2644
2645
2646TEST_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
2655TEST_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
2666TEST_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
2676TEST_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
2686TEST_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
2715TEST_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
2744TEST_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
2760TEST_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
2811TEST_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
2933TEST_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
3007TEST_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
3098TEST_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
3122TEST_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
3184TEST_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
3229TEST_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
3390TEST_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
3526TEST_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
3599TEST_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
3674class MultiTouchInputMapperTest : public TouchInputMapperTest {
3675protected:
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
3694void 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
3744void 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
3750void MultiTouchInputMapperTest::processTouchMajor(
3751        MultiTouchInputMapper* mapper, int32_t touchMajor) {
3752    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
3753}
3754
3755void MultiTouchInputMapperTest::processTouchMinor(
3756        MultiTouchInputMapper* mapper, int32_t touchMinor) {
3757    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
3758}
3759
3760void MultiTouchInputMapperTest::processToolMajor(
3761        MultiTouchInputMapper* mapper, int32_t toolMajor) {
3762    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
3763}
3764
3765void MultiTouchInputMapperTest::processToolMinor(
3766        MultiTouchInputMapper* mapper, int32_t toolMinor) {
3767    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
3768}
3769
3770void MultiTouchInputMapperTest::processOrientation(
3771        MultiTouchInputMapper* mapper, int32_t orientation) {
3772    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, orientation);
3773}
3774
3775void MultiTouchInputMapperTest::processPressure(
3776        MultiTouchInputMapper* mapper, int32_t pressure) {
3777    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, pressure);
3778}
3779
3780void MultiTouchInputMapperTest::processDistance(
3781        MultiTouchInputMapper* mapper, int32_t distance) {
3782    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_DISTANCE, distance);
3783}
3784
3785void MultiTouchInputMapperTest::processId(
3786        MultiTouchInputMapper* mapper, int32_t id) {
3787    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, id);
3788}
3789
3790void MultiTouchInputMapperTest::processSlot(
3791        MultiTouchInputMapper* mapper, int32_t slot) {
3792    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_SLOT, slot);
3793}
3794
3795void MultiTouchInputMapperTest::processToolType(
3796        MultiTouchInputMapper* mapper, int32_t toolType) {
3797    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
3798}
3799
3800void MultiTouchInputMapperTest::processKey(
3801        MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
3802    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
3803}
3804
3805void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3806    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0);
3807}
3808
3809void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3810    process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
3811}
3812
3813
3814TEST_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
4091TEST_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,