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_ENV_BASE_H
     18 #define __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
     19 
     20 #include <gtest/gtest.h>
     21 
     22 static constexpr const char* kDefaultServiceName = "default";
     23 
     24 using namespace std;
     25 
     26 namespace testing {
     27 
     28 // Enum class indicates the required combination mode for registered services.
     29 enum HalServiceCombMode {
     30   // Get the full permutation of all the registered service instances.
     31   // E.g. Hal service s1 with instances (n1, n2) and s2 with instances (n3, n4),
     32   // Return combination (s1/n1, s2/n3), (s1/n1, s2/n4), (s1/n2, s2/n3),
     33   // (s1/n2, s2/n4).
     34   FULL_PERMUTATION = 0,
     35   // Get the registered service instances with the same service name.
     36   // E.g. Hal service s1 with instances (n1, n2) and s2 with instances (n1, n2),
     37   // Return combination (s1/n1, s2/n1), (s1/n2, s2/n2).
     38   NAME_MATCH,
     39   // Do not return the service instance combinations. This is used in cases when
     40   // the test logic specifically handles the testing instances. E.g. drm tests.
     41   NO_COMBINATION,
     42 };
     43 
     44 // A class for test environment setup
     45 class VtsHalHidlTargetTestEnvBase : public ::testing::Environment {
     46  public:
     47   VtsHalHidlTargetTestEnvBase() {}
     48 
     49   /*
     50    * SetUp process, should not be overridden by the test.
     51    */
     52   void SetUp() final;
     53 
     54   /*
     55    * TearDown process, should not be overridden by the test.
     56    */
     57   void TearDown() final;
     58 
     59   /*
     60    * Test should override this method for any custom setup process.
     61    */
     62   virtual void HidlSetUp() {}
     63 
     64   /*
     65    * Test should override this method for any custom teardown process.
     66    */
     67   virtual void HidlTearDown() {}
     68 
     69   /*
     70    * Test should override this method to register hal services used in the test.
     71    */
     72   virtual void registerTestServices() {}
     73 
     74   /* Parses the command line argument, extracts the vts reserved flags and
     75    * leaves other options untouched.
     76    * Must be called when the test environment is created is registered.
     77    */
     78   void init(int* argc, char** argv);
     79 
     80   /*
     81    * Adds a hal sevice identified into registeredHalServices_.
     82    */
     83   template <class T>
     84   void registerTestService() {
     85     registerTestService(T::descriptor);
     86   }
     87 
     88   /*
     89    * Gets the service name for a hal instance. Returns defaultName if the hal
     90    * instance is unkonwn (not in hal_instances_).
     91    */
     92   template <class T>
     93   string getServiceName(const string& defaultName = kDefaultServiceName) {
     94     return getServiceName(T::descriptor, defaultName);
     95   }
     96 
     97   void setServiceCombMode(HalServiceCombMode mode) { mode_ = mode; }
     98 
     99  private:
    100   /*
    101    * Parses VTS specific flags, currently support two flags:
    102    * --list_registered_services to print all registered service.
    103    * --hal_service_instance to pass a running service instance. e.g.
    104    * --hal_service_instance=android.hardware.vibrator (at) 1.0::IVibrator/default
    105    * It is possible to have mulitple --hal_service_instance options passed if
    106    * mutliple hal service is used in the test.
    107    * Returns true if successfully pased the given arg, false if arg is null or
    108    * unknown flag.
    109    */
    110   bool parseVtsTestOption(const char* arg);
    111 
    112   /*
    113    * Prints all registered sercives.
    114    */
    115   void listRegisteredServices();
    116 
    117   /*
    118    * Internal method to get the service name for a hal instance.
    119    */
    120   string getServiceName(const string& instanceName, const string& defaultName);
    121 
    122   /*
    123    * Internal method to register a HAL sevice identified with the FQName.
    124    */
    125   void registerTestService(const string& FQName);
    126 
    127   /*
    128    * Internal method to add a hal service instance.
    129    */
    130   void addHalServiceInstance(const string& halServiceInstance);
    131 
    132   // Map of hal instances with their correpoding service names.
    133   map<string, string> halServiceInstances_;
    134   // Set of all hal services used in the test.
    135   set<string> registeredHalServices_;
    136   // Flag to print registered hal services and exit the process.
    137   bool listService_ = false;
    138   // Flag whether init is called.
    139   bool inited_ = false;
    140   // Required combination mode for hal service instances.
    141   HalServiceCombMode mode_ = HalServiceCombMode::FULL_PERMUTATION;
    142 };
    143 
    144 }  // namespace testing
    145 
    146 #endif  // __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
    147