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 <VtsHalHidlTargetTestBase.h>
25 #include <VtsHalHidlTargetTestEnvBase.h>
26
27 using ::android::hardware::vibrator::V1_0::Effect;
28 using ::android::hardware::vibrator::V1_0::EffectStrength;
29 using ::android::hardware::vibrator::V1_0::IVibrator;
30 using ::android::hardware::vibrator::V1_0::Status;
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.
51 class VibratorHidlTest : 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 } else {
69 ASSERT_EQ(lengthMs, static_cast<uint32_t>(0));
70 }
71 }
72
TEST_F(VibratorHidlTest,OnThenOffBeforeTimeout)73 TEST_F(VibratorHidlTest, OnThenOffBeforeTimeout) {
74 EXPECT_EQ(Status::OK, vibrator->on(2000));
75 sleep(1);
76 EXPECT_EQ(Status::OK, vibrator->off());
77 }
78
TEST_F(VibratorHidlTest,PerformEffect)79 TEST_F(VibratorHidlTest, PerformEffect) {
80 vibrator->perform(Effect::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
81 vibrator->perform(Effect::DOUBLE_CLICK, EffectStrength::LIGHT, validatePerformEffect);
82 }
83
TEST_F(VibratorHidlTest,ChangeVibrationalAmplitude)84 TEST_F(VibratorHidlTest, ChangeVibrationalAmplitude) {
85 if (vibrator->supportsAmplitudeControl()) {
86 EXPECT_EQ(Status::OK, vibrator->setAmplitude(1));
87 EXPECT_EQ(Status::OK, vibrator->on(2000));
88 EXPECT_EQ(Status::OK, vibrator->setAmplitude(128));
89 sleep(1);
90 EXPECT_EQ(Status::OK, vibrator->setAmplitude(255));
91 sleep(1);
92 }
93 }
94
TEST_F(VibratorHidlTest,AmplitudeOutsideRangeFails)95 TEST_F(VibratorHidlTest, AmplitudeOutsideRangeFails) {
96 if (vibrator->supportsAmplitudeControl()) {
97 EXPECT_EQ(Status::BAD_VALUE, vibrator->setAmplitude(0));
98 }
99 }
100
TEST_F(VibratorHidlTest,SetAmplitudeReturnUnsupportedOperationIfNotSupported)101 TEST_F(VibratorHidlTest, SetAmplitudeReturnUnsupportedOperationIfNotSupported) {
102 if (!vibrator->supportsAmplitudeControl()) {
103 EXPECT_EQ(Status::UNSUPPORTED_OPERATION, vibrator->setAmplitude(1));
104 }
105 }
106
main(int argc,char ** argv)107 int main(int argc, char **argv) {
108 ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
109 ::testing::InitGoogleTest(&argc, argv);
110 VibratorHidlEnvironment::Instance()->init(&argc, argv);
111 int status = RUN_ALL_TESTS();
112 LOG(INFO) << "Test result = " << status;
113 return status;
114 }
115