Loading sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h +9 −17 Original line number Diff line number Diff line Loading @@ -225,7 +225,9 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_EQ(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK); ASSERT_EQ(activate(handle, 1), Result::OK); events = getEnvironment()->collectEvents(minTimeUs, minNEvent, true /*clearBeforeStart*/); events = getEnvironment()->collectEvents( minTimeUs, minNEvent, true /* clearBeforeStart */, true /* changeCollection */, [&type](const EventType& event) { return event.sensorType == type; }); ASSERT_EQ(activate(handle, 0), Result::OK); ALOGI("Collected %zu samples", events.size()); Loading @@ -233,9 +235,7 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_GT(events.size(), 0u); bool handleMismatchReported = false; bool metaSensorTypeErrorReported = false; for (auto& e : events) { if (e.sensorType == type) { // avoid generating hundreds of error if (!handleMismatchReported) { EXPECT_EQ(e.sensorHandle, handle) Loading @@ -243,14 +243,6 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { "Event of the same type must come from the sensor registered"); } sensorEvents.push_back(e); } else { // avoid generating hundreds of error if (!metaSensorTypeErrorReported) { EXPECT_TRUE(isMetaSensorType(e.sensorType)) << (metaSensorTypeErrorReported = true, "Only meta types are allowed besides the type registered"); } } } std::string s; Loading sensors/common/vts/utils/include/sensors-vts-utils/SensorsVtsEnvironmentBase.h +16 −5 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <gtest/gtest.h> #include <atomic> #include <functional> #include <memory> #include <mutex> #include <thread> Loading Loading @@ -61,9 +62,16 @@ class SensorsVtsEnvironmentBase { } // set sensor event collection status void setCollection(bool enable) { void setCollection(bool enable, const std::optional<std::function<bool(const Event&)>>& filter = std::nullopt) { std::lock_guard<std::mutex> lock(mEventsMutex); mCollectionEnabled = enable; if (enable && filter.has_value()) { mEventFilter = *filter; } else { mEventFilter.reset(); } } void registerCallback(IEventCallback<Event>* callback) { Loading @@ -76,8 +84,10 @@ class SensorsVtsEnvironmentBase { mCallback = nullptr; } std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart = true, bool changeCollection = true) { std::vector<Event> collectEvents( useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart = true, bool changeCollection = true, const std::optional<std::function<bool(const Event&)>>& filter = std::nullopt) { std::vector<Event> events; constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms Loading @@ -85,7 +95,7 @@ class SensorsVtsEnvironmentBase { clearBeforeStart); if (changeCollection) { setCollection(true); setCollection(true, filter); } if (clearBeforeStart) { catEvents(nullptr); Loading Loading @@ -121,7 +131,7 @@ class SensorsVtsEnvironmentBase { void addEvent(const Event& ev) { std::lock_guard<std::mutex> lock(mEventsMutex); if (mCollectionEnabled) { if (mCollectionEnabled && (!mEventFilter.has_value() || (*mEventFilter)(ev))) { mEvents.push_back(ev); } Loading @@ -138,6 +148,7 @@ class SensorsVtsEnvironmentBase { std::atomic_bool mStopThread; std::thread mPollThread; std::vector<Event> mEvents; std::optional<std::function<bool(const Event&)>> mEventFilter; std::mutex mEventsMutex; IEventCallback<Event>* mCallback; Loading Loading
sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlTestBase.h +9 −17 Original line number Diff line number Diff line Loading @@ -225,7 +225,9 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_EQ(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK); ASSERT_EQ(activate(handle, 1), Result::OK); events = getEnvironment()->collectEvents(minTimeUs, minNEvent, true /*clearBeforeStart*/); events = getEnvironment()->collectEvents( minTimeUs, minNEvent, true /* clearBeforeStart */, true /* changeCollection */, [&type](const EventType& event) { return event.sensorType == type; }); ASSERT_EQ(activate(handle, 0), Result::OK); ALOGI("Collected %zu samples", events.size()); Loading @@ -233,9 +235,7 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { ASSERT_GT(events.size(), 0u); bool handleMismatchReported = false; bool metaSensorTypeErrorReported = false; for (auto& e : events) { if (e.sensorType == type) { // avoid generating hundreds of error if (!handleMismatchReported) { EXPECT_EQ(e.sensorHandle, handle) Loading @@ -243,14 +243,6 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> { "Event of the same type must come from the sensor registered"); } sensorEvents.push_back(e); } else { // avoid generating hundreds of error if (!metaSensorTypeErrorReported) { EXPECT_TRUE(isMetaSensorType(e.sensorType)) << (metaSensorTypeErrorReported = true, "Only meta types are allowed besides the type registered"); } } } std::string s; Loading
sensors/common/vts/utils/include/sensors-vts-utils/SensorsVtsEnvironmentBase.h +16 −5 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <gtest/gtest.h> #include <atomic> #include <functional> #include <memory> #include <mutex> #include <thread> Loading Loading @@ -61,9 +62,16 @@ class SensorsVtsEnvironmentBase { } // set sensor event collection status void setCollection(bool enable) { void setCollection(bool enable, const std::optional<std::function<bool(const Event&)>>& filter = std::nullopt) { std::lock_guard<std::mutex> lock(mEventsMutex); mCollectionEnabled = enable; if (enable && filter.has_value()) { mEventFilter = *filter; } else { mEventFilter.reset(); } } void registerCallback(IEventCallback<Event>* callback) { Loading @@ -76,8 +84,10 @@ class SensorsVtsEnvironmentBase { mCallback = nullptr; } std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart = true, bool changeCollection = true) { std::vector<Event> collectEvents( useconds_t timeLimitUs, size_t nEventLimit, bool clearBeforeStart = true, bool changeCollection = true, const std::optional<std::function<bool(const Event&)>>& filter = std::nullopt) { std::vector<Event> events; constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000; // granularity 100 ms Loading @@ -85,7 +95,7 @@ class SensorsVtsEnvironmentBase { clearBeforeStart); if (changeCollection) { setCollection(true); setCollection(true, filter); } if (clearBeforeStart) { catEvents(nullptr); Loading Loading @@ -121,7 +131,7 @@ class SensorsVtsEnvironmentBase { void addEvent(const Event& ev) { std::lock_guard<std::mutex> lock(mEventsMutex); if (mCollectionEnabled) { if (mCollectionEnabled && (!mEventFilter.has_value() || (*mEventFilter)(ev))) { mEvents.push_back(ev); } Loading @@ -138,6 +148,7 @@ class SensorsVtsEnvironmentBase { std::atomic_bool mStopThread; std::thread mPollThread; std::vector<Event> mEvents; std::optional<std::function<bool(const Event&)>> mEventFilter; std::mutex mEventsMutex; IEventCallback<Event>* mCallback; Loading