blob: 88057dc497d469f11df75d1813914698a144833a [file] [log] [blame]
Harry Cuttse6512e12022-11-28 18:44:01 +00001/*
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>
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070026#include <gmock/gmock.h>
Harry Cuttse6512e12022-11-28 18:44:01 +000027#include <utils/StrongPointer.h>
28
29#include "FakeEventHub.h"
30#include "FakeInputReaderPolicy.h"
31#include "InstrumentedInputReader.h"
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070032#include "InterfaceMocks.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000033#include "TestConstants.h"
34#include "TestInputListener.h"
Harry Cutts7ecbb992023-12-18 14:45:09 +000035#include "input/PropertyMap.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000036
37namespace android {
38
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070039class InputMapperUnitTest : public testing::Test {
40protected:
41 static constexpr int32_t EVENTHUB_ID = 1;
42 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
Harry Cutts7ecbb992023-12-18 14:45:09 +000043 virtual void SetUp() override { SetUpWithBus(0); }
44 virtual void SetUpWithBus(int bus);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070045
Harry Cutts1b5bde42023-12-18 16:08:29 +000046 /**
47 * Initializes mDevice and mDeviceContext. When this happens, mDevice takes a copy of
48 * mPropertyMap, so tests that need to set configuration properties should do so before calling
49 * this. Others will most likely want to call it in their SetUp method.
50 */
51 void createDevice();
52
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070053 void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution);
54
55 void expectScanCodes(bool present, std::set<int> scanCodes);
56
57 void setScanCodeState(KeyState state, std::set<int> scanCodes);
58
59 void setKeyCodeState(KeyState state, std::set<int> keyCodes);
60
Harry Cuttsaa931df2024-07-15 14:54:36 +000061 void setSwitchState(int32_t state, std::set<int32_t> switchCodes);
62
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070063 std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value);
Arpit Singh82e413e2023-10-10 19:30:58 +000064 std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070065
Harry Cutts1b5bde42023-12-18 16:08:29 +000066 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070067 MockEventHubInterface mMockEventHub;
Byoungho Jungee6268f2023-10-30 17:27:26 +090068 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070069 MockInputReaderContext mMockInputReaderContext;
70 std::unique_ptr<InputDevice> mDevice;
71
72 std::unique_ptr<InputDeviceContext> mDeviceContext;
73 InputReaderConfiguration mReaderConfiguration;
74 // The mapper should be created by the subclasses.
75 std::unique_ptr<InputMapper> mMapper;
Harry Cutts7ecbb992023-12-18 14:45:09 +000076 PropertyMap mPropertyMap;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070077};
78
79/**
80 * Deprecated - use InputMapperUnitTest instead.
81 */
Harry Cuttse6512e12022-11-28 18:44:01 +000082class InputMapperTest : public testing::Test {
83protected:
84 static const char* DEVICE_NAME;
85 static const char* DEVICE_LOCATION;
86 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
87 static constexpr int32_t DEVICE_GENERATION = 2;
88 static constexpr int32_t DEVICE_CONTROLLER_NUMBER = 0;
89 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
90 static constexpr int32_t EVENTHUB_ID = 1;
91
92 std::shared_ptr<FakeEventHub> mFakeEventHub;
93 sp<FakeInputReaderPolicy> mFakePolicy;
94 std::unique_ptr<TestInputListener> mFakeListener;
95 std::unique_ptr<InstrumentedInputReader> mReader;
96 std::shared_ptr<InputDevice> mDevice;
97
98 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0);
99 void SetUp() override;
100 void TearDown() override;
101
102 void addConfigurationProperty(const char* key, const char* value);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000103 std::list<NotifyArgs> configureDevice(ConfigurationChanges changes);
Harry Cuttse6512e12022-11-28 18:44:01 +0000104 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
105 const std::string& location, int32_t eventHubId,
106 ftl::Flags<InputDeviceClass> classes, int bus = 0);
107 template <class T, typename... Args>
108 T& addMapperAndConfigure(Args... args) {
Arpit Singh8e6fb252023-04-06 11:49:17 +0000109 T& mapper =
110 mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000111 configureDevice(/*changes=*/{});
Harry Cuttse6512e12022-11-28 18:44:01 +0000112 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
113 resetArgList += mapper.reset(ARBITRARY_TIME);
114 // Loop the reader to flush the input listener queue.
115 for (const NotifyArgs& loopArgs : resetArgList) {
116 mFakeListener->notify(loopArgs);
117 }
118 mReader->loopOnce();
119 return mapper;
120 }
121
Arpit Singh56adebc2023-04-25 13:56:05 +0000122 template <class T, typename... Args>
123 T& constructAndAddMapper(Args... args) {
124 // ensure a device entry exists for this eventHubId
125 mDevice->addEmptyEventHubDevice(EVENTHUB_ID);
126 // configure the empty device
127 configureDevice(/*changes=*/{});
128
129 return mDevice->constructAndAddMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
130 args...);
131 }
132
Linnan Li13bf76a2024-05-05 19:18:02 +0800133 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Harry Cuttse6512e12022-11-28 18:44:01 +0000134 ui::Rotation orientation, const std::string& uniqueId,
135 std::optional<uint8_t> physicalPort,
136 ViewportType viewportType);
137 void clearViewports();
138 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
139 int32_t code, int32_t value);
140 void resetMapper(InputMapper& mapper, nsecs_t when);
141
142 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when);
Harry Cuttse6512e12022-11-28 18:44:01 +0000143};
144
Harry Cutts7ecbb992023-12-18 14:45:09 +0000145void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min,
146 float max, float flat, float fuzz);
147
148void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size,
149 float touchMajor, float touchMinor, float toolMajor, float toolMinor,
150 float orientation, float distance, float scaledAxisEpsilon = 1.f);
151
Harry Cuttse6512e12022-11-28 18:44:01 +0000152} // namespace android