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