tests/auto/qprogressbar/tst_qprogressbar.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 
       
    43 #include <QtTest/QtTest>
       
    44 #include "qprogressbar.h"
       
    45 #include <qapplication.h>
       
    46 #include <qstyleoption.h>
       
    47 #include <qdebug.h>
       
    48 #include <qtimer.h>
       
    49 #include <QStyleFactory>
       
    50 
       
    51 #include "../../shared/util.h"
       
    52 
       
    53 //TESTED_CLASS=
       
    54 //TESTED_FILES=
       
    55 
       
    56 class tst_QProgressBar : public QObject
       
    57 {
       
    58 Q_OBJECT
       
    59 private slots:
       
    60     void getSetCheck();
       
    61     void minMaxSameValue();
       
    62     void destroyIndeterminate();
       
    63     void text();
       
    64     void format();
       
    65     void setValueRepaint();
       
    66     void sizeHint();
       
    67     void formatedText_data();
       
    68     void formatedText();
       
    69 
       
    70     void task245201_testChangeStyleAndDelete_data();
       
    71     void task245201_testChangeStyleAndDelete();
       
    72 };
       
    73 
       
    74 // Testing get/set functions
       
    75 void tst_QProgressBar::getSetCheck()
       
    76 {
       
    77     QProgressBar obj1;
       
    78     // bool QProgressBar::invertedAppearance()
       
    79     // void QProgressBar::setInvertedAppearance(bool)
       
    80     obj1.setInvertedAppearance(false);
       
    81     QCOMPARE(false, obj1.invertedAppearance());
       
    82     obj1.setInvertedAppearance(true);
       
    83     QCOMPARE(true, obj1.invertedAppearance());
       
    84 
       
    85     // int QProgressBar::minimum()
       
    86     // void QProgressBar::setMinimum(int)
       
    87     obj1.setMinimum(0);
       
    88     QCOMPARE(0, obj1.minimum());
       
    89     obj1.setMinimum(INT_MAX);
       
    90     QCOMPARE(INT_MAX, obj1.minimum());
       
    91     obj1.setMinimum(INT_MIN);
       
    92     QCOMPARE(INT_MIN, obj1.minimum());
       
    93 
       
    94     // int QProgressBar::maximum()
       
    95     // void QProgressBar::setMaximum(int)
       
    96     obj1.setMaximum(0);
       
    97     QCOMPARE(0, obj1.maximum());
       
    98     obj1.setMaximum(INT_MIN);
       
    99     QCOMPARE(INT_MIN, obj1.maximum());
       
   100     obj1.setMaximum(INT_MAX);
       
   101     QCOMPARE(INT_MAX, obj1.maximum());
       
   102 
       
   103     // int QProgressBar::value()
       
   104     // void QProgressBar::setValue(int)
       
   105     obj1.setValue(0);
       
   106     QCOMPARE(0, obj1.value());
       
   107     obj1.setValue(INT_MIN);
       
   108     QCOMPARE(INT_MIN, obj1.value());
       
   109     obj1.setValue(INT_MAX);
       
   110     QCOMPARE(INT_MAX, obj1.value());
       
   111 }
       
   112 
       
   113 void tst_QProgressBar::minMaxSameValue()
       
   114 {
       
   115     QProgressBar bar;
       
   116     bar.setRange(10, 10);
       
   117     bar.setValue(10);
       
   118     bar.show();
       
   119 }
       
   120 
       
   121 void tst_QProgressBar::destroyIndeterminate()
       
   122 {
       
   123     // This test crashes on styles that animate indeterminate / busy
       
   124     // progressbars, and forget to remove the bars from internal logics when
       
   125     // it's deleted.
       
   126     QPointer<QProgressBar> bar = new QProgressBar;
       
   127     bar->setMaximum(0);
       
   128     bar->show();
       
   129 
       
   130     QEventLoop loop;
       
   131     QTimer::singleShot(500, bar, SLOT(deleteLater()));
       
   132     QTimer::singleShot(3000, &loop, SLOT(quit()));
       
   133     loop.exec();
       
   134 
       
   135     QVERIFY(!bar);
       
   136 }
       
   137 
       
   138 void tst_QProgressBar::text()
       
   139 {
       
   140     QProgressBar bar;
       
   141     bar.setRange(10, 10);
       
   142     bar.setValue(10);
       
   143     QCOMPARE(bar.text(), QString("100%"));
       
   144     bar.setRange(0, 10);
       
   145     QCOMPARE(bar.text(), QString("100%"));
       
   146     bar.setValue(5);
       
   147     QCOMPARE(bar.text(), QString("50%"));
       
   148     bar.setRange(0, 5);
       
   149     bar.setValue(0);
       
   150     bar.setRange(5, 5);
       
   151     QCOMPARE(bar.text(), QString());
       
   152 }
       
   153 
       
   154 class ProgressBar : public QProgressBar
       
   155 {
       
   156     void paintEvent(QPaintEvent *event)
       
   157     {
       
   158         repainted = true;
       
   159         QProgressBar::paintEvent(event);
       
   160     }
       
   161 public:
       
   162     bool repainted;
       
   163     using QProgressBar::initStyleOption;
       
   164 };
       
   165 
       
   166 void tst_QProgressBar::format()
       
   167 {
       
   168     ProgressBar bar;
       
   169     bar.setRange(0, 10);
       
   170     bar.setValue(1);
       
   171     bar.show();
       
   172     QTest::qWaitForWindowShown(&bar);
       
   173 
       
   174     QTest::qWait(20);
       
   175     bar.repainted = false;
       
   176     bar.setFormat("%v of %m (%p%)");
       
   177     QTest::qWait(20);
       
   178     QTRY_VERIFY(bar.repainted);
       
   179     bar.repainted = false;
       
   180     bar.setFormat("%v of %m (%p%)");
       
   181     qApp->processEvents();
       
   182 #ifndef Q_WS_MAC
       
   183     // The Mac scroll bar is animated, which means we get paint events all the time.
       
   184     QVERIFY(!bar.repainted);
       
   185 #endif
       
   186     QCOMPARE(bar.text(), QString("1 of 10 (10%)"));
       
   187     bar.setRange(5, 5);
       
   188     bar.setValue(5);
       
   189     QCOMPARE(bar.text(), QString("5 of 0 (100%)"));
       
   190     bar.setRange(0, 5);
       
   191     bar.setValue(0);
       
   192     bar.setRange(5, 5);
       
   193     QCOMPARE(bar.text(), QString());
       
   194 }
       
   195 
       
   196 void tst_QProgressBar::setValueRepaint()
       
   197 {
       
   198     ProgressBar pbar;
       
   199     pbar.setMinimum(0);
       
   200     pbar.setMaximum(10);
       
   201     pbar.setFormat("%v");
       
   202     pbar.show();
       
   203     QTest::qWaitForWindowShown(&pbar);
       
   204 
       
   205     QApplication::processEvents();
       
   206     for (int i = pbar.minimum(); i < pbar.maximum(); ++i) {
       
   207         pbar.repainted = false;
       
   208         pbar.setValue(i);
       
   209         QTest::qWait(50);
       
   210         QTRY_VERIFY(pbar.repainted);
       
   211     }
       
   212 }
       
   213 
       
   214 void tst_QProgressBar::sizeHint()
       
   215 {
       
   216     ProgressBar bar;
       
   217     bar.setMinimum(0);
       
   218     bar.setMaximum(10);
       
   219     bar.setValue(5);
       
   220 
       
   221     //test if the sizeHint is big enough
       
   222     QFontMetrics fm = bar.fontMetrics();
       
   223     QStyleOptionProgressBarV2 opt;
       
   224     bar.initStyleOption(&opt);
       
   225     QSize size = QSize(9 * 7 + fm.width(QLatin1Char('0')) * 4, fm.height() + 8);
       
   226     size= bar.style()->sizeFromContents(QStyle::CT_ProgressBar, &opt, size, &bar);
       
   227     QSize barSize = bar.sizeHint();
       
   228     QVERIFY(barSize.width() >= size.width());
       
   229     QCOMPARE(barSize.height(), size.height());
       
   230 }
       
   231 
       
   232 void tst_QProgressBar::formatedText_data()
       
   233 {
       
   234     QTest::addColumn<int>("minimum");
       
   235     QTest::addColumn<int>("maximum");
       
   236     QTest::addColumn<int>("value");
       
   237     QTest::addColumn<QString>("format");
       
   238     QTest::addColumn<QString>("text");
       
   239 
       
   240     QTest::newRow("1") <<  -100 << 100 << 0 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 50 - 0 - 200 ");
       
   241     QTest::newRow("2") <<  -100 << 0 << -25 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 75 - -25 - 100 ");
       
   242     QTest::newRow("3") <<  10 << 10 << 10 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 100 - 10 - 0 ");
       
   243     QTest::newRow("task152227") <<  INT_MIN << INT_MAX << 42 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 50 - 42 - 4294967295 ");
       
   244 }
       
   245 
       
   246 void tst_QProgressBar::formatedText()
       
   247 {
       
   248     QFETCH(int, minimum);
       
   249     QFETCH(int, maximum);
       
   250     QFETCH(int, value);
       
   251     QFETCH(QString, format);
       
   252     QFETCH(QString, text);
       
   253     QProgressBar bar;
       
   254     bar.setRange(minimum, maximum);
       
   255     bar.setValue(value);
       
   256     bar.setFormat(format);
       
   257     QCOMPARE(bar.text(), text);
       
   258 }
       
   259 
       
   260 void tst_QProgressBar::task245201_testChangeStyleAndDelete_data()
       
   261 {
       
   262     QTest::addColumn<QString>("style1_str");
       
   263     QTest::addColumn<QString>("style2_str");
       
   264 
       
   265     QTest::newRow("plastique-windows") << QString::fromLatin1("plastique") << QString::fromLatin1("windows");
       
   266     QTest::newRow("mlotif-windows") << QString::fromLatin1("motif") << QString::fromLatin1("windows");
       
   267     QTest::newRow("cleanlooks-cde") << QString::fromLatin1("cleanlooks") << QString::fromLatin1("cde");
       
   268     QTest::newRow("gtk-plastique") << QString::fromLatin1("gtk") << QString::fromLatin1("plastique");
       
   269 }
       
   270 
       
   271 void tst_QProgressBar::task245201_testChangeStyleAndDelete()
       
   272 {
       
   273     QFETCH(QString, style1_str);
       
   274     QFETCH(QString, style2_str);
       
   275 
       
   276     QProgressBar *bar = new QProgressBar;
       
   277 
       
   278     QStyle *style = QStyleFactory::create(style1_str);
       
   279     bar->setStyle(style);
       
   280     bar->show();
       
   281     QStyle *style2 = QStyleFactory::create(style2_str);
       
   282     bar->setStyle(style2);
       
   283     QTest::qWait(10);
       
   284 
       
   285     delete bar;
       
   286     QTest::qWait(100); //should not crash
       
   287     delete style;
       
   288     delete style2;
       
   289 }
       
   290 
       
   291 QTEST_MAIN(tst_QProgressBar)
       
   292 #include "tst_qprogressbar.moc"