Convert VtsHalAudioV*_0TargetTest to parameterized gtest

For version 2, 4 and 5.

Bug: 150299743
Test: atest VtsHalAudioV2_0TargetTest \
  VtsHalAudioV4_0TargetTest \
  VtsHalAudioV5_0TargetTest
Change-Id: I09be1dc79b69caadd2a5468c4e6ed9c1efd76ffd
diff --git a/audio/2.0/config/Android.bp b/audio/2.0/config/Android.bp
new file mode 100644
index 0000000..65a32eb
--- /dev/null
+++ b/audio/2.0/config/Android.bp
@@ -0,0 +1,20 @@
+//
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+filegroup {
+    name: "audio_policy_configuration_V2_0",
+    srcs: ["audio_policy_configuration.xsd"],
+}
diff --git a/audio/common/all-versions/test/utility/include/utility/EnvironmentTearDown.h b/audio/common/all-versions/test/utility/include/utility/EnvironmentTearDown.h
deleted file mode 100644
index 2b240ce..0000000
--- a/audio/common/all-versions/test/utility/include/utility/EnvironmentTearDown.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H
-#define ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H
-
-#include <functional>
-#include <list>
-
-namespace android {
-namespace hardware {
-namespace audio {
-namespace common {
-namespace test {
-namespace utility {
-
-/** Register callback for static object destruction
- * Avoid destroying static objects after main return.
- * Post main return destruction leads to incorrect gtest timing measurements as
- * well as harder debuging if anything goes wrong during destruction. */
-class EnvironmentTearDown {
-  public:
-    using TearDownFunc = std::function<void()>;
-    void registerTearDown(TearDownFunc&& tearDown) { tearDowns.push_front(std::move(tearDown)); }
-
-  protected:
-    void executeAllTearDowns() {
-        // Call the tear downs in reverse order of insertion
-        for (auto& tearDown : tearDowns) {
-            tearDown();
-        }
-    }
-
-  private:
-    std::list<TearDownFunc> tearDowns;
-};
-
-}  // namespace utility
-}  // namespace test
-}  // namespace common
-}  // namespace audio
-}  // namespace hardware
-}  // namespace android
-
-#endif  // ANDROID_HARDWARE_AUDIO_COMMON_TEST_UTILITY_ENVIRONMENT_TEARDOWN_H
diff --git a/audio/core/all-versions/default/Android.bp b/audio/core/all-versions/default/Android.bp
index 6f18d1d..e11d158 100644
--- a/audio/core/all-versions/default/Android.bp
+++ b/audio/core/all-versions/default/Android.bp
@@ -18,8 +18,11 @@
 
     export_include_dirs: ["include"],
 
-    shared_libs: [
+    static_libs: [
         "libaudiofoundation",
+    ],
+
+    shared_libs: [
         "libbase",
         "libcutils",
         "libfmq",
@@ -55,7 +58,7 @@
         "-DMAJOR_VERSION=2",
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
-    ]
+    ],
 }
 
 cc_library_shared {
@@ -71,7 +74,7 @@
         "-DMAJOR_VERSION=4",
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
-    ]
+    ],
 }
 
 cc_library_shared {
@@ -86,7 +89,7 @@
         "-DMAJOR_VERSION=5",
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
-    ]
+    ],
 }
 
 cc_library_shared {
@@ -101,5 +104,5 @@
         "-DMAJOR_VERSION=6",
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
-    ]
+    ],
 }
