diff --git a/audio/core/all-versions/vts/functional/4.0/AudioPrimaryHidlHalTest.cpp b/audio/core/all-versions/vts/functional/4.0/AudioPrimaryHidlHalTest.cpp index 2814c63713..2703b2bf4b 100644 --- a/audio/core/all-versions/vts/functional/4.0/AudioPrimaryHidlHalTest.cpp +++ b/audio/core/all-versions/vts/functional/4.0/AudioPrimaryHidlHalTest.cpp @@ -29,18 +29,21 @@ static void waitForDeviceDestruction() { TEST_F(AudioHidlTest, OpenPrimaryDeviceUsingGetDevice) { doc::test("Calling openDevice(\"primary\") should return the primary device."); - { - Result result; - sp baseDevice; - ASSERT_OK(devicesFactory->openDevice("primary", returnIn(result, baseDevice))); - ASSERT_OK(result); - ASSERT_TRUE(baseDevice != nullptr); + struct WaitExecutor { + ~WaitExecutor() { waitForDeviceDestruction(); } + } waitExecutor; // Make sure we wait for the device destruction on exiting from the test. + Result result; + sp baseDevice; + ASSERT_OK(devicesFactory->openDevice("primary", returnIn(result, baseDevice))); + if (result != Result::OK && isPrimaryDeviceOptional()) { + GTEST_SKIP() << "No primary device on this factory"; // returns + } + ASSERT_OK(result); + ASSERT_TRUE(baseDevice != nullptr); - Return> primaryDevice = IPrimaryDevice::castFrom(baseDevice); - ASSERT_TRUE(primaryDevice.isOk()); - ASSERT_TRUE(sp(primaryDevice) != nullptr); - } // Destroy local IDevice proxy - waitForDeviceDestruction(); + Return> primaryDevice = IPrimaryDevice::castFrom(baseDevice); + ASSERT_TRUE(primaryDevice.isOk()); + ASSERT_TRUE(sp(primaryDevice) != nullptr); } ////////////////////////////////////////////////////////////////////////////// @@ -152,6 +155,7 @@ TEST_F(AudioPrimaryHidlTest, SetConnectedState) { // initial state. To workaround this, destroy the HAL at the end of this test. device.clear(); waitForDeviceDestruction(); + ASSERT_NO_FATAL_FAILURE(initPrimaryDevice()); } static void testGetDevices(IStream* stream, AudioDevice expectedDevice) { diff --git a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h index 7f8b6a0646..fb963233aa 100644 --- a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h +++ b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h @@ -215,25 +215,32 @@ TEST_F(AudioPolicyConfigTest, LoadAudioPolicyXMLConfiguration) { // Test all audio devices class AudioHidlTest : public AudioPolicyConfigTest { public: - void SetUp() override { - ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base + static void SetUpTestSuite() { + devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService( + environment->getServiceName()); + } - if (devicesFactory == nullptr) { - environment->registerTearDown([] { devicesFactory.clear(); }); - devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService( - environment->getServiceName("default")); - } - ASSERT_TRUE(devicesFactory != nullptr); - } + static void TearDownTestSuite() { devicesFactory.clear(); } + + void SetUp() override { + ASSERT_NO_FATAL_FAILURE(AudioPolicyConfigTest::SetUp()); // setup base + // Failures during SetUpTestSuite do not cause test termination. + ASSERT_TRUE(devicesFactory != nullptr); + } protected: // Cache the devicesFactory retrieval to speed up each test by ~0.5s static sp devicesFactory; + + static bool isPrimaryDeviceOptional() { + // It's OK not to have "primary" device on non-default audio HAL service. + return environment->getServiceName() != kDefaultServiceName; + } }; sp AudioHidlTest::devicesFactory; TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) { - doc::test("Test the getService (called in SetUp)"); + doc::test("Test the getService"); } TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) { @@ -257,23 +264,31 @@ TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) { // Test the primary device class AudioPrimaryHidlTest : public AudioHidlTest { public: - /** Primary HAL test are NOT thread safe. */ + static void SetUpTestSuite() { + ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUpTestSuite()); + ASSERT_NO_FATAL_FAILURE(initPrimaryDevice()); + } + + static void TearDownTestSuite() { + device.clear(); + AudioHidlTest::TearDownTestSuite(); + } + void SetUp() override { ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base - - if (device == nullptr) { - initPrimaryDevice(); - ASSERT_TRUE(device != nullptr); - environment->registerTearDown([] { device.clear(); }); + if (device == nullptr && isPrimaryDeviceOptional()) { + GTEST_SKIP() << "No primary device on this factory"; } + ASSERT_TRUE(device != nullptr); } protected: // Cache the device opening to speed up each test by ~0.5s static sp device; - private: - void initPrimaryDevice() { + static void initPrimaryDevice() { + // Failures during test suite set up do not cause test termination. + ASSERT_TRUE(devicesFactory != nullptr); Result result; #if MAJOR_VERSION == 2 sp baseDevice; @@ -292,7 +307,7 @@ class AudioPrimaryHidlTest : public AudioHidlTest { sp AudioPrimaryHidlTest::device; TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) { - doc::test("Test the openDevice (called in SetUp)"); + doc::test("Test the openDevice (called during setup)"); } TEST_F(AudioPrimaryHidlTest, Init) { @@ -692,6 +707,7 @@ class OpenStreamTest : public AudioConfigPrimaryTest, if (open) { ASSERT_OK(stream->close()); } + AudioConfigPrimaryTest::TearDown(); } protected: @@ -704,8 +720,9 @@ class OpenStreamTest : public AudioConfigPrimaryTest, ////////////////////////////// openOutputStream ////////////////////////////// class OutputStreamTest : public OpenStreamTest { - virtual void SetUp() override { + void SetUp() override { ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base + if (IsSkipped()) return; // do not attempt to use 'device' address.device = AudioDevice::OUT_DEFAULT; const AudioConfig& config = GetParam(); // TODO: test all flag combination @@ -752,8 +769,9 @@ INSTANTIATE_TEST_CASE_P( ////////////////////////////// openInputStream ////////////////////////////// class InputStreamTest : public OpenStreamTest { - virtual void SetUp() override { + void SetUp() override { ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base + if (IsSkipped()) return; // do not attempt to use 'device' address.device = AudioDevice::IN_DEFAULT; const AudioConfig& config = GetParam(); // TODO: test all supported flags and source