Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [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 <InputDevice.h> |
| 20 | #include <InputMapper.h> |
| 21 | #include <InputReader.h> |
| 22 | #include <MapperHelpers.h> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | class FuzzContainer { |
| 27 | std::shared_ptr<FuzzEventHub> mFuzzEventHub; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 28 | FuzzInputListener mFuzzListener; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 29 | std::unique_ptr<FuzzInputReaderContext> mFuzzContext; |
| 30 | std::unique_ptr<InputDevice> mFuzzDevice; |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 31 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 32 | |
| 33 | public: |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 34 | FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(fdp) { |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 35 | // Setup parameters. |
| 36 | std::string deviceName = mFdp->ConsumeRandomLengthString(16); |
| 37 | std::string deviceLocation = mFdp->ConsumeRandomLengthString(12); |
| 38 | int32_t deviceID = mFdp->ConsumeIntegralInRange<int32_t>(0, 5); |
Harry Cutts | 9d2b527 | 2023-06-23 13:09:28 +0000 | [diff] [blame] | 39 | int32_t deviceGeneration = mFdp->ConsumeIntegralInRange<int32_t>(/*from=*/0, /*to=*/5); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 40 | |
| 41 | // Create mocked objects. |
| 42 | mFuzzEventHub = std::make_shared<FuzzEventHub>(mFdp); |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 43 | sp<FuzzInputReaderPolicy> policy = sp<FuzzInputReaderPolicy>::make(mFdp); |
| 44 | mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, policy, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 45 | mFuzzListener, mFdp); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 46 | |
| 47 | InputDeviceIdentifier identifier; |
| 48 | identifier.name = deviceName; |
| 49 | identifier.location = deviceLocation; |
| 50 | mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration, |
| 51 | identifier); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ~FuzzContainer() {} |
| 55 | |
| 56 | void configureDevice() { |
| 57 | nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 58 | std::list<NotifyArgs> out; |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 59 | out += mFuzzDevice->configure(arbitraryTime, /*readerConfig=*/{}, /*changes=*/{}); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 60 | out += mFuzzDevice->reset(arbitraryTime); |
| 61 | for (const NotifyArgs& args : out) { |
| 62 | mFuzzListener.notify(args); |
| 63 | } |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void addProperty(std::string key, std::string value) { |
| 67 | mFuzzEventHub->addProperty(key, value); |
| 68 | configureDevice(); |
| 69 | } |
| 70 | |
Harry Cutts | ccb75e8 | 2023-06-23 14:08:06 +0000 | [diff] [blame] | 71 | void setAbsoluteAxisInfo(int axis, const RawAbsoluteAxisInfo& axisInfo) { |
| 72 | mFuzzEventHub->setAbsoluteAxisInfo(mFuzzDevice->getId(), axis, axisInfo); |
| 73 | } |
| 74 | |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 75 | template <class T, typename... Args> |
| 76 | T& getMapper(Args... args) { |
Arpit Singh | a8c236b | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 77 | int32_t eventhubId = mFdp->ConsumeIntegral<int32_t>(); |
| 78 | // ensure a device entry exists for this eventHubId |
| 79 | mFuzzDevice->addEmptyEventHubDevice(eventhubId); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 80 | configureDevice(); |
Arpit Singh | a8c236b | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 81 | |
| 82 | return mFuzzDevice->template constructAndAddMapper<T>(eventhubId, args...); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // namespace android |