xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/TestInputListener.cpp (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 #include "TestInputListener.h"
18 
19 #include <gtest/gtest.h>
20 
21 namespace android {
22 
23 // --- TestInputListener ---
24 
TestInputListener(std::chrono::milliseconds eventHappenedTimeout,std::chrono::milliseconds eventDidNotHappenTimeout)25 TestInputListener::TestInputListener(std::chrono::milliseconds eventHappenedTimeout,
26                                      std::chrono::milliseconds eventDidNotHappenTimeout)
27       : mEventHappenedTimeout(eventHappenedTimeout),
28         mEventDidNotHappenTimeout(eventDidNotHappenTimeout) {}
29 
~TestInputListener()30 TestInputListener::~TestInputListener() {}
31 
assertNotifyInputDevicesChangedWasCalled(NotifyInputDevicesChangedArgs * outEventArgs)32 void TestInputListener::assertNotifyInputDevicesChangedWasCalled(
33         NotifyInputDevicesChangedArgs* outEventArgs) {
34     ASSERT_NO_FATAL_FAILURE(
35             assertCalled<NotifyInputDevicesChangedArgs>(outEventArgs,
36                                                         "Expected notifyInputDevicesChanged() "
37                                                         "to have been called."));
38 }
39 
assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs * outEventArgs)40 void TestInputListener::assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs) {
41     ASSERT_NO_FATAL_FAILURE(
42             assertCalled<
43                     NotifyDeviceResetArgs>(outEventArgs,
44                                            "Expected notifyDeviceReset() to have been called."));
45 }
46 
clearNotifyDeviceResetCalls()47 void TestInputListener::clearNotifyDeviceResetCalls() {
48     std::scoped_lock<std::mutex> lock(mLock);
49     std::get<std::vector<NotifyDeviceResetArgs>>(mQueues).clear();
50 }
51 
assertNotifyDeviceResetWasCalled(const::testing::Matcher<NotifyDeviceResetArgs> & matcher)52 void TestInputListener::assertNotifyDeviceResetWasCalled(
53         const ::testing::Matcher<NotifyDeviceResetArgs>& matcher) {
54     NotifyDeviceResetArgs outEventArgs;
55     ASSERT_NO_FATAL_FAILURE(assertNotifyDeviceResetWasCalled(&outEventArgs));
56     ASSERT_THAT(outEventArgs, matcher);
57 }
58 
assertNotifyDeviceResetWasNotCalled()59 void TestInputListener::assertNotifyDeviceResetWasNotCalled() {
60     ASSERT_NO_FATAL_FAILURE(
61             assertNotCalled<NotifyDeviceResetArgs>("notifyDeviceReset() should not be called."));
62 }
63 
assertNotifyKeyWasCalled(NotifyKeyArgs * outEventArgs)64 void TestInputListener::assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs) {
65     ASSERT_NO_FATAL_FAILURE(
66             assertCalled<NotifyKeyArgs>(outEventArgs, "Expected notifyKey() to have been called."));
67 }
68 
assertNotifyKeyWasCalled(const::testing::Matcher<NotifyKeyArgs> & matcher)69 void TestInputListener::assertNotifyKeyWasCalled(const ::testing::Matcher<NotifyKeyArgs>& matcher) {
70     NotifyKeyArgs outEventArgs;
71     ASSERT_NO_FATAL_FAILURE(assertNotifyKeyWasCalled(&outEventArgs));
72     ASSERT_THAT(outEventArgs, matcher);
73 }
74 
assertNotifyKeyWasNotCalled()75 void TestInputListener::assertNotifyKeyWasNotCalled() {
76     ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyKeyArgs>("notifyKey() should not be called."));
77 }
78 
assertNotifyMotionWasCalled(NotifyMotionArgs * outEventArgs,std::optional<TimePoint> waitUntil)79 void TestInputListener::assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs,
80                                                     std::optional<TimePoint> waitUntil) {
81     ASSERT_NO_FATAL_FAILURE(
82             assertCalled<NotifyMotionArgs>(outEventArgs,
83                                            "Expected notifyMotion() to have been called.",
84                                            waitUntil));
85 }
86 
assertNotifyMotionWasCalled(const::testing::Matcher<NotifyMotionArgs> & matcher,std::optional<TimePoint> waitUntil)87 void TestInputListener::assertNotifyMotionWasCalled(
88         const ::testing::Matcher<NotifyMotionArgs>& matcher, std::optional<TimePoint> waitUntil) {
89     NotifyMotionArgs outEventArgs;
90     ASSERT_NO_FATAL_FAILURE(assertNotifyMotionWasCalled(&outEventArgs, waitUntil));
91     ASSERT_THAT(outEventArgs, matcher);
92 }
93 
assertNotifyMotionWasNotCalled(std::optional<TimePoint> waitUntil)94 void TestInputListener::assertNotifyMotionWasNotCalled(std::optional<TimePoint> waitUntil) {
95     ASSERT_NO_FATAL_FAILURE(
96             assertNotCalled<NotifyMotionArgs>("notifyMotion() should not be called.", waitUntil));
97 }
98 
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs)99 void TestInputListener::assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs) {
100     ASSERT_NO_FATAL_FAILURE(
101             assertCalled<NotifySwitchArgs>(outEventArgs,
102                                            "Expected notifySwitch() to have been called."));
103 }
104 
assertNotifySensorWasCalled(NotifySensorArgs * outEventArgs)105 void TestInputListener::assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs) {
106     ASSERT_NO_FATAL_FAILURE(
107             assertCalled<NotifySensorArgs>(outEventArgs,
108                                            "Expected notifySensor() to have been called."));
109 }
110 
assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs * outEventArgs)111 void TestInputListener::assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs) {
112     ASSERT_NO_FATAL_FAILURE(assertCalled<NotifyVibratorStateArgs>(outEventArgs,
113                                                                   "Expected notifyVibratorState() "
114                                                                   "to have been called."));
115 }
116 
assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs * outEventArgs)117 void TestInputListener::assertNotifyCaptureWasCalled(
118         NotifyPointerCaptureChangedArgs* outEventArgs) {
119     ASSERT_NO_FATAL_FAILURE(
120             assertCalled<NotifyPointerCaptureChangedArgs>(outEventArgs,
121                                                           "Expected notifyPointerCaptureChanged() "
122                                                           "to have been called."));
123 }
124 
assertNotifyCaptureWasNotCalled()125 void TestInputListener::assertNotifyCaptureWasNotCalled() {
126     ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyPointerCaptureChangedArgs>(
127             "notifyPointerCaptureChanged() should not be called."));
128 }
129 
130 template <class NotifyArgsType>
assertCalled(NotifyArgsType * outEventArgs,std::string message,std::optional<TimePoint> waitUntil)131 void TestInputListener::assertCalled(NotifyArgsType* outEventArgs, std::string message,
132                                      std::optional<TimePoint> waitUntil) {
133     std::unique_lock<std::mutex> lock(mLock);
134     base::ScopedLockAssertion assumeLocked(mLock);
135 
136     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
137     if (queue.empty()) {
138         const auto time =
139                 waitUntil.value_or(std::chrono::system_clock::now() + mEventHappenedTimeout);
140         const bool eventReceived = mCondition.wait_until(lock, time, [&queue]() REQUIRES(mLock) {
141             return !queue.empty();
142         });
143         if (!eventReceived) {
144             FAIL() << "Timed out waiting for event: " << message.c_str();
145         }
146     }
147     if (outEventArgs) {
148         *outEventArgs = *queue.begin();
149     }
150     queue.erase(queue.begin());
151 }
152 
153 template <class NotifyArgsType>
assertNotCalled(std::string message,std::optional<TimePoint> waitUntil)154 void TestInputListener::assertNotCalled(std::string message, std::optional<TimePoint> waitUntil) {
155     std::unique_lock<std::mutex> lock(mLock);
156     base::ScopedLockAssertion assumeLocked(mLock);
157 
158     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
159     const auto time =
160             waitUntil.value_or(std::chrono::system_clock::now() + mEventDidNotHappenTimeout);
161     const bool eventReceived = mCondition.wait_until(lock, time, [&queue]() REQUIRES(mLock) {
162         return !queue.empty();
163     });
164     if (eventReceived) {
165         FAIL() << "Unexpected event: " << message.c_str();
166     }
167 }
168 
169 template <class NotifyArgsType>
addToQueue(const NotifyArgsType & args)170 void TestInputListener::addToQueue(const NotifyArgsType& args) {
171     std::scoped_lock<std::mutex> lock(mLock);
172 
173     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
174     queue.push_back(args);
175     mCondition.notify_all();
176 }
177 
notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs & args)178 void TestInputListener::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) {
179     addToQueue<NotifyInputDevicesChangedArgs>(args);
180 }
181 
notifyDeviceReset(const NotifyDeviceResetArgs & args)182 void TestInputListener::notifyDeviceReset(const NotifyDeviceResetArgs& args) {
183     addToQueue<NotifyDeviceResetArgs>(args);
184 }
185 
notifyKey(const NotifyKeyArgs & args)186 void TestInputListener::notifyKey(const NotifyKeyArgs& args) {
187     addToQueue<NotifyKeyArgs>(args);
188 }
189 
notifyMotion(const NotifyMotionArgs & args)190 void TestInputListener::notifyMotion(const NotifyMotionArgs& args) {
191     addToQueue<NotifyMotionArgs>(args);
192 }
193 
notifySwitch(const NotifySwitchArgs & args)194 void TestInputListener::notifySwitch(const NotifySwitchArgs& args) {
195     addToQueue<NotifySwitchArgs>(args);
196 }
197 
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs & args)198 void TestInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) {
199     addToQueue<NotifyPointerCaptureChangedArgs>(args);
200 }
201 
notifySensor(const NotifySensorArgs & args)202 void TestInputListener::notifySensor(const NotifySensorArgs& args) {
203     addToQueue<NotifySensorArgs>(args);
204 }
205 
notifyVibratorState(const NotifyVibratorStateArgs & args)206 void TestInputListener::notifyVibratorState(const NotifyVibratorStateArgs& args) {
207     addToQueue<NotifyVibratorStateArgs>(args);
208 }
209 
210 } // namespace android
211