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