From 2932b225715a61c7337fc924d263667fbb94aacf Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Tue, 5 Nov 2019 14:30:17 -0800 Subject: [PATCH] vibrator: add getSupportedEffects It's a real gap in this interface, since we need to call perform to understand if an effect is supported. Bug: 141828236 Test: atest VtsHalVibratorTargetTest Change-Id: Iffbd9c0acf5e4c368767c7718025a4aef8f44ce3 --- .../android/hardware/vibrator/IVibrator.aidl | 8 +++++ vibrator/aidl/default/Vibrator.cpp | 5 +++ vibrator/aidl/default/Vibrator.h | 1 + .../aidl/vts/VtsHalVibratorTargetTest.cpp | 34 +++++++++++++++---- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/vibrator/aidl/android/hardware/vibrator/IVibrator.aidl b/vibrator/aidl/android/hardware/vibrator/IVibrator.aidl index b2008f4be6..d21274b50a 100644 --- a/vibrator/aidl/android/hardware/vibrator/IVibrator.aidl +++ b/vibrator/aidl/android/hardware/vibrator/IVibrator.aidl @@ -73,6 +73,14 @@ interface IVibrator { */ int perform(in Effect effect, in EffectStrength strength, in IVibratorCallback callback); + /** + * List supported effects. + * + * Return the effects which are supported (an effect is expected to be supported at every + * strength level. + */ + Effect[] getSupportedEffects(); + /** * Sets the motor's vibrational amplitude. * diff --git a/vibrator/aidl/default/Vibrator.cpp b/vibrator/aidl/default/Vibrator.cpp index cdf8e0963b..2aec8c507c 100644 --- a/vibrator/aidl/default/Vibrator.cpp +++ b/vibrator/aidl/default/Vibrator.cpp @@ -78,6 +78,11 @@ ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, return ndk::ScopedAStatus::ok(); } +ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector* _aidl_return) { + *_aidl_return = {Effect::CLICK, Effect::TICK}; + return ndk::ScopedAStatus::ok(); +} + ndk::ScopedAStatus Vibrator::setAmplitude(int32_t amplitude) { LOG(INFO) << "Vibrator set amplitude: " << amplitude; if (amplitude <= 0 || amplitude > 255) { diff --git a/vibrator/aidl/default/Vibrator.h b/vibrator/aidl/default/Vibrator.h index 77e96e3e4d..14e7292b43 100644 --- a/vibrator/aidl/default/Vibrator.h +++ b/vibrator/aidl/default/Vibrator.h @@ -31,6 +31,7 @@ class Vibrator : public BnVibrator { ndk::ScopedAStatus perform(Effect effect, EffectStrength strength, const std::shared_ptr& callback, int32_t* _aidl_return) override; + ndk::ScopedAStatus getSupportedEffects(std::vector* _aidl_return) override; ndk::ScopedAStatus setAmplitude(int32_t amplitude) override; ndk::ScopedAStatus setExternalControl(bool enabled) override; }; diff --git a/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp b/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp index 33bcaf5c9a..b0e6c7255a 100644 --- a/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp +++ b/vibrator/aidl/vts/VtsHalVibratorTargetTest.cpp @@ -32,6 +32,7 @@ using android::hardware::vibrator::Effect; using android::hardware::vibrator::EffectStrength; using android::hardware::vibrator::IVibrator; +// TODO(b/143992652): autogenerate const std::vector kEffects = { Effect::CLICK, Effect::DOUBLE_CLICK, Effect::TICK, Effect::THUD, Effect::POP, Effect::HEAVY_CLICK, Effect::RINGTONE_1, Effect::RINGTONE_2, @@ -40,6 +41,7 @@ const std::vector kEffects = { Effect::RINGTONE_11, Effect::RINGTONE_12, Effect::RINGTONE_13, Effect::RINGTONE_14, Effect::RINGTONE_15, Effect::TEXTURE_TICK}; +// TODO(b/143992652): autogenerate const std::vector kEffectStrengths = {EffectStrength::LIGHT, EffectStrength::MEDIUM, EffectStrength::STRONG}; @@ -105,15 +107,22 @@ TEST_P(VibratorAidl, OnCallbackNotSupported) { } TEST_P(VibratorAidl, ValidateEffect) { + std::vector supported; + ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); + for (Effect effect : kEffects) { + bool isEffectSupported = + std::find(supported.begin(), supported.end(), effect) != supported.end(); + for (EffectStrength strength : kEffectStrengths) { int32_t lengthMs = 0; Status status = vibrator->perform(effect, strength, nullptr /*callback*/, &lengthMs); - EXPECT_TRUE(status.isOk() || - status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION); - if (status.isOk()) { + + if (isEffectSupported) { + EXPECT_TRUE(status.isOk()); EXPECT_GT(lengthMs, 0); } else { + EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); EXPECT_EQ(lengthMs, 0); } } @@ -123,16 +132,29 @@ TEST_P(VibratorAidl, ValidateEffect) { TEST_P(VibratorAidl, ValidateEffectWithCallback) { if (!(capabilities & IVibrator::CAP_PERFORM_CALLBACK)) return; + std::vector supported; + ASSERT_TRUE(vibrator->getSupportedEffects(&supported).isOk()); + for (Effect effect : kEffects) { + bool isEffectSupported = + std::find(supported.begin(), supported.end(), effect) != supported.end(); + for (EffectStrength strength : kEffectStrengths) { std::promise completionPromise; std::future completionFuture{completionPromise.get_future()}; sp callback = new CompletionCallback([&completionPromise] { completionPromise.set_value(); }); - int lengthMs; + int lengthMs = 0; Status status = vibrator->perform(effect, strength, callback, &lengthMs); - EXPECT_TRUE(status.isOk() || - status.exceptionCode() == Status::EX_UNSUPPORTED_OPERATION); + + if (isEffectSupported) { + EXPECT_TRUE(status.isOk()); + EXPECT_GT(lengthMs, 0); + } else { + EXPECT_EQ(status.exceptionCode(), Status::EX_UNSUPPORTED_OPERATION); + EXPECT_EQ(lengthMs, 0); + } + if (!status.isOk()) continue; std::chrono::milliseconds timeout{lengthMs * 2};