diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-07-30 10:16:53 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-08-13 19:07:05 +0200 |
commit | ecb6327762e58a83309027da90ebb953411a264e (patch) | |
tree | 169dfa41a4e5053e45e681d707f8adc0fba54a9f /src/testlib/qtest.h | |
parent | 02ae522f54e93af46fb7cae5fcbc630a804cab50 (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>
Diffstat (limited to 'src/testlib/qtest.h')
-rw-r--r-- | src/testlib/qtest.h | 32 |
1 files changed, 32 insertions, 0 deletions
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; \ |