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

Commit fec6b3b1 authored by Andrew Lehmer's avatar Andrew Lehmer Committed by android-build-merger
Browse files

Fix assumptions in SensorsHidlTest.NoStaleEvents

am: d8b212ec

Change-Id: I01fe2492b1f54e6c7cb982acf77bdbb7f6175697
parents 29ba83e4 d8b212ec
Loading
Loading
Loading
Loading
+32 −5
Original line number Diff line number Diff line
@@ -176,6 +176,7 @@ class SensorsHidlTest : public SensorsHidlTestBase {
    // Helper functions
    void activateAllSensors(bool enable);
    std::vector<SensorInfo> getNonOneShotSensors();
    std::vector<SensorInfo> getNonOneShotAndNonSpecialSensors();
    std::vector<SensorInfo> getOneShotSensors();
    std::vector<SensorInfo> getInjectEventSensors();
    int32_t getInvalidSensorHandle();
@@ -257,6 +258,18 @@ std::vector<SensorInfo> SensorsHidlTest::getNonOneShotSensors() {
    return sensors;
}

std::vector<SensorInfo> SensorsHidlTest::getNonOneShotAndNonSpecialSensors() {
    std::vector<SensorInfo> sensors;
    for (const SensorInfo& info : getSensorsList()) {
        SensorFlagBits reportMode = extractReportMode(info.flags);
        if (reportMode != SensorFlagBits::ONE_SHOT_MODE &&
            reportMode != SensorFlagBits::SPECIAL_REPORTING_MODE) {
            sensors.push_back(info);
        }
    }
    return sensors;
}

std::vector<SensorInfo> SensorsHidlTest::getOneShotSensors() {
    std::vector<SensorInfo> sensors;
    for (const SensorInfo& info : getSensorsList()) {
@@ -830,9 +843,10 @@ TEST_F(SensorsHidlTest, NoStaleEvents) {
    EventCallback callback;
    getEnvironment()->registerCallback(&callback);

    const std::vector<SensorInfo> sensors = getSensorsList();
    // This test is not valid for one-shot or special-report-mode sensors
    const std::vector<SensorInfo> sensors = getNonOneShotAndNonSpecialSensors();
    milliseconds maxMinDelay(0);
    for (const SensorInfo& sensor : getSensorsList()) {
    for (const SensorInfo& sensor : sensors) {
        milliseconds minDelay = duration_cast<milliseconds>(microseconds(sensor.minDelay));
        maxMinDelay = milliseconds(std::max(maxMinDelay.count(), minDelay.count()));
    }
@@ -849,10 +863,15 @@ TEST_F(SensorsHidlTest, NoStaleEvents) {
    // Save the last received event for each sensor
    std::map<int32_t, int64_t> lastEventTimestampMap;
    for (const SensorInfo& sensor : sensors) {
        // Some on-change sensors may not report an event without stimulus
        if (extractReportMode(sensor.flags) != SensorFlagBits::ON_CHANGE_MODE) {
            ASSERT_GE(callback.getEvents(sensor.sensorHandle).size(), 1);
        }
        if (callback.getEvents(sensor.sensorHandle).size() >= 1) {
            lastEventTimestampMap[sensor.sensorHandle] =
                    callback.getEvents(sensor.sensorHandle).back().timestamp;
        }
    }

    // Allow some time to pass, reset the callback, then reactivate the sensors
    usleep(duration_cast<microseconds>(kOneSecond + (5 * maxMinDelay)).count());
@@ -862,6 +881,14 @@ TEST_F(SensorsHidlTest, NoStaleEvents) {
    activateAllSensors(false);

    for (const SensorInfo& sensor : sensors) {
        // Skip sensors that did not previously report an event
        if (lastEventTimestampMap.find(sensor.sensorHandle) == lastEventTimestampMap.end()) {
            continue;
        }
        // Skip on-change sensors that do not consistently report an initial event
        if (callback.getEvents(sensor.sensorHandle).size() < 1) {
            continue;
        }
        // Ensure that the first event received is not stale by ensuring that its timestamp is
        // sufficiently different from the previous event
        const Event newEvent = callback.getEvents(sensor.sensorHandle).front();