tests/auto/qinputdialog/tst_qinputdialog.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 <QString>
       
    45 #include <QSpinBox>
       
    46 #include <QPushButton>
       
    47 #include <QLineEdit>
       
    48 #include <QComboBox>
       
    49 #include <QDialogButtonBox>
       
    50 #include <qinputdialog.h>
       
    51 
       
    52 //TESTED_CLASS=
       
    53 //TESTED_FILES=
       
    54 
       
    55 class tst_QInputDialog : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58     QWidget *parent;
       
    59     QDialog::DialogCode doneCode;
       
    60     void (*testFunc)(QInputDialog *);
       
    61     static void testFuncGetInteger(QInputDialog *dialog);
       
    62     static void testFuncGetDouble(QInputDialog *dialog);
       
    63     static void testFuncGetText(QInputDialog *dialog);
       
    64     static void testFuncGetItem(QInputDialog *dialog);
       
    65     void timerEvent(QTimerEvent *event);
       
    66 private slots:
       
    67     void getInteger_data();
       
    68     void getInteger();
       
    69     void getDouble_data();
       
    70     void getDouble();
       
    71     void task255502getDouble();
       
    72     void getText_data();
       
    73     void getText();
       
    74     void getItem_data();
       
    75     void getItem();
       
    76     void task256299_getTextReturnNullStringOnRejected();
       
    77 };
       
    78 
       
    79 QString stripFraction(const QString &s)
       
    80 {
       
    81     int period;
       
    82     if (s.contains('.'))
       
    83         period = s.indexOf('.');
       
    84     else if (s.contains(','))
       
    85         period = s.indexOf(',');
       
    86     else
       
    87         return s;
       
    88     int end;
       
    89     for (end = s.size() - 1; end > period && s[end] == '0'; --end) ;
       
    90     return s.left(end + (end == period ? 0 : 1));
       
    91 }
       
    92 
       
    93 QString normalizeNumericString(const QString &s)
       
    94 {
       
    95     return stripFraction(s); // assumed to be sufficient
       
    96 }
       
    97 
       
    98 void _keyClick(QWidget *widget, char key)
       
    99 {
       
   100     QTest::keyClick(widget, key);
       
   101 }
       
   102 
       
   103 void _keyClick(QWidget *widget, Qt::Key key)
       
   104 {
       
   105     QTest::keyClick(widget, key);
       
   106 }
       
   107 
       
   108 template <typename SpinBoxType>
       
   109 void testTypingValue(
       
   110     SpinBoxType* sbox, QPushButton *okButton, const QString &value)
       
   111 {
       
   112     sbox->selectAll();
       
   113     for (int i = 0; i < value.size(); ++i) {
       
   114         const QChar valChar = value[i];
       
   115 	_keyClick(static_cast<QWidget *>(sbox), valChar.toAscii()); // ### always guaranteed to work?
       
   116         if (sbox->hasAcceptableInput())
       
   117             QVERIFY(okButton->isEnabled());
       
   118         else
       
   119             QVERIFY(!okButton->isEnabled());
       
   120     }
       
   121 }
       
   122 
       
   123 void testTypingValue(QLineEdit *ledit, QPushButton *okButton, const QString &value)
       
   124 {
       
   125     ledit->selectAll();
       
   126     for (int i = 0; i < value.size(); ++i) {
       
   127         const QChar valChar = value[i];
       
   128 	_keyClick(ledit, valChar.toAscii()); // ### always guaranteed to work?
       
   129         QVERIFY(ledit->hasAcceptableInput());
       
   130         QVERIFY(okButton->isEnabled());
       
   131     }
       
   132 }
       
   133 
       
   134 template <typename SpinBoxType, typename ValueType>
       
   135 void testInvalidateAndRestore(
       
   136     SpinBoxType* sbox, QPushButton *okButton, QLineEdit *ledit, ValueType * = 0)
       
   137 {
       
   138     const ValueType lastValidValue = sbox->value();
       
   139 
       
   140     sbox->selectAll();
       
   141     _keyClick(ledit, Qt::Key_Delete);
       
   142     QVERIFY(!sbox->hasAcceptableInput());
       
   143     QVERIFY(!okButton->isEnabled());
       
   144 
       
   145     _keyClick(ledit, Qt::Key_Return); // should work with Qt::Key_Enter too
       
   146     QVERIFY(sbox->hasAcceptableInput());
       
   147     QVERIFY(okButton->isEnabled());
       
   148     QCOMPARE(sbox->value(), lastValidValue);
       
   149     QCOMPARE(
       
   150         normalizeNumericString(ledit->text()),
       
   151         normalizeNumericString(QString("%1").arg(sbox->value())));
       
   152 }
       
   153 
       
   154 template <typename SpinBoxType, typename ValueType>
       
   155 void testGetNumeric(QInputDialog *dialog, SpinBoxType * = 0, ValueType * = 0)
       
   156 {
       
   157     SpinBoxType *sbox = qFindChild<SpinBoxType *>(dialog);
       
   158     QVERIFY(sbox != 0);
       
   159 
       
   160     QLineEdit *ledit = qFindChild<QLineEdit *>(static_cast<QObject *>(sbox));
       
   161     QVERIFY(ledit != 0);
       
   162 
       
   163     QDialogButtonBox *bbox = qFindChild<QDialogButtonBox *>(dialog);
       
   164     QVERIFY(bbox != 0);
       
   165     QPushButton *okButton = bbox->button(QDialogButtonBox::Ok);
       
   166     QVERIFY(okButton != 0);
       
   167 
       
   168     QVERIFY(sbox->value() >= sbox->minimum());
       
   169     QVERIFY(sbox->value() <= sbox->maximum());
       
   170     QVERIFY(sbox->hasAcceptableInput());
       
   171     QCOMPARE(
       
   172         normalizeNumericString(ledit->selectedText()),
       
   173         normalizeNumericString(QString("%1").arg(sbox->value())));
       
   174     QVERIFY(okButton->isEnabled());
       
   175 
       
   176     const ValueType origValue = sbox->value();
       
   177 
       
   178     testInvalidateAndRestore<SpinBoxType, ValueType>(sbox, okButton, ledit);
       
   179     testTypingValue<SpinBoxType>(sbox, okButton, QString("%1").arg(sbox->minimum()));
       
   180     testTypingValue<SpinBoxType>(sbox, okButton, QString("%1").arg(sbox->maximum()));
       
   181     testTypingValue<SpinBoxType>(sbox, okButton, QString("%1").arg(sbox->minimum() - 1));
       
   182     testTypingValue<SpinBoxType>(sbox, okButton, QString("%1").arg(sbox->maximum() + 1));
       
   183     testTypingValue<SpinBoxType>(sbox, okButton, "0");
       
   184     testTypingValue<SpinBoxType>(sbox, okButton, "0.0");
       
   185     testTypingValue<SpinBoxType>(sbox, okButton, "foobar");
       
   186 
       
   187     testTypingValue<SpinBoxType>(sbox, okButton, QString("%1").arg(origValue));
       
   188 }
       
   189 
       
   190 void testGetText(QInputDialog *dialog)
       
   191 {
       
   192     QLineEdit *ledit = qFindChild<QLineEdit *>(dialog);
       
   193     Q_ASSERT(ledit);
       
   194 
       
   195     QDialogButtonBox *bbox = qFindChild<QDialogButtonBox *>(dialog);
       
   196     Q_ASSERT(bbox);
       
   197     QPushButton *okButton = bbox->button(QDialogButtonBox::Ok);
       
   198     Q_ASSERT(okButton);
       
   199 
       
   200     QVERIFY(ledit->hasAcceptableInput());
       
   201     QCOMPARE(ledit->selectedText(), ledit->text());
       
   202     QVERIFY(okButton->isEnabled());
       
   203     const QString origValue = ledit->text();
       
   204 
       
   205     testTypingValue(ledit, okButton, origValue);
       
   206 }
       
   207 
       
   208 void testGetItem(QInputDialog *dialog)
       
   209 {
       
   210     QComboBox *cbox = qFindChild<QComboBox *>(dialog);
       
   211     Q_ASSERT(cbox);
       
   212 
       
   213     QDialogButtonBox *bbox = qFindChild<QDialogButtonBox *>(dialog);
       
   214     Q_ASSERT(bbox);
       
   215     QPushButton *okButton = bbox->button(QDialogButtonBox::Ok);
       
   216     Q_ASSERT(okButton);
       
   217 
       
   218     QVERIFY(okButton->isEnabled());
       
   219     const int origIndex = cbox->currentIndex();
       
   220     cbox->setCurrentIndex(origIndex - 1);
       
   221     cbox->setCurrentIndex(origIndex);
       
   222     QVERIFY(okButton->isEnabled());
       
   223 }
       
   224 
       
   225 void tst_QInputDialog::testFuncGetInteger(QInputDialog *dialog)
       
   226 {
       
   227     testGetNumeric<QSpinBox, int>(dialog);
       
   228 }
       
   229 
       
   230 void tst_QInputDialog::testFuncGetDouble(QInputDialog *dialog)
       
   231 {
       
   232     testGetNumeric<QDoubleSpinBox, double>(dialog);
       
   233 }
       
   234 
       
   235 void tst_QInputDialog::testFuncGetText(QInputDialog *dialog)
       
   236 {
       
   237     ::testGetText(dialog);
       
   238 }
       
   239 
       
   240 void tst_QInputDialog::testFuncGetItem(QInputDialog *dialog)
       
   241 {
       
   242     ::testGetItem(dialog);
       
   243 }
       
   244 
       
   245 void tst_QInputDialog::timerEvent(QTimerEvent *event)
       
   246 {
       
   247     killTimer(event->timerId());
       
   248     QInputDialog *dialog = qFindChild<QInputDialog *>(parent);
       
   249     Q_ASSERT(dialog);
       
   250     if (testFunc)
       
   251         testFunc(dialog);
       
   252     dialog->done(doneCode); // cause static function call to return
       
   253 }
       
   254 
       
   255 void tst_QInputDialog::getInteger_data()
       
   256 {
       
   257     QTest::addColumn<int>("min");
       
   258     QTest::addColumn<int>("max");
       
   259     QTest::newRow("getInteger() - -") << -20 << -10;
       
   260     QTest::newRow("getInteger() - 0") << -20 <<   0;
       
   261     QTest::newRow("getInteger() - +") << -20 <<  20;
       
   262     QTest::newRow("getInteger() 0 +") <<   0 <<  20;
       
   263     QTest::newRow("getInteger() + +") <<  10 <<  20;
       
   264 }
       
   265 
       
   266 void tst_QInputDialog::getInteger()
       
   267 {
       
   268     QFETCH(int, min);
       
   269     QFETCH(int, max);
       
   270     Q_ASSERT(min < max);
       
   271     parent = new QWidget;
       
   272     doneCode = QDialog::Accepted;
       
   273     testFunc = &tst_QInputDialog::testFuncGetInteger;
       
   274     startTimer(0);
       
   275     bool ok = false;
       
   276     const int value = min + (max - min) / 2;
       
   277     const int result = QInputDialog::getInteger(parent, "", "", value, min, max, 1, &ok);
       
   278     QVERIFY(ok);
       
   279     QCOMPARE(result, value);
       
   280     delete parent;
       
   281 }
       
   282 
       
   283 void tst_QInputDialog::getDouble_data()
       
   284 {
       
   285     QTest::addColumn<double>("min");
       
   286     QTest::addColumn<double>("max");
       
   287     QTest::addColumn<int>("decimals");
       
   288     QTest::newRow("getDouble() - - d0") << -20.0  << -10.0  << 0;
       
   289     QTest::newRow("getDouble() - 0 d0") << -20.0  <<   0.0  << 0;
       
   290     QTest::newRow("getDouble() - + d0") << -20.0  <<  20.0  << 0;
       
   291     QTest::newRow("getDouble() 0 + d0") <<   0.0  <<  20.0  << 0;
       
   292     QTest::newRow("getDouble() + + d0") <<  10.0  <<  20.0  << 0;
       
   293     QTest::newRow("getDouble() - - d1") << -20.5  << -10.5  << 1;
       
   294     QTest::newRow("getDouble() - 0 d1") << -20.5  <<   0.0  << 1;
       
   295     QTest::newRow("getDouble() - + d1") << -20.5  <<  20.5  << 1;
       
   296     QTest::newRow("getDouble() 0 + d1") <<   0.0  <<  20.5  << 1;
       
   297     QTest::newRow("getDouble() + + d1") <<  10.5  <<  20.5  << 1;
       
   298     QTest::newRow("getDouble() - - d2") << -20.05 << -10.05 << 2;
       
   299     QTest::newRow("getDouble() - 0 d2") << -20.05 <<   0.0  << 2;
       
   300     QTest::newRow("getDouble() - + d2") << -20.05 <<  20.05 << 2;
       
   301     QTest::newRow("getDouble() 0 + d2") <<   0.0  <<  20.05 << 2;
       
   302     QTest::newRow("getDouble() + + d2") <<  10.05 <<  20.05 << 2;
       
   303 }
       
   304 
       
   305 void tst_QInputDialog::getDouble()
       
   306 {
       
   307     QFETCH(double, min);
       
   308     QFETCH(double, max);
       
   309     QFETCH(int, decimals);
       
   310     Q_ASSERT(min < max && decimals >= 0 && decimals <= 13);
       
   311     parent = new QWidget;
       
   312     doneCode = QDialog::Accepted;
       
   313     testFunc = &tst_QInputDialog::testFuncGetDouble;
       
   314     startTimer(0);
       
   315     bool ok = false;
       
   316     // avoid decimals due to inconsistent roundoff behavior in QInputDialog::getDouble()
       
   317     // (at one decimal, 10.25 is rounded off to 10.2, while at two decimals, 10.025 is
       
   318     // rounded off to 10.03)
       
   319     const double value = static_cast<int>(min + (max - min) / 2);
       
   320     const double result =
       
   321         QInputDialog::getDouble(parent, "", "", value, min, max, decimals, &ok);
       
   322     QVERIFY(ok);
       
   323     QCOMPARE(result, value);
       
   324     delete parent;
       
   325 }
       
   326 
       
   327 void tst_QInputDialog::task255502getDouble()
       
   328 {
       
   329     parent = new QWidget;
       
   330     doneCode = QDialog::Accepted;
       
   331     testFunc = &tst_QInputDialog::testFuncGetDouble;
       
   332     startTimer(0);
       
   333     bool ok = false;
       
   334     const double value = 0.001;
       
   335     const double result =
       
   336         QInputDialog::getDouble(parent, "", "", value, -1, 1, 4, &ok);
       
   337     QVERIFY(ok);
       
   338     QCOMPARE(result, value);
       
   339     delete parent;
       
   340 }
       
   341 
       
   342 void tst_QInputDialog::getText_data()
       
   343 {
       
   344     QTest::addColumn<QString>("text");
       
   345     QTest::newRow("getText() 1") << "";
       
   346     QTest::newRow("getText() 2") << "foobar";
       
   347     QTest::newRow("getText() 3") << "  foobar";
       
   348     QTest::newRow("getText() 4") << "foobar  ";
       
   349     QTest::newRow("getText() 5") << "aAzZ`1234567890-=~!@#$%^&*()_+[]{}\\|;:'\",.<>/?";
       
   350 }
       
   351 
       
   352 void tst_QInputDialog::getText()
       
   353 {
       
   354     QFETCH(QString, text);
       
   355     parent = new QWidget;
       
   356     doneCode = QDialog::Accepted;
       
   357     testFunc = &tst_QInputDialog::testFuncGetText;
       
   358     startTimer(0);
       
   359     bool ok = false;
       
   360     const QString result = QInputDialog::getText(parent, "", "", QLineEdit::Normal, text, &ok);
       
   361     QVERIFY(ok);
       
   362     QCOMPARE(result, text);
       
   363     delete parent;
       
   364 }
       
   365 
       
   366 void tst_QInputDialog::task256299_getTextReturnNullStringOnRejected()
       
   367 {
       
   368     parent = new QWidget;
       
   369     doneCode = QDialog::Rejected;
       
   370     testFunc = 0;
       
   371     startTimer(0);
       
   372     bool ok = true;
       
   373     const QString result = QInputDialog::getText(parent, "", "", QLineEdit::Normal, "foobar", &ok);
       
   374     QVERIFY(!ok);
       
   375     QVERIFY(result.isNull());
       
   376     delete parent;
       
   377 }
       
   378 
       
   379 void tst_QInputDialog::getItem_data()
       
   380 {
       
   381     QTest::addColumn<QStringList>("items");
       
   382     QTest::addColumn<bool>("editable");
       
   383     QTest::newRow("getItem() 1 true") << (QStringList() << "") << true;
       
   384     QTest::newRow("getItem() 2 true") <<
       
   385         (QStringList() << "spring" << "summer" << "fall" << "winter") << true;
       
   386     QTest::newRow("getItem() 1 false") << (QStringList() << "") << false;
       
   387     QTest::newRow("getItem() 2 false") <<
       
   388         (QStringList() << "spring" << "summer" << "fall" << "winter") << false;
       
   389 }
       
   390 
       
   391 void tst_QInputDialog::getItem()
       
   392 {
       
   393     QFETCH(QStringList, items);
       
   394     QFETCH(bool, editable);
       
   395     parent = new QWidget;
       
   396     doneCode = QDialog::Accepted;
       
   397     testFunc = &tst_QInputDialog::testFuncGetItem;
       
   398     startTimer(0);
       
   399     bool ok = false;
       
   400     const int index = items.size() / 2;
       
   401     const QString result = QInputDialog::getItem(parent, "", "", items, index, editable, &ok);
       
   402     QVERIFY(ok);
       
   403     QCOMPARE(result, items[index]);
       
   404     delete parent;
       
   405 }
       
   406 
       
   407 QTEST_MAIN(tst_QInputDialog)
       
   408 #include "tst_qinputdialog.moc"