tests/auto/qgraphicseffectsource/tst_qgraphicseffectsource.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtTest/QtTest>
       
    43 #include <QtGui/qgraphicseffect.h>
       
    44 #include <QtGui/qgraphicsview.h>
       
    45 #include <QtGui/qgraphicsscene.h>
       
    46 #include <QtGui/qgraphicsitem.h>
       
    47 #include <QtGui/qstyleoption.h>
       
    48 
       
    49 //TESTED_CLASS=
       
    50 //TESTED_FILES=
       
    51 
       
    52 class CustomItem : public QGraphicsRectItem
       
    53 {
       
    54 public:
       
    55     CustomItem(qreal x, qreal y, qreal width, qreal height)
       
    56         : QGraphicsRectItem(x, y, width, height), numRepaints(0),
       
    57           m_painter(0), m_styleOption(0)
       
    58     {}
       
    59 
       
    60     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    61     {
       
    62         m_painter = painter;
       
    63         m_styleOption = option;
       
    64         ++numRepaints;
       
    65         QGraphicsRectItem::paint(painter, option, widget);
       
    66     }
       
    67 
       
    68     void reset()
       
    69     {
       
    70         numRepaints = 0;
       
    71         m_painter = 0;
       
    72         m_styleOption = 0;
       
    73     }
       
    74 
       
    75     int numRepaints;
       
    76     QPainter *m_painter;
       
    77     const QStyleOption *m_styleOption;
       
    78 };
       
    79 
       
    80 class CustomEffect : public QGraphicsEffect
       
    81 {
       
    82 public:
       
    83     CustomEffect()
       
    84         : QGraphicsEffect(), numRepaints(0), m_margin(10), m_sourceChanged(false),
       
    85           m_sourceBoundingRectChanged(false), doNothingInDraw(false),
       
    86           storeDeviceDependentStuff(false),
       
    87           m_painter(0), m_styleOption(0), m_source(0)
       
    88     {}
       
    89 
       
    90     QRectF boundingRectFor(const QRectF &rect) const
       
    91     { return rect.adjusted(-m_margin, -m_margin, m_margin, m_margin); }
       
    92 
       
    93     void reset()
       
    94     {
       
    95         numRepaints = 0;
       
    96         m_sourceChanged = false;
       
    97         m_sourceBoundingRectChanged = false;
       
    98         m_painter = 0;
       
    99         m_styleOption = 0;
       
   100         m_source = 0;
       
   101         deviceCoordinatesPixmap = QPixmap();
       
   102         deviceRect = QRect();
       
   103         sourceDeviceBoundingRect = QRectF();
       
   104     }
       
   105 
       
   106     void setMargin(int margin)
       
   107     {
       
   108         m_margin = margin;
       
   109         updateBoundingRect();
       
   110     }
       
   111 
       
   112     int margin() const
       
   113     { return m_margin; }
       
   114 
       
   115     void draw(QPainter *painter, QGraphicsEffectSource *source)
       
   116     {
       
   117         ++numRepaints;
       
   118         if (storeDeviceDependentStuff) {
       
   119             deviceCoordinatesPixmap = source->pixmap(Qt::DeviceCoordinates);
       
   120             deviceRect = source->deviceRect();
       
   121             sourceDeviceBoundingRect = source->boundingRect(Qt::DeviceCoordinates);
       
   122         }
       
   123         if (doNothingInDraw)
       
   124             return;
       
   125         m_source = source;
       
   126         m_painter = painter;
       
   127         m_styleOption = source->styleOption();
       
   128         source->draw(painter);
       
   129     }
       
   130 
       
   131     void sourceChanged()
       
   132     { m_sourceChanged = true; }
       
   133 
       
   134     void sourceBoundingRectChanged()
       
   135     { m_sourceBoundingRectChanged = true; }
       
   136 
       
   137     int numRepaints;
       
   138     int m_margin;
       
   139     bool m_sourceChanged;
       
   140     bool m_sourceBoundingRectChanged;
       
   141     bool doNothingInDraw;
       
   142     bool storeDeviceDependentStuff;
       
   143     QPainter *m_painter;
       
   144     const QStyleOption *m_styleOption;
       
   145     QGraphicsEffectSource *m_source;
       
   146     QPixmap deviceCoordinatesPixmap;
       
   147     QRect deviceRect;
       
   148     QRectF sourceDeviceBoundingRect;
       
   149 };
       
   150 
       
   151 class tst_QGraphicsEffectSource : public QObject
       
   152 {
       
   153     Q_OBJECT
       
   154 public slots:
       
   155     void initTestCase();
       
   156     void cleanupTestCase();
       
   157     void init();
       
   158 
       
   159 private slots:
       
   160     void graphicsItem();
       
   161     void styleOption();
       
   162     void isPixmap();
       
   163     void draw();
       
   164     void update();
       
   165     void boundingRect();
       
   166     void deviceRect();
       
   167     void pixmap();
       
   168 
       
   169 private:
       
   170     QGraphicsView *view;
       
   171     QGraphicsScene *scene;
       
   172     CustomItem *item;
       
   173     CustomEffect *effect;
       
   174 };
       
   175 
       
   176 void tst_QGraphicsEffectSource::initTestCase()
       
   177 {
       
   178     scene = new QGraphicsScene;
       
   179     item = new CustomItem(0, 0, 100, 100);
       
   180     effect = new CustomEffect;
       
   181     item->setGraphicsEffect(effect);
       
   182     scene->addItem(item);
       
   183     view = new QGraphicsView(scene);
       
   184     view->show();
       
   185 #ifdef Q_WS_X11
       
   186     qt_x11_wait_for_window_manager(view);
       
   187 #endif
       
   188     QTest::qWait(200);
       
   189 }
       
   190 
       
   191 void tst_QGraphicsEffectSource::cleanupTestCase()
       
   192 {
       
   193     delete view;
       
   194 }
       
   195 
       
   196 void tst_QGraphicsEffectSource::init()
       
   197 {
       
   198     QVERIFY(effect);
       
   199     QVERIFY(item);
       
   200     QVERIFY(effect->source());
       
   201     effect->reset();
       
   202     effect->storeDeviceDependentStuff = false;
       
   203     effect->doNothingInDraw = false;
       
   204     item->reset();
       
   205 }
       
   206 
       
   207 void tst_QGraphicsEffectSource::graphicsItem()
       
   208 {
       
   209     QVERIFY(effect->source());
       
   210     QCOMPARE(effect->source()->graphicsItem(), item);
       
   211 }
       
   212 
       
   213 void tst_QGraphicsEffectSource::styleOption()
       
   214 {
       
   215     // We don't have style options unless the source is drawing.
       
   216     QVERIFY(effect->source());
       
   217     QVERIFY(!effect->source()->styleOption());
       
   218 
       
   219     // Trigger an update and check that the style option in QGraphicsEffect::draw
       
   220     // was the same as the one in QGraphicsItem::paint.
       
   221     QCOMPARE(item->numRepaints, 0);
       
   222     QCOMPARE(effect->numRepaints, 0);
       
   223     item->update();
       
   224     QTest::qWait(50);
       
   225     QCOMPARE(item->numRepaints, 1);
       
   226     QCOMPARE(effect->numRepaints, 1);
       
   227     QVERIFY(effect->m_styleOption);
       
   228     QCOMPARE(effect->m_styleOption, item->m_styleOption);
       
   229 }
       
   230 
       
   231 void tst_QGraphicsEffectSource::isPixmap()
       
   232 {
       
   233     // Current source is a CustomItem (which is not a pixmap item).
       
   234     QVERIFY(!effect->source()->isPixmap());
       
   235 
       
   236     // Make sure isPixmap() returns true for QGraphicsPixmapItem.
       
   237     QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem;
       
   238     CustomEffect *anotherEffect = new CustomEffect;
       
   239     pixmapItem->setGraphicsEffect(anotherEffect);
       
   240     QVERIFY(anotherEffect->source());
       
   241     QCOMPARE(anotherEffect->source()->graphicsItem(), static_cast<QGraphicsItem *>(pixmapItem));
       
   242     QVERIFY(anotherEffect->source()->isPixmap());
       
   243     delete pixmapItem;
       
   244 }
       
   245 
       
   246 void tst_QGraphicsEffectSource::draw()
       
   247 {
       
   248     // The source can only draw as a result of QGraphicsEffect::draw.
       
   249     QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::draw: Can only begin as a result of QGraphicsEffect::draw");
       
   250     QPixmap dummyPixmap(10, 10);
       
   251     QPainter dummyPainter(&dummyPixmap);
       
   252     effect->source()->draw(&dummyPainter);
       
   253 }
       
   254 
       
   255 void tst_QGraphicsEffectSource::update()
       
   256 {
       
   257     QCOMPARE(item->numRepaints, 0);
       
   258     QCOMPARE(effect->numRepaints, 0);
       
   259 
       
   260     effect->source()->update();
       
   261     QTest::qWait(50);
       
   262 
       
   263     QCOMPARE(item->numRepaints, 1);
       
   264     QCOMPARE(effect->numRepaints, 1);
       
   265 }
       
   266 
       
   267 void tst_QGraphicsEffectSource::boundingRect()
       
   268 {
       
   269     QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
       
   270     QCOMPARE(effect->source()->boundingRect(Qt::DeviceCoordinates), QRectF());
       
   271 
       
   272     QRectF itemBoundingRect = item->boundingRect();
       
   273     if (!item->children().isEmpty())
       
   274         itemBoundingRect |= item->childrenBoundingRect();
       
   275 
       
   276     // We can at least check that the device bounding rect was correct in QGraphicsEffect::draw.
       
   277     effect->storeDeviceDependentStuff = true;
       
   278     effect->source()->update();
       
   279     QTest::qWait(50);
       
   280     const QTransform deviceTransform = item->deviceTransform(view->viewportTransform());
       
   281     QCOMPARE(effect->sourceDeviceBoundingRect, deviceTransform.mapRect(itemBoundingRect));
       
   282 
       
   283     // Bounding rect in logical coordinates is of course fine.
       
   284     QCOMPARE(effect->source()->boundingRect(Qt::LogicalCoordinates), itemBoundingRect);
       
   285     // Make sure default value is Qt::LogicalCoordinates.
       
   286     QCOMPARE(effect->source()->boundingRect(), itemBoundingRect);
       
   287 }
       
   288 
       
   289 void tst_QGraphicsEffectSource::deviceRect()
       
   290 {
       
   291     QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::deviceRect: Not yet implemented, lacking device context");
       
   292     QCOMPARE(effect->source()->deviceRect(), QRect());
       
   293 
       
   294     // We can at least check that the rect was correct in QGraphicsEffect::draw.
       
   295     effect->storeDeviceDependentStuff = true;
       
   296     effect->source()->update();
       
   297     QTest::qWait(50);
       
   298     QCOMPARE(effect->deviceRect, view->viewport()->rect());
       
   299 }
       
   300 
       
   301 void tst_QGraphicsEffectSource::pixmap()
       
   302 {
       
   303     QTest::ignoreMessage(QtWarningMsg, "QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
       
   304     QCOMPARE(effect->source()->pixmap(Qt::DeviceCoordinates), QPixmap());
       
   305 
       
   306     // We can at least verify a valid pixmap from QGraphicsEffect::draw.
       
   307     effect->storeDeviceDependentStuff = true;
       
   308     effect->source()->update();
       
   309     QTest::qWait(50);
       
   310     QVERIFY(!effect->deviceCoordinatesPixmap.isNull());
       
   311 
       
   312     // Pixmaps in logical coordinates we can do fine.
       
   313     QPixmap pixmap1 = effect->source()->pixmap(Qt::LogicalCoordinates);
       
   314     QVERIFY(!pixmap1.isNull());
       
   315 
       
   316     // Make sure default value is Qt::LogicalCoordinates.
       
   317     QPixmap pixmap2 = effect->source()->pixmap();
       
   318     QCOMPARE(pixmap1, pixmap2);
       
   319 }
       
   320 
       
   321 QTEST_MAIN(tst_QGraphicsEffectSource)
       
   322 #include "tst_qgraphicseffectsource.moc"
       
   323