tests/benchmarks/gui/kernel/qwidget/tst_qwidget.cpp
changeset 7 f7bc934e204c
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     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 <QtGui>
       
    44 
       
    45 static void processEvents()
       
    46 {
       
    47     QApplication::flush();
       
    48     QApplication::processEvents();
       
    49     QApplication::processEvents();
       
    50 }
       
    51 
       
    52 class UpdateWidget : public QWidget
       
    53 {
       
    54 public:
       
    55     UpdateWidget(int rows, int columns)
       
    56         : QWidget(0), rowCount(0), columnCount(0), opaqueChildren(false)
       
    57     {
       
    58         fill(rows, columns);
       
    59     }
       
    60 
       
    61     UpdateWidget(QWidget *parent = 0)
       
    62         : QWidget(parent), rowCount(0), columnCount(0), opaqueChildren(false) {}
       
    63 
       
    64     void fill(int rows, int columns)
       
    65     {
       
    66         if (rows == rowCount && columns == columnCount)
       
    67             return;
       
    68         delete layout();
       
    69         QGridLayout *layout = new QGridLayout;
       
    70         rowCount = rows;
       
    71         columnCount = columns;
       
    72         for (int row = 0; row < rowCount; ++row) {
       
    73             for (int column = 0; column < columnCount; ++column) {
       
    74                 UpdateWidget *widget = new UpdateWidget;
       
    75                 widget->setFixedSize(20, 20);
       
    76                 layout->addWidget(widget, row, column);
       
    77                 children.append(widget);
       
    78             }
       
    79         }
       
    80         setLayout(layout);
       
    81         adjustSize();
       
    82         QTest::qWait(250);
       
    83         processEvents();
       
    84     }
       
    85 
       
    86     void setOpaqueChildren(bool enable)
       
    87     {
       
    88         if (opaqueChildren != enable) {
       
    89             foreach (QWidget *w, children)
       
    90                 w->setAttribute(Qt::WA_OpaquePaintEvent, enable);
       
    91             opaqueChildren = enable;
       
    92             processEvents();
       
    93         }
       
    94     }
       
    95 
       
    96     void paintEvent(QPaintEvent *)
       
    97     {
       
    98         static int color = Qt::black;
       
    99 
       
   100         QPainter painter(this);
       
   101         painter.fillRect(rect(), Qt::GlobalColor(color));
       
   102 
       
   103         if (++color > Qt::darkYellow)
       
   104             color = Qt::black;
       
   105     }
       
   106 
       
   107     QRegion updateRegion;
       
   108     QList<UpdateWidget*> children;
       
   109     int rowCount;
       
   110     int columnCount;
       
   111     bool opaqueChildren;
       
   112 };
       
   113 
       
   114 class tst_QWidget : public QObject
       
   115 {
       
   116     Q_OBJECT
       
   117 
       
   118 public slots:
       
   119     void initTestCase();
       
   120     void init();
       
   121 
       
   122 private slots:
       
   123     void update_data();
       
   124     void update();
       
   125     void updatePartial_data();
       
   126     void updatePartial();
       
   127     void updateComplex_data();
       
   128     void updateComplex();
       
   129 
       
   130 private:
       
   131     UpdateWidget widget;
       
   132 };
       
   133 
       
   134 void tst_QWidget::initTestCase()
       
   135 {
       
   136     widget.show();
       
   137     QTest::qWaitForWindowShown(&widget);
       
   138     QTest::qWait(300);
       
   139     processEvents();
       
   140 }
       
   141 
       
   142 void tst_QWidget::init()
       
   143 {
       
   144     QVERIFY(widget.isVisible());
       
   145     for (int i = 0; i < 3; ++i)
       
   146         processEvents();
       
   147 }
       
   148 
       
   149 void tst_QWidget::update_data()
       
   150 {
       
   151     QTest::addColumn<int>("rows");
       
   152     QTest::addColumn<int>("columns");
       
   153     QTest::addColumn<int>("numUpdates");
       
   154     QTest::addColumn<bool>("opaque");
       
   155 
       
   156     QTest::newRow("10x10x1 transparent")   << 10 << 10 << 1   << false;
       
   157     QTest::newRow("10x10x10 transparent")  << 10 << 10 << 10  << false;
       
   158     QTest::newRow("10x10x100 transparent") << 10 << 10 << 100 << false;
       
   159     QTest::newRow("10x10x1 opaque")        << 10 << 10 << 1   << true;
       
   160     QTest::newRow("10x10x10 opaque")       << 10 << 10 << 10  << true;
       
   161     QTest::newRow("10x10x100 opaque")      << 10 << 10 << 100 << true;
       
   162     QTest::newRow("25x25x1 transparent ")  << 25 << 25 << 1   << false;
       
   163     QTest::newRow("25x25x10 transparent")  << 25 << 25 << 10  << false;
       
   164     QTest::newRow("25x25x100 transparent") << 25 << 25 << 100 << false;
       
   165     QTest::newRow("25x25x1 opaque")        << 25 << 25 << 1   << true;
       
   166     QTest::newRow("25x25x10 opaque")       << 25 << 25 << 10  << true;
       
   167     QTest::newRow("25x25x100 opaque")      << 25 << 25 << 100 << true;
       
   168 }
       
   169 
       
   170 void tst_QWidget::update()
       
   171 {
       
   172     QFETCH(int, rows);
       
   173     QFETCH(int, columns);
       
   174     QFETCH(int, numUpdates);
       
   175     QFETCH(bool, opaque);
       
   176 
       
   177     widget.fill(rows, columns);
       
   178     widget.setOpaqueChildren(opaque);
       
   179 
       
   180     QBENCHMARK {
       
   181         for (int i = 0; i < widget.children.size(); ++i) {
       
   182             for (int j = 0; j < numUpdates; ++j)
       
   183                 widget.children.at(i)->update();
       
   184             QApplication::processEvents();
       
   185         }
       
   186     }
       
   187 
       
   188     QApplication::flush();
       
   189 }
       
   190 
       
   191 void tst_QWidget::updatePartial_data()
       
   192 {
       
   193     update_data();
       
   194 }
       
   195 
       
   196 void tst_QWidget::updatePartial()
       
   197 {
       
   198     QFETCH(int, rows);
       
   199     QFETCH(int, columns);
       
   200     QFETCH(int, numUpdates);
       
   201     QFETCH(bool, opaque);
       
   202 
       
   203     widget.fill(rows, columns);
       
   204     widget.setOpaqueChildren(opaque);
       
   205 
       
   206     QBENCHMARK {
       
   207         for (int i = 0; i < widget.children.size(); ++i) {
       
   208             QWidget *w = widget.children[i];
       
   209             const int x = w->width() / 2;
       
   210             const int y = w->height() / 2;
       
   211             for (int j = 0; j < numUpdates; ++j) {
       
   212                 w->update(0, 0, x, y);
       
   213                 w->update(x, 0, x, y);
       
   214                 w->update(0, y, x, y);
       
   215                 w->update(x, y, x, y);
       
   216             }
       
   217             QApplication::processEvents();
       
   218         }
       
   219     }
       
   220 }
       
   221 
       
   222 void tst_QWidget::updateComplex_data()
       
   223 {
       
   224     update_data();
       
   225 }
       
   226 
       
   227 void tst_QWidget::updateComplex()
       
   228 {
       
   229     QFETCH(int, rows);
       
   230     QFETCH(int, columns);
       
   231     QFETCH(int, numUpdates);
       
   232     QFETCH(bool, opaque);
       
   233 
       
   234     widget.fill(rows, columns);
       
   235     widget.setOpaqueChildren(opaque);
       
   236 
       
   237     QBENCHMARK {
       
   238         for (int i = 0; i < widget.children.size(); ++i) {
       
   239             QWidget *w = widget.children[i];
       
   240             const int x = w->width() / 2;
       
   241             const int y = w->height() / 2;
       
   242             QRegion r1(0, 0, x, y, QRegion::Ellipse);
       
   243             QRegion r2(x, y, x, y, QRegion::Ellipse);
       
   244             for (int j = 0; j < numUpdates; ++j) {
       
   245                 w->update(r1);
       
   246                 w->update(r2);
       
   247             }
       
   248             QApplication::processEvents();
       
   249         }
       
   250     }
       
   251 }
       
   252 
       
   253 QTEST_MAIN(tst_QWidget)
       
   254 
       
   255 #include "tst_qwidget.moc"