From 75a6f86f685f1a5ce6cb91212641fe446a37be2e Mon Sep 17 00:00:00 2001 From: Caroline Chao Date: Thu, 22 Aug 2013 13:13:00 +0200 Subject: qmltest: Register test object as a singleton Change-Id: I5f6c404ff2901082f22b953b29aed08d3488f31d Reviewed-by: J-P Nurmi Reviewed-by: Alan Alpert --- src/qmltest/quicktest.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'src/qmltest/quicktest.cpp') diff --git a/src/qmltest/quicktest.cpp b/src/qmltest/quicktest.cpp index 2db68bad16..fd0270fd06 100644 --- a/src/qmltest/quicktest.cpp +++ b/src/qmltest/quicktest.cpp @@ -79,6 +79,15 @@ public: QTestRootObject(QObject *parent = 0) : QObject(parent), hasQuit(false), m_windowShown(false), m_hasTestCase(false) {} + static QTestRootObject *instance() { + static QPointer object = new QTestRootObject; + if (!object) { + qWarning("A new test root object has been created, the behavior may be compromised"); + object = new QTestRootObject; + } + return object; + } + bool hasQuit:1; bool hasTestCase() const { return m_hasTestCase; } void setHasTestCase(bool value) { m_hasTestCase = value; emit hasTestCaseChanged(); } @@ -86,6 +95,8 @@ public: bool windowShown() const { return m_windowShown; } void setWindowShown(bool value) { m_windowShown = value; emit windowShownChanged(); } + void init() { setWindowShown(false); setHasTestCase(false); hasQuit = false; } + Q_SIGNALS: void windowShownChanged(); void hasTestCaseChanged(); @@ -98,6 +109,13 @@ private: bool m_hasTestCase :1; }; +static QObject *testRootObject(QQmlEngine *engine, QJSEngine *jsEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(jsEngine); + return QTestRootObject::instance(); +} + static inline QString stripQuotes(const QString &s) { if (s.length() >= 2 && s.startsWith(QLatin1Char('"')) && s.endsWith(QLatin1Char('"'))) @@ -287,20 +305,21 @@ int quick_test_main(int argc, char **argv, const char *name, const char *sourceD return 1; } + // Register the test object + qmlRegisterSingletonType("Qt.test.qtestroot", 1, 0, "QTestRootObject", testRootObject); // Scan through all of the "tst_*.qml" files and run each of them // in turn with a QQuickView. QQuickView *view = new QQuickView; view->setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); - QTestRootObject rootobj; QEventLoop eventLoop; QObject::connect(view->engine(), SIGNAL(quit()), - &rootobj, SLOT(quit())); + QTestRootObject::instance(), SLOT(quit())); QObject::connect(view->engine(), SIGNAL(quit()), &eventLoop, SLOT(quit())); view->rootContext()->setContextProperty - (QLatin1String("qtest"), &rootobj); + (QLatin1String("qtest"), QTestRootObject::instance()); // Deprecated. Use QTestRootObject from Qt.test.qtestroot instead foreach (const QString &path, imports) view->engine()->addImportPath(path); foreach (const QString &file, files) { @@ -310,9 +329,7 @@ int quick_test_main(int argc, char **argv, const char *name, const char *sourceD view->setObjectName(fi.baseName()); view->setTitle(view->objectName()); - rootobj.setHasTestCase(false); - rootobj.setWindowShown(false); - rootobj.hasQuit = false; + QTestRootObject::instance()->init(); QString path = fi.absoluteFilePath(); if (path.startsWith(QLatin1String(":/"))) view->setSource(QUrl(QLatin1String("qrc:") + path.mid(2))); @@ -325,7 +342,7 @@ int quick_test_main(int argc, char **argv, const char *name, const char *sourceD handleCompileErrors(fi, view); continue; } - if (!rootobj.hasQuit) { + if (!QTestRootObject::instance()->hasQuit) { // If the test already quit, then it was performed // synchronously during setSource(). Otherwise it is // an asynchronous test and we need to show the window @@ -341,8 +358,8 @@ int quick_test_main(int argc, char **argv, const char *name, const char *sourceD view->show(); QTest::qWaitForWindowExposed(view); if (view->isExposed()) - rootobj.setWindowShown(true); - if (!rootobj.hasQuit && rootobj.hasTestCase()) + QTestRootObject::instance()->setWindowShown(true); + if (!QTestRootObject::instance()->hasQuit && QTestRootObject::instance()->hasTestCase()) eventLoop.exec(); // view->hide(); Causes a crash in Qt 3D due to deletion of the GL context, see QTBUG-27696 } -- cgit v1.2.3