blob: ade53281a6f78fc78307313d9705dca27d566306 [file] [log] [blame]
Michael Ensingb8d93262020-05-12 00:41:30 -07001/*
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 Ensingb8d93262020-05-12 00:41:30 -070023
24namespace android {
25
26class FuzzContainer {
27 std::shared_ptr<FuzzEventHub> mFuzzEventHub;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070028 FuzzInputListener mFuzzListener;
Michael Ensingb8d93262020-05-12 00:41:30 -070029 std::unique_ptr<FuzzInputReaderContext> mFuzzContext;
30 std::unique_ptr<InputDevice> mFuzzDevice;
Aditya Wazireea5be02022-10-31 12:40:10 +053031 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070032
33public:
Aditya Wazireea5be02022-10-31 12:40:10 +053034 FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(fdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -070035 // 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 Cutts9d2b5272023-06-23 13:09:28 +000039 int32_t deviceGeneration = mFdp->ConsumeIntegralInRange<int32_t>(/*from=*/0, /*to=*/5);
Michael Ensingb8d93262020-05-12 00:41:30 -070040
41 // Create mocked objects.
42 mFuzzEventHub = std::make_shared<FuzzEventHub>(mFdp);
Harry Cuttse36d8352023-07-10 13:28:45 +000043 sp<FuzzInputReaderPolicy> policy = sp<FuzzInputReaderPolicy>::make(mFdp);
44 mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, policy,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070045 mFuzzListener, mFdp);
Michael Ensingb8d93262020-05-12 00:41:30 -070046
47 InputDeviceIdentifier identifier;
48 identifier.name = deviceName;
49 identifier.location = deviceLocation;
50 mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration,
51 identifier);
Michael Ensingb8d93262020-05-12 00:41:30 -070052 }
53
54 ~FuzzContainer() {}
55
56 void configureDevice() {
57 nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070058 std::list<NotifyArgs> out;
Harry Cuttse36d8352023-07-10 13:28:45 +000059 out += mFuzzDevice->configure(arbitraryTime, /*readerConfig=*/{}, /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070060 out += mFuzzDevice->reset(arbitraryTime);
61 for (const NotifyArgs& args : out) {
62 mFuzzListener.notify(args);
63 }
Michael Ensingb8d93262020-05-12 00:41:30 -070064 }
65
66 void addProperty(std::string key, std::string value) {
67 mFuzzEventHub->addProperty(key, value);
68 configureDevice();
69 }
70
Harry Cuttsccb75e82023-06-23 14:08:06 +000071 void setAbsoluteAxisInfo(int axis, const RawAbsoluteAxisInfo& axisInfo) {
72 mFuzzEventHub->setAbsoluteAxisInfo(mFuzzDevice->getId(), axis, axisInfo);
73 }
74
Michael Ensingb8d93262020-05-12 00:41:30 -070075 template <class T, typename... Args>
76 T& getMapper(Args... args) {
Arpit Singha8c236b2023-04-25 13:56:05 +000077 int32_t eventhubId = mFdp->ConsumeIntegral<int32_t>();
78 // ensure a device entry exists for this eventHubId
79 mFuzzDevice->addEmptyEventHubDevice(eventhubId);
Michael Ensingb8d93262020-05-12 00:41:30 -070080 configureDevice();
Arpit Singha8c236b2023-04-25 13:56:05 +000081
82 return mFuzzDevice->template constructAndAddMapper<T>(eventhubId, args...);
Michael Ensingb8d93262020-05-12 00:41:30 -070083 }
84};
85
86} // namespace android