1 /*
2 * Copyright (C) 2016 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 "vibrator_hidl_hal_test"
18
19 #include <android-base/logging.h>
20 #include <android/hardware/vibrator/1.0/IVibrator.h>
21 #include <android/hardware/vibrator/1.0/types.h>
22 #include <unistd.h>
23
24 #include <VtsHalHidlTargetTestBase.h>
25 #include <VtsHalHidlTargetTestEnvBase.h>
26
27 using ::android::sp;
28 using ::android::hardware::hidl_enum_range;
29 using ::android::hardware::Return;
30 using ::android::hardware::Void;
31 using ::android::hardware::vibrator::V1_0::Effect;
32 using ::android::hardware::vibrator::V1_0::EffectStrength;
33 using ::android::hardware::vibrator::V1_0::IVibrator;
34 using ::android::hardware::vibrator::V1_0::Status;
35
36 #define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
37
38 // Test environment for Vibrator HIDL HAL.
39 class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
40 public:
41 // get the test environment singleton
Instance()42 static VibratorHidlEnvironment* Instance() {
43 static VibratorHidlEnvironment* instance = new VibratorHidlEnvironment;
44 return instance;
45 }
46
registerTestServices()47 virtual void registerTestServices() override { registerTestService<IVibrator>(); }
48
49 private:
VibratorHidlEnvironment()50 VibratorHidlEnvironment() {}
51 };
52
53 // The main test class for VIBRATOR HIDL HAL.
54 class VibratorHidlTest : public ::testing::VtsHalHidlTargetTestBase {
55 public:
SetUp()56 virtual void SetUp() override {
57 vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>(
58 VibratorHidlEnvironment::Instance()->getServiceName<IVibrator>());
59 ASSERT_NE(vibrator, nullptr);
60 }
61
TearDown()62 virtual void TearDown() override {}
63
64 sp<IVibrator> vibrator;
65 };
66
validatePerformEffect(Status status,uint32_t lengthMs)67 static void validatePerformEffect(Status status, uint32_t lengthMs) {
68 ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
69 if (status == Status::OK) {
70 ASSERT_GT(lengthMs, static_cast<uint32_t>(0));
71 } else {
72 ASSERT_EQ(lengthMs, static_cast<uint32_t>(0));
73 }
74 }
75
validatePerformEffectBadInput(Status status,uint32_t lengthMs)76 static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
77 ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
78 ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
79 << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
80 }
81
TEST_F(VibratorHidlTest,OnThenOffBeforeTimeout)82 TEST_F(VibratorHidlTest, OnThenOffBeforeTimeout) {
83 EXPECT_EQ(Status::OK, vibrator->on(2000));
84 sleep(1);
85 EXPECT_EQ(Status::OK, vibrator->off());
86 }
87
TEST_F(VibratorHidlTest,PerformEffect)88 TEST_F(VibratorHidlTest, PerformEffect) {
89 vibrator->perform(Effect::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
90 vibrator->perform(Effect::DOUBLE_CLICK, EffectStrength::LIGHT, validatePerformEffect);
91 }
92
93 /*
94 * Test to make sure effect values above the valid range are rejected.
95 */
TEST_F(VibratorHidlTest,PerformEffect_BadEffects_AboveValidRange)96 TEST_F(VibratorHidlTest, PerformEffect_BadEffects_AboveValidRange) {
97 Effect effect = *std::prev(hidl_enum_range<Effect>().end());
98 Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) + 1);
99 EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
100 }
101
102 /*
103 * Test to make sure effect values below the valid range are rejected.
104 */
TEST_F(VibratorHidlTest,PerformEffect_BadEffects_BelowValidRange)105 TEST_F(VibratorHidlTest, PerformEffect_BadEffects_BelowValidRange) {
106 Effect effect = *hidl_enum_range<Effect>().begin();
107 Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) - 1);
108 EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
109 }
110
111 /*
112 * Test to make sure strength values above the valid range are rejected.
113 */
TEST_F(VibratorHidlTest,PerformEffect_BadStrength_AboveValidRange)114 TEST_F(VibratorHidlTest, PerformEffect_BadStrength_AboveValidRange) {
115 EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
116 EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
117 EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
118 }
119
120 /*
121 * Test to make sure strength values below the valid range are rejected.
122 */
TEST_F(VibratorHidlTest,PerformEffect_BadStrength_BelowValidRange)123 TEST_F(VibratorHidlTest, PerformEffect_BadStrength_BelowValidRange) {
124 EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
125 EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
126 EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
127 }
128
TEST_F(VibratorHidlTest,ChangeVibrationalAmplitude)129 TEST_F(VibratorHidlTest, ChangeVibrationalAmplitude) {
130 if (vibrator->supportsAmplitudeControl()) {
131 EXPECT_EQ(Status::OK, vibrator->setAmplitude(1));
132 EXPECT_EQ(Status::OK, vibrator->on(2000));
133 EXPECT_EQ(Status::OK, vibrator->setAmplitude(128));
134 sleep(1);
135 EXPECT_EQ(Status::OK, vibrator->setAmplitude(255));
136 sleep(1);
137 }
138 }
139
TEST_F(VibratorHidlTest,AmplitudeOutsideRangeFails)140 TEST_F(VibratorHidlTest, AmplitudeOutsideRangeFails) {
141 if (vibrator->supportsAmplitudeControl()) {
142 EXPECT_EQ(Status::BAD_VALUE, vibrator->setAmplitude(0));
143 }
144 }
145
TEST_F(VibratorHidlTest,SetAmplitudeReturnUnsupportedOperationIfNotSupported)146 TEST_F(VibratorHidlTest, SetAmplitudeReturnUnsupportedOperationIfNotSupported) {
147 if (!vibrator->supportsAmplitudeControl()) {
148 EXPECT_EQ(Status::UNSUPPORTED_OPERATION, vibrator->setAmplitude(1));
149 }
150 }
151
main(int argc,char ** argv)152 int main(int argc, char **argv) {
153 ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
154 ::testing::InitGoogleTest(&argc, argv);
155 VibratorHidlEnvironment::Instance()->init(&argc, argv);
156 int status = RUN_ALL_TESTS();
157 LOG(INFO) << "Test result = " << status;
158 return status;
159 }
160