Skip to content

Commit

Permalink
tests/libgestures/gesturerecognizer: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taj-ny committed Oct 18, 2024
1 parent bfc3f79 commit c28352c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 239 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ Before reporting any issues related to gesture recognition, run ``libinput debug
Depending on the touchpad, 3 or 4-finger pinch gestures may sometimes be incorrectly interpreted as swipe gestures due to the touchpad only being able to track 2 fingers. As a workaround, move only 2 fingers in opposite directions. See https://wayland.freedesktop.org/libinput/doc/1.25.0/gestures.html#gestures-on-two-finger-touchpads for more information.

# Credits
- [KWin](https://invent.kde.org/plasma/kwin) - Gesture recognition code in early versions, sending keystrokes
- [KWin](https://invent.kde.org/plasma/kwin) - Gesture recognition code (parts of it), sending keystrokes
112 changes: 111 additions & 1 deletion tests/libgestures/gestures/test_gesturerecognizer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "test_gesturerecognizer_shared.h"
#include "test_gesturerecognizer.h"
#include <QSignalSpy>

Expand Down Expand Up @@ -86,6 +85,34 @@ void TestGestureRecognizer::gestureEnd_activeGesture_gestureEndedSignalEmittedAn
QVERIFY(returnValue);
}

void TestGestureRecognizer::holdGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue()
{
const auto gesture1 = std::make_shared<HoldGesture>();
gesture1->setThreshold(1);
gesture1->setFingers(2, 2);
gesture1->setTriggerWhenThresholdReached(true);
gesture1->setTriggerOneActionOnly(true);
gesture1->addAction(std::make_shared<GestureAction>());
const auto gesture2 = std::make_shared<HoldGesture>();
gesture2->setThreshold(1);
gesture2->setFingers(2, 2);
gesture2->setTriggerWhenThresholdReached(true);

gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue(
gesture1,
gesture2,
[this]()
{
m_gestureRecognizer->holdGestureBegin(2);
},
[this](bool &endedPrematurely)
{
m_gestureRecognizer->holdGestureUpdate(1, endedPrematurely);
return true;
}
);
}

void TestGestureRecognizer::pinchGestureUpdate_directions_data()
{
QTest::addColumn<PinchDirection>("direction");
Expand Down Expand Up @@ -125,6 +152,36 @@ void TestGestureRecognizer::pinchGestureUpdate_directions()
);
}

void TestGestureRecognizer::pinchGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue()
{
const auto gesture1 = std::make_shared<PinchGesture>();
gesture1->setDirection(PinchDirection::Expanding);
gesture1->setThreshold(0.1);
gesture1->setFingers(2, 2);
gesture1->setTriggerWhenThresholdReached(true);
gesture1->setTriggerOneActionOnly(true);
gesture1->addAction(std::make_shared<GestureAction>());
const auto gesture2 = std::make_shared<PinchGesture>();
gesture2->setDirection(PinchDirection::Expanding);
gesture2->setThreshold(0.1);
gesture2->setFingers(2, 2);
gesture2->setTriggerWhenThresholdReached(true);

gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue(
gesture1,
gesture2,
[this]()
{
m_gestureRecognizer->pinchGestureBegin(2);
},
[this](bool &endedPrematurely)
{
m_gestureRecognizer->pinchGestureUpdate(1.1, 0, QPointF(), endedPrematurely);
return true;
}
);
}

void TestGestureRecognizer::swipeGestureUpdate_directions_data()
{
QTest::addColumn<SwipeDirection>("direction");
Expand Down Expand Up @@ -198,6 +255,36 @@ void TestGestureRecognizer::swipeGestureUpdate_directions()
);
}

