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 25 #define VTS_HAL_HIDL_GET_STUB "VTS_HAL_HIDL_GET_STUB" 26 27 namespace testing { 28 29 using ::android::sp; 30 31 // VTS target side test template 32 class VtsHalHidlTargetTestBase : public ::testing::Test { 33 public: SetUp()34 virtual void SetUp() override { 35 ALOGI("[Test Case] %s.%s BEGIN", getTestSuiteName().c_str(), 36 getTestCaseName().c_str()); 37 ::std::string testCaseInfo = getTestCaseInfo(); 38 if (testCaseInfo.size()) { 39 ALOGD("Test case info: %s", testCaseInfo.c_str()); 40 } 41 } 42 TearDown()43 virtual void TearDown() override { 44 ALOGI("[Test Case] %s.%s END", getTestSuiteName().c_str(), 45 getTestCaseName().c_str()); 46 } 47 48 /* 49 * Return test suite name as string. 50 */ getTestSuiteName()51 ::std::string getTestSuiteName() { 52 return ::testing::UnitTest::GetInstance()->current_test_info()->name(); 53 } 54 55 /* 56 * Return test case name as string. 57 */ getTestCaseName()58 ::std::string getTestCaseName() { 59 return ::testing::UnitTest::GetInstance() 60 ->current_test_info() 61 ->test_case_name(); 62 } 63 64 /* 65 * Return test case info as string. 66 */ getTestCaseInfo()67 ::std::string getTestCaseInfo() { return ""; } 68 69 /* 70 * Get value of system property as string on target 71 */ 72 static ::std::string PropertyGet(const char* name); 73 74 /* 75 * Call interface's getService and use passthrough mode if set from host. 76 */ 77 template <class T> 78 static sp<T> getService(const ::std::string& serviceName = "default") { 79 return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub()); 80 } 81 82 /* 83 * Call interface's getService and use passthrough mode if set from host. 84 */ 85 template <class T> getService(const char serviceName[])86 static sp<T> getService(const char serviceName[]) { 87 return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub()); 88 } 89 90 /* 91 * Call interface's getService and use passthrough mode if set from host. 92 */ 93 template <class T> getService(const::android::hardware::hidl_string & serviceName)94 static sp<T> getService(const ::android::hardware::hidl_string& serviceName) { 95 return T::getService(serviceName, VtsHalHidlTargetTestBase::VtsGetStub()); 96 } 97 98 private: 99 /* 100 * Decide bool val for getStub option. Will read environment variable set 101 * from host. If environment variable is not set, return will default to 102 * false. 103 */ 104 static bool VtsGetStub(); 105 }; 106 107 } // namespace testing 108 109 #endif // __VTS_HAL_HIDL_TARGET_TEST_BASE_H 110