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