diff options
Diffstat (limited to 'tests/auto')
277 files changed, 27959 insertions, 1042 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 120e55d..6bc5ad8 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -78,6 +78,7 @@ SUBDIRS += _networkselftest \ qaction \ qactiongroup \ qalgorithms \ + qanimationgroup \ qapplication \ qatomicint \ qatomicpointer \ @@ -142,9 +143,17 @@ SUBDIRS += _networkselftest \ qglobal \ qgraphicsitem \ qgraphicsitemanimation \ + qgraphicslayout \ + qgraphicslayoutitem \ + qgraphicslinearlayout \ + qgraphicsobject \ + qgraphicspixmapitem \ + qgraphicspolygonitem \ + qgraphicsproxywidget \ qgraphicsscene \ qgraphicsview \ - qgridlayout \ + qgraphicswidget \ + qgridlayout \ qgroupbox \ qguivariant \ qhash \ @@ -213,10 +222,12 @@ SUBDIRS += _networkselftest \ qnumeric \ qobject \ qobjectrace \ + qcontiguouscache \ qpaintengine \ qpainter \ qpainterpath \ qpalette \ + qparallelanimationgroup \ qpathclipper \ qpen \ qpicture \ @@ -232,6 +243,7 @@ SUBDIRS += _networkselftest \ qprocess \ qprogressbar \ qprogressdialog \ + qpropertyanimation \ qpushbutton \ qscopedpointer \ qqueue \ @@ -243,6 +255,7 @@ SUBDIRS += _networkselftest \ qregexpvalidator \ qregion \ qresourceengine \ + qringbuffer \ qscriptable \ qscriptclass \ qscriptcontext \ @@ -259,6 +272,7 @@ SUBDIRS += _networkselftest \ qscrollarea \ qsemaphore \ qsharedpointer \ + qsequentialanimationgroup \ qset \ qsettings \ qshortcut \ @@ -292,6 +306,8 @@ SUBDIRS += _networkselftest \ qstackedwidget \ qstandarditem \ qstandarditemmodel \ + qstate \ + qstatemachine \ qstatusbar \ qstl \ qstring \ @@ -434,5 +450,9 @@ unix:!embedded:contains(QT_CONFIG, dbus):SUBDIRS += \ contains(QT_CONFIG, webkit): SUBDIRS += \ qwebframe \ - qwebpage + qwebpage \ + qwebhistoryinterface \ + qwebelement \ + qwebhistory +SUBDIRS += math3d diff --git a/tests/auto/bic/bic.pro b/tests/auto/bic/bic.pro index a168d77..82711c9 100644 --- a/tests/auto/bic/bic.pro +++ b/tests/auto/bic/bic.pro @@ -2,3 +2,9 @@ load(qttest_p4) SOURCES += tst_bic.cpp qbic.cpp QT = core +wince*:{ + DEFINES += SRCDIR=\\\"\\\" +} else { + DEFINES += SRCDIR=\\\"$$PWD/\\\" +} + diff --git a/tests/auto/bic/tst_bic.cpp b/tests/auto/bic/tst_bic.cpp index b600d9a..e7a0c04 100644 --- a/tests/auto/bic/tst_bic.cpp +++ b/tests/auto/bic/tst_bic.cpp @@ -97,8 +97,6 @@ tst_Bic::tst_Bic() bic.addBlacklistedClass(QLatin1String("QTypeInfo<*>")); bic.addBlacklistedClass(QLatin1String("QMetaTypeId<*>")); bic.addBlacklistedClass(QLatin1String("QVector<QGradientStop>*")); - bic.addBlacklistedClass(QLatin1String("QMap<*>::iterator")); - bic.addBlacklistedClass(QLatin1String("QMap<*>::Node")); /* this guy is never instantiated, just for compile-time checking */ bic.addBlacklistedClass(QLatin1String("QMap<*>::PayloadNode")); @@ -160,52 +158,55 @@ void tst_Bic::sizesAndVTables_data() QSKIP("Test not implemented for this compiler/platform", SkipAll); #else - int major = QT_VERSION >> 16; - if (major != 4) { - QFAIL("This test is for Qt 4"); - } - int minor = (QT_VERSION & 0x00FF00) >> 8; - int patch = (QT_VERSION & 0x0000FF); - - // Test every Qt 4 version up to this minor version. - typedef QPair<QString,bool> VersionPair; - QList<VersionPair> versions; - for (int i = 0; i <= minor; ++i) { - bool isPatchRelease = (i == minor && patch); - versions << qMakePair(QString("%1.%2").arg(major).arg(i), isPatchRelease); - } + QString archFileName400; + QString archFileName410; + QString archFileName420; + QString archFileName430; - QString arch; #if defined Q_OS_LINUX && defined Q_WS_X11 # if defined(__powerpc__) && !defined(__powerpc64__) - arch = "linux-gcc-ppc32"; + archFileName400 = SRCDIR "data/%1.4.0.0.linux-gcc-ppc32.txt"; + archFileName410 = SRCDIR "data/%1.4.1.0.linux-gcc-ppc32.txt"; + archFileName420 = SRCDIR "data/%1.4.2.0.linux-gcc-ppc32.txt"; # elif defined(__amd64__) - arch = "linux-gcc-amd64"; + archFileName400 = SRCDIR "data/%1.4.0.0.linux-gcc-amd64.txt"; # elif defined(__i386__) - arch = "linux-gcc-ia32"; + archFileName400 = SRCDIR "data/%1.4.0.0.linux-gcc-ia32.txt"; + archFileName410 = SRCDIR "data/%1.4.1.0.linux-gcc-ia32.txt"; + archFileName420 = SRCDIR "data/%1.4.2.0.linux-gcc-ia32.txt"; + archFileName430 = SRCDIR "data/%1.4.3.0.linux-gcc-ia32.txt"; # endif #elif defined Q_OS_AIX - if (sizeof(void*) == 4) { - arch = "aix-gcc-power32"; - } + if (sizeof(void*) == 4) + archFileName400 = SRCDIR "data/%1.4.0.0.aix-gcc-power32.txt"; #elif defined Q_OS_MAC && defined(__powerpc__) - arch = "macx-gcc-ppc32"; + archFileName400 = SRCDIR "data/%1.4.0.0.macx-gcc-ppc32.txt"; + archFileName410 = SRCDIR "data/%1.4.1.0.macx-gcc-ppc32.txt"; + archFileName420 = SRCDIR "data/%1.4.2.0.macx-gcc-ppc32.txt"; #elif defined Q_OS_MAC && defined(__i386__) - arch = "macx-gcc-ia32"; + archFileName410 = SRCDIR "data/%1.4.1.0.macx-gcc-ia32.txt"; + archFileName420 = SRCDIR "data/%1.4.2.0.macx-gcc-ia32.txt"; #elif defined Q_OS_WIN && defined Q_CC_GNU - arch = "win32-gcc-ia32"; + archFileName410 = SRCDIR "data/%1.4.1.0.win32-gcc-ia32.txt"; + archFileName420 = SRCDIR "data/%1.4.2.0.win32-gcc-ia32.txt"; #endif - if (arch.isEmpty()) - QSKIP("No reference files found for this arch", SkipAll); + if (archFileName400.isEmpty() && archFileName410.isEmpty() + && archFileName420.isEmpty()) + QSKIP("No reference files found for this platform", SkipAll); + + bool isPatchRelease400 = false; + bool isPatchRelease410 = false; + bool isPatchRelease420 = false; + bool isPatchRelease430 = false; QTest::addColumn<QString>("oldLib"); QTest::addColumn<bool>("isPatchRelease"); - foreach (VersionPair const& version, versions) { - QString archFileName = QString("data/%3.%1.0.%2.txt").arg(version.first).arg(arch); - QTest::newRow(qPrintable(version.first)) << archFileName << version.second; - } + QTest::newRow("4.0") << archFileName400 << isPatchRelease400; + QTest::newRow("4.1") << archFileName410 << isPatchRelease410; + QTest::newRow("4.2") << archFileName420 << isPatchRelease420; + QTest::newRow("4.3") << archFileName430 << isPatchRelease430; #endif } @@ -292,6 +293,7 @@ void tst_Bic::sizesAndVTables() bool isFailed = false; + qDebug() << oldLib.arg(libName); if (oldLib.isEmpty() || !QFile::exists(oldLib.arg(libName))) QSKIP("No platform spec found for this platform/version.", SkipSingle); diff --git a/tests/auto/gestures/customgesturerecognizer.cpp b/tests/auto/gestures/customgesturerecognizer.cpp new file mode 100644 index 0000000..12d07b1 --- /dev/null +++ b/tests/auto/gestures/customgesturerecognizer.cpp @@ -0,0 +1,167 @@ +#include "customgesturerecognizer.h" +#include "qgesture.h" + +const char* SingleshotGestureRecognizer::Name = "SingleshotGesture"; +const char* PinchGestureRecognizer::Name = "PinchGesture"; +const char* SecondFingerGestureRecognizer::Name = "SecondFingerGesture"; +const char* PanGestureRecognizer::Name = "PanGesture"; + +SingleshotGestureRecognizer::SingleshotGestureRecognizer(QObject *parent) + : QGestureRecognizer(QString(SingleshotGestureRecognizer::Name), parent) +{ + gesture = new SingleshotGesture(this, SingleshotGestureRecognizer::Name); +} + +QGestureRecognizer::Result SingleshotGestureRecognizer::filterEvent(const QEvent *event) +{ + if (event->type() == SingleshotEvent::Type) { + gesture->setHotSpot(static_cast<const SingleshotEvent*>(event)->point); + return QGestureRecognizer::GestureFinished; + } + return QGestureRecognizer::NotGesture; +} + +void SingleshotGestureRecognizer::reset() +{ + gesture->setHotSpot(QPoint()); + gesture->offset = QPoint(); +} + +PinchGestureRecognizer::PinchGestureRecognizer(QObject *parent) + : QGestureRecognizer(PinchGestureRecognizer::Name, parent) +{ + gesture = new PinchGesture(this, PinchGestureRecognizer::Name); +} + +QGestureRecognizer::Result PinchGestureRecognizer::filterEvent(const QEvent *event) +{ + if (event->type() != TouchEvent::Type) + return QGestureRecognizer::Ignore; + + const TouchEvent *e = static_cast<const TouchEvent*>(event); + if (e->points[0].state == TouchPoint::Begin) + gesture->startPoints[0] = e->points[0]; + if (e->points[1].state == TouchPoint::Begin) + gesture->startPoints[1] = e->points[1]; + gesture->lastPoints[0] = gesture->points[0]; + gesture->lastPoints[1] = gesture->points[1]; + gesture->points[0] = e->points[0]; + gesture->points[1] = e->points[1]; + if (((e->points[0].state == TouchPoint::Begin || e->points[0].state == TouchPoint::Update))) { + if (e->points[1].state == TouchPoint::End || e->points[1].state == TouchPoint::None) + return MaybeGesture; + return GestureStarted; + } else if (((e->points[1].state == TouchPoint::Begin || e->points[1].state == TouchPoint::Update))) { + if (e->points[0].state == TouchPoint::End || e->points[0].state == TouchPoint::None) + return MaybeGesture; + return GestureStarted; + } else if ((e->points[0].state == TouchPoint::End && e->points[1].state == TouchPoint::End) || + (e->points[0].state == TouchPoint::End && e->points[1].state == TouchPoint::None) || + (e->points[0].state == TouchPoint::None && e->points[1].state == TouchPoint::End)) { + return QGestureRecognizer::NotGesture; + } + return QGestureRecognizer::NotGesture; +} + +void PinchGestureRecognizer::reset() +{ + gesture->startPoints[0] = TouchPoint(); + gesture->startPoints[1] = TouchPoint(); + gesture->lastPoints[0] = TouchPoint(); + gesture->lastPoints[1] = TouchPoint(); + gesture->points[0] = TouchPoint(); + gesture->points[1] = TouchPoint(); + gesture->offset = QPoint(); +} + +SecondFingerGestureRecognizer::SecondFingerGestureRecognizer(QObject *parent) + : QGestureRecognizer(SecondFingerGestureRecognizer::Name, parent) +{ + gesture = new SecondFingerGesture(this, SecondFingerGestureRecognizer::Name); +} + +QGestureRecognizer::Result SecondFingerGestureRecognizer::filterEvent(const QEvent *event) +{ + if (event->type() != TouchEvent::Type) + return QGestureRecognizer::Ignore; + + const TouchEvent *e = static_cast<const TouchEvent*>(event); + if (e->points[1].state != TouchPoint::None) { + if (e->points[1].state == TouchPoint::Begin) + gesture->startPoint = e->points[1]; + gesture->lastPoint = gesture->point; + gesture->point = e->points[1]; + if (e->points[1].state == TouchPoint::End) + return QGestureRecognizer::GestureFinished; + else if (e->points[1].state != TouchPoint::None) + return QGestureRecognizer::GestureStarted; + } + return QGestureRecognizer::NotGesture; +} + +void SecondFingerGestureRecognizer::reset() +{ + gesture->startPoint = TouchPoint(); + gesture->lastPoint = TouchPoint(); + gesture->point = TouchPoint(); + gesture->offset = QPoint(); +} + +PanGestureRecognizer::PanGestureRecognizer(QObject *parent) + : QGestureRecognizer(PanGestureRecognizer::Name, parent) +{ + gesture = new PanGesture(this, PanGestureRecognizer::Name); +} + +QGestureRecognizer::Result PanGestureRecognizer::filterEvent(const QEvent *event) +{ + if (event->type() != QEvent::TouchBegin && + event->type() != QEvent::TouchUpdate && + event->type() != QEvent::TouchEnd) + return QGestureRecognizer::Ignore; + + const QTouchEvent *e = static_cast<const QTouchEvent*>(event); + const QList<QTouchEvent::TouchPoint> &points = e->touchPoints(); + + if (points.size() >= 1) { + gesture->lastPoints[0] = gesture->points[0]; + gesture->points[0].id = points.at(0).id(); + gesture->points[0].pt = points.at(0).startPos().toPoint(); + gesture->points[0].state = (TouchPoint::State)points.at(0).state(); + if (points.at(0).state() == Qt::TouchPointPressed) { + gesture->startPoints[0] = gesture->points[0]; + gesture->lastPoints[0] = gesture->points[0]; + } + } + if (points.size() >= 2) { + gesture->lastPoints[1] = gesture->points[1]; + gesture->points[1].id = points.at(1).id(); + gesture->points[1].pt = points.at(1).startPos().toPoint(); + gesture->points[1].state = (TouchPoint::State)points.at(1).state(); + if (points.at(1).state() == Qt::TouchPointPressed) { + gesture->startPoints[1] = gesture->points[1]; + gesture->lastPoints[1] = gesture->points[1]; + } + } + + if (points.size() == 2) + return QGestureRecognizer::GestureStarted; + if (points.size() > 2) + return QGestureRecognizer::MaybeGesture; + if (points.at(0).state() == Qt::TouchPointPressed) + return QGestureRecognizer::MaybeGesture; + if (points.at(0).state() == Qt::TouchPointReleased) + return QGestureRecognizer::GestureFinished; + return QGestureRecognizer::GestureStarted; +} + +void PanGestureRecognizer::reset() +{ + gesture->startPoints[0] = TouchPoint(); + gesture->startPoints[1] = TouchPoint(); + gesture->lastPoints[0] = TouchPoint(); + gesture->lastPoints[1] = TouchPoint(); + gesture->points[0] = TouchPoint(); + gesture->points[1] = TouchPoint(); + gesture->offset = QPoint(); +} diff --git a/tests/auto/gestures/customgesturerecognizer.h b/tests/auto/gestures/customgesturerecognizer.h new file mode 100644 index 0000000..519aba8 --- /dev/null +++ b/tests/auto/gestures/customgesturerecognizer.h @@ -0,0 +1,187 @@ +#ifndef CUSTOMGESTURERECOGNIZER_H +#define CUSTOMGESTURERECOGNIZER_H + +#include "qgesturerecognizer.h" +#include "qgesture.h" +#include "qevent.h" + +class SingleshotEvent : public QEvent +{ +public: + static const int Type = QEvent::User + 1; + + QPoint point; + + explicit SingleshotEvent(int x = 0, int y = 0) + : QEvent(QEvent::Type(Type)), point(x, y) { } +}; + +class SingleshotGesture : public QGesture +{ + Q_OBJECT +public: + SingleshotGesture(QObject *parent, const QString &type) + : QGesture(parent, type) { } + + QPoint offset; + +protected: + void translate(const QPoint &pt) + { + offset += pt; + } +}; + +class SingleshotGestureRecognizer : public QGestureRecognizer +{ + Q_OBJECT +public: + static const char *Name; + + SingleshotGestureRecognizer(QObject *parent = 0); + + QGestureRecognizer::Result filterEvent(const QEvent *event); + QGesture* getGesture() { return gesture; } + void reset(); + +private: + SingleshotGesture *gesture; +}; + +struct TouchPoint { + enum State + { + None = 0, + Begin = Qt::TouchPointPressed, + Update = Qt::TouchPointMoved, + End = Qt::TouchPointReleased + }; + int id; + QPoint pt; + State state; + TouchPoint() : id(0), state(None) { } + TouchPoint(int id_, int x_, int y_, State state_) : id(id_), pt(x_, y_), state(state_) { } +}; + +class TouchEvent : public QEvent +{ +public: + static const int Type = QEvent::User + 2; + + TouchEvent() + : QEvent(QEvent::Type(Type)) + { + } + + TouchPoint points[2]; +}; + +class PinchGesture : public QGesture +{ + Q_OBJECT +public: + PinchGesture(QObject *parent, const QString &type) + : QGesture(parent, type) { } + + TouchPoint startPoints[2]; + TouchPoint lastPoints[2]; + TouchPoint points[2]; + + QPoint offset; + +protected: + void translate(const QPoint &pt) + { + offset += pt; + } +}; + +class PinchGestureRecognizer : public QGestureRecognizer +{ + Q_OBJECT +public: + static const char *Name; + + PinchGestureRecognizer(QObject *parent = 0); + + QGestureRecognizer::Result filterEvent(const QEvent *event); + QGesture* getGesture() { return gesture; } + void reset(); + +private: + PinchGesture *gesture; +}; + +class SecondFingerGesture : public QGesture +{ + Q_OBJECT +public: + SecondFingerGesture(QObject *parent, const QString &type) + : QGesture(parent, type) { } + + TouchPoint startPoint; + TouchPoint lastPoint; + TouchPoint point; + + QPoint offset; + +protected: + void translate(const QPoint &pt) + { + offset += pt; + } +}; + +class SecondFingerGestureRecognizer : public QGestureRecognizer +{ + Q_OBJECT +public: + static const char *Name; + + SecondFingerGestureRecognizer(QObject *parent = 0); + + QGestureRecognizer::Result filterEvent(const QEvent *event); + QGesture* getGesture() { return gesture; } + void reset(); + +private: + SecondFingerGesture *gesture; +}; + +class PanGesture : public QGesture +{ + Q_OBJECT +public: + PanGesture(QObject *parent, const QString &type) + : QGesture(parent, type) { } + + TouchPoint startPoints[2]; + TouchPoint lastPoints[2]; + TouchPoint points[2]; + + QPoint offset; + +protected: + void translate(const QPoint &pt) + { + offset += pt; + } +}; + +class PanGestureRecognizer : public QGestureRecognizer +{ + Q_OBJECT +public: + static const char *Name; + + PanGestureRecognizer(QObject *parent = 0); + + QGestureRecognizer::Result filterEvent(const QEvent *event); + QGesture* getGesture() { return gesture; } + void reset(); + +private: + PanGesture *gesture; +}; + +#endif diff --git a/tests/auto/gestures/gestures.pro b/tests/auto/gestures/gestures.pro new file mode 100644 index 0000000..ac99b19 --- /dev/null +++ b/tests/auto/gestures/gestures.pro @@ -0,0 +1,3 @@ +load(qttest_p4) +SOURCES += tst_gestures.cpp customgesturerecognizer.cpp +HEADERS += customgesturerecognizer.h diff --git a/tests/auto/gestures/tst_gestures.cpp b/tests/auto/gestures/tst_gestures.cpp new file mode 100644 index 0000000..6d3bc0a --- /dev/null +++ b/tests/auto/gestures/tst_gestures.cpp @@ -0,0 +1,949 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtGui> + +#include <QtTest/QtTest> + +#include "customgesturerecognizer.h" + +//TESTED_CLASS= +//TESTED_FILES= + +// color generator for syntax highlighting from QQ-26 by Helder Correia +QVector<QColor> highlight(const QColor &bg, const + QColor &fg, int noColors) +{ + QVector<QColor> colors; + const int HUE_BASE = (bg.hue() == -1) ? 90 : bg.hue(); + int h, s, v; + for (int i = 0; i < noColors; i++) { + h = int(HUE_BASE + (360.0 / noColors * i)) % 360; + s = 240; + v = int(qMax(bg.value(), fg.value()) * 0.85); + + const int M = 35; + if ((h < bg.hue() + M && h > bg.hue() - M) + || (h < fg.hue() + M && h > fg.hue() - M)) + { + h = ((bg.hue() + fg.hue()) / (i+1)) % 360; + s = ((bg.saturation() + fg.saturation() + 2*i) + / 2) % 256; + v = ((bg.value() + fg.value() + 2*i) / 2) + % 256; + } + colors.append(QColor::fromHsv(h, s, v)); + } + return colors; +} + + +// a hack to mark an event as spontaneous. +class QETWidget +{ +public: + static void setSpont(QEvent *event, bool spont) { event->spont = spont; } +}; + +struct GestureState +{ + int seenGestureEvent; + struct LastGestureEvent + { + struct SingleshotGesture + { + bool delivered; + QPoint offset; + } singleshot; + struct PinchGesture + { + bool delivered; + TouchPoint startPoints[2]; + TouchPoint lastPoints[2]; + TouchPoint points[2]; + QPoint offset; + } pinch; + struct SecondFingerGesture + { + bool delivered; + TouchPoint startPoint; + TouchPoint lastPoint; + TouchPoint point; + QPoint offset; + } secondfinger; + struct PanGesture + { + bool delivered; + TouchPoint startPoints[2]; + TouchPoint lastPoints[2]; + TouchPoint points[2]; + QPoint offset; + } pan; + QSet<QString> cancelled; + } last; + + GestureState() { reset(); } + void reset() + { + seenGestureEvent = 0; + last.singleshot.delivered = false; + last.singleshot.offset = QPoint(); + last.pinch.delivered = false; + last.pinch.startPoints[0] = TouchPoint(); + last.pinch.startPoints[1] = TouchPoint(); + last.pinch.lastPoints[0] = TouchPoint(); + last.pinch.lastPoints[1] = TouchPoint(); + last.pinch.points[0] = TouchPoint(); + last.pinch.points[1] = TouchPoint(); + last.pinch.offset = QPoint(); + last.secondfinger.delivered = false; + last.secondfinger.startPoint = TouchPoint(); + last.secondfinger.lastPoint = TouchPoint(); + last.secondfinger.point = TouchPoint(); + last.secondfinger.offset = QPoint(); + last.cancelled.clear(); + } +}; + +struct TouchState +{ + int seenTouchBeginEvent; + int seenTouchUpdateEvent; + int seenTouchEndEvent; + + TouchState() { reset(); } + void reset() + { + seenTouchBeginEvent = seenTouchUpdateEvent = seenTouchEndEvent = 0; + } +}; + +class GestureWidget : public QWidget +{ + Q_OBJECT + static QVector<QColor> colors; + static int numberOfWidgets; + +public: + enum Type { DoNotGrabGestures, GrabAllGestures, GrabSingleshot, + GrabPinch, GrabSecondFinger, GrabPan }; + + static const int LeftMargin = 10; + static const int TopMargin = 20; + + GestureWidget(Type type = GrabAllGestures) + { + if (colors.isEmpty()) { + colors = highlight(palette().color(QPalette::Window), palette().color(QPalette::Text), 5); + } + QPalette p = palette(); + p.setColor(QPalette::Window, colors[numberOfWidgets % colors.size()]); + setPalette(p); + setAutoFillBackground(true); + ++numberOfWidgets; + + QVBoxLayout *l = new QVBoxLayout(this); + l->setSpacing(0); + l->setContentsMargins(LeftMargin, TopMargin, LeftMargin, TopMargin); + + singleshotGestureId = -1; + pinchGestureId = -1; + secondFingerGestureId = -1; + panGestureId = -1; + if (type == GrabAllGestures || type == GrabSingleshot) { + singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name); + } + if (type == GrabAllGestures || type == GrabPinch) { + pinchGestureId = grabGesture(PinchGestureRecognizer::Name); + } + if (type == GrabAllGestures || type == GrabSecondFinger) { + secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name); + } + if (type == GrabAllGestures || type == GrabPan) { + panGestureId = grabGesture(PanGestureRecognizer::Name); + } + reset(); + } + ~GestureWidget() + { + --numberOfWidgets; + ungrabGestures(); + } + + void grabSingleshotGesture() + { + singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name); + } + void grabPinchGesture() + { + pinchGestureId = grabGesture(PinchGestureRecognizer::Name); + } + void grabSecondFingerGesture() + { + secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name); + } + void grabPanGesture() + { + panGestureId = grabGesture(PanGestureRecognizer::Name); + } + void ungrabGestures() + { + releaseGesture(singleshotGestureId); + singleshotGestureId = -1; + releaseGesture(pinchGestureId); + pinchGestureId = -1; + releaseGesture(secondFingerGestureId); + secondFingerGestureId = -1; + releaseGesture(panGestureId); + panGestureId = -1; + } + + int singleshotGestureId; + int pinchGestureId; + int secondFingerGestureId; + int panGestureId; + + bool shouldAcceptSingleshotGesture; + bool shouldAcceptPinchGesture; + bool shouldAcceptSecondFingerGesture; + bool shouldAcceptPanGesture; + + GestureState gesture; + TouchState touch; + + void reset() + { + shouldAcceptSingleshotGesture = true; + shouldAcceptPinchGesture = true; + shouldAcceptSecondFingerGesture = true; + shouldAcceptPanGesture = true; + gesture.reset(); + touch.reset(); + } +protected: + bool event(QEvent *event) + { + if (event->type() == QEvent::TouchBegin) { + event->accept(); + ++touch.seenTouchBeginEvent; + return true; + } else if (event->type() == QEvent::TouchUpdate) { + ++touch.seenTouchUpdateEvent; + } else if (event->type() == QEvent::TouchEnd) { + ++touch.seenTouchEndEvent; + } else if (event->type() == QEvent::Gesture) { + QGestureEvent *e = static_cast<QGestureEvent*>(event); + ++gesture.seenGestureEvent; + if (SingleshotGesture *g = (SingleshotGesture*)e->gesture(SingleshotGestureRecognizer::Name)) { + gesture.last.singleshot.delivered = true; + gesture.last.singleshot.offset = g->offset; + if (shouldAcceptSingleshotGesture) + g->accept(); + } + if (PinchGesture *g = (PinchGesture*)e->gesture(PinchGestureRecognizer::Name)) { + gesture.last.pinch.delivered = true; + gesture.last.pinch.startPoints[0] = g->startPoints[0]; + gesture.last.pinch.startPoints[1] = g->startPoints[1]; + gesture.last.pinch.lastPoints[0] = g->lastPoints[0]; + gesture.last.pinch.lastPoints[1] = g->lastPoints[1]; + gesture.last.pinch.points[0] = g->points[0]; + gesture.last.pinch.points[1] = g->points[1]; + gesture.last.pinch.offset = g->offset; + if (shouldAcceptPinchGesture) + g->accept(); + } + if (SecondFingerGesture *g = (SecondFingerGesture*)e->gesture(SecondFingerGestureRecognizer::Name)) { + gesture.last.secondfinger.delivered = true; + gesture.last.secondfinger.startPoint = g->startPoint; + gesture.last.secondfinger.lastPoint = g->lastPoint; + gesture.last.secondfinger.point = g->point; + gesture.last.secondfinger.offset = g->offset; + if (shouldAcceptSecondFingerGesture) + g->accept(); + } + if (PanGesture *g = (PanGesture*)e->gesture(PanGestureRecognizer::Name)) { + gesture.last.pan.delivered = true; + gesture.last.pan.startPoints[0] = g->startPoints[0]; + gesture.last.pan.startPoints[1] = g->startPoints[1]; + gesture.last.pan.lastPoints[0] = g->lastPoints[0]; + gesture.last.pan.lastPoints[1] = g->lastPoints[1]; + gesture.last.pan.points[0] = g->points[0]; + gesture.last.pan.points[1] = g->points[1]; + gesture.last.pan.offset = g->offset; + if (shouldAcceptPanGesture) + g->accept(); + } + gesture.last.cancelled = e->cancelledGestures(); + return true; + } + return QWidget::event(event); + } +}; +QVector<QColor> GestureWidget::colors; +int GestureWidget::numberOfWidgets = 0; + +class GraphicsScene : public QGraphicsScene +{ +public: + GraphicsScene() + { + reset(); + } + bool shouldAcceptSingleshotGesture; + bool shouldAcceptPinchGesture; + bool shouldAcceptSecondFingerGesture; + GestureState gesture; + + void reset() + { + shouldAcceptSingleshotGesture = false; + shouldAcceptPinchGesture = false; + shouldAcceptSecondFingerGesture = false; + gesture.reset(); + } +protected: + bool event(QEvent *event) + { + if (event->type() == QEvent::GraphicsSceneGesture) { + QGraphicsSceneGestureEvent *e = static_cast<QGraphicsSceneGestureEvent*>(event); + ++gesture.seenGestureEvent; + QGraphicsScene::event(event); + if (SingleshotGesture *g = (SingleshotGesture*)e->gesture(SingleshotGestureRecognizer::Name)) { + gesture.last.singleshot.delivered = true; + gesture.last.singleshot.offset = g->offset; + if (shouldAcceptSingleshotGesture) + g->accept(); + } + if (PinchGesture *g = (PinchGesture*)e->gesture(PinchGestureRecognizer::Name)) { + gesture.last.pinch.delivered = true; + gesture.last.pinch.startPoints[0] = g->startPoints[0]; + gesture.last.pinch.startPoints[1] = g->startPoints[1]; + gesture.last.pinch.lastPoints[0] = g->lastPoints[0]; + gesture.last.pinch.lastPoints[1] = g->lastPoints[1]; + gesture.last.pinch.points[0] = g->points[0]; + gesture.last.pinch.points[1] = g->points[1]; + gesture.last.pinch.offset = g->offset; + if (shouldAcceptPinchGesture) + g->accept(); + } + if (SecondFingerGesture *g = (SecondFingerGesture*)e->gesture(SecondFingerGestureRecognizer::Name)) { + gesture.last.secondfinger.delivered = true; + gesture.last.secondfinger.startPoint = g->startPoint; + gesture.last.secondfinger.lastPoint = g->lastPoint; + gesture.last.secondfinger.point = g->point; + gesture.last.secondfinger.offset = g->offset; + if (shouldAcceptSecondFingerGesture) + g->accept(); + } + gesture.last.cancelled = e->cancelledGestures(); + return true; + } + return QGraphicsScene::event(event); + } +}; + +class GraphicsItem : public QGraphicsItem +{ +public: + GraphicsItem(int w = 100, int h = 100) + : width(w), height(h) + { + reset(); + } + + QRectF boundingRect() const + { + return QRectF(0, 0, width, height); + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) + { + painter->setBrush(Qt::green); + painter->drawRect(0, 0, width, height); + } + + void grabSingleshotGesture() + { + singleshotGestureId = grabGesture(SingleshotGestureRecognizer::Name); + } + void grabPinchGesture() + { + pinchGestureId = grabGesture(PinchGestureRecognizer::Name); + } + void grabSecondFingerGesture() + { + secondFingerGestureId = grabGesture(SecondFingerGestureRecognizer::Name); + } + void ungrabGestures() + { + releaseGesture(singleshotGestureId); + singleshotGestureId = -1; + releaseGesture(pinchGestureId); + pinchGestureId = -1; + releaseGesture(secondFingerGestureId); + secondFingerGestureId = -1; + } + + int width; + int height; + + int singleshotGestureId; + int pinchGestureId; + int secondFingerGestureId; + + bool shouldAcceptSingleshotGesture; + bool shouldAcceptPinchGesture; + bool shouldAcceptSecondFingerGesture; + GestureState gesture; + + TouchState touch; + + void reset() + { + shouldAcceptSingleshotGesture = true; + shouldAcceptPinchGesture = true; + shouldAcceptSecondFingerGesture = true; + gesture.reset(); + } +protected: + bool sceneEvent(QEvent *event) + { + if (event->type() == QEvent::TouchBegin) { + event->accept(); + ++touch.seenTouchBeginEvent; + return true; + } else if (event->type() == QEvent::TouchUpdate) { + ++touch.seenTouchUpdateEvent; + } else if (event->type() == QEvent::TouchEnd) { + ++touch.seenTouchEndEvent; + } else if (event->type() == QEvent::GraphicsSceneGesture) { + QGraphicsSceneGestureEvent *e = static_cast<QGraphicsSceneGestureEvent*>(event); + ++gesture.seenGestureEvent; + if (SingleshotGesture *g = (SingleshotGesture*)e->gesture(SingleshotGestureRecognizer::Name)) { + gesture.last.singleshot.delivered = true; + gesture.last.singleshot.offset = g->offset; + if (shouldAcceptSingleshotGesture) + g->accept(); + } + if (PinchGesture *g = (PinchGesture*)e->gesture(PinchGestureRecognizer::Name)) { + gesture.last.pinch.delivered = true; + gesture.last.pinch.startPoints[0] = g->startPoints[0]; + gesture.last.pinch.startPoints[1] = g->startPoints[1]; + gesture.last.pinch.lastPoints[0] = g->lastPoints[0]; + gesture.last.pinch.lastPoints[1] = g->lastPoints[1]; + gesture.last.pinch.points[0] = g->points[0]; + gesture.last.pinch.points[1] = g->points[1]; + gesture.last.pinch.offset = g->offset; + if (shouldAcceptPinchGesture) + g->accept(); + } + if (SecondFingerGesture *g = (SecondFingerGesture*)e->gesture(SecondFingerGestureRecognizer::Name)) { + gesture.last.secondfinger.delivered = true; + gesture.last.secondfinger.startPoint = g->startPoint; + gesture.last.secondfinger.lastPoint = g->lastPoint; + gesture.last.secondfinger.point = g->point; + gesture.last.secondfinger.offset = g->offset; + if (shouldAcceptSecondFingerGesture) + g->accept(); + } + gesture.last.cancelled = e->cancelledGestures(); + return true; + } + return QGraphicsItem::sceneEvent(event); + } +}; + +class tst_Gestures : public QObject +{ + Q_OBJECT + +public: + tst_Gestures(); + virtual ~tst_Gestures(); + +public slots: + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + +private slots: + void singleshotGesture(); + void pinchGesture(); + + void simplePropagation(); + void simplePropagation2(); + void acceptedGesturePropagation(); + + void simpleGraphicsView(); + void simpleGraphicsItem(); + void overlappingGraphicsItems(); + + void touch_widget(); + void touch_graphicsView(); + + void panOnWidgets(); + +private: + SingleshotGestureRecognizer *singleshotRecognizer; + PinchGestureRecognizer *pinchRecognizer; + SecondFingerGestureRecognizer *secondFingerRecognizer; + PanGestureRecognizer *panGestureRecognizer; + GestureWidget *mainWidget; + + void sendPinchEvents(QWidget *receiver, const QPoint &fromFinger1, const QPoint &fromFinger2); +}; + +tst_Gestures::tst_Gestures() +{ + singleshotRecognizer = new SingleshotGestureRecognizer; + pinchRecognizer = new PinchGestureRecognizer; + secondFingerRecognizer = new SecondFingerGestureRecognizer; + panGestureRecognizer = new PanGestureRecognizer; + qApp->addGestureRecognizer(singleshotRecognizer); + qApp->addGestureRecognizer(pinchRecognizer); + qApp->addGestureRecognizer(secondFingerRecognizer); + qApp->addGestureRecognizer(panGestureRecognizer); +} + +tst_Gestures::~tst_Gestures() +{ +} + + +void tst_Gestures::initTestCase() +{ + mainWidget = new GestureWidget(GestureWidget::DoNotGrabGestures); + mainWidget->setObjectName("MainGestureWidget"); + mainWidget->resize(500, 600); + mainWidget->show(); +} + +void tst_Gestures::cleanupTestCase() +{ + delete mainWidget; mainWidget = 0; +} + +void tst_Gestures::init() +{ + // TODO: Add initialization code here. + // This will be executed immediately before each test is run. + mainWidget->reset(); +} + +void tst_Gestures::cleanup() +{ +} + +bool sendSpontaneousEvent(QWidget *receiver, QEvent *event) +{ + QETWidget::setSpont(event, true); + return qApp->notify(receiver, event); +} + +void tst_Gestures::singleshotGesture() +{ + mainWidget->grabSingleshotGesture(); + SingleshotEvent event; + sendSpontaneousEvent(mainWidget, &event); + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.last.singleshot.delivered); + QVERIFY(mainWidget->gesture.last.cancelled.isEmpty()); +} + +void tst_Gestures::sendPinchEvents(QWidget *receiver, const QPoint &fromFinger1, const QPoint &fromFinger2) +{ + int x1 = fromFinger1.x(); + int y1 = fromFinger1.x(); + int x2 = fromFinger2.x(); + int y2 = fromFinger2.x(); + + TouchEvent event; + event.points[0] = TouchPoint(0,x1,y1, TouchPoint::Begin); + event.points[1] = TouchPoint(); + sendSpontaneousEvent(receiver, &event); + event.points[0] = TouchPoint(0, x1+=2,y1+=2, TouchPoint::Update); + event.points[1] = TouchPoint(); + sendSpontaneousEvent(receiver, &event); + event.points[0] = TouchPoint(0, x1,y1, TouchPoint::Update); + event.points[1] = TouchPoint(1, x2,y2, TouchPoint::Begin); + sendSpontaneousEvent(receiver, &event); + event.points[0] = TouchPoint(0, x1+=5,y1+=10, TouchPoint::End); + event.points[1] = TouchPoint(1, x2+=3,y2+=6, TouchPoint::Update); + sendSpontaneousEvent(receiver, &event); + event.points[0] = TouchPoint(); + event.points[1] = TouchPoint(1, x2+=10,y2+=15, TouchPoint::Update); + sendSpontaneousEvent(receiver, &event); + event.points[0] = TouchPoint(); + event.points[1] = TouchPoint(1, x2,y2, TouchPoint::End); + sendSpontaneousEvent(receiver, &event); +} + +void tst_Gestures::pinchGesture() +{ + mainWidget->grabPinchGesture(); + sendPinchEvents(mainWidget, QPoint(10,10), QPoint(20,20)); + + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.last.singleshot.delivered); + QVERIFY(mainWidget->gesture.last.cancelled.isEmpty()); + QVERIFY(mainWidget->gesture.last.pinch.delivered); + QCOMPARE(mainWidget->gesture.last.pinch.startPoints[0].pt, QPoint(10,10)); + QCOMPARE(mainWidget->gesture.last.pinch.startPoints[1].pt, QPoint(20,20)); + QCOMPARE(mainWidget->gesture.last.pinch.offset, QPoint(0,0)); +} + +void tst_Gestures::simplePropagation() +{ + mainWidget->grabSingleshotGesture(); + GestureWidget offsetWidget(GestureWidget::DoNotGrabGestures); + offsetWidget.setFixedSize(30, 30); + mainWidget->layout()->addWidget(&offsetWidget); + GestureWidget nonGestureWidget(GestureWidget::DoNotGrabGestures); + mainWidget->layout()->addWidget(&nonGestureWidget); + QApplication::processEvents(); + + SingleshotEvent event; + sendSpontaneousEvent(&nonGestureWidget, &event); + QVERIFY(!offsetWidget.gesture.seenGestureEvent); + QVERIFY(!nonGestureWidget.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.last.cancelled.isEmpty()); + QVERIFY(mainWidget->gesture.last.singleshot.delivered); + QCOMPARE(mainWidget->gesture.last.singleshot.offset, QPoint(GestureWidget::LeftMargin, 30 + GestureWidget::TopMargin)); +} + +void tst_Gestures::simplePropagation2() +{ + mainWidget->grabSingleshotGesture(); + mainWidget->grabPinchGesture(); + GestureWidget nonGestureMiddleWidget(GestureWidget::DoNotGrabGestures); + GestureWidget secondGestureWidget(GestureWidget::GrabPinch); + nonGestureMiddleWidget.layout()->addWidget(&secondGestureWidget); + mainWidget->layout()->addWidget(&nonGestureMiddleWidget); + QApplication::processEvents(); + + SingleshotEvent event; + sendSpontaneousEvent(&secondGestureWidget, &event); + QVERIFY(!secondGestureWidget.gesture.seenGestureEvent); + QVERIFY(!nonGestureMiddleWidget.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.last.singleshot.delivered); + QVERIFY(mainWidget->gesture.last.cancelled.isEmpty()); + QCOMPARE(mainWidget->gesture.last.singleshot.offset, QPoint(GestureWidget::LeftMargin*2,GestureWidget::TopMargin*2)); + + mainWidget->reset(); + nonGestureMiddleWidget.reset(); + secondGestureWidget.reset(); + + sendPinchEvents(&secondGestureWidget, QPoint(10,10), QPoint(20,20)); + QVERIFY(secondGestureWidget.gesture.seenGestureEvent); + QVERIFY(!secondGestureWidget.gesture.last.singleshot.delivered); + QVERIFY(secondGestureWidget.gesture.last.pinch.delivered); + QCOMPARE(secondGestureWidget.gesture.last.pinch.startPoints[0].pt, QPoint(10,10)); + QCOMPARE(secondGestureWidget.gesture.last.pinch.startPoints[1].pt, QPoint(20,20)); + QCOMPARE(secondGestureWidget.gesture.last.pinch.offset, QPoint(0,0)); + QVERIFY(!nonGestureMiddleWidget.gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.seenGestureEvent); +} + +void tst_Gestures::acceptedGesturePropagation() +{ + mainWidget->grabSingleshotGesture(); + mainWidget->grabPinchGesture(); + mainWidget->grabSecondFingerGesture(); + GestureWidget nonGestureMiddleWidget(GestureWidget::DoNotGrabGestures); + nonGestureMiddleWidget.setObjectName("nonGestureMiddleWidget"); + GestureWidget secondGestureWidget(GestureWidget::GrabSecondFinger); + secondGestureWidget.setObjectName("secondGestureWidget"); + nonGestureMiddleWidget.layout()->addWidget(&secondGestureWidget); + mainWidget->layout()->addWidget(&nonGestureMiddleWidget); + QApplication::processEvents(); + + sendPinchEvents(&secondGestureWidget, QPoint(10, 10), QPoint(40, 40)); + QVERIFY(secondGestureWidget.gesture.seenGestureEvent); + QVERIFY(!secondGestureWidget.gesture.last.singleshot.delivered); + QVERIFY(!secondGestureWidget.gesture.last.pinch.delivered); + QVERIFY(secondGestureWidget.gesture.last.secondfinger.delivered); + QCOMPARE(secondGestureWidget.gesture.last.secondfinger.startPoint.pt, QPoint(40,40)); + QVERIFY(!nonGestureMiddleWidget.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.last.singleshot.delivered); + QVERIFY(!mainWidget->gesture.last.secondfinger.delivered); + QVERIFY(mainWidget->gesture.last.pinch.delivered); + QCOMPARE(mainWidget->gesture.last.pinch.startPoints[0].pt, QPoint(10,10)); + QCOMPARE(mainWidget->gesture.last.pinch.startPoints[1].pt, QPoint(40,40)); + + mainWidget->reset(); + nonGestureMiddleWidget.reset(); + secondGestureWidget.reset(); + + // don't accept it and make sure it propagates to parent + secondGestureWidget.shouldAcceptSecondFingerGesture = false; + sendPinchEvents(&secondGestureWidget, QPoint(10, 10), QPoint(40, 40)); + QVERIFY(secondGestureWidget.gesture.seenGestureEvent); + QVERIFY(secondGestureWidget.gesture.last.secondfinger.delivered); + QVERIFY(!nonGestureMiddleWidget.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.last.singleshot.delivered); + QVERIFY(mainWidget->gesture.last.secondfinger.delivered); + QVERIFY(mainWidget->gesture.last.pinch.delivered); +} + +void tst_Gestures::simpleGraphicsView() +{ + mainWidget->grabSingleshotGesture(); + GraphicsScene scene; + QGraphicsView view(&scene); + view.grabGesture(SingleshotGestureRecognizer::Name); + mainWidget->layout()->addWidget(&view); + QApplication::processEvents(); + + scene.shouldAcceptSingleshotGesture = true; + + SingleshotEvent event; + sendSpontaneousEvent(&view, &event); + QVERIFY(!mainWidget->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(scene.gesture.last.singleshot.delivered); + QVERIFY(scene.gesture.last.cancelled.isEmpty()); +} + +void tst_Gestures::simpleGraphicsItem() +{ + mainWidget->grabSingleshotGesture(); + GraphicsScene scene; + QGraphicsView view(&scene); + mainWidget->layout()->addWidget(&view); + GraphicsItem *item = new GraphicsItem; + item->grabSingleshotGesture(); + item->setPos(30, 50); + scene.addItem(item); + QApplication::processEvents(); + + SingleshotEvent event(50, 80); + sendSpontaneousEvent(&view, &event); + QVERIFY(item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.seenGestureEvent); + + item->reset(); + scene.reset(); + mainWidget->reset(); + + item->shouldAcceptSingleshotGesture = false; + SingleshotEvent event2(20, 40); + sendSpontaneousEvent(&view, &event2); + QVERIFY(!item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(mainWidget->gesture.seenGestureEvent); +} + +void tst_Gestures::overlappingGraphicsItems() +{ + mainWidget->grabSingleshotGesture(); + GraphicsScene scene; + QGraphicsView view(&scene); + mainWidget->layout()->addWidget(&view); + + GraphicsItem *item = new GraphicsItem(300, 100); + item->setPos(30, 50); + scene.addItem(item); + GraphicsItem *subitem1 = new GraphicsItem(50, 70); + subitem1->setPos(70, 70); + scene.addItem(subitem1); + GraphicsItem *subitem2 = new GraphicsItem(50, 70); + subitem2->setPos(250, 70); + scene.addItem(subitem2); + QApplication::processEvents(); + + item->grabSingleshotGesture(); + item->grabPinchGesture(); + item->grabSecondFingerGesture(); + subitem1->grabSingleshotGesture(); + subitem2->grabSecondFingerGesture(); + + SingleshotEvent event(100, 100); + sendSpontaneousEvent(&view, &event); + QVERIFY(subitem1->gesture.seenGestureEvent); + QVERIFY(!subitem2->gesture.seenGestureEvent); + QVERIFY(!item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.seenGestureEvent); + QVERIFY(subitem1->gesture.last.singleshot.delivered); + + item->reset(); + subitem1->reset(); + subitem2->reset(); + scene.reset(); + mainWidget->reset(); + + subitem1->shouldAcceptSingleshotGesture = false; + SingleshotEvent event2(100, 100); + sendSpontaneousEvent(&view, &event2); + QVERIFY(subitem1->gesture.seenGestureEvent); + QVERIFY(!subitem2->gesture.seenGestureEvent); + QVERIFY(item->gesture.seenGestureEvent); + QVERIFY(scene.gesture.seenGestureEvent); + QVERIFY(!mainWidget->gesture.seenGestureEvent); + QVERIFY(subitem1->gesture.last.singleshot.delivered); + QVERIFY(item->gesture.last.singleshot.delivered); +} + +void tst_Gestures::touch_widget() +{ + GestureWidget leftWidget(GestureWidget::DoNotGrabGestures); + leftWidget.setObjectName("leftWidget"); + leftWidget.setAttribute(Qt::WA_AcceptTouchEvents); + GestureWidget rightWidget(GestureWidget::DoNotGrabGestures); + rightWidget.setObjectName("rightWidget"); + rightWidget.setAttribute(Qt::WA_AcceptTouchEvents); + delete mainWidget->layout(); + (void)new QHBoxLayout(mainWidget); + mainWidget->layout()->addWidget(&leftWidget); + mainWidget->layout()->addWidget(&rightWidget); + QApplication::processEvents(); + + QTest::touchEvent() + .press(0, QPoint(10, 10), &leftWidget); + QTest::touchEvent() + .move(0, QPoint(12, 30), &leftWidget); + QTest::touchEvent() + .stationary(0) + .press(1, QPoint(15, 15), &rightWidget); + QTest::touchEvent() + .move(0, QPoint(10, 35), &leftWidget) + .press(1, QPoint(15, 15), &rightWidget); + QTest::touchEvent() + .move(0, QPoint(10, 40), &leftWidget) + .move(1, QPoint(20, 50), &rightWidget); + QTest::touchEvent() + .release(0, QPoint(10, 40), &leftWidget) + .release(1, QPoint(20, 50), &rightWidget); + QVERIFY(!mainWidget->touch.seenTouchBeginEvent); + QVERIFY(leftWidget.touch.seenTouchBeginEvent); + QVERIFY(leftWidget.touch.seenTouchUpdateEvent); + QVERIFY(leftWidget.touch.seenTouchEndEvent); + QVERIFY(rightWidget.touch.seenTouchBeginEvent); + QVERIFY(rightWidget.touch.seenTouchUpdateEvent); + QVERIFY(rightWidget.touch.seenTouchEndEvent); +} + +void tst_Gestures::touch_graphicsView() +{ + mainWidget->setAttribute(Qt::WA_AcceptTouchEvents); + GraphicsScene scene; + QGraphicsView view(&scene); + view.viewport()->setAttribute(Qt::WA_AcceptTouchEvents); + mainWidget->layout()->addWidget(&view); + + GraphicsItem *item = new GraphicsItem(300, 100); + item->setAcceptTouchEvents(true); + item->setPos(30, 50); + scene.addItem(item); + GraphicsItem *subitem1 = new GraphicsItem(50, 70); + subitem1->setAcceptTouchEvents(true); + subitem1->setPos(70, 70); + scene.addItem(subitem1); + GraphicsItem *subitem2 = new GraphicsItem(50, 70); + subitem2->setAcceptTouchEvents(true); + subitem2->setPos(250, 70); + scene.addItem(subitem2); + QApplication::processEvents(); + + QRect itemRect = view.mapFromScene(item->mapRectToScene(item->boundingRect())).boundingRect(); + QPoint pt = itemRect.center(); + QTest::touchEvent(view.viewport()) + .press(0, pt) + .press(1, pt); + QTest::touchEvent(view.viewport()) + .move(0, pt + QPoint(20, 30)) + .move(1, QPoint(300, 300)); + QTest::touchEvent(view.viewport()) + .stationary(0) + .move(1, QPoint(330, 330)); + QTest::touchEvent(view.viewport()) + .release(0, QPoint(120, 120)) + .release(1, QPoint(300, 300)); + + QVERIFY(item->touch.seenTouchBeginEvent); + QVERIFY(item->touch.seenTouchUpdateEvent); + QVERIFY(item->touch.seenTouchEndEvent); +} + +void tst_Gestures::panOnWidgets() +{ + GestureWidget leftWidget(GestureWidget::GrabPan); + leftWidget.setObjectName("leftWidget"); + leftWidget.setAttribute(Qt::WA_AcceptTouchEvents); + GestureWidget rightWidget(GestureWidget::GrabPan); + rightWidget.setObjectName("rightWidget"); + rightWidget.setAttribute(Qt::WA_AcceptTouchEvents); + delete mainWidget->layout(); + (void)new QHBoxLayout(mainWidget); + mainWidget->layout()->addWidget(&leftWidget); + mainWidget->layout()->addWidget(&rightWidget); + QApplication::processEvents(); + + QTest::touchEvent() + .press(0, QPoint(10, 10), &leftWidget); + QTest::touchEvent() + .move(0, QPoint(12, 30), &leftWidget); + QTest::touchEvent() + .stationary(0) + .press(1, QPoint(15, 15), &rightWidget); + QTest::touchEvent() + .move(0, QPoint(10, 35), &leftWidget) + .press(1, QPoint(15, 15), &rightWidget); + QTest::touchEvent() + .move(0, QPoint(10, 40), &leftWidget) + .move(1, QPoint(20, 50), &rightWidget); + QTest::touchEvent() + .release(0, QPoint(10, 40), &leftWidget) + .release(1, QPoint(20, 50), &rightWidget); + + QVERIFY(leftWidget.gesture.last.pan.delivered); + QVERIFY(rightWidget.gesture.last.pan.delivered); +} + +QTEST_MAIN(tst_Gestures) +#include "tst_gestures.moc" diff --git a/tests/auto/headers/tst_headers.cpp b/tests/auto/headers/tst_headers.cpp index 7211163..e5bc5bf 100644 --- a/tests/auto/headers/tst_headers.cpp +++ b/tests/auto/headers/tst_headers.cpp @@ -135,6 +135,9 @@ void tst_Headers::licenseCheck() QByteArray data = f.readAll(); QStringList content = QString::fromLocal8Bit(data.replace('\r',"")).split("\n"); + if (content.first().contains("generated")) + content.takeFirst(); + QVERIFY(licensePattern.exactMatch(content.at(7)) || licensePattern.exactMatch(content.at(4))); QString licenseType = licensePattern.cap(1); diff --git a/tests/auto/math3d/math3d.pro b/tests/auto/math3d/math3d.pro new file mode 100644 index 0000000..d6189ef --- /dev/null +++ b/tests/auto/math3d/math3d.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = qmatrixnxn qquaternion qvectornd diff --git a/tests/auto/math3d/qmatrixnxn/qmatrixnxn.pro b/tests/auto/math3d/qmatrixnxn/qmatrixnxn.pro new file mode 100644 index 0000000..40c6cc0 --- /dev/null +++ b/tests/auto/math3d/qmatrixnxn/qmatrixnxn.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +VPATH += ../shared +INCLUDEPATH += ../shared +HEADERS += math3dincludes.h +SOURCES += tst_qmatrixnxn.cpp diff --git a/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp new file mode 100644 index 0000000..7facf4a --- /dev/null +++ b/tests/auto/math3d/qmatrixnxn/tst_qmatrixnxn.cpp @@ -0,0 +1,3339 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtCore/qmath.h> +#include "math3dincludes.h" + +class tst_QMatrix : public QObject +{ + Q_OBJECT +public: + tst_QMatrix() {} + ~tst_QMatrix() {} + +private slots: + void create2x2(); + void create3x3(); + void create4x4(); + void create4x3(); + + void isIdentity2x2(); + void isIdentity3x3(); + void isIdentity4x4(); + void isIdentity4x3(); + + void compare2x2(); + void compare3x3(); + void compare4x4(); + void compare4x3(); + + void transposed2x2(); + void transposed3x3(); + void transposed4x4(); + void transposed4x3(); + + void add2x2_data(); + void add2x2(); + void add3x3_data(); + void add3x3(); + void add4x4_data(); + void add4x4(); + void add4x3_data(); + void add4x3(); + + void subtract2x2_data(); + void subtract2x2(); + void subtract3x3_data(); + void subtract3x3(); + void subtract4x4_data(); + void subtract4x4(); + void subtract4x3_data(); + void subtract4x3(); + + void multiply2x2_data(); + void multiply2x2(); + void multiply3x3_data(); + void multiply3x3(); + void multiply4x4_data(); + void multiply4x4(); + void multiply4x3_data(); + void multiply4x3(); + + void multiplyFactor2x2_data(); + void multiplyFactor2x2(); + void multiplyFactor3x3_data(); + void multiplyFactor3x3(); + void multiplyFactor4x4_data(); + void multiplyFactor4x4(); + void multiplyFactor4x3_data(); + void multiplyFactor4x3(); + + void divideFactor2x2_data(); + void divideFactor2x2(); + void divideFactor3x3_data(); + void divideFactor3x3(); + void divideFactor4x4_data(); + void divideFactor4x4(); + void divideFactor4x3_data(); + void divideFactor4x3(); + + void negate2x2_data(); + void negate2x2(); + void negate3x3_data(); + void negate3x3(); + void negate4x4_data(); + void negate4x4(); + void negate4x3_data(); + void negate4x3(); + + void inverted4x4_data(); + void inverted4x4(); + + void orthonormalInverse4x4(); + + void scale4x4_data(); + void scale4x4(); + + void translate4x4_data(); + void translate4x4(); + + void rotate4x4_data(); + void rotate4x4(); + + void normalMatrix_data(); + void normalMatrix(); + + void optimizedTransforms(); + + void ortho(); + void frustum(); + void perspective(); + void flipCoordinates(); + + void convertGeneric(); + + void extractAxisRotation_data(); + void extractAxisRotation(); + + void extractTranslation_data(); + void extractTranslation(); + + void inferSpecialType_data(); + void inferSpecialType(); + + void columnsAndRows(); + + void convertQMatrix(); + void convertQTransform(); + + void fill(); + + void mapRect_data(); + void mapRect(); + +private: + static void setMatrix(QMatrix2x2& m, const qreal *values); + static void setMatrixFixed(QMatrix2x2& m, const qreal *values); + static bool isSame(const QMatrix2x2& m, const qreal *values); + static bool isIdentity(const QMatrix2x2& m); + + static void setMatrix(QMatrix3x3& m, const qreal *values); + static void setMatrixFixed(QMatrix3x3& m, const qreal *values); + static bool isSame(const QMatrix3x3& m, const qreal *values); + static bool isIdentity(const QMatrix3x3& m); + + static void setMatrix(QMatrix4x4& m, const qreal *values); + static void setMatrixFixed(QMatrix4x4& m, const qreal *values); + static bool isSame(const QMatrix4x4& m, const qreal *values); + static bool isIdentity(const QMatrix4x4& m); + + static void setMatrix(QMatrix4x3& m, const qreal *values); + static void setMatrixFixed(QMatrix4x3& m, const qreal *values); + static bool isSame(const QMatrix4x3& m, const qreal *values); + static bool isIdentity(const QMatrix4x3& m); +}; + +static const qreal nullValues2[] = + {0.0f, 0.0f, + 0.0f, 0.0f}; + +static qreal const identityValues2[16] = + {1.0f, 0.0f, + 0.0f, 1.0f}; + +static const qreal doubleIdentity2[] = + {2.0f, 0.0f, + 0.0f, 2.0f}; + +static qreal const uniqueValues2[16] = + {1.0f, 2.0f, + 5.0f, 6.0f}; + +static qreal const transposedValues2[16] = + {1.0f, 5.0f, + 2.0f, 6.0f}; + +static const qreal nullValues3[] = + {0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f}; + +static qreal const identityValues3[16] = + {1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f}; + +static const qreal doubleIdentity3[] = + {2.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, + 0.0f, 0.0f, 2.0f}; + +static qreal const uniqueValues3[16] = + {1.0f, 2.0f, 3.0f, + 5.0f, 6.0f, 7.0f, + 9.0f, 10.0f, 11.0f}; + +static qreal const transposedValues3[16] = + {1.0f, 5.0f, 9.0f, + 2.0f, 6.0f, 10.0f, + 3.0f, 7.0f, 11.0f}; + +static const qreal nullValues4[] = + {0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f}; + +static qreal const identityValues4[16] = + {1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + +static const qreal doubleIdentity4[] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 2.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 2.0f}; + +static qreal const uniqueValues4[16] = + {1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f}; + +static qreal const transposedValues4[16] = + {1.0f, 5.0f, 9.0f, 13.0f, + 2.0f, 6.0f, 10.0f, 14.0f, + 3.0f, 7.0f, 11.0f, 15.0f, + 4.0f, 8.0f, 12.0f, 16.0f}; + +static const qreal nullValues4x3[] = + {0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f}; + +static qreal const identityValues4x3[12] = + {1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f}; + +static qreal const doubleIdentity4x3[12] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 2.0f, 0.0f}; + +static qreal const uniqueValues4x3[12] = + {1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f}; + +static qreal const transposedValues3x4[12] = + {1.0f, 5.0f, 9.0f, + 2.0f, 6.0f, 10.0f, + 3.0f, 7.0f, 11.0f, + 4.0f, 8.0f, 12.0f}; + +// Set a matrix to a specified array of values, which are assumed +// to be in row-major order. This sets the values using floating-point. +void tst_QMatrix::setMatrix(QMatrix2x2& m, const qreal *values) +{ + for (int row = 0; row < 2; ++row) + for (int col = 0; col < 2; ++col) + m(row, col) = values[row * 2 + col]; +} +void tst_QMatrix::setMatrix(QMatrix3x3& m, const qreal *values) +{ + for (int row = 0; row < 3; ++row) + for (int col = 0; col < 3; ++col) + m(row, col) = values[row * 3 + col]; +} +void tst_QMatrix::setMatrix(QMatrix4x4& m, const qreal *values) +{ + for (int row = 0; row < 4; ++row) + for (int col = 0; col < 4; ++col) + m(row, col) = values[row * 4 + col]; +} +void tst_QMatrix::setMatrix(QMatrix4x3& m, const qreal *values) +{ + for (int row = 0; row < 3; ++row) + for (int col = 0; col < 4; ++col) + m(row, col) = values[row * 4 + col]; +} + +// Set a matrix to a specified array of values, which are assumed +// to be in row-major order. This sets the values using fixed-point. +void tst_QMatrix::setMatrixFixed(QMatrix2x2& m, const qreal *values) +{ + float *data = m.data(); + for (int row = 0; row < 2; ++row) { + for (int col = 0; col < 2; ++col) { + data[row + col * 2] = values[row * 2 + col]; + } + } +} +void tst_QMatrix::setMatrixFixed(QMatrix3x3& m, const qreal *values) +{ + float *data = m.data(); + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 3; ++col) { + data[row + col * 3] = values[row * 3 + col]; + } + } +} +void tst_QMatrix::setMatrixFixed(QMatrix4x4& m, const qreal *values) +{ + float *data = m.data(); + for (int row = 0; row < 4; ++row) { + for (int col = 0; col < 4; ++col) { + data[row + col * 4] = values[row * 4 + col]; + } + } +} +void tst_QMatrix::setMatrixFixed(QMatrix4x3& m, const qreal *values) +{ + float *data = m.data(); + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 4; ++col) { + data[row + col * 3] = values[row * 4 + col]; + } + } +} + +// qFuzzyCompare isn't quite "fuzzy" enough to handle conversion +// to fixed-point and back again. So create "fuzzier" compares. +static bool fuzzyCompare(float x, float y, qreal epsilon = 0.001) +{ + float diff = x - y; + if (diff < 0.0f) + diff = -diff; + return (diff < epsilon); +} + +static bool fuzzyCompare(const QVector3D &v1, const QVector3D &v2, qreal epsilon = 0.001) +{ + if (!fuzzyCompare(v1.x(), v2.x(), epsilon)) + return false; + if (!fuzzyCompare(v1.y(), v2.y(), epsilon)) + return false; + if (!fuzzyCompare(v1.z(), v2.z(), epsilon)) + return false; + return true; +} + +static bool matrixFuzzyCompare(const QMatrix4x4 &m1, const QMatrix4x4 &m2) +{ + bool ret = true; + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + ret = ret && fuzzyCompare(m1(i, j), m2(i, j)); + } + } + + return ret; +} + +// Determine if a matrix is the same as a specified array of values. +// The values are assumed to be specified in row-major order. +bool tst_QMatrix::isSame(const QMatrix2x2& m, const qreal *values) +{ + const float *mv = m.constData(); + for (int row = 0; row < 2; ++row) { + for (int col = 0; col < 2; ++col) { + // Check the values using the operator() function. + if (!fuzzyCompare((float)(m(row, col)), (float)(values[row * 2 + col]))) { + qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 2 + col]; + return false; + } + + // Check the values using direct access, which verifies that the values + // are stored internally in column-major order. + if (!fuzzyCompare((float)(mv[col * 2 + row]), (float)(values[row * 2 + col]))) { + qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 2 + row] << "expected =" << values[row * 2 + col]; + return false; + } + } + } + return true; +} +bool tst_QMatrix::isSame(const QMatrix3x3& m, const qreal *values) +{ + const float *mv = m.constData(); + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 3; ++col) { + // Check the values using the operator() access function. + if (!fuzzyCompare((float)(m(row, col)), (float)(values[row * 3 + col]))) { + qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 3 + col]; + return false; + } + + // Check the values using direct access, which verifies that the values + // are stored internally in column-major order. + if (!fuzzyCompare((float)(mv[col * 3 + row]), (float)(values[row * 3 + col]))) { + qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 3 + row] << "expected =" << values[row * 3 + col]; + return false; + } + } + } + return true; +} +bool tst_QMatrix::isSame(const QMatrix4x4& m, const qreal *values) +{ + const float *mv = m.constData(); + for (int row = 0; row < 4; ++row) { + for (int col = 0; col < 4; ++col) { + // Check the values using the operator() access function. + if (!fuzzyCompare((float)(m(row, col)), (float)(values[row * 4 + col]))) { + qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 4 + col]; + return false; + } + + // Check the values using direct access, which verifies that the values + // are stored internally in column-major order. + if (!fuzzyCompare((float)(mv[col * 4 + row]), (float)(values[row * 4 + col]))) { + qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 4 + row] << "expected =" << values[row * 4 + col]; + return false; + } + } + } + return true; +} +bool tst_QMatrix::isSame(const QMatrix4x3& m, const qreal *values) +{ + const float *mv = m.constData(); + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 4; ++col) { + // Check the values using the operator() access function. + if (!fuzzyCompare((float)(m(row, col)), (float)(values[row * 4 + col]))) { + qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 4 + col]; + return false; + } + + // Check the values using direct access, which verifies that the values + // are stored internally in column-major order. + if (!fuzzyCompare((float)(mv[col * 3 + row]), (float)(values[row * 4 + col]))) { + qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 3 + row] << "expected =" << values[row * 4 + col]; + return false; + } + } + } + return true; +} + +// Determine if a matrix is the identity. +bool tst_QMatrix::isIdentity(const QMatrix2x2& m) +{ + return isSame(m, identityValues2); +} +bool tst_QMatrix::isIdentity(const QMatrix3x3& m) +{ + return isSame(m, identityValues3); +} +bool tst_QMatrix::isIdentity(const QMatrix4x4& m) +{ + return isSame(m, identityValues4); +} +bool tst_QMatrix::isIdentity(const QMatrix4x3& m) +{ + return isSame(m, identityValues4x3); +} + +// Test the creation of QMatrix2x2 objects in various ways: +// construct, copy, and modify. +void tst_QMatrix::create2x2() +{ + QMatrix2x2 m1; + QVERIFY(isIdentity(m1)); + QVERIFY(m1.isIdentity()); + + QMatrix2x2 m2; + setMatrix(m2, uniqueValues2); + QVERIFY(isSame(m2, uniqueValues2)); + QVERIFY(!m2.isIdentity()); + + QMatrix2x2 m3; + setMatrixFixed(m3, uniqueValues2); + QVERIFY(isSame(m3, uniqueValues2)); + + QMatrix2x2 m4(m3); + QVERIFY(isSame(m4, uniqueValues2)); + + QMatrix2x2 m5; + m5 = m3; + QVERIFY(isSame(m5, uniqueValues2)); + + m5.setIdentity(); + QVERIFY(isIdentity(m5)); + + QMatrix2x2 m6(uniqueValues2); + QVERIFY(isSame(m6, uniqueValues2)); + qreal vals[4]; + m6.toValueArray(vals); + for (int index = 0; index < 4; ++index) + QCOMPARE((float)(vals[index]), (float)(uniqueValues2[index])); +} + +// Test the creation of QMatrix3x3 objects in various ways: +// construct, copy, and modify. +void tst_QMatrix::create3x3() +{ + QMatrix3x3 m1; + QVERIFY(isIdentity(m1)); + QVERIFY(m1.isIdentity()); + + QMatrix3x3 m2; + setMatrix(m2, uniqueValues3); + QVERIFY(isSame(m2, uniqueValues3)); + QVERIFY(!m2.isIdentity()); + + QMatrix3x3 m3; + setMatrixFixed(m3, uniqueValues3); + QVERIFY(isSame(m3, uniqueValues3)); + + QMatrix3x3 m4(m3); + QVERIFY(isSame(m4, uniqueValues3)); + + QMatrix3x3 m5; + m5 = m3; + QVERIFY(isSame(m5, uniqueValues3)); + + m5.setIdentity(); + QVERIFY(isIdentity(m5)); + + QMatrix3x3 m6(uniqueValues3); + QVERIFY(isSame(m6, uniqueValues3)); + qreal vals[9]; + m6.toValueArray(vals); + for (int index = 0; index < 9; ++index) + QCOMPARE((float)(vals[index]), (float)(uniqueValues3[index])); +} + +// Test the creation of QMatrix4x4 objects in various ways: +// construct, copy, and modify. +void tst_QMatrix::create4x4() +{ + QMatrix4x4 m1; + QVERIFY(isIdentity(m1)); + QVERIFY(m1.isIdentity()); + + QMatrix4x4 m2; + setMatrix(m2, uniqueValues4); + QVERIFY(isSame(m2, uniqueValues4)); + QVERIFY(!m2.isIdentity()); + + QMatrix4x4 m3; + setMatrixFixed(m3, uniqueValues4); + QVERIFY(isSame(m3, uniqueValues4)); + + QMatrix4x4 m4(m3); + QVERIFY(isSame(m4, uniqueValues4)); + + QMatrix4x4 m5; + m5 = m3; + QVERIFY(isSame(m5, uniqueValues4)); + + m5.setIdentity(); + QVERIFY(isIdentity(m5)); + + QMatrix4x4 m6(uniqueValues4); + QVERIFY(isSame(m6, uniqueValues4)); + qreal vals[16]; + m6.toValueArray(vals); + for (int index = 0; index < 16; ++index) + QCOMPARE((float)(vals[index]), (float)(uniqueValues4[index])); + + QMatrix4x4 m8 + (uniqueValues4[0], uniqueValues4[1], uniqueValues4[2], uniqueValues4[3], + uniqueValues4[4], uniqueValues4[5], uniqueValues4[6], uniqueValues4[7], + uniqueValues4[8], uniqueValues4[9], uniqueValues4[10], uniqueValues4[11], + uniqueValues4[12], uniqueValues4[13], uniqueValues4[14], uniqueValues4[15]); + QVERIFY(isSame(m8, uniqueValues4)); +} + +// Test the creation of QMatrix4x3 objects in various ways: +// construct, copy, and modify. +void tst_QMatrix::create4x3() +{ + QMatrix4x3 m1; + QVERIFY(isIdentity(m1)); + QVERIFY(m1.isIdentity()); + + QMatrix4x3 m2; + setMatrix(m2, uniqueValues4x3); + QVERIFY(isSame(m2, uniqueValues4x3)); + QVERIFY(!m2.isIdentity()); + + QMatrix4x3 m3; + setMatrixFixed(m3, uniqueValues4x3); + QVERIFY(isSame(m3, uniqueValues4x3)); + + QMatrix4x3 m4(m3); + QVERIFY(isSame(m4, uniqueValues4x3)); + + QMatrix4x3 m5; + m5 = m3; + QVERIFY(isSame(m5, uniqueValues4x3)); + + m5.setIdentity(); + QVERIFY(isIdentity(m5)); + + QMatrix4x3 m6(uniqueValues4x3); + QVERIFY(isSame(m6, uniqueValues4x3)); + qreal vals[12]; + m6.toValueArray(vals); + for (int index = 0; index < 12; ++index) + QCOMPARE((float)(vals[index]), (float)(uniqueValues4x3[index])); +} + +// Test isIdentity() for 2x2 matrices. +void tst_QMatrix::isIdentity2x2() +{ + for (int i = 0; i < 2 * 2; ++i) { + QMatrix2x2 m; + QVERIFY(m.isIdentity()); + m.data()[i] = 42.0f; + QVERIFY(!m.isIdentity()); + } +} + +// Test isIdentity() for 3x3 matrices. +void tst_QMatrix::isIdentity3x3() +{ + for (int i = 0; i < 3 * 3; ++i) { + QMatrix3x3 m; + QVERIFY(m.isIdentity()); + m.data()[i] = 42.0f; + QVERIFY(!m.isIdentity()); + } +} + +// Test isIdentity() for 4x4 matrices. +void tst_QMatrix::isIdentity4x4() +{ + for (int i = 0; i < 4 * 4; ++i) { + QMatrix4x4 m; + QVERIFY(m.isIdentity()); + m.data()[i] = 42.0f; + QVERIFY(!m.isIdentity()); + } + + // Force the "Identity" flag bit to be lost and check again. + QMatrix4x4 m2; + m2.data()[0] = 1.0f; + QVERIFY(m2.isIdentity()); +} + +// Test isIdentity() for 4x3 matrices. +void tst_QMatrix::isIdentity4x3() +{ + for (int i = 0; i < 4 * 3; ++i) { + QMatrix4x3 m; + QVERIFY(m.isIdentity()); + m.data()[i] = 42.0f; + QVERIFY(!m.isIdentity()); + } +} + +// Test 2x2 matrix comparisons. +void tst_QMatrix::compare2x2() +{ + QMatrix2x2 m1(uniqueValues2); + QMatrix2x2 m2(uniqueValues2); + QMatrix2x2 m3(transposedValues2); + + QVERIFY(m1 == m2); + QVERIFY(!(m1 != m2)); + QVERIFY(m1 != m3); + QVERIFY(!(m1 == m3)); +} + +// Test 3x3 matrix comparisons. +void tst_QMatrix::compare3x3() +{ + QMatrix3x3 m1(uniqueValues3); + QMatrix3x3 m2(uniqueValues3); + QMatrix3x3 m3(transposedValues3); + + QVERIFY(m1 == m2); + QVERIFY(!(m1 != m2)); + QVERIFY(m1 != m3); + QVERIFY(!(m1 == m3)); +} + +// Test 4x4 matrix comparisons. +void tst_QMatrix::compare4x4() +{ + QMatrix4x4 m1(uniqueValues4); + QMatrix4x4 m2(uniqueValues4); + QMatrix4x4 m3(transposedValues4); + + QVERIFY(m1 == m2); + QVERIFY(!(m1 != m2)); + QVERIFY(m1 != m3); + QVERIFY(!(m1 == m3)); +} + +// Test 4x3 matrix comparisons. +void tst_QMatrix::compare4x3() +{ + QMatrix4x3 m1(uniqueValues4x3); + QMatrix4x3 m2(uniqueValues4x3); + QMatrix4x3 m3(transposedValues3x4); + + QVERIFY(m1 == m2); + QVERIFY(!(m1 != m2)); + QVERIFY(m1 != m3); + QVERIFY(!(m1 == m3)); +} + +// Test matrix 2x2 transpose operations. +void tst_QMatrix::transposed2x2() +{ + // Transposing the identity should result in the identity. + QMatrix2x2 m1; + QMatrix2x2 m2 = m1.transposed(); + QVERIFY(isIdentity(m2)); + + // Transpose a more interesting matrix that allows us to track + // exactly where each source element ends up. + QMatrix2x2 m3(uniqueValues2); + QMatrix2x2 m4 = m3.transposed(); + QVERIFY(isSame(m4, transposedValues2)); + + // Transpose in-place, just to check that the compiler is sane. + m3 = m3.transposed(); + QVERIFY(isSame(m3, transposedValues2)); +} + +// Test matrix 3x3 transpose operations. +void tst_QMatrix::transposed3x3() +{ + // Transposing the identity should result in the identity. + QMatrix3x3 m1; + QMatrix3x3 m2 = m1.transposed(); + QVERIFY(isIdentity(m2)); + + // Transpose a more interesting matrix that allows us to track + // exactly where each source element ends up. + QMatrix3x3 m3(uniqueValues3); + QMatrix3x3 m4 = m3.transposed(); + QVERIFY(isSame(m4, transposedValues3)); + + // Transpose in-place, just to check that the compiler is sane. + m3 = m3.transposed(); + QVERIFY(isSame(m3, transposedValues3)); +} + +// Test matrix 4x4 transpose operations. +void tst_QMatrix::transposed4x4() +{ + // Transposing the identity should result in the identity. + QMatrix4x4 m1; + QMatrix4x4 m2 = m1.transposed(); + QVERIFY(isIdentity(m2)); + + // Transpose a more interesting matrix that allows us to track + // exactly where each source element ends up. + QMatrix4x4 m3(uniqueValues4); + QMatrix4x4 m4 = m3.transposed(); + QVERIFY(isSame(m4, transposedValues4)); + + // Transpose in-place, just to check that the compiler is sane. + m3 = m3.transposed(); + QVERIFY(isSame(m3, transposedValues4)); +} + +// Test matrix 4x3 transpose operations. +void tst_QMatrix::transposed4x3() +{ + QMatrix4x3 m3(uniqueValues4x3); + QMatrix3x4 m4 = m3.transposed(); + qreal values[12]; + m4.toValueArray(values); + for (int index = 0; index < 12; ++index) + QCOMPARE(values[index], transposedValues3x4[index]); +} + +// Test matrix addition for 2x2 matrices. +void tst_QMatrix::add2x2_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues2 << (void *)nullValues2 << (void *)nullValues2; + + QTest::newRow("identity/null") + << (void *)identityValues2 << (void *)nullValues2 << (void *)identityValues2; + + QTest::newRow("identity/identity") + << (void *)identityValues2 << (void *)identityValues2 << (void *)doubleIdentity2; + + static qreal const sumValues[16] = + {2.0f, 7.0f, + 7.0f, 12.0f}; + QTest::newRow("unique") + << (void *)uniqueValues2 << (void *)transposedValues2 << (void *)sumValues; +} +void tst_QMatrix::add2x2() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix2x2 m1((const qreal *)m1Values); + QMatrix2x2 m2((const qreal *)m2Values); + + QMatrix2x2 m4(m1); + m4 += m2; + QVERIFY(isSame(m4, (const qreal *)m3Values)); + + QMatrix2x2 m5; + m5 = m1 + m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix addition for 3x3 matrices. +void tst_QMatrix::add3x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues3 << (void *)nullValues3 << (void *)nullValues3; + + QTest::newRow("identity/null") + << (void *)identityValues3 << (void *)nullValues3 << (void *)identityValues3; + + QTest::newRow("identity/identity") + << (void *)identityValues3 << (void *)identityValues3 << (void *)doubleIdentity3; + + static qreal const sumValues[16] = + {2.0f, 7.0f, 12.0f, + 7.0f, 12.0f, 17.0f, + 12.0f, 17.0f, 22.0f}; + QTest::newRow("unique") + << (void *)uniqueValues3 << (void *)transposedValues3 << (void *)sumValues; +} +void tst_QMatrix::add3x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix3x3 m1((const qreal *)m1Values); + QMatrix3x3 m2((const qreal *)m2Values); + + QMatrix3x3 m4(m1); + m4 += m2; + QVERIFY(isSame(m4, (const qreal *)m3Values)); + + QMatrix3x3 m5; + m5 = m1 + m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix addition for 4x4 matrices. +void tst_QMatrix::add4x4_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues4 << (void *)nullValues4 << (void *)nullValues4; + + QTest::newRow("identity/null") + << (void *)identityValues4 << (void *)nullValues4 << (void *)identityValues4; + + QTest::newRow("identity/identity") + << (void *)identityValues4 << (void *)identityValues4 << (void *)doubleIdentity4; + + static qreal const sumValues[16] = + {2.0f, 7.0f, 12.0f, 17.0f, + 7.0f, 12.0f, 17.0f, 22.0f, + 12.0f, 17.0f, 22.0f, 27.0f, + 17.0f, 22.0f, 27.0f, 32.0f}; + QTest::newRow("unique") + << (void *)uniqueValues4 << (void *)transposedValues4 << (void *)sumValues; +} +void tst_QMatrix::add4x4() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x4 m1((const qreal *)m1Values); + QMatrix4x4 m2((const qreal *)m2Values); + + QMatrix4x4 m4(m1); + m4 += m2; + QVERIFY(isSame(m4, (const qreal *)m3Values)); + + QMatrix4x4 m5; + m5 = m1 + m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix addition for 4x3 matrices. +void tst_QMatrix::add4x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues4x3 << (void *)nullValues4x3 << (void *)nullValues4x3; + + QTest::newRow("identity/null") + << (void *)identityValues4x3 << (void *)nullValues4x3 << (void *)identityValues4x3; + + QTest::newRow("identity/identity") + << (void *)identityValues4x3 << (void *)identityValues4x3 << (void *)doubleIdentity4x3; + + static qreal const sumValues[16] = + {2.0f, 7.0f, 12.0f, 6.0f, + 11.0f, 16.0f, 10.0f, 15.0f, + 20.0f, 14.0f, 19.0f, 24.0f}; + QTest::newRow("unique") + << (void *)uniqueValues4x3 << (void *)transposedValues3x4 << (void *)sumValues; +} +void tst_QMatrix::add4x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x3 m1((const qreal *)m1Values); + QMatrix4x3 m2((const qreal *)m2Values); + + QMatrix4x3 m4(m1); + m4 += m2; + QVERIFY(isSame(m4, (const qreal *)m3Values)); + + QMatrix4x3 m5; + m5 = m1 + m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix subtraction for 2x2 matrices. +void tst_QMatrix::subtract2x2_data() +{ + // Use the same test cases as the add test. + add2x2_data(); +} +void tst_QMatrix::subtract2x2() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix2x2 m1((const qreal *)m1Values); + QMatrix2x2 m2((const qreal *)m2Values); + QMatrix2x2 m3((const qreal *)m3Values); + + QMatrix2x2 m4(m3); + m4 -= m1; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix2x2 m5; + m5 = m3 - m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); + + QMatrix2x2 m6(m3); + m6 -= m2; + QVERIFY(isSame(m6, (const qreal *)m1Values)); + + QMatrix2x2 m7; + m7 = m3 - m2; + QVERIFY(isSame(m7, (const qreal *)m1Values)); +} + +// Test matrix subtraction for 3x3 matrices. +void tst_QMatrix::subtract3x3_data() +{ + // Use the same test cases as the add test. + add3x3_data(); +} +void tst_QMatrix::subtract3x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix3x3 m1((const qreal *)m1Values); + QMatrix3x3 m2((const qreal *)m2Values); + QMatrix3x3 m3((const qreal *)m3Values); + + QMatrix3x3 m4(m3); + m4 -= m1; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix3x3 m5; + m5 = m3 - m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); + + QMatrix3x3 m6(m3); + m6 -= m2; + QVERIFY(isSame(m6, (const qreal *)m1Values)); + + QMatrix3x3 m7; + m7 = m3 - m2; + QVERIFY(isSame(m7, (const qreal *)m1Values)); +} + +// Test matrix subtraction for 4x4 matrices. +void tst_QMatrix::subtract4x4_data() +{ + // Use the same test cases as the add test. + add4x4_data(); +} +void tst_QMatrix::subtract4x4() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x4 m1((const qreal *)m1Values); + QMatrix4x4 m2((const qreal *)m2Values); + QMatrix4x4 m3((const qreal *)m3Values); + + QMatrix4x4 m4(m3); + m4 -= m1; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix4x4 m5; + m5 = m3 - m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); + + QMatrix4x4 m6(m3); + m6 -= m2; + QVERIFY(isSame(m6, (const qreal *)m1Values)); + + QMatrix4x4 m7; + m7 = m3 - m2; + QVERIFY(isSame(m7, (const qreal *)m1Values)); +} + +// Test matrix subtraction for 4x3 matrices. +void tst_QMatrix::subtract4x3_data() +{ + // Use the same test cases as the add test. + add4x3_data(); +} +void tst_QMatrix::subtract4x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x3 m1((const qreal *)m1Values); + QMatrix4x3 m2((const qreal *)m2Values); + QMatrix4x3 m3((const qreal *)m3Values); + + QMatrix4x3 m4(m3); + m4 -= m1; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix4x3 m5; + m5 = m3 - m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); + + QMatrix4x3 m6(m3); + m6 -= m2; + QVERIFY(isSame(m6, (const qreal *)m1Values)); + + QMatrix4x3 m7; + m7 = m3 - m2; + QVERIFY(isSame(m7, (const qreal *)m1Values)); +} + +// Test matrix multiplication for 2x2 matrices. +void tst_QMatrix::multiply2x2_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues2 << (void *)nullValues2 << (void *)nullValues2; + + QTest::newRow("null/unique") + << (void *)nullValues2 << (void *)uniqueValues2 << (void *)nullValues2; + + QTest::newRow("unique/null") + << (void *)uniqueValues2 << (void *)nullValues2 << (void *)nullValues2; + + QTest::newRow("unique/identity") + << (void *)uniqueValues2 << (void *)identityValues2 << (void *)uniqueValues2; + + QTest::newRow("identity/unique") + << (void *)identityValues2 << (void *)uniqueValues2 << (void *)uniqueValues2; + + static qreal uniqueResult[4]; + for (int row = 0; row < 2; ++row) { + for (int col = 0; col < 2; ++col) { + qreal sum = 0.0f; + for (int j = 0; j < 2; ++j) + sum += uniqueValues2[row * 2 + j] * transposedValues2[j * 2 + col]; + uniqueResult[row * 2 + col] = sum; + } + } + + QTest::newRow("unique/transposed") + << (void *)uniqueValues2 << (void *)transposedValues2 << (void *)uniqueResult; +} +void tst_QMatrix::multiply2x2() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix2x2 m1((const qreal *)m1Values); + QMatrix2x2 m2((const qreal *)m2Values); + + QMatrix2x2 m5; + m5 = m1 * m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix multiplication for 3x3 matrices. +void tst_QMatrix::multiply3x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues3 << (void *)nullValues3 << (void *)nullValues3; + + QTest::newRow("null/unique") + << (void *)nullValues3 << (void *)uniqueValues3 << (void *)nullValues3; + + QTest::newRow("unique/null") + << (void *)uniqueValues3 << (void *)nullValues3 << (void *)nullValues3; + + QTest::newRow("unique/identity") + << (void *)uniqueValues3 << (void *)identityValues3 << (void *)uniqueValues3; + + QTest::newRow("identity/unique") + << (void *)identityValues3 << (void *)uniqueValues3 << (void *)uniqueValues3; + + static qreal uniqueResult[9]; + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 3; ++col) { + qreal sum = 0.0f; + for (int j = 0; j < 3; ++j) + sum += uniqueValues3[row * 3 + j] * transposedValues3[j * 3 + col]; + uniqueResult[row * 3 + col] = sum; + } + } + + QTest::newRow("unique/transposed") + << (void *)uniqueValues3 << (void *)transposedValues3 << (void *)uniqueResult; +} +void tst_QMatrix::multiply3x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix3x3 m1((const qreal *)m1Values); + QMatrix3x3 m2((const qreal *)m2Values); + + QMatrix3x3 m5; + m5 = m1 * m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix multiplication for 4x4 matrices. +void tst_QMatrix::multiply4x4_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues4 << (void *)nullValues4 << (void *)nullValues4; + + QTest::newRow("null/unique") + << (void *)nullValues4 << (void *)uniqueValues4 << (void *)nullValues4; + + QTest::newRow("unique/null") + << (void *)uniqueValues4 << (void *)nullValues4 << (void *)nullValues4; + + QTest::newRow("unique/identity") + << (void *)uniqueValues4 << (void *)identityValues4 << (void *)uniqueValues4; + + QTest::newRow("identity/unique") + << (void *)identityValues4 << (void *)uniqueValues4 << (void *)uniqueValues4; + + static qreal uniqueResult[16]; + for (int row = 0; row < 4; ++row) { + for (int col = 0; col < 4; ++col) { + qreal sum = 0.0f; + for (int j = 0; j < 4; ++j) + sum += uniqueValues4[row * 4 + j] * transposedValues4[j * 4 + col]; + uniqueResult[row * 4 + col] = sum; + } + } + + QTest::newRow("unique/transposed") + << (void *)uniqueValues4 << (void *)transposedValues4 << (void *)uniqueResult; +} +void tst_QMatrix::multiply4x4() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x4 m1((const qreal *)m1Values); + QMatrix4x4 m2((const qreal *)m2Values); + + QMatrix4x4 m4; + m4 = m1; + m4 *= m2; + QVERIFY(isSame(m4, (const qreal *)m3Values)); + + QMatrix4x4 m5; + m5 = m1 * m2; + QVERIFY(isSame(m5, (const qreal *)m3Values)); +} + +// Test matrix multiplication for 4x3 matrices. +void tst_QMatrix::multiply4x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<void *>("m3Values"); + + QTest::newRow("null") + << (void *)nullValues4x3 << (void *)nullValues4x3 << (void *)nullValues3; + + QTest::newRow("null/unique") + << (void *)nullValues4x3 << (void *)uniqueValues4x3 << (void *)nullValues3; + + QTest::newRow("unique/null") + << (void *)uniqueValues4x3 << (void *)nullValues4x3 << (void *)nullValues3; + + static qreal uniqueResult[9]; + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 3; ++col) { + qreal sum = 0.0f; + for (int j = 0; j < 4; ++j) + sum += uniqueValues4x3[row * 4 + j] * transposedValues3x4[j * 3 + col]; + uniqueResult[row * 3 + col] = sum; + } + } + + QTest::newRow("unique/transposed") + << (void *)uniqueValues4x3 << (void *)transposedValues3x4 << (void *)uniqueResult; +} +void tst_QMatrix::multiply4x3() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(void *, m3Values); + + QMatrix4x3 m1((const qreal *)m1Values); + QMatrix3x4 m2((const qreal *)m2Values); + + QGenericMatrix<3, 3, qreal, float> m4; + m4 = m1 * m2; + qreal values[9]; + m4.toValueArray(values); + for (int index = 0; index < 9; ++index) + QCOMPARE(values[index], ((const qreal *)m3Values)[index]); +} + +// Test matrix multiplication by a factor for 2x2 matrices. +void tst_QMatrix::multiplyFactor2x2_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<void *>("m2Values"); + + QTest::newRow("null") + << (void *)nullValues2 << (qreal)1.0f << (void *)nullValues2; + + QTest::newRow("double identity") + << (void *)identityValues2 << (qreal)2.0f << (void *)doubleIdentity2; + + static qreal const values[16] = + {1.0f, 2.0f, + 5.0f, 6.0f}; + static qreal const doubleValues[16] = + {2.0f, 4.0f, + 10.0f, 12.0f}; + static qreal const negDoubleValues[16] = + {-2.0f, -4.0f, + -10.0f, -12.0f}; + + QTest::newRow("unique") + << (void *)values << (qreal)2.0f << (void *)doubleValues; + + QTest::newRow("neg") + << (void *)values << (qreal)-2.0f << (void *)negDoubleValues; + + QTest::newRow("zero") + << (void *)values << (qreal)0.0f << (void *)nullValues4; +} +void tst_QMatrix::multiplyFactor2x2() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + QMatrix2x2 m1((const qreal *)m1Values); + + QMatrix2x2 m3; + m3 = m1; + m3 *= factor; + QVERIFY(isSame(m3, (const qreal *)m2Values)); + + QMatrix2x2 m4; + m4 = m1 * factor; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix2x2 m5; + m5 = factor * m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); +} + +// Test matrix multiplication by a factor for 3x3 matrices. +void tst_QMatrix::multiplyFactor3x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<void *>("m2Values"); + + QTest::newRow("null") + << (void *)nullValues3 << (qreal)1.0f << (void *)nullValues3; + + QTest::newRow("double identity") + << (void *)identityValues3 << (qreal)2.0f << (void *)doubleIdentity3; + + static qreal const values[16] = + {1.0f, 2.0f, 3.0f, + 5.0f, 6.0f, 7.0f, + 9.0f, 10.0f, 11.0f}; + static qreal const doubleValues[16] = + {2.0f, 4.0f, 6.0f, + 10.0f, 12.0f, 14.0f, + 18.0f, 20.0f, 22.0f}; + static qreal const negDoubleValues[16] = + {-2.0f, -4.0f, -6.0f, + -10.0f, -12.0f, -14.0f, + -18.0f, -20.0f, -22.0f}; + + QTest::newRow("unique") + << (void *)values << (qreal)2.0f << (void *)doubleValues; + + QTest::newRow("neg") + << (void *)values << (qreal)-2.0f << (void *)negDoubleValues; + + QTest::newRow("zero") + << (void *)values << (qreal)0.0f << (void *)nullValues4; +} +void tst_QMatrix::multiplyFactor3x3() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + QMatrix3x3 m1((const qreal *)m1Values); + + QMatrix3x3 m3; + m3 = m1; + m3 *= factor; + QVERIFY(isSame(m3, (const qreal *)m2Values)); + + QMatrix3x3 m4; + m4 = m1 * factor; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix3x3 m5; + m5 = factor * m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); +} + +// Test matrix multiplication by a factor for 4x4 matrices. +void tst_QMatrix::multiplyFactor4x4_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<void *>("m2Values"); + + QTest::newRow("null") + << (void *)nullValues4 << (qreal)1.0f << (void *)nullValues4; + + QTest::newRow("double identity") + << (void *)identityValues4 << (qreal)2.0f << (void *)doubleIdentity4; + + static qreal const values[16] = + {1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 13.0f, 14.0f, 15.0f, 16.0f}; + static qreal const doubleValues[16] = + {2.0f, 4.0f, 6.0f, 8.0f, + 10.0f, 12.0f, 14.0f, 16.0f, + 18.0f, 20.0f, 22.0f, 24.0f, + 26.0f, 28.0f, 30.0f, 32.0f}; + static qreal const negDoubleValues[16] = + {-2.0f, -4.0f, -6.0f, -8.0f, + -10.0f, -12.0f, -14.0f, -16.0f, + -18.0f, -20.0f, -22.0f, -24.0f, + -26.0f, -28.0f, -30.0f, -32.0f}; + + QTest::newRow("unique") + << (void *)values << (qreal)2.0f << (void *)doubleValues; + + QTest::newRow("neg") + << (void *)values << (qreal)-2.0f << (void *)negDoubleValues; + + QTest::newRow("zero") + << (void *)values << (qreal)0.0f << (void *)nullValues4; +} +void tst_QMatrix::multiplyFactor4x4() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + QMatrix4x4 m1((const qreal *)m1Values); + + QMatrix4x4 m3; + m3 = m1; + m3 *= factor; + QVERIFY(isSame(m3, (const qreal *)m2Values)); + + QMatrix4x4 m4; + m4 = m1 * factor; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix4x4 m5; + m5 = factor * m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); +} + +// Test matrix multiplication by a factor for 4x3 matrices. +void tst_QMatrix::multiplyFactor4x3_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<void *>("m2Values"); + + QTest::newRow("null") + << (void *)nullValues4x3 << (qreal)1.0f << (void *)nullValues4x3; + + QTest::newRow("double identity") + << (void *)identityValues4x3 << (qreal)2.0f << (void *)doubleIdentity4x3; + + static qreal const values[12] = + {1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f}; + static qreal const doubleValues[12] = + {2.0f, 4.0f, 6.0f, 8.0f, + 10.0f, 12.0f, 14.0f, 16.0f, + 18.0f, 20.0f, 22.0f, 24.0f}; + static qreal const negDoubleValues[12] = + {-2.0f, -4.0f, -6.0f, -8.0f, + -10.0f, -12.0f, -14.0f, -16.0f, + -18.0f, -20.0f, -22.0f, -24.0f}; + + QTest::newRow("unique") + << (void *)values << (qreal)2.0f << (void *)doubleValues; + + QTest::newRow("neg") + << (void *)values << (qreal)-2.0f << (void *)negDoubleValues; + + QTest::newRow("zero") + << (void *)values << (qreal)0.0f << (void *)nullValues4x3; +} +void tst_QMatrix::multiplyFactor4x3() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + QMatrix4x3 m1((const qreal *)m1Values); + + QMatrix4x3 m3; + m3 = m1; + m3 *= factor; + QVERIFY(isSame(m3, (const qreal *)m2Values)); + + QMatrix4x3 m4; + m4 = m1 * factor; + QVERIFY(isSame(m4, (const qreal *)m2Values)); + + QMatrix4x3 m5; + m5 = factor * m1; + QVERIFY(isSame(m5, (const qreal *)m2Values)); +} + +// Test matrix division by a factor for 2x2 matrices. +void tst_QMatrix::divideFactor2x2_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor2x2_data(); +} +void tst_QMatrix::divideFactor2x2() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + if (factor == 0.0f) + return; + + QMatrix2x2 m2((const qreal *)m2Values); + + QMatrix2x2 m3; + m3 = m2; + m3 /= factor; + QVERIFY(isSame(m3, (const qreal *)m1Values)); + + QMatrix2x2 m4; + m4 = m2 / factor; + QVERIFY(isSame(m4, (const qreal *)m1Values)); +} + +// Test matrix division by a factor for 3x3 matrices. +void tst_QMatrix::divideFactor3x3_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor3x3_data(); +} +void tst_QMatrix::divideFactor3x3() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + if (factor == 0.0f) + return; + + QMatrix3x3 m2((const qreal *)m2Values); + + QMatrix3x3 m3; + m3 = m2; + m3 /= factor; + QVERIFY(isSame(m3, (const qreal *)m1Values)); + + QMatrix3x3 m4; + m4 = m2 / factor; + QVERIFY(isSame(m4, (const qreal *)m1Values)); +} + +// Test matrix division by a factor for 4x4 matrices. +void tst_QMatrix::divideFactor4x4_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor4x4_data(); +} +void tst_QMatrix::divideFactor4x4() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + if (factor == 0.0f) + return; + + QMatrix4x4 m2((const qreal *)m2Values); + + QMatrix4x4 m3; + m3 = m2; + m3 /= factor; + QVERIFY(isSame(m3, (const qreal *)m1Values)); + + QMatrix4x4 m4; + m4 = m2 / factor; + QVERIFY(isSame(m4, (const qreal *)m1Values)); +} + +// Test matrix division by a factor for 4x3 matrices. +void tst_QMatrix::divideFactor4x3_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor4x3_data(); +} +void tst_QMatrix::divideFactor4x3() +{ + QFETCH(void *, m1Values); + QFETCH(qreal, factor); + QFETCH(void *, m2Values); + + if (factor == 0.0f) + return; + + QMatrix4x3 m2((const qreal *)m2Values); + + QMatrix4x3 m3; + m3 = m2; + m3 /= factor; + QVERIFY(isSame(m3, (const qreal *)m1Values)); + + QMatrix4x3 m4; + m4 = m2 / factor; + QVERIFY(isSame(m4, (const qreal *)m1Values)); +} + +// Test matrix negation for 2x2 matrices. +void tst_QMatrix::negate2x2_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor2x2_data(); +} +void tst_QMatrix::negate2x2() +{ + QFETCH(void *, m1Values); + + const qreal *values = (const qreal *)m1Values; + + QMatrix2x2 m1(values); + + qreal negated[4]; + for (int index = 0; index < 4; ++index) + negated[index] = -values[index]; + + QMatrix2x2 m2; + m2 = -m1; + QVERIFY(isSame(m2, negated)); +} + +// Test matrix negation for 3x3 matrices. +void tst_QMatrix::negate3x3_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor3x3_data(); +} +void tst_QMatrix::negate3x3() +{ + QFETCH(void *, m1Values); + + const qreal *values = (const qreal *)m1Values; + + QMatrix3x3 m1(values); + + qreal negated[9]; + for (int index = 0; index < 9; ++index) + negated[index] = -values[index]; + + QMatrix3x3 m2; + m2 = -m1; + QVERIFY(isSame(m2, negated)); +} + +// Test matrix negation for 4x4 matrices. +void tst_QMatrix::negate4x4_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor4x4_data(); +} +void tst_QMatrix::negate4x4() +{ + QFETCH(void *, m1Values); + + const qreal *values = (const qreal *)m1Values; + + QMatrix4x4 m1(values); + + qreal negated[16]; + for (int index = 0; index < 16; ++index) + negated[index] = -values[index]; + + QMatrix4x4 m2; + m2 = -m1; + QVERIFY(isSame(m2, negated)); +} + +// Test matrix negation for 4x3 matrices. +void tst_QMatrix::negate4x3_data() +{ + // Use the same test cases as the multiplyFactor test. + multiplyFactor4x3_data(); +} +void tst_QMatrix::negate4x3() +{ + QFETCH(void *, m1Values); + + const qreal *values = (const qreal *)m1Values; + + QMatrix4x3 m1(values); + + qreal negated[12]; + for (int index = 0; index < 12; ++index) + negated[index] = -values[index]; + + QMatrix4x3 m2; + m2 = -m1; + QVERIFY(isSame(m2, negated)); +} + +// Matrix inverted. This is a more straight-forward implementation +// of the algorithm at http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q24 +// than the optimized version in the QMatrix4x4 code. Hopefully it is +// easier to verify that this version is the same as the reference. + +struct Matrix3 +{ + qreal v[9]; +}; +struct Matrix4 +{ + qreal v[16]; +}; + +static qreal m3Determinant(const Matrix3& m) +{ + return m.v[0] * (m.v[4] * m.v[8] - m.v[7] * m.v[5]) - + m.v[1] * (m.v[3] * m.v[8] - m.v[6] * m.v[5]) + + m.v[2] * (m.v[3] * m.v[7] - m.v[6] * m.v[4]); +} + +static bool m3Inverse(const Matrix3& min, Matrix3& mout) +{ + qreal det = m3Determinant(min); + if (det == 0.0f) + return false; + mout.v[0] = (min.v[4] * min.v[8] - min.v[5] * min.v[7]) / det; + mout.v[1] = -(min.v[1] * min.v[8] - min.v[2] * min.v[7]) / det; + mout.v[2] = (min.v[1] * min.v[5] - min.v[4] * min.v[2]) / det; + mout.v[3] = -(min.v[3] * min.v[8] - min.v[5] * min.v[6]) / det; + mout.v[4] = (min.v[0] * min.v[8] - min.v[6] * min.v[2]) / det; + mout.v[5] = -(min.v[0] * min.v[5] - min.v[3] * min.v[2]) / det; + mout.v[6] = (min.v[3] * min.v[7] - min.v[6] * min.v[4]) / det; + mout.v[7] = -(min.v[0] * min.v[7] - min.v[6] * min.v[1]) / det; + mout.v[8] = (min.v[0] * min.v[4] - min.v[1] * min.v[3]) / det; + return true; +} + +static void m3Transpose(Matrix3& m) +{ + qSwap(m.v[1], m.v[3]); + qSwap(m.v[2], m.v[6]); + qSwap(m.v[5], m.v[7]); +} + +static void m4Submatrix(const Matrix4& min, Matrix3& mout, int i, int j) +{ + for (int di = 0; di < 3; ++di) { + for (int dj = 0; dj < 3; ++dj) { + int si = di + ((di >= i) ? 1 : 0); + int sj = dj + ((dj >= j) ? 1 : 0); + mout.v[di * 3 + dj] = min.v[si * 4 + sj]; + } + } +} + +static qreal m4Determinant(const Matrix4& m) +{ + qreal det; + qreal result = 0.0f; + qreal i = 1.0f; + Matrix3 msub; + for (int n = 0; n < 4; ++n, i *= -1.0f) { + m4Submatrix(m, msub, 0, n); + det = m3Determinant(msub); + result += m.v[n] * det * i; + } + return result; +} + +static void m4Inverse(const Matrix4& min, Matrix4& mout) +{ + qreal det = m4Determinant(min); + Matrix3 msub; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + qreal sign = 1.0f - ((i + j) % 2) * 2.0f; + m4Submatrix(min, msub, i, j); + mout.v[i + j * 4] = (m3Determinant(msub) * sign) / det; + } + } +} + +// Test matrix inverted for 4x4 matrices. +void tst_QMatrix::inverted4x4_data() +{ + QTest::addColumn<void *>("m1Values"); + QTest::addColumn<void *>("m2Values"); + QTest::addColumn<bool>("invertible"); + + QTest::newRow("null") + << (void *)nullValues4 << (void *)identityValues4 << false; + + QTest::newRow("identity") + << (void *)identityValues4 << (void *)identityValues4 << true; + + QTest::newRow("unique") + << (void *)uniqueValues4 << (void *)identityValues4 << false; + + static Matrix4 const invertible = { + {5.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 6.0f, 0.0f, 3.0f, + 0.0f, 0.0f, 7.0f, 4.0f, + 0.0f, 0.0f, 0.0f, 1.0f} + }; + static Matrix4 inverted; + m4Inverse(invertible, inverted); + + QTest::newRow("invertible") + << (void *)invertible.v << (void *)inverted.v << true; + + static Matrix4 const translate = { + {1.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 1.0f, 0.0f, 3.0f, + 0.0f, 0.0f, 1.0f, 4.0f, + 0.0f, 0.0f, 0.0f, 1.0f} + }; + static Matrix4 const inverseTranslate = { + {1.0f, 0.0f, 0.0f, -2.0f, + 0.0f, 1.0f, 0.0f, -3.0f, + 0.0f, 0.0f, 1.0f, -4.0f, + 0.0f, 0.0f, 0.0f, 1.0f} + }; + + QTest::newRow("translate") + << (void *)translate.v << (void *)inverseTranslate.v << true; +} +void tst_QMatrix::inverted4x4() +{ + QFETCH(void *, m1Values); + QFETCH(void *, m2Values); + QFETCH(bool, invertible); + + QMatrix4x4 m1((const qreal *)m1Values); + + if (invertible) + QVERIFY(m1.determinant() != 0.0f); + else + QVERIFY(m1.determinant() == 0.0f); + + Matrix4 m1alt; + memcpy(m1alt.v, (const qreal *)m1Values, sizeof(m1alt.v)); + + QCOMPARE((float)(m1.determinant()), (float)(m4Determinant(m1alt))); + + QMatrix4x4 m2; + bool inv; + m2 = m1.inverted(&inv); + QVERIFY(isSame(m2, (const qreal *)m2Values)); + + if (invertible) { + QVERIFY(inv); + + Matrix4 m2alt; + m4Inverse(m1alt, m2alt); + QVERIFY(isSame(m2, m2alt.v)); + + QMatrix4x4 m3; + m3 = m1 * m2; + QVERIFY(isIdentity(m3)); + + QMatrix4x4 m4; + m4 = m2 * m1; + QVERIFY(isIdentity(m4)); + } else { + QVERIFY(!inv); + } + + // Test again, after inferring the special matrix type. + m1.inferSpecialType(); + m2 = m1.inverted(&inv); + QVERIFY(isSame(m2, (const qreal *)m2Values)); + QCOMPARE(inv, invertible); +} + +void tst_QMatrix::orthonormalInverse4x4() +{ + QMatrix4x4 m1; + QVERIFY(matrixFuzzyCompare(m1.inverted(), m1)); + + QMatrix4x4 m2; + m2.rotate(45.0, 1.0, 0.0, 0.0); + m2.translate(10.0, 0.0, 0.0); + + // Use inferSpecialType() to drop the internal flags that + // mark the matrix as orthonormal. This will force inverted() + // to compute m3.inverted() the long way. We can then compare + // the result to what the faster algorithm produces on m2. + QMatrix4x4 m3 = m2; + m3.inferSpecialType(); + bool invertible; + QVERIFY(matrixFuzzyCompare(m2.inverted(&invertible), m3.inverted())); + QVERIFY(invertible); + + QMatrix4x4 m4; + m4.rotate(45.0, 0.0, 1.0, 0.0); + QMatrix4x4 m5 = m4; + m5.inferSpecialType(); + QVERIFY(matrixFuzzyCompare(m4.inverted(), m5.inverted())); + + QMatrix4x4 m6; + m1.rotate(88, 0.0, 0.0, 1.0); + m1.translate(-20.0, 20.0, 15.0); + m1.rotate(25, 1.0, 0.0, 0.0); + QMatrix4x4 m7 = m6; + m7.inferSpecialType(); + QVERIFY(matrixFuzzyCompare(m6.inverted(), m7.inverted())); +} + +// Test the generation and use of 4x4 scale matrices. +void tst_QMatrix::scale4x4_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<void *>("resultValues"); + + static const qreal nullScale[] = + {0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (void *)nullScale; + + QTest::newRow("identity") + << (qreal)1.0f << (qreal)1.0f << (qreal)1.0f << (void *)identityValues4; + + static const qreal doubleScale[] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 2.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("double") + << (qreal)2.0f << (qreal)2.0f << (qreal)2.0f << (void *)doubleScale; + + static const qreal complexScale[] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 11.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -6.5f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("complex") + << (qreal)2.0f << (qreal)11.0f << (qreal)-6.5f << (void *)complexScale; + + static const qreal complexScale2D[] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, -11.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("complex2D") + << (qreal)2.0f << (qreal)-11.0f << (qreal)1.0f << (void *)complexScale2D; +} +void tst_QMatrix::scale4x4() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(void *, resultValues); + + QMatrix4x4 result((const qreal *)resultValues); + + QMatrix4x4 m1; + m1.scale(QVector3D(x, y, z)); + QVERIFY(isSame(m1, (const qreal *)resultValues)); + + QMatrix4x4 m2; + m2.scale(x, y, z); + QVERIFY(isSame(m2, (const qreal *)resultValues)); + + if (z == 1.0f) { + QMatrix4x4 m2b; + m2b.scale(x, y); + QVERIFY(m2b == m2); + } + + QVector3D v1(2.0f, 3.0f, -4.0f); + QVector3D v2 = m1 * v1; + QCOMPARE(v2.x(), (qreal)(2.0f * x)); + QCOMPARE(v2.y(), (qreal)(3.0f * y)); + QCOMPARE(v2.z(), (qreal)(-4.0f * z)); + + v2 = v1 * m1; + QCOMPARE(v2.x(), (qreal)(2.0f * x)); + QCOMPARE(v2.y(), (qreal)(3.0f * y)); + QCOMPARE(v2.z(), (qreal)(-4.0f * z)); + + QVector4D v3(2.0f, 3.0f, -4.0f, 34.0f); + QVector4D v4 = m1 * v3; + QCOMPARE(v4.x(), (qreal)(2.0f * x)); + QCOMPARE(v4.y(), (qreal)(3.0f * y)); + QCOMPARE(v4.z(), (qreal)(-4.0f * z)); + QCOMPARE(v4.w(), (qreal)34.0f); + + v4 = v3 * m1; + QCOMPARE(v4.x(), (qreal)(2.0f * x)); + QCOMPARE(v4.y(), (qreal)(3.0f * y)); + QCOMPARE(v4.z(), (qreal)(-4.0f * z)); + QCOMPARE(v4.w(), (qreal)34.0f); + + QPoint p1(2, 3); + QPoint p2 = m1 * p1; + QCOMPARE(p2.x(), (int)(2.0f * x)); + QCOMPARE(p2.y(), (int)(3.0f * y)); + + p2 = p1 * m1; + QCOMPARE(p2.x(), (int)(2.0f * x)); + QCOMPARE(p2.y(), (int)(3.0f * y)); + + QPointF p3(2.0f, 3.0f); + QPointF p4 = m1 * p3; + QCOMPARE(p4.x(), (qreal)(2.0f * x)); + QCOMPARE(p4.y(), (qreal)(3.0f * y)); + + p4 = p3 * m1; + QCOMPARE(p4.x(), (qreal)(2.0f * x)); + QCOMPARE(p4.y(), (qreal)(3.0f * y)); + + QMatrix4x4 m3(uniqueValues4); + QMatrix4x4 m4(m3); + m4.scale(x, y, z); + QVERIFY(m4 == m3 * m1); + + if (x == y && y == z) { + QMatrix4x4 m5; + m5.scale(x); + QVERIFY(isSame(m5, (const qreal *)resultValues)); + } + + if (z == 1.0f) { + QMatrix4x4 m4b(m3); + m4b.scale(x, y); + QVERIFY(m4b == m4); + } + + // Test coverage when the special matrix type is unknown. + + QMatrix4x4 m6; + m6(0, 0) = 1.0f; + m6.scale(QVector3D(x, y, z)); + QVERIFY(isSame(m6, (const qreal *)resultValues)); + + QMatrix4x4 m7; + m7(0, 0) = 1.0f; + m7.scale(x, y, z); + QVERIFY(isSame(m7, (const qreal *)resultValues)); + + if (x == y && y == z) { + QMatrix4x4 m8; + m8(0, 0) = 1.0f; + m8.scale(x); + QVERIFY(isSame(m8, (const qreal *)resultValues)); + + m8.inferSpecialType(); + m8.scale(1.0f); + QVERIFY(isSame(m8, (const qreal *)resultValues)); + + QMatrix4x4 m9; + m9.translate(0.0f, 0.0f, 0.0f); + m9.scale(x); + QVERIFY(isSame(m9, (const qreal *)resultValues)); + } +} + +// Test the generation and use of 4x4 translation matrices. +void tst_QMatrix::translate4x4_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<void *>("resultValues"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (void *)identityValues4; + + static const qreal identityTranslate[] = + {1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, 1.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("identity") + << (qreal)1.0f << (qreal)1.0f << (qreal)1.0f << (void *)identityTranslate; + + static const qreal complexTranslate[] = + {1.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 1.0f, 0.0f, 11.0f, + 0.0f, 0.0f, 1.0f, -6.5f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("complex") + << (qreal)2.0f << (qreal)11.0f << (qreal)-6.5f << (void *)complexTranslate; + + static const qreal complexTranslate2D[] = + {1.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 1.0f, 0.0f, -11.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("complex2D") + << (qreal)2.0f << (qreal)-11.0f << (qreal)0.0f << (void *)complexTranslate2D; +} +void tst_QMatrix::translate4x4() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(void *, resultValues); + + QMatrix4x4 result((const qreal *)resultValues); + + QMatrix4x4 m1; + m1.translate(QVector3D(x, y, z)); + QVERIFY(isSame(m1, (const qreal *)resultValues)); + + QMatrix4x4 m2; + m2.translate(x, y, z); + QVERIFY(isSame(m2, (const qreal *)resultValues)); + + if (z == 0.0f) { + QMatrix4x4 m2b; + m2b.translate(x, y); + QVERIFY(m2b == m2); + } + + QVector3D v1(2.0f, 3.0f, -4.0f); + QVector3D v2 = m1 * v1; + QCOMPARE(v2.x(), (qreal)(2.0f + x)); + QCOMPARE(v2.y(), (qreal)(3.0f + y)); + QCOMPARE(v2.z(), (qreal)(-4.0f + z)); + + QVector4D v3(2.0f, 3.0f, -4.0f, 1.0f); + QVector4D v4 = m1 * v3; + QCOMPARE(v4.x(), (qreal)(2.0f + x)); + QCOMPARE(v4.y(), (qreal)(3.0f + y)); + QCOMPARE(v4.z(), (qreal)(-4.0f + z)); + QCOMPARE(v4.w(), (qreal)1.0f); + + QVector4D v5(2.0f, 3.0f, -4.0f, 34.0f); + QVector4D v6 = m1 * v5; + QCOMPARE(v6.x(), (qreal)(2.0f + x * 34.0f)); + QCOMPARE(v6.y(), (qreal)(3.0f + y * 34.0f)); + QCOMPARE(v6.z(), (qreal)(-4.0f + z * 34.0f)); + QCOMPARE(v6.w(), (qreal)34.0f); + + QPoint p1(2, 3); + QPoint p2 = m1 * p1; + QCOMPARE(p2.x(), (int)(2.0f + x)); + QCOMPARE(p2.y(), (int)(3.0f + y)); + + QPointF p3(2.0f, 3.0f); + QPointF p4 = m1 * p3; + QCOMPARE(p4.x(), (qreal)(2.0f + x)); + QCOMPARE(p4.y(), (qreal)(3.0f + y)); + + QMatrix4x4 m3(uniqueValues4); + QMatrix4x4 m4(m3); + m4.translate(x, y, z); + QVERIFY(m4 == m3 * m1); + + if (z == 0.0f) { + QMatrix4x4 m4b(m3); + m4b.translate(x, y); + QVERIFY(m4b == m4); + } +} + +// Test the generation and use of 4x4 rotation matrices. +void tst_QMatrix::rotate4x4_data() +{ + QTest::addColumn<qreal>("angle"); + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<void *>("resultValues"); + + static const qreal nullRotate[] = + {0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("null") + << (qreal)90.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (void *)nullRotate; + + static const qreal noRotate[] = + {1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("zerodegrees") + << (qreal)0.0f + << (qreal)2.0f << (qreal)3.0f << (qreal)-4.0f + << (void *)noRotate; + + static const qreal xRotate[] = + {1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("xrotate") + << (qreal)90.0f + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (void *)xRotate; + + static const qreal xRotateNeg[] = + {1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("-xrotate") + << (qreal)90.0f + << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f + << (void *)xRotateNeg; + + static const qreal yRotate[] = + {0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("yrotate") + << (qreal)90.0f + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (void *)yRotate; + + static const qreal yRotateNeg[] = + {0.0f, 0.0f, -1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("-yrotate") + << (qreal)90.0f + << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f + << (void *)yRotateNeg; + + static const qreal zRotate[] = + {0.0f, -1.0f, 0.0f, 0.0f, + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("zrotate") + << (qreal)90.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (void *)zRotate; + + static const qreal zRotateNeg[] = + {0.0f, 1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + QTest::newRow("-zrotate") + << (qreal)90.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f + << (void *)zRotateNeg; + + // Algorithm from http://en.wikipedia.org/wiki/Rotation_matrix. + // Deliberately different from the one in the code for cross-checking. + static qreal complexRotate[16]; + qreal x = 1.0f; + qreal y = 2.0f; + qreal z = -6.0f; + qreal angle = -45.0f; + qreal c = qCos(angle * M_PI / 180.0f); + qreal s = qSin(angle * M_PI / 180.0f); + qreal len = qSqrt(x * x + y * y + z * z); + qreal xu = x / len; + qreal yu = y / len; + qreal zu = z / len; + complexRotate[0] = (qreal)((1 - xu * xu) * c + xu * xu); + complexRotate[1] = (qreal)(-zu * s - xu * yu * c + xu * yu); + complexRotate[2] = (qreal)(yu * s - xu * zu * c + xu * zu); + complexRotate[3] = 0; + complexRotate[4] = (qreal)(zu * s - xu * yu * c + xu * yu); + complexRotate[5] = (qreal)((1 - yu * yu) * c + yu * yu); + complexRotate[6] = (qreal)(-xu * s - yu * zu * c + yu * zu); + complexRotate[7] = 0; + complexRotate[8] = (qreal)(-yu * s - xu * zu * c + xu * zu); + complexRotate[9] = (qreal)(xu * s - yu * zu * c + yu * zu); + complexRotate[10] = (qreal)((1 - zu * zu) * c + zu * zu); + complexRotate[11] = 0; + complexRotate[12] = 0; + complexRotate[13] = 0; + complexRotate[14] = 0; + complexRotate[15] = 1; + + QTest::newRow("complex") + << (qreal)angle + << (qreal)x << (qreal)y << (qreal)z + << (void *)complexRotate; +} +void tst_QMatrix::rotate4x4() +{ + QFETCH(qreal, angle); + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(void *, resultValues); + + QMatrix4x4 m1; + m1.rotate(angle, QVector3D(x, y, z)); + QVERIFY(isSame(m1, (const qreal *)resultValues)); + + QMatrix4x4 m2; + m2.rotate(angle, x, y, z); + QVERIFY(isSame(m2, (const qreal *)resultValues)); + + QMatrix4x4 m3(uniqueValues4); + QMatrix4x4 m4(m3); + m4.rotate(angle, x, y, z); + QVERIFY(matrixFuzzyCompare(m4, m3 * m1)); + + // Null vectors don't make sense for quaternion rotations. + if (x != 0 || y != 0 || z != 0) { + QMatrix4x4 m5; + m5.rotate(QQuaternion::fromAxisAndAngle(QVector3D(x, y, z), angle)); + QVERIFY(isSame(m5, (const qreal *)resultValues)); + } + +#define ROTATE4(xin,yin,zin,win,xout,yout,zout,wout) \ + do { \ + xout = ((const qreal *)resultValues)[0] * xin + \ + ((const qreal *)resultValues)[1] * yin + \ + ((const qreal *)resultValues)[2] * zin + \ + ((const qreal *)resultValues)[3] * win; \ + yout = ((const qreal *)resultValues)[4] * xin + \ + ((const qreal *)resultValues)[5] * yin + \ + ((const qreal *)resultValues)[6] * zin + \ + ((const qreal *)resultValues)[7] * win; \ + zout = ((const qreal *)resultValues)[8] * xin + \ + ((const qreal *)resultValues)[9] * yin + \ + ((const qreal *)resultValues)[10] * zin + \ + ((const qreal *)resultValues)[11] * win; \ + wout = ((const qreal *)resultValues)[12] * xin + \ + ((const qreal *)resultValues)[13] * yin + \ + ((const qreal *)resultValues)[14] * zin + \ + ((const qreal *)resultValues)[15] * win; \ + } while (0) + + // Rotate various test vectors using the straight-forward approach. + qreal v1x, v1y, v1z, v1w; + ROTATE4(2.0f, 3.0f, -4.0f, 1.0f, v1x, v1y, v1z, v1w); + v1x /= v1w; + v1y /= v1w; + v1z /= v1w; + qreal v3x, v3y, v3z, v3w; + ROTATE4(2.0f, 3.0f, -4.0f, 1.0f, v3x, v3y, v3z, v3w); + qreal v5x, v5y, v5z, v5w; + ROTATE4(2.0f, 3.0f, -4.0f, 34.0f, v5x, v5y, v5z, v5w); + qreal p1x, p1y, p1z, p1w; + ROTATE4(2.0f, 3.0f, 0.0f, 1.0f, p1x, p1y, p1z, p1w); + p1x /= p1w; + p1y /= p1w; + p1z /= p1w; + + QVector3D v1(2.0f, 3.0f, -4.0f); + QVector3D v2 = m1 * v1; + QVERIFY(fuzzyCompare(v2.x(), v1x)); + QVERIFY(fuzzyCompare(v2.y(), v1y)); + QVERIFY(fuzzyCompare(v2.z(), v1z)); + + QVector4D v3(2.0f, 3.0f, -4.0f, 1.0f); + QVector4D v4 = m1 * v3; + QVERIFY(fuzzyCompare(v4.x(), v3x)); + QVERIFY(fuzzyCompare(v4.y(), v3y)); + QVERIFY(fuzzyCompare(v4.z(), v3z)); + QVERIFY(fuzzyCompare(v4.w(), v3w)); + + QVector4D v5(2.0f, 3.0f, -4.0f, 34.0f); + QVector4D v6 = m1 * v5; + QVERIFY(fuzzyCompare(v6.x(), v5x)); + QVERIFY(fuzzyCompare(v6.y(), v5y)); + QVERIFY(fuzzyCompare(v6.z(), v5z)); + QVERIFY(fuzzyCompare(v6.w(), v5w)); + + QPoint p1(2, 3); + QPoint p2 = m1 * p1; + QCOMPARE(p2.x(), qRound(p1x)); + QCOMPARE(p2.y(), qRound(p1y)); + + QPointF p3(2.0f, 3.0f); + QPointF p4 = m1 * p3; + QVERIFY(fuzzyCompare((float)(p4.x()), p1x)); + QVERIFY(fuzzyCompare((float)(p4.y()), p1y)); + + if (x != 0 || y != 0 || z != 0) { + QQuaternion q = QQuaternion::fromAxisAndAngle(QVector3D(x, y, z), angle); + QVector3D vq = q.rotateVector(v1); + QVERIFY(fuzzyCompare(vq.x(), v1x)); + QVERIFY(fuzzyCompare(vq.y(), v1y)); + QVERIFY(fuzzyCompare(vq.z(), v1z)); + } +} + +static bool isSame(const QMatrix3x3& m1, const Matrix3& m2) +{ + for (int row = 0; row < 3; ++row) { + for (int col = 0; col < 3; ++col) { + if (!fuzzyCompare(m1(row, col), m2.v[row * 3 + col])) + return false; + } + } + return true; +} + +// Test the computation of normal matrices from 4x4 transformation matrices. +void tst_QMatrix::normalMatrix_data() +{ + QTest::addColumn<void *>("mValues"); + + QTest::newRow("identity") + << (void *)identityValues4; + QTest::newRow("unique") + << (void *)uniqueValues4; // Not invertible because determinant == 0. + + static qreal const translateValues[16] = + {1.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 1.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 1.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const scaleValues[16] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 7.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 9.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const bothValues[16] = + {2.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 7.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 9.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const nullScaleValues1[16] = + {0.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 7.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 9.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const nullScaleValues2[16] = + {2.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 0.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 9.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const nullScaleValues3[16] = + {2.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 7.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 0.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + + QTest::newRow("translate") << (void *)translateValues; + QTest::newRow("scale") << (void *)scaleValues; + QTest::newRow("both") << (void *)bothValues; + QTest::newRow("null scale 1") << (void *)nullScaleValues1; + QTest::newRow("null scale 2") << (void *)nullScaleValues2; + QTest::newRow("null scale 3") << (void *)nullScaleValues3; +} +void tst_QMatrix::normalMatrix() +{ + QFETCH(void *, mValues); + const qreal *values = (const qreal *)mValues; + + // Compute the expected answer the long way. + Matrix3 min; + Matrix3 answer; + min.v[0] = values[0]; + min.v[1] = values[1]; + min.v[2] = values[2]; + min.v[3] = values[4]; + min.v[4] = values[5]; + min.v[5] = values[6]; + min.v[6] = values[8]; + min.v[7] = values[9]; + min.v[8] = values[10]; + bool invertible = m3Inverse(min, answer); + m3Transpose(answer); + + // Perform the test. + QMatrix4x4 m1(values); + QMatrix3x3 n1 = m1.normalMatrix(); + + if (invertible) + QVERIFY(::isSame(n1, answer)); + else + QVERIFY(isIdentity(n1)); + + // Perform the test again, after inferring special matrix types. + // This tests the optimized paths in the normalMatrix() function. + m1.inferSpecialType(); + n1 = m1.normalMatrix(); + + if (invertible) + QVERIFY(::isSame(n1, answer)); + else + QVERIFY(isIdentity(n1)); +} + +// Test optimized transformations on 4x4 matrices. +void tst_QMatrix::optimizedTransforms() +{ + static qreal const translateValues[16] = + {1.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 1.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 1.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const translateDoubleValues[16] = + {1.0f, 0.0f, 0.0f, 8.0f, + 0.0f, 1.0f, 0.0f, 10.0f, + 0.0f, 0.0f, 1.0f, -6.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const scaleValues[16] = + {2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 7.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 9.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const scaleDoubleValues[16] = + {4.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 49.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 81.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const bothValues[16] = + {2.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 7.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 9.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const bothReverseValues[16] = + {2.0f, 0.0f, 0.0f, 4.0f * 2.0f, + 0.0f, 7.0f, 0.0f, 5.0f * 7.0f, + 0.0f, 0.0f, 9.0f, -3.0f * 9.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const bothThenTranslateValues[16] = + {2.0f, 0.0f, 0.0f, 4.0f + 2.0f * 4.0f, + 0.0f, 7.0f, 0.0f, 5.0f + 7.0f * 5.0f, + 0.0f, 0.0f, 9.0f, -3.0f + 9.0f * -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + static qreal const bothThenScaleValues[16] = + {4.0f, 0.0f, 0.0f, 4.0f, + 0.0f, 49.0f, 0.0f, 5.0f, + 0.0f, 0.0f, 81.0f, -3.0f, + 0.0f, 0.0f, 0.0f, 1.0f}; + + QMatrix4x4 translate(translateValues); + QMatrix4x4 scale(scaleValues); + QMatrix4x4 both(bothValues); + + QMatrix4x4 m1; + m1.translate(4.0f, 5.0f, -3.0f); + QVERIFY(isSame(m1, translateValues)); + m1.translate(4.0f, 5.0f, -3.0f); + QVERIFY(isSame(m1, translateDoubleValues)); + + QMatrix4x4 m2; + m2.translate(QVector3D(4.0f, 5.0f, -3.0f)); + QVERIFY(isSame(m2, translateValues)); + m2.translate(QVector3D(4.0f, 5.0f, -3.0f)); + QVERIFY(isSame(m2, translateDoubleValues)); + + QMatrix4x4 m3; + m3.scale(2.0f, 7.0f, 9.0f); + QVERIFY(isSame(m3, scaleValues)); + m3.scale(2.0f, 7.0f, 9.0f); + QVERIFY(isSame(m3, scaleDoubleValues)); + + QMatrix4x4 m4; + m4.scale(QVector3D(2.0f, 7.0f, 9.0f)); + QVERIFY(isSame(m4, scaleValues)); + m4.scale(QVector3D(2.0f, 7.0f, 9.0f)); + QVERIFY(isSame(m4, scaleDoubleValues)); + + QMatrix4x4 m5; + m5.translate(4.0f, 5.0f, -3.0f); + m5.scale(2.0f, 7.0f, 9.0f); + QVERIFY(isSame(m5, bothValues)); + m5.translate(4.0f, 5.0f, -3.0f); + QVERIFY(isSame(m5, bothThenTranslateValues)); + + QMatrix4x4 m6; + m6.translate(QVector3D(4.0f, 5.0f, -3.0f)); + m6.scale(QVector3D(2.0f, 7.0f, 9.0f)); + QVERIFY(isSame(m6, bothValues)); + m6.translate(QVector3D(4.0f, 5.0f, -3.0f)); + QVERIFY(isSame(m6, bothThenTranslateValues)); + + QMatrix4x4 m7; + m7.scale(2.0f, 7.0f, 9.0f); + m7.translate(4.0f, 5.0f, -3.0f); + QVERIFY(isSame(m7, bothReverseValues)); + + QMatrix4x4 m8; + m8.scale(QVector3D(2.0f, 7.0f, 9.0f)); + m8.translate(QVector3D(4.0f, 5.0f, -3.0f)); + QVERIFY(isSame(m8, bothReverseValues)); + + QMatrix4x4 m9; + m9.translate(4.0f, 5.0f, -3.0f); + m9.scale(2.0f, 7.0f, 9.0f); + QVERIFY(isSame(m9, bothValues)); + m9.scale(2.0f, 7.0f, 9.0f); + QVERIFY(isSame(m9, bothThenScaleValues)); + + QMatrix4x4 m10; + m10.translate(QVector3D(4.0f, 5.0f, -3.0f)); + m10.scale(QVector3D(2.0f, 7.0f, 9.0f)); + QVERIFY(isSame(m10, bothValues)); + m10.scale(QVector3D(2.0f, 7.0f, 9.0f)); + QVERIFY(isSame(m10, bothThenScaleValues)); +} + +// Test orthographic projections. +void tst_QMatrix::ortho() +{ + QMatrix4x4 m1; + m1.ortho(QRect(0, 0, 300, 150)); + QPointF p1 = m1 * QPointF(0, 0); + QPointF p2 = m1 * QPointF(300, 0); + QPointF p3 = m1 * QPointF(0, 150); + QPointF p4 = m1 * QPointF(300, 150); + QVector3D p5 = m1 * QVector3D(300, 150, 1); + QVERIFY(fuzzyCompare(p1.x(), -1.0)); + QVERIFY(fuzzyCompare(p1.y(), 1.0)); + QVERIFY(fuzzyCompare(p2.x(), 1.0)); + QVERIFY(fuzzyCompare(p2.y(), 1.0)); + QVERIFY(fuzzyCompare(p3.x(), -1.0)); + QVERIFY(fuzzyCompare(p3.y(), -1.0)); + QVERIFY(fuzzyCompare(p4.x(), 1.0)); + QVERIFY(fuzzyCompare(p4.y(), -1.0)); + QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); + QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); + QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); + + QMatrix4x4 m2; + m2.ortho(QRectF(0, 0, 300, 150)); + p1 = m2 * QPointF(0, 0); + p2 = m2 * QPointF(300, 0); + p3 = m2 * QPointF(0, 150); + p4 = m2 * QPointF(300, 150); + p5 = m2 * QVector3D(300, 150, 1); + QVERIFY(fuzzyCompare(p1.x(), -1.0)); + QVERIFY(fuzzyCompare(p1.y(), 1.0)); + QVERIFY(fuzzyCompare(p2.x(), 1.0)); + QVERIFY(fuzzyCompare(p2.y(), 1.0)); + QVERIFY(fuzzyCompare(p3.x(), -1.0)); + QVERIFY(fuzzyCompare(p3.y(), -1.0)); + QVERIFY(fuzzyCompare(p4.x(), 1.0)); + QVERIFY(fuzzyCompare(p4.y(), -1.0)); + QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); + QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); + QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); + + QMatrix4x4 m3; + m3.ortho(0, 300, 150, 0, -1, 1); + p1 = m3 * QPointF(0, 0); + p2 = m3 * QPointF(300, 0); + p3 = m3 * QPointF(0, 150); + p4 = m3 * QPointF(300, 150); + p5 = m3 * QVector3D(300, 150, 1); + QVERIFY(fuzzyCompare(p1.x(), -1.0)); + QVERIFY(fuzzyCompare(p1.y(), 1.0)); + QVERIFY(fuzzyCompare(p2.x(), 1.0)); + QVERIFY(fuzzyCompare(p2.y(), 1.0)); + QVERIFY(fuzzyCompare(p3.x(), -1.0)); + QVERIFY(fuzzyCompare(p3.y(), -1.0)); + QVERIFY(fuzzyCompare(p4.x(), 1.0)); + QVERIFY(fuzzyCompare(p4.y(), -1.0)); + QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); + QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); + QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); + + QMatrix4x4 m4; + m4.ortho(0, 300, 150, 0, -2, 3); + p1 = m4 * QPointF(0, 0); + p2 = m4 * QPointF(300, 0); + p3 = m4 * QPointF(0, 150); + p4 = m4 * QPointF(300, 150); + p5 = m4 * QVector3D(300, 150, 1); + QVERIFY(fuzzyCompare(p1.x(), -1.0)); + QVERIFY(fuzzyCompare(p1.y(), 1.0)); + QVERIFY(fuzzyCompare(p2.x(), 1.0)); + QVERIFY(fuzzyCompare(p2.y(), 1.0)); + QVERIFY(fuzzyCompare(p3.x(), -1.0)); + QVERIFY(fuzzyCompare(p3.y(), -1.0)); + QVERIFY(fuzzyCompare(p4.x(), 1.0)); + QVERIFY(fuzzyCompare(p4.y(), -1.0)); + QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); + QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); + QVERIFY(fuzzyCompare(p5.z(), (qreal)-0.6)); + + // An empty view volume should leave the matrix alone. + QMatrix4x4 m5; + m5.ortho(0, 0, 150, 0, -2, 3); + QVERIFY(m5.isIdentity()); + m5.ortho(0, 300, 150, 150, -2, 3); + QVERIFY(m5.isIdentity()); + m5.ortho(0, 300, 150, 0, 2, 2); + QVERIFY(m5.isIdentity()); +} + +// Test perspective frustum projections. +void tst_QMatrix::frustum() +{ + QMatrix4x4 m1; + m1.frustum(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); + QVector3D p1 = m1 * QVector3D(-1.0f, -1.0f, 1.0f); + QVector3D p2 = m1 * QVector3D(1.0f, -1.0f, 1.0f); + QVector3D p3 = m1 * QVector3D(-1.0f, 1.0f, 1.0f); + QVector3D p4 = m1 * QVector3D(1.0f, 1.0f, 1.0f); + QVector3D p5 = m1 * QVector3D(0.0f, 0.0f, 2.0f); + QVERIFY(fuzzyCompare(p1.x(), -1.0f)); + QVERIFY(fuzzyCompare(p1.y(), -1.0f)); + QVERIFY(fuzzyCompare(p1.z(), -1.0f)); + QVERIFY(fuzzyCompare(p2.x(), 1.0f)); + QVERIFY(fuzzyCompare(p2.y(), -1.0f)); + QVERIFY(fuzzyCompare(p2.z(), -1.0f)); + QVERIFY(fuzzyCompare(p3.x(), -1.0f)); + QVERIFY(fuzzyCompare(p3.y(), 1.0f)); + QVERIFY(fuzzyCompare(p3.z(), -1.0f)); + QVERIFY(fuzzyCompare(p4.x(), 1.0f)); + QVERIFY(fuzzyCompare(p4.y(), 1.0f)); + QVERIFY(fuzzyCompare(p4.z(), -1.0f)); + QVERIFY(fuzzyCompare(p5.x(), 0.0f)); + QVERIFY(fuzzyCompare(p5.y(), 0.0f)); + QVERIFY(fuzzyCompare(p5.z(), -0.5f)); + + // An empty view volume should leave the matrix alone. + QMatrix4x4 m5; + m5.frustum(0, 0, 150, 0, -2, 3); + QVERIFY(m5.isIdentity()); + m5.frustum(0, 300, 150, 150, -2, 3); + QVERIFY(m5.isIdentity()); + m5.frustum(0, 300, 150, 0, 2, 2); + QVERIFY(m5.isIdentity()); +} + +// Test perspective field-of-view projections. +void tst_QMatrix::perspective() +{ + QMatrix4x4 m1; + m1.perspective(45.0f, 1.0f, -1.0f, 1.0f); + QVector3D p1 = m1 * QVector3D(-1.0f, -1.0f, 1.0f); + QVector3D p2 = m1 * QVector3D(1.0f, -1.0f, 1.0f); + QVector3D p3 = m1 * QVector3D(-1.0f, 1.0f, 1.0f); + QVector3D p4 = m1 * QVector3D(1.0f, 1.0f, 1.0f); + QVector3D p5 = m1 * QVector3D(0.0f, 0.0f, 2.0f); + QVERIFY(fuzzyCompare(p1.x(), 2.41421)); + QVERIFY(fuzzyCompare(p1.y(), 2.41421)); + QVERIFY(fuzzyCompare(p1.z(), -1)); + QVERIFY(fuzzyCompare(p2.x(), -2.41421)); + QVERIFY(fuzzyCompare(p2.y(), 2.41421)); + QVERIFY(fuzzyCompare(p2.z(), -1.0f)); + QVERIFY(fuzzyCompare(p3.x(), 2.41421)); + QVERIFY(fuzzyCompare(p3.y(), -2.41421)); + QVERIFY(fuzzyCompare(p3.z(), -1.0f)); + QVERIFY(fuzzyCompare(p4.x(), -2.41421)); + QVERIFY(fuzzyCompare(p4.y(), -2.41421)); + QVERIFY(fuzzyCompare(p4.z(), -1.0f)); + QVERIFY(fuzzyCompare(p5.x(), 0.0f)); + QVERIFY(fuzzyCompare(p5.y(), 0.0f)); + QVERIFY(fuzzyCompare(p5.z(), -0.5f)); + + // An empty view volume should leave the matrix alone. + QMatrix4x4 m5; + m5.perspective(45.0f, 1.0f, 0.0f, 0.0f); + QVERIFY(m5.isIdentity()); + m5.perspective(45.0f, 0.0f, -1.0f, 1.0f); + QVERIFY(m5.isIdentity()); + m5.perspective(0.0f, 1.0f, -1.0f, 1.0f); + QVERIFY(m5.isIdentity()); +} + +// Test left-handed vs right-handed coordinate flipping. +void tst_QMatrix::flipCoordinates() +{ + QMatrix4x4 m1; + m1.flipCoordinates(); + QVector3D p1 = m1 * QVector3D(2, 3, 4); + QVERIFY(p1 == QVector3D(2, -3, -4)); + + QMatrix4x4 m2; + m2.scale(2.0f, 3.0f, 1.0f); + m2.flipCoordinates(); + QVector3D p2 = m2 * QVector3D(2, 3, 4); + QVERIFY(p2 == QVector3D(4, -9, -4)); + + QMatrix4x4 m3; + m3.translate(2.0f, 3.0f, 1.0f); + m3.flipCoordinates(); + QVector3D p3 = m3 * QVector3D(2, 3, 4); + QVERIFY(p3 == QVector3D(4, 0, -3)); + + QMatrix4x4 m4; + m4.rotate(90.0f, 0.0f, 0.0f, 1.0f); + m4.flipCoordinates(); + QVector3D p4 = m4 * QVector3D(2, 3, 4); + QVERIFY(p4 == QVector3D(3, 2, -4)); +} + +// Test conversion of generic matrices to and from the non-generic types. +void tst_QMatrix::convertGeneric() +{ + QMatrix4x3 m1(uniqueValues4x3); + + static qreal const unique4x4[16] = { + 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }; +#if !defined(QT_NO_MEMBER_TEMPLATES) + QMatrix4x4 m4(m1); + QVERIFY(isSame(m4, unique4x4)); +#endif + QMatrix4x4 m5 = qGenericMatrixToMatrix4x4(m1); + QVERIFY(isSame(m5, unique4x4)); + + static qreal const conv4x4[12] = { + 1.0f, 2.0f, 3.0f, 4.0f, + 5.0f, 6.0f, 7.0f, 8.0f, + 9.0f, 10.0f, 11.0f, 12.0f + }; + QMatrix4x4 m9(uniqueValues4); +#if !defined(QT_NO_MEMBER_TEMPLATES) + QMatrix4x3 m10 = m9.toGenericMatrix<4, 3>(); + QVERIFY(isSame(m10, conv4x4)); +#endif + + QMatrix4x3 m11 = qGenericMatrixFromMatrix4x4<4, 3>(m9); + QVERIFY(isSame(m11, conv4x4)); +} + +void tst_QMatrix::extractAxisRotation_data() +{ + QTest::addColumn<float>("x"); + QTest::addColumn<float>("y"); + QTest::addColumn<float>("z"); + QTest::addColumn<float>("angle"); + + QTest::newRow("1, 0, 0, 0 deg") << 1.0f << 0.0f << 0.0f << 0.0f; + QTest::newRow("1, 0, 0, 90 deg") << 1.0f << 0.0f << 0.0f << 90.0f; + QTest::newRow("1, 0, 0, 270 deg") << 1.0f << 0.0f << 0.0f << 270.0f; + QTest::newRow("1, 0, 0, 45 deg") << 1.0f << 0.0f << 0.0f << 45.0f; + QTest::newRow("1, 0, 0, 120 deg") << 1.0f << 0.0f << 0.0f << 120.0f; + QTest::newRow("1, 0, 0, 300 deg") << 1.0f << 0.0f << 0.0f << 300.0f; + + QTest::newRow("0, 1, 0, 90 deg") << 0.0f << 1.0f << 0.0f << 90.0f; + QTest::newRow("0, 1, 0, 270 deg") << 0.0f << 1.0f << 0.0f << 270.0f; + QTest::newRow("0, 1, 0, 45 deg") << 0.0f << 1.0f << 0.0f << 45.0f; + QTest::newRow("0, 1, 0, 120 deg") << 0.0f << 1.0f << 0.0f << 120.0f; + QTest::newRow("0, 1, 0, 300 deg") << 0.0f << 1.0f << 0.0f << 300.0f; + + QTest::newRow("0, 0, 1, 90 deg") << 0.0f << 0.0f << 1.0f << 90.0f; + QTest::newRow("0, 0, 1, 270 deg") << 0.0f << 0.0f << 1.0f << 270.0f; + QTest::newRow("0, 0, 1, 45 deg") << 0.0f << 0.0f << 1.0f << 45.0f; + QTest::newRow("0, 0, 1, 120 deg") << 0.0f << 0.0f << 1.0f << 120.0f; + QTest::newRow("0, 0, 1, 300 deg") << 0.0f << 0.0f << 1.0f << 300.0f; + + QTest::newRow("1, 1, 1, 90 deg") << 1.0f << 1.0f << 1.0f << 90.0f; + QTest::newRow("1, 1, 1, 270 deg") << 1.0f << 1.0f << 1.0f << 270.0f; + QTest::newRow("1, 1, 1, 45 deg") << 1.0f << 1.0f << 1.0f << 45.0f; + QTest::newRow("1, 1, 1, 120 deg") << 1.0f << 1.0f << 1.0f << 120.0f; + QTest::newRow("1, 1, 1, 300 deg") << 1.0f << 1.0f << 1.0f << 300.0f; +} + +void tst_QMatrix::extractAxisRotation() +{ + QFETCH(float, x); + QFETCH(float, y); + QFETCH(float, z); + QFETCH(float, angle); + + QMatrix4x4 m; + QVector3D origAxis(x, y, z); + + m.rotate(angle, x, y, z); + + origAxis.normalize(); + QVector3D extractedAxis; + qreal extractedAngle; + + m.extractAxisRotation(extractedAngle, extractedAxis); + + qreal epsilon = 0.001; + + if (angle > 180) { + QVERIFY(fuzzyCompare(360.0f - angle, extractedAngle, epsilon)); + QVERIFY(fuzzyCompare(extractedAxis, -origAxis, epsilon)); + } else { + QVERIFY(fuzzyCompare(angle, extractedAngle, epsilon)); + QVERIFY(fuzzyCompare(extractedAxis, origAxis, epsilon)); + } +} + +void tst_QMatrix::extractTranslation_data() +{ + QTest::addColumn<QMatrix4x4>("rotation"); + QTest::addColumn<float>("x"); + QTest::addColumn<float>("y"); + QTest::addColumn<float>("z"); + + static QMatrix4x4 m1; + + QTest::newRow("identity, 100, 50, 25") + << m1 << 100.0f << 50.0f << 250.0f; + + m1.rotate(45.0, 1.0, 0.0, 0.0); + QTest::newRow("rotX 45 + 100, 50, 25") << m1 << 100.0f << 50.0f << 25.0f; + + m1.setIdentity(); + m1.rotate(45.0, 0.0, 1.0, 0.0); + QTest::newRow("rotY 45 + 100, 50, 25") << m1 << 100.0f << 50.0f << 25.0f; + + m1.setIdentity(); + m1.rotate(75, 0.0, 0.0, 1.0); + m1.rotate(25, 1.0, 0.0, 0.0); + m1.rotate(45, 0.0, 1.0, 0.0); + QTest::newRow("rotZ 75, rotX 25, rotY 45, 100, 50, 25") << m1 << 100.0f << 50.0f << 25.0f; +} + +void tst_QMatrix::extractTranslation() +{ + QFETCH(QMatrix4x4, rotation); + QFETCH(float, x); + QFETCH(float, y); + QFETCH(float, z); + + rotation.translate(x, y, z); + + QVector3D vec = rotation.extractTranslation(); + + qreal epsilon = 0.001; + + QVERIFY(fuzzyCompare(vec.x(), x, epsilon)); + QVERIFY(fuzzyCompare(vec.y(), y, epsilon)); + QVERIFY(fuzzyCompare(vec.z(), z, epsilon)); + + // Have to be careful with numbers here, it is really easy to blow away + // the precision of a fixed pointer number, especially when doing distance + // formula for vector normalization + + QMatrix4x4 lookAt; + QVector3D eye(1.5f, -2.5f, 2.5f); + lookAt.lookAt(eye, + QVector3D(10.0f, 10.0f, 10.0f), + QVector3D(0.0f, 1.0f, 0.0f)); + + QVector3D extEye = lookAt.extractTranslation(); + + QVERIFY(fuzzyCompare(eye.x(), -extEye.x(), epsilon)); + QVERIFY(fuzzyCompare(eye.y(), -extEye.y(), epsilon)); + QVERIFY(fuzzyCompare(eye.z(), -extEye.z(), epsilon)); +} + +// Copy of "flagBits" in qmatrix4x4.h. +enum { + Identity = 0x0001, // Identity matrix + General = 0x0002, // General matrix, unknown contents + Translation = 0x0004, // Contains a simple translation + Scale = 0x0008, // Contains a simple scale + Rotation = 0x0010 // Contains a simple rotation +}; + +// Structure that allows direct access to "flagBits" for testing. +struct Matrix4x4 +{ + float m[4][4]; + int flagBits; +}; + +// Test the inferring of special matrix types. +void tst_QMatrix::inferSpecialType_data() +{ + QTest::addColumn<void *>("mValues"); + QTest::addColumn<int>("flagBits"); + + QTest::newRow("null") + << (void *)nullValues4 << (int)General; + QTest::newRow("identity") + << (void *)identityValues4 << (int)Identity; + QTest::newRow("unique") + << (void *)uniqueValues4 << (int)General; + + static qreal scaleValues[16] = { + 2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 3.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 4.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }; + QTest::newRow("scale") + << (void *)scaleValues << (int)Scale; + + static qreal translateValues[16] = { + 1.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 1.0f, 0.0f, 3.0f, + 0.0f, 0.0f, 1.0f, 4.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }; + QTest::newRow("scale") + << (void *)translateValues << (int)Translation; + + static qreal bothValues[16] = { + 1.0f, 0.0f, 0.0f, 2.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 4.0f, + 0.0f, 0.0f, 0.0f, 1.0f + }; + QTest::newRow("both") + << (void *)bothValues << (int)(Scale | Translation); + + static qreal belowValues[16] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 4.0f, 0.0f, 0.0f, 1.0f + }; + QTest::newRow("below") + << (void *)belowValues << (int)General; +} +void tst_QMatrix::inferSpecialType() +{ + QFETCH(void *, mValues); + QFETCH(int, flagBits); + + QMatrix4x4 m((const qreal *)mValues); + m.inferSpecialType(); + + QCOMPARE(reinterpret_cast<Matrix4x4 *>(&m)->flagBits, flagBits); +} + +void tst_QMatrix::columnsAndRows() +{ + QMatrix4x4 m1(uniqueValues4); + + QVERIFY(m1.column(0) == QVector4D(1, 5, 9, 13)); + QVERIFY(m1.column(1) == QVector4D(2, 6, 10, 14)); + QVERIFY(m1.column(2) == QVector4D(3, 7, 11, 15)); + QVERIFY(m1.column(3) == QVector4D(4, 8, 12, 16)); + + QVERIFY(m1.row(0) == QVector4D(1, 2, 3, 4)); + QVERIFY(m1.row(1) == QVector4D(5, 6, 7, 8)); + QVERIFY(m1.row(2) == QVector4D(9, 10, 11, 12)); + QVERIFY(m1.row(3) == QVector4D(13, 14, 15, 16)); + + m1.setColumn(0, QVector4D(-1, -5, -9, -13)); + m1.setColumn(1, QVector4D(-2, -6, -10, -14)); + m1.setColumn(2, QVector4D(-3, -7, -11, -15)); + m1.setColumn(3, QVector4D(-4, -8, -12, -16)); + + QVERIFY(m1.column(0) == QVector4D(-1, -5, -9, -13)); + QVERIFY(m1.column(1) == QVector4D(-2, -6, -10, -14)); + QVERIFY(m1.column(2) == QVector4D(-3, -7, -11, -15)); + QVERIFY(m1.column(3) == QVector4D(-4, -8, -12, -16)); + + QVERIFY(m1.row(0) == QVector4D(-1, -2, -3, -4)); + QVERIFY(m1.row(1) == QVector4D(-5, -6, -7, -8)); + QVERIFY(m1.row(2) == QVector4D(-9, -10, -11, -12)); + QVERIFY(m1.row(3) == QVector4D(-13, -14, -15, -16)); + + m1.setRow(0, QVector4D(1, 5, 9, 13)); + m1.setRow(1, QVector4D(2, 6, 10, 14)); + m1.setRow(2, QVector4D(3, 7, 11, 15)); + m1.setRow(3, QVector4D(4, 8, 12, 16)); + + QVERIFY(m1.column(0) == QVector4D(1, 2, 3, 4)); + QVERIFY(m1.column(1) == QVector4D(5, 6, 7, 8)); + QVERIFY(m1.column(2) == QVector4D(9, 10, 11, 12)); + QVERIFY(m1.column(3) == QVector4D(13, 14, 15, 16)); + + QVERIFY(m1.row(0) == QVector4D(1, 5, 9, 13)); + QVERIFY(m1.row(1) == QVector4D(2, 6, 10, 14)); + QVERIFY(m1.row(2) == QVector4D(3, 7, 11, 15)); + QVERIFY(m1.row(3) == QVector4D(4, 8, 12, 16)); +} + +// Test converting QMatrix objects into QMatrix4x4 and then +// checking that transformations in the original perform the +// equivalent transformations in the new matrix. +void tst_QMatrix::convertQMatrix() +{ + QMatrix m1; + m1.translate(-3.5, 2.0); + QPointF p1 = m1.map(QPointF(100.0, 150.0)); + QCOMPARE(p1.x(), 100.0 - 3.5); + QCOMPARE(p1.y(), 150.0 + 2.0); + + QMatrix4x4 m2(m1); + QPointF p2 = m2 * QPointF(100.0, 150.0); + QCOMPARE((double)p2.x(), 100.0 - 3.5); + QCOMPARE((double)p2.y(), 150.0 + 2.0); + QVERIFY(m1 == m2.toAffine()); + + QMatrix m3; + m3.scale(1.5, -2.0); + QPointF p3 = m3.map(QPointF(100.0, 150.0)); + QCOMPARE(p3.x(), 1.5 * 100.0); + QCOMPARE(p3.y(), -2.0 * 150.0); + + QMatrix4x4 m4(m3); + QPointF p4 = m4 * QPointF(100.0, 150.0); + QCOMPARE((double)p4.x(), 1.5 * 100.0); + QCOMPARE((double)p4.y(), -2.0 * 150.0); + QVERIFY(m3 == m4.toAffine()); + + QMatrix m5; + m5.rotate(45.0); + QPointF p5 = m5.map(QPointF(100.0, 150.0)); + + QMatrix4x4 m6(m5); + QPointF p6 = m6 * QPointF(100.0, 150.0); + QVERIFY(fuzzyCompare(p5.x(), p6.x(), 0.005)); + QVERIFY(fuzzyCompare(p5.y(), p6.y(), 0.005)); + + QMatrix m7 = m6.toAffine(); + QVERIFY(fuzzyCompare(m5.m11(), m7.m11())); + QVERIFY(fuzzyCompare(m5.m12(), m7.m12())); + QVERIFY(fuzzyCompare(m5.m21(), m7.m21())); + QVERIFY(fuzzyCompare(m5.m22(), m7.m22())); + QVERIFY(fuzzyCompare(m5.dx(), m7.dx())); + QVERIFY(fuzzyCompare(m5.dy(), m7.dy())); +} + +// Test converting QTransform objects into QMatrix4x4 and then +// checking that transformations in the original perform the +// equivalent transformations in the new matrix. +void tst_QMatrix::convertQTransform() +{ + QTransform m1; + m1.translate(-3.5, 2.0); + QPointF p1 = m1.map(QPointF(100.0, 150.0)); + QCOMPARE(p1.x(), 100.0 - 3.5); + QCOMPARE(p1.y(), 150.0 + 2.0); + + QMatrix4x4 m2(m1); + QPointF p2 = m2 * QPointF(100.0, 150.0); + QCOMPARE((double)p2.x(), 100.0 - 3.5); + QCOMPARE((double)p2.y(), 150.0 + 2.0); + QVERIFY(m1 == m2.toTransform()); + + QTransform m3; + m3.scale(1.5, -2.0); + QPointF p3 = m3.map(QPointF(100.0, 150.0)); + QCOMPARE(p3.x(), 1.5 * 100.0); + QCOMPARE(p3.y(), -2.0 * 150.0); + + QMatrix4x4 m4(m3); + QPointF p4 = m4 * QPointF(100.0, 150.0); + QCOMPARE((double)p4.x(), 1.5 * 100.0); + QCOMPARE((double)p4.y(), -2.0 * 150.0); + QVERIFY(m3 == m4.toTransform()); + + QTransform m5; + m5.rotate(45.0); + QPointF p5 = m5.map(QPointF(100.0, 150.0)); + + QMatrix4x4 m6(m5); + QPointF p6 = m6 * QPointF(100.0, 150.0); + QVERIFY(fuzzyCompare(p5.x(), p6.x(), 0.005)); + QVERIFY(fuzzyCompare(p5.y(), p6.y(), 0.005)); + + QTransform m7 = m6.toTransform(); + QVERIFY(fuzzyCompare(m5.m11(), m7.m11())); + QVERIFY(fuzzyCompare(m5.m12(), m7.m12())); + QVERIFY(fuzzyCompare(m5.m21(), m7.m21())); + QVERIFY(fuzzyCompare(m5.m22(), m7.m22())); + QVERIFY(fuzzyCompare(m5.dx(), m7.dx())); + QVERIFY(fuzzyCompare(m5.dy(), m7.dy())); + QVERIFY(fuzzyCompare(m5.m13(), m7.m13())); + QVERIFY(fuzzyCompare(m5.m23(), m7.m23())); + QVERIFY(fuzzyCompare(m5.m33(), m7.m33())); +} + +// Test filling matrices with specific values. +void tst_QMatrix::fill() +{ + QMatrix4x4 m1; + m1.fill(0.0f); + QVERIFY(isSame(m1, nullValues4)); + + static const qreal fillValues4[] = + {2.5f, 2.5f, 2.5f, 2.5f, + 2.5f, 2.5f, 2.5f, 2.5f, + 2.5f, 2.5f, 2.5f, 2.5f, + 2.5f, 2.5f, 2.5f, 2.5f}; + m1.fill(2.5f); + QVERIFY(isSame(m1, fillValues4)); + + QMatrix4x3 m2; + m2.fill(0.0f); + QVERIFY(isSame(m2, nullValues4x3)); + + static const qreal fillValues4x3[] = + {2.5f, 2.5f, 2.5f, 2.5f, + 2.5f, 2.5f, 2.5f, 2.5f, + 2.5f, 2.5f, 2.5f, 2.5f}; + m2.fill(2.5f); + QVERIFY(isSame(m2, fillValues4x3)); +} + +// Test the mapRect() function for QRect and QRectF. +void tst_QMatrix::mapRect_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("width"); + QTest::addColumn<qreal>("height"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + QTest::newRow("rect") + << (qreal)1.0f << (qreal)-20.5f << (qreal)100.0f << (qreal)63.75f; +} +void tst_QMatrix::mapRect() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, width); + QFETCH(qreal, height); + + QRectF rect(x, y, width, height); + QRect recti(qRound(x), qRound(y), qRound(width), qRound(height)); + + QMatrix4x4 m1; + QVERIFY(m1.mapRect(rect) == rect); + QVERIFY(m1.mapRect(recti) == recti); + + QMatrix4x4 m2; + m2.translate(-100.5f, 64.0f); + QRectF translated = rect.translated(-100.5f, 64.0f); + QRect translatedi = QRect(qRound(recti.x() - 100.5f), recti.y() + 64, + recti.width(), recti.height()); + QVERIFY(m2.mapRect(rect) == translated); + QVERIFY(m2.mapRect(recti) == translatedi); + + QMatrix4x4 m3; + m3.scale(-100.5f, 64.0f); + qreal scalex = x * -100.5f; + qreal scaley = y * 64.0f; + qreal scalewid = width * -100.5f; + qreal scaleht = height * 64.0f; + if (scalewid < 0.0f) { + scalewid = -scalewid; + scalex -= scalewid; + } + if (scaleht < 0.0f) { + scaleht = -scaleht; + scaley -= scaleht; + } + QRectF scaled(scalex, scaley, scalewid, scaleht); + QVERIFY(m3.mapRect(rect) == scaled); + scalex = recti.x() * -100.5f; + scaley = recti.y() * 64.0f; + scalewid = recti.width() * -100.5f; + scaleht = recti.height() * 64.0f; + if (scalewid < 0.0f) { + scalewid = -scalewid; + scalex -= scalewid; + } + if (scaleht < 0.0f) { + scaleht = -scaleht; + scaley -= scaleht; + } + QRect scaledi(qRound(scalex), qRound(scaley), + qRound(scalewid), qRound(scaleht)); + QVERIFY(m3.mapRect(recti) == scaledi); + + QMatrix4x4 m4; + m4.translate(-100.5f, 64.0f); + m4.scale(-2.5f, 4.0f); + qreal transx1 = x * -2.5f - 100.5f; + qreal transy1 = y * 4.0f + 64.0f; + qreal transx2 = (x + width) * -2.5f - 100.5f; + qreal transy2 = (y + height) * 4.0f + 64.0f; + if (transx1 > transx2) + qSwap(transx1, transx2); + if (transy1 > transy2) + qSwap(transy1, transy2); + QRectF trans(transx1, transy1, transx2 - transx1, transy2 - transy1); + QVERIFY(m4.mapRect(rect) == trans); + transx1 = recti.x() * -2.5f - 100.5f; + transy1 = recti.y() * 4.0f + 64.0f; + transx2 = (recti.x() + recti.width()) * -2.5f - 100.5f; + transy2 = (recti.y() + recti.height()) * 4.0f + 64.0f; + if (transx1 > transx2) + qSwap(transx1, transx2); + if (transy1 > transy2) + qSwap(transy1, transy2); + QRect transi(qRound(transx1), qRound(transy1), + qRound(transx2) - qRound(transx1), + qRound(transy2) - qRound(transy1)); + QVERIFY(m4.mapRect(recti) == transi); + + m4.rotate(45.0f, 0.0f, 0.0f, 1.0f); + + QTransform t4; + t4.translate(-100.5f, 64.0f); + t4.scale(-2.5f, 4.0f); + t4.rotate(45.0f); + QRectF mr = m4.mapRect(rect); + QRectF tr = t4.mapRect(rect); + QVERIFY(fuzzyCompare(mr.x(), tr.x())); + QVERIFY(fuzzyCompare(mr.y(), tr.y())); + QVERIFY(fuzzyCompare(mr.width(), tr.width())); + QVERIFY(fuzzyCompare(mr.height(), tr.height())); + + QRect mri = m4.mapRect(recti); + QRect tri = t4.mapRect(recti); + QVERIFY(mri == tri); +} + +QTEST_APPLESS_MAIN(tst_QMatrix) + +#include "tst_qmatrixnxn.moc" diff --git a/tests/auto/math3d/qquaternion/qquaternion.pro b/tests/auto/math3d/qquaternion/qquaternion.pro new file mode 100644 index 0000000..eea84f0 --- /dev/null +++ b/tests/auto/math3d/qquaternion/qquaternion.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +VPATH += ../shared +INCLUDEPATH += ../shared +HEADERS += math3dincludes.h +SOURCES += tst_qquaternion.cpp diff --git a/tests/auto/math3d/qquaternion/tst_qquaternion.cpp b/tests/auto/math3d/qquaternion/tst_qquaternion.cpp new file mode 100644 index 0000000..395032f --- /dev/null +++ b/tests/auto/math3d/qquaternion/tst_qquaternion.cpp @@ -0,0 +1,830 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtCore/qmath.h> +#include "math3dincludes.h" + +class tst_QQuaternion : public QObject +{ + Q_OBJECT +public: + tst_QQuaternion() {} + ~tst_QQuaternion() {} + +private slots: + void create(); + + void length_data(); + void length(); + + void normalized_data(); + void normalized(); + + void normalize_data(); + void normalize(); + + void compare(); + + void add_data(); + void add(); + + void subtract_data(); + void subtract(); + + void multiply_data(); + void multiply(); + + void multiplyFactor_data(); + void multiplyFactor(); + + void divide_data(); + void divide(); + + void negate_data(); + void negate(); + + void conjugate_data(); + void conjugate(); + + void fromAxisAndAngle_data(); + void fromAxisAndAngle(); + + void slerp_data(); + void slerp(); + + void nlerp_data(); + void nlerp(); +}; + +// qFuzzyCompare isn't quite "fuzzy" enough to handle conversion +// to fixed-point and back again. So create "fuzzier" compares. +static bool fuzzyCompare(float x, float y) +{ + float diff = x - y; + if (diff < 0.0f) + diff = -diff; + return (diff < 0.001); +} + +// Test the creation of QQuaternion objects in various ways: +// construct, copy, and modify. +void tst_QQuaternion::create() +{ + QQuaternion identity; + QCOMPARE(identity.x(), (qreal)0.0f); + QCOMPARE(identity.y(), (qreal)0.0f); + QCOMPARE(identity.z(), (qreal)0.0f); + QCOMPARE(identity.scalar(), (qreal)1.0f); + QVERIFY(identity.isIdentity()); + + QQuaternion v1(34.0f, 1.0f, 2.5f, -89.25f); + QCOMPARE(v1.x(), (qreal)1.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.scalar(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + QQuaternion v1i(34, 1, 2, -89); + QCOMPARE(v1i.x(), (qreal)1.0f); + QCOMPARE(v1i.y(), (qreal)2.0f); + QCOMPARE(v1i.z(), (qreal)-89.0f); + QCOMPARE(v1i.scalar(), (qreal)34.0f); + QVERIFY(!v1i.isNull()); + + QQuaternion v2(v1); + QCOMPARE(v2.x(), (qreal)1.0f); + QCOMPARE(v2.y(), (qreal)2.5f); + QCOMPARE(v2.z(), (qreal)-89.25f); + QCOMPARE(v2.scalar(), (qreal)34.0f); + QVERIFY(!v2.isNull()); + + QQuaternion v4; + QCOMPARE(v4.x(), (qreal)0.0f); + QCOMPARE(v4.y(), (qreal)0.0f); + QCOMPARE(v4.z(), (qreal)0.0f); + QCOMPARE(v4.scalar(), (qreal)1.0f); + QVERIFY(v4.isIdentity()); + v4 = v1; + QCOMPARE(v4.x(), (qreal)1.0f); + QCOMPARE(v4.y(), (qreal)2.5f); + QCOMPARE(v4.z(), (qreal)-89.25f); + QCOMPARE(v4.scalar(), (qreal)34.0f); + QVERIFY(!v4.isNull()); + + QQuaternion v9(34, QVector3D(1.0f, 2.5f, -89.25f)); + QCOMPARE(v9.x(), (qreal)1.0f); + QCOMPARE(v9.y(), (qreal)2.5f); + QCOMPARE(v9.z(), (qreal)-89.25f); + QCOMPARE(v9.scalar(), (qreal)34.0f); + QVERIFY(!v9.isNull()); + + v1.setX(3.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.scalar(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setY(10.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.scalar(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setZ(15.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)15.5f); + QCOMPARE(v1.scalar(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setScalar(6.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)15.5f); + QCOMPARE(v1.scalar(), (qreal)6.0f); + QVERIFY(!v1.isNull()); + + v1.setVector(2.0f, 6.5f, -1.25f); + QCOMPARE(v1.x(), (qreal)2.0f); + QCOMPARE(v1.y(), (qreal)6.5f); + QCOMPARE(v1.z(), (qreal)-1.25f); + QCOMPARE(v1.scalar(), (qreal)6.0f); + QVERIFY(!v1.isNull()); + QVERIFY(v1.vector() == QVector3D(2.0f, 6.5f, -1.25f)); + + v1.setVector(QVector3D(-2.0f, -6.5f, 1.25f)); + QCOMPARE(v1.x(), (qreal)-2.0f); + QCOMPARE(v1.y(), (qreal)-6.5f); + QCOMPARE(v1.z(), (qreal)1.25f); + QCOMPARE(v1.scalar(), (qreal)6.0f); + QVERIFY(!v1.isNull()); + QVERIFY(v1.vector() == QVector3D(-2.0f, -6.5f, 1.25f)); + + v1.setX(0.0f); + v1.setY(0.0f); + v1.setZ(0.0f); + v1.setScalar(0.0f); + QCOMPARE(v1.x(), (qreal)0.0f); + QCOMPARE(v1.y(), (qreal)0.0f); + QCOMPARE(v1.z(), (qreal)0.0f); + QCOMPARE(v1.scalar(), (qreal)0.0f); + QVERIFY(v1.isNull()); + + QVector4D v10 = v9.toVector4D(); + QCOMPARE(v10.x(), (qreal)1.0f); + QCOMPARE(v10.y(), (qreal)2.5f); + QCOMPARE(v10.z(), (qreal)-89.25f); + QCOMPARE(v10.w(), (qreal)34.0f); +} + +// Test length computation for quaternions. +void tst_QQuaternion::length_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<qreal>("w"); + QTest::addColumn<qreal>("len"); + + QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; + QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; + QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)2.0f << (qreal)qSqrt(16.0f); +} +void tst_QQuaternion::length() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + QFETCH(qreal, len); + + QQuaternion v(w, x, y, z); + QCOMPARE((float)(v.length()), (float)len); + QCOMPARE((float)(v.lengthSquared()), (float)(x * x + y * y + z * z + w * w)); +} + +// Test the unit vector conversion for quaternions. +void tst_QQuaternion::normalized_data() +{ + // Use the same test data as the length test. + length_data(); +} +void tst_QQuaternion::normalized() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + QFETCH(qreal, len); + + QQuaternion v(w, x, y, z); + QQuaternion u = v.normalized(); + if (v.isNull()) + QVERIFY(u.isNull()); + else + QCOMPARE((float)(u.length()), (float)1.0f); + QCOMPARE((float)(u.x() * len), (float)(v.x())); + QCOMPARE((float)(u.y() * len), (float)(v.y())); + QCOMPARE((float)(u.z() * len), (float)(v.z())); + QCOMPARE((float)(u.scalar() * len), (float)(v.scalar())); +} + +// Test the unit vector conversion for quaternions. +void tst_QQuaternion::normalize_data() +{ + // Use the same test data as the length test. + length_data(); +} +void tst_QQuaternion::normalize() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + + QQuaternion v(w, x, y, z); + bool isNull = v.isNull(); + v.normalize(); + if (isNull) + QVERIFY(v.isNull()); + else + QCOMPARE((float)(v.length()), (float)1.0f); +} + +// Test the comparison operators for quaternions. +void tst_QQuaternion::compare() +{ + QQuaternion v1(8, 1, 2, 4); + QQuaternion v2(8, 1, 2, 4); + QQuaternion v3(8, 3, 2, 4); + QQuaternion v4(8, 1, 3, 4); + QQuaternion v5(8, 1, 2, 3); + QQuaternion v6(3, 1, 2, 4); + + QVERIFY(v1 == v2); + QVERIFY(v1 != v3); + QVERIFY(v1 != v4); + QVERIFY(v1 != v5); + QVERIFY(v1 != v6); +} + +// Test addition for quaternions. +void tst_QQuaternion::add_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("w3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f; + + QTest::newRow("wonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f + << (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f << (qreal)17.0f; +} +void tst_QQuaternion::add() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, w3); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(w2, x2, y2, z2); + QQuaternion v3(w3, x3, y3, z3); + + QVERIFY((v1 + v2) == v3); + + QQuaternion v4(v1); + v4 += v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() + v2.x()); + QCOMPARE(v4.y(), v1.y() + v2.y()); + QCOMPARE(v4.z(), v1.z() + v2.z()); + QCOMPARE(v4.scalar(), v1.scalar() + v2.scalar()); +} + +// Test subtraction for quaternions. +void tst_QQuaternion::subtract_data() +{ + // Use the same test data as the add test. + add_data(); +} +void tst_QQuaternion::subtract() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, w3); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(w2, x2, y2, z2); + QQuaternion v3(w3, x3, y3, z3); + + QVERIFY((v3 - v1) == v2); + QVERIFY((v3 - v2) == v1); + + QQuaternion v4(v3); + v4 -= v1; + QVERIFY(v4 == v2); + + QCOMPARE(v4.x(), v3.x() - v1.x()); + QCOMPARE(v4.y(), v3.y() - v1.y()); + QCOMPARE(v4.z(), v3.z() - v1.z()); + QCOMPARE(v4.scalar(), v3.scalar() - v1.scalar()); + + QQuaternion v5(v3); + v5 -= v2; + QVERIFY(v5 == v1); + + QCOMPARE(v5.x(), v3.x() - v2.x()); + QCOMPARE(v5.y(), v3.y() - v2.y()); + QCOMPARE(v5.z(), v3.z() - v2.z()); + QCOMPARE(v5.scalar(), v3.scalar() - v2.scalar()); +} + +// Test quaternion multiplication. +void tst_QQuaternion::multiply_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("unitvec") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; + + QTest::newRow("complex") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)7.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)6.0f << (qreal)8.0f; +} +void tst_QQuaternion::multiply() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + + QQuaternion q1(w1, x1, y1, z1); + QQuaternion q2(w2, x2, y2, z2); + + // Use the simple algorithm at: + // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q53 + // to calculate the answer we expect to get. + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + qreal scalar = w1 * w2 - QVector3D::dotProduct(v1, v2); + QVector3D vector = w1 * v2 + w2 * v1 + QVector3D::crossProduct(v1, v2); + QQuaternion result(scalar, vector); + + QVERIFY((q1 * q2) == result); +} + +// Test multiplication by a factor for quaternions. +void tst_QQuaternion::multiplyFactor_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)100.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("wonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f << (qreal)8.0f; + + QTest::newRow("allzero") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f + << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; +} +void tst_QQuaternion::multiplyFactor() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(w2, x2, y2, z2); + + QVERIFY((v1 * factor) == v2); + QVERIFY((factor * v1) == v2); + + QQuaternion v3(v1); + v3 *= factor; + QVERIFY(v3 == v2); + + QCOMPARE(v3.x(), v1.x() * factor); + QCOMPARE(v3.y(), v1.y() * factor); + QCOMPARE(v3.z(), v1.z() * factor); + QCOMPARE(v3.scalar(), v1.scalar() * factor); +} + +// Test division by a factor for quaternions. +void tst_QQuaternion::divide_data() +{ + // Use the same test data as the multiply test. + multiplyFactor_data(); +} +void tst_QQuaternion::divide() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(w2, x2, y2, z2); + + if (factor == (qreal)0.0f) + return; + + QVERIFY((v2 / factor) == v1); + + QQuaternion v3(v2); + v3 /= factor; + QVERIFY(v3 == v1); + + QCOMPARE(v3.x(), v2.x() / factor); + QCOMPARE(v3.y(), v2.y() / factor); + QCOMPARE(v3.z(), v2.z() / factor); + QCOMPARE(v3.scalar(), v2.scalar() / factor); +} + +// Test negation for quaternions. +void tst_QQuaternion::negate_data() +{ + // Use the same test data as the add test. + add_data(); +} +void tst_QQuaternion::negate() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(-w1, -x1, -y1, -z1); + + QVERIFY(-v1 == v2); +} + +// Test quaternion conjugate calculations. +void tst_QQuaternion::conjugate_data() +{ + // Use the same test data as the add test. + add_data(); +} +void tst_QQuaternion::conjugate() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + + QQuaternion v1(w1, x1, y1, z1); + QQuaternion v2(w1, -x1, -y1, -z1); + + QVERIFY(v1.conjugate() == v2); +} + +// Test quaternion creation from an axis and an angle. +void tst_QQuaternion::fromAxisAndAngle_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("angle"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)90.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)180.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)270.0f; + + QTest::newRow("complex") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)45.0f; +} +void tst_QQuaternion::fromAxisAndAngle() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, angle); + + // Use a straight-forward implementation of the algorithm at: + // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56 + // to calculate the answer we expect to get. + QVector3D vector = QVector3D(x1, y1, z1).normalized(); + qreal sin_a = qSin((angle * M_PI / 180.0) / 2.0); + qreal cos_a = qCos((angle * M_PI / 180.0) / 2.0); + QQuaternion result((qreal)cos_a, + (qreal)(vector.x() * sin_a), + (qreal)(vector.y() * sin_a), + (qreal)(vector.z() * sin_a)); + result = result.normalized(); + + QQuaternion answer = QQuaternion::fromAxisAndAngle(QVector3D(x1, y1, z1), angle); + QVERIFY(fuzzyCompare(answer.x(), result.x())); + QVERIFY(fuzzyCompare(answer.y(), result.y())); + QVERIFY(fuzzyCompare(answer.z(), result.z())); + QVERIFY(fuzzyCompare(answer.scalar(), result.scalar())); + + answer = QQuaternion::fromAxisAndAngle(x1, y1, z1, angle); + QVERIFY(fuzzyCompare(answer.x(), result.x())); + QVERIFY(fuzzyCompare(answer.y(), result.y())); + QVERIFY(fuzzyCompare(answer.z(), result.z())); + QVERIFY(fuzzyCompare(answer.scalar(), result.scalar())); +} + +// Test spherical interpolation of quaternions. +void tst_QQuaternion::slerp_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("angle1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("angle2"); + QTest::addColumn<qreal>("t"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("angle3"); + + QTest::newRow("first") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f + << (qreal)0.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f; + QTest::newRow("first2") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f + << (qreal)-0.5f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f; + QTest::newRow("second") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f + << (qreal)1.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f; + QTest::newRow("second2") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f + << (qreal)1.5f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f; + QTest::newRow("middle") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f + << (qreal)0.5f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)135.0f; + QTest::newRow("wide angle") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)0.0f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)270.0f + << (qreal)0.5f + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)-45.0f; +} +void tst_QQuaternion::slerp() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, angle1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, angle2); + QFETCH(qreal, t); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, angle3); + + QQuaternion q1 = QQuaternion::fromAxisAndAngle(x1, y1, z1, angle1); + QQuaternion q2 = QQuaternion::fromAxisAndAngle(x2, y2, z2, angle2); + QQuaternion q3 = QQuaternion::fromAxisAndAngle(x3, y3, z3, angle3); + + QQuaternion result = QQuaternion::slerp(q1, q2, t); + + QVERIFY(fuzzyCompare(result.x(), q3.x())); + QVERIFY(fuzzyCompare(result.y(), q3.y())); + QVERIFY(fuzzyCompare(result.z(), q3.z())); + QVERIFY(fuzzyCompare(result.scalar(), q3.scalar())); +} + +// Test normalized linear interpolation of quaternions. +void tst_QQuaternion::nlerp_data() +{ + slerp_data(); +} +void tst_QQuaternion::nlerp() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, angle1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, angle2); + QFETCH(qreal, t); + + QQuaternion q1 = QQuaternion::fromAxisAndAngle(x1, y1, z1, angle1); + QQuaternion q2 = QQuaternion::fromAxisAndAngle(x2, y2, z2, angle2); + + QQuaternion result = QQuaternion::nlerp(q1, q2, t); + + qreal resultx, resulty, resultz, resultscalar; + if (t <= 0.0f) { + resultx = q1.x(); + resulty = q1.y(); + resultz = q1.z(); + resultscalar = q1.scalar(); + } else if (t >= 1.0f) { + resultx = q2.x(); + resulty = q2.y(); + resultz = q2.z(); + resultscalar = q2.scalar(); + } else if (qAbs(angle1 - angle2) <= 180.f) { + resultx = q1.x() * (1 - t) + q2.x() * t; + resulty = q1.y() * (1 - t) + q2.y() * t; + resultz = q1.z() * (1 - t) + q2.z() * t; + resultscalar = q1.scalar() * (1 - t) + q2.scalar() * t; + } else { + // Angle greater than 180 degrees: negate q2. + resultx = q1.x() * (1 - t) - q2.x() * t; + resulty = q1.y() * (1 - t) - q2.y() * t; + resultz = q1.z() * (1 - t) - q2.z() * t; + resultscalar = q1.scalar() * (1 - t) - q2.scalar() * t; + } + + QQuaternion q3 = QQuaternion(resultscalar, resultx, resulty, resultz).normalized(); + + QVERIFY(fuzzyCompare(result.x(), q3.x())); + QVERIFY(fuzzyCompare(result.y(), q3.y())); + QVERIFY(fuzzyCompare(result.z(), q3.z())); + QVERIFY(fuzzyCompare(result.scalar(), q3.scalar())); +} + +QTEST_APPLESS_MAIN(tst_QQuaternion) + +#include "tst_qquaternion.moc" diff --git a/tests/auto/math3d/qvectornd/qvectornd.pro b/tests/auto/math3d/qvectornd/qvectornd.pro new file mode 100644 index 0000000..0981637 --- /dev/null +++ b/tests/auto/math3d/qvectornd/qvectornd.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +VPATH += ../shared +INCLUDEPATH += ../shared +HEADERS += math3dincludes.h +SOURCES += tst_qvectornd.cpp diff --git a/tests/auto/math3d/qvectornd/tst_qvectornd.cpp b/tests/auto/math3d/qvectornd/tst_qvectornd.cpp new file mode 100644 index 0000000..0eb5b07 --- /dev/null +++ b/tests/auto/math3d/qvectornd/tst_qvectornd.cpp @@ -0,0 +1,2045 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtCore/qmath.h> +#include "math3dincludes.h" + +class tst_QVector : public QObject +{ + Q_OBJECT +public: + tst_QVector() {} + ~tst_QVector() {} + +private slots: + void create2(); + void create3(); + void create4(); + + void length2_data(); + void length2(); + void length3_data(); + void length3(); + void length4_data(); + void length4(); + + void normalized2_data(); + void normalized2(); + void normalized3_data(); + void normalized3(); + void normalized4_data(); + void normalized4(); + + void normalize2_data(); + void normalize2(); + void normalize3_data(); + void normalize3(); + void normalize4_data(); + void normalize4(); + + void compare2(); + void compare3(); + void compare4(); + + void add2_data(); + void add2(); + void add3_data(); + void add3(); + void add4_data(); + void add4(); + + void subtract2_data(); + void subtract2(); + void subtract3_data(); + void subtract3(); + void subtract4_data(); + void subtract4(); + + void multiply2_data(); + void multiply2(); + void multiply3_data(); + void multiply3(); + void multiply4_data(); + void multiply4(); + + void multiplyFactor2_data(); + void multiplyFactor2(); + void multiplyFactor3_data(); + void multiplyFactor3(); + void multiplyFactor4_data(); + void multiplyFactor4(); + + void divide2_data(); + void divide2(); + void divide3_data(); + void divide3(); + void divide4_data(); + void divide4(); + + void negate2_data(); + void negate2(); + void negate3_data(); + void negate3(); + void negate4_data(); + void negate4(); + + void crossProduct_data(); + void crossProduct(); + void normal_data(); + void normal(); + void distanceToPlane_data(); + void distanceToPlane(); + void distanceToLine_data(); + void distanceToLine(); + + void dotProduct2_data(); + void dotProduct2(); + void dotProduct3_data(); + void dotProduct3(); + void dotProduct4_data(); + void dotProduct4(); +}; + +// qFuzzyCompare isn't quite "fuzzy" enough to handle conversion +// to fixed-point and back again. So create "fuzzier" compares. +static bool fuzzyCompare(float x, float y) +{ + float diff = x - y; + if (diff < 0.0f) + diff = -diff; + return (diff < 0.001); +} + +// Test the creation of QVector2D objects in various ways: +// construct, copy, and modify. +void tst_QVector::create2() +{ + QVector2D null; + QCOMPARE(null.x(), (qreal)0.0f); + QCOMPARE(null.y(), (qreal)0.0f); + QVERIFY(null.isNull()); + + QVector2D v1(1.0f, 2.5f); + QCOMPARE(v1.x(), (qreal)1.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QVERIFY(!v1.isNull()); + + QVector2D v1i(1, 2); + QCOMPARE(v1i.x(), (qreal)1.0f); + QCOMPARE(v1i.y(), (qreal)2.0f); + QVERIFY(!v1i.isNull()); + + QVector2D v2(v1); + QCOMPARE(v2.x(), (qreal)1.0f); + QCOMPARE(v2.y(), (qreal)2.5f); + QVERIFY(!v2.isNull()); + + QVector2D v4; + QCOMPARE(v4.x(), (qreal)0.0f); + QCOMPARE(v4.y(), (qreal)0.0f); + QVERIFY(v4.isNull()); + v4 = v1; + QCOMPARE(v4.x(), (qreal)1.0f); + QCOMPARE(v4.y(), (qreal)2.5f); + QVERIFY(!v4.isNull()); + + QVector2D v5(QPoint(1, 2)); + QCOMPARE(v5.x(), (qreal)1.0f); + QCOMPARE(v5.y(), (qreal)2.0f); + QVERIFY(!v5.isNull()); + + QVector2D v6(QPointF(1, 2.5)); + QCOMPARE(v6.x(), (qreal)1.0f); + QCOMPARE(v6.y(), (qreal)2.5f); + QVERIFY(!v6.isNull()); + + QVector2D v7(QVector3D(1.0f, 2.5f, 54.25f)); + QCOMPARE(v7.x(), (qreal)1.0f); + QCOMPARE(v7.y(), (qreal)2.5f); + QVERIFY(!v6.isNull()); + + QVector2D v8(QVector4D(1.0f, 2.5f, 54.25f, 34.0f)); + QCOMPARE(v8.x(), (qreal)1.0f); + QCOMPARE(v8.y(), (qreal)2.5f); + QVERIFY(!v6.isNull()); + + v1.setX(3.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QVERIFY(!v1.isNull()); + + v1.setY(10.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QVERIFY(!v1.isNull()); + + v1.setX(0.0f); + v1.setY(0.0f); + QCOMPARE(v1.x(), (qreal)0.0f); + QCOMPARE(v1.y(), (qreal)0.0f); + QVERIFY(v1.isNull()); + + QPoint p1 = v8.toPoint(); + QCOMPARE(p1.x(), 1); + QCOMPARE(p1.y(), 3); + + QPointF p2 = v8.toPointF(); + QCOMPARE((qreal)p2.x(), (qreal)1.0f); + QCOMPARE((qreal)p2.y(), (qreal)2.5f); + + QVector3D v9 = v8.toVector3D(); + QCOMPARE(v9.x(), (qreal)1.0f); + QCOMPARE(v9.y(), (qreal)2.5f); + QCOMPARE(v9.z(), (qreal)0.0f); + + QVector4D v10 = v8.toVector4D(); + QCOMPARE(v10.x(), (qreal)1.0f); + QCOMPARE(v10.y(), (qreal)2.5f); + QCOMPARE(v10.z(), (qreal)0.0f); + QCOMPARE(v10.w(), (qreal)0.0f); +} + +// Test the creation of QVector3D objects in various ways: +// construct, copy, and modify. +void tst_QVector::create3() +{ + QVector3D null; + QCOMPARE(null.x(), (qreal)0.0f); + QCOMPARE(null.y(), (qreal)0.0f); + QCOMPARE(null.z(), (qreal)0.0f); + QVERIFY(null.isNull()); + + QVector3D v1(1.0f, 2.5f, -89.25f); + QCOMPARE(v1.x(), (qreal)1.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QVERIFY(!v1.isNull()); + + QVector3D v1i(1, 2, -89); + QCOMPARE(v1i.x(), (qreal)1.0f); + QCOMPARE(v1i.y(), (qreal)2.0f); + QCOMPARE(v1i.z(), (qreal)-89.0f); + QVERIFY(!v1i.isNull()); + + QVector3D v2(v1); + QCOMPARE(v2.x(), (qreal)1.0f); + QCOMPARE(v2.y(), (qreal)2.5f); + QCOMPARE(v2.z(), (qreal)-89.25f); + QVERIFY(!v2.isNull()); + + QVector3D v3(1.0f, 2.5f, 0.0f); + QCOMPARE(v3.x(), (qreal)1.0f); + QCOMPARE(v3.y(), (qreal)2.5f); + QCOMPARE(v3.z(), (qreal)0.0f); + QVERIFY(!v3.isNull()); + + QVector3D v3i(1, 2, 0); + QCOMPARE(v3i.x(), (qreal)1.0f); + QCOMPARE(v3i.y(), (qreal)2.0f); + QCOMPARE(v3i.z(), (qreal)0.0f); + QVERIFY(!v3i.isNull()); + + QVector3D v4; + QCOMPARE(v4.x(), (qreal)0.0f); + QCOMPARE(v4.y(), (qreal)0.0f); + QCOMPARE(v4.z(), (qreal)0.0f); + QVERIFY(v4.isNull()); + v4 = v1; + QCOMPARE(v4.x(), (qreal)1.0f); + QCOMPARE(v4.y(), (qreal)2.5f); + QCOMPARE(v4.z(), (qreal)-89.25f); + QVERIFY(!v4.isNull()); + + QVector3D v5(QPoint(1, 2)); + QCOMPARE(v5.x(), (qreal)1.0f); + QCOMPARE(v5.y(), (qreal)2.0f); + QCOMPARE(v5.z(), (qreal)0.0f); + QVERIFY(!v5.isNull()); + + QVector3D v6(QPointF(1, 2.5)); + QCOMPARE(v6.x(), (qreal)1.0f); + QCOMPARE(v6.y(), (qreal)2.5f); + QCOMPARE(v6.z(), (qreal)0.0f); + QVERIFY(!v6.isNull()); + + QVector3D v7(QVector2D(1.0f, 2.5f)); + QCOMPARE(v7.x(), (qreal)1.0f); + QCOMPARE(v7.y(), (qreal)2.5f); + QCOMPARE(v7.z(), (qreal)0.0f); + QVERIFY(!v7.isNull()); + + QVector3D v8(QVector2D(1.0f, 2.5f), 54.25f); + QCOMPARE(v8.x(), (qreal)1.0f); + QCOMPARE(v8.y(), (qreal)2.5f); + QCOMPARE(v8.z(), (qreal)54.25f); + QVERIFY(!v8.isNull()); + + QVector3D v9(QVector4D(1.0f, 2.5f, 54.25f, 34.0f)); + QCOMPARE(v9.x(), (qreal)1.0f); + QCOMPARE(v9.y(), (qreal)2.5f); + QCOMPARE(v9.z(), (qreal)54.25f); + QVERIFY(!v9.isNull()); + + v1.setX(3.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QVERIFY(!v1.isNull()); + + v1.setY(10.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QVERIFY(!v1.isNull()); + + v1.setZ(15.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)15.5f); + QVERIFY(!v1.isNull()); + + v1.setX(0.0f); + v1.setY(0.0f); + v1.setZ(0.0f); + QCOMPARE(v1.x(), (qreal)0.0f); + QCOMPARE(v1.y(), (qreal)0.0f); + QCOMPARE(v1.z(), (qreal)0.0f); + QVERIFY(v1.isNull()); + + QPoint p1 = v8.toPoint(); + QCOMPARE(p1.x(), 1); + QCOMPARE(p1.y(), 3); + + QPointF p2 = v8.toPointF(); + QCOMPARE((qreal)p2.x(), (qreal)1.0f); + QCOMPARE((qreal)p2.y(), (qreal)2.5f); + + QVector2D v10 = v8.toVector2D(); + QCOMPARE(v10.x(), (qreal)1.0f); + QCOMPARE(v10.y(), (qreal)2.5f); + + QVector4D v11 = v8.toVector4D(); + QCOMPARE(v11.x(), (qreal)1.0f); + QCOMPARE(v11.y(), (qreal)2.5f); + QCOMPARE(v11.z(), (qreal)54.25f); + QCOMPARE(v11.w(), (qreal)0.0f); +} + +// Test the creation of QVector4D objects in various ways: +// construct, copy, and modify. +void tst_QVector::create4() +{ + QVector4D null; + QCOMPARE(null.x(), (qreal)0.0f); + QCOMPARE(null.y(), (qreal)0.0f); + QCOMPARE(null.z(), (qreal)0.0f); + QCOMPARE(null.w(), (qreal)0.0f); + QVERIFY(null.isNull()); + + QVector4D v1(1.0f, 2.5f, -89.25f, 34.0f); + QCOMPARE(v1.x(), (qreal)1.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.w(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + QVector4D v1i(1, 2, -89, 34); + QCOMPARE(v1i.x(), (qreal)1.0f); + QCOMPARE(v1i.y(), (qreal)2.0f); + QCOMPARE(v1i.z(), (qreal)-89.0f); + QCOMPARE(v1i.w(), (qreal)34.0f); + QVERIFY(!v1i.isNull()); + + QVector4D v2(v1); + QCOMPARE(v2.x(), (qreal)1.0f); + QCOMPARE(v2.y(), (qreal)2.5f); + QCOMPARE(v2.z(), (qreal)-89.25f); + QCOMPARE(v2.w(), (qreal)34.0f); + QVERIFY(!v2.isNull()); + + QVector4D v3(1.0f, 2.5f, 0.0f, 0.0f); + QCOMPARE(v3.x(), (qreal)1.0f); + QCOMPARE(v3.y(), (qreal)2.5f); + QCOMPARE(v3.z(), (qreal)0.0f); + QCOMPARE(v3.w(), (qreal)0.0f); + QVERIFY(!v3.isNull()); + + QVector4D v3i(1, 2, 0, 0); + QCOMPARE(v3i.x(), (qreal)1.0f); + QCOMPARE(v3i.y(), (qreal)2.0f); + QCOMPARE(v3i.z(), (qreal)0.0f); + QCOMPARE(v3i.w(), (qreal)0.0f); + QVERIFY(!v3i.isNull()); + + QVector4D v3b(1.0f, 2.5f, -89.25f, 0.0f); + QCOMPARE(v3b.x(), (qreal)1.0f); + QCOMPARE(v3b.y(), (qreal)2.5f); + QCOMPARE(v3b.z(), (qreal)-89.25f); + QCOMPARE(v3b.w(), (qreal)0.0f); + QVERIFY(!v3b.isNull()); + + QVector4D v3bi(1, 2, -89, 0); + QCOMPARE(v3bi.x(), (qreal)1.0f); + QCOMPARE(v3bi.y(), (qreal)2.0f); + QCOMPARE(v3bi.z(), (qreal)-89.0f); + QCOMPARE(v3bi.w(), (qreal)0.0f); + QVERIFY(!v3bi.isNull()); + + QVector4D v4; + QCOMPARE(v4.x(), (qreal)0.0f); + QCOMPARE(v4.y(), (qreal)0.0f); + QCOMPARE(v4.z(), (qreal)0.0f); + QCOMPARE(v4.w(), (qreal)0.0f); + QVERIFY(v4.isNull()); + v4 = v1; + QCOMPARE(v4.x(), (qreal)1.0f); + QCOMPARE(v4.y(), (qreal)2.5f); + QCOMPARE(v4.z(), (qreal)-89.25f); + QCOMPARE(v4.w(), (qreal)34.0f); + QVERIFY(!v4.isNull()); + + QVector4D v5(QPoint(1, 2)); + QCOMPARE(v5.x(), (qreal)1.0f); + QCOMPARE(v5.y(), (qreal)2.0f); + QCOMPARE(v5.z(), (qreal)0.0f); + QCOMPARE(v5.w(), (qreal)0.0f); + QVERIFY(!v5.isNull()); + + QVector4D v6(QPointF(1, 2.5)); + QCOMPARE(v6.x(), (qreal)1.0f); + QCOMPARE(v6.y(), (qreal)2.5f); + QCOMPARE(v6.z(), (qreal)0.0f); + QCOMPARE(v6.w(), (qreal)0.0f); + QVERIFY(!v6.isNull()); + + QVector4D v7(QVector2D(1.0f, 2.5f)); + QCOMPARE(v7.x(), (qreal)1.0f); + QCOMPARE(v7.y(), (qreal)2.5f); + QCOMPARE(v7.z(), (qreal)0.0f); + QCOMPARE(v7.w(), (qreal)0.0f); + QVERIFY(!v7.isNull()); + + QVector4D v8(QVector3D(1.0f, 2.5f, -89.25f)); + QCOMPARE(v8.x(), (qreal)1.0f); + QCOMPARE(v8.y(), (qreal)2.5f); + QCOMPARE(v8.z(), (qreal)-89.25f); + QCOMPARE(v8.w(), (qreal)0.0f); + QVERIFY(!v8.isNull()); + + QVector4D v9(QVector3D(1.0f, 2.5f, -89.25f), 34); + QCOMPARE(v9.x(), (qreal)1.0f); + QCOMPARE(v9.y(), (qreal)2.5f); + QCOMPARE(v9.z(), (qreal)-89.25f); + QCOMPARE(v9.w(), (qreal)34.0f); + QVERIFY(!v9.isNull()); + + QVector4D v10(QVector2D(1.0f, 2.5f), 23.5f, -8); + QCOMPARE(v10.x(), (qreal)1.0f); + QCOMPARE(v10.y(), (qreal)2.5f); + QCOMPARE(v10.z(), (qreal)23.5f); + QCOMPARE(v10.w(), (qreal)-8.0f); + QVERIFY(!v10.isNull()); + + v1.setX(3.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)2.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.w(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setY(10.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)-89.25f); + QCOMPARE(v1.w(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setZ(15.5f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)15.5f); + QCOMPARE(v1.w(), (qreal)34.0f); + QVERIFY(!v1.isNull()); + + v1.setW(6.0f); + QCOMPARE(v1.x(), (qreal)3.0f); + QCOMPARE(v1.y(), (qreal)10.5f); + QCOMPARE(v1.z(), (qreal)15.5f); + QCOMPARE(v1.w(), (qreal)6.0f); + QVERIFY(!v1.isNull()); + + v1.setX(0.0f); + v1.setY(0.0f); + v1.setZ(0.0f); + v1.setW(0.0f); + QCOMPARE(v1.x(), (qreal)0.0f); + QCOMPARE(v1.y(), (qreal)0.0f); + QCOMPARE(v1.z(), (qreal)0.0f); + QCOMPARE(v1.w(), (qreal)0.0f); + QVERIFY(v1.isNull()); + + QPoint p1 = v8.toPoint(); + QCOMPARE(p1.x(), 1); + QCOMPARE(p1.y(), 3); + + QPointF p2 = v8.toPointF(); + QCOMPARE((qreal)p2.x(), (qreal)1.0f); + QCOMPARE((qreal)p2.y(), (qreal)2.5f); + + QVector2D v11 = v8.toVector2D(); + QCOMPARE(v11.x(), (qreal)1.0f); + QCOMPARE(v11.y(), (qreal)2.5f); + + QVector3D v12 = v8.toVector3D(); + QCOMPARE(v12.x(), (qreal)1.0f); + QCOMPARE(v12.y(), (qreal)2.5f); + QCOMPARE(v12.z(), (qreal)-89.25f); + + QVector2D v13 = v9.toVector2DAffine(); + QVERIFY(fuzzyCompare(v13.x(), (qreal)(1.0f / 34.0f))); + QVERIFY(fuzzyCompare(v13.y(), (qreal)(2.5f / 34.0f))); + + QVector4D zerow(1.0f, 2.0f, 3.0f, 0.0f); + v13 = zerow.toVector2DAffine(); + QVERIFY(v13.isNull()); + + QVector3D v14 = v9.toVector3DAffine(); + QVERIFY(fuzzyCompare(v14.x(), (qreal)(1.0f / 34.0f))); + QVERIFY(fuzzyCompare(v14.y(), (qreal)(2.5f / 34.0f))); + QVERIFY(fuzzyCompare(v14.z(), (qreal)(-89.25f / 34.0f))); + + v14 = zerow.toVector3DAffine(); + QVERIFY(v14.isNull()); +} + +// Test vector length computation for 2D vectors. +void tst_QVector::length2_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("len"); + + QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; + QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; + QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)qSqrt(8.0f); +} +void tst_QVector::length2() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, len); + + QVector2D v(x, y); + QCOMPARE((float)(v.length()), (float)len); + QCOMPARE((float)(v.lengthSquared()), (float)(x * x + y * y)); +} + +// Test vector length computation for 3D vectors. +void tst_QVector::length3_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<qreal>("len"); + + QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; + QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; + QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)qSqrt(12.0f); +} +void tst_QVector::length3() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, len); + + QVector3D v(x, y, z); + QCOMPARE((float)(v.length()), (float)len); + QCOMPARE((float)(v.lengthSquared()), (float)(x * x + y * y + z * z)); +} + +// Test vector length computation for 4D vectors. +void tst_QVector::length4_data() +{ + QTest::addColumn<qreal>("x"); + QTest::addColumn<qreal>("y"); + QTest::addColumn<qreal>("z"); + QTest::addColumn<qreal>("w"); + QTest::addColumn<qreal>("len"); + + QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; + QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; + QTest::newRow("-1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; + QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)2.0f << (qreal)qSqrt(16.0f); +} +void tst_QVector::length4() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + QFETCH(qreal, len); + + QVector4D v(x, y, z, w); + QCOMPARE((float)(v.length()), (float)len); + QCOMPARE((float)(v.lengthSquared()), (float)(x * x + y * y + z * z + w * w)); +} + +// Test the unit vector conversion for 2D vectors. +void tst_QVector::normalized2_data() +{ + // Use the same test data as the length test. + length2_data(); +} +void tst_QVector::normalized2() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, len); + + QVector2D v(x, y); + QVector2D u = v.normalized(); + if (v.isNull()) + QVERIFY(u.isNull()); + else + QCOMPARE((float)(u.length()), (float)1.0f); + QCOMPARE((float)(u.x() * len), (float)(v.x())); + QCOMPARE((float)(u.y() * len), (float)(v.y())); +} + +// Test the unit vector conversion for 3D vectors. +void tst_QVector::normalized3_data() +{ + // Use the same test data as the length test. + length3_data(); +} +void tst_QVector::normalized3() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, len); + + QVector3D v(x, y, z); + QVector3D u = v.normalized(); + if (v.isNull()) + QVERIFY(u.isNull()); + else + QCOMPARE((float)(u.length()), (float)1.0f); + QCOMPARE((float)(u.x() * len), (float)(v.x())); + QCOMPARE((float)(u.y() * len), (float)(v.y())); + QCOMPARE((float)(u.z() * len), (float)(v.z())); +} + +// Test the unit vector conversion for 4D vectors. +void tst_QVector::normalized4_data() +{ + // Use the same test data as the length test. + length4_data(); +} +void tst_QVector::normalized4() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + QFETCH(qreal, len); + + QVector4D v(x, y, z, w); + QVector4D u = v.normalized(); + if (v.isNull()) + QVERIFY(u.isNull()); + else + QCOMPARE((float)(u.length()), (float)1.0f); + QCOMPARE((float)(u.x() * len), (float)(v.x())); + QCOMPARE((float)(u.y() * len), (float)(v.y())); + QCOMPARE((float)(u.z() * len), (float)(v.z())); + QCOMPARE((float)(u.w() * len), (float)(v.w())); +} + +// Test the unit vector conversion for 2D vectors. +void tst_QVector::normalize2_data() +{ + // Use the same test data as the length test. + length2_data(); +} +void tst_QVector::normalize2() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + + QVector2D v(x, y); + bool isNull = v.isNull(); + v.normalize(); + if (isNull) + QVERIFY(v.isNull()); + else + QCOMPARE((float)(v.length()), (float)1.0f); +} + +// Test the unit vector conversion for 3D vectors. +void tst_QVector::normalize3_data() +{ + // Use the same test data as the length test. + length3_data(); +} +void tst_QVector::normalize3() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + + QVector3D v(x, y, z); + bool isNull = v.isNull(); + v.normalize(); + if (isNull) + QVERIFY(v.isNull()); + else + QCOMPARE((float)(v.length()), (float)1.0f); +} + +// Test the unit vector conversion for 4D vectors. +void tst_QVector::normalize4_data() +{ + // Use the same test data as the length test. + length4_data(); +} +void tst_QVector::normalize4() +{ + QFETCH(qreal, x); + QFETCH(qreal, y); + QFETCH(qreal, z); + QFETCH(qreal, w); + + QVector4D v(x, y, z, w); + bool isNull = v.isNull(); + v.normalize(); + if (isNull) + QVERIFY(v.isNull()); + else + QCOMPARE((float)(v.length()), (float)1.0f); +} + +// Test the comparison operators for 2D vectors. +void tst_QVector::compare2() +{ + QVector2D v1(1, 2); + QVector2D v2(1, 2); + QVector2D v3(3, 2); + QVector2D v4(1, 3); + + QVERIFY(v1 == v2); + QVERIFY(v1 != v3); + QVERIFY(v1 != v4); +} + +// Test the comparison operators for 3D vectors. +void tst_QVector::compare3() +{ + QVector3D v1(1, 2, 4); + QVector3D v2(1, 2, 4); + QVector3D v3(3, 2, 4); + QVector3D v4(1, 3, 4); + QVector3D v5(1, 2, 3); + + QVERIFY(v1 == v2); + QVERIFY(v1 != v3); + QVERIFY(v1 != v4); + QVERIFY(v1 != v5); +} + +// Test the comparison operators for 4D vectors. +void tst_QVector::compare4() +{ + QVector4D v1(1, 2, 4, 8); + QVector4D v2(1, 2, 4, 8); + QVector4D v3(3, 2, 4, 8); + QVector4D v4(1, 3, 4, 8); + QVector4D v5(1, 2, 3, 8); + QVector4D v6(1, 2, 4, 3); + + QVERIFY(v1 == v2); + QVERIFY(v1 != v3); + QVERIFY(v1 != v4); + QVERIFY(v1 != v5); + QVERIFY(v1 != v6); +} + +// Test vector addition for 2D vectors. +void tst_QVector::add2_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f + << (qreal)3.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)3.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f + << (qreal)4.0f << (qreal)5.0f + << (qreal)5.0f << (qreal)7.0f; +} +void tst_QVector::add2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + QVector2D v3(x3, y3); + + QVERIFY((v1 + v2) == v3); + + QVector2D v4(v1); + v4 += v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() + v2.x()); + QCOMPARE(v4.y(), v1.y() + v2.y()); +} + +// Test vector addition for 3D vectors. +void tst_QVector::add3_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f + << (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f; +} +void tst_QVector::add3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QVERIFY((v1 + v2) == v3); + + QVector3D v4(v1); + v4 += v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() + v2.x()); + QCOMPARE(v4.y(), v1.y() + v2.y()); + QCOMPARE(v4.z(), v1.z() + v2.z()); +} + +// Test vector addition for 4D vectors. +void tst_QVector::add4_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("w3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f; + + QTest::newRow("wonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f + << (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f << (qreal)17.0f; +} +void tst_QVector::add4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, w3); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + QVector4D v3(x3, y3, z3, w3); + + QVERIFY((v1 + v2) == v3); + + QVector4D v4(v1); + v4 += v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() + v2.x()); + QCOMPARE(v4.y(), v1.y() + v2.y()); + QCOMPARE(v4.z(), v1.z() + v2.z()); + QCOMPARE(v4.w(), v1.w() + v2.w()); +} + +// Test vector subtraction for 2D vectors. +void tst_QVector::subtract2_data() +{ + // Use the same test data as the add test. + add2_data(); +} +void tst_QVector::subtract2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + QVector2D v3(x3, y3); + + QVERIFY((v3 - v1) == v2); + QVERIFY((v3 - v2) == v1); + + QVector2D v4(v3); + v4 -= v1; + QVERIFY(v4 == v2); + + QCOMPARE(v4.x(), v3.x() - v1.x()); + QCOMPARE(v4.y(), v3.y() - v1.y()); + + QVector2D v5(v3); + v5 -= v2; + QVERIFY(v5 == v1); + + QCOMPARE(v5.x(), v3.x() - v2.x()); + QCOMPARE(v5.y(), v3.y() - v2.y()); +} + +// Test vector subtraction for 3D vectors. +void tst_QVector::subtract3_data() +{ + // Use the same test data as the add test. + add3_data(); +} +void tst_QVector::subtract3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QVERIFY((v3 - v1) == v2); + QVERIFY((v3 - v2) == v1); + + QVector3D v4(v3); + v4 -= v1; + QVERIFY(v4 == v2); + + QCOMPARE(v4.x(), v3.x() - v1.x()); + QCOMPARE(v4.y(), v3.y() - v1.y()); + QCOMPARE(v4.z(), v3.z() - v1.z()); + + QVector3D v5(v3); + v5 -= v2; + QVERIFY(v5 == v1); + + QCOMPARE(v5.x(), v3.x() - v2.x()); + QCOMPARE(v5.y(), v3.y() - v2.y()); + QCOMPARE(v5.z(), v3.z() - v2.z()); +} + +// Test vector subtraction for 4D vectors. +void tst_QVector::subtract4_data() +{ + // Use the same test data as the add test. + add4_data(); +} +void tst_QVector::subtract4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, w3); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + QVector4D v3(x3, y3, z3, w3); + + QVERIFY((v3 - v1) == v2); + QVERIFY((v3 - v2) == v1); + + QVector4D v4(v3); + v4 -= v1; + QVERIFY(v4 == v2); + + QCOMPARE(v4.x(), v3.x() - v1.x()); + QCOMPARE(v4.y(), v3.y() - v1.y()); + QCOMPARE(v4.z(), v3.z() - v1.z()); + QCOMPARE(v4.w(), v3.w() - v1.w()); + + QVector4D v5(v3); + v5 -= v2; + QVERIFY(v5 == v1); + + QCOMPARE(v5.x(), v3.x() - v2.x()); + QCOMPARE(v5.y(), v3.y() - v2.y()); + QCOMPARE(v5.z(), v3.z() - v2.z()); + QCOMPARE(v5.w(), v3.w() - v2.w()); +} + +// Test component-wise vector multiplication for 2D vectors. +void tst_QVector::multiply2_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f + << (qreal)4.0f << (qreal)5.0f + << (qreal)4.0f << (qreal)10.0f; +} +void tst_QVector::multiply2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + QVector2D v3(x3, y3); + + QVERIFY((v1 * v2) == v3); + + QVector2D v4(v1); + v4 *= v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() * v2.x()); + QCOMPARE(v4.y(), v1.y() * v2.y()); +} + +// Test component-wise vector multiplication for 3D vectors. +void tst_QVector::multiply3_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f + << (qreal)4.0f << (qreal)10.0f << (qreal)-18.0f; +} +void tst_QVector::multiply3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QVERIFY((v1 * v2) == v3); + + QVector3D v4(v1); + v4 *= v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() * v2.x()); + QCOMPARE(v4.y(), v1.y() * v2.y()); + QCOMPARE(v4.z(), v1.z() * v2.z()); +} + +// Test component-wise vector multiplication for 4D vectors. +void tst_QVector::multiply4_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("w3"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("wonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f + << (qreal)4.0f << (qreal)10.0f << (qreal)-18.0f << (qreal)72.0f; +} +void tst_QVector::multiply4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, w3); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + QVector4D v3(x3, y3, z3, w3); + + QVERIFY((v1 * v2) == v3); + + QVector4D v4(v1); + v4 *= v2; + QVERIFY(v4 == v3); + + QCOMPARE(v4.x(), v1.x() * v2.x()); + QCOMPARE(v4.y(), v1.y() * v2.y()); + QCOMPARE(v4.z(), v1.z() * v2.z()); + QCOMPARE(v4.w(), v1.w() * v2.w()); +} + +// Test vector multiplication by a factor for 2D vectors. +void tst_QVector::multiplyFactor2_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f + << (qreal)100.0f + << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)4.0f; + + QTest::newRow("allzero") + << (qreal)1.0f << (qreal)2.0f + << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f; +} +void tst_QVector::multiplyFactor2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + + QVERIFY((v1 * factor) == v2); + QVERIFY((factor * v1) == v2); + + QVector2D v3(v1); + v3 *= factor; + QVERIFY(v3 == v2); + + QCOMPARE(v3.x(), v1.x() * factor); + QCOMPARE(v3.y(), v1.y() * factor); +} + +// Test vector multiplication by a factor for 3D vectors. +void tst_QVector::multiplyFactor3_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)100.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f; + + QTest::newRow("allzero") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f + << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; +} +void tst_QVector::multiplyFactor3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + + QVERIFY((v1 * factor) == v2); + QVERIFY((factor * v1) == v2); + + QVector3D v3(v1); + v3 *= factor; + QVERIFY(v3 == v2); + + QCOMPARE(v3.x(), v1.x() * factor); + QCOMPARE(v3.y(), v1.y() * factor); + QCOMPARE(v3.z(), v1.z() * factor); +} + +// Test vector multiplication by a factor for 4D vectors. +void tst_QVector::multiplyFactor4_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("factor"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)100.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("xonly") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("yonly") + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; + + QTest::newRow("zonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; + + QTest::newRow("wonly") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)2.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; + + QTest::newRow("all") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f + << (qreal)2.0f + << (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f << (qreal)8.0f; + + QTest::newRow("allzero") + << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f + << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; +} +void tst_QVector::multiplyFactor4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + + QVERIFY((v1 * factor) == v2); + QVERIFY((factor * v1) == v2); + + QVector4D v3(v1); + v3 *= factor; + QVERIFY(v3 == v2); + + QCOMPARE(v3.x(), v1.x() * factor); + QCOMPARE(v3.y(), v1.y() * factor); + QCOMPARE(v3.z(), v1.z() * factor); + QCOMPARE(v3.w(), v1.w() * factor); +} + +// Test vector division by a factor for 2D vectors. +void tst_QVector::divide2_data() +{ + // Use the same test data as the multiply test. + multiplyFactor2_data(); +} +void tst_QVector::divide2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + + if (factor == (qreal)0.0f) + return; + + QVERIFY((v2 / factor) == v1); + + QVector2D v3(v2); + v3 /= factor; + QVERIFY(v3 == v1); + + QCOMPARE(v3.x(), v2.x() / factor); + QCOMPARE(v3.y(), v2.y() / factor); +} + +// Test vector division by a factor for 3D vectors. +void tst_QVector::divide3_data() +{ + // Use the same test data as the multiply test. + multiplyFactor3_data(); +} +void tst_QVector::divide3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + + if (factor == (qreal)0.0f) + return; + + QVERIFY((v2 / factor) == v1); + + QVector3D v3(v2); + v3 /= factor; + QVERIFY(v3 == v1); + + QCOMPARE(v3.x(), v2.x() / factor); + QCOMPARE(v3.y(), v2.y() / factor); + QCOMPARE(v3.z(), v2.z() / factor); +} + +// Test vector division by a factor for 4D vectors. +void tst_QVector::divide4_data() +{ + // Use the same test data as the multiply test. + multiplyFactor4_data(); +} +void tst_QVector::divide4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, factor); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + + if (factor == (qreal)0.0f) + return; + + QVERIFY((v2 / factor) == v1); + + QVector4D v3(v2); + v3 /= factor; + QVERIFY(v3 == v1); + + QCOMPARE(v3.x(), v2.x() / factor); + QCOMPARE(v3.y(), v2.y() / factor); + QCOMPARE(v3.z(), v2.z() / factor); + QCOMPARE(v3.w(), v2.w() / factor); +} + +// Test vector negation for 2D vectors. +void tst_QVector::negate2_data() +{ + // Use the same test data as the add test. + add2_data(); +} +void tst_QVector::negate2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + + QVector2D v1(x1, y1); + QVector2D v2(-x1, -y1); + + QVERIFY(-v1 == v2); +} + +// Test vector negation for 3D vectors. +void tst_QVector::negate3_data() +{ + // Use the same test data as the add test. + add3_data(); +} +void tst_QVector::negate3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + + QVector3D v1(x1, y1, z1); + QVector3D v2(-x1, -y1, -z1); + + QVERIFY(-v1 == v2); +} + +// Test vector negation for 4D vectors. +void tst_QVector::negate4_data() +{ + // Use the same test data as the add test. + add4_data(); +} +void tst_QVector::negate4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(-x1, -y1, -z1, -w1); + + QVERIFY(-v1 == v2); +} + +// Test the computation of vector cross-products. +void tst_QVector::crossProduct_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("x3"); + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("dot"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("unitvec") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f; + + QTest::newRow("complex") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)6.0f + << (qreal)-3.0f << (qreal)6.0f << (qreal)-3.0f + << (qreal)32.0f; +} +void tst_QVector::crossProduct() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QVector3D v4 = QVector3D::crossProduct(v1, v2); + QVERIFY(v4 == v3); + + // Compute the cross-product long-hand and check again. + qreal xres = y1 * z2 - z1 * y2; + qreal yres = z1 * x2 - x1 * z2; + qreal zres = x1 * y2 - y1 * x2; + + QCOMPARE(v4.x(), xres); + QCOMPARE(v4.y(), yres); + QCOMPARE(v4.z(), zres); +} + +// Test the computation of normals. +void tst_QVector::normal_data() +{ + // Use the same test data as the crossProduct test. + crossProduct_data(); +} +void tst_QVector::normal() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QVERIFY(QVector3D::normal(v1, v2) == v3.normalized()); + QVERIFY(QVector3D::normal(QVector3D(), v1, v2) == v3.normalized()); + + QVector3D point(1.0f, 2.0f, 3.0f); + QVERIFY(QVector3D::normal(point, v1 + point, v2 + point) == v3.normalized()); +} + +// Test distance to plane calculations. +void tst_QVector::distanceToPlane_data() +{ + QTest::addColumn<qreal>("x1"); // Point on plane + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("x2"); // Normal to plane + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("x3"); // Point to test for distance + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("x4"); // Second point on plane + QTest::addColumn<qreal>("y4"); + QTest::addColumn<qreal>("z4"); + QTest::addColumn<qreal>("x5"); // Third point on plane + QTest::addColumn<qreal>("y5"); + QTest::addColumn<qreal>("z5"); + QTest::addColumn<qreal>("distance"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("above") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)2.0f; + + QTest::newRow("below") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)-1.0f << (qreal)1.0f << (qreal)-2.0f + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f + << (qreal)-2.0f; +} +void tst_QVector::distanceToPlane() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, x4); + QFETCH(qreal, y4); + QFETCH(qreal, z4); + QFETCH(qreal, x5); + QFETCH(qreal, y5); + QFETCH(qreal, z5); + QFETCH(qreal, distance); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + QVector3D v4(x4, y4, z4); + QVector3D v5(x5, y5, z5); + + QCOMPARE(v3.distanceToPlane(v1, v2), distance); + QCOMPARE(v3.distanceToPlane(v1, v4, v5), distance); +} + +// Test distance to line calculations. +void tst_QVector::distanceToLine_data() +{ + QTest::addColumn<qreal>("x1"); // Point on line + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("x2"); // Direction of the line + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("x3"); // Point to test for distance + QTest::addColumn<qreal>("y3"); + QTest::addColumn<qreal>("z3"); + QTest::addColumn<qreal>("distance"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("on line") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)5.0f + << (qreal)0.0f; + + QTest::newRow("off line") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)1.0f; + + QTest::newRow("off line 2") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f << (qreal)-2.0f << (qreal)0.0f + << (qreal)2.0f; + + QTest::newRow("points") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)5.0f << (qreal)0.0f + << (qreal)5.0f; +} +void tst_QVector::distanceToLine() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, distance); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + QVector3D v3(x3, y3, z3); + + QCOMPARE(v3.distanceToLine(v1, v2), distance); +} + +// Test the computation of dot products for 2D vectors. +void tst_QVector::dotProduct2_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("dot"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("unitvec") + << (qreal)1.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)1.0f + << (qreal)0.0f; + + QTest::newRow("complex") + << (qreal)1.0f << (qreal)2.0f + << (qreal)4.0f << (qreal)5.0f + << (qreal)14.0f; +} +void tst_QVector::dotProduct2() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, dot); + + QVector2D v1(x1, y1); + QVector2D v2(x2, y2); + + QVERIFY(QVector2D::dotProduct(v1, v2) == dot); + + // Compute the dot-product long-hand and check again. + qreal d = x1 * x2 + y1 * y2; + + QCOMPARE(QVector2D::dotProduct(v1, v2), d); +} + +// Test the computation of dot products for 3D vectors. +void tst_QVector::dotProduct3_data() +{ + // Use the same test data as the crossProduct test. + crossProduct_data(); +} +void tst_QVector::dotProduct3() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, x3); + QFETCH(qreal, y3); + QFETCH(qreal, z3); + QFETCH(qreal, dot); + + Q_UNUSED(x3); + Q_UNUSED(y3); + Q_UNUSED(z3); + + QVector3D v1(x1, y1, z1); + QVector3D v2(x2, y2, z2); + + QVERIFY(QVector3D::dotProduct(v1, v2) == dot); + + // Compute the dot-product long-hand and check again. + qreal d = x1 * x2 + y1 * y2 + z1 * z2; + + QCOMPARE(QVector3D::dotProduct(v1, v2), d); +} + +// Test the computation of dot products for 4D vectors. +void tst_QVector::dotProduct4_data() +{ + QTest::addColumn<qreal>("x1"); + QTest::addColumn<qreal>("y1"); + QTest::addColumn<qreal>("z1"); + QTest::addColumn<qreal>("w1"); + QTest::addColumn<qreal>("x2"); + QTest::addColumn<qreal>("y2"); + QTest::addColumn<qreal>("z2"); + QTest::addColumn<qreal>("w2"); + QTest::addColumn<qreal>("dot"); + + QTest::newRow("null") + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("unitvec") + << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f + << (qreal)0.0f; + + QTest::newRow("complex") + << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)4.0f + << (qreal)4.0f << (qreal)5.0f << (qreal)6.0f << (qreal)7.0f + << (qreal)60.0f; +} +void tst_QVector::dotProduct4() +{ + QFETCH(qreal, x1); + QFETCH(qreal, y1); + QFETCH(qreal, z1); + QFETCH(qreal, w1); + QFETCH(qreal, x2); + QFETCH(qreal, y2); + QFETCH(qreal, z2); + QFETCH(qreal, w2); + QFETCH(qreal, dot); + + QVector4D v1(x1, y1, z1, w1); + QVector4D v2(x2, y2, z2, w2); + + QVERIFY(QVector4D::dotProduct(v1, v2) == dot); + + // Compute the dot-product long-hand and check again. + qreal d = x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2; + + QCOMPARE(QVector4D::dotProduct(v1, v2), d); +} + +QTEST_APPLESS_MAIN(tst_QVector) + +#include "tst_qvectornd.moc" diff --git a/tests/auto/math3d/shared/math3dincludes.h b/tests/auto/math3d/shared/math3dincludes.h new file mode 100644 index 0000000..00d0f34 --- /dev/null +++ b/tests/auto/math3d/shared/math3dincludes.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MATH3DINCLUDES_H +#define MATH3DINCLUDES_H + +#include <QtGui/qmatrix4x4.h> +#include <QtGui/qgenericmatrix.h> +#include <QtGui/qvector2d.h> +#include <QtGui/qvector3d.h> +#include <QtGui/qvector4d.h> +#include <QtGui/qquaternion.h> + +#endif diff --git a/tests/auto/mediaobject/dummy/README b/tests/auto/mediaobject/dummy/README new file mode 100644 index 0000000..43f69d9 --- /dev/null +++ b/tests/auto/mediaobject/dummy/README @@ -0,0 +1 @@ +This is a dummy backend for phonon, used for testing purposes. diff --git a/tests/auto/mediaobject/dummy/audiooutput.cpp b/tests/auto/mediaobject/dummy/audiooutput.cpp new file mode 100644 index 0000000..41b473d --- /dev/null +++ b/tests/auto/mediaobject/dummy/audiooutput.cpp @@ -0,0 +1,53 @@ +#include "audiooutput.h" +#include "backend.h" +#include <phonon/audiooutput.h> + +QT_BEGIN_NAMESPACE + +namespace Phonon +{ +namespace Dummy +{ +AudioOutput::AudioOutput(Backend *backend, QObject *parent) + : QObject(parent) +{ + Q_UNUSED(backend) + + m_volumeLevel = 100; +} + +AudioOutput::~AudioOutput() +{ +} + +qreal AudioOutput::volume() const +{ + return m_volumeLevel; +} + +int AudioOutput::outputDevice() const +{ + return m_device; +} + +void AudioOutput::setVolume(qreal newVolume) +{ + m_volumeLevel = newVolume; + emit volumeChanged(newVolume); +} + +bool AudioOutput::setOutputDevice(int newDevice) +{ + return (newDevice == 0); +} + +bool AudioOutput::setOutputDevice(const AudioOutputDevice &newDevice) +{ + return setOutputDevice(newDevice.index()); +} + +} +} //namespace Phonon::Dummy + +QT_END_NAMESPACE +#include "moc_audiooutput.cpp" diff --git a/tests/auto/mediaobject/dummy/audiooutput.h b/tests/auto/mediaobject/dummy/audiooutput.h new file mode 100644 index 0000000..39efb55 --- /dev/null +++ b/tests/auto/mediaobject/dummy/audiooutput.h @@ -0,0 +1,41 @@ +#ifndef PHONON_DUMMY_AUDIOOUTPUT_H +#define PHONON_DUMMY_AUDIOOUTPUT_H + +#include "backend.h" +#include <phonon/audiooutputinterface.h> +#include <phonon/phononnamespace.h> + +QT_BEGIN_NAMESPACE + +namespace Phonon +{ +namespace Dummy +{ +class AudioOutput : public QObject, public AudioOutputInterface +{ + Q_OBJECT + Q_INTERFACES(Phonon::AudioOutputInterface) +public: + AudioOutput(Backend *backend, QObject *parent); + ~AudioOutput(); + + qreal volume() const; + int outputDevice() const; + void setVolume(qreal newVolume); + bool setOutputDevice(int newDevice); + bool setOutputDevice(const AudioOutputDevice &newDevice); + +Q_SIGNALS: + void volumeChanged(qreal newVolume); + void audioDeviceFailed(); + +private: + qreal m_volumeLevel; + int m_device; +}; +} +} //namespace Phonon::Dummy + +QT_END_NAMESPACE + +#endif // PHONON_DUMMY_AUDIOOUTPUT_H diff --git a/tests/auto/mediaobject/dummy/backend.cpp b/tests/auto/mediaobject/dummy/backend.cpp new file mode 100644 index 0000000..53f3896 --- /dev/null +++ b/tests/auto/mediaobject/dummy/backend.cpp @@ -0,0 +1,149 @@ +#include "audiooutput.h" +#include "mediaobject.h" +#include "videowidget.h" + +#include "backend.h" + +#include <QtCore/QSet> +#include <QtCore/QVariant> +#include <QtCore/QtPlugin> + +QT_BEGIN_NAMESPACE + +Q_EXPORT_PLUGIN2(phonon_dummy, Phonon::Dummy::Backend) + +namespace Phonon +{ +namespace Dummy +{ + +Backend::Backend(QObject *parent, const QVariantList &) + : QObject(parent) +{ + qWarning()<<"Using TEST Phonon backend"; +} + +Backend::~Backend() +{ +} + +/*** + * !reimp + */ +QObject *Backend::createObject(BackendInterface::Class c, QObject *parent, const QList<QVariant> &args) +{ + Q_UNUSED(args) + + switch (c) { + case MediaObjectClass: + return new MediaObject(this, parent); + + case AudioOutputClass: { + AudioOutput *ao = new AudioOutput(this, parent); + m_audioOutputs.append(ao); + return ao; + } + case VideoWidgetClass: { + QWidget *widget = qobject_cast<QWidget*>(parent); + return new VideoWidget(this, widget); + } + default: + qWarning("createObject() : Backend object not available"); + } + return 0; +} + +/*** + * !reimp + */ +QStringList Backend::availableMimeTypes() const +{ + QStringList availableMimeTypes; + // audio *.wav and *.mp3 files + availableMimeTypes << QLatin1String("audio/x-mp3"); + availableMimeTypes << QLatin1String("audio/x-wav"); + + // video *.ogv, *.mp4, *.avi (some) + + availableMimeTypes << QLatin1String("video/mpeg"); + availableMimeTypes << QLatin1String("video/ogg"); + availableMimeTypes << QLatin1String("video/mp4"); + + return availableMimeTypes; +} + +/*** + * !reimp + */ +QList<int> Backend::objectDescriptionIndexes(ObjectDescriptionType type) const +{ + QList<int> list; + + if(type == Phonon::AudioOutputDeviceType) + list.append(0); + + return list; +} + +/*** + * !reimp + */ +QHash<QByteArray, QVariant> Backend::objectDescriptionProperties(ObjectDescriptionType type, int index) const +{ + Q_UNUSED(index); + QHash<QByteArray, QVariant> ret; + + if(type == Phonon::AudioOutputDeviceType) + ret["name"] = QLatin1String("default audio device"); + + return ret; +} + +/*** + * !reimp + */ +bool Backend::startConnectionChange(QSet<QObject *> objects) +{ + Q_UNUSED(objects) + + return true; +} + +/*** + * !reimp + */ +bool Backend::connectNodes(QObject *source, QObject *sink) +{ + Q_UNUSED(source) + Q_UNUSED(sink) + + return true; +} + +/*** + * !reimp + */ +bool Backend::disconnectNodes(QObject *source, QObject *sink) +{ + Q_UNUSED(source) + Q_UNUSED(sink) + + return true; +} + +/*** + * !reimp + */ +bool Backend::endConnectionChange(QSet<QObject *> objects) +{ + Q_UNUSED(objects) + + return true; +} + +} +} + +QT_END_NAMESPACE + +#include "moc_backend.cpp" diff --git a/tests/auto/mediaobject/dummy/backend.h b/tests/auto/mediaobject/dummy/backend.h new file mode 100644 index 0000000..20af216 --- /dev/null +++ b/tests/auto/mediaobject/dummy/backend.h @@ -0,0 +1,55 @@ +#ifndef PHONON_DUMMY_BACKEND_H +#define PHONON_DUMMY_BACKEND_H + +#include <phonon/objectdescription.h> +#include <phonon/backendinterface.h> + +#include <phonon/medianode.h> + +#include <QtCore/QList> +#include <QtCore/QPointer> +#include <QtCore/QStringList> +#include <QtCore/QTimer> + +QT_BEGIN_NAMESPACE + +namespace Phonon +{ +namespace Dummy +{ +class AudioOutput; +class MediaObject; + +class Backend : public QObject, public BackendInterface +{ + Q_OBJECT + Q_INTERFACES(Phonon::BackendInterface) + +public: + Backend(QObject *parent = 0, const QVariantList & = QVariantList()); + virtual ~Backend(); + + QObject *createObject(BackendInterface::Class, QObject *parent, const QList<QVariant> &args); + + QStringList availableMimeTypes() const; + + QList<int> objectDescriptionIndexes(ObjectDescriptionType type) const; + QHash<QByteArray, QVariant> objectDescriptionProperties(ObjectDescriptionType type, int index) const; + + bool startConnectionChange(QSet<QObject *>); + bool connectNodes(QObject *, QObject *); + bool disconnectNodes(QObject *, QObject *); + bool endConnectionChange(QSet<QObject *>); + +Q_SIGNALS: + void objectDescriptionChanged(ObjectDescriptionType); + +private: + QList<QPointer<AudioOutput> > m_audioOutputs; +}; +} +} // namespace Phonon::Dummy + +QT_END_NAMESPACE + +#endif // PHONON_DUMMY_BACKEND_H diff --git a/tests/auto/mediaobject/dummy/dummy.pro b/tests/auto/mediaobject/dummy/dummy.pro new file mode 100644 index 0000000..b4f6109 --- /dev/null +++ b/tests/auto/mediaobject/dummy/dummy.pro @@ -0,0 +1,23 @@ +TEMPLATE = lib + +isEmpty(QT_MAJOR_VERSION) { + VERSION=4.5.2 +} else { + VERSION=$${QT_MAJOR_VERSION}.$${QT_MINOR_VERSION}.$${QT_PATCH_VERSION} +} +CONFIG += qt plugin + +TARGET = phonon_dummy +DESTDIR = $$QT_BUILD_TREE/plugins/phonon_backend +DEPENDPATH += . +INCLUDEPATH += . + +QT += phonon + + +# Input +HEADERS += backend.h audiooutput.h mediaobject.h videowidget.h +SOURCES += backend.cpp audiooutput.cpp mediaobject.cpp videowidget.cpp + +target.path = $$[QT_INSTALL_PLUGINS]/phonon_backend +INSTALLS += target diff --git a/tests/auto/mediaobject/dummy/mediaobject.cpp b/tests/auto/mediaobject/dummy/mediaobject.cpp new file mode 100644 index 0000000..521e3a6 --- /dev/null +++ b/tests/auto/mediaobject/dummy/mediaobject.cpp @@ -0,0 +1,397 @@ +#include "mediaobject.h" +#include "backend.h" + +#include <QtCore> +#include <QtCore/QTimer> +#include <QtCore/QVector> +#include <QtCore/QFile> +#include <QtCore/QByteRef> +#include <QtCore/QStringList> +#include <QtCore/QEvent> +#include <QApplication> + +QT_BEGIN_NAMESPACE + +namespace Phonon +{ +namespace Dummy +{ + +static const char* riffId = "RIFF"; + +MediaObject::MediaObject(Backend *backend, QObject *parent) + : QObject(parent) + , m_resumeState(false) + , m_oldState(Phonon::LoadingState) + , m_oldPos(0) + , currentPos(0) +{ + Q_UNUSED(backend) + + m_error = Phonon::NoError; + m_tickInterval = 100; // 100ms + m_totalTime = 26000; // 26s + m_prefinishMark = 0; + m_transitionTime = 100; //100ms + m_hasVideo = false; + m_prefinishMarkReachedNotEmitted = true; + m_aboutToFinishEmitted = false; + m_pendingState = Phonon::LoadingState; + m_state = Phonon::LoadingState; + m_pendingState = Phonon::LoadingState; + m_tickTimer = new QTimer(this); + connect(m_tickTimer, SIGNAL(timeout()), SLOT(emitTick())); +} + +MediaObject::~MediaObject() +{ + delete m_tickTimer; +} + +QString stateString(const Phonon::State &state) +{ + switch (state) { + case Phonon::LoadingState: + return QString("LoadingState"); + case Phonon::StoppedState: + return QString("StoppedState"); + case Phonon::PlayingState: + return QString("PlayingState"); + case Phonon::BufferingState: + return QString("BufferingState"); + case Phonon::PausedState: + return QString("PausedState"); + case Phonon::ErrorState: + return QString("ErrorState"); + } + return QString(); +} + +void MediaObject::saveState() +{ + if (m_resumeState) + return; + + if (m_pendingState == Phonon::PlayingState || m_pendingState == Phonon::PausedState) { + m_resumeState = true; + m_oldState = m_pendingState; + m_oldPos = currentPos; + } +} + +void MediaObject::resumeState() +{ + if (m_resumeState) + QMetaObject::invokeMethod(this, "setState", Qt::QueuedConnection, Q_ARG(State, m_oldState)); +} + +/** + * !reimp + */ +State MediaObject::state() const +{ + return m_state; +} + +/** + * !reimp + */ +bool MediaObject::hasVideo() const +{ + return m_hasVideo; +} + +/** + * !reimp + */ +bool MediaObject::isSeekable() const +{ + return true; +} + +/** + * !reimp + */ +qint64 MediaObject::currentTime() const +{ + if (m_resumeState) + return m_oldPos; + + switch (state()) { + case Phonon::PausedState: + case Phonon::BufferingState: + case Phonon::PlayingState: + return currentPos; + case Phonon::StoppedState: + case Phonon::LoadingState: + return 0; + case Phonon::ErrorState: + break; + } + return -1; +} + +/** + * !reimp + */ +qint32 MediaObject::tickInterval() const +{ + return m_tickInterval; +} + +/** + * !reimp + */ +void MediaObject::setTickInterval(qint32 newTickInterval) +{ + m_tickInterval = newTickInterval; + if (m_tickInterval <= 0) { + m_tickTimer->setInterval(100); + } else + m_tickTimer->setInterval(newTickInterval); +} + +/** + * !reimp + */ +void MediaObject::play() +{ + if(m_state == Phonon::PlayingState) + return; + if(m_state == Phonon::ErrorState) + return; + + if(m_state != Phonon::PausedState) + m_tickTimer->stop(); + + m_prefinishMarkReachedNotEmitted = true; + m_aboutToFinishEmitted = false; + + setState(Phonon::PlayingState); + m_resumeState = false; + m_tickTimer->start(); +} + +/** + * !reimp + */ +QString MediaObject::errorString() const +{ + return m_errorString; +} + +/** + * !reimp + */ +Phonon::ErrorType MediaObject::errorType() const +{ + return m_error; +} + +void MediaObject::setState(State newstate) +{ + if (m_state == newstate) + return; + + switch (newstate) { + case Phonon::PausedState: + m_pendingState = Phonon::PausedState; + emit stateChanged(newstate, m_state); + m_state = newstate; + break; + case Phonon::StoppedState: + m_pendingState = Phonon::StoppedState; + emit stateChanged(newstate, m_state); + m_state = newstate; + break; + case Phonon::PlayingState: + m_pendingState = Phonon::PlayingState; + emit stateChanged(newstate, m_state); + m_state = newstate; + break; + case Phonon::ErrorState: + emit stateChanged(newstate, m_state); + m_state = newstate; + break; + case Phonon::BufferingState: + case Phonon::LoadingState: + emit stateChanged(newstate, m_state); + m_state = newstate; + break; + } +} + +qint64 MediaObject::totalTime() const +{ + return m_totalTime; +} + +qint32 MediaObject::prefinishMark() const +{ + return m_prefinishMark; +} + +qint32 MediaObject::transitionTime() const +{ + return m_transitionTime; +} + +void MediaObject::setTransitionTime(qint32 time) +{ + m_transitionTime = time; +} + +qint64 MediaObject::remainingTime() const +{ + if(currentTime() > totalTime()) + return 0; + + return totalTime() - currentTime(); +} + +MediaSource MediaObject::source() const +{ + return m_source; +} + +void MediaObject::setNextSource(const MediaSource &source) +{ + if (source.type() == MediaSource::Invalid && + source.type() == MediaSource::Empty) + return; + m_nextSource = source; +} + +/* + * !reimp + */ +void MediaObject::setSource(const MediaSource &source) +{ + QMultiMap<QString, QString> ret; + + ret.insert(QLatin1String("ARTIST"), "Nokia Dude"); + ret.insert(QLatin1String("ALBUM"), "Sound of silence"); + ret.insert(QLatin1String("DATE"), "2009"); + + m_error = Phonon::NoError; + setState(Phonon::LoadingState); + + m_source = source; + currentPos = 0; + + if((source.fileName().contains(".avi")) || + (source.fileName().contains(".mp4"))) { + m_hasVideo = true; + emit hasVideoChanged(m_hasVideo); + } + if(source.fileName().contains(".wav")) { + QFile file(source.fileName()); + if (file.open(QIODevice::ReadOnly)) { + int len = file.read((char*)&header, sizeof(CombinedHeader)); + if(len == sizeof(CombinedHeader)) { + if(memcmp(&header.riff.descriptor.id, riffId, 4) != 0) { + // Not a valid wav file, to satisfy unit test for mediaobject + m_error = Phonon::FatalError; + //m_state = Phonon::ErrorState; + m_errorString = "Invalid wav file"; + setState(Phonon::ErrorState); + file.close(); + return; + } + } + file.close(); + } + } + emit metaDataChanged(ret); + emit currentSourceChanged(source); + emit totalTimeChanged(m_totalTime); + + setState(Phonon::StoppedState); +} + +void MediaObject::setPrefinishMark(qint32 newPrefinishMark) +{ + m_prefinishMark = newPrefinishMark; + if (currentTime() < totalTime() - m_prefinishMark) // not about to finish + m_prefinishMarkReachedNotEmitted = true; +} + +void MediaObject::pause() +{ + if (state() != Phonon::PausedState) + setState(Phonon::PausedState); + m_resumeState = false; + m_tickTimer->stop(); +} + +void MediaObject::stop() +{ + if (state() != Phonon::StoppedState) { + if(m_state != Phonon::ErrorState) { + setState(Phonon::StoppedState); + } + m_prefinishMarkReachedNotEmitted = true; + } + m_resumeState = false; + m_tickTimer->stop(); +} + +void MediaObject::emitTick() +{ + if (m_resumeState) { + return; + } + if(m_tickInterval > 0) + currentPos += m_tickInterval; + else + currentPos += 100; + + qint64 currentTime = currentPos; + qint64 totalTime = m_totalTime; + + if (m_tickInterval > 0 && currentTime != m_previousTickTime) { + emit tick(currentTime); + m_previousTickTime = currentTime; + } + if (m_state == Phonon::PlayingState) { + if (currentTime >= totalTime - m_prefinishMark) { + if (m_prefinishMarkReachedNotEmitted) { + m_prefinishMarkReachedNotEmitted = false; + emit prefinishMarkReached(totalTime - currentTime); + } + } + // Prepare load of next source + if (currentTime >= totalTime - 500) { + if (!m_aboutToFinishEmitted) { + m_aboutToFinishEmitted = true; // track is about to finish + emit aboutToFinish(); + } + } + if(currentTime >= totalTime) { + m_tickTimer->stop(); + if(m_nextSource.type() != MediaSource::Invalid && + m_nextSource.type() != MediaSource::Empty) { + setSource(m_nextSource); + m_nextSource = MediaSource(); + m_pendingState = Phonon::PlayingState; + } else { + setState(Phonon::PausedState); + currentPos = 0; + emit finished(); + } + } + } +} + +void MediaObject::seek(qint64 time) +{ + // We will assume no buffering in the object so this is not needed. + currentPos = time; +} + +} // ns Dummy +} // ns Phonon + +QT_END_NAMESPACE + +#include "moc_mediaobject.cpp" diff --git a/tests/auto/mediaobject/dummy/mediaobject.h b/tests/auto/mediaobject/dummy/mediaobject.h new file mode 100644 index 0000000..a87b32f --- /dev/null +++ b/tests/auto/mediaobject/dummy/mediaobject.h @@ -0,0 +1,169 @@ +#ifndef PHONON_DUMMY_MEDIAOBJECT_H +#define PHONON_DUMMY_MEDIAOBJECT_H + +#include "backend.h" +#include <phonon/mediaobjectinterface.h> +#include <phonon/addoninterface.h> + +#include <QtCore/QHash> +#include <QtCore/QString> +#include <QtCore/QVariant> +#include <QtCore/QObject> +#include <QtCore/QDate> +#include <QtCore/QEvent> +#include <QtCore/QUrl> + +QT_BEGIN_NAMESPACE + +class QTimer; +typedef QMultiMap<QString, QString> TagMap; + +namespace Phonon +{ +namespace Dummy +{ + +class VideoWidget; +class AudioPath; +class VideoPath; +class AudioOutput; + +class MediaObject : public QObject, public MediaObjectInterface +{ + friend class Stream; + Q_OBJECT + Q_INTERFACES(Phonon::MediaObjectInterface + ) + +public: + + MediaObject(Backend *backend, QObject *parent); + ~MediaObject(); + Phonon::State state() const; + + bool hasVideo() const; + bool isSeekable() const; + + qint64 currentTime() const; + qint32 tickInterval() const; + + void setTickInterval(qint32 newTickInterval); + + void play(); + void pause(); + void stop(); + void seek(qint64 time); + + QString errorString() const; + Phonon::ErrorType errorType() const; + + QUrl url() const; + qint64 totalTime() const; + + qint32 prefinishMark() const; + void setPrefinishMark(qint32 newPrefinishMark); + + qint32 transitionTime() const; + void setTransitionTime(qint32); + qint64 remainingTime() const; + + void setSource(const MediaSource &source); + void setNextSource(const MediaSource &source); + MediaSource source() const; + + void saveState(); + void resumeState(); + +public Q_SLOTS: + void setState(State); + +Q_SIGNALS: + void currentSourceChanged(const MediaSource &newSource); + void stateChanged(Phonon::State newstate, Phonon::State oldstate); + void tick(qint64 time); + void metaDataChanged(QMultiMap<QString, QString>); + void seekableChanged(bool); + void hasVideoChanged(bool); + + void finished(); + void prefinishMarkReached(qint32); + void aboutToFinish(); + void totalTimeChanged(qint64 length); + void bufferStatus(int percentFilled); + + QMultiMap<QString, QString> metaData(); + void setMetaData(QMultiMap<QString, QString> newData); + +private Q_SLOTS: + void emitTick(); + +private: + bool m_resumeState; + State m_oldState; + quint64 m_oldPos; + quint64 currentPos; + bool m_hasVideo; + qint32 m_tickInterval; + QTimer *m_tickTimer; + Phonon::ErrorType m_error; + QString m_errorString; + qint64 m_totalTime; + qint32 m_prefinishMark; + qint32 m_transitionTime; + MediaSource m_source; + MediaSource m_nextSource; + bool m_prefinishMarkReachedNotEmitted; + bool m_aboutToFinishEmitted; + int m_previousTickTime; + + State m_state; + State m_pendingState; + + struct chunk + { + char id[4]; + quint32 size; + }; + + struct RIFFHeader + { + chunk descriptor; + char type[4]; + }; + + struct WAVEHeader + { + chunk descriptor; + quint16 audioFormat; // PCM = 1 + quint16 numChannels; + quint32 sampleRate; + quint32 byteRate; + quint16 blockAlign; + quint16 bitsPerSample; + quint32 xFreq1; + chunk fact; + quint32 xfact; + chunk data; + }; + + struct DATAHeader + { + chunk descriptor; + quint8 data[]; + }; + + struct CombinedHeader + { + RIFFHeader riff; + WAVEHeader wave; + DATAHeader data; + }; + + CombinedHeader header; +}; +} +} //namespace Phonon::Dummy + +QT_END_NAMESPACE + +#endif // PHONON_DUMMY_MEDIAOBJECT_H diff --git a/tests/auto/mediaobject/dummy/videowidget.cpp b/tests/auto/mediaobject/dummy/videowidget.cpp new file mode 100644 index 0000000..890363f --- /dev/null +++ b/tests/auto/mediaobject/dummy/videowidget.cpp @@ -0,0 +1,205 @@ +#include "videowidget.h" +#include <QtCore/QEvent> +#include <QtGui/QResizeEvent> +#include <QtGui/QPalette> +#include <QtGui/QImage> +#include <QtGui/QPainter> +#include <QtGui/QBoxLayout> +#include <QApplication> +#include "mediaobject.h" + +QT_BEGIN_NAMESPACE + +namespace Phonon +{ +namespace Dummy +{ + +VideoWidget::VideoWidget(Backend *backend, QWidget *parent) : + QWidget(parent), + m_aspectRatio(Phonon::VideoWidget::AspectRatioAuto), + m_brightness(0.0), + m_hue(0.0), + m_contrast(0.0), + m_saturation(0.0), + m_scaleMode(Phonon::VideoWidget::FitInView) +{ + Q_UNUSED(backend) + +} + +VideoWidget::~VideoWidget() +{ +} + +void VideoWidget::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) +} + +void VideoWidget::setVisible(bool val) +{ + Q_UNUSED(val) +} + +Phonon::VideoWidget::AspectRatio VideoWidget::aspectRatio() const +{ + return m_aspectRatio; +} + +QSize VideoWidget::sizeHint() const +{ + return QSize(640, 480); +} + +void VideoWidget::setAspectRatio(Phonon::VideoWidget::AspectRatio aspectRatio) +{ + Q_UNUSED(aspectRatio) +} + +Phonon::VideoWidget::ScaleMode VideoWidget::scaleMode() const +{ + return m_scaleMode; +} + +QRect VideoWidget::scaleToAspect(QRect srcRect, int w, int h) const +{ + float width = srcRect.width(); + float height = srcRect.width() * (float(h) / float(w)); + if (height > srcRect.height()) { + height = srcRect.height(); + width = srcRect.height() * (float(w) / float(h)); + } + return QRect(0, 0, (int)width, (int)height); +} + +/*** + * Calculates the actual rectangle the movie will be presented with + **/ +QRect VideoWidget::calculateDrawFrameRect() const +{ + QRect widgetRect = rect(); + QRect drawFrameRect; + // Set m_drawFrameRect to be the size of the smallest possible + // rect conforming to the aspect and containing the whole frame: + switch (aspectRatio()) { + + case Phonon::VideoWidget::AspectRatioWidget: + drawFrameRect = widgetRect; + // No more calculations needed. + return drawFrameRect; + + case Phonon::VideoWidget::AspectRatio4_3: + drawFrameRect = scaleToAspect(widgetRect, 4, 3); + break; + + case Phonon::VideoWidget::AspectRatio16_9: + drawFrameRect = scaleToAspect(widgetRect, 16, 9); + break; + + case Phonon::VideoWidget::AspectRatioAuto: + default: + drawFrameRect = QRect(0, 0, movieSize().width(), movieSize().height()); + break; + } + + // Scale m_drawFrameRect to fill the widget + // without breaking aspect: + float widgetWidth = widgetRect.width(); + float widgetHeight = widgetRect.height(); + float frameWidth = widgetWidth; + float frameHeight = drawFrameRect.height() * float(widgetWidth) / float(drawFrameRect.width()); + + switch (scaleMode()) { + case Phonon::VideoWidget::ScaleAndCrop: + if (frameHeight < widgetHeight) { + frameWidth *= float(widgetHeight) / float(frameHeight); + frameHeight = widgetHeight; + } + break; + case Phonon::VideoWidget::FitInView: + default: + if (frameHeight > widgetHeight) { + frameWidth *= float(widgetHeight) / float(frameHeight); + frameHeight = widgetHeight; + } + break; + } + drawFrameRect.setSize(QSize(int(frameWidth), int(frameHeight))); + drawFrameRect.moveTo(int((widgetWidth - frameWidth) / 2.0f), + int((widgetHeight - frameHeight) / 2.0f)); + return drawFrameRect; +} + +void VideoWidget::setScaleMode(Phonon::VideoWidget::ScaleMode scaleMode) +{ + Q_UNUSED(scaleMode) +} + +qreal VideoWidget::brightness() const +{ + return m_brightness; +} + +qreal clampedValue(qreal val) +{ + if (val > 1.0 ) + return 1.0; + else if (val < -1.0) + return -1.0; + else return val; +} + +void VideoWidget::setBrightness(qreal newValue) +{ + Q_UNUSED(newValue) +} + +qreal VideoWidget::contrast() const +{ + return m_contrast; +} + +void VideoWidget::setContrast(qreal newValue) +{ + Q_UNUSED(newValue) +} + +qreal VideoWidget::hue() const +{ + return m_hue; +} + +void VideoWidget::setHue(qreal newValue) +{ + Q_UNUSED(newValue) +} + +qreal VideoWidget::saturation() const +{ + return m_saturation; +} + +void VideoWidget::setSaturation(qreal newValue) +{ + Q_UNUSED(newValue) +} + +bool VideoWidget::event(QEvent *event) +{ + return QWidget::event(event); +} + +void VideoWidget::setMovieSize(const QSize &size) +{ + m_movieSize = size; + widget()->updateGeometry(); + widget()->update(); +} + +} +} //namespace Phonon::Dummy + +QT_END_NAMESPACE + +#include "moc_videowidget.cpp" diff --git a/tests/auto/mediaobject/dummy/videowidget.h b/tests/auto/mediaobject/dummy/videowidget.h new file mode 100644 index 0000000..2e5a2b8 --- /dev/null +++ b/tests/auto/mediaobject/dummy/videowidget.h @@ -0,0 +1,70 @@ +#ifndef PHONON_DUMMY_VIDEOWIDGET_H +#define PHONON_DUMMY_VIDEOWIDGET_H + +#include <phonon/videowidget.h> +#include <phonon/videowidgetinterface.h> + +#include "backend.h" + +QT_BEGIN_NAMESPACE + +class QString; + +namespace Phonon +{ +namespace Dummy +{ + +class VideoWidget : public QWidget, public Phonon::VideoWidgetInterface +{ + Q_OBJECT + Q_INTERFACES(Phonon::VideoWidgetInterface) +public: + VideoWidget(Backend *backend, QWidget *parent = 0); + ~VideoWidget(); + + void paintEvent(QPaintEvent *event); + void setVisible(bool); + + Phonon::VideoWidget::AspectRatio aspectRatio() const; + void setAspectRatio(Phonon::VideoWidget::AspectRatio aspectRatio); + Phonon::VideoWidget::ScaleMode scaleMode() const; + void setScaleMode(Phonon::VideoWidget::ScaleMode); + qreal brightness() const; + void setBrightness(qreal); + qreal contrast() const; + void setContrast(qreal); + qreal hue() const; + void setHue(qreal); + qreal saturation() const; + void setSaturation(qreal); + void setMovieSize(const QSize &size); + QSize sizeHint() const; + QRect scaleToAspect(QRect srcRect, int w, int h) const; + QRect calculateDrawFrameRect() const; + + QSize movieSize() const { + return m_movieSize; + } + + bool event(QEvent *); + + QWidget *widget() { + return this; + } + +protected: + QSize m_movieSize; + +private: + Phonon::VideoWidget::AspectRatio m_aspectRatio; + qreal m_brightness, m_hue, m_contrast, m_saturation; + Phonon::VideoWidget::ScaleMode m_scaleMode; +}; + +} +} //namespace Phonon::Dummy + +QT_END_NAMESPACE + +#endif // PHONON_DUMMY_VIDEOWIDGET_H diff --git a/tests/auto/mediaobject/tst_mediaobject.cpp b/tests/auto/mediaobject/tst_mediaobject.cpp index c1a69f7..f04a4d7 100644 --- a/tests/auto/mediaobject/tst_mediaobject.cpp +++ b/tests/auto/mediaobject/tst_mediaobject.cpp @@ -148,6 +148,7 @@ class tst_MediaObject : public QObject void testPlayBeforeFinish(); void testPauseOnFinish(); void testReconnectBetweenTwoMediaObjects(); + void volumeSliderMuteVisibility(); void cleanupTestCase(); private: void _startPlayback(Phonon::State currentState = Phonon::StoppedState); @@ -937,6 +938,28 @@ void tst_MediaObject::_testOneSeek(qint64 seekTo) m_success = true; } +void tst_MediaObject::volumeSliderMuteVisibility() +{ + //this test doesn't really belong to mediaobject + // ### see if we should create a realy Phonon::VolumeSlider autotest + Phonon::VolumeSlider slider; + QVERIFY(slider.isMuteVisible()); // that is the default value + slider.setMuteVisible(true); + QVERIFY(slider.isMuteVisible()); + + //let's check that changing the visibility of the slider itself + //doesn't change what the slider reports + slider.setVisible(false); + QVERIFY(slider.isMuteVisible()); + slider.setVisible(true); + + slider.setMuteVisible(false); + QVERIFY(!slider.isMuteVisible()); + slider.setMuteVisible(true); + QVERIFY(slider.isMuteVisible()); +} + + #endif //QT_NO_PHONON diff --git a/tests/auto/modeltest/modeltest.cpp b/tests/auto/modeltest/modeltest.cpp index 68b723f..c4cc820 100644 --- a/tests/auto/modeltest/modeltest.cpp +++ b/tests/auto/modeltest/modeltest.cpp @@ -475,7 +475,7 @@ void ModelTest::data() void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int end ) { // Q_UNUSED(end); - qDebug() << "rowsAboutToBeInserted" << "start=" << start << "end=" << end << "parent=" << model->data ( parent ).value<QString>() + qDebug() << "rowsAboutToBeInserted" << "start=" << start << "end=" << end << "parent=" << model->data ( parent ).toString() << "current count of parent=" << model->rowCount ( parent ); // << "display of last=" << model->data( model->index(start-1, 0, parent) ); // qDebug() << model->index(start-1, 0, parent) << model->data( model->index(start-1, 0, parent) ); Changing c; @@ -496,7 +496,7 @@ void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end ) Changing c = insert.pop(); Q_ASSERT ( c.parent == parent ); qDebug() << "rowsInserted" << "start=" << start << "end=" << end << "oldsize=" << c.oldSize - << "parent=" << model->data ( parent ).value<QString>() << "current rowcount of parent=" << model->rowCount ( parent ); + << "parent=" << model->data ( parent ).toString() << "current rowcount of parent=" << model->rowCount ( parent ); for (int ii=start; ii <= end; ii++) { diff --git a/tests/auto/network-settings.h b/tests/auto/network-settings.h index 7351443..9488fda 100644 --- a/tests/auto/network-settings.h +++ b/tests/auto/network-settings.h @@ -1,6 +1,4 @@ /**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the test suite of the Qt Toolkit. diff --git a/tests/auto/q3filedialog/tst_q3filedialog.cpp b/tests/auto/q3filedialog/tst_q3filedialog.cpp index 3b41947..e1050a0 100644 --- a/tests/auto/q3filedialog/tst_q3filedialog.cpp +++ b/tests/auto/q3filedialog/tst_q3filedialog.cpp @@ -115,13 +115,15 @@ void tst_Q3FileDialog::getSetCheck() obj1.setPreviewMode(Q3FileDialog::PreviewMode(Q3FileDialog::NoPreview)); QCOMPARE(obj1.previewMode(), Q3FileDialog::PreviewMode(Q3FileDialog::NoPreview)); - // Note: Q3FileDialog does not update the previewMode read-state until the - // user has actually started navigating to a file that has a functioning - // preview. + obj1.setContentsPreviewEnabled(true); + obj1.setInfoPreviewEnabled(false); obj1.setPreviewMode(Q3FileDialog::PreviewMode(Q3FileDialog::Contents)); - QCOMPARE(obj1.previewMode(), Q3FileDialog::PreviewMode(Q3FileDialog::NoPreview)); + QCOMPARE(obj1.previewMode(), Q3FileDialog::PreviewMode(Q3FileDialog::Contents)); + + obj1.setInfoPreviewEnabled(true); + obj1.setContentsPreviewEnabled(false); obj1.setPreviewMode(Q3FileDialog::PreviewMode(Q3FileDialog::Info)); - QCOMPARE(obj1.previewMode(), Q3FileDialog::PreviewMode(Q3FileDialog::NoPreview)); + QCOMPARE(obj1.previewMode(), Q3FileDialog::PreviewMode(Q3FileDialog::Info)); } QTEST_MAIN(tst_Q3FileDialog) diff --git a/tests/auto/q3sqlcursor/tst_q3sqlcursor.cpp b/tests/auto/q3sqlcursor/tst_q3sqlcursor.cpp index 360c3b7..020f41d 100644 --- a/tests/auto/q3sqlcursor/tst_q3sqlcursor.cpp +++ b/tests/auto/q3sqlcursor/tst_q3sqlcursor.cpp @@ -132,10 +132,11 @@ void tst_Q3SqlCursor::createTestTables( QSqlDatabase db ) if ( !db.isValid() ) return; QSqlQuery q( db ); + if (tst_Databases::isSqlServer(db)) { QVERIFY_SQL(q, exec("SET ANSI_DEFAULTS ON")); - QVERIFY_SQL(q, exec("SET IMPLICIT_TRANSACTIONS OFF")); } + // please never ever change this table; otherwise fix all tests ;) if ( tst_Databases::isMSAccess( db ) ) { QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null," @@ -758,13 +759,19 @@ void tst_Q3SqlCursor::insertFieldNameContainsWS() { QString tableName = qTableName("qtestws"); QSqlQuery q(db); - q.exec(QString("DROP TABLE %1").arg(tableName)); + tst_Databases::safeDropTable(db, tableName); + QString query = QString("CREATE TABLE %1 (id int, \"first Name\" varchar(20), " + "lastName varchar(20))"); + QVERIFY_SQL(q, exec(query.arg(tableName))); + QString query = QString("CREATE TABLE %1 (id int, \"first Name\" varchar(20), " + "lastName varchar(20))").arg(tableName); + QVERIFY_SQL(q, exec(query)); QString query = "CREATE TABLE %1 (id int, " + db.driver()->escapeIdentifier("first Name", QSqlDriver::FieldName) + " varchar(20), lastName varchar(20))"; QVERIFY_SQL(q, exec(query.arg(tableName))); - Q3SqlCursor cur(QString("%1").arg(tableName), true, db); + Q3SqlCursor cur(tableName, true, db); cur.select(); QSqlRecord *r = cur.primeInsert(); @@ -780,8 +787,8 @@ void tst_Q3SqlCursor::insertFieldNameContainsWS() { QVERIFY(cur.value(0) == 1); QCOMPARE(cur.value(1).toString(), QString("Kong")); QCOMPARE(cur.value(2).toString(), QString("Harald")); - - q.exec(QString("DROP TABLE %1").arg(tableName)); + + tst_Databases::safeDropTable(db, tableName); } diff --git a/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp b/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp index d026cbf..ec1f805 100644 --- a/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp +++ b/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp @@ -114,7 +114,7 @@ void tst_Q3SqlSelectCursor::createTestTables( QSqlDatabase db ) void tst_Q3SqlSelectCursor::dropTestTables( QSqlDatabase db ) { - tst_Databases::safeDropTables( db, QStringList() << qTableName( "qtest" ) ); + tst_Databases::safeDropTable( db, qTableName( "qtest" ) ); } void tst_Q3SqlSelectCursor::populateTestTables( QSqlDatabase db ) diff --git a/tests/auto/q3tabdialog/tst_q3tabdialog.cpp b/tests/auto/q3tabdialog/tst_q3tabdialog.cpp index a51db51..72e0000 100644 --- a/tests/auto/q3tabdialog/tst_q3tabdialog.cpp +++ b/tests/auto/q3tabdialog/tst_q3tabdialog.cpp @@ -60,6 +60,7 @@ public: private slots: void getSetCheck(); + void task245918_show(); }; tst_Q3TabDialog::tst_Q3TabDialog() @@ -95,5 +96,32 @@ void tst_Q3TabDialog::getSetCheck() delete var1; } +class task245918_Dialog : public Q3TabDialog +{ + Q_OBJECT +public: + task245918_Dialog() + { + QTimer::singleShot(100, this, SLOT(closeWhenVisible())); + } + + private slots: + void closeWhenVisible() + { + if (isVisible()) + accept(); + else + QTimer::singleShot(100, this, SLOT(closeWhenVisible())); + } +}; + +void tst_Q3TabDialog::task245918_show() +{ + task245918_Dialog dialog; + QSignalSpy spy(&dialog, SIGNAL(aboutToShow())); + dialog.exec(); + QCOMPARE(spy.count(), 1); +} + QTEST_MAIN(tst_Q3TabDialog) #include "tst_q3tabdialog.moc" diff --git a/tests/auto/qaccessibility_mac/tst_qaccessibility_mac.cpp b/tests/auto/qaccessibility_mac/tst_qaccessibility_mac.cpp index b81fa12..deb6c8d 100644 --- a/tests/auto/qaccessibility_mac/tst_qaccessibility_mac.cpp +++ b/tests/auto/qaccessibility_mac/tst_qaccessibility_mac.cpp @@ -833,9 +833,14 @@ void tst_accessibility_mac::testTabWidget() // Window is not reported properly on 10.5 if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_5) { QVERIFY(equal(window(tabGroup), form)); - QVERIFY(equal(window(tabButton1), form)); + + // ### hangs on 10.4 +#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) + QVERIFY(equal(window(tabButton1), form)); +#endif } -#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4) +// ### hangs on 10.4 +#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) QVERIFY(equal(topLevelUIElement(tabGroup), form)); QVERIFY(equal(topLevelUIElement(tabButton1), form)); #endif @@ -1423,7 +1428,6 @@ void tst_accessibility_mac::testListView() const AXUIElementRef listElement = childByRole(scrollAreaElement, "AXList"); QVERIFY(listElement); QVERIFY(equal(::parent(listElement), scrollAreaElement)); - // Window is not reported properly on 10.5 if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_5) QVERIFY(equal(::window(listElement), windowElement)); @@ -1438,9 +1442,11 @@ void tst_accessibility_mac::testListView() QVERIFY(value(A) == "A"); QVERIFY(equal(::parent(A), listElement)); QVERIFY(enabled(A)); - // Window is not reported properly on 10.5 - if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_5) - QVERIFY(equal(::window(A), windowElement)); + + // Window is not reported properly on 10.5, this test + // hangs on 10.4. Disable it for now. + // if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_5) + // QVERIFY(equal(::window(A), windowElement)); QVERIFY(above(A, B)); QVERIFY(!above(B, A)); diff --git a/tests/auto/qaction/tst_qaction.cpp b/tests/auto/qaction/tst_qaction.cpp index e17af10..cdf5071 100644 --- a/tests/auto/qaction/tst_qaction.cpp +++ b/tests/auto/qaction/tst_qaction.cpp @@ -75,6 +75,8 @@ private slots: void alternateShortcuts(); void enabledVisibleInteraction(); void task200823_tooltip(); + void task229128TriggeredSignalWithoutActiongroup(); + void task229128TriggeredSignalWhenInActiongroup(); private: int m_lastEventType; @@ -109,7 +111,7 @@ class MyWidget : public QWidget { Q_OBJECT public: - MyWidget(tst_QAction *tst, QWidget *parent=0) : QWidget(parent) { this->tst = tst; } + MyWidget(tst_QAction *tst, QWidget *parent = 0) : QWidget(parent) { this->tst = tst; } protected: virtual void actionEvent(QActionEvent *e) { tst->updateState(e); } @@ -141,7 +143,7 @@ void tst_QAction::initTestCase() void tst_QAction::cleanupTestCase() { QWidget *testWidget = m_tstWidget; - if ( testWidget ) { + if (testWidget) { testWidget->hide(); delete testWidget; } @@ -189,7 +191,7 @@ void tst_QAction::updateState(QActionEvent *e) { if (!e) { m_lastEventType = 0; - m_lastAction = 0; + m_lastAction = 0; } else { m_lastEventType = (int)e->type(); m_lastAction = e->action(); @@ -249,12 +251,12 @@ void tst_QAction::alternateShortcuts() { //test the alternate shortcuts (by adding more than 1 shortcut) - QWidget *wid=m_tstWidget; + QWidget *wid = m_tstWidget; { QAction act(wid); wid->addAction(&act); - QList<QKeySequence> shlist= QList<QKeySequence>() << QKeySequence("CTRL+P") <<QKeySequence("CTRL+A"); + QList<QKeySequence> shlist = QList<QKeySequence>() << QKeySequence("CTRL+P") << QKeySequence("CTRL+A"); act.setShortcuts(shlist); QSignalSpy spy(&act, SIGNAL(triggered())); @@ -322,5 +324,46 @@ void tst_QAction::task200823_tooltip() QCOMPARE(action->toolTip(), ref); } +void tst_QAction::task229128TriggeredSignalWithoutActiongroup() +{ + // test without a group + QAction *actionWithoutGroup = new QAction("Test", qApp); + QSignalSpy spyWithoutGroup(actionWithoutGroup, SIGNAL(triggered(bool))); + QCOMPARE(spyWithoutGroup.count(), 0); + actionWithoutGroup->trigger(); + // signal should be emitted + QCOMPARE(spyWithoutGroup.count(), 1); + + // it is now a checkable checked action + actionWithoutGroup->setCheckable(true); + actionWithoutGroup->setChecked(true); + spyWithoutGroup.clear(); + QCOMPARE(spyWithoutGroup.count(), 0); + actionWithoutGroup->trigger(); + // signal should be emitted + QCOMPARE(spyWithoutGroup.count(), 1); +} + +void tst_QAction::task229128TriggeredSignalWhenInActiongroup() +{ + QActionGroup ag(0); + QAction *action = new QAction("Test", &ag); + QAction *checkedAction = new QAction("Test 2", &ag); + ag.addAction(action); + action->setCheckable(true); + ag.addAction(checkedAction); + checkedAction->setCheckable(true); + checkedAction->setChecked(true); + + QSignalSpy actionSpy(checkedAction, SIGNAL(triggered(bool))); + QSignalSpy actionGroupSpy(&ag, SIGNAL(triggered(QAction *))); + QCOMPARE(actionGroupSpy.count(), 0); + QCOMPARE(actionSpy.count(), 0); + checkedAction->trigger(); + // check that both the group and the action have emitted the signal + QCOMPARE(actionGroupSpy.count(), 1); + QCOMPARE(actionSpy.count(), 1); +} + QTEST_MAIN(tst_QAction) #include "tst_qaction.moc" diff --git a/tests/auto/qanimationgroup/qanimationgroup.pro b/tests/auto/qanimationgroup/qanimationgroup.pro new file mode 100644 index 0000000..97d33dd --- /dev/null +++ b/tests/auto/qanimationgroup/qanimationgroup.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +QT = core gui +SOURCES += tst_qanimationgroup.cpp + + diff --git a/tests/auto/qanimationgroup/tst_qanimationgroup.cpp b/tests/auto/qanimationgroup/tst_qanimationgroup.cpp new file mode 100644 index 0000000..0e98797 --- /dev/null +++ b/tests/auto/qanimationgroup/tst_qanimationgroup.cpp @@ -0,0 +1,413 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <QtCore/qanimationgroup.h> +#include <QtCore/qsequentialanimationgroup.h> +#include <QtCore/qparallelanimationgroup.h> + +//TESTED_CLASS=QAnimationGroup +//TESTED_FILES= + +Q_DECLARE_METATYPE(QAbstractAnimation::State) + +class tst_QAnimationGroup : public QObject +{ + Q_OBJECT +public: + tst_QAnimationGroup(); + virtual ~tst_QAnimationGroup(); + +public Q_SLOTS: + void init(); + void cleanup(); + +private slots: + void construction(); + void emptyGroup(); + void setCurrentTime(); + void statesAndSignals(); + void setParentAutoAdd(); + void beginNestedGroup(); + void addChildTwice(); + void loopWithoutStartValue(); +}; + +tst_QAnimationGroup::tst_QAnimationGroup() +{ +} + +tst_QAnimationGroup::~tst_QAnimationGroup() +{ +} + +void tst_QAnimationGroup::init() +{ + qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State"); +} + +void tst_QAnimationGroup::cleanup() +{ +} + +void tst_QAnimationGroup::construction() +{ + QSequentialAnimationGroup animationgroup; +} + +class AnimationObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int value READ value WRITE setValue) +public: + AnimationObject(int startValue = 0) + : v(startValue) + { } + + int value() const { return v; } + void setValue(int value) { v = value; } + + int v; +}; + +class TestAnimation : public QVariantAnimation +{ + Q_OBJECT +public: + virtual void updateCurrentValue(const QVariant &value) { Q_UNUSED(value)}; + virtual void updateState(QAbstractAnimation::State oldState, + QAbstractAnimation::State newState) + { + Q_UNUSED(oldState) + Q_UNUSED(newState) + }; +}; + +class UncontrolledAnimation : public QPropertyAnimation +{ + Q_OBJECT +public: + UncontrolledAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = 0) + : QPropertyAnimation(target, propertyName, parent), id(0) + { + setDuration(250); + } + + int duration() const { return -1; /* not time driven */ } + +protected: + void timerEvent(QTimerEvent *event) + { + if (event->timerId() == id) + stop(); + } + + void updateRunning(bool running) + { + if (running) { + id = startTimer(500); + } else { + killTimer(id); + id = 0; + } + } + +private: + int id; +}; + +void tst_QAnimationGroup::emptyGroup() +{ + QSequentialAnimationGroup group; + QSignalSpy groupStateChangedSpy(&group, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + group.start(); + + QCOMPARE(groupStateChangedSpy.count(), 2); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(1).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + + QTest::ignoreMessage(QtWarningMsg, "QAbstractAnimation::pause: Cannot pause a stopped animation"); + group.pause(); + + QCOMPARE(groupStateChangedSpy.count(), 2); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + + group.start(); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(2).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(3).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + + group.stop(); + + QCOMPARE(groupStateChangedSpy.count(), 4); + QCOMPARE(group.state(), QAnimationGroup::Stopped); +} + +void tst_QAnimationGroup::setCurrentTime() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject s_o3; + AnimationObject p_o1; + AnimationObject p_o2; + AnimationObject p_o3; + AnimationObject t_o1; + AnimationObject t_o2; + + // sequence operating on same object/property + QSequentialAnimationGroup *sequence = new QSequentialAnimationGroup(); + QAbstractAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QAbstractAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QAbstractAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + a2_s_o1->setLoopCount(3); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + + // sequence operating on different object/properties + QAnimationGroup *sequence2 = new QSequentialAnimationGroup(); + QAbstractAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value"); + QAbstractAnimation *a1_s_o3 = new QPropertyAnimation(&s_o3, "value"); + sequence2->addAnimation(a1_s_o2); + sequence2->addAnimation(a1_s_o3); + + // parallel operating on different object/properties + QAnimationGroup *parallel = new QParallelAnimationGroup(); + QAbstractAnimation *a1_p_o1 = new QPropertyAnimation(&p_o1, "value"); + QAbstractAnimation *a1_p_o2 = new QPropertyAnimation(&p_o2, "value"); + QAbstractAnimation *a1_p_o3 = new QPropertyAnimation(&p_o3, "value"); + a1_p_o2->setLoopCount(3); + parallel->addAnimation(a1_p_o1); + parallel->addAnimation(a1_p_o2); + parallel->addAnimation(a1_p_o3); + + QAbstractAnimation *notTimeDriven = new UncontrolledAnimation(&t_o1, "value"); + QCOMPARE(notTimeDriven->totalDuration(), -1); + + QAbstractAnimation *loopsForever = new QPropertyAnimation(&t_o2, "value"); + loopsForever->setLoopCount(-1); + QCOMPARE(loopsForever->totalDuration(), -1); + + QParallelAnimationGroup group; + group.addAnimation(sequence); + group.addAnimation(sequence2); + group.addAnimation(parallel); + group.addAnimation(notTimeDriven); + group.addAnimation(loopsForever); + + // Current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(parallel->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o3->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped); + + QCOMPARE(group.currentTime(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(a1_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 1); + QCOMPARE(a1_s_o3->currentTime(), 0); + QCOMPARE(a1_p_o1->currentTime(), 1); + QCOMPARE(a1_p_o2->currentTime(), 1); + QCOMPARE(a1_p_o3->currentTime(), 1); + QCOMPARE(notTimeDriven->currentTime(), 1); + QCOMPARE(loopsForever->currentTime(), 1); + + // Current time = 250 + group.setCurrentTime(250); + QCOMPARE(group.currentTime(), 250); + QCOMPARE(sequence->currentTime(), 250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 0); + QCOMPARE(a1_p_o1->currentTime(), 250); + QCOMPARE(a1_p_o2->currentTime(), 0); + QCOMPARE(a1_p_o2->currentLoop(), 1); + QCOMPARE(a1_p_o3->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 250); + QCOMPARE(loopsForever->currentTime(), 0); + QCOMPARE(loopsForever->currentLoop(), 1); + QCOMPARE(sequence->currentAnimation(), a2_s_o1); + + // Current time = 251 + group.setCurrentTime(251); + QCOMPARE(group.currentTime(), 251); + QCOMPARE(sequence->currentTime(), 251); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentLoop(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 251); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 1); + QCOMPARE(a1_p_o1->currentTime(), 250); + QCOMPARE(a1_p_o2->currentTime(), 1); + QCOMPARE(a1_p_o2->currentLoop(), 1); + QCOMPARE(a1_p_o3->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 251); + QCOMPARE(loopsForever->currentTime(), 1); + QCOMPARE(sequence->currentAnimation(), a2_s_o1); +} + +void tst_QAnimationGroup::statesAndSignals() +{ +} + +void tst_QAnimationGroup::setParentAutoAdd() +{ + QParallelAnimationGroup group; + QVariantAnimation *animation = new QPropertyAnimation(&group); + QCOMPARE(animation->group(), static_cast<QAnimationGroup*>(&group)); +} + +void tst_QAnimationGroup::beginNestedGroup() +{ + QAnimationGroup *subGroup; + QAnimationGroup *parent = new QParallelAnimationGroup(); + + for (int i = 0; i < 10; ++i) { + if (i & 1) + subGroup = new QParallelAnimationGroup(parent); + else + subGroup = new QSequentialAnimationGroup(parent); + + QCOMPARE(parent->animationCount(), 1); + QAnimationGroup *child = static_cast<QAnimationGroup *>(parent->animationAt(0)); + + QCOMPARE(child->parent(), static_cast<QObject *>(parent)); + if (i & 1) + QVERIFY(qobject_cast<QParallelAnimationGroup *> (child)); + else + QVERIFY(qobject_cast<QSequentialAnimationGroup *> (child)); + + parent = child; + } +} + +void tst_QAnimationGroup::addChildTwice() +{ + QAbstractAnimation *subGroup; + QAbstractAnimation *subGroup2; + QAnimationGroup *parent = new QSequentialAnimationGroup(); + + subGroup = new QPropertyAnimation(); + subGroup->setParent(parent); + parent->addAnimation(subGroup); + QCOMPARE(parent->animationCount(), 1); + + parent->clearAnimations(); + + QCOMPARE(parent->animationCount(), 0); + + // adding the same item twice to a group will remove the item from its current position + // and append it to the end + subGroup = new QPropertyAnimation(parent); + subGroup2 = new QPropertyAnimation(parent); + + QCOMPARE(parent->animationCount(), 2); + QCOMPARE(parent->animationAt(0), subGroup); + QCOMPARE(parent->animationAt(1), subGroup2); + + parent->addAnimation(subGroup); + + QCOMPARE(parent->animationCount(), 2); + QCOMPARE(parent->animationAt(0), subGroup2); + QCOMPARE(parent->animationAt(1), subGroup); + + delete parent; +} + +void tst_QAnimationGroup::loopWithoutStartValue() +{ + QAnimationGroup *parent = new QSequentialAnimationGroup(); + QObject o; + o.setProperty("ole", 0); + QCOMPARE(o.property("ole").toInt(), 0); + + QPropertyAnimation anim1(&o, "ole"); + anim1.setEndValue(-50); + anim1.setDuration(100); + + QPropertyAnimation anim2(&o, "ole"); + anim2.setEndValue(50); + anim2.setDuration(100); + + parent->addAnimation(&anim1); + parent->addAnimation(&anim2); + + parent->setLoopCount(-1); + parent->start(); + + QVERIFY(anim1.startValue().isNull()); + QCOMPARE(anim1.currentValue().toInt(), 0); + QCOMPARE(parent->currentLoop(), 0); + + parent->setCurrentTime(200); + QCOMPARE(parent->currentLoop(), 1); + QCOMPARE(anim1.currentValue().toInt(), 50); + parent->stop(); +} + +QTEST_MAIN(tst_QAnimationGroup) +#include "tst_qanimationgroup.moc" diff --git a/tests/auto/qapplication/tst_qapplication.cpp b/tests/auto/qapplication/tst_qapplication.cpp index 2122d98..473d1e7 100644 --- a/tests/auto/qapplication/tst_qapplication.cpp +++ b/tests/auto/qapplication/tst_qapplication.cpp @@ -48,6 +48,7 @@ #include "qabstracteventdispatcher.h" #include <QtGui> +#include "private/qapplication_p.h" #include "private/qstylesheetstyle_p.h" #ifdef Q_OS_WINCE #include <windows.h> @@ -132,6 +133,8 @@ private slots: void windowsCommandLine_data(); void windowsCommandLine(); + + void touchEventPropagation(); }; class MyInputContext : public QInputContext @@ -1817,6 +1820,146 @@ void tst_QApplication::windowsCommandLine() #endif } +class TouchEventPropagationTestWidget : public QWidget +{ + Q_OBJECT + +public: + bool seenTouchEvent, acceptTouchEvent, seenMouseEvent, acceptMouseEvent; + + TouchEventPropagationTestWidget(QWidget *parent = 0) + : QWidget(parent), seenTouchEvent(false), acceptTouchEvent(false), seenMouseEvent(false), acceptMouseEvent(false) + { } + + void reset() + { + seenTouchEvent = acceptTouchEvent = seenMouseEvent = acceptMouseEvent = false; + } + + bool event(QEvent *event) + { + switch (event->type()) { + case QEvent::MouseButtonPress: + case QEvent::MouseMove: + case QEvent::MouseButtonRelease: + // qDebug() << objectName() << "seenMouseEvent = true"; + seenMouseEvent = true; + event->setAccepted(acceptMouseEvent); + break; + case QEvent::TouchBegin: + case QEvent::TouchUpdate: + case QEvent::TouchEnd: + // qDebug() << objectName() << "seenTouchEvent = true"; + seenTouchEvent = true; + event->setAccepted(acceptTouchEvent); + break; + default: + return QWidget::event(event); + } + return true; + } +}; + +void tst_QApplication::touchEventPropagation() +{ + int argc = 1; + QApplication app(argc, &argv0, QApplication::GuiServer); + QTouchEvent::TouchPoint touchPoint(0); + QTouchEvent touchEvent(QEvent::TouchBegin, Qt::NoModifier, QList<QTouchEvent::TouchPoint *>() << (&touchPoint)); + + { + // touch event behavior on a window + TouchEventPropagationTestWidget window; + window.setObjectName("1. window"); + + QApplicationPrivate::sendTouchEvent(&window, &touchEvent); + QVERIFY(!window.seenTouchEvent); + QVERIFY(window.seenMouseEvent); + + window.reset(); + window.setAttribute(Qt::WA_AcceptsTouchEvents); + QApplicationPrivate::sendTouchEvent(&window, &touchEvent); + QVERIFY(window.seenTouchEvent); + QVERIFY(window.seenMouseEvent); + + window.reset(); + window.acceptTouchEvent = true; + QApplicationPrivate::sendTouchEvent(&window, &touchEvent); + QVERIFY(window.seenTouchEvent); + QVERIFY(!window.seenMouseEvent); + } + + { + // touch event behavior on a window with a child widget + TouchEventPropagationTestWidget window; + window.setObjectName("2. window"); + TouchEventPropagationTestWidget widget(&window); + widget.setObjectName("2. widget"); + + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(!widget.seenTouchEvent); + QVERIFY(widget.seenMouseEvent); + QVERIFY(!window.seenTouchEvent); + QVERIFY(window.seenMouseEvent); + + window.reset(); + widget.reset(); + widget.setAttribute(Qt::WA_AcceptsTouchEvents); + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(widget.seenTouchEvent); + QVERIFY(widget.seenMouseEvent); + QVERIFY(!window.seenTouchEvent); + QVERIFY(window.seenMouseEvent); + + window.reset(); + widget.reset(); + widget.acceptMouseEvent = true; + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(widget.seenTouchEvent); + QVERIFY(widget.seenMouseEvent); + QVERIFY(!window.seenTouchEvent); + QVERIFY(!window.seenMouseEvent); + + window.reset(); + widget.reset(); + widget.acceptTouchEvent = true; + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(widget.seenTouchEvent); + QVERIFY(!widget.seenMouseEvent); + QVERIFY(!window.seenTouchEvent); + QVERIFY(!window.seenMouseEvent); + + window.reset(); + widget.reset(); + widget.setAttribute(Qt::WA_AcceptsTouchEvents, false); + window.setAttribute(Qt::WA_AcceptsTouchEvents); + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(!widget.seenTouchEvent); + QVERIFY(widget.seenMouseEvent); + QVERIFY(window.seenTouchEvent); + QVERIFY(window.seenMouseEvent); + + window.reset(); + widget.reset(); + window.acceptTouchEvent = true; + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(!widget.seenTouchEvent); + QVERIFY(!widget.seenMouseEvent); + QVERIFY(window.seenTouchEvent); + QVERIFY(!window.seenMouseEvent); + + window.reset(); + widget.reset(); + widget.acceptMouseEvent = true; // doesn't matter, touch events are propagated first + window.acceptTouchEvent = true; + QApplicationPrivate::sendTouchEvent(&widget, &touchEvent); + QVERIFY(!widget.seenTouchEvent); + QVERIFY(!widget.seenMouseEvent); + QVERIFY(window.seenTouchEvent); + QVERIFY(!window.seenMouseEvent); + } +} + //QTEST_APPLESS_MAIN(tst_QApplication) int main(int argc, char *argv[]) { diff --git a/tests/auto/qboxlayout/tst_qboxlayout.cpp b/tests/auto/qboxlayout/tst_qboxlayout.cpp index f34fc00..be6f3dd 100644 --- a/tests/auto/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/qboxlayout/tst_qboxlayout.cpp @@ -65,8 +65,53 @@ private slots: void sizeHint(); void sizeConstraints(); void setGeometry(); + void setStyleShouldChangeSpacing(); }; +class CustomLayoutStyle : public QWindowsStyle +{ + Q_OBJECT +public: + CustomLayoutStyle() : QWindowsStyle() + { + hspacing = 5; + vspacing = 10; + } + + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, + const QWidget * widget = 0 ) const; + + int hspacing; + int vspacing; +}; + +int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, + const QWidget * widget /*= 0*/ ) const +{ + switch (metric) { + case PM_LayoutLeftMargin: + return 0; + break; + case PM_LayoutTopMargin: + return 3; + break; + case PM_LayoutRightMargin: + return 6; + break; + case PM_LayoutBottomMargin: + return 9; + break; + case PM_LayoutHorizontalSpacing: + return hspacing; + case PM_LayoutVerticalSpacing: + return vspacing; + break; + default: + break; + } + return QWindowsStyle::pixelMetric(metric, option, widget); +} + tst_QBoxLayout::tst_QBoxLayout() { @@ -163,12 +208,44 @@ void tst_QBoxLayout::setGeometry() lay->addLayout(lay2); w.setLayout(lay); w.show(); - + QRect newGeom(0, 0, 70, 70); lay2->setGeometry(newGeom); QApplication::processEvents(); QVERIFY2(newGeom.contains(dial->geometry()), "dial->geometry() should be smaller and within newGeom"); } +void tst_QBoxLayout::setStyleShouldChangeSpacing() +{ + + QWidget *window = new QWidget; + QHBoxLayout *hbox = new QHBoxLayout(window); + QPushButton *pb1 = new QPushButton(tr("The spacing between this")); + QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget"));; + hbox->addWidget(pb1); + hbox->addWidget(pb2); + CustomLayoutStyle *style1 = new CustomLayoutStyle; + style1->hspacing = 6; + window->setStyle(style1); + window->show(); + + QTest::qWait(100); + int spacing = pb2->geometry().left() - pb1->geometry().right() - 1; + QCOMPARE(spacing, 6); + + CustomLayoutStyle *style2 = new CustomLayoutStyle(); + style2->hspacing = 10; + window->setStyle(style2); + QTest::qWait(100); + spacing = pb2->geometry().left() - pb1->geometry().right() - 1; + QEXPECT_FAIL("", "Fix for next minor release", Continue); + QCOMPARE(spacing, 10); + + delete window; + delete style1; + delete style2; +} + + QTEST_MAIN(tst_QBoxLayout) #include "tst_qboxlayout.moc" diff --git a/tests/auto/qbrush/tst_qbrush.cpp b/tests/auto/qbrush/tst_qbrush.cpp index c0bf6bf..e4dadd6 100644 --- a/tests/auto/qbrush/tst_qbrush.cpp +++ b/tests/auto/qbrush/tst_qbrush.cpp @@ -44,6 +44,7 @@ #include "qbrush.h" #include <QPainter> +#include <QBitmap> #include <qdebug.h> @@ -76,6 +77,7 @@ private slots: void textures(); void nullBrush(); + void isOpaque(); }; Q_DECLARE_METATYPE(QBrush) @@ -379,5 +381,14 @@ void tst_QBrush::nullBrush() QCOMPARE(brush.color(), QColor(100,0,0)); } +void tst_QBrush::isOpaque() +{ + QBitmap bm(8, 8); + bm.fill(Qt::black); + + QBrush brush(bm); + QVERIFY(!brush.isOpaque()); +} + QTEST_MAIN(tst_QBrush) #include "tst_qbrush.moc" diff --git a/tests/auto/qbuttongroup/tst_qbuttongroup.cpp b/tests/auto/qbuttongroup/tst_qbuttongroup.cpp index f1365c0..86dd2b2 100644 --- a/tests/auto/qbuttongroup/tst_qbuttongroup.cpp +++ b/tests/auto/qbuttongroup/tst_qbuttongroup.cpp @@ -92,11 +92,15 @@ private slots: void exclusive(); void exclusiveWithActions(); void testSignals(); - void checkedButton(); void task106609(); + // fixed for Qt 4.6.0 +#if QT_VERSION >= 0x040600 + void autoIncrementId(); +#endif + void task209485_removeFromGroupInEventHandler_data(); void task209485_removeFromGroupInEventHandler(); }; @@ -329,13 +333,19 @@ void tst_QButtonGroup::testSignals() QCOMPARE(clickedSpy.count(), 1); QCOMPARE(clickedIdSpy.count(), 1); - QVERIFY(clickedIdSpy.takeFirst().at(0).toInt() == -1); + + int expectedId = -1; +#if QT_VERSION >= 0x040600 + expectedId = -2; +#endif + + QVERIFY(clickedIdSpy.takeFirst().at(0).toInt() == expectedId); QCOMPARE(pressedSpy.count(), 1); QCOMPARE(pressedIdSpy.count(), 1); - QVERIFY(pressedIdSpy.takeFirst().at(0).toInt() == -1); + QVERIFY(pressedIdSpy.takeFirst().at(0).toInt() == expectedId); QCOMPARE(releasedSpy.count(), 1); QCOMPARE(releasedIdSpy.count(), 1); - QVERIFY(releasedIdSpy.takeFirst().at(0).toInt() == -1); + QVERIFY(releasedIdSpy.takeFirst().at(0).toInt() == expectedId); clickedSpy.clear(); clickedIdSpy.clear(); @@ -483,5 +493,36 @@ void tst_QButtonGroup::task209485_removeFromGroupInEventHandler() QCOMPARE(spy1.count() + spy2.count(), signalCount); } +#if QT_VERSION >= 0x040600 +void tst_QButtonGroup::autoIncrementId() +{ + QDialog dlg(0); + QButtonGroup *buttons = new QButtonGroup(&dlg); + QVBoxLayout *vbox = new QVBoxLayout(&dlg); + + QRadioButton *radio1 = new QRadioButton(&dlg); + radio1->setText("radio1"); + QRadioButton *radio2 = new QRadioButton(&dlg); + radio2->setText("radio2"); + QRadioButton *radio3 = new QRadioButton(&dlg); + radio3->setText("radio3"); + + buttons->addButton(radio1); + vbox->addWidget(radio1); + buttons->addButton(radio2); + vbox->addWidget(radio2); + buttons->addButton(radio3); + vbox->addWidget(radio3); + + radio1->setChecked(true); + + QVERIFY(buttons->id(radio1) == -2); + QVERIFY(buttons->id(radio2) == -3); + QVERIFY(buttons->id(radio3) == -4); + + dlg.show(); +} +#endif + QTEST_MAIN(tst_QButtonGroup) #include "tst_qbuttongroup.moc" diff --git a/tests/auto/qbytearray/tst_qbytearray.cpp b/tests/auto/qbytearray/tst_qbytearray.cpp index 610fcd1..9c13c05 100644 --- a/tests/auto/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/qbytearray/tst_qbytearray.cpp @@ -243,10 +243,6 @@ void tst_QByteArray::qUncompress() QTEST(::qUncompress(in), "out"); -#if defined Q_WS_MAC && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4) - QSKIP("Corrupt data causes this test to lock up on Mac OS X Panther", SkipSingle); -#endif - QTEST(::qUncompress(in + "blah"), "out"); } #endif diff --git a/tests/auto/qcontiguouscache/qcontiguouscache.pro b/tests/auto/qcontiguouscache/qcontiguouscache.pro new file mode 100644 index 0000000..618efed --- /dev/null +++ b/tests/auto/qcontiguouscache/qcontiguouscache.pro @@ -0,0 +1,8 @@ +load(qttest_p4) + +QT = core + +SOURCES += tst_qcontiguouscache.cpp + + + diff --git a/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp b/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp new file mode 100644 index 0000000..048fa34 --- /dev/null +++ b/tests/auto/qcontiguouscache/tst_qcontiguouscache.cpp @@ -0,0 +1,479 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QObject> +#include <QTest> +#include <QCache> +#include <QContiguousCache> + +#include <QDebug> +#include <stdio.h> + +class tst_QContiguousCache : public QObject +{ + Q_OBJECT +public: + tst_QContiguousCache() {} + virtual ~tst_QContiguousCache() {} +private slots: + void empty(); + void append_data(); + void append(); + + void prepend_data(); + void prepend(); + + void asScrollingList(); + + void complexType(); + + void operatorAt(); + + void cacheBenchmark(); + void contiguousCacheBenchmark(); + + void setCapacity(); +}; + +QTEST_MAIN(tst_QContiguousCache) + +void tst_QContiguousCache::empty() +{ + QContiguousCache<int> c(10); + QCOMPARE(c.capacity(), 10); + QCOMPARE(c.count(), 0); + QVERIFY(c.isEmpty()); + c.append(1); + QCOMPARE(c.count(), 1); + QVERIFY(!c.isEmpty()); + c.clear(); + QCOMPARE(c.capacity(), 10); + QCOMPARE(c.count(), 0); + QVERIFY(c.isEmpty()); + c.prepend(1); + QCOMPARE(c.count(), 1); + QVERIFY(!c.isEmpty()); + c.clear(); + QCOMPARE(c.count(), 0); + QVERIFY(c.isEmpty()); + QCOMPARE(c.capacity(), 10); +} + +void tst_QContiguousCache::append_data() +{ + QTest::addColumn<int>("start"); + QTest::addColumn<int>("count"); + QTest::addColumn<int>("cacheSize"); + QTest::addColumn<bool>("invalidIndexes"); + + QTest::newRow("0+30[10]") << 0 << 30 << 10 << false; + QTest::newRow("300+30[10]") << 300 << 30 << 10 << false; + QTest::newRow("MAX-10+30[10]") << INT_MAX-10 << 30 << 10 << true; +} + +void tst_QContiguousCache::append() +{ + QFETCH(int, start); + QFETCH(int, count); + QFETCH(int, cacheSize); + QFETCH(bool, invalidIndexes); + + int i, j; + QContiguousCache<int> c(cacheSize); + + i = 1; + QCOMPARE(c.available(), cacheSize); + if (start == 0) + c.append(i++); + else + c.insert(start, i++); + while (i < count) { + c.append(i); + QCOMPARE(c.available(), qMax(0, cacheSize - i)); + QCOMPARE(c.first(), qMax(1, i-cacheSize+1)); + QCOMPARE(c.last(), i); + QCOMPARE(c.count(), qMin(i, cacheSize)); + QCOMPARE(c.isFull(), i >= cacheSize); + i++; + } + + QCOMPARE(c.areIndexesValid(), !invalidIndexes); + if (invalidIndexes) + c.normalizeIndexes(); + QVERIFY(c.areIndexesValid()); + + // test taking from end until empty. + for (j = 0; j < cacheSize; j++, i--) { + QCOMPARE(c.takeLast(), i-1); + QCOMPARE(c.count(), cacheSize-j-1); + QCOMPARE(c.available(), j+1); + QVERIFY(!c.isFull()); + QCOMPARE(c.isEmpty(), j==cacheSize-1); + } + +} + +void tst_QContiguousCache::prepend_data() +{ + QTest::addColumn<int>("start"); + QTest::addColumn<int>("count"); + QTest::addColumn<int>("cacheSize"); + QTest::addColumn<bool>("invalidIndexes"); + + QTest::newRow("30-30[10]") << 30 << 30 << 10 << false; + QTest::newRow("300-30[10]") << 300 << 30 << 10 << false; + QTest::newRow("10-30[10]") << 10 << 30 << 10 << true; +} + +void tst_QContiguousCache::prepend() +{ + QFETCH(int, start); + QFETCH(int, count); + QFETCH(int, cacheSize); + QFETCH(bool, invalidIndexes); + + int i, j; + QContiguousCache<int> c(cacheSize); + + i = 1; + QCOMPARE(c.available(), cacheSize); + c.insert(start, i++); + while(i < count) { + c.prepend(i); + QCOMPARE(c.available(), qMax(0, cacheSize - i)); + QCOMPARE(c.last(), qMax(1, i-cacheSize+1)); + QCOMPARE(c.first(), i); + QCOMPARE(c.count(), qMin(i, cacheSize)); + QCOMPARE(c.isFull(), i >= cacheSize); + i++; + } + + QCOMPARE(c.areIndexesValid(), !invalidIndexes); + if (invalidIndexes) + c.normalizeIndexes(); + QVERIFY(c.areIndexesValid()); + + // test taking from start until empty. + for (j = 0; j < cacheSize; j++, i--) { + QCOMPARE(c.takeFirst(), i-1); + QCOMPARE(c.count(), cacheSize-j-1); + QCOMPARE(c.available(), j+1); + QVERIFY(!c.isFull()); + QCOMPARE(c.isEmpty(), j==cacheSize-1); + } +} + +void tst_QContiguousCache::asScrollingList() +{ + int i; + QContiguousCache<int> c(10); + + // Once allocated QContiguousCache should not + // allocate any additional memory for non + // complex data types. + QBENCHMARK { + // simulate scrolling in a list of items; + for(i = 0; i < 10; ++i) { + QCOMPARE(c.available(), 10-i); + c.append(i); + } + + QCOMPARE(c.firstIndex(), 0); + QCOMPARE(c.lastIndex(), 9); + QCOMPARE(c.first(), 0); + QCOMPARE(c.last(), 9); + QVERIFY(!c.containsIndex(-1)); + QVERIFY(!c.containsIndex(10)); + QCOMPARE(c.available(), 0); + + for (i = 0; i < 10; ++i) { + QVERIFY(c.containsIndex(i)); + QCOMPARE(c.at(i), i); + QCOMPARE(c[i], i); + QCOMPARE(((const QContiguousCache<int>)c)[i], i); + } + + for (i = 10; i < 30; ++i) + c.append(i); + + QCOMPARE(c.firstIndex(), 20); + QCOMPARE(c.lastIndex(), 29); + QCOMPARE(c.first(), 20); + QCOMPARE(c.last(), 29); + QVERIFY(!c.containsIndex(19)); + QVERIFY(!c.containsIndex(30)); + QCOMPARE(c.available(), 0); + + for (i = 20; i < 30; ++i) { + QVERIFY(c.containsIndex(i)); + QCOMPARE(c.at(i), i); + QCOMPARE(c[i], i); + QCOMPARE(((const QContiguousCache<int> )c)[i], i); + } + + for (i = 19; i >= 10; --i) + c.prepend(i); + + QCOMPARE(c.firstIndex(), 10); + QCOMPARE(c.lastIndex(), 19); + QCOMPARE(c.first(), 10); + QCOMPARE(c.last(), 19); + QVERIFY(!c.containsIndex(9)); + QVERIFY(!c.containsIndex(20)); + QCOMPARE(c.available(), 0); + + for (i = 10; i < 20; ++i) { + QVERIFY(c.containsIndex(i)); + QCOMPARE(c.at(i), i); + QCOMPARE(c[i], i); + QCOMPARE(((const QContiguousCache<int> )c)[i], i); + } + + for (i = 200; i < 220; ++i) + c.insert(i, i); + + QCOMPARE(c.firstIndex(), 210); + QCOMPARE(c.lastIndex(), 219); + QCOMPARE(c.first(), 210); + QCOMPARE(c.last(), 219); + QVERIFY(!c.containsIndex(209)); + QVERIFY(!c.containsIndex(300)); + QCOMPARE(c.available(), 0); + + for (i = 210; i < 220; ++i) { + QVERIFY(c.containsIndex(i)); + QCOMPARE(c.at(i), i); + QCOMPARE(c[i], i); + QCOMPARE(((const QContiguousCache<int> )c)[i], i); + } + c.clear(); // needed to reset benchmark + } + + // from a specific bug that was encountered. 100 to 299 cached, attempted to cache 250 - 205 via insert, failed. + // bug was that item at 150 would instead be item that should have been inserted at 250 + c.setCapacity(200); + for(i = 100; i < 300; ++i) + c.insert(i, i); + for (i = 250; i <= 306; ++i) + c.insert(i, 1000+i); + for (i = 107; i <= 306; ++i) { + QVERIFY(c.containsIndex(i)); + QCOMPARE(c.at(i), i < 250 ? i : 1000+i); + } +} + +struct RefCountingClassData +{ + QBasicAtomicInt ref; + static RefCountingClassData shared_null; +}; + +RefCountingClassData RefCountingClassData::shared_null = { + Q_BASIC_ATOMIC_INITIALIZER(1) +}; + +class RefCountingClass +{ +public: + RefCountingClass() : d(&RefCountingClassData::shared_null) { d->ref.ref(); } + + RefCountingClass(const RefCountingClass &other) + { + d = other.d; + d->ref.ref(); + } + + ~RefCountingClass() + { + if (!d->ref.deref()) + delete d; + } + + RefCountingClass &operator=(const RefCountingClass &other) + { + if (!d->ref.deref()) + delete d; + d = other.d; + d->ref.ref(); + return *this; + } + + int refCount() const { return d->ref; } +private: + RefCountingClassData *d; +}; + +void tst_QContiguousCache::complexType() +{ + RefCountingClass original; + + QContiguousCache<RefCountingClass> contiguousCache(10); + contiguousCache.append(original); + QCOMPARE(original.refCount(), 3); + contiguousCache.removeFirst(); + QCOMPARE(original.refCount(), 2); // shared null, 'original'. + contiguousCache.append(original); + QCOMPARE(original.refCount(), 3); + contiguousCache.clear(); + QCOMPARE(original.refCount(), 2); + + for(int i = 0; i < 100; ++i) + contiguousCache.insert(i, original); + + QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in contiguousCache. + + contiguousCache.clear(); + QCOMPARE(original.refCount(), 2); + for (int i = 0; i < 100; i++) + contiguousCache.append(original); + + QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in contiguousCache. + contiguousCache.clear(); + QCOMPARE(original.refCount(), 2); + + for (int i = 0; i < 100; i++) + contiguousCache.prepend(original); + + QCOMPARE(original.refCount(), 12); // shared null, 'original', + 10 in contiguousCache. + contiguousCache.clear(); + QCOMPARE(original.refCount(), 2); + + for (int i = 0; i < 100; i++) + contiguousCache.append(original); + + contiguousCache.takeLast(); + QCOMPARE(original.refCount(), 11); + + contiguousCache.takeFirst(); + QCOMPARE(original.refCount(), 10); +} + +void tst_QContiguousCache::operatorAt() +{ + RefCountingClass original; + QContiguousCache<RefCountingClass> contiguousCache(10); + + for (int i = 25; i < 35; ++i) + contiguousCache[i] = original; + + QCOMPARE(original.refCount(), 12); // shared null, orig, items in cache + + // verify const access does not copy items. + const QContiguousCache<RefCountingClass> copy(contiguousCache); + for (int i = 25; i < 35; ++i) + QCOMPARE(copy[i].refCount(), 12); + + // verify modifying the original increments ref count (e.g. does a detach) + contiguousCache[25] = original; + QCOMPARE(original.refCount(), 22); +} + +/* + Benchmarks must be near identical in tasks to be fair. + QCache uses pointers to ints as its a requirement of QCache, + whereas QContiguousCache doesn't support pointers (won't free them). + Given the ability to use simple data types is a benefit, its + fair. Although this obviously must take into account we are + testing QContiguousCache use cases here, QCache has its own + areas where it is the more sensible class to use. +*/ +void tst_QContiguousCache::cacheBenchmark() +{ + QBENCHMARK { + QCache<int, int> cache; + cache.setMaxCost(100); + + for (int i = 0; i < 1000; i++) + cache.insert(i, new int(i)); + } +} + +void tst_QContiguousCache::contiguousCacheBenchmark() +{ + QBENCHMARK { + QContiguousCache<int> contiguousCache(100); + for (int i = 0; i < 1000; i++) + contiguousCache.insert(i, i); + } +} + +void tst_QContiguousCache::setCapacity() +{ + int i; + QContiguousCache<int> contiguousCache(100); + for (i = 280; i < 310; ++i) + contiguousCache.insert(i, i); + QCOMPARE(contiguousCache.capacity(), 100); + QCOMPARE(contiguousCache.count(), 30); + QCOMPARE(contiguousCache.firstIndex(), 280); + QCOMPARE(contiguousCache.lastIndex(), 309); + + for (i = contiguousCache.firstIndex(); i <= contiguousCache.lastIndex(); ++i) { + QVERIFY(contiguousCache.containsIndex(i)); + QCOMPARE(contiguousCache.at(i), i); + } + + contiguousCache.setCapacity(150); + + QCOMPARE(contiguousCache.capacity(), 150); + QCOMPARE(contiguousCache.count(), 30); + QCOMPARE(contiguousCache.firstIndex(), 280); + QCOMPARE(contiguousCache.lastIndex(), 309); + + for (i = contiguousCache.firstIndex(); i <= contiguousCache.lastIndex(); ++i) { + QVERIFY(contiguousCache.containsIndex(i)); + QCOMPARE(contiguousCache.at(i), i); + } + + contiguousCache.setCapacity(20); + + QCOMPARE(contiguousCache.capacity(), 20); + QCOMPARE(contiguousCache.count(), 20); + QCOMPARE(contiguousCache.firstIndex(), 290); + QCOMPARE(contiguousCache.lastIndex(), 309); + + for (i = contiguousCache.firstIndex(); i <= contiguousCache.lastIndex(); ++i) { + QVERIFY(contiguousCache.containsIndex(i)); + QCOMPARE(contiguousCache.at(i), i); + } +} + +#include "tst_qcontiguouscache.moc" diff --git a/tests/auto/qcssparser/tst_cssparser.cpp b/tests/auto/qcssparser/tst_cssparser.cpp index 363fd60..ce923e1 100644 --- a/tests/auto/qcssparser/tst_cssparser.cpp +++ b/tests/auto/qcssparser/tst_cssparser.cpp @@ -114,11 +114,52 @@ void tst_CssParser::scanner_data() } } + +static char *tokenName(QCss::TokenType t) +{ + switch (t) { + case QCss::NONE: return "NONE"; + case QCss::S: return "S"; + case QCss::CDO: return "CDO"; + case QCss::CDC: return "CDC"; + case QCss::INCLUDES: return "INCLUDES"; + case QCss::DASHMATCH: return "DASHMATCH"; + case QCss::LBRACE: return "LBRACE"; + case QCss::PLUS: return "PLUS"; + case QCss::GREATER: return "GREATER"; + case QCss::COMMA: return "COMMA"; + case QCss::STRING: return "STRING"; + case QCss::INVALID: return "INVALID"; + case QCss::IDENT: return "IDENT"; + case QCss::HASH: return "HASH"; + case QCss::ATKEYWORD_SYM: return "ATKEYWORD_SYM"; + case QCss::EXCLAMATION_SYM: return "EXCLAMATION_SYM"; + case QCss::LENGTH: return "LENGTH"; + case QCss::PERCENTAGE: return "PERCENTAGE"; + case QCss::NUMBER: return "NUMBER"; + case QCss::FUNCTION: return "FUNCTION"; + case QCss::COLON: return "COLON"; + case QCss::SEMICOLON: return "SEMICOLON"; + case QCss::RBRACE: return "RBRACE"; + case QCss::SLASH: return "SLASH"; + case QCss::MINUS: return "MINUS"; + case QCss::DOT: return "DOT"; + case QCss::STAR: return "STAR"; + case QCss::LBRACKET: return "LBRACKET"; + case QCss::RBRACKET: return "RBRACKET"; + case QCss::EQUAL: return "EQUAL"; + case QCss::LPAREN: return "LPAREN"; + case QCss::RPAREN: return "RPAREN"; + case QCss::OR: return "OR"; + } + return ""; +} + static void debug(const QVector<QCss::Symbol> &symbols, int index = -1) { qDebug() << "all symbols:"; for (int i = 0; i < symbols.count(); ++i) - qDebug() << "(" << i << "); Token:" << QCss::Scanner::tokenName(symbols.at(i).token) << "; Lexem:" << symbols.at(i).lexem(); + qDebug() << "(" << i << "); Token:" << tokenName(symbols.at(i).token) << "; Lexem:" << symbols.at(i).lexem(); if (index != -1) qDebug() << "failure at index" << index; } @@ -160,7 +201,7 @@ void tst_CssParser::scanner() QCOMPARE(l.count(), 2); const QString expectedToken = l.at(0); const QString expectedLexem = l.at(1); - QString actualToken = QString::fromLatin1(QCss::Scanner::tokenName(symbols.at(i).token)); + QString actualToken = QString::fromLatin1(tokenName(symbols.at(i).token)); if (actualToken != expectedToken) { debug(symbols, i); QCOMPARE(actualToken, expectedToken); diff --git a/tests/auto/qeasingcurve/qeasingcurve.pro b/tests/auto/qeasingcurve/qeasingcurve.pro new file mode 100644 index 0000000..2b66081 --- /dev/null +++ b/tests/auto/qeasingcurve/qeasingcurve.pro @@ -0,0 +1,3 @@ +load(qttest_p4) +QT = core +SOURCES += tst_qeasingcurve.cpp diff --git a/tests/auto/qeasingcurve/tst_qeasingcurve.cpp b/tests/auto/qeasingcurve/tst_qeasingcurve.cpp new file mode 100644 index 0000000..d40b6df --- /dev/null +++ b/tests/auto/qeasingcurve/tst_qeasingcurve.cpp @@ -0,0 +1,493 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#if QT_VERSION < 0x040200 +QTEST_NOOP_MAIN +#else + +#include <qeasingcurve.h> + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QEasingCurve : public QObject { + Q_OBJECT + +public: + tst_QEasingCurve(); + virtual ~tst_QEasingCurve(); + +public Q_SLOTS: + void init(); + void cleanup(); + +private slots: + void type(); + void propertyDefaults(); + void valueForProgress_data(); + void valueForProgress(); + void setCustomType(); + void operators(); + +protected: +}; + +tst_QEasingCurve::tst_QEasingCurve() +{ +} + +tst_QEasingCurve::~tst_QEasingCurve() +{ +} + +void tst_QEasingCurve::init() +{ +} + +void tst_QEasingCurve::cleanup() +{ +} +#include <qdebug.h> + +void tst_QEasingCurve::type() +{ + { + QEasingCurve curve(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + + curve.setPeriod(5); + curve.setAmplitude(3); + QCOMPARE(curve.period(), 5.0); + QCOMPARE(curve.amplitude(), 3.0); + + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 5.0); + QCOMPARE(curve.amplitude(), 3.0); + } + + { + QEasingCurve curve(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + curve.setAmplitude(2); + QCOMPARE(curve.type(), QEasingCurve::InElastic); + curve.setType(QEasingCurve::Linear); + } + + { + // check bounaries + QEasingCurve curve(QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type 9999"); + curve.setType((QEasingCurve::Type)9999); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, "QEasingCurve: Invalid curve type -9999"); + curve.setType((QEasingCurve::Type)-9999); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") + .arg(QEasingCurve::NCurveTypes).toLatin1().constData()); + curve.setType(QEasingCurve::NCurveTypes); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") + .arg(QEasingCurve::Custom).toLatin1().constData()); + curve.setType(QEasingCurve::Custom); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QEasingCurve: Invalid curve type %1") + .arg(-1).toLatin1().constData()); + curve.setType((QEasingCurve::Type)-1); + QCOMPARE(curve.type(), QEasingCurve::InCubic); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.type(), QEasingCurve::Linear); + curve.setType(QEasingCurve::CosineCurve); + QCOMPARE(curve.type(), QEasingCurve::CosineCurve); + } +} + +void tst_QEasingCurve::propertyDefaults() +{ + { + // checks if the defaults are correct, but also demonstrates a weakness with the API. + QEasingCurve curve(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158f)); + curve.setType(QEasingCurve::InBounce); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158f)); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158f)); + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.3); + QCOMPARE(curve.amplitude(), 1.0); + QCOMPARE(curve.overshoot(), qreal(1.70158f)); + curve.setPeriod(0.4); + curve.setAmplitude(0.6); + curve.setOvershoot(1.0); + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.period(), 0.4); + QCOMPARE(curve.amplitude(), 0.6); + QCOMPARE(curve.overshoot(), 1.0); + curve.setType(QEasingCurve::InElastic); + QCOMPARE(curve.period(), 0.4); + QCOMPARE(curve.amplitude(), 0.6); + QCOMPARE(curve.overshoot(), 1.0); + } +} + +typedef QList<int> IntList; +typedef QList<qreal> RealList; +Q_DECLARE_METATYPE(IntList) +Q_DECLARE_METATYPE(RealList) + +void tst_QEasingCurve::valueForProgress_data() +{ + QTest::addColumn<int>("type"); + QTest::addColumn<IntList>("at"); + QTest::addColumn<RealList>("expected"); + // automatically generated. + // note that values are scaled from range [0,1] to range [0, 100] in order to store them as + // integer values and avoid fp inaccuracies + QTest::newRow("Linear") << int(QEasingCurve::Linear) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.1 << 0.2 << 0.3 << 0.4 << 0.5 << 0.6 << 0.7 << 0.8 << 0.9 << 1); + + QTest::newRow("InQuad") << int(QEasingCurve::InQuad) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.01 << 0.04 << 0.09 << 0.16 << 0.25 << 0.36 << 0.49 << 0.64 << 0.81 << 1); + + QTest::newRow("OutQuad") << int(QEasingCurve::OutQuad) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.19 << 0.36 << 0.51 << 0.64 << 0.75 << 0.84 << 0.91 << 0.96 << 0.99 << 1); + + QTest::newRow("InOutQuad") << int(QEasingCurve::InOutQuad) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.02 << 0.08 << 0.18 << 0.32 << 0.5 << 0.68 << 0.82 << 0.92 << 0.98 << 1); + + QTest::newRow("OutInQuad") << int(QEasingCurve::OutInQuad) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.18 << 0.32 << 0.42 << 0.48 << 0.5 << 0.52 << 0.58 << 0.68 << 0.82 << 1); + + QTest::newRow("InCubic") << int(QEasingCurve::InCubic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.001 << 0.008 << 0.027 << 0.064 << 0.125 << 0.216 << 0.343 << 0.512 << 0.729 << 1); + + QTest::newRow("OutCubic") << int(QEasingCurve::OutCubic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.271 << 0.488 << 0.657 << 0.784 << 0.875 << 0.936 << 0.973 << 0.992 << 0.999 << 1); + + QTest::newRow("InOutCubic") << int(QEasingCurve::InOutCubic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.004 << 0.032 << 0.108 << 0.256 << 0.5 << 0.744 << 0.892 << 0.968 << 0.996 << 1); + + QTest::newRow("OutInCubic") << int(QEasingCurve::OutInCubic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.244 << 0.392 << 0.468 << 0.496 << 0.5 << 0.504 << 0.532 << 0.608 << 0.756 << 1); + + QTest::newRow("InQuart") << int(QEasingCurve::InQuart) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.0001 << 0.0016 << 0.0081 << 0.0256 << 0.0625 << 0.1296 << 0.2401 << 0.4096 << 0.6561 << 1); + + QTest::newRow("OutQuart") << int(QEasingCurve::OutQuart) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.3439 << 0.5904 << 0.7599 << 0.8704 << 0.9375 << 0.9744 << 0.9919 << 0.9984 << 0.9999 << 1); + + QTest::newRow("InOutQuart") << int(QEasingCurve::InOutQuart) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.0008 << 0.0128 << 0.0648 << 0.2048 << 0.5 << 0.7952 << 0.9352 << 0.9872 << 0.9992 << 1); + + QTest::newRow("OutInQuart") << int(QEasingCurve::OutInQuart) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.2952 << 0.4352 << 0.4872 << 0.4992 << 0.5 << 0.5008 << 0.5128 << 0.5648 << 0.7048 << 1); + + QTest::newRow("InQuint") << int(QEasingCurve::InQuint) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 1e-05 << 0.00032 << 0.00243 << 0.01024 << 0.03125 << 0.07776 << 0.1681 << 0.3277 << 0.5905 << 1); + + QTest::newRow("OutQuint") << int(QEasingCurve::OutQuint) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.4095 << 0.6723 << 0.8319 << 0.9222 << 0.9688 << 0.9898 << 0.9976 << 0.9997 << 1 << 1); + + QTest::newRow("InOutQuint") << int(QEasingCurve::InOutQuint) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.00016 << 0.00512 << 0.03888 << 0.1638 << 0.5 << 0.8362 << 0.9611 << 0.9949 << 0.9998 << 1); + + QTest::newRow("OutInQuint") << int(QEasingCurve::OutInQuint) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.3362 << 0.4611 << 0.4949 << 0.4998 << 0.5 << 0.5002 << 0.5051 << 0.5389 << 0.6638 << 1); + + QTest::newRow("InSine") << int(QEasingCurve::InSine) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.01231 << 0.04894 << 0.109 << 0.191 << 0.2929 << 0.4122 << 0.546 << 0.691 << 0.8436 << 1); + + QTest::newRow("OutSine") << int(QEasingCurve::OutSine) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.1564 << 0.309 << 0.454 << 0.5878 << 0.7071 << 0.809 << 0.891 << 0.9511 << 0.9877 << 1); + + QTest::newRow("InOutSine") << int(QEasingCurve::InOutSine) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.02447 << 0.09549 << 0.2061 << 0.3455 << 0.5 << 0.6545 << 0.7939 << 0.9045 << 0.9755 << 1); + + QTest::newRow("OutInSine") << int(QEasingCurve::OutInSine) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.1545 << 0.2939 << 0.4045 << 0.4755 << 0.5 << 0.5245 << 0.5955 << 0.7061 << 0.8455 << 1); + + QTest::newRow("InExpo") << int(QEasingCurve::InExpo) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.0009531 << 0.002906 << 0.006812 << 0.01462 << 0.03025 << 0.0615 << 0.124 << 0.249 << 0.499 << 1); + + QTest::newRow("OutExpo") << int(QEasingCurve::OutExpo) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.5005 << 0.7507 << 0.8759 << 0.9384 << 0.9697 << 0.9854 << 0.9932 << 0.9971 << 0.999 << 1); + + QTest::newRow("InOutExpo") << int(QEasingCurve::InOutExpo) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.001453 << 0.007312 << 0.03075 << 0.1245 << 0.5002 << 0.8754 << 0.9692 << 0.9927 << 0.9985 << 1); + + QTest::newRow("OutInExpo") << int(QEasingCurve::OutInExpo) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.3754 << 0.4692 << 0.4927 << 0.4985 << 0.5 << 0.5015 << 0.5073 << 0.5308 << 0.6245 << 1); + + QTest::newRow("InCirc") << int(QEasingCurve::InCirc) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.005013 << 0.0202 << 0.04606 << 0.08348 << 0.134 << 0.2 << 0.2859 << 0.4 << 0.5641 << 1); + + QTest::newRow("OutCirc") << int(QEasingCurve::OutCirc) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.4359 << 0.6 << 0.7141 << 0.8 << 0.866 << 0.9165 << 0.9539 << 0.9798 << 0.995 << 1); + + QTest::newRow("InOutCirc") << int(QEasingCurve::InOutCirc) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.0101 << 0.04174 << 0.1 << 0.2 << 0.5 << 0.8 << 0.9 << 0.9583 << 0.9899 << 1); + + QTest::newRow("OutInCirc") << int(QEasingCurve::OutInCirc) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.3 << 0.4 << 0.4583 << 0.4899 << 0.5 << 0.5101 << 0.5417 << 0.6 << 0.7 << 1); + + QTest::newRow("InElastic") << int(QEasingCurve::InElastic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.001953 << -0.001953 << -0.003906 << 0.01562 << -0.01562 << -0.03125 << 0.125 << -0.125 << -0.25 << 1); + + QTest::newRow("OutElastic") << int(QEasingCurve::OutElastic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 1.25 << 1.125 << 0.875 << 1.031 << 1.016 << 0.9844 << 1.004 << 1.002 << 0.998 << 1); + + QTest::newRow("InOutElastic") << int(QEasingCurve::InOutElastic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << -0.0009766 << 0.007812 << -0.01563 << -0.0625 << 0.5 << 1.062 << 1.016 << 0.9922 << 1.001 << 1); + + QTest::newRow("OutInElastic") << int(QEasingCurve::OutInElastic) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.375 << 0.5625 << 0.4922 << 0.498 << 0.5 << 0.4961 << 0.5078 << 0.5313 << 0.25 << 1); + + QTest::newRow("InBack") << int(QEasingCurve::InBack) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << -0.01431 << -0.04645 << -0.0802 << -0.09935 << -0.0877 << -0.02903 << 0.09287 << 0.2942 << 0.5912 << 1); + + QTest::newRow("OutBack") << int(QEasingCurve::OutBack) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.4088 << 0.7058 << 0.9071 << 1.029 << 1.088 << 1.099 << 1.08 << 1.046 << 1.014 << 1); + + QTest::newRow("InOutBack") << int(QEasingCurve::InOutBack) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << -0.03752 << -0.09256 << -0.07883 << 0.08993 << 0.5 << 0.9101 << 1.079 << 1.093 << 1.038 << 1); + + QTest::newRow("OutInBack") << int(QEasingCurve::OutInBack) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.3529 << 0.5145 << 0.5497 << 0.5232 << 0.5 << 0.4768 << 0.4503 << 0.4855 << 0.6471 << 1); + + QTest::newRow("InBounce") << int(QEasingCurve::InBounce) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.01188 << 0.06 << 0.06937 << 0.2275 << 0.2344 << 0.09 << 0.3194 << 0.6975 << 0.9244 << 1); + + QTest::newRow("OutBounce") << int(QEasingCurve::OutBounce) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.07563 << 0.3025 << 0.6806 << 0.91 << 0.7656 << 0.7725 << 0.9306 << 0.94 << 0.9881 << 1); + + QTest::newRow("InOutBounce") << int(QEasingCurve::InOutBounce) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.03 << 0.1138 << 0.045 << 0.3488 << 0.5 << 0.6512 << 0.955 << 0.8862 << 0.97 << 1); + + QTest::newRow("OutInBounce") << int(QEasingCurve::OutInBounce) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.1513 << 0.41 << 0.2725 << 0.44 << 0.5 << 0.56 << 0.7275 << 0.59 << 0.8488 << 1); + + QTest::newRow("InCurve") << int(QEasingCurve::InCurve) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.02447 << 0.1059 << 0.2343 << 0.3727 << 0.5 << 0.6055 << 0.7 << 0.8 << 0.9 << 1); + + QTest::newRow("OutCurve") << int(QEasingCurve::OutCurve) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.1 << 0.2 << 0.3 << 0.3945 << 0.5 << 0.6273 << 0.7657 << 0.8941 << 0.9755 << 1); + + QTest::newRow("SineCurve") << int(QEasingCurve::SineCurve) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0 << 0.09549 << 0.3455 << 0.6545 << 0.9045 << 1 << 0.9045 << 0.6545 << 0.3455 << 0.09549 << 0); + + QTest::newRow("CosineCurve") << int(QEasingCurve::CosineCurve) + << (IntList() << 0 << 10 << 20 << 30 << 40 << 50 << 60 << 70 << 80 << 90 << 100) + << (RealList() << 0.5 << 0.7939 << 0.9755 << 0.9755 << 0.7939 << 0.5 << 0.2061 << 0.02447 << 0.02447 << 0.2061 << 0.5); + +} + + +void tst_QEasingCurve::valueForProgress() +{ +#if 0 + // used to generate data tables... + QFile out; + out.open(stdout, QIODevice::WriteOnly); + for (int c = QEasingCurve::Linear; c < QEasingCurve::NCurveTypes - 1; ++c) { + QEasingCurve curve((QEasingCurve::Type)c); + QMetaObject mo = QEasingCurve::staticMetaObject; + QString strCurve = QLatin1String(mo.enumerator(mo.indexOfEnumerator("Type")).key(c)); + QString strInputs; + QString strOutputs; + + for (int t = 0; t <= 100; t+= 10) { + qreal ease = curve.valueForProgress(t/qreal(100)); + strInputs += QString::fromAscii(" << %1").arg(t); + strOutputs += " << " + QString().setNum(ease, 'g', 4); + } + QString str = QString::fromAscii(" QTest::newRow(\"%1\") << int(QEasingCurve::%2)\n" + " << (IntList() %3)\n" + " << (RealList()%4);\n\n") + .arg(strCurve) + .arg(strCurve) + .arg(strInputs) + .arg(strOutputs); + out.write(str.toLatin1().constData()); + } + out.close(); + exit(1); +#else + QFETCH(int, type); + QFETCH(IntList, at); + QFETCH(RealList, expected); + + QEasingCurve curve((QEasingCurve::Type)type); + for (int i = 0; i < at.count(); ++i) { + qreal ease = curve.valueForProgress(at.at(i)/qreal(100)); + // converting ease to 4 precision qreal to match the generated samples + qreal easeConv = qreal(QString().setNum(ease, 'g', 4).toDouble()); + qreal ex = expected.at(i); + + // the least significant digit it is still subject to rounding errors + qreal error = easeConv - ex; + // accept the potential rounding error in the least significant digit + QVERIFY(error <= 0.00001 ); + } +#endif +} + +static qreal discreteEase(qreal progress) +{ + return qFloor(progress * 10) / qreal(10.0); +} + +void tst_QEasingCurve::setCustomType() +{ + QEasingCurve curve; + curve.setCustomType(&discreteEase); + QCOMPARE(curve.type(), QEasingCurve::Custom); + QCOMPARE(curve.valueForProgress(0.0), 0.0); + QCOMPARE(curve.valueForProgress(0.05), 0.0); + QCOMPARE(curve.valueForProgress(0.10), 0.1); + QCOMPARE(curve.valueForProgress(0.15), 0.1); + QCOMPARE(curve.valueForProgress(0.20), 0.2); + QCOMPARE(curve.valueForProgress(0.25), 0.2); + QCOMPARE(curve.valueForProgress(0.30), 0.3); + QCOMPARE(curve.valueForProgress(0.35), 0.3); + QCOMPARE(curve.valueForProgress(0.999999), 0.9); + + curve.setType(QEasingCurve::Linear); + QCOMPARE(curve.type(), QEasingCurve::Linear); + QCOMPARE(curve.valueForProgress(0.0), 0.0); + QCOMPARE(curve.valueForProgress(0.1), 0.1); + QCOMPARE(curve.valueForProgress(0.5), 0.5); + QCOMPARE(curve.valueForProgress(0.99), 0.99); +} + +void tst_QEasingCurve::operators() +{ + // operator= + QEasingCurve curve; + QEasingCurve curve2; + curve.setCustomType(&discreteEase); + curve2 = curve; + QCOMPARE(curve2.type(), QEasingCurve::Custom); + QCOMPARE(curve2.valueForProgress(0.0), 0.0); + QCOMPARE(curve2.valueForProgress(0.05), 0.0); + QCOMPARE(curve2.valueForProgress(0.15), 0.1); + QCOMPARE(curve2.valueForProgress(0.25), 0.2); + QCOMPARE(curve2.valueForProgress(0.35), 0.3); + QCOMPARE(curve2.valueForProgress(0.999999), 0.9); + + // operator== + curve.setType(QEasingCurve::InBack); + curve2 = curve; + curve2.setOvershoot(qreal(1.70158f)); + QCOMPARE(curve.overshoot(), curve2.overshoot()); + QVERIFY(curve2 == curve); + + curve.setOvershoot(3.0); + QVERIFY(curve2 != curve); + curve2.setOvershoot(3.0); + QVERIFY(curve2 == curve); + + curve2.setType(QEasingCurve::Linear); + QCOMPARE(curve.overshoot(), curve2.overshoot()); + QVERIFY(curve2 != curve); + curve2.setType(QEasingCurve::InBack); + QCOMPARE(curve.overshoot(), curve2.overshoot()); + QVERIFY(curve2 == curve); +} + + +QTEST_MAIN(tst_QEasingCurve) +#include "tst_qeasingcurve.moc" + +#endif //QT_VERSION diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp index 43cbd5c..b67a44e 100644 --- a/tests/auto/qfile/tst_qfile.cpp +++ b/tests/auto/qfile/tst_qfile.cpp @@ -126,6 +126,7 @@ private slots: void permissions(); void setPermissions(); void copy(); + void copyAfterFail(); void copyRemovesTemporaryFile() const; void copyShouldntOverwrite(); void copyFallback(); @@ -227,8 +228,15 @@ void tst_QFile::cleanup() // for renameFallback() QFile::remove("file-rename-destination.txt"); + // for copyAfterFail() + QFile::remove("file-to-be-copied.txt"); + QFile::remove("existing-file.txt"); + QFile::remove("copied-file-1.txt"); + QFile::remove("copied-file-2.txt"); + // for renameMultiple() QFile::remove("file-to-be-renamed.txt"); + QFile::remove("existing-file.txt"); QFile::remove("file-renamed-once.txt"); QFile::remove("file-renamed-twice.txt"); } @@ -904,6 +912,39 @@ void tst_QFile::copy() QFile::copy(QDir::currentPath(), QDir::currentPath() + QLatin1String("/test2")); } +void tst_QFile::copyAfterFail() +{ + QFile file1("file-to-be-copied.txt"); + QFile file2("existing-file.txt"); + + QVERIFY(file1.open(QIODevice::ReadWrite) && "(test-precondition)"); + QVERIFY(file2.open(QIODevice::ReadWrite) && "(test-precondition)"); + QVERIFY(!QFile::exists("copied-file-1.txt") && "(test-precondition)"); + QVERIFY(!QFile::exists("copied-file-2.txt") && "(test-precondition)"); + + QVERIFY(!file1.copy("existing-file.txt")); + QCOMPARE(file1.error(), QFile::CopyError); + + QVERIFY(file1.copy("copied-file-1.txt")); + QVERIFY(!file1.isOpen()); + QCOMPARE(file1.error(), QFile::NoError); + + QVERIFY(!file1.copy("existing-file.txt")); + QCOMPARE(file1.error(), QFile::CopyError); + + QVERIFY(file1.copy("copied-file-2.txt")); + QVERIFY(!file1.isOpen()); + QCOMPARE(file1.error(), QFile::NoError); + + QVERIFY(QFile::exists("copied-file-1.txt")); + QVERIFY(QFile::exists("copied-file-2.txt")); + + QVERIFY(QFile::remove("file-to-be-copied.txt") && "(test-cleanup)"); + QVERIFY(QFile::remove("existing-file.txt") && "(test-cleanup)"); + QVERIFY(QFile::remove("copied-file-1.txt") && "(test-cleanup)"); + QVERIFY(QFile::remove("copied-file-2.txt") && "(test-cleanup)"); +} + void tst_QFile::copyRemovesTemporaryFile() const { const QString newName(QLatin1String("copyRemovesTemporaryFile")); @@ -2142,24 +2183,42 @@ void tst_QFile::renameMultiple() { // create the file if it doesn't exist QFile file("file-to-be-renamed.txt"); + QFile file2("existing-file.txt"); QVERIFY(file.open(QIODevice::ReadWrite) && "(test-precondition)"); + QVERIFY(file2.open(QIODevice::ReadWrite) && "(test-precondition)"); // any stale files from previous test failures? QFile::remove("file-renamed-once.txt"); QFile::remove("file-renamed-twice.txt"); // begin testing + QVERIFY(QFile::exists("existing-file.txt")); + QVERIFY(!file.rename("existing-file.txt")); + QCOMPARE(file.error(), QFile::RenameError); + QCOMPARE(file.fileName(), QString("file-to-be-renamed.txt")); + QVERIFY(file.rename("file-renamed-once.txt")); + QVERIFY(!file.isOpen()); QCOMPARE(file.fileName(), QString("file-renamed-once.txt")); + + QVERIFY(QFile::exists("existing-file.txt")); + QVERIFY(!file.rename("existing-file.txt")); + QCOMPARE(file.error(), QFile::RenameError); + QCOMPARE(file.fileName(), QString("file-renamed-once.txt")); + QVERIFY(file.rename("file-renamed-twice.txt")); + QVERIFY(!file.isOpen()); QCOMPARE(file.fileName(), QString("file-renamed-twice.txt")); + QVERIFY(QFile::exists("existing-file.txt")); QVERIFY(!QFile::exists("file-to-be-renamed.txt")); QVERIFY(!QFile::exists("file-renamed-once.txt")); QVERIFY(QFile::exists("file-renamed-twice.txt")); file.remove(); + file2.remove(); QVERIFY(!QFile::exists("file-renamed-twice.txt")); + QVERIFY(!QFile::exists("existing-file.txt")); } void tst_QFile::appendAndRead() diff --git a/tests/auto/qfileinfo/tst_qfileinfo.cpp b/tests/auto/qfileinfo/tst_qfileinfo.cpp index 7f27a60..b40a7dd 100644 --- a/tests/auto/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/qfileinfo/tst_qfileinfo.cpp @@ -61,6 +61,7 @@ #include <qdebug.h> #include "../network-settings.h" +#include <private/qfileinfo_p.h> //TESTED_CLASS= //TESTED_FILES= @@ -75,6 +76,9 @@ public: private slots: void getSetCheck(); + + void copy(); + void isFile_data(); void isFile(); @@ -179,6 +183,58 @@ void tst_QFileInfo::getSetCheck() QCOMPARE(true, obj1.caching()); } +static QFileInfoPrivate* getPrivate(QFileInfo &info) +{ + return (*reinterpret_cast<QFileInfoPrivate**>(&info)); +} + +void tst_QFileInfo::copy() +{ + QTemporaryFile *t; + t = new QTemporaryFile; + t->open(); + QFileInfo info(t->fileName()); + QVERIFY(info.exists()); + + //copy constructor + QFileInfo info2(info); + QFileInfoPrivate *privateInfo = getPrivate(info); + QFileInfoPrivate *privateInfo2 = getPrivate(info2); + QCOMPARE(privateInfo->data, privateInfo2->data); + + //operator = + QFileInfo info3 = info; + QFileInfoPrivate *privateInfo3 = getPrivate(info3); + QCOMPARE(privateInfo->data, privateInfo3->data); + QCOMPARE(privateInfo2->data, privateInfo3->data); + + //refreshing info3 will detach it + QFile file(info.absoluteFilePath()); + QVERIFY(file.open(QFile::WriteOnly)); + QCOMPARE(file.write("JAJAJAA"), qint64(7)); + file.flush(); + + QTest::qWait(250); +#if defined(Q_OS_WIN) || defined(Q_OS_WINCE) + if (QSysInfo::windowsVersion() & QSysInfo::WV_VISTA || + QSysInfo::windowsVersion() & QSysInfo::WV_CE_based) + file.close(); +#endif +#if defined(Q_OS_WINCE) + // On Windows CE we need to close the file. + // Otherwise the content will be cached and not + // flushed to the storage, although we flushed it + // manually!!! CE has interim cache, we cannot influence. + QTest::qWait(5000); +#endif + info3.refresh(); + QVERIFY(privateInfo->data != privateInfo3->data); + QVERIFY(privateInfo2->data != privateInfo3->data); + QCOMPARE(privateInfo->data, privateInfo2->data); + + +} + tst_QFileInfo::tst_QFileInfo() { Q_SET_DEFAULT_IAP diff --git a/tests/auto/qfontdialog/tst_qfontdialog.cpp b/tests/auto/qfontdialog/tst_qfontdialog.cpp index c12c229..1444ee0 100644 --- a/tests/auto/qfontdialog/tst_qfontdialog.cpp +++ b/tests/auto/qfontdialog/tst_qfontdialog.cpp @@ -47,7 +47,9 @@ #include <qfontinfo.h> #include <qtimer.h> #include <qmainwindow.h> +#include <qlistview.h> #include "qfontdialog.h" +#include <private/qfontdialog_p.h> //TESTED_CLASS= //TESTED_FILES= @@ -74,6 +76,7 @@ public slots: private slots: void defaultOkButton(); void setFont(); + void task256466_wrongStyle(); }; tst_QFontDialog::tst_QFontDialog() @@ -151,6 +154,29 @@ void tst_QFontDialog::setFont() } +class FriendlyFontDialog : public QFontDialog +{ + friend tst_QFontDialog; + Q_DECLARE_PRIVATE(QFontDialog); +}; + +void tst_QFontDialog::task256466_wrongStyle() +{ + QFontDatabase fdb; + FriendlyFontDialog dialog; + QListView *familyList = reinterpret_cast<QListView*>(dialog.d_func()->familyList); + QListView *styleList = reinterpret_cast<QListView*>(dialog.d_func()->styleList); + QListView *sizeList = reinterpret_cast<QListView*>(dialog.d_func()->sizeList); + for (int i = 0; i < familyList->model()->rowCount(); ++i) { + QModelIndex currentFamily = familyList->model()->index(i, 0); + familyList->setCurrentIndex(currentFamily); + QCOMPARE(dialog.currentFont(), fdb.font(currentFamily.data().toString(), + styleList->currentIndex().data().toString(), sizeList->currentIndex().data().toInt())); + } +} + + + QTEST_MAIN(tst_QFontDialog) #include "tst_qfontdialog.moc" diff --git a/tests/auto/qformlayout/tst_qformlayout.cpp b/tests/auto/qformlayout/tst_qformlayout.cpp index 9806557..74384a9 100644 --- a/tests/auto/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/qformlayout/tst_qformlayout.cpp @@ -125,6 +125,7 @@ private slots: Qt::Orientations expandingDirections() const; */ + void fieldMinimumSize(); }; tst_QFormLayout::tst_QFormLayout() @@ -905,6 +906,35 @@ void tst_QFormLayout::layoutAlone() QTest::qWait(500); } + +void tst_QFormLayout::fieldMinimumSize() +{ + //check that the field with is bigger than its minimumSizeHint for any size of the widget + // even if the label with is not fixed + QWidget w; + QFormLayout layout; + layout.setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); + w.setLayout(&layout); + QLabel label1("Here is a strange test case"); + label1.setWordWrap(true); + QLabel label2("Here is another label"); + label2.setWordWrap(true); + QLabel shortLabel("short"); + QLabel longLabel("Quite long label"); + layout.addRow(&label1, &shortLabel); + layout.addRow(&label2, &longLabel); + w.show(); + int width = w.size().width() + 9; + + do { + w.resize(width, w.size().height()); + layout.activate(); + QVERIFY(shortLabel.size().width() >= shortLabel.minimumSizeHint().width()); + QVERIFY(longLabel.size().width() >= longLabel.minimumSizeHint().width()); + width -= 3; + } while(width >= w.minimumSizeHint().width()); +} + QTEST_MAIN(tst_QFormLayout) #include "tst_qformlayout.moc" diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index ce4828a..a548329 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -153,8 +153,11 @@ void tst_QGraphicsGridLayout::qgraphicsgridlayout() layout.columnStretchFactor(0); layout.count(); layout.horizontalSpacing(); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsGridLayout::itemAt: invalid row, column 0, 0"); layout.itemAt(0, 0); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsGridLayout::itemAt: invalid index 0"); layout.itemAt(0); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsGridLayout::removeAt: invalid index 0"); layout.removeAt(0); layout.rowAlignment(0); layout.rowCount(); @@ -191,7 +194,7 @@ class RectWidget : public QGraphicsWidget public: RectWidget(QGraphicsItem *parent = 0) : QGraphicsWidget(parent){} - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); @@ -235,11 +238,11 @@ static void populateLayout(QGraphicsGridLayout *gridLayout, int width, int heigh * |+---|---+|xxxx| * ||span=2 ||hole| * |+---|---+|xxxx| - * +----+----+----+ + * +----+----+----+ * |xxxx|+---|---+| * |hole||span=2 || * |xxxx|+---|---+| - * +----+----+----+ + * +----+----+----+ */ static void populateLayoutWithSpansAndHoles(QGraphicsGridLayout *gridLayout) { @@ -296,7 +299,7 @@ void tst_QGraphicsGridLayout::addItem() QTest::ignoreMessage(QtWarningMsg, "QGraphicsGridLayout::addItem: invalid row/column: -1"); } else if (rowSpan < 1 || columnSpan < 1) { char buf[1024]; - ::qsnprintf(buf, sizeof(buf), "QGraphicsGridLayout::addItem: invalid row span/column span: %d", + ::qsnprintf(buf, sizeof(buf), "QGraphicsGridLayout::addItem: invalid row span/column span: %d", rowSpan < 1 ? rowSpan : columnSpan); QTest::ignoreMessage(QtWarningMsg, buf); } @@ -458,7 +461,7 @@ void tst_QGraphicsGridLayout::columnCount() QCOMPARE(layout->columnCount(), 3); layout->addItem(new RectWidget(widget), 1, 2); QCOMPARE(layout->columnCount(), 3); - + // ### Talk with Jasmin. Not sure if removeAt() should adjust columnCount(). widget->setLayout(0); layout = new QGraphicsGridLayout(); @@ -524,7 +527,7 @@ void tst_QGraphicsGridLayout::columnMinimumWidth() populateLayout(layout, 3, 2); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + // should at least be a very large number QCOMPARE(layout->columnMinimumWidth(0), 0.0); QCOMPARE(layout->columnMinimumWidth(0), layout->columnMinimumWidth(1)); @@ -559,7 +562,7 @@ void tst_QGraphicsGridLayout::columnPreferredWidth() populateLayout(layout, 3, 2); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + // default preferred width ?? QCOMPARE(layout->columnPreferredWidth(0), 0.0); QCOMPARE(layout->columnPreferredWidth(0), layout->columnPreferredWidth(1)); @@ -594,7 +597,7 @@ void tst_QGraphicsGridLayout::setColumnFixedWidth() populateLayout(layout, 3, 2); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + layout->setColumnFixedWidth(0, 20); layout->setColumnFixedWidth(2, 40); @@ -626,7 +629,7 @@ void tst_QGraphicsGridLayout::columnSpacing() layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); QCOMPARE(layout->columnSpacing(0), 0.0); - + layout->setColumnSpacing(0, 20); view.show(); widget->show(); @@ -655,7 +658,7 @@ void tst_QGraphicsGridLayout::columnStretchFactor() populateLayout(layout, 3, 2); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + layout->setColumnStretchFactor(0, 1); layout->setColumnStretchFactor(1, 2); layout->setColumnStretchFactor(2, 3); @@ -741,7 +744,7 @@ void tst_QGraphicsGridLayout::contentsMargins() scene.addItem(widget); widget->setLayout(layout); layout->addItem(sublayout,0, 1); - + qreal left, top, right, bottom; // sublayouts have 0 margin sublayout->getContentsMargins(&left, &top, &right, &bottom); @@ -750,7 +753,7 @@ void tst_QGraphicsGridLayout::contentsMargins() QCOMPARE(right, 0.0); QCOMPARE(bottom, 0.0); - // top level layouts have style dependent margins. + // top level layouts have style dependent margins. // we'll just check if its different from 0. (applies to all our styles) layout->getContentsMargins(&left, &top, &right, &bottom); QVERIFY(left >= 0.0); @@ -771,7 +774,7 @@ void tst_QGraphicsGridLayout::itemAt() scene.addItem(widget); widget->setLayout(layout); populateLayoutWithSpansAndHoles(layout); - + //itemAt(int row, int column) QVERIFY( layout->itemAt(0,0)); QVERIFY( layout->itemAt(0,1)); @@ -874,7 +877,7 @@ void tst_QGraphicsGridLayout::rowAlignment() QCOMPARE(layout->alignment(layout->itemAt(1,0)), Qt::AlignTop); QCOMPARE(layout->itemAt(1,0)->geometry(), QRectF(0, 101, 50, 50)); QCOMPARE(layout->rowAlignment(1), Qt::AlignVCenter); - QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(101, 126, 50, 50)); + QCOMPARE(layout->itemAt(1,1)->geometry(), QRectF(101, 126, 50, 50)); QCOMPARE(layout->alignment(layout->itemAt(2,0)), Qt::AlignHCenter); QCOMPARE(layout->itemAt(2,0)->geometry(), QRectF(25, 202, 50, 50)); QCOMPARE(layout->rowAlignment(2), Qt::AlignTop); @@ -922,7 +925,7 @@ void tst_QGraphicsGridLayout::rowMaximumHeight() populateLayout(layout, 2, 3); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + // should at least be a very large number QVERIFY(layout->rowMaximumHeight(0) >= 10000); QCOMPARE(layout->rowMaximumHeight(0), layout->rowMaximumHeight(1)); @@ -957,7 +960,7 @@ void tst_QGraphicsGridLayout::rowMinimumHeight() populateLayout(layout, 2, 3); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + // should at least be a very large number QCOMPARE(layout->rowMinimumHeight(0), 0.0); QCOMPARE(layout->rowMinimumHeight(0), layout->rowMinimumHeight(1)); @@ -992,7 +995,7 @@ void tst_QGraphicsGridLayout::rowPreferredHeight() populateLayout(layout, 2, 3); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + // default preferred height ?? QCOMPARE(layout->rowPreferredHeight(0), 0.0); QCOMPARE(layout->rowPreferredHeight(0), layout->rowPreferredHeight(1)); @@ -1028,7 +1031,7 @@ void tst_QGraphicsGridLayout::setRowFixedHeight() populateLayout(layout, 2, 3); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + layout->setRowFixedHeight(0, 20.); layout->setRowFixedHeight(2, 40.); @@ -1060,7 +1063,7 @@ void tst_QGraphicsGridLayout::rowSpacing() layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); QCOMPARE(layout->columnSpacing(0), 0.0); - + layout->setColumnSpacing(0, 20); view.show(); widget->show(); @@ -1090,7 +1093,7 @@ void tst_QGraphicsGridLayout::rowStretchFactor() populateLayout(layout, 2, 3); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); - + layout->setRowStretchFactor(0, 1); layout->setRowStretchFactor(1, 2); layout->setRowStretchFactor(2, 3); @@ -1121,7 +1124,7 @@ void tst_QGraphicsGridLayout::setColumnSpacing() { QFETCH(int, column); QFETCH(qreal, spacing); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -1175,7 +1178,7 @@ void tst_QGraphicsGridLayout::setRowSpacing() { QFETCH(int, row); QFETCH(qreal, spacing); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -1229,7 +1232,7 @@ void tst_QGraphicsGridLayout::setSpacing() void tst_QGraphicsGridLayout::sizeHint_data() { - + /* QTest::addColumn<Qt::SizeHint>("which"); QTest::addColumn<QSizeF>("constraint"); @@ -1308,7 +1311,7 @@ void tst_QGraphicsGridLayout::layoutDirection() RectWidget *w4 = new RectWidget; w4->setMinimumSize(30, 20); layout->addItem(w4, 1, 1); - + layout->setAlignment(w2, Qt::AlignRight); layout->setAlignment(w3, Qt::AlignLeft); @@ -1325,7 +1328,7 @@ void tst_QGraphicsGridLayout::layoutDirection() QCOMPARE(w3->geometry().right(), 21.0); QCOMPARE(w4->geometry().left(), 37.0); QCOMPARE(w4->geometry().right(), 67.0); - + window->setLayoutDirection(Qt::RightToLeft); QApplication::processEvents(); QCOMPARE(w1->geometry().left(), 39.0); @@ -1336,7 +1339,7 @@ void tst_QGraphicsGridLayout::layoutDirection() QCOMPARE(w3->geometry().right(), 69.0); QCOMPARE(w4->geometry().left(), 3.0); QCOMPARE(w4->geometry().right(), 33.0); - + delete window; } @@ -1437,7 +1440,7 @@ struct ItemDesc m_sizes[Qt::MaximumSize] = sz; return (*this); } - + ItemDesc &alignment(Qt::Alignment alignment) { m_align = alignment; return (*this); @@ -1506,8 +1509,8 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); QTest::newRow("ignoreitem01") << (ItemList() @@ -1527,8 +1530,8 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); QTest::newRow("ignoreitem01_resize120x40") << (ItemList() @@ -1548,8 +1551,8 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF(120, 40) << (SizeList() - << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) - << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) + << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) + << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) ); QTest::newRow("ignoreitem11_resize120x40") << (ItemList() @@ -1569,8 +1572,8 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF(120, 60) << (SizeList() - << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) - << QSizeF(20,40) << QSizeF(40,40) << QSizeF(60,40) + << QSizeF(20,20) << QSizeF(40,20) << QSizeF(60,20) + << QSizeF(20,40) << QSizeF(40,40) << QSizeF(60,40) ); QTest::newRow("ignoreitem01_span01_resize70x60") << (ItemList() @@ -1589,10 +1592,10 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF(70, 60) << (SizeList() - << QSizeF(20,20) << QSizeF(10,60) << QSizeF(40,20) - << QSizeF(20,40) << QSizeF(40,40) + << QSizeF(20,20) << QSizeF(10,60) << QSizeF(40,20) + << QSizeF(20,40) << QSizeF(40,40) ); - + QTest::newRow("ignoreitem10_resize40x120") << (ItemList() << ItemDesc(0,0) .preferredSizeHint(QSizeF(10,10)) @@ -1610,11 +1613,11 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF(40, 120) << (SizeList() - << QSizeF(20,20) << QSizeF(20,20) - << QSizeF(20,40) << QSizeF(20,40) - << QSizeF(20,60) << QSizeF(20,60) + << QSizeF(20,20) << QSizeF(20,20) + << QSizeF(20,40) << QSizeF(20,40) + << QSizeF(20,60) << QSizeF(20,60) ); - + QTest::newRow("ignoreitem01_span02") << (ItemList() << ItemDesc(0,0) .preferredSizeHint(QSizeF(10,10)) @@ -1631,7 +1634,7 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(0,20) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(0,20) << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); @@ -1651,7 +1654,7 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(0,20) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(0,20) << QSizeF(10,10) << QSizeF(10,10) ); @@ -1670,7 +1673,7 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,20) << QSizeF(10,10) << QSizeF(0,20) + << QSizeF(10,20) << QSizeF(10,10) << QSizeF(0,20) << QSizeF(10,10) ); @@ -1690,7 +1693,7 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(20,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(20,10) << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); @@ -1710,10 +1713,10 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(20,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(20,10) << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); - + QTest::newRow("ignorecolumn1_resize70x60") << (ItemList() << ItemDesc(0,0) .preferredSizeHint(QSizeF(10,10)) @@ -1732,8 +1735,8 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF(70, 60) << (SizeList() - << QSizeF(20,20) << QSizeF(10,20) << QSizeF(40,20) - << QSizeF(20,40) << QSizeF(10,40) << QSizeF(40,40) + << QSizeF(20,20) << QSizeF(10,20) << QSizeF(40,20) + << QSizeF(20,40) << QSizeF(10,40) << QSizeF(40,40) ); QTest::newRow("ignorerow0") << (ItemList() @@ -1756,7 +1759,7 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() << QSizeF() << (SizeList() << QSizeF(10,0) << QSizeF(10,0) << QSizeF(10,0) - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) ); QTest::newRow("ignorerow1") << (ItemList() @@ -1778,10 +1781,10 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() ) << QSizeF() << (SizeList() - << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) + << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,10) << QSizeF(10,0) << QSizeF(10,0) << QSizeF(10,0) ); - + QTest::newRow("ignorerow0_resize60x50") << (ItemList() << ItemDesc(0,0) .sizePolicy(QSizePolicy::Ignored) @@ -1802,9 +1805,9 @@ void tst_QGraphicsGridLayout::defaultStretchFactors_data() << QSizeF(60, 50) << (SizeList() << QSizeF(10,10) << QSizeF(20,10) << QSizeF(30,10) - << QSizeF(10,40) << QSizeF(20,40) << QSizeF(30,40) + << QSizeF(10,40) << QSizeF(20,40) << QSizeF(30,40) ); - + } void tst_QGraphicsGridLayout::defaultStretchFactors() @@ -1837,13 +1840,13 @@ void tst_QGraphicsGridLayout::defaultStretchFactors() view.resize(400,300); if (newSize.isValid()) widget->resize(newSize); - + QApplication::processEvents(); for (i = 0; i < expectedSizes.count(); ++i) { QSizeF itemSize = layout->itemAt(i)->geometry().size(); QCOMPARE(itemSize, expectedSizes.at(i)); } - + delete widget; } @@ -2024,7 +2027,7 @@ void tst_QGraphicsGridLayout::geometries_data() << (RectList() << QRectF(0, 0, 50,10) << QRectF(0, 10, 10,10) ); - + QTest::newRow("combine_min_sizes") << (ItemList() << ItemDesc(0,0) .minSize(QSizeF(50,10)) @@ -2035,7 +2038,7 @@ void tst_QGraphicsGridLayout::geometries_data() << (RectList() << QRectF(0, 0, 60,10) << QRectF(0, 10, 60,10) ); - + } void tst_QGraphicsGridLayout::geometries() diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index 90c4636..7552f18 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -44,6 +44,7 @@ #include <private/qtextcontrol_p.h> #include <private/qgraphicsitem_p.h> +#include <QStyleOptionGraphicsItem> #include <QAbstractTextDocumentLayout> #include <QBitmap> #include <QCursor> @@ -121,6 +122,7 @@ private slots: void construction(); void constructionWithParent(); void destruction(); + void deleteChildItem(); void scene(); void parentItem(); void setParentItem(); @@ -170,6 +172,7 @@ private slots: void boundingRects2(); void sceneBoundingRect(); void childrenBoundingRect(); + void childrenBoundingRectTransformed(); void childrenBoundingRect2(); void group(); void setGroup(); @@ -219,6 +222,15 @@ private slots: void updateCachedItemAfterMove(); void deviceTransform_data(); void deviceTransform(); + void update(); + void setTransformProperties_data(); + void setTransformProperties(); + void itemUsesExtendedStyleOption(); + void itemSendsGeometryChanges(); + void moveItem(); + void sorting_data(); + void sorting(); + void itemHasNoContents(); // task specific tests below me void task141694_textItemEnsureVisible(); @@ -229,6 +241,9 @@ private slots: void task240400_clickOnTextItem(); void task243707_addChildBeforeParent(); void task197802_childrenVisibility(); + +private: + QList<QGraphicsItem *> paintedItems; }; void tst_QGraphicsItem::init() @@ -248,50 +263,59 @@ void tst_QGraphicsItem::construction() QCOMPARE(int(item->type()), int(QGraphicsEllipseItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsEllipseItem *>(item), (QGraphicsEllipseItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + QCOMPARE(item->flags(), 0); break; case 1: item = new QGraphicsLineItem; QCOMPARE(int(item->type()), int(QGraphicsLineItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsLineItem *>(item), (QGraphicsLineItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + QCOMPARE(item->flags(), 0); break; case 2: item = new QGraphicsPathItem; QCOMPARE(int(item->type()), int(QGraphicsPathItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsPathItem *>(item), (QGraphicsPathItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + QCOMPARE(item->flags(), 0); break; case 3: item = new QGraphicsPixmapItem; QCOMPARE(int(item->type()), int(QGraphicsPixmapItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsPixmapItem *>(item), (QGraphicsPixmapItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + QCOMPARE(item->flags(), 0); break; case 4: item = new QGraphicsPolygonItem; QCOMPARE(int(item->type()), int(QGraphicsPolygonItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsPolygonItem *>(item), (QGraphicsPolygonItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + QCOMPARE(item->flags(), 0); break; case 5: item = new QGraphicsRectItem; QCOMPARE(int(item->type()), int(QGraphicsRectItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsLineItem *>(item), (QGraphicsLineItem *)0); + QCOMPARE(item->flags(), 0); break; case 6: - default: item = new QGraphicsTextItem; QCOMPARE(int(item->type()), int(QGraphicsTextItem::Type)); QCOMPARE(qgraphicsitem_cast<QGraphicsTextItem *>(item), (QGraphicsTextItem *)item); QCOMPARE(qgraphicsitem_cast<QGraphicsRectItem *>(item), (QGraphicsRectItem *)0); + // This is the only item that uses an extended style option. + QCOMPARE(item->flags(), QGraphicsItem::GraphicsItemFlags(QGraphicsItem::ItemUsesExtendedStyleOption)); + break; + default: + qFatal("You broke the logic, please fix!"); break; } QCOMPARE(item->scene(), (QGraphicsScene *)0); QCOMPARE(item->parentItem(), (QGraphicsItem *)0); QVERIFY(item->children().isEmpty()); - QCOMPARE(item->flags(), 0); QVERIFY(item->isVisible()); QVERIFY(item->isEnabled()); QVERIFY(!item->isSelected()); @@ -516,6 +540,29 @@ void tst_QGraphicsItem::destruction() QCOMPARE(itemDeleted, 59); } QCOMPARE(itemDeleted, 109); + { + QGraphicsScene *scene = new QGraphicsScene; + QGraphicsRectItem *parent = new QGraphicsRectItem; + Item *child = new Item; + child->setParentItem(parent); + parent->setVisible(false); + scene->addItem(parent); + QCOMPARE(child->parentItem(), parent); + delete scene; + QCOMPARE(itemDeleted, 110); + } +} + +void tst_QGraphicsItem::deleteChildItem() +{ + QGraphicsScene scene; + QGraphicsItem *rect = scene.addRect(QRectF()); + QGraphicsItem *child1 = new QGraphicsRectItem(rect); + QGraphicsItem *child2 = new QGraphicsRectItem(rect); + QGraphicsItem *child3 = new QGraphicsRectItem(rect); + delete child1; + child2->setParentItem(0); + delete child2; } void tst_QGraphicsItem::scene() @@ -1756,15 +1803,15 @@ void tst_QGraphicsItem::setMatrix() QCOMPARE(rlist.at(2), unrotatedRect); // From post-update (update current state) } -static QList<QGraphicsItem *> paintedItems; +static QList<QGraphicsItem *> _paintedItems; class PainterItem : public QGraphicsItem { protected: QRectF boundingRect() const { return QRectF(-10, -10, 20, 20); } - void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) - { paintedItems << this; } + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) + { _paintedItems << this; painter->fillRect(boundingRect(), Qt::red); } }; void tst_QGraphicsItem::zValue() @@ -1796,10 +1843,10 @@ void tst_QGraphicsItem::zValue() QApplication::sendPostedEvents(); //glib workaround #endif - QVERIFY(!paintedItems.isEmpty()); - QVERIFY((paintedItems.size() % 4) == 0); + QVERIFY(!_paintedItems.isEmpty()); + QVERIFY((_paintedItems.size() % 4) == 0); for (int i = 0; i < 3; ++i) - QVERIFY(paintedItems.at(i)->zValue() < paintedItems.at(i + 1)->zValue()); + QVERIFY(_paintedItems.at(i)->zValue() < _paintedItems.at(i + 1)->zValue()); } void tst_QGraphicsItem::shape() @@ -2898,9 +2945,57 @@ void tst_QGraphicsItem::childrenBoundingRect() childChild->setParentItem(child); childChild->setPos(500, 500); child->rotate(90); + + + scene.addPolygon(parent->mapToScene(parent->boundingRect() | parent->childrenBoundingRect()))->setPen(QPen(Qt::red));; + + QGraphicsView view(&scene); + view.show(); + + QTest::qWait(5000); + QCOMPARE(parent->childrenBoundingRect(), QRectF(-500, -100, 600, 800)); } +void tst_QGraphicsItem::childrenBoundingRectTransformed() +{ + QGraphicsScene scene; + + QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100)); + QGraphicsRectItem *rect2 = scene.addRect(QRectF(0, 0, 100, 100)); + QGraphicsRectItem *rect3 = scene.addRect(QRectF(0, 0, 100, 100)); + QGraphicsRectItem *rect4 = scene.addRect(QRectF(0, 0, 100, 100)); + QGraphicsRectItem *rect5 = scene.addRect(QRectF(0, 0, 100, 100)); + rect2->setParentItem(rect); + rect3->setParentItem(rect2); + rect4->setParentItem(rect3); + rect5->setParentItem(rect4); + + rect2->setTransform(QTransform().translate(50, 50).rotate(45)); + rect2->setPos(25, 25); + rect3->setTransform(QTransform().translate(50, 50).rotate(45)); + rect3->setPos(25, 25); + rect4->setTransform(QTransform().translate(50, 50).rotate(45)); + rect4->setPos(25, 25); + rect5->setTransform(QTransform().translate(50, 50).rotate(45)); + rect5->setPos(25, 25); + + QRectF subTreeRect = rect->childrenBoundingRect(); + QCOMPARE(subTreeRect.left(), qreal(-206.0660171779821)); + QCOMPARE(subTreeRect.top(), qreal(75.0)); + QCOMPARE(subTreeRect.width(), qreal(351.7766952966369)); + QCOMPARE(subTreeRect.height(), qreal(251.7766952966369)); + + rect->rotate(45); + rect2->rotate(-45); + rect3->rotate(45); + rect4->rotate(-45); + rect5->rotate(45); + + subTreeRect = rect->childrenBoundingRect(); + QCOMPARE(rect->childrenBoundingRect(), QRectF(-100, 75, 275, 250)); +} + void tst_QGraphicsItem::childrenBoundingRect2() { QGraphicsItemGroup box; @@ -3109,150 +3204,134 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *) { ++counter; } }; + void tst_QGraphicsItem::handlesChildEvents() { - ChildEventTester *item2_level2 = new ChildEventTester(QRectF(0, 0, 25, 25)); - ChildEventTester *item1_level1 = new ChildEventTester(QRectF(0, 0, 50, 50)); - ChildEventTester *item_level0 = new ChildEventTester(QRectF(0, 0, 100, 100)); - ChildEventTester *item1_level2 = new ChildEventTester(QRectF(0, 0, 25, 25)); - ChildEventTester *item2_level1 = new ChildEventTester(QRectF(0, 0, 50, 50)); - - item_level0->setBrush(QBrush(Qt::blue)); - item1_level1->setBrush(QBrush(Qt::red)); - item2_level1->setBrush(QBrush(Qt::yellow)); - item1_level2->setBrush(QBrush(Qt::green)); - item2_level2->setBrush(QBrush(Qt::gray)); - item1_level1->setPos(50, 0); - item2_level1->setPos(50, 50); - item1_level2->setPos(25, 0); - item2_level2->setPos(25, 25); - item1_level1->setParentItem(item_level0); - item2_level1->setParentItem(item_level0); - item1_level2->setParentItem(item1_level1); - item2_level2->setParentItem(item1_level1); + ChildEventTester *blue = new ChildEventTester(QRectF(0, 0, 100, 100)); + ChildEventTester *red = new ChildEventTester(QRectF(0, 0, 50, 50)); + ChildEventTester *green = new ChildEventTester(QRectF(0, 0, 25, 25)); + ChildEventTester *gray = new ChildEventTester(QRectF(0, 0, 25, 25)); + ChildEventTester *yellow = new ChildEventTester(QRectF(0, 0, 50, 50)); + + blue->setBrush(QBrush(Qt::blue)); + red->setBrush(QBrush(Qt::red)); + yellow->setBrush(QBrush(Qt::yellow)); + green->setBrush(QBrush(Qt::green)); + gray->setBrush(QBrush(Qt::gray)); + red->setPos(50, 0); + yellow->setPos(50, 50); + green->setPos(25, 0); + gray->setPos(25, 25); + red->setParentItem(blue); + yellow->setParentItem(blue); + green->setParentItem(red); + gray->setParentItem(red); QGraphicsScene scene; - scene.addItem(item_level0); - - // Pull out the items, closest item first - QList<QGraphicsItem *> items = scene.items(scene.itemsBoundingRect()); - QCOMPARE(items.at(4), (QGraphicsItem *)item_level0); - if (item1_level1 < item2_level1) { - QCOMPARE(items.at(3), (QGraphicsItem *)item1_level1); - if (item1_level2 < item2_level2) { - QCOMPARE(items.at(2), (QGraphicsItem *)item1_level2); - QCOMPARE(items.at(1), (QGraphicsItem *)item2_level2); - } else { - QCOMPARE(items.at(2), (QGraphicsItem *)item2_level2); - QCOMPARE(items.at(1), (QGraphicsItem *)item1_level2); - } - QCOMPARE(items.at(0), (QGraphicsItem *)item2_level1); - } else { - QCOMPARE(items.at(3), (QGraphicsItem *)item2_level1); - QCOMPARE(items.at(2), (QGraphicsItem *)item1_level1); - if (item1_level2 < item2_level2) { - QCOMPARE(items.at(1), (QGraphicsItem *)item1_level2); - QCOMPARE(items.at(0), (QGraphicsItem *)item2_level2); - } else { - QCOMPARE(items.at(1), (QGraphicsItem *)item2_level2); - QCOMPARE(items.at(0), (QGraphicsItem *)item1_level2); - } - } + scene.addItem(blue); QGraphicsView view(&scene); view.show(); QTest::qWait(1000); - QCOMPARE(item_level0->counter, 0); + // Pull out the items, closest item first + QList<QGraphicsItem *> items = scene.items(scene.itemsBoundingRect()); + QCOMPARE(items.at(0), (QGraphicsItem *)yellow); + QCOMPARE(items.at(1), (QGraphicsItem *)gray); + QCOMPARE(items.at(2), (QGraphicsItem *)green); + QCOMPARE(items.at(3), (QGraphicsItem *)red); + QCOMPARE(items.at(4), (QGraphicsItem *)blue); + + QCOMPARE(blue->counter, 0); // Send events to the toplevel item QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); pressEvent.setButton(Qt::LeftButton); - pressEvent.setScenePos(item_level0->mapToScene(5, 5)); + pressEvent.setScenePos(blue->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); releaseEvent.setButton(Qt::LeftButton); - releaseEvent.setScenePos(item_level0->mapToScene(5, 5)); + releaseEvent.setScenePos(blue->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 2); + QCOMPARE(blue->counter, 2); // Send events to a level1 item - pressEvent.setScenePos(item1_level1->mapToScene(5, 5)); + pressEvent.setScenePos(red->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level1->mapToScene(5, 5)); + releaseEvent.setScenePos(red->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 2); - QCOMPARE(item1_level1->counter, 2); + QCOMPARE(blue->counter, 2); + QCOMPARE(red->counter, 2); // Send events to a level2 item - pressEvent.setScenePos(item1_level2->mapToScene(5, 5)); + pressEvent.setScenePos(green->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level2->mapToScene(5, 5)); + releaseEvent.setScenePos(green->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 2); - QCOMPARE(item1_level1->counter, 2); - QCOMPARE(item1_level2->counter, 2); + QCOMPARE(blue->counter, 2); + QCOMPARE(red->counter, 2); + QCOMPARE(green->counter, 2); - item_level0->setHandlesChildEvents(true); + blue->setHandlesChildEvents(true); // Send events to a level1 item - pressEvent.setScenePos(item1_level1->mapToScene(5, 5)); + pressEvent.setScenePos(red->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level1->mapToScene(5, 5)); + releaseEvent.setScenePos(red->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 4); - QCOMPARE(item1_level1->counter, 2); + QCOMPARE(blue->counter, 4); + QCOMPARE(red->counter, 2); // Send events to a level2 item - pressEvent.setScenePos(item1_level2->mapToScene(5, 5)); + pressEvent.setScenePos(green->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level2->mapToScene(5, 5)); + releaseEvent.setScenePos(green->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 6); - QCOMPARE(item1_level1->counter, 2); - QCOMPARE(item1_level2->counter, 2); + QCOMPARE(blue->counter, 6); + QCOMPARE(red->counter, 2); + QCOMPARE(green->counter, 2); - item_level0->setHandlesChildEvents(false); + blue->setHandlesChildEvents(false); // Send events to a level1 item - pressEvent.setScenePos(item1_level1->mapToScene(5, 5)); + pressEvent.setScenePos(red->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level1->mapToScene(5, 5)); + releaseEvent.setScenePos(red->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 6); - QCOMPARE(item1_level1->counter, 4); + QCOMPARE(blue->counter, 6); + QCOMPARE(red->counter, 4); // Send events to a level2 item - pressEvent.setScenePos(item1_level2->mapToScene(5, 5)); + pressEvent.setScenePos(green->mapToScene(5, 5)); pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos())); - releaseEvent.setScenePos(item1_level2->mapToScene(5, 5)); + releaseEvent.setScenePos(green->mapToScene(5, 5)); releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos())); QApplication::sendEvent(&scene, &pressEvent); QApplication::sendEvent(&scene, &releaseEvent); - QCOMPARE(item_level0->counter, 6); - QCOMPARE(item1_level1->counter, 4); - QCOMPARE(item1_level2->counter, 4); + QCOMPARE(blue->counter, 6); + QCOMPARE(red->counter, 4); + QCOMPARE(green->counter, 4); } void tst_QGraphicsItem::handlesChildEvents2() @@ -3670,8 +3749,20 @@ void tst_QGraphicsItem::defaultItemTest_QGraphicsEllipseItem() class ItemChangeTester : public QGraphicsRectItem { public: - ItemChangeTester(){} - ItemChangeTester(QGraphicsItem *parent) : QGraphicsRectItem(parent) {} + ItemChangeTester() + { setFlag(ItemSendsGeometryChanges); clear(); } + ItemChangeTester(QGraphicsItem *parent) : QGraphicsRectItem(parent) + { setFlag(ItemSendsGeometryChanges); clear(); } + + void clear() + { + itemChangeReturnValue = QVariant(); + itemSceneChangeTargetScene = 0; + changes.clear(); + values.clear(); + oldValues.clear(); + } + QVariant itemChangeReturnValue; QGraphicsScene *itemSceneChangeTargetScene; @@ -3848,6 +3939,20 @@ void tst_QGraphicsItem::itemChange() QCOMPARE(tester.pos(), QPointF(42, 0)); } { + // ItemZValueChange / ItemZValueHasChanged + tester.itemChangeReturnValue = qreal(2.0); + tester.setZValue(1.0); + ++changeCount; // notification sent too + ++changeCount; + QCOMPARE(tester.changes.size(), changeCount); + QCOMPARE(tester.changes.at(tester.changes.size() - 2), QGraphicsItem::ItemZValueChange); + QCOMPARE(tester.changes.at(tester.changes.size() - 1), QGraphicsItem::ItemZValueHasChanged); + QCOMPARE(tester.values.at(tester.changes.size() - 2), QVariant(qreal(1.0))); + QCOMPARE(tester.values.at(tester.changes.size() - 1), QVariant(qreal(2.0))); + QCOMPARE(tester.oldValues.last(), QVariant(qreal(0.0))); + QCOMPARE(tester.zValue(), qreal(2.0)); + } + { // ItemFlagsChange tester.itemChangeReturnValue = QGraphicsItem::ItemIsSelectable; tester.setFlag(QGraphicsItem::ItemIsSelectable, false); @@ -3858,7 +3963,8 @@ void tst_QGraphicsItem::itemChange() QCOMPARE(tester.changes.size(), changeCount); QCOMPARE(tester.changes.at(tester.changes.size() - 2), QGraphicsItem::ItemFlagsChange); QCOMPARE(tester.changes.at(tester.changes.size() - 1), QGraphicsItem::ItemFlagsHaveChanged); - QCOMPARE(tester.values.at(tester.values.size() - 2), qVariantFromValue<quint32>(QGraphicsItem::ItemIsSelectable)); + QVariant expectedFlags = qVariantFromValue<quint32>(QGraphicsItem::GraphicsItemFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges)); + QCOMPARE(tester.values.at(tester.values.size() - 2), expectedFlags); QCOMPARE(tester.values.at(tester.values.size() - 1), qVariantFromValue<quint32>(QGraphicsItem::ItemIsSelectable)); } { @@ -4656,7 +4762,7 @@ void tst_QGraphicsItem::itemClipsChildrenToShape2() QPainter painter(&image); scene.render(&painter); painter.end(); - + QCOMPARE(image.pixel(5, 5), QColor(0, 0, 255).rgba()); QCOMPARE(image.pixel(5, 10), QRgb(0)); QCOMPARE(image.pixel(10, 5), QRgb(0)); @@ -5313,7 +5419,7 @@ void tst_QGraphicsItem::task240400_clickOnTextItem() QVERIFY(selectable ? item->isSelected() : !item->isSelected()); - // + // if (textFlags & Qt::TextEditorInteraction) QVERIFY(item->textCursor().columnNumber() > column); else @@ -5753,19 +5859,36 @@ void tst_QGraphicsItem::opacityZeroUpdates() QCOMPARE(view.paintedRegion, expectedRegion); } +class StacksBehindParentHelper : public QGraphicsRectItem +{ +public: + StacksBehindParentHelper(QList<QGraphicsItem *> *paintedItems, const QRectF &rect, QGraphicsItem *parent = 0) + : QGraphicsRectItem(rect, parent), paintedItems(paintedItems) + { } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + QGraphicsRectItem::paint(painter, option, widget); + paintedItems->append(this); + } + +private: + QList<QGraphicsItem *> *paintedItems; +}; + void tst_QGraphicsItem::itemStacksBehindParent() { - QGraphicsRectItem *parent1 = new QGraphicsRectItem(QRectF(0, 0, 100, 50)); - QGraphicsRectItem *child11 = new QGraphicsRectItem(QRectF(-10, 10, 50, 50), parent1); - QGraphicsRectItem *grandChild111 = new QGraphicsRectItem(QRectF(-20, 20, 50, 50), child11); - QGraphicsRectItem *child12 = new QGraphicsRectItem(QRectF(60, 10, 50, 50), parent1); - QGraphicsRectItem *grandChild121 = new QGraphicsRectItem(QRectF(70, 20, 50, 50), child12); + StacksBehindParentHelper *parent1 = new StacksBehindParentHelper(&paintedItems, QRectF(0, 0, 100, 50)); + StacksBehindParentHelper *child11 = new StacksBehindParentHelper(&paintedItems, QRectF(-10, 10, 50, 50), parent1); + StacksBehindParentHelper *grandChild111 = new StacksBehindParentHelper(&paintedItems, QRectF(-20, 20, 50, 50), child11); + StacksBehindParentHelper *child12 = new StacksBehindParentHelper(&paintedItems, QRectF(60, 10, 50, 50), parent1); + StacksBehindParentHelper *grandChild121 = new StacksBehindParentHelper(&paintedItems, QRectF(70, 20, 50, 50), child12); - QGraphicsRectItem *parent2 = new QGraphicsRectItem(QRectF(0, 0, 100, 50)); - QGraphicsRectItem *child21 = new QGraphicsRectItem(QRectF(-10, 10, 50, 50), parent2); - QGraphicsRectItem *grandChild211 = new QGraphicsRectItem(QRectF(-20, 20, 50, 50), child21); - QGraphicsRectItem *child22 = new QGraphicsRectItem(QRectF(60, 10, 50, 50), parent2); - QGraphicsRectItem *grandChild221 = new QGraphicsRectItem(QRectF(70, 20, 50, 50), child22); + StacksBehindParentHelper *parent2 = new StacksBehindParentHelper(&paintedItems, QRectF(0, 0, 100, 50)); + StacksBehindParentHelper *child21 = new StacksBehindParentHelper(&paintedItems, QRectF(-10, 10, 50, 50), parent2); + StacksBehindParentHelper *grandChild211 = new StacksBehindParentHelper(&paintedItems, QRectF(-20, 20, 50, 50), child21); + StacksBehindParentHelper *child22 = new StacksBehindParentHelper(&paintedItems, QRectF(60, 10, 50, 50), parent2); + StacksBehindParentHelper *grandChild221 = new StacksBehindParentHelper(&paintedItems, QRectF(70, 20, 50, 50), child22); parent1->setData(0, "parent1"); child11->setData(0, "child11"); @@ -5787,25 +5910,57 @@ void tst_QGraphicsItem::itemStacksBehindParent() scene.addItem(parent1); scene.addItem(parent2); + paintedItems.clear(); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(250); + QCOMPARE(scene.items(0, 0, 100, 100), (QList<QGraphicsItem *>() << grandChild111 << child11 << grandChild121 << child12 << parent1 << grandChild211 << child21 << grandChild221 << child22 << parent2)); + QCOMPARE(paintedItems, QList<QGraphicsItem *>() + << parent2 << child22 << grandChild221 + << child21 << grandChild211 + << parent1 << child12 << grandChild121 + << child11 << grandChild111); child11->setFlag(QGraphicsItem::ItemStacksBehindParent); + scene.update(); + paintedItems.clear(); + QTest::qWait(250); + QCOMPARE(scene.items(0, 0, 100, 100), (QList<QGraphicsItem *>() << grandChild121 << child12 << parent1 << grandChild111 << child11 << grandChild211 << child21 << grandChild221 << child22 << parent2)); + QCOMPARE(paintedItems, QList<QGraphicsItem *>() + << parent2 << child22 << grandChild221 + << child21 << grandChild211 + << child11 << grandChild111 + << parent1 << child12 << grandChild121); child12->setFlag(QGraphicsItem::ItemStacksBehindParent); + paintedItems.clear(); + scene.update(); + QTest::qWait(250); + QCOMPARE(scene.items(0, 0, 100, 100), (QList<QGraphicsItem *>() << parent1 << grandChild111 << child11 << grandChild121 << child12 << grandChild211 << child21 << grandChild221 << child22 << parent2)); + QCOMPARE(paintedItems, QList<QGraphicsItem *>() + << parent2 << child22 << grandChild221 + << child21 << grandChild211 + << child12 << grandChild121 + << child11 << grandChild111 << parent1); } class ClippingAndTransformsScene : public QGraphicsScene @@ -5852,8 +6007,9 @@ void tst_QGraphicsItem::nestedClipping() l1->setData(0, "l1"); l2->setData(0, "l2"); l3->setData(0, "l3"); - + QGraphicsView view(&scene); + view.setOptimizationFlag(QGraphicsView::IndirectPainting); view.show(); #ifdef Q_WS_X11 qt_x11_wait_for_window_manager(&view); @@ -5880,7 +6036,7 @@ void tst_QGraphicsItem::nestedClipping() QCOMPARE(image.pixel(80, 130), qRgba(255, 255, 0, 255)); QCOMPARE(image.pixel(92, 105), qRgba(0, 255, 0, 255)); QCOMPARE(image.pixel(105, 105), qRgba(0, 0, 255, 255)); -#if 0 +#if 0 // Enable this to compare if the test starts failing. image.save("nestedClipping_reference.png"); #endif @@ -6022,7 +6178,7 @@ void tst_QGraphicsItem::tabChangesFocus_data() void tst_QGraphicsItem::tabChangesFocus() { QFETCH(bool, tabChangesFocus); - + QGraphicsScene scene; QGraphicsTextItem *item = scene.addText("Hello"); item->setTabChangesFocus(tabChangesFocus); @@ -6406,5 +6562,512 @@ void tst_QGraphicsItem::deviceTransform() QCOMPARE(rect3->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult3); } +class MyGraphicsView : public QGraphicsView +{ +public: + int repaints; + QRegion paintedRegion; + MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {} + void paintEvent(QPaintEvent *e) + { + paintedRegion += e->region(); + ++repaints; + QGraphicsView::paintEvent(e); + } + void reset() { repaints = 0; paintedRegion = QRegion(); } +}; + +void tst_QGraphicsItem::update() +{ + QGraphicsScene scene; + MyGraphicsView view(&scene); + + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + EventTester *item = new EventTester; + scene.addItem(item); + QTest::qWait(100); // Make sure all pending updates are processed. + item->repaints = 0; + + item->update(); // Item marked as dirty + scene.update(); // Entire scene marked as dirty + qApp->processEvents(); + QCOMPARE(item->repaints, 1); + + // Make sure the dirty state from the previous update is reset so that + // the item don't think it is already dirty and discards this update. + item->update(); + qApp->processEvents(); + QCOMPARE(item->repaints, 2); + + // Make sure a partial update doesn't cause a full update to be discarded. + view.reset(); + item->repaints = 0; + item->update(QRectF(0, 0, 5, 5)); + item->update(); + qApp->processEvents(); + QCOMPARE(item->repaints, 1); + QCOMPARE(view.repaints, 1); + const QRect itemDeviceBoundingRect = item->deviceTransform(view.viewportTransform()) + .mapRect(item->boundingRect()).toRect(); + const QRegion expectedRegion = itemDeviceBoundingRect.adjusted(-2, -2, 2, 2); + // The entire item's bounding rect (adjusted for antialiasing) should have been painted. + QCOMPARE(view.paintedRegion, expectedRegion); + + // Make sure update requests outside the bounding rect are discarded. + view.reset(); + item->repaints = 0; + item->update(-15, -15, 5, 5); // Item's brect: (-10, -10, 20, 20) + qApp->processEvents(); + QCOMPARE(item->repaints, 0); + QCOMPARE(view.repaints, 0); +} + +void tst_QGraphicsItem::setTransformProperties_data() +{ + QTest::addColumn<QPointF>("origin"); + QTest::addColumn<qreal>("rotationX"); + QTest::addColumn<qreal>("rotationY"); + QTest::addColumn<qreal>("rotationZ"); + QTest::addColumn<qreal>("scaleX"); + QTest::addColumn<qreal>("scaleY"); + QTest::addColumn<qreal>("shearX"); + QTest::addColumn<qreal>("shearY"); + + QTest::newRow("nothing") << QPointF() << qreal(0.0) << qreal(0.0) << qreal(0.0) + << qreal(1.0) << qreal(1.0) << qreal(0.0) << qreal(0.0); + + QTest::newRow("rotationZ") << QPointF() << qreal(0.0) << qreal(0.0) << qreal(42.2) + << qreal(1.0) << qreal(1.0) << qreal(0.0) << qreal(0.0); + + QTest::newRow("rotationXY") << QPointF() << qreal(12.5) << qreal(53.6) << qreal(0.0) + << qreal(1.0) << qreal(1.0) << qreal(0.0) << qreal(0.0); + + QTest::newRow("rotationXYZ") << QPointF() << qreal(-25) << qreal(12) << qreal(556) + << qreal(1.0) << qreal(1.0) << qreal(0.0) << qreal(0.0); + + QTest::newRow("rotationXYZ dicentred") << QPointF(-53, 25.2) + << qreal(-2578.2) << qreal(4565.2) << qreal(56) + << qreal(1.0) << qreal(1.0) << qreal(0.0) << qreal(0.0); + + QTest::newRow("Scale") << QPointF() << qreal(0.0) << qreal(0.0) << qreal(0.0) + << qreal(6) << qreal(0.5) << qreal(0.0) << qreal(0.0); + + QTest::newRow("Shear") << QPointF() << qreal(0.0) << qreal(0.0) << qreal(0.0) + << qreal(1.0) << qreal(1.0) << qreal(2.2) << qreal(0.5); + + QTest::newRow("Scale and Shear") << QPointF() << qreal(0.0) << qreal(0.0) << qreal(0.0) + << qreal(5.2) << qreal(2.1) << qreal(5.2) << qreal(5.5); + + QTest::newRow("Everything") << QPointF() << qreal(41) << qreal(-23) << qreal(0.56) + << qreal(8.2) << qreal(-0.2) << qreal(-12) << qreal(-0.8); + + QTest::newRow("Everything dicentred") << QPointF(qreal(22.3), qreal(-56.2)) << qreal(-175) << qreal(196) << qreal(-1260) + << qreal(4) << qreal(2) << qreal(2.56) << qreal(0.8); +} + +/** + * the normal QCOMPARE doesn't work because it doesn't use qFuzzyCompare + */ +#define QCOMPARE_TRANSFORM(X1, X2) QVERIFY(((X1)*(X2).inverted()).isIdentity()) + +void tst_QGraphicsItem::setTransformProperties() +{ + QFETCH(QPointF,origin); + QFETCH(qreal,rotationX); + QFETCH(qreal,rotationY); + QFETCH(qreal,rotationZ); + QFETCH(qreal,scaleX); + QFETCH(qreal,scaleY); + QFETCH(qreal,shearX); + QFETCH(qreal,shearY); + + QTransform result; + result.translate(origin.x(), origin.y()); + result.rotate(rotationX, Qt::XAxis); + result.rotate(rotationY, Qt::YAxis); + result.rotate(rotationZ, Qt::ZAxis); + result.shear(shearX, shearY); + result.scale(scaleX, scaleY); + result.translate(-origin.x(), -origin.y()); + + QGraphicsScene scene; + QGraphicsRectItem *item = new QGraphicsRectItem(QRectF(0, 0, 100, 100)); + scene.addItem(item); + + item->setRotation(rotationX, rotationY, rotationZ); + item->setScale(scaleX, scaleY); + item->setShear(shearX, shearY); + item->setTransformOrigin(origin); + + QCOMPARE(item->xRotation(), rotationX); + QCOMPARE(item->yRotation(), rotationY); + QCOMPARE(item->zRotation(), rotationZ); + QCOMPARE(item->xScale(), scaleX); + QCOMPARE(item->yScale(), scaleY); + QCOMPARE(item->horizontalShear(), shearX); + QCOMPARE(item->verticalShear(), shearY); + QCOMPARE(item->transformOrigin(), origin); + + QCOMPARE(QTransform(), item->transform()); + QCOMPARE(result, item->sceneTransform()); + + //----------------------------------------------------------------- + //Change the rotation Z + item->setZRotation(45); + QTransform result2; + result2.translate(origin.x(), origin.y()); + result2.rotate(rotationX, Qt::XAxis); + result2.rotate(rotationY, Qt::YAxis); + result2.rotate(45, Qt::ZAxis); + result2.shear(shearX, shearY); + result2.scale(scaleX, scaleY); + result2.translate(-origin.x(), -origin.y()); + + QCOMPARE(item->xRotation(), rotationX); + QCOMPARE(item->yRotation(), rotationY); + QCOMPARE(item->zRotation(), 45.0); + QCOMPARE(item->xScale(), scaleX); + QCOMPARE(item->yScale(), scaleY); + QCOMPARE(item->horizontalShear(), shearX); + QCOMPARE(item->verticalShear(), shearY); + QCOMPARE(item->transformOrigin(), origin); + + QCOMPARE(QTransform(), item->transform()); + QCOMPARE(result2, item->sceneTransform()); + + //----------------------------------------------------------------- + // calling setTransform() and setPos shoukld change the sceneTransform + item->setTransform(result); + item->setPos(100, -150.5); + + QCOMPARE(item->xRotation(), rotationX); + QCOMPARE(item->yRotation(), rotationY); + QCOMPARE(item->zRotation(), 45.0); + QCOMPARE(item->xScale(), scaleX); + QCOMPARE(item->yScale(), scaleY); + QCOMPARE(item->horizontalShear(), shearX); + QCOMPARE(item->verticalShear(), shearY); + QCOMPARE(item->transformOrigin(), origin); + QCOMPARE(result, item->transform()); + + QTransform result3; + + result3.translate(origin.x(), origin.y()); + result3 = result * result3; + result3.rotate(rotationX, Qt::XAxis); + result3.rotate(rotationY, Qt::YAxis); + result3.rotate(45, Qt::ZAxis); + result3.shear(shearX, shearY); + result3.scale(scaleX, scaleY); + result3.translate(-origin.x(), -origin.y()); + + result3 *= QTransform::fromTranslate(100, -150.5); //the pos; + + QCOMPARE(result3, item->sceneTransform()); + + //----------------------------------------------------- + // setting the propertiees should be the same as setting a transform + {//with center origin on the matrix + QGraphicsRectItem *item1 = new QGraphicsRectItem(QRectF(50.2, -150, 230.5, 119)); + scene.addItem(item1); + QGraphicsRectItem *item2 = new QGraphicsRectItem(QRectF(50.2, -150, 230.5, 119)); + scene.addItem(item2); + + item1->setPos(12.3, -5); + item2->setPos(12.3, -5); + item1->setRotation(rotationX, rotationY, rotationZ); + item1->setScale(scaleX, scaleY); + item1->setShear(shearX, shearY); + item1->setTransformOrigin(origin); + + item2->setTransform(result); + + QCOMPARE_TRANSFORM(item1->sceneTransform(), item2->sceneTransform()); + + QCOMPARE_TRANSFORM(item1->itemTransform(item2), QTransform()); + QCOMPARE_TRANSFORM(item2->itemTransform(item1), QTransform()); + } + + {//with center origin on the item + QGraphicsRectItem *item1 = new QGraphicsRectItem(QRectF(50.2, -150, 230.5, 119)); + scene.addItem(item1); + QGraphicsRectItem *item2 = new QGraphicsRectItem(QRectF(50.2, -150, 230.5, 119)); + scene.addItem(item2); + + item1->setPos(12.3, -5); + item2->setPos(12.3, -5); + item1->setTransformOrigin(origin); + item2->setTransformOrigin(origin); + + item1->setRotation(rotationX, rotationY, rotationZ); + item1->setScale(scaleX, scaleY); + item1->setShear(shearX, shearY); + + QTransform tr; + tr.rotate(rotationX, Qt::XAxis); + tr.rotate(rotationY, Qt::YAxis); + tr.rotate(rotationZ, Qt::ZAxis); + tr.shear(shearX, shearY); + tr.scale(scaleX, scaleY); + + item2->setTransform(tr); + + QCOMPARE_TRANSFORM(item1->sceneTransform(), item2->sceneTransform()); + + QCOMPARE_TRANSFORM(item1->itemTransform(item2), QTransform()); + QCOMPARE_TRANSFORM(item2->itemTransform(item1), QTransform()); + } +} + +class MyStyleOptionTester : public QGraphicsRectItem +{ +public: + MyStyleOptionTester(const QRectF &rect) + : QGraphicsRectItem(rect), startTrack(false) + {} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) + { + if (startTrack) { + //Doesn't use the extended style option so the exposed rect is the boundingRect + if (!(flags() & QGraphicsItem::ItemUsesExtendedStyleOption)) { + QCOMPARE(option->exposedRect, boundingRect()); + } else { + QVERIFY(option->exposedRect != QRect()); + QVERIFY(option->exposedRect != boundingRect()); + } + } + QGraphicsRectItem::paint(painter, option, widget); + } + bool startTrack; +}; + +void tst_QGraphicsItem::itemUsesExtendedStyleOption() +{ + QGraphicsScene scene(0, 0, 300, 300); + QGraphicsPixmapItem item; + item.setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true); + QCOMPARE(item.flags(), QGraphicsItem::GraphicsItemFlags(QGraphicsItem::ItemUsesExtendedStyleOption)); + item.setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, false); + QCOMPARE(item.flags(), 0); + + //We now test the content of the style option + MyStyleOptionTester *rect = new MyStyleOptionTester(QRect(0, 0, 100, 100)); + scene.addItem(rect); + rect->setPos(200, 200); + QGraphicsView view(&scene); + QTest::qWait(500); + rect->startTrack = true; + rect->update(10, 10, 10, 10); + QTest::qWait(125); + rect->startTrack = false; + rect->setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true); + QVERIFY((rect->flags() & QGraphicsItem::ItemUsesExtendedStyleOption)); + QTest::qWait(125); + rect->startTrack = true; + rect->update(10, 10, 10, 10); + QTest::qWait(125); +} + +void tst_QGraphicsItem::itemSendsGeometryChanges() +{ + ItemChangeTester item; + item.setFlags(0); + item.clear(); + + QTransform x = QTransform().rotate(45); + QPointF pos(10, 10); + qreal o(0.5); + item.setTransform(x); + item.setPos(pos); + QCOMPARE(item.transform(), x); + QCOMPARE(item.pos(), pos); + QCOMPARE(item.changes.size(), 0); + + item.setOpacity(o); + QCOMPARE(item.changes.size(), 2); // opacity + + item.setFlag(QGraphicsItem::ItemSendsGeometryChanges); + QCOMPARE(item.changes.size(), 4); // flags + item.setTransform(QTransform()); + item.setPos(QPointF()); + QCOMPARE(item.changes.size(), 8); // transform + pos + QCOMPARE(item.transform(), QTransform()); + QCOMPARE(item.pos(), QPointF()); + QCOMPARE(item.opacity(), o); + + QCOMPARE(item.changes, QList<QGraphicsItem::GraphicsItemChange>() + << QGraphicsItem::ItemOpacityChange + << QGraphicsItem::ItemOpacityHasChanged + << QGraphicsItem::ItemFlagsChange + << QGraphicsItem::ItemFlagsHaveChanged + << QGraphicsItem::ItemTransformChange + << QGraphicsItem::ItemTransformHasChanged + << QGraphicsItem::ItemPositionChange + << QGraphicsItem::ItemPositionHasChanged); +} + +// Make sure we update moved items correctly. +void tst_QGraphicsItem::moveItem() +{ + QGraphicsScene scene; + scene.setSceneRect(-50, -50, 200, 200); + + MyGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + EventTester *parent = new EventTester; + EventTester *child = new EventTester(parent); + EventTester *grandChild = new EventTester(child); + +#define RESET_COUNTERS \ + parent->repaints = 0; \ + child->repaints = 0; \ + grandChild->repaints = 0; \ + view.reset(); + + scene.addItem(parent); + QTest::qWait(100); + + RESET_COUNTERS + + // Item's boundingRect: (-10, -10, 20, 20). + QRect parentDeviceBoundingRect = parent->deviceTransform(view.viewportTransform()) + .mapRect(parent->boundingRect()).toRect() + .adjusted(-2, -2, 2, 2); // Adjusted for antialiasing. + + parent->setPos(20, 20); + qApp->processEvents(); + QCOMPARE(parent->repaints, 1); + QCOMPARE(view.repaints, 1); + QRegion expectedParentRegion = parentDeviceBoundingRect; // old position + parentDeviceBoundingRect.translate(20, 20); + expectedParentRegion += parentDeviceBoundingRect; // new position + QCOMPARE(view.paintedRegion, expectedParentRegion); + + RESET_COUNTERS + + child->setPos(20, 20); + qApp->processEvents(); + QCOMPARE(parent->repaints, 1); + QCOMPARE(child->repaints, 1); + QCOMPARE(view.repaints, 1); + const QRegion expectedChildRegion = expectedParentRegion.translated(20, 20); + QCOMPARE(view.paintedRegion, expectedChildRegion); + + RESET_COUNTERS + + grandChild->setPos(20, 20); + qApp->processEvents(); + QCOMPARE(parent->repaints, 1); + QCOMPARE(child->repaints, 1); + QCOMPARE(grandChild->repaints, 1); + QCOMPARE(view.repaints, 1); + const QRegion expectedGrandChildRegion = expectedParentRegion.translated(40, 40); + QCOMPARE(view.paintedRegion, expectedGrandChildRegion); + + RESET_COUNTERS + + parent->translate(20, 20); + qApp->processEvents(); + QCOMPARE(parent->repaints, 1); + QCOMPARE(child->repaints, 1); + QCOMPARE(grandChild->repaints, 1); + QCOMPARE(view.repaints, 1); + expectedParentRegion.translate(20, 20); + expectedParentRegion += expectedChildRegion.translated(20, 20); + expectedParentRegion += expectedGrandChildRegion.translated(20, 20); + QCOMPARE(view.paintedRegion, expectedParentRegion); +} + +void tst_QGraphicsItem::sorting_data() +{ + QTest::addColumn<int>("index"); + + QTest::newRow("NoIndex") << int(QGraphicsScene::NoIndex); + QTest::newRow("BspTreeIndex") << int(QGraphicsScene::BspTreeIndex); +} + +void tst_QGraphicsItem::sorting() +{ + _paintedItems.clear(); + + QGraphicsScene scene; + QGraphicsItem *grid[100][100]; + for (int x = 0; x < 100; ++x) { + for (int y = 0; y < 100; ++y) { + PainterItem *item = new PainterItem; + item->setPos(x * 25, y * 25); + item->setData(0, QString("%1x%2").arg(x).arg(y)); + grid[x][y] = item; + scene.addItem(item); + } + } + + PainterItem *item1 = new PainterItem; + PainterItem *item2 = new PainterItem; + item1->setData(0, "item1"); + item2->setData(0, "item2"); + scene.addItem(item1); + scene.addItem(item2); + + QGraphicsView view(&scene); + view.setResizeAnchor(QGraphicsView::NoAnchor); + view.setTransformationAnchor(QGraphicsView::NoAnchor); + view.resize(100, 100); + view.setFrameStyle(0); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + _paintedItems.clear(); + + view.viewport()->repaint(); + + QCOMPARE(_paintedItems, QList<QGraphicsItem *>() + << grid[0][0] << grid[0][1] << grid[0][2] << grid[0][3] + << grid[1][0] << grid[1][1] << grid[1][2] << grid[1][3] + << grid[2][0] << grid[2][1] << grid[2][2] << grid[2][3] + << grid[3][0] << grid[3][1] << grid[3][2] << grid[3][3] + << item1 << item2); +} + +void tst_QGraphicsItem::itemHasNoContents() +{ + PainterItem *item1 = new PainterItem; + PainterItem *item2 = new PainterItem; + item2->setParentItem(item1); + item2->setPos(50, 50); + item1->setFlag(QGraphicsItem::ItemHasNoContents); + item1->setFlag(QGraphicsItem::ItemClipsChildrenToShape); + + QGraphicsScene scene; + scene.addItem(item1); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + _paintedItems.clear(); + + view.viewport()->repaint(); + + QCOMPARE(_paintedItems, QList<QGraphicsItem *>() << item2); +} + QTEST_MAIN(tst_QGraphicsItem) #include "tst_qgraphicsitem.moc" diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index b61b2bb..a30e5e5 100644 --- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -163,19 +163,19 @@ class RectWidget : public QGraphicsWidget public: RectWidget(QGraphicsItem *parent = 0, const QBrush &brush = QBrush()) : QGraphicsWidget(parent){ m_brush = brush;} - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(m_brush); painter->drawRoundRect(rect()); } - + void setSizeHint(Qt::SizeHint which, const QSizeF &size) { m_sizeHints[which] = size; updateGeometry(); } - + virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const { if (m_sizeHints[which].isValid()) { return m_sizeHints[which]; @@ -210,17 +210,16 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() layout.setOrientation(Qt::Vertical); layout.orientation(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addItem(0); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addStretch(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); layout.insertItem(0, 0); layout.insertStretch(0, 0); layout.removeItem(0); - layout.removeAt(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); layout.setSpacing(0); layout.spacing(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::setStretchFactor: cannot assign a stretch factor to a null item"); @@ -231,8 +230,7 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() QCOMPARE(layout.alignment(0), 0); layout.setGeometry(QRectF()); layout.geometry(); - //QCOMPARE(layout.count(), 1); - layout.itemAt(0); + QCOMPARE(layout.count(), 0); layout.invalidate(); layout.sizeHint(Qt::MinimumSize, QSizeF()); } @@ -285,7 +283,7 @@ void tst_QGraphicsLinearLayout::alignment() int i; bool addWidget = true; for (i = 0; i < sizeof(alignmentsToTest)/sizeof(Qt::Alignment); ++i) { - QGraphicsLayoutItem *loutItem; + QGraphicsLayoutItem *loutItem; Qt::Alignment align = alignmentsToTest[i]; if (!align && i > 0) addWidget = false; @@ -537,7 +535,10 @@ void tst_QGraphicsLinearLayout::insertItem() QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); layout.insertItem(insertItemAt, item); QCOMPARE(layout.count(), itemCount + layoutCount + 1); - QCOMPARE(layout.itemAt(insertItemAt), insertItemAt == -1 ? (QGraphicsLayoutItem*)0 : item); + + if (insertItemAt >= 0 && (itemCount + layoutCount >= 0)) { + QCOMPARE(layout.itemAt(itemCount + layoutCount), item); + } layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -599,8 +600,7 @@ void tst_QGraphicsLinearLayout::insertStretch() } widget->setLayout(layout); layout->insertStretch(insertItemAt, stretch); - QCOMPARE(layout->itemAt(insertItemAt), (QGraphicsLayoutItem*)0); - QCOMPARE(layout->count(), itemCount + layoutCount + 1); + QCOMPARE(layout->count(), itemCount + layoutCount); layout->activate(); view.show(); @@ -669,7 +669,6 @@ void tst_QGraphicsLinearLayout::invalidate() void tst_QGraphicsLinearLayout::itemAt_data() { QTest::addColumn<int>("index"); - QTest::newRow("-1") << -1; QTest::newRow("0") << 0; QTest::newRow("1") << 1; QTest::newRow("2") << 2; @@ -681,7 +680,10 @@ void tst_QGraphicsLinearLayout::itemAt() // see also the insertItem() etc tests QFETCH(int, index); SubQGraphicsLinearLayout layout; - QCOMPARE(layout.itemAt(index), (QGraphicsLayoutItem*)0); + for (int i = 0; i < 3; ++i) + layout.addItem(new QGraphicsWidget); + + QVERIFY(layout.itemAt(index) != 0); } void tst_QGraphicsLinearLayout::orientation_data() @@ -695,7 +697,7 @@ void tst_QGraphicsLinearLayout::orientation_data() void tst_QGraphicsLinearLayout::orientation() { QFETCH(Qt::Orientation, orientation); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -721,7 +723,7 @@ void tst_QGraphicsLinearLayout::orientation() qreal pos; if (initialOrientation == Qt::Horizontal) pos = item->pos().x(); - else + else pos = item->pos().y(); positions.append(pos); @@ -736,7 +738,7 @@ void tst_QGraphicsLinearLayout::orientation() QGraphicsWidget *item = static_cast<QGraphicsWidget*>(layout.itemAt(i)); if (initialOrientation == Qt::Horizontal) QCOMPARE(item->pos().y(), positions.at(i)); - else + else QCOMPARE(item->pos().x(), positions.at(i)); } @@ -779,19 +781,18 @@ void tst_QGraphicsLinearLayout::removeAt() layout.addItem(new SubQGraphicsLinearLayout); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); - QGraphicsLayoutItem *wParent = 0; + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); if (w) { - wParent = w->parentLayoutItem(); + QGraphicsLayoutItem *wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(&layout)); - } - layout.removeAt(removeItemAt); - if (w) { + layout.removeAt(removeItemAt); wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(0)); + delete w; } - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -829,11 +830,15 @@ void tst_QGraphicsLinearLayout::removeItem() for (int i = 0; i < layoutCount; ++i) layout.addItem(new SubQGraphicsLinearLayout); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - layout.removeItem(w); - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + if (w) { + layout.removeItem(w); + delete w; + } + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -882,7 +887,7 @@ void tst_QGraphicsLinearLayout::setSpacing_data() void tst_QGraphicsLinearLayout::setSpacing() { QFETCH(qreal, spacing); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -994,7 +999,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() /** * The stretch factors are not applied linearly, but they are used together with both the preferred size, maximum size to form the * internal effective stretch factor. - * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. + * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. * (If the size of the layout is the preferred size, then all items should get their preferred sizes. * However, imagine this use case: * Layout @@ -1007,7 +1012,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() * layout->resize(120, h) * * In QLayout, C would become 50, B would become 50 and A would get 20. When scaling a layout this would give a jerky feeling, since - * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will + * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will * resize, and finally, item with the lowest stretch factor will resize. * In QGraphicsLinearLayout we try to scale all items so that they all reach their maximum at the same time. This means that * their relative sizes are not proportional to their stretch factors. @@ -1059,7 +1064,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() view.show(); widget->show(); - + qreal firstStretch = -1; qreal firstExtent = -1.; qreal sumExtent = 0; @@ -1073,7 +1078,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() QCOMPARE(extent, firstExtent); else if (stretch > firstStretch) QVERIFY(extent > firstExtent); - else + else QVERIFY(extent < firstExtent); } else { firstStretch = (qreal)stretch; @@ -1082,7 +1087,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() sumExtent+= extent; } QCOMPARE(sumExtent, totalSize); - + delete widget; } @@ -1167,7 +1172,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() QSizePolicy sp = item->sizePolicy(); if (orientation == Qt::Horizontal) sp.setHorizontalPolicy(QSizePolicy::Policy(sp.horizontalPolicy() | QSizePolicy::IgnoreFlag)); - else + else sp.setVerticalPolicy(QSizePolicy::Policy(sp.verticalPolicy() | QSizePolicy::IgnoreFlag)); item->setSizePolicy(sp); } @@ -1180,7 +1185,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() view.resize(400,300); if (newSize.isValid()) widget->resize(newSize); - + QApplication::processEvents(); for (i = 0; i < count; ++i) { QSizeF itemSize = layout->itemAt(i)->geometry().size(); @@ -1189,7 +1194,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() if (i < expectedSizes.count()) QCOMPARE(itemSize.width(), qreal(expectedSizes.at(i))); } - + delete widget; } @@ -1254,7 +1259,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() layout->setContentsMargins(0, 0, 0, 0); QCOMPARE(w1->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(layout)); QCOMPARE(layout->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(window)); - + view.show(); QApplication::processEvents(); QCOMPARE(window->size().toSize(), QSize(100, 50)); @@ -1283,7 +1288,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() QApplication::processEvents(); w1->setMinimumSize(120, 60); QApplication::processEvents(); - QCOMPARE(window->size().toSize(), QSize(124, 64)); + QCOMPARE(window->size().toSize(), QSize(124, 64)); } { @@ -1341,7 +1346,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() w2->setPreferredSize(20, 20); w2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); layout->addItem(w2); - + scene.addItem(window); window->setLayout(layout); view.show(); @@ -1359,7 +1364,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() QCOMPARE(w1->geometry().left(), 29.0); QCOMPARE(w2->geometry().right(), 23.0); QCOMPARE(w2->geometry().left(), 3.0); - + delete window; } diff --git a/tests/auto/qgraphicsobject/qgraphicsobject.pro b/tests/auto/qgraphicsobject/qgraphicsobject.pro new file mode 100644 index 0000000..965b319 --- /dev/null +++ b/tests/auto/qgraphicsobject/qgraphicsobject.pro @@ -0,0 +1,2 @@ +load(qttest_p4) +SOURCES += tst_qgraphicsobject.cpp diff --git a/tests/auto/qgraphicsobject/tst_qgraphicsobject.cpp b/tests/auto/qgraphicsobject/tst_qgraphicsobject.cpp new file mode 100644 index 0000000..e7a436f --- /dev/null +++ b/tests/auto/qgraphicsobject/tst_qgraphicsobject.cpp @@ -0,0 +1,255 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> +#include <qgraphicsitem.h> +#include <qgraphicsscene.h> +#include <qgraphicssceneevent.h> +#include <qgraphicsview.h> +#include <qstyleoption.h> +#include "../../shared/util.h" + +class tst_QGraphicsObject : public QObject { + Q_OBJECT + +public slots: + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + +private slots: + void pos(); + void x(); + void y(); + void z(); + void opacity(); + void enabled(); + void visible(); +}; + + +// This will be called before the first test function is executed. +// It is only called once. +void tst_QGraphicsObject::initTestCase() +{ +} + +// This will be called after the last test function is executed. +// It is only called once. +void tst_QGraphicsObject::cleanupTestCase() +{ +} + +// This will be called before each test function is executed. +void tst_QGraphicsObject::init() +{ +} + +// This will be called after every test function. +void tst_QGraphicsObject::cleanup() +{ +} + + +class MyGraphicsObject : public QGraphicsObject +{ +public: + MyGraphicsObject() : QGraphicsObject() {} + virtual QRectF boundingRect() const { return QRectF(); } + virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {} +}; + +void tst_QGraphicsObject::pos() +{ + MyGraphicsObject object; + QSignalSpy xSpy(&object, SIGNAL(xChanged())); + QSignalSpy ySpy(&object, SIGNAL(yChanged())); + QVERIFY(object.pos() == QPointF(0, 0)); + object.setPos(10, 10); + QCOMPARE(xSpy.count(), 1); + QCOMPARE(ySpy.count(), 1); + + QVERIFY(object.pos() == QPointF(10,10)); + + object.setPos(10, 10); + QCOMPARE(xSpy.count(), 1); + QCOMPARE(ySpy.count(), 1); + + object.setProperty("pos", QPointF(0, 0)); + QCOMPARE(xSpy.count(), 2); + QCOMPARE(ySpy.count(), 2); + QVERIFY(object.property("pos") == QPointF(0,0)); + + object.setProperty("pos", QPointF(10, 0)); + QCOMPARE(xSpy.count(), 3); + QCOMPARE(ySpy.count(), 2); + QVERIFY(object.property("pos") == QPointF(10,0)); + + object.setProperty("pos", QPointF(10, 10)); + QCOMPARE(xSpy.count(), 3); + QCOMPARE(ySpy.count(), 3); + QVERIFY(object.property("pos") == QPointF(10, 10)); +} + +void tst_QGraphicsObject::x() +{ + MyGraphicsObject object; + QSignalSpy xSpy(&object, SIGNAL(xChanged())); + QSignalSpy ySpy(&object, SIGNAL(yChanged())); + QVERIFY(object.pos() == QPointF(0, 0)); + object.setX(10); + QCOMPARE(xSpy.count(), 1); + QCOMPARE(ySpy.count(), 0); + + QVERIFY(object.pos() == QPointF(10, 0)); + QVERIFY(object.x() == 10); + + object.setX(10); + QCOMPARE(xSpy.count(), 1); + QCOMPARE(ySpy.count(), 0); + + object.setProperty("x", 0); + QCOMPARE(xSpy.count(), 2); + QCOMPARE(ySpy.count(), 0); + QVERIFY(object.property("x") == 0); +} + +void tst_QGraphicsObject::y() +{ + MyGraphicsObject object; + QSignalSpy xSpy(&object, SIGNAL(xChanged())); + QSignalSpy ySpy(&object, SIGNAL(yChanged())); + QVERIFY(object.pos() == QPointF(0, 0)); + object.setY(10); + QCOMPARE(xSpy.count(), 0); + QCOMPARE(ySpy.count(), 1); + + QVERIFY(object.pos() == QPointF(0, 10)); + QVERIFY(object.y() == 10); + + object.setY(10); + QCOMPARE(xSpy.count(), 0); + QCOMPARE(ySpy.count(), 1); + + object.setProperty("y", 0); + QCOMPARE(xSpy.count(), 0); + QCOMPARE(ySpy.count(), 2); + QVERIFY(object.property("y") == 0); +} + +void tst_QGraphicsObject::z() +{ + MyGraphicsObject object; + QSignalSpy zSpy(&object, SIGNAL(zChanged())); + QVERIFY(object.zValue() == 0); + object.setZValue(10); + QCOMPARE(zSpy.count(), 1); + + QVERIFY(object.zValue() == 10); + + object.setZValue(10); + QCOMPARE(zSpy.count(), 1); + + object.setProperty("z", 0); + QCOMPARE(zSpy.count(), 2); + QVERIFY(object.property("z") == 0); +} + +void tst_QGraphicsObject::opacity() +{ + MyGraphicsObject object; + QSignalSpy spy(&object, SIGNAL(opacityChanged())); + QVERIFY(object.opacity() == 1.); + object.setOpacity(0); + QCOMPARE(spy.count(), 1); + + QVERIFY(object.opacity() == 0.); + + object.setOpacity(0); + QCOMPARE(spy.count(), 1); + + object.setProperty("opacity", .5); + QCOMPARE(spy.count(), 2); + QVERIFY(object.property("opacity") == .5); +} + +void tst_QGraphicsObject::enabled() +{ + MyGraphicsObject object; + QSignalSpy spy(&object, SIGNAL(enabledChanged())); + QVERIFY(object.isEnabled() == true); + object.setEnabled(false); + QCOMPARE(spy.count(), 1); + + QVERIFY(object.isEnabled() == false); + + object.setEnabled(false); + QCOMPARE(spy.count(), 1); + + object.setProperty("enabled", true); + QCOMPARE(spy.count(), 2); + QVERIFY(object.property("enabled") == true); +} + +void tst_QGraphicsObject::visible() +{ + MyGraphicsObject object; + QSignalSpy spy(&object, SIGNAL(visibleChanged())); + QVERIFY(object.isVisible() == true); + object.setVisible(false); + QCOMPARE(spy.count(), 1); + + QVERIFY(object.isVisible() == false); + + object.setVisible(false); + QCOMPARE(spy.count(), 1); + + object.setProperty("visible", true); + QCOMPARE(spy.count(), 2); + QVERIFY(object.property("visible") == true); +} + + +QTEST_MAIN(tst_QGraphicsObject) +#include "tst_qgraphicsobject.moc" + diff --git a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp index 125c021..0b1d5cf 100644 --- a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp +++ b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp @@ -175,6 +175,9 @@ private slots: void bypassGraphicsProxyWidget_data(); void bypassGraphicsProxyWidget(); void dragDrop(); + void windowFlags_data(); + void windowFlags(); + void comboboxWindowFlags(); }; // Subclass that exposes the protected functions. @@ -1484,7 +1487,7 @@ void tst_QGraphicsProxyWidget::scrollUpdate() QVector<QRect>() << QRect(0, 0, 200, 12) << QRect(0, 12, 102, 10)); QCOMPARE(widget->npaints, 2); QCOMPARE(widget->paintEventRegion.rects(), - QVector<QRect>() << QRect(0, 0, 200, 13) << QRect(0, 13, 103, 10)); + QVector<QRect>() << QRect(0, 0, 200, 12) << QRect(0, 12, 102, 10)); } void tst_QGraphicsProxyWidget::setWidget_simple() @@ -3153,6 +3156,67 @@ void tst_QGraphicsProxyWidget::dragDrop() QCOMPARE(edit->text(), QString("hei")); } +void tst_QGraphicsProxyWidget::windowFlags_data() +{ + QTest::addColumn<int>("proxyFlags"); + QTest::addColumn<int>("widgetFlags"); + QTest::addColumn<int>("resultingProxyFlags"); + QTest::addColumn<int>("resultingWidgetFlags"); + + QTest::newRow("proxy(0) widget(0)") << 0 << 0 << 0 << int(Qt::Window); + QTest::newRow("proxy(window)") << int(Qt::Window) << 0 << int(Qt::Window) << int(Qt::Window); + QTest::newRow("proxy(window) widget(window)") << int(Qt::Window) << int(Qt::Window) << int(Qt::Window) << int(Qt::Window); + QTest::newRow("proxy(0) widget(window)") << int(0) << int(Qt::Window) << int(0) << int(Qt::Window); +} + +void tst_QGraphicsProxyWidget::windowFlags() +{ + QFETCH(int, proxyFlags); + QFETCH(int, widgetFlags); + QFETCH(int, resultingProxyFlags); + QFETCH(int, resultingWidgetFlags); + Qt::WindowFlags proxyWFlags = Qt::WindowFlags(proxyFlags); + Qt::WindowFlags widgetWFlags = Qt::WindowFlags(widgetFlags); + Qt::WindowFlags resultingProxyWFlags = Qt::WindowFlags(resultingProxyFlags); + Qt::WindowFlags resultingWidgetWFlags = Qt::WindowFlags(resultingWidgetFlags); + + QGraphicsProxyWidget proxy(0, proxyWFlags); + QVERIFY((proxy.windowFlags() & proxyWFlags) == proxyWFlags); + + QWidget *widget = new QWidget(0, widgetWFlags); + QVERIFY((widget->windowFlags() & widgetWFlags) == widgetWFlags); + + proxy.setWidget(widget); + + if (resultingProxyFlags == 0) + QVERIFY(!proxy.windowFlags()); + else + QVERIFY((proxy.windowFlags() & resultingProxyWFlags) == resultingProxyWFlags); + QVERIFY((widget->windowFlags() & resultingWidgetWFlags) == resultingWidgetWFlags); +} + +void tst_QGraphicsProxyWidget::comboboxWindowFlags() +{ + QComboBox *comboBox = new QComboBox; + comboBox->addItem("Item 1"); + comboBox->addItem("Item 2"); + comboBox->addItem("Item 3"); + QWidget *embedWidget = comboBox; + + QGraphicsScene scene; + QGraphicsProxyWidget *proxy = scene.addWidget(embedWidget); + proxy->setWindowFlags(Qt::Window); + QVERIFY(embedWidget->isWindow()); + QVERIFY(proxy->isWindow()); + + comboBox->showPopup(); + + QCOMPARE(proxy->childItems().size(), 1); + QGraphicsItem *popupProxy = proxy->childItems().first(); + QVERIFY(popupProxy->isWindow()); + QVERIFY((static_cast<QGraphicsWidget *>(popupProxy)->windowFlags() & Qt::Popup) == Qt::Popup); +} + QTEST_MAIN(tst_QGraphicsProxyWidget) #include "tst_qgraphicsproxywidget.moc" diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp index d59f8f6..617177f 100644 --- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp @@ -282,7 +282,9 @@ void tst_QGraphicsScene::sceneRect() QCOMPARE(scene.sceneRect(), QRectF()); QGraphicsItem *item = scene.addRect(QRectF(0, 0, 10, 10)); + qApp->processEvents(); item->setPos(-5, -5); + qApp->processEvents(); QCOMPARE(scene.itemAt(0, 0), item); QCOMPARE(scene.itemAt(10, 10), (QGraphicsItem *)0); @@ -1325,8 +1327,9 @@ void tst_QGraphicsScene::removeItem() scene.removeItem(hoverItem); hoverItem->setAcceptsHoverEvents(false); scene.addItem(hoverItem); - qApp->processEvents(); // update - qApp->processEvents(); // draw + qApp->processEvents(); // <- delayed update is called + qApp->processEvents(); // <- scene schedules pending update + qApp->processEvents(); // <- pending update is sent to view QVERIFY(!hoverItem->isHovered); } @@ -2722,6 +2725,7 @@ void tst_QGraphicsScene::update() QGraphicsRectItem *rect = new QGraphicsRectItem(0, 0, 100, 100); scene.addItem(rect); + qApp->processEvents(); rect->setPos(-100, -100); // This function forces indexing diff --git a/tests/auto/qgraphicsview/tst_qgraphicsview.cpp b/tests/auto/qgraphicsview/tst_qgraphicsview.cpp index 2e6ba39..8cedc18 100644 --- a/tests/auto/qgraphicsview/tst_qgraphicsview.cpp +++ b/tests/auto/qgraphicsview/tst_qgraphicsview.cpp @@ -72,6 +72,7 @@ Q_DECLARE_METATYPE(QList<QRectF>) Q_DECLARE_METATYPE(QMatrix) Q_DECLARE_METATYPE(QPainterPath) Q_DECLARE_METATYPE(QPointF) +Q_DECLARE_METATYPE(QPolygonF) Q_DECLARE_METATYPE(QRectF) Q_DECLARE_METATYPE(Qt::ScrollBarPolicy) @@ -159,6 +160,7 @@ private slots: void itemAt2(); void mapToScene(); void mapToScenePoint(); + void mapToSceneRect_data(); void mapToSceneRect(); void mapToScenePoly(); void mapToScenePath(); @@ -186,7 +188,13 @@ private slots: void embeddedViews(); void scrollAfterResize_data(); void scrollAfterResize(); + void moveItemWhileScrolling_data(); + void moveItemWhileScrolling(); void centerOnDirtyItem(); + void mouseTracking(); + void mouseTracking2(); + void render(); + void exposeRegion(); // task specific tests below me void task172231_untransformableItems(); @@ -1612,23 +1620,51 @@ void tst_QGraphicsView::mapToScenePoint() view.mapToScene(center) + QPointF(0, -10)); } +void tst_QGraphicsView::mapToSceneRect_data() +{ + QTest::addColumn<QRect>("viewRect"); + QTest::addColumn<QPolygonF>("scenePoly"); + QTest::addColumn<qreal>("rotation"); + + QTest::newRow("nil") << QRect() << QPolygonF() << qreal(0); + QTest::newRow("0, 0, 1, 1") << QRect(0, 0, 1, 1) << QPolygonF(QRectF(0, 0, 1, 1)) << qreal(0); + QTest::newRow("0, 0, 10, 10") << QRect(0, 0, 10, 10) << QPolygonF(QRectF(0, 0, 10, 10)) << qreal(0); + QTest::newRow("nil") << QRect() << QPolygonF() << qreal(90); + QPolygonF p; + p << QPointF(0, 0) << QPointF(0, -1) << QPointF(1, -1) << QPointF(1, 0) << QPointF(0, 0); + QTest::newRow("0, 0, 1, 1") << QRect(0, 0, 1, 1) + << p + << qreal(90); + p.clear(); + p << QPointF(0, 0) << QPointF(0, -10) << QPointF(10, -10) << QPointF(10, 0) << QPointF(0, 0); + QTest::newRow("0, 0, 10, 10") << QRect(0, 0, 10, 10) + << p + << qreal(90); +} + void tst_QGraphicsView::mapToSceneRect() { - QGraphicsScene scene; + QFETCH(QRect, viewRect); + QFETCH(QPolygonF, scenePoly); + QFETCH(qreal, rotation); + + QGraphicsScene scene(-1000, -1000, 2000, 2000); + scene.addRect(25, -25, 50, 50); QGraphicsView view(&scene); - view.rotate(90); - view.setFixedSize(117, 117); + view.setFrameStyle(0); + view.setAlignment(Qt::AlignTop | Qt::AlignLeft); + view.setFixedSize(200, 200); + view.setTransformationAnchor(QGraphicsView::NoAnchor); + view.setResizeAnchor(QGraphicsView::NoAnchor); view.show(); - QPoint center = view.viewport()->rect().center(); - QRect rect(center + QPoint(10, 0), QSize(10, 10)); - QPolygonF poly; - poly << view.mapToScene(rect.topLeft()); - poly << view.mapToScene(rect.topRight()); - poly << view.mapToScene(rect.bottomRight()); - poly << view.mapToScene(rect.bottomLeft()); + view.rotate(rotation); - QCOMPARE(view.mapToScene(rect), poly); + QPolygonF poly = view.mapToScene(viewRect); + if (!poly.isEmpty()) + poly << poly[0]; + + QCOMPARE(poly, scenePoly); } void tst_QGraphicsView::mapToScenePoly() @@ -2343,7 +2379,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *viewport) { - lastLod = option->levelOfDetail; + lastLod = option->levelOfDetailFromTransform(painter->worldTransform()); QGraphicsRectItem::paint(painter, option, viewport); } @@ -2497,6 +2533,8 @@ void tst_QGraphicsView::acceptMousePressEvent() scene.addRect(0, 0, 2000, 2000)->setFlag(QGraphicsItem::ItemIsMovable); + qApp->processEvents(); // ensure scene rect is updated + QApplication::sendEvent(view.viewport(), &event); QVERIFY(view.accepted); } @@ -2516,7 +2554,8 @@ void tst_QGraphicsView::replayMouseMove() // One mouse event should be translated into one scene event. for (int i = 0; i < 3; ++i) { - sendMouseMove(view.viewport(), view.viewport()->rect().center()); + sendMouseMove(view.viewport(), view.viewport()->rect().center(), + Qt::LeftButton, Qt::MouseButtons(Qt::LeftButton)); QCOMPARE(viewSpy.count(), i + 1); QCOMPARE(sceneSpy.count(), i + 1); } @@ -2706,6 +2745,7 @@ void tst_QGraphicsView::task186827_deleteReplayedItem() MouseMoveCounter view; view.setScene(&scene); view.show(); + view.viewport()->setMouseTracking(true); #ifdef Q_WS_X11 qt_x11_wait_for_window_manager(&view); #endif @@ -3012,6 +3052,67 @@ void tst_QGraphicsView::scrollAfterResize() QCOMPARE(view.viewportTransform(), x3); } +void tst_QGraphicsView::moveItemWhileScrolling_data() +{ + QTest::addColumn<bool>("adjustForAntialiasing"); + + QTest::newRow("no adjust") << false; + QTest::newRow("adjust") << true; +} + +void tst_QGraphicsView::moveItemWhileScrolling() +{ + QFETCH(bool, adjustForAntialiasing); + + class MoveItemScrollView : public QGraphicsView + { + public: + MoveItemScrollView() + { + setScene(new QGraphicsScene(0, 0, 1000, 1000)); + rect = scene()->addRect(0, 0, 10, 10); + rect->setPos(50, 50); + } + QRegion lastPaintedRegion; + QGraphicsItem *rect; + protected: + void paintEvent(QPaintEvent *event) + { + lastPaintedRegion = event->region(); + QGraphicsView::paintEvent(event); + } + }; + + MoveItemScrollView view; + view.setFrameStyle(0); + view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + view.setResizeAnchor(QGraphicsView::NoAnchor); + view.setTransformationAnchor(QGraphicsView::NoAnchor); + if (!adjustForAntialiasing) + view.setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing); + view.show(); + view.resize(200, 200); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + view.lastPaintedRegion = QRegion(); + view.horizontalScrollBar()->setValue(view.horizontalScrollBar()->value() + 10); + view.rect->moveBy(0, 10); + QTest::qWait(100); + + QRegion expectedRegion; + expectedRegion += QRect(0, 0, 200, 200); + expectedRegion -= QRect(0, 0, 190, 200); + int a = adjustForAntialiasing ? 2 : 1; + expectedRegion += QRect(40, 50, 10, 10).adjusted(-a, -a, a, a); + expectedRegion += QRect(40, 60, 10, 10).adjusted(-a, -a, a, a); + + QCOMPARE(view.lastPaintedRegion, expectedRegion); +} + void tst_QGraphicsView::centerOnDirtyItem() { QGraphicsView view; @@ -3049,6 +3150,217 @@ void tst_QGraphicsView::centerOnDirtyItem() QCOMPARE(before, after); } +void tst_QGraphicsView::mouseTracking() +{ + // Mouse tracking should only be automatically enabled if items either accept hover events + // or have a cursor set. We never disable mouse tracking if it is already enabled. + + { // Make sure mouse tracking is disabled by default. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsView view(&scene); + QVERIFY(!view.viewport()->hasMouseTracking()); + } + + { // Make sure we don't disable mouse tracking in setupViewport/setScene. + QGraphicsView view; + QWidget *viewport = new QWidget; + viewport->setMouseTracking(true); + view.setViewport(viewport); + QVERIFY(viewport->hasMouseTracking()); + + QGraphicsScene scene(-10000, -10000, 20000, 20000); + view.setScene(&scene); + QVERIFY(viewport->hasMouseTracking()); + } + + // Make sure we enable mouse tracking when having items that accept hover events. + { + // Adding an item to the scene after the scene is set on the view. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsView view(&scene); + + QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); + item->setAcceptHoverEvents(true); + scene.addItem(item); + QVERIFY(view.viewport()->hasMouseTracking()); + } + { + // Adding an item to the scene before the scene is set on the view. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); + item->setAcceptHoverEvents(true); + scene.addItem(item); + + QGraphicsView view(&scene); + QVERIFY(view.viewport()->hasMouseTracking()); + } + { + // QGraphicsWidget implicitly accepts hover if it has window decoration. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsView view(&scene); + + QGraphicsWidget *widget = new QGraphicsWidget; + scene.addItem(widget); + QVERIFY(!view.viewport()->hasMouseTracking()); + // Enable window decoraton. + widget->setWindowFlags(Qt::Window | Qt::WindowTitleHint); + QVERIFY(view.viewport()->hasMouseTracking()); + } + + // Make sure we enable mouse tracking when having items with a cursor set. + { + // Adding an item to the scene after the scene is set on the view. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsView view(&scene); + + QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); + item->setCursor(Qt::CrossCursor); + scene.addItem(item); + QVERIFY(view.viewport()->hasMouseTracking()); + } + { + // Adding an item to the scene before the scene is set on the view. + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); + item->setCursor(Qt::CrossCursor); + scene.addItem(item); + + QGraphicsView view(&scene); + QVERIFY(view.viewport()->hasMouseTracking()); + } + + // Make sure we propagate mouse tracking to all views. + { + QGraphicsScene scene(-10000, -10000, 20000, 20000); + QGraphicsView view1(&scene); + QGraphicsView view2(&scene); + QGraphicsView view3(&scene); + + QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10); + item->setCursor(Qt::CrossCursor); + scene.addItem(item); + + QVERIFY(view1.viewport()->hasMouseTracking()); + QVERIFY(view2.viewport()->hasMouseTracking()); + QVERIFY(view3.viewport()->hasMouseTracking()); + } +} + +void tst_QGraphicsView::mouseTracking2() +{ + // Make sure mouse move events propagates to the scene when + // mouse tracking is explicitly enabled on the view, + // even when all items ignore hover events / use default cursor. + + QGraphicsScene scene; + scene.addRect(0, 0, 100, 100); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(200); + + QVERIFY(!view.viewport()->hasMouseTracking()); + view.viewport()->setMouseTracking(true); // Explicitly enable mouse tracking. + QVERIFY(view.viewport()->hasMouseTracking()); + + EventSpy spy(&scene, QEvent::GraphicsSceneMouseMove); + QCOMPARE(spy.count(), 0); + sendMouseMove(view.viewport(), view.viewport()->rect().center()); + QCOMPARE(spy.count(), 1); +} + +class RenderTester : public QGraphicsRectItem +{ +public: + RenderTester(const QRectF &rect) + : QGraphicsRectItem(rect), paints(0) + { } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) + { + QGraphicsRectItem::paint(painter, option, widget); + ++paints; + } + + int paints; +}; + +void tst_QGraphicsView::render() +{ + // ### This test can be much more thorough - see QGraphicsScene::render. + QGraphicsScene scene; + RenderTester *r1 = new RenderTester(QRectF(0, 0, 50, 50)); + RenderTester *r2 = new RenderTester(QRectF(50, 50, 50, 50)); + RenderTester *r3 = new RenderTester(QRectF(0, 50, 50, 50)); + RenderTester *r4 = new RenderTester(QRectF(50, 0, 50, 50)); + scene.addItem(r1); + scene.addItem(r2); + scene.addItem(r3); + scene.addItem(r4); + + QGraphicsView view(&scene); + view.setFrameStyle(0); + view.resize(200, 200); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(200); + + QCOMPARE(r1->paints, 1); + QCOMPARE(r2->paints, 1); + QCOMPARE(r3->paints, 1); + QCOMPARE(r4->paints, 1); + + QPixmap pix(200, 200); + pix.fill(Qt::transparent); + QPainter painter(&pix); + view.render(&painter); + painter.end(); + + QCOMPARE(r1->paints, 2); + QCOMPARE(r2->paints, 2); + QCOMPARE(r3->paints, 2); + QCOMPARE(r4->paints, 2); +} + +void tst_QGraphicsView::exposeRegion() +{ + RenderTester *item = new RenderTester(QRectF(0, 0, 20, 20)); + QGraphicsScene scene; + scene.addItem(item); + + CustomView view; + view.setScene(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(125); + + item->paints = 0; + view.lastUpdateRegions.clear(); + + // Update a small area in the viewport's topLeft() and bottomRight(). + // (the boundingRect() of this area covers the entire viewport). + QWidget *viewport = view.viewport(); + QRegion expectedExposeRegion = QRect(0, 0, 5, 5); + expectedExposeRegion += QRect(viewport->rect().bottomRight() - QPoint(5, 5), QSize(5, 5)); + viewport->update(expectedExposeRegion); + qApp->processEvents(); + + // Make sure it triggers correct repaint on the view. + QCOMPARE(view.lastUpdateRegions.size(), 1); + QCOMPARE(view.lastUpdateRegions.at(0), expectedExposeRegion); + + // Make sure the item didn't get any repaints. + QCOMPARE(item->paints, 0); +} + void tst_QGraphicsView::task253415_reconnectUpdateSceneOnSceneChanged() { QGraphicsView view; diff --git a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp index 41dc779..78d13d3 100644 --- a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp @@ -148,7 +148,14 @@ private slots: void setSizes(); void closePopupOnOutsideClick(); void defaultSize(); + void explicitMouseGrabber(); + void implicitMouseGrabber(); + void doubleClickAfterExplicitMouseGrab(); + void popupMouseGrabber(); + void windowFlags_data(); + void windowFlags(); void shortcutsDeletion(); + void painterStateProtectionOnWindowFrame(); // Task fixes void task236127_bspTreeIndexFails(); @@ -1736,6 +1743,10 @@ void tst_QGraphicsWidget::task236127_bspTreeIndexFails() widget->resize(10, 10); widget2->resize(10, 10); widget2->setZValue(1); + QCOMPARE(widget2->zValue(), qreal(1)); + QCOMPARE(widget->zValue(), qreal(0)); + widget->setData(0, "widget"); + widget2->setData(0, "widget2"); QGraphicsScene scene; scene.addItem(widget); @@ -1746,7 +1757,7 @@ void tst_QGraphicsWidget::task236127_bspTreeIndexFails() #ifdef Q_WS_X11 qt_x11_wait_for_window_manager(&view); #endif - QTest::qWait(50); + QTest::qWait(100); QVERIFY(!scene.itemAt(25, 25)); widget->setGeometry(0, 112, 360, 528); @@ -1784,6 +1795,480 @@ void tst_QGraphicsWidget::defaultSize() } +void tst_QGraphicsWidget::explicitMouseGrabber() +{ + QGraphicsWidget *widget = new QGraphicsWidget; + EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse); + EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse); + + // Grab without scene + QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::grabMouse: cannot grab mouse without scene"); + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 0); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::ungrabMouse: cannot ungrab mouse without scene"); + widget->ungrabMouse(); + QCOMPARE(widgetUngrabEventSpy.count(), 0); + + // Add to scene + QGraphicsScene scene; + scene.addItem(widget); + + // Ungrab while not grabber + QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::ungrabMouse: not a mouse grabber"); + widget->ungrabMouse(); + + // Simple grab with scene + QVERIFY(!scene.mouseGrabberItem()); + widget->grabMouse(); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 1); + widget->ungrabMouse(); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + QCOMPARE(widgetUngrabEventSpy.count(), 1); + + // Grab while grabbing + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 2); + QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::grabMouse: already a mouse grabber"); + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 2); + QCOMPARE(widgetUngrabEventSpy.count(), 1); + widget->ungrabMouse(); + QCOMPARE(widgetUngrabEventSpy.count(), 2); + + // Add two more widgets to the scene + QGraphicsWidget *widget2 = new QGraphicsWidget; + scene.addItem(widget2); + EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse); + EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse); + QGraphicsWidget *widget3 = new QGraphicsWidget; + scene.addItem(widget3); + EventSpy widget3GrabEventSpy(widget3, QEvent::GrabMouse); + EventSpy widget3UngrabEventSpy(widget3, QEvent::UngrabMouse); + + widget->setData(0, "widget"); + widget2->setData(0, "widget2"); + widget3->setData(0, "widget3"); + + // Simple nested grabbing + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 3); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + widget2->grabMouse(); + QCOMPARE(widgetUngrabEventSpy.count(), 3); + QCOMPARE(widget2GrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + widget3->grabMouse(); + QCOMPARE(widget2UngrabEventSpy.count(), 1); + QCOMPARE(widget3GrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3); + widget3->ungrabMouse(); + QCOMPARE(widget3UngrabEventSpy.count(), 1); + QCOMPARE(widget2GrabEventSpy.count(), 2); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + widget2->ungrabMouse(); + QCOMPARE(widget2UngrabEventSpy.count(), 2); + QCOMPARE(widgetGrabEventSpy.count(), 4); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + widget->ungrabMouse(); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + + // Out of order ungrab + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 5); + widget2->grabMouse(); + QCOMPARE(widget2GrabEventSpy.count(), 3); + widget3->grabMouse(); + QCOMPARE(widget3GrabEventSpy.count(), 2); + widget2->ungrabMouse(); + QCOMPARE(widget3UngrabEventSpy.count(), 2); + QCOMPARE(widget2UngrabEventSpy.count(), 4); + QCOMPARE(widgetGrabEventSpy.count(), 6); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); +} + +void tst_QGraphicsWidget::implicitMouseGrabber() +{ + QGraphicsScene scene; + QGraphicsWidget *widget = new QGraphicsWidget; + widget->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse + widget->resize(200, 200); + EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse); + EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse); + scene.addItem(widget); + + QVERIFY(!scene.mouseGrabberItem()); + + // Click on an item, see if gain and lose implicit mouse grab. + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 1); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + QCOMPARE(widgetGrabEventSpy.count(), 1); + QCOMPARE(widgetUngrabEventSpy.count(), 1); + + // Click on an item that already grabs the mouse. Shouldn't have any effect. + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 2); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 2); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 2); + QCOMPARE(widgetUngrabEventSpy.count(), 1); + widget->ungrabMouse(); + QCOMPARE(widgetUngrabEventSpy.count(), 2); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + + // Implicit mouse grabber tries to explicitly grab the mouse + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 3); + widget->grabMouse(); + QCOMPARE(widgetUngrabEventSpy.count(), 2); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 3); + QCOMPARE(widgetUngrabEventSpy.count(), 2); + widget->ungrabMouse(); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + QCOMPARE(widgetGrabEventSpy.count(), 3); + QCOMPARE(widgetUngrabEventSpy.count(), 3); + + // Arrival of a new widget + QGraphicsWidget *widget2 = new QGraphicsWidget; + widget2->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse + widget2->resize(200, 200); + widget2->setPos(205, 0); + EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse); + EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse); + scene.addItem(widget2); + + // Implicit grab while there's an explicit grab is not possible. + widget->grabMouse(); + QCOMPARE(widgetGrabEventSpy.count(), 4); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(250, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + QCOMPARE(widgetGrabEventSpy.count(), 4); + QCOMPARE(widget2GrabEventSpy.count(), 0); + QCOMPARE(widget2UngrabEventSpy.count(), 0); + + scene.removeItem(widget); + QCOMPARE(widgetUngrabEventSpy.count(), 4); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); +} + +class GrabOnPressItem : public QGraphicsRectItem +{ +public: + GrabOnPressItem(const QRectF &rect) + : QGraphicsRectItem(rect), + npress(0), nrelease(0), ndoubleClick(0), + ngrab(0), nungrab(0) + { + } + int npress; + int nrelease; + int ndoubleClick; + int ngrab; + int nungrab; +protected: + bool sceneEvent(QEvent *event) + { + switch (event->type()) { + case QEvent::GrabMouse: + ++ngrab; + break; + case QEvent::UngrabMouse: + ++nungrab; + break; + default: + break; + } + return QGraphicsRectItem::sceneEvent(event); + } + + void mousePressEvent(QGraphicsSceneMouseEvent *) + { + grabMouse(); + ++npress; + } + void mouseReleaseEvent(QGraphicsSceneMouseEvent *) + { + ungrabMouse(); + ++nrelease; + } + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) + { + ++ndoubleClick; + } +}; + +void tst_QGraphicsWidget::doubleClickAfterExplicitMouseGrab() +{ + QGraphicsScene scene; + GrabOnPressItem *item = new GrabOnPressItem(QRectF(0, 0, 100, 100)); + scene.addItem(item); + + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.setButton(Qt::LeftButton); + event.setButtons(Qt::LeftButton); + event.ignore(); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)item); + QCOMPARE(item->npress, 1); + QCOMPARE(item->ngrab, 1); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease); + event.setButton(Qt::LeftButton); + event.setButtons(0); + event.ignore(); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + QCOMPARE(item->nrelease, 1); + QCOMPARE(item->nungrab, 1); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseDoubleClick); + event.setButton(Qt::LeftButton); + event.setButtons(Qt::LeftButton); + event.ignore(); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)item); + QCOMPARE(item->ndoubleClick, 1); + QCOMPARE(item->ngrab, 2); + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease); + event.setButton(Qt::LeftButton); + event.setButtons(0); + event.ignore(); + event.setScenePos(QPointF(50, 50)); + qApp->sendEvent(&scene, &event); + } + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + QCOMPARE(item->nrelease, 2); + QCOMPARE(item->nungrab, 2); +} + +void tst_QGraphicsWidget::popupMouseGrabber() +{ + QGraphicsScene scene; + QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Popup); + widget->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse + widget->resize(200, 200); + EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse); + EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse); + + // Simply adding a visible popup to the scene immediately grabs the mouse. + scene.addItem(widget); + QCOMPARE(widgetGrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + + // Hiding it loses the grab again. + widget->hide(); + QCOMPARE(widgetUngrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0); + + // Showing it grabs the mosue again + widget->show(); + QCOMPARE(widgetGrabEventSpy.count(), 2); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget); + + // Add two popups + QGraphicsWidget *widget2 = new QGraphicsWidget(0, Qt::Popup); + widget2->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse + widget2->resize(200, 200); + EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse); + EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse); + QGraphicsWidget *widget3 = new QGraphicsWidget(0, Qt::Popup); + widget3->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse + widget3->resize(200, 200); + EventSpy widget3GrabEventSpy(widget3, QEvent::GrabMouse); + EventSpy widget3UngrabEventSpy(widget3, QEvent::UngrabMouse); + + // Adding to the scene grabs + scene.addItem(widget2); + QCOMPARE(widgetUngrabEventSpy.count(), 2); + QCOMPARE(widget2GrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + + // Adding to the scene grabs again + scene.addItem(widget3); + QCOMPARE(widget2UngrabEventSpy.count(), 1); + QCOMPARE(widget3GrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3); + + // Hiding the topmost widget causes widget 2 to regain grab. + widget3->hide(); + QCOMPARE(widget2GrabEventSpy.count(), 2); + QCOMPARE(widget3UngrabEventSpy.count(), 1); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + widget3->show(); + QCOMPARE(widget2UngrabEventSpy.count(), 2); + QCOMPARE(widget3GrabEventSpy.count(), 2); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3); + + // Clicking outside the popup still causes it to close (despite that it's + // an explicit mouse grabber). + { + QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress); + event.ignore(); + event.setButton(Qt::LeftButton); + event.setScenePos(QPointF(500, 500)); // outside + qApp->sendEvent(&scene, &event); + } + QVERIFY(!widget3->isVisible()); + QCOMPARE(widget3UngrabEventSpy.count(), 2); + QCOMPARE(widget2GrabEventSpy.count(), 3); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + QVERIFY(widget2->isVisible()); + QVERIFY(widget->isVisible()); + widget3->show(); + QCOMPARE(widget3GrabEventSpy.count(), 3); + QCOMPARE(widget2UngrabEventSpy.count(), 3); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3); + + // This is something of a curiosity. What happens if you call + // ungrabMouse() on a popup? The answer is - it loses the grab. If you + // hide and show the popup again, it will regain the grab. + widget3->ungrabMouse(); + QCOMPARE(widget3UngrabEventSpy.count(), 3); + QCOMPARE(widget2GrabEventSpy.count(), 4); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2); + widget3->hide(); + widget3->show(); + QCOMPARE(widget3GrabEventSpy.count(), 4); + QCOMPARE(widget2UngrabEventSpy.count(), 4); + QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3); +} + +void tst_QGraphicsWidget::windowFlags_data() +{ + QTest::addColumn<int>("inputFlags"); + QTest::addColumn<int>("outputFlags"); + + QTest::newRow("nil") << 0 << 0; + + // Window types + QTest::newRow("Qt::Window") << int(Qt::Window) + << int(Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint + | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); + QTest::newRow("Qt::SubWindow") << int(Qt::SubWindow) + << int(Qt::SubWindow | Qt::WindowTitleHint | Qt::WindowSystemMenuHint + | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint); + QTest::newRow("Qt::Dialog") << int(Qt::Dialog) + << int(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowSystemMenuHint + | Qt::WindowContextHelpButtonHint); + QTest::newRow("Qt::Sheet") << int(Qt::Sheet) + << int(Qt::Sheet | Qt::WindowTitleHint | Qt::WindowSystemMenuHint + | Qt::WindowContextHelpButtonHint); + QTest::newRow("Qt::Tool") << int(Qt::Tool) + << int(Qt::Tool | Qt::WindowTitleHint | Qt::WindowSystemMenuHint); + + // Custom window flags + QTest::newRow("Qt::FramelessWindowHint") << int(Qt::FramelessWindowHint) + << int(Qt::FramelessWindowHint); + QTest::newRow("Qt::CustomizeWindowHint") << int(Qt::CustomizeWindowHint) + << int(Qt::CustomizeWindowHint); +} + +void tst_QGraphicsWidget::windowFlags() +{ + QFETCH(int, inputFlags); + QFETCH(int, outputFlags); + + // Construct with flags set already + QGraphicsWidget widget(0, Qt::WindowFlags(inputFlags)); + QCOMPARE(widget.windowFlags(), Qt::WindowFlags(outputFlags)); + + // Set flags after construction + QGraphicsWidget widget2; + widget2.setWindowFlags(Qt::WindowFlags(inputFlags)); + QCOMPARE(widget2.windowFlags(), Qt::WindowFlags(outputFlags)); + + // Reset flags + widget2.setWindowFlags(0); + QVERIFY(!widget2.windowFlags()); + + // Set flags back again + widget2.setWindowFlags(Qt::WindowFlags(inputFlags)); + QCOMPARE(widget2.windowFlags(), Qt::WindowFlags(outputFlags)); + + // Construct with custom flags set already + QGraphicsWidget widget3(0, Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + QCOMPARE(widget3.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + + // Set custom flags after construction + QGraphicsWidget widget4; + widget4.setWindowFlags(Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + QCOMPARE(widget4.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + + // Reset flags + widget4.setWindowFlags(0); + QVERIFY(!widget4.windowFlags()); + + // Set custom flags back again + widget4.setWindowFlags(Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + QCOMPARE(widget4.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint)); + + QGraphicsWidget *widget5 = new QGraphicsWidget; + widget5->setWindowFlags(Qt::WindowFlags(inputFlags)); + QCOMPARE(widget5->windowFlags(), Qt::WindowFlags(outputFlags)); + QGraphicsWidget window(0, Qt::Window); + widget5->setParentItem(&window); + QCOMPARE(widget5->windowFlags(), Qt::WindowFlags(outputFlags)); +} + void tst_QGraphicsWidget::shortcutsDeletion() { QGraphicsWidget *widget = new QGraphicsWidget; @@ -1798,6 +2283,41 @@ void tst_QGraphicsWidget::shortcutsDeletion() delete widget; } +class MessUpPainterWidget : public QGraphicsWidget +{ +public: + MessUpPainterWidget(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0) + : QGraphicsWidget(parent, wFlags) + {} + + void paintWindowFrame(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + QCOMPARE(painter->opacity(), 1.0); + painter->setOpacity(0.0); + QGraphicsWidget::paintWindowFrame(painter, option, widget); + } + void paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + QCOMPARE(painter->opacity(), 1.0); + painter->drawRect(0, 0, 100, 100); + QGraphicsWidget::paint(painter, option, widget); + } + +}; + +void tst_QGraphicsWidget::painterStateProtectionOnWindowFrame() +{ + MessUpPainterWidget *widget = new MessUpPainterWidget(0, Qt::Window); + QGraphicsScene scene(0, 0, 300, 300); + QGraphicsView view(&scene); + scene.addItem(widget); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(500); +} + class ProxyStyle : public QCommonStyle { public: diff --git a/tests/auto/qhttpnetworkconnection/qhttpnetworkconnection.pro b/tests/auto/qhttpnetworkconnection/qhttpnetworkconnection.pro index bc750c5..3283873 100644 --- a/tests/auto/qhttpnetworkconnection/qhttpnetworkconnection.pro +++ b/tests/auto/qhttpnetworkconnection/qhttpnetworkconnection.pro @@ -1,4 +1,5 @@ load(qttest_p4) SOURCES += tst_qhttpnetworkconnection.cpp +INCLUDEPATH += $$(QTDIR)/src/3rdparty/zlib QT = core network diff --git a/tests/auto/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp b/tests/auto/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp index 8a25a51..236d1a1 100644 --- a/tests/auto/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp +++ b/tests/auto/qhttpnetworkconnection/tst_qhttpnetworkconnection.cpp @@ -42,6 +42,7 @@ #include <QtTest/QtTest> #include "private/qhttpnetworkconnection_p.h" +#include "private/qnoncontiguousbytedevice_p.h" #include <QAuthenticator> #include "../network-settings.h" @@ -311,8 +312,8 @@ void tst_QHttpNetworkConnection::put() QHttpNetworkRequest request(protocol + host + path, QHttpNetworkRequest::Put); QByteArray array = data.toLatin1(); - QBuffer buffer(&array); - request.setData(&buffer); + QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(&array); + request.setUploadByteDevice(bd); finishedCalled = false; finishedWithErrorCalled = false; @@ -401,8 +402,8 @@ void tst_QHttpNetworkConnection::post() QHttpNetworkRequest request(protocol + host + path, QHttpNetworkRequest::Post); QByteArray array = data.toLatin1(); - QBuffer buffer(&array); - request.setData(&buffer); + QNonContiguousByteDevice *bd = QNonContiguousByteDeviceFactory::create(&array); + request.setUploadByteDevice(bd); QHttpNetworkReply *reply = connection.sendRequest(request); diff --git a/tests/auto/qhttpnetworkreply/qhttpnetworkreply.pro b/tests/auto/qhttpnetworkreply/qhttpnetworkreply.pro index 9593c55..2e41fcd 100644 --- a/tests/auto/qhttpnetworkreply/qhttpnetworkreply.pro +++ b/tests/auto/qhttpnetworkreply/qhttpnetworkreply.pro @@ -1,4 +1,5 @@ load(qttest_p4) SOURCES += tst_qhttpnetworkreply.cpp +INCLUDEPATH += $$QT_SOURCE_TREE/src/3rdparty/zlib QT = core network diff --git a/tests/auto/qimage/images/image.tif b/tests/auto/qimage/images/image.tif Binary files differnew file mode 100644 index 0000000..ee0637c --- /dev/null +++ b/tests/auto/qimage/images/image.tif diff --git a/tests/auto/qimage/tst_qimage.cpp b/tests/auto/qimage/tst_qimage.cpp index dfa5565..af02bbf 100644 --- a/tests/auto/qimage/tst_qimage.cpp +++ b/tests/auto/qimage/tst_qimage.cpp @@ -135,6 +135,8 @@ private slots: void nullSize(); void premultipliedAlphaConsistency(); + + void compareIndexed(); }; tst_QImage::tst_QImage() @@ -281,6 +283,9 @@ void tst_QImage::formatHandlersInput_data() QTest::newRow("PPM") << "PPM" << prefix + "image.ppm"; QTest::newRow("XBM") << "XBM" << prefix + "image.xbm"; QTest::newRow("XPM") << "XPM" << prefix + "image.xpm"; +#if defined QTEST_HAVE_TIFF + QTest::newRow("TIFF") << "TIFF" << prefix + "image.tif"; +#endif } void tst_QImage::formatHandlersInput() @@ -1767,5 +1772,31 @@ void tst_QImage::premultipliedAlphaConsistency() } } +void tst_QImage::compareIndexed() +{ + QImage img(256, 1, QImage::Format_Indexed8); + + QVector<QRgb> colorTable(256); + for (int i = 0; i < 256; ++i) + colorTable[i] = qRgb(i, i, i); + img.setColorTable(colorTable); + + for (int i = 0; i < 256; ++i) { + img.setPixel(i, 0, i); + } + + QImage imgInverted(256, 1, QImage::Format_Indexed8); + QVector<QRgb> invertedColorTable(256); + for (int i = 0; i < 256; ++i) + invertedColorTable[255-i] = qRgb(i, i, i); + imgInverted.setColorTable(invertedColorTable); + + for (int i = 0; i < 256; ++i) { + imgInverted.setPixel(i, 0, (255-i)); + } + + QCOMPARE(img, imgInverted); +} + QTEST_MAIN(tst_QImage) #include "tst_qimage.moc" diff --git a/tests/auto/qimagereader/baseline/35floppy.ico b/tests/auto/qimagereader/baseline/35floppy.ico Binary files differnew file mode 100644 index 0000000..59fd37e --- /dev/null +++ b/tests/auto/qimagereader/baseline/35floppy.ico diff --git a/tests/auto/qimagereader/baseline/kde_favicon.ico b/tests/auto/qimagereader/baseline/kde_favicon.ico Binary files differnew file mode 100644 index 0000000..15bcdbb --- /dev/null +++ b/tests/auto/qimagereader/baseline/kde_favicon.ico diff --git a/tests/auto/qimagereader/baseline/semitransparent.ico b/tests/auto/qimagereader/baseline/semitransparent.ico Binary files differnew file mode 100644 index 0000000..dd23de9 --- /dev/null +++ b/tests/auto/qimagereader/baseline/semitransparent.ico diff --git a/tests/auto/qimagereader/images/image_100dpi.tif b/tests/auto/qimagereader/images/image_100dpi.tif Binary files differnew file mode 100644 index 0000000..fcf3cd8 --- /dev/null +++ b/tests/auto/qimagereader/images/image_100dpi.tif diff --git a/tests/auto/qimagereader/qimagereader.pro b/tests/auto/qimagereader/qimagereader.pro index 4925498..44a0ddc 100644 --- a/tests/auto/qimagereader/qimagereader.pro +++ b/tests/auto/qimagereader/qimagereader.pro @@ -3,6 +3,7 @@ SOURCES += tst_qimagereader.cpp MOC_DIR=tmp QT += network RESOURCES += qimagereader.qrc +DEFINES += SRCDIR=\\\"$$PWD\\\" !contains(QT_CONFIG, no-gif):DEFINES += QTEST_HAVE_GIF !contains(QT_CONFIG, no-jpeg):DEFINES += QTEST_HAVE_JPEG @@ -22,6 +23,7 @@ wince*: { imagePlugins.path = imageformats DEPLOYMENT += images imagePlugins + DEFINES += SRCDIR=\\\".\\\" } symbian*: { diff --git a/tests/auto/qimagereader/qimagereader.qrc b/tests/auto/qimagereader/qimagereader.qrc index 13ce582..c6b963b 100644 --- a/tests/auto/qimagereader/qimagereader.qrc +++ b/tests/auto/qimagereader/qimagereader.qrc @@ -30,6 +30,7 @@ <file>images/image.pgm</file> <file>images/image.png</file> <file>images/image.ppm</file> + <file>images/image_100dpi.tif</file> <file>images/kollada.png</file> <file>images/marble.xpm</file> <file>images/namedcolors.xpm</file> diff --git a/tests/auto/qimagereader/tst_qimagereader.cpp b/tests/auto/qimagereader/tst_qimagereader.cpp index 5b8fa2c..ea30b3c 100644 --- a/tests/auto/qimagereader/tst_qimagereader.cpp +++ b/tests/auto/qimagereader/tst_qimagereader.cpp @@ -153,8 +153,16 @@ private slots: void autoDetectImageFormat(); void fileNameProbing(); + + void pixelCompareWithBaseline_data(); + void pixelCompareWithBaseline(); + + void task255627_setNullScaledSize_data(); + void task255627_setNullScaledSize(); }; +static const QLatin1String prefix(SRCDIR "/images/"); + // Testing get/set functions void tst_QImageReader::getSetCheck() { @@ -229,7 +237,7 @@ void tst_QImageReader::readImage() QFETCH(QByteArray, format); for (int i = 0; i < 2; ++i) { - QImageReader io("images/" + fileName, i ? QByteArray() : format); + QImageReader io(prefix + fileName, i ? QByteArray() : format); if (success) { if (!io.supportsAnimation()) QVERIFY(io.imageCount() > 0); @@ -245,16 +253,16 @@ void tst_QImageReader::readImage() QVERIFY2(!image.isNull(), io.errorString().toLatin1().constData()); // No format - QImageReader io2("images/" + fileName); + QImageReader io2(prefix + fileName); QVERIFY2(!io2.read().isNull(), io.errorString().toLatin1().constData()); // No extension, no format - QImageReader io3("images/" + fileName.left(fileName.lastIndexOf(QLatin1Char('.')))); + QImageReader io3(prefix + fileName.left(fileName.lastIndexOf(QLatin1Char('.')))); QVERIFY2(!io3.read().isNull(), io.errorString().toLatin1().constData()); // Read into \a image2 QImage image2; - QImageReader image2Reader("images/" + fileName, i ? QByteArray() : format); + QImageReader image2Reader(prefix + fileName, i ? QByteArray() : format); QCOMPARE(image2Reader.format(), format); QVERIFY(image2Reader.read(&image2)); if (image2Reader.canRead()) { @@ -278,8 +286,8 @@ void tst_QImageReader::readImage() void tst_QImageReader::jpegRgbCmyk() { - QImage image1(QLatin1String("images/YCbCr_cmyk.jpg")); - QImage image2(QLatin1String("images/YCbCr_cmyk.png")); + QImage image1(prefix + QLatin1String("YCbCr_cmyk.jpg")); + QImage image2(prefix + QLatin1String("YCbCr_cmyk.png")); QCOMPARE(image1, image2); } @@ -290,24 +298,24 @@ void tst_QImageReader::setScaledSize_data() QTest::addColumn<QSize>("newSize"); QTest::addColumn<QByteArray>("format"); - QTest::newRow("BMP: colorful") << "images/colorful" << QSize(200, 200) << QByteArray("bmp"); - QTest::newRow("BMP: font") << "images/font" << QSize(200, 200) << QByteArray("bmp"); - QTest::newRow("XPM: marble") << "images/marble" << QSize(200, 200) << QByteArray("xpm"); - QTest::newRow("PNG: kollada") << "images/kollada" << QSize(200, 200) << QByteArray("png"); - QTest::newRow("PPM: teapot") << "images/teapot" << QSize(200, 200) << QByteArray("ppm"); - QTest::newRow("PPM: runners") << "images/runners.ppm" << QSize(400, 400) << QByteArray("ppm"); - QTest::newRow("PPM: test") << "images/test.ppm" << QSize(10, 10) << QByteArray("ppm"); - QTest::newRow("XBM: gnus") << "images/gnus" << QSize(200, 200) << QByteArray("xbm"); + QTest::newRow("BMP: colorful") << "colorful" << QSize(200, 200) << QByteArray("bmp"); + QTest::newRow("BMP: font") << "font" << QSize(200, 200) << QByteArray("bmp"); + QTest::newRow("XPM: marble") << "marble" << QSize(200, 200) << QByteArray("xpm"); + QTest::newRow("PNG: kollada") << "kollada" << QSize(200, 200) << QByteArray("png"); + QTest::newRow("PPM: teapot") << "teapot" << QSize(200, 200) << QByteArray("ppm"); + QTest::newRow("PPM: runners") << "runners.ppm" << QSize(400, 400) << QByteArray("ppm"); + QTest::newRow("PPM: test") << "test.ppm" << QSize(10, 10) << QByteArray("ppm"); + QTest::newRow("XBM: gnus") << "gnus" << QSize(200, 200) << QByteArray("xbm"); #ifdef QTEST_HAVE_JPEG - QTest::newRow("JPEG: beavis") << "images/beavis" << QSize(200, 200) << QByteArray("jpeg"); + QTest::newRow("JPEG: beavis") << "beavis" << QSize(200, 200) << QByteArray("jpeg"); #endif // QTEST_HAVE_JPEG #ifdef QTEST_HAVE_GIF - QTest::newRow("GIF: earth") << "images/earth" << QSize(200, 200) << QByteArray("gif"); - QTest::newRow("GIF: trolltech") << "images/trolltech" << QSize(200, 200) << QByteArray("gif"); + QTest::newRow("GIF: earth") << "earth" << QSize(200, 200) << QByteArray("gif"); + QTest::newRow("GIF: trolltech") << "trolltech" << QSize(200, 200) << QByteArray("gif"); #endif // QTEST_HAVE_GIF #ifdef QTEST_HAVE_MNG - QTest::newRow("MNG: ball") << "images/ball" << QSize(200, 200) << QByteArray("mng"); - QTest::newRow("MNG: fire") << "images/fire" << QSize(200, 200) << QByteArray("mng"); + QTest::newRow("MNG: ball") << "ball" << QSize(200, 200) << QByteArray("mng"); + QTest::newRow("MNG: fire") << "fire" << QSize(200, 200) << QByteArray("mng"); #endif // QTEST_HAVE_MNG } @@ -320,7 +328,7 @@ void tst_QImageReader::setScaledSize() if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(format)) QSKIP("Qt does not support reading the \"" + format + "\" format", SkipSingle); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); reader.setScaledSize(newSize); QImage image = reader.read(); QVERIFY(!image.isNull()); @@ -328,30 +336,53 @@ void tst_QImageReader::setScaledSize() QCOMPARE(image.size(), newSize); } +void tst_QImageReader::task255627_setNullScaledSize_data() +{ + setScaledSize_data(); +} + +void tst_QImageReader::task255627_setNullScaledSize() +{ + QFETCH(QString, fileName); + QFETCH(QByteArray, format); + + if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(format)) + QSKIP("Qt does not support reading the \"" + format + "\" format", SkipSingle); + + QImageReader reader(prefix + fileName); + + // set a null size + reader.setScaledSize(QSize(0, 0)); + reader.setQuality(0); + QImage image = reader.read(); + QVERIFY(image.isNull()); + QCOMPARE(image.size(), QSize(0, 0)); +} + void tst_QImageReader::setClipRect_data() { QTest::addColumn<QString>("fileName"); QTest::addColumn<QRect>("newRect"); QTest::addColumn<QByteArray>("format"); - QTest::newRow("BMP: colorful") << "images/colorful" << QRect(0, 0, 50, 50) << QByteArray("bmp"); - QTest::newRow("BMP: font") << "images/font" << QRect(0, 0, 50, 50) << QByteArray("bmp"); - QTest::newRow("BMP: 4bpp uncompressed") << "images/tst7.bmp" << QRect(0, 0, 31, 31) << QByteArray("bmp"); - QTest::newRow("XPM: marble") << "images/marble" << QRect(0, 0, 50, 50) << QByteArray("xpm"); - QTest::newRow("PNG: kollada") << "images/kollada" << QRect(0, 0, 50, 50) << QByteArray("png"); - QTest::newRow("PPM: teapot") << "images/teapot" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("PPM: runners") << "images/runners.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("PPM: test") << "images/test.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("XBM: gnus") << "images/gnus" << QRect(0, 0, 50, 50) << QByteArray("xbm"); + QTest::newRow("BMP: colorful") << "colorful" << QRect(0, 0, 50, 50) << QByteArray("bmp"); + QTest::newRow("BMP: font") << "font" << QRect(0, 0, 50, 50) << QByteArray("bmp"); + QTest::newRow("BMP: 4bpp uncompressed") << "tst7.bmp" << QRect(0, 0, 31, 31) << QByteArray("bmp"); + QTest::newRow("XPM: marble") << "marble" << QRect(0, 0, 50, 50) << QByteArray("xpm"); + QTest::newRow("PNG: kollada") << "kollada" << QRect(0, 0, 50, 50) << QByteArray("png"); + QTest::newRow("PPM: teapot") << "teapot" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("PPM: runners") << "runners.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("PPM: test") << "test.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("XBM: gnus") << "gnus" << QRect(0, 0, 50, 50) << QByteArray("xbm"); #ifdef QTEST_HAVE_JPEG - QTest::newRow("JPEG: beavis") << "images/beavis" << QRect(0, 0, 50, 50) << QByteArray("jpeg"); + QTest::newRow("JPEG: beavis") << "beavis" << QRect(0, 0, 50, 50) << QByteArray("jpeg"); #endif // QTEST_HAVE_JPEG #ifdef QTEST_HAVE_GIF - QTest::newRow("GIF: earth") << "images/earth" << QRect(0, 0, 50, 50) << QByteArray("gif"); - QTest::newRow("GIF: trolltech") << "images/trolltech" << QRect(0, 0, 50, 50) << QByteArray("gif"); + QTest::newRow("GIF: earth") << "earth" << QRect(0, 0, 50, 50) << QByteArray("gif"); + QTest::newRow("GIF: trolltech") << "trolltech" << QRect(0, 0, 50, 50) << QByteArray("gif"); #endif // QTEST_HAVE_GIF #ifdef QTEST_HAVE_MNG - QTest::newRow("MNG: ball") << "images/ball" << QRect(0, 0, 50, 50) << QByteArray("mng"); - QTest::newRow("MNG: fire") << "images/fire" << QRect(0, 0, 50, 50) << QByteArray("mng"); + QTest::newRow("MNG: ball") << "ball" << QRect(0, 0, 50, 50) << QByteArray("mng"); + QTest::newRow("MNG: fire") << "fire" << QRect(0, 0, 50, 50) << QByteArray("mng"); #endif // QTEST_HAVE_MNG } @@ -364,13 +395,13 @@ void tst_QImageReader::setClipRect() if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(format)) QSKIP("Qt does not support reading the \"" + format + "\" format", SkipSingle); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); reader.setClipRect(newRect); QImage image = reader.read(); QVERIFY(!image.isNull()); QCOMPARE(image.rect(), newRect); - QImageReader originalReader(fileName); + QImageReader originalReader(prefix + fileName); QImage originalImage = originalReader.read(); QCOMPARE(originalImage.copy(newRect), image); } @@ -381,24 +412,24 @@ void tst_QImageReader::setScaledClipRect_data() QTest::addColumn<QRect>("newRect"); QTest::addColumn<QByteArray>("format"); - QTest::newRow("BMP: colorful") << "images/colorful" << QRect(0, 0, 50, 50) << QByteArray("bmp"); - QTest::newRow("BMP: font") << "images/font" << QRect(0, 0, 50, 50) << QByteArray("bmp"); - QTest::newRow("XPM: marble") << "images/marble" << QRect(0, 0, 50, 50) << QByteArray("xpm"); - QTest::newRow("PNG: kollada") << "images/kollada" << QRect(0, 0, 50, 50) << QByteArray("png"); - QTest::newRow("PPM: teapot") << "images/teapot" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("PPM: runners") << "images/runners.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("PPM: test") << "images/test.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); - QTest::newRow("XBM: gnus") << "images/gnus" << QRect(0, 0, 50, 50) << QByteArray("xbm"); + QTest::newRow("BMP: colorful") << "colorful" << QRect(0, 0, 50, 50) << QByteArray("bmp"); + QTest::newRow("BMP: font") << "font" << QRect(0, 0, 50, 50) << QByteArray("bmp"); + QTest::newRow("XPM: marble") << "marble" << QRect(0, 0, 50, 50) << QByteArray("xpm"); + QTest::newRow("PNG: kollada") << "kollada" << QRect(0, 0, 50, 50) << QByteArray("png"); + QTest::newRow("PPM: teapot") << "teapot" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("PPM: runners") << "runners.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("PPM: test") << "test.ppm" << QRect(0, 0, 50, 50) << QByteArray("ppm"); + QTest::newRow("XBM: gnus") << "gnus" << QRect(0, 0, 50, 50) << QByteArray("xbm"); #ifdef QTEST_HAVE_JPEG - QTest::newRow("JPEG: beavis") << "images/beavis" << QRect(0, 0, 50, 50) << QByteArray("jpeg"); + QTest::newRow("JPEG: beavis") << "beavis" << QRect(0, 0, 50, 50) << QByteArray("jpeg"); #endif // QTEST_HAVE_JPEG #ifdef QTEST_HAVE_GIF - QTest::newRow("GIF: earth") << "images/earth" << QRect(0, 0, 50, 50) << QByteArray("gif"); - QTest::newRow("GIF: trolltech") << "images/trolltech" << QRect(0, 0, 50, 50) << QByteArray("gif"); + QTest::newRow("GIF: earth") << "earth" << QRect(0, 0, 50, 50) << QByteArray("gif"); + QTest::newRow("GIF: trolltech") << "trolltech" << QRect(0, 0, 50, 50) << QByteArray("gif"); #endif // QTEST_HAVE_GIF #ifdef QTEST_HAVE_MNG - QTest::newRow("MNG: ball") << "images/ball" << QRect(0, 0, 50, 50) << QByteArray("mng"); - QTest::newRow("MNG: fire") << "images/fire" << QRect(0, 0, 50, 50) << QByteArray("mng"); + QTest::newRow("MNG: ball") << "ball" << QRect(0, 0, 50, 50) << QByteArray("mng"); + QTest::newRow("MNG: fire") << "fire" << QRect(0, 0, 50, 50) << QByteArray("mng"); #endif // QTEST_HAVE_MNG } @@ -411,14 +442,14 @@ void tst_QImageReader::setScaledClipRect() if (!format.isEmpty() && !QImageReader::supportedImageFormats().contains(format)) QSKIP("Qt does not support reading the \"" + format + "\" format", SkipSingle); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); reader.setScaledSize(QSize(300, 300)); reader.setScaledClipRect(newRect); QImage image = reader.read(); QVERIFY(!image.isNull()); QCOMPARE(image.rect(), newRect); - QImageReader originalReader(fileName); + QImageReader originalReader(prefix + fileName); originalReader.setScaledSize(QSize(300, 300)); QImage originalImage = originalReader.read(); QCOMPARE(originalImage.copy(newRect), image); @@ -430,29 +461,29 @@ void tst_QImageReader::imageFormat_data() QTest::addColumn<QByteArray>("format"); QTest::addColumn<QImage::Format>("imageFormat"); - QTest::newRow("pbm") << QString("images/image.pbm") << QByteArray("pbm") << QImage::Format_Mono; - QTest::newRow("pgm") << QString("images/image.pgm") << QByteArray("pgm") << QImage::Format_Indexed8; - QTest::newRow("ppm-1") << QString("images/image.ppm") << QByteArray("ppm") << QImage::Format_RGB32; - QTest::newRow("ppm-2") << QString("images/teapot.ppm") << QByteArray("ppm") << QImage::Format_RGB32; - QTest::newRow("ppm-3") << QString("images/runners.ppm") << QByteArray("ppm") << QImage::Format_RGB32; - QTest::newRow("ppm-4") << QString("images/test.ppm") << QByteArray("ppm") << QImage::Format_RGB32; + QTest::newRow("pbm") << QString("image.pbm") << QByteArray("pbm") << QImage::Format_Mono; + QTest::newRow("pgm") << QString("image.pgm") << QByteArray("pgm") << QImage::Format_Indexed8; + QTest::newRow("ppm-1") << QString("image.ppm") << QByteArray("ppm") << QImage::Format_RGB32; + QTest::newRow("ppm-2") << QString("teapot.ppm") << QByteArray("ppm") << QImage::Format_RGB32; + QTest::newRow("ppm-3") << QString("runners.ppm") << QByteArray("ppm") << QImage::Format_RGB32; + QTest::newRow("ppm-4") << QString("test.ppm") << QByteArray("ppm") << QImage::Format_RGB32; #ifdef QTEST_HAVE_JPEG - QTest::newRow("jpeg-1") << QString("images/beavis.jpg") << QByteArray("jpeg") << QImage::Format_Indexed8; - QTest::newRow("jpeg-2") << QString("images/YCbCr_cmyk.jpg") << QByteArray("jpeg") << QImage::Format_RGB32; - QTest::newRow("jpeg-3") << QString("images/YCbCr_rgb.jpg") << QByteArray("jpeg") << QImage::Format_RGB32; + QTest::newRow("jpeg-1") << QString("beavis.jpg") << QByteArray("jpeg") << QImage::Format_Indexed8; + QTest::newRow("jpeg-2") << QString("YCbCr_cmyk.jpg") << QByteArray("jpeg") << QImage::Format_RGB32; + QTest::newRow("jpeg-3") << QString("YCbCr_rgb.jpg") << QByteArray("jpeg") << QImage::Format_RGB32; #endif #if defined QTEST_HAVE_GIF - QTest::newRow("gif-1") << QString("images/earth.gif") << QByteArray("gif") << QImage::Format_Invalid; - QTest::newRow("gif-2") << QString("images/trolltech.gif") << QByteArray("gif") << QImage::Format_Invalid; + QTest::newRow("gif-1") << QString("earth.gif") << QByteArray("gif") << QImage::Format_Invalid; + QTest::newRow("gif-2") << QString("trolltech.gif") << QByteArray("gif") << QImage::Format_Invalid; #endif - QTest::newRow("xbm") << QString("images/gnus.xbm") << QByteArray("xbm") << QImage::Format_MonoLSB; - QTest::newRow("xpm") << QString("images/marble.xpm") << QByteArray("xpm") << QImage::Format_Indexed8; - QTest::newRow("bmp-1") << QString("images/colorful.bmp") << QByteArray("bmp") << QImage::Format_Indexed8; - QTest::newRow("bmp-2") << QString("images/font.bmp") << QByteArray("bmp") << QImage::Format_Indexed8; - QTest::newRow("png") << QString("images/kollada.png") << QByteArray("png") << QImage::Format_ARGB32; - QTest::newRow("png-2") << QString("images/YCbCr_cmyk.png") << QByteArray("png") << QImage::Format_RGB32; - QTest::newRow("mng-1") << QString("images/ball.mng") << QByteArray("mng") << QImage::Format_Invalid; - QTest::newRow("mng-2") << QString("images/fire.mng") << QByteArray("mng") << QImage::Format_Invalid; + QTest::newRow("xbm") << QString("gnus.xbm") << QByteArray("xbm") << QImage::Format_MonoLSB; + QTest::newRow("xpm") << QString("marble.xpm") << QByteArray("xpm") << QImage::Format_Indexed8; + QTest::newRow("bmp-1") << QString("colorful.bmp") << QByteArray("bmp") << QImage::Format_Indexed8; + QTest::newRow("bmp-2") << QString("font.bmp") << QByteArray("bmp") << QImage::Format_Indexed8; + QTest::newRow("png") << QString("kollada.png") << QByteArray("png") << QImage::Format_ARGB32; + QTest::newRow("png-2") << QString("YCbCr_cmyk.png") << QByteArray("png") << QImage::Format_RGB32; + QTest::newRow("mng-1") << QString("ball.mng") << QByteArray("mng") << QImage::Format_Invalid; + QTest::newRow("mng-2") << QString("fire.mng") << QByteArray("mng") << QImage::Format_Invalid; } void tst_QImageReader::imageFormat() @@ -460,7 +491,8 @@ void tst_QImageReader::imageFormat() QFETCH(QString, fileName); QFETCH(QByteArray, format); QFETCH(QImage::Format, imageFormat); - if (QImageReader::imageFormat(fileName).isEmpty()) { + + if (QImageReader::imageFormat(prefix + fileName).isEmpty()) { if (QByteArray("jpeg") == format) #ifndef QTEST_HAVE_JPEG return; @@ -475,31 +507,31 @@ void tst_QImageReader::imageFormat() #endif // !QTEST_HAVE_MNG QSKIP(("Qt does not support the " + format + " format.").constData(), SkipSingle); } else { - QCOMPARE(QImageReader::imageFormat(fileName), format); + QCOMPARE(QImageReader::imageFormat(prefix + fileName), format); } - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); QCOMPARE(reader.imageFormat(), imageFormat); } void tst_QImageReader::blackXPM() { - QImage image(QLatin1String("images/black.xpm")); - QImage image2(QLatin1String("images/black.png")); + QImage image(prefix + QLatin1String("black.xpm")); + QImage image2(prefix + QLatin1String("black.png")); QCOMPARE(image.pixel(25, 25), qRgb(190, 190, 190)); QCOMPARE(image.pixel(25, 25), image2.pixel(25, 25)); } void tst_QImageReader::transparentXPM() { - QImage image(QLatin1String("images/nontransparent.xpm")); - QImage image2(QLatin1String("images/transparent.xpm")); + QImage image(prefix + QLatin1String("nontransparent.xpm")); + QImage image2(prefix + QLatin1String("transparent.xpm")); QCOMPARE(image.format(), QImage::Format_RGB32); QCOMPARE(image2.format(), QImage::Format_ARGB32); } void tst_QImageReader::multiWordNamedColorXPM() { - QImage image(QLatin1String("images/namedcolors.xpm")); + QImage image(prefix + QLatin1String("namedcolors.xpm")); QCOMPARE(image.pixel(0, 0), qRgb(102, 139, 139)); // pale turquoise 4 QCOMPARE(image.pixel(0, 1), qRgb(250, 250, 210)); // light golden rod yellow QCOMPARE(image.pixel(0, 2), qRgb(255, 250, 205)); // lemon chiffon @@ -590,7 +622,7 @@ void tst_QImageReader::supportsAnimation() { QFETCH(QString, fileName); QFETCH(bool, success); - QImageReader io("images/" + fileName); + QImageReader io(prefix + fileName); QCOMPARE(io.supportsAnimation(), success); } @@ -603,7 +635,7 @@ void tst_QImageReader::sizeBeforeRead() { QFETCH(QString, fileName); QFETCH(QByteArray, format); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); QVERIFY(reader.canRead()); if (format == "mng") { QCOMPARE(reader.size(), QSize()); @@ -641,7 +673,7 @@ void tst_QImageReader::imageFormatBeforeRead() void tst_QImageReader::gifHandlerBugs() { { - QImageReader io("images/trolltech.gif"); + QImageReader io(prefix + "trolltech.gif"); QVERIFY(io.loopCount() != 1); int count=0; for (; io.canRead(); io.read(), ++count) ; @@ -650,8 +682,8 @@ void tst_QImageReader::gifHandlerBugs() // Task 95166 { - QImageReader io1("images/bat1.gif"); - QImageReader io2("images/bat2.gif"); + QImageReader io1(prefix + "bat1.gif"); + QImageReader io2(prefix + "bat2.gif"); QVERIFY(io1.canRead()); QVERIFY(io2.canRead()); QImage im1 = io1.read(); @@ -663,8 +695,8 @@ void tst_QImageReader::gifHandlerBugs() // Task 9994 { - QImageReader io1("images/noclearcode.gif"); - QImageReader io2("images/noclearcode.bmp"); + QImageReader io1(prefix + "noclearcode.gif"); + QImageReader io2(prefix + "noclearcode.bmp"); QVERIFY(io1.canRead()); QVERIFY(io2.canRead()); QImage im1 = io1.read(); QImage im2 = io2.read(); QVERIFY(!im1.isNull()); QVERIFY(!im2.isNull()); @@ -728,29 +760,29 @@ void tst_QImageReader::readFromDevice_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<QByteArray>("format"); - QTest::newRow("pbm") << QString("images/image.pbm") << QByteArray("pbm"); - QTest::newRow("pgm") << QString("images/image.pgm") << QByteArray("pgm"); - QTest::newRow("ppm-1") << QString("images/image.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-2") << QString("images/teapot.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-3") << QString("images/teapot.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-4") << QString("images/runners.ppm") << QByteArray("ppm"); + QTest::newRow("pbm") << QString("image.pbm") << QByteArray("pbm"); + QTest::newRow("pgm") << QString("image.pgm") << QByteArray("pgm"); + QTest::newRow("ppm-1") << QString("image.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-2") << QString("teapot.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-3") << QString("teapot.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-4") << QString("runners.ppm") << QByteArray("ppm"); #ifdef QTEST_HAVE_JPEG - QTest::newRow("jpeg-1") << QString("images/beavis.jpg") << QByteArray("jpeg"); - QTest::newRow("jpeg-2") << QString("images/YCbCr_cmyk.jpg") << QByteArray("jpeg"); - QTest::newRow("jpeg-3") << QString("images/YCbCr_rgb.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-1") << QString("beavis.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-2") << QString("YCbCr_cmyk.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-3") << QString("YCbCr_rgb.jpg") << QByteArray("jpeg"); #endif // QTEST_HAVE_JPEG #ifdef QTEST_HAVE_GIF - QTest::newRow("gif-1") << QString("images/earth.gif") << QByteArray("gif"); - QTest::newRow("gif-2") << QString("images/trolltech.gif") << QByteArray("gif"); + QTest::newRow("gif-1") << QString("earth.gif") << QByteArray("gif"); + QTest::newRow("gif-2") << QString("trolltech.gif") << QByteArray("gif"); #endif // QTEST_HAVE_GIF - QTest::newRow("xbm") << QString("images/gnus.xbm") << QByteArray("xbm"); - QTest::newRow("xpm") << QString("images/marble.xpm") << QByteArray("xpm"); - QTest::newRow("bmp-1") << QString("images/colorful.bmp") << QByteArray("bmp"); - QTest::newRow("bmp-2") << QString("images/font.bmp") << QByteArray("bmp"); - QTest::newRow("png") << QString("images/kollada.png") << QByteArray("png"); + QTest::newRow("xbm") << QString("gnus.xbm") << QByteArray("xbm"); + QTest::newRow("xpm") << QString("marble.xpm") << QByteArray("xpm"); + QTest::newRow("bmp-1") << QString("colorful.bmp") << QByteArray("bmp"); + QTest::newRow("bmp-2") << QString("font.bmp") << QByteArray("bmp"); + QTest::newRow("png") << QString("kollada.png") << QByteArray("png"); #ifdef QTEST_HAVE_MNG - QTest::newRow("mng-1") << QString("images/ball.mng") << QByteArray("mng"); - QTest::newRow("mng-2") << QString("images/fire.mng") << QByteArray("mng"); + QTest::newRow("mng-1") << QString("ball.mng") << QByteArray("mng"); + QTest::newRow("mng-2") << QString("fire.mng") << QByteArray("mng"); #endif // QTEST_HAVE_MNG } @@ -761,9 +793,9 @@ void tst_QImageReader::readFromDevice() #ifdef Q_OS_SYMBIAN QSKIP("Symbian local sockets are not working", SkipAll); #endif - QImage expectedImage(fileName, format); + QImage expectedImage(prefix + fileName, format); - QFile file(fileName); + QFile file(prefix + fileName); QVERIFY(file.open(QFile::ReadOnly)); QByteArray imageData = file.readAll(); QVERIFY(!imageData.isEmpty()); @@ -812,26 +844,26 @@ void tst_QImageReader::readFromFileAfterJunk_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<QByteArray>("format"); - QTest::newRow("pbm") << QString("images/image.pbm") << QByteArray("pbm"); - QTest::newRow("pgm") << QString("images/image.pgm") << QByteArray("pgm"); - QTest::newRow("ppm-1") << QString("images/image.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-2") << QString("images/teapot.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-3") << QString("images/teapot.ppm") << QByteArray("ppm"); - QTest::newRow("ppm-4") << QString("images/runners.ppm") << QByteArray("ppm"); + QTest::newRow("pbm") << QString("image.pbm") << QByteArray("pbm"); + QTest::newRow("pgm") << QString("image.pgm") << QByteArray("pgm"); + QTest::newRow("ppm-1") << QString("image.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-2") << QString("teapot.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-3") << QString("teapot.ppm") << QByteArray("ppm"); + QTest::newRow("ppm-4") << QString("runners.ppm") << QByteArray("ppm"); #ifdef QTEST_HAVE_JPEG - QTest::newRow("jpeg-1") << QString("images/beavis.jpg") << QByteArray("jpeg"); - QTest::newRow("jpeg-2") << QString("images/YCbCr_cmyk.jpg") << QByteArray("jpeg"); - QTest::newRow("jpeg-3") << QString("images/YCbCr_rgb.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-1") << QString("beavis.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-2") << QString("YCbCr_cmyk.jpg") << QByteArray("jpeg"); + QTest::newRow("jpeg-3") << QString("YCbCr_rgb.jpg") << QByteArray("jpeg"); #endif #if defined QTEST_HAVE_GIF // QTest::newRow("gif-1") << QString("images/earth.gif") << QByteArray("gif"); // QTest::newRow("gif-2") << QString("images/trolltech.gif") << QByteArray("gif"); #endif - QTest::newRow("xbm") << QString("images/gnus.xbm") << QByteArray("xbm"); - QTest::newRow("xpm") << QString("images/marble.xpm") << QByteArray("xpm"); - QTest::newRow("bmp-1") << QString("images/colorful.bmp") << QByteArray("bmp"); - QTest::newRow("bmp-2") << QString("images/font.bmp") << QByteArray("bmp"); - QTest::newRow("png") << QString("images/kollada.png") << QByteArray("png"); + QTest::newRow("xbm") << QString("gnus.xbm") << QByteArray("xbm"); + QTest::newRow("xpm") << QString("marble.xpm") << QByteArray("xpm"); + QTest::newRow("bmp-1") << QString("colorful.bmp") << QByteArray("bmp"); + QTest::newRow("bmp-2") << QString("font.bmp") << QByteArray("bmp"); + QTest::newRow("png") << QString("kollada.png") << QByteArray("png"); // QTest::newRow("mng-1") << QString("images/ball.mng") << QByteArray("mng"); // QTest::newRow("mng-2") << QString("images/fire.mng") << QByteArray("mng"); } @@ -850,7 +882,7 @@ void tst_QImageReader::readFromFileAfterJunk() QFile junkFile("junk"); QVERIFY(junkFile.open(QFile::WriteOnly)); - QFile imageFile(fileName); + QFile imageFile(prefix + fileName); QVERIFY(imageFile.open(QFile::ReadOnly)); QByteArray imageData = imageFile.readAll(); QVERIFY(!imageData.isNull()); @@ -868,7 +900,7 @@ void tst_QImageReader::readFromFileAfterJunk() for (int i = 0; i < iterations; ++i) { QImageWriter writer(&junkFile, format); junkFile.write("deadbeef", 9); - QVERIFY(writer.write(QImage(fileName))); + QVERIFY(writer.write(QImage(prefix + fileName))); } } junkFile.close(); @@ -902,8 +934,8 @@ void tst_QImageReader::description_data() willem["Software"] = "Created on a NeXTstation color using \"pnmtopng\"."; willem["Disclaimer"] = "Freeware."; - QTest::newRow("PNG") << QString("images/pngwithtext.png") << willem; - QTest::newRow("PNG Compressed") << QString("images/pngwithcompressedtext.png") << willem; + QTest::newRow("PNG") << QString("pngwithtext.png") << willem; + QTest::newRow("PNG Compressed") << QString("pngwithcompressedtext.png") << willem; } void tst_QImageReader::description() @@ -912,9 +944,9 @@ void tst_QImageReader::description() QFETCH(QStringMap, description); // Sanity check - QVERIFY(!QImage(fileName).isNull()); + QVERIFY(!QImage(prefix + fileName).isNull()); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); foreach (QString key, description.keys()) QCOMPARE(reader.text(key), description.value(key)); @@ -939,143 +971,143 @@ void tst_QImageReader::readFromResources_data() QTest::addColumn<QSize>("size"); QTest::addColumn<QString>("message"); - QTest::newRow("images/corrupt.bmp") << QString("images/corrupt.bmp") + QTest::newRow("corrupt.bmp") << QString("corrupt.bmp") << QByteArray("bmp") << QSize(0, 0) << QString(""); - QTest::newRow("images/negativeheight.bmp") << QString("images/negativeheight.bmp") + QTest::newRow("negativeheight.bmp") << QString("negativeheight.bmp") << QByteArray("bmp") << QSize(127, 64) << QString(""); - QTest::newRow("images/font.bmp") << QString("images/font.bmp") + QTest::newRow("font.bmp") << QString("font.bmp") << QByteArray("bmp") << QSize(240, 8) << QString(""); - QTest::newRow("images/noclearcode.bmp") << QString("images/noclearcode.bmp") + QTest::newRow("noclearcode.bmp") << QString("noclearcode.bmp") << QByteArray("bmp") << QSize(29, 18) << QString(""); - QTest::newRow("images/colorful.bmp") << QString("images/colorful.bmp") + QTest::newRow("colorful.bmp") << QString("colorful.bmp") << QByteArray("bmp") << QSize(320, 200) << QString(""); - QTest::newRow("images/16bpp.bmp") << QString("images/16bpp.bmp") + QTest::newRow("16bpp.bmp") << QString("16bpp.bmp") << QByteArray("bmp") << QSize(320, 240) << QString(""); - QTest::newRow("images/crash-signed-char.bmp") << QString("images/crash-signed-char.bmp") + QTest::newRow("crash-signed-char.bmp") << QString("crash-signed-char.bmp") << QByteArray("bmp") << QSize(360, 280) << QString(""); - QTest::newRow("images/4bpp-rle.bmp") << QString("images/4bpp-rle.bmp") + QTest::newRow("4bpp-rle.bmp") << QString("4bpp-rle.bmp") << QByteArray("bmp") << QSize(640, 480) << QString(""); #ifdef QTEST_HAVE_GIF - QTest::newRow("images/corrupt.gif") << QString("images/corrupt.gif") + QTest::newRow("corrupt.gif") << QString("corrupt.gif") << QByteArray("gif") << QSize(0, 0) << QString(""); - QTest::newRow("images/trolltech.gif") << QString("images/trolltech.gif") + QTest::newRow("trolltech.gif") << QString("trolltech.gif") << QByteArray("gif") << QSize(128, 64) << QString(""); - QTest::newRow("images/noclearcode.gif") << QString("images/noclearcode.gif") + QTest::newRow("noclearcode.gif") << QString("noclearcode.gif") << QByteArray("gif") << QSize(29, 18) << QString(""); - QTest::newRow("images/earth.gif") << QString("images/earth.gif") + QTest::newRow("earth.gif") << QString("earth.gif") << QByteArray("gif") << QSize(320, 200) << QString(""); - QTest::newRow("images/bat1.gif") << QString("images/bat1.gif") + QTest::newRow("bat1.gif") << QString("bat1.gif") << QByteArray("gif") << QSize(32, 32) << QString(""); - QTest::newRow("images/bat2.gif") << QString("images/bat2.gif") + QTest::newRow("bat2.gif") << QString("bat2.gif") << QByteArray("gif") << QSize(32, 32) << QString(""); #endif #ifdef QTEST_HAVE_JPEG - QTest::newRow("images/corrupt.jpg") << QString("images/corrupt.jpg") + QTest::newRow("corrupt.jpg") << QString("corrupt.jpg") << QByteArray("jpg") << QSize(0, 0) << QString("JPEG datastream contains no image"); - QTest::newRow("images/beavis.jpg") << QString("images/beavis.jpg") + QTest::newRow("beavis.jpg") << QString("beavis.jpg") << QByteArray("jpg") << QSize(350, 350) << QString(""); - QTest::newRow("images/YCbCr_cmyk.jpg") << QString("images/YCbCr_cmyk.jpg") + QTest::newRow("YCbCr_cmyk.jpg") << QString("YCbCr_cmyk.jpg") << QByteArray("jpg") << QSize(75, 50) << QString(""); - QTest::newRow("images/YCbCr_rgb.jpg") << QString("images/YCbCr_rgb.jpg") + QTest::newRow("YCbCr_rgb.jpg") << QString("YCbCr_rgb.jpg") << QByteArray("jpg") << QSize(75, 50) << QString(""); #endif #ifdef QTEST_HAVE_MNG - QTest::newRow("images/corrupt.mng") << QString("images/corrupt.mng") + QTest::newRow("corrupt.mng") << QString("corrupt.mng") << QByteArray("mng") << QSize(0, 0) << QString("MNG error 901: Application signalled I/O error; chunk IHDR; subcode 0:0"); - QTest::newRow("images/fire.mng") << QString("images/fire.mng") + QTest::newRow("fire.mng") << QString("fire.mng") << QByteArray("mng") << QSize(30, 60) << QString(""); - QTest::newRow("images/ball.mng") << QString("images/ball.mng") + QTest::newRow("ball.mng") << QString("ball.mng") << QByteArray("mng") << QSize(32, 32) << QString(""); #endif - QTest::newRow("images/image.pbm") << QString("images/image.pbm") + QTest::newRow("image.pbm") << QString("image.pbm") << QByteArray("pbm") << QSize(16, 6) << QString(""); - QTest::newRow("images/image.pgm") << QString("images/image.pgm") + QTest::newRow("image.pgm") << QString("image.pgm") << QByteArray("pgm") << QSize(24, 7) << QString(""); - QTest::newRow("images/corrupt.png") << QString("images/corrupt.png") + QTest::newRow("corrupt.png") << QString("corrupt.png") << QByteArray("png") << QSize(0, 0) << QString(""); - QTest::newRow("images/away.png") << QString("images/away.png") + QTest::newRow("away.png") << QString("away.png") << QByteArray("png") << QSize(16, 16) << QString(""); - QTest::newRow("images/image.png") << QString("images/image.png") + QTest::newRow("image.png") << QString("image.png") << QByteArray("png") << QSize(22, 22) << QString(""); - QTest::newRow("images/pngwithcompressedtext.png") << QString("images/pngwithcompressedtext.png") + QTest::newRow("pngwithcompressedtext.png") << QString("pngwithcompressedtext.png") << QByteArray("png") << QSize(32, 32) << QString(""); - QTest::newRow("images/pngwithtext.png") << QString("images/pngwithtext.png") + QTest::newRow("pngwithtext.png") << QString("pngwithtext.png") << QByteArray("png") << QSize(32, 32) << QString(""); - QTest::newRow("images/kollada.png") << QString("images/kollada.png") + QTest::newRow("kollada.png") << QString("kollada.png") << QByteArray("png") << QSize(436, 160) << QString(""); - QTest::newRow("images/black.png") << QString("images/black.png") + QTest::newRow("black.png") << QString("black.png") << QByteArray("png") << QSize(48, 48) << QString(""); - QTest::newRow("images/YCbCr_cmyk.png") << QString("images/YCbCr_cmyk.png") + QTest::newRow("YCbCr_cmyk.png") << QString("YCbCr_cmyk.png") << QByteArray("png") << QSize(75, 50) << QString(""); - QTest::newRow("images/teapot.ppm") << QString("images/teapot.ppm") + QTest::newRow("teapot.ppm") << QString("teapot.ppm") << QByteArray("ppm") << QSize(256, 256) << QString(""); - QTest::newRow("images/image.ppm") << QString("images/image.ppm") + QTest::newRow("image.ppm") << QString("image.ppm") << QByteArray("ppm") << QSize(4, 4) << QString(""); - QTest::newRow("images/runners.ppm") << QString("images/runners.ppm") + QTest::newRow("runners.ppm") << QString("runners.ppm") << QByteArray("ppm") << QSize(400, 400) << QString(""); - QTest::newRow("images/test.ppm") << QString("images/test.ppm") + QTest::newRow("test.ppm") << QString("test.ppm") << QByteArray("ppm") << QSize(10, 10) << QString(""); -// QTest::newRow("images/corrupt.xbm") << QString("images/corrupt.xbm") << QByteArray("xbm") << QSize(0, 0); - QTest::newRow("images/gnus.xbm") << QString("images/gnus.xbm") +// QTest::newRow("corrupt.xbm") << QString("corrupt.xbm") << QByteArray("xbm") << QSize(0, 0); + QTest::newRow("gnus.xbm") << QString("gnus.xbm") << QByteArray("xbm") << QSize(271, 273) << QString(""); - QTest::newRow("images/corrupt-colors.xpm") << QString("images/corrupt-colors.xpm") + QTest::newRow("corrupt-colors.xpm") << QString("corrupt-colors.xpm") << QByteArray("xpm") << QSize(0, 0) << QString("QImage: XPM color specification is missing: bla9an.n#x"); - QTest::newRow("images/corrupt-pixels.xpm") << QString("images/corrupt-pixels.xpm") + QTest::newRow("corrupt-pixels.xpm") << QString("corrupt-pixels.xpm") << QByteArray("xpm") << QSize(0, 0) << QString("QImage: XPM pixels missing on image line 3"); - QTest::newRow("images/marble.xpm") << QString("images/marble.xpm") + QTest::newRow("marble.xpm") << QString("marble.xpm") << QByteArray("xpm") << QSize(240, 240) << QString(""); - QTest::newRow("images/test.xpm") << QString("images/test.xpm") + QTest::newRow("test.xpm") << QString("test.xpm") << QByteArray("xpm") << QSize(256, 256) << QString(""); - QTest::newRow("images/black.xpm") << QString("images/black.xpm") + QTest::newRow("black.xpm") << QString("black.xpm") << QByteArray("xpm") << QSize(48, 48) << QString(""); - QTest::newRow("images/namedcolors.xpm") << QString("images/namedcolors.xpm") + QTest::newRow("namedcolors.xpm") << QString("namedcolors.xpm") << QByteArray("xpm") << QSize(8, 8) << QString(""); - QTest::newRow("images/nontransparent.xpm") << QString("images/nontransparent.xpm") + QTest::newRow("nontransparent.xpm") << QString("nontransparent.xpm") << QByteArray("xpm") << QSize(8, 8) << QString(""); - QTest::newRow("images/transparent.xpm") << QString("images/transparent.xpm") + QTest::newRow("transparent.xpm") << QString("transparent.xpm") << QByteArray("xpm") << QSize(8, 8) << QString(""); } @@ -1086,9 +1118,8 @@ void tst_QImageReader::readFromResources() QFETCH(QByteArray, format); QFETCH(QSize, size); QFETCH(QString, message); - for (int i = 0; i < 2; ++i) { - QString file = i ? (":/" + fileName) : fileName; + QString file = i ? (":/images/" + fileName) : (prefix + fileName); { // suppress warnings if we expect them if (!message.isEmpty()) { @@ -1152,7 +1183,7 @@ void tst_QImageReader::readFromResources() QTest::ignoreMessage(QtWarningMsg, message.toLatin1()); QTest::ignoreMessage(QtWarningMsg, message.toLatin1()); } - QCOMPARE(QImageReader(fileName).read(), QImageReader(":/" + fileName).read()); + QCOMPARE(QImageReader(prefix + fileName).read(), QImageReader(":/images/" + fileName).read()); } void tst_QImageReader::readCorruptImage_data() @@ -1161,25 +1192,25 @@ void tst_QImageReader::readCorruptImage_data() QTest::addColumn<bool>("shouldFail"); QTest::addColumn<QString>("message"); #if defined QTEST_HAVE_JPEG - QTest::newRow("corrupt jpeg") << QString("images/corrupt.jpg") << true + QTest::newRow("corrupt jpeg") << QString("corrupt.jpg") << true << QString("JPEG datastream contains no image"); #endif #if defined QTEST_HAVE_GIF - QTest::newRow("corrupt gif") << QString("images/corrupt.gif") << true << QString(""); + QTest::newRow("corrupt gif") << QString("corrupt.gif") << true << QString(""); #endif #ifdef QTEST_HAVE_MNG - QTest::newRow("corrupt mng") << QString("images/corrupt.mng") << true + QTest::newRow("corrupt mng") << QString("corrupt.mng") << true << QString("MNG error 901: Application signalled I/O error; chunk IHDR; subcode 0:0"); #endif - QTest::newRow("corrupt png") << QString("images/corrupt.png") << true << QString(""); - QTest::newRow("corrupt bmp") << QString("images/corrupt.bmp") << true << QString(""); - QTest::newRow("corrupt xpm (colors)") << QString("images/corrupt-colors.xpm") << true + QTest::newRow("corrupt png") << QString("corrupt.png") << true << QString(""); + QTest::newRow("corrupt bmp") << QString("corrupt.bmp") << true << QString(""); + QTest::newRow("corrupt xpm (colors)") << QString("corrupt-colors.xpm") << true << QString("QImage: XPM color specification is missing: bla9an.n#x"); - QTest::newRow("corrupt xpm (pixels)") << QString("images/corrupt-pixels.xpm") << true + QTest::newRow("corrupt xpm (pixels)") << QString("corrupt-pixels.xpm") << true << QString("QImage: XPM pixels missing on image line 3"); - QTest::newRow("corrupt xbm") << QString("images/corrupt.xbm") << false << QString(""); + QTest::newRow("corrupt xbm") << QString("corrupt.xbm") << false << QString(""); #if defined QTEST_HAVE_TIFF - QTest::newRow("corrupt tiff") << QString("images/corrupt-data.tif") << true << QString(""); + QTest::newRow("corrupt tiff") << QString("corrupt-data.tif") << true << QString(""); #endif } @@ -1188,16 +1219,17 @@ void tst_QImageReader::readCorruptImage() QFETCH(QString, fileName); QFETCH(bool, shouldFail); QFETCH(QString, message); + if (!message.isEmpty()) QTest::ignoreMessage(QtWarningMsg, message.toLatin1()); - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); QVERIFY(reader.canRead()); QCOMPARE(reader.read().isNull(), shouldFail); } void tst_QImageReader::readCorruptBmp() { - QCOMPARE(QImage("images/tst7.bmp").convertToFormat(QImage::Format_ARGB32_Premultiplied), QImage("images/tst7.png").convertToFormat(QImage::Format_ARGB32_Premultiplied)); + QCOMPARE(QImage("tst7.bmp").convertToFormat(QImage::Format_ARGB32_Premultiplied), QImage("images/tst7.png").convertToFormat(QImage::Format_ARGB32_Premultiplied)); } void tst_QImageReader::supportsOption_data() @@ -1205,7 +1237,7 @@ void tst_QImageReader::supportsOption_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<QIntList>("options"); - QTest::newRow("png") << QString("images/black.png") + QTest::newRow("png") << QString("black.png") << (QIntList() << QImageIOHandler::Gamma << QImageIOHandler::Description << QImageIOHandler::Quality @@ -1233,7 +1265,7 @@ void tst_QImageReader::supportsOption() << QImageIOHandler::Animation << QImageIOHandler::BackgroundColor; - QImageReader reader(fileName); + QImageReader reader(prefix + fileName); for (int i = 0; i < options.size(); ++i) { QVERIFY(reader.supportsOption(QImageIOHandler::ImageOption(options.at(i)))); allOptions.remove(QImageIOHandler::ImageOption(options.at(i))); @@ -1249,14 +1281,14 @@ void tst_QImageReader::tiffCompression_data() QTest::addColumn<QString>("uncompressedFile"); QTest::addColumn<QString>("compressedFile"); - QTest::newRow("TIFF: adobedeflate") << "images/rgba_nocompression_littleendian.tif" - << "images/rgba_adobedeflate_littleendian.tif"; - QTest::newRow("TIFF: lzw") << "images/rgba_nocompression_littleendian.tif" - << "images/rgba_lzw_littleendian.tif"; - QTest::newRow("TIFF: packbits") << "images/rgba_nocompression_littleendian.tif" - << "images/rgba_packbits_littleendian.tif"; - QTest::newRow("TIFF: zipdeflate") << "images/rgba_nocompression_littleendian.tif" - << "images/rgba_zipdeflate_littleendian.tif"; + QTest::newRow("TIFF: adobedeflate") << "rgba_nocompression_littleendian.tif" + << "rgba_adobedeflate_littleendian.tif"; + QTest::newRow("TIFF: lzw") << "rgba_nocompression_littleendian.tif" + << "rgba_lzw_littleendian.tif"; + QTest::newRow("TIFF: packbits") << "rgba_nocompression_littleendian.tif" + << "rgba_packbits_littleendian.tif"; + QTest::newRow("TIFF: zipdeflate") << "rgba_nocompression_littleendian.tif" + << "rgba_zipdeflate_littleendian.tif"; } void tst_QImageReader::tiffCompression() @@ -1264,16 +1296,16 @@ void tst_QImageReader::tiffCompression() QFETCH(QString, uncompressedFile); QFETCH(QString, compressedFile); - QImage uncompressedImage(uncompressedFile); - QImage compressedImage(compressedFile); + QImage uncompressedImage(prefix + uncompressedFile); + QImage compressedImage(prefix + compressedFile); QCOMPARE(uncompressedImage, compressedImage); } void tst_QImageReader::tiffEndianness() { - QImage littleEndian("images/rgba_nocompression_littleendian.tif"); - QImage bigEndian("images/rgba_nocompression_bigendian.tif"); + QImage littleEndian(prefix + "rgba_nocompression_littleendian.tif"); + QImage bigEndian(prefix + "rgba_nocompression_bigendian.tif"); QCOMPARE(littleEndian, bigEndian); } @@ -1285,8 +1317,10 @@ void tst_QImageReader::dotsPerMeter_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<int>("expectedDotsPerMeterX"); QTest::addColumn<int>("expectedDotsPerMeterY"); - - QTest::newRow("TIFF: 72 dpi") << "images/rgba_nocompression_littleendian.tif" << qRound(72 * (100 / 2.54)) << qRound(72 * (100 / 2.54)); +#if defined QTEST_HAVE_TIFF + QTest::newRow("TIFF: 72 dpi") << ("rgba_nocompression_littleendian.tif") << qRound(72 * (100 / 2.54)) << qRound(72 * (100 / 2.54)); + QTest::newRow("TIFF: 100 dpi") << ("image_100dpi.tif") << qRound(100 * (100 / 2.54)) << qRound(100 * (100 / 2.54)); +#endif } void tst_QImageReader::dotsPerMeter() @@ -1295,7 +1329,7 @@ void tst_QImageReader::dotsPerMeter() QFETCH(int, expectedDotsPerMeterX); QFETCH(int, expectedDotsPerMeterY); - QImage image(fileName); + QImage image(prefix + fileName); QCOMPARE(image.dotsPerMeterX(), expectedDotsPerMeterX); QCOMPARE(image.dotsPerMeterY(), expectedDotsPerMeterY); @@ -1306,8 +1340,10 @@ void tst_QImageReader::physicalDpi_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<int>("expectedPhysicalDpiX"); QTest::addColumn<int>("expectedPhysicalDpiY"); - - QTest::newRow("TIFF: 72 dpi") << "images/rgba_nocompression_littleendian.tif" << 72 << 72; +#if defined QTEST_HAVE_TIFF + QTest::newRow("TIFF: 72 dpi") << "rgba_nocompression_littleendian.tif" << 72 << 72; + QTest::newRow("TIFF: 100 dpi") << "image_100dpi.tif" << 100 << 100; +#endif } void tst_QImageReader::physicalDpi() @@ -1316,7 +1352,7 @@ void tst_QImageReader::physicalDpi() QFETCH(int, expectedPhysicalDpiX); QFETCH(int, expectedPhysicalDpiY); - QImage image(fileName); + QImage image(prefix + fileName); QCOMPARE(image.physicalDpiX(), expectedPhysicalDpiX); QCOMPARE(image.physicalDpiY(), expectedPhysicalDpiY); @@ -1327,7 +1363,7 @@ void tst_QImageReader::autoDetectImageFormat() // Assume PNG is supported :-) { // Disables file name extension probing - QImageReader reader("images/kollada"); + QImageReader reader(prefix + "kollada"); reader.setAutoDetectImageFormat(false); QVERIFY(!reader.canRead()); QVERIFY(reader.read().isNull()); @@ -1337,7 +1373,7 @@ void tst_QImageReader::autoDetectImageFormat() } { // Disables detection based on suffix - QImageReader reader("images/kollada.png"); + QImageReader reader(prefix + "kollada.png"); reader.setAutoDetectImageFormat(false); QVERIFY(!reader.canRead()); QVERIFY(reader.read().isNull()); @@ -1347,7 +1383,7 @@ void tst_QImageReader::autoDetectImageFormat() } { // Disables detection based on content - QImageReader reader("images/kollada-noext"); + QImageReader reader(prefix + "kollada-noext"); reader.setAutoDetectImageFormat(false); QVERIFY(!reader.canRead()); QVERIFY(reader.read().isNull()); @@ -1370,5 +1406,32 @@ void tst_QImageReader::fileNameProbing() QCOMPARE(r.fileName(), name); } +void tst_QImageReader::pixelCompareWithBaseline_data() +{ + QTest::addColumn<QString>("fileName"); + + QTest::newRow("floppy (16px,32px - 16 colors)") << "35floppy.ico"; + QTest::newRow("semitransparent") << "semitransparent.ico"; + QTest::newRow("slightlybroken") << "kde_favicon.ico"; +} + +void tst_QImageReader::pixelCompareWithBaseline() +{ + QFETCH(QString, fileName); + + QImage icoImg; + // might fail if the plugin does not exist, which is ok. + if (icoImg.load(QString::fromAscii("images/%1").arg(fileName))) { + QString baselineFileName = QString::fromAscii("baseline/%1").arg(fileName); +#if 0 + icoImg.save(baselineFileName); +#else + QImage baseImg; + QVERIFY(baseImg.load(baselineFileName)); + QCOMPARE(baseImg, icoImg); +#endif + } +} + QTEST_MAIN(tst_QImageReader) #include "tst_qimagereader.moc" diff --git a/tests/auto/qimagewriter/tst_qimagewriter.cpp b/tests/auto/qimagewriter/tst_qimagewriter.cpp index 21c503e..f5b4e91 100644 --- a/tests/auto/qimagewriter/tst_qimagewriter.cpp +++ b/tests/auto/qimagewriter/tst_qimagewriter.cpp @@ -62,6 +62,7 @@ Q_DECLARE_METATYPE(QStringMap) Q_DECLARE_METATYPE(QIntList) Q_DECLARE_METATYPE(QImageWriter::ImageWriterError) Q_DECLARE_METATYPE(QIODevice *) +Q_DECLARE_METATYPE(QImage::Format) //TESTED_FILES= @@ -85,6 +86,9 @@ private slots: void writeImage2(); void supportedFormats(); + void readWriteNonDestructive_data(); + void readWriteNonDestructive(); + #if defined QTEST_HAVE_TIFF void largeTiff(); #endif @@ -100,6 +104,9 @@ private slots: void saveWithNoFormat_data(); void saveWithNoFormat(); + void resolution_data(); + void resolution(); + void saveToTemporaryFile(); }; #ifdef Q_OS_SYMBIAN @@ -167,7 +174,7 @@ tst_QImageWriter::tst_QImageWriter() tst_QImageWriter::~tst_QImageWriter() { - QDir dir("images"); + QDir dir(prefix); QStringList filesToDelete = dir.entryList(QStringList() << "gen-*" , QDir::NoDotAndDotDot | QDir::Files); foreach( QString file, filesToDelete) { QFile::remove(dir.absoluteFilePath(file)); @@ -380,6 +387,28 @@ void tst_QImageWriter::supportedFormats() QCOMPARE(formatSet.size(), formats.size()); } +void tst_QImageWriter::readWriteNonDestructive_data() +{ + QTest::addColumn<QImage::Format>("format"); + QTest::addColumn<QImage::Format>("expectedFormat"); + QTest::newRow("tiff mono") << QImage::Format_Mono << QImage::Format_Mono; + QTest::newRow("tiff indexed") << QImage::Format_Indexed8 << QImage::Format_Indexed8; + QTest::newRow("tiff rgb32") << QImage::Format_ARGB32 << QImage::Format_ARGB32; +} + +void tst_QImageWriter::readWriteNonDestructive() +{ + QFETCH(QImage::Format, format); + QFETCH(QImage::Format, expectedFormat); + QImage image = QImage(prefix + "colorful.bmp").convertToFormat(format); + QVERIFY(image.save(prefix + "gen-readWriteNonDestructive.tiff")); + + QImage image2 = QImage(prefix + "gen-readWriteNonDestructive.tiff"); + QImage::Format readFormat = image2.format(); + QCOMPARE(readFormat, expectedFormat); + QCOMPARE(image, image2); +} + void tst_QImageWriter::setDescription_data() { QTest::addColumn<QString>("fileName"); @@ -449,13 +478,13 @@ void tst_QImageWriter::supportsOption_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<QIntList>("options"); - QTest::newRow("png") << QString(prefix + "gen-black.png") + QTest::newRow("png") << QString("gen-black.png") << (QIntList() << QImageIOHandler::Gamma << QImageIOHandler::Description << QImageIOHandler::Quality << QImageIOHandler::Size); #if defined QTEST_HAVE_TIFF - QTest::newRow("tiff") << QString("images/gen-black.tiff") + QTest::newRow("tiff") << QString("gen-black.tiff") << (QIntList() << QImageIOHandler::Size << QImageIOHandler::CompressionRatio); #endif @@ -482,7 +511,7 @@ void tst_QImageWriter::supportsOption() << QImageIOHandler::Animation << QImageIOHandler::BackgroundColor; - QImageWriter writer(fileName); + QImageWriter writer(prefix + fileName); for (int i = 0; i < options.size(); ++i) { QVERIFY(writer.supportsOption(QImageIOHandler::ImageOption(options.at(i)))); allOptions.remove(QImageIOHandler::ImageOption(options.at(i))); @@ -537,6 +566,39 @@ void tst_QImageWriter::saveWithNoFormat() QVERIFY2(!outImage.isNull(), qPrintable(reader.errorString())); } +void tst_QImageWriter::resolution_data() +{ + QTest::addColumn<QString>("filename"); + QTest::addColumn<int>("expectedDotsPerMeterX"); + QTest::addColumn<int>("expectedDotsPerMeterY"); +#if defined QTEST_HAVE_TIFF + QTest::newRow("TIFF: 100 dpi") << ("image_100dpi.tif") << qRound(100 * (100 / 2.54)) << qRound(100 * (100 / 2.54)); + QTest::newRow("TIFF: 50 dpi") << ("image_50dpi.tif") << qRound(50 * (100 / 2.54)) << qRound(50 * (100 / 2.54)); + QTest::newRow("TIFF: 300 dot per meter") << ("image_300dpm.tif") << 300 << 300; +#endif +} + +void tst_QImageWriter::resolution() +{ + QFETCH(QString, filename); + QFETCH(int, expectedDotsPerMeterX); + QFETCH(int, expectedDotsPerMeterY); + + QImage image(prefix + QLatin1String("colorful.bmp")); + image.setDotsPerMeterX(expectedDotsPerMeterX); + image.setDotsPerMeterY(expectedDotsPerMeterY); + const QString generatedFilepath = prefix + "gen-" + filename; + { + QImageWriter writer(generatedFilepath); + QVERIFY(writer.write(image)); + } + QImageReader reader(generatedFilepath); + const QImage generatedImage = reader.read(); + + QCOMPARE(expectedDotsPerMeterX, generatedImage.dotsPerMeterX()); + QCOMPARE(expectedDotsPerMeterY, generatedImage.dotsPerMeterY()); +} + void tst_QImageWriter::saveToTemporaryFile() { QImage image(prefix + "kollada.png"); diff --git a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp index 0c1408f..f1adc51 100644 --- a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp @@ -1123,7 +1123,7 @@ void tst_QItemDelegate::enterKey() if (widget == 2 || widget == 3) { QVERIFY(!editor.isNull()); - QCOMPARE(editor->hasFocus(), expectedFocus); + QCOMPARE(editor && editor->hasFocus(), expectedFocus); } } diff --git a/tests/auto/qitemmodel/qitemmodel.pro b/tests/auto/qitemmodel/qitemmodel.pro index 962449c..381a008 100644 --- a/tests/auto/qitemmodel/qitemmodel.pro +++ b/tests/auto/qitemmodel/qitemmodel.pro @@ -1,6 +1,5 @@ -CONFIG += qtestlib +load(qttest_p4) SOURCES += tst_qitemmodel.cpp -TARGET = tst_qitemmodel QT += sql diff --git a/tests/auto/qlineedit/tst_qlineedit.cpp b/tests/auto/qlineedit/tst_qlineedit.cpp index 2b2c794..ea0afa0 100644 --- a/tests/auto/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/qlineedit/tst_qlineedit.cpp @@ -258,6 +258,7 @@ private slots: void task233101_cursorPosAfterInputMethod_data(); void task233101_cursorPosAfterInputMethod(); void task241436_passwordEchoOnEditRestoreEchoMode(); + void task248948_redoRemovedSelection(); protected slots: #ifdef QT3_SUPPORT @@ -3485,5 +3486,17 @@ void tst_QLineEdit::task241436_passwordEchoOnEditRestoreEchoMode() testWidget->setEchoMode(QLineEdit::Normal); } +void tst_QLineEdit::task248948_redoRemovedSelection() +{ + testWidget->setText("a"); + testWidget->selectAll(); + QTest::keyPress(testWidget, Qt::Key_Delete); + testWidget->undo(); + testWidget->redo(); + QTest::keyPress(testWidget, 'a'); + QTest::keyPress(testWidget, 'b'); + QCOMPARE(testWidget->text(), QLatin1String("ab")); +} + QTEST_MAIN(tst_QLineEdit) #include "tst_qlineedit.moc" diff --git a/tests/auto/qlistview/tst_qlistview.cpp b/tests/auto/qlistview/tst_qlistview.cpp index 49552e7..390ec19 100644 --- a/tests/auto/qlistview/tst_qlistview.cpp +++ b/tests/auto/qlistview/tst_qlistview.cpp @@ -54,6 +54,7 @@ #include <math.h> #include <QtGui/QScrollBar> #include <QtGui/QDialog> +#include <QtGui/QStyledItemDelegate> #if defined(Q_OS_WIN) || defined(Q_OS_WINCE) #include <windows.h> #endif @@ -108,6 +109,9 @@ private slots: void task228566_infiniteRelayout(); void task248430_crashWith0SizedItem(); void task250446_scrollChanged(); + void task196118_visualRegionForSelection(); + void task254449_draggingItemToNegativeCoordinates(); + void keyboardSearch(); }; // Testing get/set functions @@ -1557,6 +1561,102 @@ void tst_QListView::task250446_scrollChanged() QCOMPARE(view.currentIndex(), index); } +void tst_QListView::task196118_visualRegionForSelection() +{ + class MyListView : public QListView + { + public: + QRegion visualRegionForSelection() const + { return QListView::visualRegionForSelection( selectionModel()->selection()); } + } view; + + QStandardItemModel model; + QStandardItem top1("top1"); + QStandardItem sub1("sub1"); + top1.appendRow(QList<QStandardItem*>() << &sub1); + model.appendColumn(QList<QStandardItem*>() << &top1); + view.setModel(&model); + view.setRootIndex(top1.index()); + + view.selectionModel()->select(top1.index(), QItemSelectionModel::Select); + + QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1); + QVERIFY(view.visualRegionForSelection().isEmpty()); +} + +void tst_QListView::task254449_draggingItemToNegativeCoordinates() +{ + //we'll check that the items are painted correctly + class MyListView : public QListView + { + public: + void setPositionForIndex(const QPoint &position, const QModelIndex &index) + { QListView::setPositionForIndex(position, index); } + + } list; + + QStandardItemModel model(1,1); + QModelIndex index = model.index(0,0); + model.setData(index, QLatin1String("foo")); + list.setModel(&model); + list.setViewMode(QListView::IconMode); + list.show(); + QTest::qWait(200); //makes sure the layout is done + + const QPoint topLeft(-6, 0); + + + list.setPositionForIndex(topLeft, index); + + class MyItemDelegate : public QStyledItemDelegate + { + public: + MyItemDelegate() : numPaints(0) { } + void paint(QPainter *painter, + const QStyleOptionViewItem &option, const QModelIndex &index) const + { + numPaints++; + QStyledItemDelegate::paint(painter, option, index); + } + + mutable int numPaints; + } delegate; + + list.setItemDelegate(&delegate); + + //we'll make sure the item is repainted + delegate.numPaints = 0; + list.viewport()->repaint(); + + QCOMPARE(list.visualRect(index).topLeft(), topLeft); + QCOMPARE(delegate.numPaints, 1); +} + + +void tst_QListView::keyboardSearch() +{ + QStringList items; + items << "AB" << "AC" << "BA" << "BB" << "BD" << "KAFEINE" << "KONQUEROR" << "KOPETE" << "KOOKA" << "OKULAR"; + QStringListModel model(items); + + QListView view; + view.setModel(&model); + view.show(); + QTest::qWait(30); +// QCOMPARE(view.currentIndex() , model.index(0,0)); + + QTest::keyClick(&view, Qt::Key_K); + QTest::qWait(10); + QCOMPARE(view.currentIndex() , model.index(5,0)); //KAFEINE + + QTest::keyClick(&view, Qt::Key_O); + QTest::qWait(10); + QCOMPARE(view.currentIndex() , model.index(6,0)); //KONQUEROR + + QTest::keyClick(&view, Qt::Key_N); + QTest::qWait(10); + QCOMPARE(view.currentIndex() , model.index(6,0)); //KONQUEROR +} QTEST_MAIN(tst_QListView) #include "tst_qlistview.moc" diff --git a/tests/auto/qlistwidget/tst_qlistwidget.cpp b/tests/auto/qlistwidget/tst_qlistwidget.cpp index 690ea0f..6f49a37 100644 --- a/tests/auto/qlistwidget/tst_qlistwidget.cpp +++ b/tests/auto/qlistwidget/tst_qlistwidget.cpp @@ -883,31 +883,46 @@ void tst_QListWidget::itemStreaming() void tst_QListWidget::sortItems_data() { QTest::addColumn<int>("order"); - QTest::addColumn<QStringList>("initialList"); - QTest::addColumn<QStringList>("expectedList"); + QTest::addColumn<QVariantList>("initialList"); + QTest::addColumn<QVariantList>("expectedList"); QTest::addColumn<IntList>("expectedRows"); - QTest::newRow("ascending order") + QTest::newRow("ascending strings") << static_cast<int>(Qt::AscendingOrder) - << (QStringList() << "c" << "d" << "a" << "b") - << (QStringList() << "a" << "b" << "c" << "d") + << (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) + << (QVariantList() << QString("a") << QString("b") << QString("c") << QString("d")) << (IntList() << 2 << 3 << 0 << 1); - QTest::newRow("descending order") + QTest::newRow("descending strings") << static_cast<int>(Qt::DescendingOrder) - << (QStringList() << "c" << "d" << "a" << "b") - << (QStringList() << "d" << "c" << "b" << "a") + << (QVariantList() << QString("c") << QString("d") << QString("a") << QString("b")) + << (QVariantList() << QString("d") << QString("c") << QString("b") << QString("a")) << (IntList() << 1 << 0 << 3 << 2); + + QTest::newRow("ascending numbers") + << static_cast<int>(Qt::AscendingOrder) + << (QVariantList() << 1 << 11 << 2 << 22) + << (QVariantList() << 1 << 2 << 11 << 22) + << (IntList() << 0 << 2 << 1 << 3); + + QTest::newRow("descending numbers") + << static_cast<int>(Qt::DescendingOrder) + << (QVariantList() << 1 << 11 << 2 << 22) + << (QVariantList() << 22 << 11 << 2 << 1) + << (IntList() << 3 << 1 << 2 << 0); } void tst_QListWidget::sortItems() { QFETCH(int, order); - QFETCH(QStringList, initialList); - QFETCH(QStringList, expectedList); + QFETCH(QVariantList, initialList); + QFETCH(QVariantList, expectedList); QFETCH(IntList, expectedRows); - testWidget->addItems(initialList); + foreach (const QVariant &data, initialList) { + QListWidgetItem *item = new QListWidgetItem(testWidget); + item->setData(Qt::DisplayRole, data); + } QAbstractItemModel *model = testWidget->model(); QList<QPersistentModelIndex> persistent; @@ -918,7 +933,7 @@ void tst_QListWidget::sortItems() QCOMPARE(testWidget->count(), expectedList.count()); for (int i = 0; i < testWidget->count(); ++i) - QCOMPARE(testWidget->item(i)->text(), expectedList.at(i)); + QCOMPARE(testWidget->item(i)->text(), expectedList.at(i).toString()); for (int k = 0; k < testWidget->count(); ++k) QCOMPARE(persistent.at(k).row(), expectedRows.at(k)); diff --git a/tests/auto/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/qlocalsocket/tst_qlocalsocket.cpp index b3c34c4..be8ec3f 100644 --- a/tests/auto/qlocalsocket/tst_qlocalsocket.cpp +++ b/tests/auto/qlocalsocket/tst_qlocalsocket.cpp @@ -108,6 +108,8 @@ private slots: void recycleServer(); + void multiConnect(); + void debug(); #ifdef Q_OS_SYMBIAN @@ -899,6 +901,31 @@ void tst_QLocalSocket::recycleServer() QVERIFY(server.nextPendingConnection() != 0); } +void tst_QLocalSocket::multiConnect() +{ + QLocalServer server; + QLocalSocket client1; + QLocalSocket client2; + QLocalSocket client3; + + QVERIFY(server.listen("multiconnect")); + + client1.connectToServer("multiconnect"); + client2.connectToServer("multiconnect"); + client3.connectToServer("multiconnect"); + + QVERIFY(client1.waitForConnected(201)); + QVERIFY(client2.waitForConnected(202)); + QVERIFY(client3.waitForConnected(203)); + + QVERIFY(server.waitForNewConnection(201)); + QVERIFY(server.nextPendingConnection() != 0); + QVERIFY(server.waitForNewConnection(202)); + QVERIFY(server.nextPendingConnection() != 0); + QVERIFY(server.waitForNewConnection(203)); + QVERIFY(server.nextPendingConnection() != 0); +} + void tst_QLocalSocket::debug() { // Make sure this compiles diff --git a/tests/auto/qmainwindow/tst_qmainwindow.cpp b/tests/auto/qmainwindow/tst_qmainwindow.cpp index 80ba198..e46c2e1 100644 --- a/tests/auto/qmainwindow/tst_qmainwindow.cpp +++ b/tests/auto/qmainwindow/tst_qmainwindow.cpp @@ -1434,10 +1434,7 @@ void MoveSeparator::apply(QMainWindow *mw) const } QVERIFY(!path.isEmpty()); - QVector<QLayoutStruct> cache; - - l->layoutState.dockAreaLayout.separatorMove(path, QPoint(0, 0), QPoint(delta, delta), &cache); - + l->layoutState.dockAreaLayout.separatorMove(path, QPoint(0, 0), QPoint(delta, delta)); } QMap<QString, QRect> dockWidgetGeometries(QMainWindow *mw) diff --git a/tests/auto/qmake/testdata/bundle-spaces/some-file b/tests/auto/qmake/testdata/bundle-spaces/some-file index e69de29..9975dba 100644 --- a/tests/auto/qmake/testdata/bundle-spaces/some-file +++ b/tests/auto/qmake/testdata/bundle-spaces/some-file @@ -0,0 +1,6 @@ +all: + C:\git\qt-kinetic-animations\bin\qmake qdir.pro -o Makefile -spec win32-msvc2008 + nmake -f Makefile +first: all +qmake: + C:\git\qt-kinetic-animations\bin\qmake qdir.pro -o Makefile -spec win32-msvc2008 diff --git a/tests/auto/qmap/tst_qmap.cpp b/tests/auto/qmap/tst_qmap.cpp index 7e34d6f..1b3d68b 100644 --- a/tests/auto/qmap/tst_qmap.cpp +++ b/tests/auto/qmap/tst_qmap.cpp @@ -39,6 +39,7 @@ ** ****************************************************************************/ +#define QT_STRICT_ITERATORS #include <QtTest/QtTest> #include <QDebug> @@ -400,7 +401,7 @@ void tst_QMap::operator_eq() QVERIFY(a == b); QVERIFY(!(a != b)); - + a.insert(1,1); b.insert(1,1); QVERIFY(a == b); @@ -422,7 +423,7 @@ void tst_QMap::operator_eq() b.insert(-1, -1); QVERIFY(a != b); - QVERIFY(!(a == b)); + QVERIFY(!(a == b)); } { @@ -468,7 +469,7 @@ void tst_QMap::operator_eq() b.insert("willy", 1); QVERIFY(a != b); QVERIFY(!(a == b)); - } + } } void tst_QMap::empty() @@ -523,9 +524,9 @@ void tst_QMap::find() map1.insertMulti(4, compareString); } - QMap<int, QString>::const_iterator it=map1.find(4); + QMap<int, QString>::const_iterator it=map1.constFind(4); - for(i = 9; i > 2 && it != map1.end() && it.key() == 4; --i) { + for(i = 9; i > 2 && it != map1.constEnd() && it.key() == 4; --i) { compareString = testString.arg(i); QVERIFY(it.value() == compareString); ++it; @@ -546,9 +547,9 @@ void tst_QMap::constFind() map1.insert(1,"Mensch"); map1.insert(1,"Mayer"); map1.insert(2,"Hej"); - + QVERIFY(map1.constFind(4) == map1.constEnd()); - + QVERIFY(map1.constFind(1).value() == "Mayer"); QVERIFY(map1.constFind(2).value() == "Hej"); @@ -677,7 +678,7 @@ void tst_QMap::iterators() cstlIt--; QVERIFY(cstlIt.value() == "Teststring 3"); - for(cstlIt = map.begin(), i = 1; cstlIt != map.constEnd(), i < 100; ++cstlIt, ++i) + for(cstlIt = map.constBegin(), i = 1; cstlIt != map.constEnd(), i < 100; ++cstlIt, ++i) QVERIFY(cstlIt.value() == testString.arg(i)); //Java-Style iterators diff --git a/tests/auto/qmenu/tst_qmenu.cpp b/tests/auto/qmenu/tst_qmenu.cpp index 6298800..ce6afa1 100644 --- a/tests/auto/qmenu/tst_qmenu.cpp +++ b/tests/auto/qmenu/tst_qmenu.cpp @@ -92,7 +92,9 @@ private slots: void activeSubMenuPosition(); void task242454_sizeHint(); void task176201_clear(); - void task250673_activeMutliColumnSubMenuPosition(); + void task250673_activeMultiColumnSubMenuPosition(); + void task256918_setFont(); + void menuSizeHint(); protected slots: void onActivated(QAction*); void onHighlighted(QAction*); @@ -686,7 +688,7 @@ void tst_QMenu::task176201_clear() QTest::mouseClick(&menu, Qt::LeftButton, 0, menu.rect().center()); } -void tst_QMenu::task250673_activeMutliColumnSubMenuPosition() +void tst_QMenu::task250673_activeMultiColumnSubMenuPosition() { class MyMenu : public QMenu { @@ -720,5 +722,53 @@ void tst_QMenu::task250673_activeMutliColumnSubMenuPosition() const int subMenuOffset = main.style()->pixelMetric(QStyle::PM_SubMenuOverlap, 0, &main); QVERIFY((sub.geometry().left() - subMenuOffset + 5) < main.geometry().right()); } + + +void tst_QMenu::task256918_setFont() +{ + QMenu menu; + QAction *action = menu.addAction("foo"); + QFont f; + f.setPointSize(30); + action->setFont(f); + menu.show(); //ensures that the actiongeometry are calculated + QVERIFY(menu.actionGeometry(action).height() > f.pointSize()); +} + +void tst_QMenu::menuSizeHint() +{ + QMenu menu; + //this is a list of arbitrary strings so that we check the geometry + QStringList list = QStringList() << "trer" << "ezrfgtgvqd" << "sdgzgzerzerzer" << "eerzertz" << "er"; + foreach(QString str, list) + menu.addAction(str); + + int left, top, right, bottom; + menu.getContentsMargins(&left, &top, &right, &bottom); + const int panelWidth = menu.style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, &menu); + const int hmargin = menu.style()->pixelMetric(QStyle::PM_MenuHMargin, 0, &menu), + vmargin = menu.style()->pixelMetric(QStyle::PM_MenuVMargin, 0, &menu); + + int maxWidth =0; + QRect result; + foreach(QAction *action, menu.actions()) { + maxWidth = qMax(maxWidth, menu.actionGeometry(action).width()); + result |= menu.actionGeometry(action); + QCOMPARE(result.x(), left + hmargin + panelWidth); + QCOMPARE(result.y(), top + vmargin + panelWidth); + } + + QStyleOption opt(0); + opt.rect = menu.rect(); + opt.state = QStyle::State_None; + + QSize resSize = QSize(result.x(), result.y()) + result.size() + QSize(hmargin + right + panelWidth, vmargin + top + panelWidth); + + resSize = menu.style()->sizeFromContents(QStyle::CT_Menu, &opt, + resSize.expandedTo(QApplication::globalStrut()), &menu); + + QCOMPARE(resSize, menu.sizeHint()); +} + QTEST_MAIN(tst_QMenu) #include "tst_qmenu.moc" diff --git a/tests/auto/qmenubar/tst_qmenubar.cpp b/tests/auto/qmenubar/tst_qmenubar.cpp index bbf02d2..197d6f0 100644 --- a/tests/auto/qmenubar/tst_qmenubar.cpp +++ b/tests/auto/qmenubar/tst_qmenubar.cpp @@ -48,8 +48,10 @@ #include <q3popupmenu.h> #endif #include <qstyle.h> +#include <qwindowsstyle.h> #include <qdesktopwidget.h> #include <qaction.h> +#include <qstyleoption.h> #ifdef Q_WS_WIN #include <windows.h> @@ -149,6 +151,8 @@ private slots: void check_shortcutPress(); void check_menuPosition(); void task223138_triggered(); + void task256322_highlight(); + void menubarSizeHint(); #if defined(QT3_SUPPORT) void indexBasedInsertion_data(); @@ -1524,6 +1528,107 @@ void tst_QMenuBar::task223138_triggered() QCOMPARE(submenuSpy.count(), 1); } +void tst_QMenuBar::task256322_highlight() +{ + QMainWindow win; + QMenu menu; + QAction *file = win.menuBar()->addMenu(&menu); + file->setText("file"); + QMenu menu2; + QAction *file2 = win.menuBar()->addMenu(&menu2); + file2->setText("file2"); + QAction *nothing = win.menuBar()->addAction("nothing"); + + win.show(); + + QTest::mouseClick(win.menuBar(), Qt::LeftButton, 0, win.menuBar()->actionGeometry(file).center()); + QVERIFY(menu.isVisible()); + QVERIFY(!menu2.isVisible()); + QCOMPARE(win.menuBar()->activeAction(), file); + + QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(file2).center()); + QVERIFY(!menu.isVisible()); + QVERIFY(menu2.isVisible()); + QCOMPARE(win.menuBar()->activeAction(), file2); + + QTest::mouseMove(win.menuBar(), win.menuBar()->actionGeometry(nothing).center()); + QVERIFY(!menu.isVisible()); + QVERIFY(!menu2.isVisible()); + QCOMPARE(win.menuBar()->activeAction(), nothing); + + QTest::mouseMove(&win, win.menuBar()->geometry().bottomLeft() + QPoint(1,1)); + + QVERIFY(!menu.isVisible()); + QVERIFY(!menu2.isVisible()); + QVERIFY(!win.menuBar()->activeAction()); +} + +void tst_QMenuBar::menubarSizeHint() +{ + struct MyStyle : public QWindowsStyle + { + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const + { + // I chose strange values (prime numbers to be more sure that the size of the menubar is correct) + switch (metric) + { + case QStyle::PM_MenuBarItemSpacing: + return 7; + case PM_MenuBarHMargin: + return 13; + case PM_MenuBarVMargin: + return 11; + case PM_MenuBarPanelWidth: + return 1; + } + return QWindowsStyle::pixelMetric(metric, option, widget); + } + } style; + + QMenuBar mb; + mb.setStyle(&style); + //this is a list of arbitrary strings so that we check the geometry + QStringList list = QStringList() << "trer" << "ezrfgtgvqd" << "sdgzgzerzerzer" << "eerzertz" << "er"; + foreach(QString str, list) + mb.addAction(str); + + const int panelWidth = style.pixelMetric(QStyle::PM_MenuBarPanelWidth); + const int hmargin = style.pixelMetric(QStyle::PM_MenuBarHMargin); + const int vmargin = style.pixelMetric(QStyle::PM_MenuBarVMargin); + const int spacing = style.pixelMetric(QStyle::PM_MenuBarItemSpacing); + + mb.show(); + QRect result; + foreach(QAction *action, mb.actions()) { + const QRect actionRect = mb.actionGeometry(action); + if (!result.isNull()) //this is the first item + QCOMPARE(actionRect.left() - result.right() - 1, spacing); + result |= actionRect; + QCOMPARE(result.x(), panelWidth + hmargin + spacing); + QCOMPARE(result.y(), panelWidth + vmargin); + } + + //this code is copied from QMenuBar + //there is no public member that allows to initialize a styleoption instance + QStyleOptionMenuItem opt; + opt.rect = mb.rect(); + opt.menuRect = mb.rect(); + opt.state = QStyle::State_None; + opt.menuItemType = QStyleOptionMenuItem::Normal; + opt.checkType = QStyleOptionMenuItem::NotCheckable; + opt.palette = mb.palette(); + + QSize resSize = QSize(result.x(), result.y()) + result.size() + + QSize(panelWidth + hmargin, panelWidth + vmargin); + + + resSize = style.sizeFromContents(QStyle::CT_MenuBar, &opt, + resSize.expandedTo(QApplication::globalStrut()), + &mb); + + QCOMPARE(resSize, mb.sizeHint()); +} + #if defined(QT3_SUPPORT) void tst_QMenuBar::indexBasedInsertion_data() diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp index 7735653..305eca6 100644 --- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp @@ -52,6 +52,7 @@ #include <QtNetwork/QTcpSocket> #include <QtNetwork/QLocalSocket> #include <QtNetwork/QLocalServer> +#include <QtNetwork/QHostInfo> #include <QtNetwork/QFtp> #include <QtNetwork/qauthenticator.h> #include <QtNetwork/qnetworkaccessmanager.h> @@ -99,6 +100,15 @@ class tst_QNetworkReply: public QObject { Q_OBJECT + struct ProxyData { + ProxyData(const QNetworkProxy &p, const QByteArray &t, bool auth) + : tag(t), proxy(p), requiresAuthentication(auth) + { } + QByteArray tag; + QNetworkProxy proxy; + bool requiresAuthentication; + }; + QEventLoop *loop; enum RunSimpleRequestReturn { Timeout = 0, Success, Failure }; int returnCode; @@ -107,6 +117,7 @@ class tst_QNetworkReply: public QObject QString wronlyFileName; #endif QString uniqueExtension; + QList<ProxyData> proxies; QNetworkAccessManager manager; MyCookieJar *cookieJar; #ifndef QT_NO_OPENSSL @@ -159,6 +170,10 @@ private Q_SLOTS: void putToHttp(); void postToHttp_data(); void postToHttp(); + void deleteFromHttp_data(); + void deleteFromHttp(); + void putGetDeleteGetFromHttp_data(); + void putGetDeleteGetFromHttp(); void ioGetFromData_data(); void ioGetFromData(); @@ -201,11 +216,20 @@ private Q_SLOTS: void ioPutToHttpFromFile(); void ioPostToHttpFromFile_data(); void ioPostToHttpFromFile(); + void ioPostToHttpFromSocket_data(); + void ioPostToHttpFromSocket(); + void ioPostToHttpFromMiddleOfFileToEnd(); + void ioPostToHttpFromMiddleOfFileFiveBytes(); + void ioPostToHttpFromMiddleOfQBufferFiveBytes(); + void ioPostToHttpNoBufferFlag(); + void ioPostToHttpUploadProgress(); + void ioPostToHttpEmtpyUploadProgress(); void rateControl_data(); void rateControl(); void downloadPerformance(); void uploadPerformance(); + void httpUploadPerformance(); void performanceControlRate(); void downloadProgress_data(); @@ -228,6 +252,8 @@ private Q_SLOTS: void proxyChange(); void authorizationError_data(); void authorizationError(); + + void httpConnectionCount(); }; QT_BEGIN_NAMESPACE @@ -377,6 +403,63 @@ public slots: } }; +class FixedSizeDataGenerator : public QIODevice +{ + Q_OBJECT + enum { Idle, Started, Stopped } state; +public: + FixedSizeDataGenerator(qint64 size) : state(Idle) + { open(ReadOnly | Unbuffered); + toBeGeneratedTotalCount = toBeGeneratedCount = size; + } + + virtual qint64 bytesAvailable() const + { + return state == Started ? toBeGeneratedCount + QIODevice::bytesAvailable() : 0; + } + + virtual bool isSequential() const{ + return false; + } + + virtual bool reset() const{ + return false; + } + + qint64 size() const { + return toBeGeneratedTotalCount; + } + +public slots: + void start() { state = Started; emit readyRead(); } + +protected: + virtual qint64 readData(char *data, qint64 maxlen) + { + memset(data, '@', maxlen); + + if (toBeGeneratedCount <= 0) { + return -1; + } + + qint64 n = qMin(maxlen, toBeGeneratedCount); + toBeGeneratedCount -= n; + + if (toBeGeneratedCount <= 0) { + // make sure this is a queued connection! + emit readChannelFinished(); + } + + return n; + } + virtual qint64 writeData(const char *, qint64) + { return -1; } + + qint64 toBeGeneratedCount; + qint64 toBeGeneratedTotalCount; +}; + + class DataGenerator: public QIODevice { Q_OBJECT @@ -397,6 +480,7 @@ protected: { if (state == Stopped) return -1; // EOF + // return as many bytes as are wanted memset(data, '@', maxlen); return maxlen; @@ -405,6 +489,8 @@ protected: { return -1; } }; + + class SocketPair: public QObject { Q_OBJECT @@ -650,7 +736,7 @@ protected: return; transferRate = totalBytes * 1000 / timer.elapsed(); - qDebug() << "receive rate:" << (transferRate / 1024) << "kB/s in" + qDebug() << "TimedSender::run" << "receive rate:" << (transferRate / 1024) << "kB/s in" << timer.elapsed() << "ms"; } @@ -664,12 +750,13 @@ protected: class ThreadedDataReader: public QThread { Q_OBJECT + // used to make the constructor only return after the tcp server started listening QSemaphore ready; QTcpSocket *client; int timeout; int port; public: - int transferRate; + qint64 transferRate; ThreadedDataReader() : port(-1), transferRate(-1) { @@ -697,12 +784,65 @@ protected: QTime timer; timer.start(); eventLoop.exec(); + qint64 elapsed = timer.elapsed(); + + transferRate = reader.totalBytes * 1000 / elapsed; + qDebug() << "ThreadedDataReader::run" << "send rate:" << (transferRate / 1024) << "kB/s in" << elapsed << "msec"; + } +}; + +class ThreadedDataReaderHttpServer: public QThread +{ + Q_OBJECT + // used to make the constructor only return after the tcp server started listening + QSemaphore ready; + QTcpSocket *client; + int timeout; + int port; +public: + qint64 transferRate; + ThreadedDataReaderHttpServer() + : port(-1), transferRate(-1) + { + start(); + ready.acquire(); + } + + inline int serverPort() const { return port; } + +protected: + void run() + { + QTcpServer server; + server.listen(); + port = server.serverPort(); + ready.release(); + + server.waitForNewConnection(-1); + client = server.nextPendingConnection(); + client->write("HTTP/1.0 200 OK\r\n"); + client->write("Content-length: 0\r\n"); + client->write("\r\n"); + client->flush(); + + QCoreApplication::processEvents(); + + QEventLoop eventLoop; + DataReader reader(client, false); + QObject::connect(client, SIGNAL(disconnected()), &eventLoop, SLOT(quit())); + + QTime timer; + timer.start(); + eventLoop.exec(); + qint64 elapsed = timer.elapsed(); - transferRate = reader.totalBytes * 1000 / timer.elapsed(); - qDebug() << "send rate:" << (transferRate / 1024) << "kB/s"; + transferRate = reader.totalBytes * 1000 / elapsed; + qDebug() << "ThreadedDataReaderHttpServer::run" << "send rate:" << (transferRate / 1024) << "kB/s in" << elapsed << "msec"; } }; + + tst_QNetworkReply::tst_QNetworkReply() { Q_SET_DEFAULT_IAP @@ -714,13 +854,32 @@ tst_QNetworkReply::tst_QNetworkReply() uniqueExtension = QString("%1%2").arg((qulonglong)this).arg(rand()); #endif cookieJar = new MyCookieJar; - manager.setCookieJar(cookieJar); + manager.setCookieJar(cookieJar); + + QHostInfo hostInfo = QHostInfo::fromName(QtNetworkSettings::serverName()); + + proxies << ProxyData(QNetworkProxy::NoProxy, "", false); + + if (hostInfo.error() == QHostInfo::NoError && !hostInfo.addresses().isEmpty()) { + QString proxyserver = hostInfo.addresses().first().toString(); + proxies << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3128), "+proxy", false) + << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3129), "+proxyauth", true) + // currently unsupported + // << ProxyData(QNetworkProxy(QNetworkProxy::HttpProxy, proxyserver, 3130), "+proxyauth-ntlm", true); + << ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1080), "+socks", false) + << ProxyData(QNetworkProxy(QNetworkProxy::Socks5Proxy, proxyserver, 1081), "+socksauth", true); + } else { + printf("==================================================================\n"); + printf("Proxy could not be looked up. No proxy will be used while testing!\n"); + printf("==================================================================\n"); + } } tst_QNetworkReply::~tst_QNetworkReply() { } + void tst_QNetworkReply::authenticationRequired(QNetworkReply*, QAuthenticator* auth) { auth->setUser("httptest"); @@ -772,6 +931,10 @@ QString tst_QNetworkReply::runSimpleRequest(QNetworkAccessManager::Operation op, reply = manager.post(request, data); break; + case QNetworkAccessManager::DeleteOperation: + reply = manager.deleteResource(request); + break; + default: Q_ASSERT_X(false, "tst_QNetworkReply", "Invalid/unknown operation requested"); } @@ -1347,6 +1510,97 @@ void tst_QNetworkReply::postToHttp() QCOMPARE(uploadedData, md5sum.toHex()); } +void tst_QNetworkReply::deleteFromHttp_data() +{ + QTest::addColumn<QUrl>("url"); + QTest::addColumn<int>("resultCode"); + QTest::addColumn<QNetworkReply::NetworkError>("error"); + + // for status codes to expect, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html + + QTest::newRow("405-method-not-allowed") << QUrl("http://" + QtNetworkSettings::serverName() + "/index.html") << 405 << QNetworkReply::ContentOperationNotPermittedError; + QTest::newRow("200-ok") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?200-ok") << 200 << QNetworkReply::NoError; + QTest::newRow("202-accepted") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?202-accepted") << 202 << QNetworkReply::NoError; + QTest::newRow("204-no-content") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?204-no-content") << 204 << QNetworkReply::NoError; + QTest::newRow("404-not-found") << QUrl("http://" + QtNetworkSettings::serverName() + "/cgi-bin/http-delete.cgi?404-not-found") << 404 << QNetworkReply::ContentNotFoundError; +} + +void tst_QNetworkReply::deleteFromHttp() +{ + QFETCH(QUrl, url); + QFETCH(int, resultCode); + QFETCH(QNetworkReply::NetworkError, error); + QNetworkRequest request(url); + QNetworkReplyPtr reply; + runSimpleRequest(QNetworkAccessManager::DeleteOperation, request, reply, 0); + QCOMPARE(reply->url(), url); + QCOMPARE(reply->error(), error); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), resultCode); +} + +void tst_QNetworkReply::putGetDeleteGetFromHttp_data() +{ + QTest::addColumn<QUrl>("putUrl"); + QTest::addColumn<int>("putResultCode"); + QTest::addColumn<QNetworkReply::NetworkError>("putError"); + QTest::addColumn<QUrl>("deleteUrl"); + QTest::addColumn<int>("deleteResultCode"); + QTest::addColumn<QNetworkReply::NetworkError>("deleteError"); + QTest::addColumn<QUrl>("get2Url"); + QTest::addColumn<int>("get2ResultCode"); + QTest::addColumn<QNetworkReply::NetworkError>("get2Error"); + + QUrl url("http://" + QtNetworkSettings::serverName()); + url.setPath(QString("/dav/qnetworkaccess-putToHttp-%1-%2") + .arg(QTest::currentDataTag()) + .arg(uniqueExtension)); + + // first use case: put, get (to check it is there), delete, get (to check it is not there anymore) + QTest::newRow("success") << url << 201 << QNetworkReply::NoError << url << 204 << QNetworkReply::NoError << url << 404 << QNetworkReply::ContentNotFoundError; + + QUrl wrongUrl("http://" + QtNetworkSettings::serverName()); + wrongUrl.setPath(QString("/dav/qnetworkaccess-thisURLisNotAvailable")); + + // second use case: put, get (to check it is there), delete wrong URL, get (to check it is still there) + QTest::newRow("delete-error") << url << 201 << QNetworkReply::NoError << wrongUrl << 404 << QNetworkReply::ContentNotFoundError << url << 200 << QNetworkReply::NoError; + +} + +void tst_QNetworkReply::putGetDeleteGetFromHttp() +{ + QFETCH(QUrl, putUrl); + QFETCH(int, putResultCode); + QFETCH(QNetworkReply::NetworkError, putError); + QFETCH(QUrl, deleteUrl); + QFETCH(int, deleteResultCode); + QFETCH(QNetworkReply::NetworkError, deleteError); + QFETCH(QUrl, get2Url); + QFETCH(int, get2ResultCode); + QFETCH(QNetworkReply::NetworkError, get2Error); + + QNetworkRequest putRequest(putUrl); + QNetworkRequest deleteRequest(deleteUrl); + QNetworkRequest get2Request(get2Url); + QNetworkReplyPtr reply; + + RUN_REQUEST(runSimpleRequest(QNetworkAccessManager::PutOperation, putRequest, reply, 0)); + QCOMPARE(reply->error(), putError); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), putResultCode); + + runSimpleRequest(QNetworkAccessManager::GetOperation, putRequest, reply, 0); + QCOMPARE(reply->error(), QNetworkReply::NoError); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); + + runSimpleRequest(QNetworkAccessManager::DeleteOperation, deleteRequest, reply, 0); + QCOMPARE(reply->error(), deleteError); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), deleteResultCode); + + runSimpleRequest(QNetworkAccessManager::GetOperation, get2Request, reply, 0); + QCOMPARE(reply->error(), get2Error); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), get2ResultCode); + +} + void tst_QNetworkReply::ioGetFromData_data() { QTest::addColumn<QString>("urlStr"); @@ -2552,6 +2806,320 @@ void tst_QNetworkReply::ioPostToHttpFromFile() QCOMPARE(reply->readAll().trimmed(), md5sum(sourceFile.readAll()).toHex()); } +void tst_QNetworkReply::ioPostToHttpFromSocket_data() +{ + QTest::addColumn<QByteArray>("data"); + QTest::addColumn<QByteArray>("md5sum"); + QTest::addColumn<QUrl>("url"); + QTest::addColumn<QNetworkProxy>("proxy"); + QTest::addColumn<int>("authenticationRequiredCount"); + QTest::addColumn<int>("proxyAuthenticationRequiredCount"); + + for (int i = 0; i < proxies.count(); ++i) + for (int auth = 0; auth < 2; ++auth) { + QUrl url; + if (auth) + url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; + else + url = "http://" + QtNetworkSettings::serverName() + "/qtest/cgi-bin/md5sum.cgi"; + + QNetworkProxy proxy = proxies.at(i).proxy; + QByteArray testsuffix = QByteArray(auth ? "+auth" : "") + proxies.at(i).tag; + int proxyauthcount = proxies.at(i).requiresAuthentication; + + QByteArray data; + data = ""; + QTest::newRow("empty" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + + data = "This is a normal message."; + QTest::newRow("generic" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + + data = "This is a message to show that Qt rocks!\r\n\n"; + QTest::newRow("small" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + + data = QByteArray("abcd\0\1\2\abcd",12); + QTest::newRow("with-nul" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + + data = QByteArray(4097, '\4'); + QTest::newRow("4k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + + data = QByteArray(128*1024+1, '\177'); + QTest::newRow("128k+1" + testsuffix) << data << md5sum(data) << url << proxy << auth << proxyauthcount; + } +} + +void tst_QNetworkReply::ioPostToHttpFromSocket() +{ + qRegisterMetaType<QNetworkProxy>(); // for QSignalSpy + qRegisterMetaType<QAuthenticator *>(); + qRegisterMetaType<QNetworkReply *>(); + + QFETCH(QByteArray, data); + QFETCH(QUrl, url); + QFETCH(QNetworkProxy, proxy); + SocketPair socketpair; + socketpair.create(); + QVERIFY(socketpair.endPoints[0] && socketpair.endPoints[1]); + + socketpair.endPoints[0]->write(data); + + QNetworkRequest request(url); + manager.setProxy(proxy); + QNetworkReplyPtr reply = manager.post(QNetworkRequest(url), socketpair.endPoints[1]); + socketpair.endPoints[0]->close(); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), + SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); + connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + QSignalSpy authenticationRequiredSpy(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*))); + QSignalSpy proxyAuthenticationRequiredSpy(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); + + QTestEventLoop::instance().enterLoop(1); + + disconnect(&manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), + this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); + disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + QVERIFY(!QTestEventLoop::instance().timeout()); + + QCOMPARE(reply->url(), url); + QCOMPARE(reply->error(), QNetworkReply::NoError); + // verify that the HTTP status code is 200 Ok + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200); + + QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); + + QTEST(authenticationRequiredSpy.count(), "authenticationRequiredCount"); + QTEST(proxyAuthenticationRequiredSpy.count(), "proxyAuthenticationRequiredCount"); + } + +// this tests checks if rewinding the POST-data to some place in the middle +// worked. +void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileToEnd() +{ + QFile sourceFile(SRCDIR "/rfc3252.txt"); + QVERIFY(sourceFile.open(QIODevice::ReadOnly)); + // seeking to the middle + sourceFile.seek(sourceFile.size() / 2); + + QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; + QNetworkRequest request(url); + QNetworkReplyPtr reply = manager.post(QNetworkRequest(url), &sourceFile); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + QTestEventLoop::instance().enterLoop(2); + disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + QVERIFY(!QTestEventLoop::instance().timeout()); + + // compare half data + sourceFile.seek(sourceFile.size() / 2); + QByteArray data = sourceFile.readAll(); + QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); +} + +void tst_QNetworkReply::ioPostToHttpFromMiddleOfFileFiveBytes() +{ + QFile sourceFile(SRCDIR "/rfc3252.txt"); + QVERIFY(sourceFile.open(QIODevice::ReadOnly)); + // seeking to the middle + sourceFile.seek(sourceFile.size() / 2); + + QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; + QNetworkRequest request(url); + // only send 5 bytes + request.setHeader(QNetworkRequest::ContentLengthHeader, 5); + QVERIFY(request.header(QNetworkRequest::ContentLengthHeader).isValid()); + QNetworkReplyPtr reply = manager.post(request, &sourceFile); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + QTestEventLoop::instance().enterLoop(2); + disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + QVERIFY(!QTestEventLoop::instance().timeout()); + + // compare half data + sourceFile.seek(sourceFile.size() / 2); + QByteArray data = sourceFile.read(5); + QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); +} + +void tst_QNetworkReply::ioPostToHttpFromMiddleOfQBufferFiveBytes() +{ + // test needed since a QBuffer goes with a different codepath than the QFile + // tested in ioPostToHttpFromMiddleOfFileFiveBytes + QBuffer uploadBuffer; + uploadBuffer.open(QIODevice::ReadWrite); + uploadBuffer.write("1234567890"); + uploadBuffer.seek(5); + + QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; + QNetworkRequest request(url); + QNetworkReplyPtr reply = manager.post(request, &uploadBuffer); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + QTestEventLoop::instance().enterLoop(2); + disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + QVERIFY(!QTestEventLoop::instance().timeout()); + + // compare half data + uploadBuffer.seek(5); + QByteArray data = uploadBuffer.read(5); + QCOMPARE(reply->readAll().trimmed(), md5sum(data).toHex()); +} + + +void tst_QNetworkReply::ioPostToHttpNoBufferFlag() +{ + QByteArray data = QByteArray("daaaaaaataaaaaaa"); + // create a sequential QIODevice by feeding the data into a local TCP server + SocketPair socketpair; + socketpair.create(); + QVERIFY(socketpair.endPoints[0] && socketpair.endPoints[1]); + socketpair.endPoints[0]->write(data); + + QUrl url = "http://" + QtNetworkSettings::serverName() + "/qtest/protected/cgi-bin/md5sum.cgi"; + QNetworkRequest request(url); + // disallow buffering + request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true); + request.setHeader(QNetworkRequest::ContentLengthHeader, data.size()); + QNetworkReplyPtr reply = manager.post(request, socketpair.endPoints[1]); + socketpair.endPoints[0]->close(); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + QTestEventLoop::instance().enterLoop(2); + disconnect(&manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), + this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*))); + + // verify: error code is QNetworkReply::ContentReSendError + QCOMPARE(reply->error(), QNetworkReply::ContentReSendError); +} + + +void tst_QNetworkReply::ioPostToHttpUploadProgress() +{ + QFile sourceFile(SRCDIR "/bigfile"); + QVERIFY(sourceFile.open(QIODevice::ReadOnly)); + + // emulate a minimal http server + QTcpServer server; + server.listen(QHostAddress(QHostAddress::LocalHost), 0); + + // create the request + QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort())); + QNetworkRequest request(url); + QNetworkReplyPtr reply = manager.post(request, &sourceFile); + QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); + connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); + + // get the request started and the incoming socket connected + QTestEventLoop::instance().enterLoop(10); + QVERIFY(!QTestEventLoop::instance().timeout()); + QTcpSocket *incomingSocket = server.nextPendingConnection(); + QVERIFY(incomingSocket); + disconnect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); + + incomingSocket->setReadBufferSize(1*1024); + QTestEventLoop::instance().enterLoop(2); + // some progress should have been made + QList<QVariant> args = spy.last(); + QVERIFY(!args.isEmpty()); + QVERIFY(args.at(0).toLongLong() > 0); + + incomingSocket->setReadBufferSize(32*1024); + incomingSocket->read(16*1024); + QTestEventLoop::instance().enterLoop(2); + // some more progress than before + QList<QVariant> args2 = spy.last(); + QVERIFY(!args2.isEmpty()); + QVERIFY(args2.at(0).toLongLong() > args.at(0).toLongLong()); + + // set the read buffer to unlimited + incomingSocket->setReadBufferSize(0); + QTestEventLoop::instance().enterLoop(10); + // progress should be finished + QList<QVariant> args3 = spy.last(); + QVERIFY(!args3.isEmpty()); + QVERIFY(args3.at(0).toLongLong() > args2.at(0).toLongLong()); + QCOMPARE(args3.at(0).toLongLong(), args3.at(1).toLongLong()); + QCOMPARE(args3.at(0).toLongLong(), sourceFile.size()); + + // after sending this, the QNAM should emit finished() + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + incomingSocket->write("HTTP/1.0 200 OK\r\n"); + incomingSocket->write("Content-Length: 0\r\n"); + incomingSocket->write("\r\n"); + QTestEventLoop::instance().enterLoop(10); + // not timeouted -> finished() was emitted + QVERIFY(!QTestEventLoop::instance().timeout()); + + incomingSocket->close(); + server.close(); +} + +void tst_QNetworkReply::ioPostToHttpEmtpyUploadProgress() +{ + QByteArray ba; + ba.resize(0); + QBuffer buffer(&ba,0); + QVERIFY(buffer.open(QIODevice::ReadOnly)); + + // emulate a minimal http server + QTcpServer server; + server.listen(QHostAddress(QHostAddress::LocalHost), 0); + + // create the request + QUrl url = QUrl(QString("http://127.0.0.1:%1/").arg(server.serverPort())); + QNetworkRequest request(url); + QNetworkReplyPtr reply = manager.post(request, &buffer); + QSignalSpy spy(reply, SIGNAL(uploadProgress(qint64,qint64))); + connect(&server, SIGNAL(newConnection()), &QTestEventLoop::instance(), SLOT(exitLoop())); + + + // get the request started and the incoming socket connected + QTestEventLoop::instance().enterLoop(10); + QVERIFY(!QTestEventLoop::instance().timeout()); + QTcpSocket *incomingSocket = server.nextPendingConnection(); + QVERIFY(incomingSocket); + + // after sending this, the QNAM should emit finished() + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + incomingSocket->write("HTTP/1.0 200 OK\r\n"); + incomingSocket->write("Content-Length: 0\r\n"); + incomingSocket->write("\r\n"); + incomingSocket->flush(); + QTestEventLoop::instance().enterLoop(10); + // not timeouted -> finished() was emitted + QVERIFY(!QTestEventLoop::instance().timeout()); + + // final check: only 1 uploadProgress has been emitted + QVERIFY(spy.length() == 1); + QList<QVariant> args = spy.last(); + QVERIFY(!args.isEmpty()); + QCOMPARE(args.at(0).toLongLong(), buffer.size()); + QCOMPARE(args.at(0).toLongLong(), buffer.size()); + + incomingSocket->close(); + server.close(); +} + + void tst_QNetworkReply::rateControl_data() { QTest::addColumn<int>("rate"); @@ -2588,8 +3156,8 @@ void tst_QNetworkReply::rateControl() QTestEventLoop::instance().enterLoop(40); int elapsedTime = loopTime.elapsed(); - qDebug() << "send rate:" << sender.transferRate; - qDebug() << "receive rate:" << reader.totalBytesRead * 1000 / elapsedTime + qDebug() << "tst_QNetworkReply::rateControl" << "send rate:" << sender.transferRate; + qDebug() << "tst_QNetworkReply::rateControl" << "receive rate:" << reader.totalBytesRead * 1000 / elapsedTime << "(it received" << reader.totalBytesRead << "bytes in" << elapsedTime << "ms)"; sender.wait(); @@ -2622,23 +3190,54 @@ void tst_QNetworkReply::downloadPerformance() sender.wait(); qint64 receivedBytes = reader.totalBytes; - qDebug() << "receive rate:" << (receivedBytes * 1000 / elapsedTime / 1024) << "kB/s and" + qDebug() << "tst_QNetworkReply::downloadPerformance" << "receive rate:" << (receivedBytes * 1000 / elapsedTime / 1024) << "kB/s and" << elapsedTime << "ms"; } void tst_QNetworkReply::uploadPerformance() { - ThreadedDataReader reader; - DataGenerator generator; - QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(reader.serverPort()) + "/?bare=1"); - QNetworkReplyPtr reply = manager.put(request, &generator); + ThreadedDataReader reader; + DataGenerator generator; - connect(&reader, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); - QTimer::singleShot(5000, &generator, SLOT(stop())); - generator.start(); - QTestEventLoop::instance().enterLoop(40); + + QNetworkRequest request("debugpipe://127.0.0.1:" + QString::number(reader.serverPort()) + "/?bare=1"); + QNetworkReplyPtr reply = manager.put(request, &generator); + generator.start(); + connect(&reader, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + QTimer::singleShot(5000, &generator, SLOT(stop())); + + QTestEventLoop::instance().enterLoop(10); + QVERIFY(!QTestEventLoop::instance().timeout()); } +void tst_QNetworkReply::httpUploadPerformance() +{ + enum {UploadSize = 1000*1024*1024}; // 1000 MB + ThreadedDataReaderHttpServer reader; + FixedSizeDataGenerator generator(UploadSize); + + QNetworkRequest request(QUrl("http://127.0.0.1:" + QString::number(reader.serverPort()) + "/?bare=1")); + request.setHeader(QNetworkRequest::ContentLengthHeader,UploadSize); + + QNetworkReplyPtr reply = manager.put(request, &generator); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + + QTime time; + generator.start(); + time.start(); + QTestEventLoop::instance().enterLoop(40); + QVERIFY(!QTestEventLoop::instance().timeout()); + + qint64 elapsed = time.elapsed(); + qWarning() << "tst_QNetworkReply::httpUploadPerformance" << elapsed << "msec, " + << ((UploadSize/1024.0)/(elapsed/1000.0)) << " kB/sec"; + + reader.exit(); + reader.wait(3000); +} + + void tst_QNetworkReply::performanceControlRate() { // this is a control comparison for the other two above @@ -2659,7 +3258,7 @@ void tst_QNetworkReply::performanceControlRate() sender.wait(); qint64 receivedBytes = reader.totalBytes; - qDebug() << "receive rate:" << (receivedBytes * 1000 / elapsedTime / 1024) << "kB/s and" + qDebug() << "tst_QNetworkReply::performanceControlRate" << "receive rate:" << (receivedBytes * 1000 / elapsedTime / 1024) << "kB/s and" << elapsedTime << "ms"; } @@ -2696,6 +3295,7 @@ void tst_QNetworkReply::downloadProgress() QByteArray data(128, 'a'); QTcpSocket *sender = server.nextPendingConnection(); + QVERIFY(sender); QFETCH(int, loopCount); for (int i = 1; i <= loopCount; ++i) { @@ -3137,5 +3737,34 @@ void tst_QNetworkReply::authorizationError() QCOMPARE(QString(reply->readAll()), httpBody); } +void tst_QNetworkReply::httpConnectionCount() +{ + QTcpServer server; + QVERIFY(server.listen()); + QCoreApplication::instance()->processEvents(); + + for (int i = 0; i < 10; i++) { + QNetworkRequest request (QUrl("http://127.0.0.1:" + QString::number(server.serverPort()) + "/" + QString::number(i))); + QNetworkReply* reply = manager.get(request); + reply->setParent(this); + } + + int pendingConnectionCount = 0; + QTime time; + time.start(); + + while(pendingConnectionCount != 6) { + QCoreApplication::instance()->processEvents(); + while (server.nextPendingConnection()) + pendingConnectionCount++; + + // at max. wait 10 sec + if (time.elapsed() > 10000) + break; + } + + QCOMPARE(pendingConnectionCount, 6); +} + QTEST_MAIN(tst_QNetworkReply) #include "tst_qnetworkreply.moc" diff --git a/tests/auto/qobject/tst_qobject.cpp b/tests/auto/qobject/tst_qobject.cpp index 5a81310..3276dee 100644 --- a/tests/auto/qobject/tst_qobject.cpp +++ b/tests/auto/qobject/tst_qobject.cpp @@ -106,6 +106,7 @@ private slots: void childDeletesItsSibling(); void dynamicProperties(); void floatProperty(); + void qrealProperty(); void property(); void recursiveSignalEmission(); void blockingQueuedConnection(); @@ -116,6 +117,7 @@ private slots: void dumpObjectInfo(); void connectToSender(); void qobjectConstCast(); + void uniqConnection(); protected: }; @@ -204,12 +206,20 @@ public: sequence_slot3 = 0; sequence_slot2 = 0; sequence_slot1 = 0; + count_slot1 = 0; + count_slot2 = 0; + count_slot3 = 0; + count_slot4 = 0; } int sequence_slot1; int sequence_slot2; int sequence_slot3; int sequence_slot4; + int count_slot1; + int count_slot2; + int count_slot3; + int count_slot4; bool called(int slot) { switch (slot) { @@ -224,10 +234,10 @@ public: static int sequence; public slots: - void slot1() { sequence_slot1 = ++sequence; } - void slot2() { sequence_slot2 = ++sequence; } - void slot3() { sequence_slot3 = ++sequence; } - void slot4() { sequence_slot4 = ++sequence; } + void slot1() { sequence_slot1 = ++sequence; count_slot1++; } + void slot2() { sequence_slot2 = ++sequence; count_slot2++; } + void slot3() { sequence_slot3 = ++sequence; count_slot3++; } + void slot4() { sequence_slot4 = ++sequence; count_slot4++; } }; @@ -1104,6 +1114,7 @@ class PropertyObject : public QObject Q_PROPERTY(QVariant variant READ variant WRITE setVariant) Q_PROPERTY(CustomType* custom READ custom WRITE setCustom) Q_PROPERTY(float myFloat READ myFloat WRITE setMyFloat) + Q_PROPERTY(qreal myQReal READ myQReal WRITE setMyQReal) public: enum Alpha { @@ -1139,6 +1150,9 @@ public: void setMyFloat(float value) { m_float = value; } inline float myFloat() const { return m_float; } + void setMyQReal(qreal value) { m_qreal = value; } + qreal myQReal() const { return m_qreal; } + private: Alpha m_alpha; Priority m_priority; @@ -1147,6 +1161,7 @@ private: QVariant m_variant; CustomType *m_custom; float m_float; + qreal m_qreal; }; Q_DECLARE_METATYPE(PropertyObject::Priority) @@ -2349,6 +2364,27 @@ void tst_QObject::floatProperty() QVERIFY(qVariantValue<float>(v) == 128.0f); } +void tst_QObject::qrealProperty() +{ + PropertyObject obj; + const int idx = obj.metaObject()->indexOfProperty("myQReal"); + QVERIFY(idx > 0); + QMetaProperty prop = obj.metaObject()->property(idx); + QVERIFY(prop.isValid()); + QVERIFY(prop.type() == uint(QMetaType::type("qreal"))); + QVERIFY(!prop.write(&obj, QVariant("Hello"))); + + QVERIFY(prop.write(&obj, qVariantFromValue(128.0f))); + QVariant v = prop.read(&obj); + QCOMPARE(v.userType(), qMetaTypeId<qreal>()); + QVERIFY(qVariantValue<qreal>(v) == 128.0); + + QVERIFY(prop.write(&obj, qVariantFromValue(double(127)))); + v = prop.read(&obj); + QCOMPARE(v.userType(), qMetaTypeId<qreal>()); + QVERIFY(qVariantValue<qreal>(v) == 127.0); +} + class DynamicPropertyObject : public PropertyObject { public: @@ -2796,5 +2832,69 @@ void tst_QObject::qobjectConstCast() QVERIFY(qobject_cast<const FooObject *>(cptr)); } +void tst_QObject::uniqConnection() +{ + SenderObject *s = new SenderObject; + ReceiverObject *r1 = new ReceiverObject; + ReceiverObject *r2 = new ReceiverObject; + r1->reset(); + r2->reset(); + ReceiverObject::sequence = 0; + + QVERIFY( connect( s, SIGNAL( signal1() ), r1, SLOT( slot1() ) , Qt::UniqueConnection) ); + QVERIFY( connect( s, SIGNAL( signal1() ), r2, SLOT( slot1() ) , Qt::UniqueConnection) ); + QVERIFY( connect( s, SIGNAL( signal1() ), r1, SLOT( slot3() ) , Qt::UniqueConnection) ); + QVERIFY( connect( s, SIGNAL( signal3() ), r1, SLOT( slot3() ) , Qt::UniqueConnection) ); + + s->emitSignal1(); + s->emitSignal2(); + s->emitSignal3(); + s->emitSignal4(); + + QCOMPARE( r1->count_slot1, 1 ); + QCOMPARE( r1->count_slot2, 0 ); + QCOMPARE( r1->count_slot3, 2 ); + QCOMPARE( r1->count_slot4, 0 ); + QCOMPARE( r2->count_slot1, 1 ); + QCOMPARE( r2->count_slot2, 0 ); + QCOMPARE( r2->count_slot3, 0 ); + QCOMPARE( r2->count_slot4, 0 ); + QCOMPARE( r1->sequence_slot1, 1 ); + QCOMPARE( r2->sequence_slot1, 2 ); + QCOMPARE( r1->sequence_slot3, 4 ); + + r1->reset(); + r2->reset(); + ReceiverObject::sequence = 0; + + QVERIFY( connect( s, SIGNAL( signal4() ), r1, SLOT( slot4() ) , Qt::UniqueConnection) ); + QVERIFY( connect( s, SIGNAL( signal4() ), r2, SLOT( slot4() ) , Qt::UniqueConnection) ); + QVERIFY(!connect( s, SIGNAL( signal4() ), r2, SLOT( slot4() ) , Qt::UniqueConnection) ); + QVERIFY( connect( s, SIGNAL( signal1() ), r2, SLOT( slot4() ) , Qt::UniqueConnection) ); + QVERIFY(!connect( s, SIGNAL( signal4() ), r1, SLOT( slot4() ) , Qt::UniqueConnection) ); + + s->emitSignal4(); + QCOMPARE( r1->count_slot4, 1 ); + QCOMPARE( r2->count_slot4, 1 ); + QCOMPARE( r1->sequence_slot4, 1 ); + QCOMPARE( r2->sequence_slot4, 2 ); + + r1->reset(); + r2->reset(); + ReceiverObject::sequence = 0; + + connect( s, SIGNAL( signal4() ), r1, SLOT( slot4() ) ); + + s->emitSignal4(); + QCOMPARE( r1->count_slot4, 2 ); + QCOMPARE( r2->count_slot4, 1 ); + QCOMPARE( r1->sequence_slot4, 3 ); + QCOMPARE( r2->sequence_slot4, 2 ); + + delete s; + delete r1; + delete r2; +} + QTEST_MAIN(tst_QObject) #include "tst_qobject.moc" diff --git a/tests/auto/qobjectrace/tst_qobjectrace.cpp b/tests/auto/qobjectrace/tst_qobjectrace.cpp index a9b0d8c..b3052e0 100644 --- a/tests/auto/qobjectrace/tst_qobjectrace.cpp +++ b/tests/auto/qobjectrace/tst_qobjectrace.cpp @@ -43,6 +43,7 @@ #include <QtCore> #include <QtTest/QtTest> + enum { OneMinute = 60 * 1000, TwoMinutes = OneMinute * 2, TenMinutes = OneMinute * 10, @@ -73,12 +74,18 @@ public: public slots: void theSlot() { - enum { step = 1000 }; + enum { step = 35 }; if ((++count % step) == 0) { QThread *nextThread = threads.at((count / step) % threads.size()); moveToThread(nextThread); } } + + void destroSlot() { + emit theSignal(); + } +signals: + void theSignal(); }; class RaceThread : public QThread @@ -123,6 +130,10 @@ private slots: if (stopWatch.elapsed() >= OneMinute / 2) #endif quit(); + + QObject o; + connect(&o, SIGNAL(destroyed()) , object, SLOT(destroSlot())); + connect(object, SIGNAL(destroyed()) , &o, SLOT(deleteLater())); } }; @@ -146,11 +157,17 @@ void tst_QObjectRace::moveToThreadRace() for (int i = 0; i < ThreadCount; ++i) threads[i]->start(); - QVERIFY(threads[0]->wait(TwoMinutes)); - + + while(!threads[0]->isFinished()) { + QPointer<RaceObject> foo (object); + QObject o; + connect(&o, SIGNAL(destroyed()) , object, SLOT(destroSlot())); + connect(object, SIGNAL(destroyed()) , &o, SLOT(deleteLater())); + QTest::qWait(10); + } // the other threads should finish pretty quickly now for (int i = 1; i < ThreadCount; ++i) - QVERIFY(threads[i]->wait(30000)); + QVERIFY(threads[i]->wait(300)); for (int i = 0; i < ThreadCount; ++i) delete threads[i]; diff --git a/tests/auto/qpainter/tst_qpainter.cpp b/tests/auto/qpainter/tst_qpainter.cpp index 6ccb9f5..bbfa3e6 100644 --- a/tests/auto/qpainter/tst_qpainter.cpp +++ b/tests/auto/qpainter/tst_qpainter.cpp @@ -206,6 +206,7 @@ private slots: void drawImage_task217400_data(); void drawImage_task217400(); + void drawImage_1x1(); void drawRect_task215378(); void drawRect_task247505(); @@ -231,6 +232,7 @@ private slots: void extendedBlendModes(); void zeroOpacity(); + void clippingBug(); void emptyClip(); private: @@ -4198,6 +4200,29 @@ void tst_QPainter::zeroOpacity() QCOMPARE(target.pixel(0, 0), 0xff000000); } +void tst_QPainter::clippingBug() +{ + QImage img(32, 32, QImage::Format_ARGB32_Premultiplied); + img.fill(0); + + QImage expected = img; + QPainter p(&expected); + p.fillRect(1, 1, 30, 30, Qt::red); + p.end(); + + QPainterPath path; + path.addRect(1, 1, 30, 30); + path.addRect(1, 1, 30, 30); + path.addRect(1, 1, 30, 30); + + p.begin(&img); + p.setClipPath(path); + p.fillRect(0, 0, 32, 32, Qt::red); + p.end(); + + QCOMPARE(img, expected); +} + void tst_QPainter::emptyClip() { QImage img(64, 64, QImage::Format_ARGB32_Premultiplied); @@ -4216,5 +4241,25 @@ void tst_QPainter::emptyClip() p.fillPath(path, Qt::green); } +void tst_QPainter::drawImage_1x1() +{ + QImage source(1, 1, QImage::Format_ARGB32_Premultiplied); + source.fill(0xffffffff); + + QImage img(32, 32, QImage::Format_ARGB32_Premultiplied); + img.fill(0xff000000); + QPainter p(&img); + p.drawImage(QRectF(0.9, 0.9, 32, 32), source); + p.end(); + + QImage expected = img; + expected.fill(0xff000000); + p.begin(&expected); + p.fillRect(1, 1, 31, 31, Qt::white); + p.end(); + + QCOMPARE(img, expected); +} + QTEST_MAIN(tst_QPainter) #include "tst_qpainter.moc" diff --git a/tests/auto/qpainterpath/tst_qpainterpath.cpp b/tests/auto/qpainterpath/tst_qpainterpath.cpp index 0e7c000..4a8c4fc 100644 --- a/tests/auto/qpainterpath/tst_qpainterpath.cpp +++ b/tests/auto/qpainterpath/tst_qpainterpath.cpp @@ -107,6 +107,8 @@ private slots: void operators(); void connectPathDuplicatePoint(); + + void translate(); }; // Testing get/set functions @@ -1167,6 +1169,44 @@ void tst_QPainterPath::connectPathDuplicatePoint() QCOMPARE(c, a); } +void tst_QPainterPath::translate() +{ + QPainterPath path; + + // Path with no elements. + QCOMPARE(path.currentPosition(), QPointF()); + path.translate(50.5, 50.5); + QCOMPARE(path.currentPosition(), QPointF()); + QCOMPARE(path.translated(50.5, 50.5).currentPosition(), QPointF()); + + // path.isEmpty(), but we have one MoveTo element that should be translated. + path.moveTo(50, 50); + QCOMPARE(path.currentPosition(), QPointF(50, 50)); + path.translate(99.9, 99.9); + QCOMPARE(path.currentPosition(), QPointF(149.9, 149.9)); + path.translate(-99.9, -99.9); + QCOMPARE(path.currentPosition(), QPointF(50, 50)); + QCOMPARE(path.translated(-50, -50).currentPosition(), QPointF(0, 0)); + + // Complex path. + QRegion shape(100, 100, 300, 200, QRegion::Ellipse); + shape -= QRect(225, 175, 50, 50); + QPainterPath complexPath; + complexPath.addRegion(shape); + QVector<QPointF> untranslatedElements; + for (int i = 0; i < complexPath.elementCount(); ++i) + untranslatedElements.append(QPointF(complexPath.elementAt(i))); + + const QPainterPath untranslatedComplexPath(complexPath); + const QPointF offset(100, 100); + complexPath.translate(offset); + + for (int i = 0; i < complexPath.elementCount(); ++i) + QCOMPARE(QPointF(complexPath.elementAt(i)) - offset, untranslatedElements.at(i)); + + QCOMPARE(complexPath.translated(-offset), untranslatedComplexPath); +} + QTEST_APPLESS_MAIN(tst_QPainterPath) #include "tst_qpainterpath.moc" diff --git a/tests/auto/qparallelanimationgroup/qparallelanimationgroup.pro b/tests/auto/qparallelanimationgroup/qparallelanimationgroup.pro new file mode 100644 index 0000000..f2cacd3 --- /dev/null +++ b/tests/auto/qparallelanimationgroup/qparallelanimationgroup.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +QT = core gui +SOURCES += tst_qparallelanimationgroup.cpp + + diff --git a/tests/auto/qparallelanimationgroup/tst_qparallelanimationgroup.cpp b/tests/auto/qparallelanimationgroup/tst_qparallelanimationgroup.cpp new file mode 100644 index 0000000..d7d6b88 --- /dev/null +++ b/tests/auto/qparallelanimationgroup/tst_qparallelanimationgroup.cpp @@ -0,0 +1,832 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <QtCore/qparallelanimationgroup.h> + +//TESTED_CLASS=QParallelAnimationGroup +//TESTED_FILES= + +Q_DECLARE_METATYPE(QAbstractAnimation::State) + +class tst_QParallelAnimationGroup : public QObject +{ + Q_OBJECT +public: + tst_QParallelAnimationGroup(); + virtual ~tst_QParallelAnimationGroup(); + +public Q_SLOTS: + void init(); + void cleanup(); + +private slots: + void construction(); + void setCurrentTime(); + void clearGroup(); + void propagateGroupUpdateToChildren(); + void updateChildrenWithRunningGroup(); + void deleteChildrenWithRunningGroup(); + void startChildrenWithStoppedGroup(); + void stopGroupWithRunningChild(); + void startGroupWithRunningChild(); + void zeroDurationAnimation(); + void stopUncontrolledAnimations(); + void loopCount_data(); + void loopCount(); + void autoAdd(); +}; + +tst_QParallelAnimationGroup::tst_QParallelAnimationGroup() +{ +} + +tst_QParallelAnimationGroup::~tst_QParallelAnimationGroup() +{ +} + +void tst_QParallelAnimationGroup::init() +{ + qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State"); +} + +void tst_QParallelAnimationGroup::cleanup() +{ +} + +void tst_QParallelAnimationGroup::construction() +{ + QParallelAnimationGroup animationgroup; +} + +class AnimationObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int value READ value WRITE setValue) +public: + AnimationObject(int startValue = 0) + : v(startValue) + { } + + int value() const { return v; } + void setValue(int value) { v = value; } + + int v; +}; + +class TestAnimation : public QVariantAnimation +{ + Q_OBJECT +public: + virtual void updateCurrentValue(const QVariant &value) { Q_UNUSED(value)}; + virtual void updateState(QAbstractAnimation::State oldState, + QAbstractAnimation::State newState) + { + Q_UNUSED(oldState) + Q_UNUSED(newState) + }; +}; + +class TestAnimation2 : public QVariantAnimation +{ + Q_OBJECT +public: + TestAnimation2(QAbstractAnimation *animation) : QVariantAnimation(animation) {} + TestAnimation2(int duration, QAbstractAnimation *animation) : QVariantAnimation(animation), m_duration(duration) {} + + virtual void updateCurrentValue(const QVariant &value) { Q_UNUSED(value)}; + virtual void updateState(QAbstractAnimation::State oldState, + QAbstractAnimation::State newState) + { + Q_UNUSED(oldState) + Q_UNUSED(newState) + }; + + virtual int duration() const { + return m_duration; + } +private: + int m_duration; +}; + +class UncontrolledAnimation : public QPropertyAnimation +{ + Q_OBJECT +public: + UncontrolledAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = 0) + : QPropertyAnimation(target, propertyName, parent), id(0) + { + setDuration(250); + } + + int duration() const { return -1; /* not time driven */ } + +protected: + void timerEvent(QTimerEvent *event) + { + if (event->timerId() == id) + stop(); + } + + void updateRunning(bool running) + { + if (running) { + id = startTimer(500); + } else { + killTimer(id); + id = 0; + } + } + +private: + int id; +}; + +void tst_QParallelAnimationGroup::setCurrentTime() +{ + AnimationObject p_o1; + AnimationObject p_o2; + AnimationObject p_o3; + AnimationObject t_o1; + AnimationObject t_o2; + + // parallel operating on different object/properties + QAnimationGroup *parallel = new QParallelAnimationGroup(); + QVariantAnimation *a1_p_o1 = new QPropertyAnimation(&p_o1, "value"); + QVariantAnimation *a1_p_o2 = new QPropertyAnimation(&p_o2, "value"); + QVariantAnimation *a1_p_o3 = new QPropertyAnimation(&p_o3, "value"); + a1_p_o2->setLoopCount(3); + parallel->addAnimation(a1_p_o1); + parallel->addAnimation(a1_p_o2); + parallel->addAnimation(a1_p_o3); + + UncontrolledAnimation *notTimeDriven = new UncontrolledAnimation(&t_o1, "value"); + QCOMPARE(notTimeDriven->totalDuration(), -1); + + QVariantAnimation *loopsForever = new QPropertyAnimation(&t_o2, "value"); + loopsForever->setLoopCount(-1); + QCOMPARE(loopsForever->totalDuration(), -1); + + QParallelAnimationGroup group; + group.addAnimation(parallel); + group.addAnimation(notTimeDriven); + group.addAnimation(loopsForever); + + // Current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(parallel->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_p_o3->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped); + + QCOMPARE(group.currentTime(), 1); + QCOMPARE(a1_p_o1->currentTime(), 1); + QCOMPARE(a1_p_o2->currentTime(), 1); + QCOMPARE(a1_p_o3->currentTime(), 1); + QCOMPARE(notTimeDriven->currentTime(), 1); + QCOMPARE(loopsForever->currentTime(), 1); + + // Current time = 250 + group.setCurrentTime(250); + QCOMPARE(group.currentTime(), 250); + QCOMPARE(a1_p_o1->currentTime(), 250); + QCOMPARE(a1_p_o2->currentTime(), 0); + QCOMPARE(a1_p_o2->currentLoop(), 1); + QCOMPARE(a1_p_o3->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 250); + QCOMPARE(loopsForever->currentTime(), 0); + QCOMPARE(loopsForever->currentLoop(), 1); + + // Current time = 251 + group.setCurrentTime(251); + QCOMPARE(group.currentTime(), 251); + QCOMPARE(a1_p_o1->currentTime(), 250); + QCOMPARE(a1_p_o2->currentTime(), 1); + QCOMPARE(a1_p_o2->currentLoop(), 1); + QCOMPARE(a1_p_o3->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 251); + QCOMPARE(loopsForever->currentTime(), 1); +} + +void tst_QParallelAnimationGroup::clearGroup() +{ + QParallelAnimationGroup group; + static const int animationCount = 10; + + for (int i = 0; i < animationCount; ++i) { + new QParallelAnimationGroup(&group); + } + + QCOMPARE(group.animationCount(), animationCount); + + QPointer<QAbstractAnimation> children[animationCount]; + for (int i = 0; i < animationCount; ++i) { + QVERIFY(group.animationAt(i) != 0); + children[i] = group.animationAt(i); + } + + group.clearAnimations(); + QCOMPARE(group.animationCount(), 0); + QCOMPARE(group.currentTime(), 0); + for (int i = 0; i < animationCount; ++i) + QVERIFY(children[i].isNull()); +} + +void tst_QParallelAnimationGroup::propagateGroupUpdateToChildren() +{ + // this test verifies if group state changes are updating its children correctly + QParallelAnimationGroup group; + + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation anim1(&o, "ole"); + anim1.setEndValue(43); + anim1.setDuration(100); + QVERIFY(!anim1.currentValue().isValid()); + QCOMPARE(anim1.currentValue().toInt(), 0); + QCOMPARE(o.property("ole").toInt(), 42); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QVERIFY(anim2.currentValue().isValid()); + QCOMPARE(anim2.currentValue().toInt(), 0); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + group.start(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Running); + + group.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(anim1.state(), QAnimationGroup::Paused); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); +} + +void tst_QParallelAnimationGroup::updateChildrenWithRunningGroup() +{ + // assert that its possible to modify a child's state directly while their group is running + QParallelAnimationGroup group; + + TestAnimation anim; + anim.setStartValue(0); + anim.setEndValue(100); + anim.setDuration(200); + + QSignalSpy groupStateChangedSpy(&group, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy childStateChangedSpy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QCOMPARE(groupStateChangedSpy.count(), 0); + QCOMPARE(childStateChangedSpy.count(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim); + + group.start(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Running); + + QCOMPARE(groupStateChangedSpy.count(), 1); + QCOMPARE(childStateChangedSpy.count(), 1); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(childStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + + // starting directly a running child will not have any effect + anim.start(); + + QCOMPARE(groupStateChangedSpy.count(), 1); + QCOMPARE(childStateChangedSpy.count(), 1); + + anim.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Paused); + + // in the animation stops directly, the group will still be running + anim.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Stopped); +} + +void tst_QParallelAnimationGroup::deleteChildrenWithRunningGroup() +{ + // test if children can be activated when their group is stopped + QParallelAnimationGroup group; + + QVariantAnimation *anim1 = new TestAnimation; + anim1->setStartValue(0); + anim1->setEndValue(100); + anim1->setDuration(200); + group.addAnimation(anim1); + + QCOMPARE(group.duration(), anim1->duration()); + + group.start(); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1->state(), QAnimationGroup::Running); + + QTest::qWait(50); + QVERIFY(group.currentTime() > 0); + + delete anim1; + QVERIFY(group.animationCount() == 0); + QCOMPARE(group.duration(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(group.currentTime(), 0); //that's the invariant +} + +void tst_QParallelAnimationGroup::startChildrenWithStoppedGroup() +{ + // test if children can be activated when their group is stopped + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(200); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + anim1.start(); + anim2.start(); + anim2.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); +} + +void tst_QParallelAnimationGroup::stopGroupWithRunningChild() +{ + // children that started independently will not be affected by a group stop + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(200); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + anim1.start(); + anim2.start(); + anim2.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + anim1.stop(); + anim2.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); +} + +void tst_QParallelAnimationGroup::startGroupWithRunningChild() +{ + // as the group has precedence over its children, starting a group will restart all the children + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(200); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QSignalSpy stateChangedSpy1(&anim1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy stateChangedSpy2(&anim2, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QCOMPARE(stateChangedSpy1.count(), 0); + QCOMPARE(stateChangedSpy2.count(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + anim1.start(); + anim2.start(); + anim2.pause(); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(1).at(1)), + QAnimationGroup::Paused); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + group.start(); + + QCOMPARE(stateChangedSpy1.count(), 3); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(1).at(1)), + QAnimationGroup::Stopped); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(2).at(1)), + QAnimationGroup::Running); + + QCOMPARE(stateChangedSpy2.count(), 4); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(2).at(1)), + QAnimationGroup::Stopped); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(3).at(1)), + QAnimationGroup::Running); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Running); +} + +void tst_QParallelAnimationGroup::zeroDurationAnimation() +{ + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(0); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(100); + + QSignalSpy stateChangedSpy1(&anim1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy finishedSpy1(&anim1, SIGNAL(finished())); + + QSignalSpy stateChangedSpy2(&anim2, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy finishedSpy2(&anim2, SIGNAL(finished())); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + QCOMPARE(stateChangedSpy1.count(), 0); + group.start(); + QCOMPARE(stateChangedSpy1.count(), 2); + QCOMPARE(finishedSpy1.count(), 1); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(1).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(stateChangedSpy2.count(), 1); + QCOMPARE(finishedSpy2.count(), 0); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy1.at(0).at(1)), + QAnimationGroup::Running); + + + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Running); + QCOMPARE(group.state(), QAnimationGroup::Running); + + + group.stop(); + group.setLoopCount(4); + stateChangedSpy1.clear(); + stateChangedSpy2.clear(); + + group.start(); + QCOMPARE(stateChangedSpy1.count(), 2); + QCOMPARE(stateChangedSpy2.count(), 1); + group.setCurrentTime(50); + QCOMPARE(stateChangedSpy1.count(), 2); + QCOMPARE(stateChangedSpy2.count(), 1); + group.setCurrentTime(150); + QCOMPARE(stateChangedSpy1.count(), 4); + QCOMPARE(stateChangedSpy2.count(), 3); + group.setCurrentTime(50); + QCOMPARE(stateChangedSpy1.count(), 6); + QCOMPARE(stateChangedSpy2.count(), 5); + +} + +void tst_QParallelAnimationGroup::stopUncontrolledAnimations() +{ + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(0); + + AnimationObject o1; + UncontrolledAnimation notTimeDriven(&o1, "value"); + QCOMPARE(notTimeDriven.totalDuration(), -1); + + TestAnimation loopsForever; + loopsForever.setStartValue(0); + loopsForever.setEndValue(100); + loopsForever.setDuration(100); + loopsForever.setLoopCount(-1); + + QSignalSpy stateChangedSpy(&anim1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + group.addAnimation(&anim1); + group.addAnimation(¬TimeDriven); + group.addAnimation(&loopsForever); + + group.start(); + + QCOMPARE(stateChangedSpy.count(), 2); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy.at(1).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Running); + QCOMPARE(loopsForever.state(), QAnimationGroup::Running); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + + notTimeDriven.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever.state(), QAnimationGroup::Running); + + loopsForever.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever.state(), QAnimationGroup::Stopped); +} + +struct AnimState { + AnimState(int time = -1) : time(time), state(-1) {} + AnimState(int time, int state) : time(time), state(state) {} + int time; + int state; +}; + +#define Running QAbstractAnimation::Running +#define Stopped QAbstractAnimation::Stopped + +Q_DECLARE_METATYPE(AnimState) +void tst_QParallelAnimationGroup::loopCount_data() +{ + QTest::addColumn<bool>("directionBackward"); + QTest::addColumn<int>("setLoopCount"); + QTest::addColumn<int>("initialGroupTime"); + QTest::addColumn<int>("currentGroupTime"); + QTest::addColumn<AnimState>("expected1"); + QTest::addColumn<AnimState>("expected2"); + QTest::addColumn<AnimState>("expected3"); + + // D U R A T I O N + // 100 60*2 0 + // direction = Forward + QTest::newRow("50") << false << 3 << 0 << 50 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("100") << false << 3 << 0 << 100 << AnimState(100 ) << AnimState( 40, Running) << AnimState( 0, Stopped); + QTest::newRow("110") << false << 3 << 0 << 110 << AnimState(100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("120") << false << 3 << 0 << 120 << AnimState( 0, Running) << AnimState( 0, Running) << AnimState( 0, Stopped); + + QTest::newRow("170") << false << 3 << 0 << 170 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("220") << false << 3 << 0 << 220 << AnimState(100 ) << AnimState( 40, Running) << AnimState( 0, Stopped); + QTest::newRow("230") << false << 3 << 0 << 230 << AnimState(100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("240") << false << 3 << 0 << 240 << AnimState( 0, Running) << AnimState( 0, Running) << AnimState( 0, Stopped); + + QTest::newRow("290") << false << 3 << 0 << 290 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("340") << false << 3 << 0 << 340 << AnimState(100 ) << AnimState( 40, Running) << AnimState( 0, Stopped); + QTest::newRow("350") << false << 3 << 0 << 350 << AnimState(100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("360") << false << 3 << 0 << 360 << AnimState(100, Stopped) << AnimState( 60 ) << AnimState( 0, Stopped); + + QTest::newRow("410") << false << 3 << 0 << 410 << AnimState(100, Stopped) << AnimState( 60, Stopped) << AnimState( 0, Stopped); + QTest::newRow("460") << false << 3 << 0 << 460 << AnimState(100, Stopped) << AnimState( 60, Stopped) << AnimState( 0, Stopped); + QTest::newRow("470") << false << 3 << 0 << 470 << AnimState(100, Stopped) << AnimState( 60, Stopped) << AnimState( 0, Stopped); + QTest::newRow("480") << false << 3 << 0 << 480 << AnimState(100, Stopped) << AnimState( 60, Stopped) << AnimState( 0, Stopped); + + // direction = Forward, rewind + QTest::newRow("120-110") << false << 3 << 120 << 110 << AnimState( 0, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("120-50") << false << 3 << 120 << 50 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("120-0") << false << 3 << 120 << 0 << AnimState( 0, Running) << AnimState( 0, Running) << AnimState( 0, Stopped); + QTest::newRow("300-110") << false << 3 << 300 << 110 << AnimState( 0, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("300-50") << false << 3 << 300 << 50 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("300-0") << false << 3 << 300 << 0 << AnimState( 0, Running) << AnimState( 0, Running) << AnimState( 0, Stopped); + QTest::newRow("115-105") << false << 3 << 115 << 105 << AnimState( 42, Stopped) << AnimState( 45, Running) << AnimState( 0, Stopped); + + // direction = Backward + QTest::newRow("b120-120") << true << 3 << 120 << 120 << AnimState( 42, Stopped) << AnimState( 60, Running) << AnimState( 0, Stopped); + QTest::newRow("b120-110") << true << 3 << 120 << 110 << AnimState( 42, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("b120-100") << true << 3 << 120 << 100 << AnimState(100, Running) << AnimState( 40, Running) << AnimState( 0, Stopped); + QTest::newRow("b120-50") << true << 3 << 120 << 50 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("b120-0") << true << 3 << 120 << 0 << AnimState( 0, Stopped) << AnimState( 0, Stopped) << AnimState( 0, Stopped); + QTest::newRow("b360-170") << true << 3 << 360 << 170 << AnimState( 50, Running) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("b360-220") << true << 3 << 360 << 220 << AnimState(100, Running) << AnimState( 40, Running) << AnimState( 0, Stopped); + QTest::newRow("b360-210") << true << 3 << 360 << 210 << AnimState( 90, Running) << AnimState( 30, Running) << AnimState( 0, Stopped); + QTest::newRow("b360-120") << true << 3 << 360 << 120 << AnimState( 0, Stopped) << AnimState( 60, Running) << AnimState( 0, Stopped); + + // rewind, direction = Backward + QTest::newRow("b50-110") << true << 3 << 50 << 110 << AnimState(100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + QTest::newRow("b50-120") << true << 3 << 50 << 120 << AnimState(100, Stopped) << AnimState( 60, Running) << AnimState( 0, Stopped); + QTest::newRow("b50-140") << true << 3 << 50 << 140 << AnimState( 20, Running) << AnimState( 20, Running) << AnimState( 0, Stopped); + QTest::newRow("b50-240") << true << 3 << 50 << 240 << AnimState(100, Stopped) << AnimState( 60, Running) << AnimState( 0, Stopped); + QTest::newRow("b50-260") << true << 3 << 50 << 260 << AnimState( 20, Running) << AnimState( 20, Running) << AnimState( 0, Stopped); + QTest::newRow("b50-350") << true << 3 << 50 << 350 << AnimState(100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + + // infinite looping + QTest::newRow("inf1220") << false << -1 << 0 << 1220 << AnimState( 20, Running) << AnimState( 20, Running) << AnimState( 0, Stopped); + QTest::newRow("inf1310") << false << -1 << 0 << 1310 << AnimState( 100, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + // infinite looping, direction = Backward (will only loop once) + QTest::newRow("b.inf120-120") << true << -1 << 120 << 120 << AnimState( 42, Stopped) << AnimState( 60, Running) << AnimState( 0, Stopped); + QTest::newRow("b.inf120-20") << true << -1 << 120 << 20 << AnimState( 20, Running) << AnimState( 20, Running) << AnimState( 0, Stopped); + QTest::newRow("b.inf120-110") << true << -1 << 120 << 110 << AnimState( 42, Stopped) << AnimState( 50, Running) << AnimState( 0, Stopped); + + +} + +void tst_QParallelAnimationGroup::loopCount() +{ + QFETCH(bool, directionBackward); + QFETCH(int, setLoopCount); + QFETCH(int, initialGroupTime); + QFETCH(int, currentGroupTime); + QFETCH(AnimState, expected1); + QFETCH(AnimState, expected2); + QFETCH(AnimState, expected3); + + QParallelAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(100); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(60); //total 120 + anim2.setLoopCount(2); + + TestAnimation anim3; + anim3.setStartValue(0); + anim3.setEndValue(100); + anim3.setDuration(0); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + group.addAnimation(&anim3); + + group.setLoopCount(setLoopCount); + if (initialGroupTime >= 0) + group.setCurrentTime(initialGroupTime); + if (directionBackward) + group.setDirection(QAbstractAnimation::Backward); + + group.start(); + if (initialGroupTime >= 0) + group.setCurrentTime(initialGroupTime); + + anim1.setCurrentTime(42); // 42 is "untouched" + anim2.setCurrentTime(42); + + group.setCurrentTime(currentGroupTime); + + QCOMPARE(anim1.currentTime(), expected1.time); + QCOMPARE(anim2.currentTime(), expected2.time); + QCOMPARE(anim3.currentTime(), expected3.time); + + if (expected1.state >=0) + QCOMPARE(int(anim1.state()), expected1.state); + if (expected2.state >=0) + QCOMPARE(int(anim2.state()), expected2.state); + if (expected3.state >=0) + QCOMPARE(int(anim3.state()), expected3.state); + +} + +void tst_QParallelAnimationGroup::autoAdd() +{ + QParallelAnimationGroup group; + QCOMPARE(group.duration(), 0); + TestAnimation2 *test = new TestAnimation2(250, &group); // 0, duration = 250; + QCOMPARE(test->group(), static_cast<QAnimationGroup*>(&group)); + QCOMPARE(test->duration(), 250); + QCOMPARE(group.duration(), 250); + + test = new TestAnimation2(750, &group); // 1 + QCOMPARE(test->group(), static_cast<QAnimationGroup*>(&group)); + QCOMPARE(group.duration(), 750); + test = new TestAnimation2(500, &group); // 2 + QCOMPARE(test->group(), static_cast<QAnimationGroup*>(&group)); + QCOMPARE(group.duration(), 750); + + delete group.animationAt(1); // remove the one with duration = 750 + QCOMPARE(group.duration(), 500); + + delete group.animationAt(1); // remove the one with duration = 500 + QCOMPARE(group.duration(), 250); + + test = static_cast<TestAnimation2*>(group.animationAt(0)); + test->setParent(0); // remove the last one (with duration = 250) + QCOMPARE(test->group(), static_cast<QAnimationGroup*>(0)); + QCOMPARE(group.duration(), 0); +} + +QTEST_MAIN(tst_QParallelAnimationGroup) +#include "tst_qparallelanimationgroup.moc" diff --git a/tests/auto/qpicture/tst_qpicture.cpp b/tests/auto/qpicture/tst_qpicture.cpp index 9ea465d..8d6cc0a 100644 --- a/tests/auto/qpicture/tst_qpicture.cpp +++ b/tests/auto/qpicture/tst_qpicture.cpp @@ -47,6 +47,7 @@ #include <qimage.h> #include <qdesktopwidget.h> #include <qapplication.h> +#include <limits.h> //TESTED_CLASS= //TESTED_FILES= @@ -66,6 +67,9 @@ private slots: void operator_lt_lt(); void save_restore(); + + void boundaryValues_data(); + void boundaryValues(); }; // Testing get/set functions @@ -234,5 +238,39 @@ void tst_QPicture::save_restore() QVERIFY( pix1.toImage() == pix2.toImage() ); } +void tst_QPicture::boundaryValues_data() +{ + QTest::addColumn<int>("x"); + QTest::addColumn<int>("y"); + QTest::newRow("max x") << INT_MAX << 50; + QTest::newRow("max y") << 50 << INT_MAX; + QTest::newRow("max x and y") << INT_MAX << INT_MAX; + + QTest::newRow("min x") << INT_MIN << 50; + QTest::newRow("min y") << 50 << INT_MIN; + QTest::newRow("min x and y") << INT_MIN << INT_MIN; + + QTest::newRow("min x, max y") << INT_MIN << INT_MAX; + QTest::newRow("max x, min y") << INT_MAX << INT_MIN; + +} + +void tst_QPicture::boundaryValues() +{ + QPicture picture; + + QPainter painter; + painter.begin(&picture); + + QFETCH(int, x); + QFETCH(int, y); + painter.drawPoint(QPoint(x, y)); + + painter.end(); + + +} + + QTEST_MAIN(tst_QPicture) #include "tst_qpicture.moc" diff --git a/tests/auto/qpixmap/images/designer.png b/tests/auto/qpixmap/images/designer.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/designer.png diff --git a/tests/auto/qpixmap/images/dx_-10_dy_-10_50_50_100_100.png b/tests/auto/qpixmap/images/dx_-10_dy_-10_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..a4a1924 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-10_dy_-10_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_-10_dy_-10_x_y_w_h.png b/tests/auto/qpixmap/images/dx_-10_dy_-10_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..1506af5 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-10_dy_-10_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_-10_dy_0_50_50_100_100.png b/tests/auto/qpixmap/images/dx_-10_dy_0_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..8500ab1 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-10_dy_0_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_-10_dy_0_x_y_w_h.png b/tests/auto/qpixmap/images/dx_-10_dy_0_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..2145c61 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-10_dy_0_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_-128_dy_-128_x_y_w_h.png b/tests/auto/qpixmap/images/dx_-128_dy_-128_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-128_dy_-128_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_-128_dy_0_x_y_w_h.png b/tests/auto/qpixmap/images/dx_-128_dy_0_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_-128_dy_0_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_-10_50_50_100_100.png b/tests/auto/qpixmap/images/dx_0_dy_-10_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..728ee79 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_-10_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_-10_x_y_w_h.png b/tests/auto/qpixmap/images/dx_0_dy_-10_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..e9d5850 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_-10_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_-128_x_y_w_h.png b/tests/auto/qpixmap/images/dx_0_dy_-128_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_-128_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_0_50_50_100_100.png b/tests/auto/qpixmap/images/dx_0_dy_0_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_0_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_0_null.png b/tests/auto/qpixmap/images/dx_0_dy_0_null.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_0_null.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_0_x_y_w_h.png b/tests/auto/qpixmap/images/dx_0_dy_0_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_0_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_10_50_50_100_100.png b/tests/auto/qpixmap/images/dx_0_dy_10_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..7c09b17 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_10_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_10_x_y_w_h.png b/tests/auto/qpixmap/images/dx_0_dy_10_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..70a6340 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_10_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_128_x_y_w_h.png b/tests/auto/qpixmap/images/dx_0_dy_128_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_128_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_0_dy_1_null.png b/tests/auto/qpixmap/images/dx_0_dy_1_null.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_0_dy_1_null.png diff --git a/tests/auto/qpixmap/images/dx_10_dy_0_50_50_100_100.png b/tests/auto/qpixmap/images/dx_10_dy_0_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..85abada --- /dev/null +++ b/tests/auto/qpixmap/images/dx_10_dy_0_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_10_dy_0_x_y_w_h.png b/tests/auto/qpixmap/images/dx_10_dy_0_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..3e03450 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_10_dy_0_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_10_dy_10_50_50_100_100.png b/tests/auto/qpixmap/images/dx_10_dy_10_50_50_100_100.png Binary files differnew file mode 100644 index 0000000..315fbe0 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_10_dy_10_50_50_100_100.png diff --git a/tests/auto/qpixmap/images/dx_10_dy_10_x_y_w_h.png b/tests/auto/qpixmap/images/dx_10_dy_10_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..d91dc71 --- /dev/null +++ b/tests/auto/qpixmap/images/dx_10_dy_10_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_128_dy_0_x_y_w_h.png b/tests/auto/qpixmap/images/dx_128_dy_0_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_128_dy_0_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_128_dy_128_64_64_128_128.png b/tests/auto/qpixmap/images/dx_128_dy_128_64_64_128_128.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_128_dy_128_64_64_128_128.png diff --git a/tests/auto/qpixmap/images/dx_128_dy_128_x_y_w_h.png b/tests/auto/qpixmap/images/dx_128_dy_128_x_y_w_h.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_128_dy_128_x_y_w_h.png diff --git a/tests/auto/qpixmap/images/dx_1_dy_0_null.png b/tests/auto/qpixmap/images/dx_1_dy_0_null.png Binary files differnew file mode 100644 index 0000000..bca471d --- /dev/null +++ b/tests/auto/qpixmap/images/dx_1_dy_0_null.png diff --git a/tests/auto/qpixmap/qpixmap.pro b/tests/auto/qpixmap/qpixmap.pro index 74600e8..02ed3f2 100644 --- a/tests/auto/qpixmap/qpixmap.pro +++ b/tests/auto/qpixmap/qpixmap.pro @@ -19,3 +19,4 @@ wince*: { win32:LIBS += -lgdi32 -luser32 } +RESOURCES += qpixmap.qrc diff --git a/tests/auto/qpixmap/qpixmap.qrc b/tests/auto/qpixmap/qpixmap.qrc new file mode 100644 index 0000000..99fde61 --- /dev/null +++ b/tests/auto/qpixmap/qpixmap.qrc @@ -0,0 +1,29 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/designer.png</file> + <file>images/dx_0_dy_0_50_50_100_100.png</file> + <file>images/dx_0_dy_0_null.png</file> + <file>images/dx_0_dy_0_x_y_w_h.png</file> + <file>images/dx_0_dy_-10_50_50_100_100.png</file> + <file>images/dx_0_dy_10_50_50_100_100.png</file> + <file>images/dx_0_dy_-10_x_y_w_h.png</file> + <file>images/dx_0_dy_10_x_y_w_h.png</file> + <file>images/dx_0_dy_-128_x_y_w_h.png</file> + <file>images/dx_0_dy_128_x_y_w_h.png</file> + <file>images/dx_0_dy_1_null.png</file> + <file>images/dx_-10_dy_0_50_50_100_100.png</file> + <file>images/dx_10_dy_0_50_50_100_100.png</file> + <file>images/dx_-10_dy_0_x_y_w_h.png</file> + <file>images/dx_10_dy_0_x_y_w_h.png</file> + <file>images/dx_-10_dy_-10_50_50_100_100.png</file> + <file>images/dx_10_dy_10_50_50_100_100.png</file> + <file>images/dx_-10_dy_-10_x_y_w_h.png</file> + <file>images/dx_10_dy_10_x_y_w_h.png</file> + <file>images/dx_-128_dy_0_x_y_w_h.png</file> + <file>images/dx_128_dy_0_x_y_w_h.png</file> + <file>images/dx_128_dy_128_64_64_128_128.png</file> + <file>images/dx_-128_dy_-128_x_y_w_h.png</file> + <file>images/dx_128_dy_128_x_y_w_h.png</file> + <file>images/dx_1_dy_0_null.png</file> +</qresource> +</RCC> diff --git a/tests/auto/qpixmap/tst_qpixmap.cpp b/tests/auto/qpixmap/tst_qpixmap.cpp index acec65e..5e94cb4 100644 --- a/tests/auto/qpixmap/tst_qpixmap.cpp +++ b/tests/auto/qpixmap/tst_qpixmap.cpp @@ -48,6 +48,8 @@ #include <qdesktopwidget.h> #include <qpaintengine.h> +#include <private/qpixmapdata_p.h> + #include <QSet> #ifdef Q_WS_WIN @@ -98,6 +100,9 @@ private slots: void testMetrics(); + void scroll_data(); + void scroll(); + void fill_data(); void fill(); void fill_transparent(); @@ -152,6 +157,38 @@ private slots: void fromData(); }; +static bool lenientCompare(const QPixmap &actual, const QPixmap &expected) +{ + QImage expectedImage = expected.toImage().convertToFormat(QImage::Format_RGB32); + QImage actualImage = actual.toImage().convertToFormat(QImage::Format_RGB32); + + if (expectedImage.size() != actualImage.size()) + return false; + + int size = actual.width() * actual.height(); + + QRgb *a = (QRgb *)actualImage.bits(); + QRgb *e = (QRgb *)expectedImage.bits(); + for (int i = 0; i < size; ++i) { + QColor ca(a[i]); + QColor ce(e[i]); + + bool result = true; + + if (qAbs(ca.red() - ce.red()) > 2) + result = false; + if (qAbs(ca.green() - ce.green()) > 2) + result = false; + if (qAbs(ca.blue() - ce.blue()) > 2) + result = false; + + if (!result) + return false; + } + + return true; +} + Q_DECLARE_METATYPE(QImage) Q_DECLARE_METATYPE(QPixmap) Q_DECLARE_METATYPE(QMatrix) @@ -214,7 +251,7 @@ void tst_QPixmap::setAlphaChannel() pixmap.setAlphaChannel(alphaChannel); #ifdef Q_WS_X11 - if (!pixmap.x11PictureHandle()) + if (pixmap.pixmapData()->classId() == QPixmapData::X11Class && !pixmap.x11PictureHandle()) QSKIP("Requires XRender support", SkipAll); #endif @@ -273,7 +310,7 @@ void tst_QPixmap::fromImage() const QPixmap pixmap = QPixmap::fromImage(image); #ifdef Q_WS_X11 - if (!pixmap.x11PictureHandle()) + if (pixmap.pixmapData()->classId() == QPixmapData::X11Class && !pixmap.x11PictureHandle()) QSKIP("Requires XRender support", SkipAll); #endif const QImage result = pixmap.toImage(); @@ -320,6 +357,99 @@ void tst_QPixmap::convertFromImage() QVERIFY( pixmapsAreEqual(&pix, &res) ); } +void tst_QPixmap::scroll_data() +{ + QTest::addColumn<QImage>("input"); + QTest::addColumn<int>("dx"); + QTest::addColumn<int>("dy"); + QTest::addColumn<QRect>("rect"); + QTest::addColumn<QRegion>("exposed"); + QTest::addColumn<bool>("newPix"); + + QImage input(":/images/designer.png"); + + // Noop tests + QTest::newRow("null") << QImage() << 0 << 0 << QRect() << QRegion() << false; + QTest::newRow("dx_0_dy_0_null") << input << 0 << 0 << QRect() << QRegion() << false; + QTest::newRow("dx_1_dy_0_null") << input << 1 << 0 << QRect() << QRegion() << false; + QTest::newRow("dx_0_dy_1_null") << input << 0 << 1 << QRect() << QRegion() << false; + QTest::newRow("dx_0_dy_0_x_y_w_h") << input << 0 << 0 << input.rect() << QRegion() << false; + + QRegion r; + // Scroll whole pixmap + r = QRegion(); r += QRect(0, 0, 128, 10); + QTest::newRow("dx_0_dy_10_x_y_w_h") << input << 0 << 10 << input.rect() << r << true; + r = QRegion(); r += QRect(0, 0, 10, 128); + QTest::newRow("dx_10_dy_0_x_y_w_h") << input << 10 << 0 << input.rect() << r << true; + r = QRegion(); r += QRect(0, 0, 128, 10); r += QRect(0, 10, 10, 118); + QTest::newRow("dx_10_dy_10_x_y_w_h") << input << 10 << 10 << input.rect() << r << true; + r = QRegion(); r += QRect(118, 0, 10, 128); + QTest::newRow("dx_-10_dy_0_x_y_w_h") << input << -10 << 0 << input.rect() << r << true; + r = QRegion(); r += QRect(0, 118, 128, 10); + QTest::newRow("dx_0_dy_-10_x_y_w_h") << input << 0 << -10 << input.rect() << r << true; + r = QRegion(); r += QRect(118, 0, 10, 118); r += QRect(0, 118, 128, 10); + QTest::newRow("dx_-10_dy_-10_x_y_w_h") << input << -10 << -10 << input.rect() << r << true; + + // Scroll part of pixmap + QTest::newRow("dx_0_dy_0_50_50_100_100") << input << 0 << 0 << QRect(50, 50, 100, 100) << QRegion() << false; + r = QRegion(); r += QRect(50, 50, 10, 78); + QTest::newRow("dx_10_dy_0_50_50_100_100") << input << 10 << 0 << QRect(50, 50, 100, 100) << r << true; + r = QRegion(); r += QRect(50, 50, 78, 10); + QTest::newRow("dx_0_dy_10_50_50_100_100") << input << 0 << 10 << QRect(50, 50, 100, 100) << r << true; + r = QRegion(); r += QRect(50, 50, 78, 10); r += QRect(50, 60, 10, 68); + QTest::newRow("dx_10_dy_10_50_50_100_100") << input << 10 << 10 << QRect(50, 50, 100, 100) << r << true; + r = QRegion(); r += QRect(118, 50, 10, 78); + QTest::newRow("dx_-10_dy_0_50_50_100_100") << input << -10 << 0 << QRect(50, 50, 100, 100) << r << true; + r = QRegion(); r += QRect(50, 118, 78, 10); + QTest::newRow("dx_0_dy_-10_50_50_100_100") << input << 0 << -10 << QRect(50, 50, 100, 100) << r << true; + r = QRegion(); r += QRect(118, 50, 10, 68); r += QRect(50, 118, 78, 10); + QTest::newRow("dx_-10_dy_-10_50_50_100_100") << input << -10 << -10 << QRect(50, 50, 100, 100) << r << true; + + // Scroll away the whole pixmap + r = input.rect(); + QTest::newRow("dx_128_dy_0_x_y_w_h") << input << 128 << 0 << input.rect() << r << false; + QTest::newRow("dx_0_dy_128_x_y_w_h") << input << 0 << 128 << input.rect() << r << false; + QTest::newRow("dx_128_dy_128_x_y_w_h") << input << 128 << 128 << input.rect() << r << false; + QTest::newRow("dx_-128_dy_0_x_y_w_h") << input << -128 << 0 << input.rect() << r << false; + QTest::newRow("dx_0_dy_-128_x_y_w_h") << input << 0 << -128 << input.rect() << r << false; + QTest::newRow("dx_-128_dy_-128_x_y_w_h") << input << -128 << -128 << input.rect() << r << false; + + // Scroll away part of the pixmap + r = QRegion(); r += QRect(64, 64, 64, 64); + QTest::newRow("dx_128_dy_128_64_64_128_128") << input << 128 << 128 << QRect(64, 64, 128, 128) << r << false; +} + +void tst_QPixmap::scroll() +{ + QFETCH(QImage, input); + QFETCH(int, dx); + QFETCH(int, dy); + QFETCH(QRect, rect); + QFETCH(QRegion, exposed); + QFETCH(bool, newPix); + + QPixmap pixmap = QPixmap::fromImage(input); + QRegion exp; + qint64 oldKey = pixmap.cacheKey(); + pixmap.scroll(dx, dy, rect, &exp); + if (!newPix) + QCOMPARE(pixmap.cacheKey(), oldKey); + else + QVERIFY(pixmap.cacheKey() != oldKey); + +#if 0 + // Remember to add to resources. + QString fileName = QString("images/%1.png").arg(QTest::currentDataTag()); + pixmap.toImage().save(fileName); +#else + QString fileName = QString(":/images/%1.png").arg(QTest::currentDataTag()); +#endif + QPixmap output(fileName); + QVERIFY(input.isNull() == output.isNull()); + QVERIFY(lenientCompare(pixmap, output)); + QCOMPARE(exp, exposed); +} + void tst_QPixmap::fill_data() { QTest::addColumn<uint>("pixel"); @@ -385,7 +515,7 @@ void tst_QPixmap::fill() pm = QPixmap(400, 400); #if defined(Q_WS_X11) - if (!bitmap && !pm.x11PictureHandle()) + if (!bitmap && pm.pixmapData()->classId() == QPixmapData::X11Class && !pm.x11PictureHandle()) QSKIP("Requires XRender support", SkipSingle); #endif @@ -397,7 +527,7 @@ void tst_QPixmap::fill() QImage image = pm.toImage(); if (bitmap && syscolor) { int pixelindex = (pixel == Qt::color0) ? 0 : 1; - QVERIFY(image.pixelIndex(0,0) == pixelindex); + QCOMPARE(image.pixelIndex(0,0), pixelindex); } QImage::Format format = compareColor.alpha() != 255 ? QImage::Format_ARGB32 @@ -415,7 +545,7 @@ void tst_QPixmap::fill_transparent() { QPixmap pixmap(10, 10); #ifdef Q_WS_X11 - if (!pixmap.x11PictureHandle()) + if (pixmap.pixmapData()->classId() == QPixmapData::X11Class && !pixmap.x11PictureHandle()) QSKIP("Requires XRender support", SkipAll); #endif pixmap.fill(Qt::transparent); @@ -535,7 +665,7 @@ void tst_QPixmap::testMetrics() QCOMPARE(pixmap.width(), 100); QCOMPARE(pixmap.height(), 100); - QCOMPARE(pixmap.depth(), QPixmap::defaultDepth()); + QVERIFY(pixmap.depth() >= QPixmap::defaultDepth()); QBitmap bitmap(100, 100); @@ -597,7 +727,11 @@ void tst_QPixmap::drawBitmap() painter2.setPen(Qt::red); painter2.drawPixmap(0,0,10,10, bitmap); painter2.end(); - QCOMPARE(pixmap.toImage().pixel(5,5), QColor(Qt::red).rgb()); + + QPixmap expected(10, 10); + expected.fill(Qt::red); + + QVERIFY(lenientCompare(pixmap, expected)); } void tst_QPixmap::grabWidget() @@ -610,8 +744,7 @@ void tst_QPixmap::grabWidget() expected.fill(Qt::green); QPixmap actual = QPixmap::grabWidget(&widget, QRect(64, 64, 64, 64)); - - QCOMPARE(actual, expected); + QVERIFY(lenientCompare(actual, expected)); } void tst_QPixmap::grabWindow() @@ -642,7 +775,7 @@ void tst_QPixmap::grabWindow() QPixmap grabWindowPixmap = QPixmap::grabWindow(child.winId()); QPixmap grabWidgetPixmap = QPixmap::grabWidget(&child); - QCOMPARE(grabWindowPixmap, grabWidgetPixmap); + lenientCompare(grabWindowPixmap, grabWidgetPixmap); } void tst_QPixmap::isNull() @@ -978,10 +1111,10 @@ void tst_QPixmap::copy() } QPixmap dest = src.copy(10, 10, 10, 10); - QImage result = dest.toImage().convertToFormat(QImage::Format_RGB32); - QImage expected(10, 10, QImage::Format_RGB32); - expected.fill(0xff0000ff); - QCOMPARE(result, expected); + + QPixmap expected(10, 10); + expected.fill(Qt::blue); + QVERIFY(lenientCompare(dest, expected)); } #ifdef QT3_SUPPORT @@ -1049,27 +1182,33 @@ void tst_QPixmap::transformed() } QPixmap p2(10, 20); - p2.fill(Qt::red); { QPainter p(&p2); - p.drawRect(0, 0, p2.width() - 1, p2.height() - 1); + p.rotate(90); + p.drawPixmap(0, -p1.height(), p1); + } + + QPixmap p3(20, 10); + { + QPainter p(&p3); + p.rotate(180); + p.drawPixmap(-p1.width(), -p1.height(), p1); + } + + QPixmap p4(10, 20); + { + QPainter p(&p4); + p.rotate(270); + p.drawPixmap(-p1.width(), 0, p1); } QPixmap p1_90 = p1.transformed(QTransform().rotate(90)); QPixmap p1_180 = p1.transformed(QTransform().rotate(180)); QPixmap p1_270 = p1.transformed(QTransform().rotate(270)); - QCOMPARE(p1_90.size(), p2.size()); - QCOMPARE(p1_90.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied), - p2.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied)); - - QCOMPARE(p1_180.size(), p1.size()); - QCOMPARE(p1_180.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied), - p1.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied)); - - QCOMPARE(p1_270.size(), p2.size()); - QCOMPARE(p1_270.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied), - p2.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied)); + QVERIFY(lenientCompare(p1_90, p2)); + QVERIFY(lenientCompare(p1_180, p3)); + QVERIFY(lenientCompare(p1_270, p4)); } void tst_QPixmap::transformed2() @@ -1097,9 +1236,7 @@ void tst_QPixmap::transformed2() p.drawRect(3, 6, 3, 3); p.end(); - QCOMPARE(actual.size(), expected.size()); - QCOMPARE(actual.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied), - expected.toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied)); + QVERIFY(lenientCompare(actual, expected)); } void tst_QPixmap::fromImage_crash() diff --git a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp index 77db107..e71bd5d 100644 --- a/tests/auto/qpixmapcache/tst_qpixmapcache.cpp +++ b/tests/auto/qpixmapcache/tst_qpixmapcache.cpp @@ -44,9 +44,7 @@ #include <qpixmapcache.h> - - - +#include "../../../src/gui/image/qpixmapcache_p.h" //TESTED_CLASS= @@ -68,10 +66,21 @@ private slots: void setCacheLimit(); void find(); void insert(); + void replace(); void remove(); void clear(); + void pixmapKey(); }; +static QPixmapCache::KeyData* getPrivate(QPixmapCache::Key &key) +{ + return (*reinterpret_cast<QPixmapCache::KeyData**>(&key)); +} + +static QPixmapCache::KeyData** getPrivateRef(QPixmapCache::Key &key) +{ + return (reinterpret_cast<QPixmapCache::KeyData**>(&key)); +} static int originalCacheLimit; @@ -119,6 +128,72 @@ void tst_QPixmapCache::setCacheLimit() QVERIFY(QPixmapCache::find("P1") != 0); delete p1; + + //The int part of the API + p1 = new QPixmap(2, 3); + QPixmapCache::Key key = QPixmapCache::insert(*p1); + QVERIFY(QPixmapCache::find(key, p1) != 0); + delete p1; + + QPixmapCache::setCacheLimit(0); + QVERIFY(QPixmapCache::find(key, p1) == 0); + + p1 = new QPixmap(2, 3); + QPixmapCache::setCacheLimit(1000); + QPixmapCache::replace(key, *p1); + QVERIFY(QPixmapCache::find(key, p1) == 0); + + delete p1; + + //Let check if keys are released when the pixmap cache is + //full or has been flushed. + QPixmapCache::clear(); + p1 = new QPixmap(2, 3); + key = QPixmapCache::insert(*p1); + QVERIFY(QPixmapCache::find(key, p1) != 0); + QPixmapCache::setCacheLimit(0); + QVERIFY(QPixmapCache::find(key, p1) == 0); + QPixmapCache::setCacheLimit(1000); + key = QPixmapCache::insert(*p1); + QCOMPARE(getPrivate(key)->isValid, true); + QCOMPARE(getPrivate(key)->key, 1); + + delete p1; + + //Let check if removing old entries doesn't let you get + // wrong pixmaps + QPixmapCache::clear(); + QPixmap p2; + p1 = new QPixmap(2, 3); + key = QPixmapCache::insert(*p1); + QVERIFY(QPixmapCache::find(key, &p2) != 0); + //we flush the cache + QPixmapCache::setCacheLimit(0); + QPixmapCache::setCacheLimit(1000); + QPixmapCache::Key key2 = QPixmapCache::insert(*p1); + QCOMPARE(getPrivate(key2)->key, 2); + QVERIFY(QPixmapCache::find(key, &p2) == 0); + QVERIFY(QPixmapCache::find(key2, &p2) != 0); + QCOMPARE(p2, *p1); + + delete p1; + + //Here we simulate the flushing when the app is idle + /*QPixmapCache::clear(); + QPixmapCache::setCacheLimit(originalCacheLimit); + p1 = new QPixmap(300, 300); + key = QPixmapCache::insert(*p1); + QCOMPARE(getPrivate(key)->key, 1); + key2 = QPixmapCache::insert(*p1); + key2 = QPixmapCache::insert(*p1); + QPixmapCache::Key key3 = QPixmapCache::insert(*p1); + QTest::qWait(32000); + key2 = QPixmapCache::insert(*p1); + QCOMPARE(getPrivate(key2)->key, 1); + //This old key is not valid anymore after the flush + QCOMPARE(getPrivate(key)->isValid, false); + QVERIFY(QPixmapCache::find(key, &p2) == 0); + delete p1;*/ } void tst_QPixmapCache::find() @@ -137,6 +212,28 @@ void tst_QPixmapCache::find() QPixmap *p3 = QPixmapCache::find("P1"); QVERIFY(p3); QCOMPARE(p1, *p3); + + //The int part of the API + QPixmapCache::Key key = QPixmapCache::insert(p1); + + QVERIFY(QPixmapCache::find(key, &p2)); + QCOMPARE(p2.width(), 10); + QCOMPARE(p2.height(), 10); + QCOMPARE(p1, p2); + + QPixmapCache::clear(); + + key = QPixmapCache::insert(p1); + + //The int part of the API + // make sure it doesn't explode + QList<QPixmapCache::Key> keys; + for (int i = 0; i < 40000; ++i) + QPixmapCache::insert(p1); + + //at that time the first key has been erase because no more place in the cache + QVERIFY(QPixmapCache::find(key, &p1) == 0); + QCOMPARE(getPrivate(key)->isValid, false); } void tst_QPixmapCache::insert() @@ -152,18 +249,22 @@ void tst_QPixmapCache::insert() QPixmapCache::insert("0", p1); // ditto - for (int j = 0; j < 20000; ++j) + for (int j = 0; j < 40000; ++j) QPixmapCache::insert(QString::number(j), p1); int num = 0; - for (int k = 0; k < 20000; ++k) { + for (int k = 0; k < 40000; ++k) { if (QPixmapCache::find(QString::number(k))) ++num; } + if (QPixmapCache::find("0")) + ++num; + int estimatedNum = (1024 * QPixmapCache::cacheLimit()) / ((p1.width() * p1.height() * p1.depth()) / 8); - QVERIFY(estimatedNum - 1 <= num <= estimatedNum + 1); + + QVERIFY(num <= estimatedNum); QPixmap p3; QPixmapCache::insert("null", p3); @@ -176,6 +277,50 @@ void tst_QPixmapCache::insert() QPixmapCache::insert("custom", c2); //We have deleted the old pixmap in the cache for the same key QVERIFY(c1.isDetached()); + + //The int part of the API + // make sure it doesn't explode + QList<QPixmapCache::Key> keys; + for (int i = 0; i < 40000; ++i) + keys.append(QPixmapCache::insert(p1)); + + num = 0; + for (int k = 0; k < 40000; ++k) { + if (QPixmapCache::find(keys.at(k), &p2)) + ++num; + } + + estimatedNum = (1024 * QPixmapCache::cacheLimit()) + / ((p1.width() * p1.height() * p1.depth()) / 8); + QVERIFY(num <= estimatedNum); + QPixmapCache::insert(p3); + +} + +void tst_QPixmapCache::replace() +{ + //The int part of the API + QPixmap p1(10, 10); + p1.fill(Qt::red); + + QPixmap p2(10, 10); + p2.fill(Qt::yellow); + + QPixmapCache::Key key = QPixmapCache::insert(p1); + + QPixmap p3; + QVERIFY(QPixmapCache::find(key, &p3) == 1); + + QPixmapCache::replace(key,p2); + + QVERIFY(QPixmapCache::find(key, &p3) == 1); + + QCOMPARE(p3.width(), 10); + QCOMPARE(p3.height(), 10); + QCOMPARE(p3, p2); + + //Broken keys + QCOMPARE(QPixmapCache::replace(QPixmapCache::Key(), p2), false); } void tst_QPixmapCache::remove() @@ -198,6 +343,48 @@ void tst_QPixmapCache::remove() QPixmapCache::remove("green"); QVERIFY(QPixmapCache::find("green") == 0); + + //The int part of the API + QPixmapCache::clear(); + p1.fill(Qt::red); + QPixmapCache::Key key = QPixmapCache::insert(p1); + p1.fill(Qt::yellow); + + QVERIFY(QPixmapCache::find(key, &p2)); + QVERIFY(p1.toImage() != p2.toImage()); + QVERIFY(p1.toImage() == p1.toImage()); // sanity check + + QPixmapCache::remove(key); + QVERIFY(QPixmapCache::find(key, &p1) == 0); + + //Broken key + QPixmapCache::remove(QPixmapCache::Key()); + QVERIFY(QPixmapCache::find(QPixmapCache::Key(), &p1) == 0); + + //Test if keys are release + QPixmapCache::clear(); + key = QPixmapCache::insert(p1); + QCOMPARE(getPrivate(key)->key, 1); + QPixmapCache::remove(key); + key = QPixmapCache::insert(p1); + QCOMPARE(getPrivate(key)->key, 1); + + //Test if pixmaps are correctly deleted + QPixmapCache::clear(); + key = QPixmapCache::insert(p1); + QCOMPARE(getPrivate(key)->key, 1); + QVERIFY(QPixmapCache::find(key, &p1) != 0); + QPixmapCache::remove(key); + QCOMPARE(p1.isDetached(), true); + + //We mix both part of the API + QPixmapCache::clear(); + p1.fill(Qt::red); + QPixmapCache::insert("red", p1); + key = QPixmapCache::insert(p1); + QPixmapCache::remove(key); + QVERIFY(QPixmapCache::find(key, &p1) == 0); + QVERIFY(QPixmapCache::find("red") != 0); } void tst_QPixmapCache::clear() @@ -205,12 +392,11 @@ void tst_QPixmapCache::clear() QPixmap p1(10, 10); p1.fill(Qt::red); - for (int i = 0; i < 20000; ++i) { + for (int i = 0; i < 20000; ++i) QVERIFY(QPixmapCache::find("x" + QString::number(i)) == 0); - } - for (int j = 0; j < 20000; ++j) { + + for (int j = 0; j < 20000; ++j) QPixmapCache::insert(QString::number(j), p1); - } int num = 0; for (int k = 0; k < 20000; ++k) { @@ -221,10 +407,68 @@ void tst_QPixmapCache::clear() QPixmapCache::clear(); - for (int k = 0; k < 20000; ++k) { + for (int k = 0; k < 20000; ++k) QVERIFY(QPixmapCache::find(QString::number(k)) == 0); + + //The int part of the API + QPixmap p2(10, 10); + p2.fill(Qt::red); + + QList<QPixmapCache::Key> keys; + for (int k = 0; k < 20000; ++k) + keys.append(QPixmapCache::insert(p2)); + + QPixmapCache::clear(); + + for (int k = 0; k < 20000; ++k) { + QVERIFY(QPixmapCache::find(keys.at(k), &p1) == 0); + QCOMPARE(getPrivate(keys[k])->isValid, false); } } +void tst_QPixmapCache::pixmapKey() +{ + QPixmapCache::Key key; + //Default constructed keys have no d pointer unless + //we use them + QVERIFY(!getPrivate(key)); + //Let's put a d pointer + QPixmapCache::KeyData** keyd = getPrivateRef(key); + *keyd = new QPixmapCache::KeyData; + QCOMPARE(getPrivate(key)->ref, 1); + QPixmapCache::Key key2; + //Let's put a d pointer + QPixmapCache::KeyData** key2d = getPrivateRef(key2); + *key2d = new QPixmapCache::KeyData; + QCOMPARE(getPrivate(key2)->ref, 1); + key = key2; + QCOMPARE(getPrivate(key2)->ref, 2); + QCOMPARE(getPrivate(key)->ref, 2); + QPixmapCache::Key key3; + //Let's put a d pointer + QPixmapCache::KeyData** key3d = getPrivateRef(key3); + *key3d = new QPixmapCache::KeyData; + QPixmapCache::Key key4 = key3; + QCOMPARE(getPrivate(key3)->ref, 2); + QCOMPARE(getPrivate(key4)->ref, 2); + key4 = key; + QCOMPARE(getPrivate(key4)->ref, 3); + QCOMPARE(getPrivate(key3)->ref, 1); + QPixmapCache::Key key5(key3); + QCOMPARE(getPrivate(key3)->ref, 2); + QCOMPARE(getPrivate(key5)->ref, 2); + + //let test default constructed keys + QPixmapCache::Key key6; + QVERIFY(!getPrivate(key6)); + QPixmapCache::Key key7; + QVERIFY(!getPrivate(key7)); + key6 = key7; + QVERIFY(!getPrivate(key6)); + QVERIFY(!getPrivate(key7)); + QPixmapCache::Key key8(key7); + QVERIFY(!getPrivate(key8)); +} + QTEST_MAIN(tst_QPixmapCache) #include "tst_qpixmapcache.moc" diff --git a/tests/auto/qpoint/tst_qpoint.cpp b/tests/auto/qpoint/tst_qpoint.cpp index 6f5a4e8..14a61ae 100644 --- a/tests/auto/qpoint/tst_qpoint.cpp +++ b/tests/auto/qpoint/tst_qpoint.cpp @@ -60,6 +60,8 @@ public: private slots: void getSetCheck(); void division(); + + void manhattanLength(); }; tst_QPoint::tst_QPoint() @@ -70,6 +72,24 @@ tst_QPoint::~tst_QPoint() { } + + +void tst_QPoint::manhattanLength() +{ + { + QPoint p(10, 20); + QCOMPARE(p.manhattanLength(), 30); + } + { + QPointF p(10., 20.); + QCOMPARE(p.manhattanLength(), 30.); + } + { + QPointF p(10.1, 20.2); + QCOMPARE(p.manhattanLength(), 30.3); + } +} + // Testing get/set functions void tst_QPoint::getSetCheck() { diff --git a/tests/auto/qprocess/qprocess.pro b/tests/auto/qprocess/qprocess.pro index 57d15ad..047828a 100644 --- a/tests/auto/qprocess/qprocess.pro +++ b/tests/auto/qprocess/qprocess.pro @@ -4,6 +4,7 @@ SUBDIRS = testProcessCrash \ testProcessEcho \ testProcessEcho2 \ testProcessEcho3 \ + testProcessEnvironment \ testProcessLoopback \ testProcessNormal \ testProcessOutput \ diff --git a/tests/auto/qprocess/testProcessEnvironment/main.cpp b/tests/auto/qprocess/testProcessEnvironment/main.cpp new file mode 100644 index 0000000..759bd54 --- /dev/null +++ b/tests/auto/qprocess/testProcessEnvironment/main.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char **argv) +{ +#if defined(_WIN32_WCE) + // no environment in Windows CE + return 0; +#else + if (argc == 1) + return 1; + + char *env = getenv(argv[1]); + if (env) { + printf("%s", env); + return 0; + } + return 1; +#endif +} diff --git a/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro b/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro new file mode 100644 index 0000000..14ddae5 --- /dev/null +++ b/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro @@ -0,0 +1,12 @@ +SOURCES = main.cpp +CONFIG -= qt +CONFIG += console +DESTDIR = ./ + +mac { + CONFIG -= app_bundle +} + +# no install rule for application used by test +INSTALLS = + diff --git a/tests/auto/qprocess/tst_qprocess.cpp b/tests/auto/qprocess/tst_qprocess.cpp index 6c6f4e3..bc2a87b 100644 --- a/tests/auto/qprocess/tst_qprocess.cpp +++ b/tests/auto/qprocess/tst_qprocess.cpp @@ -128,6 +128,8 @@ private slots: void spaceArgsTest_data(); void spaceArgsTest(); void exitCodeTest(); + void setEnvironment_data(); + void setEnvironment(); void systemEnvironment(); void spaceInName(); void lockupsInStartDetached(); @@ -1780,13 +1782,117 @@ void tst_QProcess::removeFileWhileProcessIsRunning() } //----------------------------------------------------------------------------- +void tst_QProcess::setEnvironment_data() +{ + QTest::addColumn<QString>("name"); + QTest::addColumn<QString>("value"); + + QTest::newRow("setting-empty") << "tst_QProcess" << ""; + QTest::newRow("setting") << "tst_QProcess" << "value"; + +#ifdef Q_OS_WIN + QTest::newRow("unsetting") << "PROMPT" << QString(); + QTest::newRow("overriding") << "PROMPT" << "value"; +#else + QTest::newRow("unsetting") << "PATH" << QString(); + QTest::newRow("overriding") << "PATH" << "value"; +#endif +} + +void tst_QProcess::setEnvironment() +{ +#if !defined (Q_OS_WINCE) + // there is no concept of system variables on Windows CE as there is no console + + // make sure our environment variables are correct + QVERIFY(qgetenv("tst_QProcess").isEmpty()); + QVERIFY(!qgetenv("PATH").isEmpty()); +#ifdef Q_OS_WIN + QVERIFY(!qgetenv("PROMPT").isEmpty()); +#endif + + QFETCH(QString, name); + QFETCH(QString, value); + QString executable = QDir::currentPath() + "/testProcessEnvironment/testProcessEnvironment"; + + { + QProcess process; + QStringList environment = QProcess::systemEnvironment(); + if (value.isNull()) { + int pos; + QRegExp rx(name + "=.*"); +#ifdef Q_OS_WIN + rx.setCaseSensitivity(Qt::CaseInsensitive); +#endif + while ((pos = environment.indexOf(rx)) != -1) + environment.removeAt(pos); + } else { + environment.append(name + '=' + value); + } + process.setEnvironment(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (value.isNull()) + QCOMPARE(process.exitCode(), 1); + else if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } + + // re-do the test but set the environment twice, to make sure + // that the latter addition overrides + // this test doesn't make sense in unsetting + if (!value.isNull()) { + QProcess process; + QStringList environment = QProcess::systemEnvironment(); + environment.prepend(name + "=This is not the right value"); + environment.append(name + '=' + value); + process.setEnvironment(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } + + // use the hash variant now + { + QProcess process; + QHash<QString, QString> environment = QProcess::systemEnvironmentHash(); + if (value.isNull()) + environment.remove(name); + else + environment.insert(name, value); + process.setEnvironmentHash(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (value.isNull()) + QCOMPARE(process.exitCode(), 1); + else if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } +#endif +} +//----------------------------------------------------------------------------- void tst_QProcess::systemEnvironment() { #if defined (Q_OS_WINCE) || defined(Q_OS_SYMBIAN) // there is no concept of system variables on Windows CE as there is no console QVERIFY(QProcess::systemEnvironment().isEmpty()); + QVERIFY(QProcess::systemEnvironmentHash().isEmpty()); #else QVERIFY(!QProcess::systemEnvironment().isEmpty()); + QVERIFY(!QProcess::systemEnvironmentHash().isEmpty()); + + QVERIFY(QProcess::systemEnvironmentHash().contains("PATH")); + QVERIFY(!QProcess::systemEnvironment().filter(QRegExp("^PATH=", Qt::CaseInsensitive)).isEmpty()); #endif } diff --git a/tests/auto/qpropertyanimation/qpropertyanimation.pro b/tests/auto/qpropertyanimation/qpropertyanimation.pro new file mode 100644 index 0000000..6d6ddbf --- /dev/null +++ b/tests/auto/qpropertyanimation/qpropertyanimation.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +QT = core gui +SOURCES += tst_qpropertyanimation.cpp + + diff --git a/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp b/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp new file mode 100644 index 0000000..a30e9f9 --- /dev/null +++ b/tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp @@ -0,0 +1,958 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <QtCore/qpropertyanimation.h> +#include <QtCore/qvariantanimation.h> +#include <QtGui/qwidget.h> + +//TESTED_CLASS=QPropertyAnimation +//TESTED_FILES= + +class UncontrolledAnimation : public QPropertyAnimation +{ + Q_OBJECT +public: + int duration() const { return -1; /* not time driven */ } + +protected: + void updateCurrentTime(int msecs) + { + QPropertyAnimation::updateCurrentTime(msecs); + if (msecs >= QPropertyAnimation::duration()) + stop(); + } +}; + +class MyObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x WRITE setX) +public: + MyObject() : m_x(0) { } + qreal x() const { return m_x; } + void setX(qreal x) { m_x = x; } +private: + qreal m_x; +}; + +class tst_QPropertyAnimation : public QObject +{ + Q_OBJECT +public: + tst_QPropertyAnimation(); + virtual ~tst_QPropertyAnimation(); + +public Q_SLOTS: + void init(); + void cleanup(); + +private slots: + void construction(); + void setCurrentTime_data(); + void setCurrentTime(); + void statesAndSignals_data(); + void statesAndSignals(); + void deletion1(); + void deletion2(); + void deletion3(); + void duration0(); + void noStartValue(); + void noStartValueWithLoop(); + void startWhenAnotherIsRunning(); + void easingcurve_data(); + void easingcurve(); + void startWithoutStartValue(); + void playForwardBackward(); + void interpolated(); + void setStartEndValues_data(); + void setStartEndValues(); + void zeroDurationStart(); + void operationsInStates_data(); + void operationsInStates(); + void oneKeyValue(); + void updateOnSetKeyValues(); +}; + +tst_QPropertyAnimation::tst_QPropertyAnimation() +{ +} + +tst_QPropertyAnimation::~tst_QPropertyAnimation() +{ +} + +void tst_QPropertyAnimation::init() +{ + qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State"); + qRegisterMetaType<QAbstractAnimation::DeletionPolicy>("QAbstractAnimation::DeletionPolicy"); +} + +void tst_QPropertyAnimation::cleanup() +{ +} + +class AnimationObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int value READ value WRITE setValue) + Q_PROPERTY(qreal realValue READ realValue WRITE setRealValue) +public: + AnimationObject(int startValue = 0) + : v(startValue) + { } + + int value() const { return v; } + void setValue(int value) { v = value; } + + qreal realValue() const { return rv; } + void setRealValue(qreal value) { rv = value; } + + int v; + qreal rv; +}; + + +void tst_QPropertyAnimation::construction() +{ + QPropertyAnimation panimation; +} + +void tst_QPropertyAnimation::setCurrentTime_data() +{ + QTest::addColumn<int>("duration"); + QTest::addColumn<int>("loopCount"); + QTest::addColumn<int>("currentTime"); + QTest::addColumn<int>("testCurrentTime"); + QTest::addColumn<int>("testCurrentLoop"); + + QTest::newRow("-1") << -1 << 1 << 0 << 0 << 0; + QTest::newRow("0") << 0 << 1 << 0 << 0 << 0; + QTest::newRow("1") << 0 << 1 << 1 << 0 << 0; + QTest::newRow("2") << 0 << 2 << 1 << 0 << 0; + QTest::newRow("3") << 1 << 1 << 0 << 0 << 0; + QTest::newRow("4") << 1 << 1 << 1 << 1 << 0; + QTest::newRow("5") << 1 << 2 << 1 << 0 << 1; + QTest::newRow("6") << 1 << 2 << 2 << 1 << 1; + QTest::newRow("7") << 1 << 2 << 3 << 1 << 1; + QTest::newRow("8") << 1 << 3 << 2 << 0 << 2; + QTest::newRow("9") << 1 << 3 << 3 << 1 << 2; + QTest::newRow("a") << 10 << 1 << 0 << 0 << 0; + QTest::newRow("b") << 10 << 1 << 1 << 1 << 0; + QTest::newRow("c") << 10 << 1 << 10 << 10 << 0; + QTest::newRow("d") << 10 << 2 << 10 << 0 << 1; + QTest::newRow("e") << 10 << 2 << 11 << 1 << 1; + QTest::newRow("f") << 10 << 2 << 20 << 10 << 1; + QTest::newRow("g") << 10 << 2 << 21 << 10 << 1; + QTest::newRow("negloop 0") << 10 << -1 << 0 << 0 << 0; + QTest::newRow("negloop 1") << 10 << -1 << 10 << 0 << 1; + QTest::newRow("negloop 2") << 10 << -1 << 15 << 5 << 1; + QTest::newRow("negloop 3") << 10 << -1 << 20 << 0 << 2; + QTest::newRow("negloop 4") << 10 << -1 << 30 << 0 << 3; +} + +void tst_QPropertyAnimation::setCurrentTime() +{ + QFETCH(int, duration); + QFETCH(int, loopCount); + QFETCH(int, currentTime); + QFETCH(int, testCurrentTime); + QFETCH(int, testCurrentLoop); + + QPropertyAnimation animation; + if (duration < 0) + QTest::ignoreMessage(QtWarningMsg, "QVariantAnimation::setDuration: cannot set a negative duration"); + animation.setDuration(duration); + animation.setLoopCount(loopCount); + animation.setCurrentTime(currentTime); + + QCOMPARE(animation.currentTime(), testCurrentTime); + QCOMPARE(animation.currentLoop(), testCurrentLoop); +} + +void tst_QPropertyAnimation::statesAndSignals_data() +{ + QTest::addColumn<bool>("uncontrolled"); + QTest::newRow("normal animation") << false; + QTest::newRow("animation with undefined duration") << true; +} + +void tst_QPropertyAnimation::statesAndSignals() +{ + QFETCH(bool, uncontrolled); + QPropertyAnimation *anim = uncontrolled ? new UncontrolledAnimation : new QPropertyAnimation; + anim->setDuration(100); + + QSignalSpy finishedSpy(anim, SIGNAL(finished())); + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy currentLoopSpy(anim, SIGNAL(currentLoopChanged(int))); + + anim->setCurrentTime(1); + anim->setCurrentTime(100); + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(runningSpy.count(), 0); + QCOMPARE(currentLoopSpy.count(), 0); + QCOMPARE(anim->state(), QAnimationGroup::Stopped); + + anim->setLoopCount(3); + anim->setCurrentTime(101); + + if (uncontrolled) + QSKIP("Uncontrolled animations don't handle looping", SkipSingle); + + QCOMPARE(currentLoopSpy.count(), 1); + QCOMPARE(anim->currentLoop(), 1); + + anim->setCurrentTime(0); + QCOMPARE(currentLoopSpy.count(), 2); + QCOMPARE(anim->currentLoop(), 0); + + anim->start(); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QCOMPARE(runningSpy.count(), 1); //anim must have started + QCOMPARE(anim->currentLoop(), 0); + runningSpy.clear(); + + anim->stop(); + QCOMPARE(anim->state(), QAnimationGroup::Stopped); + QCOMPARE(runningSpy.count(), 1); //anim must have stopped + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(anim->currentTime(), 0); + QCOMPARE(anim->currentLoop(), 0); + QCOMPARE(currentLoopSpy.count(), 2); + runningSpy.clear(); + + anim->start(); + QTest::qWait(1000); + QCOMPARE(anim->state(), QAnimationGroup::Stopped); + QCOMPARE(runningSpy.count(), 2); //started and stopped again + runningSpy.clear(); + QCOMPARE(finishedSpy.count(), 1); + QCOMPARE(anim->currentTime(), 100); + QCOMPARE(anim->currentLoop(), 2); + QCOMPARE(currentLoopSpy.count(), 4); + + anim->start(); // auto-rewinds + QCOMPARE(anim->state(), QAnimationGroup::Running); + QCOMPARE(anim->currentTime(), 0); + QCOMPARE(anim->currentLoop(), 0); + QCOMPARE(currentLoopSpy.count(), 5); + QCOMPARE(runningSpy.count(), 1); // anim has started + QCOMPARE(finishedSpy.count(), 1); + QCOMPARE(anim->currentLoop(), 0); + runningSpy.clear(); + + QTest::qWait(1000); + + QCOMPARE(currentLoopSpy.count(), 7); + QCOMPARE(anim->state(), QAnimationGroup::Stopped); + QCOMPARE(anim->currentLoop(), 2); + QCOMPARE(runningSpy.count(), 1); // anim has stopped + QCOMPARE(finishedSpy.count(), 2); + QCOMPARE(anim->currentTime(), 100); + + delete anim; +} + +void tst_QPropertyAnimation::deletion1() +{ + QObject *object = new QWidget; + QPointer<QPropertyAnimation> anim = new QPropertyAnimation(object, "minimumWidth"); + + //test that the animation is deleted correctly depending of the deletion flag passed in start() + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy finishedSpy(anim, SIGNAL(finished())); + anim->setStartValue(10); + anim->setEndValue(20); + anim->setDuration(200); + anim->start(); + QCOMPARE(runningSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + + QVERIFY(anim); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QTest::qWait(100); + QVERIFY(anim); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QTest::qWait(150); + QVERIFY(anim); //The animation should not have been deleted + QCOMPARE(anim->state(), QAnimationGroup::Stopped); + QCOMPARE(runningSpy.count(), 2); + QCOMPARE(finishedSpy.count(), 1); + + anim->start(QVariantAnimation::DeleteWhenStopped); + QVERIFY(anim); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QTest::qWait(100); + QVERIFY(anim); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QTest::qWait(150); + QVERIFY(!anim); //The animation must have been deleted + QCOMPARE(runningSpy.count(), 4); + QCOMPARE(finishedSpy.count(), 2); + delete object; +} + +void tst_QPropertyAnimation::deletion2() +{ + //test that the animation get deleted if the object is deleted + QObject *object = new QWidget; + QPointer<QPropertyAnimation> anim = new QPropertyAnimation(object,"minimumWidth"); + anim->setStartValue(10); + anim->setEndValue(20); + anim->setDuration(200); + + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy finishedSpy(anim, SIGNAL(finished())); + + anim->setStartValue(10); + anim->setEndValue(20); + anim->setDuration(200); + anim->start(); + + QTest::qWait(50); + QVERIFY(anim); + QCOMPARE(anim->state(), QAnimationGroup::Running); + + QCOMPARE(runningSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + + //we can't call deletaLater directly because the delete would only happen in the next loop of _this_ event loop + QTimer::singleShot(0, object, SLOT(deleteLater())); + QTest::qWait(50); + + QVERIFY(anim->targetObject() == 0); +} + +void tst_QPropertyAnimation::deletion3() +{ + //test that the stopped signal is emit when the animation is destroyed + QObject *object = new QWidget; + QPropertyAnimation *anim = new QPropertyAnimation(object,"minimumWidth"); + anim->setStartValue(10); + anim->setEndValue(20); + anim->setDuration(200); + + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy finishedSpy(anim, SIGNAL(finished())); + anim->start(); + + QTest::qWait(50); + QCOMPARE(anim->state(), QAnimationGroup::Running); + QCOMPARE(runningSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + delete anim; + QCOMPARE(runningSpy.count(), 2); + QCOMPARE(finishedSpy.count(), 0); +} + +void tst_QPropertyAnimation::duration0() +{ + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation animation(&o, "ole"); + animation.setEndValue(43); + QVERIFY(!animation.currentValue().isValid()); + QCOMPARE(animation.currentValue().toInt(), 0); + QCOMPARE(o.property("ole").toInt(), 42); + animation.setDuration(0); + animation.start(); + QCOMPARE(animation.state(), QAnimationGroup::Stopped); + QCOMPARE(animation.currentTime(), 0); + QCOMPARE(o.property("ole").toInt(), 43); +} + +class StartValueTester : public QObject +{ + Q_OBJECT + Q_PROPERTY(int ole READ ole WRITE setOle) +public: + StartValueTester() : o(0) { } + int ole() const { return o; } + void setOle(int v) { o = v; values << v; } + + int o; + QList<int> values; +}; + +void tst_QPropertyAnimation::noStartValue() +{ + StartValueTester o; + o.setProperty("ole", 42); + o.values.clear(); + + QPropertyAnimation a(&o, "ole"); + a.setEndValue(420); + a.setDuration(250); + a.start(); + + QTest::qWait(300); + + QCOMPARE(o.values.first(), 42); + QCOMPARE(o.values.last(), 420); +} + +void tst_QPropertyAnimation::noStartValueWithLoop() +{ + StartValueTester o; + o.setProperty("ole", 42); + o.values.clear(); + + QPropertyAnimation a(&o, "ole"); + a.setEndValue(420); + a.setDuration(250); + a.setLoopCount(2); + a.start(); + + a.setCurrentTime(250); + QCOMPARE(o.values.first(), 42); + QCOMPARE(a.currentValue().toInt(), 42); + QCOMPARE(o.values.last(), 42); + + a.setCurrentTime(500); + QCOMPARE(a.currentValue().toInt(), 420); +} + +void tst_QPropertyAnimation::startWhenAnotherIsRunning() +{ + StartValueTester o; + o.setProperty("ole", 42); + o.values.clear(); + + { + //normal case: the animation finishes and is deleted + QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole"); + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + anim->start(QVariantAnimation::DeleteWhenStopped); + QTest::qWait(anim->duration() + 50); + QCOMPARE(runningSpy.count(), 2); //started and then stopped + QVERIFY(!anim); + } + + { + QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole"); + QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + anim->start(QVariantAnimation::DeleteWhenStopped); + QTest::qWait(anim->duration()/2); + QPointer<QVariantAnimation> anim2 = new QPropertyAnimation(&o, "ole"); + QCOMPARE(runningSpy.count(), 1); + QCOMPARE(anim->state(), QVariantAnimation::Running); + + //anim2 will interrupt anim1 + QMetaObject::invokeMethod(anim2, "start", Qt::QueuedConnection, Q_ARG(QAbstractAnimation::DeletionPolicy, QVariantAnimation::DeleteWhenStopped)); + QTest::qWait(50); + QVERIFY(!anim); //anim should have been deleted + QVERIFY(anim2); + QTest::qWait(anim2->duration()); + QVERIFY(!anim2); //anim2 is finished: it should have been deleted by now + QVERIFY(!anim); + } + +} + +// copy from easing.cpp in case that function changes definition +static qreal easeInOutBack(qreal t) +{ + qreal s = 1.70158; + qreal t_adj = 2.0f * (qreal)t; + if (t_adj < 1) { + s *= 1.525f; + return 1.0/2*(t_adj*t_adj*((s+1)*t_adj - s)); + } else { + t_adj -= 2; + s *= 1.525f; + return 1.0/2*(t_adj*t_adj*((s+1)*t_adj + s) + 2); + } +} + +void tst_QPropertyAnimation::easingcurve_data() +{ + QTest::addColumn<int>("currentTime"); + QTest::addColumn<int>("expectedvalue"); + + QTest::newRow("interpolation1") << 0 << 0; + QTest::newRow("interpolation2") << 1000 << 1000; + QTest::newRow("extrapolationbelow") << 250 << -99; + QTest::newRow("extrapolationabove") << 750 << 1099; +} + +void tst_QPropertyAnimation::easingcurve() +{ + QFETCH(int, currentTime); + QFETCH(int, expectedvalue); + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation pAnimation(&o, "ole"); + pAnimation.setStartValue(0); + pAnimation.setEndValue(1000); + pAnimation.setDuration(1000); + + // this easingcurve assumes that we extrapolate before startValue and after endValue + QEasingCurve easingCurve; + easingCurve.setCustomType(easeInOutBack); + pAnimation.setEasingCurve(easingCurve); + pAnimation.start(); + pAnimation.pause(); + pAnimation.setCurrentTime(currentTime); + QCOMPARE(o.property("ole").toInt(), expectedvalue); +} + +void tst_QPropertyAnimation::startWithoutStartValue() +{ + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation anim(&o, "ole"); + anim.setEndValue(100); + + anim.start(); + + QTest::qWait(100); + int current = anim.currentValue().toInt(); + //it is somewhere in the animation + QVERIFY(current > 42); + QVERIFY(current < 100); + + QTest::qWait(200); + QCOMPARE(anim.state(), QVariantAnimation::Stopped); + + anim.setEndValue(110); + anim.start(); + current = anim.currentValue().toInt(); + // the default start value will reevaluate the current property + // and set it to the end value of the last iteration + QCOMPARE(current, 100); + QTest::qWait(100); + current = anim.currentValue().toInt(); + //it is somewhere in the animation + QVERIFY(current >= 100); + QVERIFY(current <= 110); +} + +void tst_QPropertyAnimation::playForwardBackward() +{ + QObject o; + o.setProperty("ole", 0); + QCOMPARE(o.property("ole").toInt(), 0); + + QPropertyAnimation anim(&o, "ole"); + anim.setEndValue(100); + anim.start(); + QTest::qWait(anim.duration() + 50); + QCOMPARE(anim.state(), QAbstractAnimation::Stopped); + QCOMPARE(anim.currentTime(), anim.duration()); + + //the animation is at the end + anim.setDirection(QVariantAnimation::Backward); + anim.start(); + QCOMPARE(anim.state(), QAbstractAnimation::Running); + QTest::qWait(anim.duration() + 50); + QCOMPARE(anim.state(), QAbstractAnimation::Stopped); + QCOMPARE(anim.currentTime(), 0); + + //the direction is backward + //restarting should jump to the end + anim.start(); + QCOMPARE(anim.state(), QAbstractAnimation::Running); + QCOMPARE(anim.currentTime(), anim.duration()); + QTest::qWait(anim.duration() + 50); + QCOMPARE(anim.state(), QAbstractAnimation::Stopped); + QCOMPARE(anim.currentTime(), 0); +} + +struct Number +{ + Number() {} + Number(int n) + : n(n) {} + + Number(const Number &other) + : n(other.n){} + + Number &operator=(const Number &other) { + n = other.n; + return *this; + } + bool operator==(const Number &other) const { + return n == other.n; + } + + int n; +}; + +Q_DECLARE_METATYPE(Number) +Q_DECLARE_METATYPE(QAbstractAnimation::State) + +QVariant numberInterpolator(const Number &f, const Number &t, qreal progress) +{ + return qVariantFromValue<Number>(Number(f.n + (t.n - f.n)*progress)); +} + +QVariant xaxisQPointInterpolator(const QPointF &f, const QPointF &t, qreal progress) +{ + return QPointF(f.x() + (t.x() - f.x())*progress, f.y()); +} + +void tst_QPropertyAnimation::interpolated() +{ + QObject o; + o.setProperty("point", QPointF()); //this will avoid warnings + o.setProperty("number", qVariantFromValue<Number>(Number(42))); + QCOMPARE(qVariantValue<Number>(o.property("number")), Number(42)); + { + qRegisterAnimationInterpolator<Number>(numberInterpolator); + QPropertyAnimation anim(&o, "number"); + anim.setStartValue(qVariantFromValue<Number>(Number(0))); + anim.setEndValue(qVariantFromValue<Number>(Number(100))); + anim.setDuration(1000); + anim.start(); + anim.pause(); + anim.setCurrentTime(100); + Number t(qVariantValue<Number>(o.property("number"))); + QCOMPARE(t, Number(10)); + anim.setCurrentTime(500); + QCOMPARE(qVariantValue<Number>(o.property("number")), Number(50)); + } + { + qRegisterAnimationInterpolator<QPointF>(xaxisQPointInterpolator); + QPropertyAnimation anim(&o, "point"); + anim.setStartValue(QPointF(0,0)); + anim.setEndValue(QPointF(100, 100)); + anim.setDuration(1000); + anim.start(); + anim.pause(); + anim.setCurrentTime(100); + QCOMPARE(o.property("point"), QVariant(QPointF(10, 0))); + anim.setCurrentTime(500); + QCOMPARE(o.property("point"), QVariant(QPointF(50, 0))); + } + { + // unregister it and see if we get back the default behaviour + qRegisterAnimationInterpolator<QPointF>(0); + QPropertyAnimation anim(&o, "point"); + anim.setStartValue(QPointF(0,0)); + anim.setEndValue(QPointF(100, 100)); + anim.setDuration(1000); + anim.start(); + anim.pause(); + anim.setCurrentTime(100); + QCOMPARE(o.property("point").toPointF(), QPointF(10, 10)); + anim.setCurrentTime(500); + QCOMPARE(o.property("point").toPointF(), QPointF(50, 50)); + } + + { + // Interpolate a qreal property with a int interpolator + AnimationObject o1; + o1.setRealValue(42.42); + QPropertyAnimation anim(&o1, "realValue"); + anim.setStartValue(0); + anim.setEndValue(100); + anim.start(); + QCOMPARE(o1.realValue(), qreal(0)); + anim.setCurrentTime(250); + QCOMPARE(o1.realValue(), qreal(100)); + } +} + +Q_DECLARE_METATYPE(QVariant) + +void tst_QPropertyAnimation::setStartEndValues_data() +{ + QTest::addColumn<QByteArray>("propertyName"); + QTest::addColumn<QVariant>("initialValue"); + QTest::addColumn<QVariant>("startValue"); + QTest::addColumn<QVariant>("endValue"); + + QTest::newRow("dynamic property") << QByteArray("ole") << QVariant(42) << QVariant(0) << QVariant(10); + QTest::newRow("real property, with unmatching types") << QByteArray("x") << QVariant(42.) << QVariant(0) << QVariant(10.); +} + +void tst_QPropertyAnimation::setStartEndValues() +{ + MyObject object; + QFETCH(QByteArray, propertyName); + QFETCH(QVariant, initialValue); + QFETCH(QVariant, startValue); + QFETCH(QVariant, endValue); + + //this tests the start value, end value and default start value + object.setProperty(propertyName, initialValue); + QPropertyAnimation anim(&object, propertyName); + QVariantAnimation::KeyValues values; + QCOMPARE(anim.keyValues(), values); + + //let's add a start value + anim.setStartValue(startValue); + values << QVariantAnimation::KeyValue(0, startValue); + QCOMPARE(anim.keyValues(), values); + + anim.setEndValue(endValue); + values << QVariantAnimation::KeyValue(1, endValue); + QCOMPARE(anim.keyValues(), values); + + //now we can play with objects + QCOMPARE(object.property(propertyName).toDouble(), initialValue.toDouble()); + anim.start(); + QVERIFY(anim.startValue().isValid()); + QCOMPARE(object.property(propertyName), anim.startValue()); + anim.setCurrentTime(anim.duration()/2); + QCOMPARE(object.property(propertyName).toDouble(), (startValue.toDouble() + endValue.toDouble())/2 ); //just in the middle of the animation + anim.setCurrentTime(anim.duration()); //we go to the end of the animation + QCOMPARE(anim.state(), QAnimationGroup::Stopped); //it should have stopped + QVERIFY(anim.endValue().isValid()); + QCOMPARE(object.property(propertyName), anim.endValue()); //end of the animations + + //now we remove the explicit start value and test the implicit one + anim.stop(); + object.setProperty(propertyName, initialValue); + + //let's reset the start value + values.remove(0); + anim.setStartValue(QVariant()); + QCOMPARE(anim.keyValues(), values); + QVERIFY(!anim.startValue().isValid()); + + anim.start(); + QCOMPARE(object.property(propertyName), initialValue); + anim.setCurrentTime(anim.duration()/2); + QCOMPARE(object.property(propertyName).toDouble(), (initialValue.toDouble() + endValue.toDouble())/2 ); //just in the middle of the animation + anim.setCurrentTime(anim.duration()); //we go to the end of the animation + QCOMPARE(anim.state(), QAnimationGroup::Stopped); //it should have stopped + QVERIFY(anim.endValue().isValid()); + QCOMPARE(object.property(propertyName), anim.endValue()); //end of the animations + + //now we set back the startValue + anim.setStartValue(startValue); + QVERIFY(anim.startValue().isValid()); + anim.start(); + QCOMPARE(object.property(propertyName), startValue); +} + +void tst_QPropertyAnimation::zeroDurationStart() +{ + QPropertyAnimation anim; + QSignalSpy spy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + anim.setDuration(0); + QCOMPARE(anim.state(), QAbstractAnimation::Stopped); + anim.start(); + //the animation stops immediately + QCOMPARE(anim.state(), QAbstractAnimation::Stopped); + QCOMPARE(spy.count(), 2); + + //let's check the first state change + const QVariantList firstChange = spy.first(); + //old state + QCOMPARE(qVariantValue<QAbstractAnimation::State>(firstChange.first()), QAbstractAnimation::Stopped); + //new state + QCOMPARE(qVariantValue<QAbstractAnimation::State>(firstChange.last()), QAbstractAnimation::Running); + + //let's check the first state change + const QVariantList secondChange = spy.last(); + //old state + QCOMPARE(qVariantValue<QAbstractAnimation::State>(secondChange.first()), QAbstractAnimation::Running); + //new state + QCOMPARE(qVariantValue<QAbstractAnimation::State>(secondChange.last()), QAbstractAnimation::Stopped); +} + +#define Pause 1 +#define Start 2 +#define Resume 3 +#define Stop 4 + +void tst_QPropertyAnimation::operationsInStates_data() +{ + QTest::addColumn<QAbstractAnimation::State>("originState"); + QTest::addColumn<int>("operation"); + QTest::addColumn<QString>("expectedWarning"); + QTest::addColumn<QAbstractAnimation::State>("expectedState"); + + QString pauseWarn(QLatin1String("QAbstractAnimation::pause: Cannot pause a stopped animation")); + QString resumeWarn(QLatin1String("QAbstractAnimation::resume: Cannot resume an animation that is not paused")); + + QTest::newRow("S-pause") << QAbstractAnimation::Stopped << Pause << pauseWarn << QAbstractAnimation::Stopped; + QTest::newRow("S-start") << QAbstractAnimation::Stopped << Start << QString() << QAbstractAnimation::Running; + QTest::newRow("S-resume") << QAbstractAnimation::Stopped << Resume << resumeWarn << QAbstractAnimation::Stopped; + QTest::newRow("S-stop") << QAbstractAnimation::Stopped << Stop << QString() << QAbstractAnimation::Stopped; + + QTest::newRow("P-pause") << QAbstractAnimation::Paused << Pause << QString() << QAbstractAnimation::Paused; + QTest::newRow("P-start") << QAbstractAnimation::Paused << Start << QString() << QAbstractAnimation::Running; + QTest::newRow("P-resume") << QAbstractAnimation::Paused << Resume << QString() << QAbstractAnimation::Running; + QTest::newRow("P-stop") << QAbstractAnimation::Paused << Stop << QString() << QAbstractAnimation::Stopped; + + QTest::newRow("R-pause") << QAbstractAnimation::Running << Pause << QString() << QAbstractAnimation::Paused; + QTest::newRow("R-start") << QAbstractAnimation::Running << Start << QString() << QAbstractAnimation::Running; + QTest::newRow("R-resume") << QAbstractAnimation::Running << Resume << resumeWarn << QAbstractAnimation::Running; + QTest::newRow("R-stop") << QAbstractAnimation::Running << Stop << QString() << QAbstractAnimation::Stopped; +} + +void tst_QPropertyAnimation::operationsInStates() +{ +/** + * | pause() |start() |resume() |stop() + * ----------+------------+-----------+-----------+-------------------+ + * Stopped | Stopped |Running |Stopped |Stopped | + * _| qWarning |restart |qWarning | | + * Paused | Paused |Running |Running |Stopped | + * _| | | | | + * Running | Paused |Running |Running |Stopped | + * | |restart |qWarning | | + * ----------+------------+-----------+-----------+-------------------+ +**/ + + QFETCH(QAbstractAnimation::State, originState); + QFETCH(int, operation); + QFETCH(QString, expectedWarning); + QFETCH(QAbstractAnimation::State, expectedState); + + QObject o; + o.setProperty("ole", 42); + QPropertyAnimation anim(&o, "ole"); + QSignalSpy spy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + anim.stop(); + switch (originState) { + case QAbstractAnimation::Stopped: + break; + case QAbstractAnimation::Paused: + anim.start(); + anim.pause(); + break; + case QAbstractAnimation::Running: + anim.start(); + break; + } + if (!expectedWarning.isEmpty()) { + QTest::ignoreMessage(QtWarningMsg, qPrintable(expectedWarning)); + } + QCOMPARE(anim.state(), originState); + switch (operation) { + case Pause: + anim.pause(); + break; + case Start: + anim.start(); + break; + case Resume: + anim.resume(); + break; + case Stop: + anim.stop(); + break; + } + + QCOMPARE(anim.state(), expectedState); +} +#undef Pause +#undef Start +#undef Resume +#undef Stop + +void tst_QPropertyAnimation::oneKeyValue() +{ + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation animation(&o, "ole"); + animation.setStartValue(43); + animation.setEndValue(44); + animation.setDuration(100); + + animation.setCurrentTime(0); + + QVERIFY(animation.currentValue().isValid()); + QCOMPARE(animation.currentValue().toInt(), 43); + QCOMPARE(o.property("ole").toInt(), 42); + + // remove the last key value + animation.setKeyValueAt(1.0, QVariant()); + + // we will neither interpolate, nor update the current value + // since there is only one 1 key value defined + animation.setCurrentTime(100); + + // the animation should not have been modified + QVERIFY(animation.currentValue().isValid()); + QCOMPARE(animation.currentValue().toInt(), 43); + QCOMPARE(o.property("ole").toInt(), 42); +} + +void tst_QPropertyAnimation::updateOnSetKeyValues() +{ + QObject o; + o.setProperty("ole", 100); + QCOMPARE(o.property("ole").toInt(), 100); + + QPropertyAnimation animation(&o, "ole"); + animation.setStartValue(100); + animation.setEndValue(200); + animation.setDuration(100); + + animation.setCurrentTime(50); + QCOMPARE(animation.currentValue().toInt(), 150); + animation.setKeyValueAt(0.0, 300); + QCOMPARE(animation.currentValue().toInt(), 250); + + o.setProperty("ole", 100); + QPropertyAnimation animation2(&o, "ole"); + QVariantAnimation::KeyValues kValues; + kValues << QVariantAnimation::KeyValue(0.0, 100) << QVariantAnimation::KeyValue(1.0, 200); + animation2.setKeyValues(kValues); + animation2.setDuration(100); + animation2.setCurrentTime(50); + QCOMPARE(animation2.currentValue().toInt(), 150); + + kValues.clear(); + kValues << QVariantAnimation::KeyValue(0.0, 300) << QVariantAnimation::KeyValue(1.0, 200); + animation2.setKeyValues(kValues); + + QCOMPARE(animation2.currentValue().toInt(), animation.currentValue().toInt()); +} + +QTEST_MAIN(tst_QPropertyAnimation) +#include "tst_qpropertyanimation.moc" diff --git a/tests/auto/qpushbutton/tst_qpushbutton.cpp b/tests/auto/qpushbutton/tst_qpushbutton.cpp index cf79ffc..7a81dbf 100644 --- a/tests/auto/qpushbutton/tst_qpushbutton.cpp +++ b/tests/auto/qpushbutton/tst_qpushbutton.cpp @@ -51,6 +51,8 @@ #include <qtimer.h> #include <QDialog> #include <QGridLayout> +#include <QStyleFactory> +#include <QTabWidget> Q_DECLARE_METATYPE(QPushButton*) @@ -90,6 +92,8 @@ private slots: void toggled(); void isEnabled(); void defaultAndAutoDefault(); + void sizeHint_data(); + void sizeHint(); /* void state(); void group(); @@ -590,5 +594,77 @@ void tst_QPushButton::defaultAndAutoDefault() } } +void tst_QPushButton::sizeHint_data() +{ + QTest::addColumn<QString>("stylename"); + QTest::newRow("motif") << QString::fromAscii("motif"); + QTest::newRow("cde") << QString::fromAscii("cde"); + QTest::newRow("windows") << QString::fromAscii("windows"); + QTest::newRow("cleanlooks") << QString::fromAscii("cleanlooks"); + QTest::newRow("gtk") << QString::fromAscii("gtk"); + QTest::newRow("mac") << QString::fromAscii("mac"); + QTest::newRow("plastique") << QString::fromAscii("plastique"); + QTest::newRow("windowsxp") << QString::fromAscii("windowsxp"); + QTest::newRow("windowsvista") << QString::fromAscii("windowsvista"); +} + +void tst_QPushButton::sizeHint() +{ + QFETCH(QString, stylename); + + QStyle *style = QStyleFactory::create(stylename); + if (!style) + QSKIP(qPrintable(QString::fromLatin1("Qt has been compiled without style: %1") + .arg(stylename)), SkipSingle); + QApplication::setStyle(style); + +// Test 1 + { + QPushButton *button = new QPushButton("123"); + QSize initSizeHint = button->sizeHint(); + + QDialog *dialog = new QDialog; + QWidget *widget = new QWidget(dialog); + button->setParent(widget); + button->sizeHint(); + + widget->setParent(0); + delete dialog; + button->setDefault(false); + QCOMPARE(button->sizeHint(), initSizeHint); + delete button; + } + +// Test 2 + { + QWidget *tab1 = new QWidget; + QHBoxLayout *layout1 = new QHBoxLayout(tab1); + QPushButton *button1_1 = new QPushButton("123"); + QPushButton *button1_2 = new QPushButton("123"); + layout1->addWidget(button1_1); + layout1->addWidget(button1_2); + + QWidget *tab2 = new QWidget; + QHBoxLayout *layout2 = new QHBoxLayout(tab2); + QPushButton *button2_1 = new QPushButton("123"); + QPushButton *button2_2 = new QPushButton("123"); + layout2->addWidget(button2_1); + layout2->addWidget(button2_2); + + QDialog *dialog = new QDialog; + QTabWidget *tabWidget = new QTabWidget; + tabWidget->addTab(tab1, "1"); + tabWidget->addTab(tab2, "2"); + QVBoxLayout *mainLayout = new QVBoxLayout(dialog); + mainLayout->addWidget(tabWidget); + dialog->show(); + tabWidget->setCurrentWidget(tab2); + tabWidget->setCurrentWidget(tab1); + QTest::qWait(100); + + QCOMPARE(button1_2->size(), button2_2->size()); + } +} + QTEST_MAIN(tst_QPushButton) #include "tst_qpushbutton.moc" diff --git a/tests/auto/qringbuffer/qringbuffer.pro b/tests/auto/qringbuffer/qringbuffer.pro new file mode 100644 index 0000000..91fb0a0 --- /dev/null +++ b/tests/auto/qringbuffer/qringbuffer.pro @@ -0,0 +1,6 @@ +load(qttest_p4) +SOURCES += tst_qringbuffer.cpp + +QT = core + + diff --git a/tests/auto/qringbuffer/tst_qringbuffer.cpp b/tests/auto/qringbuffer/tst_qringbuffer.cpp new file mode 100644 index 0000000..93257ad --- /dev/null +++ b/tests/auto/qringbuffer/tst_qringbuffer.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include <private/qringbuffer_p.h> + +class tst_QRingBuffer : public QObject +{ + Q_OBJECT + +public: + tst_QRingBuffer(); + virtual ~tst_QRingBuffer(); +public slots: + void initTestCase(); + void cleanupTestCase(); +private slots: + void readPointerAtPositionWriteRead(); + void readPointerAtPositionEmptyRead(); + void readPointerAtPositionWithHead(); + void readPointerAtPositionReadTooMuch(); + void sizeWhenEmpty(); + void sizeWhenReservedAndChopped(); + void sizeWhenReserved(); +}; + +tst_QRingBuffer::tst_QRingBuffer() +{ +} + +tst_QRingBuffer::~tst_QRingBuffer() +{ +} + +void tst_QRingBuffer::initTestCase() +{ +} + +void tst_QRingBuffer::cleanupTestCase() +{ +} + +void tst_QRingBuffer::sizeWhenReserved() +{ + QRingBuffer ringBuffer; + ringBuffer.reserve(5); + + QCOMPARE(ringBuffer.size(), 5); +} + +void tst_QRingBuffer::sizeWhenReservedAndChopped() +{ + QRingBuffer ringBuffer; + ringBuffer.reserve(31337); + ringBuffer.chop(31337); + + QCOMPARE(ringBuffer.size(), 0); +} + +void tst_QRingBuffer::sizeWhenEmpty() +{ + QRingBuffer ringBuffer; + + QCOMPARE(ringBuffer.size(), 0); +} + +void tst_QRingBuffer::readPointerAtPositionReadTooMuch() +{ + QRingBuffer ringBuffer; + + qint64 length; + const char *buf = ringBuffer.readPointerAtPosition(42, length); + QVERIFY(buf == 0); + QVERIFY(length == 0); +} + +void tst_QRingBuffer::readPointerAtPositionWithHead() +{ + QRingBuffer ringBuffer; + char *buf = ringBuffer.reserve(4); + memcpy (buf, "0123", 4); + ringBuffer.free(2); + + // ringBuffer should have stayed the same except + // its head it had moved to position 2 + qint64 length; + const char* buf2 = ringBuffer.readPointerAtPosition(0, length); + + QCOMPARE(length, qint64(2)); + QVERIFY(*buf2 == '2'); + QVERIFY(*(buf2+1) == '3'); + + // advance 2 more, ringBuffer should be empty then + ringBuffer.free(2); + buf2 = ringBuffer.readPointerAtPosition(0, length); + QCOMPARE(length, qint64(0)); + QVERIFY(buf2 == 0); +} + +void tst_QRingBuffer::readPointerAtPositionEmptyRead() +{ + QRingBuffer ringBuffer; + + qint64 length; + const char *buf = ringBuffer.readPointerAtPosition(0, length); + QVERIFY(buf == 0); + QVERIFY(length == 0); +} + +void tst_QRingBuffer::readPointerAtPositionWriteRead() +{ + //create some data + QBuffer inData; + inData.open(QIODevice::ReadWrite); + inData.putChar(0x42); + inData.putChar(0x23); + inData.write("Qt rocks!"); + for (int i = 0; i < 5000; i++) + inData.write(QString("Number %1").arg(i).toUtf8()); + inData.reset(); + QVERIFY(inData.size() > 0); + + //put the inData in the QRingBuffer + QRingBuffer ringBuffer; + qint64 remaining = inData.size(); + while (remaining > 0) { + // write in chunks of 50 bytes + // this ensures there will be multiple QByteArrays inside the QRingBuffer + // since QRingBuffer is then only using individual arrays of around 4000 bytes + qint64 thisWrite = qMin(remaining, qint64(50)); + char *pos = ringBuffer.reserve(thisWrite); + inData.read(pos, thisWrite); + remaining -= thisWrite; + } + // was data put into it? + QVERIFY(ringBuffer.size() > 0); + QCOMPARE(qint64(ringBuffer.size()), inData.size()); + + //read from the QRingBuffer in loop, put back into another QBuffer + QBuffer outData; + outData.open(QIODevice::ReadWrite); + remaining = ringBuffer.size(); + while (remaining > 0) { + qint64 thisRead; + // always try to read as much as possible + const char *buf = ringBuffer.readPointerAtPosition(ringBuffer.size() - remaining, thisRead); + outData.write(buf, thisRead); + remaining -= thisRead; + } + outData.reset(); + + QVERIFY(outData.size() > 0); + + // was the data read from the QRingBuffer the same as the one written into it? + QCOMPARE(outData.size(), inData.size()); + QVERIFY(outData.buffer().startsWith(inData.buffer())); +} + + +QTEST_APPLESS_MAIN(tst_QRingBuffer) +#include "tst_qringbuffer.moc" diff --git a/tests/auto/qscriptable/tst_qscriptable.cpp b/tests/auto/qscriptable/tst_qscriptable.cpp index 40a57ab..7900d25 100644 --- a/tests/auto/qscriptable/tst_qscriptable.cpp +++ b/tests/auto/qscriptable/tst_qscriptable.cpp @@ -49,7 +49,7 @@ //TESTED_CLASS= //TESTED_FILES= -class MyScriptable : public QObject, protected QScriptable +class MyScriptable : public QObject, public QScriptable { Q_OBJECT Q_PROPERTY(int baz READ baz WRITE setBaz) @@ -87,6 +87,7 @@ public slots: QObject *zab(); QObject *setZab(QObject *); QScriptValue getArguments(); + int getArgumentCount(); signals: void sig(int); @@ -128,6 +129,11 @@ QScriptValue MyScriptable::getArguments() return context()->argumentsObject(); } +int MyScriptable::getArgumentCount() +{ + return context()->argumentCount(); +} + void MyScriptable::foo() { m_lastEngine = engine(); @@ -224,6 +230,8 @@ void tst_QScriptable::cleanupTestCase() void tst_QScriptable::engine() { + QCOMPARE(m_scriptable.engine(), (QScriptEngine*)0); + QCOMPARE(m_scriptable.context(), (QScriptContext*)0); QCOMPARE(m_scriptable.lastEngine(), (QScriptEngine *)0); // reading property @@ -360,6 +368,14 @@ void tst_QScriptable::arguments() QVERIFY(args.property("1").strictlyEquals(QScriptValue(&m_engine, 20))); QVERIFY(args.property("2").strictlyEquals(QScriptValue(&m_engine, 30))); QVERIFY(args.property("3").strictlyEquals(QScriptValue(&m_engine, "hi"))); + + QScriptValue argc = m_engine.evaluate("scriptable.getArgumentCount(1, 2, 3)"); + QVERIFY(argc.isNumber()); + QCOMPARE(argc.toInt32(), 3); + + QCOMPARE(m_scriptable.argumentCount(), -1); + QVERIFY(!m_scriptable.argument(-1).isValid()); + QVERIFY(!m_scriptable.argument(0).isValid()); } void tst_QScriptable::throwError() diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index 5b8818a..d96006d 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -72,6 +72,7 @@ private slots: void scopeChain(); void pushAndPopScope(); void getSetActivationObject(); + void toString(); }; tst_QScriptContext::tst_QScriptContext() @@ -438,6 +439,7 @@ void tst_QScriptContext::pushAndPopContext() QCOMPARE(ctx->isCalledAsConstructor(), false); QCOMPARE(ctx->argumentCount(), 0); QCOMPARE(ctx->argument(0).isUndefined(), true); + QVERIFY(!ctx->argument(-1).isValid()); QCOMPARE(ctx->argumentsObject().isObject(), true); QCOMPARE(ctx->activationObject().isObject(), true); QCOMPARE(ctx->callee().isValid(), false); @@ -687,5 +689,21 @@ void tst_QScriptContext::getSetActivationObject() } } +static QScriptValue parentContextToString(QScriptContext *ctx, QScriptEngine *) +{ + return ctx->parentContext()->toString(); +} + +void tst_QScriptContext::toString() +{ + QScriptEngine eng; + eng.globalObject().setProperty("parentContextToString", eng.newFunction(parentContextToString)); + QScriptValue ret = eng.evaluate("function foo(first, second, third) {\n" + " return parentContextToString();\n" + "}; foo(1, 2, 3)", "script.qs"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo (first=1, second=2, third=3) at script.qs:2")); +} + QTEST_MAIN(tst_QScriptContext) #include "tst_qscriptcontext.moc" diff --git a/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp b/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp index 32f6c62..4b33022 100644 --- a/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp +++ b/tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp @@ -95,6 +95,7 @@ private slots: void builtinFunctionNames(); void nullContext(); void streaming(); + void assignmentAndComparison(); }; tst_QScriptContextInfo::tst_QScriptContextInfo() @@ -553,5 +554,24 @@ void tst_QScriptContextInfo::streaming() } } +void tst_QScriptContextInfo::assignmentAndComparison() +{ + QScriptEngine eng; + eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList)); + QString fileName = "ciao.qs"; + int lineNumber = 456; + QScriptValue ret = eng.evaluate("function bar(a, b, c) {\n return getContextInfoList();\n}\nbar()", + fileName, lineNumber); + QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret); + QCOMPARE(lst.size(), 3); + QScriptContextInfo ci = lst.at(0); + QScriptContextInfo same = ci; + QVERIFY(ci == same); + QVERIFY(!(ci != same)); + QScriptContextInfo other = lst.at(1); + QVERIFY(!(ci == other)); + QVERIFY(ci != other); +} + QTEST_MAIN(tst_QScriptContextInfo) #include "tst_qscriptcontextinfo.moc" diff --git a/tests/auto/qscriptengine/qscriptengine.pro b/tests/auto/qscriptengine/qscriptengine.pro index 599185c..6ff7b53 100644 --- a/tests/auto/qscriptengine/qscriptengine.pro +++ b/tests/auto/qscriptengine/qscriptengine.pro @@ -1,6 +1,7 @@ load(qttest_p4) QT += script SOURCES += tst_qscriptengine.cpp +DEFINES += SRCDIR=\\\"$$PWD\\\" wince*|symbian*: { addFiles.sources = script diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 0e07da0..3b17c3c 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -64,6 +64,7 @@ public: virtual ~tst_QScriptEngine(); private slots: + void constructWithParent(); void currentContext(); void pushPopContext(); void getSetDefaultPrototype(); @@ -100,6 +101,7 @@ private slots: void automaticSemicolonInsertion(); void abortEvaluation(); void isEvaluating(); + void printFunctionWithCustomHandler(); void printThrowsException(); void errorConstructors(); void argumentsProperty(); @@ -119,6 +121,7 @@ private slots: void getSetAgent(); void reentrancy(); void incDecNonObjectProperty(); + void installTranslatorFunctions(); }; tst_QScriptEngine::tst_QScriptEngine() @@ -129,6 +132,17 @@ tst_QScriptEngine::~tst_QScriptEngine() { } +void tst_QScriptEngine::constructWithParent() +{ + QPointer<QScriptEngine> ptr; + { + QObject obj; + QScriptEngine *engine = new QScriptEngine(&obj); + ptr = engine; + } + QVERIFY(ptr == 0); +} + void tst_QScriptEngine::currentContext() { QScriptEngine eng; @@ -636,9 +650,21 @@ void tst_QScriptEngine::newQObject() QScriptValue val = ret.property("objectName"); QVERIFY(val.isString()); } + // "promote" variant object to QObject + { + QScriptValue obj = eng.newVariant(123); + QVERIFY(obj.isVariant()); + QScriptValue originalProto = obj.prototype(); + QScriptValue ret = eng.newQObject(obj, this); + QVERIFY(ret.isQObject()); + QVERIFY(ret.strictlyEquals(obj)); + QVERIFY(obj.isQObject()); + QCOMPARE(ret.toQObject(), (QObject *)this); + QVERIFY(ret.prototype().strictlyEquals(originalProto)); + } // replace QObject* of existing object { - QScriptValue object = eng.newQObject(this); + QScriptValue object = eng.newVariant(123); QScriptValue originalProto = object.prototype(); QObject otherQObject; QScriptValue ret = eng.newQObject(object, &otherQObject); @@ -933,6 +959,22 @@ void tst_QScriptEngine::checkSyntax() QCOMPARE(result.errorLineNumber(), errorLineNumber); QCOMPARE(result.errorColumnNumber(), errorColumnNumber); QCOMPARE(result.errorMessage(), errorMessage); + + // assignment + { + QScriptSyntaxCheckResult copy = result; + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } + { + QScriptSyntaxCheckResult copy(result); + QCOMPARE(copy.state(), result.state()); + QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); + QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); + QCOMPARE(copy.errorMessage(), result.errorMessage()); + } } void tst_QScriptEngine::canEvaluate_data() @@ -1455,6 +1497,61 @@ void tst_QScriptEngine::valueConversion() Foo foo = qScriptValueToValue<Foo>(str); QCOMPARE(foo.x, 123); } + + // more built-in types + { + QScriptValue val = qScriptValueFromValue(&eng, uint(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, qulonglong(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, float(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, short(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, ushort(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, char(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QScriptValue val = qScriptValueFromValue(&eng, uchar(123)); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), 123); + } + { + QDateTime in = QDateTime::currentDateTime(); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isDate()); + QCOMPARE(val.toDateTime(), in); + } + { + QDate in = QDate::currentDate(); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isDate()); + QCOMPARE(val.toDateTime().date(), in); + } + { + QRegExp in = QRegExp("foo"); + QScriptValue val = qScriptValueFromValue(&eng, in); + QVERIFY(val.isRegExp()); + QCOMPARE(val.toRegExp(), in); + } } static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) @@ -1465,7 +1562,7 @@ static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) void tst_QScriptEngine::importExtension() { QStringList libPaths = QCoreApplication::instance()->libraryPaths(); - QCoreApplication::instance()->setLibraryPaths(QStringList() << "."); + QCoreApplication::instance()->setLibraryPaths(QStringList() << SRCDIR); QStringList availableExtensions; { @@ -2377,6 +2474,33 @@ void tst_QScriptEngine::isEvaluating() } } +static QtMsgType theMessageType; +static QString theMessage; + +static void myMsgHandler(QtMsgType type, const char *msg) +{ + theMessageType = type; + theMessage = QString::fromLatin1(msg); +} + +void tst_QScriptEngine::printFunctionWithCustomHandler() +{ + QScriptEngine eng; + QtMsgHandler oldHandler = qInstallMsgHandler(myMsgHandler); + QVERIFY(eng.globalObject().property("print").isFunction()); + theMessageType = QtSystemMsg; + QVERIFY(theMessage.isEmpty()); + QVERIFY(eng.evaluate("print('test')").isUndefined()); + QCOMPARE(theMessageType, QtDebugMsg); + QCOMPARE(theMessage, QString::fromLatin1("test")); + theMessageType = QtSystemMsg; + theMessage.clear(); + QVERIFY(eng.evaluate("print(3, true, 'little pigs')").isUndefined()); + QCOMPARE(theMessageType, QtDebugMsg); + QCOMPARE(theMessage, QString::fromLatin1("3 true little pigs")); + qInstallMsgHandler(oldHandler); +} + void tst_QScriptEngine::printThrowsException() { QScriptEngine eng; @@ -3403,5 +3527,49 @@ void tst_QScriptEngine:: incDecNonObjectProperty() } } +void tst_QScriptEngine::installTranslatorFunctions() +{ + QScriptEngine eng; + QScriptValue global = eng.globalObject(); + QVERIFY(!global.property("qsTranslate").isValid()); + QVERIFY(!global.property("QT_TRANSLATE_NOOP").isValid()); + QVERIFY(!global.property("qsTr").isValid()); + QVERIFY(!global.property("QT_TR_NOOP").isValid()); + QVERIFY(!global.property("String").property("prototype").property("arg").isValid()); + + eng.installTranslatorFunctions(); + QVERIFY(global.property("qsTranslate").isFunction()); + QVERIFY(global.property("QT_TRANSLATE_NOOP").isFunction()); + QVERIFY(global.property("qsTr").isFunction()); + QVERIFY(global.property("QT_TR_NOOP").isFunction()); + QVERIFY(global.property("String").property("prototype").property("arg").isFunction()); + + { + QScriptValue ret = eng.evaluate("qsTr('foo')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo")); + } + { + QScriptValue ret = eng.evaluate("qsTranslate('foo', 'bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("bar")); + } + { + QScriptValue ret = eng.evaluate("QT_TR_NOOP('foo')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foo")); + } + { + QScriptValue ret = eng.evaluate("QT_TRANSLATE_NOOP('foo', 'bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("bar")); + } + { + QScriptValue ret = eng.evaluate("'foo%0'.arg('bar')"); + QVERIFY(ret.isString()); + QCOMPARE(ret.toString(), QString::fromLatin1("foobar")); + } +} + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" diff --git a/tests/auto/qscriptenginedebugger/tst_qscriptenginedebugger.cpp b/tests/auto/qscriptenginedebugger/tst_qscriptenginedebugger.cpp index dd597fc..cbdaf1e 100644 --- a/tests/auto/qscriptenginedebugger/tst_qscriptenginedebugger.cpp +++ b/tests/auto/qscriptenginedebugger/tst_qscriptenginedebugger.cpp @@ -111,15 +111,29 @@ void tst_QScriptEngineDebugger::attachAndDetach() { { QScriptEngineDebugger debugger; + QCOMPARE(debugger.state(), QScriptEngineDebugger::SuspendedState); debugger.attachTo(0); QScriptEngine engine; debugger.attachTo(&engine); + QCOMPARE(debugger.state(), QScriptEngineDebugger::SuspendedState); } { QScriptEngineDebugger debugger; QScriptEngine engine; + QScriptValue oldPrint = engine.globalObject().property("print"); + QVERIFY(oldPrint.isFunction()); + QVERIFY(!engine.globalObject().property("__FILE__").isValid()); + QVERIFY(!engine.globalObject().property("__LINE__").isValid()); + debugger.attachTo(&engine); + QVERIFY(engine.globalObject().property("__FILE__").isUndefined()); + QVERIFY(engine.globalObject().property("__LINE__").isNumber()); + QVERIFY(!engine.globalObject().property("print").strictlyEquals(oldPrint)); + debugger.detach(); + QVERIFY(engine.globalObject().property("print").strictlyEquals(oldPrint)); + QVERIFY(!engine.globalObject().property("__FILE__").isValid()); + QVERIFY(!engine.globalObject().property("__LINE__").isValid()); } { QScriptEngineDebugger debugger; @@ -147,6 +161,14 @@ void tst_QScriptEngineDebugger::attachAndDetach() debugger2.attachTo(&engine); } #endif + { + QScriptEngine *engine = new QScriptEngine; + QScriptEngineDebugger debugger; + debugger.attachTo(engine); + delete engine; + QScriptEngine engine2; + debugger.attachTo(&engine2); + } } void tst_QScriptEngineDebugger::action() diff --git a/tests/auto/qscriptjstestsuite/qscriptjstestsuite.pro b/tests/auto/qscriptjstestsuite/qscriptjstestsuite.pro index 69ded3f..06b861e 100644 --- a/tests/auto/qscriptjstestsuite/qscriptjstestsuite.pro +++ b/tests/auto/qscriptjstestsuite/qscriptjstestsuite.pro @@ -1,7 +1,11 @@ load(qttest_p4) QT = core script SOURCES += tst_qscriptjstestsuite.cpp -symbian:TARGET.EPOCHEAPSIZE = 0x020000 0xA00000 +symbian { + TARGET.EPOCHEAPSIZE = 0x020000 0xA00000 +} else { + DEFINES += SRCDIR=\\\"$$PWD\\\" +} wince*|symbian*: { testFiles.sources = tests testFiles.path = . diff --git a/tests/auto/qscriptjstestsuite/tst_qscriptjstestsuite.cpp b/tests/auto/qscriptjstestsuite/tst_qscriptjstestsuite.cpp index 453907e..c110076 100644 --- a/tests/auto/qscriptjstestsuite/tst_qscriptjstestsuite.cpp +++ b/tests/auto/qscriptjstestsuite/tst_qscriptjstestsuite.cpp @@ -389,7 +389,7 @@ int tst_Suite::qt_metacall(QMetaObject::Call _c, int _id, void **_a) tst_Suite::tst_Suite() { - testsDir = QDir("."); + testsDir = QDir(SRCDIR); bool testsFound = testsDir.cd("tests"); if (!testsFound) { qWarning("*** no tests/ dir!"); diff --git a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp index 58937aa..a93223e 100644 --- a/tests/auto/qscriptqobject/tst_qscriptqobject.cpp +++ b/tests/auto/qscriptqobject/tst_qscriptqobject.cpp @@ -111,6 +111,7 @@ class MyQObject : public QObject Q_PROPERTY(int readOnlyProperty READ readOnlyProperty) Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut) Q_PROPERTY(CustomType propWithCustomType READ propWithCustomType WRITE setPropWithCustomType) + Q_PROPERTY(Policy enumProperty READ enumProperty WRITE setEnumProperty) Q_ENUMS(Policy Strategy) Q_FLAGS(Ability) @@ -148,6 +149,7 @@ public: m_hiddenValue(456.0), m_writeOnlyValue(789), m_readOnlyValue(987), + m_enumValue(BarPolicy), m_qtFunctionInvoked(-1) { } @@ -209,6 +211,11 @@ public: void setPropWithCustomType(const CustomType &c) { m_customType = c; } + Policy enumProperty() const + { return m_enumValue; } + void setEnumProperty(Policy policy) + { m_enumValue = policy; } + int qtFunctionInvoked() const { return m_qtFunctionInvoked; } @@ -259,8 +266,8 @@ public: { m_qtFunctionInvoked = 36; m_actuals << policy; } Q_INVOKABLE Policy myInvokableReturningEnum() { m_qtFunctionInvoked = 37; return BazPolicy; } - Q_INVOKABLE MyQObject::Policy myInvokableReturningQualifiedEnum() - { m_qtFunctionInvoked = 38; return BazPolicy; } + Q_INVOKABLE MyQObject::Strategy myInvokableReturningQualifiedEnum() + { m_qtFunctionInvoked = 38; return BazStrategy; } Q_INVOKABLE QVector<int> myInvokableReturningVectorOfInt() { m_qtFunctionInvoked = 11; return QVector<int>(); } Q_INVOKABLE void myInvokableWithVectorOfIntArg(const QVector<int> &) @@ -330,6 +337,10 @@ public: { emit mySignalWithDefaultArg(arg); } void emitMySignalWithDefaultArg() { emit mySignalWithDefaultArg(); } + void emitMySignalWithVariantArg(const QVariant &arg) + { emit mySignalWithVariantArg(arg); } + void emitMySignalWithScriptEngineArg(QScriptEngine *arg) + { emit mySignalWithScriptEngineArg(arg); } public Q_SLOTS: void mySlot() @@ -392,6 +403,8 @@ Q_SIGNALS: void myOtherOverloadedSignal(const QString &arg); void myOtherOverloadedSignal(int arg); void mySignalWithDefaultArg(int arg = 123); + void mySignalWithVariantArg(const QVariant &arg); + void mySignalWithScriptEngineArg(QScriptEngine *arg); protected: void connectNotify(const char *signal) { @@ -414,6 +427,7 @@ protected: int m_readOnlyValue; QKeySequence m_shortcut; CustomType m_customType; + Policy m_enumValue; int m_qtFunctionInvoked; QVariantList m_actuals; QByteArray m_connectedSignal; @@ -421,6 +435,7 @@ protected: }; Q_DECLARE_METATYPE(MyQObject*) +Q_DECLARE_METATYPE(MyQObject::Policy) class MyOtherQObject : public MyQObject { @@ -534,6 +549,24 @@ static QScriptValue getSetProperty(QScriptContext *ctx, QScriptEngine *) return ctx->callee().property("value"); } +static QScriptValue policyToScriptValue(QScriptEngine *engine, const MyQObject::Policy &policy) +{ + return qScriptValueFromValue(engine, policy); +} + +static void policyFromScriptValue(const QScriptValue &value, MyQObject::Policy &policy) +{ + QString str = value.toString(); + if (str == QLatin1String("red")) + policy = MyQObject::FooPolicy; + else if (str == QLatin1String("green")) + policy = MyQObject::BarPolicy; + else if (str == QLatin1String("blue")) + policy = MyQObject::BazPolicy; + else + policy = (MyQObject::Policy)-1; +} + void tst_QScriptExtQObject::getSetStaticProperty() { QCOMPARE(m_engine->evaluate("myObject.noSuchProperty").isUndefined(), true); @@ -755,6 +788,31 @@ void tst_QScriptExtQObject::getSetStaticProperty() QScriptValue::ReadOnly); } + // enum property + QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy); + { + QScriptValue val = m_engine->evaluate("myObject.enumProperty"); + QVERIFY(val.isNumber()); + QCOMPARE(val.toInt32(), (int)MyQObject::BarPolicy); + } + m_engine->evaluate("myObject.enumProperty = 2"); + QCOMPARE(m_myObject->enumProperty(), MyQObject::BazPolicy); + m_engine->evaluate("myObject.enumProperty = 'BarPolicy'"); + QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy); + m_engine->evaluate("myObject.enumProperty = 'ScoobyDoo'"); + // ### ouch! Shouldn't QMetaProperty::write() rather not change the value...? + QCOMPARE(m_myObject->enumProperty(), (MyQObject::Policy)-1); + // enum property with custom conversion + qScriptRegisterMetaType<MyQObject::Policy>(m_engine, policyToScriptValue, policyFromScriptValue); + m_engine->evaluate("myObject.enumProperty = 'red'"); + QCOMPARE(m_myObject->enumProperty(), MyQObject::FooPolicy); + m_engine->evaluate("myObject.enumProperty = 'green'"); + QCOMPARE(m_myObject->enumProperty(), MyQObject::BarPolicy); + m_engine->evaluate("myObject.enumProperty = 'blue'"); + QCOMPARE(m_myObject->enumProperty(), MyQObject::BazPolicy); + m_engine->evaluate("myObject.enumProperty = 'nada'"); + QCOMPARE(m_myObject->enumProperty(), (MyQObject::Policy)-1); + // auto-dereferencing of pointers { QBrush b = QColor(0xCA, 0xFE, 0xBA, 0xBE); @@ -1505,6 +1563,29 @@ void tst_QScriptExtQObject::connectAndDisconnect() m_myObject->emitMyOtherOverloadedSignal(123); QVERIFY(!m_engine->evaluate("gotSignal").toBoolean()); + // signal with QVariant arg: argument conversion should work + m_myObject->clearConnectedSignal(); + QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.connect(myHandler)").isUndefined()); + QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithVariantArg(QVariant))); + m_engine->evaluate("gotSignal = false"); + m_myObject->emitMySignalWithVariantArg(123); + QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true); + QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0); + QCOMPARE(m_engine->evaluate("signalArgs[0]").toNumber(), 123.0); + QVERIFY(m_engine->evaluate("myObject.mySignalWithVariantArg.disconnect(myHandler)").isUndefined()); + + // signal with argument type that's unknown to the meta-type system + m_myObject->clearConnectedSignal(); + QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.connect(myHandler)").isUndefined()); + QCOMPARE(m_myObject->connectedSignal().constData(), SIGNAL(mySignalWithScriptEngineArg(QScriptEngine*))); + m_engine->evaluate("gotSignal = false"); + QTest::ignoreMessage(QtWarningMsg, "QScriptEngine: Unable to handle unregistered datatype 'QScriptEngine*' when invoking handler of signal MyQObject::mySignalWithScriptEngineArg(QScriptEngine*)"); + m_myObject->emitMySignalWithScriptEngineArg(m_engine); + QCOMPARE(m_engine->evaluate("gotSignal").toBoolean(), true); + QCOMPARE(m_engine->evaluate("signalArgs.length").toNumber(), 1.0); + QVERIFY(m_engine->evaluate("signalArgs[0]").isUndefined()); + QVERIFY(m_engine->evaluate("myObject.mySignalWithScriptEngineArg.disconnect(myHandler)").isUndefined()); + // connect(object, function) m_engine->evaluate("otherObject = { name:'foo' }"); QVERIFY(m_engine->evaluate("myObject.mySignal.connect(otherObject, myHandler)").isUndefined()); @@ -1829,6 +1910,22 @@ void tst_QScriptExtQObject::cppConnectAndDisconnect() QVERIFY(qScriptDisconnect(m_myObject, SIGNAL(mySignal()), QScriptValue(), fun)); QCOMPARE(m_myObject->disconnectedSignal().constData(), SIGNAL(mySignal())); } + + // bad args + QVERIFY(!qScriptConnect(0, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, 0, QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue())); + QVERIFY(!qScriptDisconnect(0, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, 0, QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(foo()), QScriptValue(), fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), QScriptValue(), QScriptValue())); + { + QScriptEngine eng2; + QScriptValue receiverInDifferentEngine = eng2.newObject(); + QVERIFY(!qScriptConnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun)); + QVERIFY(!qScriptDisconnect(&edit, SIGNAL(textChanged(QString)), receiverInDifferentEngine, fun)); + } } void tst_QScriptExtQObject::classEnums() @@ -1861,7 +1958,7 @@ void tst_QScriptExtQObject::classEnums() QCOMPARE(MyQObject::Ability(m_engine->evaluate("MyQObject.AllAbility").toInt32()), MyQObject::AllAbility); - QScriptValue::PropertyFlags expectedEnumFlags = QScriptValue::ReadOnly; + QScriptValue::PropertyFlags expectedEnumFlags = QScriptValue::ReadOnly | QScriptValue::Undeletable; QCOMPARE(myClass.propertyFlags("FooPolicy"), expectedEnumFlags); QCOMPARE(myClass.propertyFlags("BarPolicy"), expectedEnumFlags); QCOMPARE(myClass.propertyFlags("BazPolicy"), expectedEnumFlags); @@ -1883,6 +1980,16 @@ void tst_QScriptExtQObject::classEnums() QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), int(MyQObject::BazPolicy)); m_myObject->resetQtFunctionInvoked(); + QCOMPARE(m_engine->evaluate("myObject.myInvokableWithEnumArg('BarPolicy')").isUndefined(), true); + QCOMPARE(m_myObject->qtFunctionInvoked(), 10); + QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); + QCOMPARE(m_myObject->qtFunctionActuals().at(0).toInt(), int(MyQObject::BarPolicy)); + + m_myObject->resetQtFunctionInvoked(); + QVERIFY(m_engine->evaluate("myObject.myInvokableWithEnumArg('NoSuchPolicy')").isError()); + QCOMPARE(m_myObject->qtFunctionInvoked(), -1); + + m_myObject->resetQtFunctionInvoked(); QCOMPARE(m_engine->evaluate("myObject.myInvokableWithQualifiedEnumArg(MyQObject.BazPolicy)").isUndefined(), true); QCOMPARE(m_myObject->qtFunctionInvoked(), 36); QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); @@ -1902,6 +2009,15 @@ void tst_QScriptExtQObject::classEnums() QCOMPARE(m_myObject->qtFunctionActuals().size(), 0); QCOMPARE(ret.isNumber(), true); } + + // enum properties are not deletable or writable + QVERIFY(!m_engine->evaluate("delete MyQObject.BazPolicy").toBool()); + myClass.setProperty("BazPolicy", QScriptValue()); + QCOMPARE(static_cast<MyQObject::Policy>(myClass.property("BazPolicy").toInt32()), + MyQObject::BazPolicy); + myClass.setProperty("BazPolicy", MyQObject::FooPolicy); + QCOMPARE(static_cast<MyQObject::Policy>(myClass.property("BazPolicy").toInt32()), + MyQObject::BazPolicy); } QT_BEGIN_NAMESPACE diff --git a/tests/auto/qscriptv8testsuite/qscriptv8testsuite.pro b/tests/auto/qscriptv8testsuite/qscriptv8testsuite.pro index 9976bb2..58c53e4 100644 --- a/tests/auto/qscriptv8testsuite/qscriptv8testsuite.pro +++ b/tests/auto/qscriptv8testsuite/qscriptv8testsuite.pro @@ -1,6 +1,7 @@ load(qttest_p4) QT = core script SOURCES += tst_qscriptv8testsuite.cpp +DEFINES += SRCDIR=\\\"$$PWD\\\" wince*|symbian: { testFiles.sources = tests diff --git a/tests/auto/qscriptv8testsuite/tst_qscriptv8testsuite.cpp b/tests/auto/qscriptv8testsuite/tst_qscriptv8testsuite.cpp index 0b4429b..7215852 100644 --- a/tests/auto/qscriptv8testsuite/tst_qscriptv8testsuite.cpp +++ b/tests/auto/qscriptv8testsuite/tst_qscriptv8testsuite.cpp @@ -193,7 +193,7 @@ int tst_Suite::qt_metacall(QMetaObject::Call _c, int _id, void **_a) tst_Suite::tst_Suite() { - testsDir = QDir("."); + testsDir = QDir(SRCDIR); bool testsFound = testsDir.cd("tests"); if (!testsFound) { qWarning("*** no tests/ dir!"); diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp index e4b843e..ff2df7a 100644 --- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp +++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp @@ -1301,6 +1301,27 @@ void tst_QScriptValue::toVariant() QCOMPARE(str.toVariant(), QVariant(QString("ciao"))); QCOMPARE(qscriptvalue_cast<QVariant>(str), QVariant(QString("ciao"))); } + + // array + { + QVariantList listIn; + listIn << 123 << "hello"; + QScriptValue array = qScriptValueFromValue(&eng, listIn); + QVERIFY(array.isArray()); + QCOMPARE(array.property("length").toInt32(), 2); + QVariant ret = array.toVariant(); + QCOMPARE(ret.type(), QVariant::List); + QVariantList listOut = ret.toList(); + QCOMPARE(listOut.size(), listIn.size()); + for (int i = 0; i < listIn.size(); ++i) + QVERIFY(listOut.at(i) == listIn.at(i)); + // round-trip conversion + QScriptValue array2 = qScriptValueFromValue(&eng, ret); + QVERIFY(array2.isArray()); + QCOMPARE(array2.property("length").toInt32(), array.property("length").toInt32()); + for (int i = 0; i < array.property("length").toInt32(); ++i) + QVERIFY(array2.property(i).strictlyEquals(array.property(i))); + } } // unfortunately, this is necessary in order to do qscriptvalue_cast<QPushButton*>(...) @@ -1461,21 +1482,43 @@ void tst_QScriptValue::toObject() { QScriptValue undefined = QScriptValue(QScriptValue::UndefinedValue); QVERIFY(!undefined.toObject().isValid()); + QVERIFY(!eng.toObject(undefined).isValid()); QScriptValue null = QScriptValue(QScriptValue::NullValue); QVERIFY(!null.toObject().isValid()); + QVERIFY(!eng.toObject(null).isValid()); QScriptValue falskt = QScriptValue(false); QVERIFY(!falskt.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(falskt); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + } QScriptValue sant = QScriptValue(true); QVERIFY(!sant.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(sant); + QVERIFY(tmp.isObject()); + QVERIFY(tmp.toBool()); + } QScriptValue number = QScriptValue(123.0); QVERIFY(!number.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(number); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toInt32(), number.toInt32()); + } - QScriptValue str = QScriptValue(QString("ciao")); + QScriptValue str = QScriptValue(QString::fromLatin1("ciao")); QVERIFY(!str.toObject().isValid()); + { + QScriptValue tmp = eng.toObject(str); + QVERIFY(tmp.isObject()); + QCOMPARE(tmp.toString(), QString::fromLatin1("ciao")); + } } } @@ -2339,6 +2382,16 @@ void tst_QScriptValue::call() QCOMPARE(qIsNaN(ret.toNumber()), true); } } + { + QScriptValue fun = eng.evaluate("Object"); + QVERIFY(fun.isFunction()); + QScriptEngine eng2; + QScriptValue objectInDifferentEngine = eng2.newObject(); + QScriptValueList args; + args << objectInDifferentEngine; + QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: cannot call function with argument created in a different engine"); + fun.call(QScriptValue(), args); + } // test that invalid return value is handled gracefully { diff --git a/tests/auto/qsequentialanimationgroup/qsequentialanimationgroup.pro b/tests/auto/qsequentialanimationgroup/qsequentialanimationgroup.pro new file mode 100644 index 0000000..ad861c3 --- /dev/null +++ b/tests/auto/qsequentialanimationgroup/qsequentialanimationgroup.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +QT = core gui +SOURCES += tst_qsequentialanimationgroup.cpp + + diff --git a/tests/auto/qsequentialanimationgroup/tst_qsequentialanimationgroup.cpp b/tests/auto/qsequentialanimationgroup/tst_qsequentialanimationgroup.cpp new file mode 100644 index 0000000..97ff04b --- /dev/null +++ b/tests/auto/qsequentialanimationgroup/tst_qsequentialanimationgroup.cpp @@ -0,0 +1,1648 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include "../../shared/util.h" + +#include <QtCore/qanimationgroup.h> +#include <QtCore/qsequentialanimationgroup.h> + +//TESTED_CLASS=QSequentialAnimationGroup +//TESTED_FILES= + +Q_DECLARE_METATYPE(QAbstractAnimation::State) +Q_DECLARE_METATYPE(QAbstractAnimation*) + +class tst_QSequentialAnimationGroup : public QObject +{ + Q_OBJECT +public: + tst_QSequentialAnimationGroup(); + virtual ~tst_QSequentialAnimationGroup(); + +public Q_SLOTS: + void init(); + void cleanup(); + +private slots: + void construction(); + void setCurrentTime(); + void setCurrentTimeWithUncontrolledAnimation(); + void seekingForwards(); + void seekingBackwards(); + void pauseAndResume(); + void restart(); + void looping(); + void startDelay(); + void clearGroup(); + void groupWithZeroDurationAnimations(); + void propagateGroupUpdateToChildren(); + void updateChildrenWithRunningGroup(); + void deleteChildrenWithRunningGroup(); + void startChildrenWithStoppedGroup(); + void stopGroupWithRunningChild(); + void startGroupWithRunningChild(); + void zeroDurationAnimation(); + void stopUncontrolledAnimations(); + void finishWithUncontrolledAnimation(); + void addRemoveAnimation(); + void currentAnimation(); + void currentAnimationWithZeroDuration(); + void insertAnimation(); + void clearAnimations(); +}; + +tst_QSequentialAnimationGroup::tst_QSequentialAnimationGroup() +{ +} + +tst_QSequentialAnimationGroup::~tst_QSequentialAnimationGroup() +{ +} + +void tst_QSequentialAnimationGroup::init() +{ + qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State"); + qRegisterMetaType<QAbstractAnimation*>("QAbstractAnimation*"); +} + +void tst_QSequentialAnimationGroup::cleanup() +{ +} + +void tst_QSequentialAnimationGroup::construction() +{ + QSequentialAnimationGroup animationgroup; +} + +class AnimationObject : public QObject +{ + Q_OBJECT + Q_PROPERTY(int value READ value WRITE setValue) +public: + AnimationObject(int startValue = 0) + : v(startValue) + { } + + int value() const { return v; } + void setValue(int value) { v = value; } + + int v; +}; + +class TestAnimation : public QVariantAnimation +{ + Q_OBJECT +public: + virtual void updateCurrentValue(const QVariant &value) { Q_UNUSED(value)}; + virtual void updateState(QAbstractAnimation::State oldState, + QAbstractAnimation::State newState) + { + Q_UNUSED(oldState) + Q_UNUSED(newState) + }; +}; + +class UncontrolledAnimation : public QPropertyAnimation +{ + Q_OBJECT +public: + UncontrolledAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = 0) + : QPropertyAnimation(target, propertyName, parent) + { + setDuration(250); + } + + int duration() const { return -1; /* not time driven */ } + +protected: + void updateCurrentTime(int msecs) + { + QPropertyAnimation::updateCurrentTime(msecs); + if (msecs >= QPropertyAnimation::duration()) + stop(); + } +}; + +void tst_QSequentialAnimationGroup::setCurrentTime() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject s_o3; + + // sequence operating on same object/property + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + a2_s_o1->setLoopCount(3); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + + // sequence operating on different object/properties + QAnimationGroup *sequence2 = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value"); + QVariantAnimation *a1_s_o3 = new QPropertyAnimation(&s_o3, "value"); + sequence2->addAnimation(a1_s_o2); + sequence2->addAnimation(a1_s_o3); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + group.addAnimation(sequence2); + + // Current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + + QCOMPARE(group.currentTime(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(a1_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 250 + group.setCurrentTime(250); + QCOMPARE(group.currentTime(), 250); + QCOMPARE(sequence->currentTime(), 250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 251 + group.setCurrentTime(251); + QCOMPARE(group.currentTime(), 251); + QCOMPARE(sequence->currentTime(), 251); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentLoop(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 750 + group.setCurrentTime(750); + QCOMPARE(group.currentTime(), 750); + QCOMPARE(sequence->currentTime(), 750); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1000 + group.setCurrentTime(1000); + QCOMPARE(group.currentTime(), 1000); + QCOMPARE(sequence->currentTime(), 1000); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1010 + group.setCurrentTime(1010); + QCOMPARE(group.currentTime(), 1010); + QCOMPARE(sequence->currentTime(), 1010); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 10); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1250 + group.setCurrentTime(1250); + QCOMPARE(group.currentTime(), 1250); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1500 + group.setCurrentTime(1500); + QCOMPARE(group.currentTime(), 1500); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1750 + group.setCurrentTime(1750); + QCOMPARE(group.currentTime(), 1750); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 500); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 250); + + // Current time = 2000 + group.setCurrentTime(2000); + QCOMPARE(group.currentTime(), 1750); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 500); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 250); +} + +void tst_QSequentialAnimationGroup::setCurrentTimeWithUncontrolledAnimation() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject t_o1; + AnimationObject t_o2; + + // sequence operating on different object/properties + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QAbstractAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QAbstractAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value"); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a1_s_o2); + + QAbstractAnimation *notTimeDriven = new UncontrolledAnimation(&t_o1, "value"); + QCOMPARE(notTimeDriven->totalDuration(), -1); + + QAbstractAnimation *loopsForever = new QPropertyAnimation(&t_o2, "value"); + loopsForever->setLoopCount(-1); + QCOMPARE(loopsForever->totalDuration(), -1); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + group.addAnimation(notTimeDriven); + group.addAnimation(loopsForever); + group.start(); + group.pause(); // this allows the group to listen for the finish signal of its children + + // Current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped); + + QCOMPARE(group.currentTime(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(a1_s_o1->currentTime(), 1); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(notTimeDriven->currentTime(), 0); + QCOMPARE(loopsForever->currentTime(), 0); + + // Current time = 250 + group.setCurrentTime(250); + QCOMPARE(group.currentTime(), 250); + QCOMPARE(sequence->currentTime(), 250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(notTimeDriven->currentTime(), 0); + QCOMPARE(loopsForever->currentTime(), 0); + + // Current time = 500 + group.setCurrentTime(500); + QCOMPARE(group.currentTime(), 500); + QCOMPARE(sequence->currentTime(), 500); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 0); + QCOMPARE(loopsForever->currentTime(), 0); + QCOMPARE(group.currentAnimation(), notTimeDriven); + + // Current time = 505 + group.setCurrentTime(505); + QCOMPARE(group.currentTime(), 505); + QCOMPARE(sequence->currentTime(), 500); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 5); + QCOMPARE(loopsForever->currentTime(), 0); + QCOMPARE(group.currentAnimation(), notTimeDriven); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Paused); + QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped); + + // Current time = 750 (end of notTimeDriven animation) + group.setCurrentTime(750); + QCOMPARE(group.currentTime(), 750); + QCOMPARE(sequence->currentTime(), 500); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 250); + QCOMPARE(loopsForever->currentTime(), 0); + QCOMPARE(group.currentAnimation(), loopsForever); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever->state(), QAnimationGroup::Paused); + + // Current time = 800 (as notTimeDriven was finished at 750, loopsforever should still run) + group.setCurrentTime(800); + QCOMPARE(group.currentTime(), 800); + QCOMPARE(group.currentAnimation(), loopsForever); + QCOMPARE(sequence->currentTime(), 500); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(notTimeDriven->currentTime(), 250); + QCOMPARE(loopsForever->currentTime(), 50); + + loopsForever->stop(); // this should stop the group + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::seekingForwards() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject s_o3; + + // sequence operating on same object/property + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + a2_s_o1->setLoopCount(3); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + + // sequence operating on different object/properties + QAnimationGroup *sequence2 = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value"); + QVariantAnimation *a1_s_o3 = new QPropertyAnimation(&s_o3, "value"); + sequence2->addAnimation(a1_s_o2); + sequence2->addAnimation(a1_s_o3); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + group.addAnimation(sequence2); + + // Current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o3->state(), QAnimationGroup::Stopped); + + QCOMPARE(group.currentTime(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(a1_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentTime(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // Current time = 1500 + group.setCurrentTime(1500); + QCOMPARE(group.currentTime(), 1500); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 250); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 0); + + // this will restart the group + group.start(); + group.pause(); + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Paused); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o3->state(), QAnimationGroup::Stopped); + + // Current time = 1750 + group.setCurrentTime(1750); + QCOMPARE(group.currentTime(), 1750); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 500); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 250); +} + +void tst_QSequentialAnimationGroup::seekingBackwards() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject s_o3; + + // sequence operating on same object/property + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + a2_s_o1->setLoopCount(3); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + + // sequence operating on different object/properties + QAnimationGroup *sequence2 = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value"); + QVariantAnimation *a1_s_o3 = new QPropertyAnimation(&s_o3, "value"); + sequence2->addAnimation(a1_s_o2); + sequence2->addAnimation(a1_s_o3); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + group.addAnimation(sequence2); + + group.start(); + + // Current time = 1600 + group.setCurrentTime(1600); + QCOMPARE(group.currentTime(), 1600); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 350); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 100); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(sequence2->state(), QAnimationGroup::Running); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o3->state(), QAnimationGroup::Running); + + // Seeking backwards, current time = 1 + group.setCurrentTime(1); + QCOMPARE(group.currentTime(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(a1_s_o1->currentTime(), 1); + + QEXPECT_FAIL("", "rewinding in nested groups is considered as a restart from the children," + "hence they don't reset from their current animation", Continue); + QCOMPARE(a2_s_o1->currentTime(), 0); + QEXPECT_FAIL("", "rewinding in nested groups is considered as a restart from the children," + "hence they don't reset from their current animation", Continue); + QCOMPARE(a2_s_o1->currentLoop(), 0); + QEXPECT_FAIL("", "rewinding in nested groups is considered as a restart from the children," + "hence they don't reset from their current animation", Continue); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence2->currentTime(), 0); + QCOMPARE(a1_s_o2->currentTime(), 0); + QCOMPARE(a1_s_o3->currentTime(), 0); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(sequence->state(), QAnimationGroup::Running); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Running); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o3->state(), QAnimationGroup::Stopped); + + // Current time = 2000 + group.setCurrentTime(2000); + QCOMPARE(group.currentTime(), 1750); + QCOMPARE(sequence->currentTime(), 1250); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 2); + QCOMPARE(a3_s_o1->currentTime(), 250); + QCOMPARE(sequence2->currentTime(), 500); + QCOMPARE(a1_s_o2->currentTime(), 250); + QCOMPARE(a1_s_o3->currentTime(), 250); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(sequence->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(sequence2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped); + QCOMPARE(a1_s_o3->state(), QAnimationGroup::Stopped); +} + +typedef QList<QAbstractAnimation::State> StateList; + +static bool compareStates(const QSignalSpy& spy, const StateList &expectedStates) +{ + bool equals = true; + for (int i = 0; i < qMax(expectedStates.count(), spy.count()); ++i) { + if (i >= spy.count() || i >= expectedStates.count()) { + equals = false; + break; + } + QList<QVariant> args = spy.at(i); + QAbstractAnimation::State st = expectedStates.at(i); + QAbstractAnimation::State actual = qVariantValue<QAbstractAnimation::State>(args.value(1)); + if (equals && actual != st) { + equals = false; + break; + } + } + if (!equals) { + const char *stateStrings[] = {"Stopped", "Paused", "Running"}; + QString e,a; + for (int i = 0; i < qMax(expectedStates.count(), spy.count()); ++i) { + if (i < expectedStates.count()) { + int exp = int(expectedStates.at(i)); + if (!e.isEmpty()) + e += QLatin1String(", "); + e += QLatin1String(stateStrings[exp]); + } + if (i < spy.count()) { + QList<QVariant> args = spy.at(i); + QAbstractAnimation::State actual = qVariantValue<QAbstractAnimation::State>(args.value(1)); + if (!a.isEmpty()) + a += QLatin1String(", "); + if (int(actual) >= 0 && int(actual) <= 2) { + a += QLatin1String(stateStrings[int(actual)]); + } else { + a += QLatin1String("NaN"); + } + } + + } + qDebug("\n" + "expected (count == %d): %s\n" + "actual (count == %d): %s\n", expectedStates.count(), qPrintable(e), spy.count(), qPrintable(a)); + } + return equals; +} + +void tst_QSequentialAnimationGroup::pauseAndResume() +{ + AnimationObject s_o1; + + // sequence operating on same object/property + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QVariantAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QVariantAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + a2_s_o1->setLoopCount(2); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + sequence->setLoopCount(2); + + QSignalSpy a1StateChangedSpy(a1_s_o1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy seqStateChangedSpy(sequence, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + + group.start(); + group.pause(); + + // Current time = 1751 + group.setCurrentTime(1751); + QCOMPARE(group.currentTime(), 1751); + QCOMPARE(sequence->currentTime(), 751); + QCOMPARE(sequence->currentLoop(), 1); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 1); + QCOMPARE(a3_s_o1->currentLoop(), 0); + QCOMPARE(a3_s_o1->currentTime(), 1); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a3_s_o1->state(), QAnimationGroup::Paused); + + QCOMPARE(a1StateChangedSpy.count(), 5); // Running,Paused,Stopped,Running,Stopped + QCOMPARE(seqStateChangedSpy.count(), 2); // Running,Paused + + QVERIFY(compareStates(a1StateChangedSpy, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Paused + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running + << QAbstractAnimation::Stopped))); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(a1StateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(a1StateChangedSpy.at(1).at(1)), + QAnimationGroup::Paused); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(a1StateChangedSpy.at(2).at(1)), + QAnimationGroup::Stopped); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(a1StateChangedSpy.at(3).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(a1StateChangedSpy.at(4).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(1).at(1)), + QAnimationGroup::Paused); + + group.resume(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(sequence->state(), QAnimationGroup::Running); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a3_s_o1->state(), QAnimationGroup::Running); + + QVERIFY(group.currentTime() >= 1751); + QVERIFY(sequence->currentTime() >= 751); + QCOMPARE(sequence->currentLoop(), 1); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 1); + QCOMPARE(a3_s_o1->currentLoop(), 0); + QVERIFY(a3_s_o1->currentTime() >= 1); + + QCOMPARE(seqStateChangedSpy.count(), 3); // Running,Paused,Running + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(2).at(1)), + QAnimationGroup::Running); + + group.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a3_s_o1->state(), QAnimationGroup::Paused); + + QVERIFY(group.currentTime() >= 1751); + QVERIFY(sequence->currentTime() >= 751); + QCOMPARE(sequence->currentLoop(), 1); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 1); + QCOMPARE(a3_s_o1->currentLoop(), 0); + QVERIFY(a3_s_o1->currentTime() >= 1); + + QCOMPARE(seqStateChangedSpy.count(), 4); // Running,Paused,Running,Paused + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(3).at(1)), + QAnimationGroup::Paused); + + group.stop(); + + QCOMPARE(seqStateChangedSpy.count(), 5); // Running,Paused,Running,Paused,Stopped + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(4).at(1)), + QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::restart() +{ + AnimationObject s_o1; + + // sequence operating on same object/property + QAnimationGroup *sequence = new QSequentialAnimationGroup(); + QSignalSpy seqCurrentAnimChangedSpy(sequence, SIGNAL(currentAnimationChanged(QAbstractAnimation*))); + QSignalSpy seqStateChangedSpy(sequence, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QVariantAnimation *anims[3]; + QSignalSpy *animsStateChanged[3]; + + for (int i = 0; i < 3; i++) { + anims[i] = new QPropertyAnimation(&s_o1, "value"); + anims[i]->setDuration(100); + animsStateChanged[i] = new QSignalSpy(anims[i], SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + } + + anims[1]->setLoopCount(2); + sequence->addAnimation(anims[0]); + sequence->addAnimation(anims[1]); + sequence->addAnimation(anims[2]); + sequence->setLoopCount(2); + + QSequentialAnimationGroup group; + group.addAnimation(sequence); + + group.start(); + + QTest::qWait(500); + + QCOMPARE(group.state(), QAnimationGroup::Running); + + QTest::qWait(300); + QTRY_COMPARE(group.state(), QAnimationGroup::Stopped); + + for (int i = 0; i < 3; i++) { + QCOMPARE(animsStateChanged[i]->count(), 4); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(animsStateChanged[i]->at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(animsStateChanged[i]->at(1).at(1)), + QAnimationGroup::Stopped); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(animsStateChanged[i]->at(2).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(animsStateChanged[i]->at(3).at(1)), + QAnimationGroup::Stopped); + } + + QCOMPARE(seqStateChangedSpy.count(), 2); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(seqStateChangedSpy.at(1).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(seqCurrentAnimChangedSpy.count(), 6); + for(int i=0; i<seqCurrentAnimChangedSpy.count(); i++) + QCOMPARE(static_cast<QAbstractAnimation*>(anims[i%3]), qVariantValue<QAbstractAnimation*>(seqCurrentAnimChangedSpy.at(i).at(0))); + + group.start(); + + QCOMPARE(animsStateChanged[0]->count(), 5); + QCOMPARE(animsStateChanged[1]->count(), 4); + QCOMPARE(animsStateChanged[2]->count(), 4); + QCOMPARE(seqStateChangedSpy.count(), 3); +} + +void tst_QSequentialAnimationGroup::looping() +{ + AnimationObject s_o1; + AnimationObject s_o2; + AnimationObject s_o3; + + // sequence operating on same object/property + QSequentialAnimationGroup *sequence = new QSequentialAnimationGroup(); + QAbstractAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QAbstractAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value"); + QAbstractAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value"); + + QSignalSpy a1Spy(a1_s_o1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy a2Spy(a2_s_o1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy a3Spy(a3_s_o1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy seqSpy(sequence, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + a2_s_o1->setLoopCount(2); + sequence->addAnimation(a1_s_o1); + sequence->addAnimation(a2_s_o1); + sequence->addAnimation(a3_s_o1); + sequence->setLoopCount(2); + + QSequentialAnimationGroup group; + QSignalSpy groupSpy(&group, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + group.addAnimation(sequence); + group.setLoopCount(2); + + group.start(); + group.pause(); + + // Current time = 1750 + group.setCurrentTime(1750); + QCOMPARE(group.currentTime(), 1750); + QCOMPARE(sequence->currentTime(), 750); + QCOMPARE(sequence->currentLoop(), 1); + QCOMPARE(a1_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 1); + // this animation is at the beginning because it is the current one inside sequence + QCOMPARE(a3_s_o1->currentLoop(), 0); + QCOMPARE(a3_s_o1->currentTime(), 0); + QCOMPARE(sequence->currentAnimation(), a3_s_o1); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a3_s_o1->state(), QAnimationGroup::Paused); + + QCOMPARE(a1Spy.count(), 5); // Running,Paused,Stopped,Running,Stopped + QVERIFY(compareStates(a1Spy, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Paused + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running + << QAbstractAnimation::Stopped))); + + QCOMPARE(a2Spy.count(), 4); // Running,Stopped,Running,Stopped + QVERIFY(compareStates(a3Spy, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running + << QAbstractAnimation::Paused))); + + QCOMPARE(seqSpy.count(), 2); // Running,Paused + QCOMPARE(groupSpy.count(), 2); // Running,Paused + + // Looping, current time = duration + 1 + group.setCurrentTime(group.duration() + 1); + QCOMPARE(group.currentTime(), 1); + QCOMPARE(group.currentLoop(), 1); + QCOMPARE(sequence->currentTime(), 1); + QCOMPARE(sequence->currentLoop(), 0); + QCOMPARE(a1_s_o1->currentTime(), 1); + QCOMPARE(a2_s_o1->currentTime(), 250); + QCOMPARE(a2_s_o1->currentLoop(), 1); + // this animation is at the end because it was run on the previous loop + QCOMPARE(a3_s_o1->currentLoop(), 0); + QCOMPARE(a3_s_o1->currentTime(), 250); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(sequence->state(), QAnimationGroup::Paused); + QCOMPARE(a1_s_o1->state(), QAnimationGroup::Paused); + QCOMPARE(a2_s_o1->state(), QAnimationGroup::Stopped); + QCOMPARE(a3_s_o1->state(), QAnimationGroup::Stopped); + + QCOMPARE(a1Spy.count(), 7); // Running,Paused,Stopped,Running,Stopped,Running,Stopped + QCOMPARE(a2Spy.count(), 4); // Running, Stopped, Running, Stopped + QVERIFY(compareStates(a3Spy, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running + << QAbstractAnimation::Paused + << QAbstractAnimation::Stopped))); + QVERIFY(compareStates(seqSpy, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Paused + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running + << QAbstractAnimation::Paused))); + QCOMPARE(groupSpy.count(), 2); +} + +void tst_QSequentialAnimationGroup::startDelay() +{ + QSequentialAnimationGroup group; + group.addPause(250); + group.addPause(125); + QCOMPARE(group.totalDuration(), 375); + + QEventLoop loop; + QObject::connect(&group, SIGNAL(finished()), &loop, SLOT(quit())); + + QTime time; + time.start(); + group.start(); + loop.exec(); + + QVERIFY(time.elapsed() >= 375); + QVERIFY(time.elapsed() < 1000); +} + +void tst_QSequentialAnimationGroup::clearGroup() +{ + QSequentialAnimationGroup group; + + static const int animationCount = 20; + + for (int i = 0; i < animationCount/2; ++i) { + QSequentialAnimationGroup *subGroup = new QSequentialAnimationGroup(&group); + group.addPause(100); + subGroup->addPause(10); + } + + QCOMPARE(group.animationCount(), animationCount); + + QPointer<QAbstractAnimation> children[animationCount]; + for (int i = 0; i < animationCount; ++i) { + QVERIFY(group.animationAt(i) != 0); + children[i] = group.animationAt(i); + } + + group.clearAnimations(); + QCOMPARE(group.animationCount(), 0); + QCOMPARE(group.currentTime(), 0); + for (int i = 0; i < animationCount; ++i) + QVERIFY(children[i].isNull()); +} + +void tst_QSequentialAnimationGroup::groupWithZeroDurationAnimations() +{ + QObject o; + QObject o2; + + o.setProperty("myProperty", 42); + o.setProperty("myOtherProperty", 13); + o2.setProperty("myProperty", 42); + o2.setProperty("myOtherProperty", 13); + + QSequentialAnimationGroup group; + + QVariantAnimation *a1 = new QPropertyAnimation(&o, "myProperty"); + a1->setDuration(0); + a1->setEndValue(43); + group.addAnimation(a1); + + //this should just run fine and change nothing + group.setCurrentTime(0); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(a1)); + + QVariantAnimation *a2 = new QPropertyAnimation(&o2, "myOtherProperty"); + a2->setDuration(500); + a2->setEndValue(31); + group.addAnimation(a2); + + QVariantAnimation *a3 = new QPropertyAnimation(&o, "myProperty"); + a3->setDuration(0); + a3->setEndValue(44); + group.addAnimation(a3); + + QVariantAnimation *a4 = new QPropertyAnimation(&o, "myOtherProperty"); + a4->setDuration(250); + a4->setEndValue(75); + group.addAnimation(a4); + + QVariantAnimation *a5 = new QPropertyAnimation(&o2, "myProperty"); + a5->setDuration(0); + a5->setEndValue(12); + group.addAnimation(a5); + + QCOMPARE(o.property("myProperty").toInt(), 42); + QCOMPARE(o.property("myOtherProperty").toInt(), 13); + QCOMPARE(o2.property("myProperty").toInt(), 42); + QCOMPARE(o2.property("myOtherProperty").toInt(), 13); + + + group.start(); + + QCOMPARE(o.property("myProperty").toInt(), 43); + QCOMPARE(o.property("myOtherProperty").toInt(), 13); + QCOMPARE(o2.property("myProperty").toInt(), 42); + QCOMPARE(o2.property("myOtherProperty").toInt(), 13); + + QTest::qWait(50); + + int o2val = o2.property("myOtherProperty").toInt(); + QVERIFY(o2val > 13); + QVERIFY(o2val < 31); + QCOMPARE(o.property("myProperty").toInt(), 43); + QCOMPARE(o.property("myOtherProperty").toInt(), 13); + + QTest::qWait(500); + + QCOMPARE(o.property("myProperty").toInt(), 44); + QCOMPARE(o2.property("myProperty").toInt(), 42); + QCOMPARE(o2.property("myOtherProperty").toInt(), 31); + QCOMPARE(a1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2->state(), QAnimationGroup::Stopped); + QCOMPARE(a3->state(), QAnimationGroup::Stopped); + QCOMPARE(a4->state(), QAnimationGroup::Running); + QCOMPARE(a5->state(), QAnimationGroup::Stopped); + QCOMPARE(group.state(), QAnimationGroup::Running); + QTest::qWait(500); + + QTRY_COMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(o.property("myProperty").toInt(), 44); + QCOMPARE(o.property("myOtherProperty").toInt(), 75); + QCOMPARE(o2.property("myProperty").toInt(), 12); + QCOMPARE(o2.property("myOtherProperty").toInt(), 31); + QCOMPARE(a1->state(), QAnimationGroup::Stopped); + QCOMPARE(a2->state(), QAnimationGroup::Stopped); + QCOMPARE(a3->state(), QAnimationGroup::Stopped); + QCOMPARE(a4->state(), QAnimationGroup::Stopped); + QCOMPARE(a5->state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::propagateGroupUpdateToChildren() +{ + // this test verifies if group state changes are updating its children correctly + QSequentialAnimationGroup group; + + QObject o; + o.setProperty("ole", 42); + QCOMPARE(o.property("ole").toInt(), 42); + + QPropertyAnimation anim1(&o, "ole"); + anim1.setEndValue(43); + anim1.setDuration(100); + QVERIFY(!anim1.currentValue().isValid()); + QCOMPARE(anim1.currentValue().toInt(), 0); + QCOMPARE(o.property("ole").toInt(), 42); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QVERIFY(anim2.currentValue().isValid()); + QCOMPARE(anim2.currentValue().toInt(), 0); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + group.start(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Paused); + QCOMPARE(anim1.state(), QAnimationGroup::Paused); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::updateChildrenWithRunningGroup() +{ + // assert that its possible to modify a child's state directly while their group is running + QSequentialAnimationGroup group; + + TestAnimation anim; + anim.setStartValue(0); + anim.setEndValue(100); + anim.setDuration(200); + + QSignalSpy groupStateChangedSpy(&group, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy childStateChangedSpy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QCOMPARE(groupStateChangedSpy.count(), 0); + QCOMPARE(childStateChangedSpy.count(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim); + + group.start(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Running); + + QCOMPARE(groupStateChangedSpy.count(), 1); + QCOMPARE(childStateChangedSpy.count(), 1); + + QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(childStateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + + // starting directly a running child will not have any effect + anim.start(); + + QCOMPARE(groupStateChangedSpy.count(), 1); + QCOMPARE(childStateChangedSpy.count(), 1); + + anim.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Paused); + + // in the animation stops directly, the group will still be running + anim.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::deleteChildrenWithRunningGroup() +{ + // test if children can be activated when their group is stopped + QSequentialAnimationGroup group; + + QVariantAnimation *anim1 = new TestAnimation; + anim1->setStartValue(0); + anim1->setEndValue(100); + anim1->setDuration(200); + group.addAnimation(anim1); + + QCOMPARE(group.duration(), anim1->duration()); + + group.start(); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1->state(), QAnimationGroup::Running); + + QTest::qWait(50); + QVERIFY(group.currentTime() > 0); + + delete anim1; + QCOMPARE(group.animationCount(), 0); + QCOMPARE(group.duration(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(group.currentTime(), 0); //that's the invariant +} + +void tst_QSequentialAnimationGroup::startChildrenWithStoppedGroup() +{ + // test if children can be activated when their group is stopped + QSequentialAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(200); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + anim1.start(); + anim2.start(); + anim2.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); +} + +void tst_QSequentialAnimationGroup::stopGroupWithRunningChild() +{ + // children that started independently will not be affected by a group stop + QSequentialAnimationGroup group; + + TestAnimation anim1; + anim1.setStartValue(0); + anim1.setEndValue(100); + anim1.setDuration(200); + + TestAnimation anim2; + anim2.setStartValue(0); + anim2.setEndValue(100); + anim2.setDuration(200); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); + + group.addAnimation(&anim1); + group.addAnimation(&anim2); + + anim1.start(); + anim2.start(); + anim2.pause(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Running); + QCOMPARE(anim2.state(), QAnimationGroup::Paused); + + anim1.stop(); + anim2.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1.state(), QAnimationGroup::Stopped); + QCOMPARE(anim2.state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::startGroupWithRunningChild() +{ + // as the group has precedence over its children, starting a group will restart all the children + QSequentialAnimationGroup group; + + TestAnimation *anim1 = new TestAnimation(); + anim1->setStartValue(0); + anim1->setEndValue(100); + anim1->setDuration(200); + + TestAnimation *anim2 = new TestAnimation(); + anim2->setStartValue(0); + anim2->setEndValue(100); + anim2->setDuration(200); + + QSignalSpy stateChangedSpy1(anim1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + QSignalSpy stateChangedSpy2(anim2, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + QCOMPARE(stateChangedSpy1.count(), 0); + QCOMPARE(stateChangedSpy2.count(), 0); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1->state(), QAnimationGroup::Stopped); + QCOMPARE(anim2->state(), QAnimationGroup::Stopped); + + group.addAnimation(anim1); + group.addAnimation(anim2); + + anim1->start(); + anim2->start(); + anim2->pause(); + + QVERIFY(compareStates(stateChangedSpy1, (StateList() << QAbstractAnimation::Running))); + + QVERIFY(compareStates(stateChangedSpy2, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Paused))); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1->state(), QAnimationGroup::Running); + QCOMPARE(anim2->state(), QAnimationGroup::Paused); + + group.start(); + + QVERIFY(compareStates(stateChangedSpy1, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Stopped + << QAbstractAnimation::Running))); + QVERIFY(compareStates(stateChangedSpy2, (StateList() << QAbstractAnimation::Running + << QAbstractAnimation::Paused))); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1->state(), QAnimationGroup::Running); + QCOMPARE(anim2->state(), QAnimationGroup::Paused); + + QTest::qWait(300); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim1->state(), QAnimationGroup::Stopped); + QCOMPARE(anim2->state(), QAnimationGroup::Running); + + QCOMPARE(stateChangedSpy2.count(), 4); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(2).at(1)), + QAnimationGroup::Stopped); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy2.at(3).at(1)), + QAnimationGroup::Running); + + group.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(anim1->state(), QAnimationGroup::Stopped); + QCOMPARE(anim2->state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::zeroDurationAnimation() +{ + QSequentialAnimationGroup group; + + TestAnimation *anim1 = new TestAnimation(); + anim1->setStartValue(0); + anim1->setEndValue(100); + anim1->setDuration(0); + + TestAnimation *anim2 = new TestAnimation(); + anim2->setStartValue(0); + anim2->setEndValue(100); + anim2->setDuration(100); + + AnimationObject o1; + QPropertyAnimation *anim3 = new QPropertyAnimation(&o1, "value"); + anim3->setEndValue(100); + anim3->setDuration(0); + + QSignalSpy stateChangedSpy(anim1, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + group.addAnimation(anim1); + group.addAnimation(anim2); + group.addAnimation(anim3); + group.setLoopCount(2); + group.start(); + + QCOMPARE(stateChangedSpy.count(), 2); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy.at(0).at(1)), + QAnimationGroup::Running); + QCOMPARE(qVariantValue<QAbstractAnimation::State>(stateChangedSpy.at(1).at(1)), + QAnimationGroup::Stopped); + + QCOMPARE(anim1->state(), QAnimationGroup::Stopped); + QCOMPARE(anim2->state(), QAnimationGroup::Running); + QCOMPARE(group.state(), QAnimationGroup::Running); + + //now let's try to seek to the next loop + group.setCurrentTime(group.duration() + 1); + QCOMPARE(anim1->state(), QAnimationGroup::Stopped); + QCOMPARE(anim2->state(), QAnimationGroup::Running); + QCOMPARE(anim3->state(), QAnimationGroup::Stopped); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(o1.value(), 100); //anim3 should have been run +} + +void tst_QSequentialAnimationGroup::stopUncontrolledAnimations() +{ + QSequentialAnimationGroup group; + + AnimationObject o1; + UncontrolledAnimation notTimeDriven(&o1, "value"); + QCOMPARE(notTimeDriven.totalDuration(), -1); + + TestAnimation loopsForever; + loopsForever.setStartValue(0); + loopsForever.setEndValue(100); + loopsForever.setDuration(100); + loopsForever.setLoopCount(-1); + + group.addAnimation(¬TimeDriven); + group.addAnimation(&loopsForever); + + group.start(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Running); + QCOMPARE(loopsForever.state(), QAnimationGroup::Stopped); + + notTimeDriven.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever.state(), QAnimationGroup::Running); + + loopsForever.stop(); + + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + QCOMPARE(loopsForever.state(), QAnimationGroup::Stopped); +} + +void tst_QSequentialAnimationGroup::finishWithUncontrolledAnimation() +{ + AnimationObject o1; + + //1st case: + //first we test a group with one uncontrolled animation + QSequentialAnimationGroup group; + UncontrolledAnimation notTimeDriven(&o1, "value", &group); + QSignalSpy spy(&group, SIGNAL(finished())); + + group.start(); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Running); + QCOMPARE(group.currentTime(), 0); + QCOMPARE(notTimeDriven.currentTime(), 0); + + QTest::qWait(300); //wait for the end of notTimeDriven + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + const int actualDuration = notTimeDriven.currentTime(); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(group.currentTime(), actualDuration); + QCOMPARE(spy.count(), 1); + + //2nd case: + // lets make sure the seeking will work again + spy.clear(); + QPropertyAnimation anim(&group); + QSignalSpy animStateChangedSpy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); + + group.setCurrentTime(300); + QCOMPARE(group.state(), QAnimationGroup::Stopped); + QCOMPARE(notTimeDriven.currentTime(), actualDuration); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&anim)); + + //3rd case: + //now let's add a perfectly defined animation at the end + QCOMPARE(animStateChangedSpy.count(), 0); + group.start(); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Running); + QCOMPARE(group.currentTime(), 0); + QCOMPARE(notTimeDriven.currentTime(), 0); + + QCOMPARE(animStateChangedSpy.count(), 0); + + QTest::qWait(300); //wait for the end of notTimeDriven + QCOMPARE(notTimeDriven.state(), QAnimationGroup::Stopped); + QCOMPARE(group.state(), QAnimationGroup::Running); + QCOMPARE(anim.state(), QAnimationGroup::Running); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&anim)); + QCOMPARE(animStateChangedSpy.count(), 1); + QTest::qWait(300); //wait for the end of anim + + QCOMPARE(anim.state(), QAnimationGroup::Stopped); + QCOMPARE(anim.currentTime(), anim.duration()); + + //we should simply be at the end + QCOMPARE(spy.count(), 1); + QCOMPARE(animStateChangedSpy.count(), 2); + QCOMPARE(group.currentTime(), notTimeDriven.currentTime() + anim.currentTime()); +} + +void tst_QSequentialAnimationGroup::addRemoveAnimation() +{ + //this test is specific to the sequential animation group + QSequentialAnimationGroup group; + + QCOMPARE(group.duration(), 0); + QCOMPARE(group.currentTime(), 0); + QAbstractAnimation *anim1 = new QPropertyAnimation; + group.addAnimation(anim1); + QCOMPARE(group.duration(), 250); + QCOMPARE(group.currentTime(), 0); + QCOMPARE(group.currentAnimation(), anim1); + + //let's append an animation + QAbstractAnimation *anim2 = new QPropertyAnimation; + group.addAnimation(anim2); + QCOMPARE(group.duration(), 500); + QCOMPARE(group.currentTime(), 0); + QCOMPARE(group.currentAnimation(), anim1); + + //let's prepend an animation + QAbstractAnimation *anim0 = new QPropertyAnimation; + group.insertAnimationAt(0, anim0); + QCOMPARE(group.duration(), 750); + QCOMPARE(group.currentTime(), 0); + QCOMPARE(group.currentAnimation(), anim0); //anim0 has become the new currentAnimation + + group.setCurrentTime(300); //anim0 | anim1 | anim2 + QCOMPARE(group.currentTime(), 300); + QCOMPARE(group.currentAnimation(), anim1); + QCOMPARE(anim1->currentTime(), 50); + + group.removeAnimation(anim0); //anim1 | anim2 + QCOMPARE(group.currentTime(), 50); + QCOMPARE(group.currentAnimation(), anim1); + QCOMPARE(anim1->currentTime(), 50); + + group.setCurrentTime(0); + group.insertAnimationAt(0, anim0); //anim0 | anim1 | anim2 + group.setCurrentTime(300); + QCOMPARE(group.currentTime(), 300); + QCOMPARE(group.currentAnimation(), anim1); + QCOMPARE(anim1->currentTime(), 50); + + group.removeAnimation(anim1); //anim0 | anim2 + QCOMPARE(group.currentTime(), 250); + QCOMPARE(group.currentAnimation(), anim2); + QCOMPARE(anim0->currentTime(), 250); +} + +void tst_QSequentialAnimationGroup::currentAnimation() +{ + QSequentialAnimationGroup group; + QVERIFY(group.currentAnimation() == 0); + + QPropertyAnimation anim; + anim.setDuration(0); + group.addAnimation(&anim); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&anim)); +} + +void tst_QSequentialAnimationGroup::currentAnimationWithZeroDuration() +{ + QSequentialAnimationGroup group; + QVERIFY(group.currentAnimation() == 0); + + QPropertyAnimation zero1; + zero1.setDuration(0); + QPropertyAnimation zero2; + zero2.setDuration(0); + + QPropertyAnimation anim; + + QPropertyAnimation zero3; + zero3.setDuration(0); + QPropertyAnimation zero4; + zero4.setDuration(0); + + + group.addAnimation(&zero1); + group.addAnimation(&zero2); + group.addAnimation(&anim); + group.addAnimation(&zero3); + group.addAnimation(&zero4); + + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&zero1)); + + group.setCurrentTime(0); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&anim)); + + group.setCurrentTime(group.duration()); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&zero4)); + + group.setDirection(QAbstractAnimation::Backward); + + group.setCurrentTime(0); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&zero1)); + + group.setCurrentTime(group.duration()); + QCOMPARE(group.currentAnimation(), static_cast<QAbstractAnimation*>(&anim)); +} + +void tst_QSequentialAnimationGroup::insertAnimation() +{ + QSequentialAnimationGroup group; + group.setLoopCount(2); + QPropertyAnimation *anim = new QPropertyAnimation(&group); + QCOMPARE(group.duration(), anim->duration()); + group.setCurrentTime(300); + QCOMPARE(group.currentLoop(), 1); + + //this will crash if the sequential group calls duration on the created animation + new QPropertyAnimation(&group); +} + + +class SequentialAnimationGroup : public QSequentialAnimationGroup +{ + Q_OBJECT +public slots: + void clearAnimations() + { + QSequentialAnimationGroup::clearAnimations(); + } + + void refill() + { + stop(); + clearAnimations(); + new QPropertyAnimation(this); + start(); + } + +}; + + +void tst_QSequentialAnimationGroup::clearAnimations() +{ + SequentialAnimationGroup group; + QPointer<QAbstractAnimation> anim1 = new QPropertyAnimation(&group); + group.connect(anim1, SIGNAL(finished()), SLOT(clearAnimations())); + new QPropertyAnimation(&group); + QCOMPARE(group.animationCount(), 2); + + group.start(); + QTest::qWait(anim1->duration() + 100); + QCOMPARE(group.animationCount(), 0); + QCOMPARE(group.state(), QAbstractAnimation::Stopped); + QCOMPARE(group.currentTime(), 0); + + anim1 = new QPropertyAnimation(&group); + group.connect(anim1, SIGNAL(finished()), SLOT(refill())); + group.start(); + QTest::qWait(anim1->duration() + 100); + QVERIFY(anim1 == 0); //anim1 should have been deleted + QCOMPARE(group.state(), QAbstractAnimation::Running); +} + +QTEST_MAIN(tst_QSequentialAnimationGroup) +#include "tst_qsequentialanimationgroup.moc" diff --git a/tests/auto/qset/tst_qset.cpp b/tests/auto/qset/tst_qset.cpp index 50c9bd6..5ed0010 100644 --- a/tests/auto/qset/tst_qset.cpp +++ b/tests/auto/qset/tst_qset.cpp @@ -74,6 +74,7 @@ private slots: void clear(); void remove(); void contains(); + void containsSet(); void begin(); void end(); void insert(); @@ -347,6 +348,40 @@ void tst_QSet::contains() } } +void tst_QSet::containsSet() +{ + QSet<QString> set1; + QSet<QString> set2; + + // empty set contains the empty set + QVERIFY(set1.contains(set2)); + + for (int i = 0; i < 500; ++i) { + set1.insert(QString::number(i)); + set2.insert(QString::number(i)); + } + QVERIFY(set1.contains(set2)); + + set2.remove(QString::number(19)); + set2.remove(QString::number(82)); + set2.remove(QString::number(7)); + QVERIFY(set1.contains(set2)); + + set1.remove(QString::number(23)); + QVERIFY(!set1.contains(set2)); + + // filled set contains the empty set as well + QSet<QString> set3; + QVERIFY(set1.contains(set3)); + + // the empty set doesn't contain a filled set + QVERIFY(!set3.contains(set1)); + + // verify const signature + const QSet<QString> set4; + QVERIFY(set3.contains(set4)); +} + void tst_QSet::begin() { QSet<int> set1; diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp index 4b8c204..3adef09 100644 --- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp @@ -912,7 +912,7 @@ void tst_QSharedPointer::invalidConstructs_data() QTest::newRow("implicit-initialization3") << &QTest::QExternalTest::tryCompileFail << "QWeakPointer<Data> ptr = new Data;"; - QTest::newRow("implicit-initialization1") + QTest::newRow("implicit-initialization4") << &QTest::QExternalTest::tryCompileFail << "QWeakPointer<Data> ptr;" "ptr = new Data;"; @@ -976,11 +976,13 @@ void tst_QSharedPointer::invalidConstructs_data() << &QTest::QExternalTest::tryCompileFail << "QSharedPointer<Data> ptr1;\n" "QSharedPointer<int> ptr2 = qSharedPointerCast<int>(ptr1);"; +#ifndef QTEST_NO_RTTI QTest::newRow("invalid-cast2") << &QTest::QExternalTest::tryCompileFail << "QSharedPointer<Data> ptr1;\n" "QSharedPointer<int> ptr2 = qSharedPointerDynamicCast<int>(ptr1);"; - QTest::newRow("implicit-initialization1") +#endif + QTest::newRow("invalid-cast3") << &QTest::QExternalTest::tryCompileFail << "QSharedPointer<Data> ptr1;\n" "QSharedPointer<int> ptr2 = qSharedPointerConstCast<int>(ptr1);"; @@ -1028,7 +1030,12 @@ void tst_QSharedPointer::invalidConstructs() QByteArray body = code.toLatin1(); - if (!(test.*testFunction)(body)) { + bool result = (test.*testFunction)(body); + if (qgetenv("QTEST_EXTERNAL_DEBUG").toInt() > 0) { + qDebug("External test output:"); + printf("%s\n", test.standardError().constData()); + } + if (!result) { qWarning("External code testing failed\nCode:\n%s\n", body.constData()); QFAIL("Fail"); } diff --git a/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp b/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp index 5fc5bd4..de239fb 100644 --- a/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp @@ -134,6 +134,7 @@ private slots: void task250023_fetchMore(); void task251296_hiddenChildren(); void task252507_mapFromToSource(); + void task255652_removeRowsRecursive(); protected: void buildHierarchy(const QStringList &data, QAbstractItemModel *model); @@ -390,7 +391,7 @@ void tst_QSortFilterProxyModel::sort() QModelIndex index = m_proxy->index(row, 0, QModelIndex()); QCOMPARE(m_proxy->data(index, Qt::DisplayRole).toString(), initial.at(row)); } - + } void tst_QSortFilterProxyModel::sortHierarchy_data() @@ -411,7 +412,7 @@ void tst_QSortFilterProxyModel::sortHierarchy_data() << static_cast<int>(Qt::AscendingOrder) << (QStringList() << "a" << "<" << "b" << "<" << "c" << ">" << ">") << (QStringList() << "a" << "<" << "b" << "<" << "c" << ">" << ">"); - + #if 1 QTest::newRow("hierarchical ascending") << static_cast<int>(Qt::AscendingOrder) @@ -571,7 +572,7 @@ void tst_QSortFilterProxyModel::insertRows() QModelIndex index = m_proxy->index(row, 0, QModelIndex()); QCOMPARE(m_proxy->data(index, Qt::DisplayRole).toString(), initial.at(row)); } - + // insert the row m_proxy->insertRows(position, insert.count(), QModelIndex()); QCOMPARE(m_model->rowCount(QModelIndex()), expected.count()); @@ -615,7 +616,7 @@ void tst_QSortFilterProxyModel::prependRow() QStandardItem sub2("sub2"); sub2.appendRow(new QStandardItem("sub3")); item.insertRow(0, &sub2); - + QModelIndex index_sub2 = proxy.mapFromSource(model.indexFromItem(&sub2)); QCOMPARE(sub2.rowCount(), proxy.rowCount(index_sub2)); @@ -626,7 +627,7 @@ void tst_QSortFilterProxyModel::prependRow() /* void tst_QSortFilterProxyModel::insertColumns_data() { - + } void tst_QSortFilterProxyModel::insertColumns() @@ -923,7 +924,7 @@ void tst_QSortFilterProxyModel::removeRows() // prepare model foreach (QString s, initial) model.appendRow(new QStandardItem(s)); - + // remove the rows QCOMPARE(proxy.removeRows(position, count, QModelIndex()), success); QCOMPARE(model.rowCount(QModelIndex()), expectedSource.count()); @@ -1164,10 +1165,10 @@ void tst_QSortFilterProxyModel::removeColumns() proxy.setSourceModel(&model); if (!filter.isEmpty()) proxy.setFilterRegExp(QRegExp(filter)); - + // prepare model model.setHorizontalHeaderLabels(initial); - + // remove the columns QCOMPARE(proxy.removeColumns(position, count, QModelIndex()), success); QCOMPARE(model.columnCount(QModelIndex()), expectedSource.count()); @@ -1266,7 +1267,7 @@ void tst_QSortFilterProxyModel::filter_data() << "golf" << "quebec" << "foxtrot" - << "india" + << "india" << "romeo" << "november" << "oskar" @@ -1274,9 +1275,9 @@ void tst_QSortFilterProxyModel::filter_data() << "kilo" << "whiskey" << "mike" - << "papa" + << "papa" << "sierra" - << "xray" + << "xray" << "viktor") << (QStringList() << "delta" @@ -1334,7 +1335,7 @@ void tst_QSortFilterProxyModel::filterHierarchy_data() << "foo" << "boo" << "baz" << "moo" << "laa" << "haa") << (QStringList() << "foo" << "boo" << "moo"); - + QTest::newRow("simple hierarchy") << "b.*z" << (QStringList() << "baz" << "<" << "boz" << "<" << "moo" << ">" << ">") << (QStringList() << "baz" << "<" << "boz" << ">"); @@ -1671,7 +1672,7 @@ void tst_QSortFilterProxyModel::removeSourceRows() QSignalSpy insertSpy(&proxy, SIGNAL(rowsInserted(QModelIndex, int, int))); QSignalSpy aboutToRemoveSpy(&proxy, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int))); QSignalSpy aboutToInsertSpy(&proxy, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int))); - + model.removeRows(start, count, QModelIndex()); QCOMPARE(aboutToRemoveSpy.count(), expectedRemovedProxyIntervals.count()); @@ -1827,7 +1828,7 @@ void tst_QSortFilterProxyModel::changeFilter() QFETCH(IntPairList, finalRemoveIntervals); QFETCH(IntPairList, insertIntervals); QFETCH(QStringList, finalProxyItems); - + QStandardItemModel model; QSortFilterProxyModel proxy; @@ -1880,7 +1881,7 @@ void tst_QSortFilterProxyModel::changeFilter() #ifdef Q_OS_IRIX QEXPECT_FAIL("filter (2)", "Not reliable on IRIX", Abort); -#endif +#endif QCOMPARE(finalInsertSpy.count(), insertIntervals.count()); for (int i = 0; i < finalInsertSpy.count(); ++i) { QList<QVariant> args = finalInsertSpy.at(i); @@ -2068,7 +2069,7 @@ void tst_QSortFilterProxyModel::sortFilterRole() model.setData(index, sourceItems.at(i).first, Qt::DisplayRole); model.setData(index, sourceItems.at(i).second, Qt::UserRole); } - + proxy.setFilterRegExp("2"); QCOMPARE(proxy.rowCount(), 0); // Qt::DisplayRole is default role @@ -2358,7 +2359,7 @@ void tst_QSortFilterProxyModel::sourceInsertRows() model.insertColumns(0, 1, parent); model.insertRows(0, 1, parent); } - + model.insertRows(0, 1, QModelIndex()); model.insertRows(0, 1, QModelIndex()); @@ -2462,7 +2463,7 @@ void tst_QSortFilterProxyModel::task236755_hiddenColumns() QVERIFY(view.isColumnHidden(0)); } -void tst_QSortFilterProxyModel::task247867_insertRowsSort() +void tst_QSortFilterProxyModel::task247867_insertRowsSort() { QStandardItemModel model(2,2); QSortFilterProxyModel proxyModel; @@ -2567,7 +2568,7 @@ void tst_QSortFilterProxyModel::task248868_dynamicSorting() QStringList initial2 = initial; initial2.replaceInStrings("bateau", "girafe"); model1.setStringList(initial2); //this will cause a reset - + QStringList expected2 = initial2; expected2.sort(); @@ -2648,7 +2649,7 @@ class QtTestModel: public QAbstractItemModel { if (!idx.isValid()) return QVariant(); - + if (role == Qt::DisplayRole) { if (idx.row() < 0 || idx.column() < 0 || idx.column() >= cols || idx.row() >= rows) { wrongIndex = true; @@ -2727,6 +2728,16 @@ void tst_QSortFilterProxyModel::task251296_hiddenChildren() QCOMPARE(proxy.rowCount(indexA) , 1); QModelIndex indexC = proxy.index(0, 0, indexA); QCOMPARE(proxy.data(indexC).toString(), QString::fromLatin1("C VISIBLE")); + + proxy.setFilterRegExp("C"); + QCOMPARE(proxy.rowCount(QModelIndex()), 0); + itemC->setText("invisible"); + itemA->setText("AC"); + + QCOMPARE(proxy.rowCount(QModelIndex()), 1); + indexA = proxy.index(0,0); + QCOMPARE(proxy.data(indexA).toString(), QString::fromLatin1("AC")); + QCOMPARE(proxy.rowCount(indexA) , 0); } void tst_QSortFilterProxyModel::task252507_mapFromToSource() @@ -2746,5 +2757,63 @@ void tst_QSortFilterProxyModel::task252507_mapFromToSource() QCOMPARE(proxy.mapFromSource(proxy.index(6, 2)), QModelIndex()); } +static QStandardItem *addEntry(QStandardItem* pParent, const QString &description) +{ + QStandardItem* pItem = new QStandardItem(description); + pParent->appendRow(pItem); + return pItem; +} + + +void tst_QSortFilterProxyModel::task255652_removeRowsRecursive() +{ + QStandardItemModel pModel; + QStandardItem *pItem1 = new QStandardItem("root"); + pModel.appendRow(pItem1); + QList<QStandardItem *> items; + + QStandardItem *pItem11 = addEntry(pItem1,"Sub-heading"); + items << pItem11; + QStandardItem *pItem111 = addEntry(pItem11,"A"); + items << pItem111; + items << addEntry(pItem111,"A1"); + items << addEntry(pItem111,"A2"); + QStandardItem *pItem112 = addEntry(pItem11,"B"); + items << pItem112; + items << addEntry(pItem112,"B1"); + items << addEntry(pItem112,"B2"); + QStandardItem *pItem1123 = addEntry(pItem112,"B3"); + items << pItem1123; + items << addEntry(pItem1123,"B3-"); + + QSortFilterProxyModel proxy; + proxy.setSourceModel(&pModel); + + QList<QPersistentModelIndex> sourceIndexes; + QList<QPersistentModelIndex> proxyIndexes; + foreach (QStandardItem *item, items) { + QModelIndex idx = item->index(); + sourceIndexes << idx; + proxyIndexes << proxy.mapFromSource(idx); + } + + foreach (const QPersistentModelIndex &pidx, sourceIndexes) + QVERIFY(pidx.isValid()); + foreach (const QPersistentModelIndex &pidx, proxyIndexes) + QVERIFY(pidx.isValid()); + + QList<QStandardItem*> itemRow = pItem1->takeRow(0); + + QCOMPARE(itemRow.count(), 1); + QCOMPARE(itemRow.first(), pItem11); + + foreach (const QPersistentModelIndex &pidx, sourceIndexes) + QVERIFY(!pidx.isValid()); + foreach (const QPersistentModelIndex &pidx, proxyIndexes) + QVERIFY(!pidx.isValid()); + + delete pItem11; +} + QTEST_MAIN(tst_QSortFilterProxyModel) #include "tst_qsortfilterproxymodel.moc" diff --git a/tests/auto/qspinbox/tst_qspinbox.cpp b/tests/auto/qspinbox/tst_qspinbox.cpp index 88c3f65..d6a208d 100644 --- a/tests/auto/qspinbox/tst_qspinbox.cpp +++ b/tests/auto/qspinbox/tst_qspinbox.cpp @@ -65,6 +65,7 @@ #include <QDoubleSpinBox> #include <QVBoxLayout> #include <QKeySequence> +#include <QStackedWidget> #include <QDebug> #include "../../shared/util.h" @@ -142,6 +143,9 @@ private slots: void specialValue(); void textFromValue(); + + void sizeHint(); + public slots: void valueChangedHelper(const QString &); void valueChangedHelper(int); @@ -241,6 +245,12 @@ void tst_QSpinBox::getSetCheck() QCOMPARE(0.0, obj2.value()); obj2.setValue(1.0); QCOMPARE(1.0, obj2.value()); + + // Make sure we update line edit geometry when updating + // buttons - see task 235747 + QRect oldEditGeometry = obj1.childrenRect(); + obj1.setButtonSymbols(QAbstractSpinBox::NoButtons); + QVERIFY(obj1.childrenRect() != oldEditGeometry); } tst_QSpinBox::tst_QSpinBox() @@ -950,5 +960,45 @@ void tst_QSpinBox::textFromValue() QCOMPARE(spinBox.textFromValue(INT_MIN), QString::number(INT_MIN)); } +class sizeHint_SpinBox : public QSpinBox +{ +public: + QSize sizeHint() const + { + ++sizeHintRequests; + return QSpinBox::sizeHint(); + } + mutable int sizeHintRequests; +}; + +void tst_QSpinBox::sizeHint() +{ + QWidget *widget = new QWidget; + QHBoxLayout *layout = new QHBoxLayout(widget); + sizeHint_SpinBox *spinBox = new sizeHint_SpinBox; + layout->addWidget(spinBox); + widget->show(); + QTest::qWait(100); + + // Prefix + spinBox->sizeHintRequests = 0; + spinBox->setPrefix(QLatin1String("abcdefghij")); + qApp->processEvents(); + QVERIFY(spinBox->sizeHintRequests > 0); + + // Suffix + spinBox->sizeHintRequests = 0; + spinBox->setSuffix(QLatin1String("abcdefghij")); + qApp->processEvents(); + QVERIFY(spinBox->sizeHintRequests > 0); + + // Range + spinBox->sizeHintRequests = 0; + spinBox->setRange(0, 1234567890); + spinBox->setValue(spinBox->maximum()); + qApp->processEvents(); + QVERIFY(spinBox->sizeHintRequests > 0); +} + QTEST_MAIN(tst_QSpinBox) #include "tst_qspinbox.moc" diff --git a/tests/auto/qsplitter/qsplitter.pro b/tests/auto/qsplitter/qsplitter.pro index 86d02b5..8c3e40b 100644 --- a/tests/auto/qsplitter/qsplitter.pro +++ b/tests/auto/qsplitter/qsplitter.pro @@ -8,4 +8,7 @@ wince*|symbian*: { addFiles.sources = extradata.txt setSizes3.dat addFiles.path = . DEPLOYMENT += addFiles + DEFINES += SRCDIR=\\\"./\\\" +} else { + DEFINES += SRCDIR=\\\"$$PWD/\\\" } diff --git a/tests/auto/qsplitter/tst_qsplitter.cpp b/tests/auto/qsplitter/tst_qsplitter.cpp index 9930a78..a44e25d 100644 --- a/tests/auto/qsplitter/tst_qsplitter.cpp +++ b/tests/auto/qsplitter/tst_qsplitter.cpp @@ -214,7 +214,7 @@ void tst_QSplitter::setSizes3_data() QTest::addColumn<IntList>("collapsibleStates"); QTest::addColumn<bool>("childrenCollapse"); - QFile file("setSizes3.dat"); + QFile file(SRCDIR "setSizes3.dat"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Can't open file, reason:" << file.errorString(); return; diff --git a/tests/auto/qsqldatabase/tst_databases.h b/tests/auto/qsqldatabase/tst_databases.h index bd51b97..b81182a 100644 --- a/tests/auto/qsqldatabase/tst_databases.h +++ b/tests/auto/qsqldatabase/tst_databases.h @@ -105,11 +105,7 @@ inline static QString qTableName( const QString& prefix, QSqlDriver* driver = 0 inline static bool testWhiteSpaceNames( const QString &name ) { -/* return name.startsWith( "QPSQL" ) - || name.startsWith( "QODBC" ) - || name.startsWith( "QSQLITE" ) - || name.startsWith( "QMYSQL" );*/ - return name != QLatin1String("QSQLITE2"); + return name != QLatin1String("QTDS7"); } inline static QString toHex( const QString& binary ) @@ -211,7 +207,7 @@ public: // This requires a local ODBC data source to be configured( pointing to a MySql database ) // addDb( "QODBC", "mysqlodbc", "troll", "trond" ); // addDb( "QODBC", "SqlServer", "troll", "trond" ); -// addDb( "QTDS7", "testdb", "troll", "trondk", "horsehead.nokia.troll.no" ); +// addDb( "QTDS7", "testdb", "troll", "trondk", "horsehead" ); // addDb( "QODBC", "silencetestdb", "troll", "trond", "silence" ); // addDb( "QODBC", "horseheadtestdb", "troll", "trondk", "horsehead" ); @@ -221,7 +217,7 @@ public: // addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3309, "CLIENT_COMPRESS=1;CLIENT_SSL=1" ); // MySQL 5.0.18 Linux // addDb( "QMYSQL3", "testdb", "troll", "trond", "iceblink.nokia.troll.no" ); // MySQL 5.0.13 Windows // addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql4-nokia.trolltech.com.au" ); // MySQL 4.1.22-2.el4 linux -// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql5-nokia.trolltech.com.au" ); // MySQL 5.0.45-7.el5 linux +// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql5-nokia.trolltech.com.au" ); // MySQL 5.0.45-7.el5 linux // addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no" ); // V7.2 NOT SUPPORTED! // addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5434 ); // V7.2 NOT SUPPORTED! Multi-byte @@ -243,16 +239,16 @@ public: // use in-memory database to prevent local files // addDb("QSQLITE", ":memory:"); - addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") ); + addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") ); // addDb( "QSQLITE2", QDir::toNativeSeparators(QDir::tempPath()+"/foo2.db") ); // addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.nokia.troll.no\\ICEBLINK", "troll", "trond", "" ); // addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.nokia.troll.no\\SQLEXPRESS", "troll", "trond", "" ); // addDb( "QODBC", "DRIVER={MySQL ODBC 3.51 Driver};SERVER=mysql5-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" ); -// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=horsehead.nokia.troll.no;DATABASE=testdb;PORT=4101;UID=troll;PWD=trondk", "troll", "trondk", "" ); -// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=silence.nokia.troll.no;DATABASE=testdb;PORT=2392;UID=troll;PWD=trond", "troll", "trond", "" ); -// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" ); -// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" ); +// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=horsehead.nokia.troll.no;DATABASE=testdb;PORT=4101;UID=troll;PWD=trondk;TDS_Version=8.0", "troll", "trondk", "" ); +// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=silence.nokia.troll.no;DATABASE=testdb;PORT=2392;UID=troll;PWD=trond;TDS_Version=8.0", "troll", "trond", "" ); +// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "testuser", "Ee4Gabf6_", "" ); +// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "testuser", "Ee4Gabf6_", "" ); // addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2003" ); // addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2008" ); } @@ -320,21 +316,28 @@ public: QSqlQuery q( db ); QStringList dbtables=db.tables(); - foreach(const QString &tableName, tableNames) { + foreach(QString tableName, tableNames) + { wasDropped = true; - foreach(const QString dbtablesName, dbtables) { - if(dbtablesName.toUpper() == tableName.toUpper()) { - dbtables.removeAll(dbtablesName); - wasDropped = q.exec("drop table " + db.driver()->escapeIdentifier( dbtablesName, QSqlDriver::TableName )); - if(!wasDropped) - wasDropped = q.exec("drop table " + dbtablesName); - } - } + QString table=tableName; + if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName)) + table = db.driver()->stripDelimiters(table, QSqlDriver::TableName); + + if ( dbtables.contains( table, Qt::CaseSensitive ) ) + wasDropped = q.exec( "drop table " + tableName); + else if ( dbtables.contains( table, Qt::CaseInsensitive ) ) + wasDropped = q.exec( "drop table " + tableName); + if ( !wasDropped ) qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError().text() << "tables:" << dbtables; } } + static void safeDropTable( QSqlDatabase db, const QString& tableName ) + { + safeDropTables(db, QStringList() << tableName); + } + static void safeDropViews( QSqlDatabase db, const QStringList &viewNames ) { if ( isMSAccess( db ) ) // Access is sooo stupid. @@ -356,6 +359,11 @@ public: } } + static void safeDropView( QSqlDatabase db, const QString& tableName ) + { + safeDropViews(db, QStringList() << tableName); + } + // returns the type name of the blob datatype for the database db. // blobSize is only used if the db doesn't have a generic blob type static QString blobTypeName( QSqlDatabase db, int blobSize = 10000 ) diff --git a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp index 21064c3..31a699c 100644 --- a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp @@ -249,7 +249,7 @@ struct FieldDef { // excluding the primary key field static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db) { - tst_Databases::safeDropTables(db, QStringList() << qTableName("qtestfields")); + tst_Databases::safeDropTable(db, qTableName("qtestfields")); QSqlQuery q(db); // construct a create table statement consisting of all fieldtypes QString qs = "create table " + qTableName("qtestfields"); @@ -358,6 +358,7 @@ void tst_QSqlDatabase::dropTestTables(QSqlDatabase db) tableNames << (qTableName("qtest") + " test"); tst_Databases::safeDropTables(db, tableNames); + tst_Databases::safeDropView(db, qTableName("qtest_view")); } void tst_QSqlDatabase::populateTestTables(QSqlDatabase db) @@ -541,11 +542,6 @@ void tst_QSqlDatabase::tables() QVERIFY(tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive)); QVERIFY(tables.contains(qTableName("qtest"), Qt::CaseInsensitive)); - if (tst_Databases::isMSAccess(db)) - QSqlQuery("drop table " + qTableName("qtest_view"), db); - else - QSqlQuery("drop view " + qTableName("qtest_view"), db); - if (db.driverName().startsWith("QPSQL")) { QVERIFY(tables.contains(qTableName("qtest") + " test")); } @@ -1082,8 +1078,8 @@ void tst_QSqlDatabase::recordMySQL() FieldDef("bigint unsigned", QVariant::ULongLong, Q_UINT64_C(18446744073709551615)), FieldDef("float", QVariant::Double, 1.12345), FieldDef("double", QVariant::Double, 1.123456789), - FieldDef("decimal(10, 9)", QVariant::String,1.123456789), - FieldDef("numeric(5, 2)", QVariant::String, 123.67), + FieldDef("decimal(10, 9)", QVariant::Double,1.123456789), + FieldDef("numeric(5, 2)", QVariant::Double, 123.67), FieldDef("date", QVariant::Date, QDate::currentDate()), FieldDef("datetime", QVariant::DateTime, dt), FieldDef("timestamp", QVariant::DateTime, dt, false), @@ -1630,62 +1626,82 @@ void tst_QSqlDatabase::precisionPolicy() QSKIP("Driver or database doesn't support setting precision policy", SkipSingle); // Create a test table with some data - QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num numeric(20,0))").arg(tableName))); + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num numeric(18,5))").arg(tableName))); QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); q.bindValue(0, 1); q.bindValue(1, 123); QVERIFY_SQL(q, exec()); q.bindValue(0, 2); - q.bindValue(1, QString("18500000000000000000")); + q.bindValue(1, 1850000000000.0001); QVERIFY_SQL(q, exec()); // These are expected to pass + q.setNumericalPrecisionPolicy(QSql::HighPrecision); QString query = QString("SELECT num FROM %1 WHERE id = 1").arg(tableName); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::String); q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(q.value(0).type() != QVariant::LongLong) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::LongLong); QCOMPARE(q.value(0).toLongLong(), (qlonglong)123); q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt32); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::Int); QCOMPARE(q.value(0).toInt(), 123); q.setNumericalPrecisionPolicy(QSql::LowPrecisionDouble); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::Double); QCOMPARE(q.value(0).toDouble(), (double)123); query = QString("SELECT num FROM %1 WHERE id = 2").arg(tableName); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::Double); - QCOMPARE(q.value(0).toDouble(), QString("18500000000000000000").toDouble()); + QCOMPARE(q.value(0).toDouble(), QString("1850000000000.0001").toDouble()); // Postgres returns invalid QVariants on overflow q.setNumericalPrecisionPolicy(QSql::HighPrecision); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); QCOMPARE(q.value(0).type(), QVariant::String); q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64); + QEXPECT_FAIL("QOCI", "Oracle fails here, to retrieve next", Continue); QVERIFY_SQL(q, exec(query)); QVERIFY_SQL(q, next()); - QCOMPARE(q.value(0).type(), QVariant::Invalid); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); + QCOMPARE(q.value(0).type(), QVariant::LongLong); - q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt32); - QVERIFY_SQL(q, exec(query)); - if(db.driverName().startsWith("QOCI")) - QEXPECT_FAIL("", "Oracle fails to move to next when data columns are oversize", Abort); - QVERIFY_SQL(q, next()); - QCOMPARE(q.value(0).type(), QVariant::Invalid); + QSql::NumericalPrecisionPolicy oldPrecision= db.numericalPrecisionPolicy(); + db.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64); + QSqlQuery q2(db); + q2.exec(QString("SELECT num FROM %1 WHERE id = 2").arg(tableName)); + QVERIFY_SQL(q2, exec(query)); + QVERIFY_SQL(q2, next()); + if(db.driverName().startsWith("QSQLITE")) + QEXPECT_FAIL("", "SQLite returns this value as determined by contents of the field, not the declaration", Continue); + QCOMPARE(q2.value(0).type(), QVariant::LongLong); + db.setNumericalPrecisionPolicy(oldPrecision); } // This test needs a ODBC data source containing MYSQL in it's name diff --git a/tests/auto/qsqldriver/qsqldriver.pro b/tests/auto/qsqldriver/qsqldriver.pro new file mode 100644 index 0000000..0024841 --- /dev/null +++ b/tests/auto/qsqldriver/qsqldriver.pro @@ -0,0 +1,16 @@ +load(qttest_p4) +SOURCES += tst_qsqldriver.cpp + +QT += sql + +wince*: { + plugFiles.sources = ../../../plugins/sqldrivers + plugFiles.path = . + DEPLOYMENT += plugFiles +} else { + win32-g++ { + LIBS += -lws2_32 + } else:win32 { + LIBS += ws2_32.lib + } +} diff --git a/tests/auto/qsqldriver/tst_qsqldriver.cpp b/tests/auto/qsqldriver/tst_qsqldriver.cpp new file mode 100644 index 0000000..6d428df --- /dev/null +++ b/tests/auto/qsqldriver/tst_qsqldriver.cpp @@ -0,0 +1,214 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> +#include <QtSql/QtSql> + +#include "../qsqldatabase/tst_databases.h" + + + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QSqlDriver : public QObject +{ + Q_OBJECT + +public: + void recreateTestTables(QSqlDatabase); + + tst_Databases dbs; + +public slots: + void initTestCase_data(); + void initTestCase(); + void cleanupTestCase(); + void init(); + void cleanup(); + +private slots: + void record(); + void primaryIndex(); +}; + + +void tst_QSqlDriver::initTestCase_data() +{ + dbs.open(); + if (dbs.fillTestTable() == 0) { + qWarning("NO DATABASES"); + QSKIP("No database drivers are available in this Qt configuration", SkipAll); + } +} + +void tst_QSqlDriver::recreateTestTables(QSqlDatabase db) +{ + QSqlQuery q(db); + + tst_Databases::safeDropTable( db, qTableName( "relTEST1" ) ); + + QVERIFY_SQL( q, exec("create table " + qTableName("relTEST1") + + " (id int not null primary key, name varchar(20), title_key int, another_title_key int)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(1, 'harry', 1, 2)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(2, 'trond', 2, 1)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(3, 'vohi', 1, 2)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(4, 'boris', 2, 2)")); +} + +void tst_QSqlDriver::initTestCase() +{ + foreach (const QString &dbname, dbs.dbNames) + recreateTestTables(QSqlDatabase::database(dbname)); +} + +void tst_QSqlDriver::cleanupTestCase() +{ + foreach (const QString &dbName, dbs.dbNames) { + QSqlDatabase db = QSqlDatabase::database(dbName); + tst_Databases::safeDropTable( db, qTableName( "relTEST1" ) ); + } + dbs.close(); +} + +void tst_QSqlDriver::init() +{ +} + +void tst_QSqlDriver::cleanup() +{ +} + +void tst_QSqlDriver::record() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + QString tablename = qTableName("relTEST1"); + QStringList fields; + fields << "id" << "name" << "title_key" << "another_title_key"; + + //check we can get records using an unquoted mixed case table name + QSqlRecord rec = db.driver()->record(tablename); + QCOMPARE(rec.count(), 4); + + if (db.driverName().startsWith("QIBASE")|| db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + for(int i = 0; i < fields.count(); ++i) + fields[i] = fields[i].toUpper(); + + for (int i = 0; i < fields.count(); ++i) + QCOMPARE(rec.fieldName(i), fields[i]); + + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + tablename = tablename.toUpper(); + else if (db.driverName().startsWith("QPSQL")) + tablename = tablename.toLower(); + + //check we can get records using a properly quoted table name + rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); + QCOMPARE(rec.count(), 4); + + for (int i = 0; i < fields.count(); ++i) + QCOMPARE(rec.fieldName(i), fields[i]); + + if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + tablename = tablename.toLower(); + else if (db.driverName().startsWith("QPSQL")) + tablename = tablename.toUpper(); + + //check that we can't get records using incorrect tablename casing that's been quoted + rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); + if (db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QTDS")) + QCOMPARE(rec.count(), 4); //mysql, sqlite and tds will match + else + QCOMPARE(rec.count(), 0); + +} + +void tst_QSqlDriver::primaryIndex() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + QString tablename = qTableName("relTEST1"); + //check that we can get primary index using unquoted mixed case table name + QSqlIndex index = db.driver()->primaryIndex(tablename); + QCOMPARE(index.count(), 1); + + if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + QCOMPARE(index.fieldName(0), QString::fromLatin1("ID")); + else + QCOMPARE(index.fieldName(0), QString::fromLatin1("id")); + + + //check that we can get the primary index using a quoted tablename + if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + tablename = tablename.toUpper(); + else if (db.driverName().startsWith("QPSQL")) + tablename = tablename.toLower(); + + index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName)); + QCOMPARE(index.count(), 1); + if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + QCOMPARE(index.fieldName(0), QString::fromLatin1("ID")); + else + QCOMPARE(index.fieldName(0), QString::fromLatin1("id")); + + + + //check that we can not get the primary index using a quoted but incorrect table name casing + if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) + tablename = tablename.toLower(); + else if (db.driverName().startsWith("QPSQL")) + tablename = tablename.toUpper(); + + index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName)); + if (db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QTDS")) + QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing + else + QCOMPARE(index.count(), 0); +} + +QTEST_MAIN(tst_QSqlDriver) +#include "tst_qsqldriver.moc" diff --git a/tests/auto/qsqlquery/tst_qsqlquery.cpp b/tests/auto/qsqlquery/tst_qsqlquery.cpp index ed19e91..ab7f0c9 100644 --- a/tests/auto/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/qsqlquery/tst_qsqlquery.cpp @@ -290,7 +290,8 @@ void tst_QSqlQuery::dropTestTables( QSqlDatabase db ) << qTableName( "bindtest" ) << qTableName( "more_results" ) << qTableName( "blobstest" ) - << qTableName( "oraRowId" ); + << qTableName( "oraRowId" ) + << qTableName( "qtest_batch" ); if ( db.driverName().startsWith("QSQLITE") ) tablenames << qTableName( "record_sqlite" ); @@ -303,6 +304,7 @@ void tst_QSqlQuery::dropTestTables( QSqlDatabase db ) tablenames << qTableName( "Planet" ); tablenames << qTableName( "task_250026" ); + tablenames << qTableName( "task_234422" ); tst_Databases::safeDropTables( db, tablenames ); } @@ -1902,71 +1904,48 @@ void tst_QSqlQuery::batchExec() QSKIP( "Database can't do BatchOperations", SkipSingle ); QSqlQuery q( db ); + QString tableName = qTableName( "qtest_batch" ); - q.exec( "drop table " + qTableName( "qtest_batch" ) ); - - QVERIFY_SQL( q, exec( "create table " + qTableName( "qtest_batch" ) + " (id int, name varchar(20), dt date, num numeric(8, 4))" ) ); - - QVERIFY_SQL( q, prepare( "insert into " + qTableName( "qtest_batch" ) + " (id, name, dt, num) values (?, ?, ?, ?)" ) ); + QVERIFY_SQL( q, exec( "create table " + tableName + " (id int, name varchar(20), dt date, num numeric(8, 4))" ) ); + QVERIFY_SQL( q, prepare( "insert into " + tableName + " (id, name, dt, num) values (?, ?, ?, ?)" ) ); QVariantList intCol; - intCol << 1 << 2 << QVariant( QVariant::Int ); QVariantList charCol; - charCol << QLatin1String( "harald" ) << QLatin1String( "boris" ) << QVariant( QVariant::String ); QVariantList dateCol; - QDateTime dt = QDateTime( QDate::currentDate(), QTime( 1, 2, 3 ) ); - dateCol << dt << dt.addDays( -1 ) << QVariant( QVariant::DateTime ); QVariantList numCol; - numCol << 2.3 << 3.4 << QVariant( QVariant::Double ); q.addBindValue( intCol ); - q.addBindValue( charCol ); - q.addBindValue( dateCol ); - q.addBindValue( numCol ); QVERIFY_SQL( q, execBatch() ); - - QVERIFY_SQL( q, exec( "select id, name, dt, num from " + qTableName( "qtest_batch" ) + " order by id" ) ); + QVERIFY_SQL( q, exec( "select id, name, dt, num from " + tableName + " order by id" ) ); QVERIFY( q.next() ); - QCOMPARE( q.value( 0 ).toInt(), 1 ); - QCOMPARE( q.value( 1 ).toString(), QString( "harald" ) ); - QCOMPARE( q.value( 2 ).toDateTime(), dt ); - QCOMPARE( q.value( 3 ).toDouble(), 2.3 ); QVERIFY( q.next() ); - QCOMPARE( q.value( 0 ).toInt(), 2 ); - QCOMPARE( q.value( 1 ).toString(), QString( "boris" ) ); - QCOMPARE( q.value( 2 ).toDateTime(), dt.addDays( -1 ) ); - QCOMPARE( q.value( 3 ).toDouble(), 3.4 ); QVERIFY( q.next() ); - QVERIFY( q.value( 0 ).isNull() ); - QVERIFY( q.value( 1 ).isNull() ); - QVERIFY( q.value( 2 ).isNull() ); - QVERIFY( q.value( 3 ).isNull() ); } @@ -2344,6 +2323,8 @@ void tst_QSqlQuery::sqlite_finish() QString tableName = qTableName( "qtest_lockedtable" ); QSqlQuery q( db ); + + tst_Databases::safeDropTable( db2, tableName ); q.exec( "CREATE TABLE " + tableName + " (pk_id INTEGER PRIMARY KEY, whatever TEXT)" ); q.exec( "INSERT INTO " + tableName + " values(1, 'whatever')" ); q.exec( "INSERT INTO " + tableName + " values(2, 'whatever more')" ); @@ -2361,7 +2342,8 @@ void tst_QSqlQuery::sqlite_finish() q.finish(); QVERIFY_SQL( q2, exec( "DELETE FROM " + tableName + " WHERE pk_id=2" ) ); QCOMPARE( q2.numRowsAffected(), 1 ); - q.exec( "DROP TABLE " + tableName ); + + tst_Databases::safeDropTable( db2, tableName ); } QSqlDatabase::removeDatabase( "sqlite_finish_sqlite" ); @@ -2574,71 +2556,47 @@ void tst_QSqlQuery::blobsPreparedQuery() QString tableName = qTableName( "blobstest" ); QSqlQuery q( db ); - q.setForwardOnly( true ); // This is needed to make the test work with DB2. - QString shortBLOB( "abc" ); - QString longerBLOB( "abcdefghijklmnopqrstuvxyz¿äëïöü¡ " ); // In PostgreSQL a BLOB is not called a BLOB, but a BYTEA! :-) // ... and in SQL Server it can be called a lot, but IMAGE will do. QString typeName( "BLOB" ); - if ( db.driverName().startsWith( "QPSQL" ) ) typeName = "BYTEA"; else if ( db.driverName().startsWith( "QODBC" ) ) typeName = "IMAGE"; QVERIFY_SQL( q, exec( QString( "CREATE TABLE %1(id INTEGER, data %2)" ).arg( tableName ).arg( typeName ) ) ); - q.prepare( QString( "INSERT INTO %1(id, data) VALUES(:id, :data)" ).arg( tableName ) ); - q.bindValue( ":id", 1 ); - q.bindValue( ":data", shortBLOB.toAscii() ); - QVERIFY_SQL( q, exec() ); q.bindValue( ":id", 2 ); - q.bindValue( ":data", longerBLOB.toAscii() ); - QVERIFY_SQL( q, exec() ); // Two executions and result sets q.prepare( QString( "SELECT data FROM %1 WHERE id = ?" ).arg( tableName ) ); - q.bindValue( 0, QVariant( 1 ) ); - QVERIFY_SQL( q, exec() ); - QVERIFY_SQL( q, next() ); - QCOMPARE( q.value( 0 ).toString(), shortBLOB ); q.bindValue( 0, QVariant( 2 ) ); - QVERIFY_SQL( q, exec() ); - QVERIFY_SQL( q, next() ); - QCOMPARE( q.value( 0 ).toString(), longerBLOB ); // Only one execution and result set q.prepare( QString( "SELECT id, data FROM %1 ORDER BY id" ).arg( tableName ) ); - QVERIFY_SQL( q, exec() ); - QVERIFY_SQL( q, next() ); - QCOMPARE( q.value( 1 ).toString(), shortBLOB ); - QVERIFY_SQL( q, next() ); - QCOMPARE( q.value( 1 ).toString(), longerBLOB ); - - q.exec( QString( "DROP TABLE %1" ).arg( tableName ) ); } // There were problems with navigating past the end of a table returning an error on mysql @@ -2790,7 +2748,6 @@ void tst_QSqlQuery::task_234422() QString tableName = qTableName( "task_234422" ); - query.exec("DROP TABLE " + tableName); QVERIFY_SQL(query,exec("CREATE TABLE " + tableName + " (id int primary key, " "name varchar(20), homecountry varchar(2))")); for (int i = 0; i < m_airlines.count(); ++i) { diff --git a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index 5725548..e2dace8 100644 --- a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -82,6 +82,13 @@ private slots: void insertRecordDuplicateFieldNames(); void invalidData(); void relationModel(); + void casing(); + void escapedRelations(); + void escapedTableName(); + void whiteSpaceInIdentifiers(); + +private: + void dropTestTables( QSqlDatabase db ); }; @@ -96,16 +103,9 @@ void tst_QSqlRelationalTableModel::initTestCase_data() void tst_QSqlRelationalTableModel::recreateTestTables(QSqlDatabase db) { - QSqlQuery q(db); - - QStringList tableNames; - tableNames << qTableName( "reltest1" ) - << qTableName( "reltest2" ) - << qTableName( "reltest3" ) - << qTableName( "reltest4" ) - << qTableName( "reltest5" ); - tst_Databases::safeDropTables( db, tableNames ); + dropTestTables(db); + QSqlQuery q(db); QVERIFY_SQL( q, exec("create table " + qTableName("reltest1") + " (id int not null primary key, name varchar(20), title_key int, another_title_key int)")); QVERIFY_SQL( q, exec("insert into " + qTableName("reltest1") + " values(1, 'harry', 1, 2)")); @@ -128,6 +128,19 @@ void tst_QSqlRelationalTableModel::recreateTestTables(QSqlDatabase db) QVERIFY_SQL( q, exec("create table " + qTableName("reltest5") + " (title varchar(20) not null primary key, abbrev varchar(20))")); QVERIFY_SQL( q, exec("insert into " + qTableName("reltest5") + " values('herr', 'Hr')")); QVERIFY_SQL( q, exec("insert into " + qTableName("reltest5") + " values('mister', 'Mr')")); + + if (testWhiteSpaceNames(db.driverName())) { + QString reltest6 = db.driver()->escapeIdentifier(qTableName("rel test6"), QSqlDriver::TableName); + QVERIFY_SQL( q, exec("create table " + reltest6 + " (id int not null primary key, " + db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName) + + " int, " + db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName) + " int)")); + QVERIFY_SQL( q, exec("insert into " + reltest6 + " values(1, 1,9)")); + QVERIFY_SQL( q, exec("insert into " + reltest6 + " values(2, 2,8)")); + + QString reltest7 = db.driver()->escapeIdentifier(qTableName("rel test7"), QSqlDriver::TableName); + QVERIFY_SQL( q, exec("create table " + reltest7 + " (" + db.driver()->escapeIdentifier("city id", QSqlDriver::TableName) + " int not null primary key, " + db.driver()->escapeIdentifier("city name", QSqlDriver::FieldName) + " varchar(20))")); + QVERIFY_SQL( q, exec("insert into " + reltest7 + " values(1, 'New York')")); + QVERIFY_SQL( q, exec("insert into " + reltest7 + " values(2, 'Washington')")); + } } void tst_QSqlRelationalTableModel::initTestCase() @@ -138,18 +151,29 @@ void tst_QSqlRelationalTableModel::initTestCase() void tst_QSqlRelationalTableModel::cleanupTestCase() { - QStringList tableNames; - tableNames << qTableName( "reltest1" ) - << qTableName( "reltest2" ) - << qTableName( "reltest3" ) - << qTableName( "reltest4" ); foreach (const QString &dbName, dbs.dbNames) { QSqlDatabase db = QSqlDatabase::database(dbName); - tst_Databases::safeDropTables( db, tableNames ); + CHECK_DATABASE( db ); + dropTestTables( QSqlDatabase::database(dbName) ); } dbs.close(); } +void tst_QSqlRelationalTableModel::dropTestTables( QSqlDatabase db ) +{ + QStringList tableNames; + tableNames << qTableName( "reltest1" ) + << qTableName( "reltest2" ) + << qTableName( "reltest3" ) + << qTableName( "reltest4" ) + << qTableName( "reltest5" ) + << qTableName( "rel test6", db.driver() ) + << qTableName( "rel test7", db.driver() ) + << qTableName("CASETEST1", db.driver() ) + << qTableName("casetest1", db.driver() ); + tst_Databases::safeDropTables( db, tableNames ); +} + void tst_QSqlRelationalTableModel::init() { } @@ -273,7 +297,12 @@ void tst_QSqlRelationalTableModel::setData() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + + //sybase doesn't allow tables with the same alias used twice as col names + //so don't set up an identical relation when using the tds driver + if (!db.driverName().startsWith("QTDS")) + model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setEditStrategy(QSqlTableModel::OnManualSubmit); model.setSort(0, Qt::AscendingOrder); QVERIFY_SQL(model, select()); @@ -284,7 +313,10 @@ void tst_QSqlRelationalTableModel::setData() QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi2")); QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(0, 3)).toInt(), 1); QVERIFY_SQL(model, submitAll()); } @@ -299,10 +331,15 @@ void tst_QSqlRelationalTableModel::setData() QCOMPARE(model.data(model.index(0, 3)).toInt(), 1); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + if (!db.driverName().startsWith("QTDS")) + model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); QVERIFY_SQL(model, select()); QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(0, 3)).toInt(), 1); } //check setting of data when the relational key is a non-integer type @@ -336,7 +373,8 @@ void tst_QSqlRelationalTableModel::multipleRelation() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setRelation(3, QSqlRelation(qTableName("reltest4"), "id", "name")); + model.setSort(0, Qt::AscendingOrder); QVERIFY_SQL(model, select()); QCOMPARE(model.data(model.index(2, 0)).toInt(), 3); @@ -344,7 +382,7 @@ void tst_QSqlRelationalTableModel::multipleRelation() QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0, 3)).toString(), QString("mister")); + QCOMPARE(model.data(model.index(0, 3)).toString(), QString("Trondheim")); } void tst_QSqlRelationalTableModel::insertRecord() @@ -357,6 +395,7 @@ void tst_QSqlRelationalTableModel::insertRecord() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setSort(0, Qt::AscendingOrder); QVERIFY_SQL(model, select()); QSqlRecord rec; @@ -398,6 +437,7 @@ void tst_QSqlRelationalTableModel::setRecord() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setSort(0, Qt::AscendingOrder); QVERIFY_SQL(model, select()); QSqlRecord rec; @@ -450,13 +490,18 @@ void tst_QSqlRelationalTableModel::insertWithStrategies() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + + if (!db.driverName().startsWith("QTDS")) + model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); QVERIFY_SQL(model, select()); QCOMPARE(model.data(model.index(0,0)).toInt(), 1); QCOMPARE(model.data(model.index(0,1)).toString(), QString("harry")); QCOMPARE(model.data(model.index(0,2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); + else + QCOMPARE(model.data(model.index(0,3)).toInt(), 2); model.insertRows(0, 1); model.setData(model.index(0, 0), 1011); @@ -467,12 +512,20 @@ void tst_QSqlRelationalTableModel::insertWithStrategies() QCOMPARE(model.data(model.index(0,0)).toInt(), 1011); QCOMPARE(model.data(model.index(0,1)).toString(), QString("test")); QCOMPARE(model.data(model.index(0,2)).toString(), QString("mister")); - QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(0,3)).toInt(), 1); QCOMPARE(model.data(model.index(1,0)).toInt(), 1); QCOMPARE(model.data(model.index(1,1)).toString(), QString("harry")); QCOMPARE(model.data(model.index(1,2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(1,3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(1,3)).toString(), QString("mister")); + else + QCOMPARE(model.data(model.index(1,3)).toInt(), 2); + + QVERIFY_SQL(model, submitAll()); @@ -481,9 +534,16 @@ void tst_QSqlRelationalTableModel::insertWithStrategies() QCOMPARE(model.data(model.index(0,0)).toInt(), 1); QCOMPARE(model.data(model.index(0,1)).toString(), QString("harry")); QCOMPARE(model.data(model.index(0,2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); - model.setData(model.index(0,3),1); - QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr")); + + if (!db.driverName().startsWith("QTDS")) { + QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); + model.setData(model.index(0,3),1); + QCOMPARE(model.data(model.index(0,3)).toString(), QString("herr")); + } else { + QCOMPARE(model.data(model.index(0,3)).toInt(), 2); + model.setData(model.index(0,3),1); + QCOMPARE(model.data(model.index(0,3)).toInt(), 1); + } model.insertRows(0, 2); model.setData(model.index(0, 0), 1012); @@ -499,17 +559,27 @@ void tst_QSqlRelationalTableModel::insertWithStrategies() QCOMPARE(model.data(model.index(0,0)).toInt(),1012); QCOMPARE(model.data(model.index(0,1)).toString(), QString("george")); QCOMPARE(model.data(model.index(0,2)).toString(), QString("mister")); - QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0,3)).toString(), QString("mister")); + else + QCOMPARE(model.data(model.index(0,3)).toInt(), 2); + QCOMPARE(model.data(model.index(1,0)).toInt(),1013); QCOMPARE(model.data(model.index(1,1)).toString(), QString("kramer")); QCOMPARE(model.data(model.index(1,2)).toString(), QString("mister")); - QCOMPARE(model.data(model.index(1,3)).toString(), QString("herr")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(1,3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(1,3)).toInt(), 1); QCOMPARE(model.data(model.index(2,0)).toInt(), 1); QCOMPARE(model.data(model.index(2,1)).toString(), QString("harry")); QCOMPARE(model.data(model.index(2,2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(2,3)).toString(), QString("herr")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(2,3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(2,3)).toInt(), 1); QVERIFY_SQL(model, submitAll()); } @@ -574,7 +644,8 @@ void tst_QSqlRelationalTableModel::sort() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + if (!db.driverName().startsWith("QTDS")) + model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); model.setSort(2, Qt::DescendingOrder); QVERIFY_SQL(model, select()); @@ -589,11 +660,19 @@ void tst_QSqlRelationalTableModel::sort() model.setSort(3, Qt::AscendingOrder); QVERIFY_SQL(model, select()); - QCOMPARE(model.rowCount(), 4); - QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(1, 3)).toString(), QString("mister")); - QCOMPARE(model.data(model.index(2, 3)).toString(), QString("mister")); - QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) { + QCOMPARE(model.rowCount(), 4); + QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + QCOMPARE(model.data(model.index(1, 3)).toString(), QString("mister")); + QCOMPARE(model.data(model.index(2, 3)).toString(), QString("mister")); + QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister")); + } else { + QCOMPARE(model.data(model.index(0, 3)).toInt(), 1); + QCOMPARE(model.data(model.index(1, 3)).toInt(), 2); + QCOMPARE(model.data(model.index(2, 3)).toInt(), 2); + QCOMPARE(model.data(model.index(3, 3)).toInt(), 2); + } + } static void testRevert(QSqlRelationalTableModel &model) @@ -663,7 +742,7 @@ void tst_QSqlRelationalTableModel::revert() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setRelation(3, QSqlRelation(qTableName("reltest4"), "id", "name")); model.setSort(0, Qt::AscendingOrder); @@ -689,7 +768,9 @@ void tst_QSqlRelationalTableModel::clearDisplayValuesCache() model.setTable(qTableName("reltest1")); model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + + if (!db.driverName().startsWith("QTDS")) + model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); model.setSort(1, Qt::AscendingOrder); model.setEditStrategy(QSqlTableModel::OnManualSubmit); @@ -698,7 +779,10 @@ void tst_QSqlRelationalTableModel::clearDisplayValuesCache() QCOMPARE(model.data(model.index(3, 0)).toInt(), 3); QCOMPARE(model.data(model.index(3, 1)).toString(), QString("vohi")); QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(3, 3)).toString(), QString("mister")); + else + QCOMPARE(model.data(model.index(3, 3)).toInt(), 2 ); model.insertRow(model.rowCount()); QVERIFY(model.setData(model.index(4, 0), 5, Qt::EditRole)); @@ -710,11 +794,18 @@ void tst_QSqlRelationalTableModel::clearDisplayValuesCache() QCOMPARE(model.data(model.index(0, 0)).toInt(), 5); QCOMPARE(model.data(model.index(0, 1)).toString(), QString("anders")); QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(0, 3)).toString(), QString("herr")); + else + QCOMPARE(model.data(model.index(0, 3)).toInt(), 1); + QCOMPARE(model.data(model.index(4, 0)).toInt(), 3); QCOMPARE(model.data(model.index(4, 1)).toString(), QString("vohi")); QCOMPARE(model.data(model.index(4, 2)).toString(), QString("herr")); - QCOMPARE(model.data(model.index(4, 3)).toString(), QString("mister")); + if (!db.driverName().startsWith("QTDS")) + QCOMPARE(model.data(model.index(4, 3)).toString(), QString("mister")); + else + QCOMPARE(model.data(model.index(4, 3)).toInt(), 2); } // For task 140782 and 176374: If the main table and the the related tables uses the same @@ -729,27 +820,38 @@ void tst_QSqlRelationalTableModel::insertRecordDuplicateFieldNames() QSqlRelationalTableModel model(0, db); model.setTable(qTableName("reltest3")); model.setEditStrategy(QSqlTableModel::OnManualSubmit); + model.setSort(0, Qt::AscendingOrder); // Duplication of "name", used in both reltest3 and reltest4. model.setRelation(2, QSqlRelation(qTableName("reltest4"), "id", "name")); QVERIFY_SQL(model, select()); - QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name"))).toString(), + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name")).toUpper()).toString(), + QString("Trondheim")); + } else { + QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name"))).toString(), QString("Trondheim")); + } QSqlRecord rec = model.record(); rec.setValue(0, 3); rec.setValue(1, "Berge"); rec.setValue(2, 1); // Must insert the key value - QCOMPARE(rec.fieldName(0), QLatin1String("id")); - QCOMPARE(rec.fieldName(1), QLatin1String("name")); // This comes from main table + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + QCOMPARE(rec.fieldName(0), QLatin1String("ID")); + QCOMPARE(rec.fieldName(1), QLatin1String("NAME")); // This comes from main table + } else { + QCOMPARE(rec.fieldName(0), QLatin1String("id")); + QCOMPARE(rec.fieldName(1), QLatin1String("name")); + } // The duplicate field names is aliased because it's comes from the relation's display column. - if(!db.driverName().startsWith("QIBASE")) - QCOMPARE(rec.fieldName(2), qTableName("reltest4").append(QLatin1String("_name"))); - else + if(db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) QCOMPARE(rec.fieldName(2), (qTableName("reltest4").append(QLatin1String("_name"))).toUpper()); + else + QCOMPARE(rec.fieldName(2), qTableName("reltest4").append(QLatin1String("_name"))); QVERIFY(model.insertRecord(-1, rec)); QCOMPARE(model.data(model.index(2, 2)).toString(), QString("Oslo")); @@ -793,7 +895,7 @@ void tst_QSqlRelationalTableModel::relationModel() QVERIFY(model.relationModel(3) == NULL); QVERIFY(model.relationModel(4) == NULL); - model.setRelation(3, QSqlRelation(qTableName("reltest2"), "tid", "title")); + model.setRelation(3, QSqlRelation(qTableName("reltest4"), "id", "name")); QVERIFY_SQL(model, select()); QVERIFY(model.relationModel(0) == NULL); @@ -806,5 +908,278 @@ void tst_QSqlRelationalTableModel::relationModel() QCOMPARE(rel_model->data(rel_model->index(0,1)).toString(), QString("herr")); } +void tst_QSqlRelationalTableModel::casing() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + if (db.driverName().startsWith("QSQLITE")) + QSKIP("The casing test for SQLITE is irrelevant since SQLITE is case insensitive", SkipAll); + + QSqlQuery q(db); + QVERIFY_SQL( q, exec("create table " + qTableName("CASETEST1", db.driver()).toUpper() + + " (id int not null primary key, name varchar(20), title_key int, another_title_key int)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(1, 'harry', 1, 2)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(2, 'trond', 2, 1)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(3, 'vohi', 1, 2)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("CASETEST1", db.driver()).toUpper() + " values(4, 'boris', 2, 2)")); + + QVERIFY_SQL( q, exec("create table " + qTableName("casetest1", db.driver()) + + " (ident int not null primary key, name varchar(20), title_key int)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(1, 'jerry', 1)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(2, 'george', 2)")); + QVERIFY_SQL( q, exec("insert into " + qTableName("casetest1", db.driver()) + " values(4, 'kramer', 2)")); + + if (db.driverName().startsWith("QOCI")) { + //try an owner that doesn't exist + QSqlRecord rec = db.driver()->record("doug." + qTableName("CASETEST1", db.driver()).toUpper()); + QCOMPARE( rec.count(), 0); + + //try an owner that does exist + rec = db.driver()->record(db.userName() + "." + qTableName("CASETEST1", db.driver()).toUpper()); + QCOMPARE( rec.count(), 4); + } + QSqlRecord rec = db.driver()->record(qTableName("CASETEST1", db.driver()).toUpper()); + QCOMPARE( rec.count(), 4); + + rec = db.driver()->record(qTableName("casetest1", db.driver())); + QCOMPARE( rec.count(), 3); + + QSqlTableModel upperCaseModel(0, db); + upperCaseModel.setTable(qTableName("CASETEST1", db.driver()).toUpper()); + + QCOMPARE(upperCaseModel.tableName(),qTableName("CASETEST1",db.driver()).toUpper()); + + QVERIFY_SQL(upperCaseModel, select()); + + QCOMPARE(upperCaseModel.rowCount(), 4); + + QSqlTableModel lowerCaseModel(0, db); + lowerCaseModel.setTable(qTableName("casetest1", db.driver())); + QCOMPARE(lowerCaseModel.tableName(), qTableName("casetest1",db.driver())); + QVERIFY_SQL(lowerCaseModel, select()); + + QCOMPARE(lowerCaseModel.rowCount(), 3); + + QSqlRelationalTableModel model(0, db); + model.setTable(qTableName("CASETEST1", db.driver()).toUpper()); + model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); +} + +void tst_QSqlRelationalTableModel::escapedRelations() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + recreateTestTables(db); + + QSqlRelationalTableModel model(0, db); + model.setTable(qTableName("reltest1")); + + //try with relation table name quoted + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(qTableName("reltest2").toUpper(),QSqlDriver::TableName), + "tid", + "title")); + } else { + model.setRelation(2, QSqlRelation(db.driver()->escapeIdentifier(qTableName("reltest2"),QSqlDriver::TableName), + "tid", + "title")); + + } + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); + + //try with index column quoted + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + db.driver()->escapeIdentifier("tid", QSqlDriver::FieldName).toUpper(), + "title")); + } else { + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + db.driver()->escapeIdentifier("tid", QSqlDriver::FieldName), + "title")); + } + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); + + //try with display column quoted + + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + "tid", + db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper())); + } else { + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + "tid", + db.driver()->escapeIdentifier("title", QSqlDriver::FieldName))); + } + + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); + + //try with tablename and index and display columns quoted in the relation + + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + "tid", + db.driver()->escapeIdentifier("title", QSqlDriver::FieldName).toUpper())); + } else { + model.setRelation(2, QSqlRelation(qTableName("reltest2"), + "tid", + db.driver()->escapeIdentifier("title", QSqlDriver::FieldName))); + } + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("herr")); +} + +void tst_QSqlRelationalTableModel::escapedTableName() +{ + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + // set the values using OnRowChange Strategy with an escaped tablename + { + QSqlRelationalTableModel model(0, db); + + if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { + model.setTable(db.driver()->escapeIdentifier(qTableName("reltest1").toUpper(), QSqlDriver::TableName)); + } else { + model.setTable(db.driver()->escapeIdentifier(qTableName("reltest1"), QSqlDriver::TableName)); + } + model.setSort(0, Qt::AscendingOrder); + model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); + QVERIFY_SQL(model, select()); + + QVERIFY(model.setData(model.index(0, 1), QString("harry2"))); + QVERIFY(model.setData(model.index(0, 2), 2)); + + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister")); + + model.submit(); + + QVERIFY(model.setData(model.index(3,1), QString("boris2"))); + QVERIFY(model.setData(model.index(3, 2), 1)); + + QCOMPARE(model.data(model.index(3,1)).toString(), QString("boris2")); + QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr")); + + model.submit(); + } + { //verify values + QSqlRelationalTableModel model(0, db); + model.setTable(qTableName("reltest1")); + model.setSort(0, Qt::AscendingOrder); + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); + QCOMPARE(model.data(model.index(0, 2)).toInt(), 2); + QCOMPARE(model.data(model.index(3, 1)).toString(), QString("boris2")); + QCOMPARE(model.data(model.index(3, 2)).toInt(), 1); + + model.setRelation(2, QSqlRelation(qTableName("reltest2"), "tid", "title")); + QVERIFY_SQL(model, select()); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("mister")); + QCOMPARE(model.data(model.index(3,2)).toString(), QString("herr")); + + } +} + +void tst_QSqlRelationalTableModel::whiteSpaceInIdentifiers() { + + QFETCH_GLOBAL(QString, dbName); + QSqlDatabase db = QSqlDatabase::database(dbName); + CHECK_DATABASE(db); + + if (!testWhiteSpaceNames(db.driverName())) + QSKIP("White space test irrelevant for driver", SkipAll); + QSqlRelationalTableModel model(0, db); + model.setTable(db.driver()->escapeIdentifier(qTableName("rel test6"), QSqlDriver::TableName)); + model.setSort(0, Qt::DescendingOrder); + model.setRelation(1, QSqlRelation(db.driver()->escapeIdentifier(qTableName("rel test7"), QSqlDriver::TableName), + db.driver()->escapeIdentifier("city id", QSqlDriver::FieldName), + db.driver()->escapeIdentifier("city name", QSqlDriver::FieldName))); + QVERIFY_SQL(model, select()); + + QCOMPARE(model.data(model.index(0,1)).toString(), QString("Washington")); + QCOMPARE(model.data(model.index(1,1)).toString(), QString("New York")); + + QSqlRecord rec; + QSqlField f1("id", QVariant::Int); + QSqlField f2(db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName), QVariant::Int); + QSqlField f3(db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName), QVariant::Int); + + f1.setValue(3); + f2.setValue(2); + f3.setValue(7); + + f1.setGenerated(true); + f2.setGenerated(true); + f3.setGenerated(true); + + rec.append(f1); + rec.append(f2); + rec.append(f3); + + QVERIFY_SQL(model, insertRecord(-1, rec)); + model.submitAll(); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 3); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("Washington")); + QCOMPARE(model.data(model.index(0, 2)).toInt(), 7); + + //TODO: For some reson setting a record using manual submit fails + //model.setEditStrategy(QSqlTableModel::OnManualSubmit); + + QSqlRecord recNew; + QSqlField f1New("id", QVariant::Int); + QSqlField f2New(db.driver()->escapeIdentifier("city key", QSqlDriver::FieldName), QVariant::Int); + QSqlField f3New(db.driver()->escapeIdentifier("extra field", QSqlDriver::FieldName), QVariant::Int); + + f1New.setValue(4); + f2New.setValue(1); + f3New.setValue(6); + + f1New.setGenerated(true); + f2New.setGenerated(true); + f3New.setGenerated(true); + + recNew.append(f1New); + recNew.append(f2New); + recNew.append(f3New); + + QVERIFY_SQL(model, setRecord(0, recNew)); + + QCOMPARE(model.data(model.index(0, 0)).toInt(), 4); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("New York")); + QCOMPARE(model.data(model.index(0, 2)).toInt(), 6); + + QVERIFY_SQL(model, submitAll()); + QCOMPARE(model.data(model.index(0, 0)).toInt(), 4); + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("New York")); + QCOMPARE(model.data(model.index(0, 2)).toInt(), 6); +} + QTEST_MAIN(tst_QSqlRelationalTableModel) #include "tst_qsqlrelationaltablemodel.moc" diff --git a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp index 5d1f9d4..1445f34 100644 --- a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp @@ -145,7 +145,7 @@ void tst_QSqlTableModel::dropTestTables() << qTableName("bigtable") << qTableName("foo"); if (testWhiteSpaceNames(db.driverName())) - tableNames << qTableName("qtestw hitespace"); + tableNames << qTableName("qtestw hitespace", db.driver()); tst_Databases::safeDropTables(db, tableNames); @@ -277,6 +277,7 @@ void tst_QSqlTableModel::setRecord() QList<QSqlTableModel::EditStrategy> policies = QList<QSqlTableModel::EditStrategy>() << QSqlTableModel::OnFieldChange << QSqlTableModel::OnRowChange << QSqlTableModel::OnManualSubmit; + QString Xsuffix; foreach( QSqlTableModel::EditStrategy submitpolicy, policies) { QSqlTableModel model(0, db); @@ -295,6 +296,8 @@ void tst_QSqlTableModel::setRecord() if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnManualSubmit) QVERIFY(model.submitAll()); + else if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1) + model.submit(); else { // dataChanged() is not emitted when submitAll() is called QCOMPARE(spy.count(), 2); @@ -304,10 +307,12 @@ void tst_QSqlTableModel::setRecord() } } - QCOMPARE(model.data(model.index(0, 1)).toString(), QString("fooX")); - QCOMPARE(model.data(model.index(0, 2)).toString(), QString("barX")); - QCOMPARE(model.data(model.index(1, 1)).toString(), QString("bazX")); - QCOMPARE(model.data(model.index(1, 2)).toString(), QString("joeX")); + Xsuffix.append('X'); + + QCOMPARE(model.data(model.index(0, 1)).toString(), QString("foo").append(Xsuffix)); + QCOMPARE(model.data(model.index(0, 2)).toString(), QString("bar").append(Xsuffix)); + QCOMPARE(model.data(model.index(1, 1)).toString(), QString("baz").append(Xsuffix)); + QCOMPARE(model.data(model.index(1, 2)).toString(), QString("joe").append(Xsuffix)); } } @@ -894,7 +899,7 @@ void tst_QSqlTableModel::sqlite_attachedDatabase() QVERIFY_SQL( q, exec("INSERT INTO atest2 VALUES(2, 'attached-atest2')")); QSqlQuery q2(db); - tst_Databases::safeDropTables(db, QStringList() << "atest"); + tst_Databases::safeDropTable(db, "atest"); QVERIFY_SQL(q2, exec("CREATE TABLE atest(id int, text varchar(20))")); QVERIFY_SQL(q2, exec("INSERT INTO atest VALUES(3, 'main')")); QVERIFY_SQL(q2, exec("ATTACH DATABASE \""+attachedDb.databaseName()+"\" as adb")); diff --git a/tests/auto/qsslsocket/tst_qsslsocket.cpp b/tests/auto/qsslsocket/tst_qsslsocket.cpp index ecd736b..295302f 100644 --- a/tests/auto/qsslsocket/tst_qsslsocket.cpp +++ b/tests/auto/qsslsocket/tst_qsslsocket.cpp @@ -137,6 +137,7 @@ private slots: void addCaCertificates2(); void ciphers(); void connectToHostEncrypted(); + void connectToHostEncryptedWithVerificationPeerName(); void sessionCipher(); void flush(); void isEncrypted(); @@ -609,6 +610,32 @@ void tst_QSslSocket::connectToHostEncrypted() QVERIFY(socket->waitForDisconnected()); } +void tst_QSslSocket::connectToHostEncryptedWithVerificationPeerName() +{ + if (!QSslSocket::supportsSsl()) + return; + + QSslSocketPtr socket = newSocket(); + this->socket = socket; + + socket->addCaCertificates(QLatin1String("certs/qt-test-server-cacert.pem")); +#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND + connect(&socket, SIGNAL(sslErrors(QList<QSslError>)), + this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); +#endif + + // connect to the server with its local name, but use the full name for verification. + socket->connectToHostEncrypted(QtNetworkSettings::serverLocalName(), 443, QtNetworkSettings::serverName()); + + // This should pass unconditionally when using fluke's CA certificate. + QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); + + socket->disconnectFromHost(); + QVERIFY(socket->waitForDisconnected()); + + QCOMPARE(socket->mode(), QSslSocket::SslClientMode); +} + void tst_QSslSocket::sessionCipher() { if (!QSslSocket::supportsSsl()) @@ -1287,6 +1314,8 @@ protected: // delayed start of encryption QTest::qSleep(100); QSslSocket *socket = server.socket; + Q_ASSERT(socket); + Q_ASSERT(socket->isValid()); socket->ignoreSslErrors(); socket->startServerEncryption(); if (!socket->waitForEncrypted(2000)) diff --git a/tests/auto/qstandarditemmodel/tst_qstandarditemmodel.cpp b/tests/auto/qstandarditemmodel/tst_qstandarditemmodel.cpp index 1b8b0c4..85611e2 100644 --- a/tests/auto/qstandarditemmodel/tst_qstandarditemmodel.cpp +++ b/tests/auto/qstandarditemmodel/tst_qstandarditemmodel.cpp @@ -135,6 +135,7 @@ private slots: void rootItemFlags(); void treeDragAndDrop(); + void removeRowsAndColumns(); private: QAbstractItemModel *m_model; @@ -1405,7 +1406,7 @@ bool tst_QStandardItemModel::compareItems(QStandardItem *item1, QStandardItem *i return true; if (!item1 || !item2) return false; - if (item1->text() != item2->text()){ + if (item1->text() != item2->text()){ qDebug() << item1->text() << item2->text(); return false; } @@ -1610,6 +1611,52 @@ void tst_QStandardItemModel::treeDragAndDrop() } } +void tst_QStandardItemModel::removeRowsAndColumns() +{ +#define VERIFY_MODEL \ + for (int c = 0; c < col_list.count(); c++) \ + for (int r = 0; r < row_list.count(); r++) \ + QCOMPARE(model.item(r,c)->text() , row_list[r] + "x" + col_list[c]); + + QVector<QString> row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(',').toVector(); + QVector<QString> col_list = row_list; + QStandardItemModel model; + for (int c = 0; c < col_list.count(); c++) + for (int r = 0; r < row_list.count(); r++) + model.setItem(r, c, new QStandardItem(row_list[r] + "x" + col_list[c])); + VERIFY_MODEL + + row_list.remove(3); + model.removeRow(3); + VERIFY_MODEL + + col_list.remove(5); + model.removeColumn(5); + VERIFY_MODEL + + row_list.remove(2, 5); + model.removeRows(2, 5); + VERIFY_MODEL + + col_list.remove(1, 6); + model.removeColumns(1, 6); + VERIFY_MODEL + + QList<QStandardItem *> row_taken = model.takeRow(6); + QCOMPARE(row_taken.count(), col_list.count()); + for (int c = 0; c < col_list.count(); c++) + QCOMPARE(row_taken[c]->text() , row_list[6] + "x" + col_list[c]); + row_list.remove(6); + VERIFY_MODEL + + QList<QStandardItem *> col_taken = model.takeColumn(10); + QCOMPARE(col_taken.count(), row_list.count()); + for (int r = 0; r < row_list.count(); r++) + QCOMPARE(col_taken[r]->text() , row_list[r] + "x" + col_list[10]); + col_list.remove(10); + VERIFY_MODEL +} + QTEST_MAIN(tst_QStandardItemModel) #include "tst_qstandarditemmodel.moc" diff --git a/tests/auto/qstate/qstate.pro b/tests/auto/qstate/qstate.pro new file mode 100644 index 0000000..9131fa8 --- /dev/null +++ b/tests/auto/qstate/qstate.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +QT = core +SOURCES += tst_qstate.cpp + + diff --git a/tests/auto/qstate/tst_qstate.cpp b/tests/auto/qstate/tst_qstate.cpp new file mode 100644 index 0000000..ab87767 --- /dev/null +++ b/tests/auto/qstate/tst_qstate.cpp @@ -0,0 +1,340 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include "qstate.h" +#include "qstatemachine.h" +#include "qsignaltransition.h" + +// Will try to wait for the condition while allowing event processing +#define QTRY_COMPARE(__expr, __expected) \ + do { \ + const int __step = 50; \ + const int __timeout = 5000; \ + if ((__expr) != (__expected)) { \ + QTest::qWait(0); \ + } \ + for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ + QTest::qWait(__step); \ + } \ + QCOMPARE(__expr, __expected); \ + } while(0) + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QState : public QObject +{ + Q_OBJECT + +public: + tst_QState(); + virtual ~tst_QState(); + +private slots: +#if 0 + void test(); +#endif + void assignProperty(); + void assignPropertyTwice(); + void historyInitialState(); + +private: + bool functionCalled; +}; + +tst_QState::tst_QState() : functionCalled(false) +{ +} + +tst_QState::~tst_QState() +{ +} + +#if 0 +void tst_QState::test() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + + QCOMPARE(s1->machine(), &machine); + QCOMPARE(s1->parentState(), machine.rootState()); + QCOMPARE(s1->initialState(), (QState*)0); + QVERIFY(s1->childStates().isEmpty()); + QVERIFY(s1->transitions().isEmpty()); + + QCOMPARE(s1->isFinal(), false); + s1->setFinal(true); + QCOMPARE(s1->isFinal(), true); + s1->setFinal(false); + QCOMPARE(s1->isFinal(), false); + + QCOMPARE(s1->isParallel(), false); + s1->setParallel(true); + QCOMPARE(s1->isParallel(), true); + s1->setParallel(false); + QCOMPARE(s1->isParallel(), false); + + QCOMPARE(s1->isAtomic(), true); + QCOMPARE(s1->isCompound(), false); + QCOMPARE(s1->isComplex(), false); + + QState *s11 = new QState(s1); + QCOMPARE(s11->parentState(), s1); + QCOMPARE(s11->isAtomic(), true); + QCOMPARE(s11->isCompound(), false); + QCOMPARE(s11->isComplex(), false); + QCOMPARE(s11->machine(), s1->machine()); + QVERIFY(s11->isDescendantOf(s1)); + + QCOMPARE(s1->initialState(), (QState*)0); + QCOMPARE(s1->childStates().size(), 1); + QCOMPARE(s1->childStates().at(0), s11); + + QCOMPARE(s1->isAtomic(), false); + QCOMPARE(s1->isCompound(), true); + QCOMPARE(s1->isComplex(), true); + + s1->setParallel(true); + QCOMPARE(s1->isAtomic(), false); + QCOMPARE(s1->isCompound(), false); + QCOMPARE(s1->isComplex(), true); + + QState *s12 = new QState(s1); + QCOMPARE(s12->parentState(), s1); + QCOMPARE(s12->isAtomic(), true); + QCOMPARE(s12->isCompound(), false); + QCOMPARE(s12->isComplex(), false); + QCOMPARE(s12->machine(), s1->machine()); + QVERIFY(s12->isDescendantOf(s1)); + QVERIFY(!s12->isDescendantOf(s11)); + + QCOMPARE(s1->initialState(), (QState*)0); + QCOMPARE(s1->childStates().size(), 2); + QCOMPARE(s1->childStates().at(0), s11); + QCOMPARE(s1->childStates().at(1), s12); + + QCOMPARE(s1->isAtomic(), false); + QCOMPARE(s1->isCompound(), false); + QCOMPARE(s1->isComplex(), true); + + s1->setParallel(false); + QCOMPARE(s1->isAtomic(), false); + QCOMPARE(s1->isCompound(), true); + QCOMPARE(s1->isComplex(), true); + + s1->setInitialState(s11); + QCOMPARE(s1->initialState(), s11); + + s1->setInitialState(0); + QCOMPARE(s1->initialState(), (QState*)0); + + s1->setInitialState(s12); + QCOMPARE(s1->initialState(), s12); + + QState *s13 = new QState(); + s1->setInitialState(s13); + QCOMPARE(s13->parentState(), s1); + QCOMPARE(s1->childStates().size(), 3); + QCOMPARE(s1->childStates().at(0), s11); + QCOMPARE(s1->childStates().at(1), s12); + QCOMPARE(s1->childStates().at(2), s13); + QVERIFY(s13->isDescendantOf(s1)); + + QVERIFY(s12->childStates().isEmpty()); + + QState *s121 = new QState(s12); + QCOMPARE(s121->parentState(), s12); + QCOMPARE(s121->isAtomic(), true); + QCOMPARE(s121->isCompound(), false); + QCOMPARE(s121->isComplex(), false); + QCOMPARE(s121->machine(), s12->machine()); + QVERIFY(s121->isDescendantOf(s12)); + QVERIFY(s121->isDescendantOf(s1)); + QVERIFY(!s121->isDescendantOf(s11)); + + QCOMPARE(s12->childStates().size(), 1); + QCOMPARE(s12->childStates().at(0), (QState*)s121); + + QCOMPARE(s1->childStates().size(), 3); + QCOMPARE(s1->childStates().at(0), s11); + QCOMPARE(s1->childStates().at(1), s12); + QCOMPARE(s1->childStates().at(2), s13); + + s11->addTransition(s12); + QCOMPARE(s11->transitions().size(), 1); + QCOMPARE(s11->transitions().at(0)->sourceState(), s11); + QCOMPARE(s11->transitions().at(0)->targetStates().size(), 1); + QCOMPARE(s11->transitions().at(0)->targetStates().at(0), s12); + QCOMPARE(s11->transitions().at(0)->eventType(), QEvent::None); + + QState *s14 = new QState(); + s12->addTransition(QList<QState*>() << s13 << s14); + QCOMPARE(s12->transitions().size(), 1); + QCOMPARE(s12->transitions().at(0)->sourceState(), s12); + QCOMPARE(s12->transitions().at(0)->targetStates().size(), 2); + QCOMPARE(s12->transitions().at(0)->targetStates().at(0), s13); + QCOMPARE(s12->transitions().at(0)->targetStates().at(1), s14); + QCOMPARE(s12->transitions().at(0)->eventType(), QEvent::None); + + s13->addTransition(this, SIGNAL(destroyed()), s14); + QCOMPARE(s13->transitions().size(), 1); + QCOMPARE(s13->transitions().at(0)->sourceState(), s13); + QCOMPARE(s13->transitions().at(0)->targetStates().size(), 1); + QCOMPARE(s13->transitions().at(0)->targetStates().at(0), s14); + QCOMPARE(s13->transitions().at(0)->eventType(), QEvent::Signal); + QVERIFY(qobject_cast<QSignalTransition*>(s13->transitions().at(0)) != 0); + + delete s13->transitions().at(0); + QCOMPARE(s13->transitions().size(), 0); + + s12->addTransition(this, SIGNAL(destroyed()), s11); + QCOMPARE(s12->transitions().size(), 2); +} +#endif + +class TestClass: public QObject +{ + Q_OBJECT +public: + TestClass() : called(false) {} + bool called; + +public slots: + void slot() { called = true; } + + +}; + +void tst_QState::assignProperty() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("fooBar", 10); + + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(object, "fooBar", 20); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("fooBar").toInt(), 20); +} + +void tst_QState::assignPropertyTwice() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("fooBar", 10); + + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(object, "fooBar", 20); + s1->assignProperty(object, "fooBar", 30); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("fooBar").toInt(), 30); +} + +class EventTestTransition: public QAbstractTransition +{ +public: + EventTestTransition(QEvent::Type type, QState *targetState) + : QAbstractTransition(QList<QAbstractState*>() << targetState), m_type(type) + { + } + +protected: + bool eventTest(QEvent *e) + { + return e->type() == m_type; + } + + void onTransition(QEvent *) {} + +private: + QEvent::Type m_type; + +}; + +void tst_QState::historyInitialState() +{ + QStateMachine machine; + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + QHistoryState *h1 = new QHistoryState(s2); + + s2->setInitialState(h1); + + QState *s3 = new QState(s2); + h1->setDefaultState(s3); + + QState *s4 = new QState(s2); + + s1->addTransition(new EventTestTransition(QEvent::User, s2)); + s2->addTransition(new EventTestTransition(QEvent::User, s1)); + s3->addTransition(new EventTestTransition(QEvent::Type(QEvent::User+1), s4)); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s3)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s3)); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User+1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s4)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s4)); +} + + +QTEST_MAIN(tst_QState) +#include "tst_qstate.moc" diff --git a/tests/auto/qstatemachine/qstatemachine.pro b/tests/auto/qstatemachine/qstatemachine.pro new file mode 100644 index 0000000..e5b32b5 --- /dev/null +++ b/tests/auto/qstatemachine/qstatemachine.pro @@ -0,0 +1,4 @@ +load(qttest_p4) +QT = core gui +SOURCES += tst_qstatemachine.cpp + diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp new file mode 100644 index 0000000..a859866 --- /dev/null +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -0,0 +1,3836 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> +#include <QtCore/QCoreApplication> +#include <QtGui/QPushButton> + +#include "qstatemachine.h" +#include "qstate.h" +#include "qhistorystate.h" +#include "qkeyeventtransition.h" +#include "qmouseeventtransition.h" +#include "private/qstate_p.h" +#include "private/qstatemachine_p.h" + +// Will try to wait for the condition while allowing event processing +#define QTRY_COMPARE(__expr, __expected) \ + do { \ + const int __step = 50; \ + const int __timeout = 5000; \ + if ((__expr) != (__expected)) { \ + QTest::qWait(0); \ + } \ + for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ + QTest::qWait(__step); \ + } \ + QCOMPARE(__expr, __expected); \ + } while(0) + +//TESTED_CLASS= +//TESTED_FILES= + +static int globalTick; + +// Run exec for a maximum of TIMEOUT msecs +#define QCOREAPPLICATION_EXEC(TIMEOUT) \ +{ \ + QTimer timer; \ + timer.setSingleShot(true); \ + timer.setInterval(TIMEOUT); \ + timer.start(); \ + connect(&timer, SIGNAL(timeout()), QCoreApplication::instance(), SLOT(quit())); \ + QCoreApplication::exec(); \ +} + +class SignalEmitter : public QObject +{ +Q_OBJECT + public: + SignalEmitter(QObject *parent = 0) + : QObject(parent) {} + void emitSignalWithNoArg() + { emit signalWithNoArg(); } + void emitSignalWithIntArg(int arg) + { emit signalWithIntArg(arg); } + void emitSignalWithStringArg(const QString &arg) + { emit signalWithStringArg(arg); } +Q_SIGNALS: + void signalWithNoArg(); + void signalWithIntArg(int); + void signalWithStringArg(const QString &); +}; + +class tst_QStateMachine : public QObject +{ + Q_OBJECT +public: + tst_QStateMachine(); + virtual ~tst_QStateMachine(); + +private slots: + void init(); + void cleanup(); + + void rootState(); + void addAndRemoveState(); + void stateEntryAndExit(); + void assignProperty(); + void assignPropertyWithAnimation(); + void postEvent(); + void stateFinished(); + void parallelStates(); + void parallelRootState(); + void allSourceToTargetConfigurations(); + void signalTransitions(); + void eventTransitions(); + void historyStates(); + void startAndStop(); + void targetStateWithNoParent(); + void targetStateDeleted(); + void transitionToRootState(); + void transitionEntersParent(); + + void defaultErrorState(); + void customGlobalErrorState(); + void customLocalErrorStateInBrokenState(); + void customLocalErrorStateInOtherState(); + void customLocalErrorStateInParentOfBrokenState(); + void customLocalErrorStateOverridesParent(); + void errorStateHasChildren(); + void errorStateHasErrors(); + void errorStateIsRootState(); + void errorStateEntersParentFirst(); + void customErrorStateIsNull(); + void clearError(); + void historyStateHasNowhereToGo(); + void historyStateAsInitialState(); + void brokenStateIsNeverEntered(); + void customErrorStateNotInGraph(); + void transitionToStateNotInGraph(); + void restoreProperties(); + + void defaultGlobalRestorePolicy(); + void globalRestorePolicySetToRestore(); + void globalRestorePolicySetToDoNotRestore(); + + void noInitialStateForInitialState(); + + //void restorePolicyNotInherited(); + //void mixedRestoreProperties(); + //void setRestorePolicyToDoNotRestore(); + //void setGlobalRestorePolicyToGlobalRestore(); + //void restorePolicyOnChildState(); + + void transitionWithParent(); + void transitionsFromParallelStateWithNoChildren(); + void parallelStateTransition(); + void parallelStateAssignmentsDone(); + void nestedRestoreProperties(); + void nestedRestoreProperties2(); + + void simpleAnimation(); + void twoAnimations(); + void twoAnimatedTransitions(); + void playAnimationTwice(); + void nestedTargetStateForAnimation(); + void animatedGlobalRestoreProperty(); + void specificTargetValueOfAnimation(); + + void addDefaultAnimation(); + void addDefaultAnimationWithUnusedAnimation(); + void removeDefaultAnimation(); + void overrideDefaultAnimationWithSpecific(); + +// void addDefaultAnimationForSource(); +// void addDefaultAnimationForTarget(); +// void removeDefaultAnimationForSource(); +// void removeDefaultAnimationForTarget(); +// void overrideDefaultAnimationWithSource(); +// void overrideDefaultAnimationWithTarget(); +// void overrideDefaultSourceAnimationWithSpecific(); +// void overrideDefaultTargetAnimationWithSpecific(); +// void overrideDefaultTargetAnimationWithSource(); +}; + +tst_QStateMachine::tst_QStateMachine() +{ +} + +tst_QStateMachine::~tst_QStateMachine() +{ +} + +class TestState : public QState +{ +public: + enum Event { + Entry, + Exit + }; + TestState(QState *parent) + : QState(parent) {} + QList<QPair<int, Event> > events; +protected: + virtual void onEntry(QEvent *) { + events.append(qMakePair(globalTick++, Entry)); + } + virtual void onExit(QEvent *) { + events.append(qMakePair(globalTick++, Exit)); + } +}; + +class TestTransition : public QAbstractTransition +{ +public: + TestTransition(QAbstractState *target) + : QAbstractTransition(QList<QAbstractState*>() << target) {} + QList<int> triggers; +protected: + virtual bool eventTest(QEvent *) { + return true; + } + virtual void onTransition(QEvent *) { + triggers.append(globalTick++); + } +}; + +void tst_QStateMachine::init() +{ +} + +void tst_QStateMachine::cleanup() +{ +} + +class EventTransition : public QAbstractTransition +{ +public: + EventTransition(QEvent::Type type, QAbstractState *target, QState *parent = 0) + : QAbstractTransition(QList<QAbstractState*>() << target, parent), m_type(type) {} +protected: + virtual bool eventTest(QEvent *e) { + return (e->type() == m_type); + } + virtual void onTransition(QEvent *) {} +private: + QEvent::Type m_type; +}; + +void tst_QStateMachine::transitionToRootState() +{ + QStateMachine machine; + + QState *initialState = new QState(); + machine.addState(initialState); + machine.setInitialState(initialState); + + QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::setTargetStates: root state cannot be target of transition"); + initialState->addTransition(new EventTransition(QEvent::User, machine.rootState())); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(initialState)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(initialState)); +} + +void tst_QStateMachine::transitionEntersParent() +{ + QStateMachine machine; + + QObject *entryController = new QObject(&machine); + entryController->setObjectName("entryController"); + entryController->setProperty("greatGrandParentEntered", false); + entryController->setProperty("grandParentEntered", false); + entryController->setProperty("parentEntered", false); + entryController->setProperty("stateEntered", false); + + QState *greatGrandParent = new QState(); + greatGrandParent->setObjectName("grandParent"); + greatGrandParent->assignProperty(entryController, "greatGrandParentEntered", true); + machine.addState(greatGrandParent); + machine.setInitialState(greatGrandParent); + + QState *grandParent = new QState(greatGrandParent); + grandParent->setObjectName("grandParent"); + grandParent->assignProperty(entryController, "grandParentEntered", true); + + QState *parent = new QState(grandParent); + parent->setObjectName("parent"); + parent->assignProperty(entryController, "parentEntered", true); + + QState *state = new QState(parent); + state->setObjectName("state"); + state->assignProperty(entryController, "stateEntered", true); + + QState *initialStateOfGreatGrandParent = new QState(greatGrandParent); + initialStateOfGreatGrandParent->setObjectName("initialStateOfGreatGrandParent"); + greatGrandParent->setInitialState(initialStateOfGreatGrandParent); + + initialStateOfGreatGrandParent->addTransition(new EventTransition(QEvent::User, state)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), true); + QCOMPARE(entryController->property("grandParentEntered").toBool(), false); + QCOMPARE(entryController->property("parentEntered").toBool(), false); + QCOMPARE(entryController->property("stateEntered").toBool(), false); + QCOMPARE(machine.configuration().count(), 2); + QVERIFY(machine.configuration().contains(greatGrandParent)); + QVERIFY(machine.configuration().contains(initialStateOfGreatGrandParent)); + + entryController->setProperty("greatGrandParentEntered", false); + entryController->setProperty("grandParentEntered", false); + entryController->setProperty("parentEntered", false); + entryController->setProperty("stateEntered", false); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), false); + QCOMPARE(entryController->property("grandParentEntered").toBool(), true); + QCOMPARE(entryController->property("parentEntered").toBool(), true); + QCOMPARE(entryController->property("stateEntered").toBool(), true); + QCOMPARE(machine.configuration().count(), 4); + QVERIFY(machine.configuration().contains(greatGrandParent)); + QVERIFY(machine.configuration().contains(grandParent)); + QVERIFY(machine.configuration().contains(parent)); + QVERIFY(machine.configuration().contains(state)); +} + +void tst_QStateMachine::defaultErrorState() +{ + QStateMachine machine; + QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); + + QState *brokenState = new QState(); + brokenState->setObjectName("MyInitialState"); + + machine.addState(brokenState); + machine.setInitialState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'MyInitialState'"); + + // initialState has no initial state + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); + QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'MyInitialState'")); + QCOMPARE(machine.isRunning(), false); +} + +class CustomErrorState: public QState +{ +public: + CustomErrorState(QStateMachine *machine, QState *parent = 0) + : QState(parent), error(QStateMachine::NoError), m_machine(machine) + { + } + + void onEntry(QEvent *) + { + error = m_machine->error(); + errorString = m_machine->errorString(); + } + + QStateMachine::Error error; + QString errorString; + +private: + QStateMachine *m_machine; +}; + +void tst_QStateMachine::customGlobalErrorState() +{ + QStateMachine machine; + + CustomErrorState *customErrorState = new CustomErrorState(&machine); + customErrorState->setObjectName("customErrorState"); + machine.addState(customErrorState); + machine.setErrorState(customErrorState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + machine.addState(brokenState); + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.errorState(), static_cast<QAbstractState*>(customErrorState)); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(initialState)); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(initialState)); + + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(customErrorState)); + QCOMPARE(customErrorState->error, QStateMachine::NoInitialStateError); + QCOMPARE(customErrorState->errorString, QString::fromLatin1("Missing initial state in compound state 'brokenState'")); + QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); + QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'brokenState'")); +} + +void tst_QStateMachine::customLocalErrorStateInBrokenState() +{ + QStateMachine machine; + CustomErrorState *customErrorState = new CustomErrorState(&machine); + machine.addState(customErrorState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + machine.addState(brokenState); + brokenState->setErrorState(customErrorState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(customErrorState)); + QCOMPARE(customErrorState->error, QStateMachine::NoInitialStateError); +} + +void tst_QStateMachine::customLocalErrorStateInOtherState() +{ + QStateMachine machine; + CustomErrorState *customErrorState = new CustomErrorState(&machine); + machine.addState(customErrorState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + QTest::ignoreMessage(QtWarningMsg, "QState::setErrorState: error state cannot belong to a different state machine"); + initialState->setErrorState(customErrorState); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + + machine.addState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'brokenState'"); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); +} + +void tst_QStateMachine::customLocalErrorStateInParentOfBrokenState() +{ + QStateMachine machine; + CustomErrorState *customErrorState = new CustomErrorState(&machine); + machine.addState(customErrorState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *parentOfBrokenState = new QState(); + machine.addState(parentOfBrokenState); + parentOfBrokenState->setObjectName("parentOfBrokenState"); + parentOfBrokenState->setErrorState(customErrorState); + + QState *brokenState = new QState(parentOfBrokenState); + brokenState->setObjectName("brokenState"); + parentOfBrokenState->setInitialState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(customErrorState)); +} + +void tst_QStateMachine::customLocalErrorStateOverridesParent() +{ + QStateMachine machine; + CustomErrorState *customErrorStateForParent = new CustomErrorState(&machine); + machine.addState(customErrorStateForParent); + + CustomErrorState *customErrorStateForBrokenState = new CustomErrorState(&machine); + machine.addState(customErrorStateForBrokenState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *parentOfBrokenState = new QState(); + machine.addState(parentOfBrokenState); + parentOfBrokenState->setObjectName("parentOfBrokenState"); + parentOfBrokenState->setErrorState(customErrorStateForParent); + + QState *brokenState = new QState(parentOfBrokenState); + brokenState->setObjectName("brokenState"); + brokenState->setErrorState(customErrorStateForBrokenState); + parentOfBrokenState->setInitialState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(customErrorStateForBrokenState)); + QCOMPARE(customErrorStateForBrokenState->error, QStateMachine::NoInitialStateError); + QCOMPARE(customErrorStateForParent->error, QStateMachine::NoError); +} + +void tst_QStateMachine::errorStateHasChildren() +{ + QStateMachine machine; + CustomErrorState *customErrorState = new CustomErrorState(&machine); + customErrorState->setObjectName("customErrorState"); + machine.addState(customErrorState); + + machine.setErrorState(customErrorState); + + QState *childOfErrorState = new QState(customErrorState); + childOfErrorState->setObjectName("childOfErrorState"); + customErrorState->setInitialState(childOfErrorState); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + machine.addState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.configuration().count(), 2); + QVERIFY(machine.configuration().contains(customErrorState)); + QVERIFY(machine.configuration().contains(childOfErrorState)); +} + + +void tst_QStateMachine::errorStateHasErrors() +{ + QStateMachine machine; + CustomErrorState *customErrorState = new CustomErrorState(&machine); + customErrorState->setObjectName("customErrorState"); + machine.addState(customErrorState); + + machine.setErrorState(customErrorState); + + QState *childOfErrorState = new QState(customErrorState); + childOfErrorState->setObjectName("childOfErrorState"); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + machine.addState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'customErrorState'"); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); + QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); + QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'customErrorState'")); +} + +void tst_QStateMachine::errorStateIsRootState() +{ + QStateMachine machine; + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::setErrorState: root state cannot be error state"); + machine.setErrorState(machine.rootState()); + + QState *initialState = new QState(); + initialState->setObjectName("initialState"); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + brokenState->setObjectName("brokenState"); + machine.addState(brokenState); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialState->addTransition(new EventTransition(QEvent::Type(QEvent::User + 1), brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::Type(QEvent::User + 1))); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'brokenState'"); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); +} + +void tst_QStateMachine::errorStateEntersParentFirst() +{ + QStateMachine machine; + + QObject *entryController = new QObject(&machine); + entryController->setObjectName("entryController"); + entryController->setProperty("greatGrandParentEntered", false); + entryController->setProperty("grandParentEntered", false); + entryController->setProperty("parentEntered", false); + entryController->setProperty("errorStateEntered", false); + + QState *greatGrandParent = new QState(); + greatGrandParent->setObjectName("greatGrandParent"); + greatGrandParent->assignProperty(entryController, "greatGrandParentEntered", true); + machine.addState(greatGrandParent); + machine.setInitialState(greatGrandParent); + + QState *grandParent = new QState(greatGrandParent); + grandParent->setObjectName("grandParent"); + grandParent->assignProperty(entryController, "grandParentEntered", true); + + QState *parent = new QState(grandParent); + parent->setObjectName("parent"); + parent->assignProperty(entryController, "parentEntered", true); + + QState *errorState = new QState(parent); + errorState->setObjectName("errorState"); + errorState->assignProperty(entryController, "errorStateEntered", true); + machine.setErrorState(errorState); + + QState *initialStateOfGreatGrandParent = new QState(greatGrandParent); + initialStateOfGreatGrandParent->setObjectName("initialStateOfGreatGrandParent"); + greatGrandParent->setInitialState(initialStateOfGreatGrandParent); + + QState *brokenState = new QState(greatGrandParent); + brokenState->setObjectName("brokenState"); + + QState *childState = new QState(brokenState); + childState->setObjectName("childState"); + + initialStateOfGreatGrandParent->addTransition(new EventTransition(QEvent::User, brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), true); + QCOMPARE(entryController->property("grandParentEntered").toBool(), false); + QCOMPARE(entryController->property("parentEntered").toBool(), false); + QCOMPARE(entryController->property("errorStateEntered").toBool(), false); + QCOMPARE(machine.configuration().count(), 2); + QVERIFY(machine.configuration().contains(greatGrandParent)); + QVERIFY(machine.configuration().contains(initialStateOfGreatGrandParent)); + + entryController->setProperty("greatGrandParentEntered", false); + entryController->setProperty("grandParentEntered", false); + entryController->setProperty("parentEntered", false); + entryController->setProperty("errorStateEntered", false); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(entryController->property("greatGrandParentEntered").toBool(), false); + QCOMPARE(entryController->property("grandParentEntered").toBool(), true); + QCOMPARE(entryController->property("parentEntered").toBool(), true); + QCOMPARE(entryController->property("errorStateEntered").toBool(), true); + QCOMPARE(machine.configuration().count(), 4); + QVERIFY(machine.configuration().contains(greatGrandParent)); + QVERIFY(machine.configuration().contains(grandParent)); + QVERIFY(machine.configuration().contains(parent)); + QVERIFY(machine.configuration().contains(errorState)); +} + +void tst_QStateMachine::customErrorStateIsNull() +{ + QStateMachine machine; + machine.rootState()->setErrorState(0); + + QState *initialState = new QState(); + machine.addState(initialState); + machine.setInitialState(initialState); + + QState *brokenState = new QState(); + machine.addState(brokenState); + + new QState(brokenState); + initialState->addTransition(new EventTransition(QEvent::User, brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state ''"); + QCoreApplication::processEvents(); + + QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); + QCOMPARE(machine.isRunning(), false); +} + +void tst_QStateMachine::clearError() +{ + QStateMachine machine; + machine.setErrorState(new QState(machine.rootState())); // avoid warnings + + QState *brokenState = new QState(machine.rootState()); + brokenState->setObjectName("brokenState"); + machine.setInitialState(brokenState); + new QState(brokenState); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.error(), QStateMachine::NoInitialStateError); + QCOMPARE(machine.errorString(), QString::fromLatin1("Missing initial state in compound state 'brokenState'")); + + machine.clearError(); + + QCOMPARE(machine.error(), QStateMachine::NoError); + QVERIFY(machine.errorString().isEmpty()); +} + +void tst_QStateMachine::historyStateAsInitialState() +{ + QStateMachine machine; + + QHistoryState *hs = new QHistoryState(machine.rootState()); + machine.setInitialState(hs); + + QState *s1 = new QState(machine.rootState()); + hs->setDefaultState(s1); + + QState *s2 = new QState(machine.rootState()); + + QHistoryState *s2h = new QHistoryState(s2); + s2->setInitialState(s2h); + + QState *s21 = new QState(s2); + s2h->setDefaultState(s21); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s21)); +} + +void tst_QStateMachine::historyStateHasNowhereToGo() +{ + QStateMachine machine; + + QState *initialState = new QState(machine.rootState()); + machine.setInitialState(initialState); + machine.setErrorState(new QState(machine.rootState())); // avoid warnings + + QState *brokenState = new QState(machine.rootState()); + brokenState->setObjectName("brokenState"); + brokenState->setInitialState(new QState(brokenState)); + + QHistoryState *historyState = new QHistoryState(brokenState); + historyState->setObjectName("historyState"); + initialState->addTransition(new EventTransition(QEvent::User, historyState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), true); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(machine.errorState())); + QCOMPARE(machine.error(), QStateMachine::NoDefaultStateInHistoryStateError); + QCOMPARE(machine.errorString(), QString::fromLatin1("Missing default state in history state 'historyState'")); +} + +void tst_QStateMachine::brokenStateIsNeverEntered() +{ + QStateMachine machine; + + QObject *entryController = new QObject(&machine); + entryController->setProperty("brokenStateEntered", false); + entryController->setProperty("childStateEntered", false); + entryController->setProperty("errorStateEntered", false); + + QState *initialState = new QState(machine.rootState()); + machine.setInitialState(initialState); + + QState *errorState = new QState(machine.rootState()); + errorState->assignProperty(entryController, "errorStateEntered", true); + machine.setErrorState(errorState); + + QState *brokenState = new QState(machine.rootState()); + brokenState->assignProperty(entryController, "brokenStateEntered", true); + brokenState->setObjectName("brokenState"); + + QState *childState = new QState(brokenState); + childState->assignProperty(entryController, "childStateEntered", true); + + initialState->addTransition(new EventTransition(QEvent::User, brokenState)); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(entryController->property("errorStateEntered").toBool(), true); + QCOMPARE(entryController->property("brokenStateEntered").toBool(), false); + QCOMPARE(entryController->property("childStateEntered").toBool(), false); +} + +void tst_QStateMachine::transitionToStateNotInGraph() +{ + QStateMachine machine; + + QState *initialState = new QState(machine.rootState()); + initialState->setObjectName("initialState"); + machine.setInitialState(initialState); + + QState independentState; + independentState.setObjectName("independentState"); + initialState->addTransition(&independentState); + + machine.start(); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 'initialState'"); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); +} + +void tst_QStateMachine::customErrorStateNotInGraph() +{ + QStateMachine machine; + + QState errorState; + errorState.setObjectName("errorState"); + QTest::ignoreMessage(QtWarningMsg, "QState::setErrorState: error state cannot belong to a different state machine"); + machine.setErrorState(&errorState); + QCOMPARE(machine.errorState(), reinterpret_cast<QAbstractState *>(0)); + + QState *initialBrokenState = new QState(machine.rootState()); + initialBrokenState->setObjectName("initialBrokenState"); + machine.setInitialState(initialBrokenState); + new QState(initialBrokenState); + + machine.start(); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: Missing initial state in compound state 'initialBrokenState'"); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); +} + +void tst_QStateMachine::restoreProperties() +{ + QStateMachine machine; + QCOMPARE(machine.globalRestorePolicy(), QStateMachine::DoNotRestoreProperties); + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QObject *object = new QObject(&machine); + object->setProperty("a", 1); + object->setProperty("b", 2); + + QState *S1 = new QState(); + S1->setObjectName("S1"); + S1->assignProperty(object, "a", 3); + machine.addState(S1); + + QState *S2 = new QState(); + S2->setObjectName("S2"); + S2->assignProperty(object, "b", 5); + machine.addState(S2); + + QState *S3 = new QState(); + S3->setObjectName("S3"); + machine.addState(S3); + + QFinalState *S4 = new QFinalState(); + machine.addState(S4); + + S1->addTransition(new EventTransition(QEvent::User, S2)); + S2->addTransition(new EventTransition(QEvent::User, S3)); + S3->addTransition(S4); + + machine.setInitialState(S1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 3); + QCOMPARE(object->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 1); + QCOMPARE(object->property("b").toInt(), 5); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 1); + QCOMPARE(object->property("b").toInt(), 2); +} + +void tst_QStateMachine::rootState() +{ + QStateMachine machine; + QVERIFY(machine.rootState() != 0); + QVERIFY(qobject_cast<QState*>(machine.rootState()) != 0); + QCOMPARE(qobject_cast<QState*>(machine.rootState())->parentState(), (QState*)0); + QCOMPARE(machine.rootState()->parent(), (QObject*)&machine); + QCOMPARE(machine.rootState()->machine(), &machine); + + QState *s1 = new QState(machine.rootState()); + QCOMPARE(s1->parentState(), machine.rootState()); + + QState *s2 = new QState(); + s2->setParent(&machine); + QCOMPARE(s2->parentState(), machine.rootState()); +} + +void tst_QStateMachine::addAndRemoveState() +{ + QStateMachine machine; + QStatePrivate *root_d = QStatePrivate::get(machine.rootState()); + QCOMPARE(root_d->childStates().size(), 0); + + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: cannot add null state"); + machine.addState(0); + + QState *s1 = new QState(); + QCOMPARE(s1->parentState(), (QState*)0); + QCOMPARE(s1->machine(), (QStateMachine*)0); + machine.addState(s1); + QCOMPARE(s1->machine(), &machine); + QCOMPARE(s1->parentState(), machine.rootState()); + QCOMPARE(root_d->childStates().size(), 1); + QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); + + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: state has already been added to this machine"); + machine.addState(s1); + + QState *s2 = new QState(); + QCOMPARE(s2->parentState(), (QState*)0); + machine.addState(s2); + QCOMPARE(s2->parentState(), machine.rootState()); + QCOMPARE(root_d->childStates().size(), 2); + QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s1); + QCOMPARE(root_d->childStates().at(1), (QAbstractState*)s2); + + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::addState: state has already been added to this machine"); + machine.addState(s2); + + machine.removeState(s1); + QCOMPARE(s1->parentState(), (QState*)0); + QCOMPARE(root_d->childStates().size(), 1); + QCOMPARE(root_d->childStates().at(0), (QAbstractState*)s2); + + machine.removeState(s2); + QCOMPARE(s2->parentState(), (QState*)0); + QCOMPARE(root_d->childStates().size(), 0); + + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::removeState: cannot remove null state"); + machine.removeState(0); + + { + QStateMachine machine2; + { + QString warning; + warning.sprintf("QStateMachine::removeState: state %p's machine (%p) is different from this machine (%p)", + machine2.rootState(), &machine2, &machine); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); + machine.removeState(machine2.rootState()); + } + // ### check this behavior + machine.addState(machine2.rootState()); + QCOMPARE(machine2.rootState()->parent(), (QObject*)machine.rootState()); + } + + delete s1; + delete s2; + // ### how to deal with this? + // machine.removeState(machine.errorState()); +} + +void tst_QStateMachine::stateEntryAndExit() +{ + // Two top-level states + { + QStateMachine machine; + + TestState *s1 = new TestState(machine.rootState()); + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); + s1->addTransition((QAbstractState*)0); + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add null transition"); + s1->addTransition((QAbstractTransition*)0); + QTest::ignoreMessage(QtWarningMsg, "QState::removeTransition: cannot remove null transition"); + s1->removeTransition((QAbstractTransition*)0); + + TestState *s2 = new TestState(machine.rootState()); + QFinalState *s3 = new QFinalState(machine.rootState()); + + TestTransition *t = new TestTransition(s2); + QCOMPARE(t->machine(), (QStateMachine*)0); + QCOMPARE(t->sourceState(), (QState*)0); + QCOMPARE(t->targetState(), (QAbstractState*)s2); + QCOMPARE(t->targetStates().size(), 1); + QCOMPARE(t->targetStates().at(0), (QAbstractState*)s2); + t->setTargetState(0); + QCOMPARE(t->targetState(), (QAbstractState*)0); + QVERIFY(t->targetStates().isEmpty()); + t->setTargetState(s2); + QCOMPARE(t->targetState(), (QAbstractState*)s2); + QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::setTargetStates: target state(s) cannot be null"); + t->setTargetStates(QList<QAbstractState*>() << 0); + QCOMPARE(t->targetState(), (QAbstractState*)s2); + t->setTargetStates(QList<QAbstractState*>() << s2); + QCOMPARE(t->targetState(), (QAbstractState*)s2); + QCOMPARE(t->targetStates().size(), 1); + QCOMPARE(t->targetStates().at(0), (QAbstractState*)s2); + QCOMPARE(s1->addTransition(t), (QAbstractTransition*)t); + QCOMPARE(t->sourceState(), (QState*)s1); + QCOMPARE(t->machine(), &machine); + + { + QAbstractTransition *trans = s2->addTransition(s3); + QVERIFY(trans != 0); + QCOMPARE(trans->sourceState(), (QState*)s2); + QCOMPARE(trans->targetState(), (QAbstractState*)s3); + { + QString warning; + warning.sprintf("QState::removeTransition: transition %p's source state (%p) is different from this state (%p)", trans, s2, s1); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); + s1->removeTransition(trans); + } + s2->removeTransition(trans); + QCOMPARE(trans->sourceState(), (QState*)0); + QCOMPARE(trans->targetState(), (QAbstractState*)s3); + QCOMPARE(s2->addTransition(trans), trans); + QCOMPARE(trans->sourceState(), (QState*)s2); + } + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s1); + QCOMPARE(machine.initialState(), (QAbstractState*)s1); + { + QString warning; + warning.sprintf("QState::setInitialState: state %p is not a child of this state (%p)", machine.rootState(), machine.rootState()); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); + machine.setInitialState(machine.rootState()); + QCOMPARE(machine.initialState(), (QAbstractState*)s1); + } + QVERIFY(machine.configuration().isEmpty()); + globalTick = 0; + QVERIFY(!machine.isRunning()); + QSignalSpy s1EnteredSpy(s1, SIGNAL(entered())); + QSignalSpy s1ExitedSpy(s1, SIGNAL(exited())); + QSignalSpy tTriggeredSpy(t, SIGNAL(triggered())); + QSignalSpy s2EnteredSpy(s2, SIGNAL(entered())); + QSignalSpy s2ExitedSpy(s2, SIGNAL(exited())); + machine.start(); + + QTRY_COMPARE(startedSpy.count(), 1); + QTRY_COMPARE(finishedSpy.count(), 1); + QTRY_COMPARE(stoppedSpy.count(), 0); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(s3)); + + // s1 is entered + QCOMPARE(s1->events.count(), 2); + QCOMPARE(s1->events.at(0).first, 0); + QCOMPARE(s1->events.at(0).second, TestState::Entry); + // s1 is exited + QCOMPARE(s1->events.at(1).first, 1); + QCOMPARE(s1->events.at(1).second, TestState::Exit); + // t is triggered + QCOMPARE(t->triggers.count(), 1); + QCOMPARE(t->triggers.at(0), 2); + // s2 is entered + QCOMPARE(s2->events.count(), 2); + QCOMPARE(s2->events.at(0).first, 3); + QCOMPARE(s2->events.at(0).second, TestState::Entry); + // s2 is exited + QCOMPARE(s2->events.at(1).first, 4); + QCOMPARE(s2->events.at(1).second, TestState::Exit); + + QCOMPARE(s1EnteredSpy.count(), 1); + QCOMPARE(s1ExitedSpy.count(), 1); + QCOMPARE(tTriggeredSpy.count(), 1); + QCOMPARE(s2EnteredSpy.count(), 1); + QCOMPARE(s2ExitedSpy.count(), 1); + } + // Two top-level states, one has two child states + { + QStateMachine machine; + + TestState *s1 = new TestState(machine.rootState()); + TestState *s11 = new TestState(s1); + TestState *s12 = new TestState(s1); + TestState *s2 = new TestState(machine.rootState()); + QFinalState *s3 = new QFinalState(machine.rootState()); + s1->setInitialState(s11); + TestTransition *t1 = new TestTransition(s12); + s11->addTransition(t1); + TestTransition *t2 = new TestTransition(s2); + s12->addTransition(t2); + s2->addTransition(s3); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s1); + globalTick = 0; + machine.start(); + + QTRY_COMPARE(startedSpy.count(), 1); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(s3)); + + // s1 is entered + QCOMPARE(s1->events.count(), 2); + QCOMPARE(s1->events.at(0).first, 0); + QCOMPARE(s1->events.at(0).second, TestState::Entry); + // s11 is entered + QCOMPARE(s11->events.count(), 2); + QCOMPARE(s11->events.at(0).first, 1); + QCOMPARE(s11->events.at(0).second, TestState::Entry); + // s11 is exited + QCOMPARE(s11->events.at(1).first, 2); + QCOMPARE(s11->events.at(1).second, TestState::Exit); + // t1 is triggered + QCOMPARE(t1->triggers.count(), 1); + QCOMPARE(t1->triggers.at(0), 3); + // s12 is entered + QCOMPARE(s12->events.count(), 2); + QCOMPARE(s12->events.at(0).first, 4); + QCOMPARE(s12->events.at(0).second, TestState::Entry); + // s12 is exited + QCOMPARE(s12->events.at(1).first, 5); + QCOMPARE(s12->events.at(1).second, TestState::Exit); + // s1 is exited + QCOMPARE(s1->events.at(1).first, 6); + QCOMPARE(s1->events.at(1).second, TestState::Exit); + // t2 is triggered + QCOMPARE(t2->triggers.count(), 1); + QCOMPARE(t2->triggers.at(0), 7); + // s2 is entered + QCOMPARE(s2->events.count(), 2); + QCOMPARE(s2->events.at(0).first, 8); + QCOMPARE(s2->events.at(0).second, TestState::Entry); + // s2 is exited + QCOMPARE(s2->events.at(1).first, 9); + QCOMPARE(s2->events.at(1).second, TestState::Exit); + } +} + +void tst_QStateMachine::assignProperty() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + + QTest::ignoreMessage(QtWarningMsg, "QState::assignProperty: cannot assign property 'foo' of null object"); + s1->assignProperty(0, "foo", QVariant()); + + s1->assignProperty(s1, "objectName", "s1"); + QFinalState *s2 = new QFinalState(machine.rootState()); + s1->addTransition(s2); + machine.setInitialState(s1); + machine.start(); + QTRY_COMPARE(s1->objectName(), QString::fromLatin1("s1")); + + s1->assignProperty(s1, "objectName", "foo"); + machine.start(); + QTRY_COMPARE(s1->objectName(), QString::fromLatin1("foo")); + + s1->assignProperty(s1, "noSuchProperty", 123); + machine.start(); + QTRY_COMPARE(s1->dynamicPropertyNames().size(), 1); + QCOMPARE(s1->dynamicPropertyNames().at(0), QByteArray("noSuchProperty")); + QCOMPARE(s1->objectName(), QString::fromLatin1("foo")); + + { + QSignalSpy polishedSpy(s1, SIGNAL(polished())); + machine.start(); + QTRY_COMPARE(polishedSpy.count(), 1); + } + + // nested states + { + QState *s11 = new QState(s1); + QString str = QString::fromLatin1("set by nested state"); + s11->assignProperty(s11, "objectName", str); + s1->setInitialState(s11); + machine.start(); + QTRY_COMPARE(s11->objectName(), str); + } +} + +void tst_QStateMachine::assignPropertyWithAnimation() +{ + // Single animation + { + QStateMachine machine; + QVERIFY(machine.animationsEnabled()); + machine.setAnimationsEnabled(false); + QVERIFY(!machine.animationsEnabled()); + machine.setAnimationsEnabled(true); + QVERIFY(machine.animationsEnabled()); + QObject obj; + obj.setProperty("foo", 321); + obj.setProperty("bar", 654); + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(&obj, "foo", 123); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(&obj, "foo", 456); + s2->assignProperty(&obj, "bar", 789); + QAbstractTransition *trans = s1->addTransition(s2); + QVERIFY(trans->animations().isEmpty()); + QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::addAnimation: cannot add null animation"); + trans->addAnimation(0); + QPropertyAnimation anim(&obj, "foo"); + anim.setDuration(250); + trans->addAnimation(&anim); + QCOMPARE(trans->animations().size(), 1); + QCOMPARE(trans->animations().at(0), (QAbstractAnimation*)&anim); + QCOMPARE(anim.parent(), (QObject*)0); + QTest::ignoreMessage(QtWarningMsg, "QAbstractTransition::removeAnimation: cannot remove null animation"); + trans->removeAnimation(0); + trans->removeAnimation(&anim); + QVERIFY(trans->animations().isEmpty()); + trans->addAnimation(&anim); + QCOMPARE(trans->animations().size(), 1); + QCOMPARE(trans->animations().at(0), (QAbstractAnimation*)&anim); + QFinalState *s3 = new QFinalState(machine.rootState()); + s2->addTransition(s2, SIGNAL(polished()), s3); + + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(obj.property("foo").toInt(), 456); + QCOMPARE(obj.property("bar").toInt(), 789); + } + // Two animations + { + QStateMachine machine; + QObject obj; + obj.setProperty("foo", 321); + obj.setProperty("bar", 654); + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(&obj, "foo", 123); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(&obj, "foo", 456); + s2->assignProperty(&obj, "bar", 789); + QAbstractTransition *trans = s1->addTransition(s2); + QPropertyAnimation anim(&obj, "foo"); + anim.setDuration(150); + trans->addAnimation(&anim); + QPropertyAnimation anim2(&obj, "bar"); + anim2.setDuration(150); + trans->addAnimation(&anim2); + QFinalState *s3 = new QFinalState(machine.rootState()); + s2->addTransition(s2, SIGNAL(polished()), s3); + + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(obj.property("foo").toInt(), 456); + QCOMPARE(obj.property("bar").toInt(), 789); + } + // Animation group + { + QStateMachine machine; + QObject obj; + obj.setProperty("foo", 321); + obj.setProperty("bar", 654); + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(&obj, "foo", 123); + s1->assignProperty(&obj, "bar", 321); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(&obj, "foo", 456); + s2->assignProperty(&obj, "bar", 654); + s2->assignProperty(&obj, "baz", 789); + QAbstractTransition *trans = s1->addTransition(s2); + QSequentialAnimationGroup group; + group.addAnimation(new QPropertyAnimation(&obj, "foo")); + group.addAnimation(new QPropertyAnimation(&obj, "bar")); + trans->addAnimation(&group); + QFinalState *s3 = new QFinalState(machine.rootState()); + s2->addTransition(s2, SIGNAL(polished()), s3); + + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(obj.property("foo").toInt(), 456); + QCOMPARE(obj.property("bar").toInt(), 654); + QCOMPARE(obj.property("baz").toInt(), 789); + } + // Nested states + { + QStateMachine machine; + QObject obj; + obj.setProperty("foo", 321); + obj.setProperty("bar", 654); + QState *s1 = new QState(machine.rootState()); + QCOMPARE(s1->childMode(), QState::ExclusiveStates); + s1->setChildMode(QState::ParallelStates); + QCOMPARE(s1->childMode(), QState::ParallelStates); + s1->setChildMode(QState::ExclusiveStates); + QCOMPARE(s1->childMode(), QState::ExclusiveStates); + QCOMPARE(s1->initialState(), (QAbstractState*)0); + s1->setObjectName("s1"); + s1->assignProperty(&obj, "foo", 123); + s1->assignProperty(&obj, "bar", 456); + QState *s2 = new QState(machine.rootState()); + s2->setObjectName("s2"); + s2->assignProperty(&obj, "foo", 321); + QState *s21 = new QState(s2); + s21->setObjectName("s21"); + s21->assignProperty(&obj, "bar", 654); + QState *s22 = new QState(s2); + s22->setObjectName("s22"); + s22->assignProperty(&obj, "bar", 789); + s2->setInitialState(s21); + QCOMPARE(s2->initialState(), (QAbstractState*)s21); + + QAbstractTransition *trans = s1->addTransition(s2); + QPropertyAnimation anim(&obj, "foo"); + anim.setDuration(500); + trans->addAnimation(&anim); + QPropertyAnimation anim2(&obj, "bar"); + anim2.setDuration(250); + trans->addAnimation(&anim2); + + s21->addTransition(s21, SIGNAL(polished()), s22); + + QFinalState *s3 = new QFinalState(machine.rootState()); + s22->addTransition(s2, SIGNAL(polished()), s3); + + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(obj.property("foo").toInt(), 321); + QCOMPARE(obj.property("bar").toInt(), 789); + } + // Aborted animation + { + QStateMachine machine; + SignalEmitter emitter; + QObject obj; + obj.setProperty("foo", 321); + obj.setProperty("bar", 654); + QState *group = new QState(machine.rootState()); + QState *s1 = new QState(group); + group->setInitialState(s1); + s1->assignProperty(&obj, "foo", 123); + QState *s2 = new QState(group); + s2->assignProperty(&obj, "foo", 456); + s2->assignProperty(&obj, "bar", 789); + QAbstractTransition *trans = s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s2); + QPropertyAnimation anim(&obj, "foo"); + anim.setDuration(8000); + trans->addAnimation(&anim); + QPropertyAnimation anim2(&obj, "bar"); + anim2.setDuration(8000); + trans->addAnimation(&anim2); + QState *s3 = new QState(group); + s3->assignProperty(&obj, "foo", 911); + s2->addTransition(&emitter, SIGNAL(signalWithNoArg()), s3); + + machine.setInitialState(group); + machine.start(); + QTRY_COMPARE(machine.configuration().contains(s1), true); + QSignalSpy polishedSpy(s2, SIGNAL(polished())); + emitter.emitSignalWithNoArg(); + QTRY_COMPARE(machine.configuration().contains(s2), true); + QVERIFY(polishedSpy.isEmpty()); + emitter.emitSignalWithNoArg(); // will cause animations from s1-->s2 to abort + QTRY_COMPARE(machine.configuration().contains(s3), true); + QVERIFY(polishedSpy.isEmpty()); + QCOMPARE(obj.property("foo").toInt(), 911); + QCOMPARE(obj.property("bar").toInt(), 789); + } +} + +struct StringEvent : public QEvent +{ +public: + StringEvent(const QString &val) + : QEvent(QEvent::Type(QEvent::User+2)), + value(val) {} + + QString value; +}; + +class StringTransition : public QAbstractTransition +{ +public: + StringTransition(const QString &value, QAbstractState *target) + : QAbstractTransition(QList<QAbstractState*>() << target), m_value(value) {} + +protected: + virtual bool eventTest(QEvent *e) + { + if (e->type() != QEvent::Type(QEvent::User+2)) + return false; + StringEvent *se = static_cast<StringEvent*>(e); + return (m_value == se->value) && (!m_cond.isValid() || (m_cond.indexIn(m_value) != -1)); + } + virtual void onTransition(QEvent *) {} + +private: + QString m_value; + QRegExp m_cond; +}; + +class StringEventPoster : public QState +{ +public: + StringEventPoster(QStateMachine *machine, const QString &value, QState *parent = 0) + : QState(parent), m_machine(machine), m_value(value), m_delay(0) {} + + void setString(const QString &value) + { m_value = value; } + void setDelay(int delay) + { m_delay = delay; } + +protected: + virtual void onEntry(QEvent *) + { + m_machine->postEvent(new StringEvent(m_value), m_delay); + } + virtual void onExit(QEvent *) {} + +private: + QStateMachine *m_machine; + QString m_value; + int m_delay; +}; + +void tst_QStateMachine::postEvent() +{ + for (int x = 0; x < 2; ++x) { + QStateMachine machine; + { + QEvent e(QEvent::None); + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::postEvent: cannot post event when the state machine is not running"); + machine.postEvent(&e); + } + StringEventPoster *s1 = new StringEventPoster(&machine, "a"); + if (x == 1) + s1->setDelay(100); + QFinalState *s2 = new QFinalState; + s1->addTransition(new StringTransition("a", s2)); + machine.addState(s1); + machine.addState(s2); + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); + + s1->setString("b"); + QFinalState *s3 = new QFinalState(); + machine.addState(s3); + s1->addTransition(new StringTransition("b", s3)); + finishedSpy.clear(); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s3)); + } +} + +void tst_QStateMachine::stateFinished() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + QState *s1_1 = new QState(s1); + QFinalState *s1_2 = new QFinalState(s1); + s1_1->addTransition(s1_2); + s1->setInitialState(s1_1); + QFinalState *s2 = new QFinalState(machine.rootState()); + s1->addTransition(s1, SIGNAL(finished()), s2); + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); +} + +void tst_QStateMachine::parallelStates() +{ + QStateMachine machine; + + QState *s1 = new QState(QState::ParallelStates); + QCOMPARE(s1->childMode(), QState::ParallelStates); + QState *s1_1 = new QState(s1); + QState *s1_1_1 = new QState(s1_1); + QFinalState *s1_1_f = new QFinalState(s1_1); + s1_1_1->addTransition(s1_1_f); + s1_1->setInitialState(s1_1_1); + QState *s1_2 = new QState(s1); + QState *s1_2_1 = new QState(s1_2); + QFinalState *s1_2_f = new QFinalState(s1_2); + s1_2_1->addTransition(s1_2_f); + s1_2->setInitialState(s1_2_1); + { + QString warning; + warning.sprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", s1); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); + s1->setInitialState(0); + } + machine.addState(s1); + + QFinalState *s2 = new QFinalState(); + machine.addState(s2); + + s1->addTransition(s1, SIGNAL(finished()), s2); + + machine.setInitialState(s1); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); +} + +void tst_QStateMachine::parallelRootState() +{ + QStateMachine machine; + QState *root = machine.rootState(); + QCOMPARE(root->childMode(), QState::ExclusiveStates); + root->setChildMode(QState::ParallelStates); + QCOMPARE(root->childMode(), QState::ParallelStates); + + QState *s1 = new QState(root); + QFinalState *s1_f = new QFinalState(s1); + s1->setInitialState(s1_f); + QState *s2 = new QState(root); + QFinalState *s2_f = new QFinalState(s2); + s2->setInitialState(s2_f); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start."); + machine.start(); + QCoreApplication::processEvents(); + QEXPECT_FAIL("", "parallel root state is not supported (task 256587)", Continue); + QCOMPARE(startedSpy.count(), 1); +} + +void tst_QStateMachine::allSourceToTargetConfigurations() +{ + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + s0->setObjectName("s0"); + QState *s1 = new QState(s0); + s1->setObjectName("s1"); + QState *s11 = new QState(s1); + s11->setObjectName("s11"); + QState *s2 = new QState(s0); + s2->setObjectName("s2"); + QState *s21 = new QState(s2); + s21->setObjectName("s21"); + QState *s211 = new QState(s21); + s211->setObjectName("s211"); + QFinalState *f = new QFinalState(machine.rootState()); + f->setObjectName("f"); + + s0->setInitialState(s1); + s1->setInitialState(s11); + s2->setInitialState(s21); + s21->setInitialState(s211); + + s11->addTransition(new StringTransition("g", s211)); + s1->addTransition(new StringTransition("a", s1)); + s1->addTransition(new StringTransition("b", s11)); + s1->addTransition(new StringTransition("c", s2)); + s1->addTransition(new StringTransition("d", s0)); + s1->addTransition(new StringTransition("f", s211)); + s211->addTransition(new StringTransition("d", s21)); + s211->addTransition(new StringTransition("g", s0)); + s211->addTransition(new StringTransition("h", f)); + s21->addTransition(new StringTransition("b", s211)); + s2->addTransition(new StringTransition("c", s1)); + s2->addTransition(new StringTransition("f", s11)); + s0->addTransition(new StringTransition("e", s211)); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new StringEvent("a")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("b")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("c")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("d")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("e")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("f")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("g")); + QCoreApplication::processEvents(); + machine.postEvent(new StringEvent("h")); + QCoreApplication::processEvents(); + + QTRY_COMPARE(finishedSpy.count(), 1); +} + +class TestSignalTransition : public QSignalTransition +{ +public: + TestSignalTransition(QState *sourceState = 0) + : QSignalTransition(sourceState) {} + TestSignalTransition(QObject *sender, const char *signal, + QAbstractState *target) + : QSignalTransition(sender, signal, QList<QAbstractState*>() << target) {} + QVariantList argumentsReceived() const { + return m_args; + } +protected: + bool eventTest(QEvent *e) { + if (!QSignalTransition::eventTest(e)) + return false; + QSignalEvent *se = static_cast<QSignalEvent*>(e); + const_cast<TestSignalTransition*>(this)->m_args = se->arguments(); + return true; + } +private: + QVariantList m_args; +}; + +void tst_QStateMachine::signalTransitions() +{ + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: sender cannot be null"); + QCOMPARE(s0->addTransition(0, SIGNAL(noSuchSignal()), 0), (QSignalTransition*)0); + + SignalEmitter emitter; + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: signal cannot be null"); + QCOMPARE(s0->addTransition(&emitter, 0, 0), (QSignalTransition*)0); + + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: cannot add transition to null state"); + QCOMPARE(s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), 0), (QSignalTransition*)0); + + QFinalState *s1 = new QFinalState(machine.rootState()); + QTest::ignoreMessage(QtWarningMsg, "QState::addTransition: no such signal SignalEmitter::noSuchSignal()"); + QCOMPARE(s0->addTransition(&emitter, SIGNAL(noSuchSignal()), s1), (QSignalTransition*)0); + + QSignalTransition *trans = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); + QVERIFY(trans != 0); + QCOMPARE(trans->sourceState(), s0); + QCOMPARE(trans->targetState(), (QAbstractState*)s1); + QCOMPARE(trans->senderObject(), (QObject*)&emitter); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + emitter.emitSignalWithNoArg(); + + QTRY_COMPARE(finishedSpy.count(), 1); + + emitter.emitSignalWithNoArg(); + + trans->setSignal(SIGNAL(signalWithIntArg(int))); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithIntArg(int)))); + machine.start(); + QCoreApplication::processEvents(); + emitter.emitSignalWithIntArg(123); + QTRY_COMPARE(finishedSpy.count(), 2); + + machine.start(); + QCoreApplication::processEvents(); + trans->setSignal(SIGNAL(signalWithNoArg())); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); + emitter.emitSignalWithNoArg(); + QTRY_COMPARE(finishedSpy.count(), 3); + + SignalEmitter emitter2; + machine.start(); + QCoreApplication::processEvents(); + trans->setSenderObject(&emitter2); + emitter2.emitSignalWithNoArg(); + QTRY_COMPARE(finishedSpy.count(), 4); + + machine.start(); + QCoreApplication::processEvents(); + QTest::ignoreMessage(QtWarningMsg, "QSignalTransition: no such signal: SignalEmitter::noSuchSignal()"); + trans->setSignal(SIGNAL(noSuchSignal())); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(noSuchSignal()))); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + SignalEmitter emitter; + QSignalTransition *trans = s0->addTransition(&emitter, "signalWithNoArg()", s1); + QVERIFY(trans != 0); + QCOMPARE(trans->sourceState(), s0); + QCOMPARE(trans->targetState(), (QAbstractState*)s1); + QCOMPARE(trans->senderObject(), (QObject*)&emitter); + QCOMPARE(trans->signal(), QByteArray("signalWithNoArg()")); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + emitter.emitSignalWithNoArg(); + + QTRY_COMPARE(finishedSpy.count(), 1); + + trans->setSignal("signalWithIntArg(int)"); + QCOMPARE(trans->signal(), QByteArray("signalWithIntArg(int)")); + machine.start(); + QCoreApplication::processEvents(); + emitter.emitSignalWithIntArg(123); + QTRY_COMPARE(finishedSpy.count(), 2); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + SignalEmitter emitter; + TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithIntArg(int)), s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + emitter.emitSignalWithIntArg(123); + + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(trans->argumentsReceived().size(), 1); + QCOMPARE(trans->argumentsReceived().at(0).toInt(), 123); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + SignalEmitter emitter; + TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QString testString = QString::fromLatin1("hello"); + emitter.emitSignalWithStringArg(testString); + + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(trans->argumentsReceived().size(), 1); + QCOMPARE(trans->argumentsReceived().at(0).toString(), testString); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + TestSignalTransition *trans = new TestSignalTransition(); + QCOMPARE(trans->senderObject(), (QObject*)0); + QCOMPARE(trans->signal(), QByteArray()); + + SignalEmitter emitter; + trans->setSenderObject(&emitter); + QCOMPARE(trans->senderObject(), (QObject*)&emitter); + trans->setSignal(SIGNAL(signalWithNoArg())); + QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); + trans->setTargetState(s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + emitter.emitSignalWithNoArg(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } + // Multiple transitions for same (object,signal) + { + QStateMachine machine; + SignalEmitter emitter; + QState *s0 = new QState(machine.rootState()); + QState *s1 = new QState(machine.rootState()); + QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); + QSignalTransition *t1 = s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s0); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + emitter.emitSignalWithNoArg(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + s0->removeTransition(t0); + emitter.emitSignalWithNoArg(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + emitter.emitSignalWithNoArg(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + s1->removeTransition(t1); + emitter.emitSignalWithNoArg(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + s0->addTransition(t0); + s1->addTransition(t1); + emitter.emitSignalWithNoArg(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + } + // multiple signal transitions from same source + { + QStateMachine machine; + SignalEmitter emitter; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + s0->addTransition(&emitter, SIGNAL(signalWithNoArg()), s1); + QFinalState *s2 = new QFinalState(machine.rootState()); + s0->addTransition(&emitter, SIGNAL(signalWithIntArg(int)), s2); + QFinalState *s3 = new QFinalState(machine.rootState()); + s0->addTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s3); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + emitter.emitSignalWithNoArg(); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.start(); + QTRY_COMPARE(startedSpy.count(), 2); + emitter.emitSignalWithIntArg(123); + QTRY_COMPARE(finishedSpy.count(), 2); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); + + machine.start(); + QTRY_COMPARE(startedSpy.count(), 3); + emitter.emitSignalWithStringArg("hello"); + QTRY_COMPARE(finishedSpy.count(), 3); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s3)); + } + // signature normalization + { + QStateMachine machine; + SignalEmitter emitter; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + QSignalTransition *t0 = s0->addTransition(&emitter, SIGNAL( signalWithNoArg( ) ), s1); + QVERIFY(t0 != 0); + QCOMPARE(t0->signal(), QByteArray(SIGNAL( signalWithNoArg( ) ))); + + QSignalTransition *t1 = s0->addTransition(&emitter, SIGNAL( signalWithStringArg( const QString & ) ), s1); + QVERIFY(t1 != 0); + QCOMPARE(t1->signal(), QByteArray(SIGNAL( signalWithStringArg( const QString & ) ))); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + + emitter.emitSignalWithNoArg(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } +} + +void tst_QStateMachine::eventTransitions() +{ + QPushButton button; + for (int x = 0; x < 2; ++x) { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QMouseEventTransition *trans; + if (x == 0) { + trans = new QMouseEventTransition(&button, QEvent::MouseButtonPress, Qt::LeftButton); + QCOMPARE(trans->targetState(), (QAbstractState*)0); + trans->setTargetState(s1); + } else { + trans = new QMouseEventTransition(&button, QEvent::MouseButtonPress, + Qt::LeftButton, QList<QAbstractState*>() << s1); + } + QCOMPARE(trans->eventType(), QEvent::MouseButtonPress); + QCOMPARE(trans->button(), Qt::LeftButton); + QCOMPARE(trans->targetState(), (QAbstractState*)s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QTest::mousePress(&button, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 1); + + QTest::mousePress(&button, Qt::LeftButton); + + trans->setEventType(QEvent::MouseButtonRelease); + QCOMPARE(trans->eventType(), QEvent::MouseButtonRelease); + machine.start(); + QCoreApplication::processEvents(); + QTest::mouseRelease(&button, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 2); + + machine.start(); + QCoreApplication::processEvents(); + trans->setEventType(QEvent::MouseButtonPress); + QTest::mousePress(&button, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 3); + + QPushButton button2; + machine.start(); + QCoreApplication::processEvents(); + trans->setEventObject(&button2); + QTest::mousePress(&button2, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 4); + } + for (int x = 0; x < 3; ++x) { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QEventTransition *trans; + if (x == 0) { + trans = new QEventTransition(); + QCOMPARE(trans->eventObject(), (QObject*)0); + QCOMPARE(trans->eventType(), QEvent::None); + trans->setEventObject(&button); + trans->setEventType(QEvent::MouseButtonPress); + trans->setTargetState(s1); + } else if (x == 1) { + trans = new QEventTransition(&button, QEvent::MouseButtonPress); + trans->setTargetState(s1); + } else { + trans = new QEventTransition(&button, QEvent::MouseButtonPress, + QList<QAbstractState*>() << s1); + } + QCOMPARE(trans->eventObject(), (QObject*)&button); + QCOMPARE(trans->eventType(), QEvent::MouseButtonPress); + QCOMPARE(trans->targetState(), (QAbstractState*)s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QMouseEventTransition *trans = new QMouseEventTransition(); + QCOMPARE(trans->eventObject(), (QObject*)0); + QCOMPARE(trans->eventType(), QEvent::None); + QCOMPARE(trans->button(), Qt::NoButton); + trans->setEventObject(&button); + trans->setEventType(QEvent::MouseButtonPress); + trans->setButton(Qt::LeftButton); + trans->setTargetState(s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } + + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QKeyEventTransition *trans = new QKeyEventTransition(&button, QEvent::KeyPress, Qt::Key_A); + QCOMPARE(trans->eventType(), QEvent::KeyPress); + QCOMPARE(trans->key(), (int)Qt::Key_A); + trans->setTargetState(s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QTest::keyPress(&button, Qt::Key_A); + QCoreApplication::processEvents(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QKeyEventTransition *trans = new QKeyEventTransition(); + QCOMPARE(trans->eventObject(), (QObject*)0); + QCOMPARE(trans->eventType(), QEvent::None); + QCOMPARE(trans->key(), 0); + trans->setEventObject(&button); + trans->setEventType(QEvent::KeyPress); + trans->setKey(Qt::Key_A); + trans->setTargetState(s1); + s0->addTransition(trans); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + + QTest::keyPress(&button, Qt::Key_A); + QCoreApplication::processEvents(); + + QTRY_COMPARE(finishedSpy.count(), 1); + } + // Multiple transitions for same (object,event) + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QState *s1 = new QState(machine.rootState()); + QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); + t0->setTargetState(s1); + s0->addTransition(t0); + QEventTransition *t1 = new QEventTransition(&button, QEvent::MouseButtonPress); + t1->setTargetState(s0); + s1->addTransition(t1); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + machine.start(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + s0->removeTransition(t0); + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + s1->removeTransition(t1); + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s0)); + + s0->addTransition(t0); + s1->addTransition(t1); + QTest::mousePress(&button, Qt::LeftButton); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + } + // multiple event transitions from same source + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + QFinalState *s2 = new QFinalState(machine.rootState()); + QEventTransition *t0 = new QEventTransition(&button, QEvent::MouseButtonPress); + t0->setTargetState(s1); + s0->addTransition(t0); + QEventTransition *t1 = new QEventTransition(&button, QEvent::MouseButtonRelease); + t1->setTargetState(s2); + s0->addTransition(t1); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.setInitialState(s0); + + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + QTest::mousePress(&button, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.start(); + QTRY_COMPARE(startedSpy.count(), 2); + QTest::mouseRelease(&button, Qt::LeftButton); + QTRY_COMPARE(finishedSpy.count(), 2); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); + } + // custom event + { + QStateMachine machine; + QState *s0 = new QState(machine.rootState()); + QFinalState *s1 = new QFinalState(machine.rootState()); + + QEventTransition *trans = new QEventTransition(&button, QEvent::Type(QEvent::User+1)); + trans->setTargetState(s1); + s0->addTransition(trans); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + machine.setInitialState(s0); + machine.start(); + QTest::ignoreMessage(QtWarningMsg, "QObject event transitions are not supported for custom types"); + QTRY_COMPARE(startedSpy.count(), 1); + } +} + +void tst_QStateMachine::historyStates() +{ + for (int x = 0; x < 2; ++x) { + QStateMachine machine; + QState *root = machine.rootState(); + QState *s0 = new QState(root); + QState *s00 = new QState(s0); + QState *s01 = new QState(s0); + QHistoryState *s0h; + if (x == 0) { + s0h = new QHistoryState(s0); + QCOMPARE(s0h->historyType(), QHistoryState::ShallowHistory); + s0h->setHistoryType(QHistoryState::DeepHistory); + } else { + s0h = new QHistoryState(QHistoryState::DeepHistory, s0); + } + QCOMPARE(s0h->historyType(), QHistoryState::DeepHistory); + s0h->setHistoryType(QHistoryState::ShallowHistory); + QCOMPARE(s0h->historyType(), QHistoryState::ShallowHistory); + QCOMPARE(s0h->defaultState(), (QAbstractState*)0); + s0h->setDefaultState(s00); + QCOMPARE(s0h->defaultState(), (QAbstractState*)s00); + QString warning; + warning.sprintf("QHistoryState::setDefaultState: state %p does not belong to this history state's group (%p)", s0, s0); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); + s0h->setDefaultState(s0); + QState *s1 = new QState(root); + QFinalState *s2 = new QFinalState(root); + + s00->addTransition(new StringTransition("a", s01)); + s0->addTransition(new StringTransition("b", s1)); + s1->addTransition(new StringTransition("c", s0h)); + s0->addTransition(new StringTransition("d", s2)); + + root->setInitialState(s0); + s0->setInitialState(s00); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s0)); + QVERIFY(machine.configuration().contains(s00)); + + machine.postEvent(new StringEvent("a")); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s0)); + QVERIFY(machine.configuration().contains(s01)); + + machine.postEvent(new StringEvent("b")); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.postEvent(new StringEvent("c")); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s0)); + QVERIFY(machine.configuration().contains(s01)); + + machine.postEvent(new StringEvent("d")); + QCoreApplication::processEvents(); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); + + QTRY_COMPARE(finishedSpy.count(), 1); + } +} + +void tst_QStateMachine::startAndStop() +{ + QStateMachine machine; + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + QVERIFY(!machine.isRunning()); + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start."); + machine.start(); + QCOMPARE(startedSpy.count(), 0); + QCOMPARE(stoppedSpy.count(), 0); + QCOMPARE(finishedSpy.count(), 0); + QVERIFY(!machine.isRunning()); + machine.stop(); + QCOMPARE(startedSpy.count(), 0); + QCOMPARE(stoppedSpy.count(), 0); + QCOMPARE(finishedSpy.count(), 0); + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + machine.start(); + QTRY_COMPARE(machine.isRunning(), true); + QTRY_COMPARE(startedSpy.count(), 1); + QCOMPARE(stoppedSpy.count(), 0); + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(s1)); + + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start(): already running"); + machine.start(); + + machine.stop(); + QTRY_COMPARE(machine.isRunning(), false); + QTRY_COMPARE(stoppedSpy.count(), 1); + QCOMPARE(startedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + + QCOMPARE(machine.configuration().count(), 1); + QVERIFY(machine.configuration().contains(s1)); + + machine.start(); + machine.stop(); + QTRY_COMPARE(startedSpy.count(), 2); + QCOMPARE(stoppedSpy.count(), 2); +} + +void tst_QStateMachine::targetStateWithNoParent() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + s1->setObjectName("s1"); + QState s2; + s1->addTransition(&s2); + machine.setInitialState(s1); + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 's1'"); + QTRY_COMPARE(startedSpy.count(), 1); + QCOMPARE(machine.isRunning(), false); + QCOMPARE(stoppedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(machine.error(), QStateMachine::NoCommonAncestorForTransitionError); +} + +void tst_QStateMachine::targetStateDeleted() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + s1->setObjectName("s1"); + QState *s2 = new QState(machine.rootState()); + QAbstractTransition *trans = s1->addTransition(s2); + delete s2; + QCOMPARE(trans->targetState(), (QAbstractState*)0); + QVERIFY(trans->targetStates().isEmpty()); +} + +void tst_QStateMachine::defaultGlobalRestorePolicy() +{ + QStateMachine machine; + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("a", 1); + propertyHolder->setProperty("b", 2); + + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(propertyHolder, "a", 3); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "b", 4); + + QState *s3 = new QState(machine.rootState()); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); +} + +void tst_QStateMachine::noInitialStateForInitialState() +{ + QStateMachine machine; + + QState *initialState = new QState(machine.rootState()); + initialState->setObjectName("initialState"); + machine.setInitialState(initialState); + + QState *childState = new QState(initialState); + + QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: " + "Missing initial state in compound state 'initialState'"); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.isRunning(), false); + QCOMPARE(int(machine.error()), int(QStateMachine::NoInitialStateError)); +} + +/* +void tst_QStateMachine::restorePolicyNotInherited() +{ + QStateMachine machine; + + QObject *propertyHolder = new QObject(); + propertyHolder->setProperty("a", 1); + propertyHolder->setProperty("b", 2); + + QState *parentState = new QState(machine.rootState()); + parentState->setObjectName("parentState"); + parentState->setRestorePolicy(QState::RestoreProperties); + + QState *s1 = new QState(parentState); + s1->setObjectName("s1"); + s1->assignProperty(propertyHolder, "a", 3); + parentState->setInitialState(s1); + + QState *s2 = new QState(parentState); + s2->setObjectName("s2"); + s2->assignProperty(propertyHolder, "b", 4); + + QState *s3 = new QState(parentState); + s3->setObjectName("s3"); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(parentState); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + +}*/ + +void tst_QStateMachine::globalRestorePolicySetToDoNotRestore() +{ + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::DoNotRestoreProperties); + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("a", 1); + propertyHolder->setProperty("b", 2); + + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(propertyHolder, "a", 3); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "b", 4); + + QState *s3 = new QState(machine.rootState()); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 4); +} + +/* +void tst_QStateMachine::setRestorePolicyToDoNotRestore() +{ + QObject *object = new QObject(); + object->setProperty("a", 1); + object->setProperty("b", 2); + + QStateMachine machine; + + QState *S1 = new QState(); + S1->setObjectName("S1"); + S1->assignProperty(object, "a", 3); + S1->setRestorePolicy(QState::DoNotRestoreProperties); + machine.addState(S1); + + QState *S2 = new QState(); + S2->setObjectName("S2"); + S2->assignProperty(object, "b", 5); + S2->setRestorePolicy(QState::DoNotRestoreProperties); + machine.addState(S2); + + QState *S3 = new QState(); + S3->setObjectName("S3"); + S3->setRestorePolicy(QState::DoNotRestoreProperties); + machine.addState(S3); + + QFinalState *S4 = new QFinalState(); + machine.addState(S4); + + S1->addTransition(new EventTransition(QEvent::User, S2)); + S2->addTransition(new EventTransition(QEvent::User, S3)); + S3->addTransition(S4); + + machine.setInitialState(S1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 3); + QCOMPARE(object->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 3); + QCOMPARE(object->property("b").toInt(), 5); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(object->property("a").toInt(), 3); + QCOMPARE(object->property("b").toInt(), 5); +} + +void tst_QStateMachine::setGlobalRestorePolicyToGlobalRestore() +{ + s_countWarnings = false; + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::GlobalRestorePolicy); + + QCOMPARE(machine.globalRestorePolicy(), QStateMachine::DoNotRestoreProperties); + QCOMPARE(s_msgType, QtWarningMsg); + + s_msgType = QtDebugMsg; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + machine.setGlobalRestorePolicy(QStateMachine::GlobalRestorePolicy); + + QCOMPARE(machine.globalRestorePolicy(), QStateMachine::RestoreProperties); + QCOMPARE(s_msgType, QtWarningMsg); +} + + +void tst_QStateMachine::restorePolicyOnChildState() +{ + QStateMachine machine; + + QObject *propertyHolder = new QObject(); + propertyHolder->setProperty("a", 1); + propertyHolder->setProperty("b", 2); + + QState *parentState = new QState(machine.rootState()); + parentState->setObjectName("parentState"); + + QState *s1 = new QState(parentState); + s1->setRestorePolicy(QState::RestoreProperties); + s1->setObjectName("s1"); + s1->assignProperty(propertyHolder, "a", 3); + parentState->setInitialState(s1); + + QState *s2 = new QState(parentState); + s2->setRestorePolicy(QState::RestoreProperties); + s2->setObjectName("s2"); + s2->assignProperty(propertyHolder, "b", 4); + + QState *s3 = new QState(parentState); + s3->setRestorePolicy(QState::RestoreProperties); + s3->setObjectName("s3"); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(parentState); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 1); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 1); + QCOMPARE(propertyHolder->property("b").toInt(), 2); +} +*/ + +void tst_QStateMachine::globalRestorePolicySetToRestore() +{ + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("a", 1); + propertyHolder->setProperty("b", 2); + + QState *s1 = new QState(machine.rootState()); + s1->assignProperty(propertyHolder, "a", 3); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "b", 4); + + QState *s3 = new QState(machine.rootState()); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 3); + QCOMPARE(propertyHolder->property("b").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 1); + QCOMPARE(propertyHolder->property("b").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("a").toInt(), 1); + QCOMPARE(propertyHolder->property("b").toInt(), 2); +} + +/* +void tst_QStateMachine::mixedRestoreProperties() +{ + QStateMachine machine; + + QObject *propertyHolder = new QObject(); + propertyHolder->setProperty("a", 1); + + QState *s1 = new QState(machine.rootState()); + s1->setRestorePolicy(QState::RestoreProperties); + s1->assignProperty(propertyHolder, "a", 3); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "a", 4); + + QState *s3 = new QState(machine.rootState()); + + QState *s4 = new QState(machine.rootState()); + s4->assignProperty(propertyHolder, "a", 5); + + QState *s5 = new QState(machine.rootState()); + s5->setRestorePolicy(QState::RestoreProperties); + s5->assignProperty(propertyHolder, "a", 6); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s2->addTransition(new EventTransition(QEvent::User, s3)); + s3->addTransition(new EventTransition(QEvent::User, s4)); + s4->addTransition(new EventTransition(QEvent::User, s5)); + s5->addTransition(new EventTransition(QEvent::User, s3)); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + // Enter s1, save current + QCOMPARE(propertyHolder->property("a").toInt(), 3); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + // Enter s2, restorePolicy == DoNotRestore, so restore all properties + QCOMPARE(propertyHolder->property("a").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + // Enter s3 + QCOMPARE(propertyHolder->property("a").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + // Enter s4 + QCOMPARE(propertyHolder->property("a").toInt(), 5); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + // Enter s5, save current + QCOMPARE(propertyHolder->property("a").toInt(), 6); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + // Enter s3, restore + QCOMPARE(propertyHolder->property("a").toInt(), 5); +} +*/ + +void tst_QStateMachine::transitionWithParent() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + QState *s2 = new QState(machine.rootState()); + EventTransition *trans = new EventTransition(QEvent::User, s2, s1); + QCOMPARE(trans->sourceState(), s1); + QCOMPARE(trans->targetState(), (QAbstractState*)s2); + QCOMPARE(trans->targetStates().size(), 1); + QCOMPARE(trans->targetStates().at(0), (QAbstractState*)s2); +} + +void tst_QStateMachine::simpleAnimation() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("fooBar", 1.0); + + QState *s1 = new QState(machine.rootState()); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "fooBar", 2.0); + + EventTransition *et = new EventTransition(QEvent::User, s2); + QPropertyAnimation *animation = new QPropertyAnimation(object, "fooBar", s2); + et->addAnimation(animation); + s1->addTransition(et); + + QState *s3 = new QState(machine.rootState()); + s2->addTransition(animation, SIGNAL(finished()), s3); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("fooBar").toDouble(), 2.0); +} + +class SlotCalledCounter: public QObject +{ + Q_OBJECT +public: + SlotCalledCounter() : counter(0) {} + + int counter; + +public slots: + void slot() { counter++; } +}; + +void tst_QStateMachine::twoAnimations() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + object->setProperty("bar", 3.0); + + QState *s1 = new QState(machine.rootState()); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + s2->assignProperty(object, "bar", 10.0); + + QPropertyAnimation *animationFoo = new QPropertyAnimation(object, "foo", s2); + QPropertyAnimation *animationBar = new QPropertyAnimation(object, "bar", s2); + animationBar->setDuration(900); + + SlotCalledCounter counter; + connect(animationFoo, SIGNAL(finished()), &counter, SLOT(slot())); + connect(animationBar, SIGNAL(finished()), &counter, SLOT(slot())); + + EventTransition *et = new EventTransition(QEvent::User, s2); + et->addAnimation(animationFoo); + et->addAnimation(animationBar); + s1->addTransition(et); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + s2->addTransition(s2, SIGNAL(polished()), s3); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); + QCOMPARE(object->property("bar").toDouble(), 10.0); + + QCOMPARE(counter.counter, 2); +} + +void tst_QStateMachine::twoAnimatedTransitions() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 5.0); + QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); + s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(fooAnimation); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + s2->addTransition(fooAnimation, SIGNAL(finished()), s3); + + QState *s4 = new QState(machine.rootState()); + s4->assignProperty(object, "foo", 2.0); + QPropertyAnimation *fooAnimation2 = new QPropertyAnimation(object, "foo", s4); + s3->addTransition(new EventTransition(QEvent::User, s4))->addAnimation(fooAnimation2); + + QState *s5 = new QState(machine.rootState()); + QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); + s4->addTransition(fooAnimation2, SIGNAL(finished()), s5); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 5.0); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s5)); + QCOMPARE(object->property("foo").toDouble(), 2.0); +} + +void tst_QStateMachine::playAnimationTwice() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 5.0); + QPropertyAnimation *fooAnimation = new QPropertyAnimation(object, "foo", s2); + s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(fooAnimation); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + s2->addTransition(fooAnimation, SIGNAL(finished()), s3); + + QState *s4 = new QState(machine.rootState()); + s4->assignProperty(object, "foo", 2.0); + s3->addTransition(new EventTransition(QEvent::User, s4))->addAnimation(fooAnimation); + + QState *s5 = new QState(machine.rootState()); + QObject::connect(s5, SIGNAL(entered()), QApplication::instance(), SLOT(quit())); + s4->addTransition(fooAnimation, SIGNAL(finished()), s5); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 5.0); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s5)); + QCOMPARE(object->property("foo").toDouble(), 2.0); +} + +void tst_QStateMachine::nestedTargetStateForAnimation() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + object->setProperty("bar", 3.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + QState *s2 = new QState(machine.rootState()); + + s2->assignProperty(object, "foo", 2.0); + + QState *s2Child = new QState(s2); + s2Child->assignProperty(object, "bar", 10.0); + s2->setInitialState(s2Child); + + QState *s2Child2 = new QState(s2); + s2Child2->assignProperty(object, "bar", 11.0); + QAbstractTransition *at = s2Child->addTransition(new EventTransition(QEvent::User, s2Child2)); + + QPropertyAnimation *animation = new QPropertyAnimation(object, "bar", s2); + animation->setDuration(2000); + connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(animation); + + at = s1->addTransition(new EventTransition(QEvent::User, s2)); + + animation = new QPropertyAnimation(object, "foo", s2); + connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(animation); + + animation = new QPropertyAnimation(object, "bar", s2); + connect(animation, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(animation); + + QState *s3 = new QState(machine.rootState()); + s2->addTransition(s2Child, SIGNAL(polished()), s3); + + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + machine.postEvent(new QEvent(QEvent::User)); + + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); + QCOMPARE(object->property("bar").toDouble(), 10.0); + QCOMPARE(counter.counter, 2); +} + +void tst_QStateMachine::animatedGlobalRestoreProperty() +{ + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + + QState *s4 = new QState(machine.rootState()); + QObject::connect(s4, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); + QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", s2); + connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(pa); + + at = s2->addTransition(pa, SIGNAL(finished()), s3); + pa = new QPropertyAnimation(object, "foo", s3); + connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(pa); + + at = s3->addTransition(pa, SIGNAL(finished()), s4); + pa = new QPropertyAnimation(object, "foo", s4); + connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); + at->addAnimation(pa); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s4)); + QCOMPARE(object->property("foo").toDouble(), 1.0); + QCOMPARE(counter.counter, 2); +} + +void tst_QStateMachine::specificTargetValueOfAnimation() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QPropertyAnimation *anim = new QPropertyAnimation(object, "foo"); + anim->setEndValue(10.0); + s1->addTransition(new EventTransition(QEvent::User, s2))->addAnimation(anim); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + s2->addTransition(anim, SIGNAL(finished()), s3); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); + QCOMPARE(anim->endValue().toDouble(), 10.0); +} + +void tst_QStateMachine::addDefaultAnimation() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); + machine.addDefaultAnimation(pa); + s2->addTransition(pa, SIGNAL(finished()), s3); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); +} + +void tst_QStateMachine::addDefaultAnimationWithUnusedAnimation() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + object->setProperty("bar", 2.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); + connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); + machine.addDefaultAnimation(pa); + s2->addTransition(pa, SIGNAL(finished()), s3); + + pa = new QPropertyAnimation(object, "bar", &machine); + connect(pa, SIGNAL(finished()), &counter, SLOT(slot())); + machine.addDefaultAnimation(pa); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); + QCOMPARE(counter.counter, 1); +} + +void tst_QStateMachine::removeDefaultAnimation() +{ + QStateMachine machine; + + QObject propertyHolder; + propertyHolder.setProperty("foo", 0); + + QCOMPARE(machine.defaultAnimations().size(), 0); + + QPropertyAnimation *anim = new QPropertyAnimation(&propertyHolder, "foo"); + + machine.addDefaultAnimation(anim); + + QCOMPARE(machine.defaultAnimations().size(), 1); + QVERIFY(machine.defaultAnimations().contains(anim)); + + machine.removeDefaultAnimation(anim); + + QCOMPARE(machine.defaultAnimations().size(), 0); + + machine.addDefaultAnimation(anim); + + QPropertyAnimation *anim2 = new QPropertyAnimation(&propertyHolder, "foo"); + machine.addDefaultAnimation(anim2); + + QCOMPARE(machine.defaultAnimations().size(), 2); + QVERIFY(machine.defaultAnimations().contains(anim)); + QVERIFY(machine.defaultAnimations().contains(anim2)); + + machine.removeDefaultAnimation(anim); + + QCOMPARE(machine.defaultAnimations().size(), 1); + QVERIFY(machine.defaultAnimations().contains(anim2)); + + machine.removeDefaultAnimation(anim2); + QCOMPARE(machine.defaultAnimations().size(), 0); +} + +void tst_QStateMachine::overrideDefaultAnimationWithSpecific() +{ + QStateMachine machine; + + QObject *object = new QObject(&machine); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimation(defaultAnimation); + at->addAnimation(moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped +} + +/* +void tst_QStateMachine::addDefaultAnimationForSource() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); + machine.addDefaultAnimationForSourceState(s1, pa); + s2->addTransition(pa, SIGNAL(finished()), s3); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); +} + +void tst_QStateMachine::addDefaultAnimationForTarget() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + QState *s1 = new QState(machine.rootState()); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *pa = new QPropertyAnimation(object, "foo", &machine); + machine.addDefaultAnimationForTargetState(s2, pa); + s2->addTransition(pa, SIGNAL(finished()), s3); + + machine.setInitialState(s1); + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(object->property("foo").toDouble(), 2.0); +} + +void tst_QStateMachine::removeDefaultAnimationForSource() +{ + QStateMachine machine; + + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + + QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); + + machine.addDefaultAnimationForSourceState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimations().size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); + + machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimations().size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); + + machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + + machine.addDefaultAnimationForSourceState(machine.rootState(), anim); + + QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); + machine.addDefaultAnimationForSourceState(machine.rootState(), anim2); + + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 2); + QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim)); + QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim2)); + + machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForSourceState(machine.rootState()).contains(anim2)); + + machine.removeDefaultAnimationForSourceState(machine.rootState(), anim2); + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); +} + +void tst_QStateMachine::removeDefaultAnimationForTarget() +{ + QStateMachine machine; + + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + + QPropertyAnimation *anim = new QPropertyAnimation(this, "foo"); + + machine.addDefaultAnimationForTargetState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimations().size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); + + machine.removeDefaultAnimationForSourceState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimations().size(), 0); + QCOMPARE(machine.defaultAnimationsForSourceState(machine.rootState()).size(), 0); + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); + + machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); + + machine.addDefaultAnimationForTargetState(machine.rootState(), anim); + + QPropertyAnimation *anim2 = new QPropertyAnimation(this, "foo"); + machine.addDefaultAnimationForTargetState(machine.rootState(), anim2); + + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 2); + QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim)); + QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim2)); + + machine.removeDefaultAnimationForTargetState(machine.rootState(), anim); + + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 1); + QVERIFY(machine.defaultAnimationsForTargetState(machine.rootState()).contains(anim2)); + + machine.removeDefaultAnimationForTargetState(machine.rootState(), anim2); + QCOMPARE(machine.defaultAnimationsForTargetState(machine.rootState()).size(), 0); +} + +void tst_QStateMachine::overrideDefaultAnimationWithSource() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimation(defaultAnimation); + machine.addDefaultAnimationForSourceState(s1, moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped +} + +void tst_QStateMachine::overrideDefaultAnimationWithTarget() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimation(defaultAnimation); + machine.addDefaultAnimationForTargetState(s2, moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped + +} + +void tst_QStateMachine::overrideDefaultSourceAnimationWithSpecific() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimationForSourceState(s1, defaultAnimation); + at->addAnimation(moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped +} + +void tst_QStateMachine::overrideDefaultTargetAnimationWithSpecific() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + QAbstractTransition *at = s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimationForTargetState(s2, defaultAnimation); + at->addAnimation(moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped +} + +void tst_QStateMachine::overrideDefaultTargetAnimationWithSource() +{ + QStateMachine machine; + + QObject *object = new QObject(); + object->setProperty("foo", 1.0); + + SlotCalledCounter counter; + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(object, "foo", 2.0); + + QState *s3 = new QState(machine.rootState()); + QObject::connect(s3, SIGNAL(entered()), QCoreApplication::instance(), SLOT(quit())); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + + QPropertyAnimation *defaultAnimation = new QPropertyAnimation(object, "foo"); + connect(defaultAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + QPropertyAnimation *moreSpecificAnimation = new QPropertyAnimation(object, "foo"); + s2->addTransition(moreSpecificAnimation, SIGNAL(finished()), s3); + connect(moreSpecificAnimation, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)), &counter, SLOT(slot())); + + machine.addDefaultAnimationForTargetState(s2, defaultAnimation); + machine.addDefaultAnimationForSourceState(s1, moreSpecificAnimation); + + machine.start(); + QCoreApplication::processEvents(); + + machine.postEvent(new QEvent(QEvent::User)); + QCOREAPPLICATION_EXEC(5000); + + QVERIFY(machine.configuration().contains(s3)); + QCOMPARE(counter.counter, 2); // specific animation started and stopped +} + +*/ + +void tst_QStateMachine::parallelStateAssignmentsDone() +{ + QStateMachine machine; + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("foo", 123); + propertyHolder->setProperty("bar", 456); + propertyHolder->setProperty("zoot", 789); + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + parallelState->assignProperty(propertyHolder, "foo", 321); + + QState *s2 = new QState(parallelState); + s2->assignProperty(propertyHolder, "bar", 654); + + QState *s3 = new QState(parallelState); + s3->assignProperty(propertyHolder, "zoot", 987); + + s1->addTransition(new EventTransition(QEvent::User, parallelState)); + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("foo").toInt(), 123); + QCOMPARE(propertyHolder->property("bar").toInt(), 456); + QCOMPARE(propertyHolder->property("zoot").toInt(), 789); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(propertyHolder->property("foo").toInt(), 321); + QCOMPARE(propertyHolder->property("bar").toInt(), 654); + QCOMPARE(propertyHolder->property("zoot").toInt(), 987); +} + +void tst_QStateMachine::transitionsFromParallelStateWithNoChildren() +{ + QStateMachine machine; + + QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + machine.setInitialState(parallelState); + + QState *s1 = new QState(machine.rootState()); + parallelState->addTransition(new EventTransition(QEvent::User, s1)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(1, machine.configuration().size()); + QVERIFY(machine.configuration().contains(parallelState)); + + machine.postEvent(new QEvent(QEvent::User)); + + QCoreApplication::processEvents(); + + QCOMPARE(1, machine.configuration().size()); + QVERIFY(machine.configuration().contains(s1)); +} + +void tst_QStateMachine::parallelStateTransition() +{ + QStateMachine machine; + + QState *parallelState = new QState(QState::ParallelStates, machine.rootState()); + machine.setInitialState(parallelState); + + QState *s1 = new QState(parallelState); + QState *s2 = new QState(parallelState); + + QState *s1InitialChild = new QState(s1); + s1->setInitialState(s1InitialChild); + + QState *s2InitialChild = new QState(s2); + s2->setInitialState(s2InitialChild); + + QState *s1OtherChild = new QState(s1); + + s1->addTransition(new EventTransition(QEvent::User, s1OtherChild)); + + machine.start(); + QCoreApplication::processEvents(); + + QVERIFY(machine.configuration().contains(parallelState)); + QVERIFY(machine.configuration().contains(s1)); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s1InitialChild)); + QVERIFY(machine.configuration().contains(s2InitialChild)); + QCOMPARE(machine.configuration().size(), 5); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QVERIFY(machine.configuration().contains(parallelState)); + + QVERIFY(machine.configuration().contains(s1)); + + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s1OtherChild)); + QVERIFY(machine.configuration().contains(s2InitialChild)); + QCOMPARE(machine.configuration().size(), 5); + +} + +void tst_QStateMachine::nestedRestoreProperties() +{ + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("foo", 1); + propertyHolder->setProperty("bar", 2); + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "foo", 3); + + QState *s21 = new QState(s2); + s21->assignProperty(propertyHolder, "bar", 4); + s2->setInitialState(s21); + + QState *s22 = new QState(s2); + s22->assignProperty(propertyHolder, "bar", 5); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s21->addTransition(new EventTransition(QEvent::User, s22)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + QCOMPARE(propertyHolder->property("foo").toInt(), 1); + QCOMPARE(propertyHolder->property("bar").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s21)); + QCOMPARE(propertyHolder->property("foo").toInt(), 3); + QCOMPARE(propertyHolder->property("bar").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s22)); + QCOMPARE(propertyHolder->property("foo").toInt(), 3); + QCOMPARE(propertyHolder->property("bar").toInt(), 5); +} + +void tst_QStateMachine::nestedRestoreProperties2() +{ + QStateMachine machine; + machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties); + + QObject *propertyHolder = new QObject(&machine); + propertyHolder->setProperty("foo", 1); + propertyHolder->setProperty("bar", 2); + + QState *s1 = new QState(machine.rootState()); + machine.setInitialState(s1); + + QState *s2 = new QState(machine.rootState()); + s2->assignProperty(propertyHolder, "foo", 3); + + QState *s21 = new QState(s2); + s21->assignProperty(propertyHolder, "bar", 4); + s2->setInitialState(s21); + + QState *s22 = new QState(s2); + s22->assignProperty(propertyHolder, "foo", 6); + s22->assignProperty(propertyHolder, "bar", 5); + + s1->addTransition(new EventTransition(QEvent::User, s2)); + s21->addTransition(new EventTransition(QEvent::User, s22)); + s22->addTransition(new EventTransition(QEvent::User, s21)); + + machine.start(); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + QCOMPARE(propertyHolder->property("foo").toInt(), 1); + QCOMPARE(propertyHolder->property("bar").toInt(), 2); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s21)); + QCOMPARE(propertyHolder->property("foo").toInt(), 3); + QCOMPARE(propertyHolder->property("bar").toInt(), 4); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s22)); + QCOMPARE(propertyHolder->property("foo").toInt(), 6); + QCOMPARE(propertyHolder->property("bar").toInt(), 5); + + machine.postEvent(new QEvent(QEvent::User)); + QCoreApplication::processEvents(); + + QCOMPARE(machine.configuration().size(), 2); + QVERIFY(machine.configuration().contains(s2)); + QVERIFY(machine.configuration().contains(s21)); + QCOMPARE(propertyHolder->property("foo").toInt(), 3); + QCOMPARE(propertyHolder->property("bar").toInt(), 4); + +} + + +QTEST_MAIN(tst_QStateMachine) +#include "tst_qstatemachine.moc" diff --git a/tests/auto/qstringbuilder/qstringbuilder.pro b/tests/auto/qstringbuilder/qstringbuilder.pro new file mode 100644 index 0000000..c5a26d3 --- /dev/null +++ b/tests/auto/qstringbuilder/qstringbuilder.pro @@ -0,0 +1,5 @@ + +TEMPLATE = subdirs +SUBDIRS = scenario1.pro scenario2.pro scenario3.pro scenario4.pro + + diff --git a/tests/auto/qstringbuilder/scenario1.cpp b/tests/auto/qstringbuilder/scenario1.cpp new file mode 100644 index 0000000..26b4ed3 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario1.cpp @@ -0,0 +1 @@ +#include "tst_qstringbuilder.cpp" diff --git a/tests/auto/qstringbuilder/scenario1.pro b/tests/auto/qstringbuilder/scenario1.pro new file mode 100644 index 0000000..4ce7156 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario1.pro @@ -0,0 +1,8 @@ +load(qttest_p4) + +QT = core + +SOURCES += scenario1.cpp + +DEFINES += SCENARIO=1 + diff --git a/tests/auto/qstringbuilder/scenario2.cpp b/tests/auto/qstringbuilder/scenario2.cpp new file mode 100644 index 0000000..26b4ed3 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario2.cpp @@ -0,0 +1 @@ +#include "tst_qstringbuilder.cpp" diff --git a/tests/auto/qstringbuilder/scenario2.pro b/tests/auto/qstringbuilder/scenario2.pro new file mode 100644 index 0000000..64c46e2 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario2.pro @@ -0,0 +1,7 @@ +load(qttest_p4) + +QT = core + +SOURCES += scenario2.cpp + +DEFINES += SCENARIO=2 diff --git a/tests/auto/qstringbuilder/scenario3.cpp b/tests/auto/qstringbuilder/scenario3.cpp new file mode 100644 index 0000000..26b4ed3 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario3.cpp @@ -0,0 +1 @@ +#include "tst_qstringbuilder.cpp" diff --git a/tests/auto/qstringbuilder/scenario3.pro b/tests/auto/qstringbuilder/scenario3.pro new file mode 100644 index 0000000..beedffd --- /dev/null +++ b/tests/auto/qstringbuilder/scenario3.pro @@ -0,0 +1,7 @@ +load(qttest_p4) + +QT = core + +SOURCES += scenario3.cpp + +DEFINES += SCENARIO=3 diff --git a/tests/auto/qstringbuilder/scenario4.cpp b/tests/auto/qstringbuilder/scenario4.cpp new file mode 100644 index 0000000..26b4ed3 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario4.cpp @@ -0,0 +1 @@ +#include "tst_qstringbuilder.cpp" diff --git a/tests/auto/qstringbuilder/scenario4.pro b/tests/auto/qstringbuilder/scenario4.pro new file mode 100644 index 0000000..1c45a70 --- /dev/null +++ b/tests/auto/qstringbuilder/scenario4.pro @@ -0,0 +1,7 @@ +load(qttest_p4) + +QT = core + +SOURCES += scenario4.cpp + +DEFINES += SCENARIO=4 diff --git a/tests/auto/qstringbuilder/tst_qstringbuilder.cpp b/tests/auto/qstringbuilder/tst_qstringbuilder.cpp new file mode 100644 index 0000000..fdbaf21 --- /dev/null +++ b/tests/auto/qstringbuilder/tst_qstringbuilder.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// This is included in various .cpp files as a compile test for various scenarios +// depending on NO_CAST_* and QT_USE_FAST_OPERATOR_PLUS and QT_USE_FAST_CONCATENATION + +#if SCENARIO == 1 +// this is the "no harm done" version. Only operator% is active, +// with NO_CAST * defined +#define P % +#undef QT_USE_FAST_OPERATOR_PLUS +#undef QT_USE_FAST_CONCATENATION +#define QT_NO_CAST_FROM_ASCII +#define QT_NO_CAST_TO_ASCII +#endif + + +#if SCENARIO == 2 +// this is the "full" version. Operator+ is replaced by a QStringBuilder +// based version +// with NO_CAST * defined +#define P + +#define QT_USE_FAST_OPERATOR_PLUS +#define QT_USE_FAST_CONCATENATION +#define QT_NO_CAST_FROM_ASCII +#define QT_NO_CAST_TO_ASCII +#endif + +#if SCENARIO == 3 +// this is the "no harm done" version. Only operator% is active, +// with NO_CAST * _not_ defined +#define P % +#undef QT_USE_FAST_OPERATOR_PLUS +#undef QT_USE_FAST_CONCATENATION +#undef QT_NO_CAST_FROM_ASCII +#undef QT_NO_CAST_TO_ASCII +#endif + +#if SCENARIO == 4 +// this is the "full" version. Operator+ is replaced by a QStringBuilder +// based version +// with NO_CAST * _not_ defined +#define P + +#define QT_USE_FAST_OPERATOR_PLUS +#define QT_USE_FAST_CONCATENATION +#undef QT_NO_CAST_FROM_ASCII +#undef QT_NO_CAST_TO_ASCII +#endif + + +#include <QtTest/QtTest> + +//TESTED_CLASS=QStringBuilder +//TESTED_FILES=qstringbuilder.cpp + +#include <qtest.h> + +#define LITERAL "some literal" + +class tst_QStringBuilder : public QObject +{ + Q_OBJECT + +public: + tst_QStringBuilder() {} + ~tst_QStringBuilder() {} + +public slots: + void init() {} + void cleanup() {} + + void scenario(); +}; + +void tst_QStringBuilder::scenario() +{ + QLatin1Literal l1literal(LITERAL); + QLatin1String l1string(LITERAL); + QString string(l1string); + QStringRef stringref(&string, 2, 10); + QLatin1Char achar('c'); + QString r2(QLatin1String(LITERAL LITERAL)); + QString r; + + r = l1literal P l1literal; + QCOMPARE(r, r2); + r = string P string; + QCOMPARE(r, r2); + r = stringref P stringref; + QCOMPARE(r, QString(stringref.toString() + stringref.toString())); + r = string P l1literal; + QCOMPARE(r, r2); + r = string P l1string; + QCOMPARE(r, r2); + r = string + achar; + QCOMPARE(r, QString(string P achar)); + r = achar + string; + QCOMPARE(r, QString(achar P string)); +#ifndef QT_NO_CAST_FROM_ASCII + r = string P LITERAL; + QCOMPARE(r, r2); + r = LITERAL P string; + QCOMPARE(r, r2); +#endif +} + +QTEST_APPLESS_MAIN(tst_QStringBuilder) + +#include "tst_qstringbuilder.moc" diff --git a/tests/auto/qstyle/tst_qstyle.cpp b/tests/auto/qstyle/tst_qstyle.cpp index 38e690a..f5e94e3 100644 --- a/tests/auto/qstyle/tst_qstyle.cpp +++ b/tests/auto/qstyle/tst_qstyle.cpp @@ -58,6 +58,7 @@ #include <qcdestyle.h> #include <qmotifstyle.h> #include <qcommonstyle.h> +#include <qproxystyle.h> #include <qstylefactory.h> #include <qimagereader.h> @@ -140,6 +141,7 @@ private slots: void testWindowsMobileStyle(); void testS60Style(); void testStyleFactory(); + void testProxyStyle(); void pixelMetric(); void progressBarChangeStyle(); void defaultFont(); @@ -222,6 +224,44 @@ void tst_QStyle::testStyleFactory() } } +class CustomProxy : public QProxyStyle +{ + virtual int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, + const QWidget *widget = 0) const + { + if (metric == QStyle::PM_ButtonIconSize) + return 13; + return QProxyStyle::pixelMetric(metric, option, widget); + } +}; + +void tst_QStyle::testProxyStyle() +{ + QProxyStyle *proxyStyle = new QProxyStyle(); + QVERIFY(proxyStyle->baseStyle()); + QStyle *style = new QWindowsStyle; + QVERIFY(style->proxy() == style); + + proxyStyle->setBaseStyle(style); + QVERIFY(style->proxy() == proxyStyle); + QVERIFY(style->parent() == proxyStyle); + QVERIFY(proxyStyle->baseStyle() == style); + + testAllFunctions(proxyStyle); + proxyStyle->setBaseStyle(0); + QVERIFY(proxyStyle->baseStyle()); + qApp->setStyle(proxyStyle); + + QProxyStyle doubleProxy(new QProxyStyle(new QWindowsStyle())); + testAllFunctions(&doubleProxy); + + CustomProxy customStyle; + QLineEdit edit; + edit.setStyle(&customStyle); + QVERIFY(!customStyle.parent()); + QVERIFY(edit.style()->pixelMetric(QStyle::PM_ButtonIconSize) == 13); +} + void tst_QStyle::drawItemPixmap() { testWidget->resize(300, 300); @@ -320,7 +360,7 @@ void tst_QStyle::testAllFunctions(QStyle *style) testScrollBarSubControls(style); } -void tst_QStyle::testScrollBarSubControls(QStyle *style) +void tst_QStyle::testScrollBarSubControls(QStyle *) { #ifdef Q_OS_WINCE_WM if (qobject_cast<QWindowsMobileStyle*>(style) && qt_wince_is_smartphone()) diff --git a/tests/auto/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/qstylesheetstyle/tst_qstylesheetstyle.cpp index 9917394..22b0f66 100644 --- a/tests/auto/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -81,6 +81,7 @@ private slots: void onWidgetDestroyed(); void fontPrecedence(); void focusColors(); + void hoverColors(); void background(); void tabAlignement(); void attributesList(); @@ -760,6 +761,7 @@ void tst_QStyleSheetStyle::focusColors() combobox->setEditable(true); combobox->addItems(QStringList() << "TESTING"); widgets << combobox; + widgets << new QLabel("TESTING"); #ifdef Q_WS_QWS // QWS has its own special focus logic which is slightly different @@ -809,6 +811,56 @@ void tst_QStyleSheetStyle::focusColors() // } } + +void tst_QStyleSheetStyle::hoverColors() +{ + QList<QWidget *> widgets; + widgets << new QPushButton("TESTING"); + widgets << new QLineEdit("TESTING"); + widgets << new QLabel("TESTING"); + QSpinBox *spinbox = new QSpinBox; + spinbox->setValue(8888); + widgets << spinbox; + QComboBox *combobox = new QComboBox; + combobox->setEditable(true); + combobox->addItems(QStringList() << "TESTING"); + widgets << combobox; + widgets << new QLabel("<b>TESTING</b>"); + + foreach (QWidget *widget, widgets) { + QDialog frame; + QLayout* layout = new QGridLayout; + + QLineEdit* dummy = new QLineEdit; + + widget->setStyleSheet("*:hover { border:none; background: #e8ff66; color: #ff0084 }"); + + layout->addWidget(dummy); + layout->addWidget(widget); + frame.setLayout(layout); + + frame.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&frame); +#endif + QApplication::setActiveWindow(&frame); + QTest::qWait(60); + QTest::mouseMove ( widget, QPoint(5,5)); + QTest::qWait(60); + + QImage image(frame.width(), frame.height(), QImage::Format_ARGB32); + frame.render(&image); + + QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)), + (QString::fromLatin1(widget->metaObject()->className()) + + " did not contain background color #e8ff66").toLocal8Bit().constData()); + QVERIFY2(testForColors(image, QColor(0xff, 0x00, 0x84)), + (QString::fromLatin1(widget->metaObject()->className()) + + " did not contain text color #ff0084").toLocal8Bit().constData()); + } + +} + class SingleInheritanceDialog : public QDialog { Q_OBJECT @@ -1378,8 +1430,6 @@ void tst_QStyleSheetStyle::task188195_baseBackground() QVERIFY(!testForColors(image, QColor(0xab, 0x12, 0x51))); } - - QTEST_MAIN(tst_QStyleSheetStyle) #include "tst_qstylesheetstyle.moc" diff --git a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp index e8cb089..f11aff9 100644 --- a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp +++ b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp @@ -75,7 +75,11 @@ private slots: void stylePropagation() const; void matrixForElement() const; void gradientStops() const; + void gradientRefs(); void fillRule(); + void opacity(); + void paths(); + void displayMode(); #ifndef QT_NO_COMPRESS void testGzLoading(); @@ -331,7 +335,7 @@ void tst_QSvgRenderer::nestedQXmlStreamReader() const QCOMPARE(reader.readNext(), QXmlStreamReader::StartDocument); QCOMPARE(reader.readNext(), QXmlStreamReader::StartElement); QCOMPARE(reader.name().toString(), QLatin1String("bar")); - + QPicture picture; QPainter painter(&picture); QSvgRenderer renderer(&reader); @@ -533,7 +537,10 @@ void tst_QSvgRenderer::gradientStops() const QCOMPARE(image, refImage); } +} +void tst_QSvgRenderer::gradientRefs() +{ const char *svgs[] = { "<svg>" "<defs>" @@ -565,6 +572,17 @@ void tst_QSvgRenderer::gradientStops() const "</linearGradient>" "</defs>" "<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" + "</svg>", + "<svg>" + "<rect fill=\"url(#gradient)\" height=\"8\" width=\"256\" x=\"0\" y=\"0\"/>" + "<defs>" + "<linearGradient id=\"gradient0\">" + "<stop offset=\"0\" stop-color=\"red\" stop-opacity=\"0\"/>" + "<stop offset=\"1\" stop-color=\"blue\"/>" + "</linearGradient>" + "<linearGradient id=\"gradient\" xlink:href=\"#gradient0\">" + "</linearGradient>" + "</defs>" "</svg>" }; for (int i = 0 ; i < sizeof(svgs) / sizeof(svgs[0]) ; ++i) @@ -589,6 +607,7 @@ void tst_QSvgRenderer::gradientStops() const } } + #ifndef QT_NO_COMPRESS void tst_QSvgRenderer::testGzLoading() { @@ -653,22 +672,257 @@ void tst_QSvgRenderer::testGzHelper() void tst_QSvgRenderer::fillRule() { - QByteArray data( + static const char *svgs[] = { + // Paths + // Default fill-rule (nonzero) "<svg>" - "<rect x=\"0\" y=\"0\" height=\"300\" width=\"400\" fill=\"blue\" />" - "<path d=\"M100 200 L300 200 L300 100 L100 100 M100 200 L300 200 L300 100 L100 100 Z\" fill=\"red\" stroke=\"black\" />" - "</svg>"); + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" />" + "</svg>", + // nonzero + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"nonzero\" />" + "</svg>", + // evenodd + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <path d=\"M10 0 L30 0 L30 30 L0 30 L0 10 L20 10 L20 20 L10 20 Z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", - QSvgRenderer renderer(data); + // Polygons + // Default fill-rule (nonzero) + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" />" + "</svg>", + // nonzero + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"nonzero\" />" + "</svg>", + // evenodd + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"30\" width=\"30\" fill=\"blue\" />" + " <polygon points=\"10 0 30 0 30 30 0 30 0 10 20 10 20 20 10 20\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>" + }; - QImage image(128, 128, QImage::Format_ARGB32_Premultiplied); - image.fill(0); + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage refImageNonZero(30, 30, QImage::Format_ARGB32_Premultiplied); + QImage refImageEvenOdd(30, 30, QImage::Format_ARGB32_Premultiplied); + refImageNonZero.fill(0xffff0000); + refImageEvenOdd.fill(0xffff0000); + QPainter p; + p.begin(&refImageNonZero); + p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); + p.end(); + p.begin(&refImageEvenOdd); + p.fillRect(QRectF(0, 0, 10, 10), Qt::blue); + p.fillRect(QRectF(10, 10, 10, 10), Qt::blue); + p.end(); + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QImage image(30, 30, QImage::Format_ARGB32_Premultiplied); + image.fill(0); + p.begin(&image); + renderer.render(&p); + p.end(); + QCOMPARE(image, i % 3 == 2 ? refImageEvenOdd : refImageNonZero); + } +} +static void opacity_drawSvgAndVerify(const QByteArray &data) +{ + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); + image.fill(0xffff00ff); QPainter painter(&image); renderer.render(&painter); painter.end(); + QCOMPARE(image.pixel(5, 5), 0xff7f7f7f); +} + +void tst_QSvgRenderer::opacity() +{ + static const char *opacities[] = {"-1,4641", "0", "0.5", "1", "1.337"}; + static const char *firstColors[] = {"#7f7f7f", "#7f7f7f", "#402051", "blue", "#123456"}; + static const char *secondColors[] = {"red", "#bad", "#bedead", "#7f7f7f", "#7f7f7f"}; + + // Fill-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(firstColors[i]); + data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(secondColors[i]); + data.append("\" fill-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // Stroke-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg viewBox=\"0 0 10 10\"><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); + data.append(firstColors[i]); + data.append("\" stroke-width=\"10\"/><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\""); + data.append(secondColors[i]); + data.append("\" stroke-width=\"10\" stroke-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // As gradients: + // Fill-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg><defs><linearGradient id=\"gradient\"><stop offset=\"0\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/><stop offset=\"1\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/></linearGradient></defs><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\""); + data.append(firstColors[i]); + data.append("\"/><rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"url(#gradient)\" fill-opacity=\""); + data.append(opacities[i]); + data.append("\"/></svg>"); + opacity_drawSvgAndVerify(data); + } + // When support for gradients on strokes has been implemented, add the code below. + /* + // Stroke-opacity + for (int i = 0; i < 5; ++i) { + QByteArray data("<svg viewBox=\"0 0 10 10\"><defs><linearGradient id=\"grad\"><stop offset=\"0\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/><stop offset=\"1\" stop-color=\""); + data.append(secondColors[i]); + data.append("\"/></linearGradient></defs><polyline points=\"0 5 10 5\" fill=\"none\" stroke=\""); + data.append(firstColors[i]); + data.append("\" stroke-width=\"10\" /><line x1=\"5\" y1=\"0\" x2=\"5\" y2=\"10\" fill=\"none\" stroke=\"url(#grad)\" stroke-width=\"10\" stroke-opacity=\""); + data.append(opacities[i]); + data.append("\" /></svg>"); + opacity_drawSvgAndVerify(data); + } + */ +} + +void tst_QSvgRenderer::paths() +{ + static const char *svgs[] = { + // Absolute coordinates, explicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"M50 0 V50 H0 Q0 25 25 25 T50 0 C25 0 50 50 25 50 S25 0 0 0 Z\" fill=\"red\" fill-rule=\"evenodd\"/>" + "</svg>", + // Absolute coordinates, implicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"M50 0 50 50 0 50 Q0 25 25 25 Q50 25 50 0 C25 0 50 50 25 50 C0 50 25 0 0 0 Z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Relative coordinates, explicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0 v50 h-50 q0 -25 25 -25 t25 -25 c-25 0 0 50 -25 50 s0 -50 -25 -50 z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Relative coordinates, implicit commands. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0 0 50 -50 0 q0 -25 25 -25 25 0 25 -25 c-25 0 0 50 -25 50 -25 0 0 -50 -25 -50 z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Absolute coordinates, explicit commands, minimal whitespace. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\"m50 0v50h-50q0-25 25-25t25-25c-25 0 0 50-25 50s0-50-25-50z\" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>", + // Absolute coordinates, explicit commands, extra whitespace. + "<svg>" + " <rect x=\"0\" y=\"0\" height=\"50\" width=\"50\" fill=\"blue\" />" + " <path d=\" M 50 0 V 50 H 0 Q 0 25 25 25 T 50 0 C 25 0 50 50 25 50 S 25 0 0 0 Z \" fill=\"red\" fill-rule=\"evenodd\" />" + "</svg>" + }; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QImage images[COUNT]; + QPainter p; + + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + images[i] = QImage(50, 50, QImage::Format_ARGB32_Premultiplied); + images[i].fill(0); + p.begin(&images[i]); + renderer.render(&p); + p.end(); + if (i != 0) { + QCOMPARE(images[i], images[0]); + } + } +} + +void tst_QSvgRenderer::displayMode() +{ + static const char *svgs[] = { + // All visible. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display svg element. + "<svg display=\"none\">" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display g element. + "<svg>" + " <g display=\"none\">" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display first rect element. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"none\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" />" + " </g>" + "</svg>", + // Don't display second rect element. + "<svg>" + " <g>" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"none\" />" + " </g>" + "</svg>", + // Don't display svg element, but set display mode to "inline" for other elements. + "<svg display=\"none\">" + " <g display=\"inline\">" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"red\" display=\"inline\" />" + " <rect x=\"0\" y=\"0\" height=\"10\" width=\"10\" fill=\"blue\" display=\"inline\" />" + " </g>" + "</svg>" + }; + + QRgb expectedColors[] = {0xff0000ff, 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00}; + + const int COUNT = sizeof(svgs) / sizeof(svgs[0]); + QPainter p; - QCOMPARE(image.pixel(64, 64), QColor(Qt::red).rgba()); + for (int i = 0; i < COUNT; ++i) { + QByteArray data(svgs[i]); + QSvgRenderer renderer(data); + QVERIFY(renderer.isValid()); + QImage image(10, 10, QImage::Format_ARGB32_Premultiplied); + image.fill(0xff00ff00); + p.begin(&image); + renderer.render(&p); + p.end(); + QCOMPARE(image.pixel(5, 5), expectedColors[i]); + } } QTEST_MAIN(tst_QSvgRenderer) diff --git a/tests/auto/qtableview/tst_qtableview.cpp b/tests/auto/qtableview/tst_qtableview.cpp index 8eeaae2..ae023ba 100644 --- a/tests/auto/qtableview/tst_qtableview.cpp +++ b/tests/auto/qtableview/tst_qtableview.cpp @@ -2540,6 +2540,7 @@ void tst_QTableView::span_data() << 2 << 1 << false; + /* This makes no sens. QTest::newRow("top left 2x0") << 10 << 10 << -1 << -1 @@ -2554,7 +2555,7 @@ void tst_QTableView::span_data() << 0 << 0 << 0 << 2 << 0 << 2 - << false; + << false;*/ QTest::newRow("invalid 2x2") << 10 << 10 @@ -2628,7 +2629,7 @@ void tst_QTableView::span() view.hideRow(hiddenRow); view.hideColumn(hiddenColumn); view.show(); - + QCOMPARE(view.rowSpan(row, column), expectedRowSpan); QCOMPARE(view.columnSpan(row, column), expectedColumnSpan); diff --git a/tests/auto/qtextcodec/test/test.pro b/tests/auto/qtextcodec/test/test.pro index 69cd1dd..7d4fdb3 100644 --- a/tests/auto/qtextcodec/test/test.pro +++ b/tests/auto/qtextcodec/test/test.pro @@ -6,7 +6,7 @@ wince*|symbian { addFiles.path = . DEPLOYMENT += addFiles DEPLOYMENT_PLUGIN += qcncodecs qjpcodecs qkrcodecs qtwcodecs + DEFINES += SRCDIR=\\\"\\\" +} else { + DEFINES += SRCDIR=\\\"$$PWD/../\\\" } - - - diff --git a/tests/auto/qtextcodec/tst_qtextcodec.cpp b/tests/auto/qtextcodec/tst_qtextcodec.cpp index 7b80061..750b4ec 100644 --- a/tests/auto/qtextcodec/tst_qtextcodec.cpp +++ b/tests/auto/qtextcodec/tst_qtextcodec.cpp @@ -75,11 +75,17 @@ private slots: void utf8Codec_data(); void utf8Codec(); + void utf8bom_data(); + void utf8bom(); + void utfHeaders_data(); void utfHeaders(); void codecForHtml(); + void codecForUtfText_data(); + void codecForUtfText(); + #ifdef Q_OS_UNIX void toLocal8Bit(); #endif @@ -90,8 +96,8 @@ void tst_QTextCodec::toUnicode_data() QTest::addColumn<QString>("fileName"); QTest::addColumn<QString>("codecName"); - QTest::newRow( "korean-eucKR" ) << "korean.txt" << "eucKR"; - QTest::newRow( "UTF-8" ) << "utf8.txt" << "UTF-8"; + QTest::newRow( "korean-eucKR" ) << SRCDIR "korean.txt" << "eucKR"; + QTest::newRow( "UTF-8" ) << SRCDIR "utf8.txt" << "UTF-8"; } void tst_QTextCodec::toUnicode() @@ -232,7 +238,7 @@ void tst_QTextCodec::fromUnicode() void tst_QTextCodec::toUnicode_codecForHtml() { - QFile file(QString("QT4-crashtest.txt")); + QFile file(QString(SRCDIR "QT4-crashtest.txt")); QVERIFY(file.open(QFile::ReadOnly)); QByteArray data = file.readAll(); @@ -1510,6 +1516,63 @@ void tst_QTextCodec::utf8Codec() QCOMPARE(str, res); } +void tst_QTextCodec::utf8bom_data() +{ + QTest::addColumn<QByteArray>("data"); + QTest::addColumn<QString>("result"); + + QTest::newRow("nobom") + << QByteArray("\302\240", 2) + << QString("\240"); + + { + static const ushort data[] = { 0x201d }; + QTest::newRow("nobom 2") + << QByteArray("\342\200\235", 3) + << QString::fromUtf16(data, sizeof(data)/sizeof(short)); + } + + { + static const ushort data[] = { 0xf000 }; + QTest::newRow("bom1") + << QByteArray("\357\200\200", 3) + << QString::fromUtf16(data, sizeof(data)/sizeof(short)); + } + + { + static const ushort data[] = { 0xfec0 }; + QTest::newRow("bom2") + << QByteArray("\357\273\200", 3) + << QString::fromUtf16(data, sizeof(data)/sizeof(short)); + } + + { + QTest::newRow("normal-bom") + << QByteArray("\357\273\277a", 4) + << QString("a"); + } + + { + static const ushort data[] = { 0x61, 0xfeff, 0x62 }; + QTest::newRow("middle-bom") + << QByteArray("a\357\273\277b", 5) + << QString::fromUtf16(data, sizeof(data)/sizeof(short)); + } +} + +void tst_QTextCodec::utf8bom() +{ + QFETCH(QByteArray, data); + QFETCH(QString, result); + + QTextCodec *const codec = QTextCodec::codecForMib(106); // UTF-8 + Q_ASSERT(codec); + + QCOMPARE(codec->toUnicode(data.constData(), data.length(), 0), result); + + QTextCodec::ConverterState state; + QCOMPARE(codec->toUnicode(data.constData(), data.length(), &state), result); +} void tst_QTextCodec::utfHeaders_data() { @@ -1754,6 +1817,62 @@ void tst_QTextCodec::codecForHtml() QCOMPARE(QTextCodec::codecForHtml(html, QTextCodec::codecForMib(106))->mibEnum(), 111); // latin 15 } +void tst_QTextCodec::codecForUtfText_data() +{ + QTest::addColumn<QByteArray>("encoded"); + QTest::addColumn<bool>("detected"); + QTest::addColumn<int>("mib"); + + + QTest::newRow("utf8 bom") + << QByteArray("\xef\xbb\xbfhello") + << true + << 106; + QTest::newRow("utf8 nobom") + << QByteArray("hello") + << false + << 0; + + QTest::newRow("utf16 bom be") + << QByteArray("\xfe\xff\0h\0e\0l", 8) + << true + << 1013; + QTest::newRow("utf16 bom le") + << QByteArray("\xff\xfeh\0e\0l\0", 8) + << true + << 1014; + QTest::newRow("utf16 nobom") + << QByteArray("\0h\0e\0l", 6) + << false + << 0; + + QTest::newRow("utf32 bom be") + << QByteArray("\0\0\xfe\xff\0\0\0h\0\0\0e\0\0\0l", 16) + << true + << 1018; + QTest::newRow("utf32 bom le") + << QByteArray("\xff\xfe\0\0h\0\0\0e\0\0\0l\0\0\0", 16) + << true + << 1019; + QTest::newRow("utf32 nobom") + << QByteArray("\0\0\0h\0\0\0e\0\0\0l", 12) + << false + << 0; +} + +void tst_QTextCodec::codecForUtfText() +{ + QFETCH(QByteArray, encoded); + QFETCH(bool, detected); + QFETCH(int, mib); + + QTextCodec *codec = QTextCodec::codecForUtfText(encoded, 0); + if (detected) + QCOMPARE(codec->mibEnum(), mib); + else + QVERIFY(codec == 0); +} + #ifdef Q_OS_UNIX void tst_QTextCodec::toLocal8Bit() { diff --git a/tests/auto/qtextdocument/tst_qtextdocument.cpp b/tests/auto/qtextdocument/tst_qtextdocument.cpp index 5eded52..eea3ff0 100644 --- a/tests/auto/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/qtextdocument/tst_qtextdocument.cpp @@ -161,6 +161,10 @@ private slots: void testUndoCommandAdded(); + void testUndoBlocks(); + + void receiveCursorPositionChangedAfterContentsChange(); + private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); @@ -2435,5 +2439,51 @@ void tst_QTextDocument::testUndoCommandAdded() QCOMPARE(spy.count(), 1); } +void tst_QTextDocument::testUndoBlocks() +{ + QVERIFY(doc); + cursor.insertText("Hello World"); + cursor.insertText("period"); + doc->undo(); + QCOMPARE(doc->toPlainText(), QString("")); + cursor.insertText("Hello World"); + cursor.insertText("One\nTwo\nThree"); + QCOMPARE(doc->toPlainText(), QString("Hello WorldOne\nTwo\nThree")); + doc->undo(); + QCOMPARE(doc->toPlainText(), QString("Hello World")); + doc->undo(); + QCOMPARE(doc->toPlainText(), QString("")); +} + +class Receiver : public QObject +{ + Q_OBJECT + public: + QString first; + public slots: + void cursorPositionChanged() { + if (first.isEmpty()) + first = QLatin1String("cursorPositionChanged"); + } + + void contentsChange() { + if (first.isEmpty()) + first = QLatin1String("contentsChanged"); + } +}; + +void tst_QTextDocument::receiveCursorPositionChangedAfterContentsChange() +{ + QVERIFY(doc); + doc->setDocumentLayout(new MyAbstractTextDocumentLayout(doc)); + Receiver rec; + connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), + &rec, SLOT(cursorPositionChanged())); + connect(doc, SIGNAL(contentsChange(int,int,int)), + &rec, SLOT(contentsChange())); + cursor.insertText("Hello World"); + QCOMPARE(rec.first, QString("contentsChanged")); +} + QTEST_MAIN(tst_QTextDocument) #include "tst_qtextdocument.moc" diff --git a/tests/auto/qtextodfwriter/tst_qtextodfwriter.cpp b/tests/auto/qtextodfwriter/tst_qtextodfwriter.cpp index 0ad4cc5..d784c99 100644 --- a/tests/auto/qtextodfwriter/tst_qtextodfwriter.cpp +++ b/tests/auto/qtextodfwriter/tst_qtextodfwriter.cpp @@ -137,7 +137,7 @@ void tst_QTextOdfWriter::testWriteParagraph_data() QTest::newRow("tab") << "word\ttab x" << "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">word<text:tab/>tab x</text:span></text:p>"; QTest::newRow("tab2") << "word\t\ttab\tx" << - "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">word<text:tab text:tab-ref=\"2\"/>tab<text:tab/>x</text:span></text:p>"; + "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">word<text:tab/><text:tab/>tab<text:tab/>x</text:span></text:p>"; QTest::newRow("misc") << "foobar word\ttab x" << "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">foobar <text:s text:c=\"2\"/>word<text:tab/>tab x</text:span></text:p>"; QTest::newRow("misc2") << "\t \tFoo" << diff --git a/tests/auto/qtextstream/tst_qtextstream.cpp b/tests/auto/qtextstream/tst_qtextstream.cpp index 793ac71..484d7c9 100644 --- a/tests/auto/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/qtextstream/tst_qtextstream.cpp @@ -556,8 +556,15 @@ void tst_QTextStream::generateLineData(bool for_QString) QTest::newRow("threelines/crlf/crlf/crlf") << QByteArray("ole\r\ndole\r\ndoffen\r\n") << (QStringList() << "ole" << "dole" << "doffen"); QTest::newRow("threelines/crlf/crlf/nothing") << QByteArray("ole\r\ndole\r\ndoffen") << (QStringList() << "ole" << "dole" << "doffen"); - // utf-16 if (!for_QString) { + // utf-8 + QTest::newRow("utf8/twolines") + << QByteArray("\xef\xbb\xbf" + "\x66\x67\x65\x0a" + "\x66\x67\x65\x0a", 11) + << (QStringList() << "fge" << "fge"); + + // utf-16 // one line QTest::newRow("utf16-BE/nothing") << QByteArray("\xfe\xff" @@ -597,6 +604,18 @@ void tst_QTextStream::generateLineData(bool for_QString) "\xe5\x00\x67\x00\x65\x00\x0a\x00" "\xe5\x00\x67\x00\x65\x00\x0a\x00", 26) << (QStringList() << "\345ge" << "\345ge" << "\345ge"); + + // utf-32 + QTest::newRow("utf32-BE/twolines") + << QByteArray("\x00\x00\xfe\xff" + "\x00\x00\x00\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a" + "\x00\x00\x00\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a", 36) + << (QStringList() << "\345ge" << "\345ge"); + QTest::newRow("utf32-LE/twolines") + << QByteArray("\xff\xfe\x00\x00" + "\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a\x00\x00\x00" + "\xe5\x00\x00\x00\x67\x00\x00\x00\x65\x00\x00\x00\x0a\x00\x00\x00", 36) + << (QStringList() << "\345ge" << "\345ge"); } // partials diff --git a/tests/auto/qtoolbutton/tst_qtoolbutton.cpp b/tests/auto/qtoolbutton/tst_qtoolbutton.cpp index 3c2406c..9e342ad 100644 --- a/tests/auto/qtoolbutton/tst_qtoolbutton.cpp +++ b/tests/auto/qtoolbutton/tst_qtoolbutton.cpp @@ -65,6 +65,7 @@ private slots: void getSetCheck(); void triggered(); void task230994_iconSize(); + void task176137_autoRepeatOfAction(); protected slots: void sendMouseClick(); @@ -179,6 +180,25 @@ void tst_QToolButton::task230994_iconSize() QVERIFY(option.iconSize.isValid()); } +void tst_QToolButton::task176137_autoRepeatOfAction() +{ + QAction action(0); + QToolButton tb; + tb.setDefaultAction (&action); + tb.show(); + QSignalSpy spy(&action,SIGNAL(triggered())); + QTest::mousePress ( &tb, Qt::LeftButton); + QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 2000); + QCOMPARE(spy.count(),1); + + // try again with auto repeat + tb.setAutoRepeat (true); + QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy + QTest::mousePress ( &tb, Qt::LeftButton); + QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 2000); + QCOMPARE (repeatSpy.count(), (2000 - tb.autoRepeatDelay()) / tb.autoRepeatInterval() + 1); +} + void tst_QToolButton::sendMouseClick() { diff --git a/tests/auto/qtouchevent/qtouchevent.pro b/tests/auto/qtouchevent/qtouchevent.pro new file mode 100644 index 0000000..8f6aa87 --- /dev/null +++ b/tests/auto/qtouchevent/qtouchevent.pro @@ -0,0 +1,3 @@ +SOURCES=tst_qtouchevent.cpp +TARGET=tst_qtouchevent +QT+=testlib diff --git a/tests/auto/qtouchevent/tst_qtouchevent.cpp b/tests/auto/qtouchevent/tst_qtouchevent.cpp new file mode 100644 index 0000000..0d584cd --- /dev/null +++ b/tests/auto/qtouchevent/tst_qtouchevent.cpp @@ -0,0 +1,798 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the $MODULE$ of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtTest> + +class tst_QTouchEventWidget : public QWidget +{ +public: + QList<QTouchEvent::TouchPoint> touchBeginPoints, touchUpdatePoints, touchEndPoints; + bool seenTouchBegin, seenTouchUpdate, seenTouchEnd; + bool acceptTouchBegin, acceptTouchUpdate, acceptTouchEnd; + + tst_QTouchEventWidget() + : QWidget() + { + reset(); + } + + void reset() + { + touchBeginPoints.clear(); + touchUpdatePoints.clear(); + touchEndPoints.clear(); + seenTouchBegin = seenTouchUpdate = seenTouchEnd = false; + acceptTouchBegin = acceptTouchUpdate = acceptTouchEnd = true; + } + + bool event(QEvent *event) + { + switch (event->type()) { + case QEvent::TouchBegin: + if (seenTouchBegin) qWarning("TouchBegin: already seen a TouchBegin"); + if (seenTouchUpdate) qWarning("TouchBegin: TouchUpdate cannot happen before TouchBegin"); + if (seenTouchEnd) qWarning("TouchBegin: TouchEnd cannot happen before TouchBegin"); + seenTouchBegin = !seenTouchBegin && !seenTouchUpdate && !seenTouchEnd; + touchBeginPoints = static_cast<QTouchEvent *>(event)->touchPoints(); + event->setAccepted(acceptTouchBegin); + break; + case QEvent::TouchUpdate: + if (!seenTouchBegin) qWarning("TouchUpdate: have not seen TouchBegin"); + if (seenTouchEnd) qWarning("TouchUpdate: TouchEnd cannot happen before TouchUpdate"); + seenTouchUpdate = seenTouchBegin && !seenTouchEnd; + touchUpdatePoints = static_cast<QTouchEvent *>(event)->touchPoints(); + event->setAccepted(acceptTouchUpdate); + break; + case QEvent::TouchEnd: + if (!seenTouchBegin) qWarning("TouchEnd: have not seen TouchBegin"); + if (seenTouchEnd) qWarning("TouchEnd: already seen a TouchEnd"); + seenTouchEnd = seenTouchBegin && !seenTouchEnd; + touchEndPoints = static_cast<QTouchEvent *>(event)->touchPoints(); + event->setAccepted(acceptTouchEnd); + break; + default: + return QWidget::event(event); + } + return true; + } +}; + +class tst_QTouchEvent : public QObject +{ + Q_OBJECT +public: + tst_QTouchEvent() { } + ~tst_QTouchEvent() { } + +private slots: + void touchDisabledByDefault(); + void touchEventAcceptedByDefault(); + void touchBeginPropagatesWhenIgnored(); + void touchUpdateAndEndNeverPropagate(); + void basicRawEventTranslation(); + void multiPointRawEventTranslationOnTouchScreen(); + void multiPointRawEventTranslationOnTouchPad(); +}; + +void tst_QTouchEvent::touchDisabledByDefault() +{ + // the widget attribute is not enabled by default + QWidget widget; + QVERIFY(!widget.testAttribute(Qt::WA_AcceptTouchEvents)); + + // events should not be accepted since they are not enabled + QList<QTouchEvent::TouchPoint> touchPoints; + touchPoints.append(QTouchEvent::TouchPoint(0)); + QTouchEvent touchEvent(QEvent::TouchBegin, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointPressed, + touchPoints); + bool res = QApplication::sendEvent(&widget, &touchEvent); + QVERIFY(!res); + QVERIFY(!touchEvent.isAccepted()); +} + +void tst_QTouchEvent::touchEventAcceptedByDefault() +{ + // enabling touch events should automatically accept touch events + QWidget widget; + widget.setAttribute(Qt::WA_AcceptTouchEvents); + + // QWidget handles touch event by converting them into a mouse event, so the event is both + // accepted and handled (res == true) + QList<QTouchEvent::TouchPoint> touchPoints; + touchPoints.append(QTouchEvent::TouchPoint(0)); + QTouchEvent touchEvent(QEvent::TouchBegin, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointPressed, + touchPoints); + bool res = QApplication::sendEvent(&widget, &touchEvent); + QVERIFY(res); + QVERIFY(touchEvent.isAccepted()); + + // tst_QTouchEventWidget does handle, sending succeeds + tst_QTouchEventWidget touchWidget; + touchWidget.setAttribute(Qt::WA_AcceptTouchEvents); + touchEvent.ignore(); + res = QApplication::sendEvent(&touchWidget, &touchEvent); + QVERIFY(res); + QVERIFY(touchEvent.isAccepted()); +} + +void tst_QTouchEvent::touchBeginPropagatesWhenIgnored() +{ + tst_QTouchEventWidget window, child, grandchild; + child.setParent(&window); + grandchild.setParent(&child); + + // all widgets accept touch events, grandchild ignores, so child sees the event, but not window + window.setAttribute(Qt::WA_AcceptTouchEvents); + child.setAttribute(Qt::WA_AcceptTouchEvents); + grandchild.setAttribute(Qt::WA_AcceptTouchEvents); + grandchild.acceptTouchBegin = false; + + QList<QTouchEvent::TouchPoint> touchPoints; + touchPoints.append(QTouchEvent::TouchPoint(0)); + QTouchEvent touchEvent(QEvent::TouchBegin, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointPressed, + touchPoints); + bool res = QApplication::sendEvent(&grandchild, &touchEvent); + QVERIFY(res); + QVERIFY(touchEvent.isAccepted()); + QVERIFY(grandchild.seenTouchBegin); + QVERIFY(child.seenTouchBegin); + QVERIFY(!window.seenTouchBegin); + + // disable touch on grandchild. even though it doesn't accept it, child should still get the + // TouchBegin + grandchild.reset(); + child.reset(); + window.reset(); + grandchild.setAttribute(Qt::WA_AcceptTouchEvents, false); + + touchEvent.ignore(); + res = QApplication::sendEvent(&grandchild, &touchEvent); + QVERIFY(res); + QVERIFY(touchEvent.isAccepted()); + QVERIFY(!grandchild.seenTouchBegin); + QVERIFY(child.seenTouchBegin); + QVERIFY(!window.seenTouchBegin); +} + +void tst_QTouchEvent::touchUpdateAndEndNeverPropagate() +{ + tst_QTouchEventWidget window, child; + child.setParent(&window); + + window.setAttribute(Qt::WA_AcceptTouchEvents); + child.setAttribute(Qt::WA_AcceptTouchEvents); + child.acceptTouchUpdate = false; + child.acceptTouchEnd = false; + + QList<QTouchEvent::TouchPoint> touchPoints; + touchPoints.append(QTouchEvent::TouchPoint(0)); + QTouchEvent touchBeginEvent(QEvent::TouchBegin, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointPressed, + touchPoints); + bool res = QApplication::sendEvent(&child, &touchBeginEvent); + QVERIFY(res); + QVERIFY(touchBeginEvent.isAccepted()); + QVERIFY(child.seenTouchBegin); + QVERIFY(!window.seenTouchBegin); + + // send the touch update to the child, but ignore it, it doesn't propagate + QTouchEvent touchUpdateEvent(QEvent::TouchUpdate, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointMoved, + touchPoints); + res = QApplication::sendEvent(&child, &touchUpdateEvent); + QVERIFY(res); + QVERIFY(!touchUpdateEvent.isAccepted()); + QVERIFY(child.seenTouchUpdate); + QVERIFY(!window.seenTouchUpdate); + + // send the touch end, same thing should happen as with touch update + QTouchEvent touchEndEvent(QEvent::TouchEnd, + QTouchEvent::TouchScreen, + Qt::NoModifier, + Qt::TouchPointReleased, + touchPoints); + res = QApplication::sendEvent(&child, &touchEndEvent); + QVERIFY(res); + QVERIFY(!touchEndEvent.isAccepted()); + QVERIFY(child.seenTouchEnd); + QVERIFY(!window.seenTouchEnd); +} + +QPointF normalized(const QPointF &pos, const QRectF &rect) +{ + return QPointF(pos.x() / rect.width(), pos.y() / rect.height()); +} + +void tst_QTouchEvent::basicRawEventTranslation() +{ + tst_QTouchEventWidget touchWidget; + touchWidget.setAttribute(Qt::WA_AcceptTouchEvents); + touchWidget.setGeometry(100, 100, 400, 300); + + QPointF pos = touchWidget.rect().center(); + QPointF screenPos = touchWidget.mapToGlobal(pos.toPoint()); + QPointF delta(10, 10); + QRectF screenGeometry = qApp->desktop()->screenGeometry(&touchWidget); + + QTouchEvent::TouchPoint rawTouchPoint; + rawTouchPoint.setId(0); + + // this should be translated to a TouchBegin + rawTouchPoint.setState(Qt::TouchPointPressed); + rawTouchPoint.setScreenPos(screenPos); + rawTouchPoint.setNormalizedPos(normalized(rawTouchPoint.pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, + QTouchEvent::TouchScreen, + QList<QTouchEvent::TouchPoint>() << rawTouchPoint); + QVERIFY(touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QCOMPARE(touchWidget.touchBeginPoints.count(), 1); + QTouchEvent::TouchPoint touchBeginPoint = touchWidget.touchBeginPoints.first(); + QCOMPARE(touchBeginPoint.id(), rawTouchPoint.id()); + QCOMPARE(touchBeginPoint.state(), rawTouchPoint.state()); + QCOMPARE(touchBeginPoint.pos(), pos); + QCOMPARE(touchBeginPoint.startPos(), pos); + QCOMPARE(touchBeginPoint.lastPos(), pos); + QCOMPARE(touchBeginPoint.scenePos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.startScenePos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.lastScenePos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.screenPos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.startScreenPos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.lastScreenPos(), rawTouchPoint.screenPos()); + QCOMPARE(touchBeginPoint.normalizedPos(), rawTouchPoint.normalizedPos()); + QCOMPARE(touchBeginPoint.startNormalizedPos(), touchBeginPoint.normalizedPos()); + QCOMPARE(touchBeginPoint.lastNormalizedPos(), touchBeginPoint.normalizedPos()); + QCOMPARE(touchBeginPoint.rect(), QRectF(pos, QSizeF(0, 0))); + QCOMPARE(touchBeginPoint.screenRect(), QRectF(rawTouchPoint.screenPos(), QSizeF(0, 0))); + QCOMPARE(touchBeginPoint.sceneRect(), touchBeginPoint.screenRect()); + QCOMPARE(touchBeginPoint.pressure(), qreal(1.)); + + // moving the point should translate to TouchUpdate + rawTouchPoint.setState(Qt::TouchPointMoved); + rawTouchPoint.setScreenPos(screenPos + delta); + rawTouchPoint.setNormalizedPos(normalized(rawTouchPoint.pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, + QTouchEvent::TouchScreen, + QList<QTouchEvent::TouchPoint>() << rawTouchPoint); + QVERIFY(touchWidget.seenTouchBegin); + QVERIFY(touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QCOMPARE(touchWidget.touchUpdatePoints.count(), 1); + QTouchEvent::TouchPoint touchUpdatePoint = touchWidget.touchUpdatePoints.first(); + QCOMPARE(touchUpdatePoint.id(), rawTouchPoint.id()); + QCOMPARE(touchUpdatePoint.state(), rawTouchPoint.state()); + QCOMPARE(touchUpdatePoint.pos(), pos + delta); + QCOMPARE(touchUpdatePoint.startPos(), pos); + QCOMPARE(touchUpdatePoint.lastPos(), pos); + QCOMPARE(touchUpdatePoint.scenePos(), rawTouchPoint.screenPos()); + QCOMPARE(touchUpdatePoint.startScenePos(), screenPos); + QCOMPARE(touchUpdatePoint.lastScenePos(), screenPos); + QCOMPARE(touchUpdatePoint.screenPos(), rawTouchPoint.screenPos()); + QCOMPARE(touchUpdatePoint.startScreenPos(), screenPos); + QCOMPARE(touchUpdatePoint.lastScreenPos(), screenPos); + QCOMPARE(touchUpdatePoint.normalizedPos(), rawTouchPoint.normalizedPos()); + QCOMPARE(touchUpdatePoint.startNormalizedPos(), touchBeginPoint.normalizedPos()); + QCOMPARE(touchUpdatePoint.lastNormalizedPos(), touchBeginPoint.normalizedPos()); + QCOMPARE(touchUpdatePoint.rect(), QRectF(pos + delta, QSizeF(0, 0))); + QCOMPARE(touchUpdatePoint.screenRect(), QRectF(rawTouchPoint.screenPos(), QSizeF(0, 0))); + QCOMPARE(touchUpdatePoint.sceneRect(), touchUpdatePoint.screenRect()); + QCOMPARE(touchUpdatePoint.pressure(), qreal(1.)); + + // releasing the point translates to TouchEnd + rawTouchPoint.setState(Qt::TouchPointReleased); + rawTouchPoint.setScreenPos(screenPos + delta + delta); + rawTouchPoint.setNormalizedPos(normalized(rawTouchPoint.pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, + QTouchEvent::TouchScreen, + QList<QTouchEvent::TouchPoint>() << rawTouchPoint); + QVERIFY(touchWidget.seenTouchBegin); + QVERIFY(touchWidget.seenTouchUpdate); + QVERIFY(touchWidget.seenTouchEnd); + QCOMPARE(touchWidget.touchEndPoints.count(), 1); + QTouchEvent::TouchPoint touchEndPoint = touchWidget.touchEndPoints.first(); + QCOMPARE(touchEndPoint.id(), rawTouchPoint.id()); + QCOMPARE(touchEndPoint.state(), rawTouchPoint.state()); + QCOMPARE(touchEndPoint.pos(), pos + delta + delta); + QCOMPARE(touchEndPoint.startPos(), pos); + QCOMPARE(touchEndPoint.lastPos(), pos + delta); + QCOMPARE(touchEndPoint.scenePos(), rawTouchPoint.screenPos()); + QCOMPARE(touchEndPoint.startScenePos(), screenPos); + QCOMPARE(touchEndPoint.lastScenePos(), screenPos + delta); + QCOMPARE(touchEndPoint.screenPos(), rawTouchPoint.screenPos()); + QCOMPARE(touchEndPoint.startScreenPos(), screenPos); + QCOMPARE(touchEndPoint.lastScreenPos(), screenPos + delta); + QCOMPARE(touchEndPoint.normalizedPos(), rawTouchPoint.normalizedPos()); + QCOMPARE(touchEndPoint.startNormalizedPos(), touchBeginPoint.normalizedPos()); + QCOMPARE(touchEndPoint.lastNormalizedPos(), touchUpdatePoint.normalizedPos()); + QCOMPARE(touchEndPoint.rect(), QRectF(pos + delta + delta, QSizeF(0, 0))); + QCOMPARE(touchEndPoint.screenRect(), QRectF(rawTouchPoint.screenPos(), QSizeF(0, 0))); + QCOMPARE(touchEndPoint.sceneRect(), touchEndPoint.screenRect()); + QCOMPARE(touchEndPoint.pressure(), qreal(0.)); +} + +void tst_QTouchEvent::multiPointRawEventTranslationOnTouchScreen() +{ + tst_QTouchEventWidget touchWidget; + touchWidget.setAttribute(Qt::WA_AcceptTouchEvents); + touchWidget.setGeometry(100, 100, 400, 300); + + tst_QTouchEventWidget leftWidget; + leftWidget.setParent(&touchWidget); + leftWidget.setAttribute(Qt::WA_AcceptTouchEvents); + leftWidget.setGeometry(0, 100, 100, 100); + leftWidget.show(); + + tst_QTouchEventWidget rightWidget; + rightWidget.setParent(&touchWidget); + rightWidget.setAttribute(Qt::WA_AcceptTouchEvents); + rightWidget.setGeometry(300, 100, 100, 100); + rightWidget.show(); + + QPointF leftPos = leftWidget.rect().center(); + QPointF rightPos = rightWidget.rect().center(); + QPointF centerPos = touchWidget.rect().center(); + QPointF leftScreenPos = leftWidget.mapToGlobal(leftPos.toPoint()); + QPointF rightScreenPos = rightWidget.mapToGlobal(rightPos.toPoint()); + QPointF centerScreenPos = touchWidget.mapToGlobal(centerPos.toPoint()); + QPointF delta(10, 10); + QRectF screenGeometry = qApp->desktop()->screenGeometry(&touchWidget); + + QList<QTouchEvent::TouchPoint> rawTouchPoints; + rawTouchPoints.append(QTouchEvent::TouchPoint(0)); + rawTouchPoints.append(QTouchEvent::TouchPoint(1)); + + // generate TouchBegins on both leftWidget and rightWidget + rawTouchPoints[0].setState(Qt::TouchPointPressed); + rawTouchPoints[0].setScreenPos(leftScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointPressed); + rawTouchPoints[1].setScreenPos(rightScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(!leftWidget.seenTouchUpdate); + QVERIFY(!leftWidget.seenTouchEnd); + QVERIFY(rightWidget.seenTouchBegin); + QVERIFY(!rightWidget.seenTouchUpdate); + QVERIFY(!rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchBeginPoints.count(), 1); + QCOMPARE(rightWidget.touchBeginPoints.count(), 1); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchBeginPoints.first(); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), leftPos); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftPos); + QCOMPARE(leftTouchPoint.scenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.screenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(leftScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(leftScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(1.)); + + QTouchEvent::TouchPoint rightTouchPoint = rightWidget.touchBeginPoints.first(); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), rightPos); + QCOMPARE(rightTouchPoint.startPos(), rightPos); + QCOMPARE(rightTouchPoint.lastPos(), rightPos); + QCOMPARE(rightTouchPoint.scenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.screenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(rightPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(rightScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(rightScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(1.)); + } + + // generate TouchUpdates on both leftWidget and rightWidget + rawTouchPoints[0].setState(Qt::TouchPointMoved); + rawTouchPoints[0].setScreenPos(centerScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointMoved); + rawTouchPoints[1].setScreenPos(centerScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(leftWidget.seenTouchUpdate); + QVERIFY(!leftWidget.seenTouchEnd); + QVERIFY(rightWidget.seenTouchBegin); + QVERIFY(rightWidget.seenTouchUpdate); + QVERIFY(!rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchUpdatePoints.count(), 1); + QCOMPARE(rightWidget.touchUpdatePoints.count(), 1); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchUpdatePoints.first(); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftPos); + QCOMPARE(leftTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(1.)); + + QTouchEvent::TouchPoint rightTouchPoint = rightWidget.touchUpdatePoints.first(); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), QPointF(rightWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(rightTouchPoint.startPos(), rightPos); + QCOMPARE(rightTouchPoint.lastPos(), rightPos); + QCOMPARE(rightTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(rightWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(1.)); + } + + // generate TouchEnds on both leftWidget and rightWidget + rawTouchPoints[0].setState(Qt::TouchPointReleased); + rawTouchPoints[0].setScreenPos(centerScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointReleased); + rawTouchPoints[1].setScreenPos(centerScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchScreen, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(leftWidget.seenTouchUpdate); + QVERIFY(leftWidget.seenTouchEnd); + QVERIFY(rightWidget.seenTouchBegin); + QVERIFY(rightWidget.seenTouchUpdate); + QVERIFY(rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchEndPoints.count(), 1); + QCOMPARE(rightWidget.touchEndPoints.count(), 1); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchEndPoints.first(); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftTouchPoint.pos()); + QCOMPARE(leftTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftTouchPoint.scenePos()); + QCOMPARE(leftTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftTouchPoint.screenPos()); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(0.)); + + QTouchEvent::TouchPoint rightTouchPoint = rightWidget.touchEndPoints.first(); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), QPointF(rightWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(rightTouchPoint.startPos(), rightPos); + QCOMPARE(rightTouchPoint.lastPos(), rightTouchPoint.pos()); + QCOMPARE(rightTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightTouchPoint.scenePos()); + QCOMPARE(rightTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightTouchPoint.screenPos()); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(rightWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(0.)); + } +} + +void tst_QTouchEvent::multiPointRawEventTranslationOnTouchPad() +{ + tst_QTouchEventWidget touchWidget; + touchWidget.setAttribute(Qt::WA_AcceptTouchEvents); + touchWidget.setGeometry(100, 100, 400, 300); + + tst_QTouchEventWidget leftWidget; + leftWidget.setParent(&touchWidget); + leftWidget.setAttribute(Qt::WA_AcceptTouchEvents); + leftWidget.setGeometry(0, 100, 100, 100); + leftWidget.show(); + + tst_QTouchEventWidget rightWidget; + rightWidget.setParent(&touchWidget); + rightWidget.setAttribute(Qt::WA_AcceptTouchEvents); + rightWidget.setGeometry(300, 100, 100, 100); + rightWidget.show(); + + QPointF leftPos = leftWidget.rect().center(); + QPointF rightPos = rightWidget.rect().center(); + QPointF centerPos = touchWidget.rect().center(); + QPointF leftScreenPos = leftWidget.mapToGlobal(leftPos.toPoint()); + QPointF rightScreenPos = rightWidget.mapToGlobal(rightPos.toPoint()); + QPointF centerScreenPos = touchWidget.mapToGlobal(centerPos.toPoint()); + QPointF delta(10, 10); + QRectF screenGeometry = qApp->desktop()->screenGeometry(&touchWidget); + + QList<QTouchEvent::TouchPoint> rawTouchPoints; + rawTouchPoints.append(QTouchEvent::TouchPoint(0)); + rawTouchPoints.append(QTouchEvent::TouchPoint(1)); + + // generate TouchBegin on leftWidget only + rawTouchPoints[0].setState(Qt::TouchPointPressed); + rawTouchPoints[0].setScreenPos(leftScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointPressed); + rawTouchPoints[1].setScreenPos(rightScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchPad, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(!leftWidget.seenTouchUpdate); + QVERIFY(!leftWidget.seenTouchEnd); + QVERIFY(!rightWidget.seenTouchBegin); + QVERIFY(!rightWidget.seenTouchUpdate); + QVERIFY(!rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchBeginPoints.count(), 2); + QCOMPARE(rightWidget.touchBeginPoints.count(), 0); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchBeginPoints.at(0); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), leftPos); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftPos); + QCOMPARE(leftTouchPoint.scenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.screenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(leftScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(leftScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(1.)); + + QTouchEvent::TouchPoint rightTouchPoint = leftWidget.touchBeginPoints.at(1); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.startPos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.lastPos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.scenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.screenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(rightScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(rightScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(1.)); + } + + // generate TouchUpdate on leftWidget + rawTouchPoints[0].setState(Qt::TouchPointMoved); + rawTouchPoints[0].setScreenPos(centerScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointMoved); + rawTouchPoints[1].setScreenPos(centerScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchPad, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(leftWidget.seenTouchUpdate); + QVERIFY(!leftWidget.seenTouchEnd); + QVERIFY(!rightWidget.seenTouchBegin); + QVERIFY(!rightWidget.seenTouchUpdate); + QVERIFY(!rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchUpdatePoints.count(), 2); + QCOMPARE(rightWidget.touchUpdatePoints.count(), 0); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchUpdatePoints.at(0); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftPos); + QCOMPARE(leftTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(1.)); + + QTouchEvent::TouchPoint rightTouchPoint = leftWidget.touchUpdatePoints.at(1); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(rightTouchPoint.startPos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.lastPos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(1.)); + } + + // generate TouchEnd on leftWidget + rawTouchPoints[0].setState(Qt::TouchPointReleased); + rawTouchPoints[0].setScreenPos(centerScreenPos); + rawTouchPoints[0].setNormalizedPos(normalized(rawTouchPoints[0].pos(), screenGeometry)); + rawTouchPoints[1].setState(Qt::TouchPointReleased); + rawTouchPoints[1].setScreenPos(centerScreenPos); + rawTouchPoints[1].setNormalizedPos(normalized(rawTouchPoints[1].pos(), screenGeometry)); + qt_translateRawTouchEvent(&touchWidget, QTouchEvent::TouchPad, rawTouchPoints); + QVERIFY(!touchWidget.seenTouchBegin); + QVERIFY(!touchWidget.seenTouchUpdate); + QVERIFY(!touchWidget.seenTouchEnd); + QVERIFY(leftWidget.seenTouchBegin); + QVERIFY(leftWidget.seenTouchUpdate); + QVERIFY(leftWidget.seenTouchEnd); + QVERIFY(!rightWidget.seenTouchBegin); + QVERIFY(!rightWidget.seenTouchUpdate); + QVERIFY(!rightWidget.seenTouchEnd); + QCOMPARE(leftWidget.touchEndPoints.count(), 2); + QCOMPARE(rightWidget.touchEndPoints.count(), 0); + { + QTouchEvent::TouchPoint leftTouchPoint = leftWidget.touchEndPoints.at(0); + QCOMPARE(leftTouchPoint.id(), rawTouchPoints[0].id()); + QCOMPARE(leftTouchPoint.state(), rawTouchPoints[0].state()); + QCOMPARE(leftTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(leftTouchPoint.startPos(), leftPos); + QCOMPARE(leftTouchPoint.lastPos(), leftTouchPoint.pos()); + QCOMPARE(leftTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScenePos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScenePos(), leftTouchPoint.scenePos()); + QCOMPARE(leftTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(leftTouchPoint.startScreenPos(), leftScreenPos); + QCOMPARE(leftTouchPoint.lastScreenPos(), leftTouchPoint.screenPos()); + QCOMPARE(leftTouchPoint.normalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.startNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.lastNormalizedPos(), rawTouchPoints[0].normalizedPos()); + QCOMPARE(leftTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(leftTouchPoint.pressure(), qreal(0.)); + + QTouchEvent::TouchPoint rightTouchPoint = leftWidget.touchEndPoints.at(1); + QCOMPARE(rightTouchPoint.id(), rawTouchPoints[1].id()); + QCOMPARE(rightTouchPoint.state(), rawTouchPoints[1].state()); + QCOMPARE(rightTouchPoint.pos(), QPointF(leftWidget.mapFromParent(centerPos.toPoint()))); + QCOMPARE(rightTouchPoint.startPos(), QPointF(leftWidget.mapFromGlobal(rightScreenPos.toPoint()))); + QCOMPARE(rightTouchPoint.lastPos(), rightTouchPoint.pos()); + QCOMPARE(rightTouchPoint.scenePos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScenePos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScenePos(), rightTouchPoint.scenePos()); + QCOMPARE(rightTouchPoint.screenPos(), centerScreenPos); + QCOMPARE(rightTouchPoint.startScreenPos(), rightScreenPos); + QCOMPARE(rightTouchPoint.lastScreenPos(), rightTouchPoint.screenPos()); + QCOMPARE(rightTouchPoint.normalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.startNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.lastNormalizedPos(), rawTouchPoints[1].normalizedPos()); + QCOMPARE(rightTouchPoint.rect(), QRectF(leftWidget.mapFromParent(centerPos.toPoint()), QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.sceneRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.screenRect(), QRectF(centerScreenPos, QSizeF(0, 0))); + QCOMPARE(rightTouchPoint.pressure(), qreal(0.)); + } +} + +QTEST_MAIN(tst_QTouchEvent) + +#include "tst_qtouchevent.moc" diff --git a/tests/auto/qtransform/tst_qtransform.cpp b/tests/auto/qtransform/tst_qtransform.cpp index c603058..4ef68bc 100644 --- a/tests/auto/qtransform/tst_qtransform.cpp +++ b/tests/auto/qtransform/tst_qtransform.cpp @@ -603,6 +603,13 @@ void tst_QTransform::types() 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f); QCOMPARE(m3.type(), QTransform::TxProject); + + QTransform m4; + m4.scale(5, 5); + m4.translate(4, 2); + m4.rotate(45); + + QCOMPARE(m4.type(), QTransform::TxRotate); } @@ -647,7 +654,7 @@ void tst_QTransform::transform() d.rotate(30); e.shear(0.5, 0.5); - QCOMPARE(t, e * d * c * b * a); + QVERIFY(qFuzzyCompare(t, e * d * c * b * a)); } void tst_QTransform::mapEmptyPath() diff --git a/tests/auto/qtreeview/tst_qtreeview.cpp b/tests/auto/qtreeview/tst_qtreeview.cpp index 2745414..65cb27a 100644 --- a/tests/auto/qtreeview/tst_qtreeview.cpp +++ b/tests/auto/qtreeview/tst_qtreeview.cpp @@ -212,8 +212,8 @@ private slots: void indexRowSizeHint(); void addRowsWhileSectionsAreHidden(); - void filterProxyModelCrash(); + void styleOptionViewItem(); // task-specific tests: void task174627_moveLeftToRoot(); @@ -229,6 +229,8 @@ private slots: void task238873_avoidAutoReopening(); void task244304_clickOnDecoration(); void task246536_scrollbarsNotWorking(); + void task250683_wrongSectionSize(); + void task239271_addRowsWithFirstColumnHidden(); void task254234_proxySort(); }; @@ -2037,6 +2039,8 @@ void tst_QTreeView::scrollTo() // view.show(); + view.setVerticalScrollMode(QAbstractItemView::ScrollPerItem); //some styles change that in Polish + view.resize(300, 200); //view.verticalScrollBar()->setValue(0); @@ -2049,6 +2053,7 @@ void tst_QTreeView::scrollTo() QCOMPARE(view.verticalScrollBar()->value(), 5); view.scrollTo(model.index(60, 60, QModelIndex())); + CHECK_VISIBLE(60,60); view.scrollTo(model.index(60, 30, QModelIndex())); CHECK_VISIBLE(60,30); @@ -2857,6 +2862,75 @@ void tst_QTreeView::filterProxyModelCrash() view.repaint(); //used to crash } +void tst_QTreeView::styleOptionViewItem() +{ + class MyDelegate : public QStyledItemDelegate + { + public: + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const + { + QVERIFY(qstyleoption_cast<const QStyleOptionViewItemV4 *>(&option)); + QStyleOptionViewItemV4 opt(option); + initStyleOption(&opt, index); + + QVERIFY(!opt.text.isEmpty()); + QCOMPARE(opt.index, index); + QCOMPARE(!(opt.features & QStyleOptionViewItemV2::Alternate), !(index.row() % 2)); + QCOMPARE(!(opt.features & QStyleOptionViewItemV2::HasCheckIndicator), !opt.text.contains("Checkable")); + + if (opt.text.contains("Beginning")) + QCOMPARE(opt.viewItemPosition, QStyleOptionViewItemV4::Beginning); + + if (opt.text.contains("Middle")) + QCOMPARE(opt.viewItemPosition, QStyleOptionViewItemV4::Middle); + + if (opt.text.contains("End")) + QCOMPARE(opt.viewItemPosition, QStyleOptionViewItemV4::End); + + if (opt.text.contains("OnlyOne")) + QCOMPARE(opt.viewItemPosition, QStyleOptionViewItemV4::OnlyOne); + + if (opt.text.contains("Checked")) + QCOMPARE(opt.checkState, Qt::Checked); + else + QCOMPARE(opt.checkState, Qt::Unchecked); + + QVERIFY(!opt.text.contains("Assert")); + + QStyledItemDelegate::paint(painter, option, index); + count++; + } + mutable int count; + }; + + QTreeView view; + QStandardItemModel model; + view.setModel(&model); + MyDelegate delegate; + view.setItemDelegate(&delegate); + model.appendRow(QList<QStandardItem*>() + << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + model.appendRow(QList<QStandardItem*>() + << new QStandardItem("Beginning") << new QStandardItem("Middle") << new QStandardItem("Middle") << new QStandardItem("End") ); + model.appendRow(QList<QStandardItem*>() + << new QStandardItem("OnlyOne") << new QStandardItem("Assert") << new QStandardItem("Assert") << new QStandardItem("Assert") ); + QStandardItem *checkable = new QStandardItem("Checkable"); + checkable->setCheckable(true); + QStandardItem *checked = new QStandardItem("Checkable Checked"); + checkable->setCheckable(true); + checked->setCheckState(Qt::Checked); + model.appendRow(QList<QStandardItem*>() + << new QStandardItem("Beginning") << checkable << checked << new QStandardItem("End") ); + + view.setFirstColumnSpanned(2, QModelIndex(), true); + view.setAlternatingRowColors(true); + + delegate.count = 0; + view.showMaximized(); + QTest::qWait(30); + QVERIFY(delegate.count >= 13); +} + class task174627_TreeView : public QTreeView { Q_OBJECT @@ -3282,6 +3356,59 @@ void tst_QTreeView::task246536_scrollbarsNotWorking() QVERIFY(o.count > 0); } +void tst_QTreeView::task250683_wrongSectionSize() +{ + QDirModel model; + QTreeView treeView; + treeView.header()->setResizeMode(QHeaderView::ResizeToContents); + treeView.setModel(&model); + treeView.setColumnHidden(2, true); + treeView.setColumnHidden(3, true); + + treeView.show(); + QTest::qWait(100); + + QCOMPARE(treeView.header()->sectionSize(0) + treeView.header()->sectionSize(1), treeView.viewport()->width()); +} + +void tst_QTreeView::task239271_addRowsWithFirstColumnHidden() +{ + class MyDelegate : public QStyledItemDelegate + { + public: + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const + { + paintedIndexes << index; + QStyledItemDelegate::paint(painter, option, index); + } + + mutable QSet<QModelIndex> paintedIndexes; + }; + + QTreeView view; + QStandardItemModel model; + view.setModel(&model); + MyDelegate delegate; + view.setItemDelegate(&delegate); + QStandardItem root0("root0"), root1("root1"); + model.invisibleRootItem()->appendRow(QList<QStandardItem*>() << &root0 << &root1); + QStandardItem sub0("sub0"), sub00("sub00"); + root0.appendRow(QList<QStandardItem*>() << &sub0 << &sub00); + view.expand(root0.index()); + + view.hideColumn(0); + view.show(); + QTest::qWait(200); + delegate.paintedIndexes.clear(); + QStandardItem sub1("sub1"), sub11("sub11"); + root0.appendRow(QList<QStandardItem*>() << &sub1 << &sub11); + + QTest::qWait(200); + //items in the 2nd column should have been painted + QVERIFY(delegate.paintedIndexes.contains(sub00.index())); + QVERIFY(delegate.paintedIndexes.contains(sub11.index())); +} + void tst_QTreeView::task254234_proxySort() { //based on tst_QTreeView::sortByColumn diff --git a/tests/auto/qtreewidget/tst_qtreewidget.cpp b/tests/auto/qtreewidget/tst_qtreewidget.cpp index 1b5f8bc..7d79d56 100644 --- a/tests/auto/qtreewidget/tst_qtreewidget.cpp +++ b/tests/auto/qtreewidget/tst_qtreewidget.cpp @@ -136,6 +136,7 @@ private slots: void rootItemFlags(); void task218661_setHeaderData(); void task245280_sortChildren(); + void task253109_itemHeight(); // QTreeWidgetItem void itemOperatorLessThan(); @@ -2433,6 +2434,10 @@ void tst_QTreeWidget::itemOperatorLessThan() item1.setText(0, "b"); item2.setText(0, "a"); QCOMPARE(item1 < item2, true); + tw.sortItems(0, Qt::AscendingOrder); + item1.setData(0, Qt::DisplayRole, 11); + item2.setData(0, Qt::DisplayRole, 2); + QCOMPARE(item1 < item2, false); } } @@ -2879,6 +2884,25 @@ void tst_QTreeWidget::task245280_sortChildren() QCOMPARE(top.child(i)->text(1), QString::number(i)); } +void tst_QTreeWidget::task253109_itemHeight() +{ + QTreeWidget treeWidget; + treeWidget.setColumnCount(1); + treeWidget.show(); + QTest::qWait(200); + + QTreeWidgetItem item(&treeWidget); + class MyWidget : public QWidget + { + virtual QSize sizeHint() const { return QSize(200,100); } + } w; + treeWidget.setItemWidget(&item, 0, &w); + + QTest::qWait(200); + QCOMPARE(w.geometry(), treeWidget.visualItemRect(&item)); + +} + void tst_QTreeWidget::task206367_duplication() { QTreeWidget treeWidget; diff --git a/tests/auto/qurl/tst_qurl.cpp b/tests/auto/qurl/tst_qurl.cpp index 60d7391..ea551da 100644 --- a/tests/auto/qurl/tst_qurl.cpp +++ b/tests/auto/qurl/tst_qurl.cpp @@ -608,18 +608,6 @@ void tst_QUrl::setUrl() } { - QString bigStr(QLatin1String("abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ")); - QUrl url; - url.setHost(bigStr); - QVERIFY(url.isValid()); - QCOMPARE(url.host().toLower(), bigStr.toLower()); - } - - { QUrl url; url.setUrl("hello.com#?"); QVERIFY(url.isValid()); @@ -1398,8 +1386,6 @@ void tst_QUrl::i18n_data() QTest::newRow("øl") << QString::fromLatin1("http://ole:passord@www.øl.no/index.html?ole=æsemann&ilder gud=hei#top") << QByteArray("http://ole:passord@www.xn--l-4ga.no/index.html?ole=%C3%A6semann&ilder%20gud=hei#top"); - QTest::newRow("øl") << QString::fromLatin1("http://ole:passord@www.øl.no/index.html?ole=æsemann&ilder gud=hei#top") - << QByteArray("http://ole:passord@www.xn--l-4ga.no/index.html?ole=%C3%A6semann&ilder%20gud=hei#top"); QTest::newRow("räksmörgås") << QString::fromLatin1("http://www.räksmörgås.no/") << QByteArray("http://www.xn--rksmrgs-5wao1o.no/"); QTest::newRow("bühler") << QString::fromLatin1("http://www.bühler.no/") @@ -2412,9 +2398,7 @@ void tst_QUrl::hasQueryItem() void tst_QUrl::nameprep() { QUrl url(QString::fromUtf8("http://www.fu""\xc3""\x9f""ball.de/")); - QUrl url2 = QUrl::fromEncoded("http://www.xn--fuball-cta.de/"); - QCOMPARE(url2.toString(), QString::fromLatin1("http://www.fussball.de/")); - QCOMPARE(url.toString(), url2.toString()); // should be identical + QCOMPARE(url.toString(), QString::fromLatin1("http://www.fussball.de/")); } void tst_QUrl::isValid() @@ -2425,19 +2409,18 @@ void tst_QUrl::isValid() QCOMPARE(url.path(), QString("A=B")); } { - QUrl url = QUrl::fromEncoded("http://strange<username>@ok_hostname/", QUrl::StrictMode); + QUrl url = QUrl::fromEncoded("http://strange<username>@ok-hostname/", QUrl::StrictMode); QVERIFY(!url.isValid()); // < and > are not allowed in userinfo in strict mode } { - QUrl url = QUrl::fromEncoded("http://strange<username>@ok_hostname/"); + QUrl url = QUrl::fromEncoded("http://strange<username>@ok-hostname/"); QVERIFY(url.isValid()); // < and > are allowed in tolerant mode } { QUrl url = QUrl::fromEncoded("http://strange;hostname/here"); - QVERIFY(url.isValid()); - QCOMPARE(url.host(), QString("strange;hostname")); + QVERIFY(!url.isValid()); QCOMPARE(url.path(), QString("/here")); } } @@ -3488,7 +3471,6 @@ void tst_QUrl::setAuthority_data() QTest::addColumn<QString>("authority"); QTest::addColumn<QString>("url"); QTest::newRow("Plain auth") << QString("62.70.27.22:21") << QString("//62.70.27.22:21"); - QTest::newRow("Auth with empty port") << QString("62.70.27.22:") << QString("//62.70.27.22:"); QTest::newRow("Yet another plain auth") << QString("192.168.1.1:21") << QString("//192.168.1.1:21"); QTest::newRow("Auth without port") << QString("192.168.1.1") << QString("//192.168.1.1"); QTest::newRow("Auth w/full hostname without port") << QString("shusaku.troll.no") << QString("//shusaku.troll.no"); @@ -3496,7 +3478,6 @@ void tst_QUrl::setAuthority_data() QTest::newRow("Auth w/full hostname that ends with number, without port") << QString("shusaku.troll.no.2") << QString("//shusaku.troll.no.2"); QTest::newRow("Auth w/hostname that ends with number, without port") << QString("shusaku2") << QString("//shusaku2"); QTest::newRow("Empty auth") << QString() << QString(); - QTest::newRow("Single") << QString(":") << QString("//:"); } void tst_QUrl::setAuthority() diff --git a/tests/auto/qvariant/tst_qvariant.cpp b/tests/auto/qvariant/tst_qvariant.cpp index e009ac1..6655021 100644 --- a/tests/auto/qvariant/tst_qvariant.cpp +++ b/tests/auto/qvariant/tst_qvariant.cpp @@ -256,6 +256,7 @@ private slots: void convertByteArrayToBool_data() const; void toIntFromQString() const; void toIntFromDouble() const; + void task256984_setValue(); }; Q_DECLARE_METATYPE(QDate) @@ -441,6 +442,9 @@ void tst_QVariant::canConvert_data() var = QVariant((double)0.1); QTest::newRow("Double") << var << N << N << Y << N << Y << Y << N << N << N << N << N << Y << N << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant(0.1f); + QTest::newRow("Float") + << var << N << N << Y << N << Y << Y << N << N << N << N << N << Y << N << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = qVariantFromValue(QFont()); QTest::newRow("Font") << var << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N; @@ -574,6 +578,7 @@ void tst_QVariant::canConvert() 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); #ifdef QT3_SUPPORT QCOMPARE(val.canConvert(QVariant::IconSet), IconSetCast); @@ -616,6 +621,7 @@ void tst_QVariant::toInt_data() QTest::newRow( "invalid" ) << QVariant() << 0 << false; QTest::newRow( "int" ) << QVariant( 123 ) << 123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << 3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << 3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << 123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << 1 << true; @@ -628,6 +634,7 @@ void tst_QVariant::toInt_data() QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << 0 << true; QTest::newRow( "signedint" ) << QVariant( -123 ) << -123 << true; QTest::newRow( "signeddouble" ) << QVariant( -3.1415927 ) << -3 << true; + QTest::newRow( "signedfloat" ) << QVariant( -3.1415927f ) << -3 << true; QTest::newRow( "signedint-string" ) << QVariant( QString("-123") ) << -123 << true; QTest::newRow( "signedlonglong0" ) << QVariant( (qlonglong)-34 ) << -34 << true; QTest::newRow( "QChar" ) << QVariant(QChar('a')) << int('a') << true; @@ -667,6 +674,7 @@ void tst_QVariant::toUInt_data() QTest::newRow( "int" ) << QVariant( 123 ) << (uint)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (uint)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (uint)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (uint)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (uint)1 << true; @@ -680,6 +688,7 @@ void tst_QVariant::toUInt_data() QTest::newRow( "ulonglong1" ) << QVariant( uintMax1 ) << (uint)0 << true; QTest::newRow( "negativeint" ) << QVariant( -123 ) << (uint)-123 << true; QTest::newRow( "negativedouble" ) << QVariant( -3.1415927 ) << (uint)-3 << true; + QTest::newRow( "negativefloat" ) << QVariant( -3.1415927f ) << (uint)-3 << true; QTest::newRow( "negativeint-string" ) << QVariant( QString("-123") ) << (uint)0 << false; QTest::newRow( "negativelonglong0" ) << QVariant( (qlonglong)-34 ) << (uint)-34 << true; QTest::newRow( "QChar" ) << QVariant(QChar('a')) << uint('a') << true; @@ -861,7 +870,9 @@ void tst_QVariant::toBool_data() QTest::newRow( "uint0" ) << QVariant( 0u ) << false; QTest::newRow( "uint1" ) << QVariant( 123u ) << true; QTest::newRow( "double0" ) << QVariant( 0.0 ) << false; + QTest::newRow( "float0" ) << QVariant( 0.0f ) << false; QTest::newRow( "double1" ) << QVariant( 3.1415927 ) << true; + QTest::newRow( "float1" ) << QVariant( 3.1415927f ) << true; #ifdef QT3_SUPPORT QTest::newRow( "bool0" ) << QVariant( false, 42 ) << false; QTest::newRow( "bool1" ) << QVariant( true, 42 ) << true; @@ -1091,6 +1102,7 @@ void tst_QVariant::toLongLong_data() QTest::newRow( "int0" ) << QVariant( 123 ) << (qlonglong)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qlonglong)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qlonglong)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (qlonglong)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (qlonglong)1 << true; @@ -1131,6 +1143,7 @@ void tst_QVariant::toULongLong_data() QTest::newRow( "int0" ) << QVariant( 123 ) << (qulonglong)123 << true; QTest::newRow( "double" ) << QVariant( 3.1415927 ) << (qulonglong)3 << true; + QTest::newRow( "float" ) << QVariant( 3.1415927f ) << (qulonglong)3 << true; QTest::newRow( "uint" ) << QVariant( 123u ) << (qulonglong)123 << true; #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 42 ) << (qulonglong)1 << true; @@ -1219,6 +1232,7 @@ void tst_QVariant::toByteArray_data() QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" ); + QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" ); QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" ); QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" ); } @@ -1244,6 +1258,7 @@ void tst_QVariant::toString_data() QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" ); + QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" ); #ifdef QT3_SUPPORT QTest::newRow( "bool" ) << QVariant( true, 0 ) << QString( "true" ); #else @@ -1451,6 +1466,7 @@ void tst_QVariant::writeToReadFromDataStream_data() QTest::newRow( "datetime_invalid" ) << QVariant( QDateTime() ) << true; QTest::newRow( "datetime_valid" ) << QVariant( QDateTime( QDate( 2002, 07, 06 ), QTime( 14, 0, 0 ) ) ) << false; QTest::newRow( "double_valid" ) << QVariant( 123.456 ) << false; + QTest::newRow( "float_valid" ) << QVariant( 123.456f ) << false; QTest::newRow( "font_valid" ) << qVariantFromValue( QFont( "times", 12 ) ) << false; QTest::newRow( "pixmap_invalid" ) << qVariantFromValue( QPixmap() ) << true; QPixmap pixmap( 10, 10 ); @@ -1477,6 +1493,7 @@ void tst_QVariant::writeToReadFromDataStream_data() vMap.insert( "int", QVariant( 1 ) ); vMap.insert( "string", QVariant( QString("Two") ) ); vMap.insert( "double", QVariant( 3.45 ) ); + vMap.insert( "float", QVariant( 3.45f ) ); QTest::newRow( "map_valid" ) << QVariant( vMap ) << false; QTest::newRow( "palette_valid" ) << qVariantFromValue(QPalette(QColor("turquoise"))) << false; QTest::newRow( "pen_valid" ) << qVariantFromValue( QPen( Qt::red ) ) << false; @@ -1670,6 +1687,10 @@ void tst_QVariant::operator_eq_eq_data() QVariant mDoubleString(QByteArray("42.11")); QVariant mDoubleQString(QString("42.11")); + QVariant mFloat(42.11f); + QVariant mFloatString(QByteArray("42.11")); + QVariant mFloatQString(QString("42.11")); + QVariant mLongLong((qlonglong)-42); QVariant mLongLongString(QByteArray("-42")); QVariant mLongLongQString(QString("-42")); @@ -1687,6 +1708,7 @@ void tst_QVariant::operator_eq_eq_data() QVariant mBoolQString(QString("false")); QTest::newRow( "double_int" ) << QVariant(42.0) << QVariant(42) << true; + QTest::newRow( "float_int" ) << QVariant(42.f) << QVariant(42) << true; QTest::newRow( "mInt_mIntString" ) << mInt << mIntString << true; QTest::newRow( "mIntString_mInt" ) << mIntString << mInt << true; QTest::newRow( "mInt_mIntQString" ) << mInt << mIntQString << true; @@ -1702,6 +1724,11 @@ void tst_QVariant::operator_eq_eq_data() QTest::newRow( "mDouble_mDoubleQString" ) << mDouble << mDoubleQString << true; QTest::newRow( "mDoubleQString_mDouble" ) << mDoubleQString << mDouble << true; + QTest::newRow( "mFloat_mFloatString" ) << mFloat << mFloatString << true; + QTest::newRow( "mFloatString_mFloat" ) << mFloatString << mFloat << true; + QTest::newRow( "mFloat_mFloatQString" ) << mFloat << mFloatQString << true; + QTest::newRow( "mFloatQString_mFloat" ) << mFloatQString << mFloat << true; + QTest::newRow( "mLongLong_mLongLongString" ) << mLongLong << mLongLongString << true; QTest::newRow( "mLongLongString_mLongLong" ) << mLongLongString << mLongLong << true; QTest::newRow( "mLongLong_mLongLongQString" ) << mLongLong << mLongLongQString << true; @@ -1901,6 +1928,7 @@ void tst_QVariant::typeName_data() QTest::newRow("17") << int(QVariant::UInt) << QByteArray("uint"); QTest::newRow("18") << int(QVariant::Bool) << QByteArray("bool"); QTest::newRow("19") << int(QVariant::Double) << QByteArray("double"); + QTest::newRow("20") << int(QMetaType::Float) << QByteArray("float"); QTest::newRow("21") << int(QVariant::Polygon) << QByteArray("QPolygon"); QTest::newRow("22") << int(QVariant::Region) << QByteArray("QRegion"); QTest::newRow("23") << int(QVariant::Bitmap) << QByteArray("QBitmap"); @@ -2234,6 +2262,13 @@ void tst_QVariant::basicUserType() QCOMPARE(v.toDouble(), 4.4); { + float f = 4.5f; + v = QVariant(QMetaType::Float, &f); + } + QCOMPARE(v.userType(), int(QMetaType::Float)); + QCOMPARE(v.toDouble(), 4.5); + + { QByteArray ba("bar"); v = QVariant(QMetaType::QByteArray, &ba); } @@ -2247,6 +2282,7 @@ void tst_QVariant::data_() QVariant i = 1; QVariant d = 1.12; + QVariant f = 1.12f; QVariant ll = (qlonglong)2; QVariant ull = (qulonglong)3; QVariant s(QString("hallo")); @@ -2260,6 +2296,10 @@ void tst_QVariant::data_() QVERIFY(v.data()); QCOMPARE(*static_cast<double *>(v.data()), d.toDouble()); + v = f; + QVERIFY(v.data()); + QCOMPARE(*static_cast<float *>(v.data()), qVariantValue<float>(v)); + v = ll; QVERIFY(v.data()); QCOMPARE(*static_cast<qlonglong *>(v.data()), ll.toLongLong()); @@ -2283,6 +2323,7 @@ void tst_QVariant::constData() int i = 1; double d = 1.12; + float f = 1.12f; qlonglong ll = 2; qulonglong ull = 3; QString s("hallo"); @@ -2296,6 +2337,10 @@ void tst_QVariant::constData() QVERIFY(v.constData()); QCOMPARE(*static_cast<const double *>(v.constData()), d); + v = QVariant(f); + QVERIFY(v.constData()); + QCOMPARE(*static_cast<const float *>(v.constData()), f); + v = QVariant(ll); QVERIFY(v.constData()); QCOMPARE(*static_cast<const qlonglong *>(v.constData()), ll); @@ -2340,6 +2385,7 @@ void tst_QVariant::variant_to() qVariantSetValue(v4, foo); QCOMPARE(qvariant_cast<double>(v1), 4.2); + QCOMPARE(qvariant_cast<float>(v1), 4.2f); QCOMPARE(qvariant_cast<int>(v2), 5); QCOMPARE(qvariant_cast<QStringList>(v3), sl); QCOMPARE(qvariant_cast<QString>(v3), QString::fromLatin1("blah")); @@ -2355,6 +2401,7 @@ void tst_QVariant::variant_to() QCOMPARE(qvariant_cast<int>(n), 42); QCOMPARE(qvariant_cast<uint>(n), 42u); QCOMPARE(qvariant_cast<double>(n), 42.0); + QCOMPARE(qvariant_cast<float>(n), 42.f); QCOMPARE(qvariant_cast<short>(n), short(42)); QCOMPARE(qvariant_cast<ushort>(n), ushort(42)); @@ -2362,6 +2409,7 @@ void tst_QVariant::variant_to() QCOMPARE(qvariant_cast<int>(n), 43); QCOMPARE(qvariant_cast<uint>(n), 43u); QCOMPARE(qvariant_cast<double>(n), 43.0); + QCOMPARE(qvariant_cast<float>(n), 43.f); QCOMPARE(qvariant_cast<long>(n), 43l); n = QLatin1String("44"); @@ -2403,14 +2451,22 @@ void tst_QVariant::saveLoadCustomTypes() void tst_QVariant::url() { - QUrl url("http://www.trolltech.com"); + QString str("http://www.trolltech.com"); + QUrl url(str); - QVariant v(url); + QVariant v(url); //built with a QUrl QVariant v2 = v; - QVERIFY(v2.toUrl() == url); + QVariant v3(str); //built with a QString + QCOMPARE(v2.toUrl(), url); + QVERIFY(qVariantCanConvert<QUrl>(v3)); + QCOMPARE(v2.toUrl(), v3.toUrl()); + + QVERIFY(qVariantCanConvert<QString>(v2)); + QCOMPARE(v2.toString(), str); + QCOMPARE(v3.toString(), str); } void tst_QVariant::globalColor() @@ -2743,6 +2799,9 @@ void tst_QVariant::task172061_invalidDate() const variant = foo; QVERIFY(!variant.convert(QVariant::Double)); + + variant = foo; + QVERIFY(!variant.convert(QVariant::Type(QMetaType::Float))); } struct WontCompare @@ -2935,5 +2994,21 @@ void tst_QVariant::toIntFromDouble() const QCOMPARE(result, 2147483630); } +void tst_QVariant::task256984_setValue() +{ + QTransform t; //we just take a value so that we're sure that it will be shared + QVariant v1 = t; + QVERIFY( v1.isDetached() ); + QVariant v2 = v1; + QVERIFY( !v1.isDetached() ); + QVERIFY( !v2.isDetached() ); + + qVariantSetValue(v2, 3); //set an integer value + + QVERIFY( v1.isDetached() ); + QVERIFY( v2.isDetached() ); +} + + QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc" diff --git a/tests/auto/qwebelement/.gitignore b/tests/auto/qwebelement/.gitignore new file mode 100644 index 0000000..01bbb96 --- /dev/null +++ b/tests/auto/qwebelement/.gitignore @@ -0,0 +1 @@ +tst_qwebpage diff --git a/tests/auto/qwebelement/dummy.cpp b/tests/auto/qwebelement/dummy.cpp new file mode 100644 index 0000000..0c034f3 --- /dev/null +++ b/tests/auto/qwebelement/dummy.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtTest/QtTest> + +QTEST_NOOP_MAIN + diff --git a/tests/auto/qwebelement/qwebelement.pro b/tests/auto/qwebelement/qwebelement.pro new file mode 100644 index 0000000..ac809b8 --- /dev/null +++ b/tests/auto/qwebelement/qwebelement.pro @@ -0,0 +1,14 @@ +load(qttest_p4) +contains(QT_CONFIG,webkit) { + DEFINES += QTEST_WEBKIT + QT += webkit network + isEmpty(QT_BUILD_TREE):QT_BUILD_TREE=$(QTDIR) + isEmpty(QT_SOURCE_TREE):QT_SOURCE_TREE=$$fromfile($$QT_BUILD_TREE/.qmake.cache, QT_SOURCE_TREE) + + TESTDIR = $$QT_SOURCE_TREE/src/3rdparty/webkit/WebKit/qt/tests/qwebelement + VPATH += $$TESTDIR + include($$TESTDIR/qwebelement.pro) +} else { + SOURCES += dummy.cpp +} + diff --git a/tests/auto/qwebhistory/.gitignore b/tests/auto/qwebhistory/.gitignore new file mode 100644 index 0000000..2bdefcd --- /dev/null +++ b/tests/auto/qwebhistory/.gitignore @@ -0,0 +1 @@ +tst_qwebhistory diff --git a/tests/auto/qwebhistory/dummy.cpp b/tests/auto/qwebhistory/dummy.cpp new file mode 100644 index 0000000..0c034f3 --- /dev/null +++ b/tests/auto/qwebhistory/dummy.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtTest/QtTest> + +QTEST_NOOP_MAIN + diff --git a/tests/auto/qwebhistory/qwebhistory.pro b/tests/auto/qwebhistory/qwebhistory.pro new file mode 100644 index 0000000..232f478 --- /dev/null +++ b/tests/auto/qwebhistory/qwebhistory.pro @@ -0,0 +1,14 @@ +load(qttest_p4) +contains(QT_CONFIG,webkit) { + DEFINES += QTEST_WEBKIT + QT += webkit network + isEmpty(QT_BUILD_TREE):QT_BUILD_TREE=$(QTDIR) + isEmpty(QT_SOURCE_TREE):QT_SOURCE_TREE=$$fromfile($$QT_BUILD_TREE/.qmake.cache, QT_SOURCE_TREE) + + TESTDIR = $$QT_SOURCE_TREE/src/3rdparty/webkit/WebKit/qt/tests/qwebhistory + VPATH += $$TESTDIR + include($$TESTDIR/qwebhistory.pro) +} else { + SOURCES += dummy.cpp +} + diff --git a/tests/auto/qwebhistoryinterface/.gitignore b/tests/auto/qwebhistoryinterface/.gitignore new file mode 100644 index 0000000..01bbb96 --- /dev/null +++ b/tests/auto/qwebhistoryinterface/.gitignore @@ -0,0 +1 @@ +tst_qwebpage diff --git a/tests/auto/qwebhistoryinterface/dummy.cpp b/tests/auto/qwebhistoryinterface/dummy.cpp new file mode 100644 index 0000000..0c034f3 --- /dev/null +++ b/tests/auto/qwebhistoryinterface/dummy.cpp @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtTest/QtTest> + +QTEST_NOOP_MAIN + diff --git a/tests/auto/qwebhistoryinterface/qwebhistoryinterface.pro b/tests/auto/qwebhistoryinterface/qwebhistoryinterface.pro new file mode 100644 index 0000000..7218f52 --- /dev/null +++ b/tests/auto/qwebhistoryinterface/qwebhistoryinterface.pro @@ -0,0 +1,14 @@ +load(qttest_p4) +contains(QT_CONFIG,webkit) { + DEFINES += QTEST_WEBKIT + QT += webkit network + isEmpty(QT_BUILD_TREE):QT_BUILD_TREE=$(QTDIR) + isEmpty(QT_SOURCE_TREE):QT_SOURCE_TREE=$$fromfile($$QT_BUILD_TREE/.qmake.cache, QT_SOURCE_TREE) + + TESTDIR = $$QT_SOURCE_TREE/src/3rdparty/webkit/WebKit/qt/tests/qwebhistoryinterface + VPATH += $$TESTDIR + include($$TESTDIR/qwebhistoryinterface.pro) +} else { + SOURCES += dummy.cpp +} + diff --git a/tests/auto/qwidget/tst_qwidget.cpp b/tests/auto/qwidget/tst_qwidget.cpp index abff886..4ddc6c4 100644 --- a/tests/auto/qwidget/tst_qwidget.cpp +++ b/tests/auto/qwidget/tst_qwidget.cpp @@ -296,6 +296,7 @@ private slots: void render_systemClip3_data(); void render_systemClip3(); void render_task252837(); + void render_worldTransform(); void setContentsMargins(); @@ -358,6 +359,8 @@ private slots: void updateOnDestroyedSignal(); void toplevelLineEditFocus(); + void focusWidget_task254563(); + private: bool ensureScreenSize(int width, int height); QWidget *testWidget; @@ -1561,6 +1564,10 @@ void tst_QWidget::focusChainOnReparent() QCOMPARE(w, expectedOriginalChain[i]); w = w->nextInFocusChain(); } + for (int i = 7; i >= 0; --i) { + w = w->previousInFocusChain(); + QCOMPARE(w, expectedOriginalChain[i]); + } QWidget window2; child2->setParent(&window2); @@ -1571,6 +1578,10 @@ void tst_QWidget::focusChainOnReparent() QCOMPARE(w, expectedNewChain[i]); w = w->nextInFocusChain(); } + for (int i = 4; i >= 0; --i) { + w = w->previousInFocusChain(); + QCOMPARE(w, expectedNewChain[i]); + } QWidget *expectedOldChain[5] = {&window, child1, child3, child4, &window}; w = &window; @@ -1578,6 +1589,10 @@ void tst_QWidget::focusChainOnReparent() QCOMPARE(w, expectedOldChain[i]); w = w->nextInFocusChain(); } + for (int i = 4; i >= 0; --i) { + w = w->previousInFocusChain(); + QCOMPARE(w, expectedOldChain[i]); + } } @@ -5877,6 +5892,35 @@ void tst_QWidget::setToolTip() widget.setToolTip(QString()); QCOMPARE(widget.toolTip(), QString()); QCOMPARE(spy.count(), 2); + + + + for (int pass = 0; pass < 2; ++pass) { + QWidget *popup = new QWidget(0, Qt::Popup); + popup->resize(150, 50); + QFrame *frame = new QFrame(popup); + frame->setGeometry(0, 0, 50, 50); + frame->setFrameStyle(QFrame::Box | QFrame::Plain); + EventSpy spy1(frame, QEvent::ToolTip); + EventSpy spy2(popup, QEvent::ToolTip); + frame->setMouseTracking(pass == 0 ? false : true); + frame->setToolTip(QLatin1String("TOOLTIP FRAME")); + popup->setToolTip(QLatin1String("TOOLTIP POPUP")); + popup->show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(popup); +#endif + QTest::qWait(100); + QTest::mouseMove(frame); + QTest::qWait(900); // delay is 700 + + QCOMPARE(spy1.count(), 1); + QCOMPARE(spy2.count(), 0); + if (pass == 0) + QTest::qWait(2200); // delay is 2000 + QTest::mouseMove(popup); + delete popup; + } } void tst_QWidget::testWindowIconChangeEventPropagation() @@ -7214,6 +7258,99 @@ void tst_QWidget::render_task252837() // Please do not crash. widget.render(&painter); } + +void tst_QWidget::render_worldTransform() +{ + class MyWidget : public QWidget + { public: + void paintEvent(QPaintEvent *) + { + QPainter painter(this); + // Make sure world transform is identity. + QCOMPARE(painter.worldTransform(), QTransform()); + + // Make sure device transform is correct. + const QPoint widgetOffset = geometry().topLeft(); + QTransform expectedDeviceTransform = QTransform::fromTranslate(105, 5); + expectedDeviceTransform.rotate(90); + expectedDeviceTransform.translate(widgetOffset.x(), widgetOffset.y()); + QCOMPARE(painter.deviceTransform(), expectedDeviceTransform); + + // Set new world transform. + QTransform newWorldTransform = QTransform::fromTranslate(10, 10); + newWorldTransform.rotate(90); + painter.setWorldTransform(newWorldTransform); + QCOMPARE(painter.worldTransform(), newWorldTransform); + + // Again, check device transform. + expectedDeviceTransform.translate(10, 10); + expectedDeviceTransform.rotate(90); + QCOMPARE(painter.deviceTransform(), expectedDeviceTransform); + + painter.fillRect(QRect(0, 0, 20, 10), Qt::green); + } + }; + + MyWidget widget; + widget.setFixedSize(100, 100); + widget.setPalette(Qt::red); + widget.setAutoFillBackground(true); + + MyWidget child; + child.setParent(&widget); + child.move(50, 50); + child.setFixedSize(50, 50); + child.setPalette(Qt::blue); + child.setAutoFillBackground(true); + + QImage image(QSize(110, 110), QImage::Format_RGB32); + image.fill(QColor(Qt::black).rgb()); + + QPainter painter(&image); + painter.translate(105, 5); + painter.rotate(90); + + const QTransform worldTransform = painter.worldTransform(); + const QTransform deviceTransform = painter.deviceTransform(); + + // Render widgets onto image. + widget.render(&painter); +#ifdef RENDER_DEBUG + image.save("render_worldTransform_image.png"); +#endif + + // Ensure the transforms are unchanged after render. + QCOMPARE(painter.worldTransform(), painter.worldTransform()); + QCOMPARE(painter.deviceTransform(), painter.deviceTransform()); + painter.end(); + + // Paint expected image. + QImage expected(QSize(110, 110), QImage::Format_RGB32); + expected.fill(QColor(Qt::black).rgb()); + + QPainter expectedPainter(&expected); + expectedPainter.translate(105, 5); + expectedPainter.rotate(90); + expectedPainter.save(); + expectedPainter.fillRect(widget.rect(),Qt::red); + expectedPainter.translate(10, 10); + expectedPainter.rotate(90); + expectedPainter.fillRect(QRect(0, 0, 20, 10), Qt::green); + expectedPainter.restore(); + expectedPainter.translate(50, 50); + expectedPainter.fillRect(child.rect(),Qt::blue); + expectedPainter.translate(10, 10); + expectedPainter.rotate(90); + expectedPainter.fillRect(QRect(0, 0, 20, 10), Qt::green); + expectedPainter.end(); + +#ifdef RENDER_DEBUG + expected.save("render_worldTransform_expected.png"); +#endif + + QCOMPARE(image, expected); +} + void tst_QWidget::setContentsMargins() { QLabel label("why does it always rain on me?"); @@ -9047,5 +9184,20 @@ void tst_QWidget::toplevelLineEditFocus() QCOMPARE(QApplication::focusWidget(), (QWidget*)&w); } +void tst_QWidget::focusWidget_task254563() +{ + //having different visibility for widget is important + QWidget top; + top.show(); + QWidget container(&top); + QWidget *widget = new QWidget(&container); + widget->show(); + + widget->setFocus(); //set focus (will set the focus widget up to the toplevel to be 'widget') + container.setFocus(); + delete widget; // will call clearFocus but that doesn't help + QVERIFY(top.focusWidget() != widget); //dangling pointer +} + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc" diff --git a/tests/auto/qxmlquery/tst_qxmlquery.cpp b/tests/auto/qxmlquery/tst_qxmlquery.cpp index 0fa6edb..1846dcb 100644 --- a/tests/auto/qxmlquery/tst_qxmlquery.cpp +++ b/tests/auto/qxmlquery/tst_qxmlquery.cpp @@ -156,6 +156,9 @@ private Q_SLOTS: void setFocusQIODeviceAvoidVariableClash() const; void setFocusQIODeviceFailure() const; void setFocusQIODeviceTriggerWarnings() const; + void setFocusQString() const; + void setFocusQStringFailure() const; + void setFocusQStringSignature() const; void recompilationWithEvaluateToResultFailing() const; void secondEvaluationWithEvaluateToResultFailing() const; void recompilationWithEvaluateToReceiver() const; @@ -1895,6 +1898,55 @@ void tst_QXmlQuery::setFocusQIODeviceFailure() const } } +void tst_QXmlQuery::setFocusQString() const +{ + QXmlQuery query; + + /* Basic use of focus. */ + { + QVERIFY(query.setFocus(QLatin1String("<e>textNode</e>"))); + query.setQuery(QLatin1String("string()")); + QVERIFY(query.isValid()); + QString out; + query.evaluateTo(&out); + QCOMPARE(out, QString::fromLatin1("textNode\n")); + } + + /* Set to a new focus, make sure it changes and works. */ + { + QVERIFY(query.setFocus(QLatin1String("<e>newFocus</e>"))); + QString out; + query.evaluateTo(&out); + QCOMPARE(out, QString::fromLatin1("newFocus\n")); + } +} + +void tst_QXmlQuery::setFocusQStringFailure() const +{ + QXmlQuery query; + MessageSilencer silencer; + + query.setMessageHandler(&silencer); + QVERIFY(!query.setFocus(QLatin1String("<notWellformed"))); + + /* Let's try the slight special case of a null string. */ + QVERIFY(!query.setFocus(QString())); +} + +void tst_QXmlQuery::setFocusQStringSignature() const +{ + QXmlQuery query; + MessageSilencer silencer; + query.setMessageHandler(&silencer); + + const QString argument; + /* We should take a const ref. */ + query.setFocus(argument); + + /* We should return a bool. */ + static_cast<bool>(query.setFocus(QString())); +} + void tst_QXmlQuery::setFocusQIODeviceTriggerWarnings() const { /* A null pointer. */ diff --git a/tests/auto/qxmlstream/tst_qxmlstream.cpp b/tests/auto/qxmlstream/tst_qxmlstream.cpp index 32fdb91..1533913 100644 --- a/tests/auto/qxmlstream/tst_qxmlstream.cpp +++ b/tests/auto/qxmlstream/tst_qxmlstream.cpp @@ -561,6 +561,8 @@ private slots: void garbageInXMLPrologDefaultCodec() const; void garbageInXMLPrologUTF8Explicitly() const; void clear() const; + void checkCommentIndentation() const; + void checkCommentIndentation_data() const; private: static QByteArray readFile(const QString &filename); @@ -1392,5 +1394,55 @@ void tst_QXmlStream::clear() const // task 228768 QCOMPARE(reader.tokenType(), QXmlStreamReader::EndDocument); } +void tst_QXmlStream::checkCommentIndentation_data() const +{ + + QTest::addColumn<QString>("input"); + QTest::addColumn<QString>("expectedOutput"); + + QString simpleInput = "<a><!-- bla --></a>"; + QString simpleOutput = "<?xml version=\"1.0\"?>\n" + "<a>\n" + " <!-- bla -->\n" + "</a>\n"; + QTest::newRow("simple-comment") << simpleInput << simpleOutput; + + QString advancedInput = "<a><!-- bla --><!-- bla --><b><!-- bla --><c><!-- bla --></c><!-- bla --></b></a>"; + QString advancedOutput = "<?xml version=\"1.0\"?>\n" + "<a>\n" + " <!-- bla -->\n" + " <!-- bla -->\n" + " <b>\n" + " <!-- bla -->\n" + " <c>\n" + " <!-- bla -->\n" + " </c>\n" + " <!-- bla -->\n" + " </b>\n" + "</a>\n"; + QTest::newRow("advanced-comment") << advancedInput << advancedOutput; +} + +void tst_QXmlStream::checkCommentIndentation() const // task 256468 +{ + QFETCH(QString, input); + QFETCH(QString, expectedOutput); + QString output; + QXmlStreamReader reader(input); + QXmlStreamWriter writer(&output); + writer.setAutoFormatting(true); + writer.setAutoFormattingIndent(3); + + while (!reader.atEnd()) { + reader.readNext(); + if (reader.error()) { + QFAIL("error reading XML input"); + } else { + writer.writeCurrentToken(reader); + } + } + QCOMPARE(output, expectedOutput); +} + #include "tst_qxmlstream.moc" // vim: et:ts=4:sw=4:sts=4 diff --git a/tests/auto/selftests/badxml/badxml.pro b/tests/auto/selftests/badxml/badxml.pro new file mode 100644 index 0000000..323791a --- /dev/null +++ b/tests/auto/selftests/badxml/badxml.pro @@ -0,0 +1,11 @@ +load(qttest_p4) + +SOURCES += tst_badxml.cpp +QT = core + +mac:CONFIG -= app_bundle +CONFIG -= debug_and_release_target +!win32:CONFIG += debug + +TARGET = badxml + diff --git a/tests/auto/selftests/badxml/tst_badxml.cpp b/tests/auto/selftests/badxml/tst_badxml.cpp new file mode 100644 index 0000000..9fa1929 --- /dev/null +++ b/tests/auto/selftests/badxml/tst_badxml.cpp @@ -0,0 +1,206 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtCore> +#include <QtTest/QtTest> + +/* + This test makes a testlog containing lots of characters which have a special meaning in + XML, with the purpose of exposing bugs in testlib's XML output code. +*/ +class tst_BadXml : public QObject +{ + Q_OBJECT + +private slots: + void badDataTag() const; + void badDataTag_data() const; + + void badMessage() const; + void badMessage_data() const; + + void failWithNoFile() const; + +public: + static QList<QByteArray> const& badStrings(); +}; + +/* + Custom metaobject to make it possible to change class name at runtime. +*/ +class EmptyClass : public tst_BadXml +{ Q_OBJECT }; + +class tst_BadXmlSub : public tst_BadXml +{ +public: + const QMetaObject* metaObject() const; + + static char const* className; +}; +char const* tst_BadXmlSub::className = "tst_BadXml"; + +const QMetaObject* tst_BadXmlSub::metaObject() const +{ + const QMetaObject& empty = EmptyClass::staticMetaObject; + static QMetaObject mo = { + { empty.d.superdata, empty.d.stringdata, empty.d.data, empty.d.extradata } + }; + static char currentClassName[1024]; + qstrcpy(currentClassName, className); + int len = qstrlen(className); + currentClassName[len] = 0; + currentClassName[len+1] = 0; + + mo.d.stringdata = currentClassName; + + return &mo; +} + +/* + Outputs incidents and benchmark results with the current data tag set to a bad string. +*/ +void tst_BadXml::badDataTag() const +{ + qDebug("a message"); + + QBENCHMARK { + } + + QFAIL("a failure"); +} + +void tst_BadXml::badDataTag_data() const +{ + QTest::addColumn<int>("dummy"); + + foreach (char const* str, badStrings()) { + QTest::newRow(str) << 0; + } +} + +void tst_BadXml::failWithNoFile() const +{ + QTest::qFail("failure message", 0, 0); +} + +/* + Outputs a message containing a bad string. +*/ +void tst_BadXml::badMessage() const +{ + QFETCH(QByteArray, message); + qDebug("%s", message.constData()); +} + +void tst_BadXml::badMessage_data() const +{ + QTest::addColumn<QByteArray>("message"); + + int i = 0; + foreach (QByteArray const& str, badStrings()) { + QTest::newRow(qPrintable(QString::fromLatin1("string %1").arg(i++))) << str; + } +} + +/* + Returns a list of strings likely to expose bugs in XML output code. +*/ +QList<QByteArray> const& tst_BadXml::badStrings() +{ + static QList<QByteArray> out; + if (out.isEmpty()) { + out << "end cdata ]]> text ]]> more text"; + out << "quotes \" text\" more text"; + out << "xml close > open < tags < text"; + out << "all > \" mixed ]]> up > \" in < the ]]> hopes < of triggering \"< ]]> bugs"; + } + return out; +} + +int main(int argc, char** argv) +{ + QCoreApplication app(argc, argv); + + /* + tst_selftests can't handle multiple XML documents in a single testrun, so we'll + decide before we begin which of our "bad strings" we want to use for our testcase + name. + */ + int badstring = -1; + QVector<char const*> args; + for (int i = 0; i < argc; ++i) { + if (!strcmp(argv[i], "-badstring")) { + bool ok = false; + if (i < argc-1) { + badstring = QByteArray(argv[i+1]).toInt(&ok); + ++i; + } + if (!ok) { + qFatal("Bad `-badstring' option"); + } + } + else { + args << argv[i]; + } + } + /* + We just want testlib to output a benchmark result, we don't actually care about the value, + so just do one iteration to save time. + */ + args << "-iterations" << "1"; + + if (badstring == -1) { + tst_BadXml test; + return QTest::qExec(&test, args.count(), const_cast<char**>(args.data())); + } + + QList<QByteArray> badstrings = tst_BadXml::badStrings(); + if (badstring >= badstrings.count()) + qFatal("`-badstring %d' is out of range", badstring); + + tst_BadXmlSub test; + test.className = badstrings[badstring].constData(); + return QTest::qExec(&test, args.count(), const_cast<char**>(args.data())); +} + +#include "tst_badxml.moc" diff --git a/tests/auto/selftests/expected_skip.txt b/tests/auto/selftests/expected_skip.txt index f3be5b5..88c0426 100644 --- a/tests/auto/selftests/expected_skip.txt +++ b/tests/auto/selftests/expected_skip.txt @@ -1,12 +1,12 @@ ********* Start testing of tst_Skip ********* -Config: Using QTest library 4.3.0, Qt 4.3.0 +Config: Using QTest library 4.6.0, Qt 4.6.0 PASS : tst_Skip::initTestCase() SKIP : tst_Skip::test() skipping all - Loc: [/home/fenglich/dev/qt-4.3/tests/auto/selftests/skip/tst_skip.cpp(35)] + Loc: [/home/rmcgover/depot/qt-git/mainline/tests/auto/selftests/skip/tst_skip.cpp(68)] SKIP : tst_Skip::emptytest() skipping all - Loc: [/home/fenglich/dev/qt-4.3/tests/auto/selftests/skip/tst_skip.cpp(45)] + Loc: [/home/rmcgover/depot/qt-git/mainline/tests/auto/selftests/skip/tst_skip.cpp(78)] SKIP : tst_Skip::singleSkip(local 1) skipping one - Loc: [/home/fenglich/dev/qt-4.3/tests/auto/selftests/skip/tst_skip.cpp(64)] + Loc: [/home/rmcgover/depot/qt-git/mainline/tests/auto/selftests/skip/tst_skip.cpp(97)] QDEBUG : tst_Skip::singleSkip(local 2) this line should only be reached once (true) PASS : tst_Skip::singleSkip() PASS : tst_Skip::cleanupTestCase() diff --git a/tests/auto/selftests/expected_xunit.txt b/tests/auto/selftests/expected_xunit.txt new file mode 100644 index 0000000..cb74491 --- /dev/null +++ b/tests/auto/selftests/expected_xunit.txt @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<testsuite errors="5" failures="3" tests="9" name="tst_Xunit"> + <properties> + <property value="4.6.0" name="QTestVersion"/> + <property value="4.6.0" name="QtVersion"/> + </properties> + <testcase result="pass" name="initTestCase"/> + <testcase result="pass" name="testFunc1"> + <!-- message="just a QWARN() !" type="warn" --> + </testcase> + <testcase result="fail" name="testFunc2"> + <!-- message="a qDebug() call with comment-ending stuff -->" type="qdebug" --> + <failure message="Compared values are not the same + Actual (2): 2 + Expected (3): 3" result="fail"/> + </testcase> + <testcase name="testFunc3"> + <!-- message="skipping this function!" type="skip" --> + </testcase> + <testcase result="fail" name="testFunc4"> + <failure message="a forced failure!" result="fail"/> + </testcase> + <testcase result="xfail" name="testFunc5"> + <!-- message="this failure is expected" type="info" --> + </testcase> + <testcase result="xfail" name="testFunc6"> + <!-- message="this failure is also expected" type="info" --> + </testcase> + <testcase result="xpass" name="testFunc7"> + <failure message="'true' returned FALSE. ()" result="xpass"/> + </testcase> + <testcase result="pass" name="cleanupTestCase"/> + <system-err> +<![CDATA[just a QWARN() !]]> +<![CDATA[a qDebug() call with comment-ending stuff -->]]> +<![CDATA[skipping this function!]]> +<![CDATA[this failure is expected]]> +<![CDATA[this failure is also expected]]> + </system-err> +</testsuite> diff --git a/tests/auto/selftests/selftests.pro b/tests/auto/selftests/selftests.pro index 2fa1d50..ca69afa 100644 --- a/tests/auto/selftests/selftests.pro +++ b/tests/auto/selftests/selftests.pro @@ -5,7 +5,7 @@ SUBDIRS = subtest test warnings maxwarnings cmptest globaldata skipglobal skip \ skipinit skipinitdata datetime singleskip assert waitwithoutgui differentexec \ exception qexecstringlist datatable commandlinedata\ benchlibwalltime benchlibcallgrind benchlibeventcounter benchlibtickcounter \ - benchliboptions + benchliboptions xunit badxml INSTALLS = diff --git a/tests/auto/selftests/selftests.qrc b/tests/auto/selftests/selftests.qrc index 125619e..d57ff29 100644 --- a/tests/auto/selftests/selftests.qrc +++ b/tests/auto/selftests/selftests.qrc @@ -34,5 +34,6 @@ <file>expected_benchlibeventcounter.txt</file> <file>expected_benchliboptions.txt</file> <file>expected_benchlibtickcounter.txt</file> + <file>expected_xunit.txt</file> </qresource> </RCC> diff --git a/tests/auto/selftests/tst_selftests.cpp b/tests/auto/selftests/tst_selftests.cpp index 30fe853..de51f53 100644 --- a/tests/auto/selftests/tst_selftests.cpp +++ b/tests/auto/selftests/tst_selftests.cpp @@ -54,9 +54,13 @@ private slots: void runSubTest(); void checkXML() const; void checkXML_data(); + void checkXunitxml() const; + void checkXunitxml_data(); private: QStringList m_checkXMLBlacklist; + QStringList m_checkXunitBlacklist; + void doRunSubTest(QString &subdir, QStringList &arguments ); }; struct BenchmarkResult @@ -186,17 +190,16 @@ void tst_Selftests::runSubTest_data() QTest::newRow("benchlibtickcounter") << "benchlibtickcounter" << QStringList("-tickcounter"); #endif + QTest::newRow("xunit") << "xunit" << QStringList("-xunitxml"); + } -void tst_Selftests::runSubTest() +void tst_Selftests::doRunSubTest(QString &subdir, QStringList &arguments ) { - QFETCH(QString, subdir); - QFETCH(QStringList, arguments); - QProcess proc; proc.setEnvironment(QStringList("")); proc.start(subdir + "/" + subdir, arguments); - QVERIFY(proc.waitForFinished()); + QVERIFY2(proc.waitForFinished(), qPrintable(proc.errorString())); const QByteArray out(proc.readAllStandardOutput()); const QByteArray err(proc.readAllStandardError()); @@ -206,8 +209,8 @@ void tst_Selftests::runSubTest() #if defined(Q_OS_WIN) if(subdir != QLatin1String("exception") && subdir != QLatin1String("fetchbogus")) #endif - QVERIFY2(err.isEmpty(), err.constData()); - + if(subdir != QLatin1String("xunit")) + QVERIFY2(err.isEmpty(), err.constData()); QList<QByteArray> res = splitLines(out); QList<QByteArray> exp = expectedResult(subdir); @@ -255,8 +258,8 @@ void tst_Selftests::runSubTest() { if(output != expected && qstrcmp(QTest::currentDataTag(), "subtest") == 0) { - /* The floating point formatting differs between platforms, so let's just skip it. */ - continue; + /* The floating point formatting differs between platforms, so let's just skip it. */ + continue; } else { /* @@ -284,6 +287,14 @@ void tst_Selftests::runSubTest() } } +void tst_Selftests::runSubTest() +{ + QFETCH(QString, subdir); + QFETCH(QStringList, arguments); + + doRunSubTest(subdir, arguments); +} + void tst_Selftests::initTestCase() { m_checkXMLBlacklist.append("crashes"); // This test crashes @@ -300,6 +311,8 @@ void tst_Selftests::initTestCase() m_checkXMLBlacklist.append("subtest"); m_checkXMLBlacklist.append("globaldata"); m_checkXMLBlacklist.append("warnings"); + + m_checkXunitBlacklist = m_checkXMLBlacklist; } void tst_Selftests::checkXML() const @@ -310,7 +323,53 @@ void tst_Selftests::checkXML() const if(m_checkXMLBlacklist.contains(subdir)) return; - arguments.prepend("-xml"); + QStringList args; + /* Test both old (-flush) and new XML logger implementation */ + for (int i = 0; i < 2; ++i) { + bool flush = i; + args = arguments; + args.prepend("-xml"); + if (flush) args.prepend("-flush"); + + QProcess proc; + proc.setEnvironment(QStringList("")); + proc.start(subdir + "/" + subdir, args); + QVERIFY(proc.waitForFinished()); + + QByteArray out(proc.readAllStandardOutput()); + QByteArray err(proc.readAllStandardError()); + + /* Some platforms decides to output a message for uncaught exceptions. For instance, + * this is what windows platforms says: + * "This application has requested the Runtime to terminate it in an unusual way. + * Please contact the application's support team for more information." */ + if(subdir != QLatin1String("exception") && subdir != QLatin1String("fetchbogus")) + QVERIFY2(err.isEmpty(), err.constData()); + + QXmlStreamReader reader(out); + + while(!reader.atEnd()) + reader.readNext(); + + QVERIFY2(!reader.error(), qPrintable(QString("(flush %0) line %1, col %2: %3") + .arg(flush) + .arg(reader.lineNumber()) + .arg(reader.columnNumber()) + .arg(reader.errorString()) + )); + } +} + +void tst_Selftests::checkXunitxml() const +{ + QFETCH(QString, subdir); + QFETCH(QStringList, arguments); + + if(m_checkXunitBlacklist.contains(subdir)) + return; + + arguments.prepend("-xunitxml"); + arguments.prepend("-flush"); QProcess proc; proc.setEnvironment(QStringList("")); @@ -320,6 +379,8 @@ void tst_Selftests::checkXML() const QByteArray out(proc.readAllStandardOutput()); QByteArray err(proc.readAllStandardError()); +// qDebug()<<out; + /* Some platforms decides to output a message for uncaught exceptions. For instance, * this is what windows platforms says: * "This application has requested the Runtime to terminate it in an unusual way. @@ -332,12 +393,26 @@ void tst_Selftests::checkXML() const while(!reader.atEnd()) reader.readNext(); - QVERIFY(!reader.error()); + QVERIFY2(!reader.error(), qPrintable(QString("line %1, col %2: %3") + .arg(reader.lineNumber()) + .arg(reader.columnNumber()) + .arg(reader.errorString()) + )); +} + +void tst_Selftests::checkXunitxml_data() +{ + checkXML_data(); } void tst_Selftests::checkXML_data() { runSubTest_data(); + QTest::newRow("badxml 1") << "badxml" << QStringList(); + QTest::newRow("badxml 2") << "badxml" << (QStringList() << "-badstring" << "0"); + QTest::newRow("badxml 3") << "badxml" << (QStringList() << "-badstring" << "1"); + QTest::newRow("badxml 4") << "badxml" << (QStringList() << "-badstring" << "2"); + QTest::newRow("badxml 5") << "badxml" << (QStringList() << "-badstring" << "3"); } /* Parse line into the BenchmarkResult it represents. */ diff --git a/tests/auto/selftests/xunit/tst_xunit b/tests/auto/selftests/xunit/tst_xunit Binary files differnew file mode 100755 index 0000000..31d03a8 --- /dev/null +++ b/tests/auto/selftests/xunit/tst_xunit diff --git a/tests/auto/selftests/xunit/tst_xunit.cpp b/tests/auto/selftests/xunit/tst_xunit.cpp new file mode 100644 index 0000000..df9b8af --- /dev/null +++ b/tests/auto/selftests/xunit/tst_xunit.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +class tst_Xunit : public QObject +{ + Q_OBJECT + +public: + tst_Xunit(); + +private slots: + void testFunc1(); + void testFunc2(); + void testFunc3(); + void testFunc4(); + void testFunc5(); + void testFunc6(); + void testFunc7(); +}; + +tst_Xunit::tst_Xunit() +{ +} + +void tst_Xunit::testFunc1() +{ + QWARN("just a QWARN() !"); + QCOMPARE(1,1); +} + +void tst_Xunit::testFunc2() +{ + qDebug("a qDebug() call with comment-ending stuff -->"); + QCOMPARE(2, 3); +} + +void tst_Xunit::testFunc3() +{ + QSKIP("skipping this function!", SkipAll); +} + +void tst_Xunit::testFunc4() +{ + QFAIL("a forced failure!"); +} + +/* + Note there are two testfunctions which give expected failures. + This is so we can test that expected failures don't add to failure + counts and unexpected passes do. If we had one xfail and one xpass + testfunction, we couldn't test which one of them adds to the failure + count. +*/ + +void tst_Xunit::testFunc5() +{ + QEXPECT_FAIL("", "this failure is expected", Abort); + QVERIFY(false); +} + +void tst_Xunit::testFunc6() +{ + QEXPECT_FAIL("", "this failure is also expected", Abort); + QVERIFY(false); +} + +void tst_Xunit::testFunc7() +{ + QEXPECT_FAIL("", "this pass is unexpected", Abort); + QVERIFY(true); +} + + +QTEST_APPLESS_MAIN(tst_Xunit) +#include "tst_xunit.moc" diff --git a/tests/auto/selftests/xunit/xunit.pro b/tests/auto/selftests/xunit/xunit.pro new file mode 100644 index 0000000..81ca157 --- /dev/null +++ b/tests/auto/selftests/xunit/xunit.pro @@ -0,0 +1,15 @@ +load(qttest_p4) +SOURCES += tst_xunit.cpp + +wince*: { + addImages.sources = images/* + addImages.path = images + DEPLOYMENT += addImages + DEFINES += SRCDIR=\\\".\\\" +} else { + contains(QT_CONFIG, qt3support): QT += qt3support + DEFINES += SRCDIR=\\\"$$PWD\\\" +} + +TARGET = xunit + diff --git a/tests/auto/test.pl b/tests/auto/test.pl index f2a1fe4..9fd5c9d 100755 --- a/tests/auto/test.pl +++ b/tests/auto/test.pl @@ -179,7 +179,7 @@ print " Tests started: $totalStarted \n"; print " Tests executed: $totalExecuted \n"; print " Tests timed out: $totalTimedOut \n"; -# This procedure takes care of handling death children on due time +# This procedure takes care of handling dead children on due time sub handleDeath { $buryChildren = 1; } diff --git a/tests/auto/uic/baseline/addtorrentform.ui.h b/tests/auto/uic/baseline/addtorrentform.ui.h index 35f0fad..fafcb16 100644 --- a/tests/auto/uic/baseline/addtorrentform.ui.h +++ b/tests/auto/uic/baseline/addtorrentform.ui.h @@ -68,7 +68,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(8); + vboxLayout->setContentsMargins(8, 8, 8, 8); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(AddTorrentFile); groupBox->setObjectName(QString::fromUtf8("groupBox")); @@ -79,7 +79,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label_4 = new QLabel(groupBox); label_4->setObjectName(QString::fromUtf8("label_4")); @@ -175,7 +175,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/batchtranslation.ui.h b/tests/auto/uic/baseline/batchtranslation.ui.h index 23030f5..4cdad44 100644 --- a/tests/auto/uic/baseline/batchtranslation.ui.h +++ b/tests/auto/uic/baseline/batchtranslation.ui.h @@ -101,7 +101,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(databaseTranslationDialog); @@ -116,7 +116,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); ckOnlyUntranslated = new QCheckBox(groupBox); @@ -146,7 +146,7 @@ public: vboxLayout2->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout2->setMargin(9); + vboxLayout2->setContentsMargins(9, 9, 9, 9); #endif vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); hboxLayout = new QHBoxLayout(); @@ -154,7 +154,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); phrasebookList = new QListView(groupBox_2); @@ -167,7 +167,7 @@ public: #ifndef Q_OS_MAC vboxLayout3->setSpacing(6); #endif - vboxLayout3->setMargin(0); + vboxLayout3->setContentsMargins(0, 0, 0, 0); vboxLayout3->setObjectName(QString::fromUtf8("vboxLayout3")); moveUpButton = new QPushButton(groupBox_2); moveUpButton->setObjectName(QString::fromUtf8("moveUpButton")); @@ -202,7 +202,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); spacerItem1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/bookwindow.ui.h b/tests/auto/uic/baseline/bookwindow.ui.h index 9eff8ce..eae06d9 100644 --- a/tests/auto/uic/baseline/bookwindow.ui.h +++ b/tests/auto/uic/baseline/bookwindow.ui.h @@ -61,7 +61,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(centralWidget); @@ -71,7 +71,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); bookTable = new QTableView(groupBox); diff --git a/tests/auto/uic/baseline/browserwidget.ui.h b/tests/auto/uic/baseline/browserwidget.ui.h index 3bc7542..67f637f 100644 --- a/tests/auto/uic/baseline/browserwidget.ui.h +++ b/tests/auto/uic/baseline/browserwidget.ui.h @@ -60,7 +60,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(8); + vboxLayout->setContentsMargins(8, 8, 8, 8); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); splitter_2 = new QSplitter(Browser); splitter_2->setObjectName(QString::fromUtf8("splitter_2")); @@ -104,7 +104,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); sqlEdit = new QTextEdit(groupBox); @@ -123,7 +123,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(1); + hboxLayout->setContentsMargins(1, 1, 1, 1); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/calculatorform.ui.h b/tests/auto/uic/baseline/calculatorform.ui.h index 5fab91f..8e9e620 100644 --- a/tests/auto/uic/baseline/calculatorform.ui.h +++ b/tests/auto/uic/baseline/calculatorform.ui.h @@ -59,7 +59,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); gridLayout->setObjectName(QString::fromUtf8("")); @@ -67,14 +67,14 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(1); + hboxLayout->setContentsMargins(1, 1, 1, 1); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setObjectName(QString::fromUtf8("")); vboxLayout = new QVBoxLayout(); #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(1); + vboxLayout->setContentsMargins(1, 1, 1, 1); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setObjectName(QString::fromUtf8("")); label = new QLabel(CalculatorForm); @@ -104,7 +104,7 @@ public: #ifndef Q_OS_MAC vboxLayout1->setSpacing(6); #endif - vboxLayout1->setMargin(1); + vboxLayout1->setContentsMargins(1, 1, 1, 1); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); vboxLayout1->setObjectName(QString::fromUtf8("")); label_2 = new QLabel(CalculatorForm); @@ -134,7 +134,7 @@ public: #ifndef Q_OS_MAC vboxLayout2->setSpacing(6); #endif - vboxLayout2->setMargin(1); + vboxLayout2->setContentsMargins(1, 1, 1, 1); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); vboxLayout2->setObjectName(QString::fromUtf8("")); label_2_2_2 = new QLabel(CalculatorForm); diff --git a/tests/auto/uic/baseline/chatdialog.ui.h b/tests/auto/uic/baseline/chatdialog.ui.h index 062a6be..7d19376 100644 --- a/tests/auto/uic/baseline/chatdialog.ui.h +++ b/tests/auto/uic/baseline/chatdialog.ui.h @@ -46,7 +46,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout = new QHBoxLayout(); @@ -54,7 +54,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); textEdit = new QTextEdit(ChatDialog); @@ -78,7 +78,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); label = new QLabel(ChatDialog); label->setObjectName(QString::fromUtf8("label")); diff --git a/tests/auto/uic/baseline/chatmainwindow.ui.h b/tests/auto/uic/baseline/chatmainwindow.ui.h index b2153e6..93ce435 100644 --- a/tests/auto/uic/baseline/chatmainwindow.ui.h +++ b/tests/auto/uic/baseline/chatmainwindow.ui.h @@ -66,7 +66,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(9); + hboxLayout->setContentsMargins(9, 9, 9, 9); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); vboxLayout = new QVBoxLayout(); @@ -74,7 +74,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); chatHistory = new QTextBrowser(centralwidget); @@ -88,7 +88,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); label = new QLabel(centralwidget); label->setObjectName(QString::fromUtf8("label")); diff --git a/tests/auto/uic/baseline/chatsetnickname.ui.h b/tests/auto/uic/baseline/chatsetnickname.ui.h index de52fed..c1fd62b 100644 --- a/tests/auto/uic/baseline/chatsetnickname.ui.h +++ b/tests/auto/uic/baseline/chatsetnickname.ui.h @@ -53,7 +53,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout1 = new QVBoxLayout(); @@ -61,7 +61,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(0); + vboxLayout1->setContentsMargins(0, 0, 0, 0); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); label = new QLabel(NicknameDialog); @@ -83,7 +83,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/config.ui.h b/tests/auto/uic/baseline/config.ui.h index 2b6eaf8..20de66f 100644 --- a/tests/auto/uic/baseline/config.ui.h +++ b/tests/auto/uic/baseline/config.ui.h @@ -139,7 +139,7 @@ public: Config->setSizeGripEnabled(true); vboxLayout = new QVBoxLayout(Config); vboxLayout->setSpacing(6); - vboxLayout->setMargin(11); + vboxLayout->setContentsMargins(11, 11, 11, 11); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setContentsMargins(8, 8, 8, 8); hboxLayout = new QHBoxLayout(); @@ -155,7 +155,7 @@ public: ButtonGroup1->setSizePolicy(sizePolicy); vboxLayout1 = new QVBoxLayout(ButtonGroup1); vboxLayout1->setSpacing(6); - vboxLayout1->setMargin(11); + vboxLayout1->setContentsMargins(11, 11, 11, 11); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); vboxLayout1->setContentsMargins(11, 11, 11, 11); size_176_220 = new QRadioButton(ButtonGroup1); @@ -230,7 +230,7 @@ public: ButtonGroup2->setObjectName(QString::fromUtf8("ButtonGroup2")); vboxLayout2 = new QVBoxLayout(ButtonGroup2); vboxLayout2->setSpacing(6); - vboxLayout2->setMargin(11); + vboxLayout2->setContentsMargins(11, 11, 11, 11); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); vboxLayout2->setContentsMargins(11, 11, 11, 11); depth_1 = new QRadioButton(ButtonGroup2); @@ -337,7 +337,7 @@ public: GroupBox1->setObjectName(QString::fromUtf8("GroupBox1")); gridLayout = new QGridLayout(GroupBox1); gridLayout->setSpacing(6); - gridLayout->setMargin(11); + gridLayout->setContentsMargins(11, 11, 11, 11); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); gridLayout->setHorizontalSpacing(6); gridLayout->setVerticalSpacing(6); diff --git a/tests/auto/uic/baseline/controller.ui.h b/tests/auto/uic/baseline/controller.ui.h index b5f20b7..29985c1 100644 --- a/tests/auto/uic/baseline/controller.ui.h +++ b/tests/auto/uic/baseline/controller.ui.h @@ -42,7 +42,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label = new QLabel(Controller); diff --git a/tests/auto/uic/baseline/default.ui.h b/tests/auto/uic/baseline/default.ui.h index 1769630..7717694 100644 --- a/tests/auto/uic/baseline/default.ui.h +++ b/tests/auto/uic/baseline/default.ui.h @@ -83,7 +83,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); nameLabel = new QLabel(centralwidget); diff --git a/tests/auto/uic/baseline/downloaditem.ui.h b/tests/auto/uic/baseline/downloaditem.ui.h index 1d4c92f..7df99df 100644 --- a/tests/auto/uic/baseline/downloaditem.ui.h +++ b/tests/auto/uic/baseline/downloaditem.ui.h @@ -48,7 +48,7 @@ public: DownloadItem->setObjectName(QString::fromUtf8("DownloadItem")); DownloadItem->resize(423, 110); horizontalLayout = new QHBoxLayout(DownloadItem); - horizontalLayout->setMargin(0); + horizontalLayout->setContentsMargins(0, 0, 0, 0); horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); fileIcon = new QLabel(DownloadItem); fileIcon->setObjectName(QString::fromUtf8("fileIcon")); diff --git a/tests/auto/uic/baseline/downloads.ui.h b/tests/auto/uic/baseline/downloads.ui.h index 03f3aeb..b4739f6 100644 --- a/tests/auto/uic/baseline/downloads.ui.h +++ b/tests/auto/uic/baseline/downloads.ui.h @@ -43,7 +43,7 @@ public: DownloadDialog->resize(332, 252); gridLayout = new QGridLayout(DownloadDialog); gridLayout->setSpacing(0); - gridLayout->setMargin(0); + gridLayout->setContentsMargins(0, 0, 0, 0); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); downloadsView = new EditTableView(DownloadDialog); downloadsView->setObjectName(QString::fromUtf8("downloadsView")); diff --git a/tests/auto/uic/baseline/filternamedialog.ui.h b/tests/auto/uic/baseline/filternamedialog.ui.h index ce67607..f4306ea 100644 --- a/tests/auto/uic/baseline/filternamedialog.ui.h +++ b/tests/auto/uic/baseline/filternamedialog.ui.h @@ -42,7 +42,7 @@ public: FilterNameDialogClass->resize(312, 95); gridLayout = new QGridLayout(FilterNameDialogClass); gridLayout->setSpacing(6); - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label = new QLabel(FilterNameDialogClass); label->setObjectName(QString::fromUtf8("label")); diff --git a/tests/auto/uic/baseline/finddialog.ui.h b/tests/auto/uic/baseline/finddialog.ui.h index a0c6c78..091c54a 100644 --- a/tests/auto/uic/baseline/finddialog.ui.h +++ b/tests/auto/uic/baseline/finddialog.ui.h @@ -102,15 +102,15 @@ public: FindDialog->setSizePolicy(sizePolicy); hboxLayout = new QHBoxLayout(FindDialog); hboxLayout->setSpacing(6); - hboxLayout->setMargin(11); + hboxLayout->setContentsMargins(11, 11, 11, 11); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); vboxLayout = new QVBoxLayout(); vboxLayout->setSpacing(6); - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout1 = new QHBoxLayout(); hboxLayout1->setSpacing(6); - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); findWhat = new QLabel(FindDialog); findWhat->setObjectName(QString::fromUtf8("findWhat")); @@ -129,7 +129,7 @@ public: groupBox->setObjectName(QString::fromUtf8("groupBox")); gridLayout = new QGridLayout(groupBox); gridLayout->setSpacing(6); - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); sourceText = new QCheckBox(groupBox); sourceText->setObjectName(QString::fromUtf8("sourceText")); @@ -168,7 +168,7 @@ public: vboxLayout1 = new QVBoxLayout(); vboxLayout1->setSpacing(6); - vboxLayout1->setMargin(0); + vboxLayout1->setContentsMargins(0, 0, 0, 0); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); findNxt = new QPushButton(FindDialog); findNxt->setObjectName(QString::fromUtf8("findNxt")); diff --git a/tests/auto/uic/baseline/form.ui.h b/tests/auto/uic/baseline/form.ui.h index 2a79c26..3f85b03 100644 --- a/tests/auto/uic/baseline/form.ui.h +++ b/tests/auto/uic/baseline/form.ui.h @@ -51,7 +51,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(9); + hboxLayout->setContentsMargins(9, 9, 9, 9); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); worldTimeClock = new WorldTimeClock(WorldTimeForm); @@ -63,7 +63,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(1); + vboxLayout->setContentsMargins(1, 1, 1, 1); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); @@ -73,7 +73,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(1); + hboxLayout1->setContentsMargins(1, 1, 1, 1); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); label = new QLabel(WorldTimeForm); label->setObjectName(QString::fromUtf8("label")); @@ -93,7 +93,7 @@ public: #ifndef Q_OS_MAC hboxLayout2->setSpacing(6); #endif - hboxLayout2->setMargin(1); + hboxLayout2->setContentsMargins(1, 1, 1, 1); hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); label_2 = new QLabel(WorldTimeForm); label_2->setObjectName(QString::fromUtf8("label_2")); diff --git a/tests/auto/uic/baseline/formwindowsettings.ui.h b/tests/auto/uic/baseline/formwindowsettings.ui.h index 7f0baf6..9e97b48 100644 --- a/tests/auto/uic/baseline/formwindowsettings.ui.h +++ b/tests/auto/uic/baseline/formwindowsettings.ui.h @@ -130,7 +130,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); layoutDefaultGroupBox = new QGroupBox(FormWindowSettings); layoutDefaultGroupBox->setObjectName(QString::fromUtf8("layoutDefaultGroupBox")); @@ -139,7 +139,7 @@ public: #ifndef Q_OS_MAC gridLayout1->setSpacing(6); #endif - gridLayout1->setMargin(8); + gridLayout1->setContentsMargins(8, 8, 8, 8); gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); label_2 = new QLabel(layoutDefaultGroupBox); label_2->setObjectName(QString::fromUtf8("label_2")); @@ -171,7 +171,7 @@ public: #ifndef Q_OS_MAC gridLayout2->setSpacing(6); #endif - gridLayout2->setMargin(8); + gridLayout2->setContentsMargins(8, 8, 8, 8); gridLayout2->setObjectName(QString::fromUtf8("gridLayout2")); spacingFunctionLineEdit = new QLineEdit(layoutFunctionGroupBox); spacingFunctionLineEdit->setObjectName(QString::fromUtf8("spacingFunctionLineEdit")); @@ -205,7 +205,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(8); + vboxLayout->setContentsMargins(8, 8, 8, 8); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); authorLineEdit = new QLineEdit(pixmapFunctionGroupBox_2); authorLineEdit->setObjectName(QString::fromUtf8("authorLineEdit")); @@ -221,7 +221,7 @@ public: #ifndef Q_OS_MAC vboxLayout1->setSpacing(6); #endif - vboxLayout1->setMargin(8); + vboxLayout1->setContentsMargins(8, 8, 8, 8); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); includeHintsTextEdit = new QTextEdit(includeHintsGroupBox); includeHintsTextEdit->setObjectName(QString::fromUtf8("includeHintsTextEdit")); @@ -235,7 +235,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); pixmapFunctionGroupBox = new QGroupBox(FormWindowSettings); pixmapFunctionGroupBox->setObjectName(QString::fromUtf8("pixmapFunctionGroupBox")); @@ -244,7 +244,7 @@ public: #ifndef Q_OS_MAC vboxLayout2->setSpacing(6); #endif - vboxLayout2->setMargin(8); + vboxLayout2->setContentsMargins(8, 8, 8, 8); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); pixmapFunctionLineEdit = new QLineEdit(pixmapFunctionGroupBox); pixmapFunctionLineEdit->setObjectName(QString::fromUtf8("pixmapFunctionLineEdit")); diff --git a/tests/auto/uic/baseline/helpdialog.ui.h b/tests/auto/uic/baseline/helpdialog.ui.h index e0d38b3..608dba3 100644 --- a/tests/auto/uic/baseline/helpdialog.ui.h +++ b/tests/auto/uic/baseline/helpdialog.ui.h @@ -119,7 +119,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); tabWidget = new QTabWidget(HelpDialog); tabWidget->setObjectName(QString::fromUtf8("tabWidget")); @@ -129,7 +129,7 @@ public: #ifndef Q_OS_MAC vboxLayout1->setSpacing(6); #endif - vboxLayout1->setMargin(5); + vboxLayout1->setContentsMargins(5, 5, 5, 5); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); listContents = new QTreeWidget(contentPage); listContents->setObjectName(QString::fromUtf8("listContents")); @@ -146,7 +146,7 @@ public: #ifndef Q_OS_MAC vboxLayout2->setSpacing(6); #endif - vboxLayout2->setMargin(5); + vboxLayout2->setContentsMargins(5, 5, 5, 5); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); TextLabel1 = new QLabel(indexPage); TextLabel1->setObjectName(QString::fromUtf8("TextLabel1")); @@ -171,7 +171,7 @@ public: #ifndef Q_OS_MAC vboxLayout3->setSpacing(6); #endif - vboxLayout3->setMargin(5); + vboxLayout3->setContentsMargins(5, 5, 5, 5); vboxLayout3->setObjectName(QString::fromUtf8("vboxLayout3")); listBookmarks = new QTreeWidget(bookmarkPage); listBookmarks->setObjectName(QString::fromUtf8("listBookmarks")); @@ -184,7 +184,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -210,7 +210,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(5); + gridLayout->setContentsMargins(5, 5, 5, 5); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); spacerItem1 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Fixed); @@ -241,7 +241,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(1); + hboxLayout1->setContentsMargins(1, 1, 1, 1); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); helpButton = new QPushButton(searchPage); helpButton->setObjectName(QString::fromUtf8("helpButton")); @@ -273,7 +273,7 @@ public: #ifndef Q_OS_MAC hboxLayout2->setSpacing(6); #endif - hboxLayout2->setMargin(3); + hboxLayout2->setContentsMargins(3, 3, 3, 3); hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); labelPrepare = new QLabel(framePrepare); labelPrepare->setObjectName(QString::fromUtf8("labelPrepare")); diff --git a/tests/auto/uic/baseline/imagedialog.ui.h b/tests/auto/uic/baseline/imagedialog.ui.h index fedc757..001532e 100644 --- a/tests/auto/uic/baseline/imagedialog.ui.h +++ b/tests/auto/uic/baseline/imagedialog.ui.h @@ -58,7 +58,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setObjectName(QString::fromUtf8("")); @@ -66,7 +66,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(1); + gridLayout->setContentsMargins(1, 1, 1, 1); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); gridLayout->setObjectName(QString::fromUtf8("")); widthLabel = new QLabel(dialog); @@ -164,7 +164,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(1); + hboxLayout->setContentsMargins(1, 1, 1, 1); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setObjectName(QString::fromUtf8("")); spacerItem1 = new QSpacerItem(QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/listwidgeteditor.ui.h b/tests/auto/uic/baseline/listwidgeteditor.ui.h index 316ff39..127228c 100644 --- a/tests/auto/uic/baseline/listwidgeteditor.ui.h +++ b/tests/auto/uic/baseline/listwidgeteditor.ui.h @@ -102,7 +102,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(qdesigner_internal__ListWidgetEditor); diff --git a/tests/auto/uic/baseline/mainwindow.ui.h b/tests/auto/uic/baseline/mainwindow.ui.h index 69e45f5..73a9de2 100644 --- a/tests/auto/uic/baseline/mainwindow.ui.h +++ b/tests/auto/uic/baseline/mainwindow.ui.h @@ -102,7 +102,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(centralwidget); @@ -112,7 +112,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(9); + hboxLayout->setContentsMargins(9, 9, 9, 9); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); label = new QLabel(groupBox); @@ -165,7 +165,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); chooseFromCodePoints = new QRadioButton(groupBox_2); @@ -178,7 +178,7 @@ public: #ifndef Q_OS_MAC vboxLayout2->setSpacing(6); #endif - vboxLayout2->setMargin(0); + vboxLayout2->setContentsMargins(0, 0, 0, 0); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); characterRangeView = new QListWidget(groupBox_2); characterRangeView->setObjectName(QString::fromUtf8("characterRangeView")); @@ -189,7 +189,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); selectAll = new QPushButton(groupBox_2); selectAll->setObjectName(QString::fromUtf8("selectAll")); @@ -225,7 +225,7 @@ public: #ifndef Q_OS_MAC hboxLayout2->setSpacing(6); #endif - hboxLayout2->setMargin(0); + hboxLayout2->setContentsMargins(0, 0, 0, 0); hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); label_5 = new QLabel(groupBox_2); label_5->setObjectName(QString::fromUtf8("label_5")); @@ -264,7 +264,7 @@ public: hboxLayout3->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout3->setMargin(9); + hboxLayout3->setContentsMargins(9, 9, 9, 9); #endif hboxLayout3->setObjectName(QString::fromUtf8("hboxLayout3")); preview = new QLineEdit(groupBox_3); @@ -282,7 +282,7 @@ public: hboxLayout4->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout4->setMargin(9); + hboxLayout4->setContentsMargins(9, 9, 9, 9); #endif hboxLayout4->setObjectName(QString::fromUtf8("hboxLayout4")); label_3 = new QLabel(groupBox_4); @@ -318,7 +318,7 @@ public: #ifndef Q_OS_MAC hboxLayout5->setSpacing(6); #endif - hboxLayout5->setMargin(0); + hboxLayout5->setContentsMargins(0, 0, 0, 0); hboxLayout5->setObjectName(QString::fromUtf8("hboxLayout5")); generate = new QPushButton(centralwidget); generate->setObjectName(QString::fromUtf8("generate")); diff --git a/tests/auto/uic/baseline/mainwindowbase.ui.h b/tests/auto/uic/baseline/mainwindowbase.ui.h index 3b40a5c..4a49f95 100644 --- a/tests/auto/uic/baseline/mainwindowbase.ui.h +++ b/tests/auto/uic/baseline/mainwindowbase.ui.h @@ -218,7 +218,7 @@ public: widget->setGeometry(QRect(0, 28, 724, 587)); hboxLayout = new QHBoxLayout(widget); hboxLayout->setSpacing(4); - hboxLayout->setMargin(8); + hboxLayout->setContentsMargins(8, 8, 8, 8); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setContentsMargins(0, 0, 0, 0); helpview = new QTextEdit(widget); @@ -234,13 +234,13 @@ public: tab1->setObjectName(QString::fromUtf8("tab1")); vboxLayout = new QVBoxLayout(tab1); vboxLayout->setSpacing(4); - vboxLayout->setMargin(4); + vboxLayout->setContentsMargins(4, 4, 4, 4); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); GroupBox40 = new QGroupBox(tab1); GroupBox40->setObjectName(QString::fromUtf8("GroupBox40")); hboxLayout1 = new QHBoxLayout(GroupBox40); hboxLayout1->setSpacing(4); - hboxLayout1->setMargin(8); + hboxLayout1->setContentsMargins(8, 8, 8, 8); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); gstylebuddy = new QLabel(GroupBox40); gstylebuddy->setObjectName(QString::fromUtf8("gstylebuddy")); @@ -264,7 +264,7 @@ public: groupAutoPalette->setSizePolicy(sizePolicy); hboxLayout2 = new QHBoxLayout(groupAutoPalette); hboxLayout2->setSpacing(4); - hboxLayout2->setMargin(8); + hboxLayout2->setContentsMargins(8, 8, 8, 8); hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); labelMainColor = new QLabel(groupAutoPalette); labelMainColor->setObjectName(QString::fromUtf8("labelMainColor")); @@ -317,7 +317,7 @@ public: GroupBox126->setSizePolicy(sizePolicy2); gridLayout = new QGridLayout(GroupBox126); gridLayout->setSpacing(4); - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); TextLabel1 = new QLabel(GroupBox126); TextLabel1->setObjectName(QString::fromUtf8("TextLabel1")); @@ -348,13 +348,13 @@ public: tab2->setObjectName(QString::fromUtf8("tab2")); vboxLayout1 = new QVBoxLayout(tab2); vboxLayout1->setSpacing(4); - vboxLayout1->setMargin(8); + vboxLayout1->setContentsMargins(8, 8, 8, 8); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); GroupBox1 = new QGroupBox(tab2); GroupBox1->setObjectName(QString::fromUtf8("GroupBox1")); gridLayout1 = new QGridLayout(GroupBox1); gridLayout1->setSpacing(4); - gridLayout1->setMargin(8); + gridLayout1->setContentsMargins(8, 8, 8, 8); gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); stylecombo = new QComboBox(GroupBox1); stylecombo->setObjectName(QString::fromUtf8("stylecombo")); @@ -406,12 +406,12 @@ public: GroupBox2->setObjectName(QString::fromUtf8("GroupBox2")); vboxLayout2 = new QVBoxLayout(GroupBox2); vboxLayout2->setSpacing(4); - vboxLayout2->setMargin(8); + vboxLayout2->setContentsMargins(8, 8, 8, 8); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); hboxLayout3 = new QHBoxLayout(); hboxLayout3->setSpacing(4); #ifndef Q_OS_MAC - hboxLayout3->setMargin(0); + hboxLayout3->setContentsMargins(0, 0, 0, 0); #endif hboxLayout3->setObjectName(QString::fromUtf8("hboxLayout3")); famsubbuddy = new QLabel(GroupBox2); @@ -450,7 +450,7 @@ public: hboxLayout4 = new QHBoxLayout(); hboxLayout4->setSpacing(4); - hboxLayout4->setMargin(0); + hboxLayout4->setContentsMargins(0, 0, 0, 0); hboxLayout4->setObjectName(QString::fromUtf8("hboxLayout4")); PushButton2 = new QPushButton(GroupBox2); PushButton2->setObjectName(QString::fromUtf8("PushButton2")); @@ -480,7 +480,7 @@ public: hboxLayout5 = new QHBoxLayout(); hboxLayout5->setSpacing(4); - hboxLayout5->setMargin(0); + hboxLayout5->setContentsMargins(0, 0, 0, 0); hboxLayout5->setObjectName(QString::fromUtf8("hboxLayout5")); choosebuddy = new QLabel(GroupBox2); choosebuddy->setObjectName(QString::fromUtf8("choosebuddy")); @@ -510,13 +510,13 @@ public: tab->setObjectName(QString::fromUtf8("tab")); vboxLayout3 = new QVBoxLayout(tab); vboxLayout3->setSpacing(4); - vboxLayout3->setMargin(7); + vboxLayout3->setContentsMargins(7, 7, 7, 7); vboxLayout3->setObjectName(QString::fromUtf8("vboxLayout3")); GroupBox4 = new QGroupBox(tab); GroupBox4->setObjectName(QString::fromUtf8("GroupBox4")); gridLayout2 = new QGridLayout(GroupBox4); gridLayout2->setSpacing(4); - gridLayout2->setMargin(8); + gridLayout2->setContentsMargins(8, 8, 8, 8); gridLayout2->setObjectName(QString::fromUtf8("gridLayout2")); dcispin = new QSpinBox(GroupBox4); dcispin->setObjectName(QString::fromUtf8("dcispin")); @@ -566,7 +566,7 @@ public: GroupBox3->setObjectName(QString::fromUtf8("GroupBox3")); vboxLayout4 = new QVBoxLayout(GroupBox3); vboxLayout4->setSpacing(4); - vboxLayout4->setMargin(8); + vboxLayout4->setContentsMargins(8, 8, 8, 8); vboxLayout4->setObjectName(QString::fromUtf8("vboxLayout4")); effectcheckbox = new QCheckBox(GroupBox3); effectcheckbox->setObjectName(QString::fromUtf8("effectcheckbox")); @@ -579,7 +579,7 @@ public: effectbase->setFrameShadow(QFrame::Plain); gridLayout3 = new QGridLayout(effectbase); gridLayout3->setSpacing(4); - gridLayout3->setMargin(0); + gridLayout3->setContentsMargins(0, 0, 0, 0); gridLayout3->setObjectName(QString::fromUtf8("gridLayout3")); meffectbuddy = new QLabel(effectbase); meffectbuddy->setObjectName(QString::fromUtf8("meffectbuddy")); @@ -632,7 +632,7 @@ public: GroupBox5->setObjectName(QString::fromUtf8("GroupBox5")); gridLayout4 = new QGridLayout(GroupBox5); gridLayout4->setSpacing(4); - gridLayout4->setMargin(8); + gridLayout4->setContentsMargins(8, 8, 8, 8); gridLayout4->setObjectName(QString::fromUtf8("gridLayout4")); swbuddy = new QLabel(GroupBox5); swbuddy->setObjectName(QString::fromUtf8("swbuddy")); @@ -683,7 +683,7 @@ public: tab3->setObjectName(QString::fromUtf8("tab3")); vboxLayout5 = new QVBoxLayout(tab3); vboxLayout5->setSpacing(4); - vboxLayout5->setMargin(8); + vboxLayout5->setContentsMargins(8, 8, 8, 8); vboxLayout5->setObjectName(QString::fromUtf8("vboxLayout5")); fontembeddingcheckbox = new QCheckBox(tab3); fontembeddingcheckbox->setObjectName(QString::fromUtf8("fontembeddingcheckbox")); @@ -697,12 +697,12 @@ public: GroupBox10->setSizePolicy(sizePolicy2); vboxLayout6 = new QVBoxLayout(GroupBox10); vboxLayout6->setSpacing(4); - vboxLayout6->setMargin(8); + vboxLayout6->setContentsMargins(8, 8, 8, 8); vboxLayout6->setObjectName(QString::fromUtf8("vboxLayout6")); gridLayout5 = new QGridLayout(); gridLayout5->setSpacing(4); #ifndef Q_OS_MAC - gridLayout5->setMargin(0); + gridLayout5->setContentsMargins(0, 0, 0, 0); #endif gridLayout5->setObjectName(QString::fromUtf8("gridLayout5")); PushButton11 = new QPushButton(GroupBox10); @@ -730,7 +730,7 @@ public: gridLayout6 = new QGridLayout(); gridLayout6->setSpacing(4); - gridLayout6->setMargin(0); + gridLayout6->setContentsMargins(0, 0, 0, 0); gridLayout6->setObjectName(QString::fromUtf8("gridLayout6")); spacerItem2 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/mydialog.ui.h b/tests/auto/uic/baseline/mydialog.ui.h index 12228c0..17cf6c5 100644 --- a/tests/auto/uic/baseline/mydialog.ui.h +++ b/tests/auto/uic/baseline/mydialog.ui.h @@ -40,7 +40,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); aLabel = new QLabel(MyDialog); diff --git a/tests/auto/uic/baseline/myform.ui.h b/tests/auto/uic/baseline/myform.ui.h index 9aaa709..07e1663 100644 --- a/tests/auto/uic/baseline/myform.ui.h +++ b/tests/auto/uic/baseline/myform.ui.h @@ -51,7 +51,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(8); + vboxLayout->setContentsMargins(8, 8, 8, 8); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(Form); groupBox->setObjectName(QString::fromUtf8("groupBox")); @@ -59,7 +59,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); radioButton_2 = new QRadioButton(groupBox); radioButton_2->setObjectName(QString::fromUtf8("radioButton_2")); diff --git a/tests/auto/uic/baseline/newform.ui.h b/tests/auto/uic/baseline/newform.ui.h index 46af9cb..37eb709 100644 --- a/tests/auto/uic/baseline/newform.ui.h +++ b/tests/auto/uic/baseline/newform.ui.h @@ -89,14 +89,14 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout = new QHBoxLayout(); #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(1); + hboxLayout->setContentsMargins(1, 1, 1, 1); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); treeWidget = new QTreeWidget(NewForm); treeWidget->setObjectName(QString::fromUtf8("treeWidget")); diff --git a/tests/auto/uic/baseline/pagefold.ui.h b/tests/auto/uic/baseline/pagefold.ui.h index adff3a3..9c2a453 100644 --- a/tests/auto/uic/baseline/pagefold.ui.h +++ b/tests/auto/uic/baseline/pagefold.ui.h @@ -87,7 +87,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); mainFrame = new QFrame(centralwidget); @@ -99,7 +99,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); nameCombo = new QComboBox(mainFrame); diff --git a/tests/auto/uic/baseline/paletteeditor.ui.h b/tests/auto/uic/baseline/paletteeditor.ui.h index 9310ab8..0dcefb3 100644 --- a/tests/auto/uic/baseline/paletteeditor.ui.h +++ b/tests/auto/uic/baseline/paletteeditor.ui.h @@ -106,7 +106,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); advancedBox = new QGroupBox(qdesigner_internal__PaletteEditor); @@ -118,7 +118,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); buildButton = new QtColorButton(advancedBox); @@ -167,7 +167,7 @@ public: #ifndef Q_OS_MAC gridLayout1->setSpacing(6); #endif - gridLayout1->setMargin(8); + gridLayout1->setContentsMargins(8, 8, 8, 8); gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); disabledRadio = new QRadioButton(GroupBox126); disabledRadio->setObjectName(QString::fromUtf8("disabledRadio")); diff --git a/tests/auto/uic/baseline/paletteeditoradvancedbase.ui.h b/tests/auto/uic/baseline/paletteeditoradvancedbase.ui.h index 618ddfc..bb2a959 100644 --- a/tests/auto/uic/baseline/paletteeditoradvancedbase.ui.h +++ b/tests/auto/uic/baseline/paletteeditoradvancedbase.ui.h @@ -114,7 +114,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(11); + vboxLayout->setContentsMargins(11, 11, 11, 11); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setObjectName(QString::fromUtf8("unnamed")); hboxLayout = new QHBoxLayout(); @@ -122,7 +122,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setObjectName(QString::fromUtf8("unnamed")); @@ -150,7 +150,7 @@ public: #ifndef Q_OS_MAC ButtonGroup1->layout()->setSpacing(6); #endif - ButtonGroup1->layout()->setMargin(11); + ButtonGroup1->layout()->setContentsMargins(11, 11, 11, 11); vboxLayout1 = new QVBoxLayout(); QBoxLayout *boxlayout = qobject_cast<QBoxLayout *>(ButtonGroup1->layout()); if (boxlayout) @@ -179,7 +179,7 @@ public: #ifndef Q_OS_MAC groupCentral->layout()->setSpacing(6); #endif - groupCentral->layout()->setMargin(11); + groupCentral->layout()->setContentsMargins(11, 11, 11, 11); vboxLayout2 = new QVBoxLayout(); QBoxLayout *boxlayout1 = qobject_cast<QBoxLayout *>(groupCentral->layout()); if (boxlayout1) @@ -196,7 +196,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); hboxLayout1->setObjectName(QString::fromUtf8("unnamed")); Horizontal_Spacing1 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -237,7 +237,7 @@ public: #ifndef Q_OS_MAC groupEffect->layout()->setSpacing(6); #endif - groupEffect->layout()->setMargin(11); + groupEffect->layout()->setContentsMargins(11, 11, 11, 11); vboxLayout3 = new QVBoxLayout(); QBoxLayout *boxlayout2 = qobject_cast<QBoxLayout *>(groupEffect->layout()); if (boxlayout2) @@ -250,7 +250,7 @@ public: hboxLayout2->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout2->setMargin(0); + hboxLayout2->setContentsMargins(0, 0, 0, 0); #endif hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); hboxLayout2->setObjectName(QString::fromUtf8("unnamed")); @@ -272,7 +272,7 @@ public: #ifndef Q_OS_MAC hboxLayout3->setSpacing(6); #endif - hboxLayout3->setMargin(0); + hboxLayout3->setContentsMargins(0, 0, 0, 0); hboxLayout3->setObjectName(QString::fromUtf8("hboxLayout3")); hboxLayout3->setObjectName(QString::fromUtf8("unnamed")); Horizontal_Spacing3 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -305,7 +305,7 @@ public: #ifndef Q_OS_MAC hboxLayout4->setSpacing(6); #endif - hboxLayout4->setMargin(0); + hboxLayout4->setContentsMargins(0, 0, 0, 0); hboxLayout4->setObjectName(QString::fromUtf8("hboxLayout4")); hboxLayout4->setObjectName(QString::fromUtf8("unnamed")); Horizontal_Spacing2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/phrasebookbox.ui.h b/tests/auto/uic/baseline/phrasebookbox.ui.h index c87cbc2..f4abb1b 100644 --- a/tests/auto/uic/baseline/phrasebookbox.ui.h +++ b/tests/auto/uic/baseline/phrasebookbox.ui.h @@ -96,7 +96,7 @@ public: PhraseBookBox->resize(596, 454); unnamed = new QHBoxLayout(PhraseBookBox); unnamed->setSpacing(6); - unnamed->setMargin(11); + unnamed->setContentsMargins(11, 11, 11, 11); unnamed->setObjectName(QString::fromUtf8("unnamed")); inputsLayout = new QVBoxLayout(); inputsLayout->setSpacing(6); diff --git a/tests/auto/uic/baseline/previewdialogbase.ui.h b/tests/auto/uic/baseline/previewdialogbase.ui.h index af65a18..93bfb69 100644 --- a/tests/auto/uic/baseline/previewdialogbase.ui.h +++ b/tests/auto/uic/baseline/previewdialogbase.ui.h @@ -55,7 +55,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout = new QHBoxLayout(); @@ -63,7 +63,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); label = new QLabel(PreviewDialogBase); @@ -104,7 +104,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); pageList = new QTreeWidget(PreviewDialogBase); pageList->setObjectName(QString::fromUtf8("pageList")); @@ -133,7 +133,7 @@ public: #ifndef Q_OS_MAC hboxLayout2->setSpacing(6); #endif - hboxLayout2->setMargin(0); + hboxLayout2->setContentsMargins(0, 0, 0, 0); hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); progressBar = new QProgressBar(PreviewDialogBase); progressBar->setObjectName(QString::fromUtf8("progressBar")); diff --git a/tests/auto/uic/baseline/previewwidget.ui.h b/tests/auto/uic/baseline/previewwidget.ui.h index 71755df..b76aa56 100644 --- a/tests/auto/uic/baseline/previewwidget.ui.h +++ b/tests/auto/uic/baseline/previewwidget.ui.h @@ -118,7 +118,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); vboxLayout = new QVBoxLayout(); @@ -126,7 +126,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); LineEdit1 = new QLineEdit(qdesigner_internal__PreviewWidget); @@ -143,7 +143,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); SpinBox1 = new QSpinBox(qdesigner_internal__PreviewWidget); SpinBox1->setObjectName(QString::fromUtf8("SpinBox1")); @@ -196,7 +196,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); CheckBox1 = new QCheckBox(ButtonGroup2); @@ -220,7 +220,7 @@ public: vboxLayout2->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout2->setMargin(9); + vboxLayout2->setContentsMargins(9, 9, 9, 9); #endif vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); RadioButton1 = new QRadioButton(ButtonGroup1); diff --git a/tests/auto/uic/baseline/previewwidgetbase.ui.h b/tests/auto/uic/baseline/previewwidgetbase.ui.h index f69a785..82899b7 100644 --- a/tests/auto/uic/baseline/previewwidgetbase.ui.h +++ b/tests/auto/uic/baseline/previewwidgetbase.ui.h @@ -117,7 +117,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(11); + vboxLayout->setContentsMargins(11, 11, 11, 11); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setObjectName(QString::fromUtf8("unnamed")); hboxLayout = new QHBoxLayout(); @@ -125,7 +125,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setObjectName(QString::fromUtf8("unnamed")); @@ -134,7 +134,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(0); + vboxLayout1->setContentsMargins(0, 0, 0, 0); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); vboxLayout1->setObjectName(QString::fromUtf8("unnamed")); @@ -144,7 +144,7 @@ public: #ifndef Q_OS_MAC ButtonGroup1->layout()->setSpacing(6); #endif - ButtonGroup1->layout()->setMargin(11); + ButtonGroup1->layout()->setContentsMargins(11, 11, 11, 11); vboxLayout2 = new QVBoxLayout(); QBoxLayout *boxlayout = qobject_cast<QBoxLayout *>(ButtonGroup1->layout()); if (boxlayout) @@ -177,7 +177,7 @@ public: #ifndef Q_OS_MAC ButtonGroup2->layout()->setSpacing(6); #endif - ButtonGroup2->layout()->setMargin(11); + ButtonGroup2->layout()->setContentsMargins(11, 11, 11, 11); vboxLayout3 = new QVBoxLayout(); QBoxLayout *boxlayout1 = qobject_cast<QBoxLayout *>(ButtonGroup2->layout()); if (boxlayout1) @@ -212,7 +212,7 @@ public: #ifndef Q_OS_MAC vboxLayout4->setSpacing(6); #endif - vboxLayout4->setMargin(0); + vboxLayout4->setContentsMargins(0, 0, 0, 0); vboxLayout4->setObjectName(QString::fromUtf8("vboxLayout4")); vboxLayout4->setObjectName(QString::fromUtf8("unnamed")); LineEdit1 = new QLineEdit(PreviewWidgetBase); @@ -229,7 +229,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); hboxLayout1->setObjectName(QString::fromUtf8("unnamed")); SpinBox1 = new QSpinBox(PreviewWidgetBase); diff --git a/tests/auto/uic/baseline/qpagesetupwidget.ui.h b/tests/auto/uic/baseline/qpagesetupwidget.ui.h index 628a8eb..f2a68bd 100644 --- a/tests/auto/uic/baseline/qpagesetupwidget.ui.h +++ b/tests/auto/uic/baseline/qpagesetupwidget.ui.h @@ -76,7 +76,7 @@ public: QPageSetupWidget->setObjectName(QString::fromUtf8("QPageSetupWidget")); QPageSetupWidget->resize(416, 488); gridLayout_3 = new QGridLayout(QPageSetupWidget); - gridLayout_3->setMargin(0); + gridLayout_3->setContentsMargins(0, 0, 0, 0); gridLayout_3->setObjectName(QString::fromUtf8("gridLayout_3")); horizontalLayout_4 = new QHBoxLayout(); horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4")); diff --git a/tests/auto/uic/baseline/qprintpropertieswidget.ui.h b/tests/auto/uic/baseline/qprintpropertieswidget.ui.h index 75a3e6d..16148ef 100644 --- a/tests/auto/uic/baseline/qprintpropertieswidget.ui.h +++ b/tests/auto/uic/baseline/qprintpropertieswidget.ui.h @@ -42,7 +42,7 @@ public: QPrintPropertiesWidget->setObjectName(QString::fromUtf8("QPrintPropertiesWidget")); QPrintPropertiesWidget->resize(396, 288); verticalLayout_4 = new QVBoxLayout(QPrintPropertiesWidget); - verticalLayout_4->setMargin(0); + verticalLayout_4->setContentsMargins(0, 0, 0, 0); verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4")); tabs = new QTabWidget(QPrintPropertiesWidget); tabs->setObjectName(QString::fromUtf8("tabs")); diff --git a/tests/auto/uic/baseline/qprintsettingsoutput.ui.h b/tests/auto/uic/baseline/qprintsettingsoutput.ui.h index e51aadb..307582a 100644 --- a/tests/auto/uic/baseline/qprintsettingsoutput.ui.h +++ b/tests/auto/uic/baseline/qprintsettingsoutput.ui.h @@ -77,7 +77,7 @@ public: QPrintSettingsOutput->setObjectName(QString::fromUtf8("QPrintSettingsOutput")); QPrintSettingsOutput->resize(416, 166); horizontalLayout_2 = new QHBoxLayout(QPrintSettingsOutput); - horizontalLayout_2->setMargin(0); + horizontalLayout_2->setContentsMargins(0, 0, 0, 0); horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2")); tabs = new QTabWidget(QPrintSettingsOutput); tabs->setObjectName(QString::fromUtf8("tabs")); @@ -95,7 +95,7 @@ public: gbPrintRange->setSizePolicy(sizePolicy); _3 = new QVBoxLayout(gbPrintRange); _3->setSpacing(4); - _3->setMargin(6); + _3->setContentsMargins(6, 6, 6, 6); _3->setObjectName(QString::fromUtf8("_3")); printAll = new QRadioButton(gbPrintRange); printAll->setObjectName(QString::fromUtf8("printAll")); @@ -107,7 +107,7 @@ public: #ifndef Q_OS_MAC _4->setSpacing(6); #endif - _4->setMargin(0); + _4->setContentsMargins(0, 0, 0, 0); _4->setObjectName(QString::fromUtf8("_4")); printRange = new QRadioButton(gbPrintRange); printRange->setObjectName(QString::fromUtf8("printRange")); diff --git a/tests/auto/uic/baseline/qprintwidget.ui.h b/tests/auto/uic/baseline/qprintwidget.ui.h index f6c0989..7fa0b4f 100644 --- a/tests/auto/uic/baseline/qprintwidget.ui.h +++ b/tests/auto/uic/baseline/qprintwidget.ui.h @@ -53,7 +53,7 @@ public: QPrintWidget->setObjectName(QString::fromUtf8("QPrintWidget")); QPrintWidget->resize(443, 175); horizontalLayout_2 = new QHBoxLayout(QPrintWidget); - horizontalLayout_2->setMargin(0); + horizontalLayout_2->setContentsMargins(0, 0, 0, 0); horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2")); printerGroup = new QGroupBox(QPrintWidget); printerGroup->setObjectName(QString::fromUtf8("printerGroup")); diff --git a/tests/auto/uic/baseline/qsqlconnectiondialog.ui.h b/tests/auto/uic/baseline/qsqlconnectiondialog.ui.h index f36d0ac..846f34d 100644 --- a/tests/auto/uic/baseline/qsqlconnectiondialog.ui.h +++ b/tests/auto/uic/baseline/qsqlconnectiondialog.ui.h @@ -65,7 +65,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(8); + vboxLayout->setContentsMargins(8, 8, 8, 8); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); connGroupBox = new QGroupBox(QSqlConnectionDialogUi); connGroupBox->setObjectName(QString::fromUtf8("connGroupBox")); @@ -73,7 +73,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); comboDriver = new QComboBox(connGroupBox); comboDriver->setObjectName(QString::fromUtf8("comboDriver")); @@ -146,7 +146,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -164,7 +164,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); spacerItem1 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/qttoolbardialog.ui.h b/tests/auto/uic/baseline/qttoolbardialog.ui.h index 3b487c3..7225eda 100644 --- a/tests/auto/uic/baseline/qttoolbardialog.ui.h +++ b/tests/auto/uic/baseline/qttoolbardialog.ui.h @@ -59,7 +59,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); actionTree = new QTreeWidget(QtToolBarDialog); actionTree->setObjectName(QString::fromUtf8("actionTree")); @@ -75,7 +75,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); label_2 = new QLabel(QtToolBarDialog); label_2->setObjectName(QString::fromUtf8("label_2")); @@ -104,7 +104,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); upButton = new QToolButton(QtToolBarDialog); upButton->setObjectName(QString::fromUtf8("upButton")); diff --git a/tests/auto/uic/baseline/querywidget.ui.h b/tests/auto/uic/baseline/querywidget.ui.h index e49c197..7697328 100644 --- a/tests/auto/uic/baseline/querywidget.ui.h +++ b/tests/auto/uic/baseline/querywidget.ui.h @@ -62,7 +62,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); inputGroupBox = new QGroupBox(centralwidget); @@ -75,7 +75,7 @@ public: _2->setSpacing(6); #endif #ifndef Q_OS_MAC - _2->setMargin(0); + _2->setContentsMargins(0, 0, 0, 0); #endif _2->setObjectName(QString::fromUtf8("_2")); inputTextEdit = new QTextEdit(inputGroupBox); @@ -120,7 +120,7 @@ public: _3->setSpacing(6); #endif #ifndef Q_OS_MAC - _3->setMargin(0); + _3->setContentsMargins(0, 0, 0, 0); #endif _3->setObjectName(QString::fromUtf8("_3")); outputTextEdit = new QTextEdit(outputGroupBox); diff --git a/tests/auto/uic/baseline/remotecontrol.ui.h b/tests/auto/uic/baseline/remotecontrol.ui.h index b8acf9d..854a994 100644 --- a/tests/auto/uic/baseline/remotecontrol.ui.h +++ b/tests/auto/uic/baseline/remotecontrol.ui.h @@ -77,7 +77,7 @@ public: centralWidget->setObjectName(QString::fromUtf8("centralWidget")); gridLayout = new QGridLayout(centralWidget); gridLayout->setSpacing(6); - gridLayout->setMargin(11); + gridLayout->setContentsMargins(11, 11, 11, 11); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label = new QLabel(centralWidget); label->setObjectName(QString::fromUtf8("label")); @@ -107,7 +107,7 @@ public: actionGroupBox->setEnabled(false); gridLayout1 = new QGridLayout(actionGroupBox); gridLayout1->setSpacing(6); - gridLayout1->setMargin(11); + gridLayout1->setContentsMargins(11, 11, 11, 11); gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); label_2 = new QLabel(actionGroupBox); label_2->setObjectName(QString::fromUtf8("label_2")); diff --git a/tests/auto/uic/baseline/signalslotdialog.ui.h b/tests/auto/uic/baseline/signalslotdialog.ui.h index 393532a..6adcf21 100644 --- a/tests/auto/uic/baseline/signalslotdialog.ui.h +++ b/tests/auto/uic/baseline/signalslotdialog.ui.h @@ -53,13 +53,13 @@ public: SignalSlotDialogClass->resize(617, 535); vboxLayout = new QVBoxLayout(SignalSlotDialogClass); vboxLayout->setSpacing(6); - vboxLayout->setMargin(11); + vboxLayout->setContentsMargins(11, 11, 11, 11); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); slotGroupBox = new QGroupBox(SignalSlotDialogClass); slotGroupBox->setObjectName(QString::fromUtf8("slotGroupBox")); vboxLayout1 = new QVBoxLayout(slotGroupBox); vboxLayout1->setSpacing(6); - vboxLayout1->setMargin(11); + vboxLayout1->setContentsMargins(11, 11, 11, 11); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); slotListView = new QListView(slotGroupBox); slotListView->setObjectName(QString::fromUtf8("slotListView")); @@ -93,7 +93,7 @@ public: signalGroupBox->setObjectName(QString::fromUtf8("signalGroupBox")); vboxLayout2 = new QVBoxLayout(signalGroupBox); vboxLayout2->setSpacing(6); - vboxLayout2->setMargin(11); + vboxLayout2->setContentsMargins(11, 11, 11, 11); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); signalListView = new QListView(signalGroupBox); signalListView->setObjectName(QString::fromUtf8("signalListView")); diff --git a/tests/auto/uic/baseline/statistics.ui.h b/tests/auto/uic/baseline/statistics.ui.h index eb24cfe..b8257c4 100644 --- a/tests/auto/uic/baseline/statistics.ui.h +++ b/tests/auto/uic/baseline/statistics.ui.h @@ -97,7 +97,7 @@ public: Statistics->resize(336, 164); gridLayout = new QGridLayout(Statistics); gridLayout->setSpacing(6); - gridLayout->setMargin(11); + gridLayout->setContentsMargins(11, 11, 11, 11); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); gridLayout->setObjectName(QString::fromUtf8("unnamed")); hboxLayout = new QHBoxLayout(); @@ -126,7 +126,7 @@ public: frame4->setFrameShadow(QFrame::Raised); gridLayout1 = new QGridLayout(frame4); gridLayout1->setSpacing(6); - gridLayout1->setMargin(11); + gridLayout1->setContentsMargins(11, 11, 11, 11); gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); gridLayout1->setObjectName(QString::fromUtf8("unnamed")); textLabel4 = new QLabel(frame4); diff --git a/tests/auto/uic/baseline/stringlisteditor.ui.h b/tests/auto/uic/baseline/stringlisteditor.ui.h index ffba0af..e6daa52 100644 --- a/tests/auto/uic/baseline/stringlisteditor.ui.h +++ b/tests/auto/uic/baseline/stringlisteditor.ui.h @@ -105,7 +105,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(qdesigner_internal__Dialog); @@ -115,7 +115,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); vboxLayout1 = new QVBoxLayout(); @@ -123,7 +123,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(0); + vboxLayout1->setContentsMargins(0, 0, 0, 0); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); hboxLayout = new QHBoxLayout(); @@ -131,7 +131,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); newButton = new QToolButton(groupBox); @@ -157,7 +157,7 @@ public: #ifndef Q_OS_MAC hboxLayout1->setSpacing(6); #endif - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); label = new QLabel(groupBox); label->setObjectName(QString::fromUtf8("label")); @@ -179,7 +179,7 @@ public: #ifndef Q_OS_MAC vboxLayout2->setSpacing(6); #endif - vboxLayout2->setMargin(0); + vboxLayout2->setContentsMargins(0, 0, 0, 0); vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); spacerItem1 = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); diff --git a/tests/auto/uic/baseline/stylesheeteditor.ui.h b/tests/auto/uic/baseline/stylesheeteditor.ui.h index 9f803c6..7633d5b 100644 --- a/tests/auto/uic/baseline/stylesheeteditor.ui.h +++ b/tests/auto/uic/baseline/stylesheeteditor.ui.h @@ -52,7 +52,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); spacerItem = new QSpacerItem(32, 20, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); @@ -96,7 +96,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem3 = new QSpacerItem(321, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uic/baseline/tabbedbrowser.ui.h b/tests/auto/uic/baseline/tabbedbrowser.ui.h index ecf9024..1106f13 100644 --- a/tests/auto/uic/baseline/tabbedbrowser.ui.h +++ b/tests/auto/uic/baseline/tabbedbrowser.ui.h @@ -96,7 +96,7 @@ public: TabbedBrowser->resize(710, 664); vboxLayout = new QVBoxLayout(TabbedBrowser); vboxLayout->setSpacing(0); - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); tab = new QTabWidget(TabbedBrowser); tab->setObjectName(QString::fromUtf8("tab")); @@ -106,7 +106,7 @@ public: #ifndef Q_OS_MAC gridLayout->setSpacing(6); #endif - gridLayout->setMargin(8); + gridLayout->setContentsMargins(8, 8, 8, 8); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); tab->addTab(frontpage, QString()); @@ -120,7 +120,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); toolClose = new QToolButton(frameFind); toolClose->setObjectName(QString::fromUtf8("toolClose")); diff --git a/tests/auto/uic/baseline/tablewidgeteditor.ui.h b/tests/auto/uic/baseline/tablewidgeteditor.ui.h index 47649ec..4769c42 100644 --- a/tests/auto/uic/baseline/tablewidgeteditor.ui.h +++ b/tests/auto/uic/baseline/tablewidgeteditor.ui.h @@ -169,7 +169,7 @@ public: sizePolicy.setHeightForWidth(widget->sizePolicy().hasHeightForWidth()); widget->setSizePolicy(sizePolicy); verticalLayout = new QVBoxLayout(widget); - verticalLayout->setMargin(0); + verticalLayout->setContentsMargins(0, 0, 0, 0); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); columnsBox = new QGroupBox(widget); columnsBox->setObjectName(QString::fromUtf8("columnsBox")); diff --git a/tests/auto/uic/baseline/tetrixwindow.ui.h b/tests/auto/uic/baseline/tetrixwindow.ui.h index 73f1a79..a848545 100644 --- a/tests/auto/uic/baseline/tetrixwindow.ui.h +++ b/tests/auto/uic/baseline/tetrixwindow.ui.h @@ -53,7 +53,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); gridLayout = new QGridLayout(); @@ -61,7 +61,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(0); + gridLayout->setContentsMargins(0, 0, 0, 0); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); startButton = new QPushButton(TetrixWindow); diff --git a/tests/auto/uic/baseline/textfinder.ui.h b/tests/auto/uic/baseline/textfinder.ui.h index afb1bec..f1d51f9 100644 --- a/tests/auto/uic/baseline/textfinder.ui.h +++ b/tests/auto/uic/baseline/textfinder.ui.h @@ -47,7 +47,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); gridLayout = new QGridLayout(); @@ -55,7 +55,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(0); + gridLayout->setContentsMargins(0, 0, 0, 0); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); lineEdit = new QLineEdit(Form); diff --git a/tests/auto/uic/baseline/topicchooser.ui.h b/tests/auto/uic/baseline/topicchooser.ui.h index 810f11a..8404d0c 100644 --- a/tests/auto/uic/baseline/topicchooser.ui.h +++ b/tests/auto/uic/baseline/topicchooser.ui.h @@ -48,7 +48,7 @@ public: #ifndef Q_OS_MAC vboxLayout->setSpacing(6); #endif - vboxLayout->setMargin(11); + vboxLayout->setContentsMargins(11, 11, 11, 11); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); vboxLayout->setObjectName(QString::fromUtf8("unnamed")); label = new QLabel(TopicChooser); @@ -67,7 +67,7 @@ public: #ifndef Q_OS_MAC hboxLayout->setSpacing(6); #endif - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setObjectName(QString::fromUtf8("unnamed")); hboxLayout->setContentsMargins(0, 0, 0, 0); diff --git a/tests/auto/uic/baseline/translatedialog.ui.h b/tests/auto/uic/baseline/translatedialog.ui.h index ec4a0f4..b0df4b5 100644 --- a/tests/auto/uic/baseline/translatedialog.ui.h +++ b/tests/auto/uic/baseline/translatedialog.ui.h @@ -104,7 +104,7 @@ public: TranslateDialog->setSizePolicy(sizePolicy); hboxLayout = new QHBoxLayout(TranslateDialog); hboxLayout->setSpacing(6); - hboxLayout->setMargin(11); + hboxLayout->setContentsMargins(11, 11, 11, 11); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); hboxLayout->setContentsMargins(9, 9, 9, 9); vboxLayout = new QVBoxLayout(); @@ -144,7 +144,7 @@ public: groupBox->setObjectName(QString::fromUtf8("groupBox")); vboxLayout1 = new QVBoxLayout(groupBox); vboxLayout1->setSpacing(6); - vboxLayout1->setMargin(11); + vboxLayout1->setContentsMargins(11, 11, 11, 11); vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); ckMatchCase = new QCheckBox(groupBox); ckMatchCase->setObjectName(QString::fromUtf8("ckMatchCase")); diff --git a/tests/auto/uic/baseline/translationsettings.ui.h b/tests/auto/uic/baseline/translationsettings.ui.h index 792b733..7ce0485 100644 --- a/tests/auto/uic/baseline/translationsettings.ui.h +++ b/tests/auto/uic/baseline/translationsettings.ui.h @@ -47,7 +47,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); groupBox = new QGroupBox(TranslationSettings); @@ -57,7 +57,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(9); + gridLayout->setContentsMargins(9, 9, 9, 9); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); cbLanguageList = new QComboBox(groupBox); diff --git a/tests/auto/uic/baseline/trpreviewtool.ui.h b/tests/auto/uic/baseline/trpreviewtool.ui.h index e607763..2575cda 100644 --- a/tests/auto/uic/baseline/trpreviewtool.ui.h +++ b/tests/auto/uic/baseline/trpreviewtool.ui.h @@ -142,7 +142,7 @@ public: dockWidgetContents->setObjectName(QString::fromUtf8("dockWidgetContents")); vboxLayout = new QVBoxLayout(dockWidgetContents); vboxLayout->setSpacing(0); - vboxLayout->setMargin(0); + vboxLayout->setContentsMargins(0, 0, 0, 0); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); viewForms = new QListView(dockWidgetContents); viewForms->setObjectName(QString::fromUtf8("viewForms")); diff --git a/tests/auto/uic/baseline/validators.ui.h b/tests/auto/uic/baseline/validators.ui.h index ea5d319..03bc898 100644 --- a/tests/auto/uic/baseline/validators.ui.h +++ b/tests/auto/uic/baseline/validators.ui.h @@ -87,7 +87,7 @@ public: vboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout->setMargin(9); + vboxLayout->setContentsMargins(9, 9, 9, 9); #endif vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout = new QHBoxLayout(); @@ -95,7 +95,7 @@ public: hboxLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout->setMargin(0); + hboxLayout->setContentsMargins(0, 0, 0, 0); #endif hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); localeSelector = new LocaleSelector(ValidatorsForm); @@ -117,7 +117,7 @@ public: vboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout1->setMargin(9); + vboxLayout1->setContentsMargins(9, 9, 9, 9); #endif vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1")); hboxLayout1 = new QHBoxLayout(); @@ -125,7 +125,7 @@ public: hboxLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout1->setMargin(0); + hboxLayout1->setContentsMargins(0, 0, 0, 0); #endif hboxLayout1->setObjectName(QString::fromUtf8("hboxLayout1")); gridLayout = new QGridLayout(); @@ -133,7 +133,7 @@ public: gridLayout->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout->setMargin(0); + gridLayout->setContentsMargins(0, 0, 0, 0); #endif gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label = new QLabel(groupBox); @@ -182,7 +182,7 @@ public: vboxLayout2->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout2->setMargin(9); + vboxLayout2->setContentsMargins(9, 9, 9, 9); #endif vboxLayout2->setObjectName(QString::fromUtf8("vboxLayout2")); ledWidget = new LEDWidget(frame); @@ -227,7 +227,7 @@ public: vboxLayout3->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout3->setMargin(9); + vboxLayout3->setContentsMargins(9, 9, 9, 9); #endif vboxLayout3->setObjectName(QString::fromUtf8("vboxLayout3")); hboxLayout2 = new QHBoxLayout(); @@ -235,7 +235,7 @@ public: hboxLayout2->setSpacing(6); #endif #ifndef Q_OS_MAC - hboxLayout2->setMargin(0); + hboxLayout2->setContentsMargins(0, 0, 0, 0); #endif hboxLayout2->setObjectName(QString::fromUtf8("hboxLayout2")); gridLayout1 = new QGridLayout(); @@ -243,7 +243,7 @@ public: gridLayout1->setSpacing(6); #endif #ifndef Q_OS_MAC - gridLayout1->setMargin(0); + gridLayout1->setContentsMargins(0, 0, 0, 0); #endif gridLayout1->setObjectName(QString::fromUtf8("gridLayout1")); label_3 = new QLabel(groupBox_2); @@ -313,7 +313,7 @@ public: vboxLayout4->setSpacing(6); #endif #ifndef Q_OS_MAC - vboxLayout4->setMargin(9); + vboxLayout4->setContentsMargins(9, 9, 9, 9); #endif vboxLayout4->setObjectName(QString::fromUtf8("vboxLayout4")); doubleLedWidget = new LEDWidget(frame_2); @@ -354,7 +354,7 @@ public: #ifndef Q_OS_MAC hboxLayout3->setSpacing(6); #endif - hboxLayout3->setMargin(0); + hboxLayout3->setContentsMargins(0, 0, 0, 0); hboxLayout3->setObjectName(QString::fromUtf8("hboxLayout3")); spacerItem4 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); diff --git a/tests/auto/uiloader/baseline/css_borderimage.ui b/tests/auto/uiloader/baseline/css_borderimage.ui new file mode 100644 index 0000000..4a59ca2 --- /dev/null +++ b/tests/auto/uiloader/baseline/css_borderimage.ui @@ -0,0 +1,125 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Form</class> + <widget class="QWidget" name="Form"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>530</width> + <height>309</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <property name="styleSheet"> + <string notr="true">QLabel { border-width: 28; color: #0f0; background-color: white; } + +#label_repeat_repeat { + border-image: url("images/borderimage.png") 28 repeat repeat; +} + +#label_stretch_repeat { + border-image: url("images/borderimage.png") 28 stretch repeat; +} + +#label_round_repeat { + border-image: url("images/borderimage.png") 28 round repeat; +} + + +#label_repeat_round { + border-image: url("images/borderimage.png") 28 repeat round; +} + +#label_stretch_round { + border-image: url("images/borderimage.png") 28 stretch round; +} + +#label_round_round { + border-image: url("images/borderimage.png") 28 round round; +} + +#label_repeat_stretch { + border-image: url("images/borderimage.png") 28 repeat stretch; +} + +#label_stretch_stretch { + border-image: url("images/borderimage.png") 28 stretch stretch; +} + +#label_round_stretch { + border-image: url("images/borderimage.png") 28 round stretch; +} +</string> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="label_stretch_stretch"> + <property name="text"> + <string>Strecth Stretch</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="label_stretch_round"> + <property name="text"> + <string>Stretch Round</string> + </property> + </widget> + </item> + <item row="0" column="2"> + <widget class="QLabel" name="label_stretch_repeat"> + <property name="text"> + <string>Stretch repeat</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_round_stretch"> + <property name="text"> + <string>Round Stretch</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="label_round_round"> + <property name="text"> + <string>Round Round</string> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QLabel" name="label_round_repeat"> + <property name="text"> + <string>Round Repeat</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_repeat_stretch"> + <property name="text"> + <string>Repeat Stretch</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLabel" name="label_repeat_round"> + <property name="text"> + <string>Repeat Round</string> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QLabel" name="label_repeat_repeat"> + <property name="text"> + <string>Repeat Repeat</string> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/tests/auto/uiloader/baseline/css_borderimage_allwidgets.ui b/tests/auto/uiloader/baseline/css_borderimage_allwidgets.ui new file mode 100644 index 0000000..baba66b --- /dev/null +++ b/tests/auto/uiloader/baseline/css_borderimage_allwidgets.ui @@ -0,0 +1,210 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Form</class> + <widget class="QWidget" name="Form"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>553</width> + <height>368</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <property name="styleSheet"> + <string notr="true">* { border-image: url("images/pushbutton.png") 6 6 6 6; border-width:6px; }</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_4"> + <item> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QFrame" name="frame"> + <layout class="QVBoxLayout" name="verticalLayout_2"> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Each widget should have a background image. including the top level</string> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="pushButton"> + <property name="text"> + <string>PushButton</string> + </property> + </widget> + </item> + <item> + <widget class="QProgressBar" name="progressBar"> + <property name="value"> + <number>24</number> + </property> + </widget> + </item> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWidget" name="widget" native="true"> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QPushButton" name="pushButton_2"> + <property name="text"> + <string>PushButton</string> + </property> + </widget> + </item> + <item> + <widget class="QSpinBox" name="spinBox"/> + </item> + <item> + <widget class="QRadioButton" name="radioButton"> + <property name="text"> + <string>RadioButton</string> + </property> + </widget> + </item> + <item> + <widget class="QScrollArea" name="scrollArea"> + <property name="widgetResizable"> + <bool>true</bool> + </property> + <widget class="QWidget" name="scrollAreaWidgetContents"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>260</width> + <height>197</height> + </rect> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="2" column="0" colspan="2"> + <widget class="QSlider" name="horizontalSlider"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item row="0" column="0"> + <widget class="QLCDNumber" name="lcdNumber"/> + </item> + <item row="0" column="1"> + <widget class="QRadioButton" name="radioButton_2"> + <property name="text"> + <string>RadioButton</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QRadioButton" name="radioButton_3"> + <property name="text"> + <string>RadioButton</string> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QCheckBox" name="checkBox_2"> + <property name="text"> + <string>CheckBox</string> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QCheckBox" name="checkBox_3"> + <property name="text"> + <string>CheckBox</string> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="groupBox"> + <property name="title"> + <string>GroupBox</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_3"> + <item> + <widget class="QCheckBox" name="checkBox"> + <property name="text"> + <string>CheckBox</string> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="lineEdit"> + <property name="text"> + <string>Line Edit</string> + </property> + </widget> + </item> + <item> + <widget class="QListWidget" name="listWidget"> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + </widget> + </item> + </layout> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/tests/auto/uiloader/baseline/css_splitter.ui b/tests/auto/uiloader/baseline/css_splitter.ui new file mode 100644 index 0000000..99dbc18 --- /dev/null +++ b/tests/auto/uiloader/baseline/css_splitter.ui @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Form</class> + <widget class="QWidget" name="Form"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>424</width> + <height>364</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <property name="styleSheet"> + <string notr="true"> QSplitter::handle:vertical { + image: url(images/splitter_horizontal.png); + } + + QSplitter::handle:horizontal { + image: url(images/splitter_vertical.png); + } + +#big_splitter::handle { background-color: blue; border: 3px dashed green; height:50px; } + + + QSplitter::handle:hover { + background-color: qlineargradient(spread:repeat, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 255), stop:0.17 rgba(255, 0, 0, 255), stop:0.18 rgba(255, 255, 255, 255), stop:0.210212 rgba(255, 255, 255, 255), stop:0.220212 rgba(0, 16, 255, 255), stop:0.279897 rgba(0, 16, 255, 255), stop:0.289897 rgba(255, 255, 255, 255), stop:0.32 rgba(255, 255, 255, 255), stop:0.33 rgba(255, 0, 0, 255), stop:1 rgba(255, 0, 0, 255)) + } + + QSplitter::handle:pressed { + background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(9, 41, 4, 255), stop:0.085 rgba(2, 79, 0, 255), stop:0.19 rgba(50, 147, 22, 255), stop:0.275 rgba(236, 191, 49, 255), stop:0.39 rgba(243, 61, 34, 255), stop:0.555 rgba(135, 81, 60, 255), stop:0.667 rgba(121, 75, 255, 255), stop:0.825 rgba(164, 255, 244, 255), stop:0.885 rgba(104, 222, 71, 255), stop:1 rgba(93, 128, 0, 255)); + }</string> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QSplitter" name="splitter_3"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <widget class="QSplitter" name="big_splitter"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <widget class="QTextEdit" name="textEdit"/> + <widget class="QTextEdit" name="textEdit_5"/> + <widget class="QTextEdit" name="textEdit_4"/> + </widget> + <widget class="QSplitter" name="splitter"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <widget class="QTextEdit" name="textEdit_2"/> + <widget class="QTextEdit" name="textEdit_3"/> + </widget> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/tests/auto/uiloader/baseline/css_task255849_downarrow.ui b/tests/auto/uiloader/baseline/css_task255849_downarrow.ui new file mode 100644 index 0000000..d3e99b1 --- /dev/null +++ b/tests/auto/uiloader/baseline/css_task255849_downarrow.ui @@ -0,0 +1,144 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Form</class> + <widget class="QWidget" name="Form"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>275</width> + <height>175</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <property name="styleSheet"> + <string notr="true">/* Some widget were not displaying the arrow if only the arrow was set */ +*::down-arrow { image: url("images/arrow-down.png") } +*::up-arrow { image: url("images/arrow-up.png") } + + +</string> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0" colspan="2"> + <widget class="QComboBox" name="comboBox"/> + </item> + <item row="0" column="2" rowspan="5"> + <widget class="QTreeWidget" name="treeWidget"> + <property name="sortingEnabled"> + <bool>true</bool> + </property> + <attribute name="headerShowSortIndicator" stdset="0"> + <bool>true</bool> + </attribute> + <attribute name="headerShowSortIndicator" stdset="0"> + <bool>true</bool> + </attribute> + <column> + <property name="text"> + <string>1</string> + </property> + </column> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + <item> + <property name="text"> + <string>New Item</string> + </property> + </item> + </widget> + </item> + <item row="1" column="0"> + <widget class="QSpinBox" name="spinBox"/> + </item> + <item row="1" column="1"> + <widget class="QToolButton" name="toolButton"> + <property name="text"> + <string>foo</string> + </property> + <property name="arrowType"> + <enum>Qt::DownArrow</enum> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QDoubleSpinBox" name="doubleSpinBox"/> + </item> + <item row="3" column="0" colspan="2"> + <widget class="QPushButton" name="pushButton"> + <property name="contextMenuPolicy"> + <enum>Qt::NoContextMenu</enum> + </property> + <property name="text"> + <string>PushButton</string> + </property> + </widget> + </item> + <item row="4" column="0"> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/tests/auto/uiloader/baseline/images/arrow-down.png b/tests/auto/uiloader/baseline/images/arrow-down.png Binary files differnew file mode 100644 index 0000000..7c9274f --- /dev/null +++ b/tests/auto/uiloader/baseline/images/arrow-down.png diff --git a/tests/auto/uiloader/baseline/images/arrow-up.png b/tests/auto/uiloader/baseline/images/arrow-up.png Binary files differnew file mode 100644 index 0000000..758a0d1 --- /dev/null +++ b/tests/auto/uiloader/baseline/images/arrow-up.png diff --git a/tests/auto/uiloader/baseline/images/borderimage.png b/tests/auto/uiloader/baseline/images/borderimage.png Binary files differnew file mode 100644 index 0000000..199fc89 --- /dev/null +++ b/tests/auto/uiloader/baseline/images/borderimage.png diff --git a/tests/auto/uiloader/baseline/images/splitter_horizontal.png b/tests/auto/uiloader/baseline/images/splitter_horizontal.png Binary files differnew file mode 100644 index 0000000..66107cf --- /dev/null +++ b/tests/auto/uiloader/baseline/images/splitter_horizontal.png diff --git a/tests/auto/uiloader/baseline/images/splitter_vertical.png b/tests/auto/uiloader/baseline/images/splitter_vertical.png Binary files differnew file mode 100644 index 0000000..f907c0b --- /dev/null +++ b/tests/auto/uiloader/baseline/images/splitter_vertical.png diff --git a/tests/auto/windowsmobile/test/tst_windowsmobile.cpp b/tests/auto/windowsmobile/test/tst_windowsmobile.cpp index 843974d..8c7c021 100644 --- a/tests/auto/windowsmobile/test/tst_windowsmobile.cpp +++ b/tests/auto/windowsmobile/test/tst_windowsmobile.cpp @@ -44,8 +44,10 @@ #include <QtCore/QDebug> #include <QtCore/QObject> #include <QtGui> +#ifdef Q_OS_WINCE_WM #include <windows.h> #include <ddhelper.h> +#endif |