From a2cb7ceb72fa0d7be6840c8f6c51b7b98d2dbe86 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen <erik.verbruggen@nokia.com> Date: Wed, 24 Mar 2010 12:22:37 +0100 Subject: [PATCH] More fixes to the autotests. --- .../qmldesigner/core/model/propertyparser.cpp | 14 +- .../qmldesigner/core/model/propertyparser.h | 1 + .../core/model/texttomodelmerger.cpp | 10 +- .../qml/qmldesigner/coretests/testcore.cpp | 173 +++++++++--------- 4 files changed, 98 insertions(+), 100 deletions(-) diff --git a/src/plugins/qmldesigner/core/model/propertyparser.cpp b/src/plugins/qmldesigner/core/model/propertyparser.cpp index 91cb772786c..ded2b374cc5 100644 --- a/src/plugins/qmldesigner/core/model/propertyparser.cpp +++ b/src/plugins/qmldesigner/core/model/propertyparser.cpp @@ -62,16 +62,20 @@ QVariant read(const QString &typeStr, const QString &str, const MetaInfo &metaIn QVariant read(const QString &typeStr, const QString &str) { - QMetaType::Type type = static_cast<QMetaType::Type>(QMetaType::type(typeStr.toAscii().constData())); + int type = QMetaType::type(typeStr.toAscii().constData()); if (type == 0) qWarning() << "Type " << typeStr << " is unknown to QMetaType system. Cannot create properly typed QVariant for value " << str; + return read(type, str); +} +QVariant read(int variantType, const QString &str) +{ QVariant value; bool conversionOk = true; - switch (type) { + switch (variantType) { case QMetaType::QPoint: value = QDeclarativeStringConverters::pointFFromString(str, &conversionOk).toPoint(); break; @@ -98,15 +102,15 @@ QVariant read(const QString &typeStr, const QString &str) break; default: { value = QVariant(str); - QVariant::Type varType = static_cast<QVariant::Type>(type); - value.convert(varType); + value.convert(static_cast<QVariant::Type>(variantType)); break; } } if (!conversionOk) { value = QVariant(); - qWarning() << "Could not convert" << str << "to" << QMetaType::typeName(type); + qWarning() << "Could not convert" << str + << "to" << QMetaType::typeName(variantType); } return value; diff --git a/src/plugins/qmldesigner/core/model/propertyparser.h b/src/plugins/qmldesigner/core/model/propertyparser.h index f0ee5518e0b..e8c86a84576 100644 --- a/src/plugins/qmldesigner/core/model/propertyparser.h +++ b/src/plugins/qmldesigner/core/model/propertyparser.h @@ -40,6 +40,7 @@ namespace PropertyParser { QVariant read(const QString &typeStr, const QString &str, const MetaInfo &metaInfo); QVariant read(const QString &typeStr, const QString &str); +QVariant read(int variantType, const QString &str); QString write(const QVariant &variant, const MetaInfo &metaInfo); } // namespace PropertyParser diff --git a/src/plugins/qmldesigner/core/model/texttomodelmerger.cpp b/src/plugins/qmldesigner/core/model/texttomodelmerger.cpp index ad91a111b14..87c31bfad37 100644 --- a/src/plugins/qmldesigner/core/model/texttomodelmerger.cpp +++ b/src/plugins/qmldesigner/core/model/texttomodelmerger.cpp @@ -281,7 +281,7 @@ bool TextToModelMerger::load(const QByteArray &data, DifferenceHandler &differen try { Snapshot snapshot = m_rewriterView->textModifier()->getSnapshot(); const QString fileName = url.toLocalFile(); - Document::Ptr doc = Document::create(fileName); + Document::Ptr doc = Document::create(fileName.isEmpty() ? QLatin1String("<internal>") : fileName); doc->setSource(QString::fromUtf8(data.constData())); doc->parseQml(); snapshot.insert(doc); @@ -332,7 +332,7 @@ void TextToModelMerger::syncNode(ModelNode &modelNode, if (!astObjectType || !astInitializer) return; - m_rewriterView->positionStorage()->setNodeOffset(modelNode, astNode->firstSourceLocation().offset); + m_rewriterView->positionStorage()->setNodeOffset(modelNode, astObjectType->identifierToken.offset); QString typeName; int majorVersion; @@ -446,7 +446,8 @@ void TextToModelMerger::syncNode(ModelNode &modelNode, if (!defaultPropertyItems.isEmpty()) { QString defaultPropertyName = modelNode.metaInfo().defaultProperty(); if (defaultPropertyName.isEmpty()) { - qWarning() << "No default property for node type" << modelNode.type() << ", ignoring child items."; + if (modelNode.type() != QLatin1String("Qt/Component")) + qWarning() << "No default property for node type" << modelNode.type() << ", ignoring child items."; } else { AbstractProperty modelProperty = modelNode.property(defaultPropertyName); if (modelProperty.isNodeListProperty()) { @@ -598,7 +599,6 @@ void TextToModelMerger::syncNodeListProperty(NodeListProperty &modelListProperty QString name; if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(arrayMember)) name = flatten(definition->qualifiedTypeNameId); - // TODO: resolve name here! if (name == QLatin1String("Qt/Component")) setupComponent(newNode); } @@ -646,7 +646,7 @@ QVariant TextToModelMerger::convertToVariant(const ModelNode &node, const PropertyMetaInfo propertyMetaInfo = nodeMetaInfo.property(astName, true); if (propertyMetaInfo.isValid()) { - return Internal::PropertyParser::read(propertyMetaInfo.type(), cleanedValue, nodeMetaInfo.metaInfo()); + return Internal::PropertyParser::read(propertyMetaInfo.variantTypeId(), cleanedValue); } else if (node.type() == QLatin1String("Qt/PropertyChanges")) { // In the future, we should do the type resolving in a second pass, or delay setting properties until the full file has been parsed. return QVariant(cleanedValue); diff --git a/tests/auto/qml/qmldesigner/coretests/testcore.cpp b/tests/auto/qml/qmldesigner/coretests/testcore.cpp index 11c9172621b..2568e4ead9e 100644 --- a/tests/auto/qml/qmldesigner/coretests/testcore.cpp +++ b/tests/auto/qml/qmldesigner/coretests/testcore.cpp @@ -2126,7 +2126,7 @@ void TestCore::testRewriterPropertyDeclarations() VariantProperty urlProperty = rootModelNode.property(QLatin1String("urlProperty")).toVariantProperty(); QVERIFY(urlProperty.isValid()); QVERIFY(urlProperty.isVariantProperty()); - QCOMPARE(urlProperty.value(), QVariant()); + QCOMPARE(urlProperty.value(), QVariant(QUrl())); } void TestCore::testRewriterPropertyAliases() @@ -2225,53 +2225,47 @@ void TestCore::testRewriterPositionAndOffset() QVERIFY(rootNode.isValid()); QCOMPARE(rootNode.type(), QLatin1String("Qt/Rectangle")); - QString string; - string = ""; - for (int i=testRewriterView->nodeOffset(rootNode); i < testRewriterView->nodeOffset(rootNode) + testRewriterView->nodeLength(rootNode);i++) - string +=QString(qmlString)[i]; - - const QString qmlExpected0("Rectangle {\n" - " id: root\n" - " x: 10;\n" - " y: 10;\n" - " Rectangle {\n" - " id: rectangle1\n" - " x: 10;\n" - " y: 10;\n" - " }\n" - " Rectangle {\n" - " id: rectangle2\n" - " x: 100;\n" - " y: 100;\n" - " anchors.fill: root\n" - " }\n" - " Rectangle {\n" - " id: rectangle3\n" - " x: 140;\n" - " y: 180;\n" - " gradient: Gradient {\n" - " GradientStop {\n" - " position: 0\n" - " color: \"white\"\n" - " }\n" - " GradientStop {\n" - " position: 1\n" - " color: \"black\"\n" - " }\n" - " }\n" - " }\n" - "}"); - + QString string = QString(qmlString).mid(testRewriterView->nodeOffset(rootNode), testRewriterView->nodeLength(rootNode)); + const QString qmlExpected0("Rectangle {\n" + " id: root\n" + " x: 10;\n" + " y: 10;\n" + " Rectangle {\n" + " id: rectangle1\n" + " x: 10;\n" + " y: 10;\n" + " }\n" + " Rectangle {\n" + " id: rectangle2\n" + " x: 100;\n" + " y: 100;\n" + " anchors.fill: root\n" + " }\n" + " Rectangle {\n" + " id: rectangle3\n" + " x: 140;\n" + " y: 180;\n" + " gradient: Gradient {\n" + " GradientStop {\n" + " position: 0\n" + " color: \"white\"\n" + " }\n" + " GradientStop {\n" + " position: 1\n" + " color: \"black\"\n" + " }\n" + " }\n" + " }\n" + "}"); QCOMPARE(string, qmlExpected0); ModelNode lastRectNode = rootNode.allDirectSubModelNodes().last(); ModelNode gradientNode = lastRectNode.allDirectSubModelNodes().first(); ModelNode gradientStop = gradientNode.allDirectSubModelNodes().first(); - string = ""; - for (int i=testRewriterView->nodeOffset(gradientNode); i < testRewriterView->nodeOffset(gradientNode) + testRewriterView->nodeLength(gradientNode);i++) - string +=QString(qmlString)[i]; - + int offset = testRewriterView->nodeOffset(gradientNode); + int length = testRewriterView->nodeLength(gradientNode); + string = QString(qmlString).mid(offset, length); const QString qmlExpected1( "Gradient {\n" " GradientStop {\n" " position: 0\n" @@ -2282,13 +2276,9 @@ void TestCore::testRewriterPositionAndOffset() " color: \"black\"\n" " }\n" " }"); - QCOMPARE(string, qmlExpected1); - string = ""; - for (int i=testRewriterView->nodeOffset(gradientStop); i < testRewriterView->nodeOffset(gradientStop) + testRewriterView->nodeLength(gradientStop);i++) - string +=QString(qmlString)[i]; - + string = QString(qmlString).mid(testRewriterView->nodeOffset(gradientStop), testRewriterView->nodeLength(gradientStop)); const QString qmlExpected2( "GradientStop {\n" " position: 0\n" " color: \"white\"\n" @@ -2425,51 +2415,56 @@ void TestCore::testRewriterPreserveType() void TestCore::testRewriterForArrayMagic() { - const QLatin1String qmlString("import Qt 4.6\n" - "\n" - "Rectangle {\n" - " states: State {\n" - " name: \"s1\"\n" - " }\n" - "}\n"); - QPlainTextEdit textEdit; - textEdit.setPlainText(qmlString); - NotIndentingTextEditModifier textModifier(&textEdit); - - QScopedPointer<Model> model(Model::create("Qt/Item", 4, 6)); - QVERIFY(model.data()); - - QScopedPointer<TestView> view(new TestView); - model->attachView(view.data()); + try { + const QLatin1String qmlString("import Qt 4.6\n" + "\n" + "Rectangle {\n" + " states: State {\n" + " name: \"s1\"\n" + " }\n" + "}\n"); + QPlainTextEdit textEdit; + textEdit.setPlainText(qmlString); + NotIndentingTextEditModifier textModifier(&textEdit); - // read in - QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView()); - testRewriterView->setTextModifier(&textModifier); - model->attachView(testRewriterView.data()); + QScopedPointer<Model> model(Model::create("Qt/Item", 4, 6)); + QVERIFY(model.data()); - ModelNode rootNode = view->rootModelNode(); - QVERIFY(rootNode.isValid()); - QCOMPARE(rootNode.type(), QString("Qt/Rectangle")); + QScopedPointer<TestView> view(new TestView); + model->attachView(view.data()); - QmlItemNode rootItem(rootNode); - QVERIFY(rootItem.isValid()); + // read in + QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView()); + testRewriterView->setTextModifier(&textModifier); + model->attachView(testRewriterView.data()); - QmlModelState state1(rootItem.states().addState("s2")); - QCOMPARE(state1.modelNode().type(), QString("Qt/State")); + ModelNode rootNode = view->rootModelNode(); + QVERIFY(rootNode.isValid()); + QCOMPARE(rootNode.type(), QString("Qt/Rectangle")); - const QLatin1String expected("import Qt 4.6\n" - "\n" - "Rectangle {\n" - " states: [\n" - " State {\n" - " name: \"s1\"\n" - " },\n" - " State {\n" - " name: \"s2\"\n" - " }\n" - " ]\n" - "}\n"); - QCOMPARE(textEdit.toPlainText(), expected); + QmlItemNode rootItem(rootNode); + QVERIFY(rootItem.isValid()); + + QmlModelState state1(rootItem.states().addState("s2")); + QCOMPARE(state1.modelNode().type(), QString("Qt/State")); + + const QLatin1String expected("import Qt 4.6\n" + "\n" + "Rectangle {\n" + " states: [\n" + " State {\n" + " name: \"s1\"\n" + " },\n" + " State {\n" + " name: \"s2\"\n" + " }\n" + " ]\n" + "}\n"); + QCOMPARE(textEdit.toPlainText(), expected); + } catch (Exception &e) { + qDebug() << "Exception:" << e.description() << "at line" << e.line() << "in function" << e.function() << "in file" << e.file(); + QFAIL(qPrintable(e.description())); + } } void TestCore::testRewriterWithSignals() @@ -4930,9 +4925,7 @@ void TestCore::testRewriterChangeId() QCOMPARE(rootModelNode.id(), QString("rectId")); QString expected = "import Qt 4.6\n" - "Rectangle {\n" - "id: rectId\n" - " }"; + "Rectangle { id: rectId }"; QCOMPARE(textEdit.toPlainText(), expected); -- GitLab