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