xref: /aosp_15_r20/test/vts/vts_hal_hidl_target/VtsHalHidlTargetTestEnvBase.h (revision 9a74111979c139a065a9a7e4d45972320c5732c7)
1*9a741119SAndroid Build Coastguard Worker /*
2*9a741119SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*9a741119SAndroid Build Coastguard Worker  *
4*9a741119SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*9a741119SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*9a741119SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*9a741119SAndroid Build Coastguard Worker  *
8*9a741119SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*9a741119SAndroid Build Coastguard Worker  *
10*9a741119SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*9a741119SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*9a741119SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9a741119SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*9a741119SAndroid Build Coastguard Worker  * limitations under the License.
15*9a741119SAndroid Build Coastguard Worker  */
16*9a741119SAndroid Build Coastguard Worker 
17*9a741119SAndroid Build Coastguard Worker #ifndef __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
18*9a741119SAndroid Build Coastguard Worker #define __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
19*9a741119SAndroid Build Coastguard Worker 
20*9a741119SAndroid Build Coastguard Worker #include <gtest/gtest.h>
21*9a741119SAndroid Build Coastguard Worker 
22*9a741119SAndroid Build Coastguard Worker static constexpr const char* kDefaultServiceName = "default";
23*9a741119SAndroid Build Coastguard Worker 
24*9a741119SAndroid Build Coastguard Worker using namespace std;
25*9a741119SAndroid Build Coastguard Worker 
26*9a741119SAndroid Build Coastguard Worker namespace testing {
27*9a741119SAndroid Build Coastguard Worker 
28*9a741119SAndroid Build Coastguard Worker // Enum class indicates the required combination mode for registered services.
29*9a741119SAndroid Build Coastguard Worker enum HalServiceCombMode {
30*9a741119SAndroid Build Coastguard Worker   // Get the full permutation of all the registered service instances.
31*9a741119SAndroid Build Coastguard Worker   // E.g. Hal service s1 with instances (n1, n2) and s2 with instances (n3, n4),
32*9a741119SAndroid Build Coastguard Worker   // Return combination (s1/n1, s2/n3), (s1/n1, s2/n4), (s1/n2, s2/n3),
33*9a741119SAndroid Build Coastguard Worker   // (s1/n2, s2/n4).
34*9a741119SAndroid Build Coastguard Worker   FULL_PERMUTATION = 0,
35*9a741119SAndroid Build Coastguard Worker   // Get the registered service instances with the same service name.
36*9a741119SAndroid Build Coastguard Worker   // E.g. Hal service s1 with instances (n1, n2) and s2 with instances (n1, n2),
37*9a741119SAndroid Build Coastguard Worker   // Return combination (s1/n1, s2/n1), (s1/n2, s2/n2).
38*9a741119SAndroid Build Coastguard Worker   NAME_MATCH,
39*9a741119SAndroid Build Coastguard Worker   // Do not return the service instance combinations. This is used in cases when
40*9a741119SAndroid Build Coastguard Worker   // the test logic specifically handles the testing instances. E.g. drm tests.
41*9a741119SAndroid Build Coastguard Worker   NO_COMBINATION,
42*9a741119SAndroid Build Coastguard Worker };
43*9a741119SAndroid Build Coastguard Worker 
44*9a741119SAndroid Build Coastguard Worker // A class for test environment setup
45*9a741119SAndroid Build Coastguard Worker class VtsHalHidlTargetTestEnvBase : public ::testing::Environment {
46*9a741119SAndroid Build Coastguard Worker  public:
VtsHalHidlTargetTestEnvBase()47*9a741119SAndroid Build Coastguard Worker   VtsHalHidlTargetTestEnvBase() {}
48*9a741119SAndroid Build Coastguard Worker 
49*9a741119SAndroid Build Coastguard Worker   /*
50*9a741119SAndroid Build Coastguard Worker    * SetUp process, should not be overridden by the test.
51*9a741119SAndroid Build Coastguard Worker    */
52*9a741119SAndroid Build Coastguard Worker   void SetUp() final;
53*9a741119SAndroid Build Coastguard Worker 
54*9a741119SAndroid Build Coastguard Worker   /*
55*9a741119SAndroid Build Coastguard Worker    * TearDown process, should not be overridden by the test.
56*9a741119SAndroid Build Coastguard Worker    */
57*9a741119SAndroid Build Coastguard Worker   void TearDown() final;
58*9a741119SAndroid Build Coastguard Worker 
59*9a741119SAndroid Build Coastguard Worker   /*
60*9a741119SAndroid Build Coastguard Worker    * Test should override this method for any custom setup process.
61*9a741119SAndroid Build Coastguard Worker    */
HidlSetUp()62*9a741119SAndroid Build Coastguard Worker   virtual void HidlSetUp() {}
63*9a741119SAndroid Build Coastguard Worker 
64*9a741119SAndroid Build Coastguard Worker   /*
65*9a741119SAndroid Build Coastguard Worker    * Test should override this method for any custom teardown process.
66*9a741119SAndroid Build Coastguard Worker    */
HidlTearDown()67*9a741119SAndroid Build Coastguard Worker   virtual void HidlTearDown() {}
68*9a741119SAndroid Build Coastguard Worker 
69*9a741119SAndroid Build Coastguard Worker   /*
70*9a741119SAndroid Build Coastguard Worker    * Test should override this method to register hal services used in the test.
71*9a741119SAndroid Build Coastguard Worker    */
registerTestServices()72*9a741119SAndroid Build Coastguard Worker   virtual void registerTestServices() {}
73*9a741119SAndroid Build Coastguard Worker 
74*9a741119SAndroid Build Coastguard Worker   /* Parses the command line argument, extracts the vts reserved flags and
75*9a741119SAndroid Build Coastguard Worker    * leaves other options untouched.
76*9a741119SAndroid Build Coastguard Worker    * Must be called when the test environment is created is registered.
77*9a741119SAndroid Build Coastguard Worker    */
78*9a741119SAndroid Build Coastguard Worker   void init(int* argc, char** argv);
79*9a741119SAndroid Build Coastguard Worker 
80*9a741119SAndroid Build Coastguard Worker   /*
81*9a741119SAndroid Build Coastguard Worker    * Adds a hal sevice identified into registeredHalServices_.
82*9a741119SAndroid Build Coastguard Worker    */
83*9a741119SAndroid Build Coastguard Worker   template <class T>
registerTestService()84*9a741119SAndroid Build Coastguard Worker   void registerTestService() {
85*9a741119SAndroid Build Coastguard Worker     registerTestService(T::descriptor);
86*9a741119SAndroid Build Coastguard Worker   }
87*9a741119SAndroid Build Coastguard Worker 
88*9a741119SAndroid Build Coastguard Worker   /*
89*9a741119SAndroid Build Coastguard Worker    * Gets the service name for a hal instance. Returns defaultName if the hal
90*9a741119SAndroid Build Coastguard Worker    * instance is unkonwn (not in hal_instances_).
91*9a741119SAndroid Build Coastguard Worker    */
92*9a741119SAndroid Build Coastguard Worker   template <class T>
93*9a741119SAndroid Build Coastguard Worker   string getServiceName(const string& defaultName = kDefaultServiceName) {
94*9a741119SAndroid Build Coastguard Worker     return getServiceName(T::descriptor, defaultName);
95*9a741119SAndroid Build Coastguard Worker   }
96*9a741119SAndroid Build Coastguard Worker 
setServiceCombMode(HalServiceCombMode mode)97*9a741119SAndroid Build Coastguard Worker   void setServiceCombMode(HalServiceCombMode mode) { mode_ = mode; }
98*9a741119SAndroid Build Coastguard Worker 
99*9a741119SAndroid Build Coastguard Worker  private:
100*9a741119SAndroid Build Coastguard Worker   /*
101*9a741119SAndroid Build Coastguard Worker    * Parses VTS specific flags, currently support two flags:
102*9a741119SAndroid Build Coastguard Worker    * --list_registered_services to print all registered service.
103*9a741119SAndroid Build Coastguard Worker    * --hal_service_instance to pass a running service instance. e.g.
104*9a741119SAndroid Build Coastguard Worker    * [email protected]::IVibrator/default
105*9a741119SAndroid Build Coastguard Worker    * It is possible to have mulitple --hal_service_instance options passed if
106*9a741119SAndroid Build Coastguard Worker    * mutliple hal service is used in the test.
107*9a741119SAndroid Build Coastguard Worker    * Returns true if successfully pased the given arg, false if arg is null or
108*9a741119SAndroid Build Coastguard Worker    * unknown flag.
109*9a741119SAndroid Build Coastguard Worker    */
110*9a741119SAndroid Build Coastguard Worker   bool parseVtsTestOption(const char* arg);
111*9a741119SAndroid Build Coastguard Worker 
112*9a741119SAndroid Build Coastguard Worker   /*
113*9a741119SAndroid Build Coastguard Worker    * Prints all registered sercives.
114*9a741119SAndroid Build Coastguard Worker    */
115*9a741119SAndroid Build Coastguard Worker   void listRegisteredServices();
116*9a741119SAndroid Build Coastguard Worker 
117*9a741119SAndroid Build Coastguard Worker   /*
118*9a741119SAndroid Build Coastguard Worker    * Internal method to get the service name for a hal instance.
119*9a741119SAndroid Build Coastguard Worker    */
120*9a741119SAndroid Build Coastguard Worker   string getServiceName(const string& instanceName, const string& defaultName);
121*9a741119SAndroid Build Coastguard Worker 
122*9a741119SAndroid Build Coastguard Worker   /*
123*9a741119SAndroid Build Coastguard Worker    * Internal method to register a HAL sevice identified with the FQName.
124*9a741119SAndroid Build Coastguard Worker    */
125*9a741119SAndroid Build Coastguard Worker   void registerTestService(const string& FQName);
126*9a741119SAndroid Build Coastguard Worker 
127*9a741119SAndroid Build Coastguard Worker   /*
128*9a741119SAndroid Build Coastguard Worker    * Internal method to add a hal service instance.
129*9a741119SAndroid Build Coastguard Worker    */
130*9a741119SAndroid Build Coastguard Worker   void addHalServiceInstance(const string& halServiceInstance);
131*9a741119SAndroid Build Coastguard Worker 
132*9a741119SAndroid Build Coastguard Worker   /*
133*9a741119SAndroid Build Coastguard Worker    * Helper method to check whether the given halServiceInstance is well
134*9a741119SAndroid Build Coastguard Worker    * formatted.
135*9a741119SAndroid Build Coastguard Worker    */
136*9a741119SAndroid Build Coastguard Worker   bool isValidInstance(const string& halServiceInstance);
137*9a741119SAndroid Build Coastguard Worker 
138*9a741119SAndroid Build Coastguard Worker   // Map of hal instances with their correpoding service names.
139*9a741119SAndroid Build Coastguard Worker   map<string, string> halServiceInstances_;
140*9a741119SAndroid Build Coastguard Worker   // Set of all hal services used in the test.
141*9a741119SAndroid Build Coastguard Worker   set<string> registeredHalServices_;
142*9a741119SAndroid Build Coastguard Worker   // Flag to print registered hal services and exit the process.
143*9a741119SAndroid Build Coastguard Worker   bool listService_ = false;
144*9a741119SAndroid Build Coastguard Worker   // Flag whether init is called.
145*9a741119SAndroid Build Coastguard Worker   bool inited_ = false;
146*9a741119SAndroid Build Coastguard Worker   // Required combination mode for hal service instances.
147*9a741119SAndroid Build Coastguard Worker   HalServiceCombMode mode_ = HalServiceCombMode::FULL_PERMUTATION;
148*9a741119SAndroid Build Coastguard Worker };
149*9a741119SAndroid Build Coastguard Worker 
150*9a741119SAndroid Build Coastguard Worker }  // namespace testing
151*9a741119SAndroid Build Coastguard Worker 
152*9a741119SAndroid Build Coastguard Worker #endif  // __VTS_HAL_HIDL_TARGET_TEST_ENV_BASE_H
153