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