diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro index cc8224d5e7a836e7f947213be5b2c752026672c7..a902fa669cf5ab556da9cc740663b5ca8104e3f3 100644 --- a/tests/auto/qml/qml.pro +++ b/tests/auto/qml/qml.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs -SUBDIRS += qmldesigner +SUBDIRS += qmleditor qmldesigner diff --git a/tests/auto/qml/qmleditor/lookup/lookup.pro b/tests/auto/qml/qmleditor/lookup/lookup.pro new file mode 100644 index 0000000000000000000000000000000000000000..5e593fbfe81ffd1a97ae9e8bfa9dd0376ec74a9a --- /dev/null +++ b/tests/auto/qml/qmleditor/lookup/lookup.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +CONFIG += qt warn_on console depend_includepath +QT += testlib +include(../../../../../src/libs/qml/qml-lib.pri) +EDITOR_DIR=../../../../../src/plugins/qmleditor + +INCLUDEPATH += $$EDITOR_DIR + +TARGET=tst_$$TARGET + +SOURCES += tst_lookup.cpp \ + $$EDITOR_DIR/qmllookupcontext.cpp + +HEADERS += $$EDITOR_DIR/qmllookupcontext.h diff --git a/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp b/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f55f77aa9195d8bf4bd522f333a0d055dcfc1fa4 --- /dev/null +++ b/tests/auto/qml/qmleditor/lookup/tst_lookup.cpp @@ -0,0 +1,168 @@ +#include <QDebug> +#include <QtTest> +#include <QObject> + +#include <qml/qmldocument.h> +#include <qml/parser/qmljsast_p.h> + +#include <qmllookupcontext.h> + +using namespace Qml; +using namespace QmlEditor; +using namespace QmlEditor::Internal; +using namespace QmlJS; +using namespace QmlJS::AST; + +class tst_Lookup: public QObject +{ + Q_OBJECT + +public: + tst_Lookup(): _typeSystem(0) {} + ~tst_Lookup() { if (_typeSystem) resetTypeSystem(); } + + void resetTypeSystem() { if (_typeSystem) { delete _typeSystem; _typeSystem = 0; }} + +private Q_SLOTS: + void basicSymbolTest(); + void basicLookupTest(); + +protected: + QmlDocument::Ptr basicSymbolTest(const QString &input) const + { + const QLatin1String filename("<lookup test>"); + QmlDocument::Ptr doc = QmlDocument::create(filename); + doc->setSource(input); + doc->parse(); + + QList<DiagnosticMessage> msgs = doc->diagnosticMessages(); + foreach (const DiagnosticMessage &msg, msgs) { + if (msg.isError()) { + qDebug() << "Error:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } else if (msg.isWarning()) { + qDebug() << "Warning:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } else { + qDebug() << "Diagnostic:" << filename << ":" << msg.loc.startLine << ":" << msg.loc.startColumn << ":" << msg.message; + } + } + + return doc; + } + + Snapshot snapshot(const QmlDocument::Ptr &doc) const + { + Snapshot snapshot; + snapshot.insert(doc); + return snapshot; + } + + QmlTypeSystem *typeSystem() { + if (!_typeSystem) + _typeSystem = new QmlTypeSystem; + return _typeSystem; + } + +private: + QmlTypeSystem *_typeSystem; +}; + +void tst_Lookup::basicSymbolTest() +{ + const QLatin1String input( + "import Qt 4.6\n" + "\n" + "Rectangle {\n" + " x: 10\n" + " y: 10\n" + " width: 10\n" + " height: 10\n" + "}\n" + ); + + QmlDocument::Ptr doc = basicSymbolTest(input); + QVERIFY(doc->isParsedCorrectly()); + + UiProgram *program = doc->program(); + QVERIFY(program); + QVERIFY(program->members); + QVERIFY(program->members->member); + UiObjectDefinition *rectDef = cast<UiObjectDefinition*>(program->members->member); + QVERIFY(rectDef); + QVERIFY(rectDef->qualifiedTypeNameId->name); + QCOMPARE(rectDef->qualifiedTypeNameId->name->asString(), QLatin1String("Rectangle")); + QVERIFY(rectDef->initializer); + UiObjectMemberList *rectMembers = rectDef->initializer->members; + QVERIFY(rectMembers); + QVERIFY(rectMembers->member); + + UiScriptBinding *xBinding = cast<UiScriptBinding*>(rectMembers->member); + QVERIFY(xBinding); + QVERIFY(xBinding->qualifiedId); + QVERIFY(xBinding->qualifiedId->name); + QCOMPARE(xBinding->qualifiedId->name->asString(), QLatin1String("x")); + + QmlSymbol::List docSymbols = doc->symbols(); + QCOMPARE(docSymbols.size(), 1); + + QmlSymbol *rectSymbol = docSymbols.at(0); + QCOMPARE(rectSymbol->name(), QLatin1String("Rectangle")); + QCOMPARE(rectSymbol->members().size(), 4); + + QmlSymbolFromFile *rectFromFile = rectSymbol->asSymbolFromFile(); + QVERIFY(rectFromFile); + QmlSymbolFromFile *xSymbol = rectFromFile->findMember(xBinding); + QVERIFY(xSymbol); + QCOMPARE(xSymbol->name(), QLatin1String("x")); +} + +void tst_Lookup::basicLookupTest() +{ + const QLatin1String input( + "import Qt 4.6\n" + "Item{}\n" + ); + + QmlDocument::Ptr doc = basicSymbolTest(input); + QVERIFY(doc->isParsedCorrectly()); + + UiProgram *program = doc->program(); + QVERIFY(program); + + QStack<QmlSymbol *> emptyScope; + QmlLookupContext context(emptyScope, doc, snapshot(doc), typeSystem()); + QmlSymbol *rectSymbol = context.resolveType(QLatin1String("Text")); + QVERIFY(rectSymbol); + + QmlBuildInSymbol *buildInRect = rectSymbol->asBuildInSymbol(); + QVERIFY(buildInRect); + QCOMPARE(buildInRect->name(), QLatin1String("Text")); + + QmlSymbol::List allBuildInRectMembers = buildInRect->members(true); + QVERIFY(!allBuildInRectMembers.isEmpty()); + bool xPropFound = false; + bool fontPropFound = false; + foreach (QmlSymbol *symbol, allBuildInRectMembers) { + if (symbol->name() == QLatin1String("x")) + xPropFound = true; + else if (symbol->name() == QLatin1String("font")) + fontPropFound = true; + } + QVERIFY(xPropFound); + QVERIFY(fontPropFound); + + QmlSymbol::List buildInRectMembers = buildInRect->members(false); + QVERIFY(!buildInRectMembers.isEmpty()); + + QSKIP("Getting properties _without_ the inerited properties doesn't work.", SkipSingle); + fontPropFound = false; + foreach (QmlSymbol *symbol, buildInRectMembers) { + if (symbol->name() == QLatin1String("x")) + QFAIL("Text has x property"); + else if (symbol->name() == QLatin1String("font")) + fontPropFound = true; + } + QVERIFY(fontPropFound); +} + +QTEST_APPLESS_MAIN(tst_Lookup) +#include "tst_lookup.moc" diff --git a/tests/auto/qml/qmleditor/qmleditor.pro b/tests/auto/qml/qmleditor/qmleditor.pro new file mode 100644 index 0000000000000000000000000000000000000000..089574dbe77e62d3f0b91846dd39d1e2f75a6619 --- /dev/null +++ b/tests/auto/qml/qmleditor/qmleditor.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS += lookup