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