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

Commit 29d592ca authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Increase timeouts in FakeInputDispatcherPolicy

We are speculating that these timeouts are too short, and therefore the
tests are flaking occasionally.

At the same time, do some more cleanups:
- remove the flag that's already rolled out
- increase the speed of user activity poke tests

Bug: 424941803
Test: atest inputflinger_tests
Flag: TEST_ONLY
Change-Id: Ic2c9180d09b3bf02aa8cd4dbe89e1d38c33b885a
parent 71ac6605
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -3251,7 +3251,7 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
    }

    const int32_t eventType = getUserActivityEventType(eventEntry);
    if (input_flags::rate_limit_user_activity_poke_in_dispatcher()) {

    // Note that we're directly getting the time diff between the current event and the previous
    // event. This is assuming that the first user event always happens at a timestamp that is
    // greater than `mMinTimeBetweenUserActivityPokes` (otherwise, the first user event will
@@ -3262,7 +3262,6 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) {
    if (timeSinceLastEvent < mMinTimeBetweenUserActivityPokes) {
        return;
    }
    }

    ui::LogicalDisplayId displayId = getTargetDisplayId(eventEntry);
    sp<WindowInfoHandle> focusedWindowHandle = getFocusedWindowHandleLocked(displayId);
+33 −19
Original line number Diff line number Diff line
@@ -22,6 +22,13 @@

