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> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 22 | #include <input/Input.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 23 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
| 25 | #include <linux/input.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <cinttypes> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 29 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 31 | using android::base::StringPrintf; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 32 | using android::os::InputEventInjectionResult; |
| 33 | using android::os::InputEventInjectionSync; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 34 | using namespace android::flag_operators; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 35 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 36 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | |
| 38 | // An arbitrary time value. |
| 39 | static const nsecs_t ARBITRARY_TIME = 1234; |
| 40 | |
| 41 | // An arbitrary device id. |
| 42 | static const int32_t DEVICE_ID = 1; |
| 43 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 44 | // An arbitrary display id. |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 45 | static const int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 46 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | // An arbitrary injector pid / uid pair that has permission to inject events. |
| 48 | static const int32_t INJECTOR_PID = 999; |
| 49 | static const int32_t INJECTOR_UID = 1001; |
| 50 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 51 | struct PointF { |
| 52 | float x; |
| 53 | float y; |
| 54 | }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 56 | /** |
| 57 | * Return a DOWN key event with KEYCODE_A. |
| 58 | */ |
| 59 | static KeyEvent getTestKeyEvent() { |
| 60 | KeyEvent event; |
| 61 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 62 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 63 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 64 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 65 | return event; |
| 66 | } |
| 67 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 68 | // --- FakeInputDispatcherPolicy --- |
| 69 | |
| 70 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
| 71 | InputDispatcherConfiguration mConfig; |
| 72 | |
| 73 | protected: |
| 74 | virtual ~FakeInputDispatcherPolicy() { |
| 75 | } |
| 76 | |
| 77 | public: |
| 78 | FakeInputDispatcherPolicy() { |
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 NotifyKeyArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 82 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_KEY, args.eventTime, args.action, |
| 83 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 86 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 87 | assertFilterInputEventWasCalled(AINPUT_EVENT_TYPE_MOTION, args.eventTime, args.action, |
| 88 | args.displayId); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 91 | void assertFilterInputEventWasNotCalled() { |
| 92 | std::scoped_lock lock(mLock); |
| 93 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 94 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 95 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 96 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 97 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 98 | ASSERT_TRUE(mConfigurationChangedTime) |
| 99 | << "Timed out waiting for configuration changed call"; |
| 100 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 101 | mConfigurationChangedTime = std::nullopt; |
| 102 | } |
| 103 | |
| 104 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 105 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 106 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 107 | // 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] | 108 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 109 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 110 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 111 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 112 | mLastNotifySwitch = std::nullopt; |
| 113 | } |
| 114 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 115 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 116 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 117 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 118 | mOnPointerDownToken.clear(); |
| 119 | } |
| 120 | |
| 121 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 122 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 123 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 124 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 127 | // This function must be called soon after the expected ANR timer starts, |
| 128 | // because we are also checking how much time has passed. |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 129 | void assertNotifyAnrWasCalled( |
| 130 | std::chrono::nanoseconds timeout, |
| 131 | const std::shared_ptr<InputApplicationHandle>& expectedApplication, |
| 132 | const sp<IBinder>& expectedToken) { |
| 133 | std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 134 | ASSERT_NO_FATAL_FAILURE(anrData = getNotifyAnrData(timeout)); |
| 135 | ASSERT_EQ(expectedApplication, anrData.first); |
| 136 | ASSERT_EQ(expectedToken, anrData.second); |
| 137 | } |
| 138 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 139 | std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> getNotifyAnrData( |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 140 | std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 141 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 142 | std::unique_lock lock(mLock); |
| 143 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 144 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 145 | |
| 146 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 147 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 148 | // before checking if ANR was called. |
| 149 | // Since dispatcher is not guaranteed to call notifyAnr right away, we need to provide |
| 150 | // it some time to act. 100ms seems reasonable. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 151 | mNotifyAnr.wait_for(lock, timeToWait, [this]() REQUIRES(mLock) { |
| 152 | return !mAnrApplications.empty() && !mAnrWindowTokens.empty(); |
| 153 | }); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 154 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 155 | if (mAnrApplications.empty() || mAnrWindowTokens.empty()) { |
| 156 | ADD_FAILURE() << "Did not receive ANR callback"; |
Siarhei Vishniakou | a72accd | 2020-09-22 21:43:09 -0500 | [diff] [blame] | 157 | return {}; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 158 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 159 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 160 | // the dispatcher started counting before this function was called |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 161 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 162 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 163 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 164 | << "ms, but waited " |
| 165 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 166 | << "ms instead"; |
| 167 | } |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 168 | std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 169 | std::make_pair(mAnrApplications.front(), mAnrWindowTokens.front()); |
| 170 | mAnrApplications.pop(); |
| 171 | mAnrWindowTokens.pop(); |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | void assertNotifyAnrWasNotCalled() { |
| 176 | std::scoped_lock lock(mLock); |
| 177 | ASSERT_TRUE(mAnrApplications.empty()); |
| 178 | ASSERT_TRUE(mAnrWindowTokens.empty()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 181 | void setKeyRepeatConfiguration(nsecs_t timeout, nsecs_t delay) { |
| 182 | mConfig.keyRepeatTimeout = timeout; |
| 183 | mConfig.keyRepeatDelay = delay; |
| 184 | } |
| 185 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 186 | void setAnrTimeout(std::chrono::nanoseconds timeout) { mAnrTimeout = timeout; } |
| 187 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 188 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 189 | std::mutex mLock; |
| 190 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 191 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 192 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 193 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 194 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 195 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 196 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 197 | std::queue<sp<IBinder>> mAnrWindowTokens GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 198 | std::condition_variable mNotifyAnr; |
| 199 | std::chrono::nanoseconds mAnrTimeout = 0ms; |
| 200 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 201 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 202 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 203 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 206 | std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>& application, |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 207 | const sp<IBinder>& windowToken, |
| 208 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 209 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 210 | mAnrApplications.push(application); |
| 211 | mAnrWindowTokens.push(windowToken); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 212 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 213 | return mAnrTimeout; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 216 | void notifyInputChannelBroken(const sp<IBinder>&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 217 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 218 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 219 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 220 | void notifyUntrustedTouch(const std::string& obscuringPackage) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 221 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 222 | void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | *outConfig = mConfig; |
| 224 | } |
| 225 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 226 | bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 227 | std::scoped_lock lock(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 228 | switch (inputEvent->getType()) { |
| 229 | case AINPUT_EVENT_TYPE_KEY: { |
| 230 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 231 | mFilteredEvent = std::make_unique<KeyEvent>(*keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | |
| 235 | case AINPUT_EVENT_TYPE_MOTION: { |
| 236 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(inputEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 237 | mFilteredEvent = std::make_unique<MotionEvent>(*motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 244 | void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 245 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 246 | void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 247 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 248 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 252 | bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 256 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 257 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 258 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 259 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 260 | * essentially a passthrough for notifySwitch. |
| 261 | */ |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 262 | mLastNotifySwitch = NotifySwitchArgs(1 /*id*/, when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 265 | void pokeUserActivity(nsecs_t, int32_t) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 266 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 267 | bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 268 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame^] | 269 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 270 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 271 | mOnPointerDownToken = newToken; |
| 272 | } |
| 273 | |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 274 | void assertFilterInputEventWasCalled(int type, nsecs_t eventTime, int32_t action, |
| 275 | int32_t displayId) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 276 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 277 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
| 278 | ASSERT_EQ(mFilteredEvent->getType(), type); |
| 279 | |
| 280 | if (type == AINPUT_EVENT_TYPE_KEY) { |
| 281 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*mFilteredEvent); |
| 282 | EXPECT_EQ(keyEvent.getEventTime(), eventTime); |
| 283 | EXPECT_EQ(keyEvent.getAction(), action); |
| 284 | EXPECT_EQ(keyEvent.getDisplayId(), displayId); |
| 285 | } else if (type == AINPUT_EVENT_TYPE_MOTION) { |
| 286 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*mFilteredEvent); |
| 287 | EXPECT_EQ(motionEvent.getEventTime(), eventTime); |
| 288 | EXPECT_EQ(motionEvent.getAction(), action); |
| 289 | EXPECT_EQ(motionEvent.getDisplayId(), displayId); |
| 290 | } else { |
| 291 | FAIL() << "Unknown type: " << type; |
| 292 | } |
| 293 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 294 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 295 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 296 | }; |
| 297 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 298 | // --- InputDispatcherTest --- |
| 299 | |
| 300 | class InputDispatcherTest : public testing::Test { |
| 301 | protected: |
| 302 | sp<FakeInputDispatcherPolicy> mFakePolicy; |
| 303 | sp<InputDispatcher> mDispatcher; |
| 304 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 305 | virtual void SetUp() override { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 306 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 307 | mDispatcher = new InputDispatcher(mFakePolicy); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 308 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 309 | //Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 310 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 313 | virtual void TearDown() override { |
| 314 | ASSERT_EQ(OK, mDispatcher->stop()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 315 | mFakePolicy.clear(); |
| 316 | mDispatcher.clear(); |
| 317 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * Used for debugging when writing the test |
| 321 | */ |
| 322 | void dumpDispatcherState() { |
| 323 | std::string dump; |
| 324 | mDispatcher->dump(dump); |
| 325 | std::stringstream ss(dump); |
| 326 | std::string to; |
| 327 | |
| 328 | while (std::getline(ss, to, '\n')) { |
| 329 | ALOGE("%s", to.c_str()); |
| 330 | } |
| 331 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 332 | |
| 333 | void setFocusedWindow(const sp<InputWindowHandle>& window, |
| 334 | const sp<InputWindowHandle>& focusedWindow = nullptr) { |
| 335 | FocusRequest request; |
| 336 | request.token = window->getToken(); |
| 337 | if (focusedWindow) { |
| 338 | request.focusedToken = focusedWindow->getToken(); |
| 339 | } |
| 340 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 341 | request.displayId = window->getInfo()->displayId; |
| 342 | mDispatcher->setFocusedWindow(request); |
| 343 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | |
| 347 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 348 | KeyEvent event; |
| 349 | |
| 350 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 351 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 352 | INVALID_HMAC, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 353 | /*action*/ -1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
| 354 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 355 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 356 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 357 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 358 | << "Should reject key events with undefined action."; |
| 359 | |
| 360 | // 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] | 361 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 362 | 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] | 363 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 364 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 365 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 366 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 367 | << "Should reject key events with ACTION_MULTIPLE."; |
| 368 | } |
| 369 | |
| 370 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 371 | MotionEvent event; |
| 372 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 373 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
| 374 | for (int i = 0; i <= MAX_POINTERS; i++) { |
| 375 | pointerProperties[i].clear(); |
| 376 | pointerProperties[i].id = i; |
| 377 | pointerCoords[i].clear(); |
| 378 | } |
| 379 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 380 | // Some constants commonly used below |
| 381 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 382 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 383 | constexpr int32_t metaState = AMETA_NONE; |
| 384 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 385 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 386 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 387 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 388 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 389 | /*action*/ -1, 0, 0, edgeFlags, metaState, 0, classification, |
| 390 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 391 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 392 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 393 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 394 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 395 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 396 | << "Should reject motion events with undefined action."; |
| 397 | |
| 398 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 399 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 400 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 401 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 402 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 403 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 404 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 405 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 406 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 407 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 408 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 409 | << "Should reject motion events with pointer down index too large."; |
| 410 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 411 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 412 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 413 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 414 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 415 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 416 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 417 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 418 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 419 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 420 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 421 | << "Should reject motion events with pointer down index too small."; |
| 422 | |
| 423 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 424 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 425 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 426 | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 427 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 428 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 429 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 430 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 431 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 432 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 433 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 434 | << "Should reject motion events with pointer up index too large."; |
| 435 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 436 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 437 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 438 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 439 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 440 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 441 | ARBITRARY_TIME, ARBITRARY_TIME, /*pointerCount*/ 1, pointerProperties, |
| 442 | pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 443 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 444 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 445 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 446 | << "Should reject motion events with pointer up index too small."; |
| 447 | |
| 448 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 449 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 450 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 451 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 452 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 453 | /*pointerCount*/ 0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 454 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 455 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 456 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 457 | << "Should reject motion events with 0 pointers."; |
| 458 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 459 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 460 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 461 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 462 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 463 | /*pointerCount*/ MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 464 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 465 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 466 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 467 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 468 | |
| 469 | // Rejects motion events with invalid pointer ids. |
| 470 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 471 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 472 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 473 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 474 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 475 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 476 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 477 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 478 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 479 | << "Should reject motion events with pointer ids less than 0."; |
| 480 | |
| 481 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 482 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 483 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 484 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 485 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 486 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 487 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 488 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 489 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 490 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 491 | |
| 492 | // Rejects motion events with duplicate pointer ids. |
| 493 | pointerProperties[0].id = 1; |
| 494 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 495 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 496 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 497 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 498 | AMOTION_EVENT_INVALID_CURSOR_POSITION, ARBITRARY_TIME, ARBITRARY_TIME, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 499 | /*pointerCount*/ 2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 500 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 501 | mDispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 502 | InputEventInjectionSync::NONE, 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 503 | << "Should reject motion events with duplicate pointer ids."; |
| 504 | } |
| 505 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 506 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 507 | |
| 508 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 509 | constexpr nsecs_t eventTime = 20; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 510 | NotifyConfigurationChangedArgs args(10 /*id*/, eventTime); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 511 | mDispatcher->notifyConfigurationChanged(&args); |
| 512 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 513 | |
| 514 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 515 | } |
| 516 | |
| 517 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 518 | NotifySwitchArgs args(10 /*id*/, 20 /*eventTime*/, 0 /*policyFlags*/, 1 /*switchValues*/, |
| 519 | 2 /*switchMask*/); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 520 | mDispatcher->notifySwitch(&args); |
| 521 | |
| 522 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 523 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 524 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 525 | } |
| 526 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 527 | // --- InputDispatcherTest SetInputWindowTest --- |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 528 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 529 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 5s; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 530 | |
| 531 | class FakeApplicationHandle : public InputApplicationHandle { |
| 532 | public: |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 533 | FakeApplicationHandle() { |
| 534 | mInfo.name = "Fake Application"; |
| 535 | mInfo.token = new BBinder(); |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 536 | mInfo.dispatchingTimeoutMillis = |
| 537 | std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 538 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 539 | virtual ~FakeApplicationHandle() {} |
| 540 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 541 | virtual bool updateInfo() override { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 542 | return true; |
| 543 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 544 | |
Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 545 | void setDispatchingTimeout(std::chrono::milliseconds timeout) { |
| 546 | mInfo.dispatchingTimeoutMillis = timeout.count(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 547 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 548 | }; |
| 549 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 550 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 551 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 552 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 553 | : mName(name) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 554 | mConsumer = std::make_unique<InputConsumer>(std::move(clientChannel)); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 557 | InputEvent* consume() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 558 | InputEvent* event; |
| 559 | std::optional<uint32_t> consumeSeq = receiveEvent(&event); |
| 560 | if (!consumeSeq) { |
| 561 | return nullptr; |
| 562 | } |
| 563 | finishEvent(*consumeSeq); |
| 564 | return event; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Receive an event without acknowledging it. |
| 569 | * Return the sequence number that could later be used to send finished signal. |
| 570 | */ |
| 571 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 572 | uint32_t consumeSeq; |
| 573 | InputEvent* event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 574 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 575 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 576 | status_t status = WOULD_BLOCK; |
| 577 | while (status == WOULD_BLOCK) { |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 578 | status = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 579 | &event); |
| 580 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
| 581 | if (elapsed > 100ms) { |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (status == WOULD_BLOCK) { |
| 587 | // Just means there's no event available. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 588 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | if (status != OK) { |
| 592 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 593 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 594 | } |
| 595 | if (event == nullptr) { |
| 596 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 597 | return std::nullopt; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 598 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 599 | if (outEvent != nullptr) { |
| 600 | *outEvent = event; |
| 601 | } |
| 602 | return consumeSeq; |
| 603 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 604 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 605 | /** |
| 606 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 607 | */ |
| 608 | void finishEvent(uint32_t consumeSeq) { |
| 609 | const status_t status = mConsumer->sendFinishedSignal(consumeSeq, true); |
| 610 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 614 | int32_t expectedFlags) { |
| 615 | InputEvent* event = consume(); |
| 616 | |
| 617 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 618 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 619 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 620 | << mName.c_str() << " expected " << inputEventTypeToString(expectedEventType) |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 621 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 622 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 623 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 624 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 625 | switch (expectedEventType) { |
| 626 | case AINPUT_EVENT_TYPE_KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 627 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
| 628 | EXPECT_EQ(expectedAction, keyEvent.getAction()); |
| 629 | EXPECT_EQ(expectedFlags, keyEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 630 | break; |
| 631 | } |
| 632 | case AINPUT_EVENT_TYPE_MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 633 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 634 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 635 | EXPECT_EQ(expectedFlags, motionEvent.getFlags()); |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 636 | break; |
| 637 | } |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 638 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 639 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 640 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 641 | default: { |
| 642 | FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType; |
| 643 | } |
| 644 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 645 | } |
| 646 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 647 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
| 648 | InputEvent* event = consume(); |
| 649 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 650 | << ": consumer should have returned non-NULL event."; |
| 651 | ASSERT_EQ(AINPUT_EVENT_TYPE_FOCUS, event->getType()) |
| 652 | << "Got " << inputEventTypeToString(event->getType()) |
| 653 | << " event instead of FOCUS event"; |
| 654 | |
| 655 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 656 | << mName.c_str() << ": event displayId should always be NONE."; |
| 657 | |
| 658 | FocusEvent* focusEvent = static_cast<FocusEvent*>(event); |
| 659 | EXPECT_EQ(hasFocus, focusEvent->getHasFocus()); |
| 660 | EXPECT_EQ(inTouchMode, focusEvent->getInTouchMode()); |
| 661 | } |
| 662 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 663 | void assertNoEvents() { |
| 664 | InputEvent* event = consume(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 665 | if (event == nullptr) { |
| 666 | return; |
| 667 | } |
| 668 | if (event->getType() == AINPUT_EVENT_TYPE_KEY) { |
| 669 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
| 670 | ADD_FAILURE() << "Received key event " |
| 671 | << KeyEvent::actionToString(keyEvent.getAction()); |
| 672 | } else if (event->getType() == AINPUT_EVENT_TYPE_MOTION) { |
| 673 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 674 | ADD_FAILURE() << "Received motion event " |
| 675 | << MotionEvent::actionToString(motionEvent.getAction()); |
| 676 | } else if (event->getType() == AINPUT_EVENT_TYPE_FOCUS) { |
| 677 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 678 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 679 | << (focusEvent.getHasFocus() ? "true" : "false"); |
| 680 | } |
| 681 | FAIL() << mName.c_str() |
| 682 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | sp<IBinder> getToken() { return mConsumer->getChannel()->getConnectionToken(); } |
| 686 | |
| 687 | protected: |
| 688 | std::unique_ptr<InputConsumer> mConsumer; |
| 689 | PreallocatedInputEventFactory mEventFactory; |
| 690 | |
| 691 | std::string mName; |
| 692 | }; |
| 693 | |
| 694 | class FakeWindowHandle : public InputWindowHandle { |
| 695 | public: |
| 696 | static const int32_t WIDTH = 600; |
| 697 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 698 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 699 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 700 | const sp<InputDispatcher>& dispatcher, const std::string name, |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 701 | int32_t displayId, std::optional<sp<IBinder>> token = std::nullopt) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 702 | : mName(name) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 703 | if (token == std::nullopt) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 704 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 705 | dispatcher->createInputChannel(name); |
| 706 | token = (*channel)->getConnectionToken(); |
| 707 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | inputApplicationHandle->updateInfo(); |
| 711 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 712 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 713 | mInfo.token = *token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 714 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 715 | mInfo.name = name; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 716 | mInfo.type = InputWindowInfo::Type::APPLICATION; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 717 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 718 | mInfo.frameLeft = 0; |
| 719 | mInfo.frameTop = 0; |
| 720 | mInfo.frameRight = WIDTH; |
| 721 | mInfo.frameBottom = HEIGHT; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 722 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 723 | mInfo.globalScaleFactor = 1.0; |
| 724 | mInfo.touchableRegion.clear(); |
| 725 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
| 726 | mInfo.visible = true; |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 727 | mInfo.focusable = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 728 | mInfo.hasWallpaper = false; |
| 729 | mInfo.paused = false; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 730 | mInfo.ownerPid = INJECTOR_PID; |
| 731 | mInfo.ownerUid = INJECTOR_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 732 | mInfo.displayId = displayId; |
| 733 | } |
| 734 | |
| 735 | virtual bool updateInfo() { return true; } |
| 736 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 737 | void setFocusable(bool focusable) { mInfo.focusable = focusable; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 738 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 739 | void setVisible(bool visible) { mInfo.visible = visible; } |
| 740 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 741 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 742 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 745 | void setPaused(bool paused) { mInfo.paused = paused; } |
| 746 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 747 | void setFrame(const Rect& frame) { |
| 748 | mInfo.frameLeft = frame.left; |
| 749 | mInfo.frameTop = frame.top; |
| 750 | mInfo.frameRight = frame.right; |
| 751 | mInfo.frameBottom = frame.bottom; |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 752 | mInfo.transform.set(frame.left, frame.top); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 753 | mInfo.touchableRegion.clear(); |
| 754 | mInfo.addTouchableRegion(frame); |
| 755 | } |
| 756 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 757 | void setFlags(Flags<InputWindowInfo::Flag> flags) { mInfo.flags = flags; } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 758 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 759 | void setInputFeatures(InputWindowInfo::Feature features) { mInfo.inputFeatures = features; } |
| 760 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 761 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 762 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 763 | } |
| 764 | |
| 765 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 766 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 767 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 768 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 769 | expectedFlags); |
| 770 | } |
| 771 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 772 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 773 | consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, expectedDisplayId, expectedFlags); |
| 774 | } |
| 775 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 776 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 777 | int32_t expectedFlags = 0) { |
| 778 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, expectedDisplayId, |
| 779 | expectedFlags); |
| 780 | } |
| 781 | |
| 782 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 783 | int32_t expectedFlags = 0) { |
| 784 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, expectedDisplayId, |
| 785 | expectedFlags); |
| 786 | } |
| 787 | |
| 788 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 789 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 790 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, expectedDisplayId, |
| 791 | expectedFlags); |
| 792 | } |
| 793 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 794 | void consumeMotionPointerDown(int32_t pointerIdx, |
| 795 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, int32_t expectedFlags = 0) { |
| 796 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
| 797 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 798 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 799 | } |
| 800 | |
| 801 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 802 | int32_t expectedFlags = 0) { |
| 803 | int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
| 804 | | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 805 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, action, expectedDisplayId, expectedFlags); |
| 806 | } |
| 807 | |
| 808 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 809 | int32_t expectedFlags = 0) { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 810 | consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, expectedDisplayId, |
| 811 | expectedFlags); |
| 812 | } |
| 813 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 814 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 815 | ASSERT_NE(mInputReceiver, nullptr) |
| 816 | << "Cannot consume events from a window with no receiver"; |
| 817 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 818 | } |
| 819 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 820 | void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId, |
| 821 | int32_t expectedFlags) { |
| 822 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid consume event on window with no receiver"; |
| 823 | mInputReceiver->consumeEvent(expectedEventType, expectedAction, expectedDisplayId, |
| 824 | expectedFlags); |
| 825 | } |
| 826 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 827 | std::optional<uint32_t> receiveEvent(InputEvent** outEvent = nullptr) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 828 | if (mInputReceiver == nullptr) { |
| 829 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 830 | return std::nullopt; |
| 831 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 832 | return mInputReceiver->receiveEvent(outEvent); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | void finishEvent(uint32_t sequenceNum) { |
| 836 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 837 | mInputReceiver->finishEvent(sequenceNum); |
| 838 | } |
| 839 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 840 | InputEvent* consume() { |
| 841 | if (mInputReceiver == nullptr) { |
| 842 | return nullptr; |
| 843 | } |
| 844 | return mInputReceiver->consume(); |
| 845 | } |
| 846 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 847 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 848 | if (mInputReceiver == nullptr && |
| 849 | mInfo.inputFeatures.test(InputWindowInfo::Feature::NO_INPUT_CHANNEL)) { |
| 850 | return; // Can't receive events if the window does not have input channel |
| 851 | } |
| 852 | ASSERT_NE(nullptr, mInputReceiver) |
| 853 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 854 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 855 | } |
| 856 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 857 | sp<IBinder> getToken() { return mInfo.token; } |
| 858 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 859 | const std::string& getName() { return mName; } |
| 860 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 861 | private: |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 862 | const std::string mName; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 863 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 864 | 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] | 865 | }; |
| 866 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 867 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 868 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 869 | static InputEventInjectionResult injectKey( |
| 870 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t repeatCount, |
| 871 | int32_t displayId = ADISPLAY_ID_NONE, |
| 872 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
| 873 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 874 | KeyEvent event; |
| 875 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 876 | |
| 877 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 878 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 879 | INVALID_HMAC, action, /* flags */ 0, AKEYCODE_A, KEY_A, AMETA_NONE, |
| 880 | repeatCount, currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 881 | |
| 882 | // Inject event until dispatch out. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 883 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, syncMode, |
| 884 | injectionTimeout, |
| 885 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 886 | } |
| 887 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 888 | static InputEventInjectionResult injectKeyDown(const sp<InputDispatcher>& dispatcher, |
| 889 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 890 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /* repeatCount */ 0, displayId); |
| 891 | } |
| 892 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 893 | static InputEventInjectionResult injectKeyUp(const sp<InputDispatcher>& dispatcher, |
| 894 | int32_t displayId = ADISPLAY_ID_NONE) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 895 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /* repeatCount */ 0, displayId); |
| 896 | } |
| 897 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 898 | class PointerBuilder { |
| 899 | public: |
| 900 | PointerBuilder(int32_t id, int32_t toolType) { |
| 901 | mProperties.clear(); |
| 902 | mProperties.id = id; |
| 903 | mProperties.toolType = toolType; |
| 904 | mCoords.clear(); |
| 905 | } |
| 906 | |
| 907 | PointerBuilder& x(float x) { return axis(AMOTION_EVENT_AXIS_X, x); } |
| 908 | |
| 909 | PointerBuilder& y(float y) { return axis(AMOTION_EVENT_AXIS_Y, y); } |
| 910 | |
| 911 | PointerBuilder& axis(int32_t axis, float value) { |
| 912 | mCoords.setAxisValue(axis, value); |
| 913 | return *this; |
| 914 | } |
| 915 | |
| 916 | PointerProperties buildProperties() const { return mProperties; } |
| 917 | |
| 918 | PointerCoords buildCoords() const { return mCoords; } |
| 919 | |
| 920 | private: |
| 921 | PointerProperties mProperties; |
| 922 | PointerCoords mCoords; |
| 923 | }; |
| 924 | |
| 925 | class MotionEventBuilder { |
| 926 | public: |
| 927 | MotionEventBuilder(int32_t action, int32_t source) { |
| 928 | mAction = action; |
| 929 | mSource = source; |
| 930 | mEventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 931 | } |
| 932 | |
| 933 | MotionEventBuilder& eventTime(nsecs_t eventTime) { |
| 934 | mEventTime = eventTime; |
| 935 | return *this; |
| 936 | } |
| 937 | |
| 938 | MotionEventBuilder& displayId(int32_t displayId) { |
| 939 | mDisplayId = displayId; |
| 940 | return *this; |
| 941 | } |
| 942 | |
| 943 | MotionEventBuilder& actionButton(int32_t actionButton) { |
| 944 | mActionButton = actionButton; |
| 945 | return *this; |
| 946 | } |
| 947 | |
| 948 | MotionEventBuilder& buttonState(int32_t actionButton) { |
| 949 | mActionButton = actionButton; |
| 950 | return *this; |
| 951 | } |
| 952 | |
| 953 | MotionEventBuilder& rawXCursorPosition(float rawXCursorPosition) { |
| 954 | mRawXCursorPosition = rawXCursorPosition; |
| 955 | return *this; |
| 956 | } |
| 957 | |
| 958 | MotionEventBuilder& rawYCursorPosition(float rawYCursorPosition) { |
| 959 | mRawYCursorPosition = rawYCursorPosition; |
| 960 | return *this; |
| 961 | } |
| 962 | |
| 963 | MotionEventBuilder& pointer(PointerBuilder pointer) { |
| 964 | mPointers.push_back(pointer); |
| 965 | return *this; |
| 966 | } |
| 967 | |
| 968 | MotionEvent build() { |
| 969 | std::vector<PointerProperties> pointerProperties; |
| 970 | std::vector<PointerCoords> pointerCoords; |
| 971 | for (const PointerBuilder& pointer : mPointers) { |
| 972 | pointerProperties.push_back(pointer.buildProperties()); |
| 973 | pointerCoords.push_back(pointer.buildCoords()); |
| 974 | } |
| 975 | |
| 976 | // Set mouse cursor position for the most common cases to avoid boilerplate. |
| 977 | if (mSource == AINPUT_SOURCE_MOUSE && |
| 978 | !MotionEvent::isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition) && |
| 979 | mPointers.size() == 1) { |
| 980 | mRawXCursorPosition = pointerCoords[0].getX(); |
| 981 | mRawYCursorPosition = pointerCoords[0].getY(); |
| 982 | } |
| 983 | |
| 984 | MotionEvent event; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 985 | ui::Transform identityTransform; |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 986 | event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC, |
| 987 | mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 988 | mButtonState, MotionClassification::NONE, identityTransform, |
| 989 | /* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition, |
| 990 | mRawYCursorPosition, mEventTime, mEventTime, mPointers.size(), |
| 991 | pointerProperties.data(), pointerCoords.data()); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 992 | |
| 993 | return event; |
| 994 | } |
| 995 | |
| 996 | private: |
| 997 | int32_t mAction; |
| 998 | int32_t mSource; |
| 999 | nsecs_t mEventTime; |
| 1000 | int32_t mDisplayId{ADISPLAY_ID_DEFAULT}; |
| 1001 | int32_t mActionButton{0}; |
| 1002 | int32_t mButtonState{0}; |
| 1003 | float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1004 | float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION}; |
| 1005 | |
| 1006 | std::vector<PointerBuilder> mPointers; |
| 1007 | }; |
| 1008 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1009 | static InputEventInjectionResult injectMotionEvent( |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1010 | const sp<InputDispatcher>& dispatcher, const MotionEvent& event, |
| 1011 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1012 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1013 | return dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, injectionMode, |
| 1014 | injectionTimeout, |
| 1015 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
| 1016 | } |
| 1017 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1018 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1019 | const sp<InputDispatcher>& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1020 | const PointF& position, |
| 1021 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1022 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1023 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1024 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1025 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC)) { |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1026 | MotionEvent event = MotionEventBuilder(action, source) |
| 1027 | .displayId(displayId) |
| 1028 | .eventTime(eventTime) |
| 1029 | .rawXCursorPosition(cursorPosition.x) |
| 1030 | .rawYCursorPosition(cursorPosition.y) |
| 1031 | .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER) |
| 1032 | .x(position.x) |
| 1033 | .y(position.y)) |
| 1034 | .build(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1035 | |
| 1036 | // Inject event until dispatch out. |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1037 | return injectMotionEvent(dispatcher, event); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1040 | static InputEventInjectionResult injectMotionDown(const sp<InputDispatcher>& dispatcher, |
| 1041 | int32_t source, int32_t displayId, |
| 1042 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1043 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1046 | static InputEventInjectionResult injectMotionUp(const sp<InputDispatcher>& dispatcher, |
| 1047 | int32_t source, int32_t displayId, |
| 1048 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1049 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1052 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1053 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1054 | // Define a valid key event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1055 | NotifyKeyArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
| 1056 | POLICY_FLAG_PASS_TO_USER, action, /* flags */ 0, AKEYCODE_A, KEY_A, |
| 1057 | AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1058 | |
| 1059 | return args; |
| 1060 | } |
| 1061 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1062 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId, |
| 1063 | const std::vector<PointF>& points) { |
| 1064 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1065 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1066 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1067 | } |
| 1068 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1069 | PointerProperties pointerProperties[pointerCount]; |
| 1070 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1071 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1072 | for (size_t i = 0; i < pointerCount; i++) { |
| 1073 | pointerProperties[i].clear(); |
| 1074 | pointerProperties[i].id = i; |
| 1075 | pointerProperties[i].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1076 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1077 | pointerCoords[i].clear(); |
| 1078 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1079 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1080 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1081 | |
| 1082 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1083 | // Define a valid motion event. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1084 | NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, source, displayId, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1085 | POLICY_FLAG_PASS_TO_USER, action, /* actionButton */ 0, /* flags */ 0, |
| 1086 | AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1087 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
| 1088 | pointerCoords, /* xPrecision */ 0, /* yPrecision */ 0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1089 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 1090 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1091 | |
| 1092 | return args; |
| 1093 | } |
| 1094 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1095 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1096 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1097 | } |
| 1098 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1099 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1101 | sp<FakeWindowHandle> window = new FakeWindowHandle(application, mDispatcher, "Fake Window", |
| 1102 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1103 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1104 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1105 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1106 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1107 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1108 | |
| 1109 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1110 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1113 | /** |
| 1114 | * Calling setInputWindows once with FLAG_NOT_TOUCH_MODAL should not cause any issues. |
| 1115 | * To ensure that window receives only events that were directly inside of it, add |
| 1116 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1117 | * when finding touched windows. |
| 1118 | * This test serves as a sanity check for the next test, where setInputWindows is |
| 1119 | * called twice. |
| 1120 | */ |
| 1121 | TEST_F(InputDispatcherTest, SetInputWindowOnce_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1123 | sp<FakeWindowHandle> window = |
| 1124 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1125 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1126 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1127 | |
| 1128 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1130 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1131 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1132 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1133 | |
| 1134 | // Window should receive motion event. |
| 1135 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Calling setInputWindows twice, with the same info, should not cause any issues. |
| 1140 | * To ensure that window receives only events that were directly inside of it, add |
| 1141 | * FLAG_NOT_TOUCH_MODAL. This will enforce using the touchableRegion of the input |
| 1142 | * when finding touched windows. |
| 1143 | */ |
| 1144 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1145 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1146 | sp<FakeWindowHandle> window = |
| 1147 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1148 | window->setFrame(Rect(0, 0, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1149 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1150 | |
| 1151 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1152 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1154 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 1155 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1156 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1157 | |
| 1158 | // Window should receive motion event. |
| 1159 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1160 | } |
| 1161 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1162 | // The foreground window should receive the first touch down event. |
| 1163 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1164 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1165 | sp<FakeWindowHandle> windowTop = new FakeWindowHandle(application, mDispatcher, "Top", |
| 1166 | ADISPLAY_ID_DEFAULT); |
| 1167 | sp<FakeWindowHandle> windowSecond = new FakeWindowHandle(application, mDispatcher, "Second", |
| 1168 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1169 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1170 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1171 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 1172 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 1173 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1174 | |
| 1175 | // 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] | 1176 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1177 | windowSecond->assertNoEvents(); |
| 1178 | } |
| 1179 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1180 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1182 | sp<FakeWindowHandle> windowLeft = |
| 1183 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1184 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1185 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1186 | sp<FakeWindowHandle> windowRight = |
| 1187 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1188 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1189 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1190 | |
| 1191 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1192 | |
| 1193 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
| 1194 | |
| 1195 | // Start cursor position in right window so that we can move the cursor to left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1196 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1197 | injectMotionEvent(mDispatcher, |
| 1198 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1199 | AINPUT_SOURCE_MOUSE) |
| 1200 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1201 | .x(900) |
| 1202 | .y(400)) |
| 1203 | .build())); |
| 1204 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1205 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1206 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1207 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1208 | |
| 1209 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1210 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1211 | injectMotionEvent(mDispatcher, |
| 1212 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1213 | AINPUT_SOURCE_MOUSE) |
| 1214 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1215 | .x(300) |
| 1216 | .y(400)) |
| 1217 | .build())); |
| 1218 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1219 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1220 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1221 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1222 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1223 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1224 | |
| 1225 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1227 | injectMotionEvent(mDispatcher, |
| 1228 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1229 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1230 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1231 | .x(300) |
| 1232 | .y(400)) |
| 1233 | .build())); |
| 1234 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1235 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1237 | injectMotionEvent(mDispatcher, |
| 1238 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1239 | AINPUT_SOURCE_MOUSE) |
| 1240 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1241 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1242 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1243 | .x(300) |
| 1244 | .y(400)) |
| 1245 | .build())); |
| 1246 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1247 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1248 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1249 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1250 | injectMotionEvent(mDispatcher, |
| 1251 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1252 | AINPUT_SOURCE_MOUSE) |
| 1253 | .buttonState(0) |
| 1254 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1255 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1256 | .x(300) |
| 1257 | .y(400)) |
| 1258 | .build())); |
| 1259 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1260 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1261 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1263 | injectMotionEvent(mDispatcher, |
| 1264 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1265 | .buttonState(0) |
| 1266 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1267 | .x(300) |
| 1268 | .y(400)) |
| 1269 | .build())); |
| 1270 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1271 | |
| 1272 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1273 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1274 | injectMotionEvent(mDispatcher, |
| 1275 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1276 | AINPUT_SOURCE_MOUSE) |
| 1277 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1278 | .x(900) |
| 1279 | .y(400)) |
| 1280 | .build())); |
| 1281 | windowLeft->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1282 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1283 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1284 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1285 | windowRight->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1286 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1287 | } |
| 1288 | |
| 1289 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 1290 | // directly in this test. |
| 1291 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1292 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1293 | sp<FakeWindowHandle> window = |
| 1294 | new FakeWindowHandle(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1295 | window->setFrame(Rect(0, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1296 | window->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1297 | |
| 1298 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1299 | |
| 1300 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 1301 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1302 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1303 | injectMotionEvent(mDispatcher, |
| 1304 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1305 | AINPUT_SOURCE_MOUSE) |
| 1306 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1307 | .x(300) |
| 1308 | .y(400)) |
| 1309 | .build())); |
| 1310 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 1311 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1312 | |
| 1313 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1315 | injectMotionEvent(mDispatcher, |
| 1316 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1317 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1318 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1319 | .x(300) |
| 1320 | .y(400)) |
| 1321 | .build())); |
| 1322 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1323 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1325 | injectMotionEvent(mDispatcher, |
| 1326 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1327 | AINPUT_SOURCE_MOUSE) |
| 1328 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1329 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1330 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1331 | .x(300) |
| 1332 | .y(400)) |
| 1333 | .build())); |
| 1334 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 1335 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1336 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1338 | injectMotionEvent(mDispatcher, |
| 1339 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1340 | AINPUT_SOURCE_MOUSE) |
| 1341 | .buttonState(0) |
| 1342 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 1343 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1344 | .x(300) |
| 1345 | .y(400)) |
| 1346 | .build())); |
| 1347 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 1348 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1349 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1351 | injectMotionEvent(mDispatcher, |
| 1352 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 1353 | .buttonState(0) |
| 1354 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1355 | .x(300) |
| 1356 | .y(400)) |
| 1357 | .build())); |
| 1358 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1359 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1361 | injectMotionEvent(mDispatcher, |
| 1362 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1363 | AINPUT_SOURCE_MOUSE) |
| 1364 | .pointer(PointerBuilder(0, AMOTION_EVENT_TOOL_TYPE_MOUSE) |
| 1365 | .x(300) |
| 1366 | .y(400)) |
| 1367 | .build())); |
| 1368 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 1369 | ADISPLAY_ID_DEFAULT, 0 /* expectedFlag */); |
| 1370 | } |
| 1371 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1372 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1374 | |
| 1375 | sp<FakeWindowHandle> windowLeft = |
| 1376 | new FakeWindowHandle(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1377 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1378 | windowLeft->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1379 | sp<FakeWindowHandle> windowRight = |
| 1380 | new FakeWindowHandle(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1381 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1382 | windowRight->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1383 | |
| 1384 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1385 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1386 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowLeft, windowRight}}}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1387 | |
| 1388 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 1389 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1391 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1392 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1393 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1394 | windowRight->assertNoEvents(); |
| 1395 | } |
| 1396 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1397 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1398 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1399 | sp<FakeWindowHandle> window = |
| 1400 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1401 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1402 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1403 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1404 | setFocusedWindow(window); |
| 1405 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1406 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1407 | |
| 1408 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1409 | mDispatcher->notifyKey(&keyArgs); |
| 1410 | |
| 1411 | // Window should receive key down event. |
| 1412 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1413 | |
| 1414 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 1415 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1416 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1417 | mDispatcher->notifyDeviceReset(&args); |
| 1418 | window->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 1419 | AKEY_EVENT_FLAG_CANCELED); |
| 1420 | } |
| 1421 | |
| 1422 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1423 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1424 | sp<FakeWindowHandle> window = |
| 1425 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1426 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1427 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1428 | |
| 1429 | NotifyMotionArgs motionArgs = |
| 1430 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1431 | ADISPLAY_ID_DEFAULT); |
| 1432 | mDispatcher->notifyMotion(&motionArgs); |
| 1433 | |
| 1434 | // Window should receive motion down event. |
| 1435 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1436 | |
| 1437 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 1438 | // on the app side. |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1439 | NotifyDeviceResetArgs args(10 /*id*/, 20 /*eventTime*/, DEVICE_ID); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 1440 | mDispatcher->notifyDeviceReset(&args); |
| 1441 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, ADISPLAY_ID_DEFAULT, |
| 1442 | 0 /*expectedFlags*/); |
| 1443 | } |
| 1444 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1445 | TEST_F(InputDispatcherTest, TransferTouchFocus_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1447 | |
| 1448 | // Create a couple of windows |
| 1449 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1450 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1451 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1452 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1453 | |
| 1454 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1455 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1456 | |
| 1457 | // Send down to the first window |
| 1458 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1459 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1460 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1461 | // Only the first window should get the down event |
| 1462 | firstWindow->consumeMotionDown(); |
| 1463 | secondWindow->assertNoEvents(); |
| 1464 | |
| 1465 | // Transfer touch focus to the second window |
| 1466 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1467 | // The first window gets cancel and the second gets down |
| 1468 | firstWindow->consumeMotionCancel(); |
| 1469 | secondWindow->consumeMotionDown(); |
| 1470 | |
| 1471 | // Send up event to the second window |
| 1472 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1473 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1474 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1475 | // The first window gets no events and the second gets up |
| 1476 | firstWindow->assertNoEvents(); |
| 1477 | secondWindow->consumeMotionUp(); |
| 1478 | } |
| 1479 | |
| 1480 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointerNoSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1481 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1482 | |
| 1483 | PointF touchPoint = {10, 10}; |
| 1484 | |
| 1485 | // Create a couple of windows |
| 1486 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1487 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1488 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1489 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1490 | |
| 1491 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1492 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1493 | |
| 1494 | // Send down to the first window |
| 1495 | NotifyMotionArgs downMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1496 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint}); |
| 1497 | mDispatcher->notifyMotion(&downMotionArgs); |
| 1498 | // Only the first window should get the down event |
| 1499 | firstWindow->consumeMotionDown(); |
| 1500 | secondWindow->assertNoEvents(); |
| 1501 | |
| 1502 | // Send pointer down to the first window |
| 1503 | NotifyMotionArgs pointerDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1504 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1505 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1506 | mDispatcher->notifyMotion(&pointerDownMotionArgs); |
| 1507 | // Only the first window should get the pointer down event |
| 1508 | firstWindow->consumeMotionPointerDown(1); |
| 1509 | secondWindow->assertNoEvents(); |
| 1510 | |
| 1511 | // Transfer touch focus to the second window |
| 1512 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1513 | // The first window gets cancel and the second gets down and pointer down |
| 1514 | firstWindow->consumeMotionCancel(); |
| 1515 | secondWindow->consumeMotionDown(); |
| 1516 | secondWindow->consumeMotionPointerDown(1); |
| 1517 | |
| 1518 | // Send pointer up to the second window |
| 1519 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1520 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1521 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint}); |
| 1522 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1523 | // The first window gets nothing and the second gets pointer up |
| 1524 | firstWindow->assertNoEvents(); |
| 1525 | secondWindow->consumeMotionPointerUp(1); |
| 1526 | |
| 1527 | // Send up event to the second window |
| 1528 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1529 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1530 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1531 | // The first window gets nothing and the second gets up |
| 1532 | firstWindow->assertNoEvents(); |
| 1533 | secondWindow->consumeMotionUp(); |
| 1534 | } |
| 1535 | |
| 1536 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1537 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1538 | |
| 1539 | // Create a non touch modal window that supports split touch |
| 1540 | sp<FakeWindowHandle> firstWindow = new FakeWindowHandle(application, mDispatcher, |
| 1541 | "First Window", ADISPLAY_ID_DEFAULT); |
| 1542 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1543 | firstWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1544 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1545 | |
| 1546 | // Create a non touch modal window that supports split touch |
| 1547 | sp<FakeWindowHandle> secondWindow = new FakeWindowHandle(application, mDispatcher, |
| 1548 | "Second Window", ADISPLAY_ID_DEFAULT); |
| 1549 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 1550 | secondWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 1551 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1552 | |
| 1553 | // Add the windows to the dispatcher |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1554 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {firstWindow, secondWindow}}}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1555 | |
| 1556 | PointF pointInFirst = {300, 200}; |
| 1557 | PointF pointInSecond = {300, 600}; |
| 1558 | |
| 1559 | // Send down to the first window |
| 1560 | NotifyMotionArgs firstDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 1561 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst}); |
| 1562 | mDispatcher->notifyMotion(&firstDownMotionArgs); |
| 1563 | // Only the first window should get the down event |
| 1564 | firstWindow->consumeMotionDown(); |
| 1565 | secondWindow->assertNoEvents(); |
| 1566 | |
| 1567 | // Send down to the second window |
| 1568 | NotifyMotionArgs secondDownMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_DOWN |
| 1569 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1570 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1571 | mDispatcher->notifyMotion(&secondDownMotionArgs); |
| 1572 | // The first window gets a move and the second a down |
| 1573 | firstWindow->consumeMotionMove(); |
| 1574 | secondWindow->consumeMotionDown(); |
| 1575 | |
| 1576 | // Transfer touch focus to the second window |
| 1577 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 1578 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 1579 | firstWindow->consumeMotionCancel(); |
| 1580 | secondWindow->consumeMotionPointerDown(1); |
| 1581 | |
| 1582 | // Send pointer up to the second window |
| 1583 | NotifyMotionArgs pointerUpMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_POINTER_UP |
| 1584 | | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
| 1585 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, {pointInFirst, pointInSecond}); |
| 1586 | mDispatcher->notifyMotion(&pointerUpMotionArgs); |
| 1587 | // The first window gets nothing and the second gets pointer up |
| 1588 | firstWindow->assertNoEvents(); |
| 1589 | secondWindow->consumeMotionPointerUp(1); |
| 1590 | |
| 1591 | // Send up event to the second window |
| 1592 | NotifyMotionArgs upMotionArgs = generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 1593 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT); |
| 1594 | mDispatcher->notifyMotion(&upMotionArgs); |
| 1595 | // The first window gets nothing and the second gets up |
| 1596 | firstWindow->assertNoEvents(); |
| 1597 | secondWindow->consumeMotionUp(); |
| 1598 | } |
| 1599 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1600 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1602 | sp<FakeWindowHandle> window = |
| 1603 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1604 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1605 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1606 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1607 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1608 | |
| 1609 | window->consumeFocusEvent(true); |
| 1610 | |
| 1611 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1612 | mDispatcher->notifyKey(&keyArgs); |
| 1613 | |
| 1614 | // Window should receive key down event. |
| 1615 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 1616 | } |
| 1617 | |
| 1618 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1619 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1620 | sp<FakeWindowHandle> window = |
| 1621 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1622 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1623 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1624 | |
| 1625 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1626 | mDispatcher->notifyKey(&keyArgs); |
| 1627 | mDispatcher->waitForIdle(); |
| 1628 | |
| 1629 | window->assertNoEvents(); |
| 1630 | } |
| 1631 | |
| 1632 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 1633 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1634 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1635 | sp<FakeWindowHandle> window = |
| 1636 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1637 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1638 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1639 | |
| 1640 | // Send key |
| 1641 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 1642 | mDispatcher->notifyKey(&keyArgs); |
| 1643 | // Send motion |
| 1644 | NotifyMotionArgs motionArgs = |
| 1645 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1646 | ADISPLAY_ID_DEFAULT); |
| 1647 | mDispatcher->notifyMotion(&motionArgs); |
| 1648 | |
| 1649 | // Window should receive only the motion event |
| 1650 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1651 | window->assertNoEvents(); // Key event or focus event will not be received |
| 1652 | } |
| 1653 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1654 | class FakeMonitorReceiver { |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1655 | public: |
| 1656 | FakeMonitorReceiver(const sp<InputDispatcher>& dispatcher, const std::string name, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1657 | int32_t displayId, bool isGestureMonitor = false) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1658 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1659 | dispatcher->createInputMonitor(displayId, isGestureMonitor, name); |
| 1660 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1663 | sp<IBinder> getToken() { return mInputReceiver->getToken(); } |
| 1664 | |
| 1665 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1666 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_DOWN, |
| 1667 | expectedDisplayId, expectedFlags); |
| 1668 | } |
| 1669 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1670 | std::optional<int32_t> receiveEvent() { return mInputReceiver->receiveEvent(); } |
| 1671 | |
| 1672 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver->finishEvent(consumeSeq); } |
| 1673 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1674 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1675 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1676 | expectedDisplayId, expectedFlags); |
| 1677 | } |
| 1678 | |
| 1679 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
| 1680 | mInputReceiver->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_UP, |
| 1681 | expectedDisplayId, expectedFlags); |
| 1682 | } |
| 1683 | |
| 1684 | void assertNoEvents() { mInputReceiver->assertNoEvents(); } |
| 1685 | |
| 1686 | private: |
| 1687 | std::unique_ptr<FakeInputReceiver> mInputReceiver; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | |
| 1690 | // Tests for gesture monitors |
| 1691 | TEST_F(InputDispatcherTest, GestureMonitor_ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1692 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1693 | sp<FakeWindowHandle> window = |
| 1694 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1696 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1697 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1698 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1699 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1701 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1702 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1703 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1704 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | TEST_F(InputDispatcherTest, GestureMonitor_DoesNotReceiveKeyEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1708 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1709 | sp<FakeWindowHandle> window = |
| 1710 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1711 | |
| 1712 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1713 | window->setFocusable(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1714 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1715 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1716 | setFocusedWindow(window); |
| 1717 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1718 | window->consumeFocusEvent(true); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1719 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1720 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1721 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1722 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 1724 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1725 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1726 | monitor.assertNoEvents(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | TEST_F(InputDispatcherTest, GestureMonitor_CanPilferAfterWindowIsRemovedMidStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1730 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1731 | sp<FakeWindowHandle> window = |
| 1732 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1733 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1734 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1735 | FakeMonitorReceiver monitor = FakeMonitorReceiver(mDispatcher, "GM_1", ADISPLAY_ID_DEFAULT, |
| 1736 | true /*isGestureMonitor*/); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1737 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1739 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1740 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1741 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1742 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1743 | |
| 1744 | window->releaseChannel(); |
| 1745 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1746 | mDispatcher->pilferPointers(monitor.getToken()); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1747 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1749 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1750 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1751 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1754 | TEST_F(InputDispatcherTest, UnresponsiveGestureMonitor_GetsAnr) { |
| 1755 | FakeMonitorReceiver monitor = |
| 1756 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 1757 | true /*isGestureMonitor*/); |
| 1758 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1760 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
| 1761 | std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 1762 | ASSERT_TRUE(consumeSeq); |
| 1763 | |
| 1764 | mFakePolicy->assertNotifyAnrWasCalled(DISPATCHING_TIMEOUT, nullptr, monitor.getToken()); |
| 1765 | monitor.finishEvent(*consumeSeq); |
| 1766 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 1767 | } |
| 1768 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1769 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1770 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1771 | sp<FakeWindowHandle> window = |
| 1772 | new FakeWindowHandle(application, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1773 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1774 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1775 | |
| 1776 | NotifyMotionArgs motionArgs = |
| 1777 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1778 | ADISPLAY_ID_DEFAULT); |
| 1779 | |
| 1780 | mDispatcher->notifyMotion(&motionArgs); |
| 1781 | // Window should receive motion down event. |
| 1782 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1783 | |
| 1784 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 1785 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 1786 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1787 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 1788 | motionArgs.pointerCoords[0].getX() - 10); |
| 1789 | |
| 1790 | mDispatcher->notifyMotion(&motionArgs); |
| 1791 | window->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_MOVE, ADISPLAY_ID_DEFAULT, |
| 1792 | 0 /*expectedFlags*/); |
| 1793 | } |
| 1794 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1795 | /** |
| 1796 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 1797 | * the device default right away. In the test scenario, we check both the default value, |
| 1798 | * and the action of enabling / disabling. |
| 1799 | */ |
| 1800 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1801 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1802 | sp<FakeWindowHandle> window = |
| 1803 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1804 | |
| 1805 | // Set focused application. |
| 1806 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1807 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1808 | |
| 1809 | SCOPED_TRACE("Check default value of touch mode"); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1810 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1811 | setFocusedWindow(window); |
| 1812 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1813 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1814 | |
| 1815 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1816 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1817 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1818 | window->consumeFocusEvent(false /*hasFocus*/, true /*inTouchMode*/); |
| 1819 | |
| 1820 | SCOPED_TRACE("Disable touch mode"); |
| 1821 | mDispatcher->setInTouchMode(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1822 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1823 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1824 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1825 | window->consumeFocusEvent(true /*hasFocus*/, false /*inTouchMode*/); |
| 1826 | |
| 1827 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1828 | window->setFocusable(false); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1829 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1830 | window->consumeFocusEvent(false /*hasFocus*/, false /*inTouchMode*/); |
| 1831 | |
| 1832 | SCOPED_TRACE("Enable touch mode again"); |
| 1833 | mDispatcher->setInTouchMode(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1834 | window->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1835 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1836 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1837 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1838 | |
| 1839 | window->assertNoEvents(); |
| 1840 | } |
| 1841 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1842 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1843 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1844 | sp<FakeWindowHandle> window = |
| 1845 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1846 | |
| 1847 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 1848 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1849 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1850 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1851 | setFocusedWindow(window); |
| 1852 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1853 | window->consumeFocusEvent(true /*hasFocus*/, true /*inTouchMode*/); |
| 1854 | |
| 1855 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 1856 | mDispatcher->notifyKey(&keyArgs); |
| 1857 | |
| 1858 | InputEvent* event = window->consume(); |
| 1859 | ASSERT_NE(event, nullptr); |
| 1860 | |
| 1861 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1862 | ASSERT_NE(verified, nullptr); |
| 1863 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 1864 | |
| 1865 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 1866 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 1867 | ASSERT_EQ(keyArgs.source, verified->source); |
| 1868 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 1869 | |
| 1870 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 1871 | |
| 1872 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
| 1873 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 1874 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
| 1875 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 1876 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 1877 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 1878 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 1879 | } |
| 1880 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1881 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1883 | sp<FakeWindowHandle> window = |
| 1884 | new FakeWindowHandle(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); |
| 1885 | |
| 1886 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1887 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 1888 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 1889 | |
| 1890 | NotifyMotionArgs motionArgs = |
| 1891 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 1892 | ADISPLAY_ID_DEFAULT); |
| 1893 | mDispatcher->notifyMotion(&motionArgs); |
| 1894 | |
| 1895 | InputEvent* event = window->consume(); |
| 1896 | ASSERT_NE(event, nullptr); |
| 1897 | |
| 1898 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
| 1899 | ASSERT_NE(verified, nullptr); |
| 1900 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 1901 | |
| 1902 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 1903 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 1904 | EXPECT_EQ(motionArgs.source, verified->source); |
| 1905 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 1906 | |
| 1907 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 1908 | |
| 1909 | EXPECT_EQ(motionArgs.pointerCoords[0].getX(), verifiedMotion.rawX); |
| 1910 | EXPECT_EQ(motionArgs.pointerCoords[0].getY(), verifiedMotion.rawY); |
| 1911 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
| 1912 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
| 1913 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
| 1914 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 1915 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 1916 | } |
| 1917 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 1918 | /** |
| 1919 | * Ensure that separate calls to sign the same data are generating the same key. |
| 1920 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 1921 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 1922 | * tests. |
| 1923 | */ |
| 1924 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 1925 | KeyEvent event = getTestKeyEvent(); |
| 1926 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 1927 | |
| 1928 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 1929 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 1930 | ASSERT_EQ(hmac1, hmac2); |
| 1931 | } |
| 1932 | |
| 1933 | /** |
| 1934 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 1935 | */ |
| 1936 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 1937 | KeyEvent event = getTestKeyEvent(); |
| 1938 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 1939 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 1940 | |
| 1941 | verifiedEvent.deviceId += 1; |
| 1942 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1943 | |
| 1944 | verifiedEvent.source += 1; |
| 1945 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1946 | |
| 1947 | verifiedEvent.eventTimeNanos += 1; |
| 1948 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1949 | |
| 1950 | verifiedEvent.displayId += 1; |
| 1951 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1952 | |
| 1953 | verifiedEvent.action += 1; |
| 1954 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1955 | |
| 1956 | verifiedEvent.downTimeNanos += 1; |
| 1957 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1958 | |
| 1959 | verifiedEvent.flags += 1; |
| 1960 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1961 | |
| 1962 | verifiedEvent.keyCode += 1; |
| 1963 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1964 | |
| 1965 | verifiedEvent.scanCode += 1; |
| 1966 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1967 | |
| 1968 | verifiedEvent.metaState += 1; |
| 1969 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1970 | |
| 1971 | verifiedEvent.repeatCount += 1; |
| 1972 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 1973 | } |
| 1974 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1975 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 1976 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1977 | sp<FakeWindowHandle> windowTop = |
| 1978 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 1979 | sp<FakeWindowHandle> windowSecond = |
| 1980 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 1981 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 1982 | |
| 1983 | // Top window is also focusable but is not granted focus. |
| 1984 | windowTop->setFocusable(true); |
| 1985 | windowSecond->setFocusable(true); |
| 1986 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 1987 | setFocusedWindow(windowSecond); |
| 1988 | |
| 1989 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 1991 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1992 | |
| 1993 | // Focused window should receive event. |
| 1994 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 1995 | windowTop->assertNoEvents(); |
| 1996 | } |
| 1997 | |
| 1998 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 1999 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2000 | sp<FakeWindowHandle> window = |
| 2001 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2002 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2003 | |
| 2004 | window->setFocusable(true); |
| 2005 | // Release channel for window is no longer valid. |
| 2006 | window->releaseChannel(); |
| 2007 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2008 | setFocusedWindow(window); |
| 2009 | |
| 2010 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2011 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2012 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2013 | |
| 2014 | // window channel is invalid, so it should not receive any input event. |
| 2015 | window->assertNoEvents(); |
| 2016 | } |
| 2017 | |
| 2018 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 2019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2020 | sp<FakeWindowHandle> window = |
| 2021 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2022 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2023 | |
| 2024 | // Window is not focusable. |
| 2025 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2026 | setFocusedWindow(window); |
| 2027 | |
| 2028 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2029 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2030 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2031 | |
| 2032 | // window is invalid, so it should not receive any input event. |
| 2033 | window->assertNoEvents(); |
| 2034 | } |
| 2035 | |
| 2036 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 2037 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2038 | sp<FakeWindowHandle> windowTop = |
| 2039 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2040 | sp<FakeWindowHandle> windowSecond = |
| 2041 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2042 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2043 | |
| 2044 | windowTop->setFocusable(true); |
| 2045 | windowSecond->setFocusable(true); |
| 2046 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2047 | setFocusedWindow(windowTop); |
| 2048 | windowTop->consumeFocusEvent(true); |
| 2049 | |
| 2050 | setFocusedWindow(windowSecond, windowTop); |
| 2051 | windowSecond->consumeFocusEvent(true); |
| 2052 | windowTop->consumeFocusEvent(false); |
| 2053 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2054 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2055 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2056 | |
| 2057 | // Focused window should receive event. |
| 2058 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2059 | } |
| 2060 | |
| 2061 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestFocusTokenNotFocused) { |
| 2062 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2063 | sp<FakeWindowHandle> windowTop = |
| 2064 | new FakeWindowHandle(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
| 2065 | sp<FakeWindowHandle> windowSecond = |
| 2066 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2067 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2068 | |
| 2069 | windowTop->setFocusable(true); |
| 2070 | windowSecond->setFocusable(true); |
| 2071 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowTop, windowSecond}}}); |
| 2072 | setFocusedWindow(windowSecond, windowTop); |
| 2073 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2074 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2075 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2076 | |
| 2077 | // Event should be dropped. |
| 2078 | windowTop->assertNoEvents(); |
| 2079 | windowSecond->assertNoEvents(); |
| 2080 | } |
| 2081 | |
| 2082 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 2083 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2084 | sp<FakeWindowHandle> window = |
| 2085 | new FakeWindowHandle(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2086 | sp<FakeWindowHandle> previousFocusedWindow = |
| 2087 | new FakeWindowHandle(application, mDispatcher, "previousFocusedWindow", |
| 2088 | ADISPLAY_ID_DEFAULT); |
| 2089 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 2090 | |
| 2091 | window->setFocusable(true); |
| 2092 | previousFocusedWindow->setFocusable(true); |
| 2093 | window->setVisible(false); |
| 2094 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window, previousFocusedWindow}}}); |
| 2095 | setFocusedWindow(previousFocusedWindow); |
| 2096 | previousFocusedWindow->consumeFocusEvent(true); |
| 2097 | |
| 2098 | // Requesting focus on invisible window takes focus from currently focused window. |
| 2099 | setFocusedWindow(window); |
| 2100 | previousFocusedWindow->consumeFocusEvent(false); |
| 2101 | |
| 2102 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2104 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2105 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2106 | |
| 2107 | // Window does not get focus event or key down. |
| 2108 | window->assertNoEvents(); |
| 2109 | |
| 2110 | // Window becomes visible. |
| 2111 | window->setVisible(true); |
| 2112 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); |
| 2113 | |
| 2114 | // Window receives focus event. |
| 2115 | window->consumeFocusEvent(true); |
| 2116 | // Focused window receives key down. |
| 2117 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2118 | } |
| 2119 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2120 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 2121 | protected: |
| 2122 | static constexpr nsecs_t KEY_REPEAT_TIMEOUT = 40 * 1000000; // 40 ms |
| 2123 | static constexpr nsecs_t KEY_REPEAT_DELAY = 40 * 1000000; // 40 ms |
| 2124 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2125 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2126 | sp<FakeWindowHandle> mWindow; |
| 2127 | |
| 2128 | virtual void SetUp() override { |
| 2129 | mFakePolicy = new FakeInputDispatcherPolicy(); |
| 2130 | mFakePolicy->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
| 2131 | mDispatcher = new InputDispatcher(mFakePolicy); |
| 2132 | mDispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 2133 | ASSERT_EQ(OK, mDispatcher->start()); |
| 2134 | |
| 2135 | setUpWindow(); |
| 2136 | } |
| 2137 | |
| 2138 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2139 | mApp = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2140 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
| 2141 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2142 | mWindow->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2143 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2144 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2145 | mWindow->consumeFocusEvent(true); |
| 2146 | } |
| 2147 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2148 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2149 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2150 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2151 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
| 2152 | mDispatcher->notifyKey(&keyArgs); |
| 2153 | |
| 2154 | // Window should receive key down event. |
| 2155 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2156 | } |
| 2157 | |
| 2158 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 2159 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
| 2160 | InputEvent* repeatEvent = mWindow->consume(); |
| 2161 | ASSERT_NE(nullptr, repeatEvent); |
| 2162 | |
| 2163 | uint32_t eventType = repeatEvent->getType(); |
| 2164 | ASSERT_EQ(AINPUT_EVENT_TYPE_KEY, eventType); |
| 2165 | |
| 2166 | KeyEvent* repeatKeyEvent = static_cast<KeyEvent*>(repeatEvent); |
| 2167 | uint32_t eventAction = repeatKeyEvent->getAction(); |
| 2168 | EXPECT_EQ(AKEY_EVENT_ACTION_DOWN, eventAction); |
| 2169 | EXPECT_EQ(repeatCount, repeatKeyEvent->getRepeatCount()); |
| 2170 | } |
| 2171 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2172 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2173 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2174 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2175 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
| 2176 | mDispatcher->notifyKey(&keyArgs); |
| 2177 | |
| 2178 | // Window should receive key down event. |
| 2179 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT, |
| 2180 | 0 /*expectedFlags*/); |
| 2181 | } |
| 2182 | }; |
| 2183 | |
| 2184 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2185 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2186 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2187 | expectKeyRepeatOnce(repeatCount); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
| 2192 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2193 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2194 | expectKeyRepeatOnce(repeatCount); |
| 2195 | } |
| 2196 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2197 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2198 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2199 | expectKeyRepeatOnce(repeatCount); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2204 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2205 | expectKeyRepeatOnce(1 /*repeatCount*/); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2206 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2207 | mWindow->assertNoEvents(); |
| 2208 | } |
| 2209 | |
| 2210 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
| 2211 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2212 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2213 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2214 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2215 | // Stale key up from device 1. |
| 2216 | sendAndConsumeKeyUp(1 /* deviceId */); |
| 2217 | // Device 2 is still down, keep repeating |
| 2218 | expectKeyRepeatOnce(2 /*repeatCount*/); |
| 2219 | expectKeyRepeatOnce(3 /*repeatCount*/); |
| 2220 | // Device 2 key up |
| 2221 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2222 | mWindow->assertNoEvents(); |
| 2223 | } |
| 2224 | |
| 2225 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
| 2226 | sendAndConsumeKeyDown(1 /* deviceId */); |
| 2227 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2228 | sendAndConsumeKeyDown(2 /* deviceId */); |
| 2229 | expectKeyRepeatOnce(1 /*repeatCount*/); |
| 2230 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
| 2231 | sendAndConsumeKeyUp(2 /* deviceId */); |
| 2232 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2233 | mWindow->assertNoEvents(); |
| 2234 | } |
| 2235 | |
| 2236 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2237 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2238 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2239 | InputEvent* repeatEvent = mWindow->consume(); |
| 2240 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2241 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
| 2242 | IdGenerator::getSource(repeatEvent->getId())); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 2247 | sendAndConsumeKeyDown(1 /* deviceId */); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 2248 | |
| 2249 | std::unordered_set<int32_t> idSet; |
| 2250 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 2251 | InputEvent* repeatEvent = mWindow->consume(); |
| 2252 | ASSERT_NE(nullptr, repeatEvent) << "Didn't receive event with repeat count " << repeatCount; |
| 2253 | int32_t id = repeatEvent->getId(); |
| 2254 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 2255 | idSet.insert(id); |
| 2256 | } |
| 2257 | } |
| 2258 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2259 | /* Test InputDispatcher for MultiDisplay */ |
| 2260 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 2261 | public: |
| 2262 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2263 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2264 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2265 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2266 | application1 = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2267 | windowInPrimary = new FakeWindowHandle(application1, mDispatcher, "D_1", |
| 2268 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2269 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2270 | // Set focus window for primary display, but focused display would be second one. |
| 2271 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2272 | windowInPrimary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2273 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2274 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2275 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2276 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2277 | application2 = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2278 | windowInSecondary = new FakeWindowHandle(application2, mDispatcher, "D_2", |
| 2279 | SECOND_DISPLAY_ID); |
| 2280 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2281 | // Set focus display to second one. |
| 2282 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 2283 | // Set focus window for second display. |
| 2284 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2285 | windowInSecondary->setFocusable(true); |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2286 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {windowInSecondary}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2287 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2288 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2289 | } |
| 2290 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2291 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2292 | InputDispatcherTest::TearDown(); |
| 2293 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2294 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2295 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2296 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2297 | windowInSecondary.clear(); |
| 2298 | } |
| 2299 | |
| 2300 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2301 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2302 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2303 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2304 | sp<FakeWindowHandle> windowInSecondary; |
| 2305 | }; |
| 2306 | |
| 2307 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 2308 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2309 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2310 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2311 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2312 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2313 | windowInSecondary->assertNoEvents(); |
| 2314 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2315 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2317 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2318 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2319 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2320 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2321 | } |
| 2322 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2323 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2324 | // Test inject a key down with display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2326 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2327 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 2328 | windowInSecondary->assertNoEvents(); |
| 2329 | |
| 2330 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2332 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2333 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2334 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2335 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2336 | // Remove all windows in secondary display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2337 | mDispatcher->setInputWindows({{SECOND_DISPLAY_ID, {}}}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2338 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2339 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2340 | windowInSecondary->consumeEvent(AINPUT_EVENT_TYPE_KEY, AKEY_EVENT_ACTION_UP, ADISPLAY_ID_NONE, |
| 2341 | AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2342 | |
| 2343 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2344 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 2345 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2346 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2347 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 2348 | windowInSecondary->assertNoEvents(); |
| 2349 | } |
| 2350 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2351 | // Test per-display input monitors for motion event. |
| 2352 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2353 | FakeMonitorReceiver monitorInPrimary = |
| 2354 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2355 | FakeMonitorReceiver monitorInSecondary = |
| 2356 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2357 | |
| 2358 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2360 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
| 2361 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2362 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2363 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2364 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2365 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2366 | |
| 2367 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2369 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
| 2370 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2371 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2372 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2373 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2374 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2375 | |
| 2376 | // Test inject a non-pointer motion event. |
| 2377 | // If specific a display, it will dispatch to the focused window of particular display, |
| 2378 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2379 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 2380 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
| 2381 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2382 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2383 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2384 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2385 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | // Test per-display input monitors for key event. |
| 2389 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
| 2390 | //Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2391 | FakeMonitorReceiver monitorInPrimary = |
| 2392 | FakeMonitorReceiver(mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 2393 | FakeMonitorReceiver monitorInSecondary = |
| 2394 | FakeMonitorReceiver(mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2395 | |
| 2396 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 2398 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2399 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2400 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 2401 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 2402 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 2403 | } |
| 2404 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2405 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 2406 | sp<FakeWindowHandle> secondWindowInPrimary = |
| 2407 | new FakeWindowHandle(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
| 2408 | secondWindowInPrimary->setFocusable(true); |
| 2409 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {windowInPrimary, secondWindowInPrimary}}}); |
| 2410 | setFocusedWindow(secondWindowInPrimary); |
| 2411 | windowInPrimary->consumeFocusEvent(false); |
| 2412 | secondWindowInPrimary->consumeFocusEvent(true); |
| 2413 | |
| 2414 | // Test inject a key down. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2416 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2417 | windowInPrimary->assertNoEvents(); |
| 2418 | windowInSecondary->assertNoEvents(); |
| 2419 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 2420 | } |
| 2421 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2422 | class InputFilterTest : public InputDispatcherTest { |
| 2423 | protected: |
| 2424 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
| 2425 | |
| 2426 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered) { |
| 2427 | NotifyMotionArgs motionArgs; |
| 2428 | |
| 2429 | motionArgs = generateMotionArgs( |
| 2430 | AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 2431 | mDispatcher->notifyMotion(&motionArgs); |
| 2432 | motionArgs = generateMotionArgs( |
| 2433 | AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
| 2434 | mDispatcher->notifyMotion(&motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2435 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2436 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2437 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2438 | } else { |
| 2439 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | void testNotifyKey(bool expectToBeFiltered) { |
| 2444 | NotifyKeyArgs keyArgs; |
| 2445 | |
| 2446 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 2447 | mDispatcher->notifyKey(&keyArgs); |
| 2448 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
| 2449 | mDispatcher->notifyKey(&keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2450 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2451 | |
| 2452 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 2453 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 2454 | } else { |
| 2455 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 2456 | } |
| 2457 | } |
| 2458 | }; |
| 2459 | |
| 2460 | // Test InputFilter for MotionEvent |
| 2461 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 2462 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
| 2463 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2464 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2465 | |
| 2466 | // Enable InputFilter |
| 2467 | mDispatcher->setInputFilterEnabled(true); |
| 2468 | // Test touch on both primary and second display, and check if both events are filtered. |
| 2469 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ true); |
| 2470 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ true); |
| 2471 | |
| 2472 | // Disable InputFilter |
| 2473 | mDispatcher->setInputFilterEnabled(false); |
| 2474 | // Test touch on both primary and second display, and check if both events aren't filtered. |
| 2475 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered*/ false); |
| 2476 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered*/ false); |
| 2477 | } |
| 2478 | |
| 2479 | // Test InputFilter for KeyEvent |
| 2480 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 2481 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
| 2482 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2483 | |
| 2484 | // Enable InputFilter |
| 2485 | mDispatcher->setInputFilterEnabled(true); |
| 2486 | // Send a key event, and check if it is filtered. |
| 2487 | testNotifyKey(/*expectToBeFiltered*/ true); |
| 2488 | |
| 2489 | // Disable InputFilter |
| 2490 | mDispatcher->setInputFilterEnabled(false); |
| 2491 | // Send a key event, and check if it isn't filtered. |
| 2492 | testNotifyKey(/*expectToBeFiltered*/ false); |
| 2493 | } |
| 2494 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2495 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2496 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2497 | InputDispatcherTest::SetUp(); |
| 2498 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2499 | std::shared_ptr<FakeApplicationHandle> application = |
| 2500 | std::make_shared<FakeApplicationHandle>(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2501 | mUnfocusedWindow = new FakeWindowHandle(application, mDispatcher, "Top", |
| 2502 | ADISPLAY_ID_DEFAULT); |
| 2503 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 2504 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 2505 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2506 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2507 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2508 | mFocusedWindow = |
| 2509 | new FakeWindowHandle(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
| 2510 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2511 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2512 | |
| 2513 | // Set focused application. |
| 2514 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2515 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2516 | |
| 2517 | // Expect one focus window exist in display. |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2518 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2519 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2520 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 2523 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2524 | InputDispatcherTest::TearDown(); |
| 2525 | |
| 2526 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2527 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | protected: |
| 2531 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2532 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2533 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2534 | }; |
| 2535 | |
| 2536 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2537 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 2538 | // the onPointerDownOutsideFocus callback. |
| 2539 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2540 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2541 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2542 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2543 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2544 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2545 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2546 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2547 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 2548 | } |
| 2549 | |
| 2550 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 2551 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 2552 | // onPointerDownOutsideFocus callback. |
| 2553 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2555 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2556 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2557 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2558 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2559 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2560 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 2564 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 2565 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2566 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)) |
| 2567 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2568 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2569 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2570 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2571 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 2575 | // DOWN on the window that already has focus. Ensure no window received the |
| 2576 | // onPointerDownOutsideFocus callback. |
| 2577 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, |
| 2578 | OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 2580 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 2581 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2582 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 2583 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2584 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 2585 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2586 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2589 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 2590 | // windows that point to the same client token. |
| 2591 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 2592 | virtual void SetUp() override { |
| 2593 | InputDispatcherTest::SetUp(); |
| 2594 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2595 | std::shared_ptr<FakeApplicationHandle> application = |
| 2596 | std::make_shared<FakeApplicationHandle>(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2597 | mWindow1 = new FakeWindowHandle(application, mDispatcher, "Fake Window 1", |
| 2598 | ADISPLAY_ID_DEFAULT); |
| 2599 | // Adding FLAG_NOT_TOUCH_MODAL otherwise all taps will go to the top most window. |
| 2600 | // We also need FLAG_SPLIT_TOUCH or we won't be able to get touches for both windows. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2601 | mWindow1->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2602 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2603 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 2604 | |
| 2605 | mWindow2 = new FakeWindowHandle(application, mDispatcher, "Fake Window 2", |
| 2606 | ADISPLAY_ID_DEFAULT, mWindow1->getToken()); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2607 | mWindow2->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 2608 | InputWindowInfo::Flag::SPLIT_TOUCH); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2609 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 2610 | |
Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 2611 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow1, mWindow2}}}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | protected: |
| 2615 | sp<FakeWindowHandle> mWindow1; |
| 2616 | sp<FakeWindowHandle> mWindow2; |
| 2617 | |
| 2618 | // Helper function to convert the point from screen coordinates into the window's space |
| 2619 | static PointF getPointInWindow(const InputWindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 2620 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 2621 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 2625 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 2626 | const std::string name = window->getName(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2627 | InputEvent* event = window->consume(); |
| 2628 | |
| 2629 | ASSERT_NE(nullptr, event) << name.c_str() |
| 2630 | << ": consumer should have returned non-NULL event."; |
| 2631 | |
| 2632 | ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType()) |
| 2633 | << name.c_str() << "expected " << inputEventTypeToString(AINPUT_EVENT_TYPE_MOTION) |
| 2634 | << " event, got " << inputEventTypeToString(event->getType()) << " event"; |
| 2635 | |
| 2636 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
| 2637 | EXPECT_EQ(expectedAction, motionEvent.getAction()); |
| 2638 | |
| 2639 | for (size_t i = 0; i < points.size(); i++) { |
| 2640 | float expectedX = points[i].x; |
| 2641 | float expectedY = points[i].y; |
| 2642 | |
| 2643 | EXPECT_EQ(expectedX, motionEvent.getX(i)) |
| 2644 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
| 2645 | << ", got " << motionEvent.getX(i); |
| 2646 | EXPECT_EQ(expectedY, motionEvent.getY(i)) |
| 2647 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
| 2648 | << ", got " << motionEvent.getY(i); |
| 2649 | } |
| 2650 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2651 | |
| 2652 | void touchAndAssertPositions(int32_t action, std::vector<PointF> touchedPoints, |
| 2653 | std::vector<PointF> expectedPoints) { |
| 2654 | NotifyMotionArgs motionArgs = generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 2655 | ADISPLAY_ID_DEFAULT, touchedPoints); |
| 2656 | mDispatcher->notifyMotion(&motionArgs); |
| 2657 | |
| 2658 | // Always consume from window1 since it's the window that has the InputReceiver |
| 2659 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 2660 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2661 | }; |
| 2662 | |
| 2663 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 2664 | // Touch Window 1 |
| 2665 | PointF touchedPoint = {10, 10}; |
| 2666 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2667 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2668 | |
| 2669 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2670 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2671 | |
| 2672 | // Touch Window 2 |
| 2673 | touchedPoint = {150, 150}; |
| 2674 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2675 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2678 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 2679 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2680 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2681 | |
| 2682 | // Touch Window 1 |
| 2683 | PointF touchedPoint = {10, 10}; |
| 2684 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2685 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2686 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2687 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2688 | |
| 2689 | // Touch Window 2 |
| 2690 | touchedPoint = {150, 150}; |
| 2691 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2692 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 2693 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2694 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2695 | // Update the transform so rotation is set |
| 2696 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 2697 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 2698 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2701 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2702 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2703 | |
| 2704 | // Touch Window 1 |
| 2705 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2706 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2707 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2708 | |
| 2709 | // Touch Window 2 |
| 2710 | int32_t actionPointerDown = |
| 2711 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2712 | touchedPoints.push_back(PointF{150, 150}); |
| 2713 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2714 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2715 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2716 | // Release Window 2 |
| 2717 | int32_t actionPointerUp = |
| 2718 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2719 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 2720 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2721 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2722 | // Update the transform so rotation is set for Window 2 |
| 2723 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 2724 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2725 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2728 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2729 | mWindow2->setWindowScale(0.5f, 0.5f); |
| 2730 | |
| 2731 | // Touch Window 1 |
| 2732 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2733 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2734 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2735 | |
| 2736 | // Touch Window 2 |
| 2737 | int32_t actionPointerDown = |
| 2738 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2739 | touchedPoints.push_back(PointF{150, 150}); |
| 2740 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2741 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2742 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2743 | |
| 2744 | // Move both windows |
| 2745 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2746 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2747 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2748 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2749 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2750 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2751 | // Release Window 2 |
| 2752 | int32_t actionPointerUp = |
| 2753 | AMOTION_EVENT_ACTION_POINTER_UP + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 2754 | touchAndAssertPositions(actionPointerUp, touchedPoints, expectedPoints); |
| 2755 | expectedPoints.pop_back(); |
| 2756 | |
| 2757 | // Touch Window 2 |
| 2758 | mWindow2->setWindowTransform(0, -1, 1, 0); |
| 2759 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
| 2760 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
| 2761 | |
| 2762 | // Move both windows |
| 2763 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2764 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2765 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2766 | |
| 2767 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 2771 | mWindow1->setWindowScale(0.5f, 0.5f); |
| 2772 | |
| 2773 | // Touch Window 1 |
| 2774 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 2775 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2776 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2777 | |
| 2778 | // Touch Window 2 |
| 2779 | int32_t actionPointerDown = |
| 2780 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2781 | touchedPoints.push_back(PointF{150, 150}); |
| 2782 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2783 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2784 | touchAndAssertPositions(actionPointerDown, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2785 | |
| 2786 | // Move both windows |
| 2787 | touchedPoints = {{20, 20}, {175, 175}}; |
| 2788 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 2789 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 2790 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 2791 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2794 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 2795 | virtual void SetUp() override { |
| 2796 | InputDispatcherTest::SetUp(); |
| 2797 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2798 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2799 | mApplication->setDispatchingTimeout(20ms); |
| 2800 | mWindow = |
| 2801 | new FakeWindowHandle(mApplication, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 2802 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 2803 | mWindow->setDispatchingTimeout(30ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2804 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2805 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 2806 | // window. |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 2807 | mWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2808 | |
| 2809 | // Set focused application. |
| 2810 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 2811 | |
| 2812 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 2813 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2814 | mWindow->consumeFocusEvent(true); |
| 2815 | } |
| 2816 | |
| 2817 | virtual void TearDown() override { |
| 2818 | InputDispatcherTest::TearDown(); |
| 2819 | mWindow.clear(); |
| 2820 | } |
| 2821 | |
| 2822 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 2823 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2824 | sp<FakeWindowHandle> mWindow; |
| 2825 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 2826 | |
| 2827 | void tapOnWindow() { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2829 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2830 | WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2832 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2833 | WINDOW_LOCATION)); |
| 2834 | } |
| 2835 | }; |
| 2836 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2837 | // Send a tap and respond, which should not cause an ANR. |
| 2838 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 2839 | tapOnWindow(); |
| 2840 | mWindow->consumeMotionDown(); |
| 2841 | mWindow->consumeMotionUp(); |
| 2842 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2843 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 2844 | } |
| 2845 | |
| 2846 | // Send a regular key and respond, which should not cause an ANR. |
| 2847 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2849 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 2850 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2851 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 2852 | } |
| 2853 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 2854 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 2855 | mWindow->setFocusable(false); |
| 2856 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 2857 | mWindow->consumeFocusEvent(false); |
| 2858 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2859 | InputEventInjectionResult result = |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 2860 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2861 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 2862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 2863 | // Key will not go to window because we have no focused window. |
| 2864 | // The 'no focused window' ANR timer should start instead. |
| 2865 | |
| 2866 | // Now, the focused application goes away. |
| 2867 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 2868 | // The key should get dropped and there should be no ANR. |
| 2869 | |
| 2870 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2871 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 2872 | } |
| 2873 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2874 | // Send an event to the app and have the app not respond right away. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2875 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 2876 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2877 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2878 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2879 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2880 | WINDOW_LOCATION)); |
| 2881 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2882 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); // ACTION_DOWN |
| 2883 | ASSERT_TRUE(sequenceNum); |
| 2884 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 2885 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2886 | |
| 2887 | // The remaining lines are not really needed for the test, but kept as a sanity check |
| 2888 | mWindow->finishEvent(*sequenceNum); |
| 2889 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 2890 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2891 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2892 | } |
| 2893 | |
| 2894 | // Send a key to the app and have the app not respond right away. |
| 2895 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 2896 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2898 | std::optional<uint32_t> sequenceNum = mWindow->receiveEvent(); |
| 2899 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2900 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2901 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 2902 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2903 | } |
| 2904 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2905 | // We have a focused application, but no focused window |
| 2906 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2907 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2908 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 2909 | mWindow->consumeFocusEvent(false); |
| 2910 | |
| 2911 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2913 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 2914 | WINDOW_LOCATION)); |
| 2915 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 2916 | mDispatcher->waitForIdle(); |
| 2917 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 2918 | |
| 2919 | // Once a focused event arrives, we get an ANR for this application |
| 2920 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 2921 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2922 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2923 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2924 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 2925 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2926 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 2927 | mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/); |
| 2928 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2929 | } |
| 2930 | |
| 2931 | // We have a focused application, but no focused window |
| 2932 | // If the policy wants to keep waiting on the focused window to be added, make sure |
| 2933 | // that this timeout extension is honored and ANR is raised again. |
| 2934 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_ExtendsAnr) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2935 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2936 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 2937 | mWindow->consumeFocusEvent(false); |
| 2938 | const std::chrono::duration timeout = 5ms; |
| 2939 | mFakePolicy->setAnrTimeout(timeout); |
| 2940 | |
| 2941 | // Once a focused event arrives, we get an ANR for this application |
| 2942 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 2943 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2944 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2945 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2946 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 2947 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2948 | const std::chrono::duration appTimeout = |
| 2949 | mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 2950 | mFakePolicy->assertNotifyAnrWasCalled(appTimeout, mApplication, nullptr /*windowToken*/); |
| 2951 | |
| 2952 | // After the extended time has passed, ANR should be raised again |
| 2953 | mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/); |
| 2954 | |
| 2955 | // If we stop extending the timeout, dispatcher should go to idle. |
| 2956 | // Another ANR may be raised during this time |
| 2957 | mFakePolicy->setAnrTimeout(0ms); |
| 2958 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2959 | } |
| 2960 | |
| 2961 | // We have a focused application, but no focused window |
| 2962 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 2963 | mWindow->setFocusable(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2964 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 2965 | mWindow->consumeFocusEvent(false); |
| 2966 | |
| 2967 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2968 | const InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2969 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2970 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 2971 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2972 | |
| 2973 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 2974 | mFakePolicy->assertNotifyAnrWasCalled(timeout, mApplication, nullptr /*windowToken*/); |
| 2975 | |
| 2976 | // Future focused events get dropped right away |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2977 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2978 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 2979 | mWindow->assertNoEvents(); |
| 2980 | } |
| 2981 | |
| 2982 | /** |
| 2983 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 2984 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 2985 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 2986 | * the ANR mechanism should still work. |
| 2987 | * |
| 2988 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 2989 | * DOWN event, while not responding on the second one. |
| 2990 | */ |
| 2991 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 2992 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2993 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 2994 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 2995 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 2996 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 2997 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 2998 | |
| 2999 | // Now send ACTION_UP, with identical timestamp |
| 3000 | injectMotionEvent(mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 3001 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 3002 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 3003 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3004 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3005 | |
| 3006 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 3007 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3008 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3009 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
| 3010 | } |
| 3011 | |
| 3012 | // If an app is not responding to a key event, gesture monitors should continue to receive |
| 3013 | // new motion events |
| 3014 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnKey) { |
| 3015 | FakeMonitorReceiver monitor = |
| 3016 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3017 | true /*isGestureMonitor*/); |
| 3018 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3019 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3020 | injectKeyDown(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3021 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3023 | |
| 3024 | // Stuck on the ACTION_UP |
| 3025 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3026 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken()); |
| 3027 | |
| 3028 | // New tap will go to the gesture monitor, but not to the window |
| 3029 | tapOnWindow(); |
| 3030 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3031 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3032 | |
| 3033 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3034 | mDispatcher->waitForIdle(); |
| 3035 | mWindow->assertNoEvents(); |
| 3036 | monitor.assertNoEvents(); |
| 3037 | } |
| 3038 | |
| 3039 | // If an app is not responding to a motion event, gesture monitors should continue to receive |
| 3040 | // new motion events |
| 3041 | TEST_F(InputDispatcherSingleWindowAnr, GestureMonitors_ReceiveEventsDuringAppAnrOnMotion) { |
| 3042 | FakeMonitorReceiver monitor = |
| 3043 | FakeMonitorReceiver(mDispatcher, "Gesture monitor", ADISPLAY_ID_DEFAULT, |
| 3044 | true /*isGestureMonitor*/); |
| 3045 | |
| 3046 | tapOnWindow(); |
| 3047 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3048 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3049 | |
| 3050 | mWindow->consumeMotionDown(); |
| 3051 | // Stuck on the ACTION_UP |
| 3052 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3053 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr, mWindow->getToken()); |
| 3054 | |
| 3055 | // New tap will go to the gesture monitor, but not to the window |
| 3056 | tapOnWindow(); |
| 3057 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 3058 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3059 | |
| 3060 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 3061 | mDispatcher->waitForIdle(); |
| 3062 | mWindow->assertNoEvents(); |
| 3063 | monitor.assertNoEvents(); |
| 3064 | } |
| 3065 | |
| 3066 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 3067 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 3068 | // get an ANR again. |
| 3069 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 3070 | // 2. consume all pending events (= queue becomes healthy again) |
| 3071 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 3072 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 3073 | tapOnWindow(); |
| 3074 | |
| 3075 | mWindow->consumeMotionDown(); |
| 3076 | // Block on ACTION_UP |
| 3077 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3078 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
| 3079 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 3080 | mDispatcher->waitForIdle(); |
| 3081 | mWindow->assertNoEvents(); |
| 3082 | |
| 3083 | tapOnWindow(); |
| 3084 | mWindow->consumeMotionDown(); |
| 3085 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
| 3086 | mWindow->consumeMotionUp(); |
| 3087 | |
| 3088 | mDispatcher->waitForIdle(); |
| 3089 | mWindow->assertNoEvents(); |
| 3090 | } |
| 3091 | |
| 3092 | // If the policy tells us to raise ANR again after some time, ensure that the timeout extension |
| 3093 | // is honored |
| 3094 | TEST_F(InputDispatcherSingleWindowAnr, Policy_CanExtendTimeout) { |
| 3095 | const std::chrono::duration timeout = 5ms; |
| 3096 | mFakePolicy->setAnrTimeout(timeout); |
| 3097 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3099 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3100 | WINDOW_LOCATION)); |
| 3101 | |
| 3102 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3103 | mFakePolicy->assertNotifyAnrWasCalled(windowTimeout, nullptr /*application*/, |
| 3104 | mWindow->getToken()); |
| 3105 | |
| 3106 | // Since the policy wanted to extend ANR, make sure it is called again after the extension |
| 3107 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, mWindow->getToken()); |
| 3108 | mFakePolicy->setAnrTimeout(0ms); |
| 3109 | std::this_thread::sleep_for(windowTimeout); |
| 3110 | // We are not checking if ANR has been called, because it may have been called again by the |
| 3111 | // time we set the timeout to 0 |
| 3112 | |
| 3113 | // When the policy finally says stop, we should get ACTION_CANCEL |
| 3114 | mWindow->consumeMotionDown(); |
| 3115 | mWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_CANCEL, |
| 3116 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3117 | mWindow->assertNoEvents(); |
| 3118 | } |
| 3119 | |
| 3120 | /** |
| 3121 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3122 | * not to to the focused window until the motion is processed. |
| 3123 | * |
| 3124 | * Warning!!! |
| 3125 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3126 | * and the injection timeout that we specify when injecting the key. |
| 3127 | * We must have the injection timeout (10ms) be smaller than |
| 3128 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3129 | * |
| 3130 | * If that value changes, this test should also change. |
| 3131 | */ |
| 3132 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 3133 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3134 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3135 | |
| 3136 | tapOnWindow(); |
| 3137 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3138 | ASSERT_TRUE(downSequenceNum); |
| 3139 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3140 | ASSERT_TRUE(upSequenceNum); |
| 3141 | // Don't finish the events yet, and send a key |
| 3142 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 3143 | // window even if motions are still being processed. But because the injection timeout is short, |
| 3144 | // we will receive INJECTION_TIMED_OUT as the result. |
| 3145 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3146 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3147 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3148 | InputEventInjectionSync::WAIT_FOR_RESULT, 10ms); |
| 3149 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3150 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3151 | // and the key remains pending, waiting for the touch events to be processed |
| 3152 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3153 | ASSERT_FALSE(keySequenceNum); |
| 3154 | |
| 3155 | std::this_thread::sleep_for(500ms); |
| 3156 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 3157 | // to the focused window, even though we have not yet finished the motion event |
| 3158 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3159 | mWindow->finishEvent(*downSequenceNum); |
| 3160 | mWindow->finishEvent(*upSequenceNum); |
| 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3165 | * not go to the focused window until the motion is processed. |
| 3166 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 3167 | * focused window right away. |
| 3168 | */ |
| 3169 | TEST_F(InputDispatcherSingleWindowAnr, |
| 3170 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 3171 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
| 3172 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow}}}); |
| 3173 | |
| 3174 | tapOnWindow(); |
| 3175 | std::optional<uint32_t> downSequenceNum = mWindow->receiveEvent(); |
| 3176 | ASSERT_TRUE(downSequenceNum); |
| 3177 | std::optional<uint32_t> upSequenceNum = mWindow->receiveEvent(); |
| 3178 | ASSERT_TRUE(upSequenceNum); |
| 3179 | // Don't finish the events yet, and send a key |
| 3180 | // Injection is async, so it will succeed |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3182 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3183 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3184 | // At this point, key is still pending, and should not be sent to the application yet. |
| 3185 | std::optional<uint32_t> keySequenceNum = mWindow->receiveEvent(); |
| 3186 | ASSERT_FALSE(keySequenceNum); |
| 3187 | |
| 3188 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 3189 | tapOnWindow(); |
| 3190 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); // it doesn't matter that we haven't ack'd |
| 3191 | // the other events yet. We can finish events in any order. |
| 3192 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 3193 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
| 3194 | mWindow->consumeMotionDown(); |
| 3195 | mWindow->consumeMotionUp(); |
| 3196 | mWindow->assertNoEvents(); |
| 3197 | } |
| 3198 | |
| 3199 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 3200 | virtual void SetUp() override { |
| 3201 | InputDispatcherTest::SetUp(); |
| 3202 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3203 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3204 | mApplication->setDispatchingTimeout(10ms); |
| 3205 | mUnfocusedWindow = |
| 3206 | new FakeWindowHandle(mApplication, mDispatcher, "Unfocused", ADISPLAY_ID_DEFAULT); |
| 3207 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
| 3208 | // Adding FLAG_NOT_TOUCH_MODAL to ensure taps outside this window are not sent to this |
| 3209 | // window. |
| 3210 | // Adding FLAG_WATCH_OUTSIDE_TOUCH to receive ACTION_OUTSIDE when another window is tapped |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3211 | mUnfocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3212 | InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH | |
| 3213 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3214 | |
| 3215 | mFocusedWindow = |
| 3216 | new FakeWindowHandle(mApplication, mDispatcher, "Focused", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a7d36fd | 2020-06-30 19:32:39 -0500 | [diff] [blame] | 3217 | mFocusedWindow->setDispatchingTimeout(30ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3218 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 3219 | mFocusedWindow->setFlags(InputWindowInfo::Flag::NOT_TOUCH_MODAL | |
| 3220 | InputWindowInfo::Flag::SPLIT_TOUCH); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3221 | |
| 3222 | // Set focused application. |
| 3223 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3224 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3225 | |
| 3226 | // Expect one focus window exist in display. |
| 3227 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3228 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3229 | mFocusedWindow->consumeFocusEvent(true); |
| 3230 | } |
| 3231 | |
| 3232 | virtual void TearDown() override { |
| 3233 | InputDispatcherTest::TearDown(); |
| 3234 | |
| 3235 | mUnfocusedWindow.clear(); |
| 3236 | mFocusedWindow.clear(); |
| 3237 | } |
| 3238 | |
| 3239 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3240 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3241 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 3242 | sp<FakeWindowHandle> mFocusedWindow; |
| 3243 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 3244 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 3245 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 3246 | |
| 3247 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 3248 | |
| 3249 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 3250 | |
| 3251 | private: |
| 3252 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3253 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3254 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3255 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3257 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3258 | location)); |
| 3259 | } |
| 3260 | }; |
| 3261 | |
| 3262 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 3263 | // should be ANR'd first. |
| 3264 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3266 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3267 | FOCUSED_WINDOW_LOCATION)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3268 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3269 | mFocusedWindow->consumeMotionDown(); |
| 3270 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3271 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3272 | // We consumed all events, so no ANR |
| 3273 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3274 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3275 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3277 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3278 | FOCUSED_WINDOW_LOCATION)); |
| 3279 | std::optional<uint32_t> unfocusedSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3280 | ASSERT_TRUE(unfocusedSequenceNum); |
| 3281 | std::optional<uint32_t> focusedSequenceNum = mFocusedWindow->receiveEvent(); |
| 3282 | ASSERT_TRUE(focusedSequenceNum); |
| 3283 | |
| 3284 | const std::chrono::duration timeout = |
| 3285 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3286 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, |
| 3287 | mFocusedWindow->getToken()); |
| 3288 | |
| 3289 | mFocusedWindow->finishEvent(*focusedSequenceNum); |
| 3290 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
| 3291 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3292 | } |
| 3293 | |
| 3294 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 3295 | // it doesn't matter which order they should have ANR. |
| 3296 | // But we should receive ANR for both. |
| 3297 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 3298 | // Set the timeout for unfocused window to match the focused window |
| 3299 | mUnfocusedWindow->setDispatchingTimeout(10ms); |
| 3300 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3301 | |
| 3302 | tapOnFocusedWindow(); |
| 3303 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3304 | std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData1 = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3305 | mFakePolicy->getNotifyAnrData(10ms); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3306 | std::pair<std::shared_ptr<InputApplicationHandle>, sp<IBinder>> anrData2 = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3307 | mFakePolicy->getNotifyAnrData(0ms); |
| 3308 | |
| 3309 | // We don't know which window will ANR first. But both of them should happen eventually. |
| 3310 | ASSERT_TRUE(mFocusedWindow->getToken() == anrData1.second || |
| 3311 | mFocusedWindow->getToken() == anrData2.second); |
| 3312 | ASSERT_TRUE(mUnfocusedWindow->getToken() == anrData1.second || |
| 3313 | mUnfocusedWindow->getToken() == anrData2.second); |
| 3314 | |
| 3315 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3316 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3317 | } |
| 3318 | |
| 3319 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 3320 | // We should also log an error to account for the dropped event (not tested here). |
| 3321 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 3322 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 3323 | tapOnFocusedWindow(); |
| 3324 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3325 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3326 | // Receive the events, but don't respond |
| 3327 | std::optional<uint32_t> downEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
| 3328 | ASSERT_TRUE(downEventSequenceNum); |
| 3329 | std::optional<uint32_t> upEventSequenceNum = mFocusedWindow->receiveEvent(); // ACTION_UP |
| 3330 | ASSERT_TRUE(upEventSequenceNum); |
| 3331 | const std::chrono::duration timeout = |
| 3332 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3333 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, |
| 3334 | mFocusedWindow->getToken()); |
| 3335 | |
| 3336 | // Tap once again |
| 3337 | // We cannot use "tapOnFocusedWindow" because it asserts the injection result to be success |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3338 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3339 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3340 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3341 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3342 | injectMotionUp(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3343 | FOCUSED_WINDOW_LOCATION)); |
| 3344 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 3345 | // valid touch target |
| 3346 | mUnfocusedWindow->assertNoEvents(); |
| 3347 | |
| 3348 | // Consume the first tap |
| 3349 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 3350 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 3351 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3352 | // The second tap did not go to the focused window |
| 3353 | mFocusedWindow->assertNoEvents(); |
| 3354 | // should not have another ANR after the window just became healthy again |
| 3355 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3356 | } |
| 3357 | |
| 3358 | // If you tap outside of all windows, there will not be ANR |
| 3359 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3360 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3361 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3362 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 3363 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3364 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3365 | } |
| 3366 | |
| 3367 | // Since the focused window is paused, tapping on it should not produce any events |
| 3368 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 3369 | mFocusedWindow->setPaused(true); |
| 3370 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mUnfocusedWindow, mFocusedWindow}}}); |
| 3371 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3372 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3373 | injectMotionDown(mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3374 | FOCUSED_WINDOW_LOCATION)); |
| 3375 | |
| 3376 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 3377 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3378 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 3379 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 3380 | |
| 3381 | mFocusedWindow->assertNoEvents(); |
| 3382 | mUnfocusedWindow->assertNoEvents(); |
| 3383 | } |
| 3384 | |
| 3385 | /** |
| 3386 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 3387 | * not to to the focused window until the motion is processed. |
| 3388 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 3389 | * |
| 3390 | * Warning!!! |
| 3391 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 3392 | * and the injection timeout that we specify when injecting the key. |
| 3393 | * We must have the injection timeout (10ms) be smaller than |
| 3394 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 3395 | * |
| 3396 | * If that value changes, this test should also change. |
| 3397 | */ |
| 3398 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 3399 | // Set a long ANR timeout to prevent it from triggering |
| 3400 | mFocusedWindow->setDispatchingTimeout(2s); |
| 3401 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3402 | |
| 3403 | tapOnUnfocusedWindow(); |
| 3404 | std::optional<uint32_t> downSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3405 | ASSERT_TRUE(downSequenceNum); |
| 3406 | std::optional<uint32_t> upSequenceNum = mUnfocusedWindow->receiveEvent(); |
| 3407 | ASSERT_TRUE(upSequenceNum); |
| 3408 | // Don't finish the events yet, and send a key |
| 3409 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 3410 | // window even if motions are still being processed. |
| 3411 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3412 | InputEventInjectionResult result = |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3413 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3414 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3416 | // Key will not be sent to the window, yet, because the window is still processing events |
| 3417 | // and the key remains pending, waiting for the touch events to be processed |
| 3418 | std::optional<uint32_t> keySequenceNum = mFocusedWindow->receiveEvent(); |
| 3419 | ASSERT_FALSE(keySequenceNum); |
| 3420 | |
| 3421 | // Switch the focus to the "unfocused" window that we tapped. Expect the key to go there |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3422 | mFocusedWindow->setFocusable(false); |
| 3423 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3424 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3425 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 3426 | |
| 3427 | // Focus events should precede the key events |
| 3428 | mUnfocusedWindow->consumeFocusEvent(true); |
| 3429 | mFocusedWindow->consumeFocusEvent(false); |
| 3430 | |
| 3431 | // Finish the tap events, which should unblock dispatcher |
| 3432 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 3433 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 3434 | |
| 3435 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 3436 | // can finally go to the newly focused "mUnfocusedWindow". |
| 3437 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3438 | mFocusedWindow->assertNoEvents(); |
| 3439 | mUnfocusedWindow->assertNoEvents(); |
| 3440 | } |
| 3441 | |
| 3442 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 3443 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 3444 | // The other window should not be affected by that. |
| 3445 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 3446 | // Touch Window 1 |
| 3447 | NotifyMotionArgs motionArgs = |
| 3448 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3449 | ADISPLAY_ID_DEFAULT, {FOCUSED_WINDOW_LOCATION}); |
| 3450 | mDispatcher->notifyMotion(&motionArgs); |
| 3451 | mUnfocusedWindow->consumeEvent(AINPUT_EVENT_TYPE_MOTION, AMOTION_EVENT_ACTION_OUTSIDE, |
| 3452 | ADISPLAY_ID_DEFAULT, 0 /*flags*/); |
| 3453 | |
| 3454 | // Touch Window 2 |
| 3455 | int32_t actionPointerDown = |
| 3456 | AMOTION_EVENT_ACTION_POINTER_DOWN + (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 3457 | |
| 3458 | motionArgs = |
| 3459 | generateMotionArgs(actionPointerDown, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 3460 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION}); |
| 3461 | mDispatcher->notifyMotion(&motionArgs); |
| 3462 | |
| 3463 | const std::chrono::duration timeout = |
| 3464 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 3465 | mFakePolicy->assertNotifyAnrWasCalled(timeout, nullptr /*application*/, |
| 3466 | mFocusedWindow->getToken()); |
| 3467 | |
| 3468 | mUnfocusedWindow->consumeMotionDown(); |
| 3469 | mFocusedWindow->consumeMotionDown(); |
| 3470 | // Focused window may or may not receive ACTION_MOVE |
| 3471 | // But it should definitely receive ACTION_CANCEL due to the ANR |
| 3472 | InputEvent* event; |
| 3473 | std::optional<int32_t> moveOrCancelSequenceNum = mFocusedWindow->receiveEvent(&event); |
| 3474 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 3475 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 3476 | ASSERT_NE(nullptr, event); |
| 3477 | ASSERT_EQ(event->getType(), AINPUT_EVENT_TYPE_MOTION); |
| 3478 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 3479 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 3480 | mFocusedWindow->consumeMotionCancel(); |
| 3481 | } else { |
| 3482 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 3483 | } |
| 3484 | |
| 3485 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3486 | mUnfocusedWindow->assertNoEvents(); |
| 3487 | mFocusedWindow->assertNoEvents(); |
| 3488 | } |
| 3489 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3490 | /** |
| 3491 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 3492 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 3493 | * |
| 3494 | * If the user touches another application during this time, the key should be dropped. |
| 3495 | * Next, if a new focused window comes in, without toggling the focused application, |
| 3496 | * then no ANR should occur. |
| 3497 | * |
| 3498 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 3499 | * but in some cases the policy may not update the focused application. |
| 3500 | */ |
| 3501 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 3502 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 3503 | std::make_shared<FakeApplicationHandle>(); |
| 3504 | focusedApplication->setDispatchingTimeout(60ms); |
| 3505 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 3506 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 3507 | mFocusedWindow->setFocusable(false); |
| 3508 | |
| 3509 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3510 | mFocusedWindow->consumeFocusEvent(false); |
| 3511 | |
| 3512 | // Send a key. The ANR timer should start because there is no focused window. |
| 3513 | // 'focusedApplication' will get blamed if this timer completes. |
| 3514 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3515 | InputEventInjectionResult result = |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3516 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /*repeatCount*/, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3517 | InputEventInjectionSync::NONE, 10ms /*injectionTimeout*/); |
| 3518 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 3519 | |
| 3520 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 3521 | // then the injected touches won't cause the focused event to get dropped. |
| 3522 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 3523 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 3524 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 3525 | // For this test, it means that the key would get delivered to the window once it becomes |
| 3526 | // focused. |
| 3527 | std::this_thread::sleep_for(10ms); |
| 3528 | |
| 3529 | // Touch unfocused window. This should force the pending key to get dropped. |
| 3530 | NotifyMotionArgs motionArgs = |
| 3531 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3532 | ADISPLAY_ID_DEFAULT, {UNFOCUSED_WINDOW_LOCATION}); |
| 3533 | mDispatcher->notifyMotion(&motionArgs); |
| 3534 | |
| 3535 | // We do not consume the motion right away, because that would require dispatcher to first |
| 3536 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 3537 | // Set the focused window first. |
| 3538 | mFocusedWindow->setFocusable(true); |
| 3539 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mFocusedWindow, mUnfocusedWindow}}}); |
| 3540 | setFocusedWindow(mFocusedWindow); |
| 3541 | mFocusedWindow->consumeFocusEvent(true); |
| 3542 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 3543 | // to another application. This could be a bug / behaviour in the policy. |
| 3544 | |
| 3545 | mUnfocusedWindow->consumeMotionDown(); |
| 3546 | |
| 3547 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 3548 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 3549 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 3550 | } |
| 3551 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 3552 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 3553 | // that has feature NO_INPUT_CHANNEL. |
| 3554 | // Layout: |
| 3555 | // Top (closest to user) |
| 3556 | // mNoInputWindow (above all windows) |
| 3557 | // mBottomWindow |
| 3558 | // Bottom (furthest from user) |
| 3559 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 3560 | virtual void SetUp() override { |
| 3561 | InputDispatcherTest::SetUp(); |
| 3562 | |
| 3563 | mApplication = std::make_shared<FakeApplicationHandle>(); |
| 3564 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3565 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
| 3566 | std::make_optional<sp<IBinder>>(nullptr) /*token*/); |
| 3567 | |
| 3568 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3569 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3570 | // It's perfectly valid for this window to not have an associated input channel |
| 3571 | |
| 3572 | mBottomWindow = new FakeWindowHandle(mApplication, mDispatcher, "Bottom window", |
| 3573 | ADISPLAY_ID_DEFAULT); |
| 3574 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3575 | |
| 3576 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3577 | } |
| 3578 | |
| 3579 | protected: |
| 3580 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 3581 | sp<FakeWindowHandle> mNoInputWindow; |
| 3582 | sp<FakeWindowHandle> mBottomWindow; |
| 3583 | }; |
| 3584 | |
| 3585 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 3586 | PointF touchedPoint = {10, 10}; |
| 3587 | |
| 3588 | NotifyMotionArgs motionArgs = |
| 3589 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3590 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3591 | mDispatcher->notifyMotion(&motionArgs); |
| 3592 | |
| 3593 | mNoInputWindow->assertNoEvents(); |
| 3594 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 3595 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 3596 | // and therefore should prevent mBottomWindow from receiving touches |
| 3597 | mBottomWindow->assertNoEvents(); |
| 3598 | } |
| 3599 | |
| 3600 | /** |
| 3601 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 3602 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 3603 | */ |
| 3604 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 3605 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
| 3606 | mNoInputWindow = new FakeWindowHandle(mApplication, mDispatcher, |
| 3607 | "Window with input channel and NO_INPUT_CHANNEL", |
| 3608 | ADISPLAY_ID_DEFAULT); |
| 3609 | |
| 3610 | mNoInputWindow->setInputFeatures(InputWindowInfo::Feature::NO_INPUT_CHANNEL); |
| 3611 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3612 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mNoInputWindow, mBottomWindow}}}); |
| 3613 | |
| 3614 | PointF touchedPoint = {10, 10}; |
| 3615 | |
| 3616 | NotifyMotionArgs motionArgs = |
| 3617 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 3618 | ADISPLAY_ID_DEFAULT, {touchedPoint}); |
| 3619 | mDispatcher->notifyMotion(&motionArgs); |
| 3620 | |
| 3621 | mNoInputWindow->assertNoEvents(); |
| 3622 | mBottomWindow->assertNoEvents(); |
| 3623 | } |
| 3624 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3625 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 3626 | protected: |
| 3627 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 3628 | sp<FakeWindowHandle> mWindow; |
| 3629 | sp<FakeWindowHandle> mMirror; |
| 3630 | |
| 3631 | virtual void SetUp() override { |
| 3632 | InputDispatcherTest::SetUp(); |
| 3633 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 3634 | mWindow = new FakeWindowHandle(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
| 3635 | mMirror = new FakeWindowHandle(mApp, mDispatcher, "TestWindowMirror", ADISPLAY_ID_DEFAULT, |
| 3636 | mWindow->getToken()); |
| 3637 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 3638 | mWindow->setFocusable(true); |
| 3639 | mMirror->setFocusable(true); |
| 3640 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3641 | } |
| 3642 | }; |
| 3643 | |
| 3644 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 3645 | // Request focus on a mirrored window |
| 3646 | setFocusedWindow(mMirror); |
| 3647 | |
| 3648 | // window gets focused |
| 3649 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3651 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3652 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 3653 | } |
| 3654 | |
| 3655 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 3656 | // focusable. |
| 3657 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 3658 | setFocusedWindow(mMirror); |
| 3659 | |
| 3660 | // window gets focused |
| 3661 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3663 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3664 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3666 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3667 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3668 | |
| 3669 | mMirror->setFocusable(false); |
| 3670 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3671 | |
| 3672 | // window loses focus since one of the windows associated with the token in not focusable |
| 3673 | mWindow->consumeFocusEvent(false); |
| 3674 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3675 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3676 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3677 | mWindow->assertNoEvents(); |
| 3678 | } |
| 3679 | |
| 3680 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 3681 | // invisible. |
| 3682 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 3683 | setFocusedWindow(mMirror); |
| 3684 | |
| 3685 | // window gets focused |
| 3686 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3688 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3689 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3691 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3692 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3693 | |
| 3694 | mMirror->setVisible(false); |
| 3695 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3696 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3697 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3698 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3699 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3701 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3702 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3703 | |
| 3704 | mWindow->setVisible(false); |
| 3705 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3706 | |
| 3707 | // window loses focus only after all windows associated with the token become invisible. |
| 3708 | mWindow->consumeFocusEvent(false); |
| 3709 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3710 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3711 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3712 | mWindow->assertNoEvents(); |
| 3713 | } |
| 3714 | |
| 3715 | // A focused & mirrored window remains focused until both windows are removed. |
| 3716 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 3717 | setFocusedWindow(mMirror); |
| 3718 | |
| 3719 | // window gets focused |
| 3720 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3722 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3723 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3725 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3726 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3727 | |
| 3728 | // single window is removed but the window token remains focused |
| 3729 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mMirror}}}); |
| 3730 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(mDispatcher)) |
| 3732 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3733 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(mDispatcher)) |
| 3735 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3736 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 3737 | |
| 3738 | // Both windows are removed |
| 3739 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {}}}); |
| 3740 | mWindow->consumeFocusEvent(false); |
| 3741 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3742 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(mDispatcher)) |
| 3743 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3744 | mWindow->assertNoEvents(); |
| 3745 | } |
| 3746 | |
| 3747 | // Focus request can be pending until one window becomes visible. |
| 3748 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 3749 | // Request focus on an invisible mirror. |
| 3750 | mWindow->setVisible(false); |
| 3751 | mMirror->setVisible(false); |
| 3752 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3753 | setFocusedWindow(mMirror); |
| 3754 | |
| 3755 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3757 | injectKey(mDispatcher, AKEY_EVENT_ACTION_DOWN, 0 /* repeatCount */, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3758 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3759 | |
| 3760 | mMirror->setVisible(true); |
| 3761 | mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {mWindow, mMirror}}}); |
| 3762 | |
| 3763 | // window gets focused |
| 3764 | mWindow->consumeFocusEvent(true); |
| 3765 | // window gets the pending key event |
| 3766 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3767 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 3768 | } // namespace android::inputdispatcher |