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