xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/InputMapperTest.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 <list>
20 #include <memory>
21 
22 #include <InputDevice.h>
23 #include <InputMapper.h>
24 #include <NotifyArgs.h>
25 #include <ftl/flags.h>
26 #include <gmock/gmock.h>
27 #include <utils/StrongPointer.h>
28 
29 #include "FakeEventHub.h"
30 #include "FakeInputReaderPolicy.h"
31 #include "InstrumentedInputReader.h"
32 #include "InterfaceMocks.h"
33 #include "TestConstants.h"
34 #include "TestInputListener.h"
35 #include "input/PropertyMap.h"
36 
37 namespace android {
38 
39 class InputMapperUnitTest : public testing::Test {
40 protected:
41     static constexpr int32_t EVENTHUB_ID = 1;
42     static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
SetUp()43     virtual void SetUp() override { SetUpWithBus(0); }
44     virtual void SetUpWithBus(int bus);
45 
46     void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution,
47                    int32_t flat = 0, int32_t fuzz = 0);
48 
49     void expectScanCodes(bool present, std::set<int> scanCodes);
50 
51     void setScanCodeState(KeyState state, std::set<int> scanCodes);
52 
53     void setKeyCodeState(KeyState state, std::set<int> keyCodes);
54 
55     void setSwitchState(int32_t state, std::set<int32_t> switchCodes);
56 
57     std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value);
58     std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value);
59     std::list<NotifyArgs> process(nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
60                                   int32_t value);
61 
62     InputDeviceIdentifier mIdentifier;
63     MockEventHubInterface mMockEventHub;
64     sp<FakeInputReaderPolicy> mFakePolicy;
65     MockInputReaderContext mMockInputReaderContext;
66     std::unique_ptr<MockInputDevice> mDevice;
67 
68     std::unique_ptr<InputDeviceContext> mDeviceContext;
69     InputReaderConfiguration mReaderConfiguration;
70     // The mapper should be created by the subclasses.
71     std::unique_ptr<InputMapper> mMapper;
72     PropertyMap mPropertyMap;
73 };
74 
75 /**
76  * Deprecated - use InputMapperUnitTest instead.
77  */
78 class InputMapperTest : public testing::Test {
79 protected:
80     static const char* DEVICE_NAME;
81     static const char* DEVICE_LOCATION;
82     static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
83     static constexpr int32_t DEVICE_GENERATION = 2;
84     static constexpr int32_t DEVICE_CONTROLLER_NUMBER = 0;
85     static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
86     static constexpr int32_t EVENTHUB_ID = 1;
87 
88     std::shared_ptr<FakeEventHub> mFakeEventHub;
89     sp<FakeInputReaderPolicy> mFakePolicy;
90     std::unique_ptr<TestInputListener> mFakeListener;
91     std::unique_ptr<InstrumentedInputReader> mReader;
92     std::shared_ptr<InputDevice> mDevice;
93 
94     virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0);
95     void SetUp() override;
96     void TearDown() override;
97 
98     void addConfigurationProperty(const char* key, const char* value);
99     std::list<NotifyArgs> configureDevice(ConfigurationChanges changes);
100     std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
101                                            const std::string& location, int32_t eventHubId,
102                                            ftl::Flags<InputDeviceClass> classes, int bus = 0);
103     template <class T, typename... Args>
addMapperAndConfigure(Args...args)104     T& addMapperAndConfigure(Args... args) {
105         T& mapper =
106                 mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...);
107         configureDevice(/*changes=*/{});
108         std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
109         resetArgList += mapper.reset(ARBITRARY_TIME);
110         // Loop the reader to flush the input listener queue.
111         for (const NotifyArgs& loopArgs : resetArgList) {
112             mFakeListener->notify(loopArgs);
113         }
114         mReader->loopOnce();
115         return mapper;
116     }
117 
118     template <class T, typename... Args>
constructAndAddMapper(Args...args)119     T& constructAndAddMapper(Args... args) {
120         // ensure a device entry exists for this eventHubId
121         mDevice->addEmptyEventHubDevice(EVENTHUB_ID);
122 
123         auto& mapper =
124                 mDevice->constructAndAddMapper<T>(EVENTHUB_ID,
125                                                   mFakePolicy->getReaderConfiguration(), args...);
126         configureDevice(/*changes=*/{});
127         return mapper;
128     }
129 
130     void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
131                                       ui::Rotation orientation, const std::string& uniqueId,
132                                       std::optional<uint8_t> physicalPort,
133                                       ViewportType viewportType);
134     void clearViewports();
135     std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
136                                   int32_t code, int32_t value);
137     void resetMapper(InputMapper& mapper, nsecs_t when);
138 
139     std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when);
140 };
141 
142 void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min,
143                        float max, float flat, float fuzz);
144 
145 void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size,
146                          float touchMajor, float touchMinor, float toolMajor, float toolMinor,
147                          float orientation, float distance, float scaledAxisEpsilon = 1.f);
148 
149 } // namespace android
150