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