1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2023 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 "InputDispatcherPolicyInterface.h" 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include "InputDispatcherInterface.h" 22*38e8c45fSAndroid Build Coastguard Worker #include "NotifyArgs.h" 23*38e8c45fSAndroid Build Coastguard Worker 24*38e8c45fSAndroid Build Coastguard Worker #include <condition_variable> 25*38e8c45fSAndroid Build Coastguard Worker #include <functional> 26*38e8c45fSAndroid Build Coastguard Worker #include <memory> 27*38e8c45fSAndroid Build Coastguard Worker #include <mutex> 28*38e8c45fSAndroid Build Coastguard Worker #include <optional> 29*38e8c45fSAndroid Build Coastguard Worker #include <queue> 30*38e8c45fSAndroid Build Coastguard Worker #include <string> 31*38e8c45fSAndroid Build Coastguard Worker #include <vector> 32*38e8c45fSAndroid Build Coastguard Worker 33*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h> 34*38e8c45fSAndroid Build Coastguard Worker #include <android-base/thread_annotations.h> 35*38e8c45fSAndroid Build Coastguard Worker #include <binder/IBinder.h> 36*38e8c45fSAndroid Build Coastguard Worker #include <gui/PidUid.h> 37*38e8c45fSAndroid Build Coastguard Worker #include <gui/WindowInfo.h> 38*38e8c45fSAndroid Build Coastguard Worker #include <input/BlockingQueue.h> 39*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h> 40*38e8c45fSAndroid Build Coastguard Worker 41*38e8c45fSAndroid Build Coastguard Worker namespace android { 42*38e8c45fSAndroid Build Coastguard Worker 43*38e8c45fSAndroid Build Coastguard Worker class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { 44*38e8c45fSAndroid Build Coastguard Worker public: 45*38e8c45fSAndroid Build Coastguard Worker FakeInputDispatcherPolicy() = default; 46*38e8c45fSAndroid Build Coastguard Worker virtual ~FakeInputDispatcherPolicy() = default; 47*38e8c45fSAndroid Build Coastguard Worker 48*38e8c45fSAndroid Build Coastguard Worker struct AnrResult { 49*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> token{}; 50*38e8c45fSAndroid Build Coastguard Worker std::optional<gui::Pid> pid{}; 51*38e8c45fSAndroid Build Coastguard Worker }; 52*38e8c45fSAndroid Build Coastguard Worker 53*38e8c45fSAndroid Build Coastguard Worker struct UserActivityPokeEvent { 54*38e8c45fSAndroid Build Coastguard Worker nsecs_t eventTime; 55*38e8c45fSAndroid Build Coastguard Worker int32_t eventType; 56*38e8c45fSAndroid Build Coastguard Worker ui::LogicalDisplayId displayId; 57*38e8c45fSAndroid Build Coastguard Worker 58*38e8c45fSAndroid Build Coastguard Worker bool operator==(const UserActivityPokeEvent& rhs) const = default; 59*38e8c45fSAndroid Build Coastguard Worker inline friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) { 60*38e8c45fSAndroid Build Coastguard Worker os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType 61*38e8c45fSAndroid Build Coastguard Worker << ", displayId=" << ev.displayId << "]"; 62*38e8c45fSAndroid Build Coastguard Worker return os; 63*38e8c45fSAndroid Build Coastguard Worker } 64*38e8c45fSAndroid Build Coastguard Worker }; 65*38e8c45fSAndroid Build Coastguard Worker 66*38e8c45fSAndroid Build Coastguard Worker void assertFilterInputEventWasCalled(const NotifyKeyArgs& args); 67*38e8c45fSAndroid Build Coastguard Worker void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point); 68*38e8c45fSAndroid Build Coastguard Worker void assertFilterInputEventWasNotCalled(); 69*38e8c45fSAndroid Build Coastguard Worker void assertNotifySwitchWasCalled(const NotifySwitchArgs& args); 70*38e8c45fSAndroid Build Coastguard Worker void assertOnPointerDownEquals(const sp<IBinder>& touchedToken); 71*38e8c45fSAndroid Build Coastguard Worker void assertOnPointerDownWasNotCalled(); 72*38e8c45fSAndroid Build Coastguard Worker /** 73*38e8c45fSAndroid Build Coastguard Worker * This function must be called soon after the expected ANR timer starts, 74*38e8c45fSAndroid Build Coastguard Worker * because we are also checking how much time has passed. 75*38e8c45fSAndroid Build Coastguard Worker */ 76*38e8c45fSAndroid Build Coastguard Worker void assertNotifyNoFocusedWindowAnrWasCalled( 77*38e8c45fSAndroid Build Coastguard Worker std::chrono::nanoseconds timeout, 78*38e8c45fSAndroid Build Coastguard Worker const std::shared_ptr<InputApplicationHandle>& expectedApplication); 79*38e8c45fSAndroid Build Coastguard Worker void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, 80*38e8c45fSAndroid Build Coastguard Worker const sp<gui::WindowInfoHandle>& window); 81*38e8c45fSAndroid Build Coastguard Worker void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, 82*38e8c45fSAndroid Build Coastguard Worker const sp<IBinder>& expectedToken, 83*38e8c45fSAndroid Build Coastguard Worker std::optional<gui::Pid> expectedPid); 84*38e8c45fSAndroid Build Coastguard Worker /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ 85*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout); 86*38e8c45fSAndroid Build Coastguard Worker void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, 87*38e8c45fSAndroid Build Coastguard Worker std::optional<gui::Pid> expectedPid); 88*38e8c45fSAndroid Build Coastguard Worker /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ 89*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> getResponsiveWindowToken(); 90*38e8c45fSAndroid Build Coastguard Worker void assertNotifyAnrWasNotCalled(); 91*38e8c45fSAndroid Build Coastguard Worker PointerCaptureRequest assertSetPointerCaptureCalled(const sp<gui::WindowInfoHandle>& window, 92*38e8c45fSAndroid Build Coastguard Worker bool enabled); 93*38e8c45fSAndroid Build Coastguard Worker void assertSetPointerCaptureNotCalled(); 94*38e8c45fSAndroid Build Coastguard Worker void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, 95*38e8c45fSAndroid Build Coastguard Worker const sp<IBinder>& targetToken); 96*38e8c45fSAndroid Build Coastguard Worker void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token); 97*38e8c45fSAndroid Build Coastguard Worker /** 98*38e8c45fSAndroid Build Coastguard Worker * Set policy timeout. A value of zero means next key will not be intercepted. 99*38e8c45fSAndroid Build Coastguard Worker */ 100*38e8c45fSAndroid Build Coastguard Worker void setInterceptKeyTimeout(std::chrono::milliseconds timeout); 101*38e8c45fSAndroid Build Coastguard Worker std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override; 102*38e8c45fSAndroid Build Coastguard Worker void setStaleEventTimeout(std::chrono::nanoseconds timeout); 103*38e8c45fSAndroid Build Coastguard Worker void assertUserActivityNotPoked(); 104*38e8c45fSAndroid Build Coastguard Worker /** 105*38e8c45fSAndroid Build Coastguard Worker * Asserts that a user activity poke has happened. The earliest recorded poke event will be 106*38e8c45fSAndroid Build Coastguard Worker * cleared after this call. 107*38e8c45fSAndroid Build Coastguard Worker * 108*38e8c45fSAndroid Build Coastguard Worker * If an expected UserActivityPokeEvent is provided, asserts that the given event is the 109*38e8c45fSAndroid Build Coastguard Worker * earliest recorded poke event. 110*38e8c45fSAndroid Build Coastguard Worker */ 111*38e8c45fSAndroid Build Coastguard Worker void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}); 112*38e8c45fSAndroid Build Coastguard Worker void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids); 113*38e8c45fSAndroid Build Coastguard Worker void assertNotifyDeviceInteractionWasNotCalled(); 114*38e8c45fSAndroid Build Coastguard Worker void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler); 115*38e8c45fSAndroid Build Coastguard Worker void assertUnhandledKeyReported(int32_t keycode); 116*38e8c45fSAndroid Build Coastguard Worker void assertUnhandledKeyNotReported(); 117*38e8c45fSAndroid Build Coastguard Worker void setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching); 118*38e8c45fSAndroid Build Coastguard Worker void assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay); 119*38e8c45fSAndroid Build Coastguard Worker 120*38e8c45fSAndroid Build Coastguard Worker private: 121*38e8c45fSAndroid Build Coastguard Worker std::mutex mLock; 122*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); 123*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); 124*38e8c45fSAndroid Build Coastguard Worker std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); 125*38e8c45fSAndroid Build Coastguard Worker 126*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mPointerCaptureChangedCondition; 127*38e8c45fSAndroid Build Coastguard Worker 128*38e8c45fSAndroid Build Coastguard Worker std::optional<ui::LogicalDisplayId> mNotifiedFocusedDisplay GUARDED_BY(mLock); 129*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mFocusedDisplayNotifiedCondition; 130*38e8c45fSAndroid Build Coastguard Worker 131*38e8c45fSAndroid Build Coastguard Worker std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); 132*38e8c45fSAndroid Build Coastguard Worker // ANR handling 133*38e8c45fSAndroid Build Coastguard Worker std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); 134*38e8c45fSAndroid Build Coastguard Worker std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); 135*38e8c45fSAndroid Build Coastguard Worker std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); 136*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mNotifyAnr; 137*38e8c45fSAndroid Build Coastguard Worker std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); 138*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mNotifyInputChannelBroken; 139*38e8c45fSAndroid Build Coastguard Worker 140*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); 141*38e8c45fSAndroid Build Coastguard Worker bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; 142*38e8c45fSAndroid Build Coastguard Worker 143*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mNotifyUserActivity; 144*38e8c45fSAndroid Build Coastguard Worker std::queue<UserActivityPokeEvent> mUserActivityPokeEvents; 145*38e8c45fSAndroid Build Coastguard Worker 146*38e8c45fSAndroid Build Coastguard Worker std::chrono::milliseconds mInterceptKeyTimeout = 0ms; 147*38e8c45fSAndroid Build Coastguard Worker 148*38e8c45fSAndroid Build Coastguard Worker std::chrono::nanoseconds mStaleEventTimeout = 1000ms; 149*38e8c45fSAndroid Build Coastguard Worker 150*38e8c45fSAndroid Build Coastguard Worker bool mConsumeKeyBeforeDispatching = false; 151*38e8c45fSAndroid Build Coastguard Worker 152*38e8c45fSAndroid Build Coastguard Worker BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; 153*38e8c45fSAndroid Build Coastguard Worker 154*38e8c45fSAndroid Build Coastguard Worker std::condition_variable mNotifyUnhandledKey; 155*38e8c45fSAndroid Build Coastguard Worker std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); 156*38e8c45fSAndroid Build Coastguard Worker std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); 157*38e8c45fSAndroid Build Coastguard Worker 158*38e8c45fSAndroid Build Coastguard Worker /** 159*38e8c45fSAndroid Build Coastguard Worker * All three ANR-related callbacks behave the same way, so we use this generic function to wait 160*38e8c45fSAndroid Build Coastguard Worker * for a specific container to become non-empty. When the container is non-empty, return the 161*38e8c45fSAndroid Build Coastguard Worker * first entry from the container and erase it. 162*38e8c45fSAndroid Build Coastguard Worker */ 163*38e8c45fSAndroid Build Coastguard Worker template <class T> 164*38e8c45fSAndroid Build Coastguard Worker T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, 165*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex>& lock) REQUIRES(mLock); 166*38e8c45fSAndroid Build Coastguard Worker 167*38e8c45fSAndroid Build Coastguard Worker template <class T> 168*38e8c45fSAndroid Build Coastguard Worker std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, 169*38e8c45fSAndroid Build Coastguard Worker std::queue<T>& storage, 170*38e8c45fSAndroid Build Coastguard Worker std::unique_lock<std::mutex>& lock, 171*38e8c45fSAndroid Build Coastguard Worker std::condition_variable& condition) 172*38e8c45fSAndroid Build Coastguard Worker REQUIRES(mLock); 173*38e8c45fSAndroid Build Coastguard Worker 174*38e8c45fSAndroid Build Coastguard Worker void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, 175*38e8c45fSAndroid Build Coastguard Worker const std::string&) override; 176*38e8c45fSAndroid Build Coastguard Worker void notifyWindowResponsive(const sp<IBinder>& connectionToken, 177*38e8c45fSAndroid Build Coastguard Worker std::optional<gui::Pid> pid) override; 178*38e8c45fSAndroid Build Coastguard Worker void notifyNoFocusedWindowAnr( 179*38e8c45fSAndroid Build Coastguard Worker const std::shared_ptr<InputApplicationHandle>& applicationHandle) override; 180*38e8c45fSAndroid Build Coastguard Worker void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override; 181*38e8c45fSAndroid Build Coastguard Worker void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override; 182*38e8c45fSAndroid Build Coastguard Worker void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, 183*38e8c45fSAndroid Build Coastguard Worker InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, 184*38e8c45fSAndroid Build Coastguard Worker const std::vector<float>& values) override; 185*38e8c45fSAndroid Build Coastguard Worker void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, 186*38e8c45fSAndroid Build Coastguard Worker InputDeviceSensorAccuracy accuracy) override; 187*38e8c45fSAndroid Build Coastguard Worker void notifyVibratorState(int32_t deviceId, bool isOn) override; 188*38e8c45fSAndroid Build Coastguard Worker bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override; 189*38e8c45fSAndroid Build Coastguard Worker void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override; 190*38e8c45fSAndroid Build Coastguard Worker void interceptMotionBeforeQueueing(ui::LogicalDisplayId, uint32_t, int32_t, nsecs_t, 191*38e8c45fSAndroid Build Coastguard Worker uint32_t&) override; 192*38e8c45fSAndroid Build Coastguard Worker nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override; 193*38e8c45fSAndroid Build Coastguard Worker std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, 194*38e8c45fSAndroid Build Coastguard Worker uint32_t) override; 195*38e8c45fSAndroid Build Coastguard Worker void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, 196*38e8c45fSAndroid Build Coastguard Worker uint32_t policyFlags) override; 197*38e8c45fSAndroid Build Coastguard Worker void pokeUserActivity(nsecs_t eventTime, int32_t eventType, 198*38e8c45fSAndroid Build Coastguard Worker ui::LogicalDisplayId displayId) override; 199*38e8c45fSAndroid Build Coastguard Worker bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override; 200*38e8c45fSAndroid Build Coastguard Worker void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override; 201*38e8c45fSAndroid Build Coastguard Worker void setPointerCapture(const PointerCaptureRequest& request) override; 202*38e8c45fSAndroid Build Coastguard Worker void notifyDropWindow(const sp<IBinder>& token, float x, float y) override; 203*38e8c45fSAndroid Build Coastguard Worker void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, 204*38e8c45fSAndroid Build Coastguard Worker const std::set<gui::Uid>& uids) override; 205*38e8c45fSAndroid Build Coastguard Worker void notifyFocusedDisplayChanged(ui::LogicalDisplayId displayId) override; 206*38e8c45fSAndroid Build Coastguard Worker 207*38e8c45fSAndroid Build Coastguard Worker void assertFilterInputEventWasCalledInternal( 208*38e8c45fSAndroid Build Coastguard Worker const std::function<void(const InputEvent&)>& verify); 209*38e8c45fSAndroid Build Coastguard Worker }; 210*38e8c45fSAndroid Build Coastguard Worker 211*38e8c45fSAndroid Build Coastguard Worker } // namespace android 212