tests/benchmarks/qstylesheetstyle/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 // 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     int i = 0;
       
    86     QBENCHMARK {
       
    87         w->setStyleSheet("/*" + QString::number(i) + "*/");
       
    88         i++; // we want a different string in case we have severals iterations
       
    89     }
       
    90     delete w;
       
    91 }
       
    92 
       
    93 void tst_qstylesheetstyle::empty_events()
       
    94 {
       
    95     QWidget *w = buildSimpleWidgets();
       
    96     w->setStyleSheet("/* */");
       
    97     int i = 0;
       
    98     QBENCHMARK {
       
    99         w->setStyleSheet("/*" + QString::number(i) + "*/");
       
   100         i++; // we want a different string in case we have severals iterations
       
   101         qApp->processEvents();
       
   102     }
       
   103     delete w;
       
   104 }
       
   105 
       
   106 static const char *simple_css = 
       
   107    " QLineEdit { background: red; }   QPushButton { border: 1px solid yellow; color: pink; }  \n"
       
   108    " QCheckBox { margin: 3px 5px; background-color:red; } QAbstractButton { background-color: #456; } \n"
       
   109    " QFrame { padding: 3px; } QLabel { color: black } QSpinBox:hover { background-color:blue; }  ";
       
   110 
       
   111 void tst_qstylesheetstyle::simple()
       
   112 {
       
   113     QWidget *w = buildSimpleWidgets();
       
   114     w->setStyleSheet("/* */");
       
   115     int i = 0;
       
   116     QBENCHMARK {
       
   117         w->setStyleSheet(QString(simple_css) + "/*" + QString::number(i) + "*/");
       
   118         i++; // we want a different string in case we have severals iterations
       
   119     }
       
   120     delete w;
       
   121 }
       
   122 
       
   123 void tst_qstylesheetstyle::simple_events()
       
   124 {
       
   125     QWidget *w = buildSimpleWidgets();
       
   126     w->setStyleSheet("/* */");
       
   127     int i = 0;
       
   128     QBENCHMARK {
       
   129         w->setStyleSheet(QString(simple_css) + "/*" + QString::number(i) + "*/");
       
   130         i++; // we want a different string in case we have severals iterations
       
   131         qApp->processEvents();
       
   132     }
       
   133     delete w;
       
   134 }
       
   135 
       
   136 void tst_qstylesheetstyle::grid_data()
       
   137 {
       
   138         QTest::addColumn<bool>("events");
       
   139         QTest::addColumn<bool>("show");
       
   140         QTest::addColumn<int>("N");
       
   141         for (int n = 5; n <= 25; n += 5) { 
       
   142            const QByteArray nString = QByteArray::number(n*n);
       
   143             QTest::newRow(("simple--" + nString).constData()) << false << false << n;
       
   144             QTest::newRow(("events--" + nString).constData()) << true << false << n;
       
   145             QTest::newRow(("show--" + nString).constData()) << true << true << n;
       
   146         }
       
   147 }
       
   148 
       
   149 
       
   150 void tst_qstylesheetstyle::grid()
       
   151 {
       
   152     QFETCH(bool, events);
       
   153     QFETCH(bool, show);
       
   154     QFETCH(int, N);
       
   155 
       
   156     QWidget *w = new QWidget();
       
   157     QGridLayout *layout = new QGridLayout(w);
       
   158     w->setLayout(layout);
       
   159     QString stylesheet;
       
   160     for(int x=0; x<N ;x++)
       
   161         for(int y=0; y<N ;y++) {
       
   162         QLabel *label = new QLabel(QString::number(y * N + x));
       
   163         layout->addWidget(label ,x,y);
       
   164         label->setObjectName(QString("label%1").arg(y * N + x));
       
   165         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);
       
   166     }
       
   167 
       
   168     w->setStyleSheet("/* */");
       
   169     if(show) {
       
   170         w->show();
       
   171         QTest::qWait(30);
       
   172     }
       
   173     int i = 0;
       
   174     QBENCHMARK {
       
   175         w->setStyleSheet(stylesheet + "/*" + QString::number(i) + "*/");
       
   176         i++; // we want a different string in case we have severals iterations
       
   177         if(events)
       
   178             qApp->processEvents();
       
   179     }
       
   180     delete w;
       
   181 }
       
   182 
       
   183 QTEST_MAIN(tst_qstylesheetstyle)
       
   184 
       
   185 #include "main.moc"