Merge "Convert wifi hal test to use VtsHalHidlTargetTestEnvBase"
diff --git a/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp b/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
index 6c9aa18..b9f505d 100644
--- a/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
+++ b/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
@@ -25,6 +25,7 @@
 
 #include <VtsHalHidlTargetCallbackBase.h>
 #include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
 #include <queue>
 
 using ::android::hardware::bluetooth::V1_0::IBluetoothHci;
@@ -126,6 +127,23 @@
   std::chrono::steady_clock::time_point start_time_;
 };
 
+// Test environment for Bluetooth HIDL HAL.
+class BluetoothHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+ public:
+  // get the test environment singleton
+  static BluetoothHidlEnvironment* Instance() {
+    static BluetoothHidlEnvironment* instance = new BluetoothHidlEnvironment;
+    return instance;
+  }
+
+  virtual void registerTestServices() override {
+    registerTestService<IBluetoothHci>();
+  }
+
+ private:
+  BluetoothHidlEnvironment() {}
+};
+
 // The main test class for Bluetooth HIDL HAL.
 class BluetoothHidlTest : public ::testing::VtsHalHidlTargetTestBase {
  public:
@@ -251,15 +269,6 @@
   int max_sco_data_packets;
 };
 
-// A class for test environment setup (kept since this file is a template).
-class BluetoothHidlEnvironment : public ::testing::Environment {
- public:
-  virtual void SetUp() {}
-  virtual void TearDown() {}
-
- private:
-};
-
 // Receive and check status events until a COMMAND_COMPLETE is received.
 void BluetoothHidlTest::wait_for_command_complete_event(hidl_vec<uint8_t> cmd) {
   // Allow intermediate COMMAND_STATUS events
@@ -662,8 +671,9 @@
 }
 
 int main(int argc, char** argv) {
-  ::testing::AddGlobalTestEnvironment(new BluetoothHidlEnvironment);
+  ::testing::AddGlobalTestEnvironment(BluetoothHidlEnvironment::Instance());
   ::testing::InitGoogleTest(&argc, argv);
+  BluetoothHidlEnvironment::Instance()->init(&argc, argv);
   int status = RUN_ALL_TESTS();
   ALOGI("Test result = %d", status);
   return status;
diff --git a/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp b/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp
index 5672824..7492152 100644
--- a/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp
+++ b/contexthub/1.0/vts/functional/VtsHalContexthubV1_0TargetTest.cpp
@@ -17,6 +17,7 @@
 #define LOG_TAG "contexthub_hidl_hal_test"
 
 #include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
 #include <android-base/logging.h>
 #include <android/hardware/contexthub/1.0/IContexthub.h>
 #include <android/hardware/contexthub/1.0/IContexthubCallback.h>
@@ -92,12 +93,27 @@
   return hubIds;
 }
 
