xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/InputMapperTest.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <list>
20*38e8c45fSAndroid Build Coastguard Worker #include <memory>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <InputDevice.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <InputMapper.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <NotifyArgs.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <ftl/flags.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <gmock/gmock.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <utils/StrongPointer.h>
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #include "FakeEventHub.h"
30*38e8c45fSAndroid Build Coastguard Worker #include "FakeInputReaderPolicy.h"
31*38e8c45fSAndroid Build Coastguard Worker #include "InstrumentedInputReader.h"
32*38e8c45fSAndroid Build Coastguard Worker #include "InterfaceMocks.h"
33*38e8c45fSAndroid Build Coastguard Worker #include "TestConstants.h"
34*38e8c45fSAndroid Build Coastguard Worker #include "TestInputListener.h"
35*38e8c45fSAndroid Build Coastguard Worker #include "input/PropertyMap.h"
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker namespace android {
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker class InputMapperUnitTest : public testing::Test {
40*38e8c45fSAndroid Build Coastguard Worker protected:
41*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t EVENTHUB_ID = 1;
42*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
SetUp()43*38e8c45fSAndroid Build Coastguard Worker     virtual void SetUp() override { SetUpWithBus(0); }
44*38e8c45fSAndroid Build Coastguard Worker     virtual void SetUpWithBus(int bus);
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker     void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution,
47*38e8c45fSAndroid Build Coastguard Worker                    int32_t flat = 0, int32_t fuzz = 0);
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     void expectScanCodes(bool present, std::set<int> scanCodes);
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker     void setScanCodeState(KeyState state, std::set<int> scanCodes);
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker     void setKeyCodeState(KeyState state, std::set<int> keyCodes);
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker     void setSwitchState(int32_t state, std::set<int32_t> switchCodes);
56*38e8c45fSAndroid Build Coastguard Worker 
57*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value);
58*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value);
59*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> process(nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
60*38e8c45fSAndroid Build Coastguard Worker                                   int32_t value);
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker     InputDeviceIdentifier mIdentifier;
63*38e8c45fSAndroid Build Coastguard Worker     MockEventHubInterface mMockEventHub;
64*38e8c45fSAndroid Build Coastguard Worker     sp<FakeInputReaderPolicy> mFakePolicy;
65*38e8c45fSAndroid Build Coastguard Worker     MockInputReaderContext mMockInputReaderContext;
66*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<MockInputDevice> mDevice;
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<InputDeviceContext> mDeviceContext;
69*38e8c45fSAndroid Build Coastguard Worker     InputReaderConfiguration mReaderConfiguration;
70*38e8c45fSAndroid Build Coastguard Worker     // The mapper should be created by the subclasses.
71*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<InputMapper> mMapper;
72*38e8c45fSAndroid Build Coastguard Worker     PropertyMap mPropertyMap;
73*38e8c45fSAndroid Build Coastguard Worker };
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker /**
76*38e8c45fSAndroid Build Coastguard Worker  * Deprecated - use InputMapperUnitTest instead.
77*38e8c45fSAndroid Build Coastguard Worker  */
78*38e8c45fSAndroid Build Coastguard Worker class InputMapperTest : public testing::Test {
79*38e8c45fSAndroid Build Coastguard Worker protected:
80*38e8c45fSAndroid Build Coastguard Worker     static const char* DEVICE_NAME;
81*38e8c45fSAndroid Build Coastguard Worker     static const char* DEVICE_LOCATION;
82*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
83*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t DEVICE_GENERATION = 2;
84*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t DEVICE_CONTROLLER_NUMBER = 0;
85*38e8c45fSAndroid Build Coastguard Worker     static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
86*38e8c45fSAndroid Build Coastguard Worker     static constexpr int32_t EVENTHUB_ID = 1;
87*38e8c45fSAndroid Build Coastguard Worker 
88*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<FakeEventHub> mFakeEventHub;
89*38e8c45fSAndroid Build Coastguard Worker     sp<FakeInputReaderPolicy> mFakePolicy;
90*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<TestInputListener> mFakeListener;
91*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<InstrumentedInputReader> mReader;
92*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<InputDevice> mDevice;
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker     virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0);
95*38e8c45fSAndroid Build Coastguard Worker     void SetUp() override;
96*38e8c45fSAndroid Build Coastguard Worker     void TearDown() override;
97*38e8c45fSAndroid Build Coastguard Worker 
98*38e8c45fSAndroid Build Coastguard Worker     void addConfigurationProperty(const char* key, const char* value);
99*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> configureDevice(ConfigurationChanges changes);
100*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
101*38e8c45fSAndroid Build Coastguard Worker                                            const std::string& location, int32_t eventHubId,
102*38e8c45fSAndroid Build Coastguard Worker                                            ftl::Flags<InputDeviceClass> classes, int bus = 0);
103*38e8c45fSAndroid Build Coastguard Worker     template <class T, typename... Args>
addMapperAndConfigure(Args...args)104*38e8c45fSAndroid Build Coastguard Worker     T& addMapperAndConfigure(Args... args) {
105*38e8c45fSAndroid Build Coastguard Worker         T& mapper =
106*38e8c45fSAndroid Build Coastguard Worker                 mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...);
107*38e8c45fSAndroid Build Coastguard Worker         configureDevice(/*changes=*/{});
108*38e8c45fSAndroid Build Coastguard Worker         std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
109*38e8c45fSAndroid Build Coastguard Worker         resetArgList += mapper.reset(ARBITRARY_TIME);
110*38e8c45fSAndroid Build Coastguard Worker         // Loop the reader to flush the input listener queue.
111*38e8c45fSAndroid Build Coastguard Worker         for (const NotifyArgs& loopArgs : resetArgList) {
112*38e8c45fSAndroid Build Coastguard Worker             mFakeListener->notify(loopArgs);
113*38e8c45fSAndroid Build Coastguard Worker         }
114*38e8c45fSAndroid Build Coastguard Worker         mReader->loopOnce();
115*38e8c45fSAndroid Build Coastguard Worker         return mapper;
116*38e8c45fSAndroid Build Coastguard Worker     }
117*38e8c45fSAndroid Build Coastguard Worker 
118*38e8c45fSAndroid Build Coastguard Worker     template <class T, typename... Args>
constructAndAddMapper(Args...args)119*38e8c45fSAndroid Build Coastguard Worker     T& constructAndAddMapper(Args... args) {
120*38e8c45fSAndroid Build Coastguard Worker         // ensure a device entry exists for this eventHubId
121*38e8c45fSAndroid Build Coastguard Worker         mDevice->addEmptyEventHubDevice(EVENTHUB_ID);
122*38e8c45fSAndroid Build Coastguard Worker 
123*38e8c45fSAndroid Build Coastguard Worker         auto& mapper =
124*38e8c45fSAndroid Build Coastguard Worker                 mDevice->constructAndAddMapper<T>(EVENTHUB_ID,
125*38e8c45fSAndroid Build Coastguard Worker                                                   mFakePolicy->getReaderConfiguration(), args...);
126*38e8c45fSAndroid Build Coastguard Worker         configureDevice(/*changes=*/{});
127*38e8c45fSAndroid Build Coastguard Worker         return mapper;
128*38e8c45fSAndroid Build Coastguard Worker     }
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker     void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
131*38e8c45fSAndroid Build Coastguard Worker                                       ui::Rotation orientation, const std::string& uniqueId,
132*38e8c45fSAndroid Build Coastguard Worker                                       std::optional<uint8_t> physicalPort,
133*38e8c45fSAndroid Build Coastguard Worker                                       ViewportType viewportType);
134*38e8c45fSAndroid Build Coastguard Worker     void clearViewports();
135*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
136*38e8c45fSAndroid Build Coastguard Worker                                   int32_t code, int32_t value);
137*38e8c45fSAndroid Build Coastguard Worker     void resetMapper(InputMapper& mapper, nsecs_t when);
138*38e8c45fSAndroid Build Coastguard Worker 
139*38e8c45fSAndroid Build Coastguard Worker     std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when);
140*38e8c45fSAndroid Build Coastguard Worker };
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min,
143*38e8c45fSAndroid Build Coastguard Worker                        float max, float flat, float fuzz);
144*38e8c45fSAndroid Build Coastguard Worker 
145*38e8c45fSAndroid Build Coastguard Worker void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size,
146*38e8c45fSAndroid Build Coastguard Worker                          float touchMajor, float touchMinor, float toolMajor, float toolMinor,
147*38e8c45fSAndroid Build Coastguard Worker                          float orientation, float distance, float scaledAxisEpsilon = 1.f);
148*38e8c45fSAndroid Build Coastguard Worker 
149*38e8c45fSAndroid Build Coastguard Worker } // namespace android
150