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 | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 20 | #include <binder/Binder.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 21 | #include <input/Input.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 22 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | #include <linux/input.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 25 | #include <cinttypes> |
| 26 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 27 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 29 | using android::base::StringPrintf; |
| 30 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 31 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 32 | |
| 33 | // An arbitrary time value. |
| 34 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 35 | |
| 36 | // An arbitrary device id. |
| 37 | static const int32_t DEVICE_ID = 1; |
| 38 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 39 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 40 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 41 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 42 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 43 | static const int32_t INJECTOR_PID = 999; |
| 44 | static const int32_t INJECTOR_UID = 1001; |
| 45 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 46 | struct PointF { |
| 47 | float x; |
| 48 | float y; |
| 49 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 50 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 51 | /** |
| 52 | * Return a DOWN key event with KEYCODE_A. |
| 53 | */ |
| 54 | static KeyEvent getTestKeyEvent() { |
| 55 | KeyEvent event; |
| 56 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 57 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 58 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 59 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 60 | return event; |
| 61 | } |
| 62 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 63 | // --- FakeInputDispatcherPolicy --- |
| 64 | |
| 65 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 66 | InputDispatcherConfiguration mConfig; |
| 67 | |
| 68 | protected: |
| 69 | virtual ~FakeInputDispatcherPolicy() { |
| 70 | } |
| 71 | |
| 72 | public: |
| 73 | FakeInputDispatcherPolicy() { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 76 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 77 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, |
| 78 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 81 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 82 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, |
| 83 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 86 | void assertFilterInputEventWasNotCalled() { ASSERT_EQ(nullptr, mFilteredEvent); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 87 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 88 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
| 89 | ASSERT_TRUE(mConfigurationChangedTime) |
| 90 | << "Timed out waiting for configuration changed call"; |
| 91 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 92 | mConfigurationChangedTime = std::nullopt; |
| 93 | } |
| 94 | |
| 95 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
| 96 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 97 | // We do not check id because it is not exposed to the policy |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 98 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 99 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 100 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 101 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 102 | mLastNotifySwitch = std::nullopt; |
| 103 | } |
| 104 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 105 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 106 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 107 | mOnPointerDownToken.clear(); |
| 108 | } |
| 109 | |
| 110 | void assertOnPointerDownWasNotCalled() { |
| 111 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 112 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 115 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 116 | mConfig.keyRepeatTimeout = timeout; |
| 117 | mConfig.keyRepeatDelay = delay; |
| 118 | } |
| 119 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | private: |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 121 | std::unique_ptr<InputEvent> mFilteredEvent; |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 122 | std::optional<nsecs_t> mConfigurationChangedTime; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 123 | sp<IBinder> mOnPointerDownToken; |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 124 | std::optional<NotifySwitchArgs> mLastNotifySwitch; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 125 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 126 | virtual void notifyConfigurationChanged(nsecs_t when) override { |
| 127 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 130 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 131 | const sp<IBinder>&, |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 132 | const std::string&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 136 | virtual void notifyInputChannelBroken(const sp<IBinder>&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | } |
| 138 | |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 139 | virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) { |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 142 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) { |
| 143 | *outConfig = mConfig; |
| 144 | } |
| 145 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 146 | virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 147 | switch (inputEvent->getType()) { |
| 148 | case AINPUT_EVENT_TYPE_KEY: { |
| 149 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 150 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
| 154 | case AINPUT_EVENT_TYPE_MOTION: { |
| 155 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 156 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 163 | virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 166 | virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 169 | virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 170 | const KeyEvent*, uint32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 174 | virtual bool dispatchUnhandledKey(const sp<IBinder>&, |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 175 | const KeyEvent*, uint32_t, KeyEvent*) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 179 | virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 180 | uint32_t policyFlags) override { |
| 181 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 182 | * essentially a passthrough for notifySwitch. |
| 183 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 184 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 187 | virtual void pokeUserActivity(nsecs_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Narayan Kamath | 39efe3e | 2014-10-17 10:37:08 +0100 | [diff] [blame] | 190 | virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 191 | return false; |
| 192 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 193 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 194 | virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) { |
| 195 | mOnPointerDownToken = newToken; |
| 196 | } |
| 197 | |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 198 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, |
| 199 | int32_t displayId) { |
| 200 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 201 | ASSERT_EQ(mFilteredEvent->getType(), type); |
| 202 | |
| 203 | if (type == AINPUT_EVENT_TYPE_KEY) { |
| 204 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); |
| 205 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); |
| 206 | EXPECT_EQ(keyEvent.getAction(), action); |
| 207 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); |
| 208 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { |
| 209 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); |
| 210 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); |
| 211 | EXPECT_EQ(motionEvent.getAction(), action); |
| 212 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); |
| 213 | } else { |
| 214 | FAIL() << "Unknown type: " << type; |
| 215 | } |
| 216 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 217 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 218 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | }; |
| 220 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 221 | // --- HmacKeyManagerTest --- |
| 222 | |
| 223 | class HmacKeyManagerTest : public testing::Test { |
| 224 | protected: |
| 225 | HmacKeyManager mHmacKeyManager; |
| 226 | }; |
| 227 | |
| 228 | /** |
| 229 | * Ensure that separate calls to sign the same data are generating the same key. |
| 230 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 231 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 232 | * tests. |
| 233 | */ |
| 234 | TEST_F(HmacKeyManagerTest, GeneratedHmac_IsConsistent) { |
| 235 | KeyEvent event = getTestKeyEvent(); |
| 236 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 237 | |
| 238 | std::array<uint8_t, 32> hmac1 = mHmacKeyManager.sign(verifiedEvent); |
| 239 | std::array<uint8_t, 32> hmac2 = mHmacKeyManager.sign(verifiedEvent); |
| 240 | ASSERT_EQ(hmac1, hmac2); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 245 | */ |
| 246 | TEST_F(HmacKeyManagerTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 247 | KeyEvent event = getTestKeyEvent(); |
| 248 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 249 | std::array<uint8_t, 32> initialHmac = mHmacKeyManager.sign(verifiedEvent); |
| 250 | |
| 251 | verifiedEvent.deviceId += 1; |
| 252 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 253 | |
| 254 | verifiedEvent.source += 1; |
| 255 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 256 | |
| 257 | verifiedEvent.eventTimeNanos += 1; |
| 258 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 259 | |
| 260 | verifiedEvent.displayId += 1; |
| 261 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 262 | |
| 263 | verifiedEvent.action += 1; |
| 264 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 265 | |
| 266 | verifiedEvent.downTimeNanos += 1; |
| 267 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 268 | |
| 269 | verifiedEvent.flags += 1; |
| 270 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 271 | |
| 272 | verifiedEvent.keyCode += 1; |
| 273 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 274 | |
| 275 | verifiedEvent.scanCode += 1; |
| 276 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 277 | |
| 278 | verifiedEvent.metaState += 1; |
| 279 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 280 | |
| 281 | verifiedEvent.repeatCount += 1; |
| 282 | ASSERT_NE(initialHmac, mHmacKeyManager.sign(verifiedEvent)); |
| 283 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 284 | |
| 285 | // --- InputDispatcherTest --- |
| 286 | |
| 287 | class InputDispatcherTest : public testing::Test { |
| 288 | protected: |
| 289 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 290 | sp<InputDispatcher> mDispatcher; |
| 291 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 292 | virtual void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 293 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 294 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 295 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 296 | //Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 297 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 300 | virtual void TearDown() override { |
| 301 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 302 | mFakePolicy.clear(); |
| 303 | mDispatcher.clear(); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 309 | KeyEvent event; |
| 310 | |
| 311 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 312 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 313 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 314 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 315 | ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 316 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 317 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 318 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 319 | << "Should reject key events with undefined action."; |
| 320 | |
| 321 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 322 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 323 | INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 324 | ARBITRARY_TIME, ARBITRARY_TIME); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 325 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 326 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 327 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 328 | << "Should reject key events with ACTION_MULTIPLE."; |
| 329 | } |
| 330 | |
| 331 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 332 | MotionEvent event; |
| 333 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 334 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 335 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 336 | pointerProperties[i].clear(); |
| 337 | pointerProperties[i].id = i; |
| 338 | pointerCoords[i].clear(); |
| 339 | } |
| 340 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 341 | // Some constants commonly used below |
| 342 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 343 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 344 | constexpr int32_t metaState = AMETA_NONE; |
| 345 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 346 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 347 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 348 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 349 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, |
| 350 | 1 /* yScale */, 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 351 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 352 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 353 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 354 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 355 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 356 | << "Should reject motion events with undefined action."; |
| 357 | |
| 358 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 359 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 360 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 361 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 362 | 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, |
| 363 | 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 364 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 365 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 366 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 367 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 368 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 369 | << "Should reject motion events with pointer down index too large."; |
| 370 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 371 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 372 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 373 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 374 | 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, |
| 375 | 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 376 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 377 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 378 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 379 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 380 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 381 | << "Should reject motion events with pointer down index too small."; |
| 382 | |
| 383 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 384 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 385 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 386 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 387 | 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, |
| 388 | 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 389 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 390 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 391 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 392 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 393 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 394 | << "Should reject motion events with pointer up index too large."; |
| 395 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 396 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 397 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 398 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 399 | 0, 0, edgeFlags, metaState, 0, classification, 1 /* xScale */, 1 /* yScale */, |
| 400 | 0, 0, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 401 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 402 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 403 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 404 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 405 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 406 | << "Should reject motion events with pointer up index too small."; |
| 407 | |
| 408 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 409 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 410 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 411 | 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0, |
| 412 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 413 | ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 414 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 415 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 416 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 417 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 418 | << "Should reject motion events with 0 pointers."; |
| 419 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 420 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 421 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 422 | 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0, |
| 423 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 424 | ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 425 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 426 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 427 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 428 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 429 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 430 | |
| 431 | // Rejects motion events with invalid pointer ids. |
| 432 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 433 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 434 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 435 | 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0, |
| 436 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 437 | ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 438 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 439 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 440 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 441 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 442 | << "Should reject motion events with pointer ids less than 0."; |
| 443 | |
| 444 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 445 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 446 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 447 | 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0, |
| 448 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 449 | ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 450 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 451 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 452 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 453 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 454 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 455 | |
| 456 | // Rejects motion events with duplicate pointer ids. |
| 457 | pointerProperties[0].id = 1; |
| 458 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 459 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 460 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 461 | 1 /* xScale */, 1 /* yScale */, 0, 0, 0, 0, |
| 462 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 463 | ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 464 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 465 | ASSERT_EQ(INPUT_EVENT_INJECTION_FAILED, mDispatcher->injectInputEvent( |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 466 | &event, |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 467 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_NONE, 0, 0)) |
| 468 | << "Should reject motion events with duplicate pointer ids."; |
| 469 | } |
| 470 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 471 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 472 | |
| 473 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 474 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 475 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 476 | mDispatcher->notifyConfigurationChanged(&args); |
| 477 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 478 | |
| 479 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 480 | } |
| 481 | |
| 482 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 483 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 484 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 485 | mDispatcher->notifySwitch(&args); |
| 486 | |
| 487 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 488 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 489 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 490 | } |
| 491 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 492 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 493 | static constexpr int32_t INJECT_EVENT_TIMEOUT = 500; |
| 494 | static constexpr int32_t DISPATCHING_TIMEOUT = 100; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 495 | |
| 496 | class FakeApplicationHandle : public InputApplicationHandle { |
| 497 | public: |
| 498 | FakeApplicationHandle() {} |
| 499 | virtual ~FakeApplicationHandle() {} |
| 500 | |
| 501 | virtual bool updateInfo() { |
Arthur Hung | 7a0c39a | 2019-03-20 16:52:24 +0800 | [diff] [blame] | 502 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | }; |
| 506 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 507 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 508 | public: |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 509 | explicit FakeInputReceiver(const sp<InputChannel>& clientChannel, const std::string name) |
| 510 | : mName(name) { |
| 511 | mConsumer = std::make_unique<InputConsumer>(clientChannel); |
| 512 | } |
| 513 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 514 | InputEvent* consume() { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 515 | uint32_t consumeSeq; |
| 516 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 517 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 518 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 519 | status_t status = WOULD_BLOCK; |
| 520 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 521 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 522 | &event); |
| 523 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 524 | if (elapsed > 100ms) { |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (status == WOULD_BLOCK) { |
| 530 | // Just means there's no event available. |
| 531 | return nullptr; |
| 532 | } |
| 533 | |
| 534 | if (status != OK) { |
| 535 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
| 536 | return nullptr; |
| 537 | } |
| 538 | if (event == nullptr) { |
| 539 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
| 540 | return nullptr; |
| 541 | } |
| 542 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 543 | status = mConsumer->sendFinishedSignal(consumeSeq, true); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 544 | if (status != OK) { |
| 545 | ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
| 546 | } |
| 547 | return event; |
| 548 | } |
| 549 | |
| 550 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 551 | int32_t expectedFlags) { |
| 552 | InputEvent* event = consume(); |
| 553 | |
| 554 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 555 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 556 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 557 | << mName.c_str() << "expected " << inputEventTypeToString(expectedEventType) |
| 558 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 559 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 560 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 561 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 562 | switch (expectedEventType) { |
| 563 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 564 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 565 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
| 566 | EXPECT_EQ(expectedFlags, keyEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 570 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 571 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 572 | EXPECT_EQ(expectedFlags, motionEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 573 | break; |
| 574 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 575 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 576 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 577 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 578 | default: { |
| 579 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 580 | } |
| 581 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 582 | } |
| 583 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 584 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 585 | InputEvent* event = consume(); |
| 586 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 587 | << ": consumer should have returned non-NULL event."; |
| 588 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 589 | << "Got " << inputEventTypeToString(event->getType()) |
| 590 | << " event instead of FOCUS event"; |
| 591 | |
| 592 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 593 | << mName.c_str() << ": event displayId should always be NONE."; |
| 594 | |
| 595 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 596 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 597 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); |
| 598 | } |
| 599 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 600 | void assertNoEvents() { |
| 601 | InputEvent* event = consume(); |
| 602 | ASSERT_EQ(nullptr, event) |
| 603 | << mName.c_str() |
| 604 | << ": should not have received any events, so consume() should return NULL"; |
| 605 | } |
| 606 | |
| 607 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 608 | |
| 609 | protected: |
| 610 | std::unique_ptr<InputConsumer> mConsumer; |
| 611 | PreallocatedInputEventFactory mEventFactory; |
| 612 | |
| 613 | std::string mName; |
| 614 | }; |
| 615 | |
| 616 | class FakeWindowHandle : public InputWindowHandle { |
| 617 | public: |
| 618 | static const int32_t WIDTH = 600; |
| 619 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 620 | |
| 621 | FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle, |
| 622 | const sp<InputDispatcher>& dispatcher, const std::string name, |
| 623 | int32_t displayId, sp<IBinder> token = nullptr) |
| 624 | : mName(name) { |
| 625 | if (token == nullptr) { |
| 626 | sp<InputChannel> serverChannel, clientChannel; |
| 627 | InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
| 628 | mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name); |
| 629 | dispatcher->registerInputChannel(serverChannel); |
| 630 | token = serverChannel->getConnectionToken(); |
| 631 | } |
| 632 | |
| 633 | inputApplicationHandle->updateInfo(); |
| 634 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 635 | |
| 636 | mInfo.token = token; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 637 | mInfo.id = 0; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 638 | mInfo.name = name; |
| 639 | mInfo.layoutParamsFlags = 0; |
| 640 | mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION; |
| 641 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
| 642 | mInfo.frameLeft = 0; |
| 643 | mInfo.frameTop = 0; |
| 644 | mInfo.frameRight = WIDTH; |
| 645 | mInfo.frameBottom = HEIGHT; |
| 646 | mInfo.globalScaleFactor = 1.0; |
| 647 | mInfo.touchableRegion.clear(); |
| 648 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 649 | mInfo.visible = true; |
| 650 | mInfo.canReceiveKeys = true; |
| 651 | mInfo.hasFocus = false; |
| 652 | mInfo.hasWallpaper = false; |
| 653 | mInfo.paused = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 654 | mInfo.ownerPid = INJECTOR_PID; |
| 655 | mInfo.ownerUid = INJECTOR_UID; |
| 656 | mInfo.inputFeatures = 0; |
| 657 | mInfo.displayId = displayId; |
| 658 | } |
| 659 | |
| 660 | virtual bool updateInfo() { return true; } |
| 661 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 662 | void setFocus(bool hasFocus) { mInfo.hasFocus = hasFocus; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 663 | |
| 664 | void setFrame(const Rect& frame) { |
| 665 | mInfo.frameLeft = frame.left; |
| 666 | mInfo.frameTop = frame.top; |
| 667 | mInfo.frameRight = frame.right; |
| 668 | mInfo.frameBottom = frame.bottom; |
| 669 | mInfo.touchableRegion.clear(); |
| 670 | mInfo.addTouchableRegion(frame); |
| 671 | } |
| 672 | |
| 673 | void setLayoutParamFlags(int32_t flags) { mInfo.layoutParamsFlags = flags; } |
| 674 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 675 | void setId(int32_t id) { mInfo.id = id; } |
| 676 | |
| 677 | void setWindowScale(float xScale, float yScale) { |
| 678 | mInfo.windowXScale = xScale; |
| 679 | mInfo.windowYScale = yScale; |
| 680 | } |
| 681 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 682 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 683 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 684 | expectedFlags); |
| 685 | } |
| 686 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 687 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 688 | int32_t expectedFlags = 0) { |
| 689 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 690 | expectedFlags); |
| 691 | } |
| 692 | |
| 693 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 694 | int32_t expectedFlags = 0) { |
| 695 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 696 | expectedFlags); |
| 697 | } |
| 698 | |
| 699 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 700 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 701 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 702 | expectedFlags); |
| 703 | } |
| 704 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 705 | void consumeMotionPointerDown(int32_t pointerIdx, |
| 706 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) { |
| 707 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
| 708 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 709 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 710 | } |
| 711 | |
| 712 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 713 | int32_t expectedFlags = 0) { |
| 714 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
| 715 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 716 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 717 | } |
| 718 | |
| 719 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 720 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 721 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 722 | expectedFlags); |
| 723 | } |
| 724 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 725 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 726 | ASSERT_NE(mInputReceiver, nullptr) |
| 727 | << "Cannot consume events from a window with no receiver"; |
| 728 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 729 | } |
| 730 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 731 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 732 | int32_t expectedFlags) { |
| 733 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 734 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 735 | expectedFlags); |
| 736 | } |
| 737 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 738 | InputEvent* consume() { |
| 739 | if (mInputReceiver == nullptr) { |
| 740 | return nullptr; |
| 741 | } |
| 742 | return mInputReceiver->consume(); |
| 743 | } |
| 744 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 745 | void assertNoEvents() { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 746 | ASSERT_NE(mInputReceiver, nullptr) |
| 747 | << "Call 'assertNoEvents' on a window with an InputReceiver"; |
| 748 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 749 | } |
| 750 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 751 | sp<IBinder> getToken() { return mInfo.token; } |
| 752 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 753 | const std::string& getName() { return mName; } |
| 754 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 755 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 756 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 757 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 758 | }; |
| 759 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 760 | static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 761 | int32_t displayId = ADISPLAY_ID_NONE) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 762 | KeyEvent event; |
| 763 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 764 | |
| 765 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 766 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 767 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 768 | AMETA_NONE, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 769 | /* repeatCount */ 0, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 770 | |
| 771 | // Inject event until dispatch out. |
| 772 | return dispatcher->injectInputEvent( |
| 773 | &event, |
| 774 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 775 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 776 | } |
| 777 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 778 | static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action, |
| 779 | int32_t source, int32_t displayId, int32_t x, int32_t y, |
| 780 | int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 781 | int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 782 | MotionEvent event; |
| 783 | PointerProperties pointerProperties[1]; |
| 784 | PointerCoords pointerCoords[1]; |
| 785 | |
| 786 | pointerProperties[0].clear(); |
| 787 | pointerProperties[0].id = 0; |
| 788 | pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 789 | |
| 790 | pointerCoords[0].clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 791 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 792 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 793 | |
| 794 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 795 | // Define a valid motion down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 796 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, displayId, INVALID_HMAC, action, |
| 797 | /* actionButton */ 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 798 | /* flags */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 799 | /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 800 | /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0, |
| 801 | /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition, |
| 802 | currentTime, currentTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 803 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 804 | |
| 805 | // Inject event until dispatch out. |
| 806 | return dispatcher->injectInputEvent( |
| 807 | &event, |
| 808 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 809 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 810 | } |
| 811 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 812 | static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 813 | int32_t displayId, int32_t x = 100, int32_t y = 200) { |
| 814 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y); |
| 815 | } |
| 816 | |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 817 | static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 818 | int32_t displayId, int32_t x = 100, int32_t y = 200) { |
| 819 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, x, y); |
| 820 | } |
| 821 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 822 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 823 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 824 | // Define a valid key event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 825 | NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 826 | POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 827 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 828 | |
| 829 | return args; |
| 830 | } |
| 831 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 832 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 833 | const std::vector<PointF>& points) { |
| 834 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 835 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 836 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 837 | } |
| 838 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 839 | PointerProperties pointerProperties[pointerCount]; |
| 840 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 841 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 842 | for (size_t i = 0; i < pointerCount; i++) { |
| 843 | pointerProperties[i].clear(); |
| 844 | pointerProperties[i].id = i; |
| 845 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 846 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 847 | pointerCoords[i].clear(); |
| 848 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 849 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 850 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 851 | |
| 852 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 853 | // Define a valid motion event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 854 | NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 855 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 856 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 857 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 858 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 859 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 860 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 861 | |
| 862 | return args; |
| 863 | } |
| 864 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 865 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 866 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 867 | } |
| 868 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 869 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
| 870 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 871 | sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", |
| 872 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 873 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 874 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 875 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 876 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 877 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 878 | |
| 879 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 880 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | // The foreground window should receive the first touch down event. |
| 884 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
| 885 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 886 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 887 | ADISPLAY_ID_DEFAULT); |
| 888 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 889 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 890 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 891 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 892 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 893 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 894 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 895 | |
| 896 | // 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] | 897 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 898 | windowSecond->assertNoEvents(); |
| 899 | } |
| 900 | |
| 901 | TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) { |
| 902 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 903 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 904 | ADISPLAY_ID_DEFAULT); |
| 905 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 906 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 907 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 908 | // Set focused application. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 909 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 910 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 911 | // Display should have only one focused window |
| 912 | windowSecond->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 913 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 914 | |
| 915 | windowSecond->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 916 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 917 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 918 | |
| 919 | // Focused window should receive event. |
| 920 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 921 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 922 | } |
| 923 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 924 | TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) { |
| 925 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 926 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 927 | ADISPLAY_ID_DEFAULT); |
| 928 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 929 | ADISPLAY_ID_DEFAULT); |
| 930 | |
| 931 | // Set focused application. |
| 932 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 933 | |
| 934 | // Display has two focused windows. Add them to inputWindowsHandles in z-order (top most first) |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 935 | windowTop->setFocus(true); |
| 936 | windowSecond->setFocus(true); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 937 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 938 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 939 | windowTop->consumeFocusEvent(true); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 940 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 941 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 942 | |
| 943 | // Top focused window should receive event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 944 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 945 | windowSecond->assertNoEvents(); |
| 946 | } |
| 947 | |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 948 | TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) { |
| 949 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 950 | |
| 951 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 952 | ADISPLAY_ID_DEFAULT); |
| 953 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 954 | ADISPLAY_ID_DEFAULT); |
| 955 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 956 | // Set focused application. |
| 957 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 958 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 959 | windowTop->setFocus(true); |
| 960 | windowSecond->setFocus(true); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 961 | // Release channel for window is no longer valid. |
| 962 | windowTop->releaseChannel(); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 963 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 964 | windowSecond->consumeFocusEvent(true); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 965 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 966 | // Test inject a key down, should dispatch to a valid window. |
| 967 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 968 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 969 | |
| 970 | // Top window is invalid, so it should not receive any input event. |
| 971 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 972 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 975 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
| 976 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 977 | |
| 978 | sp<FakeWindowHandle> windowLeft = |
| 979 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 980 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
| 981 | windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 982 | sp<FakeWindowHandle> windowRight = |
| 983 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 984 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
| 985 | windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 986 | |
| 987 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 988 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 989 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 990 | |
| 991 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 992 | // left window. This event should be dispatched to the left window. |
| 993 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 994 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
| 995 | ADISPLAY_ID_DEFAULT, 610, 400, 599, 400)); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 996 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 997 | windowRight->assertNoEvents(); |
| 998 | } |
| 999 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1000 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
| 1001 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1002 | sp<FakeWindowHandle> window = |
| 1003 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1004 | window->setFocus(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1005 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1006 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1007 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1008 | |
| 1009 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1010 | mDispatcher->notifyKey(&keyArgs); |
| 1011 | |
| 1012 | // Window should receive key down event. |
| 1013 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1014 | |
| 1015 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1016 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1017 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1018 | mDispatcher->notifyDeviceReset(&args); |
| 1019 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1020 | AKEY_EVENT_FLAG_CANCELED); |
| 1021 | } |
| 1022 | |
| 1023 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
| 1024 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1025 | sp<FakeWindowHandle> window = |
| 1026 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1027 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1028 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1029 | |
| 1030 | NotifyMotionArgs motionArgs = |
| 1031 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1032 | ADISPLAY_ID_DEFAULT); |
| 1033 | mDispatcher->notifyMotion(&motionArgs); |
| 1034 | |
| 1035 | // Window should receive motion down event. |
| 1036 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1037 | |
| 1038 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1039 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1040 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1041 | mDispatcher->notifyDeviceReset(&args); |
| 1042 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1043 | 0 /*expectedFlags*/); |
| 1044 | } |
| 1045 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1046 | TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) { |
| 1047 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1048 | |
| 1049 | // Create a couple of windows |
| 1050 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1051 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1052 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1053 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1054 | |
| 1055 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1056 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1057 | |
| 1058 | // Send down to the first window |
| 1059 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1060 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1061 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1062 | // Only the first window should get the down event |
| 1063 | firstWindow->consumeMotionDown(); |
| 1064 | secondWindow->assertNoEvents(); |
| 1065 | |
| 1066 | // Transfer touch focus to the second window |
| 1067 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1068 | // The first window gets cancel and the second gets down |
| 1069 | firstWindow->consumeMotionCancel(); |
| 1070 | secondWindow->consumeMotionDown(); |
| 1071 | |
| 1072 | // Send up event to the second window |
| 1073 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1074 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1075 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1076 | // The first window gets no events and the second gets up |
| 1077 | firstWindow->assertNoEvents(); |
| 1078 | secondWindow->consumeMotionUp(); |
| 1079 | } |
| 1080 | |
| 1081 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) { |
| 1082 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1083 | |
| 1084 | PointF touchPoint = {10, 10}; |
| 1085 | |
| 1086 | // Create a couple of windows |
| 1087 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1088 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1089 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1090 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1091 | |
| 1092 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1093 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1094 | |
| 1095 | // Send down to the first window |
| 1096 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1097 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint}); |
| 1098 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1099 | // Only the first window should get the down event |
| 1100 | firstWindow->consumeMotionDown(); |
| 1101 | secondWindow->assertNoEvents(); |
| 1102 | |
| 1103 | // Send pointer down to the first window |
| 1104 | NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1105 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1106 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1107 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 1108 | // Only the first window should get the pointer down event |
| 1109 | firstWindow->consumeMotionPointerDown(1); |
| 1110 | secondWindow->assertNoEvents(); |
| 1111 | |
| 1112 | // Transfer touch focus to the second window |
| 1113 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1114 | // The first window gets cancel and the second gets down and pointer down |
| 1115 | firstWindow->consumeMotionCancel(); |
| 1116 | secondWindow->consumeMotionDown(); |
| 1117 | secondWindow->consumeMotionPointerDown(1); |
| 1118 | |
| 1119 | // Send pointer up to the second window |
| 1120 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1121 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1122 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1123 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1124 | // The first window gets nothing and the second gets pointer up |
| 1125 | firstWindow->assertNoEvents(); |
| 1126 | secondWindow->consumeMotionPointerUp(1); |
| 1127 | |
| 1128 | // Send up event to the second window |
| 1129 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1130 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1131 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1132 | // The first window gets nothing and the second gets up |
| 1133 | firstWindow->assertNoEvents(); |
| 1134 | secondWindow->consumeMotionUp(); |
| 1135 | } |
| 1136 | |
| 1137 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
| 1138 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1139 | |
| 1140 | // Create a non touch modal window that supports split touch |
| 1141 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1142 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1143 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
| 1144 | firstWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
| 1145 | | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1146 | |
| 1147 | // Create a non touch modal window that supports split touch |
| 1148 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1149 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1150 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
| 1151 | secondWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
| 1152 | | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1153 | |
| 1154 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1155 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1156 | |
| 1157 | PointF pointInFirst = {300, 200}; |
| 1158 | PointF pointInSecond = {300, 600}; |
| 1159 | |
| 1160 | // Send down to the first window |
| 1161 | NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1162 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 1163 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1164 | // Only the first window should get the down event |
| 1165 | firstWindow->consumeMotionDown(); |
| 1166 | secondWindow->assertNoEvents(); |
| 1167 | |
| 1168 | // Send down to the second window |
| 1169 | NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1170 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1171 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1172 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1173 | // The first window gets a move and the second a down |
| 1174 | firstWindow->consumeMotionMove(); |
| 1175 | secondWindow->consumeMotionDown(); |
| 1176 | |
| 1177 | // Transfer touch focus to the second window |
| 1178 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1179 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 1180 | firstWindow->consumeMotionCancel(); |
| 1181 | secondWindow->consumeMotionPointerDown(1); |
| 1182 | |
| 1183 | // Send pointer up to the second window |
| 1184 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1185 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1186 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1187 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1188 | // The first window gets nothing and the second gets pointer up |
| 1189 | firstWindow->assertNoEvents(); |
| 1190 | secondWindow->consumeMotionPointerUp(1); |
| 1191 | |
| 1192 | // Send up event to the second window |
| 1193 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1194 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1195 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1196 | // The first window gets nothing and the second gets up |
| 1197 | firstWindow->assertNoEvents(); |
| 1198 | secondWindow->consumeMotionUp(); |
| 1199 | } |
| 1200 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1201 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
| 1202 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1203 | sp<FakeWindowHandle> window = |
| 1204 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1205 | |
| 1206 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1207 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1208 | |
| 1209 | window->consumeFocusEvent(true); |
| 1210 | |
| 1211 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1212 | mDispatcher->notifyKey(&keyArgs); |
| 1213 | |
| 1214 | // Window should receive key down event. |
| 1215 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1216 | } |
| 1217 | |
| 1218 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
| 1219 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1220 | sp<FakeWindowHandle> window = |
| 1221 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1222 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1223 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1224 | |
| 1225 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1226 | mDispatcher->notifyKey(&keyArgs); |
| 1227 | mDispatcher->waitForIdle(); |
| 1228 | |
| 1229 | window->assertNoEvents(); |
| 1230 | } |
| 1231 | |
| 1232 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 1233 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
| 1234 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1235 | sp<FakeWindowHandle> window = |
| 1236 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1237 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1238 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1239 | |
| 1240 | // Send key |
| 1241 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1242 | mDispatcher->notifyKey(&keyArgs); |
| 1243 | // Send motion |
| 1244 | NotifyMotionArgs motionArgs = |
| 1245 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1246 | ADISPLAY_ID_DEFAULT); |
| 1247 | mDispatcher->notifyMotion(&motionArgs); |
| 1248 | |
| 1249 | // Window should receive only the motion event |
| 1250 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1251 | window->assertNoEvents(); // Key event or focus event will not be received |
| 1252 | } |
| 1253 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1254 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1255 | public: |
| 1256 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1257 | int32_t displayId, bool isGestureMonitor = false) { |
| 1258 | sp<InputChannel> serverChannel, clientChannel; |
| 1259 | InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
| 1260 | mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name); |
| 1261 | dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1264 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1265 | |
| 1266 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1267 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1268 | expectedDisplayId, expectedFlags); |
| 1269 | } |
| 1270 | |
| 1271 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1272 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1273 | expectedDisplayId, expectedFlags); |
| 1274 | } |
| 1275 | |
| 1276 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1277 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 1278 | expectedDisplayId, expectedFlags); |
| 1279 | } |
| 1280 | |
| 1281 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1282 | |
| 1283 | private: |
| 1284 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1285 | }; |
| 1286 | |
| 1287 | // Tests for gesture monitors |
| 1288 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
| 1289 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1290 | sp<FakeWindowHandle> window = |
| 1291 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1292 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1293 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1294 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1295 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1296 | |
| 1297 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1298 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1299 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1300 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1301 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
| 1305 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1306 | sp<FakeWindowHandle> window = |
| 1307 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1308 | |
| 1309 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1310 | window->setFocus(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1311 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1312 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1313 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1314 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1315 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1316 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1317 | |
| 1318 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1319 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1320 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1321 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
| 1325 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1326 | sp<FakeWindowHandle> window = |
| 1327 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1328 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1329 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1330 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1331 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1332 | |
| 1333 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1334 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1335 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1336 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1337 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1338 | |
| 1339 | window->releaseChannel(); |
| 1340 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1341 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1342 | |
| 1343 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1344 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1345 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1346 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1349 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
| 1350 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1351 | sp<FakeWindowHandle> window = |
| 1352 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1353 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1354 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1355 | |
| 1356 | NotifyMotionArgs motionArgs = |
| 1357 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1358 | ADISPLAY_ID_DEFAULT); |
| 1359 | |
| 1360 | mDispatcher->notifyMotion(&motionArgs); |
| 1361 | // Window should receive motion down event. |
| 1362 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1363 | |
| 1364 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1365 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1366 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1367 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1368 | motionArgs.pointerCoords[0].getX() - 10); |
| 1369 | |
| 1370 | mDispatcher->notifyMotion(&motionArgs); |
| 1371 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 1372 | 0 /*expectedFlags*/); |
| 1373 | } |
| 1374 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1375 | /** |
| 1376 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 1377 | * the device default right away. In the test scenario, we check both the default value, |
| 1378 | * and the action of enabling / disabling. |
| 1379 | */ |
| 1380 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
| 1381 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1382 | sp<FakeWindowHandle> window = |
| 1383 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1384 | |
| 1385 | // Set focused application. |
| 1386 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1387 | window->setFocus(true); |
| 1388 | |
| 1389 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1390 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1391 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1392 | |
| 1393 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
| 1394 | window->setFocus(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1395 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1396 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 1397 | |
| 1398 | SCOPED_TRACE("Disable touch mode"); |
| 1399 | mDispatcher->setInTouchMode(false); |
| 1400 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1401 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1402 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 1403 | |
| 1404 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
| 1405 | window->setFocus(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1406 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1407 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 1408 | |
| 1409 | SCOPED_TRACE("Enable touch mode again"); |
| 1410 | mDispatcher->setInTouchMode(true); |
| 1411 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1412 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1413 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1414 | |
| 1415 | window->assertNoEvents(); |
| 1416 | } |
| 1417 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1418 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
| 1419 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1420 | sp<FakeWindowHandle> window = |
| 1421 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1422 | |
| 1423 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1424 | window->setFocus(true); |
| 1425 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1426 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1427 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1428 | |
| 1429 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1430 | mDispatcher->notifyKey(&keyArgs); |
| 1431 | |
| 1432 | InputEvent* event = window->consume(); |
| 1433 | ASSERT_NE(event, nullptr); |
| 1434 | |
| 1435 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1436 | ASSERT_NE(verified, nullptr); |
| 1437 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 1438 | |
| 1439 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 1440 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 1441 | ASSERT_EQ(keyArgs.source, verified->source); |
| 1442 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 1443 | |
| 1444 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 1445 | |
| 1446 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 1447 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1448 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 1449 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 1450 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 1451 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 1452 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 1453 | } |
| 1454 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1455 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
| 1456 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1457 | sp<FakeWindowHandle> window = |
| 1458 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1459 | |
| 1460 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1461 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1462 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1463 | |
| 1464 | NotifyMotionArgs motionArgs = |
| 1465 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1466 | ADISPLAY_ID_DEFAULT); |
| 1467 | mDispatcher->notifyMotion(&motionArgs); |
| 1468 | |
| 1469 | InputEvent* event = window->consume(); |
| 1470 | ASSERT_NE(event, nullptr); |
| 1471 | |
| 1472 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1473 | ASSERT_NE(verified, nullptr); |
| 1474 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 1475 | |
| 1476 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 1477 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 1478 | EXPECT_EQ(motionArgs.source, verified->source); |
| 1479 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 1480 | |
| 1481 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 1482 | |
| 1483 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 1484 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 1485 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 1486 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 1487 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 1488 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 1489 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 1490 | } |
| 1491 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1492 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 1493 | protected: |
| 1494 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 1495 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 1496 | |
| 1497 | sp<FakeApplicationHandle> mApp; |
| 1498 | sp<FakeWindowHandle> mWindow; |
| 1499 | |
| 1500 | virtual void SetUp() override { |
| 1501 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 1502 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 1503 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 1504 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 1505 | ASSERT_EQ(OK, mDispatcher->start()); |
| 1506 | |
| 1507 | setUpWindow(); |
| 1508 | } |
| 1509 | |
| 1510 | void setUpWindow() { |
| 1511 | mApp = new FakeApplicationHandle(); |
| 1512 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1513 | |
| 1514 | mWindow->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1515 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1516 | |
| 1517 | mWindow->consumeFocusEvent(true); |
| 1518 | } |
| 1519 | |
| 1520 | void sendAndConsumeKeyDown() { |
| 1521 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1522 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 1523 | mDispatcher->notifyKey(&keyArgs); |
| 1524 | |
| 1525 | // Window should receive key down event. |
| 1526 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1527 | } |
| 1528 | |
| 1529 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 1530 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 1531 | InputEvent* repeatEvent = mWindow->consume(); |
| 1532 | ASSERT_NE(nullptr, repeatEvent); |
| 1533 | |
| 1534 | uint32_t eventType = repeatEvent->getType(); |
| 1535 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 1536 | |
| 1537 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 1538 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 1539 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 1540 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 1541 | } |
| 1542 | |
| 1543 | void sendAndConsumeKeyUp() { |
| 1544 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 1545 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 1546 | mDispatcher->notifyKey(&keyArgs); |
| 1547 | |
| 1548 | // Window should receive key down event. |
| 1549 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1550 | 0 /*expectedFlags*/); |
| 1551 | } |
| 1552 | }; |
| 1553 | |
| 1554 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
| 1555 | sendAndConsumeKeyDown(); |
| 1556 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1557 | expectKeyRepeatOnce(repeatCount); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
| 1562 | sendAndConsumeKeyDown(); |
| 1563 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 1564 | sendAndConsumeKeyUp(); |
| 1565 | mWindow->assertNoEvents(); |
| 1566 | } |
| 1567 | |
| 1568 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
| 1569 | sendAndConsumeKeyDown(); |
| 1570 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1571 | InputEvent* repeatEvent = mWindow->consume(); |
| 1572 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 1573 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 1574 | IdGenerator::getSource(repeatEvent->getId())); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
| 1579 | sendAndConsumeKeyDown(); |
| 1580 | |
| 1581 | std::unordered_set<int32_t> idSet; |
| 1582 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1583 | InputEvent* repeatEvent = mWindow->consume(); |
| 1584 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 1585 | int32_t id = repeatEvent->getId(); |
| 1586 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 1587 | idSet.insert(id); |
| 1588 | } |
| 1589 | } |
| 1590 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1591 | /* Test InputDispatcher for MultiDisplay */ |
| 1592 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 1593 | public: |
| 1594 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1595 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1596 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1597 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1598 | application1 = new FakeApplicationHandle(); |
| 1599 | windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", |
| 1600 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1601 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1602 | // Set focus window for primary display, but focused display would be second one. |
| 1603 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1604 | windowInPrimary->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1605 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1606 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1607 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1608 | application2 = new FakeApplicationHandle(); |
| 1609 | windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", |
| 1610 | SECOND_DISPLAY_ID); |
| 1611 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1612 | // Set focus display to second one. |
| 1613 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 1614 | // Set focus window for second display. |
| 1615 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1616 | windowInSecondary->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1617 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1618 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1619 | } |
| 1620 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1621 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1622 | InputDispatcherTest::TearDown(); |
| 1623 | |
| 1624 | application1.clear(); |
| 1625 | windowInPrimary.clear(); |
| 1626 | application2.clear(); |
| 1627 | windowInSecondary.clear(); |
| 1628 | } |
| 1629 | |
| 1630 | protected: |
| 1631 | sp<FakeApplicationHandle> application1; |
| 1632 | sp<FakeWindowHandle> windowInPrimary; |
| 1633 | sp<FakeApplicationHandle> application2; |
| 1634 | sp<FakeWindowHandle> windowInSecondary; |
| 1635 | }; |
| 1636 | |
| 1637 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 1638 | // Test touch down on primary display. |
| 1639 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1640 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1641 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1642 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1643 | windowInSecondary->assertNoEvents(); |
| 1644 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1645 | // Test touch down on second display. |
| 1646 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1647 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1648 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1649 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1650 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1651 | } |
| 1652 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1653 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1654 | // Test inject a key down with display id specified. |
| 1655 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1656 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1657 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1658 | windowInSecondary->assertNoEvents(); |
| 1659 | |
| 1660 | // Test inject a key down without display id specified. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1661 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 1662 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1663 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1664 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1665 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1666 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1667 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1668 | |
| 1669 | // Expect old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1670 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 1671 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1672 | |
| 1673 | // Test inject a key down, should timeout because of no target window. |
| 1674 | ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) |
| 1675 | << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; |
| 1676 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1677 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1678 | windowInSecondary->assertNoEvents(); |
| 1679 | } |
| 1680 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1681 | // Test per-display input monitors for motion event. |
| 1682 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1683 | FakeMonitorReceiver monitorInPrimary = |
| 1684 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 1685 | FakeMonitorReceiver monitorInSecondary = |
| 1686 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1687 | |
| 1688 | // Test touch down on primary display. |
| 1689 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1690 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1691 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1692 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1693 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1694 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1695 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1696 | |
| 1697 | // Test touch down on second display. |
| 1698 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1699 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 1700 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1701 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1702 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1703 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1704 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1705 | |
| 1706 | // Test inject a non-pointer motion event. |
| 1707 | // If specific a display, it will dispatch to the focused window of particular display, |
| 1708 | // or it will dispatch to the focused window of focused display. |
| 1709 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1710 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 1711 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1712 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1713 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1714 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1715 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | // Test per-display input monitors for key event. |
| 1719 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
| 1720 | //Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1721 | FakeMonitorReceiver monitorInPrimary = |
| 1722 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 1723 | FakeMonitorReceiver monitorInSecondary = |
| 1724 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1725 | |
| 1726 | // Test inject a key down. |
| 1727 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 1728 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1729 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1730 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1731 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1732 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1735 | class InputFilterTest : public InputDispatcherTest { |
| 1736 | protected: |
| 1737 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 1738 | |
| 1739 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 1740 | NotifyMotionArgs motionArgs; |
| 1741 | |
| 1742 | motionArgs = generateMotionArgs( |
| 1743 | AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 1744 | mDispatcher->notifyMotion(&motionArgs); |
| 1745 | motionArgs = generateMotionArgs( |
| 1746 | AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 1747 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1748 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1749 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 1750 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1751 | } else { |
| 1752 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | void testNotifyKey(bool expectToBeFiltered) { |
| 1757 | NotifyKeyArgs keyArgs; |
| 1758 | |
| 1759 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1760 | mDispatcher->notifyKey(&keyArgs); |
| 1761 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 1762 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1763 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1764 | |
| 1765 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 1766 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1767 | } else { |
| 1768 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 1769 | } |
| 1770 | } |
| 1771 | }; |
| 1772 | |
| 1773 | // Test InputFilter for MotionEvent |
| 1774 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 1775 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 1776 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 1777 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 1778 | |
| 1779 | // Enable InputFilter |
| 1780 | mDispatcher->setInputFilterEnabled(true); |
| 1781 | // Test touch on both primary and second display, and check if both events are filtered. |
| 1782 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 1783 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 1784 | |
| 1785 | // Disable InputFilter |
| 1786 | mDispatcher->setInputFilterEnabled(false); |
| 1787 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 1788 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 1789 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 1790 | } |
| 1791 | |
| 1792 | // Test InputFilter for KeyEvent |
| 1793 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 1794 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 1795 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 1796 | |
| 1797 | // Enable InputFilter |
| 1798 | mDispatcher->setInputFilterEnabled(true); |
| 1799 | // Send a key event, and check if it is filtered. |
| 1800 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 1801 | |
| 1802 | // Disable InputFilter |
| 1803 | mDispatcher->setInputFilterEnabled(false); |
| 1804 | // Send a key event, and check if it isn't filtered. |
| 1805 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 1806 | } |
| 1807 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1808 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1809 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1810 | InputDispatcherTest::SetUp(); |
| 1811 | |
| 1812 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1813 | mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top", |
| 1814 | ADISPLAY_ID_DEFAULT); |
| 1815 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 1816 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 1817 | // window. |
| 1818 | mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 1819 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1820 | mFocusedWindow = |
| 1821 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 1822 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
| 1823 | mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 1824 | mFocusedWindowTouchPoint = 60; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1825 | |
| 1826 | // Set focused application. |
| 1827 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1828 | mFocusedWindow->setFocus(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1829 | |
| 1830 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1831 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1832 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1835 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1836 | InputDispatcherTest::TearDown(); |
| 1837 | |
| 1838 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1839 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | protected: |
| 1843 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1844 | sp<FakeWindowHandle> mFocusedWindow; |
| 1845 | int32_t mFocusedWindowTouchPoint; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1846 | }; |
| 1847 | |
| 1848 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1849 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 1850 | // the onPointerDownOutsideFocus callback. |
| 1851 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
| 1852 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1853 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1854 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1855 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1856 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1857 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 1858 | } |
| 1859 | |
| 1860 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 1861 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 1862 | // onPointerDownOutsideFocus callback. |
| 1863 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
| 1864 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1865 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1866 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1867 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1868 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1869 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 1873 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 1874 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
| 1875 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1876 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1877 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1878 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1879 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1883 | // DOWN on the window that already has focus. Ensure no window received the |
| 1884 | // onPointerDownOutsideFocus callback. |
| 1885 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, |
| 1886 | OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1887 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1888 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1889 | mFocusedWindowTouchPoint, mFocusedWindowTouchPoint)) |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1890 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1891 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1892 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1893 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1896 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 1897 | // windows that point to the same client token. |
| 1898 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 1899 | virtual void SetUp() override { |
| 1900 | InputDispatcherTest::SetUp(); |
| 1901 | |
| 1902 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1903 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 1904 | ADISPLAY_ID_DEFAULT); |
| 1905 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 1906 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
| 1907 | mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL | |
| 1908 | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1909 | mWindow1->setId(0); |
| 1910 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 1911 | |
| 1912 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 1913 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
| 1914 | mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL | |
| 1915 | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1916 | mWindow2->setId(1); |
| 1917 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 1918 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame^] | 1919 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | protected: |
| 1923 | sp<FakeWindowHandle> mWindow1; |
| 1924 | sp<FakeWindowHandle> mWindow2; |
| 1925 | |
| 1926 | // Helper function to convert the point from screen coordinates into the window's space |
| 1927 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
| 1928 | float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft); |
| 1929 | float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop); |
| 1930 | return {x, y}; |
| 1931 | } |
| 1932 | |
| 1933 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 1934 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1935 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1936 | InputEvent* event = window->consume(); |
| 1937 | |
| 1938 | ASSERT_NE(nullptr, event) << name.c_str() |
| 1939 | << ": consumer should have returned non-NULL event."; |
| 1940 | |
| 1941 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 1942 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 1943 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 1944 | |
| 1945 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 1946 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 1947 | |
| 1948 | for (size_t i = 0; i < points.size(); i++) { |
| 1949 | float expectedX = points[i].x; |
| 1950 | float expectedY = points[i].y; |
| 1951 | |
| 1952 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 1953 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 1954 | << ", got " << motionEvent.getX(i); |
| 1955 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 1956 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 1957 | << ", got " << motionEvent.getY(i); |
| 1958 | } |
| 1959 | } |
| 1960 | }; |
| 1961 | |
| 1962 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 1963 | // Touch Window 1 |
| 1964 | PointF touchedPoint = {10, 10}; |
| 1965 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
| 1966 | |
| 1967 | NotifyMotionArgs motionArgs = |
| 1968 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1969 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1970 | mDispatcher->notifyMotion(&motionArgs); |
| 1971 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 1972 | |
| 1973 | // Release touch on Window 1 |
| 1974 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1975 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1976 | mDispatcher->notifyMotion(&motionArgs); |
| 1977 | // consume the UP event |
| 1978 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint}); |
| 1979 | |
| 1980 | // Touch Window 2 |
| 1981 | touchedPoint = {150, 150}; |
| 1982 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 1983 | |
| 1984 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1985 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1986 | mDispatcher->notifyMotion(&motionArgs); |
| 1987 | |
| 1988 | // Consuming from window1 since it's the window that has the InputReceiver |
| 1989 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 1990 | } |
| 1991 | |
| 1992 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) { |
| 1993 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 1994 | |
| 1995 | // Touch Window 1 |
| 1996 | PointF touchedPoint = {10, 10}; |
| 1997 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
| 1998 | |
| 1999 | NotifyMotionArgs motionArgs = |
| 2000 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2001 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2002 | mDispatcher->notifyMotion(&motionArgs); |
| 2003 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 2004 | |
| 2005 | // Release touch on Window 1 |
| 2006 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2007 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2008 | mDispatcher->notifyMotion(&motionArgs); |
| 2009 | // consume the UP event |
| 2010 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint}); |
| 2011 | |
| 2012 | // Touch Window 2 |
| 2013 | touchedPoint = {150, 150}; |
| 2014 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 2015 | |
| 2016 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2017 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2018 | mDispatcher->notifyMotion(&motionArgs); |
| 2019 | |
| 2020 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2021 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 2022 | } |
| 2023 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2024 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) { |
| 2025 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2026 | |
| 2027 | // Touch Window 1 |
| 2028 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2029 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2030 | |
| 2031 | NotifyMotionArgs motionArgs = |
| 2032 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2033 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2034 | mDispatcher->notifyMotion(&motionArgs); |
| 2035 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2036 | |
| 2037 | // Touch Window 2 |
| 2038 | int32_t actionPointerDown = |
| 2039 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2040 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2041 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2042 | |
| 2043 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2044 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2045 | mDispatcher->notifyMotion(&motionArgs); |
| 2046 | |
| 2047 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2048 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2049 | } |
| 2050 | |
| 2051 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) { |
| 2052 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2053 | |
| 2054 | // Touch Window 1 |
| 2055 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2056 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2057 | |
| 2058 | NotifyMotionArgs motionArgs = |
| 2059 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2060 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2061 | mDispatcher->notifyMotion(&motionArgs); |
| 2062 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2063 | |
| 2064 | // Touch Window 2 |
| 2065 | int32_t actionPointerDown = |
| 2066 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2067 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2068 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2069 | |
| 2070 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2071 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2072 | mDispatcher->notifyMotion(&motionArgs); |
| 2073 | |
| 2074 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2075 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2076 | |
| 2077 | // Move both windows |
| 2078 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2079 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2080 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2081 | |
| 2082 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2083 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2084 | mDispatcher->notifyMotion(&motionArgs); |
| 2085 | |
| 2086 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints); |
| 2087 | } |
| 2088 | |
| 2089 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 2090 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 2091 | |
| 2092 | // Touch Window 1 |
| 2093 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2094 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2095 | |
| 2096 | NotifyMotionArgs motionArgs = |
| 2097 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2098 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2099 | mDispatcher->notifyMotion(&motionArgs); |
| 2100 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2101 | |
| 2102 | // Touch Window 2 |
| 2103 | int32_t actionPointerDown = |
| 2104 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2105 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2106 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2107 | |
| 2108 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2109 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2110 | mDispatcher->notifyMotion(&motionArgs); |
| 2111 | |
| 2112 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2113 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2114 | |
| 2115 | // Move both windows |
| 2116 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2117 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2118 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2119 | |
| 2120 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2121 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2122 | mDispatcher->notifyMotion(&motionArgs); |
| 2123 | |
| 2124 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints); |
| 2125 | } |
| 2126 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2127 | } // namespace android::inputdispatcher |