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 <gmock/gmock.h> 22 #include <gtest/gtest.h> 23 #include "InputListener.h" 24 25 using std::chrono_literals::operator""ms; 26 27 namespace android { 28 29 // --- TestInputListener --- 30 31 class TestInputListener : public InputListenerInterface { 32 public: 33 TestInputListener(std::chrono::milliseconds eventHappenedTimeout = 0ms, 34 std::chrono::milliseconds eventDidNotHappenTimeout = 0ms); 35 virtual ~TestInputListener(); 36 37 void assertNotifyConfigurationChangedWasCalled( 38 NotifyConfigurationChangedArgs* outEventArgs = nullptr); 39 40 void assertNotifyConfigurationChangedWasNotCalled(); 41 42 void assertNotifyDeviceResetWasCalled(NotifyDeviceResetArgs* outEventArgs = nullptr); 43 44 void assertNotifyDeviceResetWasNotCalled(); 45 46 void assertNotifyKeyWasCalled(NotifyKeyArgs* outEventArgs = nullptr); 47 48 void assertNotifyKeyWasNotCalled(); 49 50 void assertNotifyMotionWasCalled(NotifyMotionArgs* outEventArgs = nullptr); 51 52 void assertNotifyMotionWasCalled(const ::testing::Matcher<NotifyMotionArgs>& matcher); 53 54 void assertNotifyMotionWasNotCalled(); 55 56 void assertNotifySwitchWasCalled(NotifySwitchArgs* outEventArgs = nullptr); 57 58 void assertNotifyCaptureWasCalled(NotifyPointerCaptureChangedArgs* outEventArgs = nullptr); 59 void assertNotifyCaptureWasNotCalled(); 60 void assertNotifySensorWasCalled(NotifySensorArgs* outEventArgs = nullptr); 61 void assertNotifyVibratorStateWasCalled(NotifyVibratorStateArgs* outEventArgs = nullptr); 62 63 private: 64 template <class NotifyArgsType> 65 void assertCalled(NotifyArgsType* outEventArgs, std::string message); 66 67 template <class NotifyArgsType> 68 void assertNotCalled(std::string message); 69 70 template <class NotifyArgsType> 71 void notify(const NotifyArgsType* args); 72 73 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override; 74 75 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override; 76 77 virtual void notifyKey(const NotifyKeyArgs* args) override; 78 79 virtual void notifyMotion(const NotifyMotionArgs* args) override; 80 81 virtual void notifySwitch(const NotifySwitchArgs* args) override; 82 83 virtual void notifySensor(const NotifySensorArgs* args) override; 84 85 virtual void notifyVibratorState(const NotifyVibratorStateArgs* args) override; 86 87 virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override; 88 89 std::mutex mLock; 90 std::condition_variable mCondition; 91 const std::chrono::milliseconds mEventHappenedTimeout; 92 const std::chrono::milliseconds mEventDidNotHappenTimeout; 93 94 std::tuple<std::vector<NotifyConfigurationChangedArgs>, // 95 std::vector<NotifyDeviceResetArgs>, // 96 std::vector<NotifyKeyArgs>, // 97 std::vector<NotifyMotionArgs>, // 98 std::vector<NotifySwitchArgs>, // 99 std::vector<NotifySensorArgs>, // 100 std::vector<NotifyVibratorStateArgs>, // 101 std::vector<NotifyPointerCaptureChangedArgs>> // 102 mQueues GUARDED_BY(mLock); 103 }; 104 105 } // namespace android 106 #endif 107