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
assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs * outEventArgs)32 void TestInputListener::assertNotifyConfigurationChangedWasCalled(
33 NotifyConfigurationChangedArgs* outEventArgs) {
34 ASSERT_NO_FATAL_FAILURE(
35 assertCalled<NotifyConfigurationChangedArgs>(outEventArgs,
36 "Expected notifyConfigurationChanged() "
37 "to have been called."));
38 }
39
assertNotifyConfigurationChangedWasNotCalled()40 void TestInputListener::assertNotifyConfigurationChangedWasNotCalled() {
41 ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyConfigurationChangedArgs>(
42 "notifyConfigurationChanged() should not be called."));
43 }
44
assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs * outEventArgs)45 void TestInputListener::assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs) {
46 ASSERT_NO_FATAL_FAILURE(
47 assertCalled<
48 NotifyDeviceResetArgs>(outEventArgs,
49 "Expected notifyDeviceReset() to have been called."));
50 }
51
assertNotifyDeviceResetWasNotCalled()52 void TestInputListener::assertNotifyDeviceResetWasNotCalled() {
53 ASSERT_NO_FATAL_FAILURE(
54 assertNotCalled<NotifyDeviceResetArgs>("notifyDeviceReset() should not be called."));
55 }
56
assertNotifyKeyWasCalled(NotifyKeyArgs * outEventArgs)57 void TestInputListener::assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs) {
58 ASSERT_NO_FATAL_FAILURE(
59 assertCalled<NotifyKeyArgs>(outEventArgs, "Expected notifyKey() to have been called."));
60 }
61
assertNotifyKeyWasNotCalled()62 void TestInputListener::assertNotifyKeyWasNotCalled() {
63 ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyKeyArgs>("notifyKey() should not be called."));
64 }
65
assertNotifyMotionWasCalled(NotifyMotionArgs * outEventArgs)66 void TestInputListener::assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs) {
67 ASSERT_NO_FATAL_FAILURE(
68 assertCalled<NotifyMotionArgs>(outEventArgs,
69 "Expected notifyMotion() to have been called."));
70 }
71
assertNotifyMotionWasNotCalled()72 void TestInputListener::assertNotifyMotionWasNotCalled() {
73 ASSERT_NO_FATAL_FAILURE(
74 assertNotCalled<NotifyMotionArgs>("notifyMotion() should not be called."));
75 }
76
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs)77 void TestInputListener::assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs) {
78 ASSERT_NO_FATAL_FAILURE(
79 assertCalled<NotifySwitchArgs>(outEventArgs,
80 "Expected notifySwitch() to have been called."));
81 }
82
assertNotifySensorWasCalled(NotifySensorArgs * outEventArgs)83 void TestInputListener::assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs) {
84 ASSERT_NO_FATAL_FAILURE(
85 assertCalled<NotifySensorArgs>(outEventArgs,
86 "Expected notifySensor() to have been called."));
87 }
88
assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs * outEventArgs)89 void TestInputListener::assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs) {
90 ASSERT_NO_FATAL_FAILURE(assertCalled<NotifyVibratorStateArgs>(outEventArgs,
91 "Expected notifyVibratorState() "
92 "to have been called."));
93 }
94
assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs * outEventArgs)95 void TestInputListener::assertNotifyCaptureWasCalled(
96 NotifyPointerCaptureChangedArgs* outEventArgs) {
97 ASSERT_NO_FATAL_FAILURE(
98 assertCalled<NotifyPointerCaptureChangedArgs>(outEventArgs,
99 "Expected notifyPointerCaptureChanged() "
100 "to have been called."));
101 }
102
assertNotifyCaptureWasNotCalled()103 void TestInputListener::assertNotifyCaptureWasNotCalled() {
104 ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyPointerCaptureChangedArgs>(
105 "notifyPointerCaptureChanged() should not be called."));
106 }
107
108 template <class NotifyArgsType>
assertCalled(NotifyArgsType * outEventArgs,std::string message)109 void TestInputListener::assertCalled(NotifyArgsType* outEventArgs, std::string message) {
110 std::unique_lock<std::mutex> lock(mLock);
111 base::ScopedLockAssertion assumeLocked(mLock);
112
113 std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
114 if (queue.empty()) {
115 const bool eventReceived =
116 mCondition.wait_for(lock, mEventHappenedTimeout,
117 [&queue]() REQUIRES(mLock) { return !queue.empty(); });
118 if (!eventReceived) {
119 FAIL() << "Timed out waiting for event: " << message.c_str();
120 }
121 }
122 if (outEventArgs) {
123 *outEventArgs = *queue.begin();
124 }
125 queue.erase(queue.begin());
126 }
127
128 template <class NotifyArgsType>
assertNotCalled(std::string message)129 void TestInputListener::assertNotCalled(std::string message) {
130 std::unique_lock<std::mutex> lock(mLock);
131 base::ScopedLockAssertion assumeLocked(mLock);
132
133 std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
134 const bool eventReceived =
135 mCondition.wait_for(lock, mEventDidNotHappenTimeout,
136 [&queue]() REQUIRES(mLock) { return !queue.empty(); });
137 if (eventReceived) {
138 FAIL() << "Unexpected event: " << message.c_str();
139 }
140 }
141
142 template <class NotifyArgsType>
notify(const NotifyArgsType * args)143 void TestInputListener::notify(const NotifyArgsType* args) {
144 std::scoped_lock<std::mutex> lock(mLock);
145
146 std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
147 queue.push_back(*args);
148 mCondition.notify_all();
149 }
150
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)151 void TestInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
152 notify<NotifyConfigurationChangedArgs>(args);
153 }
154
notifyDeviceReset(const NotifyDeviceResetArgs * args)155 void TestInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
156 notify<NotifyDeviceResetArgs>(args);
157 }
158
notifyKey(const NotifyKeyArgs * args)159 void TestInputListener::notifyKey(const NotifyKeyArgs* args) {
160 notify<NotifyKeyArgs>(args);
161 }
162
notifyMotion(const NotifyMotionArgs * args)163 void TestInputListener::notifyMotion(const NotifyMotionArgs* args) {
164 notify<NotifyMotionArgs>(args);
165 }
166
notifySwitch(const NotifySwitchArgs * args)167 void TestInputListener::notifySwitch(const NotifySwitchArgs* args) {
168 notify<NotifySwitchArgs>(args);
169 }
170
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs * args)171 void TestInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
172 notify<NotifyPointerCaptureChangedArgs>(args);
173 }
174
notifySensor(const NotifySensorArgs * args)175 void TestInputListener::notifySensor(const NotifySensorArgs* args) {
176 notify<NotifySensorArgs>(args);
177 }
178
notifyVibratorState(const NotifyVibratorStateArgs * args)179 void TestInputListener::notifyVibratorState(const NotifyVibratorStateArgs* args) {
180 notify<NotifyVibratorStateArgs>(args);
181 }
182
183 } // namespace android
184