Harry Cutts | 144ff54 | 2022-11-28 17:41:06 +0000 | [diff] [blame] | 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); |
| 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( |
Arpit Singh | 4f61f3c | 2023-06-13 15:05:53 +0000 | [diff] [blame] | 47 | nsecs_t when, int32_t eventHubId, const InputDeviceIdentifier& identifier); |
Harry Cutts | 144ff54 | 2022-11-28 17:41:06 +0000 | [diff] [blame] | 48 | |
| 49 | class FakeInputReaderContext : public ContextImpl { |
| 50 | public: |
| 51 | FakeInputReaderContext(InputReader* reader) |
| 52 | : ContextImpl(reader), |
| 53 | mGlobalMetaState(0), |
| 54 | mUpdateGlobalMetaStateWasCalled(false), |
| 55 | mGeneration(1) {} |
| 56 | |
| 57 | virtual ~FakeInputReaderContext() {} |
| 58 | |
| 59 | void assertUpdateGlobalMetaStateWasCalled() { |
| 60 | ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled) |
| 61 | << "Expected updateGlobalMetaState() to have been called."; |
| 62 | mUpdateGlobalMetaStateWasCalled = false; |
| 63 | } |
| 64 | |
| 65 | void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; } |
| 66 | |
| 67 | uint32_t getGeneration() { return mGeneration; } |
| 68 | |
| 69 | void updateGlobalMetaState() override { |
| 70 | mUpdateGlobalMetaStateWasCalled = true; |
| 71 | ContextImpl::updateGlobalMetaState(); |
| 72 | } |
| 73 | |
| 74 | int32_t getGlobalMetaState() override { |
| 75 | return mGlobalMetaState | ContextImpl::getGlobalMetaState(); |
| 76 | } |
| 77 | |
| 78 | int32_t bumpGeneration() override { |
| 79 | mGeneration = ContextImpl::bumpGeneration(); |
| 80 | return mGeneration; |
| 81 | } |
| 82 | |
| 83 | void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; } |
| 84 | |
| 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 | |
| 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 | |
| 98 | void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override { |
| 99 | outDevices = mExternalStylusDevices; |
| 100 | } |
| 101 | |
| 102 | void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) { |
| 103 | mExternalStylusDevices = devices; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | int32_t mGlobalMetaState; |
| 108 | bool mUpdateGlobalMetaStateWasCalled; |
| 109 | int32_t mGeneration; |
| 110 | std::optional<nsecs_t> mRequestedTimeout; |
| 111 | std::vector<InputDeviceInfo> mExternalStylusDevices; |
| 112 | } mFakeContext; |
| 113 | |
| 114 | friend class InputReaderTest; |
| 115 | |
| 116 | public: |
| 117 | FakeInputReaderContext* getContext() { return &mFakeContext; } |
| 118 | |
| 119 | private: |
| 120 | std::queue<std::shared_ptr<InputDevice>> mNextDevices; |
| 121 | }; |
| 122 | |
| 123 | } // namespace android |