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 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 18 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 19 | #include <InputDispatcherThread.h> |
| 20 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
| 22 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | #include <linux/input.h> |
| 25 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 26 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 27 | |
| 28 | // An arbitrary time value. |
| 29 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 30 | |
| 31 | // An arbitrary device id. |
| 32 | static const int32_t DEVICE_ID = 1; |
| 33 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 34 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 35 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 36 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 38 | static const int32_t INJECTOR_PID = 999; |
| 39 | static const int32_t INJECTOR_UID = 1001; |
| 40 | |
| 41 | |
| 42 | // --- FakeInputDispatcherPolicy --- |
| 43 | |
| 44 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 45 | InputDispatcherConfiguration mConfig; |
| 46 | |
| 47 | protected: |
| 48 | virtual ~FakeInputDispatcherPolicy() { |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | FakeInputDispatcherPolicy() { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 53 | mOnPointerDownToken.clear(); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 56 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
| 57 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 58 | ASSERT_EQ(mFilteredEvent->getType(), AINPUT_EVENT_TYPE_KEY); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 59 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 60 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); |
| 61 | ASSERT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 62 | ASSERT_EQ(keyEvent.getAction(), args.action); |
| 63 | ASSERT_EQ(keyEvent.getDisplayId(), args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 64 | |
| 65 | reset(); |
| 66 | } |
| 67 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 68 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { |
| 69 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 70 | ASSERT_EQ(mFilteredEvent->getType(), AINPUT_EVENT_TYPE_MOTION); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 71 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 72 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); |
| 73 | ASSERT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 74 | ASSERT_EQ(motionEvent.getAction(), args.action); |
| 75 | ASSERT_EQ(motionEvent.getDisplayId(), args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 76 | |
| 77 | reset(); |
| 78 | } |
| 79 | |
| 80 | void assertFilterInputEventWasNotCalled() { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 81 | ASSERT_EQ(nullptr, mFilteredEvent) |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 82 | << "Expected filterInputEvent() to not have been called."; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 83 | } |
| 84 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 85 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
| 86 | ASSERT_EQ(mOnPointerDownToken, touchedToken) |
| 87 | << "Expected token from onPointerDownOutsideFocus was not matched"; |
| 88 | reset(); |
| 89 | } |
| 90 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | private: |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 92 | std::unique_ptr<InputEvent> mFilteredEvent; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 93 | sp<IBinder> mOnPointerDownToken; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 94 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 95 | virtual void notifyConfigurationChanged(nsecs_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 98 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 99 | const sp<IBinder>&, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 100 | const std::string&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 104 | virtual void notifyInputChannelBroken(const sp<IBinder>&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 105 | } |
| 106 | |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 107 | virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) { |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 110 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) { |
| 111 | *outConfig = mConfig; |
| 112 | } |
| 113 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 114 | virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 115 | switch (inputEvent->getType()) { |
| 116 | case AINPUT_EVENT_TYPE_KEY: { |
| 117 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 118 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | |
| 122 | case AINPUT_EVENT_TYPE_MOTION: { |
| 123 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 124 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 131 | virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 134 | virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 137 | virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 138 | const KeyEvent*, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 142 | virtual bool dispatchUnhandledKey(const sp<IBinder>&, |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 143 | const KeyEvent*, uint32_t, KeyEvent*) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 147 | virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 150 | virtual void pokeUserActivity(nsecs_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 153 | virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 154 | return false; |
| 155 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 156 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 157 | virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) { |
| 158 | mOnPointerDownToken = newToken; |
| 159 | } |
| 160 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 161 | void reset() { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 162 | mFilteredEvent = nullptr; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 163 | mOnPointerDownToken.clear(); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 164 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | |
| 168 | // --- InputDispatcherTest --- |
| 169 | |
| 170 | class InputDispatcherTest : public testing::Test { |
| 171 | protected: |
| 172 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 173 | sp<InputDispatcher> mDispatcher; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 174 | sp<InputDispatcherThread> mDispatcherThread; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | |
| 176 | virtual void SetUp() { |
| 177 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 178 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 179 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 180 | //Start InputDispatcher thread |
| 181 | mDispatcherThread = new InputDispatcherThread(mDispatcher); |
| 182 | mDispatcherThread->run("InputDispatcherTest", PRIORITY_URGENT_DISPLAY); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | virtual void TearDown() { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 186 | mDispatcherThread->requestExit(); |
| 187 | mDispatcherThread.clear(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 188 | mFakePolicy.clear(); |
| 189 | mDispatcher.clear(); |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 195 | KeyEvent event; |
| 196 | |
| 197 | // Rejects undefined key actions. |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 198 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 | /*action*/ -1, 0, |
| 200 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 201 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 202 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 203 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 204 | << "Should reject key events with undefined action."; |
| 205 | |
| 206 | // 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] | 207 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | AKEY_EVENT_ACTION_MULTIPLE, 0, |
| 209 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 210 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 211 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 213 | << "Should reject key events with ACTION_MULTIPLE."; |
| 214 | } |
| 215 | |
| 216 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 217 | MotionEvent event; |
| 218 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 219 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 220 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 221 | pointerProperties[i].clear(); |
| 222 | pointerProperties[i].id = i; |
| 223 | pointerCoords[i].clear(); |
| 224 | } |
| 225 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 226 | // Some constants commonly used below |
| 227 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 228 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 229 | constexpr int32_t metaState = AMETA_NONE; |
| 230 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 231 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | // Rejects undefined motion actions. |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 233 | event.initialize(DEVICE_ID, source, DISPLAY_ID, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 234 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0, |
| 235 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 236 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 237 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 238 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 239 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 240 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 241 | << "Should reject motion events with undefined action."; |
| 242 | |
| 243 | // Rejects pointer down with invalid index. |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 244 | event.initialize(DEVICE_ID, source, DISPLAY_ID, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 245 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 246 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 247 | 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0, |
| 248 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 249 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 250 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 251 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 252 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 254 | << "Should reject motion events with pointer down index too large."; |
| 255 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 256 | event.initialize(DEVICE_ID, source, DISPLAY_ID, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 257 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 258 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 259 | 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0, |
| 260 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 261 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 262 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 263 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 264 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 265 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 266 | << "Should reject motion events with pointer down index too small."; |
| 267 | |
| 268 | // Rejects pointer up with invalid index. |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 269 | event.initialize(DEVICE_ID, source, DISPLAY_ID, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 270 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 271 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 272 | 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0, |
| 273 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 274 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 275 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 276 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 277 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 279 | << "Should reject motion events with pointer up index too large."; |
| 280 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 281 | event.initialize(DEVICE_ID, source, DISPLAY_ID, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 282 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 283 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 284 | 0, 0, edgeFlags, metaState, 0, classification, 0, 0, 0, 0, |
| 285 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 286 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 287 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 288 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 289 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 290 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 291 | << "Should reject motion events with pointer up index too small."; |
| 292 | |
| 293 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 294 | event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, |
| 295 | metaState, 0, classification, 0, 0, 0, 0, |
| 296 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 297 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 298 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 299 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 300 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 301 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 302 | << "Should reject motion events with 0 pointers."; |
| 303 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 304 | event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, |
| 305 | metaState, 0, classification, 0, 0, 0, 0, |
| 306 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 307 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 308 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 309 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 310 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 311 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 312 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 313 | |
| 314 | // Rejects motion events with invalid pointer ids. |
| 315 | pointerProperties[0].id = -1; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 316 | event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, |
| 317 | metaState, 0, classification, 0, 0, 0, 0, |
| 318 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 319 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 320 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 321 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 322 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 323 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 324 | << "Should reject motion events with pointer ids less than 0."; |
| 325 | |
| 326 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 327 | event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, |
| 328 | metaState, 0, classification, 0, 0, 0, 0, |
| 329 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 330 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 331 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 332 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 333 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 334 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 335 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 336 | |
| 337 | // Rejects motion events with duplicate pointer ids. |
| 338 | pointerProperties[0].id = 1; |
| 339 | pointerProperties[1].id = 1; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 340 | event.initialize(DEVICE_ID, source, DISPLAY_ID, AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, |
| 341 | metaState, 0, classification, 0, 0, 0, 0, |
| 342 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 343 | ARBITRARY_TIME, ARBITRARY_TIME, |
| 344 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 345 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 346 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 347 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 348 | << "Should reject motion events with duplicate pointer ids."; |
| 349 | } |
| 350 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 351 | // --- InputDispatcherTest SetInputWindowTest --- |
| 352 | static const int32_t INJECT_EVENT_TIMEOUT = 500; |
| 353 | static const int32_t DISPATCHING_TIMEOUT = 100; |
| 354 | |
| 355 | class FakeApplicationHandle : public InputApplicationHandle { |
| 356 | public: |
| 357 | FakeApplicationHandle() {} |
| 358 | virtual ~FakeApplicationHandle() {} |
| 359 | |
| 360 | virtual bool updateInfo() { |
Arthur Hung | 7a0c39a | 2019-03-20 16:52:24 +0800 | [diff] [blame] | 361 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 362 | return true; |
| 363 | } |
| 364 | }; |
| 365 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 366 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 367 | public: |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 368 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 369 | int32_t expectedFlags) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 370 | uint32_t consumeSeq; |
| 371 | InputEvent* event; |
| 372 | status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, |
| 373 | &consumeSeq, &event); |
| 374 | |
| 375 | ASSERT_EQ(OK, status) |
| 376 | << mName.c_str() << ": consumer consume should return OK."; |
| 377 | ASSERT_TRUE(event != nullptr) |
| 378 | << mName.c_str() << ": consumer should have returned non-NULL event."; |
| 379 | ASSERT_EQ(expectedEventType, event->getType()) |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 380 | << mName.c_str() << ": event type should match."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 381 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 382 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 383 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 384 | switch (expectedEventType) { |
| 385 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 386 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 387 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
| 388 | EXPECT_EQ(expectedFlags, keyEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 389 | break; |
| 390 | } |
| 391 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 392 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 393 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 394 | EXPECT_EQ(expectedFlags, motionEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | default: { |
| 398 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 399 | } |
| 400 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 401 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 402 | status = mConsumer->sendFinishedSignal(consumeSeq, handled()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 403 | ASSERT_EQ(OK, status) |
| 404 | << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
| 405 | } |
| 406 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 407 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 408 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 409 | expectedFlags); |
| 410 | } |
| 411 | |
| 412 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 413 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 414 | expectedFlags); |
| 415 | } |
| 416 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 417 | void assertNoEvents() { |
| 418 | uint32_t consumeSeq; |
| 419 | InputEvent* event; |
| 420 | status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, |
| 421 | &consumeSeq, &event); |
| 422 | ASSERT_NE(OK, status) |
| 423 | << mName.c_str() |
| 424 | << ": should not have received any events, so consume(..) should not return OK."; |
| 425 | } |
| 426 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 427 | protected: |
| 428 | explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, |
| 429 | const std::string name, int32_t displayId) : |
| 430 | mDispatcher(dispatcher), mName(name), mDisplayId(displayId) { |
| 431 | InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel); |
| 432 | mConsumer = new InputConsumer(mClientChannel); |
| 433 | } |
| 434 | |
| 435 | virtual ~FakeInputReceiver() { |
| 436 | } |
| 437 | |
| 438 | // return true if the event has been handled. |
| 439 | virtual bool handled() { |
| 440 | return false; |
| 441 | } |
| 442 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 443 | sp<InputDispatcher> mDispatcher; |
| 444 | sp<InputChannel> mServerChannel, mClientChannel; |
| 445 | InputConsumer *mConsumer; |
| 446 | PreallocatedInputEventFactory mEventFactory; |
| 447 | |
| 448 | std::string mName; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 449 | int32_t mDisplayId; |
| 450 | }; |
| 451 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 452 | class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver { |
| 453 | public: |
| 454 | static const int32_t WIDTH = 600; |
| 455 | static const int32_t HEIGHT = 800; |
| 456 | |
| 457 | FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle, |
| 458 | const sp<InputDispatcher>& dispatcher, const std::string name, int32_t displayId) : |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 459 | FakeInputReceiver(dispatcher, name, displayId), |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 460 | mFocused(false), mFrame(Rect(0, 0, WIDTH, HEIGHT)), mLayoutParamFlags(0) { |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 461 | mDispatcher->registerInputChannel(mServerChannel); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 462 | |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 463 | inputApplicationHandle->updateInfo(); |
| 464 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | virtual bool updateInfo() { |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 468 | mInfo.token = mServerChannel ? mServerChannel->getConnectionToken() : nullptr; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 469 | mInfo.name = mName; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 470 | mInfo.layoutParamsFlags = mLayoutParamFlags; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 471 | mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION; |
| 472 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 473 | mInfo.frameLeft = mFrame.left; |
| 474 | mInfo.frameTop = mFrame.top; |
| 475 | mInfo.frameRight = mFrame.right; |
| 476 | mInfo.frameBottom = mFrame.bottom; |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 477 | mInfo.globalScaleFactor = 1.0; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 478 | mInfo.touchableRegion.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 479 | mInfo.addTouchableRegion(mFrame); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 480 | mInfo.visible = true; |
| 481 | mInfo.canReceiveKeys = true; |
| 482 | mInfo.hasFocus = mFocused; |
| 483 | mInfo.hasWallpaper = false; |
| 484 | mInfo.paused = false; |
| 485 | mInfo.layer = 0; |
| 486 | mInfo.ownerPid = INJECTOR_PID; |
| 487 | mInfo.ownerUid = INJECTOR_UID; |
| 488 | mInfo.inputFeatures = 0; |
| 489 | mInfo.displayId = mDisplayId; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 490 | |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | void setFocus() { |
| 495 | mFocused = true; |
| 496 | } |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 497 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 498 | void setFrame(const Rect& frame) { |
| 499 | mFrame.set(frame); |
| 500 | } |
| 501 | |
| 502 | void setLayoutParamFlags(int32_t flags) { |
| 503 | mLayoutParamFlags = flags; |
| 504 | } |
| 505 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 506 | void releaseChannel() { |
Arthur Hung | 6b5a2b9 | 2019-01-31 16:39:28 +0800 | [diff] [blame] | 507 | mServerChannel.clear(); |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 508 | InputWindowHandle::releaseChannel(); |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 509 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 510 | protected: |
| 511 | virtual bool handled() { |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | bool mFocused; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 516 | Rect mFrame; |
| 517 | int32_t mLayoutParamFlags; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 518 | }; |
| 519 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 520 | static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 521 | int32_t displayId = ADISPLAY_ID_NONE) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 522 | KeyEvent event; |
| 523 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 524 | |
| 525 | // Define a valid key down event. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 526 | event.initialize(DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 527 | AKEY_EVENT_ACTION_DOWN, /* flags */ 0, |
| 528 | AKEYCODE_A, KEY_A, AMETA_NONE, /* repeatCount */ 0, currentTime, currentTime); |
| 529 | |
| 530 | // Inject event until dispatch out. |
| 531 | return dispatcher->injectInputEvent( |
| 532 | &event, |
| 533 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 534 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 535 | } |
| 536 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 537 | static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action, |
| 538 | int32_t source, int32_t displayId, int32_t x, int32_t y, |
| 539 | int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 540 | int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 541 | MotionEvent event; |
| 542 | PointerProperties pointerProperties[1]; |
| 543 | PointerCoords pointerCoords[1]; |
| 544 | |
| 545 | pointerProperties[0].clear(); |
| 546 | pointerProperties[0].id = 0; |
| 547 | pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 548 | |
| 549 | pointerCoords[0].clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 550 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 551 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 552 | |
| 553 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 554 | // Define a valid motion down event. |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 555 | event.initialize(DEVICE_ID, source, displayId, action, /* actionButton */ 0, /* flags */ 0, |
| 556 | /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
| 557 | /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0, |
| 558 | /* yPrecision */ 0, xCursorPosition, yCursorPosition, currentTime, currentTime, |
| 559 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 560 | |
| 561 | // Inject event until dispatch out. |
| 562 | return dispatcher->injectInputEvent( |
| 563 | &event, |
| 564 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 565 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 566 | } |
| 567 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 568 | static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 569 | int32_t displayId, int32_t x = 100, int32_t y = 200) { |
| 570 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y); |
| 571 | } |
| 572 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 573 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 574 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 575 | // Define a valid key event. |
| 576 | NotifyKeyArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 577 | displayId, POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, |
| 578 | AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
| 579 | |
| 580 | return args; |
| 581 | } |
| 582 | |
| 583 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 584 | PointerProperties pointerProperties[1]; |
| 585 | PointerCoords pointerCoords[1]; |
| 586 | |
| 587 | pointerProperties[0].clear(); |
| 588 | pointerProperties[0].id = 0; |
| 589 | pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 590 | |
| 591 | pointerCoords[0].clear(); |
| 592 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); |
| 593 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 200); |
| 594 | |
| 595 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 596 | // Define a valid motion event. |
| 597 | NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 598 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 599 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
Atif Niyaz | 21da0ff | 2019-06-28 13:22:51 -0700 | [diff] [blame] | 600 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, pointerProperties, pointerCoords, |
| 601 | /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 602 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 603 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 604 | |
| 605 | return args; |
| 606 | } |
| 607 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 608 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
| 609 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 610 | sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", |
| 611 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 612 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 613 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 614 | inputWindowHandles.push_back(window); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 615 | |
| 616 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 617 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 618 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 619 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 620 | |
| 621 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 622 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // The foreground window should receive the first touch down event. |
| 626 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
| 627 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 628 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 629 | ADISPLAY_ID_DEFAULT); |
| 630 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 631 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 632 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 633 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 634 | inputWindowHandles.push_back(windowTop); |
| 635 | inputWindowHandles.push_back(windowSecond); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 636 | |
| 637 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 638 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 639 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 640 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 641 | |
| 642 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 643 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 644 | windowSecond->assertNoEvents(); |
| 645 | } |
| 646 | |
| 647 | TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) { |
| 648 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 649 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 650 | ADISPLAY_ID_DEFAULT); |
| 651 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 652 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 653 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 654 | // Set focused application. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 655 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 656 | |
| 657 | // Expect one focus window exist in display. |
| 658 | windowSecond->setFocus(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 659 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 660 | inputWindowHandles.push_back(windowTop); |
| 661 | inputWindowHandles.push_back(windowSecond); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 662 | |
| 663 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
| 664 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 665 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 666 | |
| 667 | // Focused window should receive event. |
| 668 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 669 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 670 | } |
| 671 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 672 | TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) { |
| 673 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 674 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 675 | ADISPLAY_ID_DEFAULT); |
| 676 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 677 | ADISPLAY_ID_DEFAULT); |
| 678 | |
| 679 | // Set focused application. |
| 680 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 681 | |
| 682 | // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first) |
| 683 | windowTop->setFocus(); |
| 684 | windowSecond->setFocus(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 685 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 686 | inputWindowHandles.push_back(windowTop); |
| 687 | inputWindowHandles.push_back(windowSecond); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 688 | |
| 689 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
| 690 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 691 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 692 | |
| 693 | // Top focused window should receive event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 694 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 695 | windowSecond->assertNoEvents(); |
| 696 | } |
| 697 | |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 698 | TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) { |
| 699 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 700 | |
| 701 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 702 | ADISPLAY_ID_DEFAULT); |
| 703 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 704 | ADISPLAY_ID_DEFAULT); |
| 705 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 706 | // Set focused application. |
| 707 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 708 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 709 | windowTop->setFocus(); |
| 710 | windowSecond->setFocus(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 711 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 712 | inputWindowHandles.push_back(windowTop); |
| 713 | inputWindowHandles.push_back(windowSecond); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 714 | // Release channel for window is no longer valid. |
| 715 | windowTop->releaseChannel(); |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 716 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 717 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 718 | // Test inject a key down, should dispatch to a valid window. |
| 719 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 720 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 721 | |
| 722 | // Top window is invalid, so it should not receive any input event. |
| 723 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 724 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 725 | } |
| 726 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 727 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
| 728 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 729 | |
| 730 | sp<FakeWindowHandle> windowLeft = |
| 731 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 732 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
| 733 | windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 734 | sp<FakeWindowHandle> windowRight = |
| 735 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 736 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
| 737 | windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 738 | |
| 739 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 740 | |
| 741 | std::vector<sp<InputWindowHandle>> inputWindowHandles{windowLeft, windowRight}; |
| 742 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
| 743 | |
| 744 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 745 | // left window. This event should be dispatched to the left window. |
| 746 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 747 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
| 748 | ADISPLAY_ID_DEFAULT, 610, 400, 599, 400)); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 749 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 750 | windowRight->assertNoEvents(); |
| 751 | } |
| 752 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 753 | /* Test InputDispatcher for MultiDisplay */ |
| 754 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 755 | public: |
| 756 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 757 | virtual void SetUp() { |
| 758 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 759 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 760 | application1 = new FakeApplicationHandle(); |
| 761 | windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", |
| 762 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 763 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 764 | inputWindowHandles.push_back(windowInPrimary); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 765 | // Set focus window for primary display, but focused display would be second one. |
| 766 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
| 767 | windowInPrimary->setFocus(); |
| 768 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 769 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 770 | application2 = new FakeApplicationHandle(); |
| 771 | windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", |
| 772 | SECOND_DISPLAY_ID); |
| 773 | // Set focus to second display window. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 774 | std::vector<sp<InputWindowHandle>> inputWindowHandles_Second; |
| 775 | inputWindowHandles_Second.push_back(windowInSecondary); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 776 | // Set focus display to second one. |
| 777 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 778 | // Set focus window for second display. |
| 779 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
| 780 | windowInSecondary->setFocus(); |
| 781 | mDispatcher->setInputWindows(inputWindowHandles_Second, SECOND_DISPLAY_ID); |
| 782 | } |
| 783 | |
| 784 | virtual void TearDown() { |
| 785 | InputDispatcherTest::TearDown(); |
| 786 | |
| 787 | application1.clear(); |
| 788 | windowInPrimary.clear(); |
| 789 | application2.clear(); |
| 790 | windowInSecondary.clear(); |
| 791 | } |
| 792 | |
| 793 | protected: |
| 794 | sp<FakeApplicationHandle> application1; |
| 795 | sp<FakeWindowHandle> windowInPrimary; |
| 796 | sp<FakeApplicationHandle> application2; |
| 797 | sp<FakeWindowHandle> windowInSecondary; |
| 798 | }; |
| 799 | |
| 800 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 801 | // Test touch down on primary display. |
| 802 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 803 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 804 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 805 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 806 | windowInSecondary->assertNoEvents(); |
| 807 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 808 | // Test touch down on second display. |
| 809 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 810 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 811 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 812 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 813 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 814 | } |
| 815 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 816 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 817 | // Test inject a key down with display id specified. |
| 818 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 819 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 820 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 821 | windowInSecondary->assertNoEvents(); |
| 822 | |
| 823 | // Test inject a key down without display id specified. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 824 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 825 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 826 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 827 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 828 | |
| 829 | // Remove secondary display. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 830 | std::vector<sp<InputWindowHandle>> noWindows; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 831 | mDispatcher->setInputWindows(noWindows, SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 832 | |
| 833 | // Expect old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 834 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 835 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 836 | |
| 837 | // Test inject a key down, should timeout because of no target window. |
| 838 | ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) |
| 839 | << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; |
| 840 | windowInPrimary->assertNoEvents(); |
| 841 | windowInSecondary->assertNoEvents(); |
| 842 | } |
| 843 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 844 | class FakeMonitorReceiver : public FakeInputReceiver, public RefBase { |
| 845 | public: |
| 846 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 847 | int32_t displayId, bool isGestureMonitor = false) |
| 848 | : FakeInputReceiver(dispatcher, name, displayId) { |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 849 | mDispatcher->registerInputMonitor(mServerChannel, displayId, isGestureMonitor); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 850 | } |
| 851 | }; |
| 852 | |
| 853 | // Test per-display input monitors for motion event. |
| 854 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
| 855 | sp<FakeMonitorReceiver> monitorInPrimary = |
| 856 | new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 857 | sp<FakeMonitorReceiver> monitorInSecondary = |
| 858 | new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 859 | |
| 860 | // Test touch down on primary display. |
| 861 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 862 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 863 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 864 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 865 | monitorInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 866 | windowInSecondary->assertNoEvents(); |
| 867 | monitorInSecondary->assertNoEvents(); |
| 868 | |
| 869 | // Test touch down on second display. |
| 870 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 871 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 872 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 873 | windowInPrimary->assertNoEvents(); |
| 874 | monitorInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 875 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 876 | monitorInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 877 | |
| 878 | // Test inject a non-pointer motion event. |
| 879 | // If specific a display, it will dispatch to the focused window of particular display, |
| 880 | // or it will dispatch to the focused window of focused display. |
| 881 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 882 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 883 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 884 | windowInPrimary->assertNoEvents(); |
| 885 | monitorInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 886 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
| 887 | monitorInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // Test per-display input monitors for key event. |
| 891 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
| 892 | //Input monitor per display. |
| 893 | sp<FakeMonitorReceiver> monitorInPrimary = |
| 894 | new FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 895 | sp<FakeMonitorReceiver> monitorInSecondary = |
| 896 | new FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 897 | |
| 898 | // Test inject a key down. |
| 899 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 900 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 901 | windowInPrimary->assertNoEvents(); |
| 902 | monitorInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 903 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
| 904 | monitorInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 905 | } |
| 906 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 907 | class InputFilterTest : public InputDispatcherTest { |
| 908 | protected: |
| 909 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 910 | |
| 911 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 912 | NotifyMotionArgs motionArgs; |
| 913 | |
| 914 | motionArgs = generateMotionArgs( |
| 915 | AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 916 | mDispatcher->notifyMotion(&motionArgs); |
| 917 | motionArgs = generateMotionArgs( |
| 918 | AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 919 | mDispatcher->notifyMotion(&motionArgs); |
| 920 | |
| 921 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 922 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 923 | } else { |
| 924 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | void testNotifyKey(bool expectToBeFiltered) { |
| 929 | NotifyKeyArgs keyArgs; |
| 930 | |
| 931 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 932 | mDispatcher->notifyKey(&keyArgs); |
| 933 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 934 | mDispatcher->notifyKey(&keyArgs); |
| 935 | |
| 936 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 937 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 938 | } else { |
| 939 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 940 | } |
| 941 | } |
| 942 | }; |
| 943 | |
| 944 | // Test InputFilter for MotionEvent |
| 945 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 946 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 947 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 948 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 949 | |
| 950 | // Enable InputFilter |
| 951 | mDispatcher->setInputFilterEnabled(true); |
| 952 | // Test touch on both primary and second display, and check if both events are filtered. |
| 953 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 954 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 955 | |
| 956 | // Disable InputFilter |
| 957 | mDispatcher->setInputFilterEnabled(false); |
| 958 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 959 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 960 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 961 | } |
| 962 | |
| 963 | // Test InputFilter for KeyEvent |
| 964 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 965 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 966 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 967 | |
| 968 | // Enable InputFilter |
| 969 | mDispatcher->setInputFilterEnabled(true); |
| 970 | // Send a key event, and check if it is filtered. |
| 971 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 972 | |
| 973 | // Disable InputFilter |
| 974 | mDispatcher->setInputFilterEnabled(false); |
| 975 | // Send a key event, and check if it isn't filtered. |
| 976 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 977 | } |
| 978 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 979 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
| 980 | virtual void SetUp() { |
| 981 | InputDispatcherTest::SetUp(); |
| 982 | |
| 983 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 984 | mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top", |
| 985 | ADISPLAY_ID_DEFAULT); |
| 986 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 987 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 988 | // window. |
| 989 | mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 990 | |
| 991 | mWindowFocused = new FakeWindowHandle(application, mDispatcher, "Second", |
| 992 | ADISPLAY_ID_DEFAULT); |
| 993 | mWindowFocused->setFrame(Rect(50, 50, 100, 100)); |
| 994 | mWindowFocused->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 995 | mWindowFocusedTouchPoint = 60; |
| 996 | |
| 997 | // Set focused application. |
| 998 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 999 | mWindowFocused->setFocus(); |
| 1000 | |
| 1001 | // Expect one focus window exist in display. |
| 1002 | std::vector<sp<InputWindowHandle>> inputWindowHandles; |
| 1003 | inputWindowHandles.push_back(mUnfocusedWindow); |
| 1004 | inputWindowHandles.push_back(mWindowFocused); |
| 1005 | mDispatcher->setInputWindows(inputWindowHandles, ADISPLAY_ID_DEFAULT); |
| 1006 | } |
| 1007 | |
| 1008 | virtual void TearDown() { |
| 1009 | InputDispatcherTest::TearDown(); |
| 1010 | |
| 1011 | mUnfocusedWindow.clear(); |
| 1012 | mWindowFocused.clear(); |
| 1013 | } |
| 1014 | |
| 1015 | protected: |
| 1016 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 1017 | sp<FakeWindowHandle> mWindowFocused; |
| 1018 | int32_t mWindowFocusedTouchPoint; |
| 1019 | }; |
| 1020 | |
| 1021 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1022 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 1023 | // the onPointerDownOutsideFocus callback. |
| 1024 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
| 1025 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1026 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1027 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1028 | // Call monitor to wait for the command queue to get flushed. |
| 1029 | mDispatcher->monitor(); |
| 1030 | |
| 1031 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 1032 | } |
| 1033 | |
| 1034 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 1035 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 1036 | // onPointerDownOutsideFocus callback. |
| 1037 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
| 1038 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1039 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1040 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1041 | // Call monitor to wait for the command queue to get flushed. |
| 1042 | mDispatcher->monitor(); |
| 1043 | |
| 1044 | mFakePolicy->assertOnPointerDownEquals(nullptr); |
| 1045 | } |
| 1046 | |
| 1047 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 1048 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 1049 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
| 1050 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1051 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1052 | // Call monitor to wait for the command queue to get flushed. |
| 1053 | mDispatcher->monitor(); |
| 1054 | |
| 1055 | mFakePolicy->assertOnPointerDownEquals(nullptr); |
| 1056 | } |
| 1057 | |
| 1058 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1059 | // DOWN on the window that already has focus. Ensure no window received the |
| 1060 | // onPointerDownOutsideFocus callback. |
| 1061 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, |
| 1062 | OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
| 1063 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1064 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, mWindowFocusedTouchPoint, |
| 1065 | mWindowFocusedTouchPoint)) |
| 1066 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1067 | // Call monitor to wait for the command queue to get flushed. |
| 1068 | mDispatcher->monitor(); |
| 1069 | |
| 1070 | mFakePolicy->assertOnPointerDownEquals(nullptr); |
| 1071 | } |
| 1072 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1073 | } // namespace android::inputdispatcher |