audio: Skip tests if audio HAL service lacks "primary" device

Non-default audio service (e.g. MSD) is allowed not to have
a "primary" device. In this case tests that require it can be
skipped.

Manage static objects of a test suite in a canonical gtest way
using SetUp/TearDownTestSuite.

Bug: 139321356
Bug: 141433379
Test: vts-tradefed run commandAndExit vts -m VtsHalAudioV5_0Target
      on a device with "msd" audio HAL module

Change-Id: I08a11b2caa9a913e812f1c203007070e4e68c44e
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 022f75e..fa4fa9d 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 @@
 
 TEST_F(AudioHidlTest, OpenPrimaryDeviceUsingGetDevice) {
     doc::test("Calling openDevice(\"primary\") should return the primary device.");
-    {
-        Result result;
-        sp<IDevice> 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<IDevice> 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<sp<IPrimaryDevice>> primaryDevice = IPrimaryDevice::castFrom(baseDevice);
-        ASSERT_TRUE(primaryDevice.isOk());
-        ASSERT_TRUE(sp<IPrimaryDevice>(primaryDevice) != nullptr);
-    }  // Destroy local IDevice proxy
-    waitForDeviceDestruction();
+    Return<sp<IPrimaryDevice>> primaryDevice = IPrimaryDevice::castFrom(baseDevice);
+    ASSERT_TRUE(primaryDevice.isOk());
+    ASSERT_TRUE(sp<IPrimaryDevice>(primaryDevice) != nullptr);
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -148,6 +151,7 @@
     // 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 7f8b6a0..fb96323 100644
--- a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h
+++ b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h
@@ -215,25 +215,32 @@
 // 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<IDevicesFactory>(
+                 environment->getServiceName<IDevicesFactory>());
+     }
 
-        if (devicesFactory == nullptr) {
-            environment->registerTearDown([] { devicesFactory.clear(); });
-            devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<IDevicesFactory>(
-                environment->getServiceName<IDevicesFactory>("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<IDevicesFactory> devicesFactory;
+
+    static bool isPrimaryDeviceOptional() {
+        // It's OK not to have "primary" device on non-default audio HAL service.
+        return environment->getServiceName<IDevicesFactory>() != kDefaultServiceName;
+    }
 };
 sp<IDevicesFactory> 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 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<IPrimaryDevice> 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<IDevice> baseDevice;
@@ -292,7 +307,7 @@
 sp<IPrimaryDevice> 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 @@
         if (open) {
             ASSERT_OK(stream->close());
         }
+        AudioConfigPrimaryTest::TearDown();
     }
 
    protected:
@@ -704,8 +720,9 @@
 ////////////////////////////// openOutputStream //////////////////////////////
 
 class OutputStreamTest : public OpenStreamTest<IStreamOut> {
-    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 @@
 ////////////////////////////// openInputStream //////////////////////////////
 
 class InputStreamTest : public OpenStreamTest<IStreamIn> {
-    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