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