Improve composite property type unit test
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlproperty / tst_qqmlproperty.cpp
index d99ed30..12a8325 100644 (file)
@@ -135,6 +135,7 @@ private slots:
     void noContext();
     void assignEmptyVariantMap();
     void warnOnInvalidBinding();
+    void registeredCompositeTypeProperty();
 
     void copy();
 private:
@@ -181,7 +182,7 @@ void tst_qqmlproperty::qmlmetaproperty()
     QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
     QVERIFY(binding == 0);
     QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-    QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+    QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
     QVERIFY(sigExprWatcher.wasDeleted());
     QCOMPARE(prop.index(), -1);
     QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -189,6 +190,120 @@ void tst_qqmlproperty::qmlmetaproperty()
     delete obj;
 }
 
+// 1 = equal, 0 = unknown, -1 = not equal.
+static int compareVariantAndListReference(const QVariant &v, QQmlListReference &r)
+{
+    if (QLatin1String(v.typeName()) != QLatin1String("QQmlListReference"))
+        return -1;
+
+    QQmlListReference lhs = v.value<QQmlListReference>();
+    if (lhs.isValid() != r.isValid())
+        return -1;
+
+    if (lhs.canCount() != r.canCount())
+        return -1;
+
+    if (!lhs.canCount()) {
+        if (lhs.canAt() != r.canAt())
+            return -1; // not equal.
+        return 0; // not sure if they're equal or not, and no way to tell.
+    }
+
+    // if we get here, we must be able to count.
+    if (lhs.count() != r.count())
+        return -1;
+
+    if (lhs.canAt() != r.canAt())
+        return -1;
+
+    if (!lhs.canAt())
+        return 0; // can count, but can't check element equality.
+
+    for (int i = 0; i < lhs.count(); ++i) {
+        if (lhs.at(i) != r.at(i)) {
+            return -1; // different elements :. not equal.
+        }
+    }
+
+    return 1; // equal.
+}
+
+void tst_qqmlproperty::registeredCompositeTypeProperty()
+{
+    // Composite type properties
+    {
+        QQmlEngine engine;
+        QQmlComponent component(&engine, testFileUrl("registeredCompositeTypeProperty.qml"));
+        QObject *obj = component.create();
+        QVERIFY(obj);
+
+        // create property accessors and check types.
+        QQmlProperty p1(obj, "first");
+        QQmlProperty p2(obj, "second");
+        QQmlProperty p3(obj, "third");
+        QQmlProperty p1e(obj, "first", &engine);
+        QQmlProperty p2e(obj, "second", &engine);
+        QQmlProperty p3e(obj, "third", &engine);
+        QVERIFY(p1.propertyType() == p2.propertyType());
+        QVERIFY(p1.propertyType() != p3.propertyType());
+
+        // check that the values are retrievable from CPP
+        QVariant first = obj->property("first");
+        QVariant second = obj->property("second");
+        QVariant third = obj->property("third");
+        QVERIFY(first.isValid());
+        QVERIFY(second.isValid());
+        QVERIFY(third.isValid());
+        // ensure that conversion from qobject-derived-ptr to qobject-ptr works.
+        QVERIFY(first.value<QObject*>());
+        QVERIFY(second.value<QObject*>());
+        QVERIFY(third.value<QObject*>());
+
+        // check that the values retrieved via QQmlProperty match those retrieved via QMetaProperty::read().
+        QCOMPARE(p1.read().value<QObject*>(), first.value<QObject*>());
+        QCOMPARE(p2.read().value<QObject*>(), second.value<QObject*>());
+        QCOMPARE(p3.read().value<QObject*>(), third.value<QObject*>());
+        QCOMPARE(p1e.read().value<QObject*>(), first.value<QObject*>());
+        QCOMPARE(p2e.read().value<QObject*>(), second.value<QObject*>());
+        QCOMPARE(p3e.read().value<QObject*>(), third.value<QObject*>());
+
+        delete obj;
+    }
+
+    // List-of-composite-type type properties
+    {
+        QQmlEngine engine;
+        QQmlComponent component(&engine, testFileUrl("registeredCompositeTypeProperty.qml"));
+        QObject *obj = component.create();
+        QVERIFY(obj);
+
+        // create list property accessors and check types
+        QQmlProperty lp1e(obj, "fclist", &engine);
+        QQmlProperty lp2e(obj, "sclistOne", &engine);
+        QQmlProperty lp3e(obj, "sclistTwo", &engine);
+        QVERIFY(lp1e.propertyType() != lp2e.propertyType());
+        QVERIFY(lp2e.propertyType() == lp3e.propertyType());
+
+        // check that the list values are retrievable from CPP
+        QVariant firstList = obj->property("fclist");
+        QVariant secondList = obj->property("sclistOne");
+        QVariant thirdList = obj->property("sclistTwo");
+        QVERIFY(firstList.isValid());
+        QVERIFY(secondList.isValid());
+        QVERIFY(thirdList.isValid());
+
+        // check that the value returned by QQmlProperty::read() is equivalent to the list reference.
+        QQmlListReference r1(obj, "fclist", &engine);
+        QQmlListReference r2(obj, "sclistOne", &engine);
+        QQmlListReference r3(obj, "sclistTwo", &engine);
+        QCOMPARE(compareVariantAndListReference(lp1e.read(), r1), 1);
+        QCOMPARE(compareVariantAndListReference(lp2e.read(), r2), 1);
+        QCOMPARE(compareVariantAndListReference(lp3e.read(), r3), 1);
+
+        delete obj;
+    }
+}
+
 class PropertyObject : public QObject
 {
     Q_OBJECT
@@ -285,7 +400,7 @@ void tst_qqmlproperty::qmlmetaproperty_object()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), -1);
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -335,7 +450,7 @@ void tst_qqmlproperty::qmlmetaproperty_object()
         QVERIFY(binding != 0);
         QVERIFY(QQmlPropertyPrivate::binding(prop) == binding.data());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfProperty("defaultProperty"));
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -388,7 +503,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), -1);
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -438,7 +553,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string()
         QVERIFY(binding != 0);
         QVERIFY(QQmlPropertyPrivate::binding(prop) == binding.data());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfProperty("defaultProperty"));
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -486,7 +601,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(!sigExprWatcher.wasDeleted());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == sigExpr);
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfMethod("clicked()"));
@@ -535,7 +650,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(!sigExprWatcher.wasDeleted());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == sigExpr);
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfMethod("oddlyNamedNotifySignal()"));
@@ -589,7 +704,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_context()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), -1);
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -639,7 +754,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_context()
         QVERIFY(binding != 0);
         QVERIFY(QQmlPropertyPrivate::binding(prop) == binding.data());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfProperty("defaultProperty"));
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -692,7 +807,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string_context()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), -1);
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -742,7 +857,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string_context()
         QVERIFY(binding != 0);
         QVERIFY(QQmlPropertyPrivate::binding(prop) == binding.data());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(sigExprWatcher.wasDeleted());
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfProperty("defaultProperty"));
         QCOMPARE(QQmlPropertyPrivate::valueTypeCoreIndex(prop), -1);
