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 #pragma once
18 
19 #include <android-base/thread_annotations.h>
20 #include <gmock/gmock.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 public:
32     TestInputListener(std::chrono::milliseconds eventHappenedTimeout = 0ms,
33                       std::chrono::milliseconds eventDidNotHappenTimeout = 0ms);
34     virtual ~TestInputListener();
35 
36     using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
37 
38     void assertNotifyInputDevicesChangedWasCalled(
39             NotifyInputDevicesChangedArgs* outEventArgs = nullptr);
40 
41     void assertNotifyConfigurationChangedWasCalled(
42             NotifyConfigurationChangedArgs* outEventArgs = nullptr);
43 
44     void assertNotifyConfigurationChangedWasNotCalled();
45 
46     void clearNotifyDeviceResetCalls();
47 
48     void assertNotifyDeviceResetWasCalled(const ::testing::Matcher<NotifyDeviceResetArgs>& matcher);
49 
50     void assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs = nullptr);
51 
52     void assertNotifyDeviceResetWasNotCalled();
53 
54     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = nullptr);
55 
56     void assertNotifyKeyWasCalled(const ::testing::Matcher<NotifyKeyArgs>& matcher);
57 
58     void assertNotifyKeyWasNotCalled();
59 
60     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = nullptr,
61                                      std::optional<TimePoint> waitUntil = {});
62 
63     void assertNotifyMotionWasCalled(const ::testing::Matcher<NotifyMotionArgs>& matcher,
64                                      std::optional<TimePoint> waitUntil = {});
65 
66     void assertNotifyMotionWasNotCalled(std::optional<TimePoint> waitUntil = {});
67 
68     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = nullptr);
69 
70     void assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs* outEventArgs = nullptr);
71     void assertNotifyCaptureWasNotCalled();
72     void assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs = nullptr);
73     void assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs = nullptr);
74 
75 private:
76     template <class NotifyArgsType>
77     void assertCalled(NotifyArgsType* outEventArgs, std::string message,
78                       std::optional<TimePoint> waitUntil = {});
79 
80     template <class NotifyArgsType>
81     void assertNotCalled(std::string message, std::optional<TimePoint> timeout = {});
82 
83     template <class NotifyArgsType>
84     void addToQueue(const NotifyArgsType& args);
85 
86     virtual void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
87 
88     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
89 
90     virtual void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
91 
92     virtual void notifyKey(const NotifyKeyArgs& args) override;
93 
94     virtual void notifyMotion(const NotifyMotionArgs& args) override;
95 
96     virtual void notifySwitch(const NotifySwitchArgs& args) override;
97 
98     virtual void notifySensor(const NotifySensorArgs& args) override;
99 
100     virtual void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
101 
102     virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
103 
104     std::mutex mLock;
105     std::condition_variable mCondition;
106     const std::chrono::milliseconds mEventHappenedTimeout;
107     const std::chrono::milliseconds mEventDidNotHappenTimeout;
108 
109     std::tuple<std::vector<NotifyInputDevicesChangedArgs>,   //
110                std::vector<NotifyConfigurationChangedArgs>,  //
111                std::vector<NotifyDeviceResetArgs>,           //
112                std::vector<NotifyKeyArgs>,                   //
113                std::vector<NotifyMotionArgs>,                //
114                std::vector<NotifySwitchArgs>,                //
115                std::vector<NotifySensorArgs>,                //
116                std::vector<NotifyVibratorStateArgs>,         //
117                std::vector<NotifyPointerCaptureChangedArgs>> //
118             mQueues GUARDED_BY(mLock);
119 };
120 
121 } // namespace android
122