Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f07e3f2b authored by Matthew Sedam's avatar Matthew Sedam Committed by Android (Google) Code Review
Browse files

Merge "Fix event collection in SensorsVtsEnvironmentBase.h" into udc-dev

parents 7388ac1a 2d858209
Loading
Loading
Loading
Loading
+9 −17
Original line number Diff line number Diff line
@@ -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,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)
@@ -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;
+16 −5
Original line number Diff line number Diff line
@@ -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;