diff --git a/src/testlib/doc/src/qttestlib-manual.qdoc b/src/testlib/doc/src/qttestlib-manual.qdoc index 71b4541313..65836d0706 100644 --- a/src/testlib/doc/src/qttestlib-manual.qdoc +++ b/src/testlib/doc/src/qttestlib-manual.qdoc @@ -107,6 +107,11 @@ Example: \snippet code/doc_src_qtestlib.cpp 0 + Finally, if the test class has a static public \c{void initMain()} method, + it is called by the QTEST_MAIN macros before the QApplication object + is instantiated. For example, this allows for setting application + attributes like Qt::AA_DisableHighDpiScaling. This was added in 5.14. + For more examples, refer to the \l{Qt Test Tutorial}. \if !defined(qtforpython) diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index cdf0800371..27fe08e8f4 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -379,8 +379,36 @@ inline bool qCompare(quint32 const &t1, quint64 const &t2, const char *actual, { return qCompare(static_cast(t1), t2, actual, expected, file, line); } +namespace Internal { +template +class HasInitMain // SFINAE test for the presence of initMain() +{ +private: + using YesType = char[1]; + using NoType = char[2]; + + template static YesType& test( decltype(&C::initMain) ) ; + template static NoType& test(...); + +public: + enum { value = sizeof(test(nullptr)) == sizeof(YesType) }; +}; + +template +typename std::enable_if::value, void>::type callInitMain() +{ + T::initMain(); } + +template +typename std::enable_if::value, void>::type callInitMain() +{ +} + +} // namespace Internal + +} // namespace QTest QT_END_NAMESPACE #ifdef QT_TESTCASE_BUILDDIR @@ -441,6 +469,7 @@ int main(int argc, char *argv[]) \ #define QTEST_MAIN_IMPL(TestObject) \ TESTLIB_SELFCOVERAGE_START(#TestObject) \ + QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)(); \ QApplication app(argc, argv); \ app.setAttribute(Qt::AA_Use96Dpi, true); \ QTEST_DISABLE_KEYPAD_NAVIGATION \ @@ -454,6 +483,7 @@ int main(int argc, char *argv[]) \ #define QTEST_MAIN_IMPL(TestObject) \ TESTLIB_SELFCOVERAGE_START(#TestObject) \ + QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)(); \ QGuiApplication app(argc, argv); \ app.setAttribute(Qt::AA_Use96Dpi, true); \ TestObject tc; \ @@ -464,6 +494,7 @@ int main(int argc, char *argv[]) \ #define QTEST_MAIN_IMPL(TestObject) \ TESTLIB_SELFCOVERAGE_START(#TestObject) \ + QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)(); \ QCoreApplication app(argc, argv); \ app.setAttribute(Qt::AA_Use96Dpi, true); \ TestObject tc; \ @@ -482,6 +513,7 @@ int main(int argc, char *argv[]) \ int main(int argc, char *argv[]) \ { \ TESTLIB_SELFCOVERAGE_START(#TestObject) \ + QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)(); \ QCoreApplication app(argc, argv); \ app.setAttribute(Qt::AA_Use96Dpi, true); \ TestObject tc; \ diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 262dbea913..8e0bdac520 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -47,7 +47,11 @@ class tst_QTimer : public QObject { Q_OBJECT +public: + static void initMain(); + private slots: + void cleanupTestCase(); void zeroTimer(); void singleShotTimeout(); void timeout(); @@ -1138,22 +1142,27 @@ struct StaticSingleShotUser } }; +// NOTE: to prevent any static initialization order fiasco, we implement +// initMain() to instantiate staticSingleShotUser before qApp + static StaticSingleShotUser *s_staticSingleShotUser = nullptr; +void tst_QTimer::initMain() +{ + s_staticSingleShotUser = new StaticSingleShotUser; +} + +void tst_QTimer::cleanupTestCase() +{ + delete s_staticSingleShotUser; +} + void tst_QTimer::singleShot_static() { QCoreApplication::processEvents(); QCOMPARE(s_staticSingleShotUser->helper.calls, s_staticSingleShotUser->calls()); } -// NOTE: to prevent any static initialization order fiasco, we handle QTEST_MAIN -// ourselves, but instantiate the staticSingleShotUser before qApp - -int main(int argc, char *argv[]) -{ - StaticSingleShotUser staticSingleShotUser; - s_staticSingleShotUser = &staticSingleShotUser; - QTEST_MAIN_IMPL(tst_QTimer) -} +QTEST_MAIN(tst_QTimer) #include "tst_qtimer.moc" diff --git a/tests/auto/opengl/qgl/tst_qgl.cpp b/tests/auto/opengl/qgl/tst_qgl.cpp index 053c4b026b..3cc9592fb1 100644 --- a/tests/auto/opengl/qgl/tst_qgl.cpp +++ b/tests/auto/opengl/qgl/tst_qgl.cpp @@ -60,6 +60,8 @@ public: tst_QGL(); virtual ~tst_QGL(); + static void initMain(); + private slots: void initTestCase(); void getSetCheck(); @@ -101,6 +103,11 @@ tst_QGL::~tst_QGL() { } +void tst_QGL::initMain() +{ + QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); +} + void tst_QGL::initTestCase() { QGLWidget glWidget; diff --git a/tests/auto/testlib/initmain/initmain.pro b/tests/auto/testlib/initmain/initmain.pro new file mode 100644 index 0000000000..4c12aba08d --- /dev/null +++ b/tests/auto/testlib/initmain/initmain.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +SOURCES += tst_initmain.cpp +QT = core testlib + +TARGET = tst_initmain diff --git a/tests/auto/testlib/initmain/tst_initmain.cpp b/tests/auto/testlib/initmain/tst_initmain.cpp new file mode 100644 index 0000000000..f08f82c5ec --- /dev/null +++ b/tests/auto/testlib/initmain/tst_initmain.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include +#include + +class tst_InitMain : public QObject +{ + Q_OBJECT + +public: + static void initMain() { m_initMainCalled = true; } + +private slots: + void testcase(); + +private: + static bool m_initMainCalled; +}; + +bool tst_InitMain::m_initMainCalled = false; + +void tst_InitMain::testcase() +{ + QVERIFY(m_initMainCalled); +} + +QTEST_MAIN(tst_InitMain) + +#include "tst_initmain.moc" diff --git a/tests/auto/testlib/testlib.pro b/tests/auto/testlib/testlib.pro index 587c76a189..55f1f9b87b 100644 --- a/tests/auto/testlib/testlib.pro +++ b/tests/auto/testlib/testlib.pro @@ -1,5 +1,6 @@ TEMPLATE = subdirs SUBDIRS = \ + initmain \ outformat \ qsignalspy \ selftests \