diff options
author | Bjørn Erik Nilsen <bjorn.nilsen@nokia.com> | 2009-08-21 13:26:33 (GMT) |
---|---|---|
committer | Bjørn Erik Nilsen <bjorn.nilsen@nokia.com> | 2009-08-21 13:26:33 (GMT) |
commit | ef93943579210dffaa83185cea99146fa9f3f9b6 (patch) | |
tree | ca1a4bf586a54dedd6d8e85d086a8f7cd8c3dbcd /tests | |
parent | 00ba962d428dcdff70fcde74a9e5cb316545cef6 (diff) | |
parent | 1b305c739979f7d0e8cb6ebbac6d29ca2e6b060d (diff) | |
download | Qt-ef93943579210dffaa83185cea99146fa9f3f9b6.zip Qt-ef93943579210dffaa83185cea99146fa9f3f9b6.tar.gz Qt-ef93943579210dffaa83185cea99146fa9f3f9b6.tar.bz2 |
Merge commit 'qt-kinetic/kinetic-graphicseffect'
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/auto.pro | 2 | ||||
-rw-r--r-- | tests/auto/qgraphicseffect/qgraphicseffect.pro | 3 | ||||
-rw-r--r-- | tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp | 347 | ||||
-rw-r--r-- | tests/auto/qgraphicseffectsource/qgraphicseffectsource.pro | 3 | ||||
-rw-r--r-- | tests/auto/qgraphicseffectsource/tst_qgraphicseffectsource.cpp | 323 | ||||
-rw-r--r-- | tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 156 | ||||
-rw-r--r-- | tests/auto/qwidget/tst_qwidget.cpp | 32 |
7 files changed, 866 insertions, 0 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 0f3c099..2a1feb3 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -134,6 +134,8 @@ SUBDIRS += \ qgetputenv \ qgl \ qglobal \ + qgraphicseffect \ + qgraphicseffectsource \ qgraphicsgridlayout \ qgraphicsitem \ qgraphicsitemanimation \ diff --git a/tests/auto/qgraphicseffect/qgraphicseffect.pro b/tests/auto/qgraphicseffect/qgraphicseffect.pro new file mode 100644 index 0000000..7effaca --- /dev/null +++ b/tests/auto/qgraphicseffect/qgraphicseffect.pro @@ -0,0 +1,3 @@ +load(qttest_p4) +SOURCES += tst_qgraphicseffect.cpp + diff --git a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp new file mode 100644 index 0000000..1e62bf8 --- /dev/null +++ b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp @@ -0,0 +1,347 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 <QtGui/qgraphicseffect.h> +#include <QtGui/qgraphicsview.h> +#include <QtGui/qgraphicsscene.h> +#include <QtGui/qgraphicsitem.h> +#include <QtGui/qstyleoption.h> + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QGraphicsEffect : public QObject +{ + Q_OBJECT +public slots: + void initTestCase(); + +private slots: + void setEnabled(); + void source(); + void boundingRectFor(); + void boundingRect(); + void draw(); +}; + +void tst_QGraphicsEffect::initTestCase() +{} + +class CustomItem : public QGraphicsRectItem +{ +public: + CustomItem(qreal x, qreal y, qreal width, qreal height) + : QGraphicsRectItem(x, y, width, height), numRepaints(0), + m_painter(0), m_styleOption(0) + {} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + m_painter = painter; + m_styleOption = option; + ++numRepaints; + QGraphicsRectItem::paint(painter, option, widget); + } + + void reset() + { + numRepaints = 0; + m_painter = 0; + m_styleOption = 0; + } + + int numRepaints; + QPainter *m_painter; + const QStyleOption *m_styleOption; +}; + +class CustomEffect : public QGraphicsEffect +{ +public: + CustomEffect() + : QGraphicsEffect(), numRepaints(0), m_margin(10), + doNothingInDraw(false), m_painter(0), m_styleOption(0), m_source(0) + {} + + QRectF boundingRectFor(const QRectF &rect) const + { return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); } + + void reset() + { + numRepaints = 0; + m_sourceChangedFlags = QGraphicsEffect::ChangeFlags(); + m_painter = 0; + m_styleOption = 0; + m_source = 0; + } + + void setMargin(int margin) + { + m_margin = margin; + updateBoundingRect(); + } + + int margin() const + { return m_margin; } + + void draw(QPainter *painter, QGraphicsEffectSource *source) + { + ++numRepaints; + if (doNothingInDraw) + return; + m_source = source; + m_painter = painter; + m_styleOption = source->styleOption(); + source->draw(painter); + } + + void sourceChanged(QGraphicsEffect::ChangeFlags flags) + { m_sourceChangedFlags |= flags; } + + int numRepaints; + int m_margin; + QGraphicsEffect::ChangeFlags m_sourceChangedFlags; + bool doNothingInDraw; + QPainter *m_painter; + const QStyleOption *m_styleOption; + QGraphicsEffectSource *m_source; +}; + +void tst_QGraphicsEffect::setEnabled() +{ + CustomEffect effect; + QVERIFY(effect.isEnabled()); + + effect.setEnabled(false); + QVERIFY(!effect.isEnabled()); +} + +void tst_QGraphicsEffect::source() +{ + QPointer<CustomEffect> effect = new CustomEffect; + QVERIFY(!effect->source()); + QVERIFY(!effect->m_sourceChangedFlags); + + // Install effect on QGraphicsItem. + QGraphicsItem *item = new QGraphicsRectItem(0, 0, 10, 10); + item->setGraphicsEffect(effect); + QVERIFY(effect->source()); + QCOMPARE(effect->source()->graphicsItem(), item); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceAttached); + effect->reset(); + + // Make sure disabling/enabling the effect doesn't change the source. + effect->setEnabled(false); + QVERIFY(effect->source()); + QCOMPARE(effect->source()->graphicsItem(), item); + QVERIFY(!effect->m_sourceChangedFlags); + effect->reset(); + + effect->setEnabled(true); + QVERIFY(effect->source()); + QCOMPARE(effect->source()->graphicsItem(), item); + QVERIFY(!effect->m_sourceChangedFlags); + effect->reset(); + + // Uninstall effect on QGraphicsItem. + effect->reset(); + item->setGraphicsEffect(0); + QVERIFY(!effect->source()); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceDetached); + + // The item takes ownership and should delete the effect when destroyed. + item->setGraphicsEffect(effect); + QPointer<QGraphicsEffectSource> source = effect->source(); + QVERIFY(source); + QCOMPARE(source->graphicsItem(), item); + delete item; + QVERIFY(!effect); + QVERIFY(!source); +} + +void tst_QGraphicsEffect::boundingRectFor() +{ + CustomEffect effect; + int margin = effect.margin(); + const QRectF source(0, 0, 100, 100); + QCOMPARE(effect.boundingRectFor(source), source.adjusted(-margin, -margin, margin, margin)); + + effect.setMargin(margin = 20); + QCOMPARE(effect.boundingRectFor(source), source.adjusted(-margin, -margin, margin, margin)); +} + +void tst_QGraphicsEffect::boundingRect() +{ + // No source; empty bounding rect. + CustomEffect *effect = new CustomEffect; + QCOMPARE(effect->boundingRect(), QRectF()); + + // Install effect on QGraphicsItem. + QRectF itemRect(0, 0, 100, 100); + QGraphicsRectItem *item = new QGraphicsRectItem; + item->setRect(itemRect); + item->setGraphicsEffect(effect); + int margin = effect->margin(); + QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); + QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); + + // Make sure disabling/enabling the effect doesn't change the bounding rect. + effect->setEnabled(false); + QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); + QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); + effect->setEnabled(true); + QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); + QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); + + // Change effect margins. + effect->setMargin(margin = 20); + QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin)); + QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect)); + + // Uninstall effect on QGraphicsItem. + item->setGraphicsEffect(0); + QCOMPARE(effect->boundingRect(), QRectF()); + + delete effect; + delete item; +} + +void tst_QGraphicsEffect::draw() +{ + QGraphicsScene scene; + CustomItem *item = new CustomItem(0, 0, 100, 100); + scene.addItem(item); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + item->reset(); + + // Make sure installing the effect triggers a repaint. + CustomEffect *effect = new CustomEffect; + item->setGraphicsEffect(effect); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 1); + + // Make sure QPainter* and QStyleOptionGraphicsItem* stays persistent + // during QGraphicsEffect::draw/QGraphicsItem::paint. + QVERIFY(effect->m_painter); + QCOMPARE(effect->m_painter, item->m_painter); + QCOMPARE(effect->m_styleOption, item->m_styleOption); + // Make sure QGraphicsEffect::source is persistent. + QCOMPARE(effect->m_source, effect->source()); + effect->reset(); + item->reset(); + + // Make sure updating the source triggers a repaint. + item->update(); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 1); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceInvalidated); + effect->reset(); + item->reset(); + + // Make sure changing the effect's bounding rect triggers a repaint. + effect->setMargin(20); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 1); + effect->reset(); + item->reset(); + + // Make sure change the item's bounding rect triggers a repaint. + item->setRect(0, 0, 50, 50); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 1); + QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceBoundingRectChanged); + effect->reset(); + item->reset(); + + // Make sure the effect is the one to issue a repaint of the item. + effect->doNothingInDraw = true; + item->update(); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 0); + effect->doNothingInDraw = false; + effect->reset(); + item->reset(); + + // Make sure we update the source when disabling/enabling the effect. + effect->setEnabled(false); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 0); + QCOMPARE(item->numRepaints, 1); + effect->reset(); + item->reset(); + + effect->setEnabled(true); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 1); + QCOMPARE(item->numRepaints, 1); + effect->reset(); + item->reset(); + + // Effect is already enabled; nothing should happen. + effect->setEnabled(true); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 0); + QCOMPARE(item->numRepaints, 0); + + // Make sure uninstalling an effect triggers a repaint. + item->setGraphicsEffect(0); + QTest::qWait(50); + QCOMPARE(effect->numRepaints, 0); + QCOMPARE(item->numRepaints, 1); + delete effect; +} + +QTEST_MAIN(tst_QGraphicsEffect) +#include "tst_qgraphicseffect.moc" + diff --git a/tests/auto/qgraphicseffectsource/qgraphicseffectsource.pro b/tests/auto/qgraphicseffectsource/qgraphicseffectsource.pro new file mode 100644 index 0000000..d506c6d --- /dev/null +++ b/tests/auto/qgraphicseffectsource/qgraphicseffectsource.pro @@ -0,0 +1,3 @@ +load(qttest_p4) +SOURCES += tst_qgraphicseffectsource.cpp + diff --git a/tests/auto/qgraphicseffectsource/tst_qgraphicseffectsource.cpp b/tests/auto/qgraphicseffectsource/tst_qgraphicseffectsource.cpp new file mode 100644 index 0000000..e7f7ba5 --- /dev/null +++ b/tests/auto/qgraphicseffectsource/tst_qgraphicseffectsource.cpp @@ -0,0 +1,323 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 <QtGui/qgraphicseffect.h> +#include <QtGui/qgraphicsview.h> +#include <QtGui/qgraphicsscene.h> +#include <QtGui/qgraphicsitem.h> +#include <QtGui/qstyleoption.h> + +//TESTED_CLASS= +//TESTED_FILES= + +class CustomItem : public QGraphicsRectItem +{ +public: + CustomItem(qreal x, qreal y, qreal width, qreal height) + : QGraphicsRectItem(x, y, width, height), numRepaints(0), + m_painter(0), m_styleOption(0) + {} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + m_painter = painter; + m_styleOption = option; + ++numRepaints; + QGraphicsRectItem::paint(painter, option, widget); + } + + void reset() + { + numRepaints = 0; + m_painter = 0; + m_styleOption = 0; + } + + int numRepaints; + QPainter *m_painter; + const QStyleOption *m_styleOption; +}; + +class CustomEffect : public QGraphicsEffect +{ +public: + CustomEffect() + : QGraphicsEffect(), numRepaints(0), m_margin(10), m_sourceChanged(false), + m_sourceBoundingRectChanged(false), doNothingInDraw(false), + storeDeviceDependentStuff(false), + m_painter(0), m_styleOption(0), m_source(0) + {} + + QRectF boundingRectFor(const QRectF &rect) const + { return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); } + + void reset() + { + numRepaints = 0; + m_sourceChanged = false; + m_sourceBoundingRectChanged = false; + m_painter = 0; + m_styleOption = 0; + m_source = 0; + deviceCoordinatesPixmap = QPixmap(); + deviceRect = QRect(); + sourceDeviceBoundingRect = QRectF(); + } + + void setMargin(int margin) + { + m_margin = margin; + updateBoundingRect(); + } + + int margin() const + { return m_margin; } + + void draw(QPainter *painter, QGraphicsEffectSource *source) + { + ++numRepaints; + if (storeDeviceDependentStuff) { + deviceCoordinatesPixmap = source->pixmap(Qt::DeviceCoordinates); + deviceRect = source->deviceRect(); + sourceDeviceBoundingRect = source->boundingRect(Qt::DeviceCoordinates); + } + if (doNothingInDraw) + return; + m_source = source; + m_painter = painter; + m_styleOption = source->styleOption(); + source->draw(painter); + } + + void sourceChanged() + { m_sourceChanged = true; } + + void sourceBoundingRectChanged() + { m_sourceBoundingRectChanged = true; } + + int numRepaints; + int m_margin; + bool m_sourceChanged; + bool m_sourceBoundingRectChanged; + bool doNothingInDraw; + bool storeDeviceDependentStuff; + QPainter *m_painter; + const QStyleOption *m_styleOption; + QGraphicsEffectSource *m_source; + QPixmap deviceCoordinatesPixmap; + QRect deviceRect; + QRectF sourceDeviceBoundingRect; +}; + +class tst_QGraphicsEffectSource : public QObject +{ + Q_OBJECT +public slots: + void initTestCase(); + void cleanupTestCase(); + void init(); + +private slots: + void graphicsItem(); + void styleOption(); + void isPixmap(); + void draw(); + void update(); + void boundingRect(); + void deviceRect(); + void pixmap(); + +private: + QGraphicsView *view; + QGraphicsScene *scene; + CustomItem *item; + CustomEffect *effect; +}; + +void tst_QGraphicsEffectSource::initTestCase() +{ + scene = new QGraphicsScene; + item = new CustomItem(0, 0, 100, 100); + effect = new CustomEffect; + item->setGraphicsEffect(effect); + scene->addItem(item); + view = new QGraphicsView(scene); + view->show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(view); +#endif + QTest::qWait(200); +} + +void tst_QGraphicsEffectSource::cleanupTestCase() +{ + delete view; +} + +void tst_QGraphicsEffectSource::init() +{ + QVERIFY(effect); + QVERIFY(item); + QVERIFY(effect->source()); + effect->reset(); + effect->storeDeviceDependentStuff = false; + effect->doNothingInDraw = false; + item->reset(); +} + +void tst_QGraphicsEffectSource::graphicsItem() +{ + QVERIFY(effect->source()); + QCOMPARE(effect->source()->graphicsItem(), item); +} + +void tst_QGraphicsEffectSource::styleOption() +{ + // We don't have style options unless the source is drawing. + QVERIFY(effect->source()); + QVERIFY(!effect->source()->styleOption()); + + // Trigger an update and check that the style option in QGraphicsEffect::draw + // was the same as the one in QGraphicsItem::paint. + QCOMPARE(item->numRepaints, 0); + QCOMPARE(effect->numRepaints, 0); + item->update(); + QTest::qWait(50); + QCOMPARE(item->numRepaints, 1); + QCOMPARE(effect->numRepaints, 1); + QVERIFY(effect->m_styleOption); + QCOMPARE(effect->m_styleOption, item->m_styleOption); +} + +void tst_QGraphicsEffectSource::isPixmap() +{ + // Current source is a CustomItem (which is not a pixmap item). + QVERIFY(!effect->source()->isPixmap()); + + // Make sure isPixmap() returns true for QGraphicsPixmapItem. + QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem; + CustomEffect *anotherEffect = new CustomEffect; + pixmapItem->setGraphicsEffect(anotherEffect); + QVERIFY(anotherEffect->source()); + QCOMPARE(anotherEffect->source()->graphicsItem(), static_cast<QGraphicsItem *>(pixmapItem)); + QVERIFY(anotherEffect->source()->isPixmap()); + delete pixmapItem; +} + +void tst_QGraphicsEffectSource::draw() +{ + // The source can only draw as a result of QGraphicsEffect::draw. + QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::draw: Can only begin as a result of QGraphicsEffect::draw"); + QPixmap dummyPixmap(10, 10); + QPainter dummyPainter(&dummyPixmap); + effect->source()->draw(&dummyPainter); +} + +void tst_QGraphicsEffectSource::update() +{ + QCOMPARE(item->numRepaints, 0); + QCOMPARE(effect->numRepaints, 0); + + effect->source()->update(); + QTest::qWait(50); + + QCOMPARE(item->numRepaints, 1); + QCOMPARE(effect->numRepaints, 1); +} + +void tst_QGraphicsEffectSource::boundingRect() +{ + QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context"); + QCOMPARE(effect->source()->boundingRect(Qt::DeviceCoordinates), QRectF()); + + QRectF itemBoundingRect = item->boundingRect(); + if (!item->children().isEmpty()) + itemBoundingRect |= item->childrenBoundingRect(); + + // We can at least check that the device bounding rect was correct in QGraphicsEffect::draw. + effect->storeDeviceDependentStuff = true; + effect->source()->update(); + QTest::qWait(50); + const QTransform deviceTransform = item->deviceTransform(view->viewportTransform()); + QCOMPARE(effect->sourceDeviceBoundingRect, deviceTransform.mapRect(itemBoundingRect)); + + // Bounding rect in logical coordinates is of course fine. + QCOMPARE(effect->source()->boundingRect(Qt::LogicalCoordinates), itemBoundingRect); + // Make sure default value is Qt::LogicalCoordinates. + QCOMPARE(effect->source()->boundingRect(), itemBoundingRect); +} + +void tst_QGraphicsEffectSource::deviceRect() +{ + QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::deviceRect: Not yet implemented, lacking device context"); + QCOMPARE(effect->source()->deviceRect(), QRect()); + + // We can at least check that the rect was correct in QGraphicsEffect::draw. + effect->storeDeviceDependentStuff = true; + effect->source()->update(); + QTest::qWait(50); + QCOMPARE(effect->deviceRect, view->viewport()->rect()); +} + +void tst_QGraphicsEffectSource::pixmap() +{ + QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context"); + QCOMPARE(effect->source()->pixmap(Qt::DeviceCoordinates), QPixmap()); + + // We can at least verify a valid pixmap from QGraphicsEffect::draw. + effect->storeDeviceDependentStuff = true; + effect->source()->update(); + QTest::qWait(50); + QVERIFY(!effect->deviceCoordinatesPixmap.isNull()); + + // Pixmaps in logical coordinates we can do fine. + QPixmap pixmap1 = effect->source()->pixmap(Qt::LogicalCoordinates); + QVERIFY(!pixmap1.isNull()); + + // Make sure default value is Qt::LogicalCoordinates. + QPixmap pixmap2 = effect->source()->pixmap(); + QCOMPARE(pixmap1, pixmap2); +} + +QTEST_MAIN(tst_QGraphicsEffectSource) +#include "tst_qgraphicseffectsource.moc" + diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index 10f0e42..c335f16 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -58,6 +58,7 @@ #include <QPainter> #include <QScrollBar> #include <QVBoxLayout> +#include <QGraphicsEffect> //TESTED_CLASS= //TESTED_FILES= @@ -216,6 +217,7 @@ private slots: void childrenBoundingRect(); void childrenBoundingRectTransformed(); void childrenBoundingRect2(); + void childrenBoundingRect3(); void group(); void setGroup(); void nestedGroups(); @@ -276,12 +278,14 @@ private slots: void sorting(); void itemHasNoContents(); void hitTestUntransformableItem(); + void hitTestGraphicsEffectItem(); void focusProxy(); void autoDetectFocusProxy(); void subFocus(); void reverseCreateAutoFocusProxy(); void focusProxyDeletion(); void negativeZStacksBehindParent(); + void setGraphicsEffect(); // task specific tests below me void task141694_textItemEnsureVisible(); @@ -3085,6 +3089,39 @@ void tst_QGraphicsItem::childrenBoundingRect2() QCOMPARE(box.childrenBoundingRect(), QRectF(0, 0, 100, 100)); } +void tst_QGraphicsItem::childrenBoundingRect3() +{ + 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); + + // Try to mess up the cached bounding rect. + (void)rect2->childrenBoundingRect(); + + 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)); +} + void tst_QGraphicsItem::group() { QGraphicsScene scene; @@ -7335,6 +7372,94 @@ void tst_QGraphicsItem::hitTestUntransformableItem() QCOMPARE(items.at(0), static_cast<QGraphicsItem*>(item3)); } +void tst_QGraphicsItem::hitTestGraphicsEffectItem() +{ + QGraphicsScene scene; + scene.setSceneRect(-100, -100, 200, 200); + + QGraphicsView view(&scene); + view.show(); +#ifdef Q_WS_X11 + qt_x11_wait_for_window_manager(&view); +#endif + QTest::qWait(100); + + // Confuse the BSP with dummy items. + QGraphicsRectItem *dummy = new QGraphicsRectItem(0, 0, 20, 20); + dummy->setPos(-100, -100); + scene.addItem(dummy); + for (int i = 0; i < 100; ++i) { + QGraphicsItem *parent = dummy; + dummy = new QGraphicsRectItem(0, 0, 20, 20); + dummy->setPos(-100 + i, -100 + i); + dummy->setParentItem(parent); + } + + const QRectF itemBoundingRect(0, 0, 20, 20); + EventTester *item1 = new EventTester; + item1->br = itemBoundingRect; + item1->setPos(-200, -200); + + EventTester *item2 = new EventTester; + item2->br = itemBoundingRect; + item2->setFlag(QGraphicsItem::ItemIgnoresTransformations); + item2->setParentItem(item1); + item2->setPos(200, 200); + + EventTester *item3 = new EventTester; + item3->br = itemBoundingRect; + item3->setParentItem(item2); + item3->setPos(80, 80); + + scene.addItem(item1); + QTest::qWait(100); + + item1->repaints = 0; + item2->repaints = 0; + item3->repaints = 0; + + // Apply shadow effect to the entire sub-tree. + QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect; + shadow->setOffset(-20, -20); + item1->setGraphicsEffect(shadow); + QTest::qWait(50); + + // Make sure all items are repainted. + QCOMPARE(item1->repaints, 1); + QCOMPARE(item2->repaints, 1); + QCOMPARE(item3->repaints, 1); + + // Make sure an item doesn't respond to a click on its shadow. + QList<QGraphicsItem *> items = scene.items(QPointF(75, 75)); + QVERIFY(items.isEmpty()); + items = scene.items(QPointF(80, 80)); + QCOMPARE(items.size(), 1); + QCOMPARE(items.at(0), static_cast<EventTester *>(item3)); + + item1->repaints = 0; + item2->repaints = 0; + item3->repaints = 0; + + view.viewport()->update(75, 75, 20, 20); + QTest::qWait(50); + + // item1 is the effect source and must therefore be repainted. + // item2 intersects with the exposed region + // item3 is just another child outside the exposed region + QCOMPARE(item1->repaints, 1); + QCOMPARE(item2->repaints, 1); + QCOMPARE(item3->repaints, 0); + + scene.setItemIndexMethod(QGraphicsScene::NoIndex); + QTest::qWait(100); + + items = scene.items(QPointF(75, 75)); + QVERIFY(items.isEmpty()); + items = scene.items(QPointF(80, 80)); + QCOMPARE(items.size(), 1); + QCOMPARE(items.at(0), static_cast<EventTester *>(item3)); +} + void tst_QGraphicsItem::focusProxy() { QGraphicsScene scene; @@ -7584,5 +7709,36 @@ void tst_QGraphicsItem::negativeZStacksBehindParent() QVERIFY(rect.flags() & QGraphicsItem::ItemStacksBehindParent); } +void tst_QGraphicsItem::setGraphicsEffect() +{ + // Check that we don't have any effect by default. + QGraphicsItem *item = new QGraphicsRectItem(0, 0, 10, 10); + QVERIFY(!item->graphicsEffect()); + + // SetGet check. + QPointer<QGraphicsEffect> blurEffect = new QGraphicsBlurEffect; + item->setGraphicsEffect(blurEffect); + QCOMPARE(item->graphicsEffect(), static_cast<QGraphicsEffect *>(blurEffect)); + + // Ensure the existing effect is deleted when setting a new one. + QPointer<QGraphicsEffect> shadowEffect = new QGraphicsDropShadowEffect; + item->setGraphicsEffect(shadowEffect); + QVERIFY(!blurEffect); + QCOMPARE(item->graphicsEffect(), static_cast<QGraphicsEffect *>(shadowEffect)); + blurEffect = new QGraphicsBlurEffect; + + // Ensure the effect is uninstalled when setting it on a new target. + QGraphicsItem *anotherItem = new QGraphicsRectItem(0, 0, 10, 10); + anotherItem->setGraphicsEffect(blurEffect); + item->setGraphicsEffect(blurEffect); + QVERIFY(!anotherItem->graphicsEffect()); + QVERIFY(!shadowEffect); + + // Ensure the existing effect is deleted when deleting the item. + delete item; + QVERIFY(!blurEffect); + delete anotherItem; +} + QTEST_MAIN(tst_QGraphicsItem) #include "tst_qgraphicsitem.moc" diff --git a/tests/auto/qwidget/tst_qwidget.cpp b/tests/auto/qwidget/tst_qwidget.cpp index 34971a9..36f4ddf 100644 --- a/tests/auto/qwidget/tst_qwidget.cpp +++ b/tests/auto/qwidget/tst_qwidget.cpp @@ -355,6 +355,7 @@ private slots: void focusWidget_task254563(); void rectOutsideCoordinatesLimit_task144779(); + void setGraphicsEffect(); private: bool ensureScreenSize(int width, int height); @@ -9169,5 +9170,36 @@ void tst_QWidget::inputFocus_task257832() delete widget; } +void tst_QWidget::setGraphicsEffect() +{ + // Check that we don't have any effect by default. + QWidget *widget = new QWidget; + QVERIFY(!widget->graphicsEffect()); + + // SetGet check. + QPointer<QGraphicsEffect> blurEffect = new QGraphicsBlurEffect; + widget->setGraphicsEffect(blurEffect); + QCOMPARE(widget->graphicsEffect(), static_cast<QGraphicsEffect *>(blurEffect)); + + // Ensure the existing effect is deleted when setting a new one. + QPointer<QGraphicsEffect> shadowEffect = new QGraphicsDropShadowEffect; + widget->setGraphicsEffect(shadowEffect); + QVERIFY(!blurEffect); + QCOMPARE(widget->graphicsEffect(), static_cast<QGraphicsEffect *>(shadowEffect)); + blurEffect = new QGraphicsBlurEffect; + + // Ensure the effect is uninstalled when setting it on a new target. + QWidget *anotherWidget = new QWidget; + anotherWidget->setGraphicsEffect(blurEffect); + widget->setGraphicsEffect(blurEffect); + QVERIFY(!anotherWidget->graphicsEffect()); + QVERIFY(!shadowEffect); + + // Ensure the existing effect is deleted when deleting the widget. + delete widget; + QVERIFY(!blurEffect); + delete anotherWidget; +} + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc" |