tests: change testqml to allow direct register of C++ objects

Signed-off-by: Jan Iversen <jani@apache.org>
This commit is contained in:
jan Iversen 2018-09-08 18:18:16 +02:00 committed by Dirk Hohndel
parent d0edc29636
commit d5bbfac6a3
3 changed files with 38 additions and 37 deletions

View file

@ -115,7 +115,7 @@ TEST(TestQPrefProxy testqPrefProxy.cpp)
TEST(TestQPrefTechnicalDetails testqPrefTechnicalDetails.cpp) TEST(TestQPrefTechnicalDetails testqPrefTechnicalDetails.cpp)
TEST(TestQPrefUnits testqPrefUnits.cpp) TEST(TestQPrefUnits testqPrefUnits.cpp)
TEST(TestQPrefUpdateManager testqPrefUpdateManager.cpp) TEST(TestQPrefUpdateManager testqPrefUpdateManager.cpp)
add_test(NAME TestQML COMMAND $<TARGET_FILE:TestQML> ${SUBSURFACE_SOURCE}/tests) add_test(NAME TestQML COMMAND $<TARGET_FILE:TestQML> -input ${SUBSURFACE_SOURCE}/tests)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}

View file

@ -1,47 +1,29 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include <QApplication> #include "testqml.h"
#include <QQmlContext>
#include <QQmlEngine>
#include <QtQuickTest>
#include <QtTest>
#include "core/qt-gui.h"
#include "core/settings/qPref.h" #include "core/settings/qPref.h"
// this is the content of QUICK_TEST_MAIN amended with #include <QtQuickTest>
// registration of ssrf classes
// main loosely copied from QUICK_TEST_MAIN_WITH_SETUP macro
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// QML testing is not supported in the oldest Qt versions we support #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
// if running with Qt version less than 5.10 then skip test return 0;
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) #else
QTEST_ADD_GPU_BLACKLIST_SUPPORT QTEST_ADD_GPU_BLACKLIST_SUPPORT
QTEST_SET_MAIN_SOURCE_PATH QTEST_SET_MAIN_SOURCE_PATH
QMLTestSetup setup;
// check that qPref classes exists // register C++ types and classes (but not objects)
qPref::instance();
qPrefDisplay::instance();
// prepare Qt application
new QApplication(argc, argv);
// check that we have a directory
if (argc < 2) {
qDebug() << "ERROR: missing tst_* directory";
return -1;
}
// save tst_dir and pass rest to Qt
const char *tst_dir = argv[1];
for (int i = 1; i < argc; i++)
argv[i] = argv[i + 1];
argc--;
// Register types
qPref::instance()->registerQML(NULL); qPref::instance()->registerQML(NULL);
// Run all tst_*.qml files return quick_test_main_with_setup(argc, argv, "TestQML", nullptr, &setup);
return quick_test_main(argc, argv, "TestQML", tst_dir); #endif //QT_VERSION
#else
return 0;
#endif
} }
void QMLTestSetup::qmlEngineAvailable(QQmlEngine *engine)
{
// register C++ objects (but not types and classes)
qPref::instance()->registerQML(engine);
};

19
tests/testqml.h Normal file
View file

@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef TESTQML_H
#define TESTQML_H
#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>
#include <QObject>
class QMLTestSetup : public QObject {
Q_OBJECT
public:
QMLTestSetup() {}
public slots:
void qmlEngineAvailable(QQmlEngine *engine);
};
#endif // TESTQML_H