1 /*
2 * Copyright (C) 2024 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 #ifndef VIBRATORSERVICE_UNITTEST_MOCKS_H_
18 #define VIBRATORSERVICE_UNITTEST_MOCKS_H_
19
20 #include <gmock/gmock.h>
21
22 #include <aidl/android/hardware/vibrator/IVibrator.h>
23
24 #include <vibratorservice/VibratorCallbackScheduler.h>
25 #include <vibratorservice/VibratorHalController.h>
26 #include <vibratorservice/VibratorHalWrapper.h>
27
28 namespace android {
29
30 namespace vibrator {
31
32 using std::chrono::milliseconds;
33
34 using namespace testing;
35
36 using aidl::android::hardware::vibrator::Braking;
37 using aidl::android::hardware::vibrator::CompositeEffect;
38 using aidl::android::hardware::vibrator::CompositePrimitive;
39 using aidl::android::hardware::vibrator::CompositePwleV2;
40 using aidl::android::hardware::vibrator::Effect;
41 using aidl::android::hardware::vibrator::EffectStrength;
42 using aidl::android::hardware::vibrator::FrequencyAccelerationMapEntry;
43 using aidl::android::hardware::vibrator::IVibrator;
44 using aidl::android::hardware::vibrator::IVibratorCallback;
45 using aidl::android::hardware::vibrator::PrimitivePwle;
46 using aidl::android::hardware::vibrator::VendorEffect;
47
48 // -------------------------------------------------------------------------------------------------
49
50 class MockIVibrator : public IVibrator {
51 public:
52 MockIVibrator() = default;
53
54 MOCK_METHOD(ndk::ScopedAStatus, getCapabilities, (int32_t * ret), (override));
55 MOCK_METHOD(ndk::ScopedAStatus, off, (), (override));
56 MOCK_METHOD(ndk::ScopedAStatus, on,
57 (int32_t timeout, const std::shared_ptr<IVibratorCallback>& cb), (override));
58 MOCK_METHOD(ndk::ScopedAStatus, perform,
59 (Effect e, EffectStrength s, const std::shared_ptr<IVibratorCallback>& cb,
60 int32_t* ret),
61 (override));
62 MOCK_METHOD(ndk::ScopedAStatus, performVendorEffect,
63 (const VendorEffect& e, const std::shared_ptr<IVibratorCallback>& cb), (override));
64 MOCK_METHOD(ndk::ScopedAStatus, getSupportedEffects, (std::vector<Effect> * ret), (override));
65 MOCK_METHOD(ndk::ScopedAStatus, setAmplitude, (float amplitude), (override));
66 MOCK_METHOD(ndk::ScopedAStatus, setExternalControl, (bool enabled), (override));
67 MOCK_METHOD(ndk::ScopedAStatus, getCompositionDelayMax, (int32_t * ret), (override));
68 MOCK_METHOD(ndk::ScopedAStatus, getCompositionSizeMax, (int32_t * ret), (override));
69 MOCK_METHOD(ndk::ScopedAStatus, getSupportedPrimitives, (std::vector<CompositePrimitive> * ret),
70 (override));
71 MOCK_METHOD(ndk::ScopedAStatus, getPrimitiveDuration, (CompositePrimitive p, int32_t* ret),
72 (override));
73 MOCK_METHOD(ndk::ScopedAStatus, compose,
74 (const std::vector<CompositeEffect>& e,
75 const std::shared_ptr<IVibratorCallback>& cb),
76 (override));
77 MOCK_METHOD(ndk::ScopedAStatus, composePwle,
78 (const std::vector<PrimitivePwle>& e, const std::shared_ptr<IVibratorCallback>& cb),
79 (override));
80 MOCK_METHOD(ndk::ScopedAStatus, getSupportedAlwaysOnEffects, (std::vector<Effect> * ret),
81 (override));
82 MOCK_METHOD(ndk::ScopedAStatus, alwaysOnEnable, (int32_t id, Effect e, EffectStrength s),
83 (override));
84 MOCK_METHOD(ndk::ScopedAStatus, alwaysOnDisable, (int32_t id), (override));
85 MOCK_METHOD(ndk::ScopedAStatus, getQFactor, (float* ret), (override));
86 MOCK_METHOD(ndk::ScopedAStatus, getResonantFrequency, (float* ret), (override));
87 MOCK_METHOD(ndk::ScopedAStatus, getFrequencyResolution, (float* ret), (override));
88 MOCK_METHOD(ndk::ScopedAStatus, getFrequencyMinimum, (float* ret), (override));
89 MOCK_METHOD(ndk::ScopedAStatus, getBandwidthAmplitudeMap, (std::vector<float> * ret),
90 (override));
91 MOCK_METHOD(ndk::ScopedAStatus, getPwlePrimitiveDurationMax, (int32_t * ret), (override));
92 MOCK_METHOD(ndk::ScopedAStatus, getPwleCompositionSizeMax, (int32_t * ret), (override));
93 MOCK_METHOD(ndk::ScopedAStatus, getSupportedBraking, (std::vector<Braking> * ret), (override));
94 MOCK_METHOD(ndk::ScopedAStatus, getFrequencyToOutputAccelerationMap,
95 (std::vector<FrequencyAccelerationMapEntry> * ret), (override));
96 MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMaxMillis, (int32_t* ret),
97 (override));
98 MOCK_METHOD(ndk::ScopedAStatus, getPwleV2PrimitiveDurationMinMillis, (int32_t* ret),
99 (override));
100 MOCK_METHOD(ndk::ScopedAStatus, getPwleV2CompositionSizeMax, (int32_t* ret), (override));
101 MOCK_METHOD(ndk::ScopedAStatus, composePwleV2,
102 (const CompositePwleV2& e, const std::shared_ptr<IVibratorCallback>& cb),
103 (override));
104 MOCK_METHOD(ndk::ScopedAStatus, getInterfaceVersion, (int32_t*), (override));
105 MOCK_METHOD(ndk::ScopedAStatus, getInterfaceHash, (std::string*), (override));
106 MOCK_METHOD(ndk::SpAIBinder, asBinder, (), (override));
107 MOCK_METHOD(bool, isRemote, (), (override));
108 };
109
110 // gmock requirement to provide a WithArg<0>(TriggerCallback()) matcher
111 typedef void TriggerCallbackFunction(const std::shared_ptr<IVibratorCallback>&);
112
113 class TriggerCallbackAction : public ActionInterface<TriggerCallbackFunction> {
114 public:
TriggerCallbackAction()115 explicit TriggerCallbackAction() {}
116
Perform(const ArgumentTuple & args)117 virtual Result Perform(const ArgumentTuple& args) {
118 const std::shared_ptr<IVibratorCallback>& callback = get<0>(args);
119 if (callback) {
120 callback->onComplete();
121 }
122 }
123 };
124
TriggerCallback()125 inline Action<TriggerCallbackFunction> TriggerCallback() {
126 return MakeAction(new TriggerCallbackAction());
127 }
128
129 // -------------------------------------------------------------------------------------------------
130
131 class MockCallbackScheduler : public CallbackScheduler {
132 public:
133 MOCK_METHOD(void, schedule, (std::function<void()> callback, std::chrono::milliseconds delay),
134 (override));
135 };
136
ACTION(TriggerSchedulerCallback)137 ACTION(TriggerSchedulerCallback) {
138 arg0();
139 }
140
141 // -------------------------------------------------------------------------------------------------
142
143 class MockHalWrapper : public HalWrapper {
144 public:
MockHalWrapper(std::shared_ptr<CallbackScheduler> scheduler)145 MockHalWrapper(std::shared_ptr<CallbackScheduler> scheduler) : HalWrapper(scheduler) {}
146 virtual ~MockHalWrapper() = default;
147
148 MOCK_METHOD(vibrator::HalResult<void>, ping, (), (override));
149 MOCK_METHOD(void, tryReconnect, (), (override));
150 MOCK_METHOD(vibrator::HalResult<void>, on,
151 (milliseconds timeout, const std::function<void()>& completionCallback),
152 (override));
153 MOCK_METHOD(vibrator::HalResult<void>, off, (), (override));
154 MOCK_METHOD(vibrator::HalResult<void>, setAmplitude, (float amplitude), (override));
155 MOCK_METHOD(vibrator::HalResult<void>, setExternalControl, (bool enabled), (override));
156 MOCK_METHOD(vibrator::HalResult<void>, alwaysOnEnable,
157 (int32_t id, Effect effect, EffectStrength strength), (override));
158 MOCK_METHOD(vibrator::HalResult<void>, alwaysOnDisable, (int32_t id), (override));
159 MOCK_METHOD(vibrator::HalResult<milliseconds>, performEffect,
160 (Effect effect, EffectStrength strength,
161 const std::function<void()>& completionCallback),
162 (override));
163 MOCK_METHOD(vibrator::HalResult<vibrator::Capabilities>, getCapabilitiesInternal, (),
164 (override));
165
getCallbackScheduler()166 CallbackScheduler* getCallbackScheduler() { return mCallbackScheduler.get(); }
167 };
168
169 class MockHalController : public vibrator::HalController {
170 public:
171 MockHalController() = default;
172 virtual ~MockHalController() = default;
173
174 MOCK_METHOD(bool, init, (), (override));
175 MOCK_METHOD(void, tryReconnect, (), (override));
176 };
177
178 // -------------------------------------------------------------------------------------------------
179
180 } // namespace vibrator
181
182 } // namespace android
183
184 #endif // VIBRATORSERVICE_UNITTEST_MOCKS_H_
185