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