From c260d3062de83d7f051e531007771455915285e5 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 19 Jan 2018 09:02:39 +0100 Subject: Add QUICK_TEST_MAIN_WITH_SETUP to allow executing C++ before a QML test This macro is the same as QUICK_TEST_MAIN, but takes the user's QObject subclass as an argument, and calls pre-defined slots/invokable functions on it, similar to how e.g. init() is called for C++ tests. This allows e.g. context properties to be set for the QML tests. By basing the API on invokable functions, we give ourselves the freedom to easily add more functions in the future. [ChangeLog][QtQuickTest] Added QUICK_TEST_MAIN_WITH_SETUP macro to allow executing C++ before a QML test (such as registering context properties). Task-number: QTBUG-50064 Change-Id: Id566e388553811c220871248403d32545f8ae1eb Reviewed-by: Shawn Rutledge --- src/qmltest/quicktest.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/qmltest/quicktest.cpp') diff --git a/src/qmltest/quicktest.cpp b/src/qmltest/quicktest.cpp index 463833a100..817f9a5389 100644 --- a/src/qmltest/quicktest.cpp +++ b/src/qmltest/quicktest.cpp @@ -326,6 +326,11 @@ private: }; int quick_test_main(int argc, char **argv, const char *name, const char *sourceDir) +{ + return quick_test_main_with_setup(argc, argv, name, sourceDir, nullptr); +} + +int quick_test_main_with_setup(int argc, char **argv, const char *name, const char *sourceDir, QObject *setup) { // Peek at arguments to check for '-widgets' argument #ifdef QT_QMLTEST_WITH_WIDGETS @@ -516,6 +521,14 @@ int quick_test_main(int argc, char **argv, const char *name, const char *sourceD view.rootContext()->setContextProperty (QLatin1String("qtest"), QTestRootObject::instance()); // Deprecated. Use QTestRootObject from Qt.test.qtestroot instead + // Do this down here so that import paths, plugin paths, + // file selectors, etc. are available in case the user needs access to them. + if (setup) { + // Don't check the return value; it's OK if it doesn't exist. + // If we add more callbacks in the future, it makes sense if the user only implements one of them. + QMetaObject::invokeMethod(setup, "qmlEngineAvailable", Q_ARG(QQmlEngine*, view.engine())); + } + view.setObjectName(fi.baseName()); view.setTitle(view.objectName()); QTestRootObject::instance()->init(); -- cgit v1.2.3