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

Commit 1d329e63 authored by Brian Stack's avatar Brian Stack
Browse files

Add CallInitializeTwice Test

Adds a test to Sensors 2.0 to ensure that if the initialize function
is called twice, then the FMQs used in the second call receive events.

Bug: 115969174
Test: New test passes (SensorsHidlTest#CallInitializeTwice)
Change-Id: I21a9307397eca1f2be93a826db89d24002e848c0
parent c3c937e3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ class SensorsHidlEnvironmentV2_0 : public SensorsHidlEnvironmentBase {

    virtual void HidlTearDown() override;

   private:
   protected:
    friend SensorsHidlTest;

    SensorsHidlEnvironmentV2_0() : mEventQueueFlag(nullptr) {}
+51 −0
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ class SensorsHidlTest : public SensorsHidlTestBase {
    SensorsHidlEnvironmentBase* getEnvironment() override {
        return SensorsHidlEnvironmentV2_0::Instance();
    }

    // Helper functions
    void activateAllSensors(bool enable);
};

Return<Result> SensorsHidlTest::activate(int32_t sensorHandle, bool enabled) {
@@ -437,6 +440,54 @@ TEST_F(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) {
                              RateLevel::VERY_FAST, NullChecker());
}

void SensorsHidlTest::activateAllSensors(bool enable) {
    for (const SensorInfo& sensorInfo : getSensorsList()) {
        if (isValidType(sensorInfo.type)) {
            batch(sensorInfo.sensorHandle, sensorInfo.minDelay, 0 /* maxReportLatencyNs */);
            activate(sensorInfo.sensorHandle, enable);
        }
    }
}

// Test that if initialize is called twice, then the HAL writes events to the FMQs from the second
// call to the function.
TEST_F(SensorsHidlTest, CallInitializeTwice) {
    // Create a helper class so that a second environment is able to be instantiated
    class SensorsHidlEnvironmentTest : public SensorsHidlEnvironmentV2_0 {};

    if (getSensorsList().size() == 0) {
        // No sensors
        return;
    }

    constexpr useconds_t kCollectionTimeoutUs = 1000 * 1000;  // 1s
    constexpr int32_t kNumEvents = 1;

    // Create a new environment that calls initialize()
    std::unique_ptr<SensorsHidlEnvironmentTest> newEnv =
        std::make_unique<SensorsHidlEnvironmentTest>();
    newEnv->HidlSetUp();

    activateAllSensors(true);
    // Verify that the old environment does not receive any events
    ASSERT_EQ(collectEvents(kCollectionTimeoutUs, kNumEvents, getEnvironment()).size(), 0);
    // Verify that the new event queue receives sensor events
    ASSERT_GE(collectEvents(kCollectionTimeoutUs, kNumEvents, newEnv.get()).size(), kNumEvents);
    activateAllSensors(false);

    // Cleanup the test environment
    newEnv->HidlTearDown();

    // Restore the test environment for future tests
    SensorsHidlEnvironmentV2_0::Instance()->HidlTearDown();
    SensorsHidlEnvironmentV2_0::Instance()->HidlSetUp();

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

int main(int argc, char** argv) {
    ::testing::AddGlobalTestEnvironment(SensorsHidlEnvironmentV2_0::Instance());
    ::testing::InitGoogleTest(&argc, argv);
+12 −4
Original line number Diff line number Diff line
@@ -40,6 +40,14 @@ const Vec3NormChecker SensorsHidlTestBase::sGyroNormChecker(
std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
                                                      bool clearBeforeStart,
                                                      bool changeCollection) {
    return collectEvents(timeLimitUs, nEventLimit, getEnvironment(), clearBeforeStart,
                         changeCollection);
}

std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
                                                      SensorsHidlEnvironmentBase* environment,
                                                      bool clearBeforeStart,
                                                      bool changeCollection) {
    std::vector<Event> events;
    constexpr useconds_t SLEEP_GRANULARITY = 100 * 1000;  // granularity 100 ms

@@ -47,10 +55,10 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
          clearBeforeStart);

    if (changeCollection) {
        getEnvironment()->setCollection(true);
        environment->setCollection(true);
    }
    if (clearBeforeStart) {
        getEnvironment()->catEvents(nullptr);
        environment->catEvents(nullptr);
    }

    while (timeLimitUs > 0) {
@@ -58,7 +66,7 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
        usleep(duration);
        timeLimitUs -= duration;

        getEnvironment()->catEvents(&events);
        environment->catEvents(&events);
        if (events.size() >= nEventLimit) {
            break;
        }
@@ -67,7 +75,7 @@ std::vector<Event> SensorsHidlTestBase::collectEvents(useconds_t timeLimitUs, si
    }

    if (changeCollection) {
        getEnvironment()->setCollection(false);
        environment->setCollection(false);
    }
    return events;
}
+4 −0
Original line number Diff line number Diff line
@@ -85,6 +85,10 @@ class SensorsHidlTestBase : public ::testing::VtsHalHidlTargetTestBase {

    std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
                                     bool clearBeforeStart = true, bool changeCollection = true);
    static std::vector<Event> collectEvents(useconds_t timeLimitUs, size_t nEventLimit,
                                            SensorsHidlEnvironmentBase* environment,
                                            bool clearBeforeStart = true,
                                            bool changeCollection = true);

    inline static SensorFlagBits extractReportMode(uint64_t flag) {
        return (SensorFlagBits)(flag & ((uint64_t)SensorFlagBits::CONTINUOUS_MODE |