diff options
author | Liang Qi <liang.qi@theqtcompany.com> | 2015-10-14 15:45:35 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-10-14 15:45:35 +0200 |
commit | 4456984da780b14572e1ec0f079a4d349ab299bd (patch) | |
tree | f586a281a81c57c91c49e83a5d3ec6c7eece0578 /tests/auto/corelib/kernel/qmetaproperty | |
parent | e824abd987d77efaa085fe1f9fb514d270798d55 (diff) | |
parent | 281121697340084f7d385eab530f41916789b94d (diff) |
Merge remote-tracking branch 'origin/5.6' into dev
Conflicts:
tests/auto/corelib/io/qfile/tst_qfile.cpp
tests/auto/corelib/io/qprocess/tst_qprocess.cpp
tests/auto/corelib/tools/qversionnumber/qversionnumber.pro
Change-Id: Ia93ce500349d96a2fbf0b4a37b73f088cc505c6e
Diffstat (limited to 'tests/auto/corelib/kernel/qmetaproperty')
-rw-r--r-- | tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp index 4d54aa4dc8..22c78f8e48 100644 --- a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp +++ b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. -** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com> +** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -38,15 +38,29 @@ #include <qobject.h> #include <qmetaobject.h> +struct CustomType +{ + int padding; + QString str; + CustomType(const QString &str = QString()) : str(str) {} + operator QString() const { return str; } + friend bool operator!=(const CustomType &a, const CustomType &b) + { return a.str != b.str; } +}; + +Q_DECLARE_METATYPE(CustomType) + class tst_QMetaProperty : public QObject { Q_OBJECT Q_PROPERTY(EnumType value WRITE setValue READ getValue) Q_PROPERTY(EnumType value2 WRITE set_value READ get_value) + Q_PROPERTY(QString value7 MEMBER value7 RESET resetValue7) Q_PROPERTY(int value8 READ value8) Q_PROPERTY(int value9 READ value9 CONSTANT) Q_PROPERTY(int value10 READ value10 FINAL) Q_PROPERTY(QMap<int, int> map MEMBER map) + Q_PROPERTY(CustomType custom MEMBER custom) private slots: void hasStdCppSet(); @@ -55,6 +69,7 @@ private slots: void gadget(); void readAndWriteWithLazyRegistration(); void mapProperty(); + void conversion(); public: enum EnumType { EnumType1 }; @@ -64,11 +79,14 @@ public: void set_value(EnumType) {} EnumType get_value() const { return EnumType1; } + void resetValue7() { value7 = QStringLiteral("reset"); } int value8() const { return 1; } int value9() const { return 1; } int value10() const { return 1; } + QString value7; QMap<int, int> map; + CustomType custom; }; void tst_QMetaProperty::hasStdCppSet() @@ -195,5 +213,42 @@ void tst_QMetaProperty::mapProperty() QCOMPARE(map, (v.value<QMap<int,int> >())); } +void tst_QMetaProperty::conversion() +{ + QMetaType::registerConverter<QString, CustomType>(); + QMetaType::registerConverter<CustomType, QString>(); + + QString hello = QStringLiteral("Hello"); + + // Write to a QString property using a CustomType in a QVariant + QMetaProperty value7P = metaObject()->property(metaObject()->indexOfProperty("value7")); + QVERIFY(value7P.isValid()); + QVERIFY(value7P.write(this, QVariant::fromValue(CustomType(hello)))); + QCOMPARE(value7, hello); + + // Write to a CustomType property using a QString in a QVariant + QMetaProperty customP = metaObject()->property(metaObject()->indexOfProperty("custom")); + QVERIFY(customP.isValid()); + QVERIFY(customP.write(this, hello)); + QCOMPARE(custom.str, hello); + + // Something that cannot be converted should fail + QVERIFY(!customP.write(this, 45)); + QVERIFY(!customP.write(this, QVariant::fromValue(this))); + QVERIFY(!value7P.write(this, QVariant::fromValue(this))); + QVERIFY(!value7P.write(this, QVariant::fromValue<QObject*>(this))); + + // none of this should have changed the values + QCOMPARE(value7, hello); + QCOMPARE(custom.str, hello); + + // Empty variant should be converted to default object + QVERIFY(customP.write(this, QVariant())); + QCOMPARE(custom.str, QString()); + // or reset resetable + QVERIFY(value7P.write(this, QVariant())); + QCOMPARE(value7, QLatin1Literal("reset")); +} + QTEST_MAIN(tst_QMetaProperty) #include "tst_qmetaproperty.moc" |