diff --git a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.cpp b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.cpp index 0525bdc63b..03fcc174ef 100644 --- a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.cpp +++ b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.cpp @@ -38,6 +38,13 @@ constexpr typename std::underlying_type::type asBaseType(EnumType valu constexpr size_t SensorsHidlEnvironmentV2_0::MAX_RECEIVE_BUFFER_EVENT_COUNT; +void SensorsHalDeathRecipient::serviceDied( + uint64_t /* cookie */, + const ::android::wp<::android::hidl::base::V1_0::IBase>& /* service */) { + ALOGE("Sensors HAL died (likely crashed) during test"); + FAIL() << "Sensors HAL died during test"; +} + struct SensorsCallback : ISensorsCallback { Return onDynamicSensorsConnected(const hidl_vec& /* sensorInfos */) { return Return(); @@ -56,6 +63,7 @@ bool SensorsHidlEnvironmentV2_0::resetHal() { if (mSensors == nullptr) { break; } + mSensors->linkToDeath(mDeathRecipient, 0 /* cookie */); // Initialize FMQs mEventQueue = std::make_unique(MAX_RECEIVE_BUFFER_EVENT_COUNT, diff --git a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h index 5e54530917..b0dbd90b05 100644 --- a/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h +++ b/sensors/2.0/vts/functional/SensorsHidlEnvironmentV2_0.h @@ -32,6 +32,13 @@ using ::android::sp; using ::android::hardware::MessageQueue; class SensorsHidlTest; + +class SensorsHalDeathRecipient : public ::android::hardware::hidl_death_recipient { + virtual void serviceDied( + uint64_t cookie, + const ::android::wp<::android::hidl::base::V1_0::IBase>& service) override; +}; + class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase { public: using Event = ::android::hardware::sensors::V1_0::Event; @@ -83,6 +90,11 @@ class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase { */ sp mSensors; + /** + * Monitors the HAL for crashes, triggering test failure if seen + */ + sp mDeathRecipient = new SensorsHalDeathRecipient(); + /** * Type used to simplify the creation of the Event FMQ */ diff --git a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp index 582ab79b69..c6d7cada26 100644 --- a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp +++ b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp @@ -115,7 +115,13 @@ class EventCallback : public IEventCallback { // The main test class for SENSORS HIDL HAL. class SensorsHidlTest : public SensorsHidlTestBase { - protected: + public: + virtual void SetUp() override { + // Ensure that we have a valid environment before performing tests + ASSERT_NE(getSensors(), nullptr); + } + + protected: SensorInfo defaultSensorByType(SensorType type) override; std::vector getSensorsList(); // implementation wrapper @@ -612,6 +618,9 @@ TEST_F(SensorsHidlTest, CallInitializeTwice) { std::unique_ptr newEnv = std::make_unique(); newEnv->HidlSetUp(); + if (HasFatalFailure()) { + return; // Exit early if setting up the new environment failed + } activateAllSensors(true); // Verify that the old environment does not receive any events @@ -624,8 +633,11 @@ TEST_F(SensorsHidlTest, CallInitializeTwice) { newEnv->HidlTearDown(); // Restore the test environment for future tests - SensorsHidlEnvironmentV2_0::Instance()->HidlTearDown(); - SensorsHidlEnvironmentV2_0::Instance()->HidlSetUp(); + getEnvironment()->HidlTearDown(); + getEnvironment()->HidlSetUp(); + if (HasFatalFailure()) { + return; // Exit early if resetting the environment failed + } // Ensure that the original environment is receiving events activateAllSensors(true); @@ -644,8 +656,11 @@ TEST_F(SensorsHidlTest, CleanupConnectionsOnInitialize) { // Clear the active sensor handles so they are not disabled during TearDown auto handles = mSensorHandles; mSensorHandles.clear(); - getEnvironment()->TearDown(); - getEnvironment()->SetUp(); + getEnvironment()->HidlTearDown(); + getEnvironment()->HidlSetUp(); + if (HasFatalFailure()) { + return; // Exit early if resetting the environment failed + } // Verify no events are received until sensors are re-activated ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0); @@ -1023,8 +1038,11 @@ TEST_F(SensorsHidlTest, CleanupDirectConnectionOnInitialize) { // Clear the active direct connections so they are not stopped during TearDown auto handles = mDirectChannelHandles; mDirectChannelHandles.clear(); - getEnvironment()->TearDown(); - getEnvironment()->SetUp(); + getEnvironment()->HidlTearDown(); + getEnvironment()->HidlSetUp(); + if (HasFatalFailure()) { + return; // Exit early if resetting the environment failed + } // Attempt to configure the direct channel and expect it to fail configDirectReport(