diff options
author | Caroline Chao <caroline.chao@digia.com> | 2013-08-22 13:13:00 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-28 20:22:18 +0200 |
commit | 75a6f86f685f1a5ce6cb91212641fe446a37be2e (patch) | |
tree | 9a6632bff3edfc687394abbfba1e88778961e002 /src | |
parent | 6e79a590c541c420e7f481bbc876c9c18669ff0b (diff) |
qmltest: Register test object as a singleton
Change-Id: I5f6c404ff2901082f22b953b29aed08d3488f31d
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/testlib/TestCase.qml | 5 | ||||
-rw-r--r-- | src/qmltest/quicktest.cpp | 35 |
2 files changed, 29 insertions, 11 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 6e2c8e73ab..1544bb2799 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -42,6 +42,7 @@ import QtQuick 2.0 import QtTest 1.0 import "testlogger.js" as TestLogger +import Qt.test.qtestroot 1.0 Item { id: testCase @@ -66,7 +67,7 @@ Item { // other test failed which this one depends on). property bool optional: false - property bool windowShown: qtest.windowShown + property bool windowShown: QTestRootObject.windowShown // Internal private state. Identifiers prefixed with qtest are reserved. property bool qtest_prevWhen: true @@ -744,7 +745,7 @@ Item { Component.onCompleted: { - qtest.hasTestCase = true; + QTestRootObject.hasTestCase = true; qtest_componentCompleted = true; if (util.printAvailableFunctions) { 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<QTestRootObject> 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<QTestRootObject>("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 } |