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     void assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs* outEventArgs = nullptr);
58     void assertNotifyCaptureWasNotCalled();
59     void assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs = nullptr);
60     void assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs = nullptr);
61 
62 private:
63     template <class NotifyArgsType>
64     void assertCalled(NotifyArgsType* outEventArgs, std::string message);
65 
66     template <class NotifyArgsType>
67     void assertNotCalled(std::string message);
68 
69     template <class NotifyArgsType>
70     void notify(const NotifyArgsType* args);
71 
72     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
73 
74     virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
75 
76     virtual void notifyKey(const NotifyKeyArgs* args) override;
77 
78     virtual void notifyMotion(const NotifyMotionArgs* args) override;
79 
80     virtual void notifySwitch(const NotifySwitchArgs* args) override;
81 
82     virtual void notifySensor(const NotifySensorArgs* args) override;
83 
84     virtual void notifyVibratorState(const NotifyVibratorStateArgs* args) override;
85 
86     virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override;
87 
88     std::mutex mLock;
89     std::condition_variable mCondition;
90     const std::chrono::milliseconds mEventHappenedTimeout;
91     const std::chrono::milliseconds mEventDidNotHappenTimeout;
92 
93     std::tuple<std::vector<NotifyConfigurationChangedArgs>,  //
94                std::vector<NotifyDeviceResetArgs>,           //
95                std::vector<NotifyKeyArgs>,                   //
96                std::vector<NotifyMotionArgs>,                //
97                std::vector<NotifySwitchArgs>,                //
98                std::vector<NotifySensorArgs>,                //
99                std::vector<NotifyVibratorStateArgs>,         //
100                std::vector<NotifyPointerCaptureChangedArgs>> //
101             mQueues GUARDED_BY(mLock);
102 };
103 
104 } // namespace android
105 #endif
106