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