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; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 494 | static constexpr nsecs_t DISPATCHING_TIMEOUT = seconds_to_nanoseconds(5); |
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; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame^] | 637 | mInfo.id = sId++; |
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 setWindowScale(float xScale, float yScale) { |
| 676 | mInfo.windowXScale = xScale; |
| 677 | mInfo.windowYScale = yScale; |
| 678 | } |
| 679 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 680 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 681 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 682 | expectedFlags); |
| 683 | } |
| 684 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 685 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 686 | int32_t expectedFlags = 0) { |
| 687 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 688 | expectedFlags); |
| 689 | } |
| 690 | |
| 691 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 692 | int32_t expectedFlags = 0) { |
| 693 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 694 | expectedFlags); |
| 695 | } |
| 696 | |
| 697 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 698 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 699 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 700 | expectedFlags); |
| 701 | } |
| 702 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 703 | void consumeMotionPointerDown(int32_t pointerIdx, |
| 704 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) { |
| 705 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
| 706 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 707 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 708 | } |
| 709 | |
| 710 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 711 | int32_t expectedFlags = 0) { |
| 712 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
| 713 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 714 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 715 | } |
| 716 | |
| 717 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 718 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 719 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 720 | expectedFlags); |
| 721 | } |
| 722 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 723 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 724 | ASSERT_NE(mInputReceiver, nullptr) |
| 725 | << "Cannot consume events from a window with no receiver"; |
| 726 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 727 | } |
| 728 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 729 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 730 | int32_t expectedFlags) { |
| 731 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 732 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 733 | expectedFlags); |
| 734 | } |
| 735 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 736 | InputEvent* consume() { |
| 737 | if (mInputReceiver == nullptr) { |
| 738 | return nullptr; |
| 739 | } |
| 740 | return mInputReceiver->consume(); |
| 741 | } |
| 742 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 743 | void assertNoEvents() { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 744 | ASSERT_NE(mInputReceiver, nullptr) |
| 745 | << "Call 'assertNoEvents' on a window with an InputReceiver"; |
| 746 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 747 | } |
| 748 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 749 | sp<IBinder> getToken() { return mInfo.token; } |
| 750 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 751 | const std::string& getName() { return mName; } |
| 752 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 753 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 754 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 755 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame^] | 756 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 757 | }; |
| 758 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame^] | 759 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 760 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 761 | static int32_t injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 762 | int32_t displayId = ADISPLAY_ID_NONE) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 763 | KeyEvent event; |
| 764 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 765 | |
| 766 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 767 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 768 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 769 | AMETA_NONE, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 770 | /* repeatCount */ 0, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 771 | |
| 772 | // Inject event until dispatch out. |
| 773 | return dispatcher->injectInputEvent( |
| 774 | &event, |
| 775 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 776 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 777 | } |
| 778 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 779 | static int32_t injectMotionEvent(const sp<InputDispatcher>& dispatcher, int32_t action, |
| 780 | int32_t source, int32_t displayId, int32_t x, int32_t y, |
| 781 | int32_t xCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 782 | int32_t yCursorPosition = AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 783 | MotionEvent event; |
| 784 | PointerProperties pointerProperties[1]; |
| 785 | PointerCoords pointerCoords[1]; |
| 786 | |
| 787 | pointerProperties[0].clear(); |
| 788 | pointerProperties[0].id = 0; |
| 789 | pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 790 | |
| 791 | pointerCoords[0].clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 792 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 793 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 794 | |
| 795 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 796 | // Define a valid motion down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 797 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, displayId, INVALID_HMAC, action, |
| 798 | /* actionButton */ 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 799 | /* flags */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 800 | /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 801 | /* xScale */ 1, /* yScale */ 1, /* xOffset */ 0, /* yOffset */ 0, |
| 802 | /* xPrecision */ 0, /* yPrecision */ 0, xCursorPosition, yCursorPosition, |
| 803 | currentTime, currentTime, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 804 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 805 | |
| 806 | // Inject event until dispatch out. |
| 807 | return dispatcher->injectInputEvent( |
| 808 | &event, |
| 809 | INJECTOR_PID, INJECTOR_UID, INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT, |
| 810 | INJECT_EVENT_TIMEOUT, POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 811 | } |
| 812 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 813 | static int32_t injectMotionDown(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 814 | int32_t displayId, int32_t x = 100, int32_t y = 200) { |
| 815 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, x, y); |
| 816 | } |
| 817 | |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 818 | static int32_t injectMotionUp(const sp<InputDispatcher>& dispatcher, int32_t source, |
| 819 | int32_t displayId, int32_t x = 100, int32_t y = 200) { |
| 820 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, x, y); |
| 821 | } |
| 822 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 823 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 824 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 825 | // Define a valid key event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 826 | NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 827 | POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 828 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 829 | |
| 830 | return args; |
| 831 | } |
| 832 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 833 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 834 | const std::vector<PointF>& points) { |
| 835 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 836 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 837 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 838 | } |
| 839 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 840 | PointerProperties pointerProperties[pointerCount]; |
| 841 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 842 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 843 | for (size_t i = 0; i < pointerCount; i++) { |
| 844 | pointerProperties[i].clear(); |
| 845 | pointerProperties[i].id = i; |
| 846 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 847 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 848 | pointerCoords[i].clear(); |
| 849 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 850 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 851 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 852 | |
| 853 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 854 | // Define a valid motion event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 855 | NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 856 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 857 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 858 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 859 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 860 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 861 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 862 | |
| 863 | return args; |
| 864 | } |
| 865 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 866 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 867 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 868 | } |
| 869 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 870 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
| 871 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 872 | sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", |
| 873 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 874 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 875 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 876 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 877 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 878 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 879 | |
| 880 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 881 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | // The foreground window should receive the first touch down event. |
| 885 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
| 886 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 887 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 888 | ADISPLAY_ID_DEFAULT); |
| 889 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 890 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 891 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 892 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 893 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 894 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 895 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 896 | |
| 897 | // 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] | 898 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 899 | windowSecond->assertNoEvents(); |
| 900 | } |
| 901 | |
| 902 | TEST_F(InputDispatcherTest, SetInputWindow_FocusedWindow) { |
| 903 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 904 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 905 | ADISPLAY_ID_DEFAULT); |
| 906 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 907 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 908 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 909 | // Set focused application. |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 910 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 911 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 912 | // Display should have only one focused window |
| 913 | windowSecond->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 914 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 915 | |
| 916 | windowSecond->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 917 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 918 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 919 | |
| 920 | // Focused window should receive event. |
| 921 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 922 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 923 | } |
| 924 | |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 925 | TEST_F(InputDispatcherTest, SetInputWindow_FocusPriority) { |
| 926 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 927 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 928 | ADISPLAY_ID_DEFAULT); |
| 929 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 930 | ADISPLAY_ID_DEFAULT); |
| 931 | |
| 932 | // Set focused application. |
| 933 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 934 | |
| 935 | // 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] | 936 | windowTop->setFocus(true); |
| 937 | windowSecond->setFocus(true); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 938 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 939 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 940 | windowTop->consumeFocusEvent(true); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 941 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 942 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 943 | |
| 944 | // Top focused window should receive event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 945 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 7ab76b1 | 2019-01-09 19:17:20 +0800 | [diff] [blame] | 946 | windowSecond->assertNoEvents(); |
| 947 | } |
| 948 | |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 949 | TEST_F(InputDispatcherTest, SetInputWindow_InputWindowInfo) { |
| 950 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 951 | |
| 952 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 953 | ADISPLAY_ID_DEFAULT); |
| 954 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 955 | ADISPLAY_ID_DEFAULT); |
| 956 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 957 | // Set focused application. |
| 958 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 959 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 960 | windowTop->setFocus(true); |
| 961 | windowSecond->setFocus(true); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 962 | // Release channel for window is no longer valid. |
| 963 | windowTop->releaseChannel(); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 964 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 965 | windowSecond->consumeFocusEvent(true); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 966 | |
Arthur Hung | 832bc4a | 2019-01-28 11:43:17 +0800 | [diff] [blame] | 967 | // Test inject a key down, should dispatch to a valid window. |
| 968 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 969 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 970 | |
| 971 | // Top window is invalid, so it should not receive any input event. |
| 972 | windowTop->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 973 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 3b413f2 | 2018-10-26 18:05:34 +0800 | [diff] [blame] | 974 | } |
| 975 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 976 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
| 977 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 978 | |
| 979 | sp<FakeWindowHandle> windowLeft = |
| 980 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 981 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
| 982 | windowLeft->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 983 | sp<FakeWindowHandle> windowRight = |
| 984 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 985 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
| 986 | windowRight->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 987 | |
| 988 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 989 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 990 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 991 | |
| 992 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 993 | // left window. This event should be dispatched to the left window. |
| 994 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 995 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
| 996 | ADISPLAY_ID_DEFAULT, 610, 400, 599, 400)); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 997 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 998 | windowRight->assertNoEvents(); |
| 999 | } |
| 1000 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1001 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
| 1002 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1003 | sp<FakeWindowHandle> window = |
| 1004 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | window->setFocus(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1006 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1007 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1008 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1009 | |
| 1010 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1011 | mDispatcher->notifyKey(&keyArgs); |
| 1012 | |
| 1013 | // Window should receive key down event. |
| 1014 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1015 | |
| 1016 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1017 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1018 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1019 | mDispatcher->notifyDeviceReset(&args); |
| 1020 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1021 | AKEY_EVENT_FLAG_CANCELED); |
| 1022 | } |
| 1023 | |
| 1024 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
| 1025 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1026 | sp<FakeWindowHandle> window = |
| 1027 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1028 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1029 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1030 | |
| 1031 | NotifyMotionArgs motionArgs = |
| 1032 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1033 | ADISPLAY_ID_DEFAULT); |
| 1034 | mDispatcher->notifyMotion(&motionArgs); |
| 1035 | |
| 1036 | // Window should receive motion down event. |
| 1037 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1038 | |
| 1039 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1040 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1041 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1042 | mDispatcher->notifyDeviceReset(&args); |
| 1043 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1044 | 0 /*expectedFlags*/); |
| 1045 | } |
| 1046 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1047 | TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) { |
| 1048 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1049 | |
| 1050 | // Create a couple of windows |
| 1051 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1052 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1053 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1054 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1055 | |
| 1056 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1057 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1058 | |
| 1059 | // Send down to the first window |
| 1060 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1061 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1062 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1063 | // Only the first window should get the down event |
| 1064 | firstWindow->consumeMotionDown(); |
| 1065 | secondWindow->assertNoEvents(); |
| 1066 | |
| 1067 | // Transfer touch focus to the second window |
| 1068 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1069 | // The first window gets cancel and the second gets down |
| 1070 | firstWindow->consumeMotionCancel(); |
| 1071 | secondWindow->consumeMotionDown(); |
| 1072 | |
| 1073 | // Send up event to the second window |
| 1074 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1075 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1076 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1077 | // The first window gets no events and the second gets up |
| 1078 | firstWindow->assertNoEvents(); |
| 1079 | secondWindow->consumeMotionUp(); |
| 1080 | } |
| 1081 | |
| 1082 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) { |
| 1083 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1084 | |
| 1085 | PointF touchPoint = {10, 10}; |
| 1086 | |
| 1087 | // Create a couple of windows |
| 1088 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1089 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1090 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1091 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1092 | |
| 1093 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1094 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1095 | |
| 1096 | // Send down to the first window |
| 1097 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1098 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint}); |
| 1099 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1100 | // Only the first window should get the down event |
| 1101 | firstWindow->consumeMotionDown(); |
| 1102 | secondWindow->assertNoEvents(); |
| 1103 | |
| 1104 | // Send pointer down to the first window |
| 1105 | NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1106 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1107 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1108 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 1109 | // Only the first window should get the pointer down event |
| 1110 | firstWindow->consumeMotionPointerDown(1); |
| 1111 | secondWindow->assertNoEvents(); |
| 1112 | |
| 1113 | // Transfer touch focus to the second window |
| 1114 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1115 | // The first window gets cancel and the second gets down and pointer down |
| 1116 | firstWindow->consumeMotionCancel(); |
| 1117 | secondWindow->consumeMotionDown(); |
| 1118 | secondWindow->consumeMotionPointerDown(1); |
| 1119 | |
| 1120 | // Send pointer up to the second window |
| 1121 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1122 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1123 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1124 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1125 | // The first window gets nothing and the second gets pointer up |
| 1126 | firstWindow->assertNoEvents(); |
| 1127 | secondWindow->consumeMotionPointerUp(1); |
| 1128 | |
| 1129 | // Send up event to the second window |
| 1130 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1131 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1132 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1133 | // The first window gets nothing and the second gets up |
| 1134 | firstWindow->assertNoEvents(); |
| 1135 | secondWindow->consumeMotionUp(); |
| 1136 | } |
| 1137 | |
| 1138 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
| 1139 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1140 | |
| 1141 | // Create a non touch modal window that supports split touch |
| 1142 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1143 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1144 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
| 1145 | firstWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
| 1146 | | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1147 | |
| 1148 | // Create a non touch modal window that supports split touch |
| 1149 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1150 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1151 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
| 1152 | secondWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL |
| 1153 | | InputWindowInfo::FLAG_SPLIT_TOUCH); |
| 1154 | |
| 1155 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1156 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1157 | |
| 1158 | PointF pointInFirst = {300, 200}; |
| 1159 | PointF pointInSecond = {300, 600}; |
| 1160 | |
| 1161 | // Send down to the first window |
| 1162 | NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1163 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 1164 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1165 | // Only the first window should get the down event |
| 1166 | firstWindow->consumeMotionDown(); |
| 1167 | secondWindow->assertNoEvents(); |
| 1168 | |
| 1169 | // Send down to the second window |
| 1170 | NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1171 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1172 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1173 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1174 | // The first window gets a move and the second a down |
| 1175 | firstWindow->consumeMotionMove(); |
| 1176 | secondWindow->consumeMotionDown(); |
| 1177 | |
| 1178 | // Transfer touch focus to the second window |
| 1179 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1180 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 1181 | firstWindow->consumeMotionCancel(); |
| 1182 | secondWindow->consumeMotionPointerDown(1); |
| 1183 | |
| 1184 | // Send pointer up to the second window |
| 1185 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1186 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1187 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1188 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1189 | // The first window gets nothing and the second gets pointer up |
| 1190 | firstWindow->assertNoEvents(); |
| 1191 | secondWindow->consumeMotionPointerUp(1); |
| 1192 | |
| 1193 | // Send up event to the second window |
| 1194 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1195 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1196 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1197 | // The first window gets nothing and the second gets up |
| 1198 | firstWindow->assertNoEvents(); |
| 1199 | secondWindow->consumeMotionUp(); |
| 1200 | } |
| 1201 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1202 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
| 1203 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1204 | sp<FakeWindowHandle> window = |
| 1205 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1206 | |
| 1207 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1208 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1209 | |
| 1210 | window->consumeFocusEvent(true); |
| 1211 | |
| 1212 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1213 | mDispatcher->notifyKey(&keyArgs); |
| 1214 | |
| 1215 | // Window should receive key down event. |
| 1216 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1217 | } |
| 1218 | |
| 1219 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
| 1220 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1221 | sp<FakeWindowHandle> window = |
| 1222 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1223 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1224 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1225 | |
| 1226 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1227 | mDispatcher->notifyKey(&keyArgs); |
| 1228 | mDispatcher->waitForIdle(); |
| 1229 | |
| 1230 | window->assertNoEvents(); |
| 1231 | } |
| 1232 | |
| 1233 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 1234 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
| 1235 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1236 | sp<FakeWindowHandle> window = |
| 1237 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1238 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1239 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1240 | |
| 1241 | // Send key |
| 1242 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1243 | mDispatcher->notifyKey(&keyArgs); |
| 1244 | // Send motion |
| 1245 | NotifyMotionArgs motionArgs = |
| 1246 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1247 | ADISPLAY_ID_DEFAULT); |
| 1248 | mDispatcher->notifyMotion(&motionArgs); |
| 1249 | |
| 1250 | // Window should receive only the motion event |
| 1251 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1252 | window->assertNoEvents(); // Key event or focus event will not be received |
| 1253 | } |
| 1254 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1255 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1256 | public: |
| 1257 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1258 | int32_t displayId, bool isGestureMonitor = false) { |
| 1259 | sp<InputChannel> serverChannel, clientChannel; |
| 1260 | InputChannel::openInputChannelPair(name, serverChannel, clientChannel); |
| 1261 | mInputReceiver = std::make_unique<FakeInputReceiver>(clientChannel, name); |
| 1262 | dispatcher->registerInputMonitor(serverChannel, displayId, isGestureMonitor); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1265 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1266 | |
| 1267 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1268 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1269 | expectedDisplayId, expectedFlags); |
| 1270 | } |
| 1271 | |
| 1272 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1273 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1274 | expectedDisplayId, expectedFlags); |
| 1275 | } |
| 1276 | |
| 1277 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1278 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 1279 | expectedDisplayId, expectedFlags); |
| 1280 | } |
| 1281 | |
| 1282 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1283 | |
| 1284 | private: |
| 1285 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1286 | }; |
| 1287 | |
| 1288 | // Tests for gesture monitors |
| 1289 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
| 1290 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1291 | sp<FakeWindowHandle> window = |
| 1292 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1293 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1294 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1295 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1296 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1297 | |
| 1298 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1299 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1300 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1301 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1302 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
| 1306 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1307 | sp<FakeWindowHandle> window = |
| 1308 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1309 | |
| 1310 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1311 | window->setFocus(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1312 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1313 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1314 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1315 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1316 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1317 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1318 | |
| 1319 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1320 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1321 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1322 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
| 1326 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1327 | sp<FakeWindowHandle> window = |
| 1328 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1329 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1330 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1331 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1332 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1333 | |
| 1334 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1335 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1336 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1337 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1338 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1339 | |
| 1340 | window->releaseChannel(); |
| 1341 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1342 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1343 | |
| 1344 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1345 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1346 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1347 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1350 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
| 1351 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1352 | sp<FakeWindowHandle> window = |
| 1353 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1354 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1355 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1356 | |
| 1357 | NotifyMotionArgs motionArgs = |
| 1358 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1359 | ADISPLAY_ID_DEFAULT); |
| 1360 | |
| 1361 | mDispatcher->notifyMotion(&motionArgs); |
| 1362 | // Window should receive motion down event. |
| 1363 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1364 | |
| 1365 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1366 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1367 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1368 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1369 | motionArgs.pointerCoords[0].getX() - 10); |
| 1370 | |
| 1371 | mDispatcher->notifyMotion(&motionArgs); |
| 1372 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 1373 | 0 /*expectedFlags*/); |
| 1374 | } |
| 1375 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1376 | /** |
| 1377 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 1378 | * the device default right away. In the test scenario, we check both the default value, |
| 1379 | * and the action of enabling / disabling. |
| 1380 | */ |
| 1381 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
| 1382 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1383 | sp<FakeWindowHandle> window = |
| 1384 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1385 | |
| 1386 | // Set focused application. |
| 1387 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1388 | window->setFocus(true); |
| 1389 | |
| 1390 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1391 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1392 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1393 | |
| 1394 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
| 1395 | window->setFocus(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1396 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1397 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 1398 | |
| 1399 | SCOPED_TRACE("Disable touch mode"); |
| 1400 | mDispatcher->setInTouchMode(false); |
| 1401 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1402 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1403 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 1404 | |
| 1405 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
| 1406 | window->setFocus(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1407 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1408 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 1409 | |
| 1410 | SCOPED_TRACE("Enable touch mode again"); |
| 1411 | mDispatcher->setInTouchMode(true); |
| 1412 | window->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1413 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1414 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1415 | |
| 1416 | window->assertNoEvents(); |
| 1417 | } |
| 1418 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1419 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
| 1420 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1421 | sp<FakeWindowHandle> window = |
| 1422 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1423 | |
| 1424 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1425 | window->setFocus(true); |
| 1426 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1428 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1429 | |
| 1430 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1431 | mDispatcher->notifyKey(&keyArgs); |
| 1432 | |
| 1433 | InputEvent* event = window->consume(); |
| 1434 | ASSERT_NE(event, nullptr); |
| 1435 | |
| 1436 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1437 | ASSERT_NE(verified, nullptr); |
| 1438 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 1439 | |
| 1440 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 1441 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 1442 | ASSERT_EQ(keyArgs.source, verified->source); |
| 1443 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 1444 | |
| 1445 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 1446 | |
| 1447 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 1448 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1449 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 1450 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 1451 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 1452 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 1453 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 1454 | } |
| 1455 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1456 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
| 1457 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1458 | sp<FakeWindowHandle> window = |
| 1459 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1460 | |
| 1461 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1462 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1463 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1464 | |
| 1465 | NotifyMotionArgs motionArgs = |
| 1466 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1467 | ADISPLAY_ID_DEFAULT); |
| 1468 | mDispatcher->notifyMotion(&motionArgs); |
| 1469 | |
| 1470 | InputEvent* event = window->consume(); |
| 1471 | ASSERT_NE(event, nullptr); |
| 1472 | |
| 1473 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1474 | ASSERT_NE(verified, nullptr); |
| 1475 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 1476 | |
| 1477 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 1478 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 1479 | EXPECT_EQ(motionArgs.source, verified->source); |
| 1480 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 1481 | |
| 1482 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 1483 | |
| 1484 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 1485 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 1486 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 1487 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 1488 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 1489 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 1490 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 1491 | } |
| 1492 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1493 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 1494 | protected: |
| 1495 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 1496 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 1497 | |
| 1498 | sp<FakeApplicationHandle> mApp; |
| 1499 | sp<FakeWindowHandle> mWindow; |
| 1500 | |
| 1501 | virtual void SetUp() override { |
| 1502 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 1503 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 1504 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 1505 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 1506 | ASSERT_EQ(OK, mDispatcher->start()); |
| 1507 | |
| 1508 | setUpWindow(); |
| 1509 | } |
| 1510 | |
| 1511 | void setUpWindow() { |
| 1512 | mApp = new FakeApplicationHandle(); |
| 1513 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1514 | |
| 1515 | mWindow->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1516 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1517 | |
| 1518 | mWindow->consumeFocusEvent(true); |
| 1519 | } |
| 1520 | |
| 1521 | void sendAndConsumeKeyDown() { |
| 1522 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1523 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 1524 | mDispatcher->notifyKey(&keyArgs); |
| 1525 | |
| 1526 | // Window should receive key down event. |
| 1527 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1528 | } |
| 1529 | |
| 1530 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 1531 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 1532 | InputEvent* repeatEvent = mWindow->consume(); |
| 1533 | ASSERT_NE(nullptr, repeatEvent); |
| 1534 | |
| 1535 | uint32_t eventType = repeatEvent->getType(); |
| 1536 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 1537 | |
| 1538 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 1539 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 1540 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 1541 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 1542 | } |
| 1543 | |
| 1544 | void sendAndConsumeKeyUp() { |
| 1545 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 1546 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 1547 | mDispatcher->notifyKey(&keyArgs); |
| 1548 | |
| 1549 | // Window should receive key down event. |
| 1550 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1551 | 0 /*expectedFlags*/); |
| 1552 | } |
| 1553 | }; |
| 1554 | |
| 1555 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
| 1556 | sendAndConsumeKeyDown(); |
| 1557 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1558 | expectKeyRepeatOnce(repeatCount); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
| 1563 | sendAndConsumeKeyDown(); |
| 1564 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 1565 | sendAndConsumeKeyUp(); |
| 1566 | mWindow->assertNoEvents(); |
| 1567 | } |
| 1568 | |
| 1569 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
| 1570 | sendAndConsumeKeyDown(); |
| 1571 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1572 | InputEvent* repeatEvent = mWindow->consume(); |
| 1573 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 1574 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 1575 | IdGenerator::getSource(repeatEvent->getId())); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
| 1580 | sendAndConsumeKeyDown(); |
| 1581 | |
| 1582 | std::unordered_set<int32_t> idSet; |
| 1583 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 1584 | InputEvent* repeatEvent = mWindow->consume(); |
| 1585 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 1586 | int32_t id = repeatEvent->getId(); |
| 1587 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 1588 | idSet.insert(id); |
| 1589 | } |
| 1590 | } |
| 1591 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1592 | /* Test InputDispatcher for MultiDisplay */ |
| 1593 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 1594 | public: |
| 1595 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1596 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1597 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1598 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1599 | application1 = new FakeApplicationHandle(); |
| 1600 | windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", |
| 1601 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1602 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1603 | // Set focus window for primary display, but focused display would be second one. |
| 1604 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1605 | windowInPrimary->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1607 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1608 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1609 | application2 = new FakeApplicationHandle(); |
| 1610 | windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", |
| 1611 | SECOND_DISPLAY_ID); |
| 1612 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1613 | // Set focus display to second one. |
| 1614 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 1615 | // Set focus window for second display. |
| 1616 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1617 | windowInSecondary->setFocus(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1618 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1619 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1620 | } |
| 1621 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1622 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1623 | InputDispatcherTest::TearDown(); |
| 1624 | |
| 1625 | application1.clear(); |
| 1626 | windowInPrimary.clear(); |
| 1627 | application2.clear(); |
| 1628 | windowInSecondary.clear(); |
| 1629 | } |
| 1630 | |
| 1631 | protected: |
| 1632 | sp<FakeApplicationHandle> application1; |
| 1633 | sp<FakeWindowHandle> windowInPrimary; |
| 1634 | sp<FakeApplicationHandle> application2; |
| 1635 | sp<FakeWindowHandle> windowInSecondary; |
| 1636 | }; |
| 1637 | |
| 1638 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 1639 | // Test touch down on primary display. |
| 1640 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1641 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1642 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1643 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1644 | windowInSecondary->assertNoEvents(); |
| 1645 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1646 | // Test touch down on second display. |
| 1647 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1648 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1649 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1650 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1651 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1652 | } |
| 1653 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1654 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1655 | // Test inject a key down with display id specified. |
| 1656 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1657 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1658 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1659 | windowInSecondary->assertNoEvents(); |
| 1660 | |
| 1661 | // Test inject a key down without display id specified. |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1662 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 1663 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1664 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1665 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1666 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1667 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1668 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1669 | |
| 1670 | // Expect old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1671 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 1672 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1673 | |
| 1674 | // Test inject a key down, should timeout because of no target window. |
| 1675 | ASSERT_EQ(INPUT_EVENT_INJECTION_TIMED_OUT, injectKeyDown(mDispatcher)) |
| 1676 | << "Inject key event should return INPUT_EVENT_INJECTION_TIMED_OUT"; |
| 1677 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1678 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1679 | windowInSecondary->assertNoEvents(); |
| 1680 | } |
| 1681 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1682 | // Test per-display input monitors for motion event. |
| 1683 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1684 | FakeMonitorReceiver monitorInPrimary = |
| 1685 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 1686 | FakeMonitorReceiver monitorInSecondary = |
| 1687 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1688 | |
| 1689 | // Test touch down on primary display. |
| 1690 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1691 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1692 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1693 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1694 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1695 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1696 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1697 | |
| 1698 | // Test touch down on second display. |
| 1699 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1700 | AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 1701 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1702 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1703 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1704 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1705 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1706 | |
| 1707 | // Test inject a non-pointer motion event. |
| 1708 | // If specific a display, it will dispatch to the focused window of particular display, |
| 1709 | // or it will dispatch to the focused window of focused display. |
| 1710 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1711 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 1712 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1713 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1714 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1715 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1716 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | // Test per-display input monitors for key event. |
| 1720 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
| 1721 | //Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1722 | FakeMonitorReceiver monitorInPrimary = |
| 1723 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 1724 | FakeMonitorReceiver monitorInSecondary = |
| 1725 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1726 | |
| 1727 | // Test inject a key down. |
| 1728 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher)) |
| 1729 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
| 1730 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1731 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1732 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1733 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1734 | } |
| 1735 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1736 | class InputFilterTest : public InputDispatcherTest { |
| 1737 | protected: |
| 1738 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 1739 | |
| 1740 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 1741 | NotifyMotionArgs motionArgs; |
| 1742 | |
| 1743 | motionArgs = generateMotionArgs( |
| 1744 | AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 1745 | mDispatcher->notifyMotion(&motionArgs); |
| 1746 | motionArgs = generateMotionArgs( |
| 1747 | AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 1748 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1749 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1750 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 1751 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1752 | } else { |
| 1753 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | void testNotifyKey(bool expectToBeFiltered) { |
| 1758 | NotifyKeyArgs keyArgs; |
| 1759 | |
| 1760 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1761 | mDispatcher->notifyKey(&keyArgs); |
| 1762 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 1763 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1764 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1765 | |
| 1766 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 1767 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1768 | } else { |
| 1769 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 1770 | } |
| 1771 | } |
| 1772 | }; |
| 1773 | |
| 1774 | // Test InputFilter for MotionEvent |
| 1775 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 1776 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 1777 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 1778 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 1779 | |
| 1780 | // Enable InputFilter |
| 1781 | mDispatcher->setInputFilterEnabled(true); |
| 1782 | // Test touch on both primary and second display, and check if both events are filtered. |
| 1783 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 1784 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 1785 | |
| 1786 | // Disable InputFilter |
| 1787 | mDispatcher->setInputFilterEnabled(false); |
| 1788 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 1789 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 1790 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 1791 | } |
| 1792 | |
| 1793 | // Test InputFilter for KeyEvent |
| 1794 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 1795 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 1796 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 1797 | |
| 1798 | // Enable InputFilter |
| 1799 | mDispatcher->setInputFilterEnabled(true); |
| 1800 | // Send a key event, and check if it is filtered. |
| 1801 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 1802 | |
| 1803 | // Disable InputFilter |
| 1804 | mDispatcher->setInputFilterEnabled(false); |
| 1805 | // Send a key event, and check if it isn't filtered. |
| 1806 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 1807 | } |
| 1808 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1809 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1810 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1811 | InputDispatcherTest::SetUp(); |
| 1812 | |
| 1813 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1814 | mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top", |
| 1815 | ADISPLAY_ID_DEFAULT); |
| 1816 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 1817 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 1818 | // window. |
| 1819 | mUnfocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 1820 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1821 | mFocusedWindow = |
| 1822 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 1823 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
| 1824 | mFocusedWindow->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL); |
| 1825 | mFocusedWindowTouchPoint = 60; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1826 | |
| 1827 | // Set focused application. |
| 1828 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1829 | mFocusedWindow->setFocus(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1830 | |
| 1831 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1832 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1833 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 1836 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1837 | InputDispatcherTest::TearDown(); |
| 1838 | |
| 1839 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1840 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | protected: |
| 1844 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1845 | sp<FakeWindowHandle> mFocusedWindow; |
| 1846 | int32_t mFocusedWindowTouchPoint; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1847 | }; |
| 1848 | |
| 1849 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1850 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 1851 | // the onPointerDownOutsideFocus callback. |
| 1852 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
| 1853 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1854 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1855 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 1856 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1857 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1858 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1859 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 1860 | } |
| 1861 | |
| 1862 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 1863 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 1864 | // onPointerDownOutsideFocus callback. |
| 1865 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
| 1866 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectMotionDown(mDispatcher, |
| 1867 | AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, 20, 20)) |
| 1868 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 1869 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1870 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1871 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1872 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 1876 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 1877 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
| 1878 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1879 | << "Inject key event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 1880 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1881 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1882 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1883 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 1887 | // DOWN on the window that already has focus. Ensure no window received the |
| 1888 | // onPointerDownOutsideFocus callback. |
| 1889 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, |
| 1890 | OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 1891 | ASSERT_EQ(INPUT_EVENT_INJECTION_SUCCEEDED, |
| 1892 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1893 | mFocusedWindowTouchPoint, mFocusedWindowTouchPoint)) |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1894 | << "Inject motion event should return INPUT_EVENT_INJECTION_SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 1895 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1896 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1897 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1898 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1901 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 1902 | // windows that point to the same client token. |
| 1903 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 1904 | virtual void SetUp() override { |
| 1905 | InputDispatcherTest::SetUp(); |
| 1906 | |
| 1907 | sp<FakeApplicationHandle> application = new FakeApplicationHandle(); |
| 1908 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 1909 | ADISPLAY_ID_DEFAULT); |
| 1910 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 1911 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
| 1912 | mWindow1->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL | |
| 1913 | InputWindowInfo::FLAG_SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1914 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 1915 | |
| 1916 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 1917 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
| 1918 | mWindow2->setLayoutParamFlags(InputWindowInfo::FLAG_NOT_TOUCH_MODAL | |
| 1919 | InputWindowInfo::FLAG_SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1920 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 1921 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1922 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | protected: |
| 1926 | sp<FakeWindowHandle> mWindow1; |
| 1927 | sp<FakeWindowHandle> mWindow2; |
| 1928 | |
| 1929 | // Helper function to convert the point from screen coordinates into the window's space |
| 1930 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
| 1931 | float x = windowInfo->windowXScale * (point.x - windowInfo->frameLeft); |
| 1932 | float y = windowInfo->windowYScale * (point.y - windowInfo->frameTop); |
| 1933 | return {x, y}; |
| 1934 | } |
| 1935 | |
| 1936 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 1937 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1938 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1939 | InputEvent* event = window->consume(); |
| 1940 | |
| 1941 | ASSERT_NE(nullptr, event) << name.c_str() |
| 1942 | << ": consumer should have returned non-NULL event."; |
| 1943 | |
| 1944 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 1945 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 1946 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 1947 | |
| 1948 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 1949 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 1950 | |
| 1951 | for (size_t i = 0; i < points.size(); i++) { |
| 1952 | float expectedX = points[i].x; |
| 1953 | float expectedY = points[i].y; |
| 1954 | |
| 1955 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 1956 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 1957 | << ", got " << motionEvent.getX(i); |
| 1958 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 1959 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 1960 | << ", got " << motionEvent.getY(i); |
| 1961 | } |
| 1962 | } |
| 1963 | }; |
| 1964 | |
| 1965 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 1966 | // Touch Window 1 |
| 1967 | PointF touchedPoint = {10, 10}; |
| 1968 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
| 1969 | |
| 1970 | NotifyMotionArgs motionArgs = |
| 1971 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1972 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1973 | mDispatcher->notifyMotion(&motionArgs); |
| 1974 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 1975 | |
| 1976 | // Release touch on Window 1 |
| 1977 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 1978 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1979 | mDispatcher->notifyMotion(&motionArgs); |
| 1980 | // consume the UP event |
| 1981 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint}); |
| 1982 | |
| 1983 | // Touch Window 2 |
| 1984 | touchedPoint = {150, 150}; |
| 1985 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 1986 | |
| 1987 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1988 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 1989 | mDispatcher->notifyMotion(&motionArgs); |
| 1990 | |
| 1991 | // Consuming from window1 since it's the window that has the InputReceiver |
| 1992 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 1993 | } |
| 1994 | |
| 1995 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentScale) { |
| 1996 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 1997 | |
| 1998 | // Touch Window 1 |
| 1999 | PointF touchedPoint = {10, 10}; |
| 2000 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
| 2001 | |
| 2002 | NotifyMotionArgs motionArgs = |
| 2003 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2004 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2005 | mDispatcher->notifyMotion(&motionArgs); |
| 2006 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 2007 | |
| 2008 | // Release touch on Window 1 |
| 2009 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 2010 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2011 | mDispatcher->notifyMotion(&motionArgs); |
| 2012 | // consume the UP event |
| 2013 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_UP, {expectedPoint}); |
| 2014 | |
| 2015 | // Touch Window 2 |
| 2016 | touchedPoint = {150, 150}; |
| 2017 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 2018 | |
| 2019 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2020 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 2021 | mDispatcher->notifyMotion(&motionArgs); |
| 2022 | |
| 2023 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2024 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, {expectedPoint}); |
| 2025 | } |
| 2026 | |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2027 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentScale) { |
| 2028 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2029 | |
| 2030 | // Touch Window 1 |
| 2031 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2032 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2033 | |
| 2034 | NotifyMotionArgs motionArgs = |
| 2035 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2036 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2037 | mDispatcher->notifyMotion(&motionArgs); |
| 2038 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2039 | |
| 2040 | // Touch Window 2 |
| 2041 | int32_t actionPointerDown = |
| 2042 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2043 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2044 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2045 | |
| 2046 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2047 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2048 | mDispatcher->notifyMotion(&motionArgs); |
| 2049 | |
| 2050 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2051 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2052 | } |
| 2053 | |
| 2054 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentScale) { |
| 2055 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2056 | |
| 2057 | // Touch Window 1 |
| 2058 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2059 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2060 | |
| 2061 | NotifyMotionArgs motionArgs = |
| 2062 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2063 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2064 | mDispatcher->notifyMotion(&motionArgs); |
| 2065 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2066 | |
| 2067 | // Touch Window 2 |
| 2068 | int32_t actionPointerDown = |
| 2069 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2070 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2071 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2072 | |
| 2073 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2074 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2075 | mDispatcher->notifyMotion(&motionArgs); |
| 2076 | |
| 2077 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2078 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2079 | |
| 2080 | // Move both windows |
| 2081 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2082 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2083 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2084 | |
| 2085 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2086 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2087 | mDispatcher->notifyMotion(&motionArgs); |
| 2088 | |
| 2089 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints); |
| 2090 | } |
| 2091 | |
| 2092 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 2093 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 2094 | |
| 2095 | // Touch Window 1 |
| 2096 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2097 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
| 2098 | |
| 2099 | NotifyMotionArgs motionArgs = |
| 2100 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2101 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2102 | mDispatcher->notifyMotion(&motionArgs); |
| 2103 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_DOWN, expectedPoints); |
| 2104 | |
| 2105 | // Touch Window 2 |
| 2106 | int32_t actionPointerDown = |
| 2107 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2108 | touchedPoints.emplace_back(PointF{150, 150}); |
| 2109 | expectedPoints.emplace_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2110 | |
| 2111 | motionArgs = generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, |
| 2112 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2113 | mDispatcher->notifyMotion(&motionArgs); |
| 2114 | |
| 2115 | // Consuming from window1 since it's the window that has the InputReceiver |
| 2116 | consumeMotionEvent(mWindow1, actionPointerDown, expectedPoints); |
| 2117 | |
| 2118 | // Move both windows |
| 2119 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2120 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2121 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2122 | |
| 2123 | motionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 2124 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2125 | mDispatcher->notifyMotion(&motionArgs); |
| 2126 | |
| 2127 | consumeMotionEvent(mWindow1, AMOTION_EVENT_ACTION_MOVE, expectedPoints); |
| 2128 | } |
| 2129 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 2130 | } // namespace android::inputdispatcher |