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

Commit b8fbfb36 authored by Jimmy's avatar Jimmy
Browse files

Refactor interceptKeyBeforeQueueing tests in FakeInputDispatcherPolicy

Refactor to explicitly set the `delay` of an intercept result, which
allows granular return values.

Bug: 384113980
Test: atest InputDispatcherTest

Flag: TEST_ONLY

Change-Id: I8c581780bc5fbac96fc00ac2750e5d015dbcc9f0
parent 50098249
Loading
Loading
Loading
Loading
+13 −11
Original line number Diff line number Diff line
@@ -198,10 +198,6 @@ void FakeInputDispatcherPolicy::assertNotifyInputChannelBrokenWasCalled(const sp
    ASSERT_EQ(token, *receivedToken);
}

void FakeInputDispatcherPolicy::setInterceptKeyTimeout(std::chrono::milliseconds timeout) {
    mInterceptKeyTimeout = timeout;
}

std::chrono::nanoseconds FakeInputDispatcherPolicy::getKeyWaitingForEventsTimeout() {
    return 500ms;
}
@@ -210,8 +206,9 @@ void FakeInputDispatcherPolicy::setStaleEventTimeout(std::chrono::nanoseconds ti
    mStaleEventTimeout = timeout;
}

void FakeInputDispatcherPolicy::setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching) {
    mConsumeKeyBeforeDispatching = consumeKeyBeforeDispatching;
void FakeInputDispatcherPolicy::setInterceptKeyBeforeDispatchingResult(
        std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult> result) {
    mInterceptKeyBeforeDispatchingResult = result;
}

void FakeInputDispatcherPolicy::assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay) {
@@ -404,7 +401,9 @@ bool FakeInputDispatcherPolicy::filterInputEvent(const InputEvent& inputEvent,
void FakeInputDispatcherPolicy::interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) {
    if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) {
        // Clear intercept state when we handled the event.
        mInterceptKeyTimeout = 0ms;
        if (std::holds_alternative<nsecs_t>(mInterceptKeyBeforeDispatchingResult)) {
            mInterceptKeyBeforeDispatchingResult = nsecs_t(0);
        }
    }
}

@@ -414,17 +413,20 @@ void FakeInputDispatcherPolicy::interceptMotionBeforeQueueing(ui::LogicalDisplay
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&,
                                                         uint32_t) {
    if (mConsumeKeyBeforeDispatching) {
        return inputdispatcher::KeyEntry::InterceptKeyResult::SKIP;
    if (std::holds_alternative<inputdispatcher::KeyEntry::InterceptKeyResult>(
                mInterceptKeyBeforeDispatchingResult)) {
        return mInterceptKeyBeforeDispatchingResult;
    }

    nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
    nsecs_t delay =
            std::chrono::nanoseconds(std::get<nsecs_t>(mInterceptKeyBeforeDispatchingResult))
                    .count();
    if (delay == 0) {
        return inputdispatcher::KeyEntry::InterceptKeyResult::CONTINUE;
    }

    // Clear intercept state so we could dispatch the event in next wake.
    mInterceptKeyTimeout = 0ms;
    mInterceptKeyBeforeDispatchingResult = nsecs_t(0);
    return delay;
}

+8 −8
Original line number Diff line number Diff line
@@ -96,10 +96,6 @@ public:
    void assertDropTargetEquals(const InputDispatcherInterface& dispatcher,
                                const sp<IBinder>& targetToken);
    void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token);
    /**
     * Set policy timeout. A value of zero means next key will not be intercepted.
     */
    void setInterceptKeyTimeout(std::chrono::milliseconds timeout);
    std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override;
    void setStaleEventTimeout(std::chrono::nanoseconds timeout);
    void assertUserActivityNotPoked();
@@ -116,7 +112,12 @@ public:
    void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler);
    void assertUnhandledKeyReported(int32_t keycode);
    void assertUnhandledKeyNotReported();
    void setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching);
    /**
     * Set policy timeout or the interception result.
     * A timeout value of zero means next key will not be intercepted.
     */
    void setInterceptKeyBeforeDispatchingResult(
            std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult> result);
    void assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay);

private:
@@ -145,11 +146,10 @@ private:
    std::condition_variable mNotifyUserActivity;
    std::queue<UserActivityPokeEvent> mUserActivityPokeEvents;

    std::chrono::milliseconds mInterceptKeyTimeout = 0ms;

    std::chrono::nanoseconds mStaleEventTimeout = 1000ms;

    bool mConsumeKeyBeforeDispatching = false;
    std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
            mInterceptKeyBeforeDispatchingResult;

    BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;

+7 −4
Original line number Diff line number Diff line
@@ -5491,7 +5491,8 @@ TEST_F(InputDispatcherTest, InterceptKeyByPolicy) {
            generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT);
    const std::chrono::milliseconds interceptKeyTimeout = 50ms;
    const nsecs_t injectTime = keyArgs.eventTime;
    mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout);
    mFakePolicy->setInterceptKeyBeforeDispatchingResult(
            std::chrono::nanoseconds(interceptKeyTimeout).count());
    mDispatcher->notifyKey(keyArgs);
    // The dispatching time should be always greater than or equal to intercept key timeout.
    window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
@@ -5519,7 +5520,7 @@ TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
    // Set a value that's significantly larger than the default consumption timeout. If the
    // implementation is correct, the actual value doesn't matter; it won't slow down the test.
    mFakePolicy->setInterceptKeyTimeout(600ms);
    mFakePolicy->setInterceptKeyBeforeDispatchingResult(std::chrono::nanoseconds(600ms).count());
    mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT));
    // Window should receive key event immediately when same key up.
    window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
@@ -7438,7 +7439,8 @@ TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) {
    window->consumeFocusEvent(true);
    mFakePolicy->setConsumeKeyBeforeDispatching(true);
    mFakePolicy->setInterceptKeyBeforeDispatchingResult(
            inputdispatcher::KeyEntry::InterceptKeyResult::SKIP);
    mDispatcher->notifyKey(
            KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
@@ -7464,7 +7466,8 @@ TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserAct
    window->consumeFocusEvent(true);
    mFakePolicy->setConsumeKeyBeforeDispatching(true);
    mFakePolicy->setInterceptKeyBeforeDispatchingResult(
            inputdispatcher::KeyEntry::InterceptKeyResult::SKIP);
    mDispatcher->notifyKey(
            KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());