1 /*
2  * Copyright (C) 2018 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/types.h>
21 #include <android/hardware/vibrator/1.2/IVibrator.h>
22 #include <android/hardware/vibrator/1.2/types.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 #include <unistd.h>
27 
28 using ::android::hardware::vibrator::V1_0::Status;
29 using ::android::hardware::vibrator::V1_0::EffectStrength;
30 using ::android::hardware::vibrator::V1_2::Effect;
31 using ::android::hardware::vibrator::V1_2::IVibrator;
32 using ::android::hardware::hidl_enum_range;
33 using ::android::hardware::Return;
34 using ::android::hardware::Void;
35 using ::android::sp;
36 
37 #define EXPECT_OK(ret) ASSERT_TRUE((ret).isOk())
38 
39 // The main test class for VIBRATOR HIDL HAL 1.2.
40 class VibratorHidlTest_1_2 : public ::testing::TestWithParam<std::string> {
41    public:
SetUp()42     virtual void SetUp() override {
43         vibrator = IVibrator::getService(GetParam());
44         ASSERT_NE(vibrator, nullptr);
45     }
46 
TearDown()47     virtual void TearDown() override {}
48 
49     sp<IVibrator> vibrator;
50 };
51 
validatePerformEffect(Status status,uint32_t lengthMs)52 static void validatePerformEffect(Status status, uint32_t lengthMs) {
53     ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
54     if (status == Status::OK) {
55         ASSERT_LT(static_cast<uint32_t>(0), lengthMs)
56             << "Effects that return OK must return a positive duration";
57     } else {
58         ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
59             << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
60     }
61 }
62 
validatePerformEffectBadInput(Status status,uint32_t lengthMs)63 static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
64     ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
65     ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
66         << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
67 }
68 
69 /*
70  * Test to make sure effects within the valid range return are either supported and return OK with
71  * a valid duration, or are unsupported and return UNSUPPORTED_OPERATION with a duration of 0.
72  */
TEST_P(VibratorHidlTest_1_2,PerformEffect_1_2)73 TEST_P(VibratorHidlTest_1_2, PerformEffect_1_2) {
74     for (const auto& effect : hidl_enum_range<Effect>()) {
75         for (const auto& strength : hidl_enum_range<EffectStrength>()) {
76             EXPECT_OK(vibrator->perform_1_2(effect, strength, validatePerformEffect));
77         }
78     }
79 }
80 
81 /*
82  * Test to make sure effect values above the valid range are rejected.
83  */
TEST_P(VibratorHidlTest_1_2,PerformEffect_1_2_BadEffects_AboveValidRange)84 TEST_P(VibratorHidlTest_1_2, PerformEffect_1_2_BadEffects_AboveValidRange) {
85     Effect effect = *std::prev(hidl_enum_range<Effect>().end());
86     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) + 1);
87     EXPECT_OK(
88         vibrator->perform_1_2(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
89 }
90 
91 /*
92  * Test to make sure effect values below the valid range are rejected.
93  */
TEST_P(VibratorHidlTest_1_2,PerformEffect_1_2_BadEffects_BelowValidRange)94 TEST_P(VibratorHidlTest_1_2, PerformEffect_1_2_BadEffects_BelowValidRange) {
95     Effect effect = *hidl_enum_range<Effect>().begin();
96     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) - 1);
97     EXPECT_OK(
98         vibrator->perform_1_2(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
99 }
100 
101 /*
102  * Test to make sure strength values above the valid range are rejected.
103  */
TEST_P(VibratorHidlTest_1_2,PerformEffect_1_2_BadStrength_AboveValidRange)104 TEST_P(VibratorHidlTest_1_2, PerformEffect_1_2_BadStrength_AboveValidRange) {
105     EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
106     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
107     EXPECT_OK(vibrator->perform_1_2(Effect::THUD, badStrength, validatePerformEffectBadInput));
108 }
109 
110 /*
111  * Test to make sure strength values below the valid range are rejected.
112  */
TEST_P(VibratorHidlTest_1_2,PerformEffect_1_2_BadStrength_BelowValidRange)113 TEST_P(VibratorHidlTest_1_2, PerformEffect_1_2_BadStrength_BelowValidRange) {
114     EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
115     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
116     EXPECT_OK(vibrator->perform_1_2(Effect::THUD, badStrength, validatePerformEffectBadInput));
117 }
118 
119 INSTANTIATE_TEST_SUITE_P(
120         PerInstance, VibratorHidlTest_1_2,
121         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IVibrator::descriptor)),
122         android::hardware::PrintInstanceNameToString);
123