diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp index 5cac80191c..df76dfc47f 100644 --- a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp +++ b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -50,6 +51,8 @@ private slots: void hasStdCppSet(); void isConstant(); void isFinal(); + void gadget(); + void readAndWriteWithLazyRegistration(); public: enum EnumType { EnumType1 }; @@ -103,5 +106,82 @@ void tst_QMetaProperty::isFinal() QVERIFY(!prop.isFinal()); } +class MyGadget { + Q_GADGET + Q_PROPERTY(QString value READ getValue WRITE setValue RESET resetValue) +public: + QString m_value; + void setValue(const QString &value) { m_value = value; } + QString getValue() { return m_value; } + void resetValue() { m_value = QLatin1Literal("reset"); } +}; + +void tst_QMetaProperty::gadget() +{ + const QMetaObject *mo = &MyGadget::staticMetaObject; + QMetaProperty valueProp = mo->property(mo->indexOfProperty("value")); + QVERIFY(valueProp.isValid()); + { + MyGadget g; + QString hello = QLatin1Literal("hello"); + QVERIFY(valueProp.writeOnGadget(&g, hello)); + QCOMPARE(g.m_value, QLatin1String("hello")); + QCOMPARE(valueProp.readOnGadget(&g), QVariant(hello)); + QVERIFY(valueProp.resetOnGadget(&g)); + QCOMPARE(valueProp.readOnGadget(&g), QVariant(QLatin1String("reset"))); + } +} + +struct CustomReadObject : QObject +{ + Q_OBJECT +}; + +struct CustomWriteObject : QObject +{ + Q_OBJECT +}; + +struct CustomWriteObjectChild : CustomWriteObject +{ + Q_OBJECT +}; + +struct TypeLazyRegistration : QObject +{ + Q_OBJECT + Q_PROPERTY(CustomReadObject *read MEMBER _read) + Q_PROPERTY(CustomWriteObject *write MEMBER _write) + + CustomReadObject *_read; + CustomWriteObject *_write; + +public: + TypeLazyRegistration() + : _read() + , _write() + {} +}; + +void tst_QMetaProperty::readAndWriteWithLazyRegistration() +{ + QCOMPARE(QMetaType::type("CustomReadObject*"), int(QMetaType::UnknownType)); + QCOMPARE(QMetaType::type("CustomWriteObject*"), int(QMetaType::UnknownType)); + + TypeLazyRegistration o; + QVERIFY(o.property("read").isValid()); + QVERIFY(QMetaType::type("CustomReadObject*") != QMetaType::UnknownType); + QCOMPARE(QMetaType::type("CustomWriteObject*"), int(QMetaType::UnknownType)); + + CustomWriteObjectChild data; + QVariant value = QVariant::fromValue(&data); // this register CustomWriteObjectChild + // check if base classes are not registered automatically, otherwise this test would be meaningless + QCOMPARE(QMetaType::type("CustomWriteObject*"), int(QMetaType::UnknownType)); + QVERIFY(o.setProperty("write", value)); + QVERIFY(QMetaType::type("CustomWriteObject*") != QMetaType::UnknownType); + QCOMPARE(o.property("write").value<CustomWriteObjectChild*>(), &data); +} + + QTEST_MAIN(tst_QMetaProperty) #include "tst_qmetaproperty.moc" |