diff options
author | Scott Lobdell <slobdell@google.com> | 2022-01-14 21:43:37 +0000 |
---|---|---|
committer | Scott Lobdell <slobdell@google.com> | 2022-01-14 21:59:57 +0000 |
commit | 78c23e4f5f9e8b61b8bace9a2b4cb691073ef906 (patch) | |
tree | 0aad971c2e86b1aae91ded1764bfb5832a4ef7f3 /sensors | |
parent | b05840bdecec2929591de61ab2f10fea13e4ec74 (diff) | |
parent | 92dd7d8b66930e5a813bbce25b779dc2c8e062e8 (diff) |
Merge TP1A.211116.001
Change-Id: Iba540947aa34084b7ef3210f93332bb757b4bb43
Diffstat (limited to 'sensors')
13 files changed, 192 insertions, 217 deletions
diff --git a/sensors/1.0/vts/functional/OWNERS b/sensors/1.0/vts/functional/OWNERS index 892da1548c..e20125b501 100644 --- a/sensors/1.0/vts/functional/OWNERS +++ b/sensors/1.0/vts/functional/OWNERS @@ -1,8 +1,2 @@ -# Sensors team -arthuri@google.com -bduddie@google.com -stange@google.com - -# VTS team -trong@google.com -yim@google.com +# Bug component: 62965 +include ../../../common/vts/OWNERS diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp index aca6961e0d..c69f32a76d 100644 --- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp +++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp @@ -25,7 +25,7 @@ using ::android::hardware::sensors::V1_0::ISensors; using ::android::hardware::sensors::V1_0::Result; using ::android::hardware::sensors::V1_0::SensorInfo; -void SensorsHidlEnvironmentV1_0::HidlTearDown() { +void SensorsHidlEnvironmentV1_0::TearDown() { mStopThread = true; if (mPollThread.joinable()) { mPollThread.detach(); diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h index 168777d8be..b802e6c4b1 100644 --- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h +++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h @@ -17,7 +17,7 @@ #ifndef ANDROID_SENSORS_HIDL_ENVIRONMENT_V1_0_H #define ANDROID_SENSORS_HIDL_ENVIRONMENT_V1_0_H -#include "sensors-vts-utils/SensorsHidlEnvironmentBase.h" +#include "sensors-vts-utils/SensorsVtsEnvironmentBase.h" #include <android/hardware/sensors/1.0/ISensors.h> #include <android/hardware/sensors/1.0/types.h> @@ -30,13 +30,13 @@ using ::android::sp; class SensorsHidlTest; class SensorsHidlEnvironmentV1_0 - : public SensorsHidlEnvironmentBase<::android::hardware::sensors::V1_0::Event> { + : public SensorsVtsEnvironmentBase<::android::hardware::sensors::V1_0::Event> { public: - void HidlTearDown() override; + void TearDown() override; using Event = ::android::hardware::sensors::V1_0::Event; SensorsHidlEnvironmentV1_0(const std::string& service_name) - : SensorsHidlEnvironmentBase(service_name) {} + : SensorsVtsEnvironmentBase(service_name) {} private: friend SensorsHidlTest; diff --git a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp index d46cf5a490..e4fa96930c 100644 --- a/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp +++ b/sensors/1.0/vts/functional/VtsHalSensorsV1_0TargetTest.cpp @@ -39,12 +39,12 @@ class SensorsHidlTest : public SensorsHidlTestBase<SensorType, Event, SensorInfo public: virtual void SetUp() override { mEnvironment = new SensorsHidlEnvironmentV1_0(GetParam()); - mEnvironment->HidlSetUp(); + mEnvironment->SetUp(); // Ensure that we have a valid environment before performing tests ASSERT_NE(S(), nullptr); } - virtual void TearDown() override { mEnvironment->HidlTearDown(); } + virtual void TearDown() override { mEnvironment->TearDown(); } protected: SensorInfo defaultSensorByType(SensorType type) override; @@ -81,7 +81,7 @@ class SensorsHidlTest : public SensorsHidlTestBase<SensorType, Event, SensorInfo inline sp<ISensors>& S() { return mEnvironment->sensors; } - SensorsHidlEnvironmentBase<Event>* getEnvironment() override { return mEnvironment; } + SensorsVtsEnvironmentBase<Event>* getEnvironment() override { return mEnvironment; } private: // Test environment for sensors HAL. diff --git a/sensors/2.0/vts/functional/OWNERS b/sensors/2.0/vts/functional/OWNERS index 892da1548c..e20125b501 100644 --- a/sensors/2.0/vts/functional/OWNERS +++ b/sensors/2.0/vts/functional/OWNERS @@ -1,8 +1,2 @@ -# Sensors team -arthuri@google.com -bduddie@google.com -stange@google.com - -# VTS team -trong@google.com -yim@google.com +# Bug component: 62965 +include ../../../common/vts/OWNERS diff --git a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp index e212423fa7..d7d061a48e 100644 --- a/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp +++ b/sensors/2.0/vts/functional/VtsHalSensorsV2_0TargetTest.cpp @@ -256,8 +256,8 @@ TEST_P(SensorsHidlTest, CleanupDirectConnectionOnInitialize) { // Clear the active direct connections so they are not stopped during TearDown auto handles = mDirectChannelHandles; mDirectChannelHandles.clear(); - getEnvironment()->HidlTearDown(); - getEnvironment()->HidlSetUp(); + getEnvironment()->TearDown(); + getEnvironment()->SetUp(); if (HasFatalFailure()) { return; // Exit early if resetting the environment failed } diff --git a/sensors/2.1/vts/functional/OWNERS b/sensors/2.1/vts/functional/OWNERS index 892da1548c..e20125b501 100644 --- a/sensors/2.1/vts/functional/OWNERS +++ b/sensors/2.1/vts/functional/OWNERS @@ -1,8 +1,2 @@ -# Sensors team -arthuri@google.com -bduddie@google.com -stange@google.com - -# VTS team -trong@google.com -yim@google.com +# Bug component: 62965 +include ../../../common/vts/OWNERS diff --git a/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.cpp b/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.cpp index a8c2513e9f..d3958209f5 100644 --- a/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.cpp +++ b/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.cpp @@ -103,7 +103,7 @@ bool SensorsHidlEnvironmentV2_X::resetHal() { return succeed; } -void SensorsHidlEnvironmentV2_X::HidlTearDown() { +void SensorsHidlEnvironmentV2_X::TearDown() { mStopThread = true; if (mEventQueueFlag != nullptr) { diff --git a/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.h b/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.h index 01f451f5ad..5fe4d8b54f 100644 --- a/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.h +++ b/sensors/common/vts/2_X/SensorsHidlEnvironmentV2_X.h @@ -18,7 +18,7 @@ #define ANDROID_SENSORS_HIDL_ENVIRONMENT_V2_X_H #include "ISensorsWrapper.h" -#include "sensors-vts-utils/SensorsHidlEnvironmentBase.h" +#include "sensors-vts-utils/SensorsVtsEnvironmentBase.h" #include <android/hardware/sensors/2.1/ISensors.h> #include <android/hardware/sensors/2.1/types.h> @@ -46,14 +46,14 @@ class SensorsHalDeathRecipient : public ::android::hardware::hidl_death_recipien }; class SensorsHidlEnvironmentV2_X - : public SensorsHidlEnvironmentBase<::android::hardware::sensors::V2_1::Event> { + : public SensorsVtsEnvironmentBase<::android::hardware::sensors::V2_1::Event> { public: - virtual void HidlTearDown() override; + virtual void TearDown() override; protected: friend SensorsHidlTest; SensorsHidlEnvironmentV2_X(const std::string& service_name) - : SensorsHidlEnvironmentBase(service_name), mEventQueueFlag(nullptr) {} + : SensorsVtsEnvironmentBase(service_name), mEventQueueFlag(nullptr) {} /** * Resets the HAL with new FMQs and a new Event Flag diff --git a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h index ea5dc70c5b..7e22b1902f 100644 --- a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h +++ b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h @@ -170,12 +170,12 @@ class SensorsHidlTest : public SensorsHidlTestBaseV2_X { public: virtual void SetUp() override { mEnvironment = new SensorsHidlEnvironmentV2_X(GetParam()); - mEnvironment->HidlSetUp(); + mEnvironment->SetUp(); // Ensure that we have a valid environment before performing tests ASSERT_NE(getSensors(), nullptr); } - virtual void TearDown() override { mEnvironment->HidlTearDown(); } + virtual void TearDown() override { mEnvironment->TearDown(); } protected: SensorInfoType defaultSensorByType(SensorTypeVersion type) override; @@ -216,7 +216,7 @@ class SensorsHidlTest : public SensorsHidlTestBaseV2_X { inline sp<ISensorsWrapperBase>& getSensors() { return mEnvironment->mSensors; } - SensorsHidlEnvironmentBase<EventType>* getEnvironment() override { return mEnvironment; } + SensorsVtsEnvironmentBase<EventType>* getEnvironment() override { return mEnvironment; } // Test helpers void runSingleFlushTest(const std::vector<SensorInfoType>& sensors, bool activateSensor, @@ -530,32 +530,31 @@ TEST_P(SensorsHidlTest, CallInitializeTwice) { // Create a new environment that calls initialize() std::unique_ptr<SensorsHidlEnvironmentTest> newEnv = std::make_unique<SensorsHidlEnvironmentTest>(GetParam()); - newEnv->HidlSetUp(); + newEnv->SetUp(); if (HasFatalFailure()) { return; // Exit early if setting up the new environment failed } activateAllSensors(true); // Verify that the old environment does not receive any events - EXPECT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0); + EXPECT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0); // Verify that the new event queue receives sensor events - EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get(), newEnv.get()).size(), - kNumEvents); + EXPECT_GE(newEnv.get()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); activateAllSensors(false); // Cleanup the test environment - newEnv->HidlTearDown(); + newEnv->TearDown(); // Restore the test environment for future tests - getEnvironment()->HidlTearDown(); - getEnvironment()->HidlSetUp(); + getEnvironment()->TearDown(); + getEnvironment()->SetUp(); if (HasFatalFailure()) { return; // Exit early if resetting the environment failed } // Ensure that the original environment is receiving events activateAllSensors(true); - EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); + EXPECT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); activateAllSensors(false); } @@ -565,21 +564,21 @@ TEST_P(SensorsHidlTest, CleanupConnectionsOnInitialize) { // Verify that events are received constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000; // 1s constexpr int32_t kNumEvents = 1; - ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), kNumEvents); + ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); // Clear the active sensor handles so they are not disabled during TearDown auto handles = mSensorHandles; mSensorHandles.clear(); - getEnvironment()->HidlTearDown(); - getEnvironment()->HidlSetUp(); + getEnvironment()->TearDown(); + getEnvironment()->SetUp(); if (HasFatalFailure()) { return; // Exit early if resetting the environment failed } // Verify no events are received until sensors are re-activated - ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0); + ASSERT_EQ(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), 0); activateAllSensors(true); - ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), kNumEvents); + ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents); // Disable sensors activateAllSensors(false); diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h deleted file mode 100644 index 19dfbe55e4..0000000000 --- a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h +++ /dev/null @@ -1,110 +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_SENSORS_HIDL_ENVIRONMENT_BASE_H -#define ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H - -#include <gtest/gtest.h> - -#include <atomic> -#include <memory> -#include <mutex> -#include <thread> -#include <vector> - -template <class Event> -class IEventCallback { - public: - virtual ~IEventCallback() = default; - virtual void onEvent(const Event& event) = 0; -}; - -template <class Event> -class SensorsHidlEnvironmentBase { - public: - virtual void HidlSetUp() { - ASSERT_TRUE(resetHal()) << "could not get hidl service"; - - mCollectionEnabled = 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)); - } - - virtual void HidlTearDown() = 0; - - // 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) { - std::lock_guard<std::mutex> lock(mEventsMutex); - if (output) { - output->insert(output->end(), mEvents.begin(), mEvents.end()); - } - mEvents.clear(); - } - - // set sensor event collection status - void setCollection(bool enable) { - std::lock_guard<std::mutex> lock(mEventsMutex); - mCollectionEnabled = enable; - } - - void registerCallback(IEventCallback<Event>* callback) { - std::lock_guard<std::mutex> lock(mEventsMutex); - mCallback = callback; - } - - void unregisterCallback() { - std::lock_guard<std::mutex> lock(mEventsMutex); - mCallback = nullptr; - } - - protected: - SensorsHidlEnvironmentBase(const std::string& service_name) - : mCollectionEnabled(false), mCallback(nullptr) { - mServiceName = service_name; - } - virtual ~SensorsHidlEnvironmentBase(){}; - - void addEvent(const Event& ev) { - std::lock_guard<std::mutex> lock(mEventsMutex); - if (mCollectionEnabled) { - mEvents.push_back(ev); - } - - if (mCallback != nullptr) { - mCallback->onEvent(ev); - } - } - - virtual void startPollingThread() = 0; - virtual bool resetHal() = 0; - - std::string mServiceName; - bool mCollectionEnabled; - std::atomic_bool mStopThread; - std::thread mPollThread; - std::vector<Event> mEvents; - std::mutex mEventsMutex; - - IEventCallback<Event>* mCallback; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsHidlEnvironmentBase<Event>); -}; - -#endif // ANDROID_SENSORS_HIDL_ENVIRONMENT_BASE_H
\ No newline at end of file diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h index af140096b8..f3cbd78a20 100644 --- a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h +++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h @@ -18,8 +18,8 @@ #define ANDROID_SENSORS_HIDL_TEST_BASE_H #include "sensors-vts-utils/SensorEventsChecker.h" -#include "sensors-vts-utils/SensorsHidlEnvironmentBase.h" #include "sensors-vts-utils/SensorsTestSharedMemory.h" +#include "sensors-vts-utils/SensorsVtsEnvironmentBase.h" #include <android/hardware/sensors/1.0/ISensors.h> #include <android/hardware/sensors/1.0/types.h> @@ -163,7 +163,7 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { : mAccelNormChecker(Vec3NormChecker<EventType>::byNominal(GRAVITY_EARTH, 1.0f /*m/s^2*/)), mGyroNormChecker(Vec3NormChecker<EventType>::byNominal(0.f, 0.1f /*rad/s*/)) {} - virtual SensorsHidlEnvironmentBase<EventType>* getEnvironment() = 0; + virtual SensorsVtsEnvironmentBase<EventType>* getEnvironment() = 0; virtual void SetUp() override {} @@ -198,49 +198,6 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { RateLevel rate, ISensors::configDirectReport_cb _hidl_cb) = 0; - std::vector<EventType> collectEvents(useconds_t timeLimitUs, size_t nEventLimit, - bool clearBeforeStart = true, - bool changeCollection = true) { - return collectEvents(timeLimitUs, nEventLimit, getEnvironment(), clearBeforeStart, - changeCollection); - } - - std::vector<EventType> collectEvents(useconds_t timeLimitUs, size_t nEventLimit, - SensorsHidlEnvironmentBase<EventType>* environment, - bool clearBeforeStart = true, - bool changeCollection = true) { - std::vector<EventType> events; - constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms - - ALOGI("collect max of %zu events for %d us, clearBeforeStart %d", nEventLimit, timeLimitUs, - clearBeforeStart); - - if (changeCollection) { - environment->setCollection(true); - } - if (clearBeforeStart) { - environment->catEvents(nullptr); - } - - while (timeLimitUs > 0) { - useconds_t duration = std::min(SLEEP_GRANULARITY, timeLimitUs); - usleep(duration); - timeLimitUs -= duration; - - environment->catEvents(&events); - if (events.size() >= nEventLimit) { - break; - } - ALOGV("time to go = %d, events to go = %d", (int)timeLimitUs, - (int)(nEventLimit - events.size())); - } - - if (changeCollection) { - environment->setCollection(false); - } - return events; - } - void testStreamingOperation(SensorTypeVersion type, std::chrono::nanoseconds samplingPeriod, std::chrono::seconds duration, const SensorEventsChecker<EventType>& checker) { @@ -268,7 +225,7 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_EQ(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK); ASSERT_EQ(activate(handle, 1), Result::OK); - events = collectEvents(minTimeUs, minNEvent, getEnvironment(), true /*clearBeforeStart*/); + events = getEnvironment()->collectEvents(minTimeUs, minNEvent, true /*clearBeforeStart*/); ASSERT_EQ(activate(handle, 0), Result::OK); ALOGI("Collected %zu samples", events.size()); @@ -335,13 +292,13 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_EQ(activate(handle, 1), Result::OK); usleep(500000); // sleep 0.5 sec to wait for change rate to happen - events1 = collectEvents(collectionTimeoutUs, minNEvent, getEnvironment()); + events1 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent); // second collection, without stopping the sensor ASSERT_EQ(batch(handle, secondCollectionPeriod, batchingPeriodInNs), Result::OK); usleep(500000); // sleep 0.5 sec to wait for change rate to happen - events2 = collectEvents(collectionTimeoutUs, minNEvent, getEnvironment()); + events2 = getEnvironment()->collectEvents(collectionTimeoutUs, minNEvent); // end of collection, stop sensor ASSERT_EQ(activate(handle, 0), Result::OK); @@ -447,16 +404,17 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { getEnvironment()->setCollection(true); // clean existing collections - collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/, true /*clearBeforeStart*/, - false /*change collection*/); + getEnvironment()->collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/, + true /*clearBeforeStart*/, false /*change collection*/); // 0.8 + 0.2 times the batching period usleep(batchingPeriodInNs / 1000 * 2 / 10); ASSERT_EQ(flush(handle), Result::OK); // plus some time for the event to deliver - events = collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount, - false /*clearBeforeStart*/, false /*change collection*/); + events = getEnvironment()->collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount, + false /*clearBeforeStart*/, + false /*change collection*/); getEnvironment()->setCollection(false); ASSERT_EQ(activate(handle, 0), Result::OK); diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsVtsEnvironmentBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsVtsEnvironmentBase.h new file mode 100644 index 0000000000..17a96ed739 --- /dev/null +++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsVtsEnvironmentBase.h @@ -0,0 +1,146 @@ +/* + * 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_VTS_ENVIRONMENT_BASE_H +#define ANDROID_SENSORS_VTS_ENVIRONMENT_BASE_H + +#include <gtest/gtest.h> + +#include <atomic> +#include <memory> +#include <mutex> +#include <thread> +#include <vector> + +#include <log/log.h> + +template <class Event> +class IEventCallback { + public: + virtual ~IEventCallback() = default; + virtual void onEvent(const Event& event) = 0; +}; + +template <class Event> +class SensorsVtsEnvironmentBase { + public: + virtual void SetUp() { + ASSERT_TRUE(resetHal()) << "could not get hidl service"; + + mCollectionEnabled = 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)); + } + + virtual void TearDown() = 0; + + // 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) { + std::lock_guard<std::mutex> lock(mEventsMutex); + if (output) { + output->insert(output->end(), mEvents.begin(), mEvents.end()); + } + mEvents.clear(); + } + + // set sensor event collection status + void setCollection(bool enable) { + std::lock_guard<std::mutex> lock(mEventsMutex); + mCollectionEnabled = enable; + } + + void registerCallback(IEventCallback<Event>* callback) { + std::lock_guard<std::mutex> lock(mEventsMutex); + mCallback = callback; + } + + void unregisterCallback() { + std::lock_guard<std::mutex> lock(mEventsMutex); + mCallback = nullptr; + } + + std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit, + bool clearBeforeStart = true, bool changeCollection = true) { + std::vector<Event> events; + constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms + + ALOGI("collect max of %zu events for %d us, clearBeforeStart %d", nEventLimit, timeLimitUs, + clearBeforeStart); + + if (changeCollection) { + setCollection(true); + } + if (clearBeforeStart) { + catEvents(nullptr); + } + + while (timeLimitUs > 0) { + useconds_t duration = std::min(SLEEP_GRANULARITY, timeLimitUs); + usleep(duration); + timeLimitUs -= duration; + + catEvents(&events); + if (events.size() >= nEventLimit) { + break; + } + ALOGV("time to go = %d, events to go = %d", (int)timeLimitUs, + (int)(nEventLimit - events.size())); + } + + if (changeCollection) { + setCollection(false); + } + return events; + } + + protected: + SensorsVtsEnvironmentBase(const std::string& service_name) + : mCollectionEnabled(false), mCallback(nullptr) { + mServiceName = service_name; + } + virtual ~SensorsVtsEnvironmentBase(){}; + + void addEvent(const Event& ev) { + std::lock_guard<std::mutex> lock(mEventsMutex); + if (mCollectionEnabled) { + mEvents.push_back(ev); + } + + if (mCallback != nullptr) { + mCallback->onEvent(ev); + } + } + + virtual void startPollingThread() = 0; + virtual bool resetHal() = 0; + + std::string mServiceName; + bool mCollectionEnabled; + std::atomic_bool mStopThread; + std::thread mPollThread; + std::vector<Event> mEvents; + std::mutex mEventsMutex; + + IEventCallback<Event>* mCallback; + + GTEST_DISALLOW_COPY_AND_ASSIGN_(SensorsVtsEnvironmentBase<Event>); +}; + +#endif // ANDROID_SENSORS_VTS_ENVIRONMENT_BASE_H
\ No newline at end of file |