Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace android { |
| 23 | |
| 24 | // An arbitrary time value. |
| 25 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 26 | |
| 27 | // An arbitrary device id. |
| 28 | static const int32_t DEVICE_ID = 1; |
| 29 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 30 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 31 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 32 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 34 | static const int32_t INJECTOR_PID = 999; |
| 35 | static const int32_t INJECTOR_UID = 1001; |
| 36 | |
| 37 | |
| 38 | // --- FakeInputDispatcherPolicy --- |
| 39 | |
| 40 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 41 | InputDispatcherConfiguration mConfig; |
| 42 | |
| 43 | protected: |
| 44 | virtual ~FakeInputDispatcherPolicy() { |
| 45 | } |
| 46 | |
| 47 | public: |
| 48 | FakeInputDispatcherPolicy() { |
| 49 | } |
| 50 | |
| 51 | private: |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 52 | virtual void notifyConfigurationChanged(nsecs_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 55 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, |
| 56 | const sp<InputWindowHandle>&, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 57 | const std::string&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 61 | virtual void notifyInputChannelBroken(const sp<InputWindowHandle>&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) { |
| 65 | *outConfig = mConfig; |
| 66 | } |
| 67 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 68 | virtual bool filterInputEvent(const InputEvent*, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 72 | virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 75 | virtual void interceptMotionBeforeQueueing(nsecs_t, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 78 | virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>&, |
| 79 | const KeyEvent*, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 83 | virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>&, |
| 84 | const KeyEvent*, uint32_t, KeyEvent*) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 88 | virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 91 | virtual void pokeUserActivity(nsecs_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 94 | virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | // --- InputDispatcherTest --- |
| 101 | |
| 102 | class InputDispatcherTest : public testing::Test { |
| 103 | protected: |
| 104 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 105 | sp<InputDispatcher> mDispatcher; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 106 | sp<InputDispatcherThread> mDispatcherThread; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | |
| 108 | virtual void SetUp() { |
| 109 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 110 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 111 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 112 | //Start InputDispatcher thread |
| 113 | mDispatcherThread = new InputDispatcherThread(mDispatcher); |
| 114 | mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | virtual void TearDown() { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 118 | mDispatcherThread->requestExit(); |
| 119 | mDispatcherThread.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | mFakePolicy.clear(); |
| 121 | mDispatcher.clear(); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 127 | KeyEvent event; |
| 128 | |
| 129 | // Rejects undefined key actions. |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 130 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 131 | /*action*/ -1, 0, |
| 132 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 133 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 134 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 136 | << "Should reject key events with undefined action."; |
| 137 | |
| 138 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 139 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 140 | AKEY_EVENT_ACTION_MULTIPLE, 0, |
| 141 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 142 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 143 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 145 | << "Should reject key events with ACTION_MULTIPLE."; |
| 146 | } |
| 147 | |
| 148 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 149 | MotionEvent event; |
| 150 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 151 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 152 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 153 | pointerProperties[i].clear(); |
| 154 | pointerProperties[i].id = i; |
| 155 | pointerCoords[i].clear(); |
| 156 | } |
| 157 | |
| 158 | // Rejects undefined motion actions. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 159 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 160 | /*action*/ -1, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 161 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 162 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 163 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 164 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 166 | << "Should reject motion events with undefined action."; |
| 167 | |
| 168 | // Rejects pointer down with invalid index. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 169 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 170 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 171 | 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 173 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 174 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 175 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 177 | << "Should reject motion events with pointer down index too large."; |
| 178 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 179 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Dan Albert | 8b10c65 | 2016-02-02 17:08:05 -0800 | [diff] [blame] | 180 | AMOTION_EVENT_ACTION_POINTER_DOWN | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 181 | 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 182 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 183 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 184 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 185 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 187 | << "Should reject motion events with pointer down index too small."; |
| 188 | |
| 189 | // Rejects pointer up with invalid index. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 190 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 192 | 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 193 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 194 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 195 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 196 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 197 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 198 | << "Should reject motion events with pointer up index too large."; |
| 199 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 200 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Dan Albert | 8b10c65 | 2016-02-02 17:08:05 -0800 | [diff] [blame] | 201 | AMOTION_EVENT_ACTION_POINTER_UP | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 202 | 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 204 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 205 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 206 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 207 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 208 | << "Should reject motion events with pointer up index too small."; |
| 209 | |
| 210 | // Rejects motion events with invalid number of pointers. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 211 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 212 | AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 213 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 214 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 215 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 216 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 217 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 218 | << "Should reject motion events with 0 pointers."; |
| 219 | |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 220 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 221 | AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 222 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 223 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 224 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 225 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 226 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 227 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 228 | |
| 229 | // Rejects motion events with invalid pointer ids. |
| 230 | pointerProperties[0].id = -1; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 231 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 232 | AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 233 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 234 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 235 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 236 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 237 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 238 | << "Should reject motion events with pointer ids less than 0."; |
| 239 | |
| 240 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 241 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 242 | AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 243 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 244 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 245 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 246 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 248 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 249 | |
| 250 | // Rejects motion events with duplicate pointer ids. |
| 251 | pointerProperties[0].id = 1; |
| 252 | pointerProperties[1].id = 1; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 253 | event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 254 | AMOTION_EVENT_ACTION_DOWN, 0, 0, 0, AMETA_NONE, 0, 0, 0, 0, 0, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 255 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 256 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 257 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 258 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 260 | << "Should reject motion events with duplicate pointer ids."; |
| 261 | } |
| 262 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 263 | // --- InputDispatcherTest SetInputWindowTest --- |
| 264 | static const int32_t INJECT_EVENT_TIMEOUT = 500; |
| 265 | static const int32_t DISPATCHING_TIMEOUT = 100; |
| 266 | |
| 267 | class FakeApplicationHandle : public InputApplicationHandle { |
| 268 | public: |
| 269 | FakeApplicationHandle() {} |
| 270 | virtual ~FakeApplicationHandle() {} |
| 271 | |
| 272 | virtual bool updateInfo() { |
| 273 | if (!mInfo) { |
| 274 | mInfo = new InputApplicationInfo(); |
| 275 | } |
| 276 | mInfo->dispatchingTimeout = DISPATCHING_TIMEOUT; |
| 277 | return true; |
| 278 | } |
| 279 | }; |
| 280 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 281 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 282 | public: |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 283 | void consumeEvent(int32_t expectedEventType, int32_t expectedDisplayId, |
| 284 | int32_t expectedFlags = 0) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 285 | uint32_t consumeSeq; |
| 286 | InputEvent* event; |
| 287 | status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, |
| 288 | &consumeSeq, &event); |
| 289 | |
| 290 | ASSERT_EQ(OK, status) |
| 291 | << mName.c_str() << ": consumer consume should return OK."; |
| 292 | ASSERT_TRUE(event != nullptr) |
| 293 | << mName.c_str() << ": consumer should have returned non-NULL event."; |
| 294 | ASSERT_EQ(expectedEventType, event->getType()) |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 295 | << mName.c_str() << ": event type should match."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 296 | |
| 297 | ASSERT_EQ(expectedDisplayId, event->getDisplayId()) |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 298 | << mName.c_str() << ": event displayId should be the same as expected."; |
| 299 | |
| 300 | int32_t flags; |
| 301 | switch (expectedEventType) { |
| 302 | case AINPUT_EVENT_TYPE_KEY: { |
| 303 | KeyEvent* typedEvent = static_cast<KeyEvent*>(event); |
| 304 | flags = typedEvent->getFlags(); |
| 305 | break; |
| 306 | } |
| 307 | case AINPUT_EVENT_TYPE_MOTION: { |
| 308 | MotionEvent* typedEvent = static_cast<MotionEvent*>(event); |
| 309 | flags = typedEvent->getFlags(); |
| 310 | break; |
| 311 | } |
| 312 | default: { |
| 313 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 314 | } |
| 315 | } |
| 316 | ASSERT_EQ(expectedFlags, flags) |
| 317 | << mName.c_str() << ": event flags should be the same as expected."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 318 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 319 | status = mConsumer->sendFinishedSignal(consumeSeq, handled()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 320 | ASSERT_EQ(OK, status) |
| 321 | << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
| 322 | } |
| 323 | |
| 324 | void assertNoEvents() { |
| 325 | uint32_t consumeSeq; |
| 326 | InputEvent* event; |
| 327 | status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, |
| 328 | &consumeSeq, &event); |
| 329 | ASSERT_NE(OK, status) |
| 330 | << mName.c_str() |
| 331 | << ": should not have received any events, so consume(..) should not return OK."; |
| 332 | } |
| 333 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 334 | protected: |
| 335 | explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, |
| 336 | const std::string name, int32_t displayId) : |
| 337 | mDispatcher(dispatcher), mName(name), mDisplayId(displayId) { |
| 338 | InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel); |
| 339 | mConsumer = new InputConsumer(mClientChannel); |
| 340 | } |
| 341 | |
| 342 | virtual ~FakeInputReceiver() { |
| 343 | } |
| 344 | |
| 345 | // return true if the event has been handled. |
| 346 | virtual bool handled() { |
| 347 | return false; |
| 348 | } |
| 349 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 350 | sp<InputDispatcher> mDispatcher; |
| 351 | sp<InputChannel> mServerChannel, mClientChannel; |
| 352 | InputConsumer *mConsumer; |
| 353 | PreallocatedInputEventFactory mEventFactory; |
| 354 | |
| 355 | std::string mName; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 356 | int32_t mDisplayId; |
| 357 | }; |
| 358 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 359 | class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver { |
| 360 | public: |
| 361 | static const int32_t WIDTH = 600; |
| 362 | static const int32_t HEIGHT = 800; |
| 363 | |
| 364 | FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle, |
| 365 | const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) : |
| 366 | InputWindowHandle(inputApplicationHandle), |
| 367 | FakeInputReceiver(dispatcher, name, displayId), |
| 368 | mFocused(false) { |
| 369 | mDispatcher->registerInputChannel(mServerChannel, this, displayId); |
| 370 | } |
| 371 | |
| 372 | virtual bool updateInfo() { |
| 373 | if (!mInfo) { |
| 374 | mInfo = new InputWindowInfo(); |
| 375 | } |
| 376 | mInfo->inputChannel = mServerChannel; |
| 377 | mInfo->name = mName; |
| 378 | mInfo->layoutParamsFlags = 0; |
| 379 | mInfo->layoutParamsType = InputWindowInfo::TYPE_APPLICATION; |
| 380 | mInfo->dispatchingTimeout = DISPATCHING_TIMEOUT; |
| 381 | mInfo->frameLeft = 0; |
| 382 | mInfo->frameTop = 0; |
| 383 | mInfo->frameRight = WIDTH; |
| 384 | mInfo->frameBottom = HEIGHT; |
| 385 | mInfo->scaleFactor = 1.0; |
| 386 | mInfo->addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 387 | mInfo->visible = true; |
| 388 | mInfo->canReceiveKeys = true; |
| 389 | mInfo->hasFocus = mFocused; |
| 390 | mInfo->hasWallpaper = false; |
| 391 | mInfo->paused = false; |
| 392 | mInfo->layer = 0; |
| 393 | mInfo->ownerPid = INJECTOR_PID; |
| 394 | mInfo->ownerUid = INJECTOR_UID; |
| 395 | mInfo->inputFeatures = 0; |
| 396 | mInfo->displayId = mDisplayId; |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | void setFocus() { |
| 402 | mFocused = true; |
| 403 | } |
| 404 | |
| 405 | void assertNoEvents() { |
| 406 | uint32_t consumeSeq; |
| 407 | InputEvent* event; |
| 408 | status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, |
| 409 | &consumeSeq, &event); |
| 410 | ASSERT_NE(OK, status) |
| 411 | << mName.c_str() |
| 412 | << ": should not have received any events, so consume(..) should not return OK."; |
| 413 | } |
| 414 | protected: |
| 415 | virtual bool handled() { |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | bool mFocused; |
| 420 | }; |
| 421 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 422 | static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 423 | int32_t displayId = ADISPLAY_ID_NONE) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 424 | KeyEvent event; |
| 425 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 426 | |
| 427 | // Define a valid key down event. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 428 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 429 | AKEY_EVENT_ACTION_DOWN, /* flags */ 0, |
| 430 | AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime); |
| 431 | |
| 432 | // Inject event until dispatch out. |
| 433 | return dispatcher->injectInputEvent( |
| 434 | &event, |
| 435 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 436 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 437 | } |
| 438 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 439 | static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 440 | int32_t displayId) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 441 | MotionEvent event; |
| 442 | PointerProperties pointerProperties[1]; |
| 443 | PointerCoords pointerCoords[1]; |
| 444 | |
| 445 | pointerProperties[0].clear(); |
| 446 | pointerProperties[0].id = 0; |
| 447 | pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 448 | |
| 449 | pointerCoords[0].clear(); |
| 450 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); |
| 451 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200); |
| 452 | |
| 453 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 454 | // Define a valid motion down event. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 455 | event.initialize(DEVICE_ID, source, displayId, |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 456 | AMOTION_EVENT_ACTION_DOWN, /* actionButton */0, /* flags */ 0, /* edgeFlags */ 0, |
| 457 | AMETA_NONE, /* buttonState */ 0, /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0, |
| 458 | /* yPrecision */ 0, currentTime, currentTime, /*pointerCount*/ 1, pointerProperties, |
| 459 | pointerCoords); |
| 460 | |
| 461 | // Inject event until dispatch out. |
| 462 | return dispatcher->injectInputEvent( |
| 463 | &event, |
| 464 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 465 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 466 | } |
| 467 | |
| 468 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
| 469 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 470 | sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", |
| 471 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 472 | |
| 473 | Vector<sp<InputWindowHandle>> inputWindowHandles; |
| 474 | inputWindowHandles.add(window); |
| 475 | |
| 476 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 477 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 478 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 479 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 480 | |
| 481 | // Window should receive motion event. |
| 482 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); |
| 483 | } |
| 484 | |
| 485 | // The foreground window should receive the first touch down event. |
| 486 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
| 487 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 488 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 489 | ADISPLAY_ID_DEFAULT); |
| 490 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 491 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 492 | |
| 493 | Vector<sp<InputWindowHandle>> inputWindowHandles; |
| 494 | inputWindowHandles.add(windowTop); |
| 495 | inputWindowHandles.add(windowSecond); |
| 496 | |
| 497 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 498 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 499 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 500 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 501 | |
| 502 | // Top window should receive the touch down event. Second window should not receive anything. |
| 503 | windowTop->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); |
| 504 | windowSecond->assertNoEvents(); |
| 505 | } |
| 506 | |
| 507 | TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) { |
| 508 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 509 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 510 | ADISPLAY_ID_DEFAULT); |
| 511 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 512 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 513 | |
| 514 | // Set focus application. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 515 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 516 | |
| 517 | // Expect one focus window exist in display. |
| 518 | windowSecond->setFocus(); |
| 519 | Vector<sp<InputWindowHandle>> inputWindowHandles; |
| 520 | inputWindowHandles.add(windowTop); |
| 521 | inputWindowHandles.add(windowSecond); |
| 522 | |
| 523 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
| 524 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 525 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 526 | |
| 527 | // Focused window should receive event. |
| 528 | windowTop->assertNoEvents(); |
| 529 | windowSecond->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); |
| 530 | } |
| 531 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 532 | /* Test InputDispatcher for MultiDisplay */ |
| 533 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 534 | public: |
| 535 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 536 | virtual void SetUp() { |
| 537 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 538 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 539 | application1 = new FakeApplicationHandle(); |
| 540 | windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", |
| 541 | ADISPLAY_ID_DEFAULT); |
| 542 | Vector<sp<InputWindowHandle>> inputWindowHandles; |
| 543 | inputWindowHandles.push(windowInPrimary); |
| 544 | // Set focus window for primary display, but focused display would be second one. |
| 545 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
| 546 | windowInPrimary->setFocus(); |
| 547 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 548 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 549 | application2 = new FakeApplicationHandle(); |
| 550 | windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", |
| 551 | SECOND_DISPLAY_ID); |
| 552 | // Set focus to second display window. |
| 553 | Vector<sp<InputWindowHandle>> inputWindowHandles_Second; |
| 554 | inputWindowHandles_Second.push(windowInSecondary); |
| 555 | // Set focus display to second one. |
| 556 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 557 | // Set focus window for second display. |
| 558 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
| 559 | windowInSecondary->setFocus(); |
| 560 | mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID); |
| 561 | } |
| 562 | |
| 563 | virtual void TearDown() { |
| 564 | InputDispatcherTest::TearDown(); |
| 565 | |
| 566 | application1.clear(); |
| 567 | windowInPrimary.clear(); |
| 568 | application2.clear(); |
| 569 | windowInSecondary.clear(); |
| 570 | } |
| 571 | |
| 572 | protected: |
| 573 | sp<FakeApplicationHandle> application1; |
| 574 | sp<FakeWindowHandle> windowInPrimary; |
| 575 | sp<FakeApplicationHandle> application2; |
| 576 | sp<FakeWindowHandle> windowInSecondary; |
| 577 | }; |
| 578 | |
| 579 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 580 | // Test touch down on primary display. |
| 581 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 582 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 583 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 584 | windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); |
| 585 | windowInSecondary->assertNoEvents(); |
| 586 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 587 | // Test touch down on second display. |
| 588 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 589 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 590 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 591 | windowInPrimary->assertNoEvents(); |
| 592 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); |
| 593 | } |
| 594 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 595 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 596 | // Test inject a key down with display id specified. |
| 597 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 598 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 599 | windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_DEFAULT); |
| 600 | windowInSecondary->assertNoEvents(); |
| 601 | |
| 602 | // Test inject a key down without display id specified. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 603 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 604 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 605 | windowInPrimary->assertNoEvents(); |
| 606 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); |
| 607 | |
| 608 | // Remove secondary display. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 609 | Vector<sp<InputWindowHandle>> noWindows; |
| 610 | mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 611 | |
| 612 | // Expect old focus should receive a cancel event. |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 613 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE, |
| 614 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 615 | |
| 616 | // Test inject a key down, should timeout because of no target window. |
| 617 | ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) |
| 618 | << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; |
| 619 | windowInPrimary->assertNoEvents(); |
| 620 | windowInSecondary->assertNoEvents(); |
| 621 | } |
| 622 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 623 | class FakeMonitorReceiver : public FakeInputReceiver, public RefBase { |
| 624 | public: |
| 625 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
| 626 | int32_t displayId) : FakeInputReceiver(dispatcher, name, displayId) { |
| 627 | mDispatcher->registerInputChannel(mServerChannel, nullptr, displayId); |
| 628 | } |
| 629 | }; |
| 630 | |
| 631 | // Test per-display input monitors for motion event. |
| 632 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
| 633 | sp<FakeMonitorReceiver> monitorInPrimary = |
| 634 | new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 635 | sp<FakeMonitorReceiver> monitorInSecondary = |
| 636 | new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 637 | |
| 638 | // Test touch down on primary display. |
| 639 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 640 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 641 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 642 | windowInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); |
| 643 | monitorInPrimary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_DEFAULT); |
| 644 | windowInSecondary->assertNoEvents(); |
| 645 | monitorInSecondary->assertNoEvents(); |
| 646 | |
| 647 | // Test touch down on second display. |
| 648 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 649 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 650 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 651 | windowInPrimary->assertNoEvents(); |
| 652 | monitorInPrimary->assertNoEvents(); |
| 653 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); |
| 654 | monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, SECOND_DISPLAY_ID); |
| 655 | |
| 656 | // Test inject a non-pointer motion event. |
| 657 | // If specific a display, it will dispatch to the focused window of particular display, |
| 658 | // or it will dispatch to the focused window of focused display. |
| 659 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 660 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 661 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 662 | windowInPrimary->assertNoEvents(); |
| 663 | monitorInPrimary->assertNoEvents(); |
| 664 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE); |
| 665 | monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_MOTION, ADISPLAY_ID_NONE); |
| 666 | } |
| 667 | |
| 668 | // Test per-display input monitors for key event. |
| 669 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
| 670 | //Input monitor per display. |
| 671 | sp<FakeMonitorReceiver> monitorInPrimary = |
| 672 | new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 673 | sp<FakeMonitorReceiver> monitorInSecondary = |
| 674 | new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 675 | |
| 676 | // Test inject a key down. |
| 677 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 678 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 679 | windowInPrimary->assertNoEvents(); |
| 680 | monitorInPrimary->assertNoEvents(); |
| 681 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); |
| 682 | monitorInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, ADISPLAY_ID_NONE); |
| 683 | } |
| 684 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 685 | } // namespace android |