• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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.2/IVibrator.h>
24 #include <android/hardware/vibrator/1.2/types.h>
25 #include <unistd.h>
26 
27 using ::android::hardware::vibrator::V1_0::Status;
28 using ::android::hardware::vibrator::V1_0::EffectStrength;
29 using ::android::hardware::vibrator::V1_2::Effect;
30 using ::android::hardware::vibrator::V1_2::IVibrator;
31 using ::android::hardware::hidl_enum_iterator;
32 using ::android::hardware::Return;
33 using ::android::hardware::Void;
34 using ::android::sp;
35 
36 #define EXPECT_OK(ret) ASSERT_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 1.2.
54 class VibratorHidlTest_1_2 : 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_LT(static_cast<uint32_t>(0), lengthMs)
71             << "Effects that return OK must return a positive duration";
72     } else {
73         ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
74             << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
75     }
76 }
77 
validatePerformEffectBadInput(Status status,uint32_t lengthMs)78 static void validatePerformEffectBadInput(Status status, uint32_t lengthMs) {
79     ASSERT_EQ(Status::UNSUPPORTED_OPERATION, status);
80     ASSERT_EQ(static_cast<uint32_t>(0), lengthMs)
81         << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
82 }
83 
84 /*
85  * Test to make sure effects within the valid range return are either supported and return OK with
86  * a valid duration, or are unsupported and return UNSUPPORTED_OPERATION with a duration of 0.
87  */
TEST_F(VibratorHidlTest_1_2,PerformEffect_1_2)88 TEST_F(VibratorHidlTest_1_2, PerformEffect_1_2) {
89     for (const auto& effect : hidl_enum_iterator<Effect>()) {
90         for (const auto& strength : hidl_enum_iterator<EffectStrength>()) {
91             EXPECT_OK(vibrator->perform_1_2(effect, strength, validatePerformEffect));
92         }
93     }
94 }
95 
96 /*
97  * Test to make sure effect values above the valid range are rejected.
98  */
TEST_F(VibratorHidlTest_1_2,PerformEffect_1_2_BadEffects_AboveValidRange)99 TEST_F(VibratorHidlTest_1_2, PerformEffect_1_2_BadEffects_AboveValidRange) {
100     Effect effect = *std::prev(hidl_enum_iterator<Effect>().end());
101     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) + 1);
102     EXPECT_OK(
103         vibrator->perform_1_2(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
104 }
105 
106 /*
107  * Test to make sure effect values below the valid range are rejected.
108  */
TEST_F(VibratorHidlTest_1_2,PerformEffect_1_2_BadEffects_BelowValidRange)109 TEST_F(VibratorHidlTest_1_2, PerformEffect_1_2_BadEffects_BelowValidRange) {
110     Effect effect = *hidl_enum_iterator<Effect>().begin();
111     Effect badEffect = static_cast<Effect>(static_cast<int32_t>(effect) - 1);
112     EXPECT_OK(
113         vibrator->perform_1_2(badEffect, EffectStrength::LIGHT, validatePerformEffectBadInput));
114 }
115 
116 /*
117  * Test to make sure strength values above the valid range are rejected.
118  */
TEST_F(VibratorHidlTest_1_2,PerformEffect_1_2_BadStrength_AboveValidRange)119 TEST_F(VibratorHidlTest_1_2, PerformEffect_1_2_BadStrength_AboveValidRange) {
120     EffectStrength strength = *std::prev(hidl_enum_iterator<EffectStrength>().end());
121     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) + 1);
122     EXPECT_OK(vibrator->perform_1_2(Effect::THUD, badStrength, validatePerformEffectBadInput));
123 }
124 
125 /*
126  * Test to make sure strength values below the valid range are rejected.
127  */
TEST_F(VibratorHidlTest_1_2,PerformEffect_1_2_BadStrength_BelowValidRange)128 TEST_F(VibratorHidlTest_1_2, PerformEffect_1_2_BadStrength_BelowValidRange) {
129     EffectStrength strength = *hidl_enum_iterator<EffectStrength>().begin();
130     EffectStrength badStrength = static_cast<EffectStrength>(static_cast<int32_t>(strength) - 1);
131     EXPECT_OK(vibrator->perform_1_2(Effect::THUD, badStrength, validatePerformEffectBadInput));
132 }
133 
main(int argc,char ** argv)134 int main(int argc, char** argv) {
135     ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
136     ::testing::InitGoogleTest(&argc, argv);
137     VibratorHidlEnvironment::Instance()->init(&argc, argv);
138     int status = RUN_ALL_TESTS();
139     LOG(INFO) << "Test result = " << status;
140     return status;
141 }
142