blob: 7a6af253321b78c7a5a1ba57db6da2d8e4a9ebdb [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 Brown49754db2011-07-01 17:37:58 -0700710 virtual bool hasScanCode(int32_t deviceId, int32_t scanCode) const {
711 Device* device = getDevice(deviceId);
712 if (device) {
713 ssize_t index = device->keys.indexOfKey(scanCode);
714 return index >= 0;
715 }
716 return false;
717 }
718
Jeff Brown7631cbb2010-10-24 15:22:06 -0700719 virtual bool hasLed(int32_t deviceId, int32_t led) const {
Jeff Brown51e7fe72010-10-29 22:19:53 -0700720 Device* device = getDevice(deviceId);
721 return device && device->leds.indexOfKey(led) >= 0;
Jeff Brown7631cbb2010-10-24 15:22:06 -0700722 }
723
724 virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
Jeff Brown51e7fe72010-10-29 22:19:53 -0700725 Device* device = getDevice(deviceId);
726 if (device) {
727 ssize_t index = device->leds.indexOfKey(led);
728 if (index >= 0) {
729 device->leds.replaceValueAt(led, on);
730 } else {
731 ADD_FAILURE()
732 << "Attempted to set the state of an LED that the EventHub declared "
733 "was not present. led=" << led;
734 }
735 }
Jeff Brown7631cbb2010-10-24 15:22:06 -0700736 }
737
Jeff Brown90655042010-12-02 13:50:46 -0800738 virtual void getVirtualKeyDefinitions(int32_t deviceId,
739 Vector<VirtualKeyDefinition>& outVirtualKeys) const {
740 outVirtualKeys.clear();
741
742 Device* device = getDevice(deviceId);
743 if (device) {
744 outVirtualKeys.appendVector(device->virtualKeys);
745 }
746 }
747
Jeff Brown56194eb2011-03-02 19:23:13 -0800748 virtual bool isExternal(int32_t deviceId) const {
749 return false;
750 }
751
Jeff Brownc3db8582010-10-20 15:33:38 -0700752 virtual void dump(String8& dump) {
753 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700754
Jeff Brown93fa9b32011-06-14 17:09:25 -0700755 virtual void requestReopenDevices() {
756 }
757
758 virtual void wake() {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700759 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700760};
761
762
763// --- FakeInputReaderContext ---
764
765class FakeInputReaderContext : public InputReaderContext {
766 sp<EventHubInterface> mEventHub;
767 sp<InputReaderPolicyInterface> mPolicy;
768 sp<InputDispatcherInterface> mDispatcher;
769 int32_t mGlobalMetaState;
770 bool mUpdateGlobalMetaStateWasCalled;
771
772public:
773 FakeInputReaderContext(const sp<EventHubInterface>& eventHub,
774 const sp<InputReaderPolicyInterface>& policy,
775 const sp<InputDispatcherInterface>& dispatcher) :
776 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
777 mGlobalMetaState(0) {
778 }
779
780 virtual ~FakeInputReaderContext() { }
781
782 void assertUpdateGlobalMetaStateWasCalled() {
783 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
784 << "Expected updateGlobalMetaState() to have been called.";
785 mUpdateGlobalMetaStateWasCalled = false;
786 }
787
788 void setGlobalMetaState(int32_t state) {
789 mGlobalMetaState = state;
790 }
791
792private:
793 virtual void updateGlobalMetaState() {
794 mUpdateGlobalMetaStateWasCalled = true;
795 }
796
797 virtual int32_t getGlobalMetaState() {
798 return mGlobalMetaState;
799 }
800
801 virtual EventHubInterface* getEventHub() {
802 return mEventHub.get();
803 }
804
805 virtual InputReaderPolicyInterface* getPolicy() {
806 return mPolicy.get();
807 }
808
809 virtual InputDispatcherInterface* getDispatcher() {
810 return mDispatcher.get();
811 }
Jeff Brownfe508922011-01-18 15:10:10 -0800812
813 virtual void disableVirtualKeysUntil(nsecs_t time) {
814 }
815
816 virtual bool shouldDropVirtualKey(nsecs_t now,
817 InputDevice* device, int32_t keyCode, int32_t scanCode) {
818 return false;
819 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800820
821 virtual void fadePointer() {
822 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700823
824 virtual void requestTimeoutAtTime(nsecs_t when) {
825 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700826};
827
828
829// --- FakeInputMapper ---
830
831class FakeInputMapper : public InputMapper {
832 uint32_t mSources;
833 int32_t mKeyboardType;
834 int32_t mMetaState;
835 KeyedVector<int32_t, int32_t> mKeyCodeStates;
836 KeyedVector<int32_t, int32_t> mScanCodeStates;
837 KeyedVector<int32_t, int32_t> mSwitchStates;
838 Vector<int32_t> mSupportedKeyCodes;
839 RawEvent mLastEvent;
840
841 bool mConfigureWasCalled;
842 bool mResetWasCalled;
843 bool mProcessWasCalled;
844
845public:
846 FakeInputMapper(InputDevice* device, uint32_t sources) :
847 InputMapper(device),
848 mSources(sources), mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
849 mMetaState(0),
850 mConfigureWasCalled(false), mResetWasCalled(false), mProcessWasCalled(false) {
851 }
852
853 virtual ~FakeInputMapper() { }
854
855 void setKeyboardType(int32_t keyboardType) {
856 mKeyboardType = keyboardType;
857 }
858
859 void setMetaState(int32_t metaState) {
860 mMetaState = metaState;
861 }
862
863 void assertConfigureWasCalled() {
864 ASSERT_TRUE(mConfigureWasCalled)
865 << "Expected configure() to have been called.";
866 mConfigureWasCalled = false;
867 }
868
869 void assertResetWasCalled() {
870 ASSERT_TRUE(mResetWasCalled)
871 << "Expected reset() to have been called.";
872 mResetWasCalled = false;
873 }
874
875 void assertProcessWasCalled(RawEvent* outLastEvent = NULL) {
876 ASSERT_TRUE(mProcessWasCalled)
877 << "Expected process() to have been called.";
878 if (outLastEvent) {
879 *outLastEvent = mLastEvent;
880 }
881 mProcessWasCalled = false;
882 }
883
884 void setKeyCodeState(int32_t keyCode, int32_t state) {
885 mKeyCodeStates.replaceValueFor(keyCode, state);
886 }
887
888 void setScanCodeState(int32_t scanCode, int32_t state) {
889 mScanCodeStates.replaceValueFor(scanCode, state);
890 }
891
892 void setSwitchState(int32_t switchCode, int32_t state) {
893 mSwitchStates.replaceValueFor(switchCode, state);
894 }
895
896 void addSupportedKeyCode(int32_t keyCode) {
897 mSupportedKeyCodes.add(keyCode);
898 }
899
900private:
901 virtual uint32_t getSources() {
902 return mSources;
903 }
904
905 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) {
906 InputMapper::populateDeviceInfo(deviceInfo);
907
908 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
909 deviceInfo->setKeyboardType(mKeyboardType);
910 }
911 }
912
Jeff Brown474dcb52011-06-14 20:22:50 -0700913 virtual void configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700914 mConfigureWasCalled = true;
915 }
916
917 virtual void reset() {
918 mResetWasCalled = true;
919 }
920
921 virtual void process(const RawEvent* rawEvent) {
922 mLastEvent = *rawEvent;
923 mProcessWasCalled = true;
924 }
925
926 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
927 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
928 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
929 }
930
931 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
932 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
933 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
934 }
935
936 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode) {
937 ssize_t index = mSwitchStates.indexOfKey(switchCode);
938 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
939 }
940
941 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
942 const int32_t* keyCodes, uint8_t* outFlags) {
943 bool result = false;
944 for (size_t i = 0; i < numCodes; i++) {
945 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
946 if (keyCodes[i] == mSupportedKeyCodes[j]) {
947 outFlags[i] = 1;
948 result = true;
949 }
950 }
951 }
952 return result;
953 }
954
955 virtual int32_t getMetaState() {
956 return mMetaState;
957 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800958
959 virtual void fadePointer() {
960 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700961};
962
963
964// --- InstrumentedInputReader ---
965
966class InstrumentedInputReader : public InputReader {
967 InputDevice* mNextDevice;
968
969public:
970 InstrumentedInputReader(const sp<EventHubInterface>& eventHub,
971 const sp<InputReaderPolicyInterface>& policy,
972 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown71c86ad2011-02-07 20:29:08 -0800973 InputReader(eventHub, policy, dispatcher),
974 mNextDevice(NULL) {
Jeff Brownc3db8582010-10-20 15:33:38 -0700975 }
976
977 virtual ~InstrumentedInputReader() {
978 if (mNextDevice) {
979 delete mNextDevice;
980 }
981 }
982
983 void setNextDevice(InputDevice* device) {
984 mNextDevice = device;
985 }
986
987protected:
988 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
989 if (mNextDevice) {
990 InputDevice* device = mNextDevice;
991 mNextDevice = NULL;
992 return device;
993 }
994 return InputReader::createDevice(deviceId, name, classes);
995 }
996
997 friend class InputReaderTest;
998};
999
1000
1001// --- InputReaderTest ---
1002
1003class InputReaderTest : public testing::Test {
1004protected:
1005 sp<FakeInputDispatcher> mFakeDispatcher;
1006 sp<FakeInputReaderPolicy> mFakePolicy;
1007 sp<FakeEventHub> mFakeEventHub;
1008 sp<InstrumentedInputReader> mReader;
1009
1010 virtual void SetUp() {
1011 mFakeEventHub = new FakeEventHub();
1012 mFakePolicy = new FakeInputReaderPolicy();
1013 mFakeDispatcher = new FakeInputDispatcher();
1014
1015 mReader = new InstrumentedInputReader(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1016 }
1017
1018 virtual void TearDown() {
1019 mReader.clear();
1020
1021 mFakeDispatcher.clear();
1022 mFakePolicy.clear();
1023 mFakeEventHub.clear();
1024 }
1025
Jeff Brown83c09682010-12-23 17:50:18 -08001026 void addDevice(int32_t deviceId, const String8& name, uint32_t classes,
1027 const PropertyMap* configuration) {
Jeff Brownc3db8582010-10-20 15:33:38 -07001028 mFakeEventHub->addDevice(deviceId, name, classes);
Jeff Brown83c09682010-12-23 17:50:18 -08001029 if (configuration) {
1030 mFakeEventHub->addConfigurationMap(deviceId, configuration);
1031 }
Jeff Brownc3db8582010-10-20 15:33:38 -07001032 mFakeEventHub->finishDeviceScan();
1033 mReader->loopOnce();
1034 mReader->loopOnce();
1035 mFakeEventHub->assertQueueIsEmpty();
1036 }
1037
1038 FakeInputMapper* addDeviceWithFakeInputMapper(int32_t deviceId,
Jeff Brown83c09682010-12-23 17:50:18 -08001039 const String8& name, uint32_t classes, uint32_t sources,
1040 const PropertyMap* configuration) {
Jeff Brownc3db8582010-10-20 15:33:38 -07001041 InputDevice* device = new InputDevice(mReader.get(), deviceId, name);
1042 FakeInputMapper* mapper = new FakeInputMapper(device, sources);
1043 device->addMapper(mapper);
1044 mReader->setNextDevice(device);
Jeff Brown83c09682010-12-23 17:50:18 -08001045 addDevice(deviceId, name, classes, configuration);
Jeff Brownc3db8582010-10-20 15:33:38 -07001046 return mapper;
1047 }
1048};
1049
1050TEST_F(InputReaderTest, GetInputConfiguration_WhenNoDevices_ReturnsDefaults) {
1051 InputConfiguration config;
1052 mReader->getInputConfiguration(&config);
1053
1054 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1055 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1056 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1057}
1058
1059TEST_F(InputReaderTest, GetInputConfiguration_WhenAlphabeticKeyboardPresent_ReturnsQwertyKeyboard) {
1060 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001061 INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001062
1063 InputConfiguration config;
1064 mReader->getInputConfiguration(&config);
1065
1066 ASSERT_EQ(InputConfiguration::KEYBOARD_QWERTY, config.keyboard);
1067 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1068 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1069}
1070
1071TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchScreenPresent_ReturnsFingerTouchScreen) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001072 PropertyMap configuration;
1073 configuration.addProperty(String8("touch.deviceType"), String8("touchScreen"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001074 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchscreen"),
Jeff Brown58a2da82011-01-25 16:02:22 -08001075 INPUT_DEVICE_CLASS_TOUCH, &configuration));
Jeff Brownc3db8582010-10-20 15:33:38 -07001076
1077 InputConfiguration config;
1078 mReader->getInputConfiguration(&config);
1079
1080 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1081 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1082 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_FINGER, config.touchScreen);
1083}
1084
Jeff Brown58a2da82011-01-25 16:02:22 -08001085TEST_F(InputReaderTest, GetInputConfiguration_WhenTouchPadPresent_ReturnsFingerNoTouch) {
1086 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("touchpad"),
1087 INPUT_DEVICE_CLASS_TOUCH, NULL));
1088
1089 InputConfiguration config;
1090 mReader->getInputConfiguration(&config);
1091
1092 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1093 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1094 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1095}
1096
Jeff Brown83c09682010-12-23 17:50:18 -08001097TEST_F(InputReaderTest, GetInputConfiguration_WhenMousePresent_ReturnsNoNavigation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001098 sp<FakePointerController> controller = new FakePointerController();
1099 mFakePolicy->setPointerController(0, controller);
1100
Jeff Brown83c09682010-12-23 17:50:18 -08001101 PropertyMap configuration;
1102 configuration.addProperty(String8("cursor.mode"), String8("pointer"));
1103 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("mouse"),
1104 INPUT_DEVICE_CLASS_CURSOR, &configuration));
1105
1106 InputConfiguration config;
1107 mReader->getInputConfiguration(&config);
1108
1109 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1110 ASSERT_EQ(InputConfiguration::NAVIGATION_NONAV, config.navigation);
1111 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1112}
1113
Jeff Brownc3db8582010-10-20 15:33:38 -07001114TEST_F(InputReaderTest, GetInputConfiguration_WhenTrackballPresent_ReturnsTrackballNavigation) {
Jeff Brown83c09682010-12-23 17:50:18 -08001115 PropertyMap configuration;
1116 configuration.addProperty(String8("cursor.mode"), String8("navigation"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001117 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("trackball"),
Jeff Brown83c09682010-12-23 17:50:18 -08001118 INPUT_DEVICE_CLASS_CURSOR, &configuration));
Jeff Brownc3db8582010-10-20 15:33:38 -07001119
1120 InputConfiguration config;
1121 mReader->getInputConfiguration(&config);
1122
1123 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1124 ASSERT_EQ(InputConfiguration::NAVIGATION_TRACKBALL, config.navigation);
1125 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1126}
1127
1128TEST_F(InputReaderTest, GetInputConfiguration_WhenDPadPresent_ReturnsDPadNavigation) {
1129 ASSERT_NO_FATAL_FAILURE(addDevice(0, String8("dpad"),
Jeff Brown83c09682010-12-23 17:50:18 -08001130 INPUT_DEVICE_CLASS_DPAD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001131
1132 InputConfiguration config;
1133 mReader->getInputConfiguration(&config);
1134
1135 ASSERT_EQ(InputConfiguration::KEYBOARD_NOKEYS, config.keyboard);
1136 ASSERT_EQ(InputConfiguration::NAVIGATION_DPAD, config.navigation);
1137 ASSERT_EQ(InputConfiguration::TOUCHSCREEN_NOTOUCH, config.touchScreen);
1138}
1139
1140TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsValid) {
1141 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001142 INPUT_DEVICE_CLASS_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001143
1144 InputDeviceInfo info;
1145 status_t result = mReader->getInputDeviceInfo(1, &info);
1146
1147 ASSERT_EQ(OK, result);
1148 ASSERT_EQ(1, info.getId());
1149 ASSERT_STREQ("keyboard", info.getName().string());
1150 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, info.getKeyboardType());
1151 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, info.getSources());
1152 ASSERT_EQ(size_t(0), info.getMotionRanges().size());
1153}
1154
1155TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsInvalid) {
1156 InputDeviceInfo info;
1157 status_t result = mReader->getInputDeviceInfo(-1, &info);
1158
1159 ASSERT_EQ(NAME_NOT_FOUND, result);
1160}
1161
1162TEST_F(InputReaderTest, GetInputDeviceInfo_WhenDeviceIdIsIgnored) {
Jeff Brown83c09682010-12-23 17:50:18 -08001163 addDevice(1, String8("ignored"), 0, NULL); // no classes so device will be ignored
Jeff Brownc3db8582010-10-20 15:33:38 -07001164
1165 InputDeviceInfo info;
1166 status_t result = mReader->getInputDeviceInfo(1, &info);
1167
1168 ASSERT_EQ(NAME_NOT_FOUND, result);
1169}
1170
1171TEST_F(InputReaderTest, GetInputDeviceIds) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001172 sp<FakePointerController> controller = new FakePointerController();
1173 mFakePolicy->setPointerController(2, controller);
1174
Jeff Brownc3db8582010-10-20 15:33:38 -07001175 ASSERT_NO_FATAL_FAILURE(addDevice(1, String8("keyboard"),
Jeff Brown83c09682010-12-23 17:50:18 -08001176 INPUT_DEVICE_CLASS_KEYBOARD | INPUT_DEVICE_CLASS_ALPHAKEY, NULL));
1177 ASSERT_NO_FATAL_FAILURE(addDevice(2, String8("mouse"),
1178 INPUT_DEVICE_CLASS_CURSOR, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001179
1180 Vector<int32_t> ids;
1181 mReader->getInputDeviceIds(ids);
1182
1183 ASSERT_EQ(size_t(2), ids.size());
1184 ASSERT_EQ(1, ids[0]);
1185 ASSERT_EQ(2, ids[1]);
1186}
1187
1188TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
1189 FakeInputMapper* mapper = NULL;
1190 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001191 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001192 mapper->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1193
1194 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1195 AINPUT_SOURCE_ANY, AKEYCODE_A))
1196 << "Should return unknown when the device id is >= 0 but unknown.";
1197
1198 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(1,
1199 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1200 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1201
1202 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(1,
1203 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1204 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1205
1206 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1207 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1208 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1209
1210 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1211 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1212 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1213}
1214
1215TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
1216 FakeInputMapper* mapper = NULL;
1217 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001218 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001219 mapper->setScanCodeState(KEY_A, AKEY_STATE_DOWN);
1220
1221 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1222 AINPUT_SOURCE_ANY, KEY_A))
1223 << "Should return unknown when the device id is >= 0 but unknown.";
1224
1225 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(1,
1226 AINPUT_SOURCE_TRACKBALL, KEY_A))
1227 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1228
1229 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(1,
1230 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1231 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1232
1233 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1234 AINPUT_SOURCE_TRACKBALL, KEY_A))
1235 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1236
1237 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1238 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1239 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1240}
1241
1242TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
1243 FakeInputMapper* mapper = NULL;
1244 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001245 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001246 mapper->setSwitchState(SW_LID, AKEY_STATE_DOWN);
1247
1248 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1249 AINPUT_SOURCE_ANY, SW_LID))
1250 << "Should return unknown when the device id is >= 0 but unknown.";
1251
1252 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(1,
1253 AINPUT_SOURCE_TRACKBALL, SW_LID))
1254 << "Should return unknown when the device id is valid but the sources are not supported by the device.";
1255
1256 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(1,
1257 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1258 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1259
1260 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1261 AINPUT_SOURCE_TRACKBALL, SW_LID))
1262 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1263
1264 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1265 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1266 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1267}
1268
1269TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
1270 FakeInputMapper* mapper = NULL;
1271 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001272 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001273 mapper->addSupportedKeyCode(AKEYCODE_A);
1274 mapper->addSupportedKeyCode(AKEYCODE_B);
1275
1276 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1277 uint8_t flags[4] = { 0, 0, 0, 1 };
1278
1279 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1280 << "Should return false when device id is >= 0 but unknown.";
1281 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1282
1283 flags[3] = 1;
1284 ASSERT_FALSE(mReader->hasKeys(1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1285 << "Should return false when device id is valid but the sources are not supported by the device.";
1286 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1287
1288 flags[3] = 1;
1289 ASSERT_TRUE(mReader->hasKeys(1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1290 << "Should return value provided by mapper when device id is valid and the device supports some of the sources.";
1291 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1292
1293 flags[3] = 1;
1294 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1295 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1296 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1297
1298 flags[3] = 1;
1299 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1300 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1301 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1302}
1303
1304TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Jeff Brown83c09682010-12-23 17:50:18 -08001305 addDevice(1, String8("ignored"), INPUT_DEVICE_CLASS_KEYBOARD, NULL);
Jeff Brownc3db8582010-10-20 15:33:38 -07001306
1307 FakeInputDispatcher::NotifyConfigurationChangedArgs args;
1308 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyConfigurationChangedWasCalled(&args));
1309 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1310}
1311
1312TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
1313 FakeInputMapper* mapper = NULL;
1314 ASSERT_NO_FATAL_FAILURE(mapper = addDeviceWithFakeInputMapper(1, String8("fake"),
Jeff Brown83c09682010-12-23 17:50:18 -08001315 INPUT_DEVICE_CLASS_KEYBOARD, AINPUT_SOURCE_KEYBOARD, NULL));
Jeff Brownc3db8582010-10-20 15:33:38 -07001316
1317 mFakeEventHub->enqueueEvent(0, 1, EV_KEY, KEY_A, AKEYCODE_A, 1, POLICY_FLAG_WAKE);
1318 mReader->loopOnce();
1319 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1320
1321 RawEvent event;
1322 ASSERT_NO_FATAL_FAILURE(mapper->assertProcessWasCalled(&event));
1323 ASSERT_EQ(0, event.when);
1324 ASSERT_EQ(1, event.deviceId);
1325 ASSERT_EQ(EV_KEY, event.type);
1326 ASSERT_EQ(KEY_A, event.scanCode);
1327 ASSERT_EQ(AKEYCODE_A, event.keyCode);
1328 ASSERT_EQ(1, event.value);
1329 ASSERT_EQ(POLICY_FLAG_WAKE, event.flags);
1330}
1331
1332
1333// --- InputDeviceTest ---
1334
1335class InputDeviceTest : public testing::Test {
1336protected:
1337 static const char* DEVICE_NAME;
1338 static const int32_t DEVICE_ID;
1339
1340 sp<FakeEventHub> mFakeEventHub;
1341 sp<FakeInputReaderPolicy> mFakePolicy;
1342 sp<FakeInputDispatcher> mFakeDispatcher;
1343 FakeInputReaderContext* mFakeContext;
1344
1345 InputDevice* mDevice;
1346
1347 virtual void SetUp() {
1348 mFakeEventHub = new FakeEventHub();
1349 mFakePolicy = new FakeInputReaderPolicy();
1350 mFakeDispatcher = new FakeInputDispatcher();
1351 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1352
Jeff Brown49ed71d2010-12-06 17:13:33 -08001353 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001354 mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1355 }
1356
1357 virtual void TearDown() {
1358 delete mDevice;
1359
1360 delete mFakeContext;
1361 mFakeDispatcher.clear();
1362 mFakePolicy.clear();
1363 mFakeEventHub.clear();
1364 }
1365};
1366
1367const char* InputDeviceTest::DEVICE_NAME = "device";
1368const int32_t InputDeviceTest::DEVICE_ID = 1;
1369
1370TEST_F(InputDeviceTest, ImmutableProperties) {
1371 ASSERT_EQ(DEVICE_ID, mDevice->getId());
1372 ASSERT_STREQ(DEVICE_NAME, mDevice->getName());
1373}
1374
1375TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
1376 // Configuration.
Jeff Brown474dcb52011-06-14 20:22:50 -07001377 InputReaderConfiguration config;
1378 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001379
1380 // Metadata.
1381 ASSERT_TRUE(mDevice->isIgnored());
1382 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
1383
1384 InputDeviceInfo info;
1385 mDevice->getDeviceInfo(&info);
1386 ASSERT_EQ(DEVICE_ID, info.getId());
1387 ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1388 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
1389 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
1390
1391 // State queries.
1392 ASSERT_EQ(0, mDevice->getMetaState());
1393
1394 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1395 << "Ignored device should return unknown key code state.";
1396 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
1397 << "Ignored device should return unknown scan code state.";
1398 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
1399 << "Ignored device should return unknown switch state.";
1400
1401 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1402 uint8_t flags[2] = { 0, 1 };
1403 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
1404 << "Ignored device should never mark any key codes.";
1405 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
1406 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
1407
1408 // Reset.
1409 mDevice->reset();
1410}
1411
1412TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
1413 // Configuration.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001414 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8("key"), String8("value"));
Jeff Brownc3db8582010-10-20 15:33:38 -07001415
1416 FakeInputMapper* mapper1 = new FakeInputMapper(mDevice, AINPUT_SOURCE_KEYBOARD);
1417 mapper1->setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1418 mapper1->setMetaState(AMETA_ALT_ON);
1419 mapper1->addSupportedKeyCode(AKEYCODE_A);
1420 mapper1->addSupportedKeyCode(AKEYCODE_B);
1421 mapper1->setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1422 mapper1->setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
1423 mapper1->setScanCodeState(2, AKEY_STATE_DOWN);
1424 mapper1->setScanCodeState(3, AKEY_STATE_UP);
1425 mapper1->setSwitchState(4, AKEY_STATE_DOWN);
1426 mDevice->addMapper(mapper1);
1427
1428 FakeInputMapper* mapper2 = new FakeInputMapper(mDevice, AINPUT_SOURCE_TOUCHSCREEN);
1429 mapper2->setMetaState(AMETA_SHIFT_ON);
1430 mDevice->addMapper(mapper2);
1431
Jeff Brown474dcb52011-06-14 20:22:50 -07001432 InputReaderConfiguration config;
1433 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001434
1435 String8 propertyValue;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001436 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
1437 << "Device should have read configuration during configuration phase.";
Jeff Brownc3db8582010-10-20 15:33:38 -07001438 ASSERT_STREQ("value", propertyValue.string());
1439
1440 ASSERT_NO_FATAL_FAILURE(mapper1->assertConfigureWasCalled());
1441 ASSERT_NO_FATAL_FAILURE(mapper2->assertConfigureWasCalled());
1442
1443 // Metadata.
1444 ASSERT_FALSE(mDevice->isIgnored());
1445 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
1446
1447 InputDeviceInfo info;
1448 mDevice->getDeviceInfo(&info);
1449 ASSERT_EQ(DEVICE_ID, info.getId());
1450 ASSERT_STREQ(DEVICE_NAME, info.getName().string());
1451 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
1452 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
1453
1454 // State queries.
1455 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
1456 << "Should query mappers and combine meta states.";
1457
1458 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1459 << "Should return unknown key code state when source not supported.";
1460 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1461 << "Should return unknown scan code state when source not supported.";
1462 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1463 << "Should return unknown switch state when source not supported.";
1464
1465 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
1466 << "Should query mapper when source is supported.";
1467 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
1468 << "Should query mapper when source is supported.";
1469 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
1470 << "Should query mapper when source is supported.";
1471
1472 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1473 uint8_t flags[4] = { 0, 0, 0, 1 };
1474 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1475 << "Should do nothing when source is unsupported.";
1476 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
1477 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
1478 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
1479 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
1480
1481 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
1482 << "Should query mapper when source is supported.";
1483 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
1484 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
1485 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
1486 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
1487
1488 // Event handling.
1489 RawEvent event;
Jeff Brownb7198742011-03-18 18:14:26 -07001490 mDevice->process(&event, 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07001491
1492 ASSERT_NO_FATAL_FAILURE(mapper1->assertProcessWasCalled());
1493 ASSERT_NO_FATAL_FAILURE(mapper2->assertProcessWasCalled());
1494
1495 // Reset.
1496 mDevice->reset();
1497
1498 ASSERT_NO_FATAL_FAILURE(mapper1->assertResetWasCalled());
1499 ASSERT_NO_FATAL_FAILURE(mapper2->assertResetWasCalled());
1500}
1501
1502
1503// --- InputMapperTest ---
1504
1505class InputMapperTest : public testing::Test {
1506protected:
1507 static const char* DEVICE_NAME;
1508 static const int32_t DEVICE_ID;
1509
1510 sp<FakeEventHub> mFakeEventHub;
1511 sp<FakeInputReaderPolicy> mFakePolicy;
1512 sp<FakeInputDispatcher> mFakeDispatcher;
1513 FakeInputReaderContext* mFakeContext;
1514 InputDevice* mDevice;
1515
1516 virtual void SetUp() {
1517 mFakeEventHub = new FakeEventHub();
1518 mFakePolicy = new FakeInputReaderPolicy();
1519 mFakeDispatcher = new FakeInputDispatcher();
1520 mFakeContext = new FakeInputReaderContext(mFakeEventHub, mFakePolicy, mFakeDispatcher);
1521 mDevice = new InputDevice(mFakeContext, DEVICE_ID, String8(DEVICE_NAME));
1522
1523 mFakeEventHub->addDevice(DEVICE_ID, String8(DEVICE_NAME), 0);
1524 }
1525
1526 virtual void TearDown() {
1527 delete mDevice;
1528 delete mFakeContext;
1529 mFakeDispatcher.clear();
1530 mFakePolicy.clear();
1531 mFakeEventHub.clear();
1532 }
1533
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001534 void addConfigurationProperty(const char* key, const char* value) {
1535 mFakeEventHub->addConfigurationProperty(DEVICE_ID, String8(key), String8(value));
Jeff Brownc3db8582010-10-20 15:33:38 -07001536 }
1537
1538 void addMapperAndConfigure(InputMapper* mapper) {
Jeff Brown474dcb52011-06-14 20:22:50 -07001539 InputReaderConfiguration config;
1540
Jeff Brownc3db8582010-10-20 15:33:38 -07001541 mDevice->addMapper(mapper);
Jeff Brown474dcb52011-06-14 20:22:50 -07001542 mDevice->configure(&config, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001543 }
1544
1545 static void process(InputMapper* mapper, nsecs_t when, int32_t deviceId, int32_t type,
1546 int32_t scanCode, int32_t keyCode, int32_t value, uint32_t flags) {
1547 RawEvent event;
1548 event.when = when;
1549 event.deviceId = deviceId;
1550 event.type = type;
1551 event.scanCode = scanCode;
1552 event.keyCode = keyCode;
1553 event.value = value;
1554 event.flags = flags;
1555 mapper->process(&event);
1556 }
1557
1558 static void assertMotionRange(const InputDeviceInfo& info,
Jeff Brownefd32662011-03-08 15:13:06 -08001559 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
1560 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
1561 ASSERT_TRUE(range != NULL) << "Axis: " << axis << " Source: " << source;
1562 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
1563 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
1564 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
1565 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
1566 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
1567 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
Jeff Brownc3db8582010-10-20 15:33:38 -07001568 }
1569
1570 static void assertPointerCoords(const PointerCoords& coords,
1571 float x, float y, float pressure, float size,
1572 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
1573 float orientation) {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001574 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), 1);
1575 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
1576 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
1577 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
1578 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), 1);
1579 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), 1);
1580 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), 1);
1581 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), 1);
1582 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
Jeff Brownc3db8582010-10-20 15:33:38 -07001583 }
1584};
1585
1586const char* InputMapperTest::DEVICE_NAME = "device";
1587const int32_t InputMapperTest::DEVICE_ID = 1;
1588
1589
1590// --- SwitchInputMapperTest ---
1591
1592class SwitchInputMapperTest : public InputMapperTest {
1593protected:
1594};
1595
1596TEST_F(SwitchInputMapperTest, GetSources) {
1597 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1598 addMapperAndConfigure(mapper);
1599
Jeff Brown89de57a2011-01-19 18:41:38 -08001600 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper->getSources());
Jeff Brownc3db8582010-10-20 15:33:38 -07001601}
1602
1603TEST_F(SwitchInputMapperTest, GetSwitchState) {
1604 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1605 addMapperAndConfigure(mapper);
1606
1607 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 1);
1608 ASSERT_EQ(1, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1609
1610 mFakeEventHub->setSwitchState(DEVICE_ID, SW_LID, 0);
1611 ASSERT_EQ(0, mapper->getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
1612}
1613
1614TEST_F(SwitchInputMapperTest, Process) {
1615 SwitchInputMapper* mapper = new SwitchInputMapper(mDevice);
1616 addMapperAndConfigure(mapper);
1617
1618 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SW, SW_LID, 0, 1, 0);
1619
1620 FakeInputDispatcher::NotifySwitchArgs args;
1621 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifySwitchWasCalled(&args));
1622 ASSERT_EQ(ARBITRARY_TIME, args.when);
1623 ASSERT_EQ(SW_LID, args.switchCode);
1624 ASSERT_EQ(1, args.switchValue);
1625 ASSERT_EQ(uint32_t(0), args.policyFlags);
1626}
1627
1628
1629// --- KeyboardInputMapperTest ---
1630
1631class KeyboardInputMapperTest : public InputMapperTest {
1632protected:
1633 void testDPadKeyRotation(KeyboardInputMapper* mapper,
1634 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode);
1635};
1636
1637void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper* mapper,
1638 int32_t originalScanCode, int32_t originalKeyCode, int32_t rotatedKeyCode) {
1639 FakeInputDispatcher::NotifyKeyArgs args;
1640
1641 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 1, 0);
1642 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1643 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1644 ASSERT_EQ(originalScanCode, args.scanCode);
1645 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1646
1647 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, originalScanCode, originalKeyCode, 0, 0);
1648 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1649 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1650 ASSERT_EQ(originalScanCode, args.scanCode);
1651 ASSERT_EQ(rotatedKeyCode, args.keyCode);
1652}
1653
1654
1655TEST_F(KeyboardInputMapperTest, GetSources) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001656 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001657 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1658 addMapperAndConfigure(mapper);
1659
1660 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper->getSources());
1661}
1662
1663TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001664 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001665 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1666 addMapperAndConfigure(mapper);
1667
1668 // Key down.
1669 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1670 EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1671 FakeInputDispatcher::NotifyKeyArgs args;
1672 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1673 ASSERT_EQ(DEVICE_ID, args.deviceId);
1674 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1675 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1676 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1677 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1678 ASSERT_EQ(KEY_HOME, args.scanCode);
1679 ASSERT_EQ(AMETA_NONE, args.metaState);
1680 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1681 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1682 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1683
1684 // Key up.
1685 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1686 EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1687 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1688 ASSERT_EQ(DEVICE_ID, args.deviceId);
1689 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1690 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
1691 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1692 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
1693 ASSERT_EQ(KEY_HOME, args.scanCode);
1694 ASSERT_EQ(AMETA_NONE, args.metaState);
1695 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1696 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
1697 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
1698}
1699
1700TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreNotDown_DoesNotSynthesizeKeyUp) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001701 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001702 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1703 addMapperAndConfigure(mapper);
1704
1705 // Key down.
1706 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1707 EV_KEY, KEY_HOME, AKEYCODE_HOME, 1, POLICY_FLAG_WAKE);
1708 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1709
1710 // Key up.
1711 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1712 EV_KEY, KEY_HOME, AKEYCODE_HOME, 0, POLICY_FLAG_WAKE);
1713 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1714
1715 // Reset. Since no keys still down, should not synthesize any key ups.
1716 mapper->reset();
1717 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1718}
1719
1720TEST_F(KeyboardInputMapperTest, Reset_WhenKeysAreDown_SynthesizesKeyUps) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001721 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001722 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1723 addMapperAndConfigure(mapper);
1724
1725 // Metakey down.
1726 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1727 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1728 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1729
1730 // Key down.
1731 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1732 EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1733 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
1734
1735 // Reset. Since two keys are still down, should synthesize two key ups in reverse order.
1736 mapper->reset();
1737
1738 FakeInputDispatcher::NotifyKeyArgs args;
1739 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1740 ASSERT_EQ(DEVICE_ID, args.deviceId);
1741 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1742 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1743 ASSERT_EQ(AKEYCODE_A, args.keyCode);
1744 ASSERT_EQ(KEY_A, args.scanCode);
1745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1746 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1747 ASSERT_EQ(uint32_t(0), args.policyFlags);
1748 ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1749
1750 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1751 ASSERT_EQ(DEVICE_ID, args.deviceId);
1752 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
1753 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1754 ASSERT_EQ(AKEYCODE_SHIFT_LEFT, args.keyCode);
1755 ASSERT_EQ(KEY_LEFTSHIFT, args.scanCode);
1756 ASSERT_EQ(AMETA_NONE, args.metaState);
1757 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
1758 ASSERT_EQ(uint32_t(0), args.policyFlags);
1759 ASSERT_EQ(ARBITRARY_TIME + 1, args.downTime);
1760
1761 // And that's it.
1762 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
1763}
1764
1765TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001766 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001767 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1768 addMapperAndConfigure(mapper);
1769
1770 // Initial metastate.
1771 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1772
1773 // Metakey down.
1774 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1775 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 1, 0);
1776 FakeInputDispatcher::NotifyKeyArgs args;
1777 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1779 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1780 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1781
1782 // Key down.
1783 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID,
1784 EV_KEY, KEY_A, AKEYCODE_A, 1, 0);
1785 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1786 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1788
1789 // Key up.
1790 process(mapper, ARBITRARY_TIME + 2, DEVICE_ID,
1791 EV_KEY, KEY_A, AKEYCODE_A, 0, 0);
1792 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1793 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
1794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper->getMetaState());
1795
1796 // Metakey up.
1797 process(mapper, ARBITRARY_TIME + 3, DEVICE_ID,
1798 EV_KEY, KEY_LEFTSHIFT, AKEYCODE_SHIFT_LEFT, 0, 0);
1799 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1800 ASSERT_EQ(AMETA_NONE, args.metaState);
1801 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
1802 ASSERT_NO_FATAL_FAILURE(mFakeContext->assertUpdateGlobalMetaStateWasCalled());
1803}
1804
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001805TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
1806 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001807 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1808 addMapperAndConfigure(mapper);
1809
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001810 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1811 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001812 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07001813 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1814 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1815 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1816 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1817 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1818 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1819 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1820 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1821}
1822
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001823TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
1824 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001825 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001826 addConfigurationProperty("keyboard.orientationAware", "1");
Jeff Brownc3db8582010-10-20 15:33:38 -07001827 addMapperAndConfigure(mapper);
1828
1829 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1830 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001831 DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07001832 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1833 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
1834 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1835 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
1836 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1837 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
1838 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1839 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
1840
1841 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1842 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001843 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07001844 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1845 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT));
1846 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1847 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP));
1848 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1849 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT));
1850 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1851 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN));
1852
1853 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1854 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001855 DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07001856 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1857 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN));
1858 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1859 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_LEFT));
1860 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1861 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_UP));
1862 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1863 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_RIGHT));
1864
1865 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1866 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001867 DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07001868 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1869 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT));
1870 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1871 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_DOWN));
1872 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1873 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_LEFT));
1874 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
1875 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_UP));
1876
1877 // Special case: if orientation changes while key is down, we still emit the same keycode
1878 // in the key up as we did in the key down.
1879 FakeInputDispatcher::NotifyKeyArgs args;
1880
1881 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1882 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001883 DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07001884 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 1, 0);
1885 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1886 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
1887 ASSERT_EQ(KEY_UP, args.scanCode);
1888 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1889
1890 mFakePolicy->setDisplayInfo(DISPLAY_ID,
1891 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001892 DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07001893 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, KEY_UP, AKEYCODE_DPAD_UP, 0, 0);
1894 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
1895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
1896 ASSERT_EQ(KEY_UP, args.scanCode);
1897 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
1898}
1899
1900TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001901 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001902 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1903 addMapperAndConfigure(mapper);
1904
1905 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 1);
1906 ASSERT_EQ(1, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1907
1908 mFakeEventHub->setKeyCodeState(DEVICE_ID, AKEYCODE_A, 0);
1909 ASSERT_EQ(0, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
1910}
1911
1912TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001913 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001914 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1915 addMapperAndConfigure(mapper);
1916
1917 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 1);
1918 ASSERT_EQ(1, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1919
1920 mFakeEventHub->setScanCodeState(DEVICE_ID, KEY_A, 0);
1921 ASSERT_EQ(0, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
1922}
1923
1924TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001925 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brownc3db8582010-10-20 15:33:38 -07001926 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1927 addMapperAndConfigure(mapper);
1928
1929 mFakeEventHub->addKey(DEVICE_ID, KEY_A, AKEYCODE_A, 0);
1930
1931 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
1932 uint8_t flags[2] = { 0, 0 };
1933 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
1934 ASSERT_TRUE(flags[0]);
1935 ASSERT_FALSE(flags[1]);
1936}
1937
Jeff Brown51e7fe72010-10-29 22:19:53 -07001938TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
1939 mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
1940 mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
1941 mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
1942
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001943 KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001944 AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
1945 addMapperAndConfigure(mapper);
1946
1947 // Initialization should have turned all of the lights off.
1948 ASSERT_FALSE(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
1952 // Toggle caps lock on.
1953 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1954 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1955 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1956 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
1957 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1958 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1959 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1960 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
1961
1962 // Toggle num lock on.
1963 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1964 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1965 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1966 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1967 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1968 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1969 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1970 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
1971
1972 // Toggle caps lock off.
1973 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1974 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
1975 process(mapper, ARBITRARY_TIME, DEVICE_ID,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001976 EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
Jeff Brown51e7fe72010-10-29 22:19:53 -07001977 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1978 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1979 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1980 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
1981
1982 // Toggle scroll lock on.
1983 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1984 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
1985 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1986 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
1987 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1988 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1989 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
1990 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
1991
1992 // Toggle num lock off.
1993 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1994 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
1995 process(mapper, ARBITRARY_TIME, DEVICE_ID,
1996 EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
1997 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
1998 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
1999 ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2000 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
2001
2002 // Toggle scroll lock off.
2003 process(mapper, ARBITRARY_TIME, DEVICE_ID,
2004 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
2005 process(mapper, ARBITRARY_TIME, DEVICE_ID,
2006 EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
2007 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
2008 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
2009 ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
2010 ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
2011}
2012
Jeff Brownc3db8582010-10-20 15:33:38 -07002013
Jeff Brown83c09682010-12-23 17:50:18 -08002014// --- CursorInputMapperTest ---
Jeff Brownc3db8582010-10-20 15:33:38 -07002015
Jeff Brown83c09682010-12-23 17:50:18 -08002016class CursorInputMapperTest : public InputMapperTest {
Jeff Brownc3db8582010-10-20 15:33:38 -07002017protected:
2018 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
2019
Jeff Brown83c09682010-12-23 17:50:18 -08002020 sp<FakePointerController> mFakePointerController;
2021
2022 virtual void SetUp() {
2023 InputMapperTest::SetUp();
2024
2025 mFakePointerController = new FakePointerController();
2026 mFakePolicy->setPointerController(DEVICE_ID, mFakePointerController);
2027 }
2028
2029 void testMotionRotation(CursorInputMapper* mapper,
Jeff Brownc3db8582010-10-20 15:33:38 -07002030 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY);
2031};
2032
Jeff Brown83c09682010-12-23 17:50:18 -08002033const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
Jeff Brownc3db8582010-10-20 15:33:38 -07002034
Jeff Brown83c09682010-12-23 17:50:18 -08002035void CursorInputMapperTest::testMotionRotation(CursorInputMapper* mapper,
Jeff Brownc3db8582010-10-20 15:33:38 -07002036 int32_t originalX, int32_t originalY, int32_t rotatedX, int32_t rotatedY) {
2037 FakeInputDispatcher::NotifyMotionArgs args;
2038
2039 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, originalX, 0);
2040 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, originalY, 0);
2041 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2042 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2045 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
2046 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD,
2047 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2048}
2049
Jeff Brown83c09682010-12-23 17:50:18 -08002050TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
2051 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2052 addConfigurationProperty("cursor.mode", "pointer");
2053 addMapperAndConfigure(mapper);
2054
2055 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper->getSources());
2056}
2057
2058TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
2059 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2060 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002061 addMapperAndConfigure(mapper);
2062
2063 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper->getSources());
2064}
2065
Jeff Brown83c09682010-12-23 17:50:18 -08002066TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
2067 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2068 addConfigurationProperty("cursor.mode", "pointer");
2069 addMapperAndConfigure(mapper);
2070
2071 InputDeviceInfo info;
2072 mapper->populateDeviceInfo(&info);
2073
2074 // Initially there may not be a valid motion range.
Jeff Brownefd32662011-03-08 15:13:06 -08002075 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
2076 ASSERT_EQ(NULL, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
2077 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2078 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brown83c09682010-12-23 17:50:18 -08002079
2080 // When the bounds are set, then there should be a valid motion range.
Jeff Brown9626b142011-03-03 02:09:54 -08002081 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
Jeff Brown83c09682010-12-23 17:50:18 -08002082
2083 InputDeviceInfo info2;
2084 mapper->populateDeviceInfo(&info2);
2085
Jeff Brownefd32662011-03-08 15:13:06 -08002086 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2087 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
Jeff Brown9626b142011-03-03 02:09:54 -08002088 1, 800 - 1, 0.0f, 0.0f));
Jeff Brownefd32662011-03-08 15:13:06 -08002089 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2090 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
Jeff Brown9626b142011-03-03 02:09:54 -08002091 2, 480 - 1, 0.0f, 0.0f));
Jeff Brownefd32662011-03-08 15:13:06 -08002092 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
2093 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002094 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brown83c09682010-12-23 17:50:18 -08002095}
2096
2097TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
2098 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2099 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002100 addMapperAndConfigure(mapper);
2101
2102 InputDeviceInfo info;
2103 mapper->populateDeviceInfo(&info);
2104
Jeff Brownefd32662011-03-08 15:13:06 -08002105 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2106 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
Jeff Brownc3db8582010-10-20 15:33:38 -07002107 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
Jeff Brownefd32662011-03-08 15:13:06 -08002108 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2109 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
Jeff Brownc3db8582010-10-20 15:33:38 -07002110 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
Jeff Brownefd32662011-03-08 15:13:06 -08002111 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
2112 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002113 0.0f, 1.0f, 0.0f, 0.0f));
Jeff Brownc3db8582010-10-20 15:33:38 -07002114}
2115
Jeff Brown83c09682010-12-23 17:50:18 -08002116TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
2117 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2118 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002119 addMapperAndConfigure(mapper);
2120
2121 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2122
2123 FakeInputDispatcher::NotifyMotionArgs args;
2124
2125 // Button press.
2126 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
2127 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002128 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002129 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2130 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2131 ASSERT_EQ(DEVICE_ID, args.deviceId);
2132 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2133 ASSERT_EQ(uint32_t(0), args.policyFlags);
2134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2135 ASSERT_EQ(0, args.flags);
2136 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002137 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002138 ASSERT_EQ(0, args.edgeFlags);
2139 ASSERT_EQ(uint32_t(1), args.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002140 ASSERT_EQ(0, args.pointerProperties[0].id);
2141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2143 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2144 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2145 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2147
2148 // Button release. Should have same down time.
2149 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002150 process(mapper, ARBITRARY_TIME + 1, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002151 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2152 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2153 ASSERT_EQ(DEVICE_ID, args.deviceId);
2154 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
2155 ASSERT_EQ(uint32_t(0), args.policyFlags);
2156 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2157 ASSERT_EQ(0, args.flags);
2158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002159 ASSERT_EQ(0, args.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002160 ASSERT_EQ(0, args.edgeFlags);
2161 ASSERT_EQ(uint32_t(1), args.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002162 ASSERT_EQ(0, args.pointerProperties[0].id);
2163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2165 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2166 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
2167 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
2168 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2169}
2170
Jeff Brown83c09682010-12-23 17:50:18 -08002171TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
2172 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2173 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002174 addMapperAndConfigure(mapper);
2175
2176 FakeInputDispatcher::NotifyMotionArgs args;
2177
2178 // Motion in X but not Y.
2179 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2180 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2181 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2184 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2185
2186 // Motion in Y but not X.
2187 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2188 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2189 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Jeff Brownc3db8582010-10-20 15:33:38 -07002191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2192 0.0f, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2193}
2194
Jeff Brown83c09682010-12-23 17:50:18 -08002195TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
2196 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2197 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002198 addMapperAndConfigure(mapper);
2199
2200 FakeInputDispatcher::NotifyMotionArgs args;
2201
2202 // Button press without following sync.
2203 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002204 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002205 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2208 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2209
2210 // Button release without following sync.
2211 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002212 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002213 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2214 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2216 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2217}
2218
Jeff Brown83c09682010-12-23 17:50:18 -08002219TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
2220 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2221 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002222 addMapperAndConfigure(mapper);
2223
2224 FakeInputDispatcher::NotifyMotionArgs args;
2225
2226 // Combined X, Y and Button.
2227 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 1, 0);
2228 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, -2, 0);
2229 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
2230 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2231 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2234 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, -2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2235 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2236
2237 // Move X, Y a bit while pressed.
2238 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_X, 0, 2, 0);
2239 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_REL, REL_Y, 0, 1, 0);
2240 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2241 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2244 2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
2245 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2246
2247 // Release Button.
2248 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002249 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002250 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2253 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2254}
2255
Jeff Brown83c09682010-12-23 17:50:18 -08002256TEST_F(CursorInputMapperTest, Reset_WhenButtonIsNotDown_ShouldNotSynthesizeButtonUp) {
2257 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2258 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002259 addMapperAndConfigure(mapper);
2260
2261 FakeInputDispatcher::NotifyMotionArgs args;
2262
2263 // Button press.
2264 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002265 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002266 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2267
2268 // Button release.
2269 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 0, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002270 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002271 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2272
2273 // Reset. Should not synthesize button up since button is not pressed.
2274 mapper->reset();
2275
2276 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2277}
2278
Jeff Brown83c09682010-12-23 17:50:18 -08002279TEST_F(CursorInputMapperTest, Reset_WhenButtonIsDown_ShouldSynthesizeButtonUp) {
2280 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2281 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002282 addMapperAndConfigure(mapper);
2283
2284 FakeInputDispatcher::NotifyMotionArgs args;
2285
2286 // Button press.
2287 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_MOUSE, 0, 1, 0);
Jeff Brown49754db2011-07-01 17:37:58 -07002288 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002289 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2290
2291 // Reset. Should synthesize button up.
2292 mapper->reset();
2293
2294 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
2295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
2297 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
2298}
2299
Jeff Brown83c09682010-12-23 17:50:18 -08002300TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateMotions) {
2301 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2302 addConfigurationProperty("cursor.mode", "navigation");
Jeff Brownc3db8582010-10-20 15:33:38 -07002303 addMapperAndConfigure(mapper);
2304
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002305 mFakePolicy->setDisplayInfo(DISPLAY_ID,
2306 DISPLAY_WIDTH, DISPLAY_HEIGHT,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002307 DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07002308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2309 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2310 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2311 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2312 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2313 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2314 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2315 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2316}
2317
Jeff Brown83c09682010-12-23 17:50:18 -08002318TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldRotateMotions) {
2319 CursorInputMapper* mapper = new CursorInputMapper(mDevice);
2320 addConfigurationProperty("cursor.mode", "navigation");
2321 addConfigurationProperty("cursor.orientationAware", "1");
Jeff Brownc3db8582010-10-20 15:33:38 -07002322 addMapperAndConfigure(mapper);
2323
2324 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002325 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002326 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
2327 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
2328 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
2329 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
2330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
2331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
2332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
2333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
2334
2335 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002336 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_90);
Jeff Brownc3db8582010-10-20 15:33:38 -07002337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
2338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
2339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
2340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
2341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
2342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
2343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
2344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
2345
2346 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002347 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_180);
Jeff Brownc3db8582010-10-20 15:33:38 -07002348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
2349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
2350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
2351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
2352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
2353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
2354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
2355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
2356
2357 mFakePolicy->setDisplayInfo(DISPLAY_ID,
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002358 DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_270);
Jeff Brownc3db8582010-10-20 15:33:38 -07002359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
2360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
2361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
2362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
2363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
2364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
2365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
2366 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
2367}
2368
2369
2370// --- TouchInputMapperTest ---
2371
2372class TouchInputMapperTest : public InputMapperTest {
2373protected:
2374 static const int32_t RAW_X_MIN;
2375 static const int32_t RAW_X_MAX;
2376 static const int32_t RAW_Y_MIN;
2377 static const int32_t RAW_Y_MAX;
2378 static const int32_t RAW_TOUCH_MIN;
2379 static const int32_t RAW_TOUCH_MAX;
2380 static const int32_t RAW_TOOL_MIN;
2381 static const int32_t RAW_TOOL_MAX;
2382 static const int32_t RAW_PRESSURE_MIN;
2383 static const int32_t RAW_PRESSURE_MAX;
2384 static const int32_t RAW_ORIENTATION_MIN;
2385 static const int32_t RAW_ORIENTATION_MAX;
2386 static const int32_t RAW_ID_MIN;
2387 static const int32_t RAW_ID_MAX;
2388 static const float X_PRECISION;
2389 static const float Y_PRECISION;
2390
2391 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
2392
2393 enum Axes {
2394 POSITION = 1 << 0,
2395 TOUCH = 1 << 1,
2396 TOOL = 1 << 2,
2397 PRESSURE = 1 << 3,
2398 ORIENTATION = 1 << 4,
2399 MINOR = 1 << 5,
2400 ID = 1 << 6,
2401 };
2402
2403 void prepareDisplay(int32_t orientation);
2404 void prepareVirtualKeys();
2405 int32_t toRawX(float displayX);
2406 int32_t toRawY(float displayY);
2407 float toDisplayX(int32_t rawX);
2408 float toDisplayY(int32_t rawY);
2409};
2410
2411const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
Jeff Brown9626b142011-03-03 02:09:54 -08002412const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
Jeff Brownc3db8582010-10-20 15:33:38 -07002413const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
Jeff Brown9626b142011-03-03 02:09:54 -08002414const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
Jeff Brownc3db8582010-10-20 15:33:38 -07002415const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
2416const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
2417const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
2418const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
2419const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = RAW_TOUCH_MIN;
2420const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = RAW_TOUCH_MAX;
2421const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
2422const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
2423const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
2424const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
Jeff Brown9626b142011-03-03 02:09:54 -08002425const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
2426const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Jeff Brownc3db8582010-10-20 15:33:38 -07002427
2428const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
2429 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
2430 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
2431};
2432
2433void TouchInputMapperTest::prepareDisplay(int32_t orientation) {
2434 mFakePolicy->setDisplayInfo(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation);
2435}
2436
2437void TouchInputMapperTest::prepareVirtualKeys() {
Jeff Brown90655042010-12-02 13:50:46 -08002438 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[0]);
2439 mFakeEventHub->addVirtualKeyDefinition(DEVICE_ID, VIRTUAL_KEYS[1]);
Jeff Brownc3db8582010-10-20 15:33:38 -07002440 mFakeEventHub->addKey(DEVICE_ID, KEY_HOME, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2441 mFakeEventHub->addKey(DEVICE_ID, KEY_MENU, AKEYCODE_MENU, POLICY_FLAG_WAKE);
2442}
2443
2444int32_t TouchInputMapperTest::toRawX(float displayX) {
Jeff Brown9626b142011-03-03 02:09:54 -08002445 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07002446}
2447
2448int32_t TouchInputMapperTest::toRawY(float displayY) {
Jeff Brown9626b142011-03-03 02:09:54 -08002449 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07002450}
2451
2452float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Jeff Brown9626b142011-03-03 02:09:54 -08002453 return float(rawX - RAW_X_MIN) * DISPLAY_WIDTH / (RAW_X_MAX - RAW_X_MIN + 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07002454}
2455
2456float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Jeff Brown9626b142011-03-03 02:09:54 -08002457 return float(rawY - RAW_Y_MIN) * DISPLAY_HEIGHT / (RAW_Y_MAX - RAW_Y_MIN + 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07002458}
2459
2460
2461// --- SingleTouchInputMapperTest ---
2462
2463class SingleTouchInputMapperTest : public TouchInputMapperTest {
2464protected:
Jeff Brown49754db2011-07-01 17:37:58 -07002465 void prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002466 void prepareAxes(int axes);
2467
2468 void processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2469 void processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y);
2470 void processUp(SingleTouchInputMapper* mappery);
2471 void processPressure(SingleTouchInputMapper* mapper, int32_t pressure);
2472 void processToolMajor(SingleTouchInputMapper* mapper, int32_t toolMajor);
2473 void processSync(SingleTouchInputMapper* mapper);
2474};
2475
Jeff Brown49754db2011-07-01 17:37:58 -07002476void SingleTouchInputMapperTest::prepareButtons() {
2477 mFakeEventHub->addKey(DEVICE_ID, BTN_TOUCH, AKEYCODE_UNKNOWN, 0);
2478}
2479
Jeff Brownc3db8582010-10-20 15:33:38 -07002480void SingleTouchInputMapperTest::prepareAxes(int axes) {
2481 if (axes & POSITION) {
Jeff Brownefd32662011-03-08 15:13:06 -08002482 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_X,
2483 RAW_X_MIN, RAW_X_MAX, 0, 0);
2484 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_Y,
2485 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002486 }
2487 if (axes & PRESSURE) {
Jeff Brownefd32662011-03-08 15:13:06 -08002488 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_PRESSURE,
2489 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002490 }
2491 if (axes & TOOL) {
Jeff Brownefd32662011-03-08 15:13:06 -08002492 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_TOOL_WIDTH,
2493 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07002494 }
2495}
2496
2497void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2498 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 1, 0);
2499 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2500 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2501}
2502
2503void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper* mapper, int32_t x, int32_t y) {
2504 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_X, 0, x, 0);
2505 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_Y, 0, y, 0);
2506}
2507
2508void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper* mapper) {
2509 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_KEY, BTN_TOUCH, 0, 0, 0);
2510}
2511
2512void SingleTouchInputMapperTest::processPressure(
2513 SingleTouchInputMapper* mapper, int32_t pressure) {
2514 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_PRESSURE, 0, pressure, 0);
2515}
2516
2517void SingleTouchInputMapperTest::processToolMajor(
2518 SingleTouchInputMapper* mapper, int32_t toolMajor) {
2519 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_TOOL_WIDTH, 0, toolMajor, 0);
2520}
2521
2522void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper* mapper) {
2523 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
2524}
2525
2526
Jeff Brownace13b12011-03-09 17:39:48 -08002527TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002528 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown49754db2011-07-01 17:37:58 -07002529 prepareButtons();
Jeff Brown58a2da82011-01-25 16:02:22 -08002530 prepareAxes(POSITION);
2531 addMapperAndConfigure(mapper);
2532
Jeff Brownace13b12011-03-09 17:39:48 -08002533 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2534}
2535
2536TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
2537 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
2538 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_X);
2539 mFakeEventHub->addRelativeAxis(DEVICE_ID, REL_Y);
Jeff Brown49754db2011-07-01 17:37:58 -07002540 prepareButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08002541 prepareAxes(POSITION);
2542 addMapperAndConfigure(mapper);
2543
Jeff Brown58a2da82011-01-25 16:02:22 -08002544 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2545}
2546
Jeff Brown49ed71d2010-12-06 17:13:33 -08002547TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002548 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown49754db2011-07-01 17:37:58 -07002549 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002550 prepareAxes(POSITION);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002551 addConfigurationProperty("touch.deviceType", "touchPad");
Jeff Brownc3db8582010-10-20 15:33:38 -07002552 addMapperAndConfigure(mapper);
2553
2554 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper->getSources());
2555}
2556
Jeff Brown49ed71d2010-12-06 17:13:33 -08002557TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002558 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown49754db2011-07-01 17:37:58 -07002559 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002560 prepareAxes(POSITION);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002561 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownc3db8582010-10-20 15:33:38 -07002562 addMapperAndConfigure(mapper);
2563
2564 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper->getSources());
2565}
2566
2567TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002568 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002569 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002570 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002571 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002572 prepareAxes(POSITION);
2573 prepareVirtualKeys();
2574 addMapperAndConfigure(mapper);
2575
2576 // Unknown key.
2577 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
2578
2579 // Virtual key is down.
2580 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2581 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2582 processDown(mapper, x, y);
2583 processSync(mapper);
2584 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2585
2586 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2587
2588 // Virtual key is up.
2589 processUp(mapper);
2590 processSync(mapper);
2591 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2592
2593 ASSERT_EQ(AKEY_STATE_UP, mapper->getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
2594}
2595
2596TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002597 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002598 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002599 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002600 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002601 prepareAxes(POSITION);
2602 prepareVirtualKeys();
2603 addMapperAndConfigure(mapper);
2604
2605 // Unknown key.
2606 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
2607
2608 // Virtual key is down.
2609 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2610 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2611 processDown(mapper, x, y);
2612 processSync(mapper);
2613 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2614
2615 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2616
2617 // Virtual key is up.
2618 processUp(mapper);
2619 processSync(mapper);
2620 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2621
2622 ASSERT_EQ(AKEY_STATE_UP, mapper->getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
2623}
2624
2625TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002626 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002627 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002628 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002629 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002630 prepareAxes(POSITION);
2631 prepareVirtualKeys();
2632 addMapperAndConfigure(mapper);
2633
2634 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
2635 uint8_t flags[2] = { 0, 0 };
2636 ASSERT_TRUE(mapper->markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
2637 ASSERT_TRUE(flags[0]);
2638 ASSERT_FALSE(flags[1]);
2639}
2640
2641TEST_F(SingleTouchInputMapperTest, Reset_WhenVirtualKeysAreDown_SendsUp) {
2642 // Note: Ideally we should send cancels but the implementation is more straightforward
2643 // with up and this will only happen if a device is forcibly removed.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002644 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002645 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002646 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002647 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002648 prepareAxes(POSITION);
2649 prepareVirtualKeys();
2650 addMapperAndConfigure(mapper);
2651
2652 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2653
2654 // Press virtual key.
2655 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2656 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2657 processDown(mapper, x, y);
2658 processSync(mapper);
2659 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2660
2661 // Reset. Since key is down, synthesize key up.
2662 mapper->reset();
2663
2664 FakeInputDispatcher::NotifyKeyArgs args;
2665 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2666 //ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2667 ASSERT_EQ(DEVICE_ID, args.deviceId);
2668 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2669 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2670 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2671 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2672 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2673 ASSERT_EQ(KEY_HOME, args.scanCode);
2674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2675 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2676}
2677
2678TEST_F(SingleTouchInputMapperTest, Reset_WhenNothingIsPressed_NothingMuchHappens) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002679 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002680 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002681 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002682 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002683 prepareAxes(POSITION);
2684 prepareVirtualKeys();
2685 addMapperAndConfigure(mapper);
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 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2693
2694 // Release virtual key.
2695 processUp(mapper);
2696 processSync(mapper);
2697 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled());
2698
2699 // Reset. Since no key is down, nothing happens.
2700 mapper->reset();
2701
2702 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2703 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2704}
2705
2706TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002707 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002708 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002709 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002710 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002711 prepareAxes(POSITION);
2712 prepareVirtualKeys();
2713 addMapperAndConfigure(mapper);
2714
2715 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2716
2717 FakeInputDispatcher::NotifyKeyArgs args;
2718
2719 // Press virtual key.
2720 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2721 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2722 processDown(mapper, x, y);
2723 processSync(mapper);
2724
2725 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2726 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2727 ASSERT_EQ(DEVICE_ID, args.deviceId);
2728 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2729 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2730 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2731 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2732 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2733 ASSERT_EQ(KEY_HOME, args.scanCode);
2734 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2735 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2736
2737 // Release virtual key.
2738 processUp(mapper);
2739 processSync(mapper);
2740
2741 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&args));
2742 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2743 ASSERT_EQ(DEVICE_ID, args.deviceId);
2744 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2745 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
2746 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2747 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
2748 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2749 ASSERT_EQ(KEY_HOME, args.scanCode);
2750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
2751 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2752
2753 // Should not have sent any motions.
2754 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2755}
2756
2757TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002758 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002759 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002760 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002761 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002762 prepareAxes(POSITION);
2763 prepareVirtualKeys();
2764 addMapperAndConfigure(mapper);
2765
2766 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2767
2768 FakeInputDispatcher::NotifyKeyArgs keyArgs;
2769
2770 // Press virtual key.
2771 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
2772 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
2773 processDown(mapper, x, y);
2774 processSync(mapper);
2775
2776 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2777 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2778 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2779 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2780 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2781 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
2782 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
2783 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2784 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2785 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2786 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2787
2788 // Move out of bounds. This should generate a cancel and a pointer down since we moved
2789 // into the display area.
2790 y -= 100;
2791 processMove(mapper, x, y);
2792 processSync(mapper);
2793
2794 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasCalled(&keyArgs));
2795 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
2796 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
2797 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
2798 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
2799 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
2800 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2801 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
2802 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
2803 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
2804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
2805 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
2806
2807 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2808 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2810 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2811 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2812 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2814 ASSERT_EQ(0, motionArgs.flags);
2815 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002816 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002817 ASSERT_EQ(0, motionArgs.edgeFlags);
2818 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2822 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2823 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2824 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2826
2827 // Keep moving out of bounds. Should generate a pointer move.
2828 y -= 50;
2829 processMove(mapper, x, y);
2830 processSync(mapper);
2831
2832 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2833 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2834 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2835 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2836 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
2838 ASSERT_EQ(0, motionArgs.flags);
2839 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002840 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002841 ASSERT_EQ(0, motionArgs.edgeFlags);
2842 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002843 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2846 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2847 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2848 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2849 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2850
2851 // Release out of bounds. Should generate a pointer up.
2852 processUp(mapper);
2853 processSync(mapper);
2854
2855 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2857 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2858 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2859 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2861 ASSERT_EQ(0, motionArgs.flags);
2862 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002863 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002864 ASSERT_EQ(0, motionArgs.edgeFlags);
2865 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2869 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2870 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2871 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2872 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2873
2874 // Should not have sent any more keys or motions.
2875 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2876 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2877}
2878
2879TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002880 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002881 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002882 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002883 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002884 prepareAxes(POSITION);
2885 prepareVirtualKeys();
2886 addMapperAndConfigure(mapper);
2887
2888 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2889
2890 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2891
2892 // Initially go down out of bounds.
2893 int32_t x = -10;
2894 int32_t y = -10;
2895 processDown(mapper, x, y);
2896 processSync(mapper);
2897
2898 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2899
2900 // Move into the display area. Should generate a pointer down.
2901 x = 50;
2902 y = 75;
2903 processMove(mapper, x, y);
2904 processSync(mapper);
2905
2906 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2907 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2908 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2909 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2910 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2912 ASSERT_EQ(0, motionArgs.flags);
2913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002914 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002915 ASSERT_EQ(0, motionArgs.edgeFlags);
2916 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002917 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2920 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2921 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2922 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2924
2925 // Release. Should generate a pointer up.
2926 processUp(mapper);
2927 processSync(mapper);
2928
2929 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2931 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2932 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2933 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
2935 ASSERT_EQ(0, motionArgs.flags);
2936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002937 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002938 ASSERT_EQ(0, motionArgs.edgeFlags);
2939 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002940 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2943 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2944 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2945 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2947
2948 // Should not have sent any more keys or motions.
2949 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
2950 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
2951}
2952
2953TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002954 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08002955 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002956 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07002957 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07002958 prepareAxes(POSITION);
2959 prepareVirtualKeys();
2960 addMapperAndConfigure(mapper);
2961
2962 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
2963
2964 FakeInputDispatcher::NotifyMotionArgs motionArgs;
2965
2966 // Down.
2967 int32_t x = 100;
2968 int32_t y = 125;
2969 processDown(mapper, x, y);
2970 processSync(mapper);
2971
2972 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2974 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
2975 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
2976 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
2977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
2978 ASSERT_EQ(0, motionArgs.flags);
2979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002980 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07002981 ASSERT_EQ(0, motionArgs.edgeFlags);
2982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
2984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07002985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
2986 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
2987 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
2988 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
2989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
2990
2991 // Move.
2992 x += 50;
2993 y += 75;
2994 processMove(mapper, x, y);
2995 processSync(mapper);
2996
2997 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
2998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
2999 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3000 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3001 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3003 ASSERT_EQ(0, motionArgs.flags);
3004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003005 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003006 ASSERT_EQ(0, motionArgs.edgeFlags);
3007 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3011 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
3012 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3013 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3015
3016 // Up.
3017 processUp(mapper);
3018 processSync(mapper);
3019
3020 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3021 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3022 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3023 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3024 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3025 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3026 ASSERT_EQ(0, motionArgs.flags);
3027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003028 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003029 ASSERT_EQ(0, motionArgs.edgeFlags);
3030 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003031 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3034 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0));
3035 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3036 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3037 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3038
3039 // Should not have sent any more keys or motions.
3040 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3041 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3042}
3043
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003044TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_DoesNotRotateMotions) {
3045 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003046 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brown49754db2011-07-01 17:37:58 -07003047 prepareButtons();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003048 prepareAxes(POSITION);
3049 addConfigurationProperty("touch.orientationAware", "0");
3050 addMapperAndConfigure(mapper);
3051
3052 FakeInputDispatcher::NotifyMotionArgs args;
3053
3054 // Rotation 90.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003055 prepareDisplay(DISPLAY_ORIENTATION_90);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003056 processDown(mapper, toRawX(50), toRawY(75));
3057 processSync(mapper);
3058
3059 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brownebbd5d12011-02-17 13:01:34 -08003060 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3061 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003062
3063 processUp(mapper);
3064 processSync(mapper);
3065 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3066}
3067
3068TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_RotatesMotions) {
3069 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003070 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brown49754db2011-07-01 17:37:58 -07003071 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07003072 prepareAxes(POSITION);
3073 addMapperAndConfigure(mapper);
3074
3075 FakeInputDispatcher::NotifyMotionArgs args;
3076
3077 // Rotation 0.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003078 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003079 processDown(mapper, toRawX(50), toRawY(75));
3080 processSync(mapper);
3081
3082 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brownebbd5d12011-02-17 13:01:34 -08003083 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3084 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003085
3086 processUp(mapper);
3087 processSync(mapper);
3088 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3089
3090 // Rotation 90.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003091 prepareDisplay(DISPLAY_ORIENTATION_90);
Jeff Brown9626b142011-03-03 02:09:54 -08003092 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Jeff Brownc3db8582010-10-20 15:33:38 -07003093 processSync(mapper);
3094
3095 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003096 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3097 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003098
3099 processUp(mapper);
3100 processSync(mapper);
3101 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3102
3103 // Rotation 180.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003104 prepareDisplay(DISPLAY_ORIENTATION_180);
Jeff Brown9626b142011-03-03 02:09:54 -08003105 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 -07003106 processSync(mapper);
3107
3108 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003109 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3110 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003111
3112 processUp(mapper);
3113 processSync(mapper);
3114 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3115
3116 // Rotation 270.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003117 prepareDisplay(DISPLAY_ORIENTATION_270);
Jeff Brown9626b142011-03-03 02:09:54 -08003118 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Jeff Brownc3db8582010-10-20 15:33:38 -07003119 processSync(mapper);
3120
3121 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown9626b142011-03-03 02:09:54 -08003122 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
3123 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
Jeff Brownc3db8582010-10-20 15:33:38 -07003124
3125 processUp(mapper);
3126 processSync(mapper);
3127 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled());
3128}
3129
3130TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003131 SingleTouchInputMapper* mapper = new SingleTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003132 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003133 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brown49754db2011-07-01 17:37:58 -07003134 prepareButtons();
Jeff Brownc3db8582010-10-20 15:33:38 -07003135 prepareAxes(POSITION | PRESSURE | TOOL);
3136 addMapperAndConfigure(mapper);
3137
3138 // These calculations are based on the input device calibration documentation.
3139 int32_t rawX = 100;
3140 int32_t rawY = 200;
3141 int32_t rawPressure = 10;
3142 int32_t rawToolMajor = 12;
3143
3144 float x = toDisplayX(rawX);
3145 float y = toDisplayY(rawY);
3146 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3147 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3148 float tool = min(DISPLAY_WIDTH, DISPLAY_HEIGHT) * size;
3149 float touch = min(tool * pressure, tool);
3150
3151 processDown(mapper, rawX, rawY);
3152 processPressure(mapper, rawPressure);
3153 processToolMajor(mapper, rawToolMajor);
3154 processSync(mapper);
3155
3156 FakeInputDispatcher::NotifyMotionArgs args;
3157 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3159 x, y, pressure, size, touch, touch, tool, tool, 0));
3160}
3161
3162
3163// --- MultiTouchInputMapperTest ---
3164
3165class MultiTouchInputMapperTest : public TouchInputMapperTest {
3166protected:
3167 void prepareAxes(int axes);
3168
3169 void processPosition(MultiTouchInputMapper* mapper, int32_t x, int32_t y);
3170 void processTouchMajor(MultiTouchInputMapper* mapper, int32_t touchMajor);
3171 void processTouchMinor(MultiTouchInputMapper* mapper, int32_t touchMinor);
3172 void processToolMajor(MultiTouchInputMapper* mapper, int32_t toolMajor);
3173 void processToolMinor(MultiTouchInputMapper* mapper, int32_t toolMinor);
3174 void processOrientation(MultiTouchInputMapper* mapper, int32_t orientation);
3175 void processPressure(MultiTouchInputMapper* mapper, int32_t pressure);
3176 void processId(MultiTouchInputMapper* mapper, int32_t id);
3177 void processMTSync(MultiTouchInputMapper* mapper);
3178 void processSync(MultiTouchInputMapper* mapper);
3179};
3180
3181void MultiTouchInputMapperTest::prepareAxes(int axes) {
3182 if (axes & POSITION) {
Jeff Brownefd32662011-03-08 15:13:06 -08003183 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_X,
3184 RAW_X_MIN, RAW_X_MAX, 0, 0);
3185 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_POSITION_Y,
3186 RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003187 }
3188 if (axes & TOUCH) {
Jeff Brownefd32662011-03-08 15:13:06 -08003189 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MAJOR,
3190 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003191 if (axes & MINOR) {
Jeff Brownefd32662011-03-08 15:13:06 -08003192 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TOUCH_MINOR,
Jeff Brownc3db8582010-10-20 15:33:38 -07003193 RAW_TOUCH_MIN, RAW_TOUCH_MAX, 0, 0);
3194 }
3195 }
3196 if (axes & TOOL) {
Jeff Brownefd32662011-03-08 15:13:06 -08003197 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MAJOR,
3198 RAW_TOOL_MIN, RAW_TOOL_MAX, 0, 0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003199 if (axes & MINOR) {
Jeff Brownefd32662011-03-08 15:13:06 -08003200 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_WIDTH_MINOR,
Jeff Brownc3db8582010-10-20 15:33:38 -07003201 RAW_TOOL_MAX, RAW_TOOL_MAX, 0, 0);
3202 }
3203 }
3204 if (axes & ORIENTATION) {
Jeff Brownefd32662011-03-08 15:13:06 -08003205 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_ORIENTATION,
Jeff Brownc3db8582010-10-20 15:33:38 -07003206 RAW_ORIENTATION_MIN, RAW_ORIENTATION_MAX, 0, 0);
3207 }
3208 if (axes & PRESSURE) {
Jeff Brownefd32662011-03-08 15:13:06 -08003209 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_PRESSURE,
Jeff Brownc3db8582010-10-20 15:33:38 -07003210 RAW_PRESSURE_MIN, RAW_PRESSURE_MAX, 0, 0);
3211 }
3212 if (axes & ID) {
Jeff Brownefd32662011-03-08 15:13:06 -08003213 mFakeEventHub->addAbsoluteAxis(DEVICE_ID, ABS_MT_TRACKING_ID,
Jeff Brownc3db8582010-10-20 15:33:38 -07003214 RAW_ID_MIN, RAW_ID_MAX, 0, 0);
3215 }
3216}
3217
3218void MultiTouchInputMapperTest::processPosition(
3219 MultiTouchInputMapper* mapper, int32_t x, int32_t y) {
3220 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_X, 0, x, 0);
3221 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_POSITION_Y, 0, y, 0);
3222}
3223
3224void MultiTouchInputMapperTest::processTouchMajor(
3225 MultiTouchInputMapper* mapper, int32_t touchMajor) {
3226 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MAJOR, 0, touchMajor, 0);
3227}
3228
3229void MultiTouchInputMapperTest::processTouchMinor(
3230 MultiTouchInputMapper* mapper, int32_t touchMinor) {
3231 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TOUCH_MINOR, 0, touchMinor, 0);
3232}
3233
3234void MultiTouchInputMapperTest::processToolMajor(
3235 MultiTouchInputMapper* mapper, int32_t toolMajor) {
3236 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MAJOR, 0, toolMajor, 0);
3237}
3238
3239void MultiTouchInputMapperTest::processToolMinor(
3240 MultiTouchInputMapper* mapper, int32_t toolMinor) {
3241 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_WIDTH_MINOR, 0, toolMinor, 0);
3242}
3243
3244void MultiTouchInputMapperTest::processOrientation(
3245 MultiTouchInputMapper* mapper, int32_t orientation) {
3246 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_ORIENTATION, 0, orientation, 0);
3247}
3248
3249void MultiTouchInputMapperTest::processPressure(
3250 MultiTouchInputMapper* mapper, int32_t pressure) {
3251 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_PRESSURE, 0, pressure, 0);
3252}
3253
3254void MultiTouchInputMapperTest::processId(
3255 MultiTouchInputMapper* mapper, int32_t id) {
3256 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_ABS, ABS_MT_TRACKING_ID, 0, id, 0);
3257}
3258
3259void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper* mapper) {
3260 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_MT_REPORT, 0, 0, 0);
3261}
3262
3263void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper* mapper) {
3264 process(mapper, ARBITRARY_TIME, DEVICE_ID, EV_SYN, SYN_REPORT, 0, 0, 0);
3265}
3266
3267
3268TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003269 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003270 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003271 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003272 prepareAxes(POSITION);
3273 prepareVirtualKeys();
3274 addMapperAndConfigure(mapper);
3275
3276 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3277
3278 FakeInputDispatcher::NotifyMotionArgs motionArgs;
3279
3280 // Two fingers down at once.
3281 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3282 processPosition(mapper, x1, y1);
3283 processMTSync(mapper);
3284 processPosition(mapper, x2, y2);
3285 processMTSync(mapper);
3286 processSync(mapper);
3287
3288 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3290 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3293 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3294 ASSERT_EQ(0, motionArgs.flags);
3295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003296 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003297 ASSERT_EQ(0, motionArgs.edgeFlags);
3298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003299 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3302 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3303 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3304 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3306
3307 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3312 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3313 motionArgs.action);
3314 ASSERT_EQ(0, motionArgs.flags);
3315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003316 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003317 ASSERT_EQ(0, motionArgs.edgeFlags);
3318 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3330
3331 // Move.
3332 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3333 processPosition(mapper, x1, y1);
3334 processMTSync(mapper);
3335 processPosition(mapper, x2, y2);
3336 processMTSync(mapper);
3337 processSync(mapper);
3338
3339 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3345 ASSERT_EQ(0, motionArgs.flags);
3346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003347 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003348 ASSERT_EQ(0, motionArgs.edgeFlags);
3349 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3352 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3355 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3357 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3358 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3359 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3361
3362 // First finger up.
3363 x2 += 15; y2 -= 20;
3364 processPosition(mapper, x2, y2);
3365 processMTSync(mapper);
3366 processSync(mapper);
3367
3368 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3373 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3374 motionArgs.action);
3375 ASSERT_EQ(0, motionArgs.flags);
3376 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003377 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003378 ASSERT_EQ(0, motionArgs.edgeFlags);
3379 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3382 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3385 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3388 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3389 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3390 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3391
3392 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3394 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3396 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3398 ASSERT_EQ(0, motionArgs.flags);
3399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003400 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003401 ASSERT_EQ(0, motionArgs.edgeFlags);
3402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003403 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
3404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3410
3411 // Move.
3412 x2 += 20; y2 -= 25;
3413 processPosition(mapper, x2, y2);
3414 processMTSync(mapper);
3415 processSync(mapper);
3416
3417 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3423 ASSERT_EQ(0, motionArgs.flags);
3424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003425 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003426 ASSERT_EQ(0, motionArgs.edgeFlags);
3427 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003428 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
3429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3435
3436 // New finger down.
3437 int32_t x3 = 700, y3 = 300;
3438 processPosition(mapper, x2, y2);
3439 processMTSync(mapper);
3440 processPosition(mapper, x3, y3);
3441 processMTSync(mapper);
3442 processSync(mapper);
3443
3444 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3446 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3447 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3448 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3449 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3450 motionArgs.action);
3451 ASSERT_EQ(0, motionArgs.flags);
3452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003453 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003454 ASSERT_EQ(0, motionArgs.edgeFlags);
3455 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3458 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3461 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3463 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3467
3468 // Second finger up.
3469 x3 += 30; y3 -= 20;
3470 processPosition(mapper, x3, y3);
3471 processMTSync(mapper);
3472 processSync(mapper);
3473
3474 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3479 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3480 motionArgs.action);
3481 ASSERT_EQ(0, motionArgs.flags);
3482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003483 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003484 ASSERT_EQ(0, motionArgs.edgeFlags);
3485 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
3488 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
3489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3491 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3494 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3495 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3497
3498 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3500 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3501 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3502 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3504 ASSERT_EQ(0, motionArgs.flags);
3505 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003506 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003507 ASSERT_EQ(0, motionArgs.edgeFlags);
3508 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003509 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3512 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3513 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3514 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3516
3517 // Last finger up.
3518 processMTSync(mapper);
3519 processSync(mapper);
3520
3521 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
3523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
3524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
3525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
3526 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3527 ASSERT_EQ(0, motionArgs.flags);
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003529 ASSERT_EQ(0, motionArgs.buttonState);
Jeff Brownc3db8582010-10-20 15:33:38 -07003530 ASSERT_EQ(0, motionArgs.edgeFlags);
3531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
3533 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(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3536 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
3537 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
3538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
3539
3540 // Should not have sent any more keys or motions.
3541 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3542 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3543}
3544
3545TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003546 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003547 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003548 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003549 prepareAxes(POSITION | ID);
3550 prepareVirtualKeys();
3551 addMapperAndConfigure(mapper);
3552
3553 mFakeContext->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
3554
3555 FakeInputDispatcher::NotifyMotionArgs motionArgs;
3556
3557 // Two fingers down at once.
3558 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
3559 processPosition(mapper, x1, y1);
3560 processId(mapper, 1);
3561 processMTSync(mapper);
3562 processPosition(mapper, x2, y2);
3563 processId(mapper, 2);
3564 processMTSync(mapper);
3565 processSync(mapper);
3566
3567 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
3569 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3573 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3574
3575 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3576 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3577 motionArgs.action);
3578 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003581 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3584 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3586 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3587
3588 // Move.
3589 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
3590 processPosition(mapper, x1, y1);
3591 processId(mapper, 1);
3592 processMTSync(mapper);
3593 processPosition(mapper, x2, y2);
3594 processId(mapper, 2);
3595 processMTSync(mapper);
3596 processSync(mapper);
3597
3598 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3609
3610 // First finger up.
3611 x2 += 15; y2 -= 20;
3612 processPosition(mapper, x2, y2);
3613 processId(mapper, 2);
3614 processMTSync(mapper);
3615 processSync(mapper);
3616
3617 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3618 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3619 motionArgs.action);
3620 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003623 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3626 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0));
3627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3628 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3629
3630 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3632 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003633 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3637
3638 // Move.
3639 x2 += 20; y2 -= 25;
3640 processPosition(mapper, x2, y2);
3641 processId(mapper, 2);
3642 processMTSync(mapper);
3643 processSync(mapper);
3644
3645 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003648 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3651 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
3652
3653 // New finger down.
3654 int32_t x3 = 700, y3 = 300;
3655 processPosition(mapper, x2, y2);
3656 processId(mapper, 2);
3657 processMTSync(mapper);
3658 processPosition(mapper, x3, y3);
3659 processId(mapper, 3);
3660 processMTSync(mapper);
3661 processSync(mapper);
3662
3663 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
Jeff Brown6894a292011-07-01 17:59:27 -07003664 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownc3db8582010-10-20 15:33:38 -07003665 motionArgs.action);
3666 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003669 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
Jeff Brownc3db8582010-10-20 15:33:38 -07003672 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
Jeff Brown6894a292011-07-01 17:59:27 -07003673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3674 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
Jeff Brownc3db8582010-10-20 15:33:38 -07003675
3676 // Second finger up.
3677 x3 += 30; y3 -= 20;
3678 processPosition(mapper, x3, y3);
3679 processId(mapper, 3);
3680 processMTSync(mapper);
3681 processSync(mapper);
3682
3683 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
Jeff Brown6894a292011-07-01 17:59:27 -07003684 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownc3db8582010-10-20 15:33:38 -07003685 motionArgs.action);
3686 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brown6894a292011-07-01 17:59:27 -07003689 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
Jeff Brownc3db8582010-10-20 15:33:38 -07003692 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
Jeff Brown6894a292011-07-01 17:59:27 -07003693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
3694 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0));
Jeff Brownc3db8582010-10-20 15:33:38 -07003695
3696 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
3698 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3702 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3703
3704 // Last finger up.
3705 processMTSync(mapper);
3706 processSync(mapper);
3707
3708 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&motionArgs));
3709 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
3710 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
Jeff Brown6894a292011-07-01 17:59:27 -07003711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Jeff Brownc3db8582010-10-20 15:33:38 -07003713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
3714 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0));
3715
3716 // Should not have sent any more keys or motions.
3717 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyKeyWasNotCalled());
3718 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasNotCalled());
3719}
3720
3721TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003722 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003723 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003724 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003725 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR);
3726 addMapperAndConfigure(mapper);
3727
3728 // These calculations are based on the input device calibration documentation.
3729 int32_t rawX = 100;
3730 int32_t rawY = 200;
3731 int32_t rawTouchMajor = 7;
3732 int32_t rawTouchMinor = 6;
3733 int32_t rawToolMajor = 9;
3734 int32_t rawToolMinor = 8;
3735 int32_t rawPressure = 11;
3736 int32_t rawOrientation = 3;
3737 int32_t id = 5;
3738
3739 float x = toDisplayX(rawX);
3740 float y = toDisplayY(rawY);
3741 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
3742 float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
3743 float toolMajor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMajor / RAW_TOOL_MAX;
3744 float toolMinor = float(min(DISPLAY_WIDTH, DISPLAY_HEIGHT)) * rawToolMinor / RAW_TOOL_MAX;
3745 float touchMajor = min(toolMajor * pressure, toolMajor);
3746 float touchMinor = min(toolMinor * pressure, toolMinor);
3747 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
3748
3749 processPosition(mapper, rawX, rawY);
3750 processTouchMajor(mapper, rawTouchMajor);
3751 processTouchMinor(mapper, rawTouchMinor);
3752 processToolMajor(mapper, rawToolMajor);
3753 processToolMinor(mapper, rawToolMinor);
3754 processPressure(mapper, rawPressure);
3755 processOrientation(mapper, rawOrientation);
3756 processId(mapper, id);
3757 processMTSync(mapper);
3758 processSync(mapper);
3759
3760 FakeInputDispatcher::NotifyMotionArgs args;
3761 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
Jeff Brown6894a292011-07-01 17:59:27 -07003762 ASSERT_EQ(0, args.pointerProperties[0].id);
Jeff Brownc3db8582010-10-20 15:33:38 -07003763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3764 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, orientation));
3765}
3766
3767TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003768 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003769 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003770 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003771 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003772 addConfigurationProperty("touch.touchSize.calibration", "geometric");
3773 addConfigurationProperty("touch.toolSize.calibration", "geometric");
Jeff Brownc3db8582010-10-20 15:33:38 -07003774 addMapperAndConfigure(mapper);
3775
3776 // These calculations are based on the input device calibration documentation.
3777 int32_t rawX = 100;
3778 int32_t rawY = 200;
3779 int32_t rawTouchMajor = 140;
3780 int32_t rawTouchMinor = 120;
3781 int32_t rawToolMajor = 180;
3782 int32_t rawToolMinor = 160;
3783
3784 float x = toDisplayX(rawX);
3785 float y = toDisplayY(rawY);
3786 float pressure = float(rawTouchMajor) / RAW_TOUCH_MAX;
3787 float size = avg(rawToolMajor, rawToolMinor) / RAW_TOOL_MAX;
Jeff Brown9626b142011-03-03 02:09:54 -08003788 float scale = avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
3789 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
Jeff Brownc3db8582010-10-20 15:33:38 -07003790 float toolMajor = float(rawToolMajor) * scale;
3791 float toolMinor = float(rawToolMinor) * scale;
3792 float touchMajor = min(float(rawTouchMajor) * scale, toolMajor);
3793 float touchMinor = min(float(rawTouchMinor) * scale, toolMinor);
3794
3795 processPosition(mapper, rawX, rawY);
3796 processTouchMajor(mapper, rawTouchMajor);
3797 processTouchMinor(mapper, rawTouchMinor);
3798 processToolMajor(mapper, rawToolMajor);
3799 processToolMinor(mapper, rawToolMinor);
3800 processMTSync(mapper);
3801 processSync(mapper);
3802
3803 FakeInputDispatcher::NotifyMotionArgs args;
3804 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3806 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor, 0));
3807}
3808
3809TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_SummedLinearCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003810 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003811 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003812 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003813 prepareAxes(POSITION | TOUCH | TOOL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003814 addConfigurationProperty("touch.touchSize.calibration", "pressure");
3815 addConfigurationProperty("touch.toolSize.calibration", "linear");
3816 addConfigurationProperty("touch.toolSize.linearScale", "10");
3817 addConfigurationProperty("touch.toolSize.linearBias", "160");
3818 addConfigurationProperty("touch.toolSize.isSummed", "1");
3819 addConfigurationProperty("touch.pressure.calibration", "amplitude");
3820 addConfigurationProperty("touch.pressure.source", "touch");
3821 addConfigurationProperty("touch.pressure.scale", "0.01");
Jeff Brownc3db8582010-10-20 15:33:38 -07003822 addMapperAndConfigure(mapper);
3823
3824 // These calculations are based on the input device calibration documentation.
3825 // Note: We only provide a single common touch/tool value because the device is assumed
3826 // not to emit separate values for each pointer (isSummed = 1).
3827 int32_t rawX = 100;
3828 int32_t rawY = 200;
3829 int32_t rawX2 = 150;
3830 int32_t rawY2 = 250;
3831 int32_t rawTouchMajor = 60;
3832 int32_t rawToolMajor = 5;
3833
3834 float x = toDisplayX(rawX);
3835 float y = toDisplayY(rawY);
3836 float x2 = toDisplayX(rawX2);
3837 float y2 = toDisplayY(rawY2);
3838 float pressure = float(rawTouchMajor) * 0.01f;
3839 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3840 float tool = (float(rawToolMajor) * 10.0f + 160.0f) / 2;
3841 float touch = min(tool * pressure, tool);
3842
3843 processPosition(mapper, rawX, rawY);
3844 processTouchMajor(mapper, rawTouchMajor);
3845 processToolMajor(mapper, rawToolMajor);
3846 processMTSync(mapper);
3847 processPosition(mapper, rawX2, rawY2);
3848 processTouchMajor(mapper, rawTouchMajor);
3849 processToolMajor(mapper, rawToolMajor);
3850 processMTSync(mapper);
3851 processSync(mapper);
3852
3853 FakeInputDispatcher::NotifyMotionArgs args;
3854 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Jeff Brown49754db2011-07-01 17:37:58 -07003856
Jeff Brownc3db8582010-10-20 15:33:38 -07003857 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3858 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
3859 args.action);
3860 ASSERT_EQ(size_t(2), args.pointerCount);
3861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3862 x, y, pressure, size, touch, touch, tool, tool, 0));
3863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
3864 x2, y2, pressure, size, touch, touch, tool, tool, 0));
3865}
3866
3867TEST_F(MultiTouchInputMapperTest, Process_TouchToolPressureSizeAxes_AreaCalibration) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003868 MultiTouchInputMapper* mapper = new MultiTouchInputMapper(mDevice);
Jeff Brown58a2da82011-01-25 16:02:22 -08003869 addConfigurationProperty("touch.deviceType", "touchScreen");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003870 prepareDisplay(DISPLAY_ORIENTATION_0);
Jeff Brownc3db8582010-10-20 15:33:38 -07003871 prepareAxes(POSITION | TOUCH | TOOL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003872 addConfigurationProperty("touch.touchSize.calibration", "pressure");
3873 addConfigurationProperty("touch.toolSize.calibration", "area");
3874 addConfigurationProperty("touch.toolSize.areaScale", "22");
3875 addConfigurationProperty("touch.toolSize.areaBias", "1");
3876 addConfigurationProperty("touch.toolSize.linearScale", "9.2");
3877 addConfigurationProperty("touch.toolSize.linearBias", "3");
3878 addConfigurationProperty("touch.pressure.calibration", "amplitude");
3879 addConfigurationProperty("touch.pressure.source", "touch");
3880 addConfigurationProperty("touch.pressure.scale", "0.01");
Jeff Brownc3db8582010-10-20 15:33:38 -07003881 addMapperAndConfigure(mapper);
3882
3883 // These calculations are based on the input device calibration documentation.
3884 int32_t rawX = 100;
3885 int32_t rawY = 200;
3886 int32_t rawTouchMajor = 60;
3887 int32_t rawToolMajor = 5;
3888
3889 float x = toDisplayX(rawX);
3890 float y = toDisplayY(rawY);
3891 float pressure = float(rawTouchMajor) * 0.01f;
3892 float size = float(rawToolMajor) / RAW_TOOL_MAX;
3893 float tool = sqrtf(float(rawToolMajor) * 22.0f + 1.0f) * 9.2f + 3.0f;
3894 float touch = min(tool * pressure, tool);
3895
3896 processPosition(mapper, rawX, rawY);
3897 processTouchMajor(mapper, rawTouchMajor);
3898 processToolMajor(mapper, rawToolMajor);
3899 processMTSync(mapper);
3900 processSync(mapper);
3901
3902 FakeInputDispatcher::NotifyMotionArgs args;
3903 ASSERT_NO_FATAL_FAILURE(mFakeDispatcher->assertNotifyMotionWasCalled(&args));
3904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
3905 x, y, pressure, size, touch, touch, tool, tool, 0));
3906}
3907
3908} // namespace android