diff --git a/audio/core/all-versions/vts/functional/2.0/EnvironmentTearDown.h b/audio/core/all-versions/vts/functional/2.0/EnvironmentTearDown.h
deleted file mode 100644
index 6373e39..0000000
--- a/audio/core/all-versions/vts/functional/2.0/EnvironmentTearDown.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_HARDWARE_AUDIO_CORE_2_0_ENVIRONMENT_TEARDOWN_H
-#define ANDROID_HARDWARE_AUDIO_CORE_2_0_ENVIRONMENT_TEARDOWN_H
-
-#include <VtsHalHidlTargetTestEnvBase.h>
-#include <gtest/gtest.h>
-
-#include "utility/EnvironmentTearDown.h"
-
-class Environment : public ::android::hardware::audio::common::test::utility::EnvironmentTearDown,
-                    public ::testing::VtsHalHidlTargetTestEnvBase {
-  private:
-    void HidlTearDown() override {
-        executeAllTearDowns();
-        VtsHalHidlTargetTestEnvBase::HidlTearDown();
-    }
-};
-
-#endif  // ANDROID_HARDWARE_AUDIO_CORE_2_0_ENVIRONMENT_TEARDOWN_H
diff --git a/audio/core/all-versions/vts/functional/6.0/AudioPrimaryHidlHalTest.cpp b/audio/core/all-versions/vts/functional/6.0/AudioPrimaryHidlHalTest.cpp
index 2afbbb8..5b58553 100644
--- a/audio/core/all-versions/vts/functional/6.0/AudioPrimaryHidlHalTest.cpp
+++ b/audio/core/all-versions/vts/functional/6.0/AudioPrimaryHidlHalTest.cpp
@@ -17,54 +17,6 @@
 // pull in all the <= 5.0 tests
 #include "5.0/AudioPrimaryHidlHalTest.cpp"
 
-const std::vector<DeviceParameter>& getDeviceParametersForFactoryTests() {
-    static std::vector<DeviceParameter> parameters = [] {
-        std::vector<DeviceParameter> result;
-        const auto factories =
-                ::android::hardware::getAllHalInstanceNames(IDevicesFactory::descriptor);
-        for (const auto& factoryName : factories) {
-            result.emplace_back(factoryName,
-                                DeviceManager::getInstance().getPrimary(factoryName) != nullptr
-                                        ? DeviceManager::kPrimaryDevice
-                                        : "");
-        }
-        return result;
-    }();
-    return parameters;
-}
-
-const std::vector<DeviceParameter>& getDeviceParametersForPrimaryDeviceTests() {
-    static std::vector<DeviceParameter> parameters = [] {
-        std::vector<DeviceParameter> result;
-        const auto primary = std::find_if(
-                getDeviceParameters().begin(), getDeviceParameters().end(), [](const auto& elem) {
-                    return std::get<PARAM_DEVICE_NAME>(elem) == DeviceManager::kPrimaryDevice;
-                });
-        if (primary != getDeviceParameters().end()) result.push_back(*primary);
-        return result;
-    }();
-    return parameters;
-}
-
-const std::vector<DeviceParameter>& getDeviceParameters() {
-    static std::vector<DeviceParameter> parameters = [] {
-        std::vector<DeviceParameter> result;
-        const auto factories =
-                ::android::hardware::getAllHalInstanceNames(IDevicesFactory::descriptor);
-        const auto devices = getCachedPolicyConfig().getModulesWithDevicesNames();
-        result.reserve(devices.size());
-        for (const auto& factoryName : factories) {
-            for (const auto& deviceName : devices) {
-                if (DeviceManager::getInstance().get(factoryName, deviceName) != nullptr) {
-                    result.emplace_back(factoryName, deviceName);
-                }
-            }
-        }
-        return result;
-    }();
-    return parameters;
-}
-
 const std::vector<DeviceConfigParameter>& getOutputDeviceConfigParameters() {
     static std::vector<DeviceConfigParameter> parameters = [] {
         std::vector<DeviceConfigParameter> result;
diff --git a/audio/core/all-versions/vts/functional/6.0/EnvironmentTearDown.h b/audio/core/all-versions/vts/functional/6.0/EnvironmentTearDown.h
deleted file mode 100644
index 593759f..0000000
--- a/audio/core/all-versions/vts/functional/6.0/EnvironmentTearDown.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_HARDWARE_AUDIO_CORE_6_0_ENVIRONMENT_TEARDOWN_H
-#define ANDROID_HARDWARE_AUDIO_CORE_6_0_ENVIRONMENT_TEARDOWN_H
-
-#include <gtest/gtest.h>
-
-#include "utility/EnvironmentTearDown.h"
-
-class Environment : public ::android::hardware::audio::common::test::utility::EnvironmentTearDown,
-                    public ::testing::Environment {
-  public:
-    void init(int* /*argc*/, char** /*argv*/) {}  // emulate VtsHalHidlTargetTestEnvBase
-  private:
-    void TearDown() override { executeAllTearDowns(); }
-};
-
-// FIXME: Will be removed while making getDeviceParameters to use the config
-static constexpr const char* kDefaultServiceName = "default";
-
-#endif  // ANDROID_HARDWARE_AUDIO_CORE_6_0_ENVIRONMENT_TEARDOWN_H
diff --git a/audio/core/all-versions/vts/functional/Android.bp b/audio/core/all-versions/vts/functional/Android.bp
index d3545c8..db52e60 100644
--- a/audio/core/all-versions/vts/functional/Android.bp
+++ b/audio/core/all-versions/vts/functional/Android.bp
@@ -19,18 +19,22 @@
     defaults: ["VtsHalTargetTestDefaults"],
     static_libs: [
         "android.hardware.audio.common.test.utility",
+        "libaudiofoundation",
         "libaudiopolicycomponents",
         "libmedia_helper",
         "libxml2",
     ],
     shared_libs: [
-        "libaudiofoundation",
+        "libbinder",
         "libfmq",
     ],
     header_libs: [
         "android.hardware.audio.common.util@all-versions",
     ],
-    test_suites: ["general-tests"],
+    test_suites: [
+        "general-tests",
+        "vts-core",
+    ],
 }
 
 cc_test {
@@ -48,6 +52,12 @@
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
     ],
+    data: [
+        ":audio_policy_configuration_V2_0",
+    ],
+    // Use test_config for vts-core suite.
+    // TODO(b/146104851): Add auto-gen rules and remove it.
+    test_config: "VtsHalAudioV2_0TargetTest.xml",
 }
 
 cc_test {
@@ -65,6 +75,12 @@
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
     ],
