tests/benchmarks/gui/styles/qstylesheetstyle/main.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     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 // This file contains benchmarks for QRect/QRectF functions.
       
    42 
       
    43 #include <QtGui>
       
    44 #include <qtest.h>
       
    45 
       
    46 class tst_qstylesheetstyle : public QObject
       
    47 {
       
    48     Q_OBJECT
       
    49 private slots:
       
    50     void empty();
       
    51     void empty_events();
       
    52 
       
    53     void simple();
       
    54     void simple_events();
       
    55 
       
    56     void grid_data();
       
    57     void grid();
       
    58 
       
    59 private:
       
    60     QWidget *buildSimpleWidgets();
       
    61 
       
    62 };
       
    63 
       
    64 
       
    65 QWidget *tst_qstylesheetstyle::buildSimpleWidgets()
       
    66 {
       
    67     QWidget *w = new QWidget();
       
    68     QGridLayout *layout = new QGridLayout(w);
       
    69     w->setLayout(layout);
       
    70     layout->addWidget(new QPushButton("pushButton")     ,0,0);
       
    71     layout->addWidget(new QComboBox()                   ,0,1);
       
    72     layout->addWidget(new QCheckBox("checkBox")         ,0,2);
       
    73     layout->addWidget(new QRadioButton("radioButton")   ,0,3);
       
    74     layout->addWidget(new QLineEdit()                   ,1,0);
       
    75     layout->addWidget(new QLabel("label")               ,1,1);
       
    76     layout->addWidget(new QSpinBox()                    ,1,2);
       
    77     layout->addWidget(new QProgressBar()                ,1,3);
       
    78     return w;
       
    79 }
       
    80 
       
    81 void tst_qstylesheetstyle::empty()
       
    82 {
       
    83     QWidget *w = buildSimpleWidgets();
       
    84     w->setStyleSheet("/* */");
       
    85     QApplication::processEvents();
       
    86     int i = 0;
       
    87     QBENCHMARK {
       
    88         w->setStyleSheet("/*" + QString::number(i) + "*/");
       
    89         i++; // we want a different string in case we have severals iterations
       
    90     }
       
    91     delete w;
       
    92 }
       
    93 
       
    94 void tst_qstylesheetstyle::empty_events()
       
    95 {
       
    96     QWidget *w = buildSimpleWidgets();
       
    97     w->setStyleSheet("/* */");
       
    98     QApplication::processEvents();
       
    99     int i = 0;
       
   100     QBENCHMARK {
       
   101         w->setStyleSheet("/*" + QString::number(i) + "*/");
       
   102         i++; // we want a different string in case we have severals iterations
       
   103         qApp->processEvents();
       
   104     }
       
   105     delete w;
       
   106 }
       
   107 
       
   108 static const char *simple_css =
       
   109    " QLineEdit { background: red; }   QPushButton { border: 1px solid yellow; color: pink; }  \n"
       
   110    " QCheckBox { margin: 3px 5px; background-color:red; } QAbstractButton { background-color: #456; } \n"
       
   111    " QFrame { padding: 3px; } QLabel { color: black } QSpinBox:hover { background-color:blue; }  ";
       
   112 
       
   113 void tst_qstylesheetstyle::simple()
       
   114 {
       
   115     QWidget *w = buildSimpleWidgets();
       
   116     w->setStyleSheet("/* */");
       
   117     QApplication::processEvents();
       
   118     int i = 0;
       
   119     QBENCHMARK {
       
   120         w->setStyleSheet(QString(simple_css) + "/*" + QString::number(i) + "*/");
       
   121         i++; // we want a different string in case we have severals iterations
       
   122     }
       
   123     delete w;
       
   124 }
       
   125 
       
   126 void tst_qstylesheetstyle::simple_events()
       
   127 {
       
   128     QWidget *w = buildSimpleWidgets();
       
   129     w->setStyleSheet("/* */");
       
   130     QApplication::processEvents();
       
   131     int i = 0;
       
   132     QBENCHMARK {
       
   133         w->setStyleSheet(QString(simple_css) + "/*" + QString::number(i) + "*/");
       
   134         i++; // we want a different string in case we have severals iterations
       
   135         qApp->processEvents();
       
   136     }
       
   137     delete w;
       
   138 }
       
   139 
       
   140 void tst_qstylesheetstyle::grid_data()
       
   141 {
       
   142         QTest::addColumn<bool>("events");
       
   143         QTest::addColumn<bool>("show");
       
   144         QTest::addColumn<int>("N");
       
   145         for (int n = 5; n <= 25; n += 5) {
       
   146            const QByteArray nString = QByteArray::number(n*n);
       
   147             QTest::newRow(("simple--" + nString).constData()) << false << false << n;
       
   148             QTest::newRow(("events--" + nString).constData()) << true << false << n;
       
   149             QTest::newRow(("show--" + nString).constData()) << true << true << n;
       
   150         }
       
   151 }
       
   152 
       
   153 
       
   154 void tst_qstylesheetstyle::grid()
       
   155 {
       
   156     QFETCH(bool, events);
       
   157     QFETCH(bool, show);
       
   158     QFETCH(int, N);
       
   159 
       
   160 #ifdef Q_OS_SYMBIAN
       
   161     // Symbian has limited stack (max 80k), which will run out when N >= 20 due to
       
   162     // QWidget::show() using recursion among grid labels somewhere down the line.
       
   163     if (show && N >= 20)
       
   164         QSKIP("Grid too big for device to show", SkipSingle);
       
   165 #endif
       
   166 
       
   167     QWidget *w = new QWidget();
       
   168     QGridLayout *layout = new QGridLayout(w);
       
   169     w->setLayout(layout);
       
   170     QString stylesheet;
       
   171     for(int x=0; x<N ;x++)
       
   172         for(int y=0; y<N ;y++) {
       
   173         QLabel *label = new QLabel(QString::number(y * N + x));
       
   174         layout->addWidget(label ,x,y);
       
   175         label->setObjectName(QString("label%1").arg(y * N + x));
       
   176         stylesheet += QString("#label%1 { background-color: rgb(0,%2,%3); color: rgb(%2,%3,255);  } ").arg(y*N+x).arg(y*255/N).arg(x*255/N);
       
   177     }
       
   178 
       
   179     w->setStyleSheet("/* */");
       
   180     if(show) {
       
   181         w->show();
       
   182         QTest::qWaitForWindowShown(w);
       
   183         QApplication::flush();
       
   184         QApplication::processEvents();
       
   185         QTest::qWait(30);
       
   186         QApplication::processEvents();
       
   187     }
       
   188     QApplication::processEvents();
       
   189     int i = 0;
       
   190     QBENCHMARK {
       
   191         w->setStyleSheet(stylesheet + "/*" + QString::number(i) + "*/");
       
   192         i++; // we want a different string in case we have severals iterations
       
   193         if(events)
       
   194             qApp->processEvents();
       
   195     }
       
   196     delete w;
       
   197 }
       
   198 
       
   199 QTEST_MAIN(tst_qstylesheetstyle)
       
   200 
       
   201 #include "main.moc"