tst_Gestures: Don't accumulate global state

(Un)Register the custom recognize in init() and cleanup() instead of
initTestCase() and cleanupTestCase(), so that a new recognizer is used
for each test function.

In the test functions, use a scope guard to unregister the locally
registered recognizers to make sure that in the case of a failing test
and early return, the recognizer is removed.

Change-Id: I4fe9509f35474514ef55191d799e6707199fe853
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 58afdea1b32899cc4ee8bf2062596ed523bbdffe)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2022-11-18 15:30:32 +01:00 committed by Qt Cherry-pick Bot
parent 3acc8fa9d3
commit 60a2136533

View File

@ -282,6 +282,10 @@ Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void customGesture();
void autoCancelingGestures();
void gestureOverChild();
@ -324,14 +328,22 @@ private:
void tst_Gestures::initTestCase()
{
CustomGesture::GestureType = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
QVERIFY(CustomGesture::GestureType != Qt::GestureType(0));
QVERIFY(CustomGesture::GestureType != Qt::CustomGesture);
const QScreen *screen = QGuiApplication::primaryScreen();
m_availableTopLeft = screen->availableGeometry().topLeft();
}
void tst_Gestures::cleanupTestCase()
{
}
void tst_Gestures::init()
{
CustomGesture::GestureType = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
QVERIFY(CustomGesture::GestureType != Qt::GestureType(0));
QVERIFY(CustomGesture::GestureType != Qt::CustomGesture);
}
void tst_Gestures::cleanup()
{
QGestureRecognizer::unregisterRecognizer(CustomGesture::GestureType);
}
@ -588,6 +600,9 @@ void tst_Gestures::conflictingGestures()
child->reset();
Qt::GestureType ContinuousGesture = QGestureRecognizer::registerRecognizer(new CustomContinuousGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([ContinuousGesture]{
QGestureRecognizer::unregisterRecognizer(ContinuousGesture);
});
static const int ContinuousGestureEventsCount = CustomGesture::SerialFinishedThreshold - CustomGesture::SerialMaybeThreshold + 1;
child->grabGesture(ContinuousGesture);
// child accepts override. And it also receives another custom gesture.
@ -600,8 +615,6 @@ void tst_Gestures::conflictingGestures()
QCOMPARE(child->events.all.size(), TotalGestureEventsCount + ContinuousGestureEventsCount);
QCOMPARE(parent.gestureOverrideEventsReceived, 0);
QCOMPARE(parent.gestureEventsReceived, 0);
QGestureRecognizer::unregisterRecognizer(ContinuousGesture);
}
void tst_Gestures::finishedWithoutStarted()
@ -1159,6 +1172,9 @@ void tst_Gestures::twoGesturesOnDifferentLevel()
l->addWidget(child);
Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([SecondGesture]{
QGestureRecognizer::unregisterRecognizer(SecondGesture);
});
parent.grabGesture(CustomGesture::GestureType);
child->grabGesture(SecondGesture);
@ -1185,8 +1201,6 @@ void tst_Gestures::twoGesturesOnDifferentLevel()
QCOMPARE(parent.gestureOverrideEventsReceived, 0);
for(int i = 0; i < child->events.all.size(); ++i)
QCOMPARE(parent.events.all.at(i), CustomGesture::GestureType);
QGestureRecognizer::unregisterRecognizer(SecondGesture);
}
void tst_Gestures::multipleGesturesInTree()
@ -1200,6 +1214,10 @@ void tst_Gestures::multipleGesturesInTree()
Qt::GestureType FirstGesture = CustomGesture::GestureType;
Qt::GestureType SecondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
Qt::GestureType ThirdGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([SecondGesture, ThirdGesture]{
QGestureRecognizer::unregisterRecognizer(SecondGesture);
QGestureRecognizer::unregisterRecognizer(ThirdGesture);
});
Qt::GestureFlags flags = Qt::ReceivePartialGestures;
A->grabGesture(FirstGesture, flags); // A [1 3]
@ -1256,9 +1274,6 @@ void tst_Gestures::multipleGesturesInTree()
QCOMPARE(A->events.all.count(FirstGesture), TotalGestureEventsCount);
QCOMPARE(A->events.all.count(SecondGesture), 0);
QCOMPARE(A->events.all.count(ThirdGesture), TotalGestureEventsCount);
QGestureRecognizer::unregisterRecognizer(SecondGesture);
QGestureRecognizer::unregisterRecognizer(ThirdGesture);
}
void tst_Gestures::multipleGesturesInComplexTree()
@ -1276,6 +1291,14 @@ void tst_Gestures::multipleGesturesInComplexTree()
Qt::GestureType FifthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
Qt::GestureType SixthGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
Qt::GestureType SeventhGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([=]{
QGestureRecognizer::unregisterRecognizer(SecondGesture);
QGestureRecognizer::unregisterRecognizer(ThirdGesture);
QGestureRecognizer::unregisterRecognizer(FourthGesture);
QGestureRecognizer::unregisterRecognizer(FifthGesture);
QGestureRecognizer::unregisterRecognizer(SixthGesture);
QGestureRecognizer::unregisterRecognizer(SeventhGesture);
});
Qt::GestureFlags flags = Qt::ReceivePartialGestures;
A->grabGesture(FirstGesture, flags); // A [1,3,4]
@ -1353,13 +1376,6 @@ void tst_Gestures::multipleGesturesInComplexTree()
QCOMPARE(A->events.all.count(FifthGesture), 0);
QCOMPARE(A->events.all.count(SixthGesture), 0);
QCOMPARE(A->events.all.count(SeventhGesture), 0);
QGestureRecognizer::unregisterRecognizer(SecondGesture);
QGestureRecognizer::unregisterRecognizer(ThirdGesture);
QGestureRecognizer::unregisterRecognizer(FourthGesture);
QGestureRecognizer::unregisterRecognizer(FifthGesture);
QGestureRecognizer::unregisterRecognizer(SixthGesture);
QGestureRecognizer::unregisterRecognizer(SeventhGesture);
}
void tst_Gestures::testMapToScene()
@ -1495,6 +1511,9 @@ void tst_Gestures::autoCancelGestures()
};
const Qt::GestureType secondGesture = QGestureRecognizer::registerRecognizer(new CustomGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([secondGesture]{
QGestureRecognizer::unregisterRecognizer(secondGesture);
});
MockWidget parent("parent"); // this one sets the cancel policy to CancelAllInContext
parent.resize(300, 100);
@ -1550,6 +1569,9 @@ void tst_Gestures::autoCancelGestures2()
};
const Qt::GestureType secondGesture = QGestureRecognizer ::registerRecognizer(new CustomGestureRecognizer);
auto unregisterRecognizer = qScopeGuard([secondGesture]{
QGestureRecognizer::unregisterRecognizer(secondGesture);
});
QGraphicsScene scene;
QGraphicsView view(&scene);
@ -2057,10 +2079,14 @@ void tst_Gestures::testQGestureRecognizerCleanup()
// Mimic QGestureManager: register both default and "platform" recognizers
// (this is done in windows when QT_NO_NATIVE_GESTURES is not defined)
PanRecognizer *def = new PanRecognizer(PanRecognizer::Default);
QGestureRecognizer::registerRecognizer(def);
auto defRecognizer = QGestureRecognizer::registerRecognizer(def);
PanRecognizer *plt = new PanRecognizer(PanRecognizer::Platform);
QGestureRecognizer::registerRecognizer(plt);
auto pltRecognizer = QGestureRecognizer::registerRecognizer(plt);
qDebug () << "register: default =" << def << "; platform =" << plt;
auto unregisterRecognizer = qScopeGuard([defRecognizer, pltRecognizer]{
QGestureRecognizer::unregisterRecognizer(defRecognizer);
QGestureRecognizer::unregisterRecognizer(pltRecognizer);
});
// ^-- Qt singleton QGManager initialization
@ -2174,6 +2200,10 @@ void tst_Gestures::testReuseCanceledGestures()
new ReuseCanceledGesturesRecognizer(ReuseCanceledGesturesRecognizer::RmbAndCancelAllType));
Qt::GestureType tapGestureTypeId = QGestureRecognizer::registerRecognizer(
new ReuseCanceledGesturesRecognizer(ReuseCanceledGesturesRecognizer::LmbType));
auto unregisterRecognizer = qScopeGuard([=]{
QGestureRecognizer::unregisterRecognizer(cancellingGestureTypeId);
QGestureRecognizer::unregisterRecognizer(tapGestureTypeId);
});
QMainWindow mw;
mw.setWindowFlags(Qt::X11BypassWindowManagerHint);