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 #ifndef _UI_TEST_INPUT_LISTENER_H
18 #define _UI_TEST_INPUT_LISTENER_H
19 
20 #include <android-base/thread_annotations.h>
21 #include <gtest/gtest.h>
22 #include "InputListener.h"
23 
24 using std::chrono_literals::operator""ms;
25 
26 namespace android {
27 
28 // --- TestInputListener ---
29 
30 class TestInputListener : public InputListenerInterface {
31 protected:
32     virtual ~TestInputListener();
33 
34 public:
35     TestInputListener(std::chrono::milliseconds eventHappenedTimeout = 0ms,
36                       std::chrono::milliseconds eventDidNotHappenTimeout = 0ms);
37 
38     void assertNotifyConfigurationChangedWasCalled(
39             NotifyConfigurationChangedArgs* outEventArgs = nullptr);
40 
41     void assertNotifyConfigurationChangedWasNotCalled();
42 
43     void assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs = nullptr);
44 
45     void assertNotifyDeviceResetWasNotCalled();
46 
47     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = nullptr);
48 
49     void assertNotifyKeyWasNotCalled();
50 
51     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = nullptr);
52 
53     void assertNotifyMotionWasNotCalled();
54 
55     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = nullptr);
56 
57 private:
58     template <class NotifyArgsType>
59     void assertCalled(NotifyArgsType* outEventArgs, std::string message);
60 
61     template <class NotifyArgsType>
62     void assertNotCalled(std::string message);
63 
64     template <class NotifyArgsType>
65     void notify(const NotifyArgsType* args);
66 
67     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
68 
69     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
70 
71     virtual void notifyKey(const NotifyKeyArgs* args) override;
72 
73     virtual void notifyMotion(const NotifyMotionArgs* args) override;
74 
75     virtual void notifySwitch(const NotifySwitchArgs* args) override;
76 
77     std::mutex mLock;
78     std::condition_variable mCondition;
79     const std::chrono::milliseconds mEventHappenedTimeout;
80     const std::chrono::milliseconds mEventDidNotHappenTimeout;
81 
82     std::tuple<std::vector<NotifyConfigurationChangedArgs>, //
83                std::vector<NotifyDeviceResetArgs>,          //
84                std::vector<NotifyKeyArgs>,                  //
85                std::vector<NotifyMotionArgs>,               //
86                std::vector<NotifySwitchArgs>>               //
87             mQueues GUARDED_BY(mLock);
88 };
89 
90 } // namespace android
91 #endif
92