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 <VtsHalHidlTargetTestBase.h>
20 #include <VtsHalHidlTargetTestEnvBase.h>
21 #include <android-base/logging.h>
22 #include <android/hardware/vibrator/1.1/IVibrator.h>
23 #include <android/hardware/vibrator/1.1/types.h>
24 #include <unistd.h>
25
26 using ::android::hardware::vibrator::V1_0::Effect;
27 using ::android::hardware::vibrator::V1_0::EffectStrength;
28 using ::android::hardware::vibrator::V1_0::Status;
29 using ::android::hardware::vibrator::V1_1::Effect_1_1;
30 using ::android::hardware::vibrator::V1_1::IVibrator;
31 using ::android::hardware::Return;
32 using ::android::hardware::Void;
33 using ::android::sp;
34
35 // Test environment for Vibrator HIDL HAL.
36 class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
37 public:
38 // get the test environment singleton
Instance()39 static VibratorHidlEnvironment* Instance() {
40 static VibratorHidlEnvironment* instance = new VibratorHidlEnvironment;
41 return instance;
42 }
43
registerTestServices()44 virtual void registerTestServices() override { registerTestService<IVibrator>(); }
45
46 private:
VibratorHidlEnvironment()47 VibratorHidlEnvironment() {}
48 };
49
50 // The main test class for VIBRATOR HIDL HAL 1.1.
51 class VibratorHidlTest_1_1 : public ::testing::VtsHalHidlTargetTestBase {
52 public:
SetUp()53 virtual void SetUp() override {
54 vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>(
55 VibratorHidlEnvironment::Instance()->getServiceName<IVibrator>());
56 ASSERT_NE(vibrator, nullptr);
57 }
58
TearDown()59 virtual void TearDown() override {}
60
61 sp<IVibrator> vibrator;
62 };
63
validatePerformEffect(Status status,uint32_t lengthMs)64 static void validatePerformEffect(Status status, uint32_t lengthMs) {
65 ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
66 if (status == Status::OK) {
67 ASSERT_GT(lengthMs, static_cast<uint32_t>(0))
68 << "Effects that return OK must return a non-zero duration";
69 } else {
70 ASSERT_EQ(lengthMs, static_cast<uint32_t>(0))
71 << "Effects that return UNSUPPORTED_OPERATION must have a duration of zero";
72 }
73 }
74
TEST_F(VibratorHidlTest_1_1,PerformEffect_1_1)75 TEST_F(VibratorHidlTest_1_1, PerformEffect_1_1) {
76 vibrator->perform_1_1(Effect_1_1::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
77 vibrator->perform_1_1(Effect_1_1::TICK, EffectStrength::STRONG, validatePerformEffect);
78 }
79
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81 ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
82 ::testing::InitGoogleTest(&argc, argv);
83 VibratorHidlEnvironment::Instance()->init(&argc, argv);
84 int status = RUN_ALL_TESTS();
85 LOG(INFO) << "Test result = " << status;
86 return status;
87 }
88