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