From 1d329e633a317bb02f95c37516c722b15c582270 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 16 Oct 2018 11:04:16 -0700 Subject: [PATCH] Add CallInitializeTwice Test Adds a test to Sensors 2.0 to ensure that if the initialize function is called twice, then the FMQs used in the second call receive events. Bug: 115969174 Test: New test passes (SensorsHidlTest#CallInitializeTwice) Change-Id: I21a9307397eca1f2be93a826db89d24002e848c0 --- .../functional/SensorsHidlEnvironmentV2_0.h | 2 +- .../VtsHalSensorsV2_0TargetTest.cpp | 51 +++++++++++++++++++ .../common/vts/utils/SensorsHidlTestBase.cpp | 16 ++++-- .../sensors-vts-utils/SensorsHidlTestBase.h | 4 ++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h index 724192376f..5e54530917 100644 --- a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h +++ b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h @@ -47,7 +47,7 @@ class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase { virtual void HidlTearDown() override; - private: + protected: friend SensorsHidlTest; SensorsHidlEnvironmentV2_0() : mEventQueueFlag(nullptr) {} diff --git a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp index 7c6f01065c..94cd6e59ca 100644 --- a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp +++ b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp @@ -79,6 +79,9 @@ class SensorsHidlTest : public SensorsHidlTestBase { SensorsHidlEnvironmentBase* getEnvironment() override { return SensorsHidlEnvironmentV2_0::Instance(); } + + // Helper functions + void activateAllSensors(bool enable); }; Return SensorsHidlTest::activate(int32_t sensorHandle, bool enabled) { @@ -437,6 +440,54 @@ TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) { RateLevel::VERY_FAST, NullChecker()); } +void SensorsHidlTest::activateAllSensors(bool enable) { + for (const SensorInfo& sensorInfo : getSensorsList()) { + if (isValidType(sensorInfo.type)) { + batch(sensorInfo.sensorHandle, sensorInfo.minDelay, 0 /* maxReportLatencyNs */); + activate(sensorInfo.sensorHandle, enable); + } + } +} + +// Test that if initialize is called twice, then the HAL writes events to the FMQs from the second +// call to the function. +TEST_F(SensorsHidlTest, CallInitializeTwice) { + // Create a helper class so that a second environment is able to be instantiated + class SensorsHidlEnvironmentTest : public SensorsHidlEnvironmentV2_0 {}; + + if (getSensorsList().size() == 0) { + // No sensors + return; + } + + constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s + constexpr int32_t kNumEvents = 1; + + // Create a new environment that calls initialize() + std::unique_ptr newEnv = + std::make_unique(); + newEnv->HidlSetUp(); + + activateAllSensors(true); + // Verify that the old environment does not receive any events + ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0); + // Verify that the new event queue receives sensor events + ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get()).size(), kNumEvents); + activateAllSensors(false); + + // Cleanup the test environment + newEnv->HidlTearDown(); + + // Restore the test environment for future tests + SensorsHidlEnvironmentV2_0::Instance()->HidlTearDown(); + SensorsHidlEnvironmentV2_0::Instance()->HidlSetUp(); + + // Ensure that the original environment is receiving events + activateAllSensors(true); + ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); + activateAllSensors(false); +} + int main(int argc, char** argv) { ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironmentV2_0::Instance()); ::testing::InitGoogleTest(&argc, argv); diff --git a/sensors/common/vts/utils/SensorsHidlTestBase.cpp b/sensors/common/vts/utils/SensorsHidlTestBase.cpp index b72fdfd535..18549dfb17 100644 --- a/sensors/common/vts/utils/SensorsHidlTestBase.cpp +++ b/sensors/common/vts/utils/SensorsHidlTestBase.cpp @@ -40,6 +40,14 @@ const Vec3NormChecker SensorsHidlTestBase::sGyroNormChecker( std::vector SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart, bool changeCollection) { + return collectEvents(timeLimitUs, nEventLimit, getEnvironment(), clearBeforeStart, + changeCollection); +} + +std::vector SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit, + SensorsHidlEnvironmentBase* environment, + bool clearBeforeStart, + bool changeCollection) { std::vector events; constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms @@ -47,10 +55,10 @@ std::vector SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si clearBeforeStart); if (changeCollection) { - getEnvironment()->setCollection(true); + environment->setCollection(true); } if (clearBeforeStart) { - getEnvironment()->catEvents(nullptr); + environment->catEvents(nullptr); } while (timeLimitUs > 0) { @@ -58,7 +66,7 @@ std::vector SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si usleep(duration); timeLimitUs -= duration; - getEnvironment()->catEvents(&events); + environment->catEvents(&events); if (events.size() >= nEventLimit) { break; } @@ -67,7 +75,7 @@ std::vector SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si } if (changeCollection) { - getEnvironment()->setCollection(false); + environment->setCollection(false); } return events; } diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h index f4b259f629..6fd9a2b5de 100644 --- a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h +++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h @@ -85,6 +85,10 @@ class SensorsHidlTestBase : public ::testing::VtsHalHidlTargetTestBase { std::vector collectEvents(useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart = true, bool changeCollection = true); + static std::vector collectEvents(useconds_t timeLimitUs, size_t nEventLimit, + SensorsHidlEnvironmentBase* environment, + bool clearBeforeStart = true, + bool changeCollection = true); inline static SensorFlagBits extractReportMode(uint64_t flag) { return (SensorFlagBits)(flag & ((uint64_t)SensorFlagBits::CONTINUOUS_MODE |