diff options
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r-- | src/testlib/qtestcase.cpp | 218 |
1 files changed, 146 insertions, 72 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 4c5c9e1eb8..497470464f 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -275,6 +275,11 @@ namespace QTest static QObject *currentTestObject = 0; static QString mainSourcePath; +#if defined(Q_OS_MACOS) + bool macNeedsActivate = false; + IOPMAssertionID powerID; +#endif + class TestMethods { Q_DISABLE_COPY(TestMethods) public: @@ -578,6 +583,7 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) " -plugins dir : Specify a directory where to search for plugins.\n" " -input dir/file : Specify the root directory for test cases or a single test case file.\n" " -translation file : Specify the translation file.\n" + " -file-selector dir : Specify a file selector for the QML engine.\n" ); } @@ -896,6 +902,10 @@ void TestMethods::invokeTestOnData(int index) const if (m_cleanupMethod.isValid()) m_cleanupMethod.invoke(QTest::currentTestObject, Qt::DirectConnection); + // Process any deleteLater(), like event-loop based apps would do. Fixes memleak reports. + if (QCoreApplication::instance()) + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + // If the test isn't a benchmark, finalize the result after cleanup() has finished. if (!isBenchmark) QTestResult::finishedCurrentTestDataCleanup(); @@ -1269,6 +1279,16 @@ char *toPrettyCString(const char *p, int length) return buffer.take(); } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +// this used to be the signature up to and including Qt 5.9 +// keep it for BC reasons: +Q_TESTLIB_EXPORT +char *toPrettyUnicode(const ushort *p, int length) +{ + return toPrettyUnicode(QStringView(p, length)); +} +#endif + /*! \internal Returns the same QString but with only the ASCII characters still shown; @@ -1276,8 +1296,10 @@ char *toPrettyCString(const char *p, int length) Similar to QDebug::putString(). */ -char *toPrettyUnicode(const ushort *p, int length) +char *toPrettyUnicode(QStringView string) { + auto p = reinterpret_cast<const ushort *>(string.utf16()); + auto length = string.size(); // keep it simple for the vast majority of cases bool trimmed = false; QScopedArrayPointer<char> buffer(new char[256]); @@ -1343,9 +1365,7 @@ void TestMethods::invokeTests(QObject *testObject) const { const QMetaObject *metaObject = testObject->metaObject(); QTEST_ASSERT(metaObject); - QTestLog::startLogging(); QTestResult::setCurrentTestFunction("initTestCase"); - QTestTable::globalTestTable(); if (m_initTestCaseDataMethod.isValid()) m_initTestCaseDataMethod.invoke(testObject, Qt::DirectConnection); @@ -1370,7 +1390,7 @@ void TestMethods::invokeTests(QObject *testObject) const if (!QTestResult::skipCurrentTest() && !previousFailed) { for (int i = 0, count = int(m_methods.size()); i < count; ++i) { - const char *data = Q_NULLPTR; + const char *data = nullptr; if (i < QTest::testTags.size() && !QTest::testTags.at(i).isEmpty()) data = qstrdup(QTest::testTags.at(i).toLatin1().constData()); const bool ok = invokeTest(i, data, watchDog.data()); @@ -1390,9 +1410,6 @@ void TestMethods::invokeTests(QObject *testObject) const } QTestResult::finishedCurrentTestFunction(); QTestResult::setCurrentTestFunction(0); - QTestTable::clearGlobalTestTable(); - - QTestLog::stopLogging(); } #if defined(Q_OS_UNIX) @@ -1518,7 +1535,7 @@ class DebugSymbolResolver Q_DISABLE_COPY(DebugSymbolResolver) public: struct Symbol { - Symbol() : name(Q_NULLPTR), address(0) {} + Symbol() : name(nullptr), address(0) {} const char *name; // Must be freed by caller. DWORD64 address; @@ -1566,11 +1583,11 @@ void DebugSymbolResolver::cleanup() if (m_dbgHelpLib) FreeLibrary(m_dbgHelpLib); m_dbgHelpLib = 0; - m_symFromAddr = Q_NULLPTR; + m_symFromAddr = nullptr; } DebugSymbolResolver::DebugSymbolResolver(HANDLE process) - : m_process(process), m_dbgHelpLib(0), m_symFromAddr(Q_NULLPTR) + : m_process(process), m_dbgHelpLib(0), m_symFromAddr(nullptr) { bool success = false; m_dbgHelpLib = LoadLibraryW(L"dbghelp.dll"); @@ -1650,15 +1667,8 @@ static LONG WINAPI windowsFaultHandler(struct _EXCEPTION_POINTERS *exInfo) } #endif // Q_OS_WIN) && !Q_OS_WINRT -static void qputenvIfEmpty(const char *name, const QByteArray &value) -{ - if (qEnvironmentVariableIsEmpty(name)) - qputenv(name, value); -} - static void initEnvironment() { - qputenvIfEmpty("QT_LOGGING_TO_CONSOLE", "1"); qputenv("QT_QTESTLIB_RUNNING", "1"); } @@ -1702,23 +1712,27 @@ static void initEnvironment() int QTest::qExec(QObject *testObject, int argc, char **argv) { - initEnvironment(); - QBenchmarkGlobalData benchmarkData; - QBenchmarkGlobalData::current = &benchmarkData; + qInit(testObject, argc, argv); + int ret = qRun(); + qCleanup(); + return ret; +} -#ifdef QTESTLIB_USE_VALGRIND - int callgrindChildExitCode = 0; -#endif +/*! \internal + */ +void QTest::qInit(QObject *testObject, int argc, char **argv) +{ + initEnvironment(); + QBenchmarkGlobalData::current = new QBenchmarkGlobalData; #if defined(Q_OS_MACX) - bool macNeedsActivate = qApp && (qstrcmp(qApp->metaObject()->className(), "QApplication") == 0); - IOPMAssertionID powerID; + macNeedsActivate = qApp && (qstrcmp(qApp->metaObject()->className(), "QApplication") == 0); // Don't restore saved window state for auto tests. QTestPrivate::disableWindowRestore(); -#endif -#ifndef QT_NO_EXCEPTIONS - try { + + // Disable App Nap which may cause tests to stall. + QTestPrivate::AppNapDisabler appNapDisabler; #endif #if defined(Q_OS_MACX) @@ -1749,6 +1763,24 @@ int QTest::qExec(QObject *testObject, int argc, char **argv) qtest_qParseArgs(argc, argv, false); + QTestTable::globalTestTable(); + QTestLog::startLogging(); +} + +/*! \internal + */ +int QTest::qRun() +{ + QTEST_ASSERT(currentTestObject); + +#ifdef QTESTLIB_USE_VALGRIND + int callgrindChildExitCode = 0; +#endif + +#ifndef QT_NO_EXCEPTIONS + try { +#endif + #if defined(Q_OS_WIN) if (!noCrashHandler) { # ifndef Q_CC_MINGW @@ -1784,17 +1816,17 @@ int QTest::qExec(QObject *testObject, int argc, char **argv) for (const QString &tf : qAsConst(QTest::testFunctions)) { const QByteArray tfB = tf.toLatin1(); const QByteArray signature = tfB + QByteArrayLiteral("()"); - QMetaMethod m = TestMethods::findMethod(testObject, signature.constData()); + QMetaMethod m = TestMethods::findMethod(currentTestObject, signature.constData()); if (!m.isValid() || !isValidSlot(m)) { fprintf(stderr, "Unknown test function: '%s'. Possible matches:\n", tfB.constData()); qPrintTestSlots(stderr, tfB.constData()); - fprintf(stderr, "\n%s -functions\nlists all available test functions.\n", argv[0]); + fprintf(stderr, "\n%s -functions\nlists all available test functions.\n", QTestResult::currentAppName()); exit(1); } commandLineMethods.push_back(m); } - TestMethods test(testObject, commandLineMethods); - test.invokeTests(testObject); + TestMethods test(currentTestObject, commandLineMethods); + test.invokeTests(currentTestObject); } #ifndef QT_NO_EXCEPTIONS @@ -1819,16 +1851,6 @@ int QTest::qExec(QObject *testObject, int argc, char **argv) } #endif - currentTestObject = 0; - - QSignalDumper::endDump(); - -#if defined(Q_OS_MACX) - if (macNeedsActivate) { - IOPMAssertionRelease(powerID); - } -#endif - #ifdef QTESTLIB_USE_VALGRIND if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) return callgrindChildExitCode; @@ -1838,6 +1860,26 @@ int QTest::qExec(QObject *testObject, int argc, char **argv) return qMin(QTestLog::failCount(), 127); } +/*! \internal + */ +void QTest::qCleanup() +{ + currentTestObject = 0; + + QTestTable::clearGlobalTestTable(); + QTestLog::stopLogging(); + + delete QBenchmarkGlobalData::current; + QBenchmarkGlobalData::current = 0; + + QSignalDumper::endDump(); + +#if defined(Q_OS_MACOS) + if (macNeedsActivate) + IOPMAssertionRelease(powerID); +#endif +} + /*! \overload \since 4.4 @@ -2244,7 +2286,7 @@ QTestData &QTest::addRow(const char *format, ...) return *tbl->newData(buf); } -/*! \fn void QTest::addColumn(const char *name, T *dummy = 0) +/*! \fn template <typename T> void QTest::addColumn(const char *name, T *dummy = 0) Adds a column with type \c{T} to the current test data. \a name is the name of the column. \a dummy is a workaround @@ -2375,7 +2417,7 @@ bool QTest::compare_helper(bool success, const char *failureMsg, return QTestResult::compare(success, failureMsg, val1, val2, actual, expected, file, line); } -/*! \fn bool QTest::qCompare(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const float &t1, const float &t2, const char *actual, const char *expected, const char *file, int line) \internal */ bool QTest::qCompare(float const &t1, float const &t2, const char *actual, const char *expected, @@ -2385,7 +2427,7 @@ bool QTest::qCompare(float const &t1, float const &t2, const char *actual, const toString(t1), toString(t2), actual, expected, file, line); } -/*! \fn bool QTest::qCompare(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const double &t1, const double &t2, const char *actual, const char *expected, const char *file, int line) \internal */ bool QTest::qCompare(double const &t1, double const &t2, const char *actual, const char *expected, @@ -2395,11 +2437,11 @@ bool QTest::qCompare(double const &t1, double const &t2, const char *actual, con toString(t1), toString(t2), actual, expected, file, line); } -/*! \fn bool QTest::qCompare(double const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const double &t1, const float &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(float const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const float &t1, const double &t2, const char *actual, const char *expected, const char *file, int line) \internal */ @@ -2496,6 +2538,30 @@ char *QTest::toString(const void *p) return msg; } +/*! \fn char *QTest::toString(const QColor &color) + \internal + */ + +/*! \fn char *QTest::toString(const QRegion ®ion) + \internal + */ + +/*! \fn char *QTest::toString(const QHostAddress &addr) + \internal + */ + +/*! \fn char *QTest::toString(QNetworkReply::NetworkError code) + \internal + */ + +/*! \fn char *QTest::toString(const QNetworkCookie &cookie) + \internal + */ + +/*! \fn char *QTest::toString(const QList<QNetworkCookie> &list) + \internal + */ + /*! \internal */ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *actual, @@ -2510,51 +2576,59 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac \internal */ -/*! \fn bool QTest::compare_ptr_helper(const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line); +/*! \fn bool QTest::compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual, const char *expected, const char *file, int line) + \internal +*/ + +/*! \fn bool QTest::compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line) + \internal +*/ + +/*! \fn bool QTest::compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int); +/*! \fn template <typename T1, typename T2> bool QTest::qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QIcon &t1, const QIcon &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QImage const &t1, QImage const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QImage &t1, const QImage &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QPixmap const &t1, QPixmap const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QPixmap &t1, const QPixmap &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(const T &t1, const T &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(T *t, std::nullptr_t, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(T *t, std::nullptr_t, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(std::nullptr_t, T *t, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(std::nullptr_t, T *t, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T1, typename T2> bool QTest::qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T1, typename T2> bool QTest::qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line) \internal */ @@ -2574,55 +2648,55 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac \internal */ -/*! \fn bool QTest::qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QString &t1, const QLatin1String &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QLatin1String const &t1, QString const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QLatin1String &t1, const QString &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const QStringList &t1, const QStringList &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(const QList<T> &t1, const QList<T> &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(const QFlags<T> &t1, const T &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qCompare(const QFlags<T> &t1, const int &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(qint64 const &t1, qint32 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const qint64 &t1, const qint32 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(qint64 const &t1, quint32 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const qint64 &t1, const quint32 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(quint64 const &t1, quint32 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const quint64 &t1, const quint32 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(qint32 const &t1, qint64 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const qint32 &t1, const qint64 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(quint32 const &t1, qint64 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const quint32 &t1, const qint64 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qCompare(quint32 const &t1, quint64 const &t2, const char *actual, const char *expected, const char *file, int line) +/*! \fn bool QTest::qCompare(const quint32 &t1, const quint64 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ -/*! \fn bool QTest::qTest(const T& actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line) +/*! \fn template <typename T> bool QTest::qTest(const T& actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line) \internal */ |