+// Test environment for Contexthub HIDL HAL.
+class ContexthubHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+ public:
+  // get the test environment singleton
+  static ContexthubHidlEnvironment* Instance() {
+    static ContexthubHidlEnvironment* instance = new ContexthubHidlEnvironment;
+    return instance;
+  }
+
+  virtual void registerTestServices() override { registerTestService<IContexthub>(); }
+ private:
+  ContexthubHidlEnvironment() {}
+};
+
 // Base test fixture that initializes the HAL and makes the context hub API
 // handle available
 class ContexthubHidlTestBase : public ::testing::VtsHalHidlTargetTestBase {
  public:
   virtual void SetUp() override {
-    hubApi = ::testing::VtsHalHidlTargetTestBase::getService<IContexthub>();
+    hubApi = ::testing::VtsHalHidlTargetTestBase::getService<IContexthub>(
+        ContexthubHidlEnvironment::Instance()->getServiceName<IContexthub>());
     ASSERT_NE(hubApi, nullptr);
 
     // getHubs() must be called at least once for proper initialization of the
@@ -381,7 +397,11 @@
 } // anonymous namespace
 
 int main(int argc, char **argv) {
+  ::testing::AddGlobalTestEnvironment(ContexthubHidlEnvironment::Instance());
   ::testing::InitGoogleTest(&argc, argv);
-  return RUN_ALL_TESTS();
+  ContexthubHidlEnvironment::Instance()->init(&argc, argv);
+  int status = RUN_ALL_TESTS();
+  ALOGI ("Test result = %d", status);
+  return status;
 }
 
diff --git a/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp b/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
index 91e75fe..8b3185d 100644
--- a/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
+++ b/gnss/1.0/vts/functional/VtsHalGnssV1_0TargetTest.cpp
@@ -19,6 +19,7 @@
 #include <log/log.h>
 
 #include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
 
 #include <chrono>
 #include <condition_variable>
@@ -41,6 +42,21 @@
 bool sAgpsIsPresent = false;  // if SUPL or XTRA assistance available
 bool sSignalIsWeak = false;   // if GNSS signals are weak (e.g. light indoor)
 
+// Test environment for GNSS HIDL HAL.
+class GnssHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+ public:
+  // get the test environment singleton
+  static GnssHidlEnvironment* Instance() {
+    static GnssHidlEnvironment* instance = new GnssHidlEnvironment;
+    return instance;
+  }
+
+  virtual void registerTestServices() override { registerTestService<IGnss>(); }
+
+ private:
+  GnssHidlEnvironment() {}
+};
+
 // The main test class for GNSS HAL.
 class GnssHalTest : public ::testing::VtsHalHidlTargetTestBase {
  public:
@@ -51,7 +67,8 @@
     info_called_count_ = 0;
     notify_count_ = 0;
 
-    gnss_hal_ = ::testing::VtsHalHidlTargetTestBase::getService<IGnss>();
+    gnss_hal_ = ::testing::VtsHalHidlTargetTestBase::getService<IGnss>(
+        GnssHidlEnvironment::Instance()->getServiceName<IGnss>());
     ASSERT_NE(gnss_hal_, nullptr);
 
     gnss_cb_ = new GnssCallback(*this);
@@ -449,17 +466,19 @@
 }
 
 int main(int argc, char** argv) {
+  ::testing::AddGlobalTestEnvironment(GnssHidlEnvironment::Instance());
   ::testing::InitGoogleTest(&argc, argv);
+  GnssHidlEnvironment::Instance()->init(&argc, argv);
   /*
    * These arguments not used by automated VTS testing.
    * Only for use in manual testing, when wanting to run
    * stronger tests that require the presence of GPS signal.
    */
   for (int i = 1; i < argc; i++) {
-      if (strcmp(argv[i], "-agps") == 0) {
-          sAgpsIsPresent = true;
-      } else if (strcmp(argv[i], "-weak") == 0) {
-          sSignalIsWeak = true;
+    if (strcmp(argv[i], "-agps") == 0) {
+        sAgpsIsPresent = true;
+    } else if (strcmp(argv[i], "-weak") == 0) {
+        sSignalIsWeak = true;
     }
   }
   int status = RUN_ALL_TESTS();
diff --git a/light/2.0/vts/functional/VtsHalLightV2_0TargetTest.cpp b/light/2.0/vts/functional/VtsHalLightV2_0TargetTest.cpp
index 3405422..13290d9 100644
--- a/light/2.0/vts/functional/VtsHalLightV2_0TargetTest.cpp
+++ b/light/2.0/vts/functional/VtsHalLightV2_0TargetTest.cpp
@@ -16,12 +16,13 @@
 
 #define LOG_TAG "light_hidl_hal_test"
 
+#include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
 #include <android-base/logging.h>
 #include <android/hardware/light/2.0/ILight.h>
 #include <android/hardware/light/2.0/types.h>
-#include <VtsHalHidlTargetTestBase.h>
-#include <set>
 #include <unistd.h>
+#include <set>
 
 using ::android::hardware::light::V2_0::Brightness;
 using ::android::hardware::light::V2_0::Flash;
@@ -72,10 +73,25 @@
     Type::WIFI
 };
 
+// Test environment for Light HIDL HAL.
+class LightHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+   public:
+    // get the test environment singleton
+    static LightHidlEnvironment* Instance() {
+        static LightHidlEnvironment* instance = new LightHidlEnvironment;
+        return instance;
+    }
+
+    virtual void registerTestServices() override { registerTestService<ILight>(); }
+   private:
+    LightHidlEnvironment() {}
+};
+
 class LightHidlTest : public ::testing::VtsHalHidlTargetTestBase {
 public:
     virtual void SetUp() override {
-        light = ::testing::VtsHalHidlTargetTestBase::getService<ILight>();
+        light = ::testing::VtsHalHidlTargetTestBase::getService<ILight>(
+            LightHidlEnvironment::Instance()->getServiceName<ILight>());
 
         ASSERT_NE(light, nullptr);
         LOG(INFO) << "Test is remote " << light->isRemote();
@@ -149,7 +165,9 @@
 }
 
 int main(int argc, char **argv) {
+    ::testing::AddGlobalTestEnvironment(LightHidlEnvironment::Instance());
     ::testing::InitGoogleTest(&argc, argv);
+    LightHidlEnvironment::Instance()->init(&argc, argv);
     int status = RUN_ALL_TESTS();
     LOG(INFO) << "Test result = " << status;
     return status;
diff --git a/vibrator/1.0/vts/functional/VtsHalVibratorV1_0TargetTest.cpp b/vibrator/1.0/vts/functional/VtsHalVibratorV1_0TargetTest.cpp
index f415ad5..a0e927b 100644
--- a/vibrator/1.0/vts/functional/VtsHalVibratorV1_0TargetTest.cpp
+++ b/vibrator/1.0/vts/functional/VtsHalVibratorV1_0TargetTest.cpp
@@ -19,9 +19,11 @@
 #include <android-base/logging.h>
 #include <android/hardware/vibrator/1.0/IVibrator.h>
 #include <android/hardware/vibrator/1.0/types.h>
-#include <VtsHalHidlTargetTestBase.h>
 #include <unistd.h>
 
+#include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
+
 using ::android::hardware::vibrator::V1_0::Effect;
 using ::android::hardware::vibrator::V1_0::EffectStrength;
 using ::android::hardware::vibrator::V1_0::IVibrator;
@@ -30,11 +32,27 @@
 using ::android::hardware::Void;
 using ::android::sp;
 
+// Test environment for Vibrator HIDL HAL.
+class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+ public:
+  // get the test environment singleton
+  static VibratorHidlEnvironment* Instance() {
+      static VibratorHidlEnvironment* instance = new VibratorHidlEnvironment;
+      return instance;
+  }
+
+  virtual void registerTestServices() override { registerTestService<IVibrator>(); }
+
+ private:
+  VibratorHidlEnvironment() {}
+};
+
 // The main test class for VIBRATOR HIDL HAL.
 class VibratorHidlTest : public ::testing::VtsHalHidlTargetTestBase {
  public:
   virtual void SetUp() override {
-    vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>();
+    vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>(
+        VibratorHidlEnvironment::Instance()->getServiceName<IVibrator>());
     ASSERT_NE(vibrator, nullptr);
   }
 
@@ -43,15 +61,6 @@
   sp<IVibrator> vibrator;
 };
 
-// A class for test environment setup (kept since this file is a template).
-class VibratorHidlEnvironment : public ::testing::Environment {
- public:
-  virtual void SetUp() {}
-  virtual void TearDown() {}
-
- private:
-};
-
 static void validatePerformEffect(Status status, uint32_t lengthMs) {
   ASSERT_TRUE(status == Status::OK || status == Status::UNSUPPORTED_OPERATION);
   if (status == Status::OK) {
@@ -96,8 +105,9 @@
 }
 
 int main(int argc, char **argv) {
-  ::testing::AddGlobalTestEnvironment(new VibratorHidlEnvironment);
+  ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
   ::testing::InitGoogleTest(&argc, argv);
+  VibratorHidlEnvironment::Instance()->init(&argc, argv);
   int status = RUN_ALL_TESTS();
   LOG(INFO) << "Test result = " << status;
   return status;
diff --git a/vibrator/1.1/vts/functional/VtsHalVibratorV1_1TargetTest.cpp b/vibrator/1.1/vts/functional/VtsHalVibratorV1_1TargetTest.cpp
index 35000f8..1a47fe9 100644
--- a/vibrator/1.1/vts/functional/VtsHalVibratorV1_1TargetTest.cpp
+++ b/vibrator/1.1/vts/functional/VtsHalVibratorV1_1TargetTest.cpp
@@ -17,6 +17,7 @@
 #define LOG_TAG "vibrator_hidl_hal_test"
 
 #include <VtsHalHidlTargetTestBase.h>
+#include <VtsHalHidlTargetTestEnvBase.h>
 #include <android-base/logging.h>
 #include <android/hardware/vibrator/1.1/IVibrator.h>
 #include <android/hardware/vibrator/1.1/types.h>
@@ -31,11 +32,27 @@
 using ::android::hardware::Void;
 using ::android::sp;
 
+// Test environment for Vibrator HIDL HAL.
+class VibratorHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+   public:
+    // get the test environment singleton
+    static VibratorHidlEnvironment* Instance() {
+        static VibratorHidlEnvironment* instance = new VibratorHidlEnvironment;
+        return instance;
+    }
+
+    virtual void registerTestServices() override { registerTestService<IVibrator>(); }
+
+   private:
+    VibratorHidlEnvironment() {}
+};
+
 // The main test class for VIBRATOR HIDL HAL 1.1.
 class VibratorHidlTest_1_1 : public ::testing::VtsHalHidlTargetTestBase {
    public:
     virtual void SetUp() override {
-        vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>();
+        vibrator = ::testing::VtsHalHidlTargetTestBase::getService<IVibrator>(
+            VibratorHidlEnvironment::Instance()->getServiceName<IVibrator>());
         ASSERT_NE(vibrator, nullptr);
     }
 
@@ -61,7 +78,9 @@
 }
 
 int main(int argc, char** argv) {
+    ::testing::AddGlobalTestEnvironment(VibratorHidlEnvironment::Instance());
     ::testing::InitGoogleTest(&argc, argv);
+    VibratorHidlEnvironment::Instance()->init(&argc, argv);
     int status = RUN_ALL_TESTS();
     LOG(INFO) << "Test result = " << status;
     return status;