1 /*
2 * Copyright (C) 2019 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 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18 #include <android/hardware/vibrator/BnVibratorCallback.h>
19 #include <android/hardware/vibrator/IVibrator.h>
20 #include <android/hardware/vibrator/IVibratorManager.h>
21 #include <binder/IServiceManager.h>
22 #include <binder/ProcessState.h>
23
24 #include <cmath>
25 #include <future>
26
27 using android::ProcessState;
28 using android::sp;
29 using android::String16;
30 using android::binder::Status;
31 using android::hardware::vibrator::ActivePwle;
32 using android::hardware::vibrator::BnVibratorCallback;
33 using android::hardware::vibrator::Braking;
34 using android::hardware::vibrator::BrakingPwle;
35 using android::hardware::vibrator::CompositeEffect;
36 using android::hardware::vibrator::CompositePrimitive;
37 using android::hardware::vibrator::Effect;
38 using android::hardware::vibrator::EffectStrength;
39 using android::hardware::vibrator::IVibrator;
40 using android::hardware::vibrator::IVibratorManager;
41 using android::hardware::vibrator::PrimitivePwle;
42 using std::chrono::high_resolution_clock;
43
44 const std::vector<Effect> kEffects{android::enum_range<Effect>().begin(),
45 android::enum_range<Effect>().end()};
46 const std::vector<EffectStrength> kEffectStrengths{android::enum_range<EffectStrength>().begin(),
47 android::enum_range<EffectStrength>().end()};
48
49 const std::vector<Effect> kInvalidEffects = {
50 static_cast<Effect>(static_cast<int32_t>(kEffects.front()) - 1),
51 static_cast<Effect>(static_cast<int32_t>(kEffects.back()) + 1),
52 };
53
54 const std::vector<EffectStrength> kInvalidEffectStrengths = {
55 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.front()) - 1),
56 static_cast<EffectStrength>(static_cast<int8_t>(kEffectStrengths.back()) + 1),
57 };
58
59 const std::vector<CompositePrimitive> kCompositePrimitives{
60 android::enum_range<CompositePrimitive>().begin(),
61 android::enum_range<CompositePrimitive>().end()};
62
63 const std::vector<CompositePrimitive> kRequiredPrimitives = {
64 CompositePrimitive::CLICK, CompositePrimitive::LIGHT_TICK,
65 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
66 CompositePrimitive::QUICK_FALL,
67 };
68
69 const std::vector<CompositePrimitive> kInvalidPrimitives = {
70 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.front()) - 1),
71 static_cast<CompositePrimitive>(static_cast<int32_t>(kCompositePrimitives.back()) + 1),
72 };
73
74 class CompletionCallback : public BnVibratorCallback {
75 public:
CompletionCallback(const std::function<void ()> & callback)76 CompletionCallback(const std::function<void()> &callback) : mCallback(callback) {}
onComplete()77 Status onComplete() override {
78 mCallback();
79 return Status::ok();
80 }
81
82 private:
83 std::function<void()> mCallback;
84 };
85
86 class VibratorAidl : public testing::TestWithParam<std::tuple<int32_t, int32_t>> {
87 public:
SetUp()88 virtual void SetUp() override {
89 int32_t managerIdx = std::get<0>(GetParam());
90 int32_t vibratorId = std::get<1>(GetParam());
91 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
92
93 if (managerIdx < 0) {
94 // Testing a unmanaged vibrator, using vibratorId as index from registered HALs
95 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
96 ASSERT_LT(vibratorId, vibratorAidlNames.size());
97 auto vibratorName = String16(vibratorAidlNames[vibratorId].c_str());
98 vibrator = android::waitForDeclaredService<IVibrator>(vibratorName);
99 } else {
100 // Testing a managed vibrator, using vibratorId to retrieve it from the manager
101 ASSERT_LT(managerIdx, managerAidlNames.size());
102 auto managerName = String16(managerAidlNames[managerIdx].c_str());
103 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
104 auto vibratorResult = vibratorManager->getVibrator(vibratorId, &vibrator);
105 ASSERT_TRUE(vibratorResult.isOk());
106 }
107
108 ASSERT_NE(vibrator, nullptr);
109 ASSERT_TRUE(vibrator->getCapabilities(&capabilities).isOk());
110 }
111
112 sp<IVibrator> vibrator;
113 int32_t capabilities;
114 };
115
isUnknownOrUnsupported(Status status)116 inline bool isUnknownOrUnsupported(Status status) {
117 return status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION ||
118 status.transactionError() == android::UNKNOWN_TRANSACTION;
119 }
120
getResonantFrequencyHz(sp<IVibrator> vibrator,int32_t capabilities)121 static float getResonantFrequencyHz(sp<IVibrator> vibrator, int32_t capabilities) {
122 float resonantFrequencyHz;
123 Status status = vibrator->getResonantFrequency(&resonantFrequencyHz);
124 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
125 EXPECT_GT(resonantFrequencyHz, 0);
126 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
127 } else {
128 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
129 }
130 return resonantFrequencyHz;
131 }
132
getFrequencyResolutionHz(sp<IVibrator> vibrator,int32_t capabilities)133 static float getFrequencyResolutionHz(sp<IVibrator> vibrator, int32_t capabilities) {
134 float freqResolutionHz;
135 Status status = vibrator->getFrequencyResolution(&freqResolutionHz);
136 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
137 EXPECT_GT(freqResolutionHz, 0);
138 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
139 } else {
140 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
141 }
142 return freqResolutionHz;
143 }
144
getFrequencyMinimumHz(sp<IVibrator> vibrator,int32_t capabilities)145 static float getFrequencyMinimumHz(sp<IVibrator> vibrator, int32_t capabilities) {
146 float freqMinimumHz;
147 Status status = vibrator->getFrequencyMinimum(&freqMinimumHz);
148 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
149 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
150
151 float resonantFrequencyHz = getResonantFrequencyHz(vibrator, capabilities);
152
153 EXPECT_GT(freqMinimumHz, 0);
154 EXPECT_LE(freqMinimumHz, resonantFrequencyHz);
155 } else {
156 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
157 }
158 return freqMinimumHz;
159 }
160
getFrequencyMaximumHz(sp<IVibrator> vibrator,int32_t capabilities)161 static float getFrequencyMaximumHz(sp<IVibrator> vibrator, int32_t capabilities) {
162 std::vector<float> bandwidthAmplitudeMap;
163 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
164 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
165 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
166 } else {
167 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
168 }
169
170 float freqMaximumHz =
171 (bandwidthAmplitudeMap.size() * getFrequencyResolutionHz(vibrator, capabilities)) +
172 getFrequencyMinimumHz(vibrator, capabilities);
173 return freqMaximumHz;
174 }
175
getAmplitudeMin()176 static float getAmplitudeMin() {
177 return 0.0;
178 }
179
getAmplitudeMax()180 static float getAmplitudeMax() {
181 return 1.0;
182 }
183
composeValidActivePwle(sp<IVibrator> vibrator,int32_t capabilities)184 static ActivePwle composeValidActivePwle(sp<IVibrator> vibrator, int32_t capabilities) {
185 float frequencyHz;
186 if (capabilities & IVibrator::CAP_GET_RESONANT_FREQUENCY) {
187 frequencyHz = getResonantFrequencyHz(vibrator, capabilities);
188 } else if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
189 frequencyHz = getFrequencyMinimumHz(vibrator, capabilities);
190 } else {
191 frequencyHz = 150.0; // default value commonly used
192 }
193
194 ActivePwle active;
195 active.startAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
196 active.startFrequency = frequencyHz;
197 active.endAmplitude = (getAmplitudeMin() + getAmplitudeMax()) / 2;
198 active.endFrequency = frequencyHz;
199 active.duration = 1000;
200
201 return active;
202 }
203
TEST_P(VibratorAidl,OnThenOffBeforeTimeout)204 TEST_P(VibratorAidl, OnThenOffBeforeTimeout) {
205 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
206 sleep(1);
207 EXPECT_TRUE(vibrator->off().isOk());
208 }
209
TEST_P(VibratorAidl,OnWithCallback)210 TEST_P(VibratorAidl, OnWithCallback) {
211 if (!(capabilities & IVibrator::CAP_ON_CALLBACK))
212 return;
213
214 std::promise<void> completionPromise;
215 std::future<void> completionFuture{completionPromise.get_future()};
216 sp<CompletionCallback> callback =
217 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
218 uint32_t durationMs = 250;
219 std::chrono::milliseconds timeout{durationMs * 2};
220 EXPECT_TRUE(vibrator->on(durationMs, callback).isOk());
221 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
222 EXPECT_TRUE(vibrator->off().isOk());
223 }
224
TEST_P(VibratorAidl,OnCallbackNotSupported)225 TEST_P(VibratorAidl, OnCallbackNotSupported) {
226 if (!(capabilities & IVibrator::CAP_ON_CALLBACK)) {
227 sp<CompletionCallback> callback = new CompletionCallback([] {});
228 Status status = vibrator->on(250, callback);
229 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
230 }
231 }
232
TEST_P(VibratorAidl,ValidateEffect)233 TEST_P(VibratorAidl, ValidateEffect) {
234 std::vector<Effect> supported;
235 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
236
237 for (Effect effect : kEffects) {
238 bool isEffectSupported =
239 std::find(supported.begin(), supported.end(), effect) != supported.end();
240
241 for (EffectStrength strength : kEffectStrengths) {
242 int32_t lengthMs = 0;
243 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
244
245 if (isEffectSupported) {
246 EXPECT_TRUE(status.isOk()) << toString(effect) << " " << toString(strength);
247 EXPECT_GT(lengthMs, 0);
248 usleep(lengthMs * 1000);
249 } else {
250 EXPECT_TRUE(isUnknownOrUnsupported(status))
251 << status << " " << toString(effect) << " " << toString(strength);
252 }
253 }
254 }
255 }
256
TEST_P(VibratorAidl,ValidateEffectWithCallback)257 TEST_P(VibratorAidl, ValidateEffectWithCallback) {
258 if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK))
259 return;
260
261 std::vector<Effect> supported;
262 ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk());
263
264 for (Effect effect : kEffects) {
265 bool isEffectSupported =
266 std::find(supported.begin(), supported.end(), effect) != supported.end();
267
268 for (EffectStrength strength : kEffectStrengths) {
269 std::promise<void> completionPromise;
270 std::future<void> completionFuture{completionPromise.get_future()};
271 sp<CompletionCallback> callback =
272 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
273 int lengthMs = 0;
274 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
275
276 if (isEffectSupported) {
277 EXPECT_TRUE(status.isOk());
278 EXPECT_GT(lengthMs, 0);
279 } else {
280 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
281 }
282
283 if (!status.isOk())
284 continue;
285
286 //TODO(b/187207798): revert back to conservative timeout values once
287 //latencies have been fixed
288 std::chrono::milliseconds timeout{lengthMs * 8};
289 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
290 }
291 }
292 }
293
TEST_P(VibratorAidl,ValidateEffectWithCallbackNotSupported)294 TEST_P(VibratorAidl, ValidateEffectWithCallbackNotSupported) {
295 if (capabilities & IVibrator::CAP_PERFORM_CALLBACK)
296 return;
297
298 for (Effect effect : kEffects) {
299 for (EffectStrength strength : kEffectStrengths) {
300 sp<CompletionCallback> callback = new CompletionCallback([] {});
301 int lengthMs;
302 Status status = vibrator->perform(effect, strength, callback, &lengthMs);
303 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
304 }
305 }
306 }
307
TEST_P(VibratorAidl,InvalidEffectsUnsupported)308 TEST_P(VibratorAidl, InvalidEffectsUnsupported) {
309 for (Effect effect : kInvalidEffects) {
310 for (EffectStrength strength : kEffectStrengths) {
311 int32_t lengthMs;
312 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
313 EXPECT_TRUE(isUnknownOrUnsupported(status))
314 << status << toString(effect) << " " << toString(strength);
315 }
316 }
317 for (Effect effect : kEffects) {
318 for (EffectStrength strength : kInvalidEffectStrengths) {
319 int32_t lengthMs;
320 Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs);
321 EXPECT_TRUE(isUnknownOrUnsupported(status))
322 << status << " " << toString(effect) << " " << toString(strength);
323 }
324 }
325 }
326
TEST_P(VibratorAidl,ChangeVibrationAmplitude)327 TEST_P(VibratorAidl, ChangeVibrationAmplitude) {
328 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
329 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.1f).exceptionCode());
330 EXPECT_TRUE(vibrator->on(2000, nullptr /*callback*/).isOk());
331 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(0.5f).exceptionCode());
332 sleep(1);
333 EXPECT_EQ(Status::EX_NONE, vibrator->setAmplitude(1.0f).exceptionCode());
334 sleep(1);
335 }
336 }
337
TEST_P(VibratorAidl,AmplitudeOutsideRangeFails)338 TEST_P(VibratorAidl, AmplitudeOutsideRangeFails) {
339 if (capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) {
340 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(-1).exceptionCode());
341 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(0).exceptionCode());
342 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT, vibrator->setAmplitude(1.1).exceptionCode());
343 }
344 }
345
TEST_P(VibratorAidl,AmplitudeReturnsUnsupportedMatchingCapabilities)346 TEST_P(VibratorAidl, AmplitudeReturnsUnsupportedMatchingCapabilities) {
347 if ((capabilities & IVibrator::CAP_AMPLITUDE_CONTROL) == 0) {
348 Status status = vibrator->setAmplitude(1);
349 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
350 }
351 }
352
TEST_P(VibratorAidl,ChangeVibrationExternalControl)353 TEST_P(VibratorAidl, ChangeVibrationExternalControl) {
354 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
355 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
356 sleep(1);
357 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
358 sleep(1);
359 }
360 }
361
TEST_P(VibratorAidl,ExternalAmplitudeControl)362 TEST_P(VibratorAidl, ExternalAmplitudeControl) {
363 const bool supportsExternalAmplitudeControl =
364 (capabilities & IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL) > 0;
365
366 if (capabilities & IVibrator::CAP_EXTERNAL_CONTROL) {
367 EXPECT_TRUE(vibrator->setExternalControl(true).isOk());
368
369 Status amplitudeStatus = vibrator->setAmplitude(0.5);
370 if (supportsExternalAmplitudeControl) {
371 EXPECT_TRUE(amplitudeStatus.isOk());
372 } else {
373 EXPECT_TRUE(isUnknownOrUnsupported(amplitudeStatus)) << amplitudeStatus;
374 }
375 EXPECT_TRUE(vibrator->setExternalControl(false).isOk());
376 } else {
377 EXPECT_FALSE(supportsExternalAmplitudeControl);
378 }
379 }
380
TEST_P(VibratorAidl,ExternalControlUnsupportedMatchingCapabilities)381 TEST_P(VibratorAidl, ExternalControlUnsupportedMatchingCapabilities) {
382 if ((capabilities & IVibrator::CAP_EXTERNAL_CONTROL) == 0) {
383 Status status = vibrator->setExternalControl(true);
384 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
385 }
386 }
387
TEST_P(VibratorAidl,GetSupportedPrimitives)388 TEST_P(VibratorAidl, GetSupportedPrimitives) {
389 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
390 std::vector<CompositePrimitive> supported;
391
392 EXPECT_EQ(Status::EX_NONE, vibrator->getSupportedPrimitives(&supported).exceptionCode());
393
394 for (auto primitive : kCompositePrimitives) {
395 bool isPrimitiveSupported =
396 std::find(supported.begin(), supported.end(), primitive) != supported.end();
397 bool isPrimitiveRequired =
398 std::find(kRequiredPrimitives.begin(), kRequiredPrimitives.end(), primitive) !=
399 kRequiredPrimitives.end();
400
401 EXPECT_TRUE(isPrimitiveSupported || !isPrimitiveRequired) << toString(primitive);
402 }
403 }
404 }
405
TEST_P(VibratorAidl,GetPrimitiveDuration)406 TEST_P(VibratorAidl, GetPrimitiveDuration) {
407 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
408 std::vector<CompositePrimitive> supported;
409 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
410
411 for (auto primitive : kCompositePrimitives) {
412 bool isPrimitiveSupported =
413 std::find(supported.begin(), supported.end(), primitive) != supported.end();
414 int32_t duration;
415
416 Status status = vibrator->getPrimitiveDuration(primitive, &duration);
417
418 if (isPrimitiveSupported) {
419 EXPECT_EQ(Status::EX_NONE, status.exceptionCode());
420 } else {
421 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
422 }
423 }
424 }
425 }
426
TEST_P(VibratorAidl,ComposeValidPrimitives)427 TEST_P(VibratorAidl, ComposeValidPrimitives) {
428 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
429 std::vector<CompositePrimitive> supported;
430 int32_t maxDelay, maxSize;
431
432 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
433 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
434 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
435
436 std::vector<CompositeEffect> composite;
437
438 for (auto primitive : supported) {
439 CompositeEffect effect;
440
441 effect.delayMs = std::rand() % (maxDelay + 1);
442 effect.primitive = primitive;
443 effect.scale = static_cast<float>(std::rand()) / RAND_MAX;
444 composite.emplace_back(effect);
445
446 if (composite.size() == maxSize) {
447 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
448 composite.clear();
449 vibrator->off();
450 }
451 }
452
453 if (composite.size() != 0) {
454 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
455 vibrator->off();
456 }
457 }
458 }
459
TEST_P(VibratorAidl,ComposeUnsupportedPrimitives)460 TEST_P(VibratorAidl, ComposeUnsupportedPrimitives) {
461 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
462 auto unsupported = kInvalidPrimitives;
463 std::vector<CompositePrimitive> supported;
464
465 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
466
467 for (auto primitive : kCompositePrimitives) {
468 bool isPrimitiveSupported =
469 std::find(supported.begin(), supported.end(), primitive) != supported.end();
470
471 if (!isPrimitiveSupported) {
472 unsupported.push_back(primitive);
473 }
474 }
475
476 for (auto primitive : unsupported) {
477 std::vector<CompositeEffect> composite(1);
478
479 for (auto &effect : composite) {
480 effect.delayMs = 0;
481 effect.primitive = primitive;
482 effect.scale = 1.0f;
483 }
484 Status status = vibrator->compose(composite, nullptr);
485 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
486 vibrator->off();
487 }
488 }
489 }
490
TEST_P(VibratorAidl,ComposeScaleBoundary)491 TEST_P(VibratorAidl, ComposeScaleBoundary) {
492 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
493 std::vector<CompositeEffect> composite(1);
494 CompositeEffect &effect = composite[0];
495
496 effect.delayMs = 0;
497 effect.primitive = CompositePrimitive::CLICK;
498
499 effect.scale = std::nextafter(0.0f, -1.0f);
500 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
501 vibrator->compose(composite, nullptr).exceptionCode());
502
503 effect.scale = 0.0f;
504 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
505
506 effect.scale = 1.0f;
507 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
508
509 effect.scale = std::nextafter(1.0f, 2.0f);
510 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
511 vibrator->compose(composite, nullptr).exceptionCode());
512
513 vibrator->off();
514 }
515 }
516
TEST_P(VibratorAidl,ComposeDelayBoundary)517 TEST_P(VibratorAidl, ComposeDelayBoundary) {
518 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
519 int32_t maxDelay;
520
521 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionDelayMax(&maxDelay).exceptionCode());
522
523 std::vector<CompositeEffect> composite(1);
524 CompositeEffect effect;
525
526 effect.delayMs = 1;
527 effect.primitive = CompositePrimitive::CLICK;
528 effect.scale = 1.0f;
529
530 std::fill(composite.begin(), composite.end(), effect);
531 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
532
533 effect.delayMs = maxDelay + 1;
534
535 std::fill(composite.begin(), composite.end(), effect);
536 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
537 vibrator->compose(composite, nullptr).exceptionCode());
538 vibrator->off();
539 }
540 }
541
TEST_P(VibratorAidl,ComposeSizeBoundary)542 TEST_P(VibratorAidl, ComposeSizeBoundary) {
543 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
544 int32_t maxSize;
545
546 EXPECT_EQ(Status::EX_NONE, vibrator->getCompositionSizeMax(&maxSize).exceptionCode());
547
548 std::vector<CompositeEffect> composite(maxSize);
549 CompositeEffect effect;
550
551 effect.delayMs = 1;
552 effect.primitive = CompositePrimitive::CLICK;
553 effect.scale = 1.0f;
554
555 std::fill(composite.begin(), composite.end(), effect);
556 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, nullptr).exceptionCode());
557
558 composite.emplace_back(effect);
559 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
560 vibrator->compose(composite, nullptr).exceptionCode());
561 vibrator->off();
562 }
563 }
564
TEST_P(VibratorAidl,ComposeCallback)565 TEST_P(VibratorAidl, ComposeCallback) {
566 if (capabilities & IVibrator::CAP_COMPOSE_EFFECTS) {
567 std::vector<CompositePrimitive> supported;
568
569 ASSERT_TRUE(vibrator->getSupportedPrimitives(&supported).isOk());
570
571 for (auto primitive : supported) {
572 if (primitive == CompositePrimitive::NOOP) {
573 continue;
574 }
575
576 std::promise<void> completionPromise;
577 std::future<void> completionFuture{completionPromise.get_future()};
578 sp<CompletionCallback> callback =
579 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
580 CompositeEffect effect;
581 std::vector<CompositeEffect> composite;
582 int32_t durationMs;
583 std::chrono::milliseconds duration;
584 std::chrono::time_point<high_resolution_clock> start, end;
585 std::chrono::milliseconds elapsed;
586
587 effect.delayMs = 0;
588 effect.primitive = primitive;
589 effect.scale = 1.0f;
590 composite.emplace_back(effect);
591
592 EXPECT_EQ(Status::EX_NONE,
593 vibrator->getPrimitiveDuration(primitive, &durationMs).exceptionCode())
594 << toString(primitive);
595 duration = std::chrono::milliseconds(durationMs);
596
597 start = high_resolution_clock::now();
598 EXPECT_EQ(Status::EX_NONE, vibrator->compose(composite, callback).exceptionCode())
599 << toString(primitive);
600
601 //TODO(b/187207798): revert back to conservative timeout values once
602 //latencies have been fixed
603 EXPECT_EQ(completionFuture.wait_for(duration * 4), std::future_status::ready)
604 << toString(primitive);
605 end = high_resolution_clock::now();
606
607 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
608 EXPECT_GE(elapsed.count(), duration.count()) << toString(primitive);
609 }
610 }
611 }
612
TEST_P(VibratorAidl,AlwaysOn)613 TEST_P(VibratorAidl, AlwaysOn) {
614 if (capabilities & IVibrator::CAP_ALWAYS_ON_CONTROL) {
615 std::vector<Effect> supported;
616 ASSERT_TRUE(vibrator->getSupportedAlwaysOnEffects(&supported).isOk());
617
618 for (Effect effect : kEffects) {
619 bool isEffectSupported =
620 std::find(supported.begin(), supported.end(), effect) != supported.end();
621
622 for (EffectStrength strength : kEffectStrengths) {
623 Status status = vibrator->alwaysOnEnable(0, effect, strength);
624
625 if (isEffectSupported) {
626 EXPECT_EQ(Status::EX_NONE, status.exceptionCode())
627 << toString(effect) << " " << toString(strength);
628 } else {
629 EXPECT_TRUE(isUnknownOrUnsupported(status))
630 << status << " " << toString(effect) << " " << toString(strength);
631 }
632 }
633 }
634
635 EXPECT_EQ(Status::EX_NONE, vibrator->alwaysOnDisable(0).exceptionCode());
636 }
637 }
638
TEST_P(VibratorAidl,GetResonantFrequency)639 TEST_P(VibratorAidl, GetResonantFrequency) {
640 getResonantFrequencyHz(vibrator, capabilities);
641 }
642
TEST_P(VibratorAidl,GetQFactor)643 TEST_P(VibratorAidl, GetQFactor) {
644 float qFactor;
645 Status status = vibrator->getQFactor(&qFactor);
646 if (capabilities & IVibrator::CAP_GET_Q_FACTOR) {
647 ASSERT_GT(qFactor, 0);
648 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
649 } else {
650 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
651 }
652 }
653
TEST_P(VibratorAidl,GetFrequencyResolution)654 TEST_P(VibratorAidl, GetFrequencyResolution) {
655 getFrequencyResolutionHz(vibrator, capabilities);
656 }
657
TEST_P(VibratorAidl,GetFrequencyMinimum)658 TEST_P(VibratorAidl, GetFrequencyMinimum) {
659 getFrequencyMinimumHz(vibrator, capabilities);
660 }
661
TEST_P(VibratorAidl,GetBandwidthAmplitudeMap)662 TEST_P(VibratorAidl, GetBandwidthAmplitudeMap) {
663 std::vector<float> bandwidthAmplitudeMap;
664 Status status = vibrator->getBandwidthAmplitudeMap(&bandwidthAmplitudeMap);
665 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
666 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
667 ASSERT_FALSE(bandwidthAmplitudeMap.empty());
668
669 int minMapSize = (getResonantFrequencyHz(vibrator, capabilities) -
670 getFrequencyMinimumHz(vibrator, capabilities)) /
671 getFrequencyResolutionHz(vibrator, capabilities);
672 ASSERT_GT(bandwidthAmplitudeMap.size(), minMapSize);
673
674 for (float e : bandwidthAmplitudeMap) {
675 ASSERT_GE(e, 0.0);
676 ASSERT_LE(e, 1.0);
677 }
678 } else {
679 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
680 }
681 }
682
TEST_P(VibratorAidl,GetPwlePrimitiveDurationMax)683 TEST_P(VibratorAidl, GetPwlePrimitiveDurationMax) {
684 int32_t durationMs;
685 Status status = vibrator->getPwlePrimitiveDurationMax(&durationMs);
686 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
687 ASSERT_NE(durationMs, 0);
688 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
689 } else {
690 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
691 }
692 }
693
TEST_P(VibratorAidl,GetPwleCompositionSizeMax)694 TEST_P(VibratorAidl, GetPwleCompositionSizeMax) {
695 int32_t maxSize;
696 Status status = vibrator->getPwleCompositionSizeMax(&maxSize);
697 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
698 ASSERT_NE(maxSize, 0);
699 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
700 } else {
701 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
702 }
703 }
704
TEST_P(VibratorAidl,GetSupportedBraking)705 TEST_P(VibratorAidl, GetSupportedBraking) {
706 std::vector<Braking> supported;
707 Status status = vibrator->getSupportedBraking(&supported);
708 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
709 bool isDefaultNoneSupported =
710 std::find(supported.begin(), supported.end(), Braking::NONE) != supported.end();
711 ASSERT_TRUE(isDefaultNoneSupported);
712 EXPECT_EQ(status.exceptionCode(), Status::EX_NONE);
713 } else {
714 EXPECT_TRUE(isUnknownOrUnsupported(status)) << status;
715 }
716 }
717
TEST_P(VibratorAidl,ComposeValidPwle)718 TEST_P(VibratorAidl, ComposeValidPwle) {
719 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
720 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
721
722 std::vector<Braking> supported;
723 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
724 bool isClabSupported =
725 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
726 BrakingPwle braking;
727 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
728 braking.duration = 100;
729
730 std::vector<PrimitivePwle> pwleQueue;
731 PrimitivePwle pwle;
732 pwle = active;
733 pwleQueue.emplace_back(std::move(pwle));
734 pwle = braking;
735 pwleQueue.emplace_back(std::move(pwle));
736 pwle = active;
737 pwleQueue.emplace_back(std::move(pwle));
738
739 EXPECT_EQ(Status::EX_NONE, vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
740 vibrator->off();
741 }
742 }
743
TEST_P(VibratorAidl,ComposeValidPwleWithCallback)744 TEST_P(VibratorAidl, ComposeValidPwleWithCallback) {
745 if (!((capabilities & IVibrator::CAP_ON_CALLBACK) &&
746 (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS)))
747 return;
748
749 std::promise<void> completionPromise;
750 std::future<void> completionFuture{completionPromise.get_future()};
751 sp<CompletionCallback> callback =
752 new CompletionCallback([&completionPromise] { completionPromise.set_value(); });
753 uint32_t durationMs = 2100; // Sum of 2 active and 1 braking below
754 //TODO(b/187207798): revert back to conservative timeout values once
755 //latencies have been fixed
756 std::chrono::milliseconds timeout{durationMs * 4};
757
758 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
759
760 std::vector<Braking> supported;
761 ASSERT_TRUE(vibrator->getSupportedBraking(&supported).isOk());
762 bool isClabSupported =
763 std::find(supported.begin(), supported.end(), Braking::CLAB) != supported.end();
764 BrakingPwle braking;
765 braking.braking = isClabSupported ? Braking::CLAB : Braking::NONE;
766 braking.duration = 100;
767
768 std::vector<PrimitivePwle> pwleQueue;
769 PrimitivePwle pwle;
770 pwle = active;
771 pwleQueue.emplace_back(std::move(pwle));
772 pwle = braking;
773 pwleQueue.emplace_back(std::move(pwle));
774 pwle = active;
775 pwleQueue.emplace_back(std::move(pwle));
776
777 EXPECT_TRUE(vibrator->composePwle(pwleQueue, callback).isOk());
778 EXPECT_EQ(completionFuture.wait_for(timeout), std::future_status::ready);
779 EXPECT_TRUE(vibrator->off().isOk());
780 }
781
TEST_P(VibratorAidl,ComposePwleSegmentBoundary)782 TEST_P(VibratorAidl, ComposePwleSegmentBoundary) {
783 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
784 std::vector<PrimitivePwle> pwleQueue;
785 // test empty queue
786 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
787 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
788 vibrator->off();
789
790 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
791
792 PrimitivePwle pwle;
793 pwle = active;
794 int segmentCountMax;
795 vibrator->getPwleCompositionSizeMax(&segmentCountMax);
796
797 // Create PWLE queue with more segments than allowed
798 for (int i = 0; i < segmentCountMax + 10; i++) {
799 pwleQueue.emplace_back(std::move(pwle));
800 }
801
802 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
803 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
804 vibrator->off();
805 }
806 }
807
TEST_P(VibratorAidl,ComposePwleAmplitudeParameterBoundary)808 TEST_P(VibratorAidl, ComposePwleAmplitudeParameterBoundary) {
809 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
810 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
811 active.startAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
812 active.endAmplitude = getAmplitudeMax() + 1.0; // Amplitude greater than allowed
813
814 std::vector<PrimitivePwle> pwleQueueGreater;
815 PrimitivePwle pwle;
816 pwle = active;
817 pwleQueueGreater.emplace_back(std::move(pwle));
818
819 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
820 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
821 vibrator->off();
822
823 active.startAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
824 active.endAmplitude = getAmplitudeMin() - 1.0; // Amplitude less than allowed
825
826 std::vector<PrimitivePwle> pwleQueueLess;
827 pwle = active;
828 pwleQueueLess.emplace_back(std::move(pwle));
829
830 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
831 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
832 vibrator->off();
833 }
834 }
835
TEST_P(VibratorAidl,ComposePwleFrequencyParameterBoundary)836 TEST_P(VibratorAidl, ComposePwleFrequencyParameterBoundary) {
837 if ((capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) &&
838 (capabilities & IVibrator::CAP_FREQUENCY_CONTROL)) {
839 float freqMinimumHz = getFrequencyMinimumHz(vibrator, capabilities);
840 float freqMaximumHz = getFrequencyMaximumHz(vibrator, capabilities);
841 float freqResolutionHz = getFrequencyResolutionHz(vibrator, capabilities);
842
843 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
844 active.startFrequency =
845 freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
846 active.endFrequency = freqMaximumHz + freqResolutionHz; // Frequency greater than allowed
847
848 std::vector<PrimitivePwle> pwleQueueGreater;
849 PrimitivePwle pwle;
850 pwle = active;
851 pwleQueueGreater.emplace_back(std::move(pwle));
852
853 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
854 vibrator->composePwle(pwleQueueGreater, nullptr).exceptionCode());
855 vibrator->off();
856
857 active.startFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
858 active.endFrequency = freqMinimumHz - freqResolutionHz; // Frequency less than allowed
859
860 std::vector<PrimitivePwle> pwleQueueLess;
861 pwle = active;
862 pwleQueueLess.emplace_back(std::move(pwle));
863
864 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
865 vibrator->composePwle(pwleQueueLess, nullptr).exceptionCode());
866 vibrator->off();
867 }
868 }
869
TEST_P(VibratorAidl,ComposePwleSegmentDurationBoundary)870 TEST_P(VibratorAidl, ComposePwleSegmentDurationBoundary) {
871 if (capabilities & IVibrator::CAP_COMPOSE_PWLE_EFFECTS) {
872 ActivePwle active = composeValidActivePwle(vibrator, capabilities);
873
874 int segmentDurationMaxMs;
875 vibrator->getPwlePrimitiveDurationMax(&segmentDurationMaxMs);
876 active.duration = segmentDurationMaxMs + 10; // Segment duration greater than allowed
877
878 std::vector<PrimitivePwle> pwleQueue;
879 PrimitivePwle pwle;
880 pwle = active;
881 pwleQueue.emplace_back(std::move(pwle));
882
883 EXPECT_EQ(Status::EX_ILLEGAL_ARGUMENT,
884 vibrator->composePwle(pwleQueue, nullptr).exceptionCode());
885 vibrator->off();
886 }
887 }
888
GenerateVibratorMapping()889 std::vector<std::tuple<int32_t, int32_t>> GenerateVibratorMapping() {
890 std::vector<std::tuple<int32_t, int32_t>> tuples;
891 auto managerAidlNames = android::getAidlHalInstanceNames(IVibratorManager::descriptor);
892 std::vector<int32_t> vibratorIds;
893
894 for (int i = 0; i < managerAidlNames.size(); i++) {
895 auto managerName = String16(managerAidlNames[i].c_str());
896 auto vibratorManager = android::waitForDeclaredService<IVibratorManager>(managerName);
897 if (vibratorManager->getVibratorIds(&vibratorIds).isOk()) {
898 for (auto &vibratorId : vibratorIds) {
899 tuples.push_back(std::make_tuple(i, vibratorId));
900 }
901 }
902 }
903
904 auto vibratorAidlNames = android::getAidlHalInstanceNames(IVibrator::descriptor);
905 for (int i = 0; i < vibratorAidlNames.size(); i++) {
906 tuples.push_back(std::make_tuple(-1, i));
907 }
908
909 return tuples;
910 }
911
PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> & info)912 std::string PrintGeneratedTest(const testing::TestParamInfo<VibratorAidl::ParamType> &info) {
913 const auto &[managerIdx, vibratorId] = info.param;
914 if (managerIdx < 0) {
915 return std::string("TOP_LEVEL_VIBRATOR_") + std::to_string(vibratorId);
916 }
917 return std::string("MANAGER_") + std::to_string(managerIdx) + "_VIBRATOR_ID_" +
918 std::to_string(vibratorId);
919 }
920
921 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorAidl);
922 INSTANTIATE_TEST_SUITE_P(Vibrator, VibratorAidl, testing::ValuesIn(GenerateVibratorMapping()),
923 PrintGeneratedTest);
924
main(int argc,char ** argv)925 int main(int argc, char **argv) {
926 ::testing::InitGoogleTest(&argc, argv);
927 ProcessState::self()->setThreadPoolMaxThreadCount(1);
928 ProcessState::self()->startThreadPool();
929 return RUN_ALL_TESTS();
930 }
931