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 <inttypes.h>
20 #include <utils/List.h>
21 #include <gtest/gtest.h>
22 #include <math.h>
23 
24 namespace android {
25 
26 // An arbitrary time value.
27 static const nsecs_t ARBITRARY_TIME = 1234;
28 
29 // Arbitrary display properties.
30 static const int32_t DISPLAY_ID = 0;
31 static const int32_t DISPLAY_WIDTH = 480;
32 static const int32_t DISPLAY_HEIGHT = 800;
33 static const int32_t VIRTUAL_DISPLAY_ID = 1;
34 static const int32_t VIRTUAL_DISPLAY_WIDTH = 400;
35 static const int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
36 static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "Vr-display-unique-ID";
37 
38 // Error tolerance for floating point assertions.
39 static const float EPSILON = 0.001f;
40 
41 template<typename T>
min(T a,T b)42 static inline T min(T a, T b) {
43     return a < b ? a : b;
44 }
45 
avg(float x,float y)46 static inline float avg(float x, float y) {
47     return (x + y) / 2;
48 }
49 
50 
51 // --- FakePointerController ---
52 
53 class FakePointerController : public PointerControllerInterface {
54     bool mHaveBounds;
55     float mMinX, mMinY, mMaxX, mMaxY;
56     float mX, mY;
57     int32_t mButtonState;
58 
59 protected:
~FakePointerController()60     virtual ~FakePointerController() { }
61 
62 public:
FakePointerController()63     FakePointerController() :
64         mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
65         mButtonState(0) {
66     }
67 
setBounds(float minX,float minY,float maxX,float maxY)68     void setBounds(float minX, float minY, float maxX, float maxY) {
69         mHaveBounds = true;
70         mMinX = minX;
71         mMinY = minY;
72         mMaxX = maxX;
73         mMaxY = maxY;
74     }
75 
setPosition(float x,float y)76     virtual void setPosition(float x, float y) {
77         mX = x;
78         mY = y;
79     }
80 
setButtonState(int32_t buttonState)81     virtual void setButtonState(int32_t buttonState) {
82         mButtonState = buttonState;
83     }
84 
getButtonState() const85     virtual int32_t getButtonState() const {
86         return mButtonState;
87     }
88 
getPosition(float * outX,float * outY) const89     virtual void getPosition(float* outX, float* outY) const {
90         *outX = mX;
91         *outY = mY;
92     }
93 
94 private:
getBounds(float * outMinX,float * outMinY,float * outMaxX,float * outMaxY) const95     virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
96         *outMinX = mMinX;
97         *outMinY = mMinY;
98         *outMaxX = mMaxX;
99         *outMaxY = mMaxY;
100         return mHaveBounds;
101     }
102 
move(float deltaX,float deltaY)103     virtual void move(float deltaX, float deltaY) {
104         mX += deltaX;
105         if (mX < mMinX) mX = mMinX;
106         if (mX > mMaxX) mX = mMaxX;
107         mY += deltaY;
108         if (mY < mMinY) mY = mMinY;
109         if (mY > mMaxY) mY = mMaxY;
110     }
111 
fade(Transition)112     virtual void fade(Transition) {
113     }
114 
unfade(Transition)115     virtual void unfade(Transition) {
116     }
117 
setPresentation(Presentation)118     virtual void setPresentation(Presentation) {
119     }
120 
setSpots(const PointerCoords *,const uint32_t *,BitSet32)121     virtual void setSpots(const PointerCoords*, const uint32_t*, BitSet32) {
122     }
123 
clearSpots()124     virtual void clearSpots() {
125     }
126 };
127 
128 
129 // --- FakeInputReaderPolicy ---
130 
131 class FakeInputReaderPolicy : public InputReaderPolicyInterface {
132     InputReaderConfiguration mConfig;
133     KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
134     Vector<InputDeviceInfo> mInputDevices;
135     TouchAffineTransformation transform;
136 
137 protected:
~FakeInputReaderPolicy()138     virtual ~FakeInputReaderPolicy() { }
139 
140 public:
FakeInputReaderPolicy()141     FakeInputReaderPolicy() {
142     }
143 
setDisplayViewport(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const String8 & uniqueId)144     void setDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
145             const String8& uniqueId) {
146         DisplayViewport v = createDisplayViewport(displayId, width, height, orientation, uniqueId);
147         // Set the size of both the internal and external display at the same time.
148         mConfig.setPhysicalDisplayViewport(ViewportType::VIEWPORT_INTERNAL, v);
149         mConfig.setPhysicalDisplayViewport(ViewportType::VIEWPORT_EXTERNAL, v);
150     }
151 
setVirtualDisplayViewport(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const String8 & uniqueId)152     void setVirtualDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
153             const String8& uniqueId) {
154         Vector<DisplayViewport> viewports;
155         viewports.push_back(createDisplayViewport(displayId, width, height, orientation, uniqueId));
156         mConfig.setVirtualDisplayViewports(viewports);
157     }
158 
addExcludedDeviceName(const String8 & deviceName)159     void addExcludedDeviceName(const String8& deviceName) {
160         mConfig.excludedDeviceNames.push(deviceName);
161     }
162 
addDisabledDevice(int32_t deviceId)163     void addDisabledDevice(int32_t deviceId) {
164         ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
165         bool currentlyEnabled = index < 0;
166         if (currentlyEnabled) {
167             mConfig.disabledDevices.add(deviceId);
168         }
169     }
170 
removeDisabledDevice(int32_t deviceId)171     void removeDisabledDevice(int32_t deviceId) {
172         ssize_t index = mConfig.disabledDevices.indexOf(deviceId);
173         bool currentlyEnabled = index < 0;
174         if (!currentlyEnabled) {
175             mConfig.disabledDevices.remove(deviceId);
176         }
177     }
178 
setPointerController(int32_t deviceId,const sp<FakePointerController> & controller)179     void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
180         mPointerControllers.add(deviceId, controller);
181     }
182 
getReaderConfiguration() const183     const InputReaderConfiguration* getReaderConfiguration() const {
184         return &mConfig;
185     }
186 
getInputDevices() const187     const Vector<InputDeviceInfo>& getInputDevices() const {
188         return mInputDevices;
189     }
190 
getTouchAffineTransformation(const String8 & inputDeviceDescriptor,int32_t surfaceRotation)191     TouchAffineTransformation getTouchAffineTransformation(const String8& inputDeviceDescriptor,
192             int32_t surfaceRotation) {
193         return transform;
194     }
195 
setTouchAffineTransformation(const TouchAffineTransformation t)196     void setTouchAffineTransformation(const TouchAffineTransformation t) {
197         transform = t;
198     }
199 
setPointerCapture(bool enabled)200     void setPointerCapture(bool enabled) {
201         mConfig.pointerCapture = enabled;
202     }
203 
204 private:
createDisplayViewport(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const String8 & uniqueId)205     DisplayViewport createDisplayViewport(int32_t displayId, int32_t width, int32_t height,
206             int32_t orientation, const String8& uniqueId) {
207         bool isRotated = (orientation == DISPLAY_ORIENTATION_90
208                 || orientation == DISPLAY_ORIENTATION_270);
209         DisplayViewport v;
210         v.displayId = displayId;
211         v.orientation = orientation;
212         v.logicalLeft = 0;
213         v.logicalTop = 0;
214         v.logicalRight = isRotated ? height : width;
215         v.logicalBottom = isRotated ? width : height;
216         v.physicalLeft = 0;
217         v.physicalTop = 0;
218         v.physicalRight = isRotated ? height : width;
219         v.physicalBottom = isRotated ? width : height;
220         v.deviceWidth = isRotated ? height : width;
221         v.deviceHeight = isRotated ? width : height;
222         v.uniqueId = uniqueId;
223         return v;
224     }
225 
getReaderConfiguration(InputReaderConfiguration * outConfig)226     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
227         *outConfig = mConfig;
228     }
229 
obtainPointerController(int32_t deviceId)230     virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
231         return mPointerControllers.valueFor(deviceId);
232     }
233 
notifyInputDevicesChanged(const Vector<InputDeviceInfo> & inputDevices)234     virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
235         mInputDevices = inputDevices;
236     }
237 
getKeyboardLayoutOverlay(const InputDeviceIdentifier &)238     virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier&) {
239         return NULL;
240     }
241 
getDeviceAlias(const InputDeviceIdentifier &)242     virtual String8 getDeviceAlias(const InputDeviceIdentifier&) {
243         return String8::empty();
244     }
245 };
246 
247 
248 // --- FakeInputListener ---
249 
250 class FakeInputListener : public InputListenerInterface {
251 private:
252     List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgsQueue;
253     List<NotifyDeviceResetArgs> mNotifyDeviceResetArgsQueue;
254     List<NotifyKeyArgs> mNotifyKeyArgsQueue;
255     List<NotifyMotionArgs> mNotifyMotionArgsQueue;
256     List<NotifySwitchArgs> mNotifySwitchArgsQueue;
257 
258 protected:
~FakeInputListener()259     virtual ~FakeInputListener() { }
260 
261 public:
FakeInputListener()262     FakeInputListener() {
263     }
264 
assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs * outEventArgs=NULL)265     void assertNotifyConfigurationChangedWasCalled(
266             NotifyConfigurationChangedArgs* outEventArgs = NULL) {
267         ASSERT_FALSE(mNotifyConfigurationChangedArgsQueue.empty())
268                 << "Expected notifyConfigurationChanged() to have been called.";
269         if (outEventArgs) {
270             *outEventArgs = *mNotifyConfigurationChangedArgsQueue.begin();
271         }
272         mNotifyConfigurationChangedArgsQueue.erase(mNotifyConfigurationChangedArgsQueue.begin());
273     }
274 
assertNotifyConfigurationChangedWasNotCalled()275     void assertNotifyConfigurationChangedWasNotCalled() {
276         ASSERT_TRUE(mNotifyConfigurationChangedArgsQueue.empty())
277                 << "Expected notifyConfigurationChanged() to not have been called.";
278     }
279 
assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs * outEventArgs=NULL)280     void assertNotifyDeviceResetWasCalled(
281             NotifyDeviceResetArgs* outEventArgs = NULL) {
282         ASSERT_FALSE(mNotifyDeviceResetArgsQueue.empty())
283                 << "Expected notifyDeviceReset() to have been called.";
284         if (outEventArgs) {
285             *outEventArgs = *mNotifyDeviceResetArgsQueue.begin();
286         }
287         mNotifyDeviceResetArgsQueue.erase(mNotifyDeviceResetArgsQueue.begin());
288     }
289 
assertNotifyDeviceResetWasNotCalled()290     void assertNotifyDeviceResetWasNotCalled() {
291         ASSERT_TRUE(mNotifyDeviceResetArgsQueue.empty())
292                 << "Expected notifyDeviceReset() to not have been called.";
293     }
294 
assertNotifyKeyWasCalled(NotifyKeyArgs * outEventArgs=NULL)295     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = NULL) {
296         ASSERT_FALSE(mNotifyKeyArgsQueue.empty())
297                 << "Expected notifyKey() to have been called.";
298         if (outEventArgs) {
299             *outEventArgs = *mNotifyKeyArgsQueue.begin();
300         }
301         mNotifyKeyArgsQueue.erase(mNotifyKeyArgsQueue.begin());
302     }
303 
assertNotifyKeyWasNotCalled()304     void assertNotifyKeyWasNotCalled() {
305         ASSERT_TRUE(mNotifyKeyArgsQueue.empty())
306                 << "Expected notifyKey() to not have been called.";
307     }
308 
assertNotifyMotionWasCalled(NotifyMotionArgs * outEventArgs=NULL)309     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = NULL) {
310         ASSERT_FALSE(mNotifyMotionArgsQueue.empty())
311                 << "Expected notifyMotion() to have been called.";
312         if (outEventArgs) {
313             *outEventArgs = *mNotifyMotionArgsQueue.begin();
314         }
315         mNotifyMotionArgsQueue.erase(mNotifyMotionArgsQueue.begin());
316     }
317 
assertNotifyMotionWasNotCalled()318     void assertNotifyMotionWasNotCalled() {
319         ASSERT_TRUE(mNotifyMotionArgsQueue.empty())
320                 << "Expected notifyMotion() to not have been called.";
321     }
322 
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs=NULL)323     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = NULL) {
324         ASSERT_FALSE(mNotifySwitchArgsQueue.empty())
325                 << "Expected notifySwitch() to have been called.";
326         if (outEventArgs) {
327             *outEventArgs = *mNotifySwitchArgsQueue.begin();
328         }
329         mNotifySwitchArgsQueue.erase(mNotifySwitchArgsQueue.begin());
330     }
331 
332 private:
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)333     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
334         mNotifyConfigurationChangedArgsQueue.push_back(*args);
335     }
336 
notifyDeviceReset(const NotifyDeviceResetArgs * args)337     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) {
338         mNotifyDeviceResetArgsQueue.push_back(*args);
339     }
340 
notifyKey(const NotifyKeyArgs * args)341     virtual void notifyKey(const NotifyKeyArgs* args) {
342         mNotifyKeyArgsQueue.push_back(*args);
343     }
344 
notifyMotion(const NotifyMotionArgs * args)345     virtual void notifyMotion(const NotifyMotionArgs* args) {
346         mNotifyMotionArgsQueue.push_back(*args);
347     }
348 
notifySwitch(const NotifySwitchArgs * args)349     virtual void notifySwitch(const NotifySwitchArgs* args) {
350         mNotifySwitchArgsQueue.push_back(*args);
351     }
352 };
353 
354 
355 // --- FakeEventHub ---
356 
357 class FakeEventHub : public EventHubInterface {
358     struct KeyInfo {
359         int32_t keyCode;
360         uint32_t flags;
361     };
362 
363     struct Device {
364         InputDeviceIdentifier identifier;
365         uint32_t classes;
366         PropertyMap configuration;
367         KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
368         KeyedVector<int, bool> relativeAxes;
369         KeyedVector<int32_t, int32_t> keyCodeStates;
370         KeyedVector<int32_t, int32_t> scanCodeStates;
371         KeyedVector<int32_t, int32_t> switchStates;
372         KeyedVector<int32_t, int32_t> absoluteAxisValue;
373         KeyedVector<int32_t, KeyInfo> keysByScanCode;
374         KeyedVector<int32_t, KeyInfo> keysByUsageCode;
375         KeyedVector<int32_t, bool> leds;
376         Vector<VirtualKeyDefinition> virtualKeys;
377         bool enabled;
378 
enableandroid::FakeEventHub::Device379         status_t enable() {
380             enabled = true;
381             return OK;
382         }
383 
disableandroid::FakeEventHub::Device384         status_t disable() {
385             enabled = false;
386             return OK;
387         }
388 
Deviceandroid::FakeEventHub::Device389         explicit Device(uint32_t classes) :
390                 classes(classes), enabled(true) {
391         }
392     };
393 
394     KeyedVector<int32_t, Device*> mDevices;
395     Vector<String8> mExcludedDevices;
396     List<RawEvent> mEvents;
397 
398 protected:
~FakeEventHub()399     virtual ~FakeEventHub() {
400         for (size_t i = 0; i < mDevices.size(); i++) {
401             delete mDevices.valueAt(i);
402         }
403     }
404 
405 public:
FakeEventHub()406     FakeEventHub() { }
407 
addDevice(int32_t deviceId,const String8 & name,uint32_t classes)408     void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
409         Device* device = new Device(classes);
410         device->identifier.name = name;
411         mDevices.add(deviceId, device);
412 
413         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
414     }
415 
removeDevice(int32_t deviceId)416     void removeDevice(int32_t deviceId) {
417         delete mDevices.valueFor(deviceId);
418         mDevices.removeItem(deviceId);
419 
420         enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
421     }
422 
isDeviceEnabled(int32_t deviceId)423     bool isDeviceEnabled(int32_t deviceId) {
424         Device* device = getDevice(deviceId);
425         if (device == NULL) {
426             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
427             return false;
428         }
429         return device->enabled;
430     }
431 
enableDevice(int32_t deviceId)432     status_t enableDevice(int32_t deviceId) {
433         status_t result;
434         Device* device = getDevice(deviceId);
435         if (device == NULL) {
436             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
437             return BAD_VALUE;
438         }
439         if (device->enabled) {
440             ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
441             return OK;
442         }
443         result = device->enable();
444         return result;
445     }
446 
disableDevice(int32_t deviceId)447     status_t disableDevice(int32_t deviceId) {
448         Device* device = getDevice(deviceId);
449         if (device == NULL) {
450             ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
451             return BAD_VALUE;
452         }
453         if (!device->enabled) {
454             ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
455             return OK;
456         }
457         return device->disable();
458     }
459 
finishDeviceScan()460     void finishDeviceScan() {
461         enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
462     }
463 
addConfigurationProperty(int32_t deviceId,const String8 & key,const String8 & value)464     void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
465         Device* device = getDevice(deviceId);
466         device->configuration.addProperty(key, value);
467     }
468 
addConfigurationMap(int32_t deviceId,const PropertyMap * configuration)469     void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
470         Device* device = getDevice(deviceId);
471         device->configuration.addAll(configuration);
472     }
473 
addAbsoluteAxis(int32_t deviceId,int axis,int32_t minValue,int32_t maxValue,int flat,int fuzz,int resolution=0)474     void addAbsoluteAxis(int32_t deviceId, int axis,
475             int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
476         Device* device = getDevice(deviceId);
477 
478         RawAbsoluteAxisInfo info;
479         info.valid = true;
480         info.minValue = minValue;
481         info.maxValue = maxValue;
482         info.flat = flat;
483         info.fuzz = fuzz;
484         info.resolution = resolution;
485         device->absoluteAxes.add(axis, info);
486     }
487 
addRelativeAxis(int32_t deviceId,int32_t axis)488     void addRelativeAxis(int32_t deviceId, int32_t axis) {
489         Device* device = getDevice(deviceId);
490         device->relativeAxes.add(axis, true);
491     }
492 
setKeyCodeState(int32_t deviceId,int32_t keyCode,int32_t state)493     void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
494         Device* device = getDevice(deviceId);
495         device->keyCodeStates.replaceValueFor(keyCode, state);
496     }
497 
setScanCodeState(int32_t deviceId,int32_t scanCode,int32_t state)498     void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
499         Device* device = getDevice(deviceId);
500         device->scanCodeStates.replaceValueFor(scanCode, state);
501     }
502 
setSwitchState(int32_t deviceId,int32_t switchCode,int32_t state)503     void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
504         Device* device = getDevice(deviceId);
505         device->switchStates.replaceValueFor(switchCode, state);
506     }
507 
setAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t value)508     void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
509         Device* device = getDevice(deviceId);
510         device->absoluteAxisValue.replaceValueFor(axis, value);
511     }
512 
addKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t keyCode,uint32_t flags)513     void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
514             int32_t keyCode, uint32_t flags) {
515         Device* device = getDevice(deviceId);
516         KeyInfo info;
517         info.keyCode = keyCode;
518         info.flags = flags;
519         if (scanCode) {
520             device->keysByScanCode.add(scanCode, info);
521         }
522         if (usageCode) {
523             device->keysByUsageCode.add(usageCode, info);
524         }
525     }
526 
addLed(int32_t deviceId,int32_t led,bool initialState)527     void addLed(int32_t deviceId, int32_t led, bool initialState) {
528         Device* device = getDevice(deviceId);
529         device->leds.add(led, initialState);
530     }
531 
getLedState(int32_t deviceId,int32_t led)532     bool getLedState(int32_t deviceId, int32_t led) {
533         Device* device = getDevice(deviceId);
534         return device->leds.valueFor(led);
535     }
536 
getExcludedDevices()537     Vector<String8>& getExcludedDevices() {
538         return mExcludedDevices;
539     }
540 
addVirtualKeyDefinition(int32_t deviceId,const VirtualKeyDefinition & definition)541     void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
542         Device* device = getDevice(deviceId);
543         device->virtualKeys.push(definition);
544     }
545 
enqueueEvent(nsecs_t when,int32_t deviceId,int32_t type,int32_t code,int32_t value)546     void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
547             int32_t code, int32_t value) {
548         RawEvent event;
549         event.when = when;
550         event.deviceId = deviceId;
551         event.type = type;
552         event.code = code;
553         event.value = value;
554         mEvents.push_back(event);
555 
556         if (type == EV_ABS) {
557             setAbsoluteAxisValue(deviceId, code, value);
558         }
559     }
560 
assertQueueIsEmpty()561     void assertQueueIsEmpty() {
562         ASSERT_EQ(size_t(0), mEvents.size())
563                 << "Expected the event queue to be empty (fully consumed).";
564     }
565 
566 private:
getDevice(int32_t deviceId) const567     Device* getDevice(int32_t deviceId) const {
568         ssize_t index = mDevices.indexOfKey(deviceId);
569         return index >= 0 ? mDevices.valueAt(index) : NULL;
570     }
571 
getDeviceClasses(int32_t deviceId) const572     virtual uint32_t getDeviceClasses(int32_t deviceId) const {
573         Device* device = getDevice(deviceId);
574         return device ? device->classes : 0;
575     }
576 
getDeviceIdentifier(int32_t deviceId) const577     virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const {
578         Device* device = getDevice(deviceId);
579         return device ? device->identifier : InputDeviceIdentifier();
580     }
581 
getDeviceControllerNumber(int32_t) const582     virtual int32_t getDeviceControllerNumber(int32_t) const {
583         return 0;
584     }
585 
getConfiguration(int32_t deviceId,PropertyMap * outConfiguration) const586     virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
587         Device* device = getDevice(deviceId);
588         if (device) {
589             *outConfiguration = device->configuration;
590         }
591     }
592 
getAbsoluteAxisInfo(int32_t deviceId,int axis,RawAbsoluteAxisInfo * outAxisInfo) const593     virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
594             RawAbsoluteAxisInfo* outAxisInfo) const {
595         Device* device = getDevice(deviceId);
596         if (device) {
597             ssize_t index = device->absoluteAxes.indexOfKey(axis);
598             if (index >= 0) {
599                 *outAxisInfo = device->absoluteAxes.valueAt(index);
600                 return OK;
601             }
602         }
603         outAxisInfo->clear();
604         return -1;
605     }
606 
hasRelativeAxis(int32_t deviceId,int axis) const607     virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
608         Device* device = getDevice(deviceId);
609         if (device) {
610             return device->relativeAxes.indexOfKey(axis) >= 0;
611         }
612         return false;
613     }
614 
hasInputProperty(int32_t,int) const615     virtual bool hasInputProperty(int32_t, int) const {
616         return false;
617     }
618 
mapKey(int32_t deviceId,int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags) const619     virtual status_t mapKey(int32_t deviceId,
620             int32_t scanCode, int32_t usageCode, int32_t metaState,
621             int32_t* outKeycode, int32_t *outMetaState, uint32_t* outFlags) const {
622         Device* device = getDevice(deviceId);
623         if (device) {
624             const KeyInfo* key = getKey(device, scanCode, usageCode);
625             if (key) {
626                 if (outKeycode) {
627                     *outKeycode = key->keyCode;
628                 }
629                 if (outFlags) {
630                     *outFlags = key->flags;
631                 }
632                 if (outMetaState) {
633                     *outMetaState = metaState;
634                 }
635                 return OK;
636             }
637         }
638         return NAME_NOT_FOUND;
639     }
640 
getKey(Device * device,int32_t scanCode,int32_t usageCode) const641     const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
642         if (usageCode) {
643             ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
644             if (index >= 0) {
645                 return &device->keysByUsageCode.valueAt(index);
646             }
647         }
648         if (scanCode) {
649             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
650             if (index >= 0) {
651                 return &device->keysByScanCode.valueAt(index);
652             }
653         }
654         return NULL;
655     }
656 
mapAxis(int32_t,int32_t,AxisInfo *) const657     virtual status_t mapAxis(int32_t, int32_t, AxisInfo*) const {
658         return NAME_NOT_FOUND;
659     }
660 
setExcludedDevices(const Vector<String8> & devices)661     virtual void setExcludedDevices(const Vector<String8>& devices) {
662         mExcludedDevices = devices;
663     }
664 
getEvents(int,RawEvent * buffer,size_t)665     virtual size_t getEvents(int, RawEvent* buffer, size_t) {
666         if (mEvents.empty()) {
667             return 0;
668         }
669 
670         *buffer = *mEvents.begin();
671         mEvents.erase(mEvents.begin());
672         return 1;
673     }
674 
getScanCodeState(int32_t deviceId,int32_t scanCode) const675     virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
676         Device* device = getDevice(deviceId);
677         if (device) {
678             ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
679             if (index >= 0) {
680                 return device->scanCodeStates.valueAt(index);
681             }
682         }
683         return AKEY_STATE_UNKNOWN;
684     }
685 
getKeyCodeState(int32_t deviceId,int32_t keyCode) const686     virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
687         Device* device = getDevice(deviceId);
688         if (device) {
689             ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
690             if (index >= 0) {
691                 return device->keyCodeStates.valueAt(index);
692             }
693         }
694         return AKEY_STATE_UNKNOWN;
695     }
696 
getSwitchState(int32_t deviceId,int32_t sw) const697     virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
698         Device* device = getDevice(deviceId);
699         if (device) {
700             ssize_t index = device->switchStates.indexOfKey(sw);
701             if (index >= 0) {
702                 return device->switchStates.valueAt(index);
703             }
704         }
705         return AKEY_STATE_UNKNOWN;
706     }
707 
getAbsoluteAxisValue(int32_t deviceId,int32_t axis,int32_t * outValue) const708     virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
709             int32_t* outValue) const {
710         Device* device = getDevice(deviceId);
711         if (device) {
712             ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
713             if (index >= 0) {
714                 *outValue = device->absoluteAxisValue.valueAt(index);
715                 return OK;
716             }
717         }
718         *outValue = 0;
719         return -1;
720     }
721 
markSupportedKeyCodes(int32_t deviceId,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags) const722     virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
723             uint8_t* outFlags) const {
724         bool result = false;
725         Device* device = getDevice(deviceId);
726         if (device) {
727             for (size_t i = 0; i < numCodes; i++) {
728                 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
729                     if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
730                         outFlags[i] = 1;
731                         result = true;
732                     }
733                 }
734                 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
735                     if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
736                         outFlags[i] = 1;
737                         result = true;
738                     }
739                 }
740             }
741         }
742         return result;
743     }
744 
hasScanCode(int32_t deviceId,int32_t scanCode) const745     virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
746         Device* device = getDevice(deviceId);
747         if (device) {
748             ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
749             return index >= 0;
750         }
751         return false;
752     }
753 
hasLed(int32_t deviceId,int32_t led) const754     virtual bool hasLed(int32_t deviceId, int32_t led) const {
755         Device* device = getDevice(deviceId);
756         return device && device->leds.indexOfKey(led) >= 0;
757     }
758 
setLedState(int32_t deviceId,int32_t led,bool on)759     virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
760         Device* device = getDevice(deviceId);
761         if (device) {
762             ssize_t index = device->leds.indexOfKey(led);
763             if (index >= 0) {
764                 device->leds.replaceValueAt(led, on);
765             } else {
766                 ADD_FAILURE()
767                         << "Attempted to set the state of an LED that the EventHub declared "
768                         "was not present.  led=" << led;
769             }
770         }
771     }
772 
getVirtualKeyDefinitions(int32_t deviceId,Vector<VirtualKeyDefinition> & outVirtualKeys) const773     virtual void getVirtualKeyDefinitions(int32_t deviceId,
774             Vector<VirtualKeyDefinition>& outVirtualKeys) const {
775         outVirtualKeys.clear();
776 
777         Device* device = getDevice(deviceId);
778         if (device) {
779             outVirtualKeys.appendVector(device->virtualKeys);
780         }
781     }
782 
getKeyCharacterMap(int32_t) const783     virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t) const {
784         return NULL;
785     }
786 
setKeyboardLayoutOverlay(int32_t,const sp<KeyCharacterMap> &)787     virtual bool setKeyboardLayoutOverlay(int32_t, const sp<KeyCharacterMap>&) {
788         return false;
789     }
790 
vibrate(int32_t,nsecs_t)791     virtual void vibrate(int32_t, nsecs_t) {
792     }
793 
cancelVibrate(int32_t)794     virtual void cancelVibrate(int32_t) {
795     }
796 
isExternal(int32_t) const797     virtual bool isExternal(int32_t) const {
798         return false;
799     }
800 
dump(std::string &)801     virtual void dump(std::string&) {
802     }
803 
monitor()804     virtual void monitor() {
805     }
806 
requestReopenDevices()807     virtual void requestReopenDevices() {
808     }
809 
wake()810     virtual void wake() {
811     }
812 };
813 
814 
815 // --- FakeInputReaderContext ---
816 
817 class FakeInputReaderContext : public InputReaderContext {
818     sp<EventHubInterface> mEventHub;
819     sp<InputReaderPolicyInterface> mPolicy;
820     sp<InputListenerInterface> mListener;
821     int32_t mGlobalMetaState;
822     bool mUpdateGlobalMetaStateWasCalled;
823     int32_t mGeneration;
824 
825 public:
FakeInputReaderContext(const sp<EventHubInterface> & eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)826     FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
827             const sp<InputReaderPolicyInterface>& policy,
828             const sp<InputListenerInterface>& listener) :
829             mEventHub(eventHub), mPolicy(policy), mListener(listener),
830             mGlobalMetaState(0) {
831     }
832 
~FakeInputReaderContext()833     virtual ~FakeInputReaderContext() { }
834 
assertUpdateGlobalMetaStateWasCalled()835     void assertUpdateGlobalMetaStateWasCalled() {
836         ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
837                 << "Expected updateGlobalMetaState() to have been called.";
838         mUpdateGlobalMetaStateWasCalled = false;
839     }
840 
setGlobalMetaState(int32_t state)841     void setGlobalMetaState(int32_t state) {
842         mGlobalMetaState = state;
843     }
844 
getGeneration()845     uint32_t getGeneration() {
846         return mGeneration;
847     }
848 
849 private:
updateGlobalMetaState()850     virtual void updateGlobalMetaState() {
851         mUpdateGlobalMetaStateWasCalled = true;
852     }
853 
getGlobalMetaState()854     virtual int32_t getGlobalMetaState() {
855         return mGlobalMetaState;
856     }
857 
getEventHub()858     virtual EventHubInterface* getEventHub() {
859         return mEventHub.get();
860     }
861 
getPolicy()862     virtual InputReaderPolicyInterface* getPolicy() {
863         return mPolicy.get();
864     }
865 
getListener()866     virtual InputListenerInterface* getListener() {
867         return mListener.get();
868     }
869 
disableVirtualKeysUntil(nsecs_t)870     virtual void disableVirtualKeysUntil(nsecs_t) {
871     }
872 
shouldDropVirtualKey(nsecs_t,InputDevice *,int32_t,int32_t)873     virtual bool shouldDropVirtualKey(nsecs_t, InputDevice*, int32_t, int32_t) {
874         return false;
875     }
876 
fadePointer()877     virtual void fadePointer() {
878     }
879 
requestTimeoutAtTime(nsecs_t)880     virtual void requestTimeoutAtTime(nsecs_t) {
881     }
882 
bumpGeneration()883     virtual int32_t bumpGeneration() {
884         return ++mGeneration;
885     }
886 
getExternalStylusDevices(Vector<InputDeviceInfo> & outDevices)887     virtual void getExternalStylusDevices(Vector<InputDeviceInfo>& outDevices) {
888 
889     }
890 
dispatchExternalStylusState(const StylusState &)891     virtual void dispatchExternalStylusState(const StylusState&) {
892 
893     }
894 };
895 
896 
897 // --- FakeInputMapper ---
898 
899 class FakeInputMapper : public InputMapper {
900     uint32_t mSources;
901     int32_t mKeyboardType;
902     int32_t mMetaState;
903     KeyedVector<int32_t, int32_t> mKeyCodeStates;
904     KeyedVector<int32_t, int32_t> mScanCodeStates;
905     KeyedVector<int32_t, int32_t> mSwitchStates;
906     Vector<int32_t> mSupportedKeyCodes;
907     RawEvent mLastEvent;
908 
909     bool mConfigureWasCalled;
910     bool mResetWasCalled;
911     bool mProcessWasCalled;
912 
913 public:
FakeInputMapper(InputDevice * device,uint32_t sources)914     FakeInputMapper(InputDevice* device, uint32_t sources) :
915             InputMapper(device),
916             mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
917             mMetaState(0),
918             mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
919     }
920 
~FakeInputMapper()921     virtual ~FakeInputMapper() { }
922 
setKeyboardType(int32_t keyboardType)923     void setKeyboardType(int32_t keyboardType) {
924         mKeyboardType = keyboardType;
925     }
926 
setMetaState(int32_t metaState)927     void setMetaState(int32_t metaState) {
928         mMetaState = metaState;
929     }
930 
assertConfigureWasCalled()931     void assertConfigureWasCalled() {
932         ASSERT_TRUE(mConfigureWasCalled)
933                 << "Expected configure() to have been called.";
934         mConfigureWasCalled = false;
935     }
936 
assertResetWasCalled()937     void assertResetWasCalled() {
938         ASSERT_TRUE(mResetWasCalled)
939                 << "Expected reset() to have been called.";
940         mResetWasCalled = false;
941     }
942 
assertProcessWasCalled(RawEvent * outLastEvent=NULL)943     void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
944         ASSERT_TRUE(mProcessWasCalled)
945                 << "Expected process() to have been called.";
946         if (outLastEvent) {
947             *outLastEvent = mLastEvent;
948         }
949         mProcessWasCalled = false;
950     }
951 
setKeyCodeState(int32_t keyCode,int32_t state)952     void setKeyCodeState(int32_t keyCode, int32_t state) {
953         mKeyCodeStates.replaceValueFor(keyCode, state);
954     }
955 
setScanCodeState(int32_t scanCode,int32_t state)956     void setScanCodeState(int32_t scanCode, int32_t state) {
957         mScanCodeStates.replaceValueFor(scanCode, state);
958     }
959 
setSwitchState(int32_t switchCode,int32_t state)960     void setSwitchState(int32_t switchCode, int32_t state) {
961         mSwitchStates.replaceValueFor(switchCode, state);
962     }
963 
addSupportedKeyCode(int32_t keyCode)964     void addSupportedKeyCode(int32_t keyCode) {
965         mSupportedKeyCodes.add(keyCode);
966     }
967 
968 private:
getSources()969     virtual uint32_t getSources() {
970         return mSources;
971     }
972 
populateDeviceInfo(InputDeviceInfo * deviceInfo)973     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
974         InputMapper::populateDeviceInfo(deviceInfo);
975 
976         if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
977             deviceInfo->setKeyboardType(mKeyboardType);
978         }
979     }
980 
configure(nsecs_t,const InputReaderConfiguration *,uint32_t)981     virtual void configure(nsecs_t, const InputReaderConfiguration*, uint32_t) {
982         mConfigureWasCalled = true;
983     }
984 
reset(nsecs_t)985     virtual void reset(nsecs_t) {
986         mResetWasCalled = true;
987     }
988 
process(const RawEvent * rawEvent)989     virtual void process(const RawEvent* rawEvent) {
990         mLastEvent = *rawEvent;
991         mProcessWasCalled = true;
992     }
993 
getKeyCodeState(uint32_t,int32_t keyCode)994     virtual int32_t getKeyCodeState(uint32_t, int32_t keyCode) {
995         ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
996         return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
997     }
998 
getScanCodeState(uint32_t,int32_t scanCode)999     virtual int32_t getScanCodeState(uint32_t, int32_t scanCode) {
1000         ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1001         return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1002     }
1003 
getSwitchState(uint32_t,int32_t switchCode)1004     virtual int32_t getSwitchState(uint32_t, int32_t switchCode) {
1005         ssize_t index = mSwitchStates.indexOfKey(switchCode);
1006         return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1007     }
1008 
markSupportedKeyCodes(uint32_t,size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)1009     virtual bool markSupportedKeyCodes(uint32_t, size_t numCodes,
1010             const int32_t* keyCodes, uint8_t* outFlags) {
1011         bool result = false;
1012         for (size_t i = 0; i < numCodes; i++) {
1013             for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1014                 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1015                     outFlags[i] = 1;
1016                     result = true;
1017                 }
1018             }
1019         }
1020         return result;
1021     }
1022 
getMetaState()1023     virtual int32_t getMetaState() {
1024         return mMetaState;
1025     }
1026 
fadePointer()1027     virtual void fadePointer() {
1028     }
1029 };
1030 
1031 
1032 // --- InstrumentedInputReader ---
1033 
1034 class InstrumentedInputReader : public InputReader {
1035     InputDevice* mNextDevice;
1036 
1037 public:
InstrumentedInputReader(const sp<EventHubInterface> & eventHub,const sp<InputReaderPolicyInterface> & policy,const sp<InputListenerInterface> & listener)1038     InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
1039             const sp<InputReaderPolicyInterface>& policy,
1040             const sp<InputListenerInterface>& listener) :
1041             InputReader(eventHub, policy, listener),
1042             mNextDevice(NULL) {
1043     }
1044 
~InstrumentedInputReader()1045     virtual ~InstrumentedInputReader() {
1046         if (mNextDevice) {
1047             delete mNextDevice;
1048         }
1049     }
1050 
setNextDevice(InputDevice * device)1051     void setNextDevice(InputDevice* device) {
1052         mNextDevice = device;
1053     }
1054 
newDevice(int32_t deviceId,int32_t controllerNumber,const String8 & name,uint32_t classes)1055     InputDevice* newDevice(int32_t deviceId, int32_t controllerNumber, const String8& name,
1056             uint32_t classes) {
1057         InputDeviceIdentifier identifier;
1058         identifier.name = name;
1059         int32_t generation = deviceId + 1;
1060         return new InputDevice(&mContext, deviceId, generation, controllerNumber, identifier,
1061                 classes);
1062     }
1063 
1064 protected:
createDeviceLocked(int32_t deviceId,int32_t controllerNumber,const InputDeviceIdentifier & identifier,uint32_t classes)1065     virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
1066             const InputDeviceIdentifier& identifier, uint32_t classes) {
1067         if (mNextDevice) {
1068             InputDevice* device = mNextDevice;
1069             mNextDevice = NULL;
1070             return device;
1071         }
1072         return InputReader::createDeviceLocked(deviceId, controllerNumber, identifier, classes);
1073     }
1074 
1075     friend class InputReaderTest;
1076 };
1077 
1078 
1079 // --- InputReaderTest ---
1080 
1081 class InputReaderTest : public testing::Test {
1082 protected:
1083     sp<FakeInputListener> mFakeListener;
1084     sp<FakeInputReaderPolicy> mFakePolicy;
1085     sp<FakeEventHub> mFakeEventHub;
1086     sp<InstrumentedInputReader> mReader;
1087 
SetUp()1088     virtual void SetUp() {
1089         mFakeEventHub = new FakeEventHub();
1090         mFakePolicy = new FakeInputReaderPolicy();
1091         mFakeListener = new FakeInputListener();
1092 
1093         mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeListener);
1094     }
1095 
TearDown()1096     virtual void TearDown() {
1097         mReader.clear();
1098 
1099         mFakeListener.clear();
1100         mFakePolicy.clear();
1101         mFakeEventHub.clear();
1102     }
1103 
addDevice(int32_t deviceId,const String8 & name,uint32_t classes,const PropertyMap * configuration)1104     void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
1105             const PropertyMap* configuration) {
1106         mFakeEventHub->addDevice(deviceId, name, classes);
1107 
1108         if (configuration) {
1109             mFakeEventHub->addConfigurationMap(deviceId, configuration);
1110         }
1111         mFakeEventHub->finishDeviceScan();
1112         mReader->loopOnce();
1113         mReader->loopOnce();
1114         mFakeEventHub->assertQueueIsEmpty();
1115     }
1116 
disableDevice(int32_t deviceId,InputDevice * device)1117     void disableDevice(int32_t deviceId, InputDevice* device) {
1118         mFakePolicy->addDisabledDevice(deviceId);
1119         configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1120     }
1121 
enableDevice(int32_t deviceId,InputDevice * device)1122     void enableDevice(int32_t deviceId, InputDevice* device) {
1123         mFakePolicy->removeDisabledDevice(deviceId);
1124         configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE, device);
1125     }
1126 
configureDevice(uint32_t changes,InputDevice * device)1127     void configureDevice(uint32_t changes, InputDevice* device) {
1128         device->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1129     }
1130 
addDeviceWithFakeInputMapper(int32_t deviceId,int32_t controllerNumber,const String8 & name,uint32_t classes,uint32_t sources,const PropertyMap * configuration)1131     FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId, int32_t controllerNumber,
1132             const String8& name, uint32_t classes, uint32_t sources,
1133             const PropertyMap* configuration) {
1134         InputDevice* device = mReader->newDevice(deviceId, controllerNumber, name, classes);
1135         FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1136         device->addMapper(mapper);
1137         mReader->setNextDevice(device);
1138         addDevice(deviceId, name, classes, configuration);
1139         return mapper;
1140     }
1141 };
1142 
TEST_F(InputReaderTest,GetInputDevices)1143 TEST_F(InputReaderTest, GetInputDevices) {
1144     ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
1145             INPUT_DEVICE_CLASS_KEYBOARD, NULL));
1146     ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("ignored"),
1147             0, NULL)); // no classes so device will be ignored
1148 
1149     Vector<InputDeviceInfo> inputDevices;
1150     mReader->getInputDevices(inputDevices);
1151 
1152     ASSERT_EQ(1U, inputDevices.size());
1153     ASSERT_EQ(1, inputDevices[0].getId());
1154     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
1155     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1156     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1157     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1158 
1159     // Should also have received a notification describing the new input devices.
1160     inputDevices = mFakePolicy->getInputDevices();
1161     ASSERT_EQ(1U, inputDevices.size());
1162     ASSERT_EQ(1, inputDevices[0].getId());
1163     ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.string());
1164     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1165     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
1166     ASSERT_EQ(size_t(0), inputDevices[0].getMotionRanges().size());
1167 }
1168 
TEST_F(InputReaderTest,WhenEnabledChanges_SendsDeviceResetNotification)1169 TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
1170     constexpr int32_t deviceId = 1;
1171     constexpr uint32_t deviceClass = INPUT_DEVICE_CLASS_KEYBOARD;
1172     InputDevice* device = mReader->newDevice(deviceId, 0, String8("fake"), deviceClass);
1173     // Must add at least one mapper or the device will be ignored!
1174     FakeInputMapper* mapper = new FakeInputMapper(device, AINPUT_SOURCE_KEYBOARD);
1175     device->addMapper(mapper);
1176     mReader->setNextDevice(device);
1177     addDevice(deviceId, String8("fake"), deviceClass, NULL);
1178 
1179     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(NULL));
1180 
1181     NotifyDeviceResetArgs resetArgs;
1182     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1183     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1184     ASSERT_EQ(deviceId, resetArgs.deviceId);
1185 
1186     ASSERT_EQ(device->isEnabled(), true);
1187     disableDevice(deviceId, device);
1188     mReader->loopOnce();
1189 
1190     mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1191     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1192     ASSERT_EQ(deviceId, resetArgs.deviceId);
1193     ASSERT_EQ(device->isEnabled(), false);
1194 
1195     disableDevice(deviceId, device);
1196     mReader->loopOnce();
1197     mFakeListener->assertNotifyDeviceResetWasNotCalled();
1198     mFakeListener->assertNotifyConfigurationChangedWasNotCalled();
1199     ASSERT_EQ(device->isEnabled(), false);
1200 
1201     enableDevice(deviceId, device);
1202     mReader->loopOnce();
1203     mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs);
1204     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1205     ASSERT_EQ(deviceId, resetArgs.deviceId);
1206     ASSERT_EQ(device->isEnabled(), true);
1207 }
1208 
TEST_F(InputReaderTest,GetKeyCodeState_ForwardsRequestsToMappers)1209 TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1210     FakeInputMapper* mapper = NULL;
1211     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1212             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1213     mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1214 
1215     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1216             AINPUT_SOURCE_ANY, AKEYCODE_A))
1217             << "Should return unknown when the device id is >= 0 but unknown.";
1218 
1219     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1220             AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1221             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1222 
1223     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1224             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1225             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1226 
1227     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1228             AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1229             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1230 
1231     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1232             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1233             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1234 }
1235 
TEST_F(InputReaderTest,GetScanCodeState_ForwardsRequestsToMappers)1236 TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1237     FakeInputMapper* mapper = NULL;
1238     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1239             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1240     mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1241 
1242     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1243             AINPUT_SOURCE_ANY, KEY_A))
1244             << "Should return unknown when the device id is >= 0 but unknown.";
1245 
1246     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1247             AINPUT_SOURCE_TRACKBALL, KEY_A))
1248             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1249 
1250     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1251             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1252             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1253 
1254     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1255             AINPUT_SOURCE_TRACKBALL, KEY_A))
1256             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1257 
1258     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1259             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1260             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1261 }
1262 
TEST_F(InputReaderTest,GetSwitchState_ForwardsRequestsToMappers)1263 TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1264     FakeInputMapper* mapper = NULL;
1265     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1266             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1267     mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1268 
1269     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1270             AINPUT_SOURCE_ANY, SW_LID))
1271             << "Should return unknown when the device id is >= 0 but unknown.";
1272 
1273     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1274             AINPUT_SOURCE_TRACKBALL, SW_LID))
1275             << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1276 
1277     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1278             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1279             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1280 
1281     ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1282             AINPUT_SOURCE_TRACKBALL, SW_LID))
1283             << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1284 
1285     ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1286             AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1287             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1288 }
1289 
TEST_F(InputReaderTest,MarkSupportedKeyCodes_ForwardsRequestsToMappers)1290 TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1291     FakeInputMapper* mapper = NULL;
1292     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1293             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1294     mapper->addSupportedKeyCode(AKEYCODE_A);
1295     mapper->addSupportedKeyCode(AKEYCODE_B);
1296 
1297     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1298     uint8_t flags[4] = { 0, 0, 0, 1 };
1299 
1300     ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1301             << "Should return false when device id is >= 0 but unknown.";
1302     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1303 
1304     flags[3] = 1;
1305     ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1306             << "Should return false when device id is valid but the sources are not supported by the device.";
1307     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1308 
1309     flags[3] = 1;
1310     ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1311             << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1312     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1313 
1314     flags[3] = 1;
1315     ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1316             << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1317     ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1318 
1319     flags[3] = 1;
1320     ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1321             << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1322     ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1323 }
1324 
TEST_F(InputReaderTest,LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged)1325 TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
1326     addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
1327 
1328     NotifyConfigurationChangedArgs args;
1329 
1330     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1331     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1332 }
1333 
TEST_F(InputReaderTest,LoopOnce_ForwardsRawEventsToMappers)1334 TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1335     FakeInputMapper* mapper = NULL;
1336     ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, 0, String8("fake"),
1337             INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
1338 
1339     mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, 1);
1340     mReader->loopOnce();
1341     ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1342 
1343     RawEvent event;
1344     ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1345     ASSERT_EQ(0, event.when);
1346     ASSERT_EQ(1, event.deviceId);
1347     ASSERT_EQ(EV_KEY, event.type);
1348     ASSERT_EQ(KEY_A, event.code);
1349     ASSERT_EQ(1, event.value);
1350 }
1351 
1352 
1353 // --- InputDeviceTest ---
1354 
1355 class InputDeviceTest : public testing::Test {
1356 protected:
1357     static const char* DEVICE_NAME;
1358     static const int32_t DEVICE_ID;
1359     static const int32_t DEVICE_GENERATION;
1360     static const int32_t DEVICE_CONTROLLER_NUMBER;
1361     static const uint32_t DEVICE_CLASSES;
1362 
1363     sp<FakeEventHub> mFakeEventHub;
1364     sp<FakeInputReaderPolicy> mFakePolicy;
1365     sp<FakeInputListener> mFakeListener;
1366     FakeInputReaderContext* mFakeContext;
1367 
1368     InputDevice* mDevice;
1369 
SetUp()1370     virtual void SetUp() {
1371         mFakeEventHub = new FakeEventHub();
1372         mFakePolicy = new FakeInputReaderPolicy();
1373         mFakeListener = new FakeInputListener();
1374         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1375 
1376         mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1377         InputDeviceIdentifier identifier;
1378         identifier.name = DEVICE_NAME;
1379         mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1380                 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1381     }
1382 
TearDown()1383     virtual void TearDown() {
1384         delete mDevice;
1385 
1386         delete mFakeContext;
1387         mFakeListener.clear();
1388         mFakePolicy.clear();
1389         mFakeEventHub.clear();
1390     }
1391 };
1392 
1393 const char* InputDeviceTest::DEVICE_NAME = "device";
1394 const int32_t InputDeviceTest::DEVICE_ID = 1;
1395 const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
1396 const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
1397 const uint32_t InputDeviceTest::DEVICE_CLASSES = INPUT_DEVICE_CLASS_KEYBOARD
1398         | INPUT_DEVICE_CLASS_TOUCH | INPUT_DEVICE_CLASS_JOYSTICK;
1399 
TEST_F(InputDeviceTest,ImmutableProperties)1400 TEST_F(InputDeviceTest, ImmutableProperties) {
1401     ASSERT_EQ(DEVICE_ID, mDevice->getId());
1402     ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
1403     ASSERT_EQ(DEVICE_CLASSES, mDevice->getClasses());
1404 }
1405 
TEST_F(InputDeviceTest,WhenDeviceCreated_EnabledIsTrue)1406 TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsTrue) {
1407     ASSERT_EQ(mDevice->isEnabled(), true);
1408 }
1409 
TEST_F(InputDeviceTest,WhenNoMappersAreRegistered_DeviceIsIgnored)1410 TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1411     // Configuration.
1412     InputReaderConfiguration config;
1413     mDevice->configure(ARBITRARY_TIME, &config, 0);
1414 
1415     // Reset.
1416     mDevice->reset(ARBITRARY_TIME);
1417 
1418     NotifyDeviceResetArgs resetArgs;
1419     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1420     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1421     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1422 
1423     // Metadata.
1424     ASSERT_TRUE(mDevice->isIgnored());
1425     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1426 
1427     InputDeviceInfo info;
1428     mDevice->getDeviceInfo(&info);
1429     ASSERT_EQ(DEVICE_ID, info.getId());
1430     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
1431     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1432     ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1433 
1434     // State queries.
1435     ASSERT_EQ(0, mDevice->getMetaState());
1436 
1437     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1438             << "Ignored device should return unknown key code state.";
1439     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1440             << "Ignored device should return unknown scan code state.";
1441     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1442             << "Ignored device should return unknown switch state.";
1443 
1444     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1445     uint8_t flags[2] = { 0, 1 };
1446     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1447             << "Ignored device should never mark any key codes.";
1448     ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1449     ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1450 }
1451 
TEST_F(InputDeviceTest,WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers)1452 TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1453     // Configuration.
1454     mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
1455 
1456     FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1457     mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1458     mapper1->setMetaState(AMETA_ALT_ON);
1459     mapper1->addSupportedKeyCode(AKEYCODE_A);
1460     mapper1->addSupportedKeyCode(AKEYCODE_B);
1461     mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1462     mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1463     mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1464     mapper1->setScanCodeState(3, AKEY_STATE_UP);
1465     mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1466     mDevice->addMapper(mapper1);
1467 
1468     FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1469     mapper2->setMetaState(AMETA_SHIFT_ON);
1470     mDevice->addMapper(mapper2);
1471 
1472     InputReaderConfiguration config;
1473     mDevice->configure(ARBITRARY_TIME, &config, 0);
1474 
1475     String8 propertyValue;
1476     ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1477             << "Device should have read configuration during configuration phase.";
1478     ASSERT_STREQ("value", propertyValue.string());
1479 
1480     ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1481     ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1482 
1483     // Reset
1484     mDevice->reset(ARBITRARY_TIME);
1485     ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1486     ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1487 
1488     NotifyDeviceResetArgs resetArgs;
1489     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1490     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
1491     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
1492 
1493     // Metadata.
1494     ASSERT_FALSE(mDevice->isIgnored());
1495     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1496 
1497     InputDeviceInfo info;
1498     mDevice->getDeviceInfo(&info);
1499     ASSERT_EQ(DEVICE_ID, info.getId());
1500     ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.string());
1501     ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1502     ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1503 
1504     // State queries.
1505     ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1506             << "Should query mappers and combine meta states.";
1507 
1508     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1509             << "Should return unknown key code state when source not supported.";
1510     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1511             << "Should return unknown scan code state when source not supported.";
1512     ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1513             << "Should return unknown switch state when source not supported.";
1514 
1515     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1516             << "Should query mapper when source is supported.";
1517     ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1518             << "Should query mapper when source is supported.";
1519     ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1520             << "Should query mapper when source is supported.";
1521 
1522     const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1523     uint8_t flags[4] = { 0, 0, 0, 1 };
1524     ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1525             << "Should do nothing when source is unsupported.";
1526     ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1527     ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1528     ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1529     ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1530 
1531     ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1532             << "Should query mapper when source is supported.";
1533     ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1534     ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1535     ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1536     ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1537 
1538     // Event handling.
1539     RawEvent event;
1540     mDevice->process(&event, 1);
1541 
1542     ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1543     ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1544 }
1545 
1546 
1547 // --- InputMapperTest ---
1548 
1549 class InputMapperTest : public testing::Test {
1550 protected:
1551     static const char* DEVICE_NAME;
1552     static const int32_t DEVICE_ID;
1553     static const int32_t DEVICE_GENERATION;
1554     static const int32_t DEVICE_CONTROLLER_NUMBER;
1555     static const uint32_t DEVICE_CLASSES;
1556 
1557     sp<FakeEventHub> mFakeEventHub;
1558     sp<FakeInputReaderPolicy> mFakePolicy;
1559     sp<FakeInputListener> mFakeListener;
1560     FakeInputReaderContext* mFakeContext;
1561     InputDevice* mDevice;
1562 
SetUp()1563     virtual void SetUp() {
1564         mFakeEventHub = new FakeEventHub();
1565         mFakePolicy = new FakeInputReaderPolicy();
1566         mFakeListener = new FakeInputListener();
1567         mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeListener);
1568         InputDeviceIdentifier identifier;
1569         identifier.name = DEVICE_NAME;
1570         mDevice = new InputDevice(mFakeContext, DEVICE_ID, DEVICE_GENERATION,
1571                 DEVICE_CONTROLLER_NUMBER, identifier, DEVICE_CLASSES);
1572 
1573         mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1574     }
1575 
TearDown()1576     virtual void TearDown() {
1577         delete mDevice;
1578         delete mFakeContext;
1579         mFakeListener.clear();
1580         mFakePolicy.clear();
1581         mFakeEventHub.clear();
1582     }
1583 
addConfigurationProperty(const char * key,const char * value)1584     void addConfigurationProperty(const char* key, const char* value) {
1585         mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
1586     }
1587 
configureDevice(uint32_t changes)1588     void configureDevice(uint32_t changes) {
1589         mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
1590     }
1591 
addMapperAndConfigure(InputMapper * mapper)1592     void addMapperAndConfigure(InputMapper* mapper) {
1593         mDevice->addMapper(mapper);
1594         configureDevice(0);
1595         mDevice->reset(ARBITRARY_TIME);
1596     }
1597 
setDisplayInfoAndReconfigure(int32_t displayId,int32_t width,int32_t height,int32_t orientation)1598     void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1599             int32_t orientation) {
1600         mFakePolicy->setDisplayViewport(displayId, width, height, orientation, String8::empty());
1601         configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1602     }
1603 
setVirtualDisplayInfoAndReconfigure(int32_t displayId,int32_t width,int32_t height,int32_t orientation,const String8 & uniqueId)1604     void setVirtualDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1605             int32_t orientation, const String8& uniqueId) {
1606         mFakePolicy->setVirtualDisplayViewport(displayId, width, height, orientation, uniqueId);
1607         configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1608     }
1609 
process(InputMapper * mapper,nsecs_t when,int32_t deviceId,int32_t type,int32_t code,int32_t value)1610     static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
1611             int32_t code, int32_t value) {
1612         RawEvent event;
1613         event.when = when;
1614         event.deviceId = deviceId;
1615         event.type = type;
1616         event.code = code;
1617         event.value = value;
1618         mapper->process(&event);
1619     }
1620 
assertMotionRange(const InputDeviceInfo & info,int32_t axis,uint32_t source,float min,float max,float flat,float fuzz)1621     static void assertMotionRange(const InputDeviceInfo& info,
1622             int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1623         const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
1624         ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
1625         ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1626         ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1627         ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1628         ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1629         ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1630         ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
1631     }
1632 
assertPointerCoords(const PointerCoords & coords,float x,float y,float pressure,float size,float touchMajor,float touchMinor,float toolMajor,float toolMinor,float orientation,float distance)1633     static void assertPointerCoords(const PointerCoords& coords,
1634             float x, float y, float pressure, float size,
1635             float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1636             float orientation, float distance) {
1637         ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1638         ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1639         ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1640         ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1641         ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1642         ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1643         ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1644         ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1645         ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
1646         ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
1647     }
1648 
assertPosition(const sp<FakePointerController> & controller,float x,float y)1649     static void assertPosition(const sp<FakePointerController>& controller, float x, float y) {
1650         float actualX, actualY;
1651         controller->getPosition(&actualX, &actualY);
1652         ASSERT_NEAR(x, actualX, 1);
1653         ASSERT_NEAR(y, actualY, 1);
1654     }
1655 };
1656 
1657 const char* InputMapperTest::DEVICE_NAME = "device";
1658 const int32_t InputMapperTest::DEVICE_ID = 1;
1659 const int32_t InputMapperTest::DEVICE_GENERATION = 2;
1660 const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
1661 const uint32_t InputMapperTest::DEVICE_CLASSES = 0; // not needed for current tests
1662 
1663 
1664 // --- SwitchInputMapperTest ---
1665 
1666 class SwitchInputMapperTest : public InputMapperTest {
1667 protected:
1668 };
1669 
TEST_F(SwitchInputMapperTest,GetSources)1670 TEST_F(SwitchInputMapperTest, GetSources) {
1671     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1672     addMapperAndConfigure(mapper);
1673 
1674     ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
1675 }
1676 
TEST_F(SwitchInputMapperTest,GetSwitchState)1677 TEST_F(SwitchInputMapperTest, GetSwitchState) {
1678     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1679     addMapperAndConfigure(mapper);
1680 
1681     mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1682     ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1683 
1684     mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1685     ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1686 }
1687 
TEST_F(SwitchInputMapperTest,Process)1688 TEST_F(SwitchInputMapperTest, Process) {
1689     SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1690     addMapperAndConfigure(mapper);
1691 
1692     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 1);
1693     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
1694     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_HEADPHONE_INSERT, 0);
1695     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
1696 
1697     NotifySwitchArgs args;
1698     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
1699     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1700     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
1701     ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
1702             args.switchMask);
1703     ASSERT_EQ(uint32_t(0), args.policyFlags);
1704 }
1705 
1706 
1707 // --- KeyboardInputMapperTest ---
1708 
1709 class KeyboardInputMapperTest : public InputMapperTest {
1710 protected:
1711     void testDPadKeyRotation(KeyboardInputMapper* mapper,
1712             int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1713 };
1714 
testDPadKeyRotation(KeyboardInputMapper * mapper,int32_t originalScanCode,int32_t,int32_t rotatedKeyCode)1715 void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
1716         int32_t originalScanCode, int32_t, int32_t rotatedKeyCode) {
1717     NotifyKeyArgs args;
1718 
1719     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 1);
1720     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1721     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1722     ASSERT_EQ(originalScanCode, args.scanCode);
1723     ASSERT_EQ(rotatedKeyCode, args.keyCode);
1724 
1725     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, 0);
1726     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1727     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1728     ASSERT_EQ(originalScanCode, args.scanCode);
1729     ASSERT_EQ(rotatedKeyCode, args.keyCode);
1730 }
1731 
1732 
TEST_F(KeyboardInputMapperTest,GetSources)1733 TEST_F(KeyboardInputMapperTest, GetSources) {
1734     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1735             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1736     addMapperAndConfigure(mapper);
1737 
1738     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1739 }
1740 
TEST_F(KeyboardInputMapperTest,Process_SimpleKeyPress)1741 TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
1742     const int32_t USAGE_A = 0x070004;
1743     const int32_t USAGE_UNKNOWN = 0x07ffff;
1744     mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
1745     mFakeEventHub->addKey(DEVICE_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
1746 
1747     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1748             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1749     addMapperAndConfigure(mapper);
1750 
1751     // Key down by scan code.
1752     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1753             EV_KEY, KEY_HOME, 1);
1754     NotifyKeyArgs args;
1755     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1756     ASSERT_EQ(DEVICE_ID, args.deviceId);
1757     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1758     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1759     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1760     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1761     ASSERT_EQ(KEY_HOME, args.scanCode);
1762     ASSERT_EQ(AMETA_NONE, args.metaState);
1763     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1764     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1765     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1766 
1767     // Key up by scan code.
1768     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1769             EV_KEY, KEY_HOME, 0);
1770     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1771     ASSERT_EQ(DEVICE_ID, args.deviceId);
1772     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1773     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1774     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1775     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1776     ASSERT_EQ(KEY_HOME, args.scanCode);
1777     ASSERT_EQ(AMETA_NONE, args.metaState);
1778     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1779     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1780     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1781 
1782     // Key down by usage code.
1783     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1784             EV_MSC, MSC_SCAN, USAGE_A);
1785     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1786             EV_KEY, 0, 1);
1787     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1788     ASSERT_EQ(DEVICE_ID, args.deviceId);
1789     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1790     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1791     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1792     ASSERT_EQ(AKEYCODE_A, args.keyCode);
1793     ASSERT_EQ(0, args.scanCode);
1794     ASSERT_EQ(AMETA_NONE, args.metaState);
1795     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1796     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1797     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1798 
1799     // Key up by usage code.
1800     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1801             EV_MSC, MSC_SCAN, USAGE_A);
1802     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1803             EV_KEY, 0, 0);
1804     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1805     ASSERT_EQ(DEVICE_ID, args.deviceId);
1806     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1807     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1808     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1809     ASSERT_EQ(AKEYCODE_A, args.keyCode);
1810     ASSERT_EQ(0, args.scanCode);
1811     ASSERT_EQ(AMETA_NONE, args.metaState);
1812     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1813     ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1814     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1815 
1816     // Key down with unknown scan code or usage code.
1817     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1818             EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1819     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1820             EV_KEY, KEY_UNKNOWN, 1);
1821     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1822     ASSERT_EQ(DEVICE_ID, args.deviceId);
1823     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1824     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1825     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1826     ASSERT_EQ(0, args.keyCode);
1827     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
1828     ASSERT_EQ(AMETA_NONE, args.metaState);
1829     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1830     ASSERT_EQ(0U, args.policyFlags);
1831     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1832 
1833     // Key up with unknown scan code or usage code.
1834     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1835             EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
1836     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1837             EV_KEY, KEY_UNKNOWN, 0);
1838     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1839     ASSERT_EQ(DEVICE_ID, args.deviceId);
1840     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1841     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1842     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1843     ASSERT_EQ(0, args.keyCode);
1844     ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
1845     ASSERT_EQ(AMETA_NONE, args.metaState);
1846     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1847     ASSERT_EQ(0U, args.policyFlags);
1848     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1849 }
1850 
TEST_F(KeyboardInputMapperTest,Process_ShouldUpdateMetaState)1851 TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
1852     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
1853     mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
1854 
1855     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1856             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1857     addMapperAndConfigure(mapper);
1858 
1859     // Initial metastate.
1860     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1861 
1862     // Metakey down.
1863     process(mapper, ARBITRARY_TIME, DEVICE_ID,
1864             EV_KEY, KEY_LEFTSHIFT, 1);
1865     NotifyKeyArgs args;
1866     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1867     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1868     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1869     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1870 
1871     // Key down.
1872     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1873             EV_KEY, KEY_A, 1);
1874     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1875     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1876     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1877 
1878     // Key up.
1879     process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
1880             EV_KEY, KEY_A, 0);
1881     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1882     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1883     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1884 
1885     // Metakey up.
1886     process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
1887             EV_KEY, KEY_LEFTSHIFT, 0);
1888     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1889     ASSERT_EQ(AMETA_NONE, args.metaState);
1890     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1891     ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1892 }
1893 
TEST_F(KeyboardInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateDPad)1894 TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
1895     mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
1896     mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
1897     mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
1898     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
1899 
1900     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1901             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1902     addMapperAndConfigure(mapper);
1903 
1904     setDisplayInfoAndReconfigure(DISPLAY_ID,
1905             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1906             DISPLAY_ORIENTATION_90);
1907     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1908             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1909     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1910             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1911     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1912             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1913     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1914             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1915 }
1916 
TEST_F(KeyboardInputMapperTest,Process_WhenOrientationAware_ShouldRotateDPad)1917 TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
1918     mFakeEventHub->addKey(DEVICE_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
1919     mFakeEventHub->addKey(DEVICE_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
1920     mFakeEventHub->addKey(DEVICE_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
1921     mFakeEventHub->addKey(DEVICE_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
1922 
1923     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
1924             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1925     addConfigurationProperty("keyboard.orientationAware", "1");
1926     addMapperAndConfigure(mapper);
1927 
1928     setDisplayInfoAndReconfigure(DISPLAY_ID,
1929             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1930             DISPLAY_ORIENTATION_0);
1931     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1932             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1933     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1934             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1935     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1936             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1937     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1938             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1939 
1940     setDisplayInfoAndReconfigure(DISPLAY_ID,
1941             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1942             DISPLAY_ORIENTATION_90);
1943     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1944             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
1945     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1946             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
1947     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1948             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
1949     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1950             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
1951 
1952     setDisplayInfoAndReconfigure(DISPLAY_ID,
1953             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1954             DISPLAY_ORIENTATION_180);
1955     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1956             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
1957     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1958             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
1959     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1960             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
1961     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1962             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
1963 
1964     setDisplayInfoAndReconfigure(DISPLAY_ID,
1965             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1966             DISPLAY_ORIENTATION_270);
1967     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1968             KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
1969     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1970             KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
1971     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1972             KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
1973     ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1974             KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
1975 
1976     // Special case: if orientation changes while key is down, we still emit the same keycode
1977     // in the key up as we did in the key down.
1978     NotifyKeyArgs args;
1979 
1980     setDisplayInfoAndReconfigure(DISPLAY_ID,
1981             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1982             DISPLAY_ORIENTATION_270);
1983     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 1);
1984     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1985     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1986     ASSERT_EQ(KEY_UP, args.scanCode);
1987     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1988 
1989     setDisplayInfoAndReconfigure(DISPLAY_ID,
1990             DISPLAY_WIDTH, DISPLAY_HEIGHT,
1991             DISPLAY_ORIENTATION_180);
1992     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, 0);
1993     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
1994     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1995     ASSERT_EQ(KEY_UP, args.scanCode);
1996     ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1997 }
1998 
TEST_F(KeyboardInputMapperTest,GetKeyCodeState)1999 TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
2000     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2001             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2002     addMapperAndConfigure(mapper);
2003 
2004     mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
2005     ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2006 
2007     mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
2008     ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2009 }
2010 
TEST_F(KeyboardInputMapperTest,GetScanCodeState)2011 TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
2012     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2013             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2014     addMapperAndConfigure(mapper);
2015 
2016     mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
2017     ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2018 
2019     mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
2020     ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2021 }
2022 
TEST_F(KeyboardInputMapperTest,MarkSupportedKeyCodes)2023 TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
2024     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2025             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2026     addMapperAndConfigure(mapper);
2027 
2028     mFakeEventHub->addKey(DEVICE_ID, KEY_A, 0, AKEYCODE_A, 0);
2029 
2030     const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2031     uint8_t flags[2] = { 0, 0 };
2032     ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
2033     ASSERT_TRUE(flags[0]);
2034     ASSERT_FALSE(flags[1]);
2035 }
2036 
TEST_F(KeyboardInputMapperTest,Process_LockedKeysShouldToggleMetaStateAndLeds)2037 TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
2038     mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
2039     mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
2040     mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
2041     mFakeEventHub->addKey(DEVICE_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
2042     mFakeEventHub->addKey(DEVICE_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
2043     mFakeEventHub->addKey(DEVICE_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
2044 
2045     KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
2046             AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2047     addMapperAndConfigure(mapper);
2048 
2049     // Initialization should have turned all of the lights off.
2050     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2051     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2052     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2053 
2054     // Toggle caps lock on.
2055     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2056             EV_KEY, KEY_CAPSLOCK, 1);
2057     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2058             EV_KEY, KEY_CAPSLOCK, 0);
2059     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2060     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2061     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2062     ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
2063 
2064     // Toggle num lock on.
2065     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2066             EV_KEY, KEY_NUMLOCK, 1);
2067     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2068             EV_KEY, KEY_NUMLOCK, 0);
2069     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2070     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2071     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2072     ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
2073 
2074     // Toggle caps lock off.
2075     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2076             EV_KEY, KEY_CAPSLOCK, 1);
2077     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2078             EV_KEY, KEY_CAPSLOCK, 0);
2079     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2080     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2081     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2082     ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
2083 
2084     // Toggle scroll lock on.
2085     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2086             EV_KEY, KEY_SCROLLLOCK, 1);
2087     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2088             EV_KEY, KEY_SCROLLLOCK, 0);
2089     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2090     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2091     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2092     ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2093 
2094     // Toggle num lock off.
2095     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2096             EV_KEY, KEY_NUMLOCK, 1);
2097     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2098             EV_KEY, KEY_NUMLOCK, 0);
2099     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2100     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2101     ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2102     ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2103 
2104     // Toggle scroll lock off.
2105     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2106             EV_KEY, KEY_SCROLLLOCK, 1);
2107     process(mapper, ARBITRARY_TIME, DEVICE_ID,
2108             EV_KEY, KEY_SCROLLLOCK, 0);
2109     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2110     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2111     ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2112     ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2113 }
2114 
2115 
2116 // --- CursorInputMapperTest ---
2117 
2118 class CursorInputMapperTest : public InputMapperTest {
2119 protected:
2120     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2121 
2122     sp<FakePointerController> mFakePointerController;
2123 
SetUp()2124     virtual void SetUp() {
2125         InputMapperTest::SetUp();
2126 
2127         mFakePointerController = new FakePointerController();
2128         mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
2129     }
2130 
2131     void testMotionRotation(CursorInputMapper* mapper,
2132             int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
2133 };
2134 
2135 const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
2136 
testMotionRotation(CursorInputMapper * mapper,int32_t originalX,int32_t originalY,int32_t rotatedX,int32_t rotatedY)2137 void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
2138         int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2139     NotifyMotionArgs args;
2140 
2141     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, originalX);
2142     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, originalY);
2143     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2144     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2145     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2146     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2147             float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2148             float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2149             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2150 }
2151 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_GetSources_ReturnsMouse)2152 TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2153     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2154     addConfigurationProperty("cursor.mode", "pointer");
2155     addMapperAndConfigure(mapper);
2156 
2157     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2158 }
2159 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_GetSources_ReturnsTrackball)2160 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2161     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2162     addConfigurationProperty("cursor.mode", "navigation");
2163     addMapperAndConfigure(mapper);
2164 
2165     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2166 }
2167 
TEST_F(CursorInputMapperTest,WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController)2168 TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2169     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2170     addConfigurationProperty("cursor.mode", "pointer");
2171     addMapperAndConfigure(mapper);
2172 
2173     InputDeviceInfo info;
2174     mapper->populateDeviceInfo(&info);
2175 
2176     // Initially there may not be a valid motion range.
2177     ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2178     ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
2179     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2180             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
2181 
2182     // When the bounds are set, then there should be a valid motion range.
2183     mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
2184 
2185     InputDeviceInfo info2;
2186     mapper->populateDeviceInfo(&info2);
2187 
2188     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2189             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
2190             1, 800 - 1, 0.0f, 0.0f));
2191     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2192             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
2193             2, 480 - 1, 0.0f, 0.0f));
2194     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2195             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
2196             0.0f, 1.0f, 0.0f, 0.0f));
2197 }
2198 
TEST_F(CursorInputMapperTest,WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange)2199 TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2200     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2201     addConfigurationProperty("cursor.mode", "navigation");
2202     addMapperAndConfigure(mapper);
2203 
2204     InputDeviceInfo info;
2205     mapper->populateDeviceInfo(&info);
2206 
2207     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2208             AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
2209             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2210     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2211             AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
2212             -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
2213     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2214             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
2215             0.0f, 1.0f, 0.0f, 0.0f));
2216 }
2217 
TEST_F(CursorInputMapperTest,Process_ShouldSetAllFieldsAndIncludeGlobalMetaState)2218 TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2219     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2220     addConfigurationProperty("cursor.mode", "navigation");
2221     addMapperAndConfigure(mapper);
2222 
2223     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2224 
2225     NotifyMotionArgs args;
2226 
2227     // Button press.
2228     // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
2229     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2230     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2231     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2232     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2233     ASSERT_EQ(DEVICE_ID, args.deviceId);
2234     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2235     ASSERT_EQ(uint32_t(0), args.policyFlags);
2236     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2237     ASSERT_EQ(0, args.flags);
2238     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2239     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2240     ASSERT_EQ(0, args.edgeFlags);
2241     ASSERT_EQ(uint32_t(1), args.pointerCount);
2242     ASSERT_EQ(0, args.pointerProperties[0].id);
2243     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2244     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2245             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2246     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2247     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2248     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2249 
2250     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2251     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2252     ASSERT_EQ(DEVICE_ID, args.deviceId);
2253     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2254     ASSERT_EQ(uint32_t(0), args.policyFlags);
2255     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2256     ASSERT_EQ(0, args.flags);
2257     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2258     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
2259     ASSERT_EQ(0, args.edgeFlags);
2260     ASSERT_EQ(uint32_t(1), args.pointerCount);
2261     ASSERT_EQ(0, args.pointerProperties[0].id);
2262     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2263     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2264             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2265     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2266     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2267     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2268 
2269     // Button release.  Should have same down time.
2270     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2271     process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2272     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2273     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2274     ASSERT_EQ(DEVICE_ID, args.deviceId);
2275     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2276     ASSERT_EQ(uint32_t(0), args.policyFlags);
2277     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2278     ASSERT_EQ(0, args.flags);
2279     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2280     ASSERT_EQ(0, args.buttonState);
2281     ASSERT_EQ(0, args.edgeFlags);
2282     ASSERT_EQ(uint32_t(1), args.pointerCount);
2283     ASSERT_EQ(0, args.pointerProperties[0].id);
2284     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2285     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2286             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2287     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2288     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2289     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2290 
2291     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2292     ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2293     ASSERT_EQ(DEVICE_ID, args.deviceId);
2294     ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2295     ASSERT_EQ(uint32_t(0), args.policyFlags);
2296     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2297     ASSERT_EQ(0, args.flags);
2298     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2299     ASSERT_EQ(0, args.buttonState);
2300     ASSERT_EQ(0, args.edgeFlags);
2301     ASSERT_EQ(uint32_t(1), args.pointerCount);
2302     ASSERT_EQ(0, args.pointerProperties[0].id);
2303     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
2304     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2305             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2306     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2307     ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2308     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2309 }
2310 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentXYUpdates)2311 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2312     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2313     addConfigurationProperty("cursor.mode", "navigation");
2314     addMapperAndConfigure(mapper);
2315 
2316     NotifyMotionArgs args;
2317 
2318     // Motion in X but not Y.
2319     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
2320     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2321     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2322     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2323     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2324             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2325 
2326     // Motion in Y but not X.
2327     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
2328     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2329     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2330     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2331     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2332             0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2333 }
2334 
TEST_F(CursorInputMapperTest,Process_ShouldHandleIndependentButtonUpdates)2335 TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2336     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2337     addConfigurationProperty("cursor.mode", "navigation");
2338     addMapperAndConfigure(mapper);
2339 
2340     NotifyMotionArgs args;
2341 
2342     // Button press.
2343     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2344     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2345     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2346     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2347     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2348             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2349 
2350     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2351     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2352     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2353             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2354 
2355     // Button release.
2356     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2357     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2358     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2359     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2360     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2361             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2362 
2363     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2364     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2365     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2366             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2367 }
2368 
TEST_F(CursorInputMapperTest,Process_ShouldHandleCombinedXYAndButtonUpdates)2369 TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2370     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2371     addConfigurationProperty("cursor.mode", "navigation");
2372     addMapperAndConfigure(mapper);
2373 
2374     NotifyMotionArgs args;
2375 
2376     // Combined X, Y and Button.
2377     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 1);
2378     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, -2);
2379     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2380     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2381     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2382     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2383     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2384             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2385             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2386 
2387     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2388     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2389     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2390             1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2391             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2392 
2393     // Move X, Y a bit while pressed.
2394     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 2);
2395     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 1);
2396     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2397     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2398     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2399     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2400             2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2401             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2402 
2403     // Release Button.
2404     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2405     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2406     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2407     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2408     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2409             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2410 
2411     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2412     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2413     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2414             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2415 }
2416 
TEST_F(CursorInputMapperTest,Process_WhenNotOrientationAware_ShouldNotRotateMotions)2417 TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2418     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2419     addConfigurationProperty("cursor.mode", "navigation");
2420     addMapperAndConfigure(mapper);
2421 
2422     setDisplayInfoAndReconfigure(DISPLAY_ID,
2423             DISPLAY_WIDTH, DISPLAY_HEIGHT,
2424             DISPLAY_ORIENTATION_90);
2425     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2426     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2427     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2428     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2429     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2430     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2431     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2432     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2433 }
2434 
TEST_F(CursorInputMapperTest,Process_WhenOrientationAware_ShouldRotateMotions)2435 TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2436     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2437     addConfigurationProperty("cursor.mode", "navigation");
2438     addConfigurationProperty("cursor.orientationAware", "1");
2439     addMapperAndConfigure(mapper);
2440 
2441     setDisplayInfoAndReconfigure(DISPLAY_ID,
2442             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
2443     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0,  1));
2444     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1,  1));
2445     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  1,  0));
2446     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1, -1));
2447     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0, -1));
2448     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2449     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0, -1,  0));
2450     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1,  1));
2451 
2452     setDisplayInfoAndReconfigure(DISPLAY_ID,
2453             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
2454     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  1,  0));
2455     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1,  1, -1));
2456     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0, -1));
2457     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1, -1));
2458     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1, -1,  0));
2459     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1,  1));
2460     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0,  1));
2461     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1,  1));
2462 
2463     setDisplayInfoAndReconfigure(DISPLAY_ID,
2464             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
2465     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1,  0, -1));
2466     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1, -1));
2467     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0, -1,  0));
2468     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1, -1,  1));
2469     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  0,  1));
2470     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1,  1));
2471     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  1,  0));
2472     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1,  1, -1));
2473 
2474     setDisplayInfoAndReconfigure(DISPLAY_ID,
2475             DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
2476     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0,  1, -1,  0));
2477     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  1, -1,  1));
2478     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1,  0,  0,  1));
2479     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  1, -1,  1,  1));
2480     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper,  0, -1,  1,  0));
2481     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1,  1, -1));
2482     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  0,  0, -1));
2483     ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1,  1, -1, -1));
2484 }
2485 
TEST_F(CursorInputMapperTest,Process_ShouldHandleAllButtons)2486 TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
2487     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2488     addConfigurationProperty("cursor.mode", "pointer");
2489     addMapperAndConfigure(mapper);
2490 
2491     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2492     mFakePointerController->setPosition(100, 200);
2493     mFakePointerController->setButtonState(0);
2494 
2495     NotifyMotionArgs motionArgs;
2496     NotifyKeyArgs keyArgs;
2497 
2498     // press BTN_LEFT, release BTN_LEFT
2499     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 1);
2500     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2501     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2502     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2503     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2504     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2505     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2506             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2507 
2508     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2509     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2510     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
2511     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
2512     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2513             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2514 
2515     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_LEFT, 0);
2516     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2517     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2518     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2519     ASSERT_EQ(0, motionArgs.buttonState);
2520     ASSERT_EQ(0, mFakePointerController->getButtonState());
2521     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2522             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2523 
2524     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2525     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2526     ASSERT_EQ(0, motionArgs.buttonState);
2527     ASSERT_EQ(0, mFakePointerController->getButtonState());
2528     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2529             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2530 
2531     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2532     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2533     ASSERT_EQ(0, motionArgs.buttonState);
2534     ASSERT_EQ(0, mFakePointerController->getButtonState());
2535     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2536             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2537 
2538     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
2539     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 1);
2540     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 1);
2541     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2542     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2543     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2544     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2545             motionArgs.buttonState);
2546     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2547             mFakePointerController->getButtonState());
2548     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2549             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2550 
2551     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2552     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2553     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2554     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2555             mFakePointerController->getButtonState());
2556     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2557             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2558 
2559     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2560     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2561     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2562             motionArgs.buttonState);
2563     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
2564             mFakePointerController->getButtonState());
2565     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2566             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2567 
2568     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_RIGHT, 0);
2569     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2570     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2571     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2572     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2573     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
2574     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2575             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2576 
2577     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2578     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2579     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
2580     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
2581     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2582             100.0f, 200.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2583 
2584     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0);
2585     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2586     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2587     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2588     ASSERT_EQ(0, motionArgs.buttonState);
2589     ASSERT_EQ(0, mFakePointerController->getButtonState());
2590     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2591             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2592     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MIDDLE, 0);
2593     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2594 
2595     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2596     ASSERT_EQ(0, motionArgs.buttonState);
2597     ASSERT_EQ(0, mFakePointerController->getButtonState());
2598     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2599     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2600             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2601 
2602     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2603     ASSERT_EQ(0, motionArgs.buttonState);
2604     ASSERT_EQ(0, mFakePointerController->getButtonState());
2605     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2606     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2607             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2608 
2609     // press BTN_BACK, release BTN_BACK
2610     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 1);
2611     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2612     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2613     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2614     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2615 
2616     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2617     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2618     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2619     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2620     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2621             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2622 
2623     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2624     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2625     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2626     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2627     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2628             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2629 
2630     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_BACK, 0);
2631     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2632     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2633     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2634     ASSERT_EQ(0, motionArgs.buttonState);
2635     ASSERT_EQ(0, mFakePointerController->getButtonState());
2636     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2637             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2638 
2639     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2640     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2641     ASSERT_EQ(0, motionArgs.buttonState);
2642     ASSERT_EQ(0, mFakePointerController->getButtonState());
2643 
2644     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2645             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2646     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2647     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2648     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2649 
2650     // press BTN_SIDE, release BTN_SIDE
2651     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 1);
2652     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2653     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2654     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2655     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2656 
2657     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2658     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2659     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2660     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2661     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2662             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2663 
2664     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2665     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2666     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
2667     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
2668     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2669             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2670 
2671     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_SIDE, 0);
2672     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2673     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2674     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2675     ASSERT_EQ(0, motionArgs.buttonState);
2676     ASSERT_EQ(0, mFakePointerController->getButtonState());
2677     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2678             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2679 
2680     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2681     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2682     ASSERT_EQ(0, motionArgs.buttonState);
2683     ASSERT_EQ(0, mFakePointerController->getButtonState());
2684     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2685             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2686 
2687     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2688     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2689     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
2690 
2691     // press BTN_FORWARD, release BTN_FORWARD
2692     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 1);
2693     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2694     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2695     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2696     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2697 
2698     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2699     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2700     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2701     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2702     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2703             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2704 
2705     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2706     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2707     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2708     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2709     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2710             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2711 
2712     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_FORWARD, 0);
2713     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2714     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2715     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2716     ASSERT_EQ(0, motionArgs.buttonState);
2717     ASSERT_EQ(0, mFakePointerController->getButtonState());
2718     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2719             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2720 
2721     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2722     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2723     ASSERT_EQ(0, motionArgs.buttonState);
2724     ASSERT_EQ(0, mFakePointerController->getButtonState());
2725     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2726             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2727 
2728     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2729     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2730     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2731 
2732     // press BTN_EXTRA, release BTN_EXTRA
2733     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 1);
2734     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2735     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2736     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2737     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2738 
2739     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2740     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2741     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2742     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2743     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2744             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2745 
2746     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2747     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
2748     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
2749     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
2750     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2751             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2752 
2753     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_EXTRA, 0);
2754     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2755     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2756     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
2757     ASSERT_EQ(0, motionArgs.buttonState);
2758     ASSERT_EQ(0, mFakePointerController->getButtonState());
2759     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2760             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2761 
2762     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
2763     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
2764     ASSERT_EQ(0, motionArgs.buttonState);
2765     ASSERT_EQ(0, mFakePointerController->getButtonState());
2766     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2767             100.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2768 
2769     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
2770     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2771     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
2772 }
2773 
TEST_F(CursorInputMapperTest,Process_WhenModeIsPointer_ShouldMoveThePointerAround)2774 TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
2775     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2776     addConfigurationProperty("cursor.mode", "pointer");
2777     addMapperAndConfigure(mapper);
2778 
2779     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2780     mFakePointerController->setPosition(100, 200);
2781     mFakePointerController->setButtonState(0);
2782 
2783     NotifyMotionArgs args;
2784 
2785     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10);
2786     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20);
2787     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2788     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2789     ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
2790     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
2791     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2792             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2793     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
2794 }
2795 
TEST_F(CursorInputMapperTest,Process_PointerCapture)2796 TEST_F(CursorInputMapperTest, Process_PointerCapture) {
2797     CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2798     addConfigurationProperty("cursor.mode", "pointer");
2799     mFakePolicy->setPointerCapture(true);
2800     addMapperAndConfigure(mapper);
2801 
2802     NotifyDeviceResetArgs resetArgs;
2803     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2804     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2805     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2806 
2807     mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
2808     mFakePointerController->setPosition(100, 200);
2809     mFakePointerController->setButtonState(0);
2810 
2811     NotifyMotionArgs args;
2812 
2813     // Move.
2814     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10);
2815     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20);
2816     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2817     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2818     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2819     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2820     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2821             10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2822     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
2823 
2824     // Button press.
2825     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 1);
2826     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2827     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2828     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2829     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2830     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2831             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2832     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2833     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2834     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
2835     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2836             0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2837 
2838     // Button release.
2839     process(mapper, ARBITRARY_TIME + 2, DEVICE_ID, EV_KEY, BTN_MOUSE, 0);
2840     process(mapper, ARBITRARY_TIME + 2, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2841     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2842     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2843     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
2844     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2845             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2846     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2847     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2848     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2849     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2850             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2851 
2852     // Another move.
2853     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 30);
2854     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 40);
2855     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2856     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2857     ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
2858     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2859     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2860             30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2861     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f));
2862 
2863     // Disable pointer capture and check that the device generation got bumped
2864     // and events are generated the usual way.
2865     const uint32_t generation = mFakeContext->getGeneration();
2866     mFakePolicy->setPointerCapture(false);
2867     configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2868     ASSERT_TRUE(mFakeContext->getGeneration() != generation);
2869 
2870     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2871     ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2872     ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2873 
2874     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 10);
2875     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 20);
2876     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
2877     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
2878     ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
2879     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
2880     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2881             110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2882     ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f));
2883 }
2884 
2885 
2886 // --- TouchInputMapperTest ---
2887 
2888 class TouchInputMapperTest : public InputMapperTest {
2889 protected:
2890     static const int32_t RAW_X_MIN;
2891     static const int32_t RAW_X_MAX;
2892     static const int32_t RAW_Y_MIN;
2893     static const int32_t RAW_Y_MAX;
2894     static const int32_t RAW_TOUCH_MIN;
2895     static const int32_t RAW_TOUCH_MAX;
2896     static const int32_t RAW_TOOL_MIN;
2897     static const int32_t RAW_TOOL_MAX;
2898     static const int32_t RAW_PRESSURE_MIN;
2899     static const int32_t RAW_PRESSURE_MAX;
2900     static const int32_t RAW_ORIENTATION_MIN;
2901     static const int32_t RAW_ORIENTATION_MAX;
2902     static const int32_t RAW_DISTANCE_MIN;
2903     static const int32_t RAW_DISTANCE_MAX;
2904     static const int32_t RAW_TILT_MIN;
2905     static const int32_t RAW_TILT_MAX;
2906     static const int32_t RAW_ID_MIN;
2907     static const int32_t RAW_ID_MAX;
2908     static const int32_t RAW_SLOT_MIN;
2909     static const int32_t RAW_SLOT_MAX;
2910     static const float X_PRECISION;
2911     static const float Y_PRECISION;
2912     static const float X_PRECISION_VIRTUAL;
2913     static const float Y_PRECISION_VIRTUAL;
2914 
2915     static const float GEOMETRIC_SCALE;
2916     static const TouchAffineTransformation AFFINE_TRANSFORM;
2917 
2918     static const VirtualKeyDefinition VIRTUAL_KEYS[2];
2919 
2920     enum Axes {
2921         POSITION = 1 << 0,
2922         TOUCH = 1 << 1,
2923         TOOL = 1 << 2,
2924         PRESSURE = 1 << 3,
2925         ORIENTATION = 1 << 4,
2926         MINOR = 1 << 5,
2927         ID = 1 << 6,
2928         DISTANCE = 1 << 7,
2929         TILT = 1 << 8,
2930         SLOT = 1 << 9,
2931         TOOL_TYPE = 1 << 10,
2932     };
2933 
2934     void prepareDisplay(int32_t orientation);
2935     void prepareVirtualDisplay(int32_t orientation);
2936     void prepareVirtualKeys();
2937     void prepareLocationCalibration();
2938     int32_t toRawX(float displayX);
2939     int32_t toRawY(float displayY);
2940     float toCookedX(float rawX, float rawY);
2941     float toCookedY(float rawX, float rawY);
2942     float toDisplayX(int32_t rawX);
2943     float toDisplayX(int32_t rawX, int32_t displayWidth);
2944     float toDisplayY(int32_t rawY);
2945     float toDisplayY(int32_t rawY, int32_t displayHeight);
2946 
2947 };
2948 
2949 const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
2950 const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
2951 const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
2952 const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
2953 const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2954 const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2955 const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2956 const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2957 const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
2958 const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
2959 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2960 const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2961 const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
2962 const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
2963 const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
2964 const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
2965 const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2966 const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
2967 const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
2968 const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
2969 const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2970 const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
2971 const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
2972         float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
2973 const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
2974         float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
2975 const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
2976         TouchAffineTransformation(1, -2, 3, -4, 5, -6);
2977 
2978 const float TouchInputMapperTest::GEOMETRIC_SCALE =
2979         avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
2980                 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
2981 
2982 const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
2983         { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
2984         { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
2985 };
2986 
prepareDisplay(int32_t orientation)2987 void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2988     setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2989 }
2990 
prepareVirtualDisplay(int32_t orientation)2991 void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
2992     setVirtualDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
2993         VIRTUAL_DISPLAY_HEIGHT, orientation, String8(VIRTUAL_DISPLAY_UNIQUE_ID));
2994 }
2995 
prepareVirtualKeys()2996 void TouchInputMapperTest::prepareVirtualKeys() {
2997     mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
2998     mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
2999     mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3000     mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
3001 }
3002 
prepareLocationCalibration()3003 void TouchInputMapperTest::prepareLocationCalibration() {
3004     mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
3005 }
3006 
toRawX(float displayX)3007 int32_t TouchInputMapperTest::toRawX(float displayX) {
3008     return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
3009 }
3010 
toRawY(float displayY)3011 int32_t TouchInputMapperTest::toRawY(float displayY) {
3012     return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
3013 }
3014 
toCookedX(float rawX,float rawY)3015 float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
3016     AFFINE_TRANSFORM.applyTo(rawX, rawY);
3017     return rawX;
3018 }
3019 
toCookedY(float rawX,float rawY)3020 float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
3021     AFFINE_TRANSFORM.applyTo(rawX, rawY);
3022     return rawY;
3023 }
3024 
toDisplayX(int32_t rawX)3025 float TouchInputMapperTest::toDisplayX(int32_t rawX) {
3026     return toDisplayX(rawX, DISPLAY_WIDTH);
3027 }
3028 
toDisplayX(int32_t rawX,int32_t displayWidth)3029 float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
3030     return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
3031 }
3032 
toDisplayY(int32_t rawY)3033 float TouchInputMapperTest::toDisplayY(int32_t rawY) {
3034     return toDisplayY(rawY, DISPLAY_HEIGHT);
3035 }
3036 
toDisplayY(int32_t rawY,int32_t displayHeight)3037 float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
3038     return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
3039 }
3040 
3041 
3042 // --- SingleTouchInputMapperTest ---
3043 
3044 class SingleTouchInputMapperTest : public TouchInputMapperTest {
3045 protected:
3046     void prepareButtons();
3047     void prepareAxes(int axes);
3048 
3049     void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3050     void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
3051     void processUp(SingleTouchInputMapper* mappery);
3052     void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
3053     void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
3054     void processDistance(SingleTouchInputMapper* mapper, int32_t distance);
3055     void processTilt(SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY);
3056     void processKey(SingleTouchInputMapper* mapper, int32_t code, int32_t value);
3057     void processSync(SingleTouchInputMapper* mapper);
3058 };
3059 
prepareButtons()3060 void SingleTouchInputMapperTest::prepareButtons() {
3061     mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
3062 }
3063 
prepareAxes(int axes)3064 void SingleTouchInputMapperTest::prepareAxes(int axes) {
3065     if (axes & POSITION) {
3066         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
3067                 RAW_X_MIN, RAW_X_MAX, 0, 0);
3068         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
3069                 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
3070     }
3071     if (axes & PRESSURE) {
3072         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
3073                 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3074     }
3075     if (axes & TOOL) {
3076         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
3077                 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
3078     }
3079     if (axes & DISTANCE) {
3080         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_DISTANCE,
3081                 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
3082     }
3083     if (axes & TILT) {
3084         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_X,
3085                 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3086         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TILT_Y,
3087                 RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
3088     }
3089 }
3090 
processDown(SingleTouchInputMapper * mapper,int32_t x,int32_t y)3091 void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
3092     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 1);
3093     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
3094     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
3095 }
3096 
processMove(SingleTouchInputMapper * mapper,int32_t x,int32_t y)3097 void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
3098     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, x);
3099     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, y);
3100 }
3101 
processUp(SingleTouchInputMapper * mapper)3102 void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
3103     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0);
3104 }
3105 
processPressure(SingleTouchInputMapper * mapper,int32_t pressure)3106 void SingleTouchInputMapperTest::processPressure(
3107         SingleTouchInputMapper* mapper, int32_t pressure) {
3108     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, pressure);
3109 }
3110 
processToolMajor(SingleTouchInputMapper * mapper,int32_t toolMajor)3111 void SingleTouchInputMapperTest::processToolMajor(
3112         SingleTouchInputMapper* mapper, int32_t toolMajor) {
3113     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
3114 }
3115 
processDistance(SingleTouchInputMapper * mapper,int32_t distance)3116 void SingleTouchInputMapperTest::processDistance(
3117         SingleTouchInputMapper* mapper, int32_t distance) {
3118     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_DISTANCE, distance);
3119 }
3120 
processTilt(SingleTouchInputMapper * mapper,int32_t tiltX,int32_t tiltY)3121 void SingleTouchInputMapperTest::processTilt(
3122         SingleTouchInputMapper* mapper, int32_t tiltX, int32_t tiltY) {
3123     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_X, tiltX);
3124     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TILT_Y, tiltY);
3125 }
3126 
processKey(SingleTouchInputMapper * mapper,int32_t code,int32_t value)3127 void SingleTouchInputMapperTest::processKey(
3128         SingleTouchInputMapper* mapper, int32_t code, int32_t value) {
3129     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
3130 }
3131 
processSync(SingleTouchInputMapper * mapper)3132 void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
3133     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
3134 }
3135 
3136 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer)3137 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
3138     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3139     prepareButtons();
3140     prepareAxes(POSITION);
3141     addMapperAndConfigure(mapper);
3142 
3143     ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
3144 }
3145 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad)3146 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
3147     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3148     mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
3149     mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
3150     prepareButtons();
3151     prepareAxes(POSITION);
3152     addMapperAndConfigure(mapper);
3153 
3154     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3155 }
3156 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad)3157 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
3158     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3159     prepareButtons();
3160     prepareAxes(POSITION);
3161     addConfigurationProperty("touch.deviceType", "touchPad");
3162     addMapperAndConfigure(mapper);
3163 
3164     ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
3165 }
3166 
TEST_F(SingleTouchInputMapperTest,GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen)3167 TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
3168     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3169     prepareButtons();
3170     prepareAxes(POSITION);
3171     addConfigurationProperty("touch.deviceType", "touchScreen");
3172     addMapperAndConfigure(mapper);
3173 
3174     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
3175 }
3176 
TEST_F(SingleTouchInputMapperTest,GetKeyCodeState)3177 TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
3178     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3179     addConfigurationProperty("touch.deviceType", "touchScreen");
3180     prepareDisplay(DISPLAY_ORIENTATION_0);
3181     prepareButtons();
3182     prepareAxes(POSITION);
3183     prepareVirtualKeys();
3184     addMapperAndConfigure(mapper);
3185 
3186     // Unknown key.
3187     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
3188 
3189     // Virtual key is down.
3190     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3191     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3192     processDown(mapper, x, y);
3193     processSync(mapper);
3194     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3195 
3196     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3197 
3198     // Virtual key is up.
3199     processUp(mapper);
3200     processSync(mapper);
3201     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3202 
3203     ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
3204 }
3205 
TEST_F(SingleTouchInputMapperTest,GetScanCodeState)3206 TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
3207     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3208     addConfigurationProperty("touch.deviceType", "touchScreen");
3209     prepareDisplay(DISPLAY_ORIENTATION_0);
3210     prepareButtons();
3211     prepareAxes(POSITION);
3212     prepareVirtualKeys();
3213     addMapperAndConfigure(mapper);
3214 
3215     // Unknown key.
3216     ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
3217 
3218     // Virtual key is down.
3219     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3220     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3221     processDown(mapper, x, y);
3222     processSync(mapper);
3223     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3224 
3225     ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3226 
3227     // Virtual key is up.
3228     processUp(mapper);
3229     processSync(mapper);
3230     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
3231 
3232     ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
3233 }
3234 
TEST_F(SingleTouchInputMapperTest,MarkSupportedKeyCodes)3235 TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
3236     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3237     addConfigurationProperty("touch.deviceType", "touchScreen");
3238     prepareDisplay(DISPLAY_ORIENTATION_0);
3239     prepareButtons();
3240     prepareAxes(POSITION);
3241     prepareVirtualKeys();
3242     addMapperAndConfigure(mapper);
3243 
3244     const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
3245     uint8_t flags[2] = { 0, 0 };
3246     ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
3247     ASSERT_TRUE(flags[0]);
3248     ASSERT_FALSE(flags[1]);
3249 }
3250 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp)3251 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
3252     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3253     addConfigurationProperty("touch.deviceType", "touchScreen");
3254     prepareDisplay(DISPLAY_ORIENTATION_0);
3255     prepareButtons();
3256     prepareAxes(POSITION);
3257     prepareVirtualKeys();
3258     addMapperAndConfigure(mapper);
3259 
3260     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3261 
3262     NotifyKeyArgs args;
3263 
3264     // Press virtual key.
3265     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3266     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3267     processDown(mapper, x, y);
3268     processSync(mapper);
3269 
3270     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3271     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3272     ASSERT_EQ(DEVICE_ID, args.deviceId);
3273     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3274     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3275     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3276     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3277     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3278     ASSERT_EQ(KEY_HOME, args.scanCode);
3279     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3280     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3281 
3282     // Release virtual key.
3283     processUp(mapper);
3284     processSync(mapper);
3285 
3286     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3287     ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3288     ASSERT_EQ(DEVICE_ID, args.deviceId);
3289     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3290     ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
3291     ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3292     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
3293     ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3294     ASSERT_EQ(KEY_HOME, args.scanCode);
3295     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3296     ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3297 
3298     // Should not have sent any motions.
3299     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3300 }
3301 
TEST_F(SingleTouchInputMapperTest,Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel)3302 TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
3303     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3304     addConfigurationProperty("touch.deviceType", "touchScreen");
3305     prepareDisplay(DISPLAY_ORIENTATION_0);
3306     prepareButtons();
3307     prepareAxes(POSITION);
3308     prepareVirtualKeys();
3309     addMapperAndConfigure(mapper);
3310 
3311     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3312 
3313     NotifyKeyArgs keyArgs;
3314 
3315     // Press virtual key.
3316     int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
3317     int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
3318     processDown(mapper, x, y);
3319     processSync(mapper);
3320 
3321     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3322     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3323     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3324     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3325     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3326     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3327     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
3328     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3329     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3330     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3331     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3332 
3333     // Move out of bounds.  This should generate a cancel and a pointer down since we moved
3334     // into the display area.
3335     y -= 100;
3336     processMove(mapper, x, y);
3337     processSync(mapper);
3338 
3339     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3340     ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
3341     ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
3342     ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
3343     ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
3344     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3345     ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3346             | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
3347     ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
3348     ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
3349     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
3350     ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
3351 
3352     NotifyMotionArgs motionArgs;
3353     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3354     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3355     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3356     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3357     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3358     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3359     ASSERT_EQ(0, motionArgs.flags);
3360     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3361     ASSERT_EQ(0, motionArgs.buttonState);
3362     ASSERT_EQ(0, motionArgs.edgeFlags);
3363     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3364     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3365     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3366     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3367             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3368     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3369     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3370     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3371 
3372     // Keep moving out of bounds.  Should generate a pointer move.
3373     y -= 50;
3374     processMove(mapper, x, y);
3375     processSync(mapper);
3376 
3377     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3378     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3379     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3380     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3381     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3382     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3383     ASSERT_EQ(0, motionArgs.flags);
3384     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3385     ASSERT_EQ(0, motionArgs.buttonState);
3386     ASSERT_EQ(0, motionArgs.edgeFlags);
3387     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3388     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3389     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3390     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3391             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3392     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3393     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3394     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3395 
3396     // Release out of bounds.  Should generate a pointer up.
3397     processUp(mapper);
3398     processSync(mapper);
3399 
3400     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3401     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3402     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3403     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3404     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3405     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3406     ASSERT_EQ(0, motionArgs.flags);
3407     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3408     ASSERT_EQ(0, motionArgs.buttonState);
3409     ASSERT_EQ(0, motionArgs.edgeFlags);
3410     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3411     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3412     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3413     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3414             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3415     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3416     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3417     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3418 
3419     // Should not have sent any more keys or motions.
3420     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3421     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3422 }
3423 
TEST_F(SingleTouchInputMapperTest,Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay)3424 TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
3425     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3426     addConfigurationProperty("touch.deviceType", "touchScreen");
3427     prepareDisplay(DISPLAY_ORIENTATION_0);
3428     prepareButtons();
3429     prepareAxes(POSITION);
3430     prepareVirtualKeys();
3431     addMapperAndConfigure(mapper);
3432 
3433     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3434 
3435     NotifyMotionArgs motionArgs;
3436 
3437     // Initially go down out of bounds.
3438     int32_t x = -10;
3439     int32_t y = -10;
3440     processDown(mapper, x, y);
3441     processSync(mapper);
3442 
3443     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3444 
3445     // Move into the display area.  Should generate a pointer down.
3446     x = 50;
3447     y = 75;
3448     processMove(mapper, x, y);
3449     processSync(mapper);
3450 
3451     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3452     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3453     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3454     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3455     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3456     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3457     ASSERT_EQ(0, motionArgs.flags);
3458     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3459     ASSERT_EQ(0, motionArgs.buttonState);
3460     ASSERT_EQ(0, motionArgs.edgeFlags);
3461     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3462     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3463     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3464     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3465             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3466     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3467     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3468     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3469 
3470     // Release.  Should generate a pointer up.
3471     processUp(mapper);
3472     processSync(mapper);
3473 
3474     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3475     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3476     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3477     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3478     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3479     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3480     ASSERT_EQ(0, motionArgs.flags);
3481     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3482     ASSERT_EQ(0, motionArgs.buttonState);
3483     ASSERT_EQ(0, motionArgs.edgeFlags);
3484     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3485     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3486     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3487     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3488             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3489     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3490     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3491     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3492 
3493     // Should not have sent any more keys or motions.
3494     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3495     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3496 }
3497 
TEST_F(SingleTouchInputMapperTest,Process_NormalSingleTouchGesture_VirtualDisplay)3498 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
3499     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3500     addConfigurationProperty("touch.deviceType", "touchScreen");
3501     addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
3502 
3503     prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
3504     prepareButtons();
3505     prepareAxes(POSITION);
3506     prepareVirtualKeys();
3507     addMapperAndConfigure(mapper);
3508 
3509     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3510 
3511     NotifyMotionArgs motionArgs;
3512 
3513     // Down.
3514     int32_t x = 100;
3515     int32_t y = 125;
3516     processDown(mapper, x, y);
3517     processSync(mapper);
3518 
3519     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3520     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3521     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3522     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3523     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3524     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3525     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3526     ASSERT_EQ(0, motionArgs.flags);
3527     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3528     ASSERT_EQ(0, motionArgs.buttonState);
3529     ASSERT_EQ(0, motionArgs.edgeFlags);
3530     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3531     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3532     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3533     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3534             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3535             1, 0, 0, 0, 0, 0, 0, 0));
3536     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3537     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3538     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3539 
3540     // Move.
3541     x += 50;
3542     y += 75;
3543     processMove(mapper, x, y);
3544     processSync(mapper);
3545 
3546     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3547     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3548     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3549     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3550     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3551     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3552     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3553     ASSERT_EQ(0, motionArgs.flags);
3554     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3555     ASSERT_EQ(0, motionArgs.buttonState);
3556     ASSERT_EQ(0, motionArgs.edgeFlags);
3557     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3558     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3559     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3560     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3561             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3562             1, 0, 0, 0, 0, 0, 0, 0));
3563     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3564     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3565     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3566 
3567     // Up.
3568     processUp(mapper);
3569     processSync(mapper);
3570 
3571     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3572     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3573     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3574     ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
3575     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3576     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3577     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3578     ASSERT_EQ(0, motionArgs.flags);
3579     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3580     ASSERT_EQ(0, motionArgs.buttonState);
3581     ASSERT_EQ(0, motionArgs.edgeFlags);
3582     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3583     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3584     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3585     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3586             toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
3587             1, 0, 0, 0, 0, 0, 0, 0));
3588     ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
3589     ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
3590     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3591 
3592     // Should not have sent any more keys or motions.
3593     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3594     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3595 }
3596 
TEST_F(SingleTouchInputMapperTest,Process_NormalSingleTouchGesture)3597 TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
3598     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3599     addConfigurationProperty("touch.deviceType", "touchScreen");
3600     prepareDisplay(DISPLAY_ORIENTATION_0);
3601     prepareButtons();
3602     prepareAxes(POSITION);
3603     prepareVirtualKeys();
3604     addMapperAndConfigure(mapper);
3605 
3606     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3607 
3608     NotifyMotionArgs motionArgs;
3609 
3610     // Down.
3611     int32_t x = 100;
3612     int32_t y = 125;
3613     processDown(mapper, x, y);
3614     processSync(mapper);
3615 
3616     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3617     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3618     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3619     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3620     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3621     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3622     ASSERT_EQ(0, motionArgs.flags);
3623     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3624     ASSERT_EQ(0, motionArgs.buttonState);
3625     ASSERT_EQ(0, motionArgs.edgeFlags);
3626     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3627     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3628     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3629     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3630             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3631     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3632     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3633     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3634 
3635     // Move.
3636     x += 50;
3637     y += 75;
3638     processMove(mapper, x, y);
3639     processSync(mapper);
3640 
3641     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3642     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3643     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3644     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3645     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3646     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3647     ASSERT_EQ(0, motionArgs.flags);
3648     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3649     ASSERT_EQ(0, motionArgs.buttonState);
3650     ASSERT_EQ(0, motionArgs.edgeFlags);
3651     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3652     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3653     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3654     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3655             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3656     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3657     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3658     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3659 
3660     // Up.
3661     processUp(mapper);
3662     processSync(mapper);
3663 
3664     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3665     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3666     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3667     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3668     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3669     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3670     ASSERT_EQ(0, motionArgs.flags);
3671     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
3672     ASSERT_EQ(0, motionArgs.buttonState);
3673     ASSERT_EQ(0, motionArgs.edgeFlags);
3674     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
3675     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3676     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3677     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3678             toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
3679     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3680     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3681     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3682 
3683     // Should not have sent any more keys or motions.
3684     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
3685     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
3686 }
3687 
TEST_F(SingleTouchInputMapperTest,Process_WhenNotOrientationAware_DoesNotRotateMotions)3688 TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3689     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3690     addConfigurationProperty("touch.deviceType", "touchScreen");
3691     prepareButtons();
3692     prepareAxes(POSITION);
3693     addConfigurationProperty("touch.orientationAware", "0");
3694     addMapperAndConfigure(mapper);
3695 
3696     NotifyMotionArgs args;
3697 
3698     // Rotation 90.
3699     prepareDisplay(DISPLAY_ORIENTATION_90);
3700     processDown(mapper, toRawX(50), toRawY(75));
3701     processSync(mapper);
3702 
3703     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3704     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3705     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3706 
3707     processUp(mapper);
3708     processSync(mapper);
3709     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3710 }
3711 
TEST_F(SingleTouchInputMapperTest,Process_WhenOrientationAware_RotatesMotions)3712 TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3713     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3714     addConfigurationProperty("touch.deviceType", "touchScreen");
3715     prepareButtons();
3716     prepareAxes(POSITION);
3717     addMapperAndConfigure(mapper);
3718 
3719     NotifyMotionArgs args;
3720 
3721     // Rotation 0.
3722     prepareDisplay(DISPLAY_ORIENTATION_0);
3723     processDown(mapper, toRawX(50), toRawY(75));
3724     processSync(mapper);
3725 
3726     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3727     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3728     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3729 
3730     processUp(mapper);
3731     processSync(mapper);
3732     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3733 
3734     // Rotation 90.
3735     prepareDisplay(DISPLAY_ORIENTATION_90);
3736     processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
3737     processSync(mapper);
3738 
3739     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3740     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3741     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3742 
3743     processUp(mapper);
3744     processSync(mapper);
3745     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3746 
3747     // Rotation 180.
3748     prepareDisplay(DISPLAY_ORIENTATION_180);
3749     processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
3750     processSync(mapper);
3751 
3752     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3753     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3754     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3755 
3756     processUp(mapper);
3757     processSync(mapper);
3758     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3759 
3760     // Rotation 270.
3761     prepareDisplay(DISPLAY_ORIENTATION_270);
3762     processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
3763     processSync(mapper);
3764 
3765     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3766     ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3767     ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
3768 
3769     processUp(mapper);
3770     processSync(mapper);
3771     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
3772 }
3773 
TEST_F(SingleTouchInputMapperTest,Process_AllAxes_DefaultCalibration)3774 TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
3775     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3776     addConfigurationProperty("touch.deviceType", "touchScreen");
3777     prepareDisplay(DISPLAY_ORIENTATION_0);
3778     prepareButtons();
3779     prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
3780     addMapperAndConfigure(mapper);
3781 
3782     // These calculations are based on the input device calibration documentation.
3783     int32_t rawX = 100;
3784     int32_t rawY = 200;
3785     int32_t rawPressure = 10;
3786     int32_t rawToolMajor = 12;
3787     int32_t rawDistance = 2;
3788     int32_t rawTiltX = 30;
3789     int32_t rawTiltY = 110;
3790 
3791     float x = toDisplayX(rawX);
3792     float y = toDisplayY(rawY);
3793     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3794     float size = float(rawToolMajor) / RAW_TOOL_MAX;
3795     float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
3796     float distance = float(rawDistance);
3797 
3798     float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
3799     float tiltScale = M_PI / 180;
3800     float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
3801     float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
3802     float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3803     float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3804 
3805     processDown(mapper, rawX, rawY);
3806     processPressure(mapper, rawPressure);
3807     processToolMajor(mapper, rawToolMajor);
3808     processDistance(mapper, rawDistance);
3809     processTilt(mapper, rawTiltX, rawTiltY);
3810     processSync(mapper);
3811 
3812     NotifyMotionArgs args;
3813     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3814     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3815             x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
3816     ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
3817 }
3818 
TEST_F(SingleTouchInputMapperTest,Process_XYAxes_AffineCalibration)3819 TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
3820     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3821     addConfigurationProperty("touch.deviceType", "touchScreen");
3822     prepareDisplay(DISPLAY_ORIENTATION_0);
3823     prepareLocationCalibration();
3824     prepareButtons();
3825     prepareAxes(POSITION);
3826     addMapperAndConfigure(mapper);
3827 
3828     int32_t rawX = 100;
3829     int32_t rawY = 200;
3830 
3831     float x = toDisplayX(toCookedX(rawX, rawY));
3832     float y = toDisplayY(toCookedY(rawX, rawY));
3833 
3834     processDown(mapper, rawX, rawY);
3835     processSync(mapper);
3836 
3837     NotifyMotionArgs args;
3838     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3839     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3840             x, y, 1, 0, 0, 0, 0, 0, 0, 0));
3841 }
3842 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllButtons)3843 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
3844     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
3845     addConfigurationProperty("touch.deviceType", "touchScreen");
3846     prepareDisplay(DISPLAY_ORIENTATION_0);
3847     prepareButtons();
3848     prepareAxes(POSITION);
3849     addMapperAndConfigure(mapper);
3850 
3851     NotifyMotionArgs motionArgs;
3852     NotifyKeyArgs keyArgs;
3853 
3854     processDown(mapper, 100, 200);
3855     processSync(mapper);
3856     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3857     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3858     ASSERT_EQ(0, motionArgs.buttonState);
3859 
3860     // press BTN_LEFT, release BTN_LEFT
3861     processKey(mapper, BTN_LEFT, 1);
3862     processSync(mapper);
3863     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3864     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3865     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3866 
3867     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3868     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3869     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
3870 
3871     processKey(mapper, BTN_LEFT, 0);
3872     processSync(mapper);
3873     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3874     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3875     ASSERT_EQ(0, motionArgs.buttonState);
3876 
3877     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3878     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3879     ASSERT_EQ(0, motionArgs.buttonState);
3880 
3881     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
3882     processKey(mapper, BTN_RIGHT, 1);
3883     processKey(mapper, BTN_MIDDLE, 1);
3884     processSync(mapper);
3885     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3886     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3887     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3888             motionArgs.buttonState);
3889 
3890     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3891     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3892     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3893 
3894     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3895     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3896     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
3897             motionArgs.buttonState);
3898 
3899     processKey(mapper, BTN_RIGHT, 0);
3900     processSync(mapper);
3901     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3902     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3903     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3904 
3905     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3906     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3907     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
3908 
3909     processKey(mapper, BTN_MIDDLE, 0);
3910     processSync(mapper);
3911     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3912     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3913     ASSERT_EQ(0, motionArgs.buttonState);
3914 
3915     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3916     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3917     ASSERT_EQ(0, motionArgs.buttonState);
3918 
3919     // press BTN_BACK, release BTN_BACK
3920     processKey(mapper, BTN_BACK, 1);
3921     processSync(mapper);
3922     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3923     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3924     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3925 
3926     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3927     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3928     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3929 
3930     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3931     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3932     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3933 
3934     processKey(mapper, BTN_BACK, 0);
3935     processSync(mapper);
3936     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3937     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3938     ASSERT_EQ(0, motionArgs.buttonState);
3939 
3940     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3941     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3942     ASSERT_EQ(0, motionArgs.buttonState);
3943 
3944     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3945     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3946     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3947 
3948     // press BTN_SIDE, release BTN_SIDE
3949     processKey(mapper, BTN_SIDE, 1);
3950     processSync(mapper);
3951     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3952     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3953     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3954 
3955     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3956     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3957     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3958 
3959     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3960     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3961     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
3962 
3963     processKey(mapper, BTN_SIDE, 0);
3964     processSync(mapper);
3965     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3966     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3967     ASSERT_EQ(0, motionArgs.buttonState);
3968 
3969     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3970     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3971     ASSERT_EQ(0, motionArgs.buttonState);
3972 
3973     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3974     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
3975     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
3976 
3977     // press BTN_FORWARD, release BTN_FORWARD
3978     processKey(mapper, BTN_FORWARD, 1);
3979     processSync(mapper);
3980     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
3981     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
3982     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
3983 
3984     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3985     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3986     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3987 
3988     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3989     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
3990     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
3991 
3992     processKey(mapper, BTN_FORWARD, 0);
3993     processSync(mapper);
3994     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3995     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
3996     ASSERT_EQ(0, motionArgs.buttonState);
3997 
3998     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
3999     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4000     ASSERT_EQ(0, motionArgs.buttonState);
4001 
4002     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4003     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4004     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4005 
4006     // press BTN_EXTRA, release BTN_EXTRA
4007     processKey(mapper, BTN_EXTRA, 1);
4008     processSync(mapper);
4009     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4010     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4011     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4012 
4013     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4014     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4015     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4016 
4017     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4018     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4019     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4020 
4021     processKey(mapper, BTN_EXTRA, 0);
4022     processSync(mapper);
4023     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4024     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4025     ASSERT_EQ(0, motionArgs.buttonState);
4026 
4027     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4028     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4029     ASSERT_EQ(0, motionArgs.buttonState);
4030 
4031     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4032     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4033     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4034 
4035     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4036 
4037     // press BTN_STYLUS, release BTN_STYLUS
4038     processKey(mapper, BTN_STYLUS, 1);
4039     processSync(mapper);
4040     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4041     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4042     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4043 
4044     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4045     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4046     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
4047 
4048     processKey(mapper, BTN_STYLUS, 0);
4049     processSync(mapper);
4050     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4051     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4052     ASSERT_EQ(0, motionArgs.buttonState);
4053 
4054     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4055     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4056     ASSERT_EQ(0, motionArgs.buttonState);
4057 
4058     // press BTN_STYLUS2, release BTN_STYLUS2
4059     processKey(mapper, BTN_STYLUS2, 1);
4060     processSync(mapper);
4061     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4062     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4063     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4064 
4065     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4066     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4067     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
4068 
4069     processKey(mapper, BTN_STYLUS2, 0);
4070     processSync(mapper);
4071     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4072     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4073     ASSERT_EQ(0, motionArgs.buttonState);
4074 
4075     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4076     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4077     ASSERT_EQ(0, motionArgs.buttonState);
4078 
4079     // release touch
4080     processUp(mapper);
4081     processSync(mapper);
4082     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4083     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4084     ASSERT_EQ(0, motionArgs.buttonState);
4085 }
4086 
TEST_F(SingleTouchInputMapperTest,Process_ShouldHandleAllToolTypes)4087 TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
4088     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4089     addConfigurationProperty("touch.deviceType", "touchScreen");
4090     prepareDisplay(DISPLAY_ORIENTATION_0);
4091     prepareButtons();
4092     prepareAxes(POSITION);
4093     addMapperAndConfigure(mapper);
4094 
4095     NotifyMotionArgs motionArgs;
4096 
4097     // default tool type is finger
4098     processDown(mapper, 100, 200);
4099     processSync(mapper);
4100     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4101     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4102     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4103 
4104     // eraser
4105     processKey(mapper, BTN_TOOL_RUBBER, 1);
4106     processSync(mapper);
4107     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4108     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4109     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4110 
4111     // stylus
4112     processKey(mapper, BTN_TOOL_RUBBER, 0);
4113     processKey(mapper, BTN_TOOL_PEN, 1);
4114     processSync(mapper);
4115     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4116     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4117     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4118 
4119     // brush
4120     processKey(mapper, BTN_TOOL_PEN, 0);
4121     processKey(mapper, BTN_TOOL_BRUSH, 1);
4122     processSync(mapper);
4123     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4124     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4125     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4126 
4127     // pencil
4128     processKey(mapper, BTN_TOOL_BRUSH, 0);
4129     processKey(mapper, BTN_TOOL_PENCIL, 1);
4130     processSync(mapper);
4131     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4132     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4133     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4134 
4135     // airbrush
4136     processKey(mapper, BTN_TOOL_PENCIL, 0);
4137     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
4138     processSync(mapper);
4139     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4140     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4141     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4142 
4143     // mouse
4144     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
4145     processKey(mapper, BTN_TOOL_MOUSE, 1);
4146     processSync(mapper);
4147     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4148     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4149     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4150 
4151     // lens
4152     processKey(mapper, BTN_TOOL_MOUSE, 0);
4153     processKey(mapper, BTN_TOOL_LENS, 1);
4154     processSync(mapper);
4155     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4156     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4157     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4158 
4159     // double-tap
4160     processKey(mapper, BTN_TOOL_LENS, 0);
4161     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
4162     processSync(mapper);
4163     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4164     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4165     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4166 
4167     // triple-tap
4168     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
4169     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
4170     processSync(mapper);
4171     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4172     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4173     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4174 
4175     // quad-tap
4176     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
4177     processKey(mapper, BTN_TOOL_QUADTAP, 1);
4178     processSync(mapper);
4179     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4180     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4181     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4182 
4183     // finger
4184     processKey(mapper, BTN_TOOL_QUADTAP, 0);
4185     processKey(mapper, BTN_TOOL_FINGER, 1);
4186     processSync(mapper);
4187     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4188     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4189     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4190 
4191     // stylus trumps finger
4192     processKey(mapper, BTN_TOOL_PEN, 1);
4193     processSync(mapper);
4194     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4195     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4196     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
4197 
4198     // eraser trumps stylus
4199     processKey(mapper, BTN_TOOL_RUBBER, 1);
4200     processSync(mapper);
4201     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4202     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4203     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
4204 
4205     // mouse trumps eraser
4206     processKey(mapper, BTN_TOOL_MOUSE, 1);
4207     processSync(mapper);
4208     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4209     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4210     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
4211 
4212     // back to default tool type
4213     processKey(mapper, BTN_TOOL_MOUSE, 0);
4214     processKey(mapper, BTN_TOOL_RUBBER, 0);
4215     processKey(mapper, BTN_TOOL_PEN, 0);
4216     processKey(mapper, BTN_TOOL_FINGER, 0);
4217     processSync(mapper);
4218     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4220     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4221 }
4222 
TEST_F(SingleTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)4223 TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
4224     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4225     addConfigurationProperty("touch.deviceType", "touchScreen");
4226     prepareDisplay(DISPLAY_ORIENTATION_0);
4227     prepareButtons();
4228     prepareAxes(POSITION);
4229     mFakeEventHub->addKey(DEVICE_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
4230     addMapperAndConfigure(mapper);
4231 
4232     NotifyMotionArgs motionArgs;
4233 
4234     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
4235     processKey(mapper, BTN_TOOL_FINGER, 1);
4236     processMove(mapper, 100, 200);
4237     processSync(mapper);
4238     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4239     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4240     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4241             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4242 
4243     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4245     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4246             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4247 
4248     // move a little
4249     processMove(mapper, 150, 250);
4250     processSync(mapper);
4251     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4252     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4253     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4254             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4255 
4256     // down when BTN_TOUCH is pressed, pressure defaults to 1
4257     processKey(mapper, BTN_TOUCH, 1);
4258     processSync(mapper);
4259     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4260     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4261     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4262             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4263 
4264     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4265     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4266     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4267             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4268 
4269     // up when BTN_TOUCH is released, hover restored
4270     processKey(mapper, BTN_TOUCH, 0);
4271     processSync(mapper);
4272     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4273     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4274     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4275             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4276 
4277     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4278     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4279     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4280             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4281 
4282     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4283     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4284     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4285             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4286 
4287     // exit hover when pointer goes away
4288     processKey(mapper, BTN_TOOL_FINGER, 0);
4289     processSync(mapper);
4290     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4292     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4293             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4294 }
4295 
TEST_F(SingleTouchInputMapperTest,Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero)4296 TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
4297     SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
4298     addConfigurationProperty("touch.deviceType", "touchScreen");
4299     prepareDisplay(DISPLAY_ORIENTATION_0);
4300     prepareButtons();
4301     prepareAxes(POSITION | PRESSURE);
4302     addMapperAndConfigure(mapper);
4303 
4304     NotifyMotionArgs motionArgs;
4305 
4306     // initially hovering because pressure is 0
4307     processDown(mapper, 100, 200);
4308     processPressure(mapper, 0);
4309     processSync(mapper);
4310     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4311     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4312     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4313             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4314 
4315     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4316     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4317     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4318             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
4319 
4320     // move a little
4321     processMove(mapper, 150, 250);
4322     processSync(mapper);
4323     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4324     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4325     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4326             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4327 
4328     // down when pressure is non-zero
4329     processPressure(mapper, RAW_PRESSURE_MAX);
4330     processSync(mapper);
4331     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4332     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4333     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4334             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4335 
4336     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4338     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4339             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4340 
4341     // up when pressure becomes 0, hover restored
4342     processPressure(mapper, 0);
4343     processSync(mapper);
4344     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4346     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4347             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
4348 
4349     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4350     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
4351     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4352             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4353 
4354     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4355     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4356     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4357             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4358 
4359     // exit hover when pointer goes away
4360     processUp(mapper);
4361     processSync(mapper);
4362     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4363     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
4364     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4365             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
4366 }
4367 
4368 
4369 // --- MultiTouchInputMapperTest ---
4370 
4371 class MultiTouchInputMapperTest : public TouchInputMapperTest {
4372 protected:
4373     void prepareAxes(int axes);
4374 
4375     void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
4376     void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
4377     void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
4378     void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
4379     void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
4380     void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
4381     void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
4382     void processDistance(MultiTouchInputMapper* mapper, int32_t distance);
4383     void processId(MultiTouchInputMapper* mapper, int32_t id);
4384     void processSlot(MultiTouchInputMapper* mapper, int32_t slot);
4385     void processToolType(MultiTouchInputMapper* mapper, int32_t toolType);
4386     void processKey(MultiTouchInputMapper* mapper, int32_t code, int32_t value);
4387     void processTimestamp(MultiTouchInputMapper* mapper, uint32_t value);
4388     void processMTSync(MultiTouchInputMapper* mapper);
4389     void processSync(MultiTouchInputMapper* mapper);
4390 };
4391 
prepareAxes(int axes)4392 void MultiTouchInputMapperTest::prepareAxes(int axes) {
4393     if (axes & POSITION) {
4394         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
4395                 RAW_X_MIN, RAW_X_MAX, 0, 0);
4396         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
4397                 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
4398     }
4399     if (axes & TOUCH) {
4400         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
4401                 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4402         if (axes & MINOR) {
4403             mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
4404                     RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
4405         }
4406     }
4407     if (axes & TOOL) {
4408         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
4409                 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
4410         if (axes & MINOR) {
4411             mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
4412                     RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
4413         }
4414     }
4415     if (axes & ORIENTATION) {
4416         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
4417                 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
4418     }
4419     if (axes & PRESSURE) {
4420         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
4421                 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
4422     }
4423     if (axes & DISTANCE) {
4424         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_DISTANCE,
4425                 RAW_DISTANCE_MIN, RAW_DISTANCE_MAX, 0, 0);
4426     }
4427     if (axes & ID) {
4428         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
4429                 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
4430     }
4431     if (axes & SLOT) {
4432         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_SLOT,
4433                 RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
4434         mFakeEventHub->setAbsoluteAxisValue(DEVICE_ID, ABS_MT_SLOT, 0);
4435     }
4436     if (axes & TOOL_TYPE) {
4437         mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOOL_TYPE,
4438                 0, MT_TOOL_MAX, 0, 0);
4439     }
4440 }
4441 
processPosition(MultiTouchInputMapper * mapper,int32_t x,int32_t y)4442 void MultiTouchInputMapperTest::processPosition(
4443         MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
4444     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, x);
4445     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, y);
4446 }
4447 
processTouchMajor(MultiTouchInputMapper * mapper,int32_t touchMajor)4448 void MultiTouchInputMapperTest::processTouchMajor(
4449         MultiTouchInputMapper* mapper, int32_t touchMajor) {
4450     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
4451 }
4452 
processTouchMinor(MultiTouchInputMapper * mapper,int32_t touchMinor)4453 void MultiTouchInputMapperTest::processTouchMinor(
4454         MultiTouchInputMapper* mapper, int32_t touchMinor) {
4455     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
4456 }
4457 
processToolMajor(MultiTouchInputMapper * mapper,int32_t toolMajor)4458 void MultiTouchInputMapperTest::processToolMajor(
4459         MultiTouchInputMapper* mapper, int32_t toolMajor) {
4460     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
4461 }
4462 
processToolMinor(MultiTouchInputMapper * mapper,int32_t toolMinor)4463 void MultiTouchInputMapperTest::processToolMinor(
4464         MultiTouchInputMapper* mapper, int32_t toolMinor) {
4465     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
4466 }
4467 
processOrientation(MultiTouchInputMapper * mapper,int32_t orientation)4468 void MultiTouchInputMapperTest::processOrientation(
4469         MultiTouchInputMapper* mapper, int32_t orientation) {
4470     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, orientation);
4471 }
4472 
processPressure(MultiTouchInputMapper * mapper,int32_t pressure)4473 void MultiTouchInputMapperTest::processPressure(
4474         MultiTouchInputMapper* mapper, int32_t pressure) {
4475     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, pressure);
4476 }
4477 
processDistance(MultiTouchInputMapper * mapper,int32_t distance)4478 void MultiTouchInputMapperTest::processDistance(
4479         MultiTouchInputMapper* mapper, int32_t distance) {
4480     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_DISTANCE, distance);
4481 }
4482 
processId(MultiTouchInputMapper * mapper,int32_t id)4483 void MultiTouchInputMapperTest::processId(
4484         MultiTouchInputMapper* mapper, int32_t id) {
4485     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, id);
4486 }
4487 
processSlot(MultiTouchInputMapper * mapper,int32_t slot)4488 void MultiTouchInputMapperTest::processSlot(
4489         MultiTouchInputMapper* mapper, int32_t slot) {
4490     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_SLOT, slot);
4491 }
4492 
processToolType(MultiTouchInputMapper * mapper,int32_t toolType)4493 void MultiTouchInputMapperTest::processToolType(
4494         MultiTouchInputMapper* mapper, int32_t toolType) {
4495     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
4496 }
4497 
processKey(MultiTouchInputMapper * mapper,int32_t code,int32_t value)4498 void MultiTouchInputMapperTest::processKey(
4499         MultiTouchInputMapper* mapper, int32_t code, int32_t value) {
4500     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, code, value);
4501 }
4502 
processTimestamp(MultiTouchInputMapper * mapper,uint32_t value)4503 void MultiTouchInputMapperTest::processTimestamp(MultiTouchInputMapper* mapper, uint32_t value) {
4504     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_MSC, MSC_TIMESTAMP, value);
4505 }
4506 
processMTSync(MultiTouchInputMapper * mapper)4507 void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
4508     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0);
4509 }
4510 
processSync(MultiTouchInputMapper * mapper)4511 void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
4512     process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0);
4513 }
4514 
4515 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithoutTrackingIds)4516 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
4517     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4518     addConfigurationProperty("touch.deviceType", "touchScreen");
4519     prepareDisplay(DISPLAY_ORIENTATION_0);
4520     prepareAxes(POSITION);
4521     prepareVirtualKeys();
4522     addMapperAndConfigure(mapper);
4523 
4524     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4525 
4526     NotifyMotionArgs motionArgs;
4527 
4528     // Two fingers down at once.
4529     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4530     processPosition(mapper, x1, y1);
4531     processMTSync(mapper);
4532     processPosition(mapper, x2, y2);
4533     processMTSync(mapper);
4534     processSync(mapper);
4535 
4536     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4537     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4538     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4539     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4540     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4541     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4542     ASSERT_EQ(0, motionArgs.flags);
4543     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4544     ASSERT_EQ(0, motionArgs.buttonState);
4545     ASSERT_EQ(0, motionArgs.edgeFlags);
4546     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4547     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4548     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4549     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4550             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4551     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4552     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4553     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4554 
4555     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4557     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4558     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4559     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4560     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4561             motionArgs.action);
4562     ASSERT_EQ(0, motionArgs.flags);
4563     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4564     ASSERT_EQ(0, motionArgs.buttonState);
4565     ASSERT_EQ(0, motionArgs.edgeFlags);
4566     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4567     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4568     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4569     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4570     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4571     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4572             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4573     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4574             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4575     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4576     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4577     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4578 
4579     // Move.
4580     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4581     processPosition(mapper, x1, y1);
4582     processMTSync(mapper);
4583     processPosition(mapper, x2, y2);
4584     processMTSync(mapper);
4585     processSync(mapper);
4586 
4587     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4588     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4589     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4590     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4591     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4592     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4593     ASSERT_EQ(0, motionArgs.flags);
4594     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4595     ASSERT_EQ(0, motionArgs.buttonState);
4596     ASSERT_EQ(0, motionArgs.edgeFlags);
4597     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4598     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4599     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4600     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4601     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4602     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4603             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4604     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4605             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4606     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4607     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4608     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4609 
4610     // First finger up.
4611     x2 += 15; y2 -= 20;
4612     processPosition(mapper, x2, y2);
4613     processMTSync(mapper);
4614     processSync(mapper);
4615 
4616     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4617     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4618     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4619     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4620     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4621     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4622             motionArgs.action);
4623     ASSERT_EQ(0, motionArgs.flags);
4624     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4625     ASSERT_EQ(0, motionArgs.buttonState);
4626     ASSERT_EQ(0, motionArgs.edgeFlags);
4627     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4628     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4629     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4630     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4631     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4632     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4633             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4634     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4635             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4636     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4637     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4638     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4639 
4640     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4641     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4642     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4643     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4644     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4645     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4646     ASSERT_EQ(0, motionArgs.flags);
4647     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4648     ASSERT_EQ(0, motionArgs.buttonState);
4649     ASSERT_EQ(0, motionArgs.edgeFlags);
4650     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4651     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4652     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4653     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4654             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4655     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4656     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4657     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4658 
4659     // Move.
4660     x2 += 20; y2 -= 25;
4661     processPosition(mapper, x2, y2);
4662     processMTSync(mapper);
4663     processSync(mapper);
4664 
4665     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4667     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4668     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4669     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4670     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4671     ASSERT_EQ(0, motionArgs.flags);
4672     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4673     ASSERT_EQ(0, motionArgs.buttonState);
4674     ASSERT_EQ(0, motionArgs.edgeFlags);
4675     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4676     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4677     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4678     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4679             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4680     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4681     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4682     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4683 
4684     // New finger down.
4685     int32_t x3 = 700, y3 = 300;
4686     processPosition(mapper, x2, y2);
4687     processMTSync(mapper);
4688     processPosition(mapper, x3, y3);
4689     processMTSync(mapper);
4690     processSync(mapper);
4691 
4692     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4693     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4694     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4695     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4696     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4697     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4698             motionArgs.action);
4699     ASSERT_EQ(0, motionArgs.flags);
4700     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4701     ASSERT_EQ(0, motionArgs.buttonState);
4702     ASSERT_EQ(0, motionArgs.edgeFlags);
4703     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4704     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4705     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4706     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4707     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4708     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4709             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4710     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4711             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4712     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4713     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4714     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4715 
4716     // Second finger up.
4717     x3 += 30; y3 -= 20;
4718     processPosition(mapper, x3, y3);
4719     processMTSync(mapper);
4720     processSync(mapper);
4721 
4722     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4723     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4724     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4725     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4726     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4727     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4728             motionArgs.action);
4729     ASSERT_EQ(0, motionArgs.flags);
4730     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4731     ASSERT_EQ(0, motionArgs.buttonState);
4732     ASSERT_EQ(0, motionArgs.edgeFlags);
4733     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4734     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4735     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4736     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4737     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4738     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4739             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4740     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4741             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4742     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4743     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4744     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4745 
4746     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4748     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4749     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4750     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4751     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4752     ASSERT_EQ(0, motionArgs.flags);
4753     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4754     ASSERT_EQ(0, motionArgs.buttonState);
4755     ASSERT_EQ(0, motionArgs.edgeFlags);
4756     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4757     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4758     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4759     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4760             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4761     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4762     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4763     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4764 
4765     // Last finger up.
4766     processMTSync(mapper);
4767     processSync(mapper);
4768 
4769     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4770     ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4771     ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4772     ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4773     ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4774     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4775     ASSERT_EQ(0, motionArgs.flags);
4776     ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4777     ASSERT_EQ(0, motionArgs.buttonState);
4778     ASSERT_EQ(0, motionArgs.edgeFlags);
4779     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4780     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4781     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4782     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4783             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4784     ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4785     ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4786     ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4787 
4788     // Should not have sent any more keys or motions.
4789     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4790     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4791 }
4792 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithTrackingIds)4793 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
4794     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4795     addConfigurationProperty("touch.deviceType", "touchScreen");
4796     prepareDisplay(DISPLAY_ORIENTATION_0);
4797     prepareAxes(POSITION | ID);
4798     prepareVirtualKeys();
4799     addMapperAndConfigure(mapper);
4800 
4801     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4802 
4803     NotifyMotionArgs motionArgs;
4804 
4805     // Two fingers down at once.
4806     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4807     processPosition(mapper, x1, y1);
4808     processId(mapper, 1);
4809     processMTSync(mapper);
4810     processPosition(mapper, x2, y2);
4811     processId(mapper, 2);
4812     processMTSync(mapper);
4813     processSync(mapper);
4814 
4815     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4817     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4818     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4819     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4820     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4821             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4822 
4823     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4824     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4825             motionArgs.action);
4826     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4827     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4828     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4829     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4830     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4831     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4832             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4833     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4834             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4835 
4836     // Move.
4837     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
4838     processPosition(mapper, x1, y1);
4839     processId(mapper, 1);
4840     processMTSync(mapper);
4841     processPosition(mapper, x2, y2);
4842     processId(mapper, 2);
4843     processMTSync(mapper);
4844     processSync(mapper);
4845 
4846     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4847     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4848     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4849     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4850     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4851     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4852     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4853     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4854             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4855     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4856             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4857 
4858     // First finger up.
4859     x2 += 15; y2 -= 20;
4860     processPosition(mapper, x2, y2);
4861     processId(mapper, 2);
4862     processMTSync(mapper);
4863     processSync(mapper);
4864 
4865     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4867             motionArgs.action);
4868     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4869     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4870     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4871     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4872     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4873     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4874             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4875     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4876             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4877 
4878     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4879     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4880     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4881     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4882     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4883     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4884             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4885 
4886     // Move.
4887     x2 += 20; y2 -= 25;
4888     processPosition(mapper, x2, y2);
4889     processId(mapper, 2);
4890     processMTSync(mapper);
4891     processSync(mapper);
4892 
4893     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4895     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4896     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
4897     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4898     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4899             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4900 
4901     // New finger down.
4902     int32_t x3 = 700, y3 = 300;
4903     processPosition(mapper, x2, y2);
4904     processId(mapper, 2);
4905     processMTSync(mapper);
4906     processPosition(mapper, x3, y3);
4907     processId(mapper, 3);
4908     processMTSync(mapper);
4909     processSync(mapper);
4910 
4911     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4912     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4913             motionArgs.action);
4914     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4915     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4916     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4917     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4918     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4919     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4920             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4921     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4922             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4923 
4924     // Second finger up.
4925     x3 += 30; y3 -= 20;
4926     processPosition(mapper, x3, y3);
4927     processId(mapper, 3);
4928     processMTSync(mapper);
4929     processSync(mapper);
4930 
4931     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4932     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
4933             motionArgs.action);
4934     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
4935     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4936     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4937     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
4938     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
4939     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4940             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4941     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
4942             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
4943 
4944     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4946     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4947     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4948     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4949     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4950             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4951 
4952     // Last finger up.
4953     processMTSync(mapper);
4954     processSync(mapper);
4955 
4956     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4957     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4958     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4959     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4960     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4961     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4962             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
4963 
4964     // Should not have sent any more keys or motions.
4965     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4966     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4967 }
4968 
TEST_F(MultiTouchInputMapperTest,Process_NormalMultiTouchGesture_WithSlots)4969 TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
4970     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
4971     addConfigurationProperty("touch.deviceType", "touchScreen");
4972     prepareDisplay(DISPLAY_ORIENTATION_0);
4973     prepareAxes(POSITION | ID | SLOT);
4974     prepareVirtualKeys();
4975     addMapperAndConfigure(mapper);
4976 
4977     mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
4978 
4979     NotifyMotionArgs motionArgs;
4980 
4981     // Two fingers down at once.
4982     int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
4983     processPosition(mapper, x1, y1);
4984     processId(mapper, 1);
4985     processSlot(mapper, 1);
4986     processPosition(mapper, x2, y2);
4987     processId(mapper, 2);
4988     processSync(mapper);
4989 
4990     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4991     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4992     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
4993     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
4994     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
4995     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4996             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
4997 
4998     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4999     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5000             motionArgs.action);
5001     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5002     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5003     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5004     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5005     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5006     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5007             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5008     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5009             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5010 
5011     // Move.
5012     x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
5013     processSlot(mapper, 0);
5014     processPosition(mapper, x1, y1);
5015     processSlot(mapper, 1);
5016     processPosition(mapper, x2, y2);
5017     processSync(mapper);
5018 
5019     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5020     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5021     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5022     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5023     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5024     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5025     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5026     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5027             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5028     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5029             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5030 
5031     // First finger up.
5032     x2 += 15; y2 -= 20;
5033     processSlot(mapper, 0);
5034     processId(mapper, -1);
5035     processSlot(mapper, 1);
5036     processPosition(mapper, x2, y2);
5037     processSync(mapper);
5038 
5039     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5040     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5041             motionArgs.action);
5042     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5043     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5044     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5045     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5046     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5047     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5048             toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
5049     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5050             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5051 
5052     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5054     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5055     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5056     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5057     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5058             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5059 
5060     // Move.
5061     x2 += 20; y2 -= 25;
5062     processPosition(mapper, x2, y2);
5063     processSync(mapper);
5064 
5065     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5067     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5068     ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
5069     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5070     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5071             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5072 
5073     // New finger down.
5074     int32_t x3 = 700, y3 = 300;
5075     processPosition(mapper, x2, y2);
5076     processSlot(mapper, 0);
5077     processId(mapper, 3);
5078     processPosition(mapper, x3, y3);
5079     processSync(mapper);
5080 
5081     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5083             motionArgs.action);
5084     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5085     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5086     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5087     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5088     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5089     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5090             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5091     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5092             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5093 
5094     // Second finger up.
5095     x3 += 30; y3 -= 20;
5096     processSlot(mapper, 1);
5097     processId(mapper, -1);
5098     processSlot(mapper, 0);
5099     processPosition(mapper, x3, y3);
5100     processSync(mapper);
5101 
5102     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5103     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5104             motionArgs.action);
5105     ASSERT_EQ(size_t(2), motionArgs.pointerCount);
5106     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5107     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5108     ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
5109     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
5110     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5111             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5112     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
5113             toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
5114 
5115     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5116     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5117     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5118     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5119     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5120     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5121             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5122 
5123     // Last finger up.
5124     processId(mapper, -1);
5125     processSync(mapper);
5126 
5127     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5128     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5129     ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5130     ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5131     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5132     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5133             toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
5134 
5135     // Should not have sent any more keys or motions.
5136     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5137     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5138 }
5139 
TEST_F(MultiTouchInputMapperTest,Process_AllAxes_WithDefaultCalibration)5140 TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
5141     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5142     addConfigurationProperty("touch.deviceType", "touchScreen");
5143     prepareDisplay(DISPLAY_ORIENTATION_0);
5144     prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
5145     addMapperAndConfigure(mapper);
5146 
5147     // These calculations are based on the input device calibration documentation.
5148     int32_t rawX = 100;
5149     int32_t rawY = 200;
5150     int32_t rawTouchMajor = 7;
5151     int32_t rawTouchMinor = 6;
5152     int32_t rawToolMajor = 9;
5153     int32_t rawToolMinor = 8;
5154     int32_t rawPressure = 11;
5155     int32_t rawDistance = 0;
5156     int32_t rawOrientation = 3;
5157     int32_t id = 5;
5158 
5159     float x = toDisplayX(rawX);
5160     float y = toDisplayY(rawY);
5161     float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5162     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5163     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5164     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5165     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5166     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5167     float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
5168     float distance = float(rawDistance);
5169 
5170     processPosition(mapper, rawX, rawY);
5171     processTouchMajor(mapper, rawTouchMajor);
5172     processTouchMinor(mapper, rawTouchMinor);
5173     processToolMajor(mapper, rawToolMajor);
5174     processToolMinor(mapper, rawToolMinor);
5175     processPressure(mapper, rawPressure);
5176     processOrientation(mapper, rawOrientation);
5177     processDistance(mapper, rawDistance);
5178     processId(mapper, id);
5179     processMTSync(mapper);
5180     processSync(mapper);
5181 
5182     NotifyMotionArgs args;
5183     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5184     ASSERT_EQ(0, args.pointerProperties[0].id);
5185     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5186             x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
5187             orientation, distance));
5188 }
5189 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_GeometricCalibration)5190 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
5191     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5192     addConfigurationProperty("touch.deviceType", "touchScreen");
5193     prepareDisplay(DISPLAY_ORIENTATION_0);
5194     prepareAxes(POSITION | TOUCH | TOOL | MINOR);
5195     addConfigurationProperty("touch.size.calibration", "geometric");
5196     addMapperAndConfigure(mapper);
5197 
5198     // These calculations are based on the input device calibration documentation.
5199     int32_t rawX = 100;
5200     int32_t rawY = 200;
5201     int32_t rawTouchMajor = 140;
5202     int32_t rawTouchMinor = 120;
5203     int32_t rawToolMajor = 180;
5204     int32_t rawToolMinor = 160;
5205 
5206     float x = toDisplayX(rawX);
5207     float y = toDisplayY(rawY);
5208     float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
5209     float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
5210     float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
5211     float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
5212     float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
5213 
5214     processPosition(mapper, rawX, rawY);
5215     processTouchMajor(mapper, rawTouchMajor);
5216     processTouchMinor(mapper, rawTouchMinor);
5217     processToolMajor(mapper, rawToolMajor);
5218     processToolMinor(mapper, rawToolMinor);
5219     processMTSync(mapper);
5220     processSync(mapper);
5221 
5222     NotifyMotionArgs args;
5223     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5224     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5225             x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
5226 }
5227 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_SummedLinearCalibration)5228 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
5229     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5230     addConfigurationProperty("touch.deviceType", "touchScreen");
5231     prepareDisplay(DISPLAY_ORIENTATION_0);
5232     prepareAxes(POSITION | TOUCH | TOOL);
5233     addConfigurationProperty("touch.size.calibration", "diameter");
5234     addConfigurationProperty("touch.size.scale", "10");
5235     addConfigurationProperty("touch.size.bias", "160");
5236     addConfigurationProperty("touch.size.isSummed", "1");
5237     addMapperAndConfigure(mapper);
5238 
5239     // These calculations are based on the input device calibration documentation.
5240     // Note: We only provide a single common touch/tool value because the device is assumed
5241     //       not to emit separate values for each pointer (isSummed = 1).
5242     int32_t rawX = 100;
5243     int32_t rawY = 200;
5244     int32_t rawX2 = 150;
5245     int32_t rawY2 = 250;
5246     int32_t rawTouchMajor = 5;
5247     int32_t rawToolMajor = 8;
5248 
5249     float x = toDisplayX(rawX);
5250     float y = toDisplayY(rawY);
5251     float x2 = toDisplayX(rawX2);
5252     float y2 = toDisplayY(rawY2);
5253     float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
5254     float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
5255     float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
5256 
5257     processPosition(mapper, rawX, rawY);
5258     processTouchMajor(mapper, rawTouchMajor);
5259     processToolMajor(mapper, rawToolMajor);
5260     processMTSync(mapper);
5261     processPosition(mapper, rawX2, rawY2);
5262     processTouchMajor(mapper, rawTouchMajor);
5263     processToolMajor(mapper, rawToolMajor);
5264     processMTSync(mapper);
5265     processSync(mapper);
5266 
5267     NotifyMotionArgs args;
5268     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5270 
5271     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5272     ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
5273             args.action);
5274     ASSERT_EQ(size_t(2), args.pointerCount);
5275     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5276             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5277     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
5278             x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
5279 }
5280 
TEST_F(MultiTouchInputMapperTest,Process_TouchAndToolAxes_AreaCalibration)5281 TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
5282     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5283     addConfigurationProperty("touch.deviceType", "touchScreen");
5284     prepareDisplay(DISPLAY_ORIENTATION_0);
5285     prepareAxes(POSITION | TOUCH | TOOL);
5286     addConfigurationProperty("touch.size.calibration", "area");
5287     addConfigurationProperty("touch.size.scale", "43");
5288     addConfigurationProperty("touch.size.bias", "3");
5289     addMapperAndConfigure(mapper);
5290 
5291     // These calculations are based on the input device calibration documentation.
5292     int32_t rawX = 100;
5293     int32_t rawY = 200;
5294     int32_t rawTouchMajor = 5;
5295     int32_t rawToolMajor = 8;
5296 
5297     float x = toDisplayX(rawX);
5298     float y = toDisplayY(rawY);
5299     float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
5300     float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
5301     float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
5302 
5303     processPosition(mapper, rawX, rawY);
5304     processTouchMajor(mapper, rawTouchMajor);
5305     processToolMajor(mapper, rawToolMajor);
5306     processMTSync(mapper);
5307     processSync(mapper);
5308 
5309     NotifyMotionArgs args;
5310     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5311     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5312             x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
5313 }
5314 
TEST_F(MultiTouchInputMapperTest,Process_PressureAxis_AmplitudeCalibration)5315 TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
5316     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5317     addConfigurationProperty("touch.deviceType", "touchScreen");
5318     prepareDisplay(DISPLAY_ORIENTATION_0);
5319     prepareAxes(POSITION | PRESSURE);
5320     addConfigurationProperty("touch.pressure.calibration", "amplitude");
5321     addConfigurationProperty("touch.pressure.scale", "0.01");
5322     addMapperAndConfigure(mapper);
5323 
5324     InputDeviceInfo info;
5325     mapper->populateDeviceInfo(&info);
5326     ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5327             AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
5328             0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
5329 
5330     // These calculations are based on the input device calibration documentation.
5331     int32_t rawX = 100;
5332     int32_t rawY = 200;
5333     int32_t rawPressure = 60;
5334 
5335     float x = toDisplayX(rawX);
5336     float y = toDisplayY(rawY);
5337     float pressure = float(rawPressure) * 0.01f;
5338 
5339     processPosition(mapper, rawX, rawY);
5340     processPressure(mapper, rawPressure);
5341     processMTSync(mapper);
5342     processSync(mapper);
5343 
5344     NotifyMotionArgs args;
5345     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5346     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5347             x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
5348 }
5349 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllButtons)5350 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
5351     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5352     addConfigurationProperty("touch.deviceType", "touchScreen");
5353     prepareDisplay(DISPLAY_ORIENTATION_0);
5354     prepareAxes(POSITION | ID | SLOT);
5355     addMapperAndConfigure(mapper);
5356 
5357     NotifyMotionArgs motionArgs;
5358     NotifyKeyArgs keyArgs;
5359 
5360     processId(mapper, 1);
5361     processPosition(mapper, 100, 200);
5362     processSync(mapper);
5363     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5364     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5365     ASSERT_EQ(0, motionArgs.buttonState);
5366 
5367     // press BTN_LEFT, release BTN_LEFT
5368     processKey(mapper, BTN_LEFT, 1);
5369     processSync(mapper);
5370     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5372     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5373 
5374     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5375     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5376     ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5377 
5378     processKey(mapper, BTN_LEFT, 0);
5379     processSync(mapper);
5380     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5381     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5382     ASSERT_EQ(0, motionArgs.buttonState);
5383 
5384     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5386     ASSERT_EQ(0, motionArgs.buttonState);
5387 
5388     // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5389     processKey(mapper, BTN_RIGHT, 1);
5390     processKey(mapper, BTN_MIDDLE, 1);
5391     processSync(mapper);
5392     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5393     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5394     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5395             motionArgs.buttonState);
5396 
5397     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5398     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5399     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5400 
5401     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5403     ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5404             motionArgs.buttonState);
5405 
5406     processKey(mapper, BTN_RIGHT, 0);
5407     processSync(mapper);
5408     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5409     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5410     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5411 
5412     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5414     ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5415 
5416     processKey(mapper, BTN_MIDDLE, 0);
5417     processSync(mapper);
5418     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5419     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5420     ASSERT_EQ(0, motionArgs.buttonState);
5421 
5422     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5424     ASSERT_EQ(0, motionArgs.buttonState);
5425 
5426     // press BTN_BACK, release BTN_BACK
5427     processKey(mapper, BTN_BACK, 1);
5428     processSync(mapper);
5429     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5430     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5431     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5432 
5433     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5434     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5435     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5436 
5437     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5439     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5440 
5441     processKey(mapper, BTN_BACK, 0);
5442     processSync(mapper);
5443     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5444     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5445     ASSERT_EQ(0, motionArgs.buttonState);
5446 
5447     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5448     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5449     ASSERT_EQ(0, motionArgs.buttonState);
5450 
5451     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5452     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5453     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5454 
5455     // press BTN_SIDE, release BTN_SIDE
5456     processKey(mapper, BTN_SIDE, 1);
5457     processSync(mapper);
5458     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5459     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5460     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5461 
5462     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5464     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5465 
5466     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5468     ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5469 
5470     processKey(mapper, BTN_SIDE, 0);
5471     processSync(mapper);
5472     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5473     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5474     ASSERT_EQ(0, motionArgs.buttonState);
5475 
5476     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5477     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5478     ASSERT_EQ(0, motionArgs.buttonState);
5479 
5480     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5481     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5482     ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5483 
5484     // press BTN_FORWARD, release BTN_FORWARD
5485     processKey(mapper, BTN_FORWARD, 1);
5486     processSync(mapper);
5487     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5488     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5489     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5490 
5491     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5493     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5494 
5495     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5496     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5497     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5498 
5499     processKey(mapper, BTN_FORWARD, 0);
5500     processSync(mapper);
5501     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5503     ASSERT_EQ(0, motionArgs.buttonState);
5504 
5505     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5506     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5507     ASSERT_EQ(0, motionArgs.buttonState);
5508 
5509     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5510     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5511     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5512 
5513     // press BTN_EXTRA, release BTN_EXTRA
5514     processKey(mapper, BTN_EXTRA, 1);
5515     processSync(mapper);
5516     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5517     ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5518     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5519 
5520     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5521     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5522     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5523 
5524     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5526     ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5527 
5528     processKey(mapper, BTN_EXTRA, 0);
5529     processSync(mapper);
5530     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5532     ASSERT_EQ(0, motionArgs.buttonState);
5533 
5534     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5535     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5536     ASSERT_EQ(0, motionArgs.buttonState);
5537 
5538     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5539     ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5540     ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5541 
5542     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5543 
5544     // press BTN_STYLUS, release BTN_STYLUS
5545     processKey(mapper, BTN_STYLUS, 1);
5546     processSync(mapper);
5547     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5548     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5549     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5550 
5551     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5553     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5554 
5555     processKey(mapper, BTN_STYLUS, 0);
5556     processSync(mapper);
5557     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5559     ASSERT_EQ(0, motionArgs.buttonState);
5560 
5561     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5562     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5563     ASSERT_EQ(0, motionArgs.buttonState);
5564 
5565     // press BTN_STYLUS2, release BTN_STYLUS2
5566     processKey(mapper, BTN_STYLUS2, 1);
5567     processSync(mapper);
5568     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5570     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5571 
5572     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5574     ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5575 
5576     processKey(mapper, BTN_STYLUS2, 0);
5577     processSync(mapper);
5578     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5579     ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5580     ASSERT_EQ(0, motionArgs.buttonState);
5581 
5582     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5583     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5584     ASSERT_EQ(0, motionArgs.buttonState);
5585 
5586     // release touch
5587     processId(mapper, -1);
5588     processSync(mapper);
5589     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5591     ASSERT_EQ(0, motionArgs.buttonState);
5592 }
5593 
TEST_F(MultiTouchInputMapperTest,Process_ShouldHandleAllToolTypes)5594 TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
5595     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5596     addConfigurationProperty("touch.deviceType", "touchScreen");
5597     prepareDisplay(DISPLAY_ORIENTATION_0);
5598     prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
5599     addMapperAndConfigure(mapper);
5600 
5601     NotifyMotionArgs motionArgs;
5602 
5603     // default tool type is finger
5604     processId(mapper, 1);
5605     processPosition(mapper, 100, 200);
5606     processSync(mapper);
5607     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5608     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5609     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5610 
5611     // eraser
5612     processKey(mapper, BTN_TOOL_RUBBER, 1);
5613     processSync(mapper);
5614     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5616     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5617 
5618     // stylus
5619     processKey(mapper, BTN_TOOL_RUBBER, 0);
5620     processKey(mapper, BTN_TOOL_PEN, 1);
5621     processSync(mapper);
5622     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5623     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5624     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5625 
5626     // brush
5627     processKey(mapper, BTN_TOOL_PEN, 0);
5628     processKey(mapper, BTN_TOOL_BRUSH, 1);
5629     processSync(mapper);
5630     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5632     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5633 
5634     // pencil
5635     processKey(mapper, BTN_TOOL_BRUSH, 0);
5636     processKey(mapper, BTN_TOOL_PENCIL, 1);
5637     processSync(mapper);
5638     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5639     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5640     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5641 
5642     // airbrush
5643     processKey(mapper, BTN_TOOL_PENCIL, 0);
5644     processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5645     processSync(mapper);
5646     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5648     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5649 
5650     // mouse
5651     processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5652     processKey(mapper, BTN_TOOL_MOUSE, 1);
5653     processSync(mapper);
5654     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5656     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5657 
5658     // lens
5659     processKey(mapper, BTN_TOOL_MOUSE, 0);
5660     processKey(mapper, BTN_TOOL_LENS, 1);
5661     processSync(mapper);
5662     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5663     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5664     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5665 
5666     // double-tap
5667     processKey(mapper, BTN_TOOL_LENS, 0);
5668     processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5669     processSync(mapper);
5670     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5672     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5673 
5674     // triple-tap
5675     processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5676     processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5677     processSync(mapper);
5678     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5680     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5681 
5682     // quad-tap
5683     processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5684     processKey(mapper, BTN_TOOL_QUADTAP, 1);
5685     processSync(mapper);
5686     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5687     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5688     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5689 
5690     // finger
5691     processKey(mapper, BTN_TOOL_QUADTAP, 0);
5692     processKey(mapper, BTN_TOOL_FINGER, 1);
5693     processSync(mapper);
5694     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5696     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5697 
5698     // stylus trumps finger
5699     processKey(mapper, BTN_TOOL_PEN, 1);
5700     processSync(mapper);
5701     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5703     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5704 
5705     // eraser trumps stylus
5706     processKey(mapper, BTN_TOOL_RUBBER, 1);
5707     processSync(mapper);
5708     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5710     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
5711 
5712     // mouse trumps eraser
5713     processKey(mapper, BTN_TOOL_MOUSE, 1);
5714     processSync(mapper);
5715     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5717     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
5718 
5719     // MT tool type trumps BTN tool types: MT_TOOL_FINGER
5720     processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
5721     processSync(mapper);
5722     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5724     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5725 
5726     // MT tool type trumps BTN tool types: MT_TOOL_PEN
5727     processToolType(mapper, MT_TOOL_PEN);
5728     processSync(mapper);
5729     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5730     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5731     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
5732 
5733     // back to default tool type
5734     processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
5735     processKey(mapper, BTN_TOOL_MOUSE, 0);
5736     processKey(mapper, BTN_TOOL_RUBBER, 0);
5737     processKey(mapper, BTN_TOOL_PEN, 0);
5738     processKey(mapper, BTN_TOOL_FINGER, 0);
5739     processSync(mapper);
5740     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5741     ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5742     ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5743 }
5744 
TEST_F(MultiTouchInputMapperTest,Process_WhenBtnTouchPresent_HoversIfItsValueIsZero)5745 TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
5746     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5747     addConfigurationProperty("touch.deviceType", "touchScreen");
5748     prepareDisplay(DISPLAY_ORIENTATION_0);
5749     prepareAxes(POSITION | ID | SLOT);
5750     mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
5751     addMapperAndConfigure(mapper);
5752 
5753     NotifyMotionArgs motionArgs;
5754 
5755     // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5756     processId(mapper, 1);
5757     processPosition(mapper, 100, 200);
5758     processSync(mapper);
5759     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5761     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5762             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5763 
5764     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5766     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5768 
5769     // move a little
5770     processPosition(mapper, 150, 250);
5771     processSync(mapper);
5772     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5773     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5774     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5775             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5776 
5777     // down when BTN_TOUCH is pressed, pressure defaults to 1
5778     processKey(mapper, BTN_TOUCH, 1);
5779     processSync(mapper);
5780     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5781     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5782     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5783             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5784 
5785     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5788             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5789 
5790     // up when BTN_TOUCH is released, hover restored
5791     processKey(mapper, BTN_TOUCH, 0);
5792     processSync(mapper);
5793     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5795     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5796             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5797 
5798     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5800     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5801             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5802 
5803     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5805     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5806             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5807 
5808     // exit hover when pointer goes away
5809     processId(mapper, -1);
5810     processSync(mapper);
5811     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5813     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5814             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5815 }
5816 
TEST_F(MultiTouchInputMapperTest,Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero)5817 TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
5818     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5819     addConfigurationProperty("touch.deviceType", "touchScreen");
5820     prepareDisplay(DISPLAY_ORIENTATION_0);
5821     prepareAxes(POSITION | ID | SLOT | PRESSURE);
5822     addMapperAndConfigure(mapper);
5823 
5824     NotifyMotionArgs motionArgs;
5825 
5826     // initially hovering because pressure is 0
5827     processId(mapper, 1);
5828     processPosition(mapper, 100, 200);
5829     processPressure(mapper, 0);
5830     processSync(mapper);
5831     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5833     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5834             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5835 
5836     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5838     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5839             toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5840 
5841     // move a little
5842     processPosition(mapper, 150, 250);
5843     processSync(mapper);
5844     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5846     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5847             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5848 
5849     // down when pressure becomes non-zero
5850     processPressure(mapper, RAW_PRESSURE_MAX);
5851     processSync(mapper);
5852     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5854     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5856 
5857     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858     ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5859     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5860             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5861 
5862     // up when pressure becomes 0, hover restored
5863     processPressure(mapper, 0);
5864     processSync(mapper);
5865     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866     ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5867     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868             toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5869 
5870     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5872     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5873             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5874 
5875     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5877     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5878             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5879 
5880     // exit hover when pointer goes away
5881     processId(mapper, -1);
5882     processSync(mapper);
5883     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884     ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5885     ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886             toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5887 }
5888 
TEST_F(MultiTouchInputMapperTest,Process_HandlesTimestamp)5889 TEST_F(MultiTouchInputMapperTest, Process_HandlesTimestamp) {
5890     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5891 
5892     addConfigurationProperty("touch.deviceType", "touchScreen");
5893     prepareDisplay(DISPLAY_ORIENTATION_0);
5894     prepareAxes(POSITION);
5895     addMapperAndConfigure(mapper);
5896     NotifyMotionArgs args;
5897 
5898     // By default, deviceTimestamp should be zero
5899     processPosition(mapper, 100, 100);
5900     processMTSync(mapper);
5901     processSync(mapper);
5902     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5903     ASSERT_EQ(0U, args.deviceTimestamp);
5904 
5905     // Now the timestamp of 1000 is reported by evdev and should appear in MotionArgs
5906     processPosition(mapper, 0, 0);
5907     processTimestamp(mapper, 1000);
5908     processMTSync(mapper);
5909     processSync(mapper);
5910     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5911     ASSERT_EQ(1000U, args.deviceTimestamp);
5912 }
5913 
TEST_F(MultiTouchInputMapperTest,WhenMapperIsReset_TimestampIsCleared)5914 TEST_F(MultiTouchInputMapperTest, WhenMapperIsReset_TimestampIsCleared) {
5915     MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
5916 
5917     addConfigurationProperty("touch.deviceType", "touchScreen");
5918     prepareDisplay(DISPLAY_ORIENTATION_0);
5919     prepareAxes(POSITION);
5920     addMapperAndConfigure(mapper);
5921     NotifyMotionArgs args;
5922 
5923     // Send a touch event with a timestamp
5924     processPosition(mapper, 100, 100);
5925     processTimestamp(mapper, 1);
5926     processMTSync(mapper);
5927     processSync(mapper);
5928     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929     ASSERT_EQ(1U, args.deviceTimestamp);
5930 
5931     // Since the data accumulates, and new timestamp has not arrived, deviceTimestamp won't change
5932     processPosition(mapper, 100, 200);
5933     processMTSync(mapper);
5934     processSync(mapper);
5935     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5936     ASSERT_EQ(1U, args.deviceTimestamp);
5937 
5938     mapper->reset(/* when */ 0);
5939     // After the mapper is reset, deviceTimestamp should become zero again
5940     processPosition(mapper, 100, 300);
5941     processMTSync(mapper);
5942     processSync(mapper);
5943     ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5944     ASSERT_EQ(0U, args.deviceTimestamp);
5945 }
5946 
5947 
5948 } // namespace android
5949