blob: 10ef6f1660f28b2f607c587727d8002070945dde [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 Cutts68ce2f62024-11-07 16:49:46 +000046 void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution,
47 int32_t flat = 0, int32_t fuzz = 0);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070048
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
Harry Cuttsaa931df2024-07-15 14:54:36 +000055 void setSwitchState(int32_t state, std::set<int32_t> switchCodes);
56
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070057 std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value);
Arpit Singh82e413e2023-10-10 19:30:58 +000058 std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value);
Harry Cuttsd62330d2024-11-11 13:43:13 +000059 std::list<NotifyArgs> process(nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
60 int32_t value);
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070061
Harry Cutts1b5bde42023-12-18 16:08:29 +000062 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070063 MockEventHubInterface mMockEventHub;
Byoungho Jungee6268f2023-10-30 17:27:26 +090064 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070065 MockInputReaderContext mMockInputReaderContext;
Prabir Pradhan31d05c42024-07-24 21:19:08 +000066 std::unique_ptr<MockInputDevice> mDevice;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070067
68 std::unique_ptr<InputDeviceContext> mDeviceContext;
69 InputReaderConfiguration mReaderConfiguration;
70 // The mapper should be created by the subclasses.
71 std::unique_ptr<InputMapper> mMapper;
Harry Cutts7ecbb992023-12-18 14:45:09 +000072 PropertyMap mPropertyMap;
Siarhei Vishniakou979f2d82023-05-16 14:26:24 -070073};
74
75/**
76 * Deprecated - use InputMapperUnitTest instead.
77 */
Harry Cuttse6512e12022-11-28 18:44:01 +000078class InputMapperTest : public testing::Test {
79protected:
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);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000099 std::list<NotifyArgs> configureDevice(ConfigurationChanges changes);
Harry Cuttse6512e12022-11-28 18:44:01 +0000100 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>
104 T& addMapperAndConfigure(Args... args) {
Arpit Singh8e6fb252023-04-06 11:49:17 +0000105 T& mapper =
106 mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000107 configureDevice(/*changes=*/{});
Harry Cuttse6512e12022-11-28 18:44:01 +0000108 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
Arpit Singh56adebc2023-04-25 13:56:05 +0000118 template <class T, typename... Args>
119 T& constructAndAddMapper(Args... args) {
120 // ensure a device entry exists for this eventHubId
121 mDevice->addEmptyEventHubDevice(EVENTHUB_ID);
Arpit Singh56adebc2023-04-25 13:56:05 +0000122
Prabir Pradhan38636652024-07-23 21:59:36 +0000123 auto& mapper =
124 mDevice->constructAndAddMapper<T>(EVENTHUB_ID,
125 mFakePolicy->getReaderConfiguration(), args...);
126 configureDevice(/*changes=*/{});
127 return mapper;
Arpit Singh56adebc2023-04-25 13:56:05 +0000128 }
129
Linnan Li13bf76a2024-05-05 19:18:02 +0800130 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Harry Cuttse6512e12022-11-28 18:44:01 +0000131 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);
Harry Cuttse6512e12022-11-28 18:44:01 +0000140};
141
Harry Cutts7ecbb992023-12-18 14:45:09 +0000142void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min,
143 float max, float flat, float fuzz);
144
145void 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
Harry Cuttse6512e12022-11-28 18:44:01 +0000149} // namespace android