tests/benchmarks/gui/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp
changeset 18 2f34d5167611
equal deleted inserted replaced
3:41300fa6a67c 18:2f34d5167611
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 <qtest.h>
       
    43 #include <QGraphicsItem>
       
    44 #include <QGraphicsScene>
       
    45 #include <QGraphicsView>
       
    46 
       
    47 //TESTED_FILES=
       
    48 
       
    49 class tst_QGraphicsScene : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_QGraphicsScene();
       
    55     virtual ~tst_QGraphicsScene();
       
    56 
       
    57 public slots:
       
    58     void init();
       
    59     void cleanup();
       
    60 
       
    61 private slots:
       
    62     void construct();
       
    63     void addItem_data();
       
    64     void addItem();
       
    65     void itemAt_data();
       
    66     void itemAt();
       
    67     void initialShow();
       
    68 };
       
    69 
       
    70 tst_QGraphicsScene::tst_QGraphicsScene()
       
    71 {
       
    72 }
       
    73 
       
    74 tst_QGraphicsScene::~tst_QGraphicsScene()
       
    75 {
       
    76 }
       
    77 
       
    78 static inline void processEvents()
       
    79 {
       
    80     QApplication::flush();
       
    81     QApplication::processEvents();
       
    82     QApplication::processEvents();
       
    83 }
       
    84 
       
    85 void tst_QGraphicsScene::init()
       
    86 {
       
    87     processEvents();
       
    88 }
       
    89 
       
    90 void tst_QGraphicsScene::cleanup()
       
    91 {
       
    92 }
       
    93 
       
    94 void tst_QGraphicsScene::construct()
       
    95 {
       
    96     QBENCHMARK {
       
    97         QGraphicsScene scene;
       
    98     }
       
    99 }
       
   100 
       
   101 void tst_QGraphicsScene::addItem_data()
       
   102 {
       
   103     QTest::addColumn<int>("indexMethod");
       
   104     QTest::addColumn<QRectF>("sceneRect");
       
   105     QTest::addColumn<int>("numItems_X");
       
   106     QTest::addColumn<int>("numItems_Y");
       
   107     QTest::addColumn<int>("itemType");
       
   108     QTest::addColumn<QRectF>("itemRect");
       
   109 
       
   110     QTest::newRow("null") << 0 << QRectF() << 0 << 0 << 0 << QRectF();
       
   111     QTest::newRow("0 QRectF() 10 x  10 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF() << 10 << 10 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   112     QTest::newRow("0 QRectF() 25 x  25 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF() << 25 << 25 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   113     QTest::newRow("0 QRectF() 100 x 100 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF() << 100 << 100 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   114     QTest::newRow("0 QRectF() 250 x 250 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF() << 250 << 250 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   115     QTest::newRow("0 QRectF()  10 x  10 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF() << 10 << 10 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   116     QTest::newRow("0 QRectF()  25 x  25 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF() << 25 << 25 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   117     QTest::newRow("0 QRectF() 100 x 100 QGraphicsEllipseItem (0,0,10,0)") << 0 << QRectF() << 100 << 100 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   118     QTest::newRow("0 QRectF() 250 x 250 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF() << 250 << 250 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   119     QTest::newRow("0 QRectF()  10 x  10 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF(0, 0, 100, 100) << 10 << 10 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   120     QTest::newRow("0 QRectF()  25 x  25 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF(0, 0, 250, 250) << 25 << 25 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   121     QTest::newRow("0 QRectF() 100 x 100 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF(0, 0, 1000, 1000) << 100 << 100 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   122     QTest::newRow("0 QRectF() 250 x 250 QGraphicsRectItem (0,0,10,10)") << 0 << QRectF(0, 0, 2500, 2500) << 250 << 250 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   123     QTest::newRow("0 QRectF()  10 x  10 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF(0, 0, 100, 100) << 10 << 10 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   124     QTest::newRow("0 QRectF()  25 x  25 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF(0, 0, 250, 250) << 25 << 25 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   125     QTest::newRow("0 QRectF() 100 x 100 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF(0, 0, 1000, 1000) << 100 << 100 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   126     QTest::newRow("0 QRectF() 250 x 250 QGraphicsEllipseItem (0,0,10,10)") << 0 << QRectF(0, 0, 2500, 2500) << 250 << 250 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   127     QTest::newRow("1 QRectF() 10 x  10 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF() << 10 << 10 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   128     QTest::newRow("1 QRectF()  25 x  25 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF() << 25 << 25 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   129     QTest::newRow("1 QRectF() 100 x 100 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF() << 100 << 100 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   130     QTest::newRow("1 QRectF() 250 x 250 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF() << 250 << 250 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   131     QTest::newRow("1 QRectF()  10 x  10 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF() << 10 << 10 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   132     QTest::newRow("1 QRectF()  25 x  25 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF() << 25 << 25 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   133     QTest::newRow("1 QRectF() 100 x 100 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF() << 100 << 100 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   134     QTest::newRow("1 QRectF() 250 x 250 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF() << 250 << 250 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   135     QTest::newRow("1 QRectF()  10 x  10 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF(0, 0, 100, 100) << 10 << 10 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   136     QTest::newRow("1 QRectF()  25 x  25 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF(0, 0, 250, 250) << 25 << 25 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   137     QTest::newRow("1 QRectF() 100 x 100 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF(0, 0, 1000, 1000) << 100 << 100 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   138     QTest::newRow("1 QRectF() 250 x 250 QGraphicsRectItem (0,0,10,10)") << 1 << QRectF(0, 0, 2500, 2500) << 250 << 250 << int(QGraphicsRectItem::Type) << QRectF(0, 0, 10, 10);
       
   139     QTest::newRow("1 QRectF()  10 x  10 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF(0, 0, 100, 100) << 10 << 10 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   140     QTest::newRow("1 QRectF()  25 x  25 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF(0, 0, 250, 250) << 25 << 25 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   141     QTest::newRow("1 QRectF() 100 x 100 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF(0, 0, 1000, 1000) << 100 << 100 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   142     QTest::newRow("1 QRectF() 250 x 250 QGraphicsEllipseItem (0,0,10,10)") << 1 << QRectF(0, 0, 2500, 2500) << 250 << 250 << int(QGraphicsEllipseItem::Type) << QRectF(0, 0, 10, 10);
       
   143 }
       
   144 
       
   145 void tst_QGraphicsScene::addItem()
       
   146 {
       
   147     QFETCH(int, indexMethod);
       
   148     QFETCH(QRectF, sceneRect);
       
   149     QFETCH(int, numItems_X);
       
   150     QFETCH(int, numItems_Y);
       
   151     QFETCH(int, itemType);
       
   152     QFETCH(QRectF, itemRect);
       
   153 
       
   154     QGraphicsScene scene;
       
   155     scene.setItemIndexMethod(indexMethod ? QGraphicsScene::BspTreeIndex : QGraphicsScene::NoIndex);
       
   156     if (!sceneRect.isNull())
       
   157         scene.setSceneRect(sceneRect);
       
   158 
       
   159     processEvents();
       
   160 
       
   161     QBENCHMARK {
       
   162         QGraphicsItem *item = 0;
       
   163         for (int y = 0; y < numItems_Y; ++y) {
       
   164             for (int x = 0; x < numItems_X; ++x) {
       
   165                 switch (itemType) {
       
   166                 case QGraphicsRectItem::Type:
       
   167                     item = new QGraphicsRectItem(itemRect);
       
   168                     break;
       
   169                 case QGraphicsEllipseItem::Type:
       
   170                 default:
       
   171                     item = new QGraphicsEllipseItem(itemRect);
       
   172                     break;
       
   173                 }
       
   174                 item->setPos(x * itemRect.width(), y * itemRect.height());
       
   175                 scene.addItem(item);
       
   176             }
       
   177         }
       
   178         scene.itemAt(0, 0);
       
   179     }
       
   180     //let QGraphicsScene::_q_polishItems be called so ~QGraphicsItem doesn't spend all his time cleaning the unpolished list
       
   181     qApp->processEvents();
       
   182 }
       
   183 
       
   184 void tst_QGraphicsScene::itemAt_data()
       
   185 {
       
   186     QTest::addColumn<int>("bspTreeDepth");
       
   187     QTest::addColumn<QRectF>("sceneRect");
       
   188     QTest::addColumn<int>("numItems_X");
       
   189     QTest::addColumn<int>("numItems_Y");
       
   190     QTest::addColumn<QRectF>("itemRect");
       
   191 
       
   192     QTest::newRow("null") << 0 << QRectF() << 0 << 0 << QRectF();
       
   193     QTest::newRow("NoIndex 10x10") << -1 << QRectF() << 10 << 10 << QRectF(-10, -10, 20, 20);
       
   194     QTest::newRow("NoIndex 25x25") << -1 << QRectF() << 25 << 25 << QRectF(-10, -10, 20, 20);
       
   195     QTest::newRow("NoIndex 100x100") << -1 << QRectF() << 100 << 100 << QRectF(-10, -10, 20, 20);
       
   196     QTest::newRow("NoIndex 250x250") << -1 << QRectF() << 250 << 250 << QRectF(-10, -10, 20, 20);
       
   197     QTest::newRow("BspTreeIndex depth=auto 10x10") << 0 << QRectF() << 10 << 10 << QRectF(-10, -10, 20, 20);
       
   198     QTest::newRow("BspTreeIndex depth=auto 25x25") << 0 << QRectF() << 25 << 25 << QRectF(-10, -10, 20, 20);
       
   199     QTest::newRow("BspTreeIndex depth=auto 100x100") << 0 << QRectF() << 100 << 100 << QRectF(-10, -10, 20, 20);
       
   200     QTest::newRow("BspTreeIndex depth=auto 250x250") << 0 << QRectF() << 250 << 250 << QRectF(-10, -10, 20, 20);
       
   201     QTest::newRow("BspTreeIndex depth=16 10x10") << 16 << QRectF() << 10 << 10 << QRectF(-10, -10, 20, 20);
       
   202     QTest::newRow("BspTreeIndex depth=16 25x25") << 16 << QRectF() << 25 << 25 << QRectF(-10, -10, 20, 20);
       
   203     QTest::newRow("BspTreeIndex depth=16 100x100") << 16 << QRectF() << 100 << 100 << QRectF(-10, -10, 20, 20);
       
   204     QTest::newRow("BspTreeIndex depth=16 250x250") << 16 << QRectF() << 250 << 250 << QRectF(-10, -10, 20, 20);
       
   205 }
       
   206 
       
   207 void tst_QGraphicsScene::itemAt()
       
   208 {
       
   209     QFETCH(int, bspTreeDepth);
       
   210     QFETCH(QRectF, sceneRect);
       
   211     QFETCH(int, numItems_X);
       
   212     QFETCH(int, numItems_Y);
       
   213     QFETCH(QRectF, itemRect);
       
   214 
       
   215     QGraphicsScene scene;
       
   216     scene.setItemIndexMethod(bspTreeDepth >= 0 ? QGraphicsScene::BspTreeIndex : QGraphicsScene::NoIndex);
       
   217     if (bspTreeDepth > 0)
       
   218         scene.setBspTreeDepth(bspTreeDepth);
       
   219     if (!sceneRect.isNull())
       
   220         scene.setSceneRect(sceneRect);
       
   221 
       
   222     for (int y = 0; y < numItems_Y; ++y) {
       
   223         for (int x = 0; x < numItems_X; ++x) {
       
   224             QGraphicsRectItem *item = new QGraphicsRectItem(itemRect);
       
   225             item->setPos((x - numItems_X/2) * itemRect.width(), (y - numItems_Y/2) * itemRect.height());
       
   226             scene.addItem(item);
       
   227         }
       
   228     }
       
   229 
       
   230     scene.itemAt(0, 0); // triggers indexing
       
   231     processEvents();
       
   232 
       
   233     QGraphicsItem *item = 0;
       
   234     QBENCHMARK {
       
   235         item = scene.itemAt(0, 0);
       
   236     }
       
   237 
       
   238     //let QGraphicsScene::_q_polishItems be called so ~QGraphicsItem doesn't spend all his time cleaning the unpolished list
       
   239     qApp->processEvents();
       
   240 }
       
   241 
       
   242 void tst_QGraphicsScene::initialShow()
       
   243 {
       
   244     QGraphicsScene scene;
       
   245 
       
   246     QBENCHMARK {
       
   247         for (int y = 0; y < 30000; ++y) {
       
   248             QGraphicsRectItem *item = new QGraphicsRectItem(0, 0, 50, 50);
       
   249             item->setPos((y/2) * item->rect().width(), (y/2) * item->rect().height());
       
   250             scene.addItem(item);
       
   251         }
       
   252         scene.itemAt(0, 0); // triggers indexing
       
   253         //This call polish the items so we bench their processing too.
       
   254         qApp->processEvents();
       
   255     }
       
   256 }
       
   257 
       
   258 QTEST_MAIN(tst_QGraphicsScene)
       
   259 #include "tst_qgraphicsscene.moc"