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 <VtsHalHidlTargetTestEnvBase.h>
21 #include <gtest/gtest.h>
22 #include <hidl/HidlSupport.h>
23 #include <log/log.h>
24 #include <utils/RefBase.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    */
SetUp()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    */
TearDown()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    */
HalHidlSetUp()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    */
HalHidlTearDown()75   virtual void HalHidlTearDown() {}
76 
77   /*
78    * Return test case info as string.
79    */
getTestCaseInfo()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                           bool getStub = false) {
93     return T::getService(serviceName,
94                          getStub || VtsHalHidlTargetTestBase::VtsGetStub());
95   }
96 
97   /*
98    * Call interface's getService with the service name stored in the test
99    * environment and use passthrough mode if set from host.
100    */
101   template <class T>
getService(VtsHalHidlTargetTestEnvBase * testEnv)102   static sp <T> getService(VtsHalHidlTargetTestEnvBase* testEnv) {
103     return T::getService(testEnv->getServiceName<T>(),
104                          VtsHalHidlTargetTestBase::VtsGetStub());
105   }
106 
107 private:
108   /*
109    * Decide bool val for getStub option. Will read environment variable set
110    * from host. If environment variable is not set, return will default to
111    * false.
112    */
113   static bool VtsGetStub();
114 
115   /*
116    * Return test suite name as string.
117    */
118   string getTestSuiteName() const;
119 
120   /*
121    * Return test case name as string.
122    */
123   string getTestCaseName() const;
124 };
125 
126 }  // namespace testing
127 
128 #endif  // __VTS_HAL_HIDL_TARGET_TEST_BASE_H
129