xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/TestInputListener.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2019 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 <android-base/thread_annotations.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "InputListener.h"
23 
24 using std::chrono_literals::operator""ms;
25 
26 namespace android {
27 
28 // --- TestInputListener ---
29 
30 class TestInputListener : public InputListenerInterface {
31 public:
32     TestInputListener(std::chrono::milliseconds eventHappenedTimeout = 0ms,
33                       std::chrono::milliseconds eventDidNotHappenTimeout = 0ms);
34     virtual ~TestInputListener();
35 
36     using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
37 
38     void assertNotifyInputDevicesChangedWasCalled(
39             NotifyInputDevicesChangedArgs* outEventArgs = nullptr);
40 
41     void clearNotifyDeviceResetCalls();
42 
43     void assertNotifyDeviceResetWasCalled(const ::testing::Matcher<NotifyDeviceResetArgs>& matcher);
44 
45     void assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs = nullptr);
46 
47     void assertNotifyDeviceResetWasNotCalled();
48 
49     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = nullptr);
50 
51     void assertNotifyKeyWasCalled(const ::testing::Matcher<NotifyKeyArgs>& matcher);
52 
53     void assertNotifyKeyWasNotCalled();
54 
55     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = nullptr,
56                                      std::optional<TimePoint> waitUntil = {});
57 
58     void assertNotifyMotionWasCalled(const ::testing::Matcher<NotifyMotionArgs>& matcher,
59                                      std::optional<TimePoint> waitUntil = {});
60 
61     void assertNotifyMotionWasNotCalled(std::optional<TimePoint> waitUntil = {});
62 
63     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = nullptr);
64 
65     void assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs* outEventArgs = nullptr);
66     void assertNotifyCaptureWasNotCalled();
67     void assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs = nullptr);
68     void assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs = nullptr);
69 
70 private:
71     template <class NotifyArgsType>
72     void assertCalled(NotifyArgsType* outEventArgs, std::string message,
73                       std::optional<TimePoint> waitUntil = {});
74 
75     template <class NotifyArgsType>
76     void assertNotCalled(std::string message, std::optional<TimePoint> timeout = {});
77 
78     template <class NotifyArgsType>
79     void addToQueue(const NotifyArgsType& args);
80 
81     virtual void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
82 
83     virtual void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
84 
85     virtual void notifyKey(const NotifyKeyArgs& args) override;
86 
87     virtual void notifyMotion(const NotifyMotionArgs& args) override;
88 
89     virtual void notifySwitch(const NotifySwitchArgs& args) override;
90 
91     virtual void notifySensor(const NotifySensorArgs& args) override;
92 
93     virtual void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
94 
95     virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
96 
97     std::mutex mLock;
98     std::condition_variable mCondition;
99     const std::chrono::milliseconds mEventHappenedTimeout;
100     const std::chrono::milliseconds mEventDidNotHappenTimeout;
101 
102     std::tuple<std::vector<NotifyInputDevicesChangedArgs>,   //
103                std::vector<NotifyDeviceResetArgs>,           //
104                std::vector<NotifyKeyArgs>,                   //
105                std::vector<NotifyMotionArgs>,                //
106                std::vector<NotifySwitchArgs>,                //
107                std::vector<NotifySensorArgs>,                //
108                std::vector<NotifyVibratorStateArgs>,         //
109                std::vector<NotifyPointerCaptureChangedArgs>> //
110             mQueues GUARDED_BY(mLock);
111 };
112 
113 } // namespace android
114