blob: 961566fe815733f01f1edb419bccd3c8edf506f9 [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 Brown46b9ac0a2010-04-22 18:58:52 -070016
Jeff Brownb4ff35d2011-01-02 16:37:43 -080017#include "../InputDispatcher.h"
18
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070019#include <gtest/gtest.h>
Jeff Brownc3db8582010-10-20 15:33:38 -070020#include <linux/input.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070021
22namespace android {
23
Jeff Brownc3db8582010-10-20 15:33:38 -070024// An arbitrary time value.
25static const nsecs_t ARBITRARY_TIME = 1234;
26
27// An arbitrary device id.
28static const int32_t DEVICE_ID = 1;
29
30// An arbitrary injector pid / uid pair that has permission to inject events.
31static const int32_t INJECTOR_PID = 999;
32static const int32_t INJECTOR_UID = 1001;
33
34
35// --- FakeInputDispatcherPolicy ---
36
37class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
Jeff Brown214eaf42011-05-26 19:17:02 -070038 InputDispatcherConfiguration mConfig;
39
Jeff Brownc3db8582010-10-20 15:33:38 -070040protected:
41 virtual ~FakeInputDispatcherPolicy() {
42 }
43
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044public:
Jeff Brownc3db8582010-10-20 15:33:38 -070045 FakeInputDispatcherPolicy() {
46 }
47
48private:
49 virtual void notifyConfigurationChanged(nsecs_t when) {
50 }
51
52 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -080053 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownc3db8582010-10-20 15:33:38 -070054 return 0;
55 }
56
Jeff Brown928e0542011-01-10 11:17:36 -080057 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownc3db8582010-10-20 15:33:38 -070058 }
59
Jeff Brown214eaf42011-05-26 19:17:02 -070060 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
61 *outConfig = mConfig;
Jeff Brownc3db8582010-10-20 15:33:38 -070062 }
63
Jeff Brown214eaf42011-05-26 19:17:02 -070064 virtual bool isKeyRepeatEnabled() {
65 return true;
Jeff Brownc3db8582010-10-20 15:33:38 -070066 }
67
Jeff Brown0029c662011-03-30 02:25:18 -070068 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
69 return true;
70 }
71
Jeff Brown1f245102010-11-18 20:53:46 -080072 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) {
Jeff Brownc3db8582010-10-20 15:33:38 -070073 }
74
Jeff Brown56194eb2011-03-02 19:23:13 -080075 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brownc3db8582010-10-20 15:33:38 -070076 }
77
Jeff Brownd5bb82d2011-10-12 13:57:59 -070078 virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownc3db8582010-10-20 15:33:38 -070079 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brownd5bb82d2011-10-12 13:57:59 -070080 return 0;
Jeff Brownc3db8582010-10-20 15:33:38 -070081 }
82
Jeff Brown928e0542011-01-10 11:17:36 -080083 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -080084 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -070085 return false;
86 }
87
Jeff Brownc3db8582010-10-20 15:33:38 -070088 virtual void notifySwitch(nsecs_t when,
89 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) {
90 }
91
92 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
93 }
94
95 virtual bool checkInjectEventsPermissionNonReentrant(
96 int32_t injectorPid, int32_t injectorUid) {
97 return false;
98 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070099};
100
Jeff Brownc3db8582010-10-20 15:33:38 -0700101
102// --- InputDispatcherTest ---
103
104class InputDispatcherTest : public testing::Test {
105protected:
106 sp<FakeInputDispatcherPolicy> mFakePolicy;
107 sp<InputDispatcher> mDispatcher;
108
109 virtual void SetUp() {
110 mFakePolicy = new FakeInputDispatcherPolicy();
111 mDispatcher = new InputDispatcher(mFakePolicy);
112 }
113
114 virtual void TearDown() {
115 mFakePolicy.clear();
116 mDispatcher.clear();
117 }
118};
119
120
121TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
122 KeyEvent event;
123
124 // Rejects undefined key actions.
125 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
126 /*action*/ -1, 0,
127 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
128 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700129 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700130 << "Should reject key events with undefined action.";
131
132 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
133 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
134 AKEY_EVENT_ACTION_MULTIPLE, 0,
135 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
136 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700137 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700138 << "Should reject key events with ACTION_MULTIPLE.";
139}
140
141TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
142 MotionEvent event;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700143 PointerProperties pointerProperties[MAX_POINTERS + 1];
Jeff Brownc3db8582010-10-20 15:33:38 -0700144 PointerCoords pointerCoords[MAX_POINTERS + 1];
145 for (int i = 0; i <= MAX_POINTERS; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700146 pointerProperties[i].clear();
147 pointerProperties[i].id = i;
148 pointerCoords[i].clear();
Jeff Brownc3db8582010-10-20 15:33:38 -0700149 }
150
151 // Rejects undefined motion actions.
152 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700153 /*action*/ -1, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700154 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700156 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700157 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700158 << "Should reject motion events with undefined action.";
159
160 // Rejects pointer down with invalid index.
161 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
162 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700163 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700164 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700165 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700166 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700167 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700168 << "Should reject motion events with pointer down index too large.";
169
170 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
171 AMOTION_EVENT_ACTION_POINTER_DOWN | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700173 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700174 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700175 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700176 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700177 << "Should reject motion events with pointer down index too small.";
178
179 // Rejects pointer up with invalid index.
180 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
181 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700182 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700183 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700185 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700186 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700187 << "Should reject motion events with pointer up index too large.";
188
189 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
190 AMOTION_EVENT_ACTION_POINTER_UP | (-1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700191 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700192 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700193 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700194 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700195 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700196 << "Should reject motion events with pointer up index too small.";
197
198 // Rejects motion events with invalid number of pointers.
199 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700200 AMOTION_EVENT_ACTION_DOWN, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700201 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700202 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700203 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700204 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700205 << "Should reject motion events with 0 pointers.";
206
207 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700208 AMOTION_EVENT_ACTION_DOWN, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700209 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700210 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700211 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700212 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700213 << "Should reject motion events with more than MAX_POINTERS pointers.";
214
215 // Rejects motion events with invalid pointer ids.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700216 pointerProperties[0].id = -1;
Jeff Brownc3db8582010-10-20 15:33:38 -0700217 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700218 AMOTION_EVENT_ACTION_DOWN, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700219 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700220 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700221 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700222 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700223 << "Should reject motion events with pointer ids less than 0.";
224
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700225 pointerProperties[0].id = MAX_POINTER_ID + 1;
Jeff Brownc3db8582010-10-20 15:33:38 -0700226 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700227 AMOTION_EVENT_ACTION_DOWN, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700228 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700229 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700230 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700231 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700232 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
233
234 // Rejects motion events with duplicate pointer ids.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700235 pointerProperties[0].id = 1;
236 pointerProperties[1].id = 1;
Jeff Brownc3db8582010-10-20 15:33:38 -0700237 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700238 AMOTION_EVENT_ACTION_DOWN, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Jeff Brownc3db8582010-10-20 15:33:38 -0700239 ARBITRARY_TIME, ARBITRARY_TIME,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700240 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownc3db8582010-10-20 15:33:38 -0700241 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(&event,
Jeff Brown0029c662011-03-30 02:25:18 -0700242 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
Jeff Brownc3db8582010-10-20 15:33:38 -0700243 << "Should reject motion events with duplicate pointer ids.";
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700244}
245
246} // namespace android