blob: b3401c37e2bffbe2917552dec7f4210ed4c29a3f [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);
57 std::list<NotifyArgs> configureDevice(uint32_t changes);
58 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) {
63 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
64 configureDevice(0);
65 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
66 resetArgList += mapper.reset(ARBITRARY_TIME);
67 // Loop the reader to flush the input listener queue.
68 for (const NotifyArgs& loopArgs : resetArgList) {
69 mFakeListener->notify(loopArgs);
70 }
71 mReader->loopOnce();
72 return mapper;
73 }
74
75 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
76 ui::Rotation orientation, const std::string& uniqueId,
77 std::optional<uint8_t> physicalPort,
78 ViewportType viewportType);
79 void clearViewports();
80 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
81 int32_t code, int32_t value);
82 void resetMapper(InputMapper& mapper, nsecs_t when);
83
84 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when);
85
86 static void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source,
87 float min, float max, float flat, float fuzz);
88 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
89 float size, float touchMajor, float touchMinor, float toolMajor,
90 float toolMinor, float orientation, float distance,
91 float scaledAxisEpsilon = 1.f);
92 static void assertPosition(const FakePointerController& controller, float x, float y);
93};
94
95} // namespace android