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

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

Ensure returned event is non-null

Currently, we aren't checking whether the returned event is null. This
causes NPE during a test run when the implementation is bad. The NPE
causes the entire test run to fail, rather than failing individual
tests. This is inconvenient when doing TDD.

In this change, add a matcher for downtime to simplify the code, and add
null checks for cases where a matcher wouldn't be convenient.

Bug: 211379801
Test: m inputflinger_tests && $ANDROID_HOST_OUT/nativetest64/inputflinger_tests/inputflinger_tests
Change-Id: Id0c3cf1908adee0d810c7e24f8507c14f689f999
parent 2f61bdcd
Loading
Loading
Loading
Loading
+19 −12
Original line number Original line Diff line number Diff line
@@ -110,6 +110,13 @@ MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
        *result_listener << "expected action " << MotionEvent::actionToString(action)
        *result_listener << "expected action " << MotionEvent::actionToString(action)
                         << ", but got " << MotionEvent::actionToString(arg.getAction());
                         << ", but got " << MotionEvent::actionToString(arg.getAction());
    }
    }
    if (action == AMOTION_EVENT_ACTION_DOWN) {
        if (!matches) {
            *result_listener << "; ";
        }
        *result_listener << "downTime should match eventTime for ACTION_DOWN events";
        matches &= arg.getDownTime() == arg.getEventTime();
    }
    if (action == AMOTION_EVENT_ACTION_CANCEL) {
    if (action == AMOTION_EVENT_ACTION_CANCEL) {
        if (!matches) {
        if (!matches) {
            *result_listener << "; ";
            *result_listener << "; ";
@@ -120,6 +127,10 @@ MATCHER_P(WithMotionAction, action, "MotionEvent with specified action") {
    return matches;
    return matches;
}
}


MATCHER_P(WithDownTime, downTime, "InputEvent with specified downTime") {
    return arg.getDownTime() == downTime;
}

MATCHER_P(WithSource, source, "InputEvent with specified source") {
MATCHER_P(WithSource, source, "InputEvent with specified source") {
    *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
    *result_listener << "expected source " << inputEventSourceToString(source) << ", but got "
                     << inputEventSourceToString(arg.getSource());
                     << inputEventSourceToString(arg.getSource());
@@ -1210,6 +1221,7 @@ public:
    void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
    void consumeMotionOutsideWithZeroedCoords(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT,
                                              int32_t expectedFlags = 0) {
                                              int32_t expectedFlags = 0) {
        InputEvent* event = consume();
        InputEvent* event = consume();
        ASSERT_NE(nullptr, event);
        ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
        ASSERT_EQ(AINPUT_EVENT_TYPE_MOTION, event->getType());
        const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
        const MotionEvent& motionEvent = static_cast<MotionEvent&>(*event);
        EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
        EXPECT_EQ(AMOTION_EVENT_ACTION_OUTSIDE, motionEvent.getActionMasked());
@@ -2056,6 +2068,7 @@ TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {


    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent1 = window1->consume();
    InputEvent* inputEvent1 = window1->consume();
    ASSERT_NE(inputEvent1, nullptr);
    window2->assertNoEvents();
    window2->assertNoEvents();
    MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
    MotionEvent& motionEvent1 = static_cast<MotionEvent&>(*inputEvent1);
    nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
    nsecs_t downTimeForWindow1 = motionEvent1.getDownTime();
@@ -2065,6 +2078,7 @@ TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
    mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
    mDispatcher->notifyMotion(&(args = generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})));
    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent2 = window2->consume();
    InputEvent* inputEvent2 = window2->consume();
    ASSERT_NE(inputEvent2, nullptr);
    MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
    MotionEvent& motionEvent2 = static_cast<MotionEvent&>(*inputEvent2);
    nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
    nsecs_t downTimeForWindow2 = motionEvent2.getDownTime();
    ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
    ASSERT_NE(downTimeForWindow1, downTimeForWindow2);
@@ -2074,17 +2088,13 @@ TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
    mDispatcher->notifyMotion(
    mDispatcher->notifyMotion(
            &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
            &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})));
    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent3 = window2->consume();
    window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
    MotionEvent& motionEvent3 = static_cast<MotionEvent&>(*inputEvent3);
    ASSERT_EQ(motionEvent3.getDownTime(), downTimeForWindow2);


    // Now add new touch down on the second window
    // Now add new touch down on the second window
    mDispatcher->notifyMotion(
    mDispatcher->notifyMotion(
            &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
            &(args = generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})));
    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent4 = window2->consume();
    window2->consumeMotionEvent(WithDownTime(downTimeForWindow2));
    MotionEvent& motionEvent4 = static_cast<MotionEvent&>(*inputEvent4);
    ASSERT_EQ(motionEvent4.getDownTime(), downTimeForWindow2);


    // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
    // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line
    window1->consumeMotionMove();
    window1->consumeMotionMove();
@@ -2094,16 +2104,12 @@ TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) {
    mDispatcher->notifyMotion(
    mDispatcher->notifyMotion(
            &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
            &(args = generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}})));
    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent5 = window1->consume();
    window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
    MotionEvent& motionEvent5 = static_cast<MotionEvent&>(*inputEvent5);
    ASSERT_EQ(motionEvent5.getDownTime(), downTimeForWindow1);


    mDispatcher->notifyMotion(&(
    mDispatcher->notifyMotion(&(
            args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
            args = generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})));
    mDispatcher->waitForIdle();
    mDispatcher->waitForIdle();
    InputEvent* inputEvent6 = window1->consume();
    window1->consumeMotionEvent(WithDownTime(downTimeForWindow1));
    MotionEvent& motionEvent6 = static_cast<MotionEvent&>(*inputEvent6);
    ASSERT_EQ(motionEvent6.getDownTime(), downTimeForWindow1);
}
}


TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) {
@@ -4693,6 +4699,7 @@ protected:


        const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
        const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event);
        assertMotionAction(expectedAction, motionEvent.getAction());
        assertMotionAction(expectedAction, motionEvent.getAction());
        ASSERT_EQ(points.size(), motionEvent.getPointerCount());


        for (size_t i = 0; i < points.size(); i++) {
        for (size_t i = 0; i < points.size(); i++) {
            float expectedX = points[i].x;
            float expectedX = points[i].x;