+    data: [
+        ":audio_policy_configuration_V4_0",
+    ],
+    // Use test_config for vts-core suite.
+    // TODO(b/146104851): Add auto-gen rules and remove it.
+    test_config: "VtsHalAudioV4_0TargetTest.xml",
 }
 
 cc_test {
@@ -82,6 +98,12 @@
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
     ],
+    data: [
+        ":audio_policy_configuration_V5_0",
+    ],
+    // Use test_config for vts-core suite.
+    // TODO(b/146104851): Add auto-gen rules and remove it.
+    test_config: "VtsHalAudioV5_0TargetTest.xml",
 }
 
 cc_test {
@@ -99,14 +121,10 @@
         "-DMINOR_VERSION=0",
         "-include common/all-versions/VersionMacro.h",
     ],
-    // Use test_config for vts-core suite.
-    // TODO(b/146104851): Add auto-gen rules and remove it.
-    test_config: "VtsHalAudioV6_0TargetTest.xml",
     data: [
         ":audio_policy_configuration_V6_0",
     ],
-    test_suites: [
-        "general-tests",
-        "vts-core",
-    ],
+    // Use test_config for vts-core suite.
+    // TODO(b/146104851): Add auto-gen rules and remove it.
+    test_config: "VtsHalAudioV6_0TargetTest.xml",
 }
diff --git a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h
index d0d39e8..da30ade 100644
--- a/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h
+++ b/audio/core/all-versions/vts/functional/AudioPrimaryHidlHalTest.h
@@ -34,10 +34,6 @@
 
 #include <hwbinder/IPCThreadState.h>
 
-#if MAJOR_VERSION <= 5
-#include <VtsHalHidlTargetTestBase.h>
-#endif
-
 #include <android-base/logging.h>
 
 #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
@@ -49,10 +45,8 @@
 #include <Serializer.h>
 #include <fmq/EventFlag.h>
 #include <fmq/MessageQueue.h>
-#if MAJOR_VERSION >= 6
 #include <hidl/GtestPrinter.h>
 #include <hidl/ServiceManagement.h>
