Home | History | Annotate | Download | only in functional
      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 <VtsHalHidlTargetTestBase.h>
     23 #include <unistd.h>
     24 
     25 using ::android::hardware::vibrator::V1_0::Effect;
     26 using ::android::hardware::vibrator::V1_0::EffectStrength;
     27 using ::android::hardware::vibrator::V1_0::IVibrator;
     28 using ::android::hardware::vibrator::V1_0::Status;
     29 using ::android::hardware::Return;
     30 using ::android::hardware::Void;
     31 using ::android::sp;
     32 
     33 // The main test class for VIBRATOR HIDL HAL.
     34 class VibratorHidlTest : public ::testing::VtsHalHidlTargetTestBase {
     35  public:
     36   virtual void SetUp() override {
     37     vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>();
     38     ASSERT_NE(vibrator, nullptr);
     39   }
     40 
     41   virtual void TearDown() override {}
     42 
     43   sp<IVibrator> vibrator;
     44 };
     45 
     46 // A class for test environment setup (kept since this file is a template).
     47 class VibratorHidlEnvironment : public ::testing::Environment {
     48  public:
     49   virtual void SetUp() {}
     50   virtual void TearDown() {}
     51 
     52  private:
     53 };
     54 
     55 static void validatePerformEffect(Status status, uint32_t lengthMs) {
     56   ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
     57   if (status == Status::OK) {
     58       ASSERT_GT(lengthMs, static_cast<uint32_t>(0));
     59   } else {
     60       ASSERT_EQ(lengthMs, static_cast<uint32_t>(0));
     61   }
     62 }
     63 
     64 TEST_F(VibratorHidlTest, OnThenOffBeforeTimeout) {
     65   EXPECT_EQ(Status::OK, vibrator->on(2000));
     66   sleep(1);
     67   EXPECT_EQ(Status::OK, vibrator->off());
     68 }
     69 
     70 TEST_F(VibratorHidlTest, PerformEffect) {
     71   vibrator->perform(Effect::CLICK, EffectStrength::MEDIUM, validatePerformEffect);
     72   vibrator->perform(Effect::DOUBLE_CLICK, EffectStrength::LIGHT, validatePerformEffect);
     73 }
     74 
     75 TEST_F(VibratorHidlTest, ChangeVibrationalAmplitude) {
     76   if (vibrator->supportsAmplitudeControl()) {
     77     EXPECT_EQ(Status::OK, vibrator->setAmplitude(1));
     78     EXPECT_EQ(Status::OK, vibrator->on(2000));
     79     EXPECT_EQ(Status::OK, vibrator->setAmplitude(128));
     80     sleep(1);
     81     EXPECT_EQ(Status::OK, vibrator->setAmplitude(255));
     82     sleep(1);
     83   }
     84 }
     85 
     86 TEST_F(VibratorHidlTest, AmplitudeOutsideRangeFails) {
     87   if (vibrator->supportsAmplitudeControl()) {
     88     EXPECT_EQ(Status::BAD_VALUE, vibrator->setAmplitude(0));
     89   }
     90 }
     91 
     92 TEST_F(VibratorHidlTest, SetAmplitudeReturnUnsupportedOperationIfNotSupported) {
     93   if (!vibrator->supportsAmplitudeControl()) {
     94     EXPECT_EQ(Status::UNSUPPORTED_OPERATION, vibrator->setAmplitude(1));
     95   }
     96 }
     97 
     98 int main(int argc, char **argv) {
     99   ::testing::AddGlobalTestEnvironment(new VibratorHidlEnvironment);
    100   ::testing::InitGoogleTest(&argc, argv);
    101   int status = RUN_ALL_TESTS();
    102   LOG(INFO) << "Test result = " << status;
    103   return status;
    104 }
    105