1 /*
2 * Copyright (C) 2020 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 #define LOG_TAG "VibratorHalWrapperHidlV1_0Test"
18
19 #include <aidl/android/hardware/vibrator/IVibrator.h>
20 #include <android/persistable_bundle_aidl.h>
21
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24
25 #include <utils/Log.h>
26 #include <thread>
27
28 #include <vibratorservice/VibratorCallbackScheduler.h>
29 #include <vibratorservice/VibratorHalWrapper.h>
30
31 #include "test_mocks.h"
32 #include "test_utils.h"
33
34 namespace V1_0 = android::hardware::vibrator::V1_0;
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::IVibrator;
43 using aidl::android::hardware::vibrator::PrimitivePwle;
44 using aidl::android::hardware::vibrator::PwleV2Primitive;
45 using aidl::android::hardware::vibrator::VendorEffect;
46 using aidl::android::os::PersistableBundle;
47
48 using namespace android;
49 using namespace std::chrono_literals;
50 using namespace testing;
51
52 // -------------------------------------------------------------------------------------------------
53
54 class MockIVibratorV1_0 : public V1_0::IVibrator {
55 public:
56 MOCK_METHOD(hardware::Return<void>, ping, (), (override));
57 MOCK_METHOD(hardware::Return<V1_0::Status>, on, (uint32_t timeoutMs), (override));
58 MOCK_METHOD(hardware::Return<V1_0::Status>, off, (), (override));
59 MOCK_METHOD(hardware::Return<bool>, supportsAmplitudeControl, (), (override));
60 MOCK_METHOD(hardware::Return<V1_0::Status>, setAmplitude, (uint8_t amplitude), (override));
61 MOCK_METHOD(hardware::Return<void>, perform,
62 (V1_0::Effect effect, V1_0::EffectStrength strength, perform_cb cb), (override));
63 };
64
65 // -------------------------------------------------------------------------------------------------
66
67 class VibratorHalWrapperHidlV1_0Test : public Test {
68 public:
SetUp()69 void SetUp() override {
70 mMockHal = new StrictMock<MockIVibratorV1_0>();
71 mMockScheduler = std::make_shared<StrictMock<vibrator::MockCallbackScheduler>>();
72 mWrapper = std::make_unique<vibrator::HidlHalWrapperV1_0>(mMockScheduler, mMockHal);
73 ASSERT_NE(mWrapper, nullptr);
74 }
75
76 protected:
77 std::shared_ptr<StrictMock<vibrator::MockCallbackScheduler>> mMockScheduler = nullptr;
78 std::unique_ptr<vibrator::HalWrapper> mWrapper = nullptr;
79 sp<StrictMock<MockIVibratorV1_0>> mMockHal = nullptr;
80 };
81
82 // -------------------------------------------------------------------------------------------------
83
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPing)84 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPing) {
85 EXPECT_CALL(*mMockHal.get(), ping())
86 .Times(Exactly(2))
87 .WillOnce([]() { return hardware::Return<void>(); })
88 .WillRepeatedly([]() {
89 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
90 });
91
92 ASSERT_TRUE(mWrapper->ping().isOk());
93 ASSERT_TRUE(mWrapper->ping().isFailed());
94 }
95
TEST_F(VibratorHalWrapperHidlV1_0Test,TestOn)96 TEST_F(VibratorHalWrapperHidlV1_0Test, TestOn) {
97 {
98 InSequence seq;
99 EXPECT_CALL(*mMockHal.get(), on(Eq(static_cast<uint32_t>(1))))
100 .Times(Exactly(1))
101 .WillRepeatedly(
102 [](uint32_t) { return hardware::Return<V1_0::Status>(V1_0::Status::OK); });
103 EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(1ms)))
104 .Times(Exactly(1))
105 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
106 EXPECT_CALL(*mMockHal.get(), on(Eq(static_cast<uint32_t>(10))))
107 .Times(Exactly(1))
108 .WillRepeatedly([](uint32_t) {
109 return hardware::Return<V1_0::Status>(V1_0::Status::UNSUPPORTED_OPERATION);
110 });
111 EXPECT_CALL(*mMockHal.get(), on(Eq(static_cast<uint32_t>(11))))
112 .Times(Exactly(1))
113 .WillRepeatedly([](uint32_t) {
114 return hardware::Return<V1_0::Status>(V1_0::Status::BAD_VALUE);
115 });
116 EXPECT_CALL(*mMockHal.get(), on(Eq(static_cast<uint32_t>(12))))
117 .Times(Exactly(1))
118 .WillRepeatedly([](uint32_t) {
119 return hardware::Return<V1_0::Status>(hardware::Status::fromExceptionCode(-1));
120 });
121 }
122
123 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
124 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
125
126 ASSERT_TRUE(mWrapper->on(1ms, callback).isOk());
127 ASSERT_EQ(1, *callbackCounter.get());
128
129 ASSERT_TRUE(mWrapper->on(10ms, callback).isUnsupported());
130 ASSERT_TRUE(mWrapper->on(11ms, callback).isFailed());
131 ASSERT_TRUE(mWrapper->on(12ms, callback).isFailed());
132
133 // Callback not triggered for unsupported and on failure
134 ASSERT_EQ(1, *callbackCounter.get());
135 }
136
TEST_F(VibratorHalWrapperHidlV1_0Test,TestOff)137 TEST_F(VibratorHalWrapperHidlV1_0Test, TestOff) {
138 EXPECT_CALL(*mMockHal.get(), off())
139 .Times(Exactly(4))
140 .WillOnce([]() { return hardware::Return<V1_0::Status>(V1_0::Status::OK); })
141 .WillOnce([]() {
142 return hardware::Return<V1_0::Status>(V1_0::Status::UNSUPPORTED_OPERATION);
143 })
144 .WillOnce([]() { return hardware::Return<V1_0::Status>(V1_0::Status::BAD_VALUE); })
145 .WillRepeatedly([]() {
146 return hardware::Return<V1_0::Status>(hardware::Status::fromExceptionCode(-1));
147 });
148
149 ASSERT_TRUE(mWrapper->off().isOk());
150 ASSERT_TRUE(mWrapper->off().isUnsupported());
151 ASSERT_TRUE(mWrapper->off().isFailed());
152 ASSERT_TRUE(mWrapper->off().isFailed());
153 }
154
TEST_F(VibratorHalWrapperHidlV1_0Test,TestSetAmplitude)155 TEST_F(VibratorHalWrapperHidlV1_0Test, TestSetAmplitude) {
156 {
157 InSequence seq;
158 EXPECT_CALL(*mMockHal.get(), setAmplitude(Eq(static_cast<uint8_t>(1))))
159 .Times(Exactly(1))
160 .WillRepeatedly(
161 [](uint8_t) { return hardware::Return<V1_0::Status>(V1_0::Status::OK); });
162 EXPECT_CALL(*mMockHal.get(), setAmplitude(Eq(static_cast<uint8_t>(2))))
163 .Times(Exactly(1))
164 .WillRepeatedly([](uint8_t) {
165 return hardware::Return<V1_0::Status>(V1_0::Status::UNSUPPORTED_OPERATION);
166 });
167 EXPECT_CALL(*mMockHal.get(), setAmplitude(Eq(static_cast<uint8_t>(3))))
168 .Times(Exactly(1))
169 .WillRepeatedly([](uint8_t) {
170 return hardware::Return<V1_0::Status>(V1_0::Status::BAD_VALUE);
171 });
172 EXPECT_CALL(*mMockHal.get(), setAmplitude(Eq(static_cast<uint8_t>(4))))
173 .Times(Exactly(1))
174 .WillRepeatedly([](uint8_t) {
175 return hardware::Return<V1_0::Status>(hardware::Status::fromExceptionCode(-1));
176 });
177 }
178
179 auto maxAmplitude = std::numeric_limits<uint8_t>::max();
180 ASSERT_TRUE(mWrapper->setAmplitude(1.0f / maxAmplitude).isOk());
181 ASSERT_TRUE(mWrapper->setAmplitude(2.0f / maxAmplitude).isUnsupported());
182 ASSERT_TRUE(mWrapper->setAmplitude(3.0f / maxAmplitude).isFailed());
183 ASSERT_TRUE(mWrapper->setAmplitude(4.0f / maxAmplitude).isFailed());
184 }
185
TEST_F(VibratorHalWrapperHidlV1_0Test,TestSetExternalControlUnsupported)186 TEST_F(VibratorHalWrapperHidlV1_0Test, TestSetExternalControlUnsupported) {
187 ASSERT_TRUE(mWrapper->setExternalControl(true).isUnsupported());
188 ASSERT_TRUE(mWrapper->setExternalControl(false).isUnsupported());
189 }
190
TEST_F(VibratorHalWrapperHidlV1_0Test,TestAlwaysOnEnableUnsupported)191 TEST_F(VibratorHalWrapperHidlV1_0Test, TestAlwaysOnEnableUnsupported) {
192 ASSERT_TRUE(mWrapper->alwaysOnEnable(1, Effect::CLICK, EffectStrength::LIGHT).isUnsupported());
193 }
194
TEST_F(VibratorHalWrapperHidlV1_0Test,TestAlwaysOnDisableUnsupported)195 TEST_F(VibratorHalWrapperHidlV1_0Test, TestAlwaysOnDisableUnsupported) {
196 ASSERT_TRUE(mWrapper->alwaysOnDisable(1).isUnsupported());
197 }
198
TEST_F(VibratorHalWrapperHidlV1_0Test,TestGetInfoDoesNotCacheFailedResult)199 TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoDoesNotCacheFailedResult) {
200 EXPECT_CALL(*mMockHal.get(), supportsAmplitudeControl())
201 .Times(Exactly(2))
202 .WillOnce([]() {
203 return hardware::Return<bool>(hardware::Status::fromExceptionCode(-1));
204 })
205 .WillRepeatedly([]() { return hardware::Return<bool>(true); });
206
207 ASSERT_TRUE(mWrapper->getInfo().capabilities.isFailed());
208
209 vibrator::Info info = mWrapper->getInfo();
210 ASSERT_EQ(vibrator::Capabilities::AMPLITUDE_CONTROL, info.capabilities.value());
211 ASSERT_TRUE(info.supportedEffects.isUnsupported());
212 ASSERT_TRUE(info.supportedBraking.isUnsupported());
213 ASSERT_TRUE(info.supportedPrimitives.isUnsupported());
214 ASSERT_TRUE(info.primitiveDurations.isUnsupported());
215 ASSERT_TRUE(info.primitiveDelayMax.isUnsupported());
216 ASSERT_TRUE(info.pwlePrimitiveDurationMax.isUnsupported());
217 ASSERT_TRUE(info.compositionSizeMax.isUnsupported());
218 ASSERT_TRUE(info.pwleSizeMax.isUnsupported());
219 ASSERT_TRUE(info.minFrequency.isUnsupported());
220 ASSERT_TRUE(info.resonantFrequency.isUnsupported());
221 ASSERT_TRUE(info.frequencyResolution.isUnsupported());
222 ASSERT_TRUE(info.qFactor.isUnsupported());
223 ASSERT_TRUE(info.maxAmplitudes.isUnsupported());
224 ASSERT_TRUE(info.maxEnvelopeEffectSize.isUnsupported());
225 ASSERT_TRUE(info.minEnvelopeEffectControlPointDuration.isUnsupported());
226 ASSERT_TRUE(info.maxEnvelopeEffectControlPointDuration.isUnsupported());
227 ASSERT_TRUE(info.frequencyToOutputAccelerationMap.isUnsupported());
228 }
229
TEST_F(VibratorHalWrapperHidlV1_0Test,TestGetInfoWithoutAmplitudeControl)230 TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoWithoutAmplitudeControl) {
231 EXPECT_CALL(*mMockHal.get(), supportsAmplitudeControl()).Times(Exactly(1)).WillRepeatedly([]() {
232 return hardware::Return<bool>(false);
233 });
234
235 ASSERT_EQ(vibrator::Capabilities::NONE, mWrapper->getInfo().capabilities.value());
236 }
237
TEST_F(VibratorHalWrapperHidlV1_0Test,TestGetInfoCachesResult)238 TEST_F(VibratorHalWrapperHidlV1_0Test, TestGetInfoCachesResult) {
239 EXPECT_CALL(*mMockHal.get(), supportsAmplitudeControl()).Times(Exactly(1)).WillRepeatedly([]() {
240 return hardware::Return<bool>(true);
241 });
242
243 std::vector<std::thread> threads;
244 for (int i = 0; i < 10; i++) {
245 threads.push_back(
246 std::thread([&]() { ASSERT_TRUE(mWrapper->getInfo().capabilities.isOk()); }));
247 }
248 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
249
250 vibrator::Info info = mWrapper->getInfo();
251 ASSERT_EQ(vibrator::Capabilities::AMPLITUDE_CONTROL, info.capabilities.value());
252 ASSERT_TRUE(info.supportedEffects.isUnsupported());
253 ASSERT_TRUE(info.supportedBraking.isUnsupported());
254 ASSERT_TRUE(info.supportedPrimitives.isUnsupported());
255 ASSERT_TRUE(info.primitiveDurations.isUnsupported());
256 ASSERT_TRUE(info.minFrequency.isUnsupported());
257 ASSERT_TRUE(info.resonantFrequency.isUnsupported());
258 ASSERT_TRUE(info.frequencyResolution.isUnsupported());
259 ASSERT_TRUE(info.qFactor.isUnsupported());
260 ASSERT_TRUE(info.maxAmplitudes.isUnsupported());
261 ASSERT_TRUE(info.maxEnvelopeEffectSize.isUnsupported());
262 ASSERT_TRUE(info.minEnvelopeEffectControlPointDuration.isUnsupported());
263 ASSERT_TRUE(info.maxEnvelopeEffectControlPointDuration.isUnsupported());
264 ASSERT_TRUE(info.frequencyToOutputAccelerationMap.isUnsupported());
265 }
266
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPerformEffect)267 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformEffect) {
268 {
269 InSequence seq;
270 EXPECT_CALL(*mMockHal.get(),
271 perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::LIGHT), _))
272 .Times(Exactly(1))
273 .WillRepeatedly(
274 [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_0::perform_cb cb) {
275 cb(V1_0::Status::OK, 10);
276 return hardware::Return<void>();
277 });
278 EXPECT_CALL(*mMockScheduler.get(), schedule(_, Eq(10ms)))
279 .Times(Exactly(1))
280 .WillRepeatedly(vibrator::TriggerSchedulerCallback());
281 EXPECT_CALL(*mMockHal.get(),
282 perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::MEDIUM), _))
283 .Times(Exactly(1))
284 .WillRepeatedly(
285 [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_0::perform_cb cb) {
286 cb(V1_0::Status::UNSUPPORTED_OPERATION, 10);
287 return hardware::Return<void>();
288 });
289 EXPECT_CALL(*mMockHal.get(),
290 perform(Eq(V1_0::Effect::CLICK), Eq(V1_0::EffectStrength::STRONG), _))
291 .Times(Exactly(2))
292 .WillOnce([](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_0::perform_cb cb) {
293 cb(V1_0::Status::BAD_VALUE, 10);
294 return hardware::Return<void>();
295 })
296 .WillRepeatedly(
297 [](V1_0::Effect, V1_0::EffectStrength, MockIVibratorV1_0::perform_cb) {
298 return hardware::Return<void>(hardware::Status::fromExceptionCode(-1));
299 });
300 }
301
302 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
303 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
304
305 auto result = mWrapper->performEffect(Effect::CLICK, EffectStrength::LIGHT, callback);
306 ASSERT_TRUE(result.isOk());
307 ASSERT_EQ(10ms, result.value());
308 ASSERT_EQ(1, *callbackCounter.get());
309
310 result = mWrapper->performEffect(Effect::CLICK, EffectStrength::MEDIUM, callback);
311 ASSERT_TRUE(result.isUnsupported());
312
313 result = mWrapper->performEffect(Effect::CLICK, EffectStrength::STRONG, callback);
314 ASSERT_TRUE(result.isFailed());
315
316 result = mWrapper->performEffect(Effect::CLICK, EffectStrength::STRONG, callback);
317 ASSERT_TRUE(result.isFailed());
318
319 // Callback not triggered for unsupported and on failure
320 ASSERT_EQ(1, *callbackCounter.get());
321 }
322
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPerformEffectUnsupported)323 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformEffectUnsupported) {
324 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
325 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
326 // Using TICK that is only available in v1.1
327 auto result = mWrapper->performEffect(Effect::TICK, EffectStrength::LIGHT, callback);
328 ASSERT_TRUE(result.isUnsupported());
329 // No callback is triggered.
330 ASSERT_EQ(0, *callbackCounter.get());
331 }
332
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPerformVendorEffectUnsupported)333 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformVendorEffectUnsupported) {
334 PersistableBundle vendorData; // empty
335 VendorEffect vendorEffect;
336 vendorEffect.vendorData = vendorData;
337 vendorEffect.strength = EffectStrength::LIGHT;
338 vendorEffect.scale = 1.0f;
339
340 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
341 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
342
343 ASSERT_TRUE(mWrapper->performVendorEffect(vendorEffect, callback).isUnsupported());
344
345 // No callback is triggered.
346 ASSERT_EQ(0, *callbackCounter.get());
347 }
348
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPerformComposedEffectUnsupported)349 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformComposedEffectUnsupported) {
350 std::vector<CompositeEffect> emptyEffects, singleEffect, multipleEffects;
351 singleEffect.push_back(
352 vibrator::TestFactory::createCompositeEffect(CompositePrimitive::CLICK, 10ms, 0.0f));
353 multipleEffects.push_back(
354 vibrator::TestFactory::createCompositeEffect(CompositePrimitive::SPIN, 100ms, 0.5f));
355 multipleEffects.push_back(
356 vibrator::TestFactory::createCompositeEffect(CompositePrimitive::THUD, 1000ms, 1.0f));
357
358 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
359 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
360
361 ASSERT_TRUE(mWrapper->performComposedEffect(singleEffect, callback).isUnsupported());
362 ASSERT_TRUE(mWrapper->performComposedEffect(multipleEffects, callback).isUnsupported());
363
364 // No callback is triggered.
365 ASSERT_EQ(0, *callbackCounter.get());
366 }
367
TEST_F(VibratorHalWrapperHidlV1_0Test,TestPerformPwleEffectUnsupported)368 TEST_F(VibratorHalWrapperHidlV1_0Test, TestPerformPwleEffectUnsupported) {
369 std::vector<PrimitivePwle> emptyPrimitives, multiplePrimitives;
370 multiplePrimitives.push_back(vibrator::TestFactory::createActivePwle(0, 1, 0, 1, 10ms));
371 multiplePrimitives.push_back(vibrator::TestFactory::createBrakingPwle(Braking::NONE, 100ms));
372
373 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
374 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
375
376 ASSERT_TRUE(mWrapper->performPwleEffect(emptyPrimitives, callback).isUnsupported());
377 ASSERT_TRUE(mWrapper->performPwleEffect(multiplePrimitives, callback).isUnsupported());
378
379 // No callback is triggered.
380 ASSERT_EQ(0, *callbackCounter.get());
381 }
382
TEST_F(VibratorHalWrapperHidlV1_0Test,TestComposePwleV2Unsupported)383 TEST_F(VibratorHalWrapperHidlV1_0Test, TestComposePwleV2Unsupported) {
384 CompositePwleV2 composite;
385 composite.pwlePrimitives = {
386 PwleV2Primitive(/*amplitude=*/0.2, /*frequency=*/50, /*time=*/100),
387 PwleV2Primitive(/*amplitude=*/0.5, /*frequency=*/150, /*time=*/100),
388 PwleV2Primitive(/*amplitude=*/0.8, /*frequency=*/250, /*time=*/100),
389 };
390
391 std::unique_ptr<int32_t> callbackCounter = std::make_unique<int32_t>();
392 auto callback = vibrator::TestFactory::createCountingCallback(callbackCounter.get());
393
394 ASSERT_TRUE(mWrapper->composePwleV2(composite, callback).isUnsupported());
395
396 // No callback is triggered.
397 ASSERT_EQ(0, *callbackCounter.get());
398 }
399