void TestGestureRecognizer::swipeGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue()
{
const auto gesture1 = std::make_shared<SwipeGesture>();
gesture1->setDirection(SwipeDirection::Right);
gesture1->setThreshold(1);
gesture1->setFingers(3, 3);
gesture1->setTriggerWhenThresholdReached(true);
gesture1->setTriggerOneActionOnly(true);
gesture1->addAction(std::make_shared<GestureAction>());
const auto gesture2 = std::make_shared<SwipeGesture>();
gesture2->setDirection(SwipeDirection::Right);
gesture2->setThreshold(1);
gesture2->setFingers(3, 3);
gesture2->setTriggerWhenThresholdReached(true);

gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue(
gesture1,
gesture2,
[this]()
{
m_gestureRecognizer->swipeGestureBegin(3);
},
[this](bool &endedPrematurely)
{
m_gestureRecognizer->swipeGestureUpdate(QPointF(1, 0), endedPrematurely);
return true;
}
);
}

void TestGestureRecognizer::gestureUpdate_directions(
std::shared_ptr<Gesture> gesture,
std::function<void()> gestureBegin,
Expand All @@ -217,6 +304,29 @@ void TestGestureRecognizer::gestureUpdate_directions(
QCOMPARE(updatedSpy.count(), correct ? 1 : 0);
}

void TestGestureRecognizer::gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue(
std::shared_ptr<Gesture> gesture1,
std::shared_ptr<Gesture> gesture2,
std::function<void()> gestureBegin,
std::function<bool(bool&)> gestureUpdate
)
{
const QSignalSpy spy1(gesture1.get(), &Gesture::updated);
const QSignalSpy spy2(gesture2.get(), &Gesture::updated);

m_gestureRecognizer->registerGesture(gesture1);
m_gestureRecognizer->registerGesture(gesture2);

gestureBegin();
bool endedPrematurely = false;
const bool returnValue = gestureUpdate(endedPrematurely);

QCOMPARE(spy1.count(), 1);
QCOMPARE(spy2.count(), 0);
QVERIFY(endedPrematurely);
QVERIFY(returnValue);
}

} // namespace libgestures

QTEST_MAIN(libgestures::TestGestureRecognizer)
Expand Down
15 changes: 11 additions & 4 deletions tests/libgestures/gestures/test_gesturerecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ private slots:
void gestureBegin_gestureConditionsNotSatisfied_hasNoActiveGestures();
void gestureBegin_twoGesturesWithSatisfiedConditions_hasTwoActiveGestures();

// void gestureUpdate_activeGestureAndWrongDirection_gestureCancelledSignalEmittedAndReturnsFalse();
// void gestureUpdate_activeGesture_gestureUpdateSignalEmittedExactlyOneTimeAndDeltaMatchesAndGestureNotEndedPrematurely();
// void gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdated();

void gestureCancel_twoActiveGestures_gestureCancelledSignalEmittedForAllGesturesAndActiveHoldGesturesCleared();

void gestureEnd_noActiveGestures_returnsFalse();
void gestureEnd_activeGesture_gestureEndedSignalEmittedAndActiveHoldGesturesClearedAndReturnsTrue();

void holdGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue();

void pinchGestureUpdate_directions_data();
void pinchGestureUpdate_directions();
void pinchGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue();

void swipeGestureUpdate_directions_data();
void swipeGestureUpdate_directions();
void swipeGestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue();
private:
void gestureUpdate_directions(
std::shared_ptr<Gesture> gesture,
Expand All @@ -36,6 +36,13 @@ private slots:
const bool &correct
);

void gestureUpdate_twoActiveGesturesAndOneEndsPrematurely_endedPrematurelySetToTrueAndOnlyOneGestureUpdatedAndReturnsTrue(
std::shared_ptr<Gesture> gesture1,
std::shared_ptr<Gesture> gesture2,
std::function<void()> gestureBegin,
std::function<bool(bool&)> gestureUpdate
);

std::shared_ptr<GestureRecognizer> m_gestureRecognizer;

std::shared_ptr<HoldGesture> m_hold2;
Expand Down
162 changes: 0 additions & 162 deletions tests/libgestures/gestures/test_gesturerecognizer_shared.cpp

This file was deleted.

Loading

0 comments on commit c28352c

Please sign in to comment.