From cb43dec31e9adac5194b2d56b3348a3865eff219 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Fri, 13 Jul 2012 20:19:15 +0200 Subject: [PATCH] Split QWidgetsVariant tests into a separate testcase. This allows building the QVariant tests without the QtWidgets module. Change-Id: I7cd7e78a60c7bc7614ec16df1abe1e93e45d4923 Reviewed-by: Olivier Goffart --- tests/auto/corelib/kernel/qvariant/qvariant.pro | 3 +- .../auto/corelib/kernel/qvariant/tst_qvariant.cpp | 231 ++---------------- .../other/qvariant_common/tst_qvariant_common.h | 227 ++++++++++++++++++ .../kernel/qwidgetsvariant/qwidgetsvariant.pro | 7 + .../kernel/qwidgetsvariant/qwidgetsvariant.qrc | 6 + .../qwidgetsvariant}/stream/qt4.9/qsizepolicy.bin | Bin .../qwidgetsvariant}/stream/qt5.0/qsizepolicy.bin | Bin .../kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp | 258 +++++++++++++++++++++ 8 files changed, 514 insertions(+), 218 deletions(-) create mode 100644 tests/auto/other/qvariant_common/tst_qvariant_common.h create mode 100644 tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro create mode 100644 tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.qrc rename tests/auto/{corelib/kernel/qvariant => widgets/kernel/qwidgetsvariant}/stream/qt4.9/qsizepolicy.bin (100%) rename tests/auto/{corelib/kernel/qvariant => widgets/kernel/qwidgetsvariant}/stream/qt5.0/qsizepolicy.bin (100%) create mode 100644 tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp diff --git a/tests/auto/corelib/kernel/qvariant/qvariant.pro b/tests/auto/corelib/kernel/qvariant/qvariant.pro index 522a915..55dd5b4 100644 --- a/tests/auto/corelib/kernel/qvariant/qvariant.pro +++ b/tests/auto/corelib/kernel/qvariant/qvariant.pro @@ -1,6 +1,7 @@ CONFIG += testcase CONFIG += parallel_test TARGET = tst_qvariant -QT += widgets testlib +QT += gui testlib +INCLUDEPATH += $$PWD/../../../other/qvariant_common SOURCES = tst_qvariant.cpp RESOURCES += qvariant.qrc diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 0840e6a..3ced9dd 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -67,6 +67,15 @@ #include +#include +#include +#include +#include +#include +#include "qnumeric.h" + +#include "tst_qvariant_common.h" + class CustomNonQObject; class tst_QVariant : public QObject @@ -272,7 +281,6 @@ private slots: void saveQt5Stream(); void guiVariantAtExit(); - void widgetsVariantAtExit(); private: void dataStream_data(QDataStream::Version version); void loadQVariantFromDataStream(QDataStream::Version version); @@ -335,39 +343,8 @@ void tst_QVariant::constructor_invalid_data() QTest::newRow("0xfffffffff") << uint(0xfffffffff); QTest::newRow("LastCoreType + 1") << uint(QMetaType::LastCoreType + 1); QVERIFY(!QMetaType::isRegistered(QMetaType::LastCoreType + 1)); - QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1); - QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1)); - QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1); - QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1)); } -struct MessageHandlerInvalidType -{ - MessageHandlerInvalidType() - : oldMsgHandler(qInstallMsgHandler(handler)) - { - ok = false; - } - - ~MessageHandlerInvalidType() - { - qInstallMsgHandler(oldMsgHandler); - } - - QtMsgHandler oldMsgHandler; - - static void handler(QtMsgType type, const char *txt) - { - Q_UNUSED(type); - QString msg = QString::fromLatin1(txt); - // uint(-1) can be platform dependent so we check only beginning of the message. - ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:"); - QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData()); - } - static bool ok; -}; -bool MessageHandlerInvalidType::ok; - void tst_QVariant::constructor_invalid() { @@ -441,38 +418,7 @@ void tst_QVariant::swap() void tst_QVariant::canConvert_data() { - QTest::addColumn("val"); - QTest::addColumn("BitArrayCast"); - QTest::addColumn("BitmapCast"); - QTest::addColumn("BoolCast"); - QTest::addColumn("BrushCast"); - QTest::addColumn("ByteArrayCast"); - QTest::addColumn("ColorCast"); - QTest::addColumn("CursorCast"); - QTest::addColumn("DateCast"); - QTest::addColumn("DateTimeCast"); - QTest::addColumn("DoubleCast"); - QTest::addColumn("FontCast"); - QTest::addColumn("ImageCast"); - QTest::addColumn("IntCast"); - QTest::addColumn("InvalidCast"); - QTest::addColumn("KeySequenceCast"); - QTest::addColumn("ListCast"); - QTest::addColumn("LongLongCast"); - QTest::addColumn("MapCast"); - QTest::addColumn("PaletteCast"); - QTest::addColumn("PenCast"); - QTest::addColumn("PixmapCast"); - QTest::addColumn("PointCast"); - QTest::addColumn("RectCast"); - QTest::addColumn("RegionCast"); - QTest::addColumn("SizeCast"); - QTest::addColumn("SizePolicyCast"); - QTest::addColumn("StringCast"); - QTest::addColumn("StringListCast"); - QTest::addColumn("TimeCast"); - QTest::addColumn("UIntCast"); - QTest::addColumn("ULongLongCast"); + TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS #ifdef Y @@ -569,9 +515,6 @@ void tst_QVariant::canConvert_data() var = QVariant(QSize()); QTest::newRow("Size") << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N; - var = QVariant::fromValue(QSizePolicy()); - QTest::newRow("SizePolicy") - << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N; var = QVariant(QString()); QTest::newRow("String") << var << N << N << Y << N << Y << Y << N << Y << Y << Y << Y << N << Y << N << Y << N << Y << N << N << N << N << N << N << N << N << N << Y << Y << Y << Y << Y; @@ -603,71 +546,9 @@ void tst_QVariant::canConvert_data() void tst_QVariant::canConvert() { - QFETCH(QVariant, val); - QFETCH(bool, BitArrayCast); - QFETCH(bool, BitmapCast); - QFETCH(bool, BoolCast); - QFETCH(bool, BrushCast); - QFETCH(bool, ByteArrayCast); - QFETCH(bool, ColorCast); - QFETCH(bool, CursorCast); - QFETCH(bool, DateCast); - QFETCH(bool, DateTimeCast); - QFETCH(bool, DoubleCast); - QFETCH(bool, FontCast); - QFETCH(bool, ImageCast); - QFETCH(bool, IntCast); - QFETCH(bool, InvalidCast); - QFETCH(bool, KeySequenceCast); - QFETCH(bool, ListCast); - QFETCH(bool, LongLongCast); - QFETCH(bool, MapCast); - QFETCH(bool, PaletteCast); - QFETCH(bool, PenCast); - QFETCH(bool, PixmapCast); - QFETCH(bool, PointCast); - QFETCH(bool, RectCast); - QFETCH(bool, RegionCast); - QFETCH(bool, SizeCast); - QFETCH(bool, SizePolicyCast); - QFETCH(bool, StringCast); - QFETCH(bool, StringListCast); - QFETCH(bool, TimeCast); - QFETCH(bool, UIntCast); - QFETCH(bool, ULongLongCast); - - QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast); - QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast); - QCOMPARE(val.canConvert(QVariant::Bool), BoolCast); - QCOMPARE(val.canConvert(QVariant::Brush), BrushCast); - QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast); - QCOMPARE(val.canConvert(QVariant::Color), ColorCast); - QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast); - QCOMPARE(val.canConvert(QVariant::Date), DateCast); - QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast); - QCOMPARE(val.canConvert(QVariant::Double), DoubleCast); - QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast); - QCOMPARE(val.canConvert(QVariant::Font), FontCast); - QCOMPARE(val.canConvert(QVariant::Image), ImageCast); - QCOMPARE(val.canConvert(QVariant::Int), IntCast); - QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast); - QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast); - QCOMPARE(val.canConvert(QVariant::List), ListCast); - QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast); - QCOMPARE(val.canConvert(QVariant::Map), MapCast); - QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast); - QCOMPARE(val.canConvert(QVariant::Pen), PenCast); - QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast); - QCOMPARE(val.canConvert(QVariant::Point), PointCast); - QCOMPARE(val.canConvert(QVariant::Rect), RectCast); - QCOMPARE(val.canConvert(QVariant::Region), RegionCast); - QCOMPARE(val.canConvert(QVariant::Size), SizeCast); - QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast); - QCOMPARE(val.canConvert(QVariant::String), StringCast); - QCOMPARE(val.canConvert(QVariant::StringList), StringListCast); - QCOMPARE(val.canConvert(QVariant::Time), TimeCast); - QCOMPARE(val.canConvert(QVariant::UInt), UIntCast); - QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast); + TST_QVARIANT_CANCONVERT_FETCH_DATA + + TST_QVARIANT_CANCONVERT_COMPARE_DATA // Invalid type ids QCOMPARE(val.canConvert(-1), false); @@ -1577,7 +1458,6 @@ void tst_QVariant::writeToReadFromDataStream_data() QTest::newRow( "pointarray_valid" ) << QVariant::fromValue( QPolygon( QRect( 10, 10, 20, 20 ) ) ) << false; QTest::newRow( "region_invalid" ) << QVariant::fromValue( QRegion() ) << true; QTest::newRow( "region_valid" ) << QVariant::fromValue( QRegion( 10, 10, 20, 20 ) ) << false; - QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false; QTest::newRow( "point_invalid" ) << QVariant::fromValue( QPoint() ) << true; QTest::newRow( "point_valid" ) << QVariant::fromValue( QPoint( 10, 10 ) ) << false; QTest::newRow( "rect_invalid" ) << QVariant( QRect() ) << true; @@ -2559,13 +2439,7 @@ class CustomQObject : public QObject { public: CustomQObject(QObject *parent = 0) : QObject(parent) {} }; -class CustomQWidget : public QWidget { - Q_OBJECT -public: - CustomQWidget(QWidget *parent = 0) : QWidget(parent) {} -}; Q_DECLARE_METATYPE(CustomQObject*) -Q_DECLARE_METATYPE(CustomQWidget*) class CustomNonQObject { }; Q_DECLARE_METATYPE(CustomNonQObject) @@ -2587,15 +2461,9 @@ void tst_QVariant::qvariant_cast_QObject_data() QTest::newRow("from QObject2") << QVariant::fromValue(obj) << true; QTest::newRow("from String") << QVariant(QLatin1String("1, 2, 3")) << false; QTest::newRow("from int") << QVariant((int) 123) << false; - QWidget *widget = new QWidget; - widget->setObjectName(QString::fromLatin1("Hello")); - QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true; CustomQObject *customObject = new CustomQObject(this); customObject->setObjectName(QString::fromLatin1("Hello")); QTest::newRow("from Derived QObject") << QVariant::fromValue(customObject) << true; - CustomQWidget *customWidget = new CustomQWidget; - customWidget->setObjectName(QString::fromLatin1("Hello")); - QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true; QTest::newRow("from custom Object") << QVariant::fromValue(CustomNonQObject()) << false; // Deleted in cleanupTestCase. @@ -2604,9 +2472,7 @@ void tst_QVariant::qvariant_cast_QObject_data() // Deleted in cleanupTestCase. objectPointerTestData.push_back(obj); - objectPointerTestData.push_back(widget); objectPointerTestData.push_back(customObject); - objectPointerTestData.push_back(customWidget); } void tst_QVariant::qvariant_cast_QObject() @@ -2656,7 +2522,6 @@ void tst_QVariant::qvariant_cast_QObject_derived() QCOMPARE(data.value(), object); QCOMPARE(data.value(), object); QCOMPARE(data.value(), object); - QVERIFY(data.value() == 0); } { CustomQObjectDerived *object = new CustomQObjectDerived(this); @@ -2667,17 +2532,6 @@ void tst_QVariant::qvariant_cast_QObject_derived() QCOMPARE(data.value(), object); QCOMPARE(data.value(), object); QCOMPARE(data.value(), object); - QVERIFY(data.value() == 0); - } - { - CustomQWidget customWidget; - QWidget *widget = &customWidget; - QVariant data = QVariant::fromValue(widget); - QVERIFY(data.userType() == QMetaType::QWidgetStar); - - QCOMPARE(data.value(), widget); - QCOMPARE(data.value(), widget); - QCOMPARE(data.value(), widget); } } @@ -3715,60 +3569,11 @@ void tst_QVariant::saveQVariantFromDataStream(QDataStream::Version version) QCOMPARE(recunstructedVariant.userType(), constructedVariant.userType()); } -class MessageHandler { -public: - MessageHandler(const int typeId, QtMsgHandler msgHandler = handler) - : oldMsgHandler(qInstallMsgHandler(msgHandler)) - { - currentId = typeId; - } - - ~MessageHandler() - { - qInstallMsgHandler(oldMsgHandler); - } - - bool testPassed() const - { - return ok; - } -protected: - static void handler(QtMsgType, const char *txt) - { - QString msg = QString::fromLatin1(txt); - // Format itself is not important, but basic data as a type name should be included in the output - ok = msg.startsWith("QVariant("); - QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData()); - ok &= (currentId == QMetaType::UnknownType - ? msg.contains("Invalid") - : msg.contains(QMetaType::typeName(currentId))); - QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData()); - if (currentId == QMetaType::Char || currentId == QMetaType::QChar) { - // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length - return; - } - if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) { - QByteArray currentName = QMetaType::typeName(currentId); - currentName.chop(1); - ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0")); - } - ok &= msg.endsWith(") "); - QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData()); - - } - - QtMsgHandler oldMsgHandler; - static int currentId; - static bool ok; -}; -bool MessageHandler::ok; -int MessageHandler::currentId; - void tst_QVariant::debugStream_data() { QTest::addColumn("variant"); QTest::addColumn("typeId"); - for (int id = 0; id < QMetaType::User; ++id) { + for (int id = 0; id < QMetaType::LastGuiType + 1; ++id) { const char *tagName = QMetaType::typeName(id); if (!tagName) continue; @@ -3842,13 +3647,5 @@ void tst_QVariant::guiVariantAtExit() QVERIFY(true); } -void tst_QVariant::widgetsVariantAtExit() -{ - // crash test, it should not crash at QGuiApplication exit - static QVariant sizePolicy = QSizePolicy(); - Q_UNUSED(sizePolicy); - QVERIFY(true); -} - QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" diff --git a/tests/auto/other/qvariant_common/tst_qvariant_common.h b/tests/auto/other/qvariant_common/tst_qvariant_common.h new file mode 100644 index 0000000..63d236d --- /dev/null +++ b/tests/auto/other/qvariant_common/tst_qvariant_common.h @@ -0,0 +1,227 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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$ +** +****************************************************************************/ + +#ifndef TST_QVARIANT_COMMON +#define TST_QVARIANT_COMMON + +#include + +struct MessageHandlerInvalidType +{ + MessageHandlerInvalidType() + : oldMsgHandler(qInstallMsgHandler(handler)) + { + ok = false; + } + + ~MessageHandlerInvalidType() + { + qInstallMsgHandler(oldMsgHandler); + } + + QtMsgHandler oldMsgHandler; + + static void handler(QtMsgType type, const char *txt) + { + Q_UNUSED(type); + QString msg = QString::fromLatin1(txt); + // uint(-1) can be platform dependent so we check only beginning of the message. + ok = msg.startsWith("Trying to construct an instance of an invalid type, type id:"); + QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData()); + } + static bool ok; +}; +bool MessageHandlerInvalidType::ok; + + +class MessageHandler { +public: + MessageHandler(const int typeId, QtMsgHandler msgHandler = handler) + : oldMsgHandler(qInstallMsgHandler(msgHandler)) + { + currentId = typeId; + } + + ~MessageHandler() + { + qInstallMsgHandler(oldMsgHandler); + } + + bool testPassed() const + { + return ok; + } +protected: + static void handler(QtMsgType, const char *txt) + { + QString msg = QString::fromLatin1(txt); + // Format itself is not important, but basic data as a type name should be included in the output + ok = msg.startsWith("QVariant("); + QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData()); + ok &= (currentId == QMetaType::UnknownType + ? msg.contains("Invalid") + : msg.contains(QMetaType::typeName(currentId))); + QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData()); + if (currentId == QMetaType::Char || currentId == QMetaType::QChar) { + // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length + return; + } + if (QMetaType::typeFlags(currentId) & QMetaType::PointerToQObject) { + QByteArray currentName = QMetaType::typeName(currentId); + currentName.chop(1); + ok &= (msg.contains(", " + currentName) || msg.contains(", 0x0")); + } + ok &= msg.endsWith(") "); + QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData()); + + } + + QtMsgHandler oldMsgHandler; + static int currentId; + static bool ok; +}; +bool MessageHandler::ok; +int MessageHandler::currentId; + +#define TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS \ + QTest::addColumn("val"); \ + QTest::addColumn("BitArrayCast"); \ + QTest::addColumn("BitmapCast"); \ + QTest::addColumn("BoolCast"); \ + QTest::addColumn("BrushCast"); \ + QTest::addColumn("ByteArrayCast"); \ + QTest::addColumn("ColorCast"); \ + QTest::addColumn("CursorCast"); \ + QTest::addColumn("DateCast"); \ + QTest::addColumn("DateTimeCast"); \ + QTest::addColumn("DoubleCast"); \ + QTest::addColumn("FontCast"); \ + QTest::addColumn("ImageCast"); \ + QTest::addColumn("IntCast"); \ + QTest::addColumn("InvalidCast"); \ + QTest::addColumn("KeySequenceCast"); \ + QTest::addColumn("ListCast"); \ + QTest::addColumn("LongLongCast"); \ + QTest::addColumn("MapCast"); \ + QTest::addColumn("PaletteCast"); \ + QTest::addColumn("PenCast"); \ + QTest::addColumn("PixmapCast"); \ + QTest::addColumn("PointCast"); \ + QTest::addColumn("RectCast"); \ + QTest::addColumn("RegionCast"); \ + QTest::addColumn("SizeCast"); \ + QTest::addColumn("SizePolicyCast"); \ + QTest::addColumn("StringCast"); \ + QTest::addColumn("StringListCast"); \ + QTest::addColumn("TimeCast"); \ + QTest::addColumn("UIntCast"); \ + QTest::addColumn("ULongLongCast"); + +#define TST_QVARIANT_CANCONVERT_FETCH_DATA \ + QFETCH(QVariant, val); \ + QFETCH(bool, BitArrayCast); \ + QFETCH(bool, BitmapCast); \ + QFETCH(bool, BoolCast); \ + QFETCH(bool, BrushCast); \ + QFETCH(bool, ByteArrayCast); \ + QFETCH(bool, ColorCast); \ + QFETCH(bool, CursorCast); \ + QFETCH(bool, DateCast); \ + QFETCH(bool, DateTimeCast); \ + QFETCH(bool, DoubleCast); \ + QFETCH(bool, FontCast); \ + QFETCH(bool, ImageCast); \ + QFETCH(bool, IntCast); \ + QFETCH(bool, InvalidCast); \ + QFETCH(bool, KeySequenceCast); \ + QFETCH(bool, ListCast); \ + QFETCH(bool, LongLongCast); \ + QFETCH(bool, MapCast); \ + QFETCH(bool, PaletteCast); \ + QFETCH(bool, PenCast); \ + QFETCH(bool, PixmapCast); \ + QFETCH(bool, PointCast); \ + QFETCH(bool, RectCast); \ + QFETCH(bool, RegionCast); \ + QFETCH(bool, SizeCast); \ + QFETCH(bool, SizePolicyCast); \ + QFETCH(bool, StringCast); \ + QFETCH(bool, StringListCast); \ + QFETCH(bool, TimeCast); \ + QFETCH(bool, UIntCast); \ + QFETCH(bool, ULongLongCast); + +#define TST_QVARIANT_CANCONVERT_COMPARE_DATA \ + QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast); \ + QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast); \ + QCOMPARE(val.canConvert(QVariant::Bool), BoolCast); \ + QCOMPARE(val.canConvert(QVariant::Brush), BrushCast); \ + QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast); \ + QCOMPARE(val.canConvert(QVariant::Color), ColorCast); \ + QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast); \ + QCOMPARE(val.canConvert(QVariant::Date), DateCast); \ + QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast); \ + QCOMPARE(val.canConvert(QVariant::Double), DoubleCast); \ + QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast); \ + QCOMPARE(val.canConvert(QVariant::Font), FontCast); \ + QCOMPARE(val.canConvert(QVariant::Image), ImageCast); \ + QCOMPARE(val.canConvert(QVariant::Int), IntCast); \ + QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast); \ + QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast); \ + QCOMPARE(val.canConvert(QVariant::List), ListCast); \ + QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast); \ + QCOMPARE(val.canConvert(QVariant::Map), MapCast); \ + QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast); \ + QCOMPARE(val.canConvert(QVariant::Pen), PenCast); \ + QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast); \ + QCOMPARE(val.canConvert(QVariant::Point), PointCast); \ + QCOMPARE(val.canConvert(QVariant::Rect), RectCast); \ + QCOMPARE(val.canConvert(QVariant::Region), RegionCast); \ + QCOMPARE(val.canConvert(QVariant::Size), SizeCast); \ + QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast); \ + QCOMPARE(val.canConvert(QVariant::String), StringCast); \ + QCOMPARE(val.canConvert(QVariant::StringList), StringListCast); \ + QCOMPARE(val.canConvert(QVariant::Time), TimeCast); \ + QCOMPARE(val.canConvert(QVariant::UInt), UIntCast); \ + QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast); + + +#endif \ No newline at end of file diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro b/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro new file mode 100644 index 0000000..e3f2da9 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro @@ -0,0 +1,7 @@ +CONFIG += testcase +CONFIG += parallel_test +TARGET = tst_qwidgetsvariant +INCLUDEPATH += $$PWD/../../../other/qvariant_common +SOURCES += tst_qwidgetsvariant.cpp +QT += testlib widgets + diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.qrc b/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.qrc new file mode 100644 index 0000000..e6f7bda --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.qrc @@ -0,0 +1,6 @@ + + + stream/qt4.9/ + stream/qt5.0/ + + diff --git a/tests/auto/corelib/kernel/qvariant/stream/qt4.9/qsizepolicy.bin b/tests/auto/widgets/kernel/qwidgetsvariant/stream/qt4.9/qsizepolicy.bin similarity index 100% rename from tests/auto/corelib/kernel/qvariant/stream/qt4.9/qsizepolicy.bin rename to tests/auto/widgets/kernel/qwidgetsvariant/stream/qt4.9/qsizepolicy.bin diff --git a/tests/auto/corelib/kernel/qvariant/stream/qt5.0/qsizepolicy.bin b/tests/auto/widgets/kernel/qwidgetsvariant/stream/qt5.0/qsizepolicy.bin similarity index 100% rename from tests/auto/corelib/kernel/qvariant/stream/qt5.0/qsizepolicy.bin rename to tests/auto/widgets/kernel/qwidgetsvariant/stream/qt5.0/qsizepolicy.bin diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp b/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp new file mode 100644 index 0000000..ccb9492 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp @@ -0,0 +1,258 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** 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 + +#include + +#include "tst_qvariant_common.h" + + +class tst_QWidgetsVariant : public QObject +{ + Q_OBJECT + +private slots: + + void constructor_invalid_data(); + void constructor_invalid(); + + void canConvert_data(); + void canConvert(); + + void writeToReadFromDataStream_data(); + void writeToReadFromDataStream(); + + void qvariant_cast_QObject_data(); + void qvariant_cast_QObject(); + void qvariant_cast_QObject_derived(); + + void debugStream_data(); + void debugStream(); + + void widgetsVariantAtExit(); +}; + +void tst_QWidgetsVariant::constructor_invalid_data() +{ + QTest::addColumn("typeId"); + + QTest::newRow("LastGuiType + 1") << uint(QMetaType::LastGuiType + 1); + QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1)); + QTest::newRow("LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1); + QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1)); +} + +void tst_QWidgetsVariant::constructor_invalid() +{ + + QFETCH(uint, typeId); + { + MessageHandlerInvalidType msg; + QVariant variant(static_cast(typeId)); + QVERIFY(!variant.isValid()); + QVERIFY(variant.userType() == QMetaType::UnknownType); + QVERIFY(msg.ok); + } + { + MessageHandlerInvalidType msg; + QVariant variant(typeId, /* copy */ 0); + QVERIFY(!variant.isValid()); + QVERIFY(variant.userType() == QMetaType::UnknownType); + QVERIFY(msg.ok); + } +} + +void tst_QWidgetsVariant::canConvert_data() +{ + TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS + +#ifdef Y +#undef Y +#endif +#ifdef N +#undef N +#endif +#define Y true +#define N false + + QVariant var; + + // bita bitm bool brsh byta col curs date dt dbl font img int inv kseq list ll map pal pen pix pnt rect reg size sp str strl time uint ull + + + var = QVariant::fromValue(QSizePolicy()); + QTest::newRow("SizePolicy") + << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N; + +#undef N +#undef Y +} + +void tst_QWidgetsVariant::canConvert() +{ + TST_QVARIANT_CANCONVERT_FETCH_DATA + + TST_QVARIANT_CANCONVERT_COMPARE_DATA +} + + +void tst_QWidgetsVariant::writeToReadFromDataStream_data() +{ + QTest::addColumn("writeVariant"); + QTest::addColumn("isNull"); + + QTest::newRow( "sizepolicy_valid" ) << QVariant::fromValue( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false; +} + +void tst_QWidgetsVariant::writeToReadFromDataStream() +{ + QFETCH( QVariant, writeVariant ); + QFETCH( bool, isNull ); + QByteArray data; + + QDataStream writeStream( &data, QIODevice::WriteOnly ); + writeStream << writeVariant; + + QVariant readVariant; + QDataStream readStream( &data, QIODevice::ReadOnly ); + readStream >> readVariant; + QVERIFY( readVariant.isNull() == isNull ); +} + +class CustomQWidget : public QWidget { + Q_OBJECT +public: + CustomQWidget(QWidget *parent = 0) : QWidget(parent) {} +}; + +void tst_QWidgetsVariant::qvariant_cast_QObject_data() +{ + QTest::addColumn("data"); + QTest::addColumn("success"); + + QWidget *widget = new QWidget; + widget->setObjectName(QString::fromLatin1("Hello")); + QTest::newRow("from QWidget") << QVariant::fromValue(widget) << true; + + CustomQWidget *customWidget = new CustomQWidget; + customWidget->setObjectName(QString::fromLatin1("Hello")); + QTest::newRow("from Derived QWidget") << QVariant::fromValue(customWidget) << true; +} + +void tst_QWidgetsVariant::qvariant_cast_QObject() +{ + QFETCH(QVariant, data); + QFETCH(bool, success); + + QObject *o = qvariant_cast(data); + QCOMPARE(o != 0, success); + if (success) { + QCOMPARE(o->objectName(), QString::fromLatin1("Hello")); + QVERIFY(data.canConvert()); + QVERIFY(data.canConvert(QMetaType::QObjectStar)); + QVERIFY(data.canConvert(::qMetaTypeId())); + QVERIFY(data.value()); + QVERIFY(data.convert(QMetaType::QObjectStar)); + QCOMPARE(data.userType(), int(QMetaType::QObjectStar)); + + QVERIFY(data.canConvert()); + QVERIFY(data.canConvert(QMetaType::QWidgetStar)); + QVERIFY(data.canConvert(::qMetaTypeId())); + QVERIFY(data.value()); + QVERIFY(data.convert(QMetaType::QWidgetStar)); + QCOMPARE(data.userType(), int(QMetaType::QWidgetStar)); + } else { + QVERIFY(!data.canConvert()); + QVERIFY(!data.canConvert(QMetaType::QObjectStar)); + QVERIFY(!data.canConvert(::qMetaTypeId())); + QVERIFY(!data.value()); + QVERIFY(!data.convert(QMetaType::QObjectStar)); + QVERIFY(data.userType() != QMetaType::QObjectStar); + } + delete o; +} + +void tst_QWidgetsVariant::qvariant_cast_QObject_derived() +{ + CustomQWidget customWidget; + QWidget *widget = &customWidget; + QVariant data = QVariant::fromValue(widget); + QVERIFY(data.userType() == QMetaType::QWidgetStar); + + QCOMPARE(data.value(), widget); + QCOMPARE(data.value(), widget); + QCOMPARE(data.value(), widget); +} + +void tst_QWidgetsVariant::debugStream_data() +{ + QTest::addColumn("variant"); + QTest::addColumn("typeId"); + for (int id = QMetaType::LastGuiType + 1; id < QMetaType::User; ++id) { + const char *tagName = QMetaType::typeName(id); + if (!tagName) + continue; + QTest::newRow(tagName) << QVariant(static_cast(id)) << id; + } +} + +void tst_QWidgetsVariant::debugStream() +{ + QFETCH(QVariant, variant); + QFETCH(int, typeId); + + MessageHandler msgHandler(typeId); + qDebug() << variant; + QVERIFY(msgHandler.testPassed()); +} + +void tst_QWidgetsVariant::widgetsVariantAtExit() +{ + // crash test, it should not crash at QApplication exit + static QVariant sizePolicy = QSizePolicy(); + Q_UNUSED(sizePolicy); + QVERIFY(true); +} + +QTEST_MAIN(tst_QWidgetsVariant) +#include "tst_qwidgetsvariant.moc" -- 2.7.4