Fix event collection in SensorsVtsEnvironmentBase.h

Only collect the events that match the description.

Bug: 270611181
Test: atest VtsAidlHalSensorsTargetTest
Test: atest VtsHalSensorsV2_0TargetTest
Change-Id: Iac4c4dc39b3a4d32dbfdb7770989204c1fe28c47
This commit is contained in:
Matthew Sedam
2023-03-06 21:13:17 +00:00
parent 6d065549e1
commit 2d85820979
2 changed files with 25 additions and 22 deletions

View File

@@ -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());
@@ -233,24 +235,14 @@ 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)
<< (handleMismatchReported = true,
"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");
}
// avoid generating hundreds of error
if (!handleMismatchReported) {
EXPECT_EQ(e.sensorHandle, handle)
<< (handleMismatchReported = true,
"Event of the same type must come from the sensor registered");
}
sensorEvents.push_back(e);
}
std::string s;

View File

@@ -20,6 +20,7 @@
#include <gtest/gtest.h>
#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
@@ -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) {
@@ -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
@@ -85,7 +95,7 @@ class SensorsVtsEnvironmentBase {
clearBeforeStart);
if (changeCollection) {
setCollection(true);
setCollection(true, filter);
}
if (clearBeforeStart) {
catEvents(nullptr);
@@ -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);
}
@@ -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;