namespace android {

// How long to wait when we are expecting an event to occur.
// This should be long, so that tests are not flaky when the device is slow.
static constexpr std::chrono::nanoseconds EVENT_SHOULD_OCCUR_TIMEOUT = 5s;
// How long to wait when we are expecting an event to not occur.
// This should be short, so that the tests are fast.
static constexpr std::chrono::nanoseconds EVENT_SHOULD_NOT_OCCUR_TIMEOUT = 10ms;

// --- FakeInputDispatcherPolicy ---

void FakeInputDispatcherPolicy::assertFilterInputEventWasCalled(const NotifyKeyArgs& args) {
@@ -150,7 +157,8 @@ PointerCaptureRequest FakeInputDispatcherPolicy::assertSetPointerCaptureCalled(
    base::ScopedLockAssertion assumeLocked(mLock);

    if (!mPointerCaptureChangedCondition
                 .wait_for(lock, 100ms, [this, enabled, window]() REQUIRES(mLock) {
                 .wait_for(lock, EVENT_SHOULD_OCCUR_TIMEOUT,
                           [this, enabled, window]() REQUIRES(mLock) {
                               if (enabled) {
                                   return mPointerCaptureRequest->isEnable() &&
                                           mPointerCaptureRequest->window == window->getToken();
@@ -171,7 +179,8 @@ void FakeInputDispatcherPolicy::assertSetPointerCaptureNotCalled() {
    std::unique_lock lock(mLock);
    base::ScopedLockAssertion assumeLocked(mLock);

    if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) {
    if (mPointerCaptureChangedCondition.wait_for(lock, EVENT_SHOULD_NOT_OCCUR_TIMEOUT) !=
        std::cv_status::timeout) {
        FAIL() << "Expected setPointerCapture(request) to not be called, but was called. "
                  "enabled = "
               << std::to_string(mPointerCaptureRequest->isEnable());
@@ -192,8 +201,8 @@ void FakeInputDispatcherPolicy::assertNotifyInputChannelBrokenWasCalled(const sp
    std::unique_lock lock(mLock);
    base::ScopedLockAssertion assumeLocked(mLock);
    std::optional<sp<IBinder>> receivedToken =
            getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock,
                                                  mNotifyInputChannelBroken);
            getItemFromStorageLockedInterruptible(EVENT_SHOULD_OCCUR_TIMEOUT, mBrokenInputChannels,
                                                  lock, mNotifyInputChannelBroken);
    ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token";
    ASSERT_EQ(token, *receivedToken);
}
@@ -215,7 +224,7 @@ void FakeInputDispatcherPolicy::assertFocusedDisplayNotified(ui::LogicalDisplayI
    std::unique_lock lock(mLock);
    base::ScopedLockAssertion assumeLocked(mLock);

    if (!mFocusedDisplayNotifiedCondition.wait_for(lock, 100ms,
    if (!mFocusedDisplayNotifiedCondition.wait_for(lock, EVENT_SHOULD_OCCUR_TIMEOUT,
                                                   [this, expectedDisplay]() REQUIRES(mLock) {
                                                       if (!mNotifiedFocusedDisplay.has_value() ||
                                                           mNotifiedFocusedDisplay.value() !=
@@ -234,7 +243,8 @@ void FakeInputDispatcherPolicy::assertUserActivityNotPoked() {
    base::ScopedLockAssertion assumeLocked(mLock);

    std::optional<UserActivityPokeEvent> pokeEvent =
            getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
            getItemFromStorageLockedInterruptible(EVENT_SHOULD_NOT_OCCUR_TIMEOUT,
                                                  mUserActivityPokeEvents, lock,
                                                  mNotifyUserActivity);

    ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked";
@@ -246,7 +256,8 @@ void FakeInputDispatcherPolicy::assertUserActivityPoked(
    base::ScopedLockAssertion assumeLocked(mLock);

    std::optional<UserActivityPokeEvent> pokeEvent =
            getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock,
            getItemFromStorageLockedInterruptible(EVENT_SHOULD_OCCUR_TIMEOUT,
                                                  mUserActivityPokeEvents, lock,
                                                  mNotifyUserActivity);
    ASSERT_TRUE(pokeEvent) << "Expected a user poke event";

@@ -257,11 +268,12 @@ void FakeInputDispatcherPolicy::assertUserActivityPoked(

void FakeInputDispatcherPolicy::assertNotifyDeviceInteractionWasCalled(int32_t deviceId,
                                                                       std::set<gui::Uid> uids) {
    ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms));
    ASSERT_EQ(std::make_pair(deviceId, uids),
              mNotifiedInteractions.popWithTimeout(EVENT_SHOULD_OCCUR_TIMEOUT));
}

void FakeInputDispatcherPolicy::assertNotifyDeviceInteractionWasNotCalled() {
    ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms));
    ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(EVENT_SHOULD_NOT_OCCUR_TIMEOUT));
}

void FakeInputDispatcherPolicy::setUnhandledKeyHandler(
@@ -274,7 +286,8 @@ void FakeInputDispatcherPolicy::assertUnhandledKeyReported(int32_t keycode) {
    std::unique_lock lock(mLock);
    base::ScopedLockAssertion assumeLocked(mLock);
    std::optional<int32_t> unhandledKeycode =
            getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock,
            getItemFromStorageLockedInterruptible(EVENT_SHOULD_OCCUR_TIMEOUT,
                                                  mReportedUnhandledKeycodes, lock,
                                                  mNotifyUnhandledKey);
    ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported";
    ASSERT_EQ(unhandledKeycode, keycode);
@@ -284,7 +297,8 @@ void FakeInputDispatcherPolicy::assertUnhandledKeyNotReported() {
    std::unique_lock lock(mLock);
    base::ScopedLockAssertion assumeLocked(mLock);
    std::optional<int32_t> unhandledKeycode =
            getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock,
            getItemFromStorageLockedInterruptible(EVENT_SHOULD_NOT_OCCUR_TIMEOUT,
                                                  mReportedUnhandledKeycodes, lock,
                                                  mNotifyUnhandledKey);
    ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported";
}
@@ -436,7 +450,7 @@ FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, con

void FakeInputDispatcherPolicy::assertKeyConsumedByPolicy(
        const ::testing::Matcher<KeyEvent>& matcher) {
    ASSERT_THAT(*mKeysConsumedByPolicy.popWithTimeout(100ms), matcher);
    ASSERT_THAT(*mKeysConsumedByPolicy.popWithTimeout(EVENT_SHOULD_OCCUR_TIMEOUT), matcher);
}

void FakeInputDispatcherPolicy::assertNoKeysConsumedByPolicy() {
+17 −45
Original line number Diff line number Diff line
@@ -9833,11 +9833,8 @@ private:
    std::chrono::nanoseconds mGestureStartTime;
};
TEST_F_WITH_FLAGS(
        InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
        REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
                                            rate_limit_user_activity_poke_in_dispatcher))) {
    mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
TEST_F(InputDispatcherUserActivityPokeTests, MinPokeTimeObserved) {
    mDispatcher->setMinTimeBetweenUserActivityPokes(10ms);
    // First event of type TOUCH. Should poke.
    NotifyMotionArgs motionArgs =
@@ -9846,88 +9843,63 @@ TEST_F_WITH_FLAGS(
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_TOUCH, ui::LogicalDisplayId::DEFAULT}});
    // 80ns > 50ns has passed since previous TOUCH event. Should poke.
    // 80ms > 10ms has passed since previous TOUCH event. Should poke.
    motionArgs =
            notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN,
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(130));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_TOUCH, ui::LogicalDisplayId::DEFAULT}});
    // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
    // First event of type OTHER. Should poke (despite being within 10ms of previous TOUCH event).
    motionArgs =
            notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(135));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_OTHER, ui::LogicalDisplayId::DEFAULT}});
    // Within 50ns of previous TOUCH event. Should NOT poke.
    // Within 10ms of previous TOUCH event. Should NOT poke.
    notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
                           std::chrono::milliseconds(140));
                           std::chrono::milliseconds(138));
    mFakePolicy->assertUserActivityNotPoked();
    // Within 50ns of previous OTHER event. Should NOT poke.
    // Within 10ms of previous OTHER event. Should NOT poke.
    notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
                           ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(150));
                           ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(142));
    mFakePolicy->assertUserActivityNotPoked();
    // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
    // Within 10ms of previous TOUCH event (which was at time 130). Should NOT poke.
    // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
    notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
                           std::chrono::milliseconds(160));
                           std::chrono::milliseconds(139));
    mFakePolicy->assertUserActivityNotPoked();
    // 65ns > 50ns has passed since previous OTHER event. Should poke.
    // 50ms > 10ms has passed since previous OTHER event at 135ms. Should poke.
    motionArgs =
            notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(200));
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(185));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_OTHER, ui::LogicalDisplayId::DEFAULT}});
    // 170ns > 50ns has passed since previous TOUCH event. Should poke.
    // 160ms > 10ms has passed since previous TOUCH event. Should poke.
    motionArgs =
            notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
                                   std::chrono::milliseconds(300));
                                   std::chrono::milliseconds(290));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_TOUCH, ui::LogicalDisplayId::DEFAULT}});
    // Assert that there's no more user activity poke event.
    // No other pokes
    mFakePolicy->assertUserActivityNotPoked();
}
TEST_F_WITH_FLAGS(
        InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
        REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
                                            rate_limit_user_activity_poke_in_dispatcher))) {
    NotifyMotionArgs motionArgs =
            notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN,
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(200));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_TOUCH, ui::LogicalDisplayId::DEFAULT}});
    notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
                           std::chrono::milliseconds(280));
    mFakePolicy->assertUserActivityNotPoked();
    motionArgs =
            notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN,
                                   ui::LogicalDisplayId::DEFAULT, std::chrono::milliseconds(340));
    mFakePolicy->assertUserActivityPoked(
            {{motionArgs.eventTime, USER_ACTIVITY_EVENT_TOUCH, ui::LogicalDisplayId::DEFAULT}});
}
TEST_F_WITH_FLAGS(
        InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
        REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
                                            rate_limit_user_activity_poke_in_dispatcher))) {
TEST_F(InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting) {
    mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
    notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
                           std::chrono::milliseconds(20));
    mFakePolicy->assertUserActivityPoked();
    notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
                           std::chrono::milliseconds(30));
    mFakePolicy->assertUserActivityPoked();
    mFakePolicy->assertUserActivityPoked();
}
class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest {