From 37bc11e707d3f29fa81675bcc41610ab65c2b314 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 28 Sep 2021 15:12:48 +0200 Subject: Refactor QTEST*_MAIN() implementations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The various variants duplicated some rather complex code around varying setup in the middle. Rework in terms of a macro that defines main() and takes the setup code as a parameter. That setup code also had some common structure, so package that in a setup macro that takes the class to be used. Reworked various testlib selftests that were using QTEST_MAIN_IMPL(); change to use the new QTEST_MAIN_WRAPPER() and TEST_MAIN_SETUP(). These might be better dealt with by supporting a second form of the initMain() test-setup function in the test classes, that takes references for argc and argv, to let a test massage its command-line options. Change-Id: I7fb16b38d51c80ba2f5c9c82f3b7a37ffc636795 Reviewed-by: Tor Arne Vestbø --- src/testlib/qtest.h | 91 +++++++++++++++++++---------------------------------- 1 file changed, 32 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 894c59f39b..20c80058ac 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2019 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Copyright (C) 2020 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** @@ -612,84 +612,57 @@ struct QtCoverageScanner #define TESTLIB_SELFCOVERAGE_START(name) #endif -#define QTEST_APPLESS_MAIN(TestObject) \ +// Internal (but used by some testlib selftests to hack argc and argv). +// Tests should normally implement initMain() if they have set-up to do before +// instantiating the test class. +#define QTEST_MAIN_WRAPPER(TestObject, ...) \ int main(int argc, char *argv[]) \ { \ - TESTLIB_SELFCOVERAGE_START(TestObject) \ + TESTLIB_SELFCOVERAGE_START(#TestObject) \ QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)(); \ + __VA_ARGS__ \ TestObject tc; \ QTEST_SET_MAIN_SOURCE_PATH \ return QTest::qExec(&tc, argc, argv); \ } +// For when you don't even want a QApplication: +#define QTEST_APPLESS_MAIN(TestObject) QTEST_MAIN_WRAPPER(TestObject) + #include #if defined(QT_NETWORK_LIB) # include #endif -#if defined(QT_WIDGETS_LIB) - -#include - -#ifdef QT_KEYPAD_NAVIGATION -# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone); -#else -# define QTEST_DISABLE_KEYPAD_NAVIGATION -#endif - -#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 \ - TestObject tc; \ - QTEST_SET_MAIN_SOURCE_PATH \ - return QTest::qExec(&tc, argc, argv); +// Internal +#define QTEST_QAPP_SETUP(klaz) \ + klaz app(argc, argv); \ + app.setAttribute(Qt::AA_Use96Dpi, true); +#if defined(QT_WIDGETS_LIB) +# include +# ifdef QT_KEYPAD_NAVIGATION +# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone); +# else +# define QTEST_DISABLE_KEYPAD_NAVIGATION +# endif +// Internal +# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QApplication) QTEST_DISABLE_KEYPAD_NAVIGATION #elif defined(QT_GUI_LIB) - -#include - -#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; \ - QTEST_SET_MAIN_SOURCE_PATH \ - return QTest::qExec(&tc, argc, argv); - +# include +// Internal +# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QGuiApplication) #else - -#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; \ - QTEST_SET_MAIN_SOURCE_PATH \ - return QTest::qExec(&tc, argc, argv); - +// Internal +# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QCoreApplication) #endif // QT_GUI_LIB -#define QTEST_MAIN(TestObject) \ -int main(int argc, char *argv[]) \ -{ \ - QTEST_MAIN_IMPL(TestObject) \ -} +// For most tests: +#define QTEST_MAIN(TestObject) QTEST_MAIN_WRAPPER(TestObject, QTEST_MAIN_SETUP()) +// For command-line tests #define QTEST_GUILESS_MAIN(TestObject) \ -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; \ - QTEST_SET_MAIN_SOURCE_PATH \ - return QTest::qExec(&tc, argc, argv); \ -} + QTEST_MAIN_WRAPPER(TestObject, QTEST_QAPP_SETUP(QCoreApplication)) #endif -- cgit v1.2.3