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