• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(nsecs_t when, int32_t eventHubId,
47                                                             const InputDeviceIdentifier& identifier,
48                                                             ftl::Flags<InputDeviceClass> classes);
49 
50     class FakeInputReaderContext : public ContextImpl {
51     public:
FakeInputReaderContext(InputReader * reader)52         FakeInputReaderContext(InputReader* reader)
53               : ContextImpl(reader),
54                 mGlobalMetaState(0),
55                 mUpdateGlobalMetaStateWasCalled(false),
56                 mGeneration(1) {}
57 
~FakeInputReaderContext()58         virtual ~FakeInputReaderContext() {}
59 
assertUpdateGlobalMetaStateWasCalled()60         void assertUpdateGlobalMetaStateWasCalled() {
61             ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
62                     << "Expected updateGlobalMetaState() to have been called.";
63             mUpdateGlobalMetaStateWasCalled = false;
64         }
65 
setGlobalMetaState(int32_t state)66         void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
67 
getGeneration()68         uint32_t getGeneration() { return mGeneration; }
69 
updateGlobalMetaState()70         void updateGlobalMetaState() override {
71             mUpdateGlobalMetaStateWasCalled = true;
72             ContextImpl::updateGlobalMetaState();
73         }
74 
getGlobalMetaState()75         int32_t getGlobalMetaState() override {
76             return mGlobalMetaState | ContextImpl::getGlobalMetaState();
77         }
78 
bumpGeneration()79         int32_t bumpGeneration() override {
80             mGeneration = ContextImpl::bumpGeneration();
81             return mGeneration;
82         }
83 
requestTimeoutAtTime(nsecs_t when)84         void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
85 
assertTimeoutWasRequested(nsecs_t when)86         void assertTimeoutWasRequested(nsecs_t when) {
87             ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
88                                            << " but there was no timeout requested.";
89             ASSERT_EQ(when, *mRequestedTimeout);
90             mRequestedTimeout.reset();
91         }
92 
assertTimeoutWasNotRequested()93         void assertTimeoutWasNotRequested() {
94             ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
95                                                " but one was requested at time "
96                                             << *mRequestedTimeout;
97         }
98 
getExternalStylusDevices(std::vector<InputDeviceInfo> & outDevices)99         void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
100             outDevices = mExternalStylusDevices;
101         }
102 
setExternalStylusDevices(std::vector<InputDeviceInfo> && devices)103         void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
104             mExternalStylusDevices = devices;
105         }
106 
setPreventingTouchpadTaps(bool prevent)107         void setPreventingTouchpadTaps(bool prevent) override { mPreventingTouchpadTaps = prevent; }
isPreventingTouchpadTaps()108         bool isPreventingTouchpadTaps() override { return mPreventingTouchpadTaps; }
109 
setLastKeyDownTimestamp(nsecs_t when)110         void setLastKeyDownTimestamp(nsecs_t when) override { mLastKeyDownTimestamp = when; };
getLastKeyDownTimestamp()111         nsecs_t getLastKeyDownTimestamp() override { return mLastKeyDownTimestamp; };
112 
113     private:
114         int32_t mGlobalMetaState;
115         bool mUpdateGlobalMetaStateWasCalled;
116         int32_t mGeneration;
117         std::optional<nsecs_t> mRequestedTimeout;
118         std::vector<InputDeviceInfo> mExternalStylusDevices;
119         bool mPreventingTouchpadTaps{false};
120         nsecs_t mLastKeyDownTimestamp;
121     } mFakeContext;
122 
123     friend class InputReaderTest;
124 
125 public:
getContext()126     FakeInputReaderContext* getContext() { return &mFakeContext; }
127 
128 private:
129     std::queue<std::shared_ptr<InputDevice>> mNextDevices;
130 };
131 
132 } // namespace android
133