From 733ee1654e0260d15f158b399a1d96822a38fa33 Mon Sep 17 00:00:00 2001 From: Zhuoyao Zhang Date: Tue, 23 Jan 2018 17:39:31 -0800 Subject: [PATCH] Convert sensors HAL test to use VtsHalHidlTargetTestEnvBase Bug: 64203181 Test: make vts vts-tradefed run vts -m VtsHalSensorsV1_0Target Change-Id: I1f7e3a8adf58bbeeb0928185a61f16610dd76223 --- .../VtsHalSensorsV1_0TargetTest.cpp | 93 ++++++++++--------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp index ca3d3e4f4d..24b58a7859 100644 --- a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp +++ b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp @@ -15,8 +15,8 @@ */ #define LOG_TAG "sensors_hidl_hal_test" -#include "GrallocWrapper.h" #include +#include #include #include #include @@ -24,6 +24,7 @@ #include // for sensor type strings #include #include +#include "GrallocWrapper.h" #include #include @@ -46,63 +47,65 @@ using namespace ::android::hardware::sensors::V1_0; // Test environment for sensors class SensorsHidlTest; -class SensorsHidlEnvironment : public ::testing::Environment { - public: - // get the test environment singleton - static SensorsHidlEnvironment* Instance() { - static SensorsHidlEnvironment* instance = new SensorsHidlEnvironment; - return instance; - } +class SensorsHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { + public: + // get the test environment singleton + static SensorsHidlEnvironment* Instance() { + static SensorsHidlEnvironment* instance = new SensorsHidlEnvironment; + return instance; + } - virtual void SetUp(); - virtual void TearDown(); + virtual void HidlSetUp() override; + virtual void HidlTearDown() override; - // Get and clear all events collected so far (like "cat" shell command). - // If output is nullptr, it clears all collected events. - void catEvents(std::vector* output); + virtual void registerTestServices() override { registerTestService(); } - // set sensor event collection status - void setCollection(bool enable); + // Get and clear all events collected so far (like "cat" shell command). + // If output is nullptr, it clears all collected events. + void catEvents(std::vector* output); - private: - friend SensorsHidlTest; - // sensors hidl service - sp sensors; + // set sensor event collection status + void setCollection(bool enable); - SensorsHidlEnvironment() {} + private: + friend SensorsHidlTest; + // sensors hidl service + sp sensors; - void addEvent(const Event& ev); - void startPollingThread(); - void resetHal(); - static void pollingThread(SensorsHidlEnvironment* env, std::shared_ptr stop); + SensorsHidlEnvironment() {} - bool collectionEnabled; - std::shared_ptr stopThread; - std::thread pollThread; - std::vector events; - std::mutex events_mutex; + void addEvent(const Event& ev); + void startPollingThread(); + void resetHal(); + static void pollingThread(SensorsHidlEnvironment* env, std::shared_ptr stop); - GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironment); + bool collectionEnabled; + std::shared_ptr stopThread; + std::thread pollThread; + std::vector events; + std::mutex events_mutex; + + GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironment); }; -void SensorsHidlEnvironment::SetUp() { - resetHal(); +void SensorsHidlEnvironment::HidlSetUp() { + resetHal(); - ASSERT_NE(sensors, nullptr) << "sensors is nullptr, cannot get hidl service"; + ASSERT_NE(sensors, nullptr) << "sensors is nullptr, cannot get hidl service"; - collectionEnabled = false; - startPollingThread(); + collectionEnabled = false; + startPollingThread(); - // In case framework just stopped for test and there is sensor events in the pipe, - // wait some time for those events to be cleared to avoid them messing up the test. - std::this_thread::sleep_for(std::chrono::seconds(3)); + // In case framework just stopped for test and there is sensor events in the pipe, + // wait some time for those events to be cleared to avoid them messing up the test. + std::this_thread::sleep_for(std::chrono::seconds(3)); } -void SensorsHidlEnvironment::TearDown() { - if (stopThread) { - *stopThread = true; - } - pollThread.detach(); +void SensorsHidlEnvironment::HidlTearDown() { + if (stopThread) { + *stopThread = true; + } + pollThread.detach(); } void SensorsHidlEnvironment::resetHal() { @@ -115,7 +118,8 @@ void SensorsHidlEnvironment::resetHal() { // this do ... while is for easy error handling do { step = "getService()"; - sensors = ISensors::getService(); + sensors = ISensors::getService( + SensorsHidlEnvironment::Instance()->getServiceName()); if (sensors == nullptr) { break; } @@ -1500,6 +1504,7 @@ TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) { int main(int argc, char **argv) { ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironment::Instance()); ::testing::InitGoogleTest(&argc, argv); + SensorsHidlEnvironment::Instance()->init(&argc, argv); int status = RUN_ALL_TESTS(); ALOGI("Test result = %d", status); return status;