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