From f5b24fd48d494d2c43c34cab84f34d1f2e4e33a8 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sun, 14 Jun 2015 16:28:25 +0200 Subject: [PATCH] Auto test anchors & attached objects Rename the former tst_declarative to tst_sanity and extend it with tests to prevent: a) use of anchors, and b) multiple attached object instances of the same type. Anchors are not allowed for two reasons: - performance (QQuickAnchors is a QObject), and - to let users relayout delegates (x/y/width/height bindings can be overridden, anchors not so easily) Multiple attached object instances (eg. Theme) can easily happen by accident. "Theme.fooColor" can be used in the control root, but in all delegate items, "control.Theme.fooColor" must be used instead. Change-Id: I4045d5bd717fa21db79d1c3bd618fc450e292fa4 Reviewed-by: J-P Nurmi --- .gitignore | 2 +- tests/auto/auto.pro | 4 +- .../declarative.pro => sanity/sanity.pro} | 4 +- .../tst_sanity.cpp} | 117 +++++++++++++++--- 4 files changed, 107 insertions(+), 20 deletions(-) rename tests/auto/{declarative/declarative.pro => sanity/sanity.pro} (68%) rename tests/auto/{declarative/tst_declarative.cpp => sanity/tst_sanity.cpp} (66%) diff --git a/.gitignore b/.gitignore index 802236a846..152d106778 100644 --- a/.gitignore +++ b/.gitignore @@ -12,8 +12,8 @@ /examples/quick/extras/drawer/drawer /tests/auto/controls/tst_controls -/tests/auto/declarative/tst_declarative /tests/auto/extras/tst_extras +/tests/auto/sanity/tst_sanity /tests/benchmarks/creationtime/tst_creationtime /tests/benchmarks/objectcount/tst_objectcount diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index fca8731ce1..a5cbd53c18 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,5 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ controls \ - declarative \ - extras + extras \ + sanity diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/sanity/sanity.pro similarity index 68% rename from tests/auto/declarative/declarative.pro rename to tests/auto/sanity/sanity.pro index aae8c20e5f..436dc7ddd9 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/sanity/sanity.pro @@ -1,9 +1,9 @@ TEMPLATE = app -TARGET = tst_declarative +TARGET = tst_sanity QT += qml testlib core-private qml-private CONFIG += testcase osx:CONFIG -= app_bundle SOURCES += \ - $$PWD/tst_declarative.cpp + $$PWD/tst_sanity.cpp diff --git a/tests/auto/declarative/tst_declarative.cpp b/tests/auto/sanity/tst_sanity.cpp similarity index 66% rename from tests/auto/declarative/tst_declarative.cpp rename to tests/auto/sanity/tst_sanity.cpp index d16046da7d..eb7c537570 100644 --- a/tests/auto/declarative/tst_declarative.cpp +++ b/tests/auto/sanity/tst_sanity.cpp @@ -36,28 +36,62 @@ #include #include +#include #include #include #include #include #include -class tst_Declarative : public QObject +Q_GLOBAL_STATIC(QObjectList, qt_qobjects) + +extern "C" Q_DECL_EXPORT void qt_addQObject(QObject *object) +{ + qt_qobjects->append(object); +} + +extern "C" Q_DECL_EXPORT void qt_removeQObject(QObject *object) +{ + qt_qobjects->removeAll(object); +} + +class tst_Sanity : public QObject { Q_OBJECT private slots: + void init(); + void cleanup(); void initTestCase(); - void testFiles(); - void testFunctions(); - void testFunctions_data(); - void testSignalHandlers(); - void testSignalHandlers_data(); + + void jsFiles(); + void functions(); + void functions_data(); + void signalHandlers(); + void signalHandlers_data(); + void anchors(); + void anchors_data(); + void attachedObjects(); + void attachedObjects_data(); private: + QQmlEngine engine; QMap files; }; +void tst_Sanity::init() +{ + qtHookData[QHooks::AddQObject] = reinterpret_cast(&qt_addQObject); + qtHookData[QHooks::RemoveQObject] = reinterpret_cast(&qt_removeQObject); +} + +void tst_Sanity::cleanup() +{ + qt_qobjects->clear(); + qtHookData[QHooks::AddQObject] = 0; + qtHookData[QHooks::RemoveQObject] = 0; +} + class BaseValidator : public QQmlJS::AST::Visitor { public: @@ -109,7 +143,7 @@ static QMap listQmlFiles(const QDir &dir) return files; } -void tst_Declarative::initTestCase() +void tst_Sanity::initTestCase() { QQmlEngine engine; foreach (const QString &path, engine.importPathList()) { @@ -119,11 +153,11 @@ void tst_Declarative::initTestCase() } } -void tst_Declarative::testFiles() +void tst_Sanity::jsFiles() { QMap::const_iterator it; for (it = files.begin(); it != files.end(); ++it) { - if (QFileInfo(it.value()).suffix() == QString("js")) + if (QFileInfo(it.value()).suffix() == QStringLiteral("js")) QFAIL(qPrintable(it.value() + ": JS files are not allowed")); } } @@ -138,7 +172,7 @@ protected: } }; -void tst_Declarative::testFunctions() +void tst_Sanity::functions() { QFETCH(QString, control); QFETCH(QString, filePath); @@ -148,7 +182,7 @@ void tst_Declarative::testFunctions() QFAIL(qPrintable(validator.errors())); } -void tst_Declarative::testFunctions_data() +void tst_Sanity::functions_data() { QTest::addColumn("control"); QTest::addColumn("filePath"); @@ -175,7 +209,7 @@ protected: } }; -void tst_Declarative::testSignalHandlers() +void tst_Sanity::signalHandlers() { QFETCH(QString, control); QFETCH(QString, filePath); @@ -185,7 +219,7 @@ void tst_Declarative::testSignalHandlers() QFAIL(qPrintable(validator.errors())); } -void tst_Declarative::testSignalHandlers_data() +void tst_Sanity::signalHandlers_data() { QTest::addColumn("control"); QTest::addColumn("filePath"); @@ -195,6 +229,59 @@ void tst_Declarative::testSignalHandlers_data() QTest::newRow(qPrintable(it.key())) << it.key() << it.value(); } -QTEST_MAIN(tst_Declarative) +void tst_Sanity::anchors() +{ + QFETCH(QString, control); + QFETCH(QString, filePath); -#include "tst_declarative.moc" + QQmlComponent component(&engine); + component.loadUrl(QUrl::fromLocalFile(filePath)); + + QScopedPointer object(component.create()); + QVERIFY(object.data()); + foreach (QObject *object, *qt_qobjects) + QVERIFY2(!object->inherits("QQuickAnchors"), "Anchors are not allowed"); +} + +void tst_Sanity::anchors_data() +{ + QTest::addColumn("control"); + QTest::addColumn("filePath"); + + QMap::const_iterator it; + for (it = files.begin(); it != files.end(); ++it) + QTest::newRow(qPrintable(it.key())) << it.key() << it.value(); +} + +void tst_Sanity::attachedObjects() +{ + QFETCH(QString, control); + QFETCH(QString, filePath); + + QQmlComponent component(&engine); + component.loadUrl(QUrl::fromLocalFile(filePath)); + + QSet classNames; + QScopedPointer object(component.create()); + QVERIFY(object.data()); + foreach (QObject *object, *qt_qobjects) { + QString className = object->metaObject()->className(); + if (className.endsWith("Attached")) + QVERIFY2(!classNames.contains(className), qPrintable(QString("Multiple instances of attached type %1").arg(className))); + classNames.insert(className); + } +} + +void tst_Sanity::attachedObjects_data() +{ + QTest::addColumn("control"); + QTest::addColumn("filePath"); + + QMap::const_iterator it; + for (it = files.begin(); it != files.end(); ++it) + QTest::newRow(qPrintable(it.key())) << it.key() << it.value(); +} + +QTEST_MAIN(tst_Sanity) + +#include "tst_sanity.moc"