1 /*
2 * Copyright (C) 2017 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.1/IVibrator.h>
21 #include <android/hardware/vibrator/1.1/types.h>
22 #include <gtest/gtest.h>
23 #include <hidl/GtestPrinter.h>
24 #include <hidl/ServiceManagement.h>
25 #include <unistd.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::EffectStrength;
32 using ::android::hardware::vibrator::V1_0::Status;
33 using ::android::hardware::vibrator::V1_1::Effect_1_1;
34 using ::android::hardware::vibrator::V1_1::IVibrator;
35
36 #define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
37
38 // The main test class for VIBRATOR HIDL HAL 1.1.
39 class VibratorHidlTest_1_1 : public ::testing::TestWithParam<std::string> {
40 public:
SetUp()41 virtual void SetUp() override {
42 vibrator = IVibrator::getService(GetParam());
43 ASSERT_NE(vibrator, nullptr);
44 }
45
TearDown()46 virtual void TearDown() override {}
47
48 sp<IVibrator> vibrator;
49 };
50
validatePerformEffect(Status status,uint32_t lengthMs)51 static void validatePerformEffect(Status status, uint32_t lengthMs) {
52 ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
53 if (status == Status::OK) {
54 ASSERT_GT(lengthMs, static_cast<uint32_t>(0))
55 << "Effects that return OK must return a non-zero duration";
56 } else {
57 ASSERT_EQ(lengthMs, static_cast<uint32_t>(0))
58 << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
59 }
60 }
61
validatePerformEffectBadInput(Status status,uint32_t lengthMs)62 static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
63 ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
64 ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
65 << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
66 }
67
TEST_P(VibratorHidlTest_1_1,PerformEffect_1_1)68 TEST_P(VibratorHidlTest_1_1, PerformEffect_1_1) {
69 vibrator->perform_1_1(Effect_1_1::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
70 vibrator->perform_1_1(Effect_1_1::TICK, EffectStrength::STRONG, validatePerformEffect);
71 }
72
73 /*
74 * Test to make sure effect values above the valid range are rejected.
75 */
TEST_P(VibratorHidlTest_1_1,PerformEffect_1_1_BadEffects_AboveValidRange)76 TEST_P(VibratorHidlTest_1_1, PerformEffect_1_1_BadEffects_AboveValidRange) {
77 Effect_1_1 effect = *std::prev(hidl_enum_range<Effect_1_1>().end());
78 Effect_1_1 badEffect = static_cast<Effect_1_1>(static_cast<int32_t>(effect) + 1);
79 EXPECT_OK(
80 vibrator->perform_1_1(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
81 }
82
83 /*
84 * Test to make sure effect values below the valid range are rejected.
85 */
TEST_P(VibratorHidlTest_1_1,PerformEffect_1_1_BadEffects_BelowValidRange)86 TEST_P(VibratorHidlTest_1_1, PerformEffect_1_1_BadEffects_BelowValidRange) {
87 Effect_1_1 effect = *hidl_enum_range<Effect_1_1>().begin();
88 Effect_1_1 badEffect = static_cast<Effect_1_1>(static_cast<int32_t>(effect) - 1);
89 EXPECT_OK(
90 vibrator->perform_1_1(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
91 }
92
93 /*
94 * Test to make sure strength values above the valid range are rejected.
95 */
TEST_P(VibratorHidlTest_1_1,PerformEffect_1_1_BadStrength_AboveValidRange)96 TEST_P(VibratorHidlTest_1_1, PerformEffect_1_1_BadStrength_AboveValidRange) {
97 EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
98 EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
99 EXPECT_OK(vibrator->perform_1_1(Effect_1_1::CLICK, badStrength, validatePerformEffectBadInput));
100 }
101
102 /*
103 * Test to make sure strength values below the valid range are rejected.
104 */
TEST_P(VibratorHidlTest_1_1,PerformEffect_1_1_BadStrength_BelowValidRange)105 TEST_P(VibratorHidlTest_1_1, PerformEffect_1_1_BadStrength_BelowValidRange) {
106 EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
107 EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
108 EXPECT_OK(vibrator->perform_1_1(Effect_1_1::CLICK, badStrength, validatePerformEffectBadInput));
109 }
110
111 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorHidlTest_1_1);
112 INSTANTIATE_TEST_SUITE_P(
113 PerInstance, VibratorHidlTest_1_1,
114 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IVibrator::descriptor)),
115 android::hardware::PrintInstanceNameToString);
116