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 <gtest/gtest.h>
25 #include <hidl/GtestPrinter.h>
26 #include <hidl/ServiceManagement.h>
27 
28 using ::android::sp;
29 using ::android::hardware::hidl_enum_range;
30 using ::android::hardware::Return;
31 using ::android::hardware::Void;
32 using ::android::hardware::vibrator::V1_0::Effect;
33 using ::android::hardware::vibrator::V1_0::EffectStrength;
34 using ::android::hardware::vibrator::V1_0::IVibrator;
35 using ::android::hardware::vibrator::V1_0::Status;
36 
37 #define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
38 
39 // The main test class for VIBRATOR HIDL HAL.
40 class VibratorHidlTest : 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_GT(lengthMs, static_cast<uint32_t>(0));
56   } else {
57       ASSERT_EQ(lengthMs, static_cast<uint32_t>(0));
58   }
59 }
60 
validatePerformEffectBadInput(Status status,uint32_t lengthMs)61 static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
62     ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
63     ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
64             << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
65 }
66 
TEST_P(VibratorHidlTest,OnThenOffBeforeTimeout)67 TEST_P(VibratorHidlTest, OnThenOffBeforeTimeout) {
68   EXPECT_EQ(Status::OK, vibrator->on(2000));
69   sleep(1);
70   EXPECT_EQ(Status::OK, vibrator->off());
71 }
72 
TEST_P(VibratorHidlTest,PerformEffect)73 TEST_P(VibratorHidlTest, PerformEffect) {
74   vibrator->perform(Effect::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
75   vibrator->perform(Effect::DOUBLE_CLICK, EffectStrength::LIGHT, validatePerformEffect);
76 }
77 
78 /*
79  * Test to make sure effect values above the valid range are rejected.
80  */
TEST_P(VibratorHidlTest,PerformEffect_BadEffects_AboveValidRange)81 TEST_P(VibratorHidlTest, PerformEffect_BadEffects_AboveValidRange) {
82     Effect effect = *std::prev(hidl_enum_range<Effect>().end());
83     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) + 1);
84     EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
85 }
86 
87 /*
88  * Test to make sure effect values below the valid range are rejected.
89  */
TEST_P(VibratorHidlTest,PerformEffect_BadEffects_BelowValidRange)90 TEST_P(VibratorHidlTest, PerformEffect_BadEffects_BelowValidRange) {
91     Effect effect = *hidl_enum_range<Effect>().begin();
92     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) - 1);
93     EXPECT_OK(vibrator->perform(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
94 }
95 
96 /*
97  * Test to make sure strength values above the valid range are rejected.
98  */
TEST_P(VibratorHidlTest,PerformEffect_BadStrength_AboveValidRange)99 TEST_P(VibratorHidlTest, PerformEffect_BadStrength_AboveValidRange) {
100     EffectStrength strength = *std::prev(hidl_enum_range<EffectStrength>().end());
101     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
102     EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
103 }
104 
105 /*
106  * Test to make sure strength values below the valid range are rejected.
107  */
TEST_P(VibratorHidlTest,PerformEffect_BadStrength_BelowValidRange)108 TEST_P(VibratorHidlTest, PerformEffect_BadStrength_BelowValidRange) {
109     EffectStrength strength = *hidl_enum_range<EffectStrength>().begin();
110     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
111     EXPECT_OK(vibrator->perform(Effect::CLICK, badStrength, validatePerformEffectBadInput));
112 }
113 
TEST_P(VibratorHidlTest,ChangeVibrationalAmplitude)114 TEST_P(VibratorHidlTest, ChangeVibrationalAmplitude) {
115   if (vibrator->supportsAmplitudeControl()) {
116     EXPECT_EQ(Status::OK, vibrator->setAmplitude(1));
117     EXPECT_EQ(Status::OK, vibrator->on(2000));
118     EXPECT_EQ(Status::OK, vibrator->setAmplitude(128));
119     sleep(1);
120     EXPECT_EQ(Status::OK, vibrator->setAmplitude(255));
121     sleep(1);
122   }
123 }
124 
TEST_P(VibratorHidlTest,AmplitudeOutsideRangeFails)125 TEST_P(VibratorHidlTest, AmplitudeOutsideRangeFails) {
126   if (vibrator->supportsAmplitudeControl()) {
127     EXPECT_EQ(Status::BAD_VALUE, vibrator->setAmplitude(0));
128   }
129 }
130 
TEST_P(VibratorHidlTest,SetAmplitudeReturnUnsupportedOperationIfNotSupported)131 TEST_P(VibratorHidlTest, SetAmplitudeReturnUnsupportedOperationIfNotSupported) {
132   if (!vibrator->supportsAmplitudeControl()) {
133     EXPECT_EQ(Status::UNSUPPORTED_OPERATION, vibrator->setAmplitude(1));
134   }
135 }
136 
137 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VibratorHidlTest);
138 INSTANTIATE_TEST_SUITE_P(
139         PerInstance, VibratorHidlTest,
140         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IVibrator::descriptor)),
141         android::hardware::PrintInstanceNameToString);
142