tests/benchmarks/qwidget/tst_qwidget.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 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 class tst_QWidget : public QObject
       
    46 {
       
    47     Q_OBJECT
       
    48 
       
    49 
       
    50 private slots:
       
    51     void update_data();
       
    52     void updateOpaque_data();
       
    53     void updateOpaque();
       
    54     void updateTransparent_data();
       
    55     void updateTransparent();
       
    56     void updatePartial_data();
       
    57     void updatePartial();
       
    58     void updateComplex_data();
       
    59     void updateComplex();
       
    60 
       
    61     void complexToplevelResize();
       
    62 };
       
    63 
       
    64 class UpdateWidget : public QWidget
       
    65 {
       
    66 public:
       
    67     UpdateWidget(int rows, int columns) : QWidget(0)
       
    68     {
       
    69         QGridLayout *layout = new QGridLayout;
       
    70         for (int row = 0; row < rows; ++row) {
       
    71             for (int column = 0; column < columns; ++column) {
       
    72                 UpdateWidget *widget = new UpdateWidget;
       
    73                 widget->setFixedSize(20, 20);
       
    74                 layout->addWidget(widget, row, column);
       
    75                 children.append(widget);
       
    76             }
       
    77         }
       
    78         setLayout(layout);
       
    79     }
       
    80 
       
    81     UpdateWidget(QWidget *parent = 0) : QWidget(parent) {}
       
    82 
       
    83     void paintEvent(QPaintEvent *)
       
    84     {
       
    85         static int color = Qt::black;
       
    86 
       
    87         QPainter painter(this);
       
    88         painter.fillRect(rect(), Qt::GlobalColor(color));
       
    89 
       
    90         if (++color > Qt::darkYellow)
       
    91             color = Qt::black;
       
    92     }
       
    93 
       
    94     QRegion updateRegion;
       
    95     QList<UpdateWidget*> children;
       
    96 };
       
    97 
       
    98 void tst_QWidget::update_data()
       
    99 {
       
   100     QTest::addColumn<int>("rows");
       
   101     QTest::addColumn<int>("columns");
       
   102     QTest::addColumn<int>("numUpdates");
       
   103 
       
   104     QTest::newRow("10x10x1") << 10 << 10 << 1;
       
   105     QTest::newRow("10x10x10") << 10 << 10 << 10;
       
   106     QTest::newRow("25x25x1") << 25 << 25 << 1;
       
   107     QTest::newRow("25x25x10") << 25 << 25 << 10;
       
   108     QTest::newRow("25x25x100") << 25 << 25 << 100;
       
   109 }
       
   110 
       
   111 void tst_QWidget::updateOpaque_data()
       
   112 {
       
   113     update_data();
       
   114 }
       
   115 
       
   116 void tst_QWidget::updateOpaque()
       
   117 {
       
   118     QFETCH(int, rows);
       
   119     QFETCH(int, columns);
       
   120     QFETCH(int, numUpdates);
       
   121 
       
   122     UpdateWidget widget(rows, columns);
       
   123     foreach (QWidget *w, widget.children) {
       
   124         w->setAttribute(Qt::WA_OpaquePaintEvent);
       
   125     }
       
   126 
       
   127     widget.show();
       
   128     QApplication::processEvents();
       
   129 
       
   130     int i = 0;
       
   131     const int n = widget.children.size();
       
   132     QBENCHMARK {
       
   133         for (int j = 0; j < numUpdates; ++j) {
       
   134             widget.children[i]->update();
       
   135             QApplication::processEvents();
       
   136             i = (i + 1) % n;
       
   137         }
       
   138     }
       
   139 }
       
   140 
       
   141 void tst_QWidget::updateTransparent_data()
       
   142 {
       
   143     update_data();
       
   144 }
       
   145 
       
   146 void tst_QWidget::updateTransparent()
       
   147 {
       
   148     QFETCH(int, rows);
       
   149     QFETCH(int, columns);
       
   150     QFETCH(int, numUpdates);
       
   151 
       
   152     UpdateWidget widget(rows, columns);
       
   153     widget.show();
       
   154     QApplication::processEvents();
       
   155 
       
   156     int i = 0;
       
   157     const int n = widget.children.size();
       
   158     QBENCHMARK {
       
   159         for (int j = 0; j < numUpdates; ++j) {
       
   160             widget.children[i]->update();
       
   161             QApplication::processEvents();
       
   162             i = (i + 1) % n;
       
   163         }
       
   164     }
       
   165 }
       
   166 
       
   167 void tst_QWidget::updatePartial_data()
       
   168 {
       
   169     update_data();
       
   170 }
       
   171 
       
   172 void tst_QWidget::updatePartial()
       
   173 {
       
   174     QFETCH(int, rows);
       
   175     QFETCH(int, columns);
       
   176     QFETCH(int, numUpdates);
       
   177 
       
   178     UpdateWidget widget(rows, columns);
       
   179     widget.show();
       
   180     QApplication::processEvents();
       
   181 
       
   182     int i = 0;
       
   183     const int n = widget.children.size();
       
   184     QBENCHMARK {
       
   185         for (int j = 0; j < numUpdates; ++j) {
       
   186             QWidget *w = widget.children[i];
       
   187             const int x = w->width() / 2;
       
   188             const int y = w->height() / 2;
       
   189             w->update(0, 0, x, y);
       
   190             w->update(x, 0, x, y);
       
   191             w->update(0, y, x, y);
       
   192             w->update(x, y, x, y);
       
   193             QApplication::processEvents();
       
   194             i = (i + 1) % n;
       
   195         }
       
   196     }
       
   197 }
       
   198 
       
   199 void tst_QWidget::updateComplex_data()
       
   200 {
       
   201     update_data();
       
   202 }
       
   203 
       
   204 void tst_QWidget::updateComplex()
       
   205 {
       
   206     QFETCH(int, rows);
       
   207     QFETCH(int, columns);
       
   208     QFETCH(int, numUpdates);
       
   209 
       
   210     UpdateWidget widget(rows, columns);
       
   211     widget.show();
       
   212     QApplication::processEvents();
       
   213 
       
   214     int i = 0;
       
   215     const int n = widget.children.size();
       
   216     QBENCHMARK {
       
   217         for (int j = 0; j < numUpdates; ++j) {
       
   218             QWidget *w = widget.children[i];
       
   219             const int x = w->width() / 2;
       
   220             const int y = w->height() / 2;
       
   221             w->update(QRegion(0, 0, x, y, QRegion::Ellipse));
       
   222             w->update(QRegion(x, y, x, y, QRegion::Ellipse));
       
   223             QApplication::processEvents();
       
   224             i = (i + 1) % n;
       
   225         }
       
   226     }
       
   227 }
       
   228 
       
   229 class ResizeWidget : public QWidget
       
   230 {
       
   231 public:
       
   232     ResizeWidget();
       
   233 };
       
   234 
       
   235 ResizeWidget::ResizeWidget() : QWidget(0)
       
   236 {
       
   237     QBoxLayout *topLayout = new QVBoxLayout;
       
   238 
       
   239     QMenuBar *menubar = new QMenuBar;
       
   240     QMenu* popup = menubar->addMenu("&File");
       
   241     popup->addAction("&Quit", qApp, SLOT(quit()));
       
   242     topLayout->setMenuBar(menubar);
       
   243 
       
   244     QBoxLayout *buttons = new QHBoxLayout;
       
   245     buttons->setMargin(5);
       
   246     buttons->addStretch(10);
       
   247     for (int i = 1; i <= 4; i++ ) {
       
   248         QPushButton* button = new QPushButton;
       
   249         button->setText(QString("Button %1").arg(i));
       
   250         buttons->addWidget(button);
       
   251     }
       
   252     topLayout->addLayout(buttons);
       
   253 
       
   254     buttons = new QHBoxLayout;
       
   255     buttons->addStretch(10);
       
   256     for (int i = 11; i <= 16; i++) {
       
   257         QPushButton* button = new QPushButton;
       
   258         button->setText(QString("Button %1").arg(i));
       
   259         buttons->addWidget(button);
       
   260     }
       
   261     topLayout->addLayout(buttons);
       
   262 
       
   263     QBoxLayout *buttons2 = new QHBoxLayout;
       
   264     buttons2->addStretch(10);
       
   265     topLayout->addLayout(buttons2);
       
   266 
       
   267     QPushButton *button = new QPushButton;
       
   268     button->setText("Button five");
       
   269     buttons2->addWidget(button);
       
   270 
       
   271     button = new QPushButton;
       
   272     button->setText("Button 6");
       
   273     buttons2->addWidget(button);
       
   274 
       
   275     QTextEdit *bigWidget = new QTextEdit;
       
   276     bigWidget->setText("This widget will get all the remaining space");
       
   277     bigWidget->setFrameStyle(QFrame::Panel | QFrame::Plain);
       
   278     topLayout->addWidget(bigWidget);
       
   279 
       
   280     const int numRows = 6;
       
   281     const int labelCol = 0;
       
   282     const int linedCol = 1;
       
   283     const int multiCol = 2;
       
   284 
       
   285     QGridLayout *grid = new QGridLayout;
       
   286     for (int row = 0; row < numRows; row++) {
       
   287         QLineEdit *lineEdit = new QLineEdit;
       
   288         grid->addWidget(lineEdit, row, linedCol);
       
   289         QLabel *label = new QLabel(QString("Line &%1").arg(row + 1));
       
   290         grid->addWidget(label, row, labelCol);
       
   291     }
       
   292     topLayout->addLayout(grid);
       
   293 
       
   294     QTextEdit *multiLineEdit = new QTextEdit;
       
   295     grid->addWidget(multiLineEdit, 0, labelCol + 1, multiCol, multiCol);
       
   296 
       
   297     grid->setColumnStretch(linedCol, 10);
       
   298     grid->setColumnStretch(multiCol, 20);
       
   299 
       
   300     QLabel* statusBar = new QLabel;
       
   301     statusBar->setText("Let's pretend this is a status bar");
       
   302     statusBar->setFrameStyle(QFrame::Panel | QFrame::Sunken);
       
   303     statusBar->setFixedHeight(statusBar->sizeHint().height());
       
   304     statusBar->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
       
   305     topLayout->addWidget(statusBar);
       
   306 
       
   307     topLayout->activate();
       
   308     setLayout(topLayout);
       
   309 }
       
   310 
       
   311 void tst_QWidget::complexToplevelResize()
       
   312 {
       
   313     ResizeWidget w;
       
   314     w.show();
       
   315 
       
   316     QApplication::processEvents();
       
   317 
       
   318     const int minSize = 100;
       
   319     const int maxSize = 800;
       
   320     int size = minSize;
       
   321 
       
   322     QBENCHMARK {
       
   323         w.resize(size, size);
       
   324         size = qMax(minSize, (size + 10) % maxSize);
       
   325         QApplication::processEvents();
       
   326         QApplication::processEvents();
       
   327     }
       
   328 }
       
   329 
       
   330 QTEST_MAIN(tst_QWidget)
       
   331 
       
   332 #include "tst_qwidget.moc"