summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2019-07-30 10:16:53 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2019-08-13 19:07:05 +0200
commitecb6327762e58a83309027da90ebb953411a264e (patch)
tree169dfa41a4e5053e45e681d707f8adc0fba54a9f
parent02ae522f54e93af46fb7cae5fcbc630a804cab50 (diff)
QTestLib: Introduce initMain() to run in main before qApp exists
When running Qt autotests on a developer machine with a high resolution, failures occur due to either some widget becoming too small, some rounding fuzz appearing when Qt High DPI scaling is active, or some test taking screenshots failing to deal with device pixel ratios != 1 in the obtained pixmaps. It is not feasible to adapt all tests to pass on high resolution monitors in both modes (Qt High DPI scaling enabled/disabled). It should be possible to specify the High DPI setting per test. Previously, it was not possible to set the Qt High DPI scaling attributes since they must be applied before QApplication instantiation. Enable this by checking for the presence of a static void initMain() function on the test object and invoking it before QApplication instantiation. Prototypically use it in tst_qtimer and to turn off High DPI scaling for tst_QGL. [ChangeLog][QtTestLib] It is now possible to perform static initialization before QApplication instantiation by implementing a initMain() function in the test class. Change-Id: Idec0134b189710a14c41a451fa8445bc0c5b1cf3 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r--src/testlib/doc/src/qttestlib-manual.qdoc5
-rw-r--r--src/testlib/qtest.h32
-rw-r--r--tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp27
-rw-r--r--tests/auto/opengl/qgl/tst_qgl.cpp7
-rw-r--r--tests/auto/testlib/initmain/initmain.pro5
-rw-r--r--tests/auto/testlib/initmain/tst_initmain.cpp56
-rw-r--r--tests/auto/testlib/testlib.pro1
7 files changed, 124 insertions, 9 deletions
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<quint64>(t1), t2, actual, expected, file, line);
}
+namespace Internal {
+template <typename T>
+class HasInitMain // SFINAE test for the presence of initMain()
+{
+private:
+ using YesType = char[1];
+ using NoType = char[2];
+
+ template <typename C> static YesType& test( decltype(&C::initMain) ) ;
+ template <typename C> static NoType& test(...);
+
+public:
+ enum { value = sizeof(test<T>(nullptr)) == sizeof(YesType) };
+};
+
+template<typename T>
+typename std::enable_if<HasInitMain<T>::value, void>::type callInitMain()
+{
+ T::initMain();
+}
+
+template<typename T>
+typename std::enable_if<!HasInitMain<T>::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)<TestObject>(); \
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)<TestObject>(); \
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)<TestObject>(); \
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)<TestObject>(); \
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::singleShot_static()
+void tst_QTimer::initMain()
{
- QCoreApplication::processEvents();
- QCOMPARE(s_staticSingleShotUser->helper.calls, s_staticSingleShotUser->calls());
+ s_staticSingleShotUser = new StaticSingleShotUser;
}
-// NOTE: to prevent any static initialization order fiasco, we handle QTEST_MAIN
-// ourselves, but instantiate the staticSingleShotUser before qApp
+void tst_QTimer::cleanupTestCase()
+{
+ delete s_staticSingleShotUser;
+}
-int main(int argc, char *argv[])
+void tst_QTimer::singleShot_static()
{
- StaticSingleShotUser staticSingleShotUser;
- s_staticSingleShotUser = &staticSingleShotUser;
- QTEST_MAIN_IMPL(tst_QTimer)
+ QCoreApplication::processEvents();
+ QCOMPARE(s_staticSingleShotUser->helper.calls, s_staticSingleShotUser->calls());
}
+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 <QtCore/QCoreApplication>
+#include <QtTest/QtTest>
+
+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 \