Merge "vibrator: vts: Add Bad Effect/Strength Checks" into qt-dev

This commit is contained in:
TreeHugger Robot 2019-04-19 09:28:13 +00:00 committed by Android (Google) Code Review
commit 756c68e0b6
2 changed files with 98 additions and 7 deletions

View file

@ -24,13 +24,16 @@
#include <VtsHalHidlTargetTestBase.h>
#include <VtsHalHidlTargetTestEnvBase.h>
using ::android::sp;
using ::android::hardware::hidl_enum_range;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::vibrator::V1_0::Effect;
using ::android::hardware::vibrator::V1_0::EffectStrength;
using ::android::hardware::vibrator::V1_0::IVibrator;
using ::android::hardware::vibrator::V1_0::Status;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
#define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
// Test environment for Vibrator HIDL HAL.
class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
@ -70,6 +73,12 @@ static void validatePerformEffect(Status status, uint32_t lengthMs) {
}
}
static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
<< "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
}
TEST_F(VibratorHidlTest, OnThenOffBeforeTimeout) {
EXPECT_EQ(Status::OK, vibrator->on(2000));
sleep(1);
@ -81,6 +90,42 @@ TEST_F(VibratorHidlTest, PerformEffect) {
vibrator->perform(Effect::DOUBLE_CLICK, EffectStrength::LIGHT, validatePerformEffect);
}
/*
* Test to make sure effect values above the valid range are rejected.
*/
TEST_F(VibratorHidlTest, PerformEffect_BadEffects_AboveValidRange) {
Effect effect = *std::prev(hidl_enum_range<Effect>().end());
Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) + 1);
EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
}
/*
* Test to make sure effect values below the valid range are rejected.
*/
TEST_F(VibratorHidlTest, PerformEffect_BadEffects_BelowValidRange) {
Effect effect = *hidl_enum_range<Effect>().begin();
Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) - 1);
EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
}
/*
* Test to make sure strength values above the valid range are rejected.
*/
TEST_F(VibratorHidlTest, PerformEffect_BadStrength_AboveValidRange) {
EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
}
/*
* Test to make sure strength values below the valid range are rejected.
*/
TEST_F(VibratorHidlTest, PerformEffect_BadStrength_BelowValidRange) {
EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
}
TEST_F(VibratorHidlTest, ChangeVibrationalAmplitude) {
if (vibrator->supportsAmplitudeControl()) {
EXPECT_EQ(Status::OK, vibrator->setAmplitude(1));

View file

@ -23,14 +23,16 @@
#include <android/hardware/vibrator/1.1/types.h>
#include <unistd.h>
using ::android::hardware::vibrator::V1_0::Effect;
using ::android::sp;
using ::android::hardware::hidl_enum_range;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::vibrator::V1_0::EffectStrength;
using ::android::hardware::vibrator::V1_0::Status;
using ::android::hardware::vibrator::V1_1::Effect_1_1;
using ::android::hardware::vibrator::V1_1::IVibrator;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
#define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
// Test environment for Vibrator HIDL HAL.
class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
@ -72,11 +74,55 @@ static void validatePerformEffect(Status status, uint32_t lengthMs) {
}
}
static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
<< "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
}
TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1) {
vibrator->perform_1_1(Effect_1_1::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
vibrator->perform_1_1(Effect_1_1::TICK, EffectStrength::STRONG, validatePerformEffect);
}
/*
* Test to make sure effect values above the valid range are rejected.
*/
TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1_BadEffects_AboveValidRange) {
Effect_1_1 effect = *std::prev(hidl_enum_range<Effect_1_1>().end());
Effect_1_1 badEffect = static_cast<Effect_1_1>(static_cast<int32_t>(effect) + 1);
EXPECT_OK(
vibrator->perform_1_1(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
}
/*
* Test to make sure effect values below the valid range are rejected.
*/
TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1_BadEffects_BelowValidRange) {
Effect_1_1 effect = *hidl_enum_range<Effect_1_1>().begin();
Effect_1_1 badEffect = static_cast<Effect_1_1>(static_cast<int32_t>(effect) - 1);
EXPECT_OK(
vibrator->perform_1_1(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
}
/*
* Test to make sure strength values above the valid range are rejected.
*/
TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1_BadStrength_AboveValidRange) {
EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
EXPECT_OK(vibrator->perform_1_1(Effect_1_1::CLICK, badStrength, validatePerformEffectBadInput));
}
/*
* Test to make sure strength values below the valid range are rejected.
*/
TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1_BadStrength_BelowValidRange) {
EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
EXPECT_OK(vibrator->perform_1_1(Effect_1_1::CLICK, badStrength, validatePerformEffectBadInput));
}
int main(int argc, char** argv) {
::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
::testing::InitGoogleTest(&argc, argv);