1 /*
2  * Copyright 2023 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 <cstdint>
20 #include <list>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 #include <EventHub.h>
29 #include <InputReaderBase.h>
30 #include <InputReaderContext.h>
31 #include <NotifyArgs.h>
32 #include <PointerChoreographerPolicyInterface.h>
33 #include <StylusState.h>
34 #include <VibrationElement.h>
35 #include <android-base/logging.h>
36 #include <android-base/result.h>
37 #include <gmock/gmock.h>
38 #include <input/InputDevice.h>
39 #include <input/KeyCharacterMap.h>
40 #include <input/KeyLayoutMap.h>
41 #include <input/KeyboardClassifier.h>
42 #include <input/PropertyMap.h>
43 #include <input/TouchVideoFrame.h>
44 #include <input/VirtualKeyMap.h>
45 #include <utils/Errors.h>
46 #include <utils/Timers.h>
47 
48 namespace android {
49 
50 class MockInputReaderContext : public InputReaderContext {
51 public:
52     MOCK_METHOD(void, updateGlobalMetaState, (), (override));
53     MOCK_METHOD(int32_t, getGlobalMetaState, (), (override));
54 
55     MOCK_METHOD(void, disableVirtualKeysUntil, (nsecs_t time), (override));
56     MOCK_METHOD(bool, shouldDropVirtualKey, (nsecs_t now, int32_t keyCode, int32_t scanCode),
57                 (override));
58 
59     MOCK_METHOD(void, requestTimeoutAtTime, (nsecs_t when), (override));
bumpGeneration()60     int32_t bumpGeneration() override { return ++mGeneration; }
61 
62     MOCK_METHOD(void, getExternalStylusDevices, (std::vector<InputDeviceInfo> & outDevices),
63                 (override));
64     MOCK_METHOD(std::list<NotifyArgs>, dispatchExternalStylusState, (const StylusState& outState),
65                 (override));
66 
67     MOCK_METHOD(InputReaderPolicyInterface*, getPolicy, (), (override));
68     MOCK_METHOD(EventHubInterface*, getEventHub, (), (override));
69 
getNextId()70     int32_t getNextId() override { return 1; };
71 
72     MOCK_METHOD(void, updateLedMetaState, (int32_t metaState), (override));
73     MOCK_METHOD(int32_t, getLedMetaState, (), (override));
74 
75     MOCK_METHOD(void, setPreventingTouchpadTaps, (bool prevent), (override));
76     MOCK_METHOD(bool, isPreventingTouchpadTaps, (), (override));
77 
78     MOCK_METHOD(void, setLastKeyDownTimestamp, (nsecs_t when));
79     MOCK_METHOD(nsecs_t, getLastKeyDownTimestamp, ());
80 
getKeyboardClassifier()81     KeyboardClassifier& getKeyboardClassifier() override { return *mClassifier; };
82 
83 private:
84     int32_t mGeneration = 0;
85     std::unique_ptr<KeyboardClassifier> mClassifier = std::make_unique<KeyboardClassifier>();
86 };
87 
88 class MockEventHubInterface : public EventHubInterface {
89 public:
90     MOCK_METHOD(ftl::Flags<InputDeviceClass>, getDeviceClasses, (int32_t deviceId), (const));
91     MOCK_METHOD(InputDeviceIdentifier, getDeviceIdentifier, (int32_t deviceId), (const));
92     MOCK_METHOD(int32_t, getDeviceControllerNumber, (int32_t deviceId), (const));
93     MOCK_METHOD(std::optional<PropertyMap>, getConfiguration, (int32_t deviceId), (const));
94     MOCK_METHOD(status_t, getAbsoluteAxisInfo,
95                 (int32_t deviceId, int axis, RawAbsoluteAxisInfo* outAxisInfo), (const));
96     MOCK_METHOD(bool, hasRelativeAxis, (int32_t deviceId, int axis), (const));
97     MOCK_METHOD(bool, hasInputProperty, (int32_t deviceId, int property), (const));
98     MOCK_METHOD(bool, hasMscEvent, (int32_t deviceId, int mscEvent), (const));
99     MOCK_METHOD(void, addKeyRemapping, (int32_t deviceId, int fromKeyCode, int toKeyCode), (const));
100     MOCK_METHOD(status_t, mapKey,
101                 (int32_t deviceId, int scanCode, int usageCode, int32_t metaState,
102                  int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags),
103                 (const));
104     MOCK_METHOD(status_t, mapAxis, (int32_t deviceId, int scanCode, AxisInfo* outAxisInfo),
105                 (const));
106     MOCK_METHOD(void, setExcludedDevices, (const std::vector<std::string>& devices));
107     MOCK_METHOD(std::vector<RawEvent>, getEvents, (int timeoutMillis));
108     MOCK_METHOD(std::vector<TouchVideoFrame>, getVideoFrames, (int32_t deviceId));
109     MOCK_METHOD((base::Result<std::pair<InputDeviceSensorType, int32_t>>), mapSensor,
110                 (int32_t deviceId, int32_t absCode), (const, override));
111     MOCK_METHOD(std::vector<int32_t>, getRawBatteryIds, (int32_t deviceId), (const, override));
112     MOCK_METHOD(std::optional<RawBatteryInfo>, getRawBatteryInfo,
113                 (int32_t deviceId, int32_t BatteryId), (const, override));
114     MOCK_METHOD(std::vector<int32_t>, getRawLightIds, (int32_t deviceId), (const, override));
115     MOCK_METHOD(std::optional<RawLightInfo>, getRawLightInfo, (int32_t deviceId, int32_t lightId),
116                 (const, override));
117     MOCK_METHOD(std::optional<int32_t>, getLightBrightness, (int32_t deviceId, int32_t lightId),
118                 (const, override));
119     MOCK_METHOD(void, setLightBrightness, (int32_t deviceId, int32_t lightId, int32_t brightness),
120                 (override));
121     MOCK_METHOD((std::optional<std::unordered_map<LightColor, int32_t>>), getLightIntensities,
122                 (int32_t deviceId, int32_t lightId), (const, override));
123     MOCK_METHOD(void, setLightIntensities,
124                 (int32_t deviceId, int32_t lightId,
125                  (std::unordered_map<LightColor, int32_t>)intensities),
126                 (override));
127 
128     MOCK_METHOD(std::optional<RawLayoutInfo>, getRawLayoutInfo, (int32_t deviceId),
129                 (const, override));
130     MOCK_METHOD(int32_t, getScanCodeState, (int32_t deviceId, int32_t scanCode), (const, override));
131     MOCK_METHOD(int32_t, getKeyCodeState, (int32_t deviceId, int32_t keyCode), (const, override));
132     MOCK_METHOD(int32_t, getSwitchState, (int32_t deviceId, int32_t sw), (const, override));
133 
134     MOCK_METHOD(status_t, getAbsoluteAxisValue, (int32_t deviceId, int32_t axis, int32_t* outValue),
135                 (const, override));
136     MOCK_METHOD(base::Result<std::vector<int32_t>>, getMtSlotValues,
137                 (int32_t deviceId, int32_t axis, size_t slotCount), (const, override));
138 
139     MOCK_METHOD(int32_t, getKeyCodeForKeyLocation, (int32_t deviceId, int32_t locationKeyCode),
140                 (const, override));
141     MOCK_METHOD(bool, markSupportedKeyCodes,
142                 (int32_t deviceId, const std::vector<int32_t>& keyCodes, uint8_t* outFlags),
143                 (const, override));
144 
145     MOCK_METHOD(bool, hasScanCode, (int32_t deviceId, int32_t scanCode), (const, override));
146 
147     MOCK_METHOD(bool, hasKeyCode, (int32_t deviceId, int32_t keyCode), (const, override));
148 
149     MOCK_METHOD(bool, hasLed, (int32_t deviceId, int32_t led), (const, override));
150 
151     MOCK_METHOD(void, setLedState, (int32_t deviceId, int32_t led, bool on), (override));
152 
153     MOCK_METHOD(void, getVirtualKeyDefinitions,
154                 (int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys),
155                 (const, override));
156 
157     MOCK_METHOD(const std::shared_ptr<KeyCharacterMap>, getKeyCharacterMap, (int32_t deviceId),
158                 (const, override));
159 
160     MOCK_METHOD(bool, setKeyboardLayoutOverlay,
161                 (int32_t deviceId, std::shared_ptr<KeyCharacterMap> map), (override));
162 
163     MOCK_METHOD(void, vibrate, (int32_t deviceId, const VibrationElement& effect), (override));
164     MOCK_METHOD(void, cancelVibrate, (int32_t deviceId), (override));
165 
166     MOCK_METHOD(std::vector<int32_t>, getVibratorIds, (int32_t deviceId), (const, override));
167     MOCK_METHOD(std::optional<int32_t>, getBatteryCapacity, (int32_t deviceId, int32_t batteryId),
168                 (const, override));
169 
170     MOCK_METHOD(std::optional<int32_t>, getBatteryStatus, (int32_t deviceId, int32_t batteryId),
171                 (const, override));
172     MOCK_METHOD(void, requestReopenDevices, (), (override));
173     MOCK_METHOD(void, wake, (), (override));
174 
175     MOCK_METHOD(void, dump, (std::string & dump), (const, override));
176     MOCK_METHOD(void, monitor, (), (const, override));
177     MOCK_METHOD(bool, isDeviceEnabled, (int32_t deviceId), (const, override));
178     MOCK_METHOD(status_t, enableDevice, (int32_t deviceId), (override));
179     MOCK_METHOD(status_t, disableDevice, (int32_t deviceId), (override));
180     MOCK_METHOD(void, sysfsNodeChanged, (const std::string& sysfsNodePath), (override));
181 };
182 
183 class MockPointerChoreographerPolicyInterface : public PointerChoreographerPolicyInterface {
184 public:
185     MOCK_METHOD(std::shared_ptr<PointerControllerInterface>, createPointerController,
186                 (PointerControllerInterface::ControllerType), (override));
187     MOCK_METHOD(void, notifyPointerDisplayIdChanged,
188                 (ui::LogicalDisplayId displayId, const FloatPoint& position), (override));
189     MOCK_METHOD(bool, isInputMethodConnectionActive, (), (override));
190 };
191 
192 } // namespace android
193