Home | History | Annotate | Download | only in vts_hal_hidl_target
      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 #ifndef __VTS_HAL_HIDL_TARGET_TEST_BASE_H
     18 #define __VTS_HAL_HIDL_TARGET_TEST_BASE_H
     19 
     20 #include <gtest/gtest.h>
     21 #include <hidl/HidlSupport.h>
     22 #include <utils/Log.h>
     23 #include <utils/RefBase.h>
     24 #include <VtsHalHidlTargetTestEnvBase.h>
     25 
     26 #define VTS_HAL_HIDL_GET_STUB "VTS_HAL_HIDL_GET_STUB"
     27 
     28 using namespace std;
     29 
     30 namespace testing {
     31 
     32 using ::android::sp;
     33 
     34 // VTS target side test template
     35 class VtsHalHidlTargetTestBase : public ::testing::Test {
     36  public:
     37   /*
     38    * Internal test class setup function.
     39    */
     40   virtual void SetUp() override {
     41     ALOGI("[Test Case] %s.%s BEGIN", getTestSuiteName().c_str(),
     42           getTestCaseName().c_str());
     43     string testCaseInfo = getTestCaseInfo();
     44     if (testCaseInfo.size()) {
     45       ALOGD("Test case info: %s", testCaseInfo.c_str());
     46     }
     47 
     48     HalHidlSetUp();
     49   }
     50 
     51   /*
     52    * Internal test class tear-down function.
     53    */
     54   virtual void TearDown() override {
     55     HalHidlTearDown();
     56 
     57     ALOGI("[Test Case] %s.%s END", getTestSuiteName().c_str(),
     58           getTestCaseName().c_str());
     59     string testCaseInfo = getTestCaseInfo();
     60     if (testCaseInfo.size()) {
     61       ALOGD("Test case info: %s", testCaseInfo.c_str());
     62     }
     63   }
     64 
     65   /*
     66    * HAL HIDL test class setup function.
     67    * Will be called in the end of SetUp() function.
     68    */
     69   virtual void HalHidlSetUp() {}
     70 
     71   /*
     72    * HAL HIDL test class tear-down function.
     73    * Will be called in the beginning of TearDown() function.
     74    */
     75   virtual void HalHidlTearDown() {}
     76 
     77   /*
     78    * Return test case info as string.
     79    */
     80   virtual string getTestCaseInfo() const { return ""; }
     81 
     82   /*
     83    * Get value of system property as string on target
     84    */
     85   static string PropertyGet(const char* name);
     86 
     87   /*
     88    * Call interface's getService and use passthrough mode if set from host.
     89    */
     90   template <class T>
     91   static sp<T> getService(const string& serviceName = "default") {
     92     return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub());
     93   }
     94 
     95   /*
     96    * Call interface's getService and use passthrough mode if set from host.
     97    */
     98   template <class T>
     99   static sp<T> getService(const char serviceName[]) {
    100     return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub());
    101   }
    102 
    103   /*
    104    * Call interface's getService and use passthrough mode if set from host.
    105    */
    106   template <class T>
    107   static sp<T> getService(const ::android::hardware::hidl_string& serviceName) {
    108     return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub());
    109   }
    110 
    111   /*
    112    * Call interface's getService with the service name stored in the test
    113    * environment and use passthrough mode if set from host.
    114    */
    115   template <class T>
    116   static sp <T> getService(VtsHalHidlTargetTestEnvBase* testEnv) {
    117     return T::getService(testEnv->getServiceName<T>(),
    118                          VtsHalHidlTargetTestBase::VtsGetStub());
    119   }
    120 
    121 private:
    122   /*
    123    * Decide bool val for getStub option. Will read environment variable set
    124    * from host. If environment variable is not set, return will default to
    125    * false.
    126    */
    127   static bool VtsGetStub();
    128 
    129   /*
    130    * Return test suite name as string.
    131    */
    132   string getTestSuiteName() const;
    133 
    134   /*
    135    * Return test case name as string.
    136    */
    137   string getTestCaseName() const;
    138 };
    139 
    140 }  // namespace testing
    141 
    142 #endif  // __VTS_HAL_HIDL_TARGET_TEST_BASE_H
    143