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

Commit b237f9e2 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Increase timeouts in InputDispatcher_tests

These tests are flaky on some platforms because we aren't allowing
enough time to consume the events.

To work around this, create another path for checking when the events
didn't occur.

Now, if we expect to receive an event, we are waiting for a very long
time. If we expect no event, we are waiting a short time.

This will speed up tests while reducing the flake rate.

In addition to the above, add some more infrastructure to be able to
match against common fields of NotifyArgs and InputEvent.

Bug: 292232423
Test: TEST=inputflinger_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Change-Id: I1d6f091376b59279f11a04d02a875991a6384e31
parent d3208d8f
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -551,7 +551,7 @@ class KeyEvent : public InputEvent {
public:
    virtual ~KeyEvent() { }

    virtual InputEventType getType() const { return InputEventType::KEY; }
    InputEventType getType() const override { return InputEventType::KEY; }

    inline int32_t getAction() const { return mAction; }

@@ -602,7 +602,7 @@ class MotionEvent : public InputEvent {
public:
    virtual ~MotionEvent() { }

    virtual InputEventType getType() const { return InputEventType::MOTION; }
    InputEventType getType() const override { return InputEventType::MOTION; }

    inline int32_t getAction() const { return mAction; }

@@ -930,7 +930,7 @@ class FocusEvent : public InputEvent {
public:
    virtual ~FocusEvent() {}

    virtual InputEventType getType() const override { return InputEventType::FOCUS; }
    InputEventType getType() const override { return InputEventType::FOCUS; }

    inline bool getHasFocus() const { return mHasFocus; }

@@ -949,7 +949,7 @@ class CaptureEvent : public InputEvent {
public:
    virtual ~CaptureEvent() {}

    virtual InputEventType getType() const override { return InputEventType::CAPTURE; }
    InputEventType getType() const override { return InputEventType::CAPTURE; }

    inline bool getPointerCaptureEnabled() const { return mPointerCaptureEnabled; }

@@ -968,7 +968,7 @@ class DragEvent : public InputEvent {
public:
    virtual ~DragEvent() {}

    virtual InputEventType getType() const override { return InputEventType::DRAG; }
    InputEventType getType() const override { return InputEventType::DRAG; }

    inline bool isExiting() const { return mIsExiting; }

@@ -992,7 +992,7 @@ class TouchModeEvent : public InputEvent {
public:
    virtual ~TouchModeEvent() {}

    virtual InputEventType getType() const override { return InputEventType::TOUCH_MODE; }
    InputEventType getType() const override { return InputEventType::TOUCH_MODE; }

    inline bool isInTouchMode() const { return mIsInTouchMode; }

+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ cc_test {
        "SlopController_test.cpp",
        "SyncQueue_test.cpp",
        "TestInputListener.cpp",
        "TestInputListenerMatchers.cpp",
        "TouchpadInputMapper_test.cpp",
        "UinputDevice.cpp",
        "UnwantedInteractionBlocker_test.cpp",
+408 −473

File changed.

Preview size limit exceeded, changes collapsed.

+37 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "TestInputListenerMatchers.h"

namespace android {

WithKeyActionMatcher WithKeyAction(int32_t action) {
    return WithKeyActionMatcher(action);
}

WithMotionActionMatcher WithMotionAction(int32_t action) {
    return WithMotionActionMatcher(action);
}

WithDisplayIdMatcher WithDisplayId(int32_t displayId) {
    return WithDisplayIdMatcher(displayId);
}

WithDeviceIdMatcher WithDeviceId(int32_t deviceId) {
    return WithDeviceIdMatcher(deviceId);
}

} // namespace android
+122 −30
Original line number Diff line number Diff line
@@ -23,53 +23,145 @@
#include <gtest/gtest.h>
#include <input/Input.h>

#include "NotifyArgs.h"
#include "TestConstants.h"

namespace android {

MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
    bool matches = action == arg.action;
    if (!matches) {
        *result_listener << "expected action " << MotionEvent::actionToString(action)
                         << ", but got " << MotionEvent::actionToString(arg.action);
MATCHER_P(WithSource, source, "InputEvent with specified source") {
    *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
                     << inputEventSourceToString(arg.source);
    return arg.source == source;
}

/// Key action
class WithKeyActionMatcher {
public:
    using is_gtest_matcher = void;
    explicit WithKeyActionMatcher(int32_t action) : mAction(action) {}

    bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
        return mAction == args.action;
    }
    if (action == AMOTION_EVENT_ACTION_CANCEL) {
        if (!matches) {
            *result_listener << "; ";

    bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
        return mAction == event.getAction();
    }
        *result_listener << "expected FLAG_CANCELED to be set with ACTION_CANCEL, but was not set";
        matches &= (arg.flags & AMOTION_EVENT_FLAG_CANCELED) != 0;

    void DescribeTo(std::ostream* os) const {
        *os << "with key action " << KeyEvent::actionToString(mAction);
    }

    void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }

private:
    const int32_t mAction;
};

WithKeyActionMatcher WithKeyAction(int32_t action);

/// Motion action
class WithMotionActionMatcher {
public:
    using is_gtest_matcher = void;
    explicit WithMotionActionMatcher(int32_t action) : mAction(action) {}

    bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
        bool matches = mAction == args.action;
        if (args.action == AMOTION_EVENT_ACTION_CANCEL) {
            matches &= (args.flags & AMOTION_EVENT_FLAG_CANCELED) != 0;
        }
        return matches;
    }

MATCHER_P(WithKeyAction, action, "KeyEvent with specified action") {
    *result_listener << "expected action " << KeyEvent::actionToString(action) << ", but got "
                     << KeyEvent::actionToString(arg.action);
    return arg.action == action;
    bool MatchAndExplain(const MotionEvent& event, std::ostream*) const {
        bool matches = mAction == event.getAction();
        if (event.getAction() == AMOTION_EVENT_ACTION_CANCEL) {
            matches &= (event.getFlags() & AMOTION_EVENT_FLAG_CANCELED) != 0;
        }
        return matches;
    }

MATCHER_P(WithSource, source, "InputEvent with specified source") {
    *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
                     << inputEventSourceToString(arg.source);
    return arg.source == source;
    void DescribeTo(std::ostream* os) const {
        *os << "with motion action " << MotionEvent::actionToString(mAction);
        if (mAction == AMOTION_EVENT_ACTION_CANCEL) {
            *os << " and FLAG_CANCELED";
        }
    }

    void DescribeNegationTo(std::ostream* os) const { *os << "wrong action"; }

MATCHER_P(WithDisplayId, displayId, "InputEvent with specified displayId") {
    *result_listener << "expected displayId " << displayId << ", but got " << arg.displayId;
    return arg.displayId == displayId;
private:
    const int32_t mAction;
};

WithMotionActionMatcher WithMotionAction(int32_t action);

/// Display Id
class WithDisplayIdMatcher {
public:
    using is_gtest_matcher = void;
    explicit WithDisplayIdMatcher(int32_t displayId) : mDisplayId(displayId) {}

    bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
        return mDisplayId == args.displayId;
    }

    bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
        return mDisplayId == args.displayId;
    }

    bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
        return mDisplayId == event.getDisplayId();
    }

    void DescribeTo(std::ostream* os) const { *os << "with display id " << mDisplayId; }

    void DescribeNegationTo(std::ostream* os) const { *os << "wrong display id"; }

private:
    const int32_t mDisplayId;
};

WithDisplayIdMatcher WithDisplayId(int32_t displayId);

/// Device Id
class WithDeviceIdMatcher {
public:
    using is_gtest_matcher = void;
    explicit WithDeviceIdMatcher(int32_t deviceId) : mDeviceId(deviceId) {}

    bool MatchAndExplain(const NotifyMotionArgs& args, std::ostream*) const {
        return mDeviceId == args.deviceId;
    }

MATCHER_P(WithDeviceId, deviceId, "InputEvent with specified deviceId") {
    *result_listener << "expected deviceId " << deviceId << ", but got " << arg.deviceId;
    return arg.deviceId == deviceId;
    bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
        return mDeviceId == args.deviceId;
    }

    bool MatchAndExplain(const InputEvent& event, std::ostream*) const {
        return mDeviceId == event.getDeviceId();
    }

    void DescribeTo(std::ostream* os) const { *os << "with device id " << mDeviceId; }

    void DescribeNegationTo(std::ostream* os) const { *os << "wrong device id"; }

private:
    const int32_t mDeviceId;
};

WithDeviceIdMatcher WithDeviceId(int32_t deviceId);

MATCHER_P(WithKeyCode, keyCode, "KeyEvent with specified key code") {
    *result_listener << "expected key code " << keyCode << ", but got " << arg.keyCode;
    return arg.keyCode == keyCode;
}

MATCHER_P(WithRepeatCount, repeatCount, "KeyEvent with specified repeat count") {
    return arg.getRepeatCount() == repeatCount;
}

MATCHER_P(WithPointerCount, count, "MotionEvent with specified number of pointers") {
    *result_listener << "expected " << count << " pointer(s), but got " << arg.getPointerCount();
    return arg.getPointerCount() == count;