tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
--- a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp	Tue Feb 02 00:43:10 2010 +0200
+++ b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp	Wed Mar 31 11:06:36 2010 +0300
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -46,6 +46,7 @@
 #include <QtGui/qgraphicsview.h>
 #include <QtGui/qgraphicsscene.h>
 #include <QtGui/qgraphicsitem.h>
+#include <QtGui/qgraphicswidget.h>
 #include <QtGui/qstyleoption.h>
 
 #include "../../shared/util.h"
@@ -70,6 +71,10 @@
     void grayscale();
     void colorize();
     void drawPixmapItem();
+    void deviceCoordinateTranslateCaching();
+    void inheritOpacity();
+    void dropShadowClipping();
+    void childrenVisibilityShouldInvalidateCache();
 };
 
 void tst_QGraphicsEffect::initTestCase()
@@ -78,8 +83,8 @@
 class CustomItem : public QGraphicsRectItem
 {
 public:
-    CustomItem(qreal x, qreal y, qreal width, qreal height)
-        : QGraphicsRectItem(x, y, width, height), numRepaints(0),
+    CustomItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = 0)
+        : QGraphicsRectItem(x, y, width, height, parent), numRepaints(0),
           m_painter(0), m_styleOption(0)
     {}
 
@@ -507,6 +512,7 @@
     QGraphicsView view(&scene);
     view.show();
     QTest::qWaitForWindowShown(&view);
+    QTRY_VERIFY(effect->repaints >= 1);
 
     item->rotate(180);
     QTest::qWait(50);
@@ -514,6 +520,143 @@
     QTRY_VERIFY(effect->repaints >= 2);
 }
 
+class DeviceEffect : public QGraphicsEffect
+{
+public:
+    QRectF boundingRectFor(const QRectF &rect) const
+    { return rect; }
+
+    void draw(QPainter *painter)
+    {
+        QPoint offset;
+        QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset, QGraphicsEffect::NoPad);
+
+        if (pixmap.isNull())
+            return;
+
+        painter->save();
+        painter->setWorldTransform(QTransform());
+        painter->drawPixmap(offset, pixmap);
+        painter->restore();
+    }
+};
+
+void tst_QGraphicsEffect::deviceCoordinateTranslateCaching()
+{
+    QGraphicsScene scene;
+    CustomItem *item = new CustomItem(0, 0, 10, 10);
+    scene.addItem(item);
+    scene.setSceneRect(0, 0, 50, 0);
+
+    item->setGraphicsEffect(new DeviceEffect);
+    item->setPen(Qt::NoPen);
+    item->setBrush(Qt::red);
+
+    QGraphicsView view(&scene);
+    view.show();
+    QTest::qWaitForWindowShown(&view);
+
+    QTRY_VERIFY(item->numRepaints >= 1);
+    int numRepaints = item->numRepaints;
+
+    item->translate(10, 0);
+    QTest::qWait(50);
+
+    QVERIFY(item->numRepaints == numRepaints);
+}
+
+void tst_QGraphicsEffect::inheritOpacity()
+{
+    QGraphicsScene scene;
+    QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 10, 10);
+    CustomItem *item = new CustomItem(0, 0, 10, 10, rectItem);
+
+    scene.addItem(rectItem);
+
+    item->setGraphicsEffect(new DeviceEffect);
+    item->setPen(Qt::NoPen);
+    item->setBrush(Qt::red);
+
+    rectItem->setOpacity(0.5);
+
+    QGraphicsView view(&scene);
+    view.show();
+    QTest::qWaitForWindowShown(&view);
+
+    QTRY_VERIFY(item->numRepaints >= 1);
+
+    int numRepaints = item->numRepaints;
+
+    rectItem->setOpacity(1);
+    QTest::qWait(50);
+
+    // item should have been rerendered due to opacity changing
+    QTRY_VERIFY(item->numRepaints > numRepaints);
+}
+
+void tst_QGraphicsEffect::dropShadowClipping()
+{
+    QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
+    img.fill(0xffffffff);
+
+    QGraphicsScene scene;
+    QGraphicsRectItem *item = new QGraphicsRectItem(-5, -500, 10, 1000);
+    item->setGraphicsEffect(new QGraphicsDropShadowEffect);
+    item->setPen(Qt::NoPen);
+    item->setBrush(Qt::red);
+
+    scene.addItem(item);
+
+    QPainter p(&img);
+    scene.render(&p, img.rect(), QRect(-64, -64, 128, 128));
+    p.end();
+
+    for (int y = 1; y < img.height(); ++y)
+        for (int x = 0; x < img.width(); ++x)
+            QCOMPARE(img.pixel(x, y), img.pixel(x, y-1));
+}
+
+class MyGraphicsItem : public QGraphicsWidget
+{
+public:
+    MyGraphicsItem(QGraphicsItem *parent = 0) :
+            QGraphicsWidget(parent), nbPaint(0)
+    {}
+    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+    {
+        nbPaint++;
+        QGraphicsWidget::paint(painter, option, widget);
+    }
+    int nbPaint;
+};
+
+void tst_QGraphicsEffect::childrenVisibilityShouldInvalidateCache()
+{
+    QGraphicsScene scene;
+    MyGraphicsItem parent;
+    parent.resize(200, 200);
+    QGraphicsWidget child(&parent);
+    child.resize(200, 200);
+    child.setVisible(false);
+    scene.addItem(&parent);
+    QGraphicsView view(&scene);
+    view.show();
+    QApplication::setActiveWindow(&view);
+    QTest::qWaitForWindowShown(&view);
+    QTRY_COMPARE(parent.nbPaint, 1);
+    //we set an effect on the parent
+    parent.setGraphicsEffect(new QGraphicsDropShadowEffect(&parent));
+    //flush the events
+    QApplication::processEvents();
+    //new effect applied->repaint
+    QCOMPARE(parent.nbPaint, 2);
+    child.setVisible(true);
+    //flush the events
+    QApplication::processEvents();
+    //a new child appears we need to redraw the effect.
+    QCOMPARE(parent.nbPaint, 3);
+}
+
 QTEST_MAIN(tst_QGraphicsEffect)
 #include "tst_qgraphicseffect.moc"