@@ -790,7 +905,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string_context()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(!sigExprWatcher.wasDeleted());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == sigExpr);
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfMethod("clicked()"));
@@ -839,7 +954,7 @@ void tst_qqmlproperty::qmlmetaproperty_object_string_context()
         QVERIFY(QQmlPropertyPrivate::setBinding(prop, binding.data()) == 0);
         QVERIFY(binding == 0);
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == 0);
-        QVERIFY(QQmlPropertyPrivate::setSignalExpression(prop, sigExpr) == 0);
+        QVERIFY(QQmlPropertyPrivate::takeSignalExpression(prop, sigExpr) == 0);
         QVERIFY(!sigExprWatcher.wasDeleted());
         QVERIFY(QQmlPropertyPrivate::signalExpression(prop) == sigExpr);
         QCOMPARE(prop.index(), dobject.metaObject()->indexOfMethod("oddlyNamedNotifySignal()"));
@@ -987,7 +1102,7 @@ void tst_qqmlproperty::read()
         QQmlProperty p(&o, "onClicked");
         QCOMPARE(p.read(), QVariant());
 
-        QVERIFY(0 == QQmlPropertyPrivate::setSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
+        QVERIFY(0 == QQmlPropertyPrivate::takeSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
         QVERIFY(0 != QQmlPropertyPrivate::signalExpression(p));
 
         QCOMPARE(p.read(), QVariant());
@@ -999,7 +1114,7 @@ void tst_qqmlproperty::read()
         QQmlProperty p(&o, "onPropertyWithNotifyChanged");
         QCOMPARE(p.read(), QVariant());
 
-        QVERIFY(0 == QQmlPropertyPrivate::setSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
+        QVERIFY(0 == QQmlPropertyPrivate::takeSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
         QVERIFY(0 != QQmlPropertyPrivate::signalExpression(p));
 
         QCOMPARE(p.read(), QVariant());
@@ -1155,7 +1270,7 @@ void tst_qqmlproperty::write()
         QQmlProperty p(&o, "onClicked");
         QCOMPARE(p.write(QVariant("console.log(1921)")), false);
 
-        QVERIFY(0 == QQmlPropertyPrivate::setSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
+        QVERIFY(0 == QQmlPropertyPrivate::takeSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
         QVERIFY(0 != QQmlPropertyPrivate::signalExpression(p));
 
         QCOMPARE(p.write(QVariant("console.log(1921)")), false);
@@ -1169,7 +1284,7 @@ void tst_qqmlproperty::write()
         QQmlProperty p(&o, "onPropertyWithNotifyChanged");
         QCOMPARE(p.write(QVariant("console.log(1921)")), false);
 
-        QVERIFY(0 == QQmlPropertyPrivate::setSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
+        QVERIFY(0 == QQmlPropertyPrivate::takeSignalExpression(p, new QQmlBoundSignalExpression(QQmlContextData::get(engine.rootContext()), 0, QLatin1String("null"), false, QString(), -1, -1)));
         QVERIFY(0 != QQmlPropertyPrivate::signalExpression(p));
 
         QCOMPARE(p.write(QVariant("console.log(1921)")), false);