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