Create SensorsHidlEnvironmentBase

Move common parts of SensorsHidlEnvironment into a base class so they
can be reused with other versions of sensors tests.

Bug: 111070257
Test: Builds
Change-Id: I1e04e734d00308adff35b9c16de1499573a84b03
diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
index bb478ae..f8b021e 100644
--- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
+++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
@@ -18,30 +18,14 @@
 
 #include <log/log.h>
 
+#include <vector>
+
 using ::android::hardware::hidl_vec;
 using ::android::hardware::sensors::V1_0::ISensors;
 using ::android::hardware::sensors::V1_0::Result;
 using ::android::hardware::sensors::V1_0::SensorInfo;
 
-void SensorsHidlEnvironment::HidlSetUp() {
-    resetHal();
-
-    ASSERT_NE(sensors, nullptr) << "sensors is nullptr, cannot get hidl service";
-
-    collectionEnabled = false;
-    startPollingThread();
-
-    // In case framework just stopped for test and there is sensor events in the pipe,
-    // wait some time for those events to be cleared to avoid them messing up the test.
-    std::this_thread::sleep_for(std::chrono::seconds(3));
-}
-
-void SensorsHidlEnvironment::HidlTearDown() {
-    stopThread = true;
-    pollThread.detach();
-}
-
-void SensorsHidlEnvironment::resetHal() {
+bool SensorsHidlEnvironmentV1_0::resetHal() {
     // wait upto 100ms * 10 = 1s for hidl service.
     constexpr auto RETRY_DELAY = std::chrono::milliseconds(100);
 
@@ -52,7 +36,7 @@
         do {
             step = "getService()";
             sensors = ISensors::getService(
-                SensorsHidlEnvironment::Instance()->getServiceName<ISensors>());
+                SensorsHidlEnvironmentV1_0::Instance()->getServiceName<ISensors>());
             if (sensors == nullptr) {
                 break;
             }
@@ -97,7 +81,7 @@
         } while (0);
 
         if (succeed) {
-            return;
+            return true;
         }
 
         // Delay 100ms before retry, hidl service is expected to come up in short time after crash.
@@ -106,35 +90,17 @@
     }
 
     sensors = nullptr;
+    return false;
 }
 
