tests/benchmarks/qgraphicsitem/tst_qgraphicsitem.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 module 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 <qtest.h>
       
    43 #include <QGraphicsItem>
       
    44 #include <QGraphicsScene>
       
    45 #include <QGraphicsView>
       
    46 
       
    47 //TESTED_FILES=
       
    48 
       
    49 class tst_QGraphicsItem : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_QGraphicsItem();
       
    55     virtual ~tst_QGraphicsItem();
       
    56 
       
    57 public slots:
       
    58     void init();
       
    59     void cleanup();
       
    60 
       
    61 private slots:
       
    62     void setParentItem();
       
    63     void setParentItem_deep();
       
    64     void setParentItem_deep_reversed();
       
    65     void deleteItemWithManyChildren();
       
    66     void setPos_data();
       
    67     void setPos();
       
    68     void setTransform_data();
       
    69     void setTransform();
       
    70     void rotate();
       
    71     void scale();
       
    72     void shear();
       
    73     void translate();
       
    74     void setRotation();
       
    75 };
       
    76 
       
    77 tst_QGraphicsItem::tst_QGraphicsItem()
       
    78 {
       
    79 }
       
    80 
       
    81 tst_QGraphicsItem::~tst_QGraphicsItem()
       
    82 {
       
    83 }
       
    84 
       
    85 void tst_QGraphicsItem::init()
       
    86 {
       
    87 }
       
    88 
       
    89 void tst_QGraphicsItem::cleanup()
       
    90 {
       
    91 }
       
    92 
       
    93 void tst_QGraphicsItem::setParentItem()
       
    94 {
       
    95     QBENCHMARK {
       
    96         QGraphicsRectItem rect;
       
    97         QGraphicsRectItem *childRect = new QGraphicsRectItem;
       
    98         childRect->setParentItem(&rect);
       
    99     }
       
   100 }
       
   101 
       
   102 void tst_QGraphicsItem::setParentItem_deep()
       
   103 {
       
   104     QBENCHMARK {
       
   105         QGraphicsRectItem rect;
       
   106         QGraphicsRectItem *lastRect = &rect;
       
   107         for (int i = 0; i < 10; ++i) {
       
   108             QGraphicsRectItem *childRect = new QGraphicsRectItem;
       
   109             childRect->setParentItem(lastRect);
       
   110             lastRect = childRect;
       
   111         }
       
   112         QGraphicsItem *first = rect.children().first();
       
   113         first->setParentItem(0);
       
   114     }
       
   115 }
       
   116 
       
   117 void tst_QGraphicsItem::setParentItem_deep_reversed()
       
   118 {
       
   119     QBENCHMARK {
       
   120         QGraphicsRectItem *lastRect = new QGraphicsRectItem;
       
   121         for (int i = 0; i < 100; ++i) {
       
   122             QGraphicsRectItem *parentRect = new QGraphicsRectItem;
       
   123             lastRect->setParentItem(parentRect);
       
   124             lastRect = parentRect;
       
   125         }
       
   126         delete lastRect;
       
   127     }
       
   128 }
       
   129 
       
   130 void tst_QGraphicsItem::deleteItemWithManyChildren()
       
   131 {
       
   132     QBENCHMARK {
       
   133         QGraphicsRectItem *rect = new QGraphicsRectItem;
       
   134         for (int i = 0; i < 1000; ++i)
       
   135             new QGraphicsRectItem(rect);
       
   136         delete rect;
       
   137     }
       
   138 }
       
   139 
       
   140 void tst_QGraphicsItem::setPos_data()
       
   141 {
       
   142     QTest::addColumn<QPointF>("pos");
       
   143 
       
   144     QTest::newRow("0, 0") << QPointF(0, 0);
       
   145     QTest::newRow("10, 10") << QPointF(10, 10);
       
   146     QTest::newRow("-10, -10") << QPointF(-10, -10);
       
   147 }
       
   148 
       
   149 void tst_QGraphicsItem::setPos()
       
   150 {
       
   151     QFETCH(QPointF, pos);
       
   152 
       
   153     QGraphicsScene scene;
       
   154     QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
       
   155 
       
   156     QBENCHMARK {
       
   157         rect->setPos(10, 10);
       
   158         rect->transform(); // prevent lazy optimizing
       
   159     }
       
   160 }
       
   161 
       
   162 void tst_QGraphicsItem::setTransform_data()
       
   163 {
       
   164     QTest::addColumn<QTransform>("transform");
       
   165 
       
   166     QTest::newRow("id") << QTransform();
       
   167     QTest::newRow("rotate 45z") << QTransform().rotate(45);
       
   168     QTest::newRow("scale 2x2") << QTransform().scale(2, 2);
       
   169     QTest::newRow("translate 100, 100") << QTransform().translate(100, 100);
       
   170     QTest::newRow("rotate 45x 45y 45z") << QTransform().rotate(45, Qt::XAxis)
       
   171         .rotate(45, Qt::YAxis).rotate(45, Qt::ZAxis);
       
   172 }
       
   173 
       
   174 void tst_QGraphicsItem::setTransform()
       
   175 {
       
   176     QFETCH(QTransform, transform);
       
   177 
       
   178     QGraphicsScene scene;
       
   179     QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   180 
       
   181     QBENCHMARK {
       
   182         item->setTransform(transform);
       
   183         item->transform(); // prevent lazy optimizing
       
   184     }
       
   185 }
       
   186 
       
   187 void tst_QGraphicsItem::rotate()
       
   188 {
       
   189     QGraphicsScene scene;
       
   190     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   191 
       
   192     QBENCHMARK {
       
   193         item->rotate(45);
       
   194         item->transform(); // prevent lazy optimizing
       
   195     }
       
   196 }
       
   197 
       
   198 void tst_QGraphicsItem::scale()
       
   199 {
       
   200     QGraphicsScene scene;
       
   201     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   202 
       
   203     QBENCHMARK {
       
   204         item->scale(2, 2);
       
   205         item->transform(); // prevent lazy optimizing
       
   206     }
       
   207 }
       
   208 
       
   209 void tst_QGraphicsItem::shear()
       
   210 {
       
   211     QGraphicsScene scene;
       
   212     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   213 
       
   214     QBENCHMARK {
       
   215         item->shear(1.5, 1.5);
       
   216         item->transform(); // prevent lazy optimizing
       
   217     }
       
   218 }
       
   219 
       
   220 void tst_QGraphicsItem::translate()
       
   221 {
       
   222     QGraphicsScene scene;
       
   223     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   224 
       
   225     QBENCHMARK {
       
   226         item->translate(100, 100);
       
   227         item->transform(); // prevent lazy optimizing
       
   228     }
       
   229 }
       
   230 
       
   231 void tst_QGraphicsItem::setRotation()
       
   232 {
       
   233     QGraphicsScene scene;
       
   234     QGraphicsItem *item = scene.addRect(QRectF(0, 0, 100, 100));
       
   235 
       
   236     QBENCHMARK {
       
   237         item->setRotation(45);
       
   238         item->transform(); // prevent lazy optimizing
       
   239     }
       
   240 }
       
   241 
       
   242 QTEST_MAIN(tst_QGraphicsItem)
       
   243 #include "tst_qgraphicsitem.moc"