diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2011-10-07 11:56:28 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-10-14 14:46:35 +0200 |
commit | d5aa4355f143e6f7fda6ace3fa6a564fcdc4493c (patch) | |
tree | 24d80eb59b65382684ada0009d6008683e8ff147 /tests/auto/gui/kernel | |
parent | 45358303d13569891ec7f058b4f3ffb3b23dc31b (diff) |
Add autotest for QMetaType creation of gui types
This test verifies that all gui types with built-in QMetaType
support can be created, either using the default constructor
or the copy constructor.
Change-Id: Ibb1c5aab8571b598638c74112471d6869516a202
Reviewed-on: http://codereview.qt-project.org/6344
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'tests/auto/gui/kernel')
-rw-r--r-- | tests/auto/gui/kernel/kernel.pro | 1 | ||||
-rw-r--r-- | tests/auto/gui/kernel/qguimetatype/qguimetatype.pro | 4 | ||||
-rw-r--r-- | tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp | 275 |
3 files changed, 280 insertions, 0 deletions
diff --git a/tests/auto/gui/kernel/kernel.pro b/tests/auto/gui/kernel/kernel.pro index c77ea2be2e..16bda68629 100644 --- a/tests/auto/gui/kernel/kernel.pro +++ b/tests/auto/gui/kernel/kernel.pro @@ -5,6 +5,7 @@ SUBDIRS=\ qevent \ qfileopenevent \ qinputpanel \ + qguimetatype \ qguivariant \ qkeysequence \ qmouseevent \ diff --git a/tests/auto/gui/kernel/qguimetatype/qguimetatype.pro b/tests/auto/gui/kernel/qguimetatype/qguimetatype.pro new file mode 100644 index 0000000000..3386cfe2f2 --- /dev/null +++ b/tests/auto/gui/kernel/qguimetatype/qguimetatype.pro @@ -0,0 +1,4 @@ +load(qttest_p4) +SOURCES += tst_qguimetatype.cpp +QT = core gui +CONFIG += parallel_test diff --git a/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp b/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp new file mode 100644 index 0000000000..4b5aa347a8 --- /dev/null +++ b/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp @@ -0,0 +1,275 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtCore> +#include <QtGui> +#include <QtTest/QtTest> + +Q_DECLARE_METATYPE(QMetaType::Type) + +class tst_QGuiMetaType: public QObject +{ + Q_OBJECT +private slots: + void create_data(); + void create(); + void createCopy_data(); + void createCopy(); +}; + +#define FOR_EACH_GUI_METATYPE(F) \ + F(QFont, QFont) \ + F(QPixmap, QPixmap) \ + F(QBrush, QBrush) \ + F(QColor, QColor) \ + F(QPalette, QPalette) \ + F(QImage, QImage) \ + F(QPolygon, QPolygon) \ + F(QRegion, QRegion) \ + F(QBitmap, QBitmap) \ + F(QCursor, QCursor) \ + F(QKeySequence, QKeySequence) \ + F(QPen, QPen) \ + F(QTextLength, QTextLength) \ + F(QTextFormat, QTextFormat) \ + F(QMatrix, QMatrix) \ + F(QTransform, QTransform) \ + F(QMatrix4x4, QMatrix4x4) \ + F(QVector2D, QVector2D) \ + F(QVector3D, QVector3D) \ + F(QVector4D, QVector4D) \ + F(QQuaternion, QQuaternion) + +template <int ID> +struct MetaEnumToType {}; + +#define DEFINE_META_ENUM_TO_TYPE(TYPE, ID) \ +template<> \ +struct MetaEnumToType<QMetaType::ID> { \ + typedef TYPE Type; \ +}; +FOR_EACH_GUI_METATYPE(DEFINE_META_ENUM_TO_TYPE) +#undef DEFINE_META_ENUM_TO_TYPE + +// Not all types have operator== +template <int ID> +struct TypeComparator +{ + typedef typename MetaEnumToType<ID>::Type Type; + static bool equal(const Type &v1, const Type &v2) + { return v1 == v2; } +}; + +template<> struct TypeComparator<QMetaType::QPixmap> +{ + static bool equal(const QPixmap &v1, const QPixmap &v2) + { return v1.size() == v2.size(); } +}; + +template<> struct TypeComparator<QMetaType::QBitmap> +{ + static bool equal(const QBitmap &v1, const QBitmap &v2) + { return v1.size() == v2.size(); } +}; + +template<> struct TypeComparator<QMetaType::QCursor> +{ + static bool equal(const QCursor &v1, const QCursor &v2) + { return v1.shape() == v2.shape(); } +}; + +template <int ID> +struct DefaultValueFactory +{ + typedef typename MetaEnumToType<ID>::Type Type; + static Type *create() { return new Type; } +}; + +template <int ID> +struct TestValueFactory {}; + +template<> struct TestValueFactory<QMetaType::QFont> { + static QFont *create() { return new QFont("Arial"); } +}; +template<> struct TestValueFactory<QMetaType::QPixmap> { + static QPixmap *create() { return new QPixmap(16, 32); } +}; +template<> struct TestValueFactory<QMetaType::QBrush> { + static QBrush *create() { return new QBrush(Qt::SolidPattern); } +}; +template<> struct TestValueFactory<QMetaType::QColor> { + static QColor *create() { return new QColor(Qt::blue); } +}; +template<> struct TestValueFactory<QMetaType::QPalette> { + static QPalette *create() { return new QPalette(Qt::yellow, Qt::green); } +}; +template<> struct TestValueFactory<QMetaType::QImage> { + static QImage *create() { return new QImage(16, 32, QImage::Format_ARGB32_Premultiplied); } +}; +template<> struct TestValueFactory<QMetaType::QPolygon> { + static QPolygon *create() { return new QPolygon(QRect(10, 20, 30, 40), true); } +}; +template<> struct TestValueFactory<QMetaType::QRegion> { + static QRegion *create() { return new QRegion(QRect(10, 20, 30, 40), QRegion::Ellipse); } +}; +template<> struct TestValueFactory<QMetaType::QBitmap> { + static QBitmap *create() { return new QBitmap(16, 32); } +}; +template<> struct TestValueFactory<QMetaType::QCursor> { + static QCursor *create() { return new QCursor(Qt::WaitCursor); } +}; +template<> struct TestValueFactory<QMetaType::QKeySequence> { + static QKeySequence *create() { return new QKeySequence(QKeySequence::Close); } +}; +template<> struct TestValueFactory<QMetaType::QPen> { + static QPen *create() { return new QPen(Qt::DashDotDotLine); } +}; +template<> struct TestValueFactory<QMetaType::QTextLength> { + static QTextLength *create() { return new QTextLength(QTextLength::PercentageLength, 50); } +}; +template<> struct TestValueFactory<QMetaType::QTextFormat> { + static QTextFormat *create() { return new QTextFormat(QTextFormat::TableFormat); } +}; +template<> struct TestValueFactory<QMetaType::QMatrix> { + static QMatrix *create() { return new QMatrix(10, 20, 30, 40, 50, 60); } +}; +template<> struct TestValueFactory<QMetaType::QTransform> { + static QTransform *create() { return new QTransform(10, 20, 30, 40, 50, 60); } +}; +template<> struct TestValueFactory<QMetaType::QMatrix4x4> { + static QMatrix4x4 *create() { return new QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); } +}; +template<> struct TestValueFactory<QMetaType::QVector2D> { + static QVector2D *create() { return new QVector2D(10, 20); } +}; +template<> struct TestValueFactory<QMetaType::QVector3D> { + static QVector3D *create() { return new QVector3D(10, 20, 30); } +}; +template<> struct TestValueFactory<QMetaType::QVector4D> { + static QVector4D *create() { return new QVector4D(10, 20, 30, 40); } +}; +template<> struct TestValueFactory<QMetaType::QQuaternion> { + static QQuaternion *create() { return new QQuaternion(10, 20, 30, 40); } +}; + +void tst_QGuiMetaType::create_data() +{ + QTest::addColumn<QMetaType::Type>("type"); +#define ADD_METATYPE_TEST_ROW(TYPE, ID) \ + QTest::newRow(QMetaType::typeName(QMetaType::ID)) << QMetaType::ID; +FOR_EACH_GUI_METATYPE(ADD_METATYPE_TEST_ROW) +#undef ADD_METATYPE_TEST_ROW +} + +template <int ID> +static void testCreateHelper() +{ + typedef typename MetaEnumToType<ID>::Type Type; + void *actual = QMetaType::create(ID); + Type *expected = DefaultValueFactory<ID>::create(); + QVERIFY(TypeComparator<ID>::equal(*static_cast<Type *>(actual), *expected)); + delete expected; + QMetaType::destroy(ID, actual); +} + +typedef void (*TypeTestFunction)(); + +void tst_QGuiMetaType::create() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CREATE_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testCreateHelper<QMetaType::ID>; +FOR_EACH_GUI_METATYPE(RETURN_CREATE_FUNCTION) +#undef RETURN_CREATE_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + +void tst_QGuiMetaType::createCopy_data() +{ + create_data(); +} + +template <int ID> +static void testCreateCopyHelper() +{ + typedef typename MetaEnumToType<ID>::Type Type; + Type *expected = TestValueFactory<ID>::create(); + void *actual = QMetaType::create(ID, expected); + QVERIFY(TypeComparator<ID>::equal(*static_cast<Type*>(actual), *expected)); + QMetaType::destroy(ID, actual); + delete expected; +} + +void tst_QGuiMetaType::createCopy() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CREATE_COPY_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testCreateCopyHelper<QMetaType::ID>; +FOR_EACH_GUI_METATYPE(RETURN_CREATE_COPY_FUNCTION) +#undef RETURN_CREATE_COPY_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + +QTEST_MAIN(tst_QGuiMetaType) +#include "tst_qguimetatype.moc" |