Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 26 | #include <gmock/gmock.h> |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 27 | #include <utils/StrongPointer.h> |
| 28 | |
| 29 | #include "FakeEventHub.h" |
| 30 | #include "FakeInputReaderPolicy.h" |
| 31 | #include "InstrumentedInputReader.h" |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 32 | #include "InterfaceMocks.h" |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 33 | #include "TestConstants.h" |
| 34 | #include "TestInputListener.h" |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 35 | #include "input/PropertyMap.h" |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 39 | class InputMapperUnitTest : public testing::Test { |
| 40 | protected: |
| 41 | static constexpr int32_t EVENTHUB_ID = 1; |
| 42 | static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000; |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 43 | virtual void SetUp() override { SetUpWithBus(0); } |
| 44 | virtual void SetUpWithBus(int bus); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 45 | |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 46 | /** |
| 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 Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 53 | 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 | |
| 61 | std::list<NotifyArgs> process(int32_t type, int32_t code, int32_t value); |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 62 | std::list<NotifyArgs> process(nsecs_t when, int32_t type, int32_t code, int32_t value); |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 63 | |
Harry Cutts | 1b5bde4 | 2023-12-18 16:08:29 +0000 | [diff] [blame] | 64 | InputDeviceIdentifier mIdentifier; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 65 | MockEventHubInterface mMockEventHub; |
Byoungho Jung | ee6268f | 2023-10-30 17:27:26 +0900 | [diff] [blame] | 66 | sp<FakeInputReaderPolicy> mFakePolicy; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 67 | MockInputReaderContext mMockInputReaderContext; |
| 68 | std::unique_ptr<InputDevice> mDevice; |
| 69 | |
| 70 | std::unique_ptr<InputDeviceContext> mDeviceContext; |
| 71 | InputReaderConfiguration mReaderConfiguration; |
| 72 | // The mapper should be created by the subclasses. |
| 73 | std::unique_ptr<InputMapper> mMapper; |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 74 | PropertyMap mPropertyMap; |
Siarhei Vishniakou | 979f2d8 | 2023-05-16 14:26:24 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Deprecated - use InputMapperUnitTest instead. |
| 79 | */ |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 80 | class InputMapperTest : public testing::Test { |
| 81 | protected: |
| 82 | static const char* DEVICE_NAME; |
| 83 | static const char* DEVICE_LOCATION; |
| 84 | static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000; |
| 85 | static constexpr int32_t DEVICE_GENERATION = 2; |
| 86 | static constexpr int32_t DEVICE_CONTROLLER_NUMBER = 0; |
| 87 | static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES; |
| 88 | static constexpr int32_t EVENTHUB_ID = 1; |
| 89 | |
| 90 | std::shared_ptr<FakeEventHub> mFakeEventHub; |
| 91 | sp<FakeInputReaderPolicy> mFakePolicy; |
| 92 | std::unique_ptr<TestInputListener> mFakeListener; |
| 93 | std::unique_ptr<InstrumentedInputReader> mReader; |
| 94 | std::shared_ptr<InputDevice> mDevice; |
| 95 | |
| 96 | virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0); |
| 97 | void SetUp() override; |
| 98 | void TearDown() override; |
| 99 | |
| 100 | void addConfigurationProperty(const char* key, const char* value); |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 101 | std::list<NotifyArgs> configureDevice(ConfigurationChanges changes); |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 102 | std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name, |
| 103 | const std::string& location, int32_t eventHubId, |
| 104 | ftl::Flags<InputDeviceClass> classes, int bus = 0); |
| 105 | template <class T, typename... Args> |
| 106 | T& addMapperAndConfigure(Args... args) { |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 107 | T& mapper = |
| 108 | mDevice->addMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), args...); |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 109 | configureDevice(/*changes=*/{}); |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 110 | std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME); |
| 111 | resetArgList += mapper.reset(ARBITRARY_TIME); |
| 112 | // Loop the reader to flush the input listener queue. |
| 113 | for (const NotifyArgs& loopArgs : resetArgList) { |
| 114 | mFakeListener->notify(loopArgs); |
| 115 | } |
| 116 | mReader->loopOnce(); |
| 117 | return mapper; |
| 118 | } |
| 119 | |
Arpit Singh | 56adebc | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 120 | template <class T, typename... Args> |
| 121 | T& constructAndAddMapper(Args... args) { |
| 122 | // ensure a device entry exists for this eventHubId |
| 123 | mDevice->addEmptyEventHubDevice(EVENTHUB_ID); |
| 124 | // configure the empty device |
| 125 | configureDevice(/*changes=*/{}); |
| 126 | |
| 127 | return mDevice->constructAndAddMapper<T>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(), |
| 128 | args...); |
| 129 | } |
| 130 | |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 131 | void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height, |
| 132 | ui::Rotation orientation, const std::string& uniqueId, |
| 133 | std::optional<uint8_t> physicalPort, |
| 134 | ViewportType viewportType); |
| 135 | void clearViewports(); |
| 136 | std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, |
| 137 | int32_t code, int32_t value); |
| 138 | void resetMapper(InputMapper& mapper, nsecs_t when); |
| 139 | |
| 140 | std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when); |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Harry Cutts | 7ecbb99 | 2023-12-18 14:45:09 +0000 | [diff] [blame] | 143 | void assertMotionRange(const InputDeviceInfo& info, int32_t axis, uint32_t source, float min, |
| 144 | float max, float flat, float fuzz); |
| 145 | |
| 146 | void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure, float size, |
| 147 | float touchMajor, float touchMinor, float toolMajor, float toolMinor, |
| 148 | float orientation, float distance, float scaledAxisEpsilon = 1.f); |
| 149 | |
Harry Cutts | e6512e1 | 2022-11-28 18:44:01 +0000 | [diff] [blame] | 150 | } // namespace android |