aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMatthew Vogt <matthew.vogt@nokia.com>2012-03-19 15:56:22 +1000
committerQt by Nokia <qt-info@nokia.com>2012-03-20 02:58:26 +0100
commit610df5cdf87b9e1566b01a273fe67905b035cb93 (patch)
tree8d9226e18eef78830b70a1f0edf3b8054cd3b1f4 /tests
parentc5cd60a96d19a89e466f0ad62c2f5a625aaf4d8c (diff)
Allow parent to be specified for Qt.createComponent
Add an optional parameter to the Qt.createComponent function which allows the caller to specify the object that will become the parent for the created component. If the parent is not specified by the caller, it becomes the QML engine; this behavior will change in the near future to allow these component objects to be collected when no longer referenced. Task-number: QTBUG-24840 Change-Id: I2742133fe8ab8cbc80d8012a9d9b9fc2e4596fca Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmlecmascript/data/componentCreation.qml52
-rw-r--r--tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp85
2 files changed, 137 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlecmascript/data/componentCreation.qml b/tests/auto/qml/qqmlecmascript/data/componentCreation.qml
new file mode 100644
index 0000000000..d21301ea13
--- /dev/null
+++ b/tests/auto/qml/qqmlecmascript/data/componentCreation.qml
@@ -0,0 +1,52 @@
+import Qt.test 1.0
+import QtQuick 2.0
+
+MyTypeObject{
+ id: obj
+ objectName: "obj"
+
+ function url()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml');
+ }
+
+ function urlMode()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', Component.PreferSynchronous);
+ }
+
+ function urlParent()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', obj);
+ }
+
+ function urlNullParent()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', null);
+ }
+
+ function urlModeParent()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', Component.PreferSynchronous, obj);
+ }
+
+ function urlModeNullParent()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', Component.PreferSynchronous, null);
+ }
+
+ function invalidSecondArg()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', 'Bad argument');
+ }
+
+ function invalidThirdArg()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', Component.PreferSynchronous, 'Bad argument');
+ }
+
+ function invalidMode()
+ {
+ obj.componentProperty = Qt.createComponent('dynamicCreation.helper.qml', -666);
+ }
+}
diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
index 2f27ccee31..650197ea4f 100644
--- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
+++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp
@@ -97,6 +97,8 @@ private slots:
void importScope();
void signalParameterTypes();
void objectsCompareAsEqual();
+ void componentCreation_data();
+ void componentCreation();
void dynamicCreation_data();
void dynamicCreation();
void dynamicDestruction();
@@ -1201,6 +1203,89 @@ void tst_qqmlecmascript::aliasPropertyReset()
delete object;
}
+void tst_qqmlecmascript::componentCreation_data()
+{
+ QTest::addColumn<QString>("method");
+ QTest::addColumn<QString>("creationError");
+ QTest::addColumn<QString>("createdParent");
+
+ QTest::newRow("url")
+ << "url"
+ << ""
+ << "";
+ QTest::newRow("urlMode")
+ << "urlMode"
+ << ""
+ << "";
+ QTest::newRow("urlParent")
+ << "urlParent"
+ << ""
+ << "obj";
+ QTest::newRow("urlNullParent")
+ << "urlNullParent"
+ << ""
+ << "null";
+ QTest::newRow("urlModeParent")
+ << "urlModeParent"
+ << ""
+ << "obj";
+ QTest::newRow("urlModeNullParent")
+ << "urlModeNullParent"
+ << ""
+ << "null";
+ QTest::newRow("invalidSecondArg")
+ << "invalidSecondArg"
+ << ":40: Error: Qt.createComponent(): Invalid arguments"
+ << "";
+ QTest::newRow("invalidThirdArg")
+ << "invalidThirdArg"
+ << ":45: Error: Qt.createComponent(): Invalid parent object"
+ << "";
+ QTest::newRow("invalidMode")
+ << "invalidMode"
+ << ":50: Error: Qt.createComponent(): Invalid arguments"
+ << "";
+}
+
+/*
+Test using createComponent to dynamically generate a component.
+*/
+void tst_qqmlecmascript::componentCreation()
+{
+ QFETCH(QString, method);
+ QFETCH(QString, creationError);
+ QFETCH(QString, createdParent);
+
+ QUrl testUrl(testFileUrl("componentCreation.qml"));
+
+ if (!creationError.isEmpty()) {
+ QString warning = testUrl.toString() + creationError;
+ QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
+ }
+
+ QQmlComponent component(&engine, testUrl);
+ MyTypeObject *object = qobject_cast<MyTypeObject*>(component.create());
+ QVERIFY(object != 0);
+
+ QMetaObject::invokeMethod(object, method.toUtf8());
+ QQmlComponent *created = object->componentProperty();
+
+ if (creationError.isEmpty()) {
+ QVERIFY(created);
+
+ QObject *expectedParent;
+ if (createdParent.isEmpty()) {
+ // For now, the parent should be the engine; this will change for QTBUG-24841
+ expectedParent = &engine;
+ } else if (createdParent == QLatin1String("obj")) {
+ expectedParent = object;
+ } else if (createdParent == QLatin1String("null")) {
+ expectedParent = 0;
+ }
+ QCOMPARE(created->parent(), expectedParent);
+ }
+}
+
void tst_qqmlecmascript::dynamicCreation_data()
{
QTest::addColumn<QString>("method");