blob: 131894af1b64e7d81057197762f28815a511b299 [file] [log] [blame]
James Dong199d1c12011-03-17 11:48:13 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeff Brownc3db8582010-10-20 15:33:38 -070016
Jeff Brownb4ff35d2011-01-02 16:37:43 -080017#include "../InputReader.h"
18
Jeff Brownc3db8582010-10-20 15:33:38 -070019#include <utils/List.h>
20#include <gtest/gtest.h>
21#include <math.h>
22
23namespace android {
24
25// An arbitrary time value.
26static const nsecs_t ARBITRARY_TIME = 1234;
27
28// Arbitrary display properties.
29static const int32_t DISPLAY_ID = 0;
30static const int32_t DISPLAY_WIDTH = 480;
31static const int32_t DISPLAY_HEIGHT = 800;
32
33// Error tolerance for floating point assertions.
34static const float EPSILON = 0.001f;
35
36template<typename T>
37static inline T min(T a, T b) {
38 return a < b ? a : b;
39}
40
41static inline float avg(float x, float y) {
42 return (x + y) / 2;
43}
44
45
Jeff Brown83c09682010-12-23 17:50:18 -080046// --- FakePointerController ---
47
48class FakePointerController : public PointerControllerInterface {
49 bool mHaveBounds;
50 float mMinX, mMinY, mMaxX, mMaxY;
51
52protected:
53 virtual ~FakePointerController() { }
54
55public:
Jeff Brownb4ff35d2011-01-02 16:37:43 -080056 FakePointerController() :
57 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0) {
Jeff Brown83c09682010-12-23 17:50:18 -080058 }
59
60 void setBounds(float minX, float minY, float maxX, float maxY) {
61 mHaveBounds = true;
62 mMinX = minX;
63 mMinY = minY;
64 mMaxX = maxX;
65 mMaxY = maxY;
66 }
67
68private:
69 virtual bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const {
70 *outMinX = mMinX;
Jeff Brown83c09682010-12-23 17:50:18 -080071 *outMinY = mMinY;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080072 *outMaxX = mMaxX;
Jeff Brown83c09682010-12-23 17:50:18 -080073 *outMaxY = mMaxY;
74 return mHaveBounds;
75 }
76
77 virtual void move(float deltaX, float deltaY) {
78 }
79
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070080 virtual void setButtonState(int32_t buttonState) {
Jeff Brown83c09682010-12-23 17:50:18 -080081 }
82
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070083 virtual int32_t getButtonState() const {
Jeff Brown83c09682010-12-23 17:50:18 -080084 return 0;
85 }
86
87 virtual void setPosition(float x, float y) {
88 }
89
90 virtual void getPosition(float* outX, float* outY) const {
91 *outX = 0;
92 *outY = 0;
93 }
Jeff Brown05dc66a2011-03-02 14:41:58 -080094
Jeff Brown538881e2011-05-25 18:23:38 -070095 virtual void fade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -080096 }
97
Jeff Brown538881e2011-05-25 18:23:38 -070098 virtual void unfade(Transition transition) {
Jeff Brown05dc66a2011-03-02 14:41:58 -080099 }
Jeff Brown2352b972011-04-12 22:39:53 -0700100
101 virtual void setPresentation(Presentation presentation) {
102 }
103
Jeff Brown9181a5f42011-06-06 21:04:14 -0700104 virtual void setSpots(const PointerCoords* spotCoords,
105 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
Jeff Brown2352b972011-04-12 22:39:53 -0700106 }
107
108 virtual void clearSpots() {
109 }
Jeff Brown83c09682010-12-23 17:50:18 -0800110};
111
112
Jeff Brownc3db8582010-10-20 15:33:38 -0700113// --- FakeInputReaderPolicy ---
114
115class FakeInputReaderPolicy : public InputReaderPolicyInterface {
116 struct DisplayInfo {
117 int32_t width;
118 int32_t height;
119 int32_t orientation;
120 };
121
122 KeyedVector<int32_t, DisplayInfo> mDisplayInfos;
Jeff Brown214eaf42011-05-26 19:17:02 -0700123 InputReaderConfiguration mConfig;
Jeff Brown83c09682010-12-23 17:50:18 -0800124 KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
Jeff Brownc3db8582010-10-20 15:33:38 -0700125
126protected:
127 virtual ~FakeInputReaderPolicy() { }
128
129public:
Jeff Brown214eaf42011-05-26 19:17:02 -0700130 FakeInputReaderPolicy() {
Jeff Brownc3db8582010-10-20 15:33:38 -0700131 }
132
133 void removeDisplayInfo(int32_t displayId) {
134 mDisplayInfos.removeItem(displayId);
135 }
136
137 void setDisplayInfo(int32_t displayId, int32_t width, int32_t height, int32_t orientation) {
138 removeDisplayInfo(displayId);
139
140 DisplayInfo info;
141 info.width = width;
142 info.height = height;
143 info.orientation = orientation;
144 mDisplayInfos.add(displayId, info);
145 }
146
Jeff Brownfe508922011-01-18 15:10:10 -0800147 virtual nsecs_t getVirtualKeyQuietTime() {
148 return 0;
149 }
150
Jeff Brownc3db8582010-10-20 15:33:38 -0700151 void addExcludedDeviceName(const String8& deviceName) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700152 mConfig.excludedDeviceNames.push(deviceName);
Jeff Brownc3db8582010-10-20 15:33:38 -0700153 }
154
Jeff Brown83c09682010-12-23 17:50:18 -0800155 void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
156 mPointerControllers.add(deviceId, controller);
157 }
158
Jeff Brownc3db8582010-10-20 15:33:38 -0700159private:
160 virtual bool getDisplayInfo(int32_t displayId,
161 int32_t* width, int32_t* height, int32_t* orientation) {
162 ssize_t index = mDisplayInfos.indexOfKey(displayId);
163 if (index >= 0) {
164 const DisplayInfo& info = mDisplayInfos.valueAt(index);
165 if (width) {
166 *width = info.width;
167 }
168 if (height) {
169 *height = info.height;
170 }
171 if (orientation) {
172 *orientation = info.orientation;
173 }
174 return true;
175 }
176 return false;
177 }
178
Jeff Brown214eaf42011-05-26 19:17:02 -0700179 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) {
180 *outConfig = mConfig;
Jeff Brownc3db8582010-10-20 15:33:38 -0700181 }
Jeff Brown83c09682010-12-23 17:50:18 -0800182
183 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
184 return mPointerControllers.valueFor(deviceId);
185 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700186};
187
188
189// --- FakeInputDispatcher ---
190
191class FakeInputDispatcher : public InputDispatcherInterface {
192public:
193 struct NotifyConfigurationChangedArgs {
Jeff Brown56194eb2011-03-02 19:23:13 -0800194 NotifyConfigurationChangedArgs() : eventTime(0) { }
195
Jeff Brownc3db8582010-10-20 15:33:38 -0700196 nsecs_t eventTime;
197 };
198
199 struct NotifyKeyArgs {
200 nsecs_t eventTime;
201 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800202 uint32_t source;
Jeff Brownc3db8582010-10-20 15:33:38 -0700203 uint32_t policyFlags;
204 int32_t action;
205 int32_t flags;
206 int32_t keyCode;
207 int32_t scanCode;
208 int32_t metaState;
209 nsecs_t downTime;
210 };
211
212 struct NotifyMotionArgs {
213 nsecs_t eventTime;
214 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800215 uint32_t source;
Jeff Brownc3db8582010-10-20 15:33:38 -0700216 uint32_t policyFlags;
217 int32_t action;
218 int32_t flags;
219 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700220 int32_t buttonState;
Jeff Brownc3db8582010-10-20 15:33:38 -0700221 int32_t edgeFlags;
222 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700223 Vector<PointerProperties> pointerProperties;
Jeff Brownc3db8582010-10-20 15:33:38 -0700224 Vector<PointerCoords> pointerCoords;
225 float xPrecision;
226 float yPrecision;
227 nsecs_t downTime;
228 };
229
230 struct NotifySwitchArgs {
231 nsecs_t when;
232 int32_t switchCode;
233 int32_t switchValue;
234 uint32_t policyFlags;
235 };
236
237private:
238 List<NotifyConfigurationChangedArgs> mNotifyConfigurationChangedArgs;
239 List<NotifyKeyArgs> mNotifyKeyArgs;
240 List<NotifyMotionArgs> mNotifyMotionArgs;
241 List<NotifySwitchArgs> mNotifySwitchArgs;
242
243protected:
244 virtual ~FakeInputDispatcher() { }
245
246public:
247 FakeInputDispatcher() {
248 }
249
250 void assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs* outArgs = NULL) {
251 ASSERT_FALSE(mNotifyConfigurationChangedArgs.empty())
252 << "Expected notifyConfigurationChanged() to have been called.";
253 if (outArgs) {
254 *outArgs = *mNotifyConfigurationChangedArgs.begin();
255 }
256 mNotifyConfigurationChangedArgs.erase(mNotifyConfigurationChangedArgs.begin());
257 }
258
259 void assertNotifyKeyWasCalled(NotifyKeyArgs* outArgs = NULL) {
260 ASSERT_FALSE(mNotifyKeyArgs.empty())
261 << "Expected notifyKey() to have been called.";
262 if (outArgs) {
263 *outArgs = *mNotifyKeyArgs.begin();
264 }
265 mNotifyKeyArgs.erase(mNotifyKeyArgs.begin());
266 }
267
268 void assertNotifyKeyWasNotCalled() {
269 ASSERT_TRUE(mNotifyKeyArgs.empty())
270 << "Expected notifyKey() to not have been called.";
271 }
272
273 void assertNotifyMotionWasCalled(NotifyMotionArgs* outArgs = NULL) {
274 ASSERT_FALSE(mNotifyMotionArgs.empty())
275 << "Expected notifyMotion() to have been called.";
276 if (outArgs) {
277 *outArgs = *mNotifyMotionArgs.begin();
278 }
279 mNotifyMotionArgs.erase(mNotifyMotionArgs.begin());
280 }
281
282 void assertNotifyMotionWasNotCalled() {
283 ASSERT_TRUE(mNotifyMotionArgs.empty())
284 << "Expected notifyMotion() to not have been called.";
285 }
286
287 void assertNotifySwitchWasCalled(NotifySwitchArgs* outArgs = NULL) {
288 ASSERT_FALSE(mNotifySwitchArgs.empty())
289 << "Expected notifySwitch() to have been called.";
290 if (outArgs) {
291 *outArgs = *mNotifySwitchArgs.begin();
292 }
293 mNotifySwitchArgs.erase(mNotifySwitchArgs.begin());
294 }
295
296private:
297 virtual void notifyConfigurationChanged(nsecs_t eventTime) {
298 NotifyConfigurationChangedArgs args;
299 args.eventTime = eventTime;
300 mNotifyConfigurationChangedArgs.push_back(args);
301 }
302
Jeff Brown58a2da82011-01-25 16:02:22 -0800303 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brownc3db8582010-10-20 15:33:38 -0700304 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
305 int32_t scanCode, int32_t metaState, nsecs_t downTime) {
306 NotifyKeyArgs args;
307 args.eventTime = eventTime;
308 args.deviceId = deviceId;
309 args.source = source;
310 args.policyFlags = policyFlags;
311 args.action = action;
312 args.flags = flags;
313 args.keyCode = keyCode;
314 args.scanCode = scanCode;
315 args.metaState = metaState;
316 args.downTime = downTime;
317 mNotifyKeyArgs.push_back(args);
318 }
319
Jeff Brown58a2da82011-01-25 16:02:22 -0800320 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brownc3db8582010-10-20 15:33:38 -0700321 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700322 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
323 uint32_t pointerCount, const PointerProperties* pointerProperties,
324 const PointerCoords* pointerCoords,
Jeff Brownc3db8582010-10-20 15:33:38 -0700325 float xPrecision, float yPrecision, nsecs_t downTime) {
326 NotifyMotionArgs args;
327 args.eventTime = eventTime;
328 args.deviceId = deviceId;
329 args.source = source;
330 args.policyFlags = policyFlags;
331 args.action = action;
332 args.flags = flags;
333 args.metaState = metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700334 args.buttonState = buttonState;
Jeff Brownc3db8582010-10-20 15:33:38 -0700335 args.edgeFlags = edgeFlags;
336 args.pointerCount = pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700337 args.pointerProperties.clear();
338 args.pointerProperties.appendArray(pointerProperties, pointerCount);
Jeff Brownc3db8582010-10-20 15:33:38 -0700339 args.pointerCoords.clear();
340 args.pointerCoords.appendArray(pointerCoords, pointerCount);
341 args.xPrecision = xPrecision;
342 args.yPrecision = yPrecision;
343 args.downTime = downTime;
344 mNotifyMotionArgs.push_back(args);
345 }
346
347 virtual void notifySwitch(nsecs_t when,
348 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) {
349 NotifySwitchArgs args;
350 args.when = when;
351 args.switchCode = switchCode;
352 args.switchValue = switchValue;
353 args.policyFlags = policyFlags;
354 mNotifySwitchArgs.push_back(args);
355 }
356
357 virtual void dump(String8& dump) {
358 ADD_FAILURE() << "Should never be called by input reader.";
359 }
360
361 virtual void dispatchOnce() {
362 ADD_FAILURE() << "Should never be called by input reader.";
363 }
364
365 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700366 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
367 uint32_t policyFlags) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700368 ADD_FAILURE() << "Should never be called by input reader.";
369 return INPUT_EVENT_INJECTION_FAILED;
370 }
371
Jeff Brown9302c872011-07-13 22:51:29 -0700372 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700373 ADD_FAILURE() << "Should never be called by input reader.";
374 }
375
Jeff Brown9302c872011-07-13 22:51:29 -0700376 virtual void setFocusedApplication(
377 const sp<InputApplicationHandle>& inputApplicationHandle) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700378 ADD_FAILURE() << "Should never be called by input reader.";
379 }
380
381 virtual void setInputDispatchMode(bool enabled, bool frozen) {
382 ADD_FAILURE() << "Should never be called by input reader.";
383 }
384
Jeff Brown0029c662011-03-30 02:25:18 -0700385 virtual void setInputFilterEnabled(bool enabled) {
386 ADD_FAILURE() << "Should never be called by input reader.";
387 }
388
Jeff Brown7631cbb2010-10-24 15:22:06 -0700389 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
390 const sp<InputChannel>& toChannel) {
391 ADD_FAILURE() << "Should never be called by input reader.";
392 return 0;
393 }
394
Jeff Brown928e0542011-01-10 11:17:36 -0800395 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
396 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700397 ADD_FAILURE() << "Should never be called by input reader.";
398 return 0;
399 }
400
401 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) {
402 ADD_FAILURE() << "Should never be called by input reader.";
403 return 0;
404 }
405};
406
407
408// --- FakeEventHub ---
409
410class FakeEventHub : public EventHubInterface {
411 struct KeyInfo {
412 int32_t keyCode;
413 uint32_t flags;
414 };
415
416 struct Device {
417 String8 name;
418 uint32_t classes;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800419 PropertyMap configuration;
Jeff Brownefd32662011-03-08 15:13:06 -0800420 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
421 KeyedVector<int, bool> relativeAxes;
Jeff Brownc3db8582010-10-20 15:33:38 -0700422 KeyedVector<int32_t, int32_t> keyCodeStates;
423 KeyedVector<int32_t, int32_t> scanCodeStates;
424 KeyedVector<int32_t, int32_t> switchStates;
Jeff Brown2717eff2011-06-30 23:53:07 -0700425 KeyedVector<int32_t, int32_t> absoluteAxisValue;
Jeff Brownc3db8582010-10-20 15:33:38 -0700426 KeyedVector<int32_t, KeyInfo> keys;
Jeff Brown51e7fe72010-10-29 22:19:53 -0700427 KeyedVector<int32_t, bool> leds;
Jeff Brown90655042010-12-02 13:50:46 -0800428 Vector<VirtualKeyDefinition> virtualKeys;
Jeff Brownc3db8582010-10-20 15:33:38 -0700429
430 Device(const String8& name, uint32_t classes) :
431 name(name), classes(classes) {
432 }
433 };
434
435 KeyedVector<int32_t, Device*> mDevices;
436 Vector<String8> mExcludedDevices;
437 List<RawEvent> mEvents;
438
439protected:
440 virtual ~FakeEventHub() {
441 for (size_t i = 0; i < mDevices.size(); i++) {
442 delete mDevices.valueAt(i);
443 }
444 }
445
446public:
447 FakeEventHub() { }
448
449 void addDevice(int32_t deviceId, const String8& name, uint32_t classes) {
450 Device* device = new Device(name, classes);
451 mDevices.add(deviceId, device);
452
453 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0, 0, 0);
454 }
455
456 void removeDevice(int32_t deviceId) {
457 delete mDevices.valueFor(deviceId);
458 mDevices.removeItem(deviceId);
459
460 enqueueEvent(ARBITRARY_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0, 0, 0);
461 }
462
463 void finishDeviceScan() {
464 enqueueEvent(ARBITRARY_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0, 0, 0);
465 }
466
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800467 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
468 Device* device = getDevice(deviceId);
469 device->configuration.addProperty(key, value);
470 }
471
Jeff Brown83c09682010-12-23 17:50:18 -0800472 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
473 Device* device = getDevice(deviceId);
474 device->configuration.addAll(configuration);
475 }
476
Jeff Brownefd32662011-03-08 15:13:06 -0800477 void addAbsoluteAxis(int32_t deviceId, int axis,
Jeff Brownb3a2d132011-06-12 18:14:50 -0700478 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700479 Device* device = getDevice(deviceId);
480
481 RawAbsoluteAxisInfo info;
482 info.valid = true;
483 info.minValue = minValue;
484 info.maxValue = maxValue;
485 info.flat = flat;
486 info.fuzz = fuzz;
Jeff Brownb3a2d132011-06-12 18:14:50 -0700487 info.resolution = resolution;
Jeff Brownefd32662011-03-08 15:13:06 -0800488 device->absoluteAxes.add(axis, info);
489 }
490
491 void addRelativeAxis(int32_t deviceId, int32_t axis) {
492 Device* device = getDevice(deviceId);
493 device->relativeAxes.add(axis, true);
Jeff Brownc3db8582010-10-20 15:33:38 -0700494 }
495
496 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
497 Device* device = getDevice(deviceId);
498 device->keyCodeStates.replaceValueFor(keyCode, state);
499 }
500
501 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
502 Device* device = getDevice(deviceId);
503 device->scanCodeStates.replaceValueFor(scanCode, state);
504 }
505
506 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
507 Device* device = getDevice(deviceId);
508 device->switchStates.replaceValueFor(switchCode, state);
509 }
510
Jeff Brown2717eff2011-06-30 23:53:07 -0700511 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
512 Device* device = getDevice(deviceId);
513 device->absoluteAxisValue.replaceValueFor(axis, value);
514 }
515
Jeff Brownc3db8582010-10-20 15:33:38 -0700516 void addKey(int32_t deviceId, int32_t scanCode, int32_t keyCode, uint32_t flags) {
517 Device* device = getDevice(deviceId);
518 KeyInfo info;
519 info.keyCode = keyCode;
520 info.flags = flags;
521 device->keys.add(scanCode, info);
522 }
523
Jeff Brown51e7fe72010-10-29 22:19:53 -0700524 void addLed(int32_t deviceId, int32_t led, bool initialState) {
525 Device* device = getDevice(deviceId);
526 device->leds.add(led, initialState);
527 }
528
529 bool getLedState(int32_t deviceId, int32_t led) {
530 Device* device = getDevice(deviceId);
531 return device->leds.valueFor(led);
532 }
533
Jeff Brownc3db8582010-10-20 15:33:38 -0700534 Vector<String8>& getExcludedDevices() {
535 return mExcludedDevices;
536 }
537
Jeff Brown90655042010-12-02 13:50:46 -0800538 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
539 Device* device = getDevice(deviceId);
540 device->virtualKeys.push(definition);
541 }
542
Jeff Brownc3db8582010-10-20 15:33:38 -0700543 void enqueueEvent(nsecs_t when, int32_t deviceId, int32_t type,
544 int32_t scanCode, int32_t keyCode, int32_t value, uint32_t flags) {
545 RawEvent event;
546 event.when = when;
547 event.deviceId = deviceId;
548 event.type = type;
549 event.scanCode = scanCode;
550 event.keyCode = keyCode;
551 event.value = value;
552 event.flags = flags;
553 mEvents.push_back(event);
554 }
555
556 void assertQueueIsEmpty() {
557 ASSERT_EQ(size_t(0), mEvents.size())
558 << "Expected the event queue to be empty (fully consumed).";
559 }
560
561private:
562 Device* getDevice(int32_t deviceId) const {
563 ssize_t index = mDevices.indexOfKey(deviceId);
564 return index >= 0 ? mDevices.valueAt(index) : NULL;
565 }
566
567 virtual uint32_t getDeviceClasses(int32_t deviceId) const {
568 Device* device = getDevice(deviceId);
569 return device ? device->classes : 0;
570 }
571
572 virtual String8 getDeviceName(int32_t deviceId) const {
573 Device* device = getDevice(deviceId);
574 return device ? device->name : String8("unknown");
575 }
576
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800577 virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
578 Device* device = getDevice(deviceId);
579 if (device) {
580 *outConfiguration = device->configuration;
581 }
582 }
583
Jeff Brownc3db8582010-10-20 15:33:38 -0700584 virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
585 RawAbsoluteAxisInfo* outAxisInfo) const {
586 Device* device = getDevice(deviceId);
587 if (device) {
Jeff Brownefd32662011-03-08 15:13:06 -0800588 ssize_t index = device->absoluteAxes.indexOfKey(axis);
Jeff Brownc3db8582010-10-20 15:33:38 -0700589 if (index >= 0) {
Jeff Brownefd32662011-03-08 15:13:06 -0800590 *outAxisInfo = device->absoluteAxes.valueAt(index);
Jeff Brownc3db8582010-10-20 15:33:38 -0700591 return OK;
592 }
593 }
594 return -1;
595 }
596
Jeff Browncc0c1592011-02-19 05:07:28 -0800597 virtual bool hasRelativeAxis(int32_t deviceId, int axis) const {
Jeff Brownefd32662011-03-08 15:13:06 -0800598 Device* device = getDevice(deviceId);
599 if (device) {
600 return device->relativeAxes.indexOfKey(axis) >= 0;
601 }
Jeff Browncc0c1592011-02-19 05:07:28 -0800602 return false;
603 }
604
Jeff Brown80fd47c2011-05-24 01:07:44 -0700605 virtual bool hasInputProperty(int32_t deviceId, int property) const {
606 return false;
607 }
608
Jeff Brown6f2fba42011-02-19 01:08:02 -0800609 virtual status_t mapKey(int32_t deviceId, int scancode,
Jeff Brownc3db8582010-10-20 15:33:38 -0700610 int32_t* outKeycode, uint32_t* outFlags) const {
611 Device* device = getDevice(deviceId);
612 if (device) {
613 ssize_t index = device->keys.indexOfKey(scancode);
614 if (index >= 0) {
615 if (outKeycode) {
616 *outKeycode = device->keys.valueAt(index).keyCode;
617 }
618 if (outFlags) {
619 *outFlags = device->keys.valueAt(index).flags;
620 }
621 return OK;
622 }
623 }
624 return NAME_NOT_FOUND;
625 }
626
Jeff Brown6f2fba42011-02-19 01:08:02 -0800627 virtual status_t mapAxis(int32_t deviceId, int scancode,
Jeff Brown85297452011-03-04 13:07:49 -0800628 AxisInfo* outAxisInfo) const {
Jeff Brown6f2fba42011-02-19 01:08:02 -0800629 return NAME_NOT_FOUND;
630 }
631
Jeff Brown1a84fd12011-06-02 01:26:32 -0700632 virtual void setExcludedDevices(const Vector<String8>& devices) {
633 mExcludedDevices = devices;
Jeff Brownc3db8582010-10-20 15:33:38 -0700634 }
635
Jeff Brownb7198742011-03-18 18:14:26 -0700636 virtual size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700637 if (mEvents.empty()) {
Jeff Brownb7198742011-03-18 18:14:26 -0700638 return 0;
Jeff Brownc3db8582010-10-20 15:33:38 -0700639 }
640
Jeff Brownb7198742011-03-18 18:14:26 -0700641 *buffer = *mEvents.begin();
Jeff Brownc3db8582010-10-20 15:33:38 -0700642 mEvents.erase(mEvents.begin());
Jeff Brownb7198742011-03-18 18:14:26 -0700643 return 1;
Jeff Brownc3db8582010-10-20 15:33:38 -0700644 }
645
646 virtual int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const {
647 Device* device = getDevice(deviceId);
648 if (device) {
649 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
650 if (index >= 0) {
651 return device->scanCodeStates.valueAt(index);
652 }
653 }
654 return AKEY_STATE_UNKNOWN;
655 }
656
657 virtual int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
658 Device* device = getDevice(deviceId);
659 if (device) {
660 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
661 if (index >= 0) {
662 return device->keyCodeStates.valueAt(index);
663 }
664 }
665 return AKEY_STATE_UNKNOWN;
666 }
667
668 virtual int32_t getSwitchState(int32_t deviceId, int32_t sw) const {
669 Device* device = getDevice(deviceId);
670 if (device) {
671 ssize_t index = device->switchStates.indexOfKey(sw);
672 if (index >= 0) {
673 return device->switchStates.valueAt(index);
674 }
675 }
676 return AKEY_STATE_UNKNOWN;
677 }
678
Jeff Brown2717eff2011-06-30 23:53:07 -0700679 virtual status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
680 int32_t* outValue) const {
681 Device* device = getDevice(deviceId);
682 if (device) {
683 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
684 if (index >= 0) {
685 *outValue = device->absoluteAxisValue.valueAt(index);
686 return OK;
687 }
688 }
689 *outValue = 0;
690 return -1;
691 }
692
Jeff Brownc3db8582010-10-20 15:33:38 -0700693 virtual bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
694 uint8_t* outFlags) const {
695 bool result = false;
696 Device* device = getDevice(deviceId);
697 if (device) {
698 for (size_t i = 0; i < numCodes; i++) {
699 for (size_t j = 0; j < device->keys.size(); j++) {
700 if (keyCodes[i] == device->keys.valueAt(j).keyCode) {
701 outFlags[i] = 1;
702 result = true;
703 }
704 }
705 }
706 }
707 return result;
708 }
709
Jeff Brown7631cbb2010-10-24 15:22:06 -0700710 virtual bool hasLed(int32_t deviceId, int32_t led) const {
Jeff Brown51e7fe72010-10-29 22:19:53 -0700711 Device* device = getDevice(deviceId);
712 return device && device->leds.indexOfKey(led) >= 0;
Jeff Brown7631cbb2010-10-24 15:22:06 -0700713 }
714
715 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
Jeff Brown51e7fe72010-10-29 22:19:53 -0700716 Device* device = getDevice(deviceId);
717 if (device) {
718 ssize_t index = device->leds.indexOfKey(led);
719 if (index >= 0) {
720 device->leds.replaceValueAt(led, on);
721 } else {
722 ADD_FAILURE()
723 << "Attempted to set the state of an LED that the EventHub declared "
724 "was not present. led=" << led;
725 }
726 }
Jeff Brown7631cbb2010-10-24 15:22:06 -0700727 }
728
Jeff Brown90655042010-12-02 13:50:46 -0800729 virtual void getVirtualKeyDefinitions(int32_t deviceId,
730 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
731 outVirtualKeys.clear();
732
733 Device* device = getDevice(deviceId);
734 if (device) {
735 outVirtualKeys.appendVector(device->virtualKeys);
736 }
737 }
738
Jeff Brown56194eb2011-03-02 19:23:13 -0800739 virtual bool isExternal(int32_t deviceId) const {
740 return false;
741 }
742
Jeff Brownc3db8582010-10-20 15:33:38 -0700743 virtual void dump(String8& dump) {
744 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700745
Jeff Brown93fa9b32011-06-14 17:09:25 -0700746 virtual void requestReopenDevices() {
747 }
748
749 virtual void wake() {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700750 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700751};
752
753
754// --- FakeInputReaderContext ---
755
756class FakeInputReaderContext : public InputReaderContext {
757 sp<EventHubInterface> mEventHub;
758 sp<InputReaderPolicyInterface> mPolicy;
759 sp<InputDispatcherInterface> mDispatcher;
760 int32_t mGlobalMetaState;
761 bool mUpdateGlobalMetaStateWasCalled;
762
763public:
764 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
765 const sp<InputReaderPolicyInterface>& policy,
766 const sp<InputDispatcherInterface>& dispatcher) :
767 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
768 mGlobalMetaState(0) {
769 }
770
771 virtual ~FakeInputReaderContext() { }
772
773 void assertUpdateGlobalMetaStateWasCalled() {
774 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
775 << "Expected updateGlobalMetaState() to have been called.";
776 mUpdateGlobalMetaStateWasCalled = false;
777 }
778
779 void setGlobalMetaState(int32_t state) {
780 mGlobalMetaState = state;
781 }
782
783private:
784 virtual void updateGlobalMetaState() {
785 mUpdateGlobalMetaStateWasCalled = true;
786 }
787
788 virtual int32_t getGlobalMetaState() {
789 return mGlobalMetaState;
790 }
791
792 virtual EventHubInterface* getEventHub() {
793 return mEventHub.get();
794 }
795
796 virtual InputReaderPolicyInterface* getPolicy() {
797 return mPolicy.get();
798 }
799
800 virtual InputDispatcherInterface* getDispatcher() {
801 return mDispatcher.get();
802 }
Jeff Brownfe508922011-01-18 15:10:10 -0800803
804 virtual void disableVirtualKeysUntil(nsecs_t time) {
805 }
806
807 virtual bool shouldDropVirtualKey(nsecs_t now,
808 InputDevice* device, int32_t keyCode, int32_t scanCode) {
809 return false;
810 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800811
812 virtual void fadePointer() {
813 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700814
815 virtual void requestTimeoutAtTime(nsecs_t when) {
816 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700817};
818
819
820// --- FakeInputMapper ---
821
822class FakeInputMapper : public InputMapper {
823 uint32_t mSources;
824 int32_t mKeyboardType;
825 int32_t mMetaState;
826 KeyedVector<int32_t, int32_t> mKeyCodeStates;
827 KeyedVector<int32_t, int32_t> mScanCodeStates;
828 KeyedVector<int32_t, int32_t> mSwitchStates;
829 Vector<int32_t> mSupportedKeyCodes;
830 RawEvent mLastEvent;
831
832 bool mConfigureWasCalled;
833 bool mResetWasCalled;
834 bool mProcessWasCalled;
835
836public:
837 FakeInputMapper(InputDevice* device, uint32_t sources) :
838 InputMapper(device),
839 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
840 mMetaState(0),
841 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
842 }
843
844 virtual ~FakeInputMapper() { }
845
846 void setKeyboardType(int32_t keyboardType) {
847 mKeyboardType = keyboardType;
848 }
849
850 void setMetaState(int32_t metaState) {
851 mMetaState = metaState;
852 }
853
854 void assertConfigureWasCalled() {
855 ASSERT_TRUE(mConfigureWasCalled)
856 << "Expected configure() to have been called.";
857 mConfigureWasCalled = false;
858 }
859
860 void assertResetWasCalled() {
861 ASSERT_TRUE(mResetWasCalled)
862 << "Expected reset() to have been called.";
863 mResetWasCalled = false;
864 }
865
866 void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
867 ASSERT_TRUE(mProcessWasCalled)
868 << "Expected process() to have been called.";
869 if (outLastEvent) {
870 *outLastEvent = mLastEvent;
871 }
872 mProcessWasCalled = false;
873 }
874
875 void setKeyCodeState(int32_t keyCode, int32_t state) {
876 mKeyCodeStates.replaceValueFor(keyCode, state);
877 }
878
879 void setScanCodeState(int32_t scanCode, int32_t state) {
880 mScanCodeStates.replaceValueFor(scanCode, state);
881 }
882
883 void setSwitchState(int32_t switchCode, int32_t state) {
884 mSwitchStates.replaceValueFor(switchCode, state);
885 }
886
887 void addSupportedKeyCode(int32_t keyCode) {
888 mSupportedKeyCodes.add(keyCode);
889 }
890
891private:
892 virtual uint32_t getSources() {
893 return mSources;
894 }
895
896 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
897 InputMapper::populateDeviceInfo(deviceInfo);
898
899 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
900 deviceInfo->setKeyboardType(mKeyboardType);
901 }
902 }
903
Jeff Brown474dcb52011-06-14 20:22:50 -0700904 virtual void configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700905 mConfigureWasCalled = true;
906 }
907
908 virtual void reset() {
909 mResetWasCalled = true;
910 }
911
912 virtual void process(const RawEvent* rawEvent) {
913 mLastEvent = *rawEvent;
914 mProcessWasCalled = true;
915 }
916
917 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
918 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
919 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
920 }
921
922 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
923 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
924 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
925 }
926
927 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode) {
928 ssize_t index = mSwitchStates.indexOfKey(switchCode);
929 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
930 }
931
932 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
933 const int32_t* keyCodes, uint8_t* outFlags) {
934 bool result = false;
935 for (size_t i = 0; i < numCodes; i++) {
936 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
937 if (keyCodes[i] == mSupportedKeyCodes[j]) {
938 outFlags[i] = 1;
939 result = true;
940 }
941 }
942 }
943 return result;
944 }
945
946 virtual int32_t getMetaState() {
947 return mMetaState;
948 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800949
950 virtual void fadePointer() {
951 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700952};
953
954
955// --- InstrumentedInputReader ---
956
957class InstrumentedInputReader : public InputReader {
958 InputDevice* mNextDevice;
959
960public:
961 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
962 const sp<InputReaderPolicyInterface>& policy,
963 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown71c86ad2011-02-07 20:29:08 -0800964 InputReader(eventHub, policy, dispatcher),
965 mNextDevice(NULL) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700966 }
967
968 virtual ~InstrumentedInputReader() {
969 if (mNextDevice) {
970 delete mNextDevice;
971 }
972 }
973
974 void setNextDevice(InputDevice* device) {
975 mNextDevice = device;
976 }
977
978protected:
979 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
980 if (mNextDevice) {
981 InputDevice* device = mNextDevice;
982 mNextDevice = NULL;
983 return device;
984 }
985 return InputReader::createDevice(deviceId, name, classes);
986 }
987
988 friend class InputReaderTest;
989};
990
991
992// --- InputReaderTest ---
993
994class InputReaderTest : public testing::Test {
995protected:
996 sp<FakeInputDispatcher> mFakeDispatcher;
997 sp<FakeInputReaderPolicy> mFakePolicy;
998 sp<FakeEventHub> mFakeEventHub;
999 sp<InstrumentedInputReader> mReader;
1000
1001 virtual void SetUp() {
1002 mFakeEventHub = new FakeEventHub();
1003 mFakePolicy = new FakeInputReaderPolicy();
1004 mFakeDispatcher = new FakeInputDispatcher();
1005
1006 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1007 }
1008
1009 virtual void TearDown() {
1010 mReader.clear();
1011
1012 mFakeDispatcher.clear();
1013 mFakePolicy.clear();
1014 mFakeEventHub.clear();
1015 }
1016
Jeff Brown83c09682010-12-23 17:50:18 -08001017 void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
1018 const PropertyMap* configuration) {
Jeff Brownc3db8582010-10-20 15:33:38 -07001019 mFakeEventHub->addDevice(deviceId, name, classes);
Jeff Brown83c09682010-12-23 17:50:18 -08001020 if (configuration) {
1021 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1022 }
Jeff Brownc3db8582010-10-20 15:33:38 -07001023 mFakeEventHub->finishDeviceScan();
1024 mReader->loopOnce();
1025 mReader->loopOnce();
1026 mFakeEventHub->assertQueueIsEmpty();
1027 }
1028
1029 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId,
Jeff Brown83c09682010-12-23 17:50:18 -08001030 const String8& name, uint32_t classes, uint32_t sources,
1031 const PropertyMap* configuration) {
Jeff Brownc3db8582010-10-20 15:33:38 -07001032 InputDevice* device = new InputDevice(mReader.get(), deviceId, name);
1033 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1034 device->addMapper(mapper);
1035 mReader->setNextDevice(device);
Jeff Brown83c09682010-12-23 17:50:18 -08001036 addDevice(deviceId, name, classes, configuration);
Jeff Brownc3db8582010-10-20 15:33:38 -07001037 return mapper;
1038 }
1039};
1040
1041TEST_F(InputReaderTest, GetInputConfiguration_WhenNoDevices_ReturnsDefaults) {
1042 InputConfiguration config;
1043 mReader->getInputConfiguration(&config);
1044
1045 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1046 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1047 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1048}
1049
1050TEST_F(InputReaderTest, GetInputConfiguration_WhenAlphabeticKeyboardPresent_ReturnsQwertyKeyboard) {
1051 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001052 INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001053
1054 InputConfiguration config;
1055 mReader->getInputConfiguration(&config);
1056
1057 ASSERT_EQ(InputConfiguration::KEYBOARD_QWERTY, config.keyboard);
1058 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1059 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1060}
1061
1062TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchScreenPresent_ReturnsFingerTouchScreen) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001063 PropertyMap configuration;
1064 configuration.addProperty(String8("touch.deviceType"), String8("touchScreen"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001065 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchscreen"),
Jeff Brown58a2da82011-01-25 16:02:22 -08001066 INPUT_DEVICE_CLASS_TOUCH, &configuration));
Jeff Brownc3db8582010-10-20 15:33:38 -07001067
1068 InputConfiguration config;
1069 mReader->getInputConfiguration(&config);
1070
1071 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1072 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1073 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_FINGER, config.touchScreen);
1074}
1075
Jeff Brown58a2da82011-01-25 16:02:22 -08001076TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchPadPresent_ReturnsFingerNoTouch) {
1077 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchpad"),
1078 INPUT_DEVICE_CLASS_TOUCH, NULL));
1079
1080 InputConfiguration config;
1081 mReader->getInputConfiguration(&config);
1082
1083 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1084 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1085 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1086}
1087
Jeff Brown83c09682010-12-23 17:50:18 -08001088TEST_F(InputReaderTest, GetInputConfiguration_WhenMousePresent_ReturnsNoNavigation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001089 sp<FakePointerController> controller = new FakePointerController();
1090 mFakePolicy->setPointerController(0, controller);
1091
Jeff Brown83c09682010-12-23 17:50:18 -08001092 PropertyMap configuration;
1093 configuration.addProperty(String8("cursor.mode"), String8("pointer"));
1094 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("mouse"),
1095 INPUT_DEVICE_CLASS_CURSOR, &configuration));
1096
1097 InputConfiguration config;
1098 mReader->getInputConfiguration(&config);
1099
1100 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1101 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1102 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1103}
1104
Jeff Brownc3db8582010-10-20 15:33:38 -07001105TEST_F(InputReaderTest, GetInputConfiguration_WhenTrackballPresent_ReturnsTrackballNavigation) {
Jeff Brown83c09682010-12-23 17:50:18 -08001106 PropertyMap configuration;
1107 configuration.addProperty(String8("cursor.mode"), String8("navigation"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001108 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("trackball"),
Jeff Brown83c09682010-12-23 17:50:18 -08001109 INPUT_DEVICE_CLASS_CURSOR, &configuration));
Jeff Brownc3db8582010-10-20 15:33:38 -07001110
1111 InputConfiguration config;
1112 mReader->getInputConfiguration(&config);
1113
1114 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1115 ASSERT_EQ(InputConfiguration::NAVIGATION_TRACKBALL, config.navigation);
1116 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1117}
1118
1119TEST_F(InputReaderTest, GetInputConfiguration_WhenDPadPresent_ReturnsDPadNavigation) {
1120 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("dpad"),
Jeff Brown83c09682010-12-23 17:50:18 -08001121 INPUT_DEVICE_CLASS_DPAD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001122
1123 InputConfiguration config;
1124 mReader->getInputConfiguration(&config);
1125
1126 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1127 ASSERT_EQ(InputConfiguration::NAVIGATION_DPAD, config.navigation);
1128 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1129}
1130
1131TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsValid) {
1132 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001133 INPUT_DEVICE_CLASS_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001134
1135 InputDeviceInfo info;
1136 status_t result = mReader->getInputDeviceInfo(1, &info);
1137
1138 ASSERT_EQ(OK, result);
1139 ASSERT_EQ(1, info.getId());
1140 ASSERT_STREQ("keyboard", info.getName().string());
1141 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, info.getKeyboardType());
1142 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, info.getSources());
1143 ASSERT_EQ(size_t(0), info.getMotionRanges().size());
1144}
1145
1146TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsInvalid) {
1147 InputDeviceInfo info;
1148 status_t result = mReader->getInputDeviceInfo(-1, &info);
1149
1150 ASSERT_EQ(NAME_NOT_FOUND, result);
1151}
1152
1153TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsIgnored) {
Jeff Brown83c09682010-12-23 17:50:18 -08001154 addDevice(1, String8("ignored"), 0, NULL); // no classes so device will be ignored
Jeff Brownc3db8582010-10-20 15:33:38 -07001155
1156 InputDeviceInfo info;
1157 status_t result = mReader->getInputDeviceInfo(1, &info);
1158
1159 ASSERT_EQ(NAME_NOT_FOUND, result);
1160}
1161
1162TEST_F(InputReaderTest, GetInputDeviceIds) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001163 sp<FakePointerController> controller = new FakePointerController();
1164 mFakePolicy->setPointerController(2, controller);
1165
Jeff Brownc3db8582010-10-20 15:33:38 -07001166 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001167 INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
1168 ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("mouse"),
1169 INPUT_DEVICE_CLASS_CURSOR, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001170
1171 Vector<int32_t> ids;
1172 mReader->getInputDeviceIds(ids);
1173
1174 ASSERT_EQ(size_t(2), ids.size());
1175 ASSERT_EQ(1, ids[0]);
1176 ASSERT_EQ(2, ids[1]);
1177}
1178
1179TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1180 FakeInputMapper* mapper = NULL;
1181 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001182 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001183 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1184
1185 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1186 AINPUT_SOURCE_ANY, AKEYCODE_A))
1187 << "Should return unknown when the device id is >= 0 but unknown.";
1188
1189 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1190 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1191 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1192
1193 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1194 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1195 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1196
1197 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1198 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1199 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1200
1201 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1202 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1203 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1204}
1205
1206TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1207 FakeInputMapper* mapper = NULL;
1208 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001209 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001210 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1211
1212 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1213 AINPUT_SOURCE_ANY, KEY_A))
1214 << "Should return unknown when the device id is >= 0 but unknown.";
1215
1216 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1217 AINPUT_SOURCE_TRACKBALL, KEY_A))
1218 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1219
1220 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1221 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1222 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1223
1224 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1225 AINPUT_SOURCE_TRACKBALL, KEY_A))
1226 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1227
1228 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1229 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1230 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1231}
1232
1233TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1234 FakeInputMapper* mapper = NULL;
1235 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001236 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001237 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1238
1239 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1240 AINPUT_SOURCE_ANY, SW_LID))
1241 << "Should return unknown when the device id is >= 0 but unknown.";
1242
1243 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1244 AINPUT_SOURCE_TRACKBALL, SW_LID))
1245 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1246
1247 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1248 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1249 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1250
1251 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1252 AINPUT_SOURCE_TRACKBALL, SW_LID))
1253 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1254
1255 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1256 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1257 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1258}
1259
1260TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1261 FakeInputMapper* mapper = NULL;
1262 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001263 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001264 mapper->addSupportedKeyCode(AKEYCODE_A);
1265 mapper->addSupportedKeyCode(AKEYCODE_B);
1266
1267 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1268 uint8_t flags[4] = { 0, 0, 0, 1 };
1269
1270 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1271 << "Should return false when device id is >= 0 but unknown.";
1272 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1273
1274 flags[3] = 1;
1275 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1276 << "Should return false when device id is valid but the sources are not supported by the device.";
1277 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1278
1279 flags[3] = 1;
1280 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1281 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1282 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1283
1284 flags[3] = 1;
1285 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1286 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1287 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1288
1289 flags[3] = 1;
1290 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1291 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1292 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1293}
1294
1295TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Jeff Brown83c09682010-12-23 17:50:18 -08001296 addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
Jeff Brownc3db8582010-10-20 15:33:38 -07001297
1298 FakeInputDispatcher::NotifyConfigurationChangedArgs args;
1299 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyConfigurationChangedWasCalled(&args));
1300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1301}
1302
1303TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1304 FakeInputMapper* mapper = NULL;
1305 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001306 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001307
1308 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, AKEYCODE_A, 1, POLICY_FLAG_WAKE);
1309 mReader->loopOnce();
1310 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1311
1312 RawEvent event;
1313 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1314 ASSERT_EQ(0, event.when);
1315 ASSERT_EQ(1, event.deviceId);
1316 ASSERT_EQ(EV_KEY, event.type);
1317 ASSERT_EQ(KEY_A, event.scanCode);
1318 ASSERT_EQ(AKEYCODE_A, event.keyCode);
1319 ASSERT_EQ(1, event.value);
1320 ASSERT_EQ(POLICY_FLAG_WAKE, event.flags);
1321}
1322
1323
1324// --- InputDeviceTest ---
1325
1326class InputDeviceTest : public testing::Test {
1327protected:
1328 static const char* DEVICE_NAME;
1329 static const int32_t DEVICE_ID;
1330
1331 sp<FakeEventHub> mFakeEventHub;
1332 sp<FakeInputReaderPolicy> mFakePolicy;
1333 sp<FakeInputDispatcher> mFakeDispatcher;
1334 FakeInputReaderContext* mFakeContext;
1335
1336 InputDevice* mDevice;
1337
1338 virtual void SetUp() {
1339 mFakeEventHub = new FakeEventHub();
1340 mFakePolicy = new FakeInputReaderPolicy();
1341 mFakeDispatcher = new FakeInputDispatcher();
1342 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1343
Jeff Brown49ed71d2010-12-06 17:13:33 -08001344 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001345 mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1346 }
1347
1348 virtual void TearDown() {
1349 delete mDevice;
1350
1351 delete mFakeContext;
1352 mFakeDispatcher.clear();
1353 mFakePolicy.clear();
1354 mFakeEventHub.clear();
1355 }
1356};
1357
1358const char* InputDeviceTest::DEVICE_NAME = "device";
1359const int32_t InputDeviceTest::DEVICE_ID = 1;
1360
1361TEST_F(InputDeviceTest, ImmutableProperties) {
1362 ASSERT_EQ(DEVICE_ID, mDevice->getId());
1363 ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
1364}
1365
1366TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1367 // Configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001368 InputReaderConfiguration config;
1369 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001370
1371 // Metadata.
1372 ASSERT_TRUE(mDevice->isIgnored());
1373 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1374
1375 InputDeviceInfo info;
1376 mDevice->getDeviceInfo(&info);
1377 ASSERT_EQ(DEVICE_ID, info.getId());
1378 ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1379 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1380 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1381
1382 // State queries.
1383 ASSERT_EQ(0, mDevice->getMetaState());
1384
1385 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1386 << "Ignored device should return unknown key code state.";
1387 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1388 << "Ignored device should return unknown scan code state.";
1389 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1390 << "Ignored device should return unknown switch state.";
1391
1392 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1393 uint8_t flags[2] = { 0, 1 };
1394 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1395 << "Ignored device should never mark any key codes.";
1396 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1397 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1398
1399 // Reset.
1400 mDevice->reset();
1401}
1402
1403TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1404 // Configuration.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001405 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001406
1407 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1408 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1409 mapper1->setMetaState(AMETA_ALT_ON);
1410 mapper1->addSupportedKeyCode(AKEYCODE_A);
1411 mapper1->addSupportedKeyCode(AKEYCODE_B);
1412 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1413 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1414 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1415 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1416 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1417 mDevice->addMapper(mapper1);
1418
1419 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1420 mapper2->setMetaState(AMETA_SHIFT_ON);
1421 mDevice->addMapper(mapper2);
1422
Jeff Brown474dcb52011-06-14 20:22:50 -07001423 InputReaderConfiguration config;
1424 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001425
1426 String8 propertyValue;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001427 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1428 << "Device should have read configuration during configuration phase.";
Jeff Brownc3db8582010-10-20 15:33:38 -07001429 ASSERT_STREQ("value", propertyValue.string());
1430
1431 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1432 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1433
1434 // Metadata.
1435 ASSERT_FALSE(mDevice->isIgnored());
1436 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1437
1438 InputDeviceInfo info;
1439 mDevice->getDeviceInfo(&info);
1440 ASSERT_EQ(DEVICE_ID, info.getId());
1441 ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1442 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1443 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1444
1445 // State queries.
1446 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1447 << "Should query mappers and combine meta states.";
1448
1449 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1450 << "Should return unknown key code state when source not supported.";
1451 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1452 << "Should return unknown scan code state when source not supported.";
1453 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1454 << "Should return unknown switch state when source not supported.";
1455
1456 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1457 << "Should query mapper when source is supported.";
1458 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1459 << "Should query mapper when source is supported.";
1460 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1461 << "Should query mapper when source is supported.";
1462
1463 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1464 uint8_t flags[4] = { 0, 0, 0, 1 };
1465 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1466 << "Should do nothing when source is unsupported.";
1467 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1468 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1469 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1470 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1471
1472 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1473 << "Should query mapper when source is supported.";
1474 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1475 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1476 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1477 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1478
1479 // Event handling.
1480 RawEvent event;
Jeff Brownb7198742011-03-18 18:14:26 -07001481 mDevice->process(&event, 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07001482
1483 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1484 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1485
1486 // Reset.
1487 mDevice->reset();
1488
1489 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1490 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1491}
1492
1493
1494// --- InputMapperTest ---
1495
1496class InputMapperTest : public testing::Test {
1497protected:
1498 static const char* DEVICE_NAME;
1499 static const int32_t DEVICE_ID;
1500
1501 sp<FakeEventHub> mFakeEventHub;
1502 sp<FakeInputReaderPolicy> mFakePolicy;
1503 sp<FakeInputDispatcher> mFakeDispatcher;
1504 FakeInputReaderContext* mFakeContext;
1505 InputDevice* mDevice;
1506
1507 virtual void SetUp() {
1508 mFakeEventHub = new FakeEventHub();
1509 mFakePolicy = new FakeInputReaderPolicy();
1510 mFakeDispatcher = new FakeInputDispatcher();
1511 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1512 mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1513
1514 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1515 }
1516
1517 virtual void TearDown() {
1518 delete mDevice;
1519 delete mFakeContext;
1520 mFakeDispatcher.clear();
1521 mFakePolicy.clear();
1522 mFakeEventHub.clear();
1523 }
1524
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001525 void addConfigurationProperty(const char* key, const char* value) {
1526 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
Jeff Brownc3db8582010-10-20 15:33:38 -07001527 }
1528
1529 void addMapperAndConfigure(InputMapper* mapper) {
Jeff Brown474dcb52011-06-14 20:22:50 -07001530 InputReaderConfiguration config;
1531
Jeff Brownc3db8582010-10-20 15:33:38 -07001532 mDevice->addMapper(mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -07001533 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001534 }
1535
1536 static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
1537 int32_t scanCode, int32_t keyCode, int32_t value, uint32_t flags) {
1538 RawEvent event;
1539 event.when = when;
1540 event.deviceId = deviceId;
1541 event.type = type;
1542 event.scanCode = scanCode;
1543 event.keyCode = keyCode;
1544 event.value = value;
1545 event.flags = flags;
1546 mapper->process(&event);
1547 }
1548
1549 static void assertMotionRange(const InputDeviceInfo& info,
Jeff Brownefd32662011-03-08 15:13:06 -08001550 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1551 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
1552 ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
1553 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1554 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1555 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1556 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1557 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1558 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
Jeff Brownc3db8582010-10-20 15:33:38 -07001559 }
1560
1561 static void assertPointerCoords(const PointerCoords& coords,
1562 float x, float y, float pressure, float size,
1563 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1564 float orientation) {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001565 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1566 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1567 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1568 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1569 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1570 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1571 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1572 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1573 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
Jeff Brownc3db8582010-10-20 15:33:38 -07001574 }
1575};
1576
1577const char* InputMapperTest::DEVICE_NAME = "device";
1578const int32_t InputMapperTest::DEVICE_ID = 1;
1579
1580
1581// --- SwitchInputMapperTest ---
1582
1583class SwitchInputMapperTest : public InputMapperTest {
1584protected:
1585};
1586
1587TEST_F(SwitchInputMapperTest, GetSources) {
1588 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1589 addMapperAndConfigure(mapper);
1590
Jeff Brown89de57a2011-01-19 18:41:38 -08001591 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
Jeff Brownc3db8582010-10-20 15:33:38 -07001592}
1593
1594TEST_F(SwitchInputMapperTest, GetSwitchState) {
1595 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1596 addMapperAndConfigure(mapper);
1597
1598 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1599 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1600
1601 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1602 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1603}
1604
1605TEST_F(SwitchInputMapperTest, Process) {
1606 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1607 addMapperAndConfigure(mapper);
1608
1609 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 0, 1, 0);
1610
1611 FakeInputDispatcher::NotifySwitchArgs args;
1612 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifySwitchWasCalled(&args));
1613 ASSERT_EQ(ARBITRARY_TIME, args.when);
1614 ASSERT_EQ(SW_LID, args.switchCode);
1615 ASSERT_EQ(1, args.switchValue);
1616 ASSERT_EQ(uint32_t(0), args.policyFlags);
1617}
1618
1619
1620// --- KeyboardInputMapperTest ---
1621
1622class KeyboardInputMapperTest : public InputMapperTest {
1623protected:
1624 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1625 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1626};
1627
1628void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
1629 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
1630 FakeInputDispatcher::NotifyKeyArgs args;
1631
1632 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 1, 0);
1633 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1634 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1635 ASSERT_EQ(originalScanCode, args.scanCode);
1636 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1637
1638 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 0, 0);
1639 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1641 ASSERT_EQ(originalScanCode, args.scanCode);
1642 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1643}
1644
1645
1646TEST_F(KeyboardInputMapperTest, GetSources) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001647 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001648 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1649 addMapperAndConfigure(mapper);
1650
1651 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1652}
1653
1654TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001655 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001656 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1657 addMapperAndConfigure(mapper);
1658
1659 // Key down.
1660 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1661 EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1662 FakeInputDispatcher::NotifyKeyArgs args;
1663 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1664 ASSERT_EQ(DEVICE_ID, args.deviceId);
1665 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1666 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1667 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1668 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1669 ASSERT_EQ(KEY_HOME, args.scanCode);
1670 ASSERT_EQ(AMETA_NONE, args.metaState);
1671 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1672 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1673 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1674
1675 // Key up.
1676 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1677 EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1678 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1679 ASSERT_EQ(DEVICE_ID, args.deviceId);
1680 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1681 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1683 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1684 ASSERT_EQ(KEY_HOME, args.scanCode);
1685 ASSERT_EQ(AMETA_NONE, args.metaState);
1686 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1687 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1688 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1689}
1690
1691TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreNotDown_DoesNotSynthesizeKeyUp) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001692 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001693 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1694 addMapperAndConfigure(mapper);
1695
1696 // Key down.
1697 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1698 EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1699 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1700
1701 // Key up.
1702 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1703 EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1704 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1705
1706 // Reset. Since no keys still down, should not synthesize any key ups.
1707 mapper->reset();
1708 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1709}
1710
1711TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreDown_SynthesizesKeyUps) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001712 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001713 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1714 addMapperAndConfigure(mapper);
1715
1716 // Metakey down.
1717 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1718 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1719 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1720
1721 // Key down.
1722 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1723 EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1724 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1725
1726 // Reset. Since two keys are still down, should synthesize two key ups in reverse order.
1727 mapper->reset();
1728
1729 FakeInputDispatcher::NotifyKeyArgs args;
1730 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1731 ASSERT_EQ(DEVICE_ID, args.deviceId);
1732 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1733 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1734 ASSERT_EQ(AKEYCODE_A, args.keyCode);
1735 ASSERT_EQ(KEY_A, args.scanCode);
1736 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1737 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1738 ASSERT_EQ(uint32_t(0), args.policyFlags);
1739 ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1740
1741 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1742 ASSERT_EQ(DEVICE_ID, args.deviceId);
1743 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1744 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1745 ASSERT_EQ(AKEYCODE_SHIFT_LEFT, args.keyCode);
1746 ASSERT_EQ(KEY_LEFTSHIFT, args.scanCode);
1747 ASSERT_EQ(AMETA_NONE, args.metaState);
1748 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1749 ASSERT_EQ(uint32_t(0), args.policyFlags);
1750 ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1751
1752 // And that's it.
1753 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1754}
1755
1756TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001757 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001758 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1759 addMapperAndConfigure(mapper);
1760
1761 // Initial metastate.
1762 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1763
1764 // Metakey down.
1765 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1766 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1767 FakeInputDispatcher::NotifyKeyArgs args;
1768 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1769 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1770 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1771 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1772
1773 // Key down.
1774 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1775 EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1776 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1777 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1779
1780 // Key up.
1781 process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
1782 EV_KEY, KEY_A, AKEYCODE_A, 0, 0);
1783 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1785 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1786
1787 // Metakey up.
1788 process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
1789 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 0, 0);
1790 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1791 ASSERT_EQ(AMETA_NONE, args.metaState);
1792 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1793 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1794}
1795
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001796TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
1797 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001798 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1799 addMapperAndConfigure(mapper);
1800
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001801 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1802 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001803 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07001804 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1805 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1806 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1807 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1808 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1809 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1810 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1811 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1812}
1813
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001814TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
1815 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001816 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001817 addConfigurationProperty("keyboard.orientationAware", "1");
Jeff Brownc3db8582010-10-20 15:33:38 -07001818 addMapperAndConfigure(mapper);
1819
1820 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1821 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001822 DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001823 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1824 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1825 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1826 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1827 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1828 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1829 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1830 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1831
1832 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1833 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001834 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07001835 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1836 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
1837 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1838 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
1839 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1840 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
1841 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1842 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
1843
1844 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1845 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001846 DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07001847 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1848 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
1849 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1850 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
1851 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1852 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
1853 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1854 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
1855
1856 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1857 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001858 DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07001859 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1860 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
1861 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1862 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
1863 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1864 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
1865 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1866 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
1867
1868 // Special case: if orientation changes while key is down, we still emit the same keycode
1869 // in the key up as we did in the key down.
1870 FakeInputDispatcher::NotifyKeyArgs args;
1871
1872 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1873 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001874 DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07001875 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 1, 0);
1876 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1877 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1878 ASSERT_EQ(KEY_UP, args.scanCode);
1879 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1880
1881 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1882 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001883 DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07001884 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 0, 0);
1885 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1886 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1887 ASSERT_EQ(KEY_UP, args.scanCode);
1888 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1889}
1890
1891TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001892 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001893 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1894 addMapperAndConfigure(mapper);
1895
1896 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
1897 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1898
1899 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
1900 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1901}
1902
1903TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001904 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001905 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1906 addMapperAndConfigure(mapper);
1907
1908 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
1909 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1910
1911 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
1912 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1913}
1914
1915TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001916 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001917 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1918 addMapperAndConfigure(mapper);
1919
1920 mFakeEventHub->addKey(DEVICE_ID, KEY_A, AKEYCODE_A, 0);
1921
1922 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1923 uint8_t flags[2] = { 0, 0 };
1924 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
1925 ASSERT_TRUE(flags[0]);
1926 ASSERT_FALSE(flags[1]);
1927}
1928
Jeff Brown51e7fe72010-10-29 22:19:53 -07001929TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
1930 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
1931 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
1932 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
1933
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001934 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001935 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1936 addMapperAndConfigure(mapper);
1937
1938 // Initialization should have turned all of the lights off.
1939 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1940 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1941 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1942
1943 // Toggle caps lock on.
1944 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1945 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1946 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1947 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
1948 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1949 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1950 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1951 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
1952
1953 // Toggle num lock on.
1954 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1955 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1956 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1957 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1958 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1959 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1960 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1961 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
1962
1963 // Toggle caps lock off.
1964 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1965 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1966 process(mapper, ARBITRARY_TIME, DEVICE_ID,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001967 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
Jeff Brown51e7fe72010-10-29 22:19:53 -07001968 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1969 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1970 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1971 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
1972
1973 // Toggle scroll lock on.
1974 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1975 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
1976 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1977 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
1978 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1979 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1980 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1981 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1982
1983 // Toggle num lock off.
1984 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1985 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1986 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1987 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1988 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1989 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1990 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1991 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1992
1993 // Toggle scroll lock off.
1994 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1995 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
1996 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1997 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
1998 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1999 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2000 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2001 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2002}
2003
Jeff Brownc3db8582010-10-20 15:33:38 -07002004
Jeff Brown83c09682010-12-23 17:50:18 -08002005// --- CursorInputMapperTest ---
Jeff Brownc3db8582010-10-20 15:33:38 -07002006
Jeff Brown83c09682010-12-23 17:50:18 -08002007class CursorInputMapperTest : public InputMapperTest {
Jeff Brownc3db8582010-10-20 15:33:38 -07002008protected:
2009 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2010
Jeff Brown83c09682010-12-23 17:50:18 -08002011 sp<FakePointerController> mFakePointerController;
2012
2013 virtual void SetUp() {
2014 InputMapperTest::SetUp();
2015
2016 mFakePointerController = new FakePointerController();
2017 mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
2018 }
2019
2020 void testMotionRotation(CursorInputMapper* mapper,
Jeff Brownc3db8582010-10-20 15:33:38 -07002021 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
2022};
2023
Jeff Brown83c09682010-12-23 17:50:18 -08002024const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
Jeff Brownc3db8582010-10-20 15:33:38 -07002025
Jeff Brown83c09682010-12-23 17:50:18 -08002026void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
Jeff Brownc3db8582010-10-20 15:33:38 -07002027 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2028 FakeInputDispatcher::NotifyMotionArgs args;
2029
2030 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, originalX, 0);
2031 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, originalY, 0);
2032 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2033 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2036 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2037 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2038 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2039}
2040
Jeff Brown83c09682010-12-23 17:50:18 -08002041TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2042 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2043 addConfigurationProperty("cursor.mode", "pointer");
2044 addMapperAndConfigure(mapper);
2045
2046 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2047}
2048
2049TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2050 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2051 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002052 addMapperAndConfigure(mapper);
2053
2054 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2055}
2056
Jeff Brown83c09682010-12-23 17:50:18 -08002057TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2058 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2059 addConfigurationProperty("cursor.mode", "pointer");
2060 addMapperAndConfigure(mapper);
2061
2062 InputDeviceInfo info;
2063 mapper->populateDeviceInfo(&info);
2064
2065 // Initially there may not be a valid motion range.
Jeff Brownefd32662011-03-08 15:13:06 -08002066 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2067 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
2068 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2069 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brown83c09682010-12-23 17:50:18 -08002070
2071 // When the bounds are set, then there should be a valid motion range.
Jeff Brown9626b142011-03-03 02:09:54 -08002072 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
Jeff Brown83c09682010-12-23 17:50:18 -08002073
2074 InputDeviceInfo info2;
2075 mapper->populateDeviceInfo(&info2);
2076
Jeff Brownefd32662011-03-08 15:13:06 -08002077 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2078 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
Jeff Brown9626b142011-03-03 02:09:54 -08002079 1, 800 - 1, 0.0f, 0.0f));
Jeff Brownefd32662011-03-08 15:13:06 -08002080 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2081 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
Jeff Brown9626b142011-03-03 02:09:54 -08002082 2, 480 - 1, 0.0f, 0.0f));
Jeff Brownefd32662011-03-08 15:13:06 -08002083 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2084 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002085 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brown83c09682010-12-23 17:50:18 -08002086}
2087
2088TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2089 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2090 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002091 addMapperAndConfigure(mapper);
2092
2093 InputDeviceInfo info;
2094 mapper->populateDeviceInfo(&info);
2095
Jeff Brownefd32662011-03-08 15:13:06 -08002096 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2097 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
Jeff Brownc3db8582010-10-20 15:33:38 -07002098 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
Jeff Brownefd32662011-03-08 15:13:06 -08002099 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2100 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
Jeff Brownc3db8582010-10-20 15:33:38 -07002101 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
Jeff Brownefd32662011-03-08 15:13:06 -08002102 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2103 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002104 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brownc3db8582010-10-20 15:33:38 -07002105}
2106
Jeff Brown83c09682010-12-23 17:50:18 -08002107TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2108 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2109 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002110 addMapperAndConfigure(mapper);
2111
2112 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2113
2114 FakeInputDispatcher::NotifyMotionArgs args;
2115
2116 // Button press.
2117 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
2118 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2119 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2120 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2121 ASSERT_EQ(DEVICE_ID, args.deviceId);
2122 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2123 ASSERT_EQ(uint32_t(0), args.policyFlags);
2124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2125 ASSERT_EQ(0, args.flags);
2126 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002127 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002128 ASSERT_EQ(0, args.edgeFlags);
2129 ASSERT_EQ(uint32_t(1), args.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002130 ASSERT_EQ(0, args.pointerProperties[0].id);
2131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002132 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2133 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2134 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2135 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2136 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2137
2138 // Button release. Should have same down time.
2139 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2140 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2141 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2142 ASSERT_EQ(DEVICE_ID, args.deviceId);
2143 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2144 ASSERT_EQ(uint32_t(0), args.policyFlags);
2145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2146 ASSERT_EQ(0, args.flags);
2147 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002148 ASSERT_EQ(0, args.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002149 ASSERT_EQ(0, args.edgeFlags);
2150 ASSERT_EQ(uint32_t(1), args.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002151 ASSERT_EQ(0, args.pointerProperties[0].id);
2152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2154 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2155 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2156 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2157 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2158}
2159
Jeff Brown83c09682010-12-23 17:50:18 -08002160TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2161 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2162 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002163 addMapperAndConfigure(mapper);
2164
2165 FakeInputDispatcher::NotifyMotionArgs args;
2166
2167 // Motion in X but not Y.
2168 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2169 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2170 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2173 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2174
2175 // Motion in Y but not X.
2176 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2177 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2178 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Jeff Brownc3db8582010-10-20 15:33:38 -07002180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2181 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2182}
2183
Jeff Brown83c09682010-12-23 17:50:18 -08002184TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2185 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2186 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002187 addMapperAndConfigure(mapper);
2188
2189 FakeInputDispatcher::NotifyMotionArgs args;
2190
2191 // Button press without following sync.
2192 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2193 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2196 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2197
2198 // Button release without following sync.
2199 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2200 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2203 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2204}
2205
Jeff Brown83c09682010-12-23 17:50:18 -08002206TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2207 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2208 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002209 addMapperAndConfigure(mapper);
2210
2211 FakeInputDispatcher::NotifyMotionArgs args;
2212
2213 // Combined X, Y and Button.
2214 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2215 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2216 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2217 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2218 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2221 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2222 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2223
2224 // Move X, Y a bit while pressed.
2225 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 2, 0);
2226 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, 1, 0);
2227 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2228 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2231 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2232 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2233
2234 // Release Button.
2235 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2236 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2237 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2239 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2240}
2241
Jeff Brown83c09682010-12-23 17:50:18 -08002242TEST_F(CursorInputMapperTest, Reset_WhenButtonIsNotDown_ShouldNotSynthesizeButtonUp) {
2243 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2244 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002245 addMapperAndConfigure(mapper);
2246
2247 FakeInputDispatcher::NotifyMotionArgs args;
2248
2249 // Button press.
2250 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2251 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2252
2253 // Button release.
2254 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
2255 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2256
2257 // Reset. Should not synthesize button up since button is not pressed.
2258 mapper->reset();
2259
2260 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2261}
2262
Jeff Brown83c09682010-12-23 17:50:18 -08002263TEST_F(CursorInputMapperTest, Reset_WhenButtonIsDown_ShouldSynthesizeButtonUp) {
2264 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2265 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002266 addMapperAndConfigure(mapper);
2267
2268 FakeInputDispatcher::NotifyMotionArgs args;
2269
2270 // Button press.
2271 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2272 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2273
2274 // Reset. Should synthesize button up.
2275 mapper->reset();
2276
2277 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2280 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2281}
2282
Jeff Brown83c09682010-12-23 17:50:18 -08002283TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2284 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2285 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002286 addMapperAndConfigure(mapper);
2287
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002288 mFakePolicy->setDisplayInfo(DISPLAY_ID,
2289 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002290 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07002291 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2292 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2293 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2294 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2295 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2298 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2299}
2300
Jeff Brown83c09682010-12-23 17:50:18 -08002301TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2302 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2303 addConfigurationProperty("cursor.mode", "navigation");
2304 addConfigurationProperty("cursor.orientationAware", "1");
Jeff Brownc3db8582010-10-20 15:33:38 -07002305 addMapperAndConfigure(mapper);
2306
2307 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002308 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002309 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2310 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2311 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2312 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2313 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2314 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2315 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2316 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2317
2318 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002319 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07002320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2325 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2326 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2327 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2328
2329 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002330 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07002331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2334 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2335 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2336 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2339
2340 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002341 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07002342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2345 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2346 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2350}
2351
2352
2353// --- TouchInputMapperTest ---
2354
2355class TouchInputMapperTest : public InputMapperTest {
2356protected:
2357 static const int32_t RAW_X_MIN;
2358 static const int32_t RAW_X_MAX;
2359 static const int32_t RAW_Y_MIN;
2360 static const int32_t RAW_Y_MAX;
2361 static const int32_t RAW_TOUCH_MIN;
2362 static const int32_t RAW_TOUCH_MAX;
2363 static const int32_t RAW_TOOL_MIN;
2364 static const int32_t RAW_TOOL_MAX;
2365 static const int32_t RAW_PRESSURE_MIN;
2366 static const int32_t RAW_PRESSURE_MAX;
2367 static const int32_t RAW_ORIENTATION_MIN;
2368 static const int32_t RAW_ORIENTATION_MAX;
2369 static const int32_t RAW_ID_MIN;
2370 static const int32_t RAW_ID_MAX;
2371 static const float X_PRECISION;
2372 static const float Y_PRECISION;
2373
2374 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
2375
2376 enum Axes {
2377 POSITION = 1 << 0,
2378 TOUCH = 1 << 1,
2379 TOOL = 1 << 2,
2380 PRESSURE = 1 << 3,
2381 ORIENTATION = 1 << 4,
2382 MINOR = 1 << 5,
2383 ID = 1 << 6,
2384 };
2385
2386 void prepareDisplay(int32_t orientation);
2387 void prepareVirtualKeys();
2388 int32_t toRawX(float displayX);
2389 int32_t toRawY(float displayY);
2390 float toDisplayX(int32_t rawX);
2391 float toDisplayY(int32_t rawY);
2392};
2393
2394const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
Jeff Brown9626b142011-03-03 02:09:54 -08002395const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
Jeff Brownc3db8582010-10-20 15:33:38 -07002396const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
Jeff Brown9626b142011-03-03 02:09:54 -08002397const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
Jeff Brownc3db8582010-10-20 15:33:38 -07002398const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2399const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2400const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2401const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2402const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
2403const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
2404const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2405const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2406const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2407const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
Jeff Brown9626b142011-03-03 02:09:54 -08002408const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2409const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Jeff Brownc3db8582010-10-20 15:33:38 -07002410
2411const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
2412 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
2413 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
2414};
2415
2416void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2417 mFakePolicy->setDisplayInfo(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2418}
2419
2420void TouchInputMapperTest::prepareVirtualKeys() {
Jeff Brown90655042010-12-02 13:50:46 -08002421 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
2422 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
Jeff Brownc3db8582010-10-20 15:33:38 -07002423 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2424 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, AKEYCODE_MENU, POLICY_FLAG_WAKE);
2425}
2426
2427int32_t TouchInputMapperTest::toRawX(float displayX) {
Jeff Brown9626b142011-03-03 02:09:54 -08002428 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07002429}
2430
2431int32_t TouchInputMapperTest::toRawY(float displayY) {
Jeff Brown9626b142011-03-03 02:09:54 -08002432 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07002433}
2434
2435float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Jeff Brown9626b142011-03-03 02:09:54 -08002436 return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07002437}
2438
2439float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Jeff Brown9626b142011-03-03 02:09:54 -08002440 return float(rawY - RAW_Y_MIN) * DISPLAY_HEIGHT / (RAW_Y_MAX - RAW_Y_MIN + 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07002441}
2442
2443
2444// --- SingleTouchInputMapperTest ---
2445
2446class SingleTouchInputMapperTest : public TouchInputMapperTest {
2447protected:
2448 void prepareAxes(int axes);
2449
2450 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2451 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2452 void processUp(SingleTouchInputMapper* mappery);
2453 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
2454 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
2455 void processSync(SingleTouchInputMapper* mapper);
2456};
2457
2458void SingleTouchInputMapperTest::prepareAxes(int axes) {
2459 if (axes & POSITION) {
Jeff Brownefd32662011-03-08 15:13:06 -08002460 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
2461 RAW_X_MIN, RAW_X_MAX, 0, 0);
2462 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
2463 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002464 }
2465 if (axes & PRESSURE) {
Jeff Brownefd32662011-03-08 15:13:06 -08002466 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
2467 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002468 }
2469 if (axes & TOOL) {
Jeff Brownefd32662011-03-08 15:13:06 -08002470 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
2471 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002472 }
2473}
2474
2475void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2476 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 1, 0);
2477 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2478 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2479}
2480
2481void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2482 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2483 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2484}
2485
2486void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
2487 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 0, 0);
2488}
2489
2490void SingleTouchInputMapperTest::processPressure(
2491 SingleTouchInputMapper* mapper, int32_t pressure) {
2492 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, 0, pressure, 0);
2493}
2494
2495void SingleTouchInputMapperTest::processToolMajor(
2496 SingleTouchInputMapper* mapper, int32_t toolMajor) {
2497 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, 0, toolMajor, 0);
2498}
2499
2500void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
2501 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2502}
2503
2504
Jeff Brownace13b12011-03-09 17:39:48 -08002505TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002506 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2507 prepareAxes(POSITION);
2508 addMapperAndConfigure(mapper);
2509
Jeff Brownace13b12011-03-09 17:39:48 -08002510 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2511}
2512
2513TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
2514 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2515 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
2516 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
2517 prepareAxes(POSITION);
2518 addMapperAndConfigure(mapper);
2519
Jeff Brown58a2da82011-01-25 16:02:22 -08002520 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2521}
2522
Jeff Brown49ed71d2010-12-06 17:13:33 -08002523TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002524 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brownc3db8582010-10-20 15:33:38 -07002525 prepareAxes(POSITION);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002526 addConfigurationProperty("touch.deviceType", "touchPad");
Jeff Brownc3db8582010-10-20 15:33:38 -07002527 addMapperAndConfigure(mapper);
2528
2529 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2530}
2531
Jeff Brown49ed71d2010-12-06 17:13:33 -08002532TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002533 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brownc3db8582010-10-20 15:33:38 -07002534 prepareAxes(POSITION);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002535 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownc3db8582010-10-20 15:33:38 -07002536 addMapperAndConfigure(mapper);
2537
2538 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
2539}
2540
2541TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002542 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002543 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002544 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002545 prepareAxes(POSITION);
2546 prepareVirtualKeys();
2547 addMapperAndConfigure(mapper);
2548
2549 // Unknown key.
2550 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2551
2552 // Virtual key is down.
2553 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2554 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2555 processDown(mapper, x, y);
2556 processSync(mapper);
2557 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2558
2559 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2560
2561 // Virtual key is up.
2562 processUp(mapper);
2563 processSync(mapper);
2564 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2565
2566 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2567}
2568
2569TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002570 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002571 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002572 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002573 prepareAxes(POSITION);
2574 prepareVirtualKeys();
2575 addMapperAndConfigure(mapper);
2576
2577 // Unknown key.
2578 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2579
2580 // Virtual key is down.
2581 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2582 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2583 processDown(mapper, x, y);
2584 processSync(mapper);
2585 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2586
2587 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2588
2589 // Virtual key is up.
2590 processUp(mapper);
2591 processSync(mapper);
2592 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2593
2594 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2595}
2596
2597TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002598 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002599 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002600 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002601 prepareAxes(POSITION);
2602 prepareVirtualKeys();
2603 addMapperAndConfigure(mapper);
2604
2605 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
2606 uint8_t flags[2] = { 0, 0 };
2607 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
2608 ASSERT_TRUE(flags[0]);
2609 ASSERT_FALSE(flags[1]);
2610}
2611
2612TEST_F(SingleTouchInputMapperTest, Reset_WhenVirtualKeysAreDown_SendsUp) {
2613 // Note: Ideally we should send cancels but the implementation is more straightforward
2614 // with up and this will only happen if a device is forcibly removed.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002615 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002616 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002617 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002618 prepareAxes(POSITION);
2619 prepareVirtualKeys();
2620 addMapperAndConfigure(mapper);
2621
2622 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2623
2624 // Press virtual key.
2625 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2626 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2627 processDown(mapper, x, y);
2628 processSync(mapper);
2629 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2630
2631 // Reset. Since key is down, synthesize key up.
2632 mapper->reset();
2633
2634 FakeInputDispatcher::NotifyKeyArgs args;
2635 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2636 //ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2637 ASSERT_EQ(DEVICE_ID, args.deviceId);
2638 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2639 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2641 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2642 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2643 ASSERT_EQ(KEY_HOME, args.scanCode);
2644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2645 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2646}
2647
2648TEST_F(SingleTouchInputMapperTest, Reset_WhenNothingIsPressed_NothingMuchHappens) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002649 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002650 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002651 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002652 prepareAxes(POSITION);
2653 prepareVirtualKeys();
2654 addMapperAndConfigure(mapper);
2655
2656 // Press virtual key.
2657 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2658 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2659 processDown(mapper, x, y);
2660 processSync(mapper);
2661 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2662
2663 // Release virtual key.
2664 processUp(mapper);
2665 processSync(mapper);
2666 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2667
2668 // Reset. Since no key is down, nothing happens.
2669 mapper->reset();
2670
2671 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2672 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2673}
2674
2675TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002676 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002677 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002678 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002679 prepareAxes(POSITION);
2680 prepareVirtualKeys();
2681 addMapperAndConfigure(mapper);
2682
2683 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2684
2685 FakeInputDispatcher::NotifyKeyArgs args;
2686
2687 // Press virtual key.
2688 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2689 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2690 processDown(mapper, x, y);
2691 processSync(mapper);
2692
2693 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2694 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2695 ASSERT_EQ(DEVICE_ID, args.deviceId);
2696 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2697 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2698 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2699 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2700 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2701 ASSERT_EQ(KEY_HOME, args.scanCode);
2702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2703 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2704
2705 // Release virtual key.
2706 processUp(mapper);
2707 processSync(mapper);
2708
2709 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2710 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2711 ASSERT_EQ(DEVICE_ID, args.deviceId);
2712 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2713 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2714 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2715 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2716 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2717 ASSERT_EQ(KEY_HOME, args.scanCode);
2718 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2719 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2720
2721 // Should not have sent any motions.
2722 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2723}
2724
2725TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002726 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002727 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002728 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002729 prepareAxes(POSITION);
2730 prepareVirtualKeys();
2731 addMapperAndConfigure(mapper);
2732
2733 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2734
2735 FakeInputDispatcher::NotifyKeyArgs keyArgs;
2736
2737 // Press virtual key.
2738 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2739 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2740 processDown(mapper, x, y);
2741 processSync(mapper);
2742
2743 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2744 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2745 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2746 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2747 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2749 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
2750 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2751 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2752 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2753 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2754
2755 // Move out of bounds. This should generate a cancel and a pointer down since we moved
2756 // into the display area.
2757 y -= 100;
2758 processMove(mapper, x, y);
2759 processSync(mapper);
2760
2761 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2762 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2763 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2764 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2765 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2766 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2767 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2768 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
2769 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2770 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2772 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2773
2774 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2775 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2777 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2778 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2779 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2781 ASSERT_EQ(0, motionArgs.flags);
2782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002783 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002784 ASSERT_EQ(0, motionArgs.edgeFlags);
2785 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002786 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2789 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2790 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2791 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2793
2794 // Keep moving out of bounds. Should generate a pointer move.
2795 y -= 50;
2796 processMove(mapper, x, y);
2797 processSync(mapper);
2798
2799 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2801 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2803 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2805 ASSERT_EQ(0, motionArgs.flags);
2806 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002807 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002808 ASSERT_EQ(0, motionArgs.edgeFlags);
2809 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2813 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2817
2818 // Release out of bounds. Should generate a pointer up.
2819 processUp(mapper);
2820 processSync(mapper);
2821
2822 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2824 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2825 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2826 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2827 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2828 ASSERT_EQ(0, motionArgs.flags);
2829 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002830 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002831 ASSERT_EQ(0, motionArgs.edgeFlags);
2832 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002833 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2836 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2837 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2838 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2840
2841 // Should not have sent any more keys or motions.
2842 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2843 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2844}
2845
2846TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002847 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002848 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002849 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002850 prepareAxes(POSITION);
2851 prepareVirtualKeys();
2852 addMapperAndConfigure(mapper);
2853
2854 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2855
2856 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2857
2858 // Initially go down out of bounds.
2859 int32_t x = -10;
2860 int32_t y = -10;
2861 processDown(mapper, x, y);
2862 processSync(mapper);
2863
2864 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2865
2866 // Move into the display area. Should generate a pointer down.
2867 x = 50;
2868 y = 75;
2869 processMove(mapper, x, y);
2870 processSync(mapper);
2871
2872 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2874 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2876 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2878 ASSERT_EQ(0, motionArgs.flags);
2879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002880 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002881 ASSERT_EQ(0, motionArgs.edgeFlags);
2882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002883 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2886 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2887 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2888 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2890
2891 // Release. Should generate a pointer up.
2892 processUp(mapper);
2893 processSync(mapper);
2894
2895 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2897 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2898 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2899 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2901 ASSERT_EQ(0, motionArgs.flags);
2902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002903 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002904 ASSERT_EQ(0, motionArgs.edgeFlags);
2905 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002906 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2909 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2913
2914 // Should not have sent any more keys or motions.
2915 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2916 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2917}
2918
2919TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002920 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002921 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002922 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002923 prepareAxes(POSITION);
2924 prepareVirtualKeys();
2925 addMapperAndConfigure(mapper);
2926
2927 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2928
2929 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2930
2931 // Down.
2932 int32_t x = 100;
2933 int32_t y = 125;
2934 processDown(mapper, x, y);
2935 processSync(mapper);
2936
2937 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2938 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2939 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2940 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2941 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2943 ASSERT_EQ(0, motionArgs.flags);
2944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002945 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002946 ASSERT_EQ(0, motionArgs.edgeFlags);
2947 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2951 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2952 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2953 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2955
2956 // Move.
2957 x += 50;
2958 y += 75;
2959 processMove(mapper, x, y);
2960 processSync(mapper);
2961
2962 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2964 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2968 ASSERT_EQ(0, motionArgs.flags);
2969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002970 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002971 ASSERT_EQ(0, motionArgs.edgeFlags);
2972 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2976 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2977 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2978 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2980
2981 // Up.
2982 processUp(mapper);
2983 processSync(mapper);
2984
2985 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2986 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2987 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2988 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2989 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2991 ASSERT_EQ(0, motionArgs.flags);
2992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002993 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002994 ASSERT_EQ(0, motionArgs.edgeFlags);
2995 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002996 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2999 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
3000 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3001 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3003
3004 // Should not have sent any more keys or motions.
3005 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3006 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3007}
3008
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003009TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3010 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003011 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003012 prepareAxes(POSITION);
3013 addConfigurationProperty("touch.orientationAware", "0");
3014 addMapperAndConfigure(mapper);
3015
3016 FakeInputDispatcher::NotifyMotionArgs args;
3017
3018 // Rotation 90.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003019 prepareDisplay(DISPLAY_ORIENTATION_90);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003020 processDown(mapper, toRawX(50), toRawY(75));
3021 processSync(mapper);
3022
3023 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brownebbd5d12011-02-17 13:01:34 -08003024 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3025 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003026
3027 processUp(mapper);
3028 processSync(mapper);
3029 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3030}
3031
3032TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3033 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003034 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownc3db8582010-10-20 15:33:38 -07003035 prepareAxes(POSITION);
3036 addMapperAndConfigure(mapper);
3037
3038 FakeInputDispatcher::NotifyMotionArgs args;
3039
3040 // Rotation 0.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003041 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003042 processDown(mapper, toRawX(50), toRawY(75));
3043 processSync(mapper);
3044
3045 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brownebbd5d12011-02-17 13:01:34 -08003046 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3047 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003048
3049 processUp(mapper);
3050 processSync(mapper);
3051 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3052
3053 // Rotation 90.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003054 prepareDisplay(DISPLAY_ORIENTATION_90);
Jeff Brown9626b142011-03-03 02:09:54 -08003055 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Jeff Brownc3db8582010-10-20 15:33:38 -07003056 processSync(mapper);
3057
3058 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003059 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3060 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003061
3062 processUp(mapper);
3063 processSync(mapper);
3064 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3065
3066 // Rotation 180.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003067 prepareDisplay(DISPLAY_ORIENTATION_180);
Jeff Brown9626b142011-03-03 02:09:54 -08003068 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07003069 processSync(mapper);
3070
3071 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003072 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3073 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003074
3075 processUp(mapper);
3076 processSync(mapper);
3077 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3078
3079 // Rotation 270.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003080 prepareDisplay(DISPLAY_ORIENTATION_270);
Jeff Brown9626b142011-03-03 02:09:54 -08003081 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07003082 processSync(mapper);
3083
3084 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003085 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3086 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003087
3088 processUp(mapper);
3089 processSync(mapper);
3090 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3091}
3092
3093TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003094 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003095 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003096 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003097 prepareAxes(POSITION | PRESSURE | TOOL);
3098 addMapperAndConfigure(mapper);
3099
3100 // These calculations are based on the input device calibration documentation.
3101 int32_t rawX = 100;
3102 int32_t rawY = 200;
3103 int32_t rawPressure = 10;
3104 int32_t rawToolMajor = 12;
3105
3106 float x = toDisplayX(rawX);
3107 float y = toDisplayY(rawY);
3108 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3109 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3110 float tool = min(DISPLAY_WIDTH, DISPLAY_HEIGHT) * size;
3111 float touch = min(tool * pressure, tool);
3112
3113 processDown(mapper, rawX, rawY);
3114 processPressure(mapper, rawPressure);
3115 processToolMajor(mapper, rawToolMajor);
3116 processSync(mapper);
3117
3118 FakeInputDispatcher::NotifyMotionArgs args;
3119 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3121 x, y, pressure, size, touch, touch, tool, tool, 0));
3122}
3123
3124
3125// --- MultiTouchInputMapperTest ---
3126
3127class MultiTouchInputMapperTest : public TouchInputMapperTest {
3128protected:
3129 void prepareAxes(int axes);
3130
3131 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
3132 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
3133 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
3134 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
3135 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
3136 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
3137 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
3138 void processId(MultiTouchInputMapper* mapper, int32_t id);
3139 void processMTSync(MultiTouchInputMapper* mapper);
3140 void processSync(MultiTouchInputMapper* mapper);
3141};
3142
3143void MultiTouchInputMapperTest::prepareAxes(int axes) {
3144 if (axes & POSITION) {
Jeff Brownefd32662011-03-08 15:13:06 -08003145 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
3146 RAW_X_MIN, RAW_X_MAX, 0, 0);
3147 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
3148 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003149 }
3150 if (axes & TOUCH) {
Jeff Brownefd32662011-03-08 15:13:06 -08003151 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
3152 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003153 if (axes & MINOR) {
Jeff Brownefd32662011-03-08 15:13:06 -08003154 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
Jeff Brownc3db8582010-10-20 15:33:38 -07003155 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3156 }
3157 }
3158 if (axes & TOOL) {
Jeff Brownefd32662011-03-08 15:13:06 -08003159 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
3160 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003161 if (axes & MINOR) {
Jeff Brownefd32662011-03-08 15:13:06 -08003162 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
Jeff Brownc3db8582010-10-20 15:33:38 -07003163 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
3164 }
3165 }
3166 if (axes & ORIENTATION) {
Jeff Brownefd32662011-03-08 15:13:06 -08003167 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
Jeff Brownc3db8582010-10-20 15:33:38 -07003168 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
3169 }
3170 if (axes & PRESSURE) {
Jeff Brownefd32662011-03-08 15:13:06 -08003171 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
Jeff Brownc3db8582010-10-20 15:33:38 -07003172 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3173 }
3174 if (axes & ID) {
Jeff Brownefd32662011-03-08 15:13:06 -08003175 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
Jeff Brownc3db8582010-10-20 15:33:38 -07003176 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
3177 }
3178}
3179
3180void MultiTouchInputMapperTest::processPosition(
3181 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
3182 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, 0, x, 0);
3183 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, 0, y, 0);
3184}
3185
3186void MultiTouchInputMapperTest::processTouchMajor(
3187 MultiTouchInputMapper* mapper, int32_t touchMajor) {
3188 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, 0, touchMajor, 0);
3189}
3190
3191void MultiTouchInputMapperTest::processTouchMinor(
3192 MultiTouchInputMapper* mapper, int32_t touchMinor) {
3193 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, 0, touchMinor, 0);
3194}
3195
3196void MultiTouchInputMapperTest::processToolMajor(
3197 MultiTouchInputMapper* mapper, int32_t toolMajor) {
3198 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, 0, toolMajor, 0);
3199}
3200
3201void MultiTouchInputMapperTest::processToolMinor(
3202 MultiTouchInputMapper* mapper, int32_t toolMinor) {
3203 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, 0, toolMinor, 0);
3204}
3205
3206void MultiTouchInputMapperTest::processOrientation(
3207 MultiTouchInputMapper* mapper, int32_t orientation) {
3208 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, 0, orientation, 0);
3209}
3210
3211void MultiTouchInputMapperTest::processPressure(
3212 MultiTouchInputMapper* mapper, int32_t pressure) {
3213 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, 0, pressure, 0);
3214}
3215
3216void MultiTouchInputMapperTest::processId(
3217 MultiTouchInputMapper* mapper, int32_t id) {
3218 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, 0, id, 0);
3219}
3220
3221void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3222 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0, 0, 0);
3223}
3224
3225void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3226 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
3227}
3228
3229
3230TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003231 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003232 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003233 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003234 prepareAxes(POSITION);
3235 prepareVirtualKeys();
3236 addMapperAndConfigure(mapper);
3237
3238 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3239
3240 FakeInputDispatcher::NotifyMotionArgs motionArgs;
3241
3242 // Two fingers down at once.
3243 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3244 processPosition(mapper, x1, y1);
3245 processMTSync(mapper);
3246 processPosition(mapper, x2, y2);
3247 processMTSync(mapper);
3248 processSync(mapper);
3249
3250 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3252 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3253 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3254 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3256 ASSERT_EQ(0, motionArgs.flags);
3257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003258 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003259 ASSERT_EQ(0, motionArgs.edgeFlags);
3260 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003261 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3264 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3265 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3266 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3268
3269 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3274 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3275 motionArgs.action);
3276 ASSERT_EQ(0, motionArgs.flags);
3277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003278 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003279 ASSERT_EQ(0, motionArgs.edgeFlags);
3280 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003281 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3283 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3286 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3288 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3292
3293 // Move.
3294 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3295 processPosition(mapper, x1, y1);
3296 processMTSync(mapper);
3297 processPosition(mapper, x2, y2);
3298 processMTSync(mapper);
3299 processSync(mapper);
3300
3301 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3307 ASSERT_EQ(0, motionArgs.flags);
3308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003309 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003310 ASSERT_EQ(0, motionArgs.edgeFlags);
3311 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3314 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3317 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3319 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3320 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3321 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3323
3324 // First finger up.
3325 x2 += 15; y2 -= 20;
3326 processPosition(mapper, x2, y2);
3327 processMTSync(mapper);
3328 processSync(mapper);
3329
3330 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3332 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3333 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3334 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3335 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3336 motionArgs.action);
3337 ASSERT_EQ(0, motionArgs.flags);
3338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003339 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003340 ASSERT_EQ(0, motionArgs.edgeFlags);
3341 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3344 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3347 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3349 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3350 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3351 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3353
3354 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3356 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3357 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3358 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3360 ASSERT_EQ(0, motionArgs.flags);
3361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003362 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003363 ASSERT_EQ(0, motionArgs.edgeFlags);
3364 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003365 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
3366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3369 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3370 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3372
3373 // Move.
3374 x2 += 20; y2 -= 25;
3375 processPosition(mapper, x2, y2);
3376 processMTSync(mapper);
3377 processSync(mapper);
3378
3379 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3385 ASSERT_EQ(0, motionArgs.flags);
3386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003387 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003388 ASSERT_EQ(0, motionArgs.edgeFlags);
3389 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003390 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
3391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3397
3398 // New finger down.
3399 int32_t x3 = 700, y3 = 300;
3400 processPosition(mapper, x2, y2);
3401 processMTSync(mapper);
3402 processPosition(mapper, x3, y3);
3403 processMTSync(mapper);
3404 processSync(mapper);
3405
3406 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3411 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3412 motionArgs.action);
3413 ASSERT_EQ(0, motionArgs.flags);
3414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003415 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003416 ASSERT_EQ(0, motionArgs.edgeFlags);
3417 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3423 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3426 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3427 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3429
3430 // Second finger up.
3431 x3 += 30; y3 -= 20;
3432 processPosition(mapper, x3, y3);
3433 processMTSync(mapper);
3434 processSync(mapper);
3435
3436 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3437 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3438 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3439 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3440 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3441 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3442 motionArgs.action);
3443 ASSERT_EQ(0, motionArgs.flags);
3444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003445 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003446 ASSERT_EQ(0, motionArgs.edgeFlags);
3447 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3450 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3453 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3455 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3456 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3457 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3459
3460 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3466 ASSERT_EQ(0, motionArgs.flags);
3467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003468 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003469 ASSERT_EQ(0, motionArgs.edgeFlags);
3470 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3474 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3475 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3476 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3478
3479 // Last finger up.
3480 processMTSync(mapper);
3481 processSync(mapper);
3482
3483 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3487 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3489 ASSERT_EQ(0, motionArgs.flags);
3490 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003491 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003492 ASSERT_EQ(0, motionArgs.edgeFlags);
3493 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3498 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3499 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3501
3502 // Should not have sent any more keys or motions.
3503 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3504 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3505}
3506
3507TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003508 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003509 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003510 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003511 prepareAxes(POSITION | ID);
3512 prepareVirtualKeys();
3513 addMapperAndConfigure(mapper);
3514
3515 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3516
3517 FakeInputDispatcher::NotifyMotionArgs motionArgs;
3518
3519 // Two fingers down at once.
3520 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3521 processPosition(mapper, x1, y1);
3522 processId(mapper, 1);
3523 processMTSync(mapper);
3524 processPosition(mapper, x2, y2);
3525 processId(mapper, 2);
3526 processMTSync(mapper);
3527 processSync(mapper);
3528
3529 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3535 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3536
3537 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3538 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3539 motionArgs.action);
3540 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003543 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3546 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3548 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3549
3550 // Move.
3551 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3552 processPosition(mapper, x1, y1);
3553 processId(mapper, 1);
3554 processMTSync(mapper);
3555 processPosition(mapper, x2, y2);
3556 processId(mapper, 2);
3557 processMTSync(mapper);
3558 processSync(mapper);
3559
3560 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3562 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003565 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3568 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3571
3572 // First finger up.
3573 x2 += 15; y2 -= 20;
3574 processPosition(mapper, x2, y2);
3575 processId(mapper, 2);
3576 processMTSync(mapper);
3577 processSync(mapper);
3578
3579 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3580 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3581 motionArgs.action);
3582 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003585 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3588 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3590 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3591
3592 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3594 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003595 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3598 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3599
3600 // Move.
3601 x2 += 20; y2 -= 25;
3602 processPosition(mapper, x2, y2);
3603 processId(mapper, 2);
3604 processMTSync(mapper);
3605 processSync(mapper);
3606
3607 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003610 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3614
3615 // New finger down.
3616 int32_t x3 = 700, y3 = 300;
3617 processPosition(mapper, x2, y2);
3618 processId(mapper, 2);
3619 processMTSync(mapper);
3620 processPosition(mapper, x3, y3);
3621 processId(mapper, 3);
3622 processMTSync(mapper);
3623 processSync(mapper);
3624
3625 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
Jeff Brown6894a292011-07-01 17:59:27 -07003626 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownc3db8582010-10-20 15:33:38 -07003627 motionArgs.action);
3628 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003631 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
Jeff Brownc3db8582010-10-20 15:33:38 -07003634 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
Jeff Brown6894a292011-07-01 17:59:27 -07003635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
Jeff Brownc3db8582010-10-20 15:33:38 -07003637
3638 // Second finger up.
3639 x3 += 30; y3 -= 20;
3640 processPosition(mapper, x3, y3);
3641 processId(mapper, 3);
3642 processMTSync(mapper);
3643 processSync(mapper);
3644
3645 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
Jeff Brown6894a292011-07-01 17:59:27 -07003646 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownc3db8582010-10-20 15:33:38 -07003647 motionArgs.action);
3648 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003651 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
Jeff Brownc3db8582010-10-20 15:33:38 -07003654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
Jeff Brown6894a292011-07-01 17:59:27 -07003655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
Jeff Brownc3db8582010-10-20 15:33:38 -07003657
3658 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3665
3666 // Last finger up.
3667 processMTSync(mapper);
3668 processSync(mapper);
3669
3670 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3677
3678 // Should not have sent any more keys or motions.
3679 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3680 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3681}
3682
3683TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003684 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003685 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003686 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003687 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR);
3688 addMapperAndConfigure(mapper);
3689
3690 // These calculations are based on the input device calibration documentation.
3691 int32_t rawX = 100;
3692 int32_t rawY = 200;
3693 int32_t rawTouchMajor = 7;
3694 int32_t rawTouchMinor = 6;
3695 int32_t rawToolMajor = 9;
3696 int32_t rawToolMinor = 8;
3697 int32_t rawPressure = 11;
3698 int32_t rawOrientation = 3;
3699 int32_t id = 5;
3700
3701 float x = toDisplayX(rawX);
3702 float y = toDisplayY(rawY);
3703 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3704 float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3705 float toolMajor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMajor / RAW_TOOL_MAX;
3706 float toolMinor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMinor / RAW_TOOL_MAX;
3707 float touchMajor = min(toolMajor * pressure, toolMajor);
3708 float touchMinor = min(toolMinor * pressure, toolMinor);
3709 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
3710
3711 processPosition(mapper, rawX, rawY);
3712 processTouchMajor(mapper, rawTouchMajor);
3713 processTouchMinor(mapper, rawTouchMinor);
3714 processToolMajor(mapper, rawToolMajor);
3715 processToolMinor(mapper, rawToolMinor);
3716 processPressure(mapper, rawPressure);
3717 processOrientation(mapper, rawOrientation);
3718 processId(mapper, id);
3719 processMTSync(mapper);
3720 processSync(mapper);
3721
3722 FakeInputDispatcher::NotifyMotionArgs args;
3723 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown6894a292011-07-01 17:59:27 -07003724 ASSERT_EQ(0, args.pointerProperties[0].id);
Jeff Brownc3db8582010-10-20 15:33:38 -07003725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3726 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, orientation));
3727}
3728
3729TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003730 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003731 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003732 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003733 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003734 addConfigurationProperty("touch.touchSize.calibration", "geometric");
3735 addConfigurationProperty("touch.toolSize.calibration", "geometric");
Jeff Brownc3db8582010-10-20 15:33:38 -07003736 addMapperAndConfigure(mapper);
3737
3738 // These calculations are based on the input device calibration documentation.
3739 int32_t rawX = 100;
3740 int32_t rawY = 200;
3741 int32_t rawTouchMajor = 140;
3742 int32_t rawTouchMinor = 120;
3743 int32_t rawToolMajor = 180;
3744 int32_t rawToolMinor = 160;
3745
3746 float x = toDisplayX(rawX);
3747 float y = toDisplayY(rawY);
3748 float pressure = float(rawTouchMajor) / RAW_TOUCH_MAX;
3749 float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
Jeff Brown9626b142011-03-03 02:09:54 -08003750 float scale = avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3751 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
Jeff Brownc3db8582010-10-20 15:33:38 -07003752 float toolMajor = float(rawToolMajor) * scale;
3753 float toolMinor = float(rawToolMinor) * scale;
3754 float touchMajor = min(float(rawTouchMajor) * scale, toolMajor);
3755 float touchMinor = min(float(rawTouchMinor) * scale, toolMinor);
3756
3757 processPosition(mapper, rawX, rawY);
3758 processTouchMajor(mapper, rawTouchMajor);
3759 processTouchMinor(mapper, rawTouchMinor);
3760 processToolMajor(mapper, rawToolMajor);
3761 processToolMinor(mapper, rawToolMinor);
3762 processMTSync(mapper);
3763 processSync(mapper);
3764
3765 FakeInputDispatcher::NotifyMotionArgs args;
3766 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3768 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, 0));
3769}
3770
3771TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_SummedLinearCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003772 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003773 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003774 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003775 prepareAxes(POSITION | TOUCH | TOOL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003776 addConfigurationProperty("touch.touchSize.calibration", "pressure");
3777 addConfigurationProperty("touch.toolSize.calibration", "linear");
3778 addConfigurationProperty("touch.toolSize.linearScale", "10");
3779 addConfigurationProperty("touch.toolSize.linearBias", "160");
3780 addConfigurationProperty("touch.toolSize.isSummed", "1");
3781 addConfigurationProperty("touch.pressure.calibration", "amplitude");
3782 addConfigurationProperty("touch.pressure.source", "touch");
3783 addConfigurationProperty("touch.pressure.scale", "0.01");
Jeff Brownc3db8582010-10-20 15:33:38 -07003784 addMapperAndConfigure(mapper);
3785
3786 // These calculations are based on the input device calibration documentation.
3787 // Note: We only provide a single common touch/tool value because the device is assumed
3788 // not to emit separate values for each pointer (isSummed = 1).
3789 int32_t rawX = 100;
3790 int32_t rawY = 200;
3791 int32_t rawX2 = 150;
3792 int32_t rawY2 = 250;
3793 int32_t rawTouchMajor = 60;
3794 int32_t rawToolMajor = 5;
3795
3796 float x = toDisplayX(rawX);
3797 float y = toDisplayY(rawY);
3798 float x2 = toDisplayX(rawX2);
3799 float y2 = toDisplayY(rawY2);
3800 float pressure = float(rawTouchMajor) * 0.01f;
3801 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3802 float tool = (float(rawToolMajor) * 10.0f + 160.0f) / 2;
3803 float touch = min(tool * pressure, tool);
3804
3805 processPosition(mapper, rawX, rawY);
3806 processTouchMajor(mapper, rawTouchMajor);
3807 processToolMajor(mapper, rawToolMajor);
3808 processMTSync(mapper);
3809 processPosition(mapper, rawX2, rawY2);
3810 processTouchMajor(mapper, rawTouchMajor);
3811 processToolMajor(mapper, rawToolMajor);
3812 processMTSync(mapper);
3813 processSync(mapper);
3814
3815 FakeInputDispatcher::NotifyMotionArgs args;
3816 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3817 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3818 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3819 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3820 args.action);
3821 ASSERT_EQ(size_t(2), args.pointerCount);
3822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3823 x, y, pressure, size, touch, touch, tool, tool, 0));
3824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
3825 x2, y2, pressure, size, touch, touch, tool, tool, 0));
3826}
3827
3828TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_AreaCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003829 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003830 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003831 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003832 prepareAxes(POSITION | TOUCH | TOOL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003833 addConfigurationProperty("touch.touchSize.calibration", "pressure");
3834 addConfigurationProperty("touch.toolSize.calibration", "area");
3835 addConfigurationProperty("touch.toolSize.areaScale", "22");
3836 addConfigurationProperty("touch.toolSize.areaBias", "1");
3837 addConfigurationProperty("touch.toolSize.linearScale", "9.2");
3838 addConfigurationProperty("touch.toolSize.linearBias", "3");
3839 addConfigurationProperty("touch.pressure.calibration", "amplitude");
3840 addConfigurationProperty("touch.pressure.source", "touch");
3841 addConfigurationProperty("touch.pressure.scale", "0.01");
Jeff Brownc3db8582010-10-20 15:33:38 -07003842 addMapperAndConfigure(mapper);
3843
3844 // These calculations are based on the input device calibration documentation.
3845 int32_t rawX = 100;
3846 int32_t rawY = 200;
3847 int32_t rawTouchMajor = 60;
3848 int32_t rawToolMajor = 5;
3849
3850 float x = toDisplayX(rawX);
3851 float y = toDisplayY(rawY);
3852 float pressure = float(rawTouchMajor) * 0.01f;
3853 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3854 float tool = sqrtf(float(rawToolMajor) * 22.0f + 1.0f) * 9.2f + 3.0f;
3855 float touch = min(tool * pressure, tool);
3856
3857 processPosition(mapper, rawX, rawY);
3858 processTouchMajor(mapper, rawTouchMajor);
3859 processToolMajor(mapper, rawToolMajor);
3860 processMTSync(mapper);
3861 processSync(mapper);
3862
3863 FakeInputDispatcher::NotifyMotionArgs args;
3864 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3866 x, y, pressure, size, touch, touch, tool, tool, 0));
3867}
3868
3869} // namespace android