• 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 #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 assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs = nullptr);
47 
48     void assertNotifyDeviceResetWasNotCalled();
49 
50     void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = nullptr);
51 
52     void assertNotifyKeyWasCalled(const ::testing::Matcher<NotifyKeyArgs>& matcher);
53 
54     void assertNotifyKeyWasNotCalled();
55 
56     void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = nullptr,
57                                      std::optional<TimePoint> waitUntil = {});
58 
59     void assertNotifyMotionWasCalled(const ::testing::Matcher<NotifyMotionArgs>& matcher,
60                                      std::optional<TimePoint> waitUntil = {});
61 
62     void assertNotifyMotionWasNotCalled(std::optional<TimePoint> waitUntil = {});
63 
64     void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = nullptr);
65 
66     void assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs* outEventArgs = nullptr);
67     void assertNotifyCaptureWasNotCalled();
68     void assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs = nullptr);
69     void assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs = nullptr);
70 
71 private:
72     template <class NotifyArgsType>
73     void assertCalled(NotifyArgsType* outEventArgs, std::string message,
74                       std::optional<TimePoint> waitUntil = {});
75 
76     template <class NotifyArgsType>
77     void assertNotCalled(std::string message, std::optional<TimePoint> timeout = {});
78 
79     template <class NotifyArgsType>
80     void addToQueue(const NotifyArgsType& args);
81 
82     virtual void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
83 
84     virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
85 
86     virtual void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
87 
88     virtual void notifyKey(const NotifyKeyArgs& args) override;
89 
90     virtual void notifyMotion(const NotifyMotionArgs& args) override;
91 
92     virtual void notifySwitch(const NotifySwitchArgs& args) override;
93 
94     virtual void notifySensor(const NotifySensorArgs& args) override;
95 
96     virtual void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
97 
98     virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
99 
100     std::mutex mLock;
101     std::condition_variable mCondition;
102     const std::chrono::milliseconds mEventHappenedTimeout;
103     const std::chrono::milliseconds mEventDidNotHappenTimeout;
104 
105     std::tuple<std::vector<NotifyInputDevicesChangedArgs>,   //
106                std::vector<NotifyConfigurationChangedArgs>,  //
107                std::vector<NotifyDeviceResetArgs>,           //
108                std::vector<NotifyKeyArgs>,                   //
109                std::vector<NotifyMotionArgs>,                //
110                std::vector<NotifySwitchArgs>,                //
111                std::vector<NotifySensorArgs>,                //
112                std::vector<NotifyVibratorStateArgs>,         //
113                std::vector<NotifyPointerCaptureChangedArgs>> //
114             mQueues GUARDED_BY(mLock);
115 };
116 
117 } // namespace android
118