tests/auto/qspinbox/tst_qspinbox.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 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qdebug.h>
       
    45 #include <qapplication.h>
       
    46 #include <limits.h>
       
    47 
       
    48 #ifdef Q_WS_X11
       
    49 #include <unistd.h>
       
    50 #endif
       
    51 
       
    52 #include <qspinbox.h>
       
    53 #include <qlocale.h>
       
    54 #include <qlineedit.h>
       
    55 #include <qlayout.h>
       
    56 #include <QSpinBox>
       
    57 #include <QWidget>
       
    58 #include <QString>
       
    59 #include <QValidator>
       
    60 #include <QLineEdit>
       
    61 #include <QObject>
       
    62 #include <QStringList>
       
    63 #include <QList>
       
    64 #include <QLocale>
       
    65 #include <QDoubleSpinBox>
       
    66 #include <QVBoxLayout>
       
    67 #include <QKeySequence>
       
    68 #include <QStackedWidget>
       
    69 #include <QDebug>
       
    70 #include "../../shared/util.h"
       
    71 
       
    72 //TESTED_CLASS=
       
    73 //TESTED_FILES=
       
    74 
       
    75 class SpinBox : public QSpinBox
       
    76 {
       
    77 public:
       
    78     SpinBox(QWidget *parent = 0)
       
    79         : QSpinBox(parent)
       
    80     {}
       
    81     QString textFromValue(int v) const
       
    82     {
       
    83         return QSpinBox::textFromValue(v);
       
    84     }
       
    85     QValidator::State validate(QString &text, int &pos) const
       
    86     {
       
    87         return QSpinBox::validate(text, pos);
       
    88     }
       
    89     int valueFromText(const QString &text) const
       
    90     {
       
    91         return QSpinBox::valueFromText(text);
       
    92     }
       
    93 
       
    94     QLineEdit *lineEdit() const { return QSpinBox::lineEdit(); }
       
    95 };
       
    96 
       
    97 class tst_QSpinBox : public QObject
       
    98 {
       
    99     Q_OBJECT
       
   100 public:
       
   101     tst_QSpinBox();
       
   102     virtual ~tst_QSpinBox();
       
   103 public slots:
       
   104     void initTestCase();
       
   105     void init();
       
   106     void cleanupTestCase();
       
   107 private slots:
       
   108     void getSetCheck();
       
   109     void setValue_data();
       
   110     void setValue();
       
   111 
       
   112     void setPrefixSuffix_data();
       
   113     void setPrefixSuffix();
       
   114 
       
   115     void setReadOnly();
       
   116 
       
   117     void setTracking_data();
       
   118     void setTracking();
       
   119 
       
   120     void locale_data();
       
   121     void locale();
       
   122 
       
   123     void setWrapping_data();
       
   124     void setWrapping();
       
   125 
       
   126     void setSpecialValueText_data();
       
   127     void setSpecialValueText();
       
   128 
       
   129     void setSingleStep_data();
       
   130     void setSingleStep();
       
   131 
       
   132     void setMinMax_data();
       
   133     void setMinMax();
       
   134 
       
   135     void editingFinished();
       
   136 
       
   137     void valueFromTextAndValidate_data();
       
   138     void valueFromTextAndValidate();
       
   139 
       
   140     void removeAll();
       
   141     void startWithDash();
       
   142     void undoRedo();
       
   143 
       
   144     void specialValue();
       
   145     void textFromValue();
       
   146 
       
   147     void sizeHint();
       
   148 
       
   149 public slots:
       
   150     void valueChangedHelper(const QString &);
       
   151     void valueChangedHelper(int);
       
   152 private:
       
   153     QStringList actualTexts;
       
   154     QList<int> actualValues;
       
   155     QWidget *testFocusWidget;
       
   156 };
       
   157 
       
   158 typedef QList<int> IntList;
       
   159 Q_DECLARE_METATYPE(IntList)
       
   160 Q_DECLARE_METATYPE(QLocale)
       
   161 
       
   162 // Testing get/set functions
       
   163 void tst_QSpinBox::getSetCheck()
       
   164 {
       
   165     QSpinBox obj1;
       
   166     // int QSpinBox::singleStep()
       
   167     // void QSpinBox::setSingleStep(int)
       
   168     obj1.setSingleStep(0);
       
   169     QCOMPARE(0, obj1.singleStep());
       
   170     obj1.setSingleStep(INT_MIN);
       
   171     QCOMPARE(0, obj1.singleStep()); // Can't have negative steps => keep old value
       
   172     obj1.setSingleStep(INT_MAX);
       
   173     QCOMPARE(INT_MAX, obj1.singleStep());
       
   174 
       
   175     // int QSpinBox::minimum()
       
   176     // void QSpinBox::setMinimum(int)
       
   177     obj1.setMinimum(0);
       
   178     QCOMPARE(0, obj1.minimum());
       
   179     obj1.setMinimum(INT_MIN);
       
   180     QCOMPARE(INT_MIN, obj1.minimum());
       
   181     obj1.setMinimum(INT_MAX);
       
   182     QCOMPARE(INT_MAX, obj1.minimum());
       
   183 
       
   184     // int QSpinBox::maximum()
       
   185     // void QSpinBox::setMaximum(int)
       
   186     obj1.setMaximum(0);
       
   187     QCOMPARE(0, obj1.maximum());
       
   188     obj1.setMaximum(INT_MIN);
       
   189     QCOMPARE(INT_MIN, obj1.maximum());
       
   190     obj1.setMaximum(INT_MAX);
       
   191     QCOMPARE(INT_MAX, obj1.maximum());
       
   192 
       
   193     // int QSpinBox::value()
       
   194     // void QSpinBox::setValue(int)
       
   195     obj1.setValue(0);
       
   196     QCOMPARE(0, obj1.value());
       
   197     obj1.setValue(INT_MIN);
       
   198     QCOMPARE(INT_MIN, obj1.value());
       
   199     obj1.setValue(INT_MAX);
       
   200     QCOMPARE(INT_MAX, obj1.value());
       
   201 
       
   202     QDoubleSpinBox obj2;
       
   203     // double QDoubleSpinBox::singleStep()
       
   204     // void QDoubleSpinBox::setSingleStep(double)
       
   205     obj2.setSingleStep(0.0);
       
   206     QCOMPARE(0.0, obj2.singleStep());
       
   207     obj2.setSingleStep(1.0);
       
   208     QCOMPARE(1.0, obj2.singleStep());
       
   209 
       
   210     // double QDoubleSpinBox::minimum()
       
   211     // void QDoubleSpinBox::setMinimum(double)
       
   212     obj2.setMinimum(1.0);
       
   213     QCOMPARE(1.0, obj2.minimum());
       
   214     obj2.setMinimum(0.0);
       
   215     QCOMPARE(0.0, obj2.minimum());
       
   216     obj2.setMinimum(-1.0);
       
   217     QCOMPARE(-1.0, obj2.minimum());
       
   218 
       
   219     // double QDoubleSpinBox::maximum()
       
   220     // void QDoubleSpinBox::setMaximum(double)
       
   221     obj2.setMaximum(-1.0);
       
   222     QCOMPARE(-1.0, obj2.maximum());
       
   223     obj2.setMaximum(0.0);
       
   224     QCOMPARE(0.0, obj2.maximum());
       
   225     obj2.setMaximum(1.0);
       
   226     QCOMPARE(1.0, obj2.maximum());
       
   227 
       
   228     // int QDoubleSpinBox::decimals()
       
   229     // void QDoubleSpinBox::setDecimals(int)
       
   230     obj2.setDecimals(0);
       
   231     QCOMPARE(0, obj2.decimals());
       
   232     obj2.setDecimals(INT_MIN);
       
   233     QCOMPARE(0, obj2.decimals()); // Range<0, 13>
       
   234 
       
   235     //obj2.setDecimals(INT_MAX);
       
   236     //QCOMPARE(13, obj2.decimals()); // Range<0, 13>
       
   237     obj2.setDecimals(128);
       
   238     QCOMPARE(obj2.decimals(), 128); // Range<0, 128>
       
   239 
       
   240     // double QDoubleSpinBox::value()
       
   241     // void QDoubleSpinBox::setValue(double)
       
   242     obj2.setValue(-1.0);
       
   243     QCOMPARE(-1.0, obj2.value());
       
   244     obj2.setValue(0.0);
       
   245     QCOMPARE(0.0, obj2.value());
       
   246     obj2.setValue(1.0);
       
   247     QCOMPARE(1.0, obj2.value());
       
   248 
       
   249     // Make sure we update line edit geometry when updating
       
   250     // buttons - see task 235747
       
   251     QRect oldEditGeometry = obj1.childrenRect();
       
   252     obj1.setButtonSymbols(QAbstractSpinBox::NoButtons);
       
   253     QVERIFY(obj1.childrenRect() != oldEditGeometry);
       
   254 }
       
   255 
       
   256 tst_QSpinBox::tst_QSpinBox()
       
   257 {
       
   258 
       
   259 }
       
   260 
       
   261 tst_QSpinBox::~tst_QSpinBox()
       
   262 {
       
   263 
       
   264 }
       
   265 
       
   266 void tst_QSpinBox::initTestCase()
       
   267 {
       
   268     testFocusWidget = new QWidget(0);
       
   269     testFocusWidget->resize(200, 100);
       
   270     testFocusWidget->show();
       
   271 }
       
   272 
       
   273 void tst_QSpinBox::init()
       
   274 {
       
   275     QLocale::setDefault(QLocale(QLocale::C));
       
   276 }
       
   277 
       
   278 void tst_QSpinBox::cleanupTestCase()
       
   279 {
       
   280     delete testFocusWidget;
       
   281     testFocusWidget = 0;
       
   282 }
       
   283 
       
   284 void tst_QSpinBox::setValue_data()
       
   285 {
       
   286     QTest::addColumn<int>("set");
       
   287     QTest::addColumn<int>("expected");
       
   288 
       
   289     QTest::newRow("data0") << 0 << 0;
       
   290     QTest::newRow("data1") << 100 << 100;
       
   291     QTest::newRow("data2") << -100 << -100;
       
   292     QTest::newRow("data3") << INT_MIN << INT_MIN;
       
   293     QTest::newRow("data4") << INT_MAX << INT_MAX;
       
   294 }
       
   295 
       
   296 void tst_QSpinBox::setValue()
       
   297 {
       
   298     QFETCH(int, set);
       
   299     QFETCH(int, expected);
       
   300     QSpinBox spin(0);
       
   301     spin.setRange(INT_MIN, INT_MAX);
       
   302     spin.setValue(set);
       
   303     QCOMPARE(spin.value(), expected);
       
   304 }
       
   305 
       
   306 void tst_QSpinBox::setPrefixSuffix_data()
       
   307 {
       
   308     QTest::addColumn<QString>("prefix");
       
   309     QTest::addColumn<QString>("suffix");
       
   310     QTest::addColumn<int>("value");
       
   311     QTest::addColumn<QString>("expectedText");
       
   312     QTest::addColumn<QString>("expectedCleanText");
       
   313     QTest::addColumn<bool>("show");
       
   314 
       
   315     QTest::newRow("data0") << QString() << QString() << 10 << "10" << "10" << false;
       
   316     QTest::newRow("data1") << QString() << "cm" << 10 << "10cm" << "10" << false;
       
   317     QTest::newRow("data2") << "cm: " << QString() << 10 << "cm: 10" << "10" << false;
       
   318     QTest::newRow("data3") << "length: " << "cm" << 10 << "length: 10cm" << "10" << false;
       
   319 
       
   320     QTest::newRow("data4") << QString() << QString() << 10 << "10" << "10" << true;
       
   321     QTest::newRow("data5") << QString() << "cm" << 10 << "10cm" << "10" << true;
       
   322     QTest::newRow("data6") << "cm: " << QString() << 10 << "cm: 10" << "10" << true;
       
   323     QTest::newRow("data7") << "length: " << "cm" << 10 << "length: 10cm" << "10" << true;
       
   324 }
       
   325 
       
   326 void tst_QSpinBox::setPrefixSuffix()
       
   327 {
       
   328     QFETCH(QString, prefix);
       
   329     QFETCH(QString, suffix);
       
   330     QFETCH(int, value);
       
   331     QFETCH(QString, expectedText);
       
   332     QFETCH(QString, expectedCleanText);
       
   333     QFETCH(bool, show);
       
   334 
       
   335     QSpinBox spin(0);
       
   336     spin.setPrefix(prefix);
       
   337     spin.setSuffix(suffix);
       
   338     spin.setValue(value);
       
   339     if (show)
       
   340         spin.show();
       
   341 
       
   342     QCOMPARE(spin.prefix(), prefix);
       
   343     QCOMPARE(spin.suffix(), suffix);
       
   344     QCOMPARE(spin.text(), expectedText);
       
   345     QCOMPARE(spin.cleanText(), expectedCleanText);
       
   346 }
       
   347 
       
   348 void tst_QSpinBox::valueChangedHelper(const QString &text)
       
   349 {
       
   350     actualTexts << text;
       
   351 }
       
   352 
       
   353 void tst_QSpinBox::valueChangedHelper(int value)
       
   354 {
       
   355     actualValues << value;
       
   356 }
       
   357 
       
   358 void tst_QSpinBox::setReadOnly()
       
   359 {
       
   360     QSpinBox spin(0);
       
   361     spin.show();
       
   362     QTest::keyClick(&spin, Qt::Key_Up);
       
   363     QCOMPARE(spin.value(), 1);
       
   364     spin.setReadOnly(true);
       
   365     QTest::keyClick(&spin, Qt::Key_Up);
       
   366     QCOMPARE(spin.value(), 1);
       
   367     spin.stepBy(1);
       
   368     QCOMPARE(spin.value(), 2);
       
   369     spin.setReadOnly(false);
       
   370     QTest::keyClick(&spin, Qt::Key_Up);
       
   371     QCOMPARE(spin.value(), 3);
       
   372 }
       
   373 void tst_QSpinBox::setTracking_data()
       
   374 {
       
   375     QTest::addColumn<QTestEventList>("keys");
       
   376     QTest::addColumn<QStringList>("texts");
       
   377     QTest::addColumn<bool>("tracking");
       
   378 
       
   379     QTestEventList keys;
       
   380     QStringList texts1;
       
   381     QStringList texts2;
       
   382 
       
   383 #ifdef Q_WS_MAC
       
   384     keys.addKeyClick(Qt::Key_Right, Qt::ControlModifier);
       
   385 #else
       
   386     keys.addKeyClick(Qt::Key_End);
       
   387 #endif
       
   388     keys.addKeyClick('7');
       
   389     keys.addKeyClick('9');
       
   390     keys.addKeyClick(Qt::Key_Enter);
       
   391     keys.addKeyClick(Qt::Key_Enter);
       
   392     keys.addKeyClick(Qt::Key_Enter);
       
   393     texts1 << "07" << "079" << "79" << "79" << "79";
       
   394     texts2 << "79";
       
   395     QTest::newRow("data1") << keys << texts1 << true;
       
   396     QTest::newRow("data2") << keys << texts2 << false;
       
   397 }
       
   398 
       
   399 void tst_QSpinBox::setTracking()
       
   400 {
       
   401     actualTexts.clear();
       
   402     QFETCH(QTestEventList, keys);
       
   403     QFETCH(QStringList, texts);
       
   404     QFETCH(bool, tracking);
       
   405 
       
   406     QSpinBox spin(0);
       
   407     spin.setKeyboardTracking(tracking);
       
   408     spin.show();
       
   409     connect(&spin, SIGNAL(valueChanged(QString)), this, SLOT(valueChangedHelper(const QString &)));
       
   410 
       
   411     keys.simulate(&spin);
       
   412     QCOMPARE(actualTexts, texts);
       
   413 }
       
   414 
       
   415 void tst_QSpinBox::setWrapping_data()
       
   416 {
       
   417     QTest::addColumn<bool>("wrapping");
       
   418     QTest::addColumn<int>("minimum");
       
   419     QTest::addColumn<int>("maximum");
       
   420     QTest::addColumn<int>("startValue");
       
   421     QTest::addColumn<QTestEventList>("keys");
       
   422     QTest::addColumn<IntList>("expected");
       
   423 
       
   424     QTestEventList keys;
       
   425     IntList values;
       
   426     keys.addKeyClick(Qt::Key_Up);
       
   427     values << 10;
       
   428     keys.addKeyClick(Qt::Key_Up);
       
   429     QTest::newRow("data0") << false << 0 << 10 << 9 << keys << values;
       
   430 
       
   431     keys.clear();
       
   432     values.clear();
       
   433     keys.addKeyClick(Qt::Key_Up);
       
   434     values << 10;
       
   435     keys.addKeyClick(Qt::Key_Up);
       
   436     values << 0;
       
   437     QTest::newRow("data1") << true << 0 << 10 << 9 << keys << values;
       
   438 
       
   439     keys.clear();
       
   440     values.clear();
       
   441     keys.addKeyClick(Qt::Key_Delete); // doesn't emit because lineedit is empty so intermediate
       
   442     keys.addKeyClick('1');
       
   443     keys.addKeyClick(Qt::Key_Down);
       
   444     keys.addKeyClick(Qt::Key_Down);
       
   445     values << 1 << 0;
       
   446     QTest::newRow("data2") << false << 0 << 10 << 9 << keys << values;
       
   447 
       
   448     keys.clear();
       
   449     values.clear();
       
   450     keys.addKeyClick(Qt::Key_Delete);
       
   451     keys.addKeyClick('1');
       
   452     keys.addKeyClick(Qt::Key_Down);
       
   453     keys.addKeyClick(Qt::Key_Down);
       
   454     values << 1 << 0 << 10;
       
   455     QTest::newRow("data3") << true << 0 << 10 << 9 << keys << values;
       
   456 
       
   457     keys.clear();
       
   458     values.clear();
       
   459     keys.addKeyClick(Qt::Key_PageDown);
       
   460     keys.addKeyClick(Qt::Key_Down);
       
   461     values << 0;
       
   462     QTest::newRow("data4") << false << 0 << 10 << 6 << keys << values;
       
   463 
       
   464     keys.clear();
       
   465     values.clear();
       
   466     keys.addKeyClick(Qt::Key_PageDown);
       
   467     keys.addKeyClick(Qt::Key_Down);
       
   468     values << 0 << 10;
       
   469     QTest::newRow("data5") << true << 0 << 10 << 6 << keys << values;
       
   470 
       
   471     keys.clear();
       
   472     values.clear();
       
   473     keys.addKeyClick(Qt::Key_PageUp);
       
   474     keys.addKeyClick(Qt::Key_PageDown);
       
   475     keys.addKeyClick(Qt::Key_Down);
       
   476     keys.addKeyClick(Qt::Key_Up);
       
   477     keys.addKeyClick(Qt::Key_PageDown);
       
   478     keys.addKeyClick(Qt::Key_PageDown);
       
   479     values << 10 << 0 << 10 << 0 << 10 << 0;
       
   480     QTest::newRow("data6") << true << 0 << 10 << 6 << keys << values;
       
   481 
       
   482 }
       
   483 
       
   484 
       
   485 void tst_QSpinBox::setWrapping()
       
   486 {
       
   487     QFETCH(bool, wrapping);
       
   488     QFETCH(int, minimum);
       
   489     QFETCH(int, maximum);
       
   490     QFETCH(int, startValue);
       
   491     QFETCH(QTestEventList, keys);
       
   492     QFETCH(IntList, expected);
       
   493 
       
   494     QSpinBox spin(0);
       
   495     QVERIFY(!spin.wrapping());
       
   496     spin.setMinimum(minimum);
       
   497     spin.setMaximum(maximum);
       
   498     spin.setValue(startValue);
       
   499     spin.setWrapping(wrapping);
       
   500     spin.show();
       
   501     actualValues.clear();
       
   502     connect(&spin, SIGNAL(valueChanged(int)), this, SLOT(valueChangedHelper(int)));
       
   503 
       
   504     keys.simulate(&spin);
       
   505 
       
   506     QCOMPARE(actualValues.size(), expected.size());
       
   507     for (int i=0; i<qMin(actualValues.size(), expected.size()); ++i) {
       
   508         QCOMPARE(actualValues.at(i), expected.at(i));
       
   509     }
       
   510 }
       
   511 
       
   512 void tst_QSpinBox::setSpecialValueText_data()
       
   513 {
       
   514     QTest::addColumn<QString>("specialValueText");
       
   515     QTest::addColumn<int>("minimum");
       
   516     QTest::addColumn<int>("maximum");
       
   517     QTest::addColumn<int>("value");
       
   518     QTest::addColumn<QString>("expected");
       
   519     QTest::addColumn<bool>("show");
       
   520 
       
   521     QTest::newRow("data0") << QString() << 0 << 10 << 1 << "1" << false;
       
   522     QTest::newRow("data1") << QString() << 0 << 10 << 1 << "1" << true;
       
   523     QTest::newRow("data2") << "foo" << 0 << 10 << 0 << "foo" << false;
       
   524     QTest::newRow("data3") << "foo" << 0 << 10 << 0 << "foo" << true;
       
   525 }
       
   526 
       
   527 void tst_QSpinBox::setSpecialValueText()
       
   528 {
       
   529     QFETCH(QString, specialValueText);
       
   530     QFETCH(int, minimum);
       
   531     QFETCH(int, maximum);
       
   532     QFETCH(int, value);
       
   533     QFETCH(QString, expected);
       
   534     QFETCH(bool, show);
       
   535 
       
   536     QSpinBox spin(0);
       
   537     spin.setSpecialValueText(specialValueText);
       
   538     QCOMPARE(spin.specialValueText(), specialValueText);
       
   539     spin.setMinimum(minimum);
       
   540     spin.setMaximum(maximum);
       
   541     spin.setValue(value);
       
   542     if (show)
       
   543         spin.show();
       
   544 
       
   545     QCOMPARE(spin.text(), expected);
       
   546 }
       
   547 
       
   548 void tst_QSpinBox::setSingleStep_data()
       
   549 {
       
   550     QTest::addColumn<int>("singleStep");
       
   551     QTest::addColumn<int>("startValue");
       
   552     QTest::addColumn<QTestEventList>("keys");
       
   553     QTest::addColumn<IntList>("expected");
       
   554     QTest::addColumn<bool>("show");
       
   555 
       
   556     QTestEventList keys;
       
   557     IntList values;
       
   558     keys.addKeyClick(Qt::Key_Up);
       
   559     keys.addKeyClick(Qt::Key_Down);
       
   560     keys.addKeyClick(Qt::Key_Up);
       
   561     values << 11 << 10 << 11;
       
   562     QTest::newRow("data0") << 1 << 10 << keys << values << false;
       
   563     QTest::newRow("data1") << 1 << 10 << keys << values << true;
       
   564 
       
   565     keys.clear();
       
   566     values.clear();
       
   567     keys.addKeyClick(Qt::Key_Up);
       
   568     keys.addKeyClick(Qt::Key_Down);
       
   569     keys.addKeyClick(Qt::Key_Up);
       
   570     values << 12 << 10 << 12;
       
   571     QTest::newRow("data2") << 2 << 10 << keys << values << false;
       
   572     QTest::newRow("data3") << 2 << 10 << keys << values << true;
       
   573 }
       
   574 
       
   575 void tst_QSpinBox::setSingleStep()
       
   576 {
       
   577     QFETCH(int, singleStep);
       
   578     QFETCH(int, startValue);
       
   579     QFETCH(QTestEventList, keys);
       
   580     QFETCH(IntList, expected);
       
   581     QFETCH(bool, show);
       
   582 
       
   583     QSpinBox spin(0);
       
   584     actualValues.clear();
       
   585     spin.setSingleStep(singleStep);
       
   586     QCOMPARE(spin.singleStep(), singleStep);
       
   587     spin.setValue(startValue);
       
   588     if (show)
       
   589         spin.show();
       
   590     connect(&spin, SIGNAL(valueChanged(int)), this, SLOT(valueChangedHelper(int)));
       
   591 
       
   592     QCOMPARE(actualValues.size(), 0);
       
   593     keys.simulate(&spin);
       
   594     QCOMPARE(actualValues.size(), expected.size());
       
   595     for (int i=0; i<qMin(actualValues.size(), expected.size()); ++i) {
       
   596         QCOMPARE(actualValues.at(i), expected.at(i));
       
   597     }
       
   598 }
       
   599 
       
   600 void tst_QSpinBox::setMinMax_data()
       
   601 {
       
   602     QTest::addColumn<int>("startValue");
       
   603     QTest::addColumn<int>("mini");
       
   604     QTest::addColumn<int>("maxi");
       
   605     QTest::addColumn<QTestEventList>("keys");
       
   606     QTest::addColumn<int>("expected");
       
   607     QTest::addColumn<bool>("show");
       
   608 
       
   609     QTestEventList keys;
       
   610     keys.addKeyClick(Qt::Key_Up);
       
   611     keys.addKeyClick(Qt::Key_Up);
       
   612     keys.addKeyClick(Qt::Key_Up);
       
   613     keys.addKeyClick(Qt::Key_Up);
       
   614     keys.addKeyClick(Qt::Key_Up);
       
   615     QTest::newRow("data0") << 1 << INT_MIN << 2 << keys << 2 << false;
       
   616     QTest::newRow("data1") << 1 << INT_MIN << 2 << keys << 2 << true;
       
   617 
       
   618     keys.clear();
       
   619     QTest::newRow("data2") << 2 << INT_MAX - 2 << INT_MAX << keys << INT_MAX - 2 << false;
       
   620     QTest::newRow("data3") << 2 << INT_MAX - 2 << INT_MAX << keys << INT_MAX - 2 << true;
       
   621 }
       
   622 
       
   623 void tst_QSpinBox::setMinMax()
       
   624 {
       
   625     QFETCH(int, startValue);
       
   626     QFETCH(int, mini);
       
   627     QFETCH(int, maxi);
       
   628     QFETCH(QTestEventList, keys);
       
   629     QFETCH(int, expected);
       
   630     QFETCH(bool, show);
       
   631 
       
   632     QSpinBox spin(0);
       
   633     spin.setValue(startValue);
       
   634     spin.setMinimum(mini);
       
   635     spin.setMaximum(maxi);
       
   636     QCOMPARE(spin.minimum(), mini);
       
   637     QCOMPARE(spin.maximum(), maxi);
       
   638     if (show)
       
   639         spin.show();
       
   640     keys.simulate(&spin);
       
   641     QCOMPARE(spin.value(), expected);
       
   642 }
       
   643 
       
   644 void tst_QSpinBox::valueFromTextAndValidate_data()
       
   645 {
       
   646     const int Intermediate = QValidator::Intermediate;
       
   647     const int Invalid = QValidator::Invalid;
       
   648     const int Acceptable = QValidator::Acceptable;
       
   649 
       
   650     QTest::addColumn<QString>("txt");
       
   651     QTest::addColumn<int>("state");
       
   652     QTest::addColumn<int>("mini");
       
   653     QTest::addColumn<int>("maxi");
       
   654     QTest::addColumn<QString>("expectedText"); // if empty we don't check
       
   655 
       
   656     QTest::newRow("data0") << QString("2") << Intermediate << 3 << 5 << QString();
       
   657     QTest::newRow("data1") << QString() << Intermediate << 0 << 100 << QString();
       
   658     QTest::newRow("data2") << QString("asd") << Invalid << 0 << 100 << QString();
       
   659     QTest::newRow("data3") << QString("2") << Acceptable << 0 << 100 << QString();
       
   660     QTest::newRow("data4") << QString() << Intermediate << 0 << 1 << QString();
       
   661     QTest::newRow("data5") << QString() << Invalid << 0 << 0 << QString();
       
   662     QTest::newRow("data5") << QString("5") << Intermediate << 2004 << 2005 << QString();
       
   663     QTest::newRow("data6") << QString("50") << Intermediate << 2004 << 2005 << QString();
       
   664     QTest::newRow("data7") << QString("205") << Intermediate << 2004 << 2005 << QString();
       
   665     QTest::newRow("data8") << QString("2005") << Acceptable << 2004 << 2005 << QString();
       
   666     QTest::newRow("data9") << QString("3") << Intermediate << 2004 << 2005 << QString();
       
   667     QTest::newRow("data10") << QString("-") << Intermediate << -20 << -10 << QString();
       
   668     QTest::newRow("data11") << QString("-1") << Intermediate << -20 << -10 << QString();
       
   669     QTest::newRow("data12") << QString("-5") << Intermediate << -20 << -10 << QString();
       
   670     QTest::newRow("data13") << QString("-5") << Intermediate << -20 << -16 << QString();
       
   671     QTest::newRow("data14") << QString("-2") << Intermediate << -20 << -16 << QString();
       
   672     QTest::newRow("data15") << QString("2") << Invalid << -20 << -16 << QString();
       
   673     QTest::newRow("data16") << QString() << Intermediate << -20 << -16 << QString();
       
   674     QTest::newRow("data17") << QString("  22") << Acceptable << 0 << 1000 << QString("22");
       
   675     QTest::newRow("data18") << QString("22  ") << Acceptable << 0 << 1000 << QString("22");
       
   676     QTest::newRow("data19") << QString("  22  ") << Acceptable << 0 << 1000 << QString("22");
       
   677     QTest::newRow("data20") << QString("2 2") << Invalid << 0 << 1000 << QString();
       
   678 }
       
   679 
       
   680 static QString stateName(int state)
       
   681 {
       
   682     switch (state) {
       
   683     case QValidator::Acceptable: return QString("Acceptable");
       
   684     case QValidator::Intermediate: return QString("Intermediate");
       
   685     case QValidator::Invalid: return QString("Invalid");
       
   686     default: break;
       
   687     }
       
   688     qWarning("%s %d: this should never happen", __FILE__, __LINE__);
       
   689     return QString();
       
   690 }
       
   691 
       
   692 void tst_QSpinBox::valueFromTextAndValidate()
       
   693 {
       
   694     QFETCH(QString, txt);
       
   695     QFETCH(int, state);
       
   696     QFETCH(int, mini);
       
   697     QFETCH(int, maxi);
       
   698     QFETCH(QString, expectedText);
       
   699 
       
   700     SpinBox sb(0);
       
   701     sb.show();
       
   702     sb.setRange(mini, maxi);
       
   703     int unused = 0;
       
   704     QCOMPARE(stateName(sb.validate(txt, unused)), stateName(state));
       
   705     if (!expectedText.isEmpty())
       
   706         QCOMPARE(txt, expectedText);
       
   707 }
       
   708 
       
   709 void tst_QSpinBox::locale_data()
       
   710 {
       
   711     QTest::addColumn<QLocale>("loc");
       
   712     QTest::addColumn<int>("value");
       
   713     QTest::addColumn<QString>("textFromVal");
       
   714     QTest::addColumn<QString>("text");
       
   715     QTest::addColumn<int>("valFromText");
       
   716 
       
   717     QTest::newRow("data0") << QLocale(QLocale::Norwegian, QLocale::Norway) << 1234 << QString("1234") << QString("2345") << 2345;
       
   718     QTest::newRow("data1") << QLocale(QLocale::German, QLocale::Germany) << 1234 << QString("1234") << QString("2345") << 2345;
       
   719 }
       
   720 
       
   721 void tst_QSpinBox::locale()
       
   722 {
       
   723     QFETCH(QLocale, loc);
       
   724     QFETCH(int, value);
       
   725     QFETCH(QString, textFromVal);
       
   726     QFETCH(QString, text);
       
   727     QFETCH(int, valFromText);
       
   728 
       
   729     QLocale old;
       
   730 
       
   731     QLocale::setDefault(loc);
       
   732     SpinBox box;
       
   733     box.setMaximum(100000);
       
   734     box.setValue(value);
       
   735     QCOMPARE(box.cleanText(), textFromVal);
       
   736 
       
   737     box.lineEdit()->setText(text);
       
   738     QCOMPARE(box.cleanText(), text);
       
   739     box.interpretText();
       
   740 
       
   741     QCOMPARE(box.value(), valFromText);
       
   742 }
       
   743 
       
   744 
       
   745 void tst_QSpinBox::editingFinished()
       
   746 {
       
   747     QVBoxLayout *layout = new QVBoxLayout(testFocusWidget);
       
   748     QSpinBox *box = new QSpinBox(testFocusWidget);
       
   749     layout->addWidget(box);
       
   750     QSpinBox *box2 = new QSpinBox(testFocusWidget);
       
   751     layout->addWidget(box2);
       
   752 
       
   753     testFocusWidget->show();
       
   754     QApplication::setActiveWindow(testFocusWidget);
       
   755     QTest::qWaitForWindowShown(testFocusWidget);
       
   756     box->activateWindow();
       
   757     box->setFocus();
       
   758 
       
   759     QTRY_COMPARE(qApp->focusWidget(), box);
       
   760 
       
   761     QSignalSpy editingFinishedSpy1(box, SIGNAL(editingFinished()));
       
   762     QSignalSpy editingFinishedSpy2(box2, SIGNAL(editingFinished()));
       
   763 
       
   764     box->setFocus();
       
   765     QTest::keyClick(box, Qt::Key_Up);
       
   766     QTest::keyClick(box, Qt::Key_Up);
       
   767 
       
   768     QCOMPARE(editingFinishedSpy1.count(), 0);
       
   769     QCOMPARE(editingFinishedSpy2.count(), 0);
       
   770 
       
   771     QTest::keyClick(box2, Qt::Key_Up);
       
   772     QTest::keyClick(box2, Qt::Key_Up);
       
   773     box2->setFocus();
       
   774     QCOMPARE(editingFinishedSpy1.count(), 1);
       
   775     box->setFocus();
       
   776     QCOMPARE(editingFinishedSpy1.count(), 1);
       
   777     QCOMPARE(editingFinishedSpy2.count(), 1);
       
   778     QTest::keyClick(box, Qt::Key_Up);
       
   779     QCOMPARE(editingFinishedSpy1.count(), 1);
       
   780     QCOMPARE(editingFinishedSpy2.count(), 1);
       
   781     QTest::keyClick(box, Qt::Key_Enter);
       
   782     QCOMPARE(editingFinishedSpy1.count(), 2);
       
   783     QCOMPARE(editingFinishedSpy2.count(), 1);
       
   784     QTest::keyClick(box, Qt::Key_Return);
       
   785     QCOMPARE(editingFinishedSpy1.count(), 3);
       
   786     QCOMPARE(editingFinishedSpy2.count(), 1);
       
   787     box2->setFocus();
       
   788     QCOMPARE(editingFinishedSpy1.count(), 4);
       
   789     QCOMPARE(editingFinishedSpy2.count(), 1);
       
   790     QTest::keyClick(box2, Qt::Key_Enter);
       
   791     QCOMPARE(editingFinishedSpy1.count(), 4);
       
   792     QCOMPARE(editingFinishedSpy2.count(), 2);
       
   793     QTest::keyClick(box2, Qt::Key_Return);
       
   794     QCOMPARE(editingFinishedSpy1.count(), 4);
       
   795     QCOMPARE(editingFinishedSpy2.count(), 3);
       
   796 
       
   797     testFocusWidget->hide();
       
   798     QCOMPARE(editingFinishedSpy1.count(), 4);
       
   799     QCOMPARE(editingFinishedSpy2.count(), 4);
       
   800     QTest::qWait(100);
       
   801 
       
   802     //task203285
       
   803     editingFinishedSpy1.clear();
       
   804     testFocusWidget->show();
       
   805     QTest::qWait(100);
       
   806     box->setKeyboardTracking(false);
       
   807     qApp->setActiveWindow(testFocusWidget);
       
   808     testFocusWidget->activateWindow();
       
   809     box->setFocus();
       
   810     QTRY_VERIFY(box->hasFocus());
       
   811     box->setValue(0);
       
   812     QTest::keyClick(box, '2');
       
   813     QCOMPARE(box->text(), QLatin1String("20"));
       
   814     box2->setFocus();
       
   815     QTRY_VERIFY(qApp->focusWidget() != box);
       
   816     QCOMPARE(box->text(), QLatin1String("20"));
       
   817     QCOMPARE(editingFinishedSpy1.count(), 1);
       
   818 
       
   819     testFocusWidget->hide();
       
   820 }
       
   821 
       
   822 void tst_QSpinBox::removeAll()
       
   823 {
       
   824     SpinBox spin(0);
       
   825     spin.setPrefix("foo");
       
   826     spin.setSuffix("bar");
       
   827     spin.setValue(2);
       
   828     spin.show();
       
   829 #ifdef Q_WS_MAC
       
   830     QTest::keyClick(&spin, Qt::Key_Left, Qt::ControlModifier);
       
   831 #else
       
   832     QTest::keyClick(&spin, Qt::Key_Home);
       
   833 #endif
       
   834 
       
   835 #ifdef Q_WS_MAC
       
   836     QTest::keyClick(&spin, Qt::Key_Right, Qt::ControlModifier|Qt::ShiftModifier);
       
   837 #else
       
   838     QTest::keyClick(&spin, Qt::Key_End, Qt::ShiftModifier);
       
   839 #endif
       
   840 
       
   841     QCOMPARE(spin.lineEdit()->selectedText(), QString("foo2bar"));
       
   842     QTest::keyClick(&spin, Qt::Key_1);
       
   843     QCOMPARE(spin.text(), QString("foo1bar"));
       
   844 }
       
   845 
       
   846 void tst_QSpinBox::startWithDash()
       
   847 {
       
   848     SpinBox spin(0);
       
   849     spin.show();
       
   850 #ifdef Q_WS_MAC
       
   851     QTest::keyClick(&spin, Qt::Key_Left, Qt::ControlModifier);
       
   852 #else
       
   853     QTest::keyClick(&spin, Qt::Key_Home);
       
   854 #endif
       
   855     QCOMPARE(spin.text(), QString("0"));
       
   856     QTest::keyClick(&spin, Qt::Key_Minus);
       
   857     QCOMPARE(spin.text(), QString("0"));
       
   858 }
       
   859 
       
   860 void tst_QSpinBox::undoRedo()
       
   861 {
       
   862     //test undo/redo feature (in conjunction with the "undoRedoEnabled" property)
       
   863     SpinBox spin(0);
       
   864     spin.show();
       
   865 
       
   866     //the undo/redo is disabled by default
       
   867 
       
   868     QCOMPARE(spin.value(), 0); //this is the default value
       
   869     QVERIFY(!spin.lineEdit()->isUndoAvailable());
       
   870     QVERIFY(!spin.lineEdit()->isRedoAvailable());
       
   871 
       
   872     spin.lineEdit()->selectAll(); //ensures everything is selected and will be cleared by typing a key
       
   873     QTest::keyClick(&spin, Qt::Key_1); //we put 1 into the spinbox
       
   874     QCOMPARE(spin.value(), 1);
       
   875     QVERIFY(spin.lineEdit()->isUndoAvailable());
       
   876 
       
   877     //testing CTRL+Z (undo)
       
   878     int val = QKeySequence(QKeySequence::Undo)[0];
       
   879     Qt::KeyboardModifiers mods = (Qt::KeyboardModifiers)(val & Qt::KeyboardModifierMask);
       
   880     QTest::keyClick(&spin, val & ~mods, mods);
       
   881 
       
   882     QCOMPARE(spin.value(), 0);
       
   883     QVERIFY(!spin.lineEdit()->isUndoAvailable());
       
   884     QVERIFY(spin.lineEdit()->isRedoAvailable());
       
   885 
       
   886     //testing CTRL+Y (redo)
       
   887     val = QKeySequence(QKeySequence::Redo)[0];
       
   888     mods = (Qt::KeyboardModifiers)(val & Qt::KeyboardModifierMask);
       
   889     QTest::keyClick(&spin, val & ~mods, mods);
       
   890     QCOMPARE(spin.value(), 1);
       
   891     QVERIFY(!spin.lineEdit()->isRedoAvailable());
       
   892     QVERIFY(spin.lineEdit()->isUndoAvailable());
       
   893 
       
   894     spin.setValue(55);
       
   895     QVERIFY(!spin.lineEdit()->isUndoAvailable());
       
   896     QVERIFY(!spin.lineEdit()->isRedoAvailable());
       
   897 
       
   898     QTest::keyClick(&spin, Qt::Key_Return);
       
   899     QTest::keyClick(&spin, '1');
       
   900     QVERIFY(spin.lineEdit()->isUndoAvailable());
       
   901     QVERIFY(!spin.lineEdit()->isRedoAvailable());
       
   902     spin.lineEdit()->undo();
       
   903     QCOMPARE(spin.value(), 55);
       
   904     QVERIFY(!spin.lineEdit()->isUndoAvailable());
       
   905     QVERIFY(spin.lineEdit()->isRedoAvailable());
       
   906     spin.lineEdit()->redo();
       
   907     QCOMPARE(spin.value(), 1);
       
   908     QVERIFY(spin.lineEdit()->isUndoAvailable());
       
   909     QVERIFY(!spin.lineEdit()->isRedoAvailable());
       
   910 }
       
   911 
       
   912 void tst_QSpinBox::specialValue()
       
   913 {
       
   914     QString specialText="foo";
       
   915 
       
   916     QWidget topWidget;
       
   917     QVBoxLayout layout(&topWidget);
       
   918     SpinBox spin(&topWidget);
       
   919     layout.addWidget(&spin);
       
   920     SpinBox box2(&topWidget);
       
   921     layout.addWidget(&box2);
       
   922 
       
   923     spin.setSpecialValueText(specialText);
       
   924     spin.setMinimum(0);
       
   925     spin.setMaximum(100);
       
   926     spin.setValue(50);
       
   927     topWidget.show();
       
   928 #ifdef Q_WS_X11
       
   929     qt_x11_wait_for_window_manager(&topWidget);
       
   930 #endif
       
   931     QTest::qWait(100);
       
   932     //make sure we have the focus (even if editingFinished fails)
       
   933     qApp->setActiveWindow(&topWidget);
       
   934     topWidget.activateWindow();
       
   935     spin.setFocus();
       
   936 
       
   937     QTest::keyClick(&spin, Qt::Key_Return);
       
   938     QTest::keyClick(&spin, '0');
       
   939     QCOMPARE(spin.text(), QString("0"));
       
   940     QTest::keyClick(&spin, Qt::Key_Return);
       
   941     QCOMPARE(spin.text(), specialText);
       
   942 
       
   943     spin.setValue(50);
       
   944     QTest::keyClick(&spin, Qt::Key_Return);
       
   945     QTest::keyClick(&spin, '0');
       
   946     QCOMPARE(spin.text(), QString("0"));
       
   947     QTest::keyClick(spin.lineEdit(), Qt::Key_Tab);
       
   948     QCOMPARE(spin.text(), specialText);
       
   949 
       
   950     spin.setValue(50);
       
   951     spin.setFocus();
       
   952     QTest::keyClick(&spin, Qt::Key_Return);
       
   953     QTest::keyClick(&spin, '0');
       
   954     QCOMPARE(spin.text(), QString("0"));
       
   955     box2.setFocus();
       
   956     QCOMPARE(spin.text(), specialText);
       
   957 }
       
   958 
       
   959 void tst_QSpinBox::textFromValue()
       
   960 {
       
   961     SpinBox spinBox;
       
   962     QCOMPARE(spinBox.textFromValue(INT_MIN), QString::number(INT_MIN));
       
   963 }
       
   964 
       
   965 class sizeHint_SpinBox : public QSpinBox
       
   966 {
       
   967 public:
       
   968     QSize sizeHint() const
       
   969     {
       
   970         ++sizeHintRequests;
       
   971         return QSpinBox::sizeHint();
       
   972     }
       
   973     mutable int sizeHintRequests;
       
   974 };
       
   975 
       
   976 void tst_QSpinBox::sizeHint()
       
   977 {
       
   978     QWidget *widget = new QWidget;
       
   979     QHBoxLayout *layout = new QHBoxLayout(widget);
       
   980     sizeHint_SpinBox *spinBox = new sizeHint_SpinBox;
       
   981     layout->addWidget(spinBox);
       
   982     widget->show();
       
   983     QTest::qWaitForWindowShown(widget);
       
   984 
       
   985     // Prefix
       
   986     spinBox->sizeHintRequests = 0;
       
   987     spinBox->setPrefix(QLatin1String("abcdefghij"));
       
   988     qApp->processEvents();
       
   989     QTRY_VERIFY(spinBox->sizeHintRequests > 0);
       
   990 
       
   991     // Suffix
       
   992     spinBox->sizeHintRequests = 0;
       
   993     spinBox->setSuffix(QLatin1String("abcdefghij"));
       
   994     qApp->processEvents();
       
   995     QTRY_VERIFY(spinBox->sizeHintRequests > 0);
       
   996 
       
   997     // Range
       
   998     spinBox->sizeHintRequests = 0;
       
   999     spinBox->setRange(0, 1234567890);
       
  1000     spinBox->setValue(spinBox->maximum());
       
  1001     qApp->processEvents();
       
  1002     QTRY_VERIFY(spinBox->sizeHintRequests > 0);
       
  1003 
       
  1004     delete widget;
       
  1005 }
       
  1006 
       
  1007 QTEST_MAIN(tst_QSpinBox)
       
  1008 #include "tst_qspinbox.moc"