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