diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 230 |
1 files changed, 213 insertions, 17 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index a35896283a..717a7633f5 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ @@ -79,6 +79,10 @@ private slots: void normalizedTypes(); void typeName_data(); void typeName(); + void type_data(); + void type(); + void type_fromSubString_data(); + void type_fromSubString(); void create_data(); void create(); void createCopy_data(); @@ -109,6 +113,7 @@ private slots: void saveAndLoadBuiltin_data(); void saveAndLoadBuiltin(); void saveAndLoadCustom(); + void metaObject_data(); void metaObject(); void constexprMetaTypeIds(); void constRefs(); @@ -116,6 +121,7 @@ private slots: void convertCustomType(); void compareCustomType_data(); void compareCustomType(); + void compareCustomEqualOnlyType(); void customDebugStream(); }; @@ -130,6 +136,11 @@ public: : QObject(parent) { } + enum CustomQEnum { Val1, Val2 }; + Q_ENUM(CustomQEnum) +}; +class CustomGadget { + Q_GADGET }; class CustomNonQObject {}; @@ -140,10 +151,13 @@ void tst_QMetaType::defined() QCOMPARE(int(QMetaTypeId2<Foo>::Defined), 0); QCOMPARE(int(QMetaTypeId2<void*>::Defined), 1); QCOMPARE(int(QMetaTypeId2<int*>::Defined), 0); - QVERIFY(QMetaTypeId2<CustomQObject*>::Defined); + QCOMPARE(int(QMetaTypeId2<CustomQObject::CustomQEnum>::Defined), 1); + QCOMPARE(int(QMetaTypeId2<CustomGadget>::Defined), 1); + QVERIFY(int(QMetaTypeId2<CustomQObject*>::Defined)); QVERIFY(!QMetaTypeId2<CustomQObject>::Defined); QVERIFY(!QMetaTypeId2<CustomNonQObject>::Defined); QVERIFY(!QMetaTypeId2<CustomNonQObject*>::Defined); + QVERIFY(!QMetaTypeId2<CustomGadget*>::Defined); } struct Bar @@ -372,6 +386,11 @@ void tst_QMetaType::typeName_data() QTest::newRow("QMap<int,int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QMap<int, int> >()) << QString::fromLatin1("QMap<int,int>"); QTest::newRow("QVector<QList<int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QList<int> > >()) << QString::fromLatin1("QVector<QList<int> >"); QTest::newRow("QVector<QMap<int,int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QMap<int, int> > >()) << QString::fromLatin1("QVector<QMap<int,int> >"); + + QTest::newRow("CustomQObject*") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomQObject*>()) << QString::fromLatin1("CustomQObject*"); + QTest::newRow("CustomGadget") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomGadget>()) << QString::fromLatin1("CustomGadget"); + QTest::newRow("CustomQObject::CustomQEnum") << static_cast<QMetaType::Type>(::qMetaTypeId<CustomQObject::CustomQEnum>()) << QString::fromLatin1("CustomQObject::CustomQEnum"); + QTest::newRow("Qt::ArrowType") << static_cast<QMetaType::Type>(::qMetaTypeId<Qt::ArrowType>()) << QString::fromLatin1("Qt::ArrowType"); } void tst_QMetaType::typeName() @@ -385,6 +404,61 @@ void tst_QMetaType::typeName() QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData())); } +void tst_QMetaType::type_data() +{ + QTest::addColumn<QMetaType::Type>("aType"); + QTest::addColumn<QByteArray>("aTypeName"); + +#define TST_QMETATYPE_TYPE_DATA(MetaTypeName, MetaTypeId, RealType)\ + QTest::newRow(#RealType) << QMetaType::MetaTypeName << QByteArray( #RealType ); +#define TST_QMETATYPE_TYPE_DATA_ALIAS(MetaTypeName, MetaTypeId, AliasType, RealTypeString)\ + QTest::newRow(RealTypeString) << QMetaType::MetaTypeName << QByteArray( #AliasType ); + + QTest::newRow("empty") << QMetaType::UnknownType << QByteArray(); + + QT_FOR_EACH_STATIC_TYPE(TST_QMETATYPE_TYPE_DATA) + QT_FOR_EACH_STATIC_ALIAS_TYPE(TST_QMETATYPE_TYPE_DATA_ALIAS) + +#undef TST_QMETATYPE_TYPE_DATA +#undef TST_METATYPE_TYPE_DATA_ALIAS +} + +void tst_QMetaType::type() +{ + QFETCH(QMetaType::Type, aType); + QFETCH(QByteArray, aTypeName); + + // QMetaType::type(QByteArray) + QCOMPARE(QMetaType::type(aTypeName), int(aType)); + // QMetaType::type(const char *) + QCOMPARE(QMetaType::type(aTypeName.constData()), int(aType)); +} + +void tst_QMetaType::type_fromSubString_data() +{ + QTest::addColumn<int>("offset"); + QTest::addColumn<int>("size"); + QTest::addColumn<int>("expectedType"); + + // The test string is defined in the test function below + QTest::newRow("int") << 0 << 3 << int(QMetaType::Int); + QTest::newRow("boo") << 3 << 3 << 0; + QTest::newRow("bool") << 3 << 4 << int(QMetaType::Bool); + QTest::newRow("intbool") << 0 << 7 << 0; + QTest::newRow("QMetaType::Type") << 7 << 15 << ::qMetaTypeId<QMetaType::Type>(); + QTest::newRow("double") << 22 << 6 << int(QMetaType::Double); +} + +void tst_QMetaType::type_fromSubString() +{ + static const char *types = "intboolQMetaType::Typedoublexxx"; + QFETCH(int, offset); + QFETCH(int, size); + QFETCH(int, expectedType); + QByteArray ba = QByteArray::fromRawData(types + offset, size); + QCOMPARE(QMetaType::type(ba), expectedType); +} + #define FOR_EACH_PRIMITIVE_METATYPE(F) \ QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F) \ QT_FOR_EACH_STATIC_CORE_POINTER(F) \ @@ -968,7 +1042,9 @@ void tst_QMetaType::flagsBinaryCompatibility5_0() QFETCH(quint32, id); QFETCH(quint32, flags); - QCOMPARE(quint32(QMetaType::typeFlags(id)), flags); + quint32 mask_5_0 = 0x1ff; // Only compare the values that were already defined in 5.0 + + QCOMPARE(quint32(QMetaType::typeFlags(id)) & mask_5_0, flags); } void tst_QMetaType::construct_data() @@ -1772,17 +1848,48 @@ void tst_QMetaType::saveAndLoadCustom() QCOMPARE(stream.status(), QDataStream::ReadPastEnd); } +class MyGadget { + Q_GADGET; +public: + enum MyEnum { Val1, Val2, Val3 }; + Q_ENUM(MyEnum) +}; + +Q_DECLARE_METATYPE(MyGadget); +Q_DECLARE_METATYPE(const QMetaObject *); +Q_DECLARE_METATYPE(Qt::ScrollBarPolicy); +Q_DECLARE_METATYPE(MyGadget::MyEnum); + +void tst_QMetaType::metaObject_data() +{ + QTest::addColumn<int>("type"); + QTest::addColumn<const QMetaObject*>("result"); + QTest::addColumn<bool>("isGadget"); + QTest::addColumn<bool>("isQObjectPtr"); + + QTest::newRow("QObject") << int(QMetaType::QObjectStar) << &QObject::staticMetaObject << false << true; + QTest::newRow("QFile*") << ::qMetaTypeId<QFile*>() << &QFile::staticMetaObject << false << true; + QTest::newRow("MyObject*") << ::qMetaTypeId<MyObject*>() << &MyObject::staticMetaObject << false << true; + QTest::newRow("int") << int(QMetaType::Int) << static_cast<const QMetaObject *>(0) << false << false; + QTest::newRow("QEasingCurve") << ::qMetaTypeId<QEasingCurve>() << &QEasingCurve::staticMetaObject << true << false; + QTest::newRow("MyGadget") << ::qMetaTypeId<MyGadget>() << &MyGadget::staticMetaObject << true << false; + QTest::newRow("MyEnum") << ::qMetaTypeId<MyGadget::MyEnum>() << &MyGadget::staticMetaObject << false << false; + QTest::newRow("Qt::ScrollBarPolicy") << ::qMetaTypeId<Qt::ScrollBarPolicy>() << &QObject::staticQtMetaObject << false << false; +} + + void tst_QMetaType::metaObject() { - QCOMPARE(QMetaType::metaObjectForType(QMetaType::QObjectStar), &QObject::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(::qMetaTypeId<QFile*>()), &QFile::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(::qMetaTypeId<MyObject*>()), &MyObject::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(QMetaType::Int), static_cast<const QMetaObject *>(0)); + QFETCH(int, type); + QFETCH(const QMetaObject *, result); + QFETCH(bool, isGadget); + QFETCH(bool, isQObjectPtr); - QCOMPARE(QMetaType(QMetaType::QObjectStar).metaObject(), &QObject::staticMetaObject); - QCOMPARE(QMetaType(::qMetaTypeId<QFile*>()).metaObject(), &QFile::staticMetaObject); - QCOMPARE(QMetaType(::qMetaTypeId<MyObject*>()).metaObject(), &MyObject::staticMetaObject); - QCOMPARE(QMetaType(QMetaType::Int).metaObject(), static_cast<const QMetaObject *>(0)); + QCOMPARE(QMetaType::metaObjectForType(type), result); + QMetaType mt(type); + QCOMPARE(mt.metaObject(), result); + QCOMPARE(!!(mt.flags() & QMetaType::IsGadget), isGadget); + QCOMPARE(!!(mt.flags() & QMetaType::PointerToQObject), isQObjectPtr); } #define METATYPE_ID_FUNCTION(Type, MetaTypeId, Name) \ @@ -1917,9 +2024,23 @@ bool operator==(const CustomConvertibleType2 &lhs, const CustomConvertibleType2 bool operator!=(const CustomConvertibleType2 &lhs, const CustomConvertibleType2 &rhs) { return !operator==(lhs, rhs); } + +struct CustomEqualsOnlyType +{ + explicit CustomEqualsOnlyType(int value = 0) : val(value) {} + virtual ~CustomEqualsOnlyType() {} + + int val; +}; +bool operator==(const CustomEqualsOnlyType &lhs, const CustomEqualsOnlyType &rhs) +{ return lhs.val == rhs.val;} +bool operator!=(const CustomEqualsOnlyType &lhs, const CustomEqualsOnlyType &rhs) +{ return !operator==(lhs, rhs); } + Q_DECLARE_METATYPE(CustomConvertibleType); Q_DECLARE_METATYPE(CustomConvertibleType2); Q_DECLARE_METATYPE(CustomDebugStreamableType); +Q_DECLARE_METATYPE(CustomEqualsOnlyType); template<typename T, typename U> U convert(const T &t) @@ -2196,6 +2317,81 @@ void tst_QMetaType::compareCustomType() QCOMPARE(unsorted, sorted); } +void tst_QMetaType::compareCustomEqualOnlyType() +{ + int metaTypeId = qRegisterMetaType<CustomEqualsOnlyType>(); + QMetaType::registerEqualsComparator<CustomEqualsOnlyType>(); + int result; + + CustomEqualsOnlyType val50(50); + CustomEqualsOnlyType val100(100); + CustomEqualsOnlyType val100x(100); + + QVariant variant50 = QVariant::fromValue(val50); + QVariant variant100 = QVariant::fromValue(val100); + QVariant variant100x = QVariant::fromValue(val100x); + + QVERIFY(variant50 != variant100); + QVERIFY(variant50 != variant100x); + QVERIFY(variant100 != variant50); + QVERIFY(variant100x != variant50); + QVERIFY(variant100 == variant100x); + QVERIFY(variant100 == variant100); + + // compare always fails + QVERIFY(!(variant50 < variant50)); + QVERIFY(!(variant50 < variant100)); + QVERIFY(!(variant100 < variant50)); + + // check QMetaType::compare works/doesn't crash for equals only comparators + bool wasSuccess = QMetaType::compare(variant50.constData(), variant50.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::compare(variant100.constData(), variant100x.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + + wasSuccess = QMetaType::compare(variant50.constData(), variant100.constData(), + metaTypeId, &result); + QVERIFY(!wasSuccess); + + // check QMetaType::equals works for equals only comparator + wasSuccess = QMetaType::equals(variant50.constData(), variant50.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::equals(variant100.constData(), variant100.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::equals(variant100x.constData(), variant100x.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::equals(variant100.constData(), variant100x.constData(), + metaTypeId, &result); + QCOMPARE(result, 0); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::equals(variant50.constData(), variant100.constData(), + metaTypeId, &result); + QCOMPARE(result, -1); + QVERIFY(wasSuccess); + wasSuccess = QMetaType::equals(variant50.constData(), variant100x.constData(), + metaTypeId, &result); + QCOMPARE(result, -1); + QVERIFY(wasSuccess); + + //check QMetaType::equals for type w/o equals comparator being registered + CustomMovable movable1; + CustomMovable movable2; + wasSuccess = QMetaType::equals(&movable1, &movable2, + qRegisterMetaType<CustomMovable>(), &result); + QVERIFY(!wasSuccess); + +} + struct MessageHandlerCustom : public MessageHandler { MessageHandlerCustom(const int typeId) |