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