From f09517bd9c907698a05ee92ccf158a06db3340b8 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 8 Aug 2012 14:53:45 +1000 Subject: Ensure that copy sequences can be passed as arguments Previously, automatic conversion from JS array to sequence copy resource was not performed in the case where the array was passed as a parameter to a QObject function invocation. This commit adds code to check if the parameter type is a sequence type - and if so, and if the value is a variantlist, we convert it to a sequence of the appropriate type. Change-Id: I3cc3e2f95604bc71d1d8d237e1acffa1e03b78ba Reviewed-by: Glenn Watson --- .../data/sequenceConversion.copy.qml | 23 +++++++++++++++++ tests/auto/qml/qqmlecmascript/testtypes.cpp | 30 +++++----------------- tests/auto/qml/qqmlecmascript/testtypes.h | 1 + .../auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 4 +++ 4 files changed, 34 insertions(+), 24 deletions(-) (limited to 'tests') diff --git a/tests/auto/qml/qqmlecmascript/data/sequenceConversion.copy.qml b/tests/auto/qml/qqmlecmascript/data/sequenceConversion.copy.qml index f6614dad0c..088e240ad4 100644 --- a/tests/auto/qml/qqmlecmascript/data/sequenceConversion.copy.qml +++ b/tests/auto/qml/qqmlecmascript/data/sequenceConversion.copy.qml @@ -157,4 +157,27 @@ Item { if (jsIntList == jsIntList2) success = false; if (jsIntList != jsIntList) success = false; } + + // this test ensures that copy resource sequences can be passed as parameters + function testCopyParameters() { + success = false; + + var jsIntList = msco.generateIntSequence(); + success = msco.parameterEqualsGeneratedIntSequence(jsIntList); + if (success == false) return; + + // here we construct something which should be converted to a copy sequence automatically. + success = msco.parameterEqualsGeneratedIntSequence([1,2,3]); + } + + // this test ensures that reference resource sequences are converted + // to copy resource sequences when passed as parameters. + function testReferenceParameters() { + success = false; + + msco.intListProperty = msco.generateIntSequence(); + var jsIntList = msco.intListProperty + success = msco.parameterEqualsGeneratedIntSequence(jsIntList); + if (success == false) return; + } } diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp index 9669e371de..a0bdbb6156 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.cpp +++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp @@ -225,35 +225,17 @@ public: { return stringList; } - Q_INVOKABLE QList integers(QVariant v) const + Q_INVOKABLE QList integers(QList v) const { - QList intList; - QList vList = v.toList(); - for (int i=0 ; i < vList.size() ; ++i) { - int iv = vList[i].toInt(); - intList.append(iv); - } - return intList; + return v; } - Q_INVOKABLE QList reals(QVariant v) const + Q_INVOKABLE QList reals(QList v) const { - QList realList; - QList vList = v.toList(); - for (int i=0 ; i < vList.size() ; ++i) { - qreal fv = vList[i].toReal(); - realList.append(fv); - } - return realList; + return v; } - Q_INVOKABLE QList bools(QVariant v) const + Q_INVOKABLE QList bools(QList v) const { - QList boolList; - QList vList = v.toList(); - for (int i=0 ; i < vList.size() ; ++i) { - bool bv = vList[i].toBool(); - boolList.append(bv); - } - return boolList; + return v; } }; diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index c05405ab53..da6baa4e6c 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -1393,6 +1393,7 @@ public: Q_INVOKABLE QList generateStringSequence() const { QList retn; retn << "one" << "two" << "three"; return retn; } Q_INVOKABLE QList generateUrlSequence() const { QList retn; retn << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com"); return retn; } Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; } + Q_INVOKABLE bool parameterEqualsGeneratedIntSequence(const QList& param) const { return (param == generateIntSequence()); } // "reference resource" underlying qobject deletion test: Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; } diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 6fa6c3b49e..55b76f1d20 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -5569,6 +5569,10 @@ void tst_qqmlecmascript::sequenceConversionCopy() QCOMPARE(object->property("success").toBool(), true); QMetaObject::invokeMethod(object, "testEqualitySemantics"); QCOMPARE(object->property("success").toBool(), true); + QMetaObject::invokeMethod(object, "testCopyParameters"); + QCOMPARE(object->property("success").toBool(), true); + QMetaObject::invokeMethod(object, "testReferenceParameters"); + QCOMPARE(object->property("success").toBool(), true); delete object; } -- cgit v1.2.3