• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
assertNotifyMotionWasCalled(const::testing::Matcher<NotifyMotionArgs> & matcher)72 void TestInputListener::assertNotifyMotionWasCalled(
73         const ::testing::Matcher<NotifyMotionArgs>& matcher) {
74     NotifyMotionArgs outEventArgs;
75     ASSERT_NO_FATAL_FAILURE(assertNotifyMotionWasCalled(&outEventArgs));
76     ASSERT_THAT(outEventArgs, matcher);
77 }
78 
assertNotifyMotionWasNotCalled()79 void TestInputListener::assertNotifyMotionWasNotCalled() {
80     ASSERT_NO_FATAL_FAILURE(
81             assertNotCalled<NotifyMotionArgs>("notifyMotion() should not be called."));
82 }
83 
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs)84 void TestInputListener::assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs) {
85     ASSERT_NO_FATAL_FAILURE(
86             assertCalled<NotifySwitchArgs>(outEventArgs,
87                                            "Expected notifySwitch() to have been called."));
88 }
89 
assertNotifySensorWasCalled(NotifySensorArgs * outEventArgs)90 void TestInputListener::assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs) {
91     ASSERT_NO_FATAL_FAILURE(
92             assertCalled<NotifySensorArgs>(outEventArgs,
93                                            "Expected notifySensor() to have been called."));
94 }
95 
assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs * outEventArgs)96 void TestInputListener::assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs) {
97     ASSERT_NO_FATAL_FAILURE(assertCalled<NotifyVibratorStateArgs>(outEventArgs,
98                                                                   "Expected notifyVibratorState() "
99                                                                   "to have been called."));
100 }
101 
assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs * outEventArgs)102 void TestInputListener::assertNotifyCaptureWasCalled(
103         NotifyPointerCaptureChangedArgs* outEventArgs) {
104     ASSERT_NO_FATAL_FAILURE(
105             assertCalled<NotifyPointerCaptureChangedArgs>(outEventArgs,
106                                                           "Expected notifyPointerCaptureChanged() "
107                                                           "to have been called."));
108 }
109 
assertNotifyCaptureWasNotCalled()110 void TestInputListener::assertNotifyCaptureWasNotCalled() {
111     ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyPointerCaptureChangedArgs>(
112             "notifyPointerCaptureChanged() should not be called."));
113 }
114 
115 template <class NotifyArgsType>
assertCalled(NotifyArgsType * outEventArgs,std::string message)116 void TestInputListener::assertCalled(NotifyArgsType* outEventArgs, std::string message) {
117     std::unique_lock<std::mutex> lock(mLock);
118     base::ScopedLockAssertion assumeLocked(mLock);
119 
120     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
121     if (queue.empty()) {
122         const bool eventReceived =
123                 mCondition.wait_for(lock, mEventHappenedTimeout,
124                                     [&queue]() REQUIRES(mLock) { return !queue.empty(); });
125         if (!eventReceived) {
126             FAIL() << "Timed out waiting for event: " << message.c_str();
127         }
128     }
129     if (outEventArgs) {
130         *outEventArgs = *queue.begin();
131     }
132     queue.erase(queue.begin());
133 }
134 
135 template <class NotifyArgsType>
assertNotCalled(std::string message)136 void TestInputListener::assertNotCalled(std::string message) {
137     std::unique_lock<std::mutex> lock(mLock);
138     base::ScopedLockAssertion assumeLocked(mLock);
139 
140     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
141     const bool eventReceived =
142             mCondition.wait_for(lock, mEventDidNotHappenTimeout,
143                                 [&queue]() REQUIRES(mLock) { return !queue.empty(); });
144     if (eventReceived) {
145         FAIL() << "Unexpected event: " << message.c_str();
146     }
147 }
148 
149 template <class NotifyArgsType>
notify(const NotifyArgsType * args)150 void TestInputListener::notify(const NotifyArgsType* args) {
151     std::scoped_lock<std::mutex> lock(mLock);
152 
153     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
154     queue.push_back(*args);
155     mCondition.notify_all();
156 }
157 
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)158 void TestInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
159     notify<NotifyConfigurationChangedArgs>(args);
160 }
161 
notifyDeviceReset(const NotifyDeviceResetArgs * args)162 void TestInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
163     notify<NotifyDeviceResetArgs>(args);
164 }
165 
notifyKey(const NotifyKeyArgs * args)166 void TestInputListener::notifyKey(const NotifyKeyArgs* args) {
167     notify<NotifyKeyArgs>(args);
168 }
169 
notifyMotion(const NotifyMotionArgs * args)170 void TestInputListener::notifyMotion(const NotifyMotionArgs* args) {
171     notify<NotifyMotionArgs>(args);
172 }
173 
notifySwitch(const NotifySwitchArgs * args)174 void TestInputListener::notifySwitch(const NotifySwitchArgs* args) {
175     notify<NotifySwitchArgs>(args);
176 }
177 
notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs * args)178 void TestInputListener::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
179     notify<NotifyPointerCaptureChangedArgs>(args);
180 }
181 
notifySensor(const NotifySensorArgs * args)182 void TestInputListener::notifySensor(const NotifySensorArgs* args) {
183     notify<NotifySensorArgs>(args);
184 }
185 
notifyVibratorState(const NotifyVibratorStateArgs * args)186 void TestInputListener::notifyVibratorState(const NotifyVibratorStateArgs* args) {
187     notify<NotifyVibratorStateArgs>(args);
188 }
189 
190 } // namespace android
191