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 
18 #include <gtest/gtest.h>
19 
20 #include "TestInputListener.h"
21 
22 namespace android {
23 
24 // --- TestInputListener ---
25 
TestInputListener(std::chrono::milliseconds eventHappenedTimeout,std::chrono::milliseconds eventDidNotHappenTimeout)26 TestInputListener::TestInputListener(std::chrono::milliseconds eventHappenedTimeout,
27                                      std::chrono::milliseconds eventDidNotHappenTimeout)
28       : mEventHappenedTimeout(eventHappenedTimeout),
29         mEventDidNotHappenTimeout(eventDidNotHappenTimeout) {}
30 
~TestInputListener()31 TestInputListener::~TestInputListener() { }
32 
assertNotifyConfigurationChangedWasCalled(NotifyConfigurationChangedArgs * outEventArgs)33 void TestInputListener::assertNotifyConfigurationChangedWasCalled(
34         NotifyConfigurationChangedArgs* outEventArgs) {
35     ASSERT_NO_FATAL_FAILURE(
36             assertCalled<NotifyConfigurationChangedArgs>(outEventArgs,
37                                                          "Expected notifyConfigurationChanged() "
38                                                          "to have been called."));
39 }
40 
assertNotifyConfigurationChangedWasNotCalled()41 void TestInputListener::assertNotifyConfigurationChangedWasNotCalled() {
42     ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyConfigurationChangedArgs>(
43             "notifyConfigurationChanged() should not be called."));
44 }
45 
assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs * outEventArgs)46 void TestInputListener::assertNotifyDeviceResetWasCalled(
47         NotifyDeviceResetArgs* outEventArgs) {
48     ASSERT_NO_FATAL_FAILURE(
49             assertCalled<
50                     NotifyDeviceResetArgs>(outEventArgs,
51                                            "Expected notifyDeviceReset() to have been called."));
52 }
53 
assertNotifyDeviceResetWasNotCalled()54 void TestInputListener::assertNotifyDeviceResetWasNotCalled() {
55     ASSERT_NO_FATAL_FAILURE(
56             assertNotCalled<NotifyDeviceResetArgs>("notifyDeviceReset() should not be called."));
57 }
58 
assertNotifyKeyWasCalled(NotifyKeyArgs * outEventArgs)59 void TestInputListener::assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs) {
60     ASSERT_NO_FATAL_FAILURE(
61             assertCalled<NotifyKeyArgs>(outEventArgs, "Expected notifyKey() to have been called."));
62 }
63 
assertNotifyKeyWasNotCalled()64 void TestInputListener::assertNotifyKeyWasNotCalled() {
65     ASSERT_NO_FATAL_FAILURE(assertNotCalled<NotifyKeyArgs>("notifyKey() should not be called."));
66 }
67 
assertNotifyMotionWasCalled(NotifyMotionArgs * outEventArgs)68 void TestInputListener::assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs) {
69     ASSERT_NO_FATAL_FAILURE(
70             assertCalled<NotifyMotionArgs>(outEventArgs,
71                                            "Expected notifyMotion() to have been called."));
72 }
73 
assertNotifyMotionWasNotCalled()74 void TestInputListener::assertNotifyMotionWasNotCalled() {
75     ASSERT_NO_FATAL_FAILURE(
76             assertNotCalled<NotifySwitchArgs>("notifySwitch() should not be called."));
77 }
78 
assertNotifySwitchWasCalled(NotifySwitchArgs * outEventArgs)79 void TestInputListener::assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs) {
80     ASSERT_NO_FATAL_FAILURE(
81             assertCalled<NotifySwitchArgs>(outEventArgs,
82                                            "Expected notifySwitch() to have been called."));
83 }
84 
85 template <class NotifyArgsType>
assertCalled(NotifyArgsType * outEventArgs,std::string message)86 void TestInputListener::assertCalled(NotifyArgsType* outEventArgs, std::string message) {
87     std::unique_lock<std::mutex> lock(mLock);
88     base::ScopedLockAssertion assumeLocked(mLock);
89 
90     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
91     if (queue.empty()) {
92         const bool eventReceived =
93                 mCondition.wait_for(lock, mEventHappenedTimeout,
94                                     [&queue]() REQUIRES(mLock) { return !queue.empty(); });
95         if (!eventReceived) {
96             FAIL() << "Timed out waiting for event: " << message.c_str();
97         }
98     }
99     if (outEventArgs) {
100         *outEventArgs = *queue.begin();
101     }
102     queue.erase(queue.begin());
103 }
104 
105 template <class NotifyArgsType>
assertNotCalled(std::string message)106 void TestInputListener::assertNotCalled(std::string message) {
107     std::unique_lock<std::mutex> lock(mLock);
108     base::ScopedLockAssertion assumeLocked(mLock);
109 
110     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
111     const bool eventReceived =
112             mCondition.wait_for(lock, mEventDidNotHappenTimeout,
113                                 [&queue]() REQUIRES(mLock) { return !queue.empty(); });
114     if (eventReceived) {
115         FAIL() << "Unexpected event: " << message.c_str();
116     }
117 }
118 
119 template <class NotifyArgsType>
notify(const NotifyArgsType * args)120 void TestInputListener::notify(const NotifyArgsType* args) {
121     std::scoped_lock<std::mutex> lock(mLock);
122 
123     std::vector<NotifyArgsType>& queue = std::get<std::vector<NotifyArgsType>>(mQueues);
124     queue.push_back(*args);
125     mCondition.notify_all();
126 }
127 
notifyConfigurationChanged(const NotifyConfigurationChangedArgs * args)128 void TestInputListener::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
129     notify<NotifyConfigurationChangedArgs>(args);
130 }
131 
notifyDeviceReset(const NotifyDeviceResetArgs * args)132 void TestInputListener::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
133     notify<NotifyDeviceResetArgs>(args);
134 }
135 
notifyKey(const NotifyKeyArgs * args)136 void TestInputListener::notifyKey(const NotifyKeyArgs* args) {
137     notify<NotifyKeyArgs>(args);
138 }
139 
notifyMotion(const NotifyMotionArgs * args)140 void TestInputListener::notifyMotion(const NotifyMotionArgs* args) {
141     notify<NotifyMotionArgs>(args);
142 }
143 
notifySwitch(const NotifySwitchArgs * args)144 void TestInputListener::notifySwitch(const NotifySwitchArgs* args) {
145     notify<NotifySwitchArgs>(args);
146 }
147 
148 } // namespace android
149