blob: c0c8975e766e850a6fe9fa6d91da554a47b511b4 [file] [log] [blame]
Prabir Pradhanc5340732024-03-20 22:53:52 +00001/*
2 * Copyright 2024 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
17#pragma once
18
19#include "../dispatcher/InputDispatcher.h"
20#include "TestEventMatchers.h"
21
22#include <android-base/logging.h>
23#include <gtest/gtest.h>
24#include <input/Input.h>
25#include <input/InputConsumer.h>
26
27namespace android {
28
29/**
30 * If we expect to receive the event, the timeout can be made very long. When the test are running
31 * correctly, we will actually never wait until the end of the timeout because the wait will end
32 * when the event comes in. Still, this value shouldn't be infinite. During development, a local
33 * change may cause the test to fail. This timeout should be short enough to not annoy so that the
34 * developer can see the failure quickly (on human scale).
35 */
36static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms;
37
38/**
39 * When no event is expected, we can have a very short timeout. A large value here would slow down
40 * the tests. In the unlikely event of system being too slow, the event may still be present but the
41 * timeout would complete before it is consumed. This would result in test flakiness. If this
42 * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this
43 * would get noticed and addressed quickly.
44 */
45static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms;
46
47/**
48 * The default pid and uid for windows created on the primary display by the test.
49 */
50static constexpr gui::Pid WINDOW_PID{999};
51static constexpr gui::Uid WINDOW_UID{1001};
52
53/**
54 * Default input dispatching timeout if there is no focused application or paused window
55 * from which to determine an appropriate dispatching timeout.
56 */
57static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds(
58 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
59 android::base::HwTimeoutMultiplier());
60
61// --- FakeInputReceiver ---
62
63class FakeInputReceiver {
64public:
65 explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name);
66
67 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false);
68 /**
69 * Receive an event without acknowledging it.
70 * Return the sequence number that could later be used to send finished signal.
71 */
72 std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent(
73 std::chrono::milliseconds timeout);
74 /**
75 * To be used together with "receiveEvent" to complete the consumption of an event.
76 */
77 void finishEvent(uint32_t consumeSeq, bool handled = true);
78
79 void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline);
80
81 void consumeEvent(android::InputEventType expectedEventType, int32_t expectedAction,
82 std::optional<int32_t> expectedDisplayId,
83 std::optional<int32_t> expectedFlags);
84
85 std::unique_ptr<MotionEvent> consumeMotion();
86 void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher);
87
88 void consumeFocusEvent(bool hasFocus, bool inTouchMode);
89 void consumeCaptureEvent(bool hasCapture);
90 void consumeDragEvent(bool isExiting, float x, float y);
91 void consumeTouchModeEvent(bool inTouchMode);
92
93 void assertNoEvents(std::chrono::milliseconds timeout);
94
95 sp<IBinder> getToken();
96 int getChannelFd();
97
98private:
99 InputConsumer mConsumer;
100 DynamicInputEventFactory mEventFactory;
101 std::string mName;
102};
103
104// --- FakeWindowHandle ---
105
106class FakeWindowHandle : public gui::WindowInfoHandle {
107public:
108 static const int32_t WIDTH = 600;
109 static const int32_t HEIGHT = 800;
110 using InputConfig = gui::WindowInfo::InputConfig;
111
112 // This is a callback that is fired when an event is received by the window.
113 // It is static to avoid having to pass it individually into all of the FakeWindowHandles
114 // created by tests.
115 // TODO(b/210460522): Update the tests to use a factory pattern so that we can avoid
116 // the need to make this static.
117 static std::function<void(const std::unique_ptr<InputEvent>&, const gui::WindowInfo&)>
118 sOnEventReceivedCallback;
119
120 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
121 const std::unique_ptr<inputdispatcher::InputDispatcher>& dispatcher,
122 const std::string name, int32_t displayId, bool createInputChannel = true);
123
124 sp<FakeWindowHandle> clone(int32_t displayId);
125
126 inline void setTouchable(bool touchable) {
127 mInfo.setInputConfig(InputConfig::NOT_TOUCHABLE, !touchable);
128 }
129
130 inline void setFocusable(bool focusable) {
131 mInfo.setInputConfig(InputConfig::NOT_FOCUSABLE, !focusable);
132 }
133
134 inline void setVisible(bool visible) {
135 mInfo.setInputConfig(InputConfig::NOT_VISIBLE, !visible);
136 }
137
138 inline void setDispatchingTimeout(std::chrono::nanoseconds timeout) {
139 mInfo.dispatchingTimeout = timeout;
140 }
141
142 inline void setPaused(bool paused) {
143 mInfo.setInputConfig(InputConfig::PAUSE_DISPATCHING, paused);
144 }
145
146 inline void setPreventSplitting(bool preventSplitting) {
147 mInfo.setInputConfig(InputConfig::PREVENT_SPLITTING, preventSplitting);
148 }
149
150 inline void setSlippery(bool slippery) {
151 mInfo.setInputConfig(InputConfig::SLIPPERY, slippery);
152 }
153
154 inline void setWatchOutsideTouch(bool watchOutside) {
155 mInfo.setInputConfig(InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside);
156 }
157
158 inline void setSpy(bool spy) { mInfo.setInputConfig(InputConfig::SPY, spy); }
159
160 inline void setInterceptsStylus(bool interceptsStylus) {
161 mInfo.setInputConfig(InputConfig::INTERCEPTS_STYLUS, interceptsStylus);
162 }
163
164 inline void setDropInput(bool dropInput) {
165 mInfo.setInputConfig(InputConfig::DROP_INPUT, dropInput);
166 }
167
168 inline void setDropInputIfObscured(bool dropInputIfObscured) {
169 mInfo.setInputConfig(InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured);
170 }
171
172 inline void setNoInputChannel(bool noInputChannel) {
173 mInfo.setInputConfig(InputConfig::NO_INPUT_CHANNEL, noInputChannel);
174 }
175
176 inline void setDisableUserActivity(bool disableUserActivity) {
177 mInfo.setInputConfig(InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity);
178 }
179
180 inline void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) {
181 mInfo.setInputConfig(InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, shouldGlobalStylusBlockTouch);
182 }
183
184 inline void setAlpha(float alpha) { mInfo.alpha = alpha; }
185
186 inline void setTouchOcclusionMode(gui::TouchOcclusionMode mode) {
187 mInfo.touchOcclusionMode = mode;
188 }
189
190 inline void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; }
191
192 inline void setFrame(const Rect& frame,
193 const ui::Transform& displayTransform = ui::Transform()) {
194 mInfo.frame = frame;
195 mInfo.touchableRegion.clear();
196 mInfo.addTouchableRegion(frame);
197
198 const Rect logicalDisplayFrame = displayTransform.transform(frame);
199 ui::Transform translate;
200 translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top);
201 mInfo.transform = translate * displayTransform;
202 }
203
204 inline void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; }
205
206 inline void setIsWallpaper(bool isWallpaper) {
207 mInfo.setInputConfig(InputConfig::IS_WALLPAPER, isWallpaper);
208 }
209
210 inline void setDupTouchToWallpaper(bool hasWallpaper) {
211 mInfo.setInputConfig(InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper);
212 }
213
214 inline void setTrustedOverlay(bool trustedOverlay) {
215 mInfo.setInputConfig(InputConfig::TRUSTED_OVERLAY, trustedOverlay);
216 }
217
218 inline void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) {
219 mInfo.transform.set(dsdx, dtdx, dtdy, dsdy);
220 }
221
222 inline void setWindowScale(float xScale, float yScale) {
223 setWindowTransform(xScale, 0, 0, yScale);
224 }
225
226 inline void setWindowOffset(float offsetX, float offsetY) {
227 mInfo.transform.set(offsetX, offsetY);
228 }
229
230 std::unique_ptr<KeyEvent> consumeKey(bool handled = true);
231
232 inline void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) {
233 std::unique_ptr<KeyEvent> keyEvent = consumeKey();
234 ASSERT_NE(nullptr, keyEvent);
235 ASSERT_THAT(*keyEvent, matcher);
236 }
237
238 inline void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
239 consumeKeyEvent(testing::AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
240 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
241 }
242
243 inline void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
244 consumeKeyEvent(testing::AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
245 WithDisplayId(expectedDisplayId), WithFlags(expectedFlags)));
246 }
247
248 inline void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
249 int32_t expectedFlags = 0) {
250 consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
251 WithDisplayId(expectedDisplayId),
252 WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED)));
253 }
254
255 inline void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
256 int32_t expectedFlags = 0) {
257 consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
258 WithDisplayId(expectedDisplayId),
259 WithFlags(expectedFlags)));
260 }
261
262 inline void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
263 int32_t expectedFlags = 0) {
264 consumeAnyMotionDown(expectedDisplayId, expectedFlags);
265 }
266
267 inline void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt,
268 std::optional<int32_t> expectedFlags = std::nullopt) {
269 consumeMotionEvent(
270 testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
271 testing::Conditional(expectedDisplayId.has_value(),
272 WithDisplayId(*expectedDisplayId), testing::_),
273 testing::Conditional(expectedFlags.has_value(),
274 WithFlags(*expectedFlags), testing::_)));
275 }
276
277 inline void consumeMotionPointerDown(int32_t pointerIdx,
278 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
279 int32_t expectedFlags = 0) {
280 const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN |
281 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
282 consumeMotionEvent(testing::AllOf(WithMotionAction(action),
283 WithDisplayId(expectedDisplayId),
284 WithFlags(expectedFlags)));
285 }
286
287 inline void consumeMotionPointerUp(int32_t pointerIdx,
288 int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
289 int32_t expectedFlags = 0) {
290 const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP |
291 (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
292 consumeMotionEvent(testing::AllOf(WithMotionAction(action),
293 WithDisplayId(expectedDisplayId),
294 WithFlags(expectedFlags)));
295 }
296
297 inline void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
298 int32_t expectedFlags = 0) {
299 consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
300 WithDisplayId(expectedDisplayId),
301 WithFlags(expectedFlags)));
302 }
303
304 inline void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
305 int32_t expectedFlags = 0) {
306 consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE),
307 WithDisplayId(expectedDisplayId),
308 WithFlags(expectedFlags)));
309 }
310
311 inline void consumeMotionOutsideWithZeroedCoords() {
312 consumeMotionEvent(testing::AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE),
313 WithRawCoords(0, 0)));
314 }
315
316 inline void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) {
317 ASSERT_NE(mInputReceiver, nullptr)
318 << "Cannot consume events from a window with no receiver";
319 mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode);
320 }
321
322 inline void consumeCaptureEvent(bool hasCapture) {
323 ASSERT_NE(mInputReceiver, nullptr)
324 << "Cannot consume events from a window with no receiver";
325 mInputReceiver->consumeCaptureEvent(hasCapture);
326 }
327
328 std::unique_ptr<MotionEvent> consumeMotionEvent(
329 const ::testing::Matcher<MotionEvent>& matcher = testing::_);
330
331 inline void consumeDragEvent(bool isExiting, float x, float y) {
332 mInputReceiver->consumeDragEvent(isExiting, x, y);
333 }
334
335 inline void consumeTouchModeEvent(bool inTouchMode) {
336 ASSERT_NE(mInputReceiver, nullptr)
337 << "Cannot consume events from a window with no receiver";
338 mInputReceiver->consumeTouchModeEvent(inTouchMode);
339 }
340
341 inline std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() {
342 return receive();
343 }
344
345 inline void finishEvent(uint32_t sequenceNum) {
346 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
347 mInputReceiver->finishEvent(sequenceNum);
348 }
349
350 inline void sendTimeline(int32_t inputEventId,
351 std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) {
352 ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver";
353 mInputReceiver->sendTimeline(inputEventId, timeline);
354 }
355
356 void assertNoEvents(std::optional<std::chrono::milliseconds> timeout = {});
357
358 inline sp<IBinder> getToken() { return mInfo.token; }
359
360 inline const std::string& getName() { return mName; }
361
362 inline void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) {
363 mInfo.ownerPid = ownerPid;
364 mInfo.ownerUid = ownerUid;
365 }
366
367 inline gui::Pid getPid() const { return mInfo.ownerPid; }
368
369 inline void destroyReceiver() { mInputReceiver = nullptr; }
370
371 inline int getChannelFd() { return mInputReceiver->getChannelFd(); }
372
373 // FakeWindowHandle uses this consume method to ensure received events are added to the trace.
374 std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true);
375
376private:
377 FakeWindowHandle(std::string name) : mName(name){};
378 const std::string mName;
379 std::shared_ptr<FakeInputReceiver> mInputReceiver;
380 static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger
381 friend class sp<FakeWindowHandle>;
382
383 // FakeWindowHandle uses this receive method to ensure received events are added to the trace.
384 std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive();
385};
386
387} // namespace android