-#endif
 
 #include <common/all-versions/VersionUtils.h>
 
@@ -61,12 +55,6 @@
 #include "utility/ReturnIn.h"
 #include "utility/ValidateXml.h"
 
-#if MAJOR_VERSION <= 5
-#include "2.0/EnvironmentTearDown.h"
-#elif MAJOR_VERSION >= 6
-#include "6.0/EnvironmentTearDown.h"
-#endif
-
 /** Provide version specific functions that are used in the generic tests */
 #if MAJOR_VERSION == 2
 #include "2.0/AudioPrimaryHidlHalUtils.h"
@@ -114,30 +102,10 @@
 static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS, Result::NOT_SUPPORTED};
 static auto invalidStateOrNotSupported = {Result::INVALID_STATE, Result::NOT_SUPPORTED};
 
-//////////////////////////////////////////////////////////////////////////////
-//////////////////////////////// Environment /////////////////////////////////
-//////////////////////////////////////////////////////////////////////////////
-
-class AudioHidlTestEnvironment : public ::Environment {
-   public:
-#if MAJOR_VERSION <= 5
-     void registerTestServices() override { registerTestService<IDevicesFactory>(); }
-#endif
-};
-
-// Instance to register global tearDown
-static AudioHidlTestEnvironment* environment;
-
 #define AUDIO_PRIMARY_HIDL_HAL_TEST
 #include "DeviceManager.h"
 
