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