diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2022-11-18 15:30:32 +0100 |
---|---|---|
committer | Axel Spoerl <axel.spoerl@qt.io> | 2023-01-09 19:37:53 +0000 |
commit | 58afdea1b32899cc4ee8bf2062596ed523bbdffe (patch) | |
tree | 40bbec95a217ad71d9544aa82bf8518054cbec58 /tests/auto/other | |
parent | 49a63d375972079ae3000c8b7d512d58d4de32bb (diff) |
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.
Pick-to: 6.5 6.4 6.2
Change-Id: I4fe9509f35474514ef55191d799e6707199fe853
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'tests/auto/other')
-rw-r--r-- | tests/auto/other/gestures/tst_gestures.cpp | 68 |
1 files changed, 49 insertions, 19 deletions
diff --git a/tests/auto/other/gestures/tst_gestures.cpp b/tests/auto/other/gestures/tst_gestures.cpp index f8828da71a..4d87ee03df 100644 --- a/tests/auto/other/gestures/tst_gestures.cpp +++ b/tests/auto/other/gestures/tst_gestures.cpp @@ -282,6 +282,10 @@ Q_OBJECT private slots: void initTestCase(); void cleanupTestCase(); + + void init(); + void cleanup(); + void customGesture(); void autoCancelingGestures(); void gestureOverChild(); @@ -324,15 +328,23 @@ 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); |