From e10b1d6ea52d8d934c8c85d835025307afe7dc85 Mon Sep 17 00:00:00 2001 From: Dan Shi Date: Thu, 12 Dec 2019 10:12:52 -0800 Subject: [PATCH] Convert VtsHalContexthubV1_0Target to be parameterized test Removing dependency from VTS infrastructure so that test can run standalone, for instance with atest/TEST_MAPPING. Once this is done for every test, VTS can use the core testing infra. Bug: 142397658 Test: atest VtsHalContexthubV1_0TargetTest Change-Id: Ib2da09dc2898f4f0503fa7c5003a19421fbeef91 --- contexthub/1.0/vts/functional/Android.bp | 6 +- contexthub/1.0/vts/functional/OWNERS | 2 +- .../VtsHalContexthubV1_0TargetTest.cpp | 150 ++++++++---------- 3 files changed, 72 insertions(+), 86 deletions(-) diff --git a/contexthub/1.0/vts/functional/Android.bp b/contexthub/1.0/vts/functional/Android.bp index aef03409ad..9e99c334bc 100644 --- a/contexthub/1.0/vts/functional/Android.bp +++ b/contexthub/1.0/vts/functional/Android.bp @@ -19,6 +19,8 @@ cc_test { defaults: ["VtsHalTargetTestDefaults"], srcs: ["VtsHalContexthubV1_0TargetTest.cpp"], static_libs: ["android.hardware.contexthub@1.0"], - test_suites: ["general-tests"], + test_suites: [ + "general-tests", + "vts-core", + ], } - diff --git a/contexthub/1.0/vts/functional/OWNERS b/contexthub/1.0/vts/functional/OWNERS index 045cc4e938..161b2f069d 100644 --- a/contexthub/1.0/vts/functional/OWNERS +++ b/contexthub/1.0/vts/functional/OWNERS @@ -4,5 +4,5 @@ bduddie@google.com stange@google.com #VTS team -yim@google.com +dshi@google.com trong@google.com diff --git a/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp b/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp index 629477aa99..a1d173bc32 100644 --- a/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp +++ b/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp @@ -16,13 +16,14 @@ #define LOG_TAG "contexthub_hidl_hal_test" -#include -#include #include #include #include #include #include +#include +#include +#include #include #include @@ -76,69 +77,44 @@ hidl_vec getHubsSync(sp hubApi) { } // Gets a list of valid hub IDs in the system -std::vector getHubIds() { - static std::vector hubIds; +std::vector getHubIds(const std::string& service_name) { + std::vector hubIds; - if (hubIds.size() == 0) { - sp hubApi = ::testing::VtsHalHidlTargetTestBase::getService(); + sp hubApi = IContexthub::getService(service_name); if (hubApi != nullptr) { - for (const ContextHub& hub : getHubsSync(hubApi)) { - hubIds.push_back(hub.hubId); - } + for (const ContextHub& hub : getHubsSync(hubApi)) { + hubIds.push_back(std::to_string(hub.hubId)); + } } - } - ALOGD("Running tests against all %zu reported hubs", hubIds.size()); - return hubIds; + ALOGD("Running tests against all %zu reported hubs for service %s", hubIds.size(), + service_name.c_str()); + return hubIds; } -// Test environment for Contexthub HIDL HAL. -class ContexthubHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { - public: - // get the test environment singleton - static ContexthubHidlEnvironment* Instance() { - static ContexthubHidlEnvironment* instance = new ContexthubHidlEnvironment; - return instance; - } +// Test fixture parameterized by hub ID, initializes the HAL and makes the context hub API handle +// available. +class ContexthubHidlTest : public ::testing::TestWithParam> { + public: + virtual void SetUp() override { + hubApi = IContexthub::getService(std::get<0>(GetParam())); + ASSERT_NE(hubApi, nullptr); - virtual void registerTestServices() override { registerTestService(); } - private: - ContexthubHidlEnvironment() {} -}; + // getHubs() must be called at least once for proper initialization of the + // HAL implementation + getHubsSync(hubApi); + } -// Base test fixture that initializes the HAL and makes the context hub API -// handle available -class ContexthubHidlTestBase : public ::testing::VtsHalHidlTargetTestBase { - public: - virtual void SetUp() override { - hubApi = ::testing::VtsHalHidlTargetTestBase::getService( - ContexthubHidlEnvironment::Instance()->getServiceName()); - ASSERT_NE(hubApi, nullptr); + uint32_t getHubId() { return std::stoi(std::get<1>(GetParam())); } - // getHubs() must be called at least once for proper initialization of the - // HAL implementation - getHubsSync(hubApi); - } + Result registerCallback(sp cb) { + Result result = hubApi->registerCallback(getHubId(), cb); + ALOGD("Registered callback, result %" PRIu32, result); + return result; + } - virtual void TearDown() override {} - - sp hubApi; -}; - -// Test fixture parameterized by hub ID -class ContexthubHidlTest : public ContexthubHidlTestBase, - public ::testing::WithParamInterface { - public: - uint32_t getHubId() { - return GetParam(); - } - - Result registerCallback(sp cb) { - Result result = hubApi->registerCallback(getHubId(), cb); - ALOGD("Registered callback, result %" PRIu32, result); - return result; - } + sp hubApi; }; // Base callback implementation that just logs all callbacks by default @@ -202,24 +178,24 @@ bool waitForCallback( } // Ensures that the metadata reported in getHubs() is sane -TEST_F(ContexthubHidlTestBase, TestGetHubs) { - hidl_vec hubs = getHubsSync(hubApi); - ALOGD("System reports %zu hubs", hubs.size()); +TEST_P(ContexthubHidlTest, TestGetHubs) { + hidl_vec hubs = getHubsSync(hubApi); + ALOGD("System reports %zu hubs", hubs.size()); - for (const ContextHub& hub : hubs) { - ALOGD("Checking hub ID %" PRIu32, hub.hubId); + for (const ContextHub& hub : hubs) { + ALOGD("Checking hub ID %" PRIu32, hub.hubId); - EXPECT_FALSE(hub.name.empty()); - EXPECT_FALSE(hub.vendor.empty()); - EXPECT_FALSE(hub.toolchain.empty()); - EXPECT_GT(hub.peakMips, 0); - EXPECT_GE(hub.stoppedPowerDrawMw, 0); - EXPECT_GE(hub.sleepPowerDrawMw, 0); - EXPECT_GT(hub.peakPowerDrawMw, 0); + EXPECT_FALSE(hub.name.empty()); + EXPECT_FALSE(hub.vendor.empty()); + EXPECT_FALSE(hub.toolchain.empty()); + EXPECT_GT(hub.peakMips, 0); + EXPECT_GE(hub.stoppedPowerDrawMw, 0); + EXPECT_GE(hub.sleepPowerDrawMw, 0); + EXPECT_GT(hub.peakPowerDrawMw, 0); - // Minimum 128 byte MTU as required by CHRE API v1.0 - EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128)); - } + // Minimum 128 byte MTU as required by CHRE API v1.0 + EXPECT_GE(hub.maxSupportedMsgLen, UINT32_C(128)); + } } TEST_P(ContexthubHidlTest, TestRegisterCallback) { @@ -388,20 +364,28 @@ TEST_P(ContexthubTxnTest, TestDisableNonexistentNanoApp) { cb->promise.get_future())); } -// Parameterize all SingleContexthubTest tests against each valid hub ID -INSTANTIATE_TEST_CASE_P(HubIdSpecificTests, ContexthubHidlTest, - ::testing::ValuesIn(getHubIds())); -INSTANTIATE_TEST_CASE_P(HubIdSpecificTests, ContexthubTxnTest, - ::testing::ValuesIn(getHubIds())); +// Return the test parameters of a vecter of tuples for all IContexthub services and each of its hub +// id: +static std::vector> get_parameters() { + std::vector> parameters; + std::vector service_names = + android::hardware::getAllHalInstanceNames(IContexthub::descriptor); + for (const std::string& service_name : service_names) { + std::vector ids = getHubIds(service_name); + for (const std::string& id : ids) { + parameters.push_back(std::make_tuple(service_name, id)); + } + } -} // anonymous namespace - -int main(int argc, char **argv) { - ::testing::AddGlobalTestEnvironment(ContexthubHidlEnvironment::Instance()); - ::testing::InitGoogleTest(&argc, argv); - ContexthubHidlEnvironment::Instance()->init(&argc, argv); - int status = RUN_ALL_TESTS(); - ALOGI ("Test result = %d", status); - return status; + return parameters; } +static std::vector> kTestParameters = get_parameters(); + +INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubHidlTest, testing::ValuesIn(kTestParameters), + android::hardware::PrintInstanceTupleNameToString<>); + +INSTANTIATE_TEST_SUITE_P(HubIdSpecificTests, ContexthubTxnTest, testing::ValuesIn(kTestParameters), + android::hardware::PrintInstanceTupleNameToString<>); + +} // anonymous namespace