1 /* 2 * Copyright 2022 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 <memory> 20 #include <queue> 21 #include <string> 22 23 #include <InputDevice.h> 24 #include <InputReader.h> 25 #include <gtest/gtest.h> 26 #include <utils/StrongPointer.h> 27 28 namespace android { 29 30 class InstrumentedInputReader : public InputReader { 31 public: 32 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub, 33 const sp<InputReaderPolicyInterface>& policy, 34 InputListenerInterface& listener); ~InstrumentedInputReader()35 virtual ~InstrumentedInputReader() {} 36 37 void pushNextDevice(std::shared_ptr<InputDevice> device); 38 39 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name, 40 const std::string& location = ""); 41 42 // Make the protected loopOnce method accessible to tests. 43 using InputReader::loopOnce; 44 45 protected: 46 virtual std::shared_ptr<InputDevice> createDeviceLocked( 47 nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier); 48 49 class FakeInputReaderContext : public ContextImpl { 50 public: FakeInputReaderContext(InputReader * reader)51 FakeInputReaderContext(InputReader* reader) 52 : ContextImpl(reader), 53 mGlobalMetaState(0), 54 mUpdateGlobalMetaStateWasCalled(false), 55 mGeneration(1) {} 56 ~FakeInputReaderContext()57 virtual ~FakeInputReaderContext() {} 58 assertUpdateGlobalMetaStateWasCalled()59 void assertUpdateGlobalMetaStateWasCalled() { 60 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled) 61 << "Expected updateGlobalMetaState() to have been called."; 62 mUpdateGlobalMetaStateWasCalled = false; 63 } 64 setGlobalMetaState(int32_t state)65 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; } 66 getGeneration()67 uint32_t getGeneration() { return mGeneration; } 68 updateGlobalMetaState()69 void updateGlobalMetaState() override { 70 mUpdateGlobalMetaStateWasCalled = true; 71 ContextImpl::updateGlobalMetaState(); 72 } 73 getGlobalMetaState()74 int32_t getGlobalMetaState() override { 75 return mGlobalMetaState | ContextImpl::getGlobalMetaState(); 76 } 77 bumpGeneration()78 int32_t bumpGeneration() override { 79 mGeneration = ContextImpl::bumpGeneration(); 80 return mGeneration; 81 } 82 requestTimeoutAtTime(nsecs_t when)83 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; } 84 assertTimeoutWasRequested(nsecs_t when)85 void assertTimeoutWasRequested(nsecs_t when) { 86 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when 87 << " but there was no timeout requested."; 88 ASSERT_EQ(when, *mRequestedTimeout); 89 mRequestedTimeout.reset(); 90 } 91 assertTimeoutWasNotRequested()92 void assertTimeoutWasNotRequested() { 93 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested," 94 " but one was requested at time " 95 << *mRequestedTimeout; 96 } 97 getExternalStylusDevices(std::vector<InputDeviceInfo> & outDevices)98 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override { 99 outDevices = mExternalStylusDevices; 100 } 101 setExternalStylusDevices(std::vector<InputDeviceInfo> && devices)102 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) { 103 mExternalStylusDevices = devices; 104 } 105 setPreventingTouchpadTaps(bool prevent)106 void setPreventingTouchpadTaps(bool prevent) override { mPreventingTouchpadTaps = prevent; } isPreventingTouchpadTaps()107 bool isPreventingTouchpadTaps() override { return mPreventingTouchpadTaps; } 108 setLastKeyDownTimestamp(nsecs_t when)109 void setLastKeyDownTimestamp(nsecs_t when) override { mLastKeyDownTimestamp = when; }; getLastKeyDownTimestamp()110 nsecs_t getLastKeyDownTimestamp() override { return mLastKeyDownTimestamp; }; 111 112 private: 113 int32_t mGlobalMetaState; 114 bool mUpdateGlobalMetaStateWasCalled; 115 int32_t mGeneration; 116 std::optional<nsecs_t> mRequestedTimeout; 117 std::vector<InputDeviceInfo> mExternalStylusDevices; 118 bool mPreventingTouchpadTaps{false}; 119 nsecs_t mLastKeyDownTimestamp; 120 } mFakeContext; 121 122 friend class InputReaderTest; 123 124 public: getContext()125 FakeInputReaderContext* getContext() { return &mFakeContext; } 126 127 private: 128 std::queue<std::shared_ptr<InputDevice>> mNextDevices; 129 }; 130 131 } // namespace android 132