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