blob: 2b6655c45e4d99b57b67280b28de6e4ac1aa1eb7 [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>
26#include <utils/StrongPointer.h>
27
28#include "FakeEventHub.h"
29#include "FakeInputReaderPolicy.h"
30#include "InstrumentedInputReader.h"
31#include "TestConstants.h"
32#include "TestInputListener.h"
33
34namespace android {
35
36class InputMapperTest : public testing::Test {
37protected:
38 static const char* DEVICE_NAME;
39 static const char* DEVICE_LOCATION;
40 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
41 static constexpr int32_t DEVICE_GENERATION = 2;
42 static constexpr int32_t DEVICE_CONTROLLER_NUMBER = 0;
43 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
44 static constexpr int32_t EVENTHUB_ID = 1;
45
46 std::shared_ptr<FakeEventHub> mFakeEventHub;
47 sp<FakeInputReaderPolicy> mFakePolicy;
48 std::unique_ptr<TestInputListener> mFakeListener;
49 std::unique_ptr<InstrumentedInputReader> mReader;
50 std::shared_ptr<InputDevice> mDevice;
51
52 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0);
53 void SetUp() override;
54 void TearDown() override;
55
56 void addConfigurationProperty(const char* key, const char* value);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000057 std::list<NotifyArgs> configureDevice(ConfigurationChanges changes);
Harry Cuttse6512e12022-11-28 18:44:01 +000058 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
59 const std::string& location, int32_t eventHubId,
60 ftl::Flags<InputDeviceClass> classes, int bus = 0);
61 template <class T, typename... Args>
62 T& addMapperAndConfigure(Args... args) {
Arpit Singh8e6fb252023-04-06 11:49:17 +000063 T& mapper =
64 mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000065 configureDevice(/*changes=*/{});
Harry Cuttse6512e12022-11-28 18:44:01 +000066 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
67 resetArgList += mapper.reset(ARBITRARY_TIME);
68 // Loop the reader to flush the input listener queue.
69 for (const NotifyArgs& loopArgs : resetArgList) {
70 mFakeListener->notify(loopArgs);
71 }
72 mReader->loopOnce();
73 return mapper;
74 }
75
Arpit Singh56adebc2023-04-25 13:56:05 +000076 template <class T, typename... Args>
77 T& constructAndAddMapper(Args... args) {
78 // ensure a device entry exists for this eventHubId
79 mDevice->addEmptyEventHubDevice(EVENTHUB_ID);
80 // configure the empty device
81 configureDevice(/*changes=*/{});
82
83 return mDevice->constructAndAddMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
84 args...);
85 }
86
Harry Cuttse6512e12022-11-28 18:44:01 +000087 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
88 ui::Rotation orientation, const std::string& uniqueId,
89 std::optional<uint8_t> physicalPort,
90 ViewportType viewportType);
91 void clearViewports();
92 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
93 int32_t code, int32_t value);
94 void resetMapper(InputMapper& mapper, nsecs_t when);
95
96 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when);
97
98 static void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source,
99 float min, float max, float flat, float fuzz);
100 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
101 float size, float touchMajor, float touchMinor, float toolMajor,
102 float toolMinor, float orientation, float distance,
103 float scaledAxisEpsilon = 1.f);
Harry Cuttse6512e12022-11-28 18:44:01 +0000104};
105
106} // namespace android