diff options
author | Matthew Vogt <matthew.vogt@nokia.com> | 2012-08-01 10:27:17 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-10 05:40:49 +0200 |
commit | 4350877d6deb58f36df24164c6edde3302a3f1a3 (patch) | |
tree | 5b1b121c1ce21aff1717de500282a5951f4e1267 /tests/auto/qml/qqmlvaluetypeproviders | |
parent | 34ae6deb78c30a80570e0c0dda7b2f071abdbf68 (diff) |
Permit value types with metatype IDs >= QMetaType::User
Remove the assumption that value types must be types defined by
Qt, having metatype IDs below QMetaType::User.
Task-number: QTBUG-26352
Change-Id: Ib5a56ff2e7892e82adf17a3a1e7517a0c9fe0534
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'tests/auto/qml/qqmlvaluetypeproviders')
-rw-r--r-- | tests/auto/qml/qqmlvaluetypeproviders/data/userType.qml | 88 | ||||
-rw-r--r-- | tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp | 118 |
2 files changed, 206 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlvaluetypeproviders/data/userType.qml b/tests/auto/qml/qqmlvaluetypeproviders/data/userType.qml new file mode 100644 index 0000000000..d2f748c4c4 --- /dev/null +++ b/tests/auto/qml/qqmlvaluetypeproviders/data/userType.qml @@ -0,0 +1,88 @@ +import QtQuick 2.0 +import Test 1.0 + +Item { + property bool success: false + + // Test user value type stored as both var and variant + property var testValue1 + property variant testValue2 + property variant testValue3 + property var testValue4 + + TestValueExporter { + id: assignmentValueType + testValue.property1: 1 + testValue.property2: 3.1415927 + } + + TestValueExporter { + id: v4BindingValueType + testValue.property1: 1 + 2 + testValue.property2: 3.1415927 / 2.0 + } + + TestValueExporter { + id: v8BindingValueType + testValue.property1: if (true) 1 + 2 + testValue.property2: if (true) 3.1415927 / 2.0 + } + + function numberEqual(lhs, rhs) { + var d = (lhs - rhs) + return (Math.abs(d) < 0.0001) + } + + Component.onCompleted: { + // Poperties assigned the result of Q_INVOKABLE: + testValue1 = testValueExporter.getTestValue() + testValue2 = testValueExporter.getTestValue() + + if (testValue1.property1 != 333) return + if (!numberEqual(testValue1.property2, 666.999)) return + + if (testValue2.property1 != 333) return + if (!numberEqual(testValue2.property2, 666.999)) return + + if (testValue1 != testValue2) return + + // Write to the properties of the value type + testValue1.property1 = 1 + testValue1.property2 = 3.1415927 + + testValue2.property1 = 1 + testValue2.property2 = 3.1415927 + + if (testValue1.property1 != 1) return + if (!numberEqual(testValue1.property2, 3.1415927)) return + + if (testValue2.property1 != 1) return + if (!numberEqual(testValue2.property2, 3.1415927)) return + + if (testValue1 != testValue2) return + + // Assignment of value type properties + testValue3 = testValue1 + testValue4 = testValue2 + + if (testValue3.property1 != 1) return + if (!numberEqual(testValue3.property2, 3.1415927)) return + + if (testValue4.property1 != 1) return + if (!numberEqual(testValue4.property2, 3.1415927)) return + + if (testValue3 != testValue4) return + + // Access a value-type property of a QObject + var vt = testValueExporter.testValue + if (vt.property1 != 0) return + if (!numberEqual(vt.property2, 0.0)) return + + testValueExporter.testValue = testValue4 + + if (vt.property1 != 1) return + if (!numberEqual(vt.property2, 3.1415927)) return + + success = true + } +} diff --git a/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp b/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp index 20cc93bb7b..7c40a73812 100644 --- a/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp +++ b/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp @@ -42,7 +42,9 @@ #include <qtest.h> #include <QQmlEngine> #include <QQmlComponent> +#include <QQmlContext> #include <QDebug> +#include <private/qqmlglobal_p.h> #include <private/qquickvaluetypes_p.h> #include "../../shared/util.h" #include "testtypes.h" @@ -71,6 +73,7 @@ private slots: void cppIntegration(); void jsObjectConversion(); void invokableFunctions(); + void userType(); }; void tst_qqmlvaluetypeproviders::initTestCase() @@ -182,6 +185,121 @@ void tst_qqmlvaluetypeproviders::invokableFunctions() delete object; } +namespace { + +// A value-type class to export to QML +class TestValue +{ +public: + TestValue() : m_p1(0), m_p2(0.0) {} + TestValue(int p1, double p2) : m_p1(p1), m_p2(p2) {} + TestValue(const TestValue &other) : m_p1(other.m_p1), m_p2(other.m_p2) {} + ~TestValue() {} + + TestValue &operator=(const TestValue &other) { m_p1 = other.m_p1; m_p2 = other.m_p2; return *this; } + + int property1() const { return m_p1; } + void setProperty1(int p1) { m_p1 = p1; } + + double property2() const { return m_p2; } + void setProperty2(double p2) { m_p2 = p2; } + + bool operator==(const TestValue &other) const { return (m_p1 == other.m_p1) && (m_p2 == other.m_p2); } + bool operator!=(const TestValue &other) const { return !operator==(other); } + +private: + int m_p1; + double m_p2; +}; + +} + +Q_DECLARE_METATYPE(TestValue); + +namespace { + +class TestValueType : public QQmlValueTypeBase<TestValue> +{ + Q_OBJECT + Q_PROPERTY(int property1 READ property1 WRITE setProperty1) + Q_PROPERTY(double property2 READ property2 WRITE setProperty2) +public: + TestValueType(QObject *parent = 0) : QQmlValueTypeBase<TestValue>(qMetaTypeId<TestValue>(), parent) {} + + virtual QString toString() const { return QString::number(property1()) + QLatin1Char(',') + QString::number(property2()); } + virtual bool isEqual(const QVariant &other) const { return (other.userType() == qMetaTypeId<TestValue>()) && (v == other.value<TestValue>()); } + + int property1() const { return v.property1(); } + void setProperty1(int p1) { v.setProperty1(p1); } + + double property2() const { return v.property2(); } + void setProperty2(double p2) { v.setProperty2(p2); } +}; + +class TestValueTypeProvider : public QQmlValueTypeProvider +{ +public: + bool create(int type, QQmlValueType *&v) + { + if (type == qMetaTypeId<TestValue>()) { + v = new TestValueType; + return true; + } + + return false; + } + +}; + +TestValueTypeProvider *getValueTypeProvider() +{ + static TestValueTypeProvider valueTypeProvider; + return &valueTypeProvider; +} + +bool initializeProviders() +{ + QQml_addValueTypeProvider(getValueTypeProvider()); + return true; +} + +const bool initialized = initializeProviders(); + +class TestValueExporter : public QObject +{ + Q_OBJECT + Q_PROPERTY(TestValue testValue READ testValue WRITE setTestValue) +public: + TestValue testValue() const { return m_testValue; } + void setTestValue(const TestValue &v) { m_testValue = v; } + + Q_INVOKABLE TestValue getTestValue() const { return TestValue(333, 666.999); } + +private: + TestValue m_testValue; +}; + +} + +void tst_qqmlvaluetypeproviders::userType() +{ + Q_ASSERT(initialized); + Q_ASSERT(qMetaTypeId<TestValue>() >= QMetaType::User); + + qRegisterMetaType<TestValue>(); + qmlRegisterType<TestValueExporter>("Test", 1, 0, "TestValueExporter"); + + TestValueExporter exporter; + + QQmlEngine e; + e.rootContext()->setContextProperty("testValueExporter", &exporter); + + QQmlComponent component(&e, testFileUrl("userType.qml")); + QScopedPointer<QObject> obj(component.create()); + QVERIFY(obj != 0); + QCOMPARE(obj->property("success").toBool(), true); +} + QTEST_MAIN(tst_qqmlvaluetypeproviders) #include "tst_qqmlvaluetypeproviders.moc" |