blob: 9c72c77846a923bc18d6243b9e33882ecb28f305 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 */
16
17#include "../InputDispatcher.h"
18
19#include <gtest/gtest.h>
20#include <linux/input.h>
21
22namespace android {
23
24// 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
Jeff Brownf086ddb2014-02-11 14:28:48 -080030// An arbitrary display id.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -080031static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Jeff Brownf086ddb2014-02-11 14:28:48 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033// An arbitrary injector pid / uid pair that has permission to inject events.
34static const int32_t INJECTOR_PID = 999;
35static const int32_t INJECTOR_UID = 1001;
36
37
38// --- FakeInputDispatcherPolicy ---
39
40class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
41 InputDispatcherConfiguration mConfig;
42
43protected:
44 virtual ~FakeInputDispatcherPolicy() {
45 }
46
47public:
48 FakeInputDispatcherPolicy() {
49 }
50
51private:
Narayan Kamath39efe3e2014-10-17 10:37:08 +010052 virtual void notifyConfigurationChanged(nsecs_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080053 }
54
Narayan Kamath39efe3e2014-10-17 10:37:08 +010055 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&,
56 const sp<InputWindowHandle>&,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080057 const std::string&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080058 return 0;
59 }
60
Narayan Kamath39efe3e2014-10-17 10:37:08 +010061 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080062 }
63
64 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
65 *outConfig = mConfig;
66 }
67
Narayan Kamath39efe3e2014-10-17 10:37:08 +010068 virtual bool filterInputEvent(const InputEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080069 return true;
70 }
71
Narayan Kamath39efe3e2014-10-17 10:37:08 +010072 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080073 }
74
Narayan Kamath39efe3e2014-10-17 10:37:08 +010075 virtual void interceptMotionBeforeQueueing(nsecs_t, uint32_t&) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080076 }
77
Narayan Kamath39efe3e2014-10-17 10:37:08 +010078 virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>&,
79 const KeyEvent*, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080080 return 0;
81 }
82
Narayan Kamath39efe3e2014-10-17 10:37:08 +010083 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>&,
84 const KeyEvent*, uint32_t, KeyEvent*) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080085 return false;
86 }
87
Narayan Kamath39efe3e2014-10-17 10:37:08 +010088 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080089 }
90
Narayan Kamath39efe3e2014-10-17 10:37:08 +010091 virtual void pokeUserActivity(nsecs_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 }
93
Narayan Kamath39efe3e2014-10-17 10:37:08 +010094 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080095 return false;
96 }
97};
98
99
100// --- InputDispatcherTest ---
101
102class InputDispatcherTest : public testing::Test {
103protected:
104 sp<FakeInputDispatcherPolicy> mFakePolicy;
105 sp<InputDispatcher> mDispatcher;
106
107 virtual void SetUp() {
108 mFakePolicy = new FakeInputDispatcherPolicy();
109 mDispatcher = new InputDispatcher(mFakePolicy);
110 }
111
112 virtual void TearDown() {
113 mFakePolicy.clear();
114 mDispatcher.clear();
115 }
116};
117
118
119TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) {
120 KeyEvent event;
121
122 // Rejects undefined key actions.
123 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
124 /*action*/ -1, 0,
125 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800126 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800127 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
129 << "Should reject key events with undefined action.";
130
131 // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API.
132 event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD,
133 AKEY_EVENT_ACTION_MULTIPLE, 0,
134 AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800135 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800136 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
138 << "Should reject key events with ACTION_MULTIPLE.";
139}
140
141TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) {
142 MotionEvent event;
143 PointerProperties pointerProperties[MAX_POINTERS + 1];
144 PointerCoords pointerCoords[MAX_POINTERS + 1];
145 for (int i = 0; i <= MAX_POINTERS; i++) {
146 pointerProperties[i].clear();
147 pointerProperties[i].id = i;
148 pointerCoords[i].clear();
149 }
150
151 // Rejects undefined motion actions.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800152 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100153 /*action*/ -1, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 ARBITRARY_TIME, ARBITRARY_TIME,
155 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800156 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800157 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
159 << "Should reject motion events with undefined action.";
160
161 // Rejects pointer down with invalid index.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800162 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Michael Wright7b159c92015-05-14 14:48:03 +0100164 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 ARBITRARY_TIME, ARBITRARY_TIME,
166 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800167 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800168 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
170 << "Should reject motion events with pointer down index too large.";
171
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800172 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Dan Albert8b10c652016-02-02 17:08:05 -0800173 AMOTION_EVENT_ACTION_POINTER_DOWN | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Michael Wright7b159c92015-05-14 14:48:03 +0100174 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 ARBITRARY_TIME, ARBITRARY_TIME,
176 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800177 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800178 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
180 << "Should reject motion events with pointer down index too small.";
181
182 // Rejects pointer up with invalid index.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800183 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Michael Wright7b159c92015-05-14 14:48:03 +0100185 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 ARBITRARY_TIME, ARBITRARY_TIME,
187 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800188 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800189 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
191 << "Should reject motion events with pointer up index too large.";
192
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800193 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Dan Albert8b10c652016-02-02 17:08:05 -0800194 AMOTION_EVENT_ACTION_POINTER_UP | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT),
Michael Wright7b159c92015-05-14 14:48:03 +0100195 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 ARBITRARY_TIME, ARBITRARY_TIME,
197 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800198 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800199 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
201 << "Should reject motion events with pointer up index too small.";
202
203 // Rejects motion events with invalid number of pointers.
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800204 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100205 AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 ARBITRARY_TIME, ARBITRARY_TIME,
207 /*pointerCount*/ 0, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800208 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800209 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
211 << "Should reject motion events with 0 pointers.";
212
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800213 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100214 AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 ARBITRARY_TIME, ARBITRARY_TIME,
216 /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800217 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800218 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
220 << "Should reject motion events with more than MAX_POINTERS pointers.";
221
222 // Rejects motion events with invalid pointer ids.
223 pointerProperties[0].id = -1;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800224 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100225 AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 ARBITRARY_TIME, ARBITRARY_TIME,
227 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800228 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800229 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
231 << "Should reject motion events with pointer ids less than 0.";
232
233 pointerProperties[0].id = MAX_POINTER_ID + 1;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800234 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100235 AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236 ARBITRARY_TIME, ARBITRARY_TIME,
237 /*pointerCount*/ 1, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800238 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800239 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
241 << "Should reject motion events with pointer ids greater than MAX_POINTER_ID.";
242
243 // Rejects motion events with duplicate pointer ids.
244 pointerProperties[0].id = 1;
245 pointerProperties[1].id = 1;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800246 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID,
Michael Wright7b159c92015-05-14 14:48:03 +0100247 AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 ARBITRARY_TIME, ARBITRARY_TIME,
249 /*pointerCount*/ 2, pointerProperties, pointerCoords);
Jeff Brownf086ddb2014-02-11 14:28:48 -0800250 ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent(
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800251 &event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0))
253 << "Should reject motion events with duplicate pointer ids.";
254}
255
256} // namespace android