-void SensorsHidlEnvironment::catEvents(std::vector<Event>* output) {
-    std::lock_guard<std::mutex> lock(events_mutex);
-    if (output) {
-        output->insert(output->end(), events.begin(), events.end());
-    }
-    events.clear();
-}
-
-void SensorsHidlEnvironment::setCollection(bool enable) {
-    std::lock_guard<std::mutex> lock(events_mutex);
-    collectionEnabled = enable;
-}
-
-void SensorsHidlEnvironment::addEvent(const Event& ev) {
-    std::lock_guard<std::mutex> lock(events_mutex);
-    if (collectionEnabled) {
-        events.push_back(ev);
-    }
-}
-
-void SensorsHidlEnvironment::startPollingThread() {
+void SensorsHidlEnvironmentV1_0::startPollingThread() {
     stopThread = false;
     pollThread = std::thread(pollingThread, this, std::ref(stopThread));
     events.reserve(128);
 }
 
-void SensorsHidlEnvironment::pollingThread(SensorsHidlEnvironment* env, std::atomic_bool& stop) {
+void SensorsHidlEnvironmentV1_0::pollingThread(SensorsHidlEnvironmentV1_0* env,
+                                               std::atomic_bool& stop) {
     ALOGD("polling thread start");
 
     while (!stop) {
diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
index d184613..0a9e59f 100644
--- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
+++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
@@ -17,62 +17,43 @@
 #ifndef ANDROID_SENSORS_HIDL_ENVIRONMENT_V1_0_H
 #define ANDROID_SENSORS_HIDL_ENVIRONMENT_V1_0_H
 
-#include <VtsHalHidlTargetTestEnvBase.h>
+#include "sensors-vts-utils/SensorsHidlEnvironmentBase.h"
+
 #include <android/hardware/sensors/1.0/ISensors.h>
 #include <android/hardware/sensors/1.0/types.h>
 #include <utils/StrongPointer.h>
 
 #include <atomic>
 #include <memory>
-#include <mutex>
-#include <thread>
-#include <vector>
 
 using ::android::sp;
 
 class SensorsHidlTest;
-class SensorsHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
+class SensorsHidlEnvironmentV1_0 : public SensorsHidlEnvironmentBase {
    public:
     using Event = ::android::hardware::sensors::V1_0::Event;
     // get the test environment singleton
-    static SensorsHidlEnvironment* Instance() {
-        static SensorsHidlEnvironment* instance = new SensorsHidlEnvironment();
+    static SensorsHidlEnvironmentV1_0* Instance() {
+        static SensorsHidlEnvironmentV1_0* instance = new SensorsHidlEnvironmentV1_0();
         return instance;
     }
 
-    virtual void HidlSetUp() override;
-    virtual void HidlTearDown() override;
-
     virtual void registerTestServices() override {
         registerTestService<android::hardware::sensors::V1_0::ISensors>();
     }
 
-    // Get and clear all events collected so far (like "cat" shell command).
-    // If output is nullptr, it clears all collected events.
-    void catEvents(std::vector<Event>* output);
-
-    // set sensor event collection status
-    void setCollection(bool enable);
-
    private:
     friend SensorsHidlTest;
     // sensors hidl service
     sp<android::hardware::sensors::V1_0::ISensors> sensors;
 
-    SensorsHidlEnvironment() {}
+    SensorsHidlEnvironmentV1_0() {}
 
-    void addEvent(const Event& ev);
-    void startPollingThread();
-    void resetHal();
-    static void pollingThread(SensorsHidlEnvironment* env, std::atomic_bool& stop);
+    bool resetHal() override;
+    void startPollingThread() override;
+    static void pollingThread(SensorsHidlEnvironmentV1_0* env, std::atomic_bool& stop);
 
-    bool collectionEnabled;
-    std::atomic_bool stopThread;
-    std::thread pollThread;
-    std::vector<Event> events;
-    std::mutex events_mutex;
-
-    GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironment);
+    GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentV1_0);
 };
 
 #endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_V1_0_H
\ No newline at end of file
diff --git a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp
index 8e66466..54c9497 100644
--- a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp
+++ b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp
@@ -365,9 +365,7 @@
     return S()->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb);
   }
 
-  inline sp<ISensors>& S() {
-    return SensorsHidlEnvironment::Instance()->sensors;
-  }
+  inline sp<ISensors>& S() { return SensorsHidlEnvironmentV1_0::Instance()->sensors; }
 
   inline static SensorFlagBits extractReportMode(uint64_t flag) {
     return (SensorFlagBits) (flag
@@ -453,10 +451,10 @@
         nEventLimit, timeLimitUs, clearBeforeStart);
 
   if (changeCollection) {
-    SensorsHidlEnvironment::Instance()->setCollection(true);
+      SensorsHidlEnvironmentV1_0::Instance()->setCollection(true);
   }
   if (clearBeforeStart) {
-    SensorsHidlEnvironment::Instance()->catEvents(nullptr);
+      SensorsHidlEnvironmentV1_0::Instance()->catEvents(nullptr);
   }
 
   while (timeLimitUs > 0) {
@@ -464,7 +462,7 @@
     usleep(duration);
     timeLimitUs -= duration;
 
-    SensorsHidlEnvironment::Instance()->catEvents(&events);
+    SensorsHidlEnvironmentV1_0::Instance()->catEvents(&events);
     if (events.size() >= nEventLimit) {
       break;
     }
@@ -473,7 +471,7 @@
   }
 
   if (changeCollection) {
-    SensorsHidlEnvironment::Instance()->setCollection(false);
+      SensorsHidlEnvironmentV1_0::Instance()->setCollection(false);
   }
   return events;
 }
@@ -1068,7 +1066,7 @@
   // since collection is not enabled all events will go down the drain
   usleep(batchingPeriodInNs / 1000 * 8 / 10);
 
-  SensorsHidlEnvironment::Instance()->setCollection(true);
+  SensorsHidlEnvironmentV1_0::Instance()->setCollection(true);
   // clean existing collections
   collectEvents(0 /*timeLimitUs*/, 0/*nEventLimit*/,
         true /*clearBeforeStart*/, false /*change collection*/);
@@ -1081,7 +1079,7 @@
   events = collectEvents(allowedBatchDeliverTimeNs / 1000,
         minFifoCount, false /*clearBeforeStart*/, false /*change collection*/);
 
-  SensorsHidlEnvironment::Instance()->setCollection(false);
+  SensorsHidlEnvironmentV1_0::Instance()->setCollection(false);
   ASSERT_EQ(activate(handle, 0), Result::OK);
 
   size_t nEvent = 0;
@@ -1337,11 +1335,11 @@
 }
 
 int main(int argc, char **argv) {
-  ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironment::Instance());
-  ::testing::InitGoogleTest(&argc, argv);
-  SensorsHidlEnvironment::Instance()->init(&argc, argv);
-  int status = RUN_ALL_TESTS();
-  ALOGI("Test result = %d", status);
-  return status;
+    ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironmentV1_0::Instance());
+    ::testing::InitGoogleTest(&argc, argv);
+    SensorsHidlEnvironmentV1_0::Instance()->init(&argc, argv);
+    int status = RUN_ALL_TESTS();
+    ALOGI("Test result = %d", status);
+    return status;
 }
 // vim: set ts=2 sw=2
