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

Commit d3c693e6 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Refactor collectEvent method in SensorsHidlTestBase

Bug: 195593357
Test: Compile, run VTS
Change-Id: Ie4db0bfd88841ce2821ed664053e27ab18262a56
parent e51ea5f4
Loading
Loading
Loading
Loading
+6 −7
Original line number Original line Diff line number Diff line
@@ -537,10 +537,9 @@ TEST_P(SensorsHidlTest, CallInitializeTwice) {


    activateAllSensors(true);
    activateAllSensors(true);
    // Verify that the old environment does not receive any events
    // 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
    // Verify that the new event queue receives sensor events
    EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get(), newEnv.get()).size(),
    EXPECT_GE(newEnv.get()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
              kNumEvents);
    activateAllSensors(false);
    activateAllSensors(false);


    // Cleanup the test environment
    // Cleanup the test environment
@@ -555,7 +554,7 @@ TEST_P(SensorsHidlTest, CallInitializeTwice) {


    // Ensure that the original environment is receiving events
    // Ensure that the original environment is receiving events
    activateAllSensors(true);
    activateAllSensors(true);
    EXPECT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
    EXPECT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);
    activateAllSensors(false);
    activateAllSensors(false);
}
}


@@ -565,7 +564,7 @@ TEST_P(SensorsHidlTest, CleanupConnectionsOnInitialize) {
    // Verify that events are received
    // Verify that events are received
    constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000;  // 1s
    constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000;  // 1s
    constexpr int32_t kNumEvents = 1;
    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
    // Clear the active sensor handles so they are not disabled during TearDown
    auto handles = mSensorHandles;
    auto handles = mSensorHandles;
@@ -577,9 +576,9 @@ TEST_P(SensorsHidlTest, CleanupConnectionsOnInitialize) {
    }
    }


    // Verify no events are received until sensors are re-activated
    // 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);
    activateAllSensors(true);
    ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), kNumEvents);
    ASSERT_GE(getEnvironment()->collectEvents(kCollectionTimeoutUs, kNumEvents).size(), kNumEvents);


    // Disable sensors
    // Disable sensors
    activateAllSensors(false);
    activateAllSensors(false);
+8 −50
Original line number Original line Diff line number Diff line
@@ -198,49 +198,6 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
                                            RateLevel rate,
                                            RateLevel rate,
                                            ISensors::configDirectReport_cb _hidl_cb) = 0;
                                            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,
                                         SensorsVtsEnvironmentBase<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,
    void testStreamingOperation(SensorTypeVersion type, std::chrono::nanoseconds samplingPeriod,
                                std::chrono::seconds duration,
                                std::chrono::seconds duration,
                                const SensorEventsChecker<EventType>& checker) {
                                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(batch(handle, samplingPeriodInNs, batchingPeriodInNs), Result::OK);
        ASSERT_EQ(activate(handle, 1), 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);
        ASSERT_EQ(activate(handle, 0), Result::OK);


        ALOGI("Collected %zu samples", events.size());
        ALOGI("Collected %zu samples", events.size());
@@ -335,13 +292,13 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {
        ASSERT_EQ(activate(handle, 1), Result::OK);
        ASSERT_EQ(activate(handle, 1), Result::OK);


        usleep(500000);  // sleep 0.5 sec to wait for change rate to happen
        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
        // second collection, without stopping the sensor
        ASSERT_EQ(batch(handle, secondCollectionPeriod, batchingPeriodInNs), Result::OK);
        ASSERT_EQ(batch(handle, secondCollectionPeriod, batchingPeriodInNs), Result::OK);


        usleep(500000);  // sleep 0.5 sec to wait for change rate to happen
        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
        // end of collection, stop sensor
        ASSERT_EQ(activate(handle, 0), Result::OK);
        ASSERT_EQ(activate(handle, 0), Result::OK);
@@ -447,16 +404,17 @@ class SensorsHidlTestBase : public testing::TestWithParam<std::string> {


        getEnvironment()->setCollection(true);
        getEnvironment()->setCollection(true);
        // clean existing collections
        // clean existing collections
        collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/, true /*clearBeforeStart*/,
        getEnvironment()->collectEvents(0 /*timeLimitUs*/, 0 /*nEventLimit*/,
                      false /*change collection*/);
                                        true /*clearBeforeStart*/, false /*change collection*/);


        // 0.8 + 0.2 times the batching period
        // 0.8 + 0.2 times the batching period
        usleep(batchingPeriodInNs / 1000 * 2 / 10);
        usleep(batchingPeriodInNs / 1000 * 2 / 10);
        ASSERT_EQ(flush(handle), Result::OK);
        ASSERT_EQ(flush(handle), Result::OK);


        // plus some time for the event to deliver
        // plus some time for the event to deliver
        events = collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount,
        events = getEnvironment()->collectEvents(allowedBatchDeliverTimeNs / 1000, minFifoCount,
                               false /*clearBeforeStart*/, false /*change collection*/);
                                                 false /*clearBeforeStart*/,
                                                 false /*change collection*/);


        getEnvironment()->setCollection(false);
        getEnvironment()->setCollection(false);
        ASSERT_EQ(activate(handle, 0), Result::OK);
        ASSERT_EQ(activate(handle, 0), Result::OK);
+36 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,8 @@
#include <thread>
#include <thread>
#include <vector>
#include <vector>


#include <log/log.h>

template <class Event>
template <class Event>
class IEventCallback {
class IEventCallback {
  public:
  public:
@@ -74,6 +76,40 @@ class SensorsVtsEnvironmentBase {
        mCallback = nullptr;
        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:
  protected:
    SensorsVtsEnvironmentBase(const std::string& service_name)
    SensorsVtsEnvironmentBase(const std::string& service_name)
        : mCollectionEnabled(false), mCallback(nullptr) {
        : mCollectionEnabled(false), mCallback(nullptr) {