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