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