-#if MAJOR_VERSION <= 5
-using HidlTestBase = ::testing::VtsHalHidlTargetTestBase;
-#elif MAJOR_VERSION >= 6
-using HidlTestBase = ::testing::Test;
-#endif
-
-class HidlTest : public HidlTestBase {
+class HidlTest : public ::testing::Test {
   public:
     virtual ~HidlTest() = default;
 
@@ -172,15 +140,6 @@
 #define QUOTE(x) #x
 #define STRINGIFY(x) QUOTE(x)
 
-TEST(CheckConfig, audioPolicyConfigurationValidation) {
-    RecordProperty("description",
-                   "Verify that the audio policy configuration file "
-                   "is valid according to the schema");
-
-    const char* xsd = "/data/local/tmp/audio_policy_configuration_" STRINGIFY(CPP_VERSION) ".xsd";
-    EXPECT_ONE_VALID_XML_MULTIPLE_LOCATIONS(kConfigFileName, kConfigLocations, xsd);
-}
-
 struct PolicyConfigData {
     android::HwModuleCollection hwModules;
     android::DeviceVector availableOutputDevices;
@@ -252,32 +211,11 @@
 const PolicyConfig& getCachedPolicyConfig() {
     static std::unique_ptr<PolicyConfig> policyConfig = [] {
         auto config = std::make_unique<PolicyConfig>();
-        environment->registerTearDown([] { policyConfig.reset(); });
         return config;
     }();
     return *policyConfig;
 }
 
-class AudioPolicyConfigTest : public HidlTestBase {
-  public:
-    void SetUp() override {
-        ASSERT_NO_FATAL_FAILURE(HidlTestBase::SetUp());  // setup base
-        auto& policyConfig = getCachedPolicyConfig();
-        ASSERT_EQ(0, policyConfig.getStatus()) << policyConfig.getError();
-    }
-};
-
-TEST_F(AudioPolicyConfigTest, LoadAudioPolicyXMLConfiguration) {
-    doc::test("Test parsing audio_policy_configuration.xml (called in SetUp)");
-}
-
-TEST_F(AudioPolicyConfigTest, HasPrimaryModule) {
-    auto& policyConfig = getCachedPolicyConfig();
-    ASSERT_TRUE(policyConfig.getPrimaryModule() != nullptr)
-            << "Could not find primary module in configuration file: "
-            << policyConfig.getFilePath();
-}
-
 //////////////////////////////////////////////////////////////////////////////
 //////////////////// Test parameter types and definitions ////////////////////
 //////////////////////////////////////////////////////////////////////////////
@@ -288,53 +226,59 @@
 static inline std::string DeviceParameterToString(
         const ::testing::TestParamInfo<DeviceParameter>& info) {
     const auto& deviceName = std::get<PARAM_DEVICE_NAME>(info.param);
-#if MAJOR_VERSION <= 5
-    return !deviceName.empty() ? deviceName : std::to_string(info.index);
-#elif MAJOR_VERSION >= 6
     const auto factoryName =
             ::android::hardware::PrintInstanceNameToString(::testing::TestParamInfo<std::string>{
                     std::get<PARAM_FACTORY_NAME>(info.param), info.index});
     return !deviceName.empty() ? factoryName + "_" + deviceName : factoryName;
-#endif
 }
 
-#if MAJOR_VERSION <= 5
-// For V2..5 the factory is looked up using the instance name passed
-// in the environment, only one factory is returned. This is because the VTS
-// framework will call the test for each instance. Only the primary device of
-// the default service factory can be tested.
-
-// Return a pair of <"default", "primary"> or <[non-default name], "">
-// This is used to parametrize device factory tests.
-// The device name is used to indicate whether IPrimaryDevice is required.
-const std::vector<DeviceParameter>& getDeviceParametersForFactoryTests() {
-    static std::vector<DeviceParameter> parameters = {
-            {environment->getServiceName<IDevicesFactory>(),
-             environment->getServiceName<IDevicesFactory>() == kDefaultServiceName
-                     ? DeviceManager::kPrimaryDevice
-                     : ""}};
-    return parameters;
-}
-// Return a pair of <"default", "primary"> or nothing.
-// This is used to parametrize primary device tests.
-const std::vector<DeviceParameter>& getDeviceParametersForPrimaryDeviceTests() {
-    static std::vector<DeviceParameter> parameters =
-            !std::get<PARAM_DEVICE_NAME>(*getDeviceParametersForFactoryTests().begin()).empty()
-                    ? getDeviceParametersForFactoryTests()
-                    : std::vector<DeviceParameter>{};
-    return parameters;
-}
-// In V2..5 device tests must only test the primary device.
-// No device tests are executed for non-primary devices.
 const std::vector<DeviceParameter>& getDeviceParameters() {
-    return getDeviceParametersForPrimaryDeviceTests();
+    static std::vector<DeviceParameter> parameters = [] {
+        std::vector<DeviceParameter> result;
+        const auto factories =
+                ::android::hardware::getAllHalInstanceNames(IDevicesFactory::descriptor);
+        const auto devices = getCachedPolicyConfig().getModulesWithDevicesNames();
+        result.reserve(devices.size());
+        for (const auto& factoryName : factories) {
+            for (const auto& deviceName : devices) {
+                if (DeviceManager::getInstance().get(factoryName, deviceName) != nullptr) {
+                    result.emplace_back(factoryName, deviceName);
+                }
+            }
+        }
+        return result;
+    }();
+    return parameters;
 }
-#elif MAJOR_VERSION >= 6
-// For V6 and above these functions are implemented in 6.0/AudioPrimaryHidlHalTest.cpp
-const std::vector<DeviceParameter>& getDeviceParametersForFactoryTests();
-const std::vector<DeviceParameter>& getDeviceParametersForPrimaryDeviceTests();
-const std::vector<DeviceParameter>& getDeviceParameters();
-#endif
+
+const std::vector<DeviceParameter>& getDeviceParametersForFactoryTests() {
+    static std::vector<DeviceParameter> parameters = [] {
+        std::vector<DeviceParameter> result;
+        const auto factories =
+                ::android::hardware::getAllHalInstanceNames(IDevicesFactory::descriptor);
+        for (const auto& factoryName : factories) {
+            result.emplace_back(factoryName,
+                                DeviceManager::getInstance().getPrimary(factoryName) != nullptr
+                                        ? DeviceManager::kPrimaryDevice
+                                        : "");
+        }
+        return result;
+    }();
+    return parameters;
+}
+
+const std::vector<DeviceParameter>& getDeviceParametersForPrimaryDeviceTests() {
+    static std::vector<DeviceParameter> parameters = [] {
+        std::vector<DeviceParameter> result;
+        const auto primary = std::find_if(
+                getDeviceParameters().begin(), getDeviceParameters().end(), [](const auto& elem) {
+                    return std::get<PARAM_DEVICE_NAME>(elem) == DeviceManager::kPrimaryDevice;
+                });
+        if (primary != getDeviceParameters().end()) result.push_back(*primary);
+        return result;
+    }();
+    return parameters;
+}
 
 class AudioHidlTestWithDeviceParameter : public HidlTest,
                                          public ::testing::WithParamInterface<DeviceParameter> {
@@ -347,6 +291,44 @@
     }
 };
 
+TEST(CheckConfig, audioPolicyConfigurationValidation) {
+    auto deviceParameters = getDeviceParametersForFactoryTests();
+    if (deviceParameters.size() == 0) {
+        GTEST_SKIP() << "Skipping audioPolicyConfigurationValidation because no device parameter "
+                        "is found.";
+    }
+    RecordProperty("description",
+                   "Verify that the audio policy configuration file "
+                   "is valid according to the schema");
+
+    const char* xsd = "/data/local/tmp/audio_policy_configuration_" STRINGIFY(CPP_VERSION) ".xsd";
+    EXPECT_ONE_VALID_XML_MULTIPLE_LOCATIONS(kConfigFileName, kConfigLocations, xsd);
+}
+
+class AudioPolicyConfigTest : public AudioHidlTestWithDeviceParameter {
+  public:
+    void SetUp() override {
+        ASSERT_NO_FATAL_FAILURE(AudioHidlTestWithDeviceParameter::SetUp());  // setup base
+        auto& policyConfig = getCachedPolicyConfig();
+        ASSERT_EQ(0, policyConfig.getStatus()) << policyConfig.getError();
+    }
+};
+
+TEST_P(AudioPolicyConfigTest, LoadAudioPolicyXMLConfiguration) {
+    doc::test("Test parsing audio_policy_configuration.xml (called in SetUp)");
+}
+
+TEST_P(AudioPolicyConfigTest, HasPrimaryModule) {
+    auto& policyConfig = getCachedPolicyConfig();
+    ASSERT_TRUE(policyConfig.getPrimaryModule() != nullptr)
+            << "Could not find primary module in configuration file: "
+            << policyConfig.getFilePath();
+}
+
+INSTANTIATE_TEST_CASE_P(AudioHidl, AudioPolicyConfigTest,
+                        ::testing::ValuesIn(getDeviceParametersForFactoryTests()),
+                        &DeviceParameterToString);
+
 //////////////////////////////////////////////////////////////////////////////
 ////////////////////// getService audio_devices_factory //////////////////////
 //////////////////////////////////////////////////////////////////////////////
@@ -1561,20 +1543,3 @@
     testAccessors<OPTIONAL>("HAC", Initial{false}, {true}, &IPrimaryDevice::setHacEnabled,
                             &IPrimaryDevice::getHacEnabled);
 }
-
-//////////////////////////////////////////////////////////////////////////////
-//////////////////// Clean caches on global tear down ////////////////////////
-//////////////////////////////////////////////////////////////////////////////
-
-int main(int argc, char** argv) {
-    environment = new AudioHidlTestEnvironment;
-    // For V2..5 it's critical to initialize environment before GTest.
-    // The environment parses the service name from the command line,
-    // then it can be used in GTest parameter generators which are
-    // initialized during the call to InitGoogleTest.
-    environment->init(&argc, argv);
-    ::testing::AddGlobalTestEnvironment(environment);
-    ::testing::InitGoogleTest(&argc, argv);
-    int status = RUN_ALL_TESTS();
-    return status;
-}
diff --git a/audio/core/all-versions/vts/functional/DeviceManager.h b/audio/core/all-versions/vts/functional/DeviceManager.h
index cb6584d..0c0727f 100644
--- a/audio/core/all-versions/vts/functional/DeviceManager.h
+++ b/audio/core/all-versions/vts/functional/DeviceManager.h
@@ -31,10 +31,6 @@
         auto existing = instances.find(name);
         if (existing != instances.end()) return existing->second;
         auto [inserted, _] = instances.emplace(name, Derived::createInterfaceInstance(name));
-        if (inserted->second) {
-            environment->registerTearDown(
-                    [name]() { (void)Derived::getInstance().reset(name, false); });
-        }
         return inserted->second;
     }
 
