diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/testlib/qtestcase.cpp | 14 | ||||
-rw-r--r-- | src/testlib/qtestcase.h | 12 | ||||
-rw-r--r-- | src/testlib/qtesteventloop.h | 2 |
3 files changed, 22 insertions, 6 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 99be5afaef..2c9f2d4049 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -468,6 +468,7 @@ class WatchDog; static QObject *currentTestObject = nullptr; static QString mainSourcePath; +static bool inTestFunction = false; #if defined(Q_OS_MACOS) static IOPMAssertionID macPowerSavingDisabled = 0; @@ -1120,6 +1121,7 @@ void TestMethods::invokeTestOnData(int index) const /* Benchmarking: for each accumulation iteration*/ bool invokeOk; do { + QTest::inTestFunction = true; if (m_initMethod.isValid()) m_initMethod.invoke(QTest::currentTestObject, Qt::DirectConnection); @@ -1141,6 +1143,7 @@ void TestMethods::invokeTestOnData(int index) const invokeOk = false; } + QTest::inTestFunction = false; QTestResult::finishedCurrentTestData(); if (!initQuit) { @@ -3012,6 +3015,17 @@ bool QTest::currentTestFailed() return QTestResult::currentTestFailed(); } +/* + Returns \c true during the run of the test-function and its set-up. + + Used by the \c{QTRY_*} macros and \l QTestEventLoop to check whether to + return when QTest::currentTestFailed() is true. +*/ +bool QTest::runningTest() +{ + return QTest::inTestFunction; +} + /*! \internal */ QObject *QTest::testObject() diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 3d268135a4..6affbf1507 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -148,9 +148,10 @@ inline void useVerifyThrowsException() {} /* Ideally we would adapt qWaitFor(), or a variant on it, to implement roughly * what the following provides as QTRY_LOOP_IMPL(); however, for now, the * reporting of how much to increase the timeout to (if within a factor of two) - * on failure and the check for QTest::currentTestFailed() go beyond - * qWaitFor(). (We no longer care about the bug in MSVC < 2017 that precluded - * using qWaitFor() in the implementation here, see QTBUG-59096.) + * on failure and the check for (QTest::runningTest() && + * QTest::currentTestFailed()) go beyond qWaitFor(). (We no longer care about + * the bug in MSVC < 2017 that precluded using qWaitFor() in the implementation + * here, see QTBUG-59096.) */ // NB: not do {...} while (0) wrapped, as qt_test_i is accessed after it @@ -159,14 +160,14 @@ inline void useVerifyThrowsException() {} QTest::qWait(0); \ } \ int qt_test_i = 0; \ - for (; qt_test_i < timeoutValue && !QTest::currentTestFailed() \ + for (; qt_test_i < timeoutValue && !(QTest::runningTest() && QTest::currentTestFailed()) \ && !(expr); qt_test_i += step) { \ QTest::qWait(step); \ } // Ends in a for-block, so doesn't want a following semicolon. #define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step) \ - if (!QTest::currentTestFailed() && !(expr)) { \ + if (!(QTest::runningTest() && QTest::currentTestFailed()) && !(expr)) { \ QTRY_LOOP_IMPL(expr, 2 * (timeoutValue), step) \ if ((expr)) { \ QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(\ @@ -419,6 +420,7 @@ namespace QTest Q_TESTLIB_EXPORT const char *currentTestFunction(); Q_TESTLIB_EXPORT const char *currentDataTag(); Q_TESTLIB_EXPORT bool currentTestFailed(); + Q_TESTLIB_EXPORT bool runningTest(); // Internal, for use by macros and QTestEventLoop. Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii); Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key); diff --git a/src/testlib/qtesteventloop.h b/src/testlib/qtesteventloop.h index a8126bc333..2ac8bdecdf 100644 --- a/src/testlib/qtesteventloop.h +++ b/src/testlib/qtesteventloop.h @@ -60,7 +60,7 @@ inline void QTestEventLoop::enterLoopMSecs(int ms) Q_ASSERT(!loop); _timeout = false; - if (QTest::currentTestFailed()) + if (QTest::runningTest() && QTest::currentTestFailed()) return; QEventLoop l; |