From 684f9df7849bc79f1f02a60844fb43c7a3927d2f Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 15 Jan 2020 14:47:35 +0100 Subject: Long live QML inline components [ChangeLog][QtQml] It is now possible to declare new QML components in a QML file via the component keyword. They can be used just as if they were declared in another file, with the only difference that the type name needs to be prefixed with the name of the containing type outside of the file were the inline component has been declared. Notably, inline components are not closures: In the following example, the output would be 42 // MyItem.qml Item { property int i: 33 component IC: Item { Component.onCompleted: console.log(i) } } // user.qml Item { property int i: 42 MyItem.IC {} } Fixes: QTBUG-79382 Change-Id: I6a5ffc43f093a76323f435cfee9bab217781b8f5 Reviewed-by: Ulf Hermann --- tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 136 ++++++++++++++++++++++- 1 file changed, 134 insertions(+), 2 deletions(-) (limited to 'tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp') diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 089daf3ed5..4d2f773dbf 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -306,6 +306,16 @@ private slots: void extendedForeignTypes(); + void inlineComponent(); + void inlineComponent_data(); + void inlineComponentReferenceCycle_data(); + void inlineComponentReferenceCycle(); + void nestedInlineComponentNotAllowed(); + void inlineComponentStaticTypeResolution(); + void inlineComponentInSingleton(); + void nonExistingInlineComponent_data(); + void nonExistingInlineComponent(); + void selfReference(); void selfReferencingSingleton(); @@ -2780,12 +2790,12 @@ void tst_qqmllanguage::importsLocal_data() QTest::newRow("local import QTBUG-7721 A") << "subdir.Test {}" // no longer allowed (QTBUG-7721) << "" - << "subdir.Test - subdir is not a namespace"; + << "subdir.Test - subdir is neither a type nor a namespace"; QTest::newRow("local import QTBUG-7721 B") << "import \"subdir\" as X\n" "X.subsubdir.SubTest {}" // no longer allowed (QTBUG-7721) << "" - << "X.subsubdir.SubTest - nested namespaces not allowed"; + << "X.subsubdir.SubTest - subsubdir is not a type"; QTest::newRow("local import as") << "import \"subdir\" as T\n" "T.Test {}" @@ -5408,6 +5418,128 @@ void tst_qqmllanguage::overrideSingleton() check("uncreatable", "UncreatableSingleton"); } +void tst_qqmllanguage::inlineComponent() +{ + QFETCH(QUrl, componentUrl); + QFETCH(QColor, color); + QFETCH(int, width); + QQmlEngine engine; + QQmlComponent component(&engine, componentUrl); + QScopedPointer o(component.create()); + if (component.isError()) { + qDebug() << component.errorString(); + } + QVERIFY(!o.isNull()); + auto icInstance = o->findChild("icInstance"); + QVERIFY(icInstance); + QCOMPARE(icInstance->property("color").value(),color); + QCOMPARE(icInstance->property("width").value(), width); +} + +void tst_qqmllanguage::inlineComponent_data() +{ + QTest::addColumn("componentUrl"); + QTest::addColumn("color"); + QTest::addColumn("width"); + + QTest::newRow("Usage from other component") << testFileUrl("inlineComponentUser1.qml") << QColorConstants::Blue << 24; + QTest::newRow("Reexport") << testFileUrl("inlineComponentUser2.qml") << QColorConstants::Svg::green << 24; + QTest::newRow("Usage in same component") << testFileUrl("inlineComponentUser3.qml") << QColorConstants::Blue << 24; + + QTest::newRow("Resolution happens at instantiation") << testFileUrl("inlineComponentUser4.qml") << QColorConstants::Blue << 24; + QTest::newRow("Non-toplevel IC is found") << testFileUrl("inlineComponentUser5.qml") << QColorConstants::Svg::red << 24; + + QTest::newRow("Resolved in correct order") << testFileUrl("inlineComponentOrder.qml") << QColorConstants::Blue << 200; +} + +void tst_qqmllanguage::inlineComponentReferenceCycle_data() +{ + QTest::addColumn("componentUrl"); + + QTest::newRow("Simple cycle") << testFileUrl("icSimpleCycle.qml"); + QTest::newRow("Via property") << testFileUrl("icCycleViaProperty.qml"); +} + +void tst_qqmllanguage::inlineComponentReferenceCycle() +{ + QFETCH(QUrl, componentUrl); + QQmlEngine engine; + QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QQmlComponent: Component is not ready"); + QQmlComponent component(&engine, componentUrl); + QScopedPointer o(component.create()); + QVERIFY(o.isNull()); + QVERIFY(component.isError()); + QCOMPARE(component.errorString(), componentUrl.toString() + QLatin1String(":-1 Inline components form a cycle!\n")); +} + +void tst_qqmllanguage::nestedInlineComponentNotAllowed() +{ + QQmlEngine engine; + auto url = testFileUrl("nestedIC.qml"); + QQmlComponent component(&engine, url); + QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QQmlComponent: Component is not ready"); + QScopedPointer o(component.create()); + QVERIFY(component.isError()); + QCOMPARE(component.errorString(), QLatin1String("%1:%2").arg(url.toString(), QLatin1String("5 Nested inline components are not supported\n"))); +} + +void tst_qqmllanguage::inlineComponentStaticTypeResolution() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("InlineComponentChild.qml")); + VERIFY_ERRORS(0); + QScopedPointer o(component.create()); + QVERIFY(o); + QCOMPARE(o->property("i").toInt(), 42); +} + +void tst_qqmllanguage::inlineComponentInSingleton() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("singletonICTest.qml")); + VERIFY_ERRORS(0); + QScopedPointer o(component.create()); + QVERIFY(!o.isNull()); + auto untyped = o->property("singleton1"); + QVERIFY(untyped.isValid()); + auto singleton1 = untyped.value(); + QVERIFY(singleton1); + QCOMPARE(singleton1->property("iProp").value(), 42); + QCOMPARE(singleton1->property("sProp").value(), QString::fromLatin1("Hello, world")); + QVERIFY(!o.isNull()); +} + +void tst_qqmllanguage::nonExistingInlineComponent_data() +{ + QTest::addColumn("componentUrl"); + QTest::addColumn("errorMessage"); + QTest::addColumn("line"); + QTest::addColumn("column"); + + QTest::newRow("Property type") << testFileUrl("nonExistingICUser1.qml") << QString("Type InlineComponentProvider has no inline component type called NonExisting") << 4 << 5; + QTest::newRow("Instantiation") << testFileUrl("nonExistingICUser2.qml") << QString("Type InlineComponentProvider has no inline component type called NotExisting") << 4 << 5; + QTest::newRow("Inheritance") << testFileUrl("nonExistingICUser3.qml") << QString("Type InlineComponentProvider has no inline component type called NotExisting") << 3 << 1; + QTest::newRow("From singleton") << testFileUrl("nonExistingICUser4.qml") << QString("Type MySingleton.SingletonTypeWithIC has no inline component type called NonExisting") << 5 << 5; + + QTest::newRow("Cannot access parent inline components from child") << testFileUrl("nonExistingICUser5.qml") << QString("Type InlineComponentProviderChild has no inline component type called StyledRectangle") << 4 << 5; +} + +void tst_qqmllanguage::nonExistingInlineComponent() +{ + QFETCH(QUrl, componentUrl); + QFETCH(QString, errorMessage); + QFETCH(int, line); + QFETCH(int, column); + QQmlEngine engine; + QQmlComponent component(&engine, componentUrl); + auto errors = component.errors(); + QCOMPARE(errors.size(), 1); + const auto &error = errors.first(); + QCOMPARE(error.description(), errorMessage); + QCOMPARE(error.line(), line); + QCOMPARE(error.column(), column); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" -- cgit v1.2.3