@@ -75,11 +71,7 @@
         return instance;
     }
     static sp<IDevicesFactory> createInterfaceInstance(const std::string& name) {
-#if MAJOR_VERSION <= 5
-        return ::testing::VtsHalHidlTargetTestBase::getService<IDevicesFactory>(name);
-#elif MAJOR_VERSION >= 6
         return IDevicesFactory::getService(name);
-#endif
     }
 };
 
diff --git a/audio/core/all-versions/vts/functional/VtsHalAudioV2_0TargetTest.xml b/audio/core/all-versions/vts/functional/VtsHalAudioV2_0TargetTest.xml
new file mode 100644
index 0000000..67fcdb6
--- /dev/null
+++ b/audio/core/all-versions/vts/functional/VtsHalAudioV2_0TargetTest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs VtsHalAudioV2_0TargetTest.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="run-command" value="stop"/>
+        <option name="run-command" value="setprop vts.native_server.on 1"/>
+        <option name="teardown-command" value="start"/>
+        <option name="teardown-command" value="setprop vts.native_server.on 0"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push-file" key="VtsHalAudioV2_0TargetTest" value="/data/local/tmp/VtsHalAudioV2_0TargetTest" />
+        <option name="push-file" key="audio_policy_configuration.xsd" value="/data/local/tmp/audio_policy_configuration_V2_0.xsd" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="VtsHalAudioV2_0TargetTest" />
+    </test>
+</configuration>
diff --git a/audio/core/all-versions/vts/functional/VtsHalAudioV4_0TargetTest.xml b/audio/core/all-versions/vts/functional/VtsHalAudioV4_0TargetTest.xml
new file mode 100644
index 0000000..2084060
--- /dev/null
+++ b/audio/core/all-versions/vts/functional/VtsHalAudioV4_0TargetTest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs VtsHalAudioV4_0TargetTest.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="run-command" value="stop"/>
+        <option name="run-command" value="setprop vts.native_server.on 1"/>
+        <option name="teardown-command" value="start"/>
+        <option name="teardown-command" value="setprop vts.native_server.on 0"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push-file" key="VtsHalAudioV4_0TargetTest" value="/data/local/tmp/VtsHalAudioV4_0TargetTest" />
+        <option name="push-file" key="audio_policy_configuration_V4_0.xsd" value="/data/local/tmp/audio_policy_configuration_V4_0.xsd" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="VtsHalAudioV4_0TargetTest" />
+    </test>
+</configuration>
diff --git a/audio/core/all-versions/vts/functional/VtsHalAudioV5_0TargetTest.xml b/audio/core/all-versions/vts/functional/VtsHalAudioV5_0TargetTest.xml
new file mode 100644
index 0000000..8b01e41
--- /dev/null
+++ b/audio/core/all-versions/vts/functional/VtsHalAudioV5_0TargetTest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs VtsHalAudioV5_0TargetTest.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="run-command" value="stop"/>
+        <option name="run-command" value="setprop vts.native_server.on 1"/>
+        <option name="teardown-command" value="start"/>
+        <option name="teardown-command" value="setprop vts.native_server.on 0"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <option name="push-file" key="VtsHalAudioV5_0TargetTest" value="/data/local/tmp/VtsHalAudioV5_0TargetTest" />
+        <option name="push-file" key="audio_policy_configuration_V5_0.xsd" value="/data/local/tmp/audio_policy_configuration_V5_0.xsd" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/local/tmp" />
+        <option name="module-name" value="VtsHalAudioV5_0TargetTest" />
+    </test>
+</configuration>