diff --git a/sensors/common/vts/utils/Android.bp b/sensors/common/vts/utils/Android.bp
index affc56d..6c25e47 100644
--- a/sensors/common/vts/utils/Android.bp
+++ b/sensors/common/vts/utils/Android.bp
@@ -18,6 +18,7 @@
     name: "VtsHalSensorsTargetTestUtils",
     srcs: [
         "GrallocWrapper.cpp",
+        "SensorsHidlEnvironmentBase.cpp",
     ],
     export_include_dirs: [
         "include",
@@ -28,6 +29,8 @@
     static_libs: [
         "android.hardware.graphics.allocator@2.0",
         "android.hardware.graphics.mapper@2.0",
+        "android.hardware.sensors@1.0",
+        "VtsHalHidlTargetTestBase",
     ],
 }
 
diff --git a/sensors/common/vts/utils/SensorsHidlEnvironmentBase.cpp b/sensors/common/vts/utils/SensorsHidlEnvironmentBase.cpp
new file mode 100644
index 0000000..21c08d2
--- /dev/null
+++ b/sensors/common/vts/utils/SensorsHidlEnvironmentBase.cpp
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+#include "SensorsHidlEnvironmentBase.h"
+
+void SensorsHidlEnvironmentBase::HidlSetUp() {
+    ASSERT_TRUE(resetHal()) << "could not get hidl service";
+
+    collectionEnabled = false;
+    startPollingThread();
+
+    // In case framework just stopped for test and there is sensor events in the pipe,
+    // wait some time for those events to be cleared to avoid them messing up the test.
+    std::this_thread::sleep_for(std::chrono::seconds(3));
+}
+
+void SensorsHidlEnvironmentBase::HidlTearDown() {
+    stopThread = true;
+    pollThread.detach();
+}
+
+void SensorsHidlEnvironmentBase::catEvents(std::vector<Event>* output) {
+    std::lock_guard<std::mutex> lock(events_mutex);
+    if (output) {
+        output->insert(output->end(), events.begin(), events.end());
+    }
+    events.clear();
+}
+
+void SensorsHidlEnvironmentBase::setCollection(bool enable) {
+    std::lock_guard<std::mutex> lock(events_mutex);
+    collectionEnabled = enable;
+}
+
+void SensorsHidlEnvironmentBase::addEvent(const Event& ev) {
+    std::lock_guard<std::mutex> lock(events_mutex);
+    if (collectionEnabled) {
+        events.push_back(ev);
+    }
+}
diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h
new file mode 100644
index 0000000..4a6f713
--- /dev/null
+++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h
@@ -0,0 +1,60 @@
+/*
+ * 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_SENSORS_HIDL_ENVIRONMENT_BASE_H
+#define ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
+
+#include <VtsHalHidlTargetTestEnvBase.h>
+
+#include <android/hardware/sensors/1.0/types.h>
+
+#include <atomic>
+#include <memory>
+#include <mutex>
+#include <thread>
+#include <vector>
+
+class SensorsHidlEnvironmentBase : public ::testing::VtsHalHidlTargetTestEnvBase {
+   public:
+    using Event = ::android::hardware::sensors::V1_0::Event;
+    void HidlSetUp() override;
+    void HidlTearDown() override;
+
+    // Get and clear all events collected so far (like "cat" shell command).
+    // If output is nullptr, it clears all collected events.
+    void catEvents(std::vector<Event>* output);
+
+    // set sensor event collection status
+    void setCollection(bool enable);
+
+   protected:
+    SensorsHidlEnvironmentBase() {}
+
+    void addEvent(const Event& ev);
+
+    virtual void startPollingThread() = 0;
+    virtual bool resetHal() = 0;
+
+    bool collectionEnabled;
+    std::atomic_bool stopThread;
+    std::thread pollThread;
+    std::vector<Event> events;
+    std::mutex events_mutex;
+
+    GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentBase);
+};
+
+#endif  // ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
\ No newline at end of file