• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace std;
23 
24 namespace testing {
25 
26 // A class for test environment setup
27 class VtsHalHidlTargetTestEnvBase : public ::testing::Environment {
28  public:
VtsHalHidlTargetTestEnvBase()29   VtsHalHidlTargetTestEnvBase() {}
30 
31   /*
32    * SetUp process, should not be overridden by the test.
33    */
34   void SetUp() final;
35 
36   /*
37    * TearDown process, should not be overridden by the test.
38    */
39   void TearDown() final;
40 
41   /*
42    * Test should override this method for any custom setup process.
43    */
HidlSetUp()44   virtual void HidlSetUp() {}
45 
46   /*
47    * Test should override this method for any custom teardown process.
48    */
HidlTearDown()49   virtual void HidlTearDown() {}
50 
51   /*
52    * Test should override this method to register hal services used in the test.
53    */
registerTestServices()54   virtual void registerTestServices() {}
55 
56   /* Parses the command line argument, extracts the vts reserved flags and
57    * leaves other options untouched.
58    * Must be called when the test environment is created is registered.
59    */
60   void init(int* argc, char** argv);
61 
62   /*
63    * Adds a hal sevice identified into registeredHalServices_.
64    */
65   template <class T>
registerTestService()66   void registerTestService() {
67     registerTestService(T::descriptor);
68   }
69 
70   /*
71    * Gets the service name for a hal instance. Returns empty string if the hal
72    * instance is unkonwn (not in hal_instances_).
73    */
74   template <class T>
getServiceName()75   string getServiceName() {
76     return getServiceName(T::descriptor);
77   }
78 
79  private:
80   /*
81    * Parses VTS specific flags, currently support two flags:
82    * --list_registered_services to print all registered service.
83    * --hal_service_instance to pass a running service instance. e.g.
84    * --hal_service_instance=android.hardware.vibrator@1.0::IVibrator/default
85    * It is possible to have mulitple --hal_service_instance options passed if
86    * mutliple hal service is used in the test.
87    * Returns true if successfully pased the given arg, false if arg is null or
88    * unknown flag.
89    */
90   bool parseVtsTestOption(const char* arg);
91 
92   /*
93    * Prints all registered sercives.
94    */
95   void listRegisteredServices();
96 
97   /*
98    * Internal method to get the service name for a hal instance.
99    */
100   string getServiceName(string instanceName);
101 
102   /*
103    * Internal method to register a HAL sevice identified with the FQName.
104    */
105   void registerTestService(string FQName);
106 
107   /*
108    * Internal method to add a hal service instance.
109    */
110   void addHalServiceInstance(string halServiceInstance);
111 
112   // Map of hal instances with their correpoding service names.
113   map<string, string> halServiceInstances_;
114   // Set of all hal services used in the test.
115   set<string> registeredHalServices_;
116   // Flag to print registered hal services and exit the process.
117   bool listService_ = false;
118   // Flag whether init is called.
119   bool inited_ = false;
120 };
121 
122 }  // namespace testing
123 
124 #endif  // __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
125