diff options
Diffstat (limited to 'tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp')
-rw-r--r-- | tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp | 191 |
1 files changed, 82 insertions, 109 deletions
diff --git a/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp b/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp index cec2da1d57..999b5cac07 100644 --- a/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp +++ b/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp @@ -1,33 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> +#include <QSignalSpy> + #include <qapplication.h> #include <limits.h> @@ -45,32 +22,34 @@ #include <QStyle> #include <QProxyStyle> +#include <QtWidgets/private/qapplication_p.h> + class DoubleSpinBox : public QDoubleSpinBox { Q_OBJECT public: - DoubleSpinBox(QWidget *parent = 0) + DoubleSpinBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent) { /*connect(this, SIGNAL(valueChanged(double)), this, SLOT(foo(double)));*/ } - QString textFromValue(double v) const + QString textFromValue(double v) const override { return QDoubleSpinBox::textFromValue(v); } - QValidator::State validate(QString &text, int &pos) const + QValidator::State validate(QString &text, int &pos) const override { return QDoubleSpinBox::validate(text, pos); } - double valueFromText(const QString &text) const + double valueFromText(const QString &text) const override { return QDoubleSpinBox::valueFromText(text); } #if QT_CONFIG(wheelevent) - void wheelEvent(QWheelEvent *event) + void wheelEvent(QWheelEvent *event) override { QDoubleSpinBox::wheelEvent(event); } #endif - void initStyleOption(QStyleOptionSpinBox *option) const + void initStyleOption(QStyleOptionSpinBox *option) const override { QDoubleSpinBox::initStyleOption(option); } @@ -132,8 +111,8 @@ public: virtual ~tst_QDoubleSpinBox(); public slots: void initTestCase(); - void cleanupTestCase(); void init(); + void cleanup(); private slots: void germanTest(); @@ -211,7 +190,6 @@ public slots: private: QStringList actualTexts; QList<double> actualValues; - QWidget *testFocusWidget; }; typedef QList<double> DoubleList; @@ -255,25 +233,18 @@ tst_QDoubleSpinBox::~tst_QDoubleSpinBox() void tst_QDoubleSpinBox::initTestCase() { - testFocusWidget = new QWidget(0); - testFocusWidget->resize(200, 100); - testFocusWidget->show(); - if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) QSKIP("Wayland: This fails. Figure out why."); - - QVERIFY(QTest::qWaitForWindowActive(testFocusWidget)); } -void tst_QDoubleSpinBox::cleanupTestCase() +void tst_QDoubleSpinBox::init() { - delete testFocusWidget; - testFocusWidget = 0; + QLocale::setDefault(QLocale(QLocale::C)); } -void tst_QDoubleSpinBox::init() +void tst_QDoubleSpinBox::cleanup() { - QLocale::setDefault(QLocale(QLocale::C)); + QTRY_VERIFY(QApplication::topLevelWidgets().isEmpty()); } void tst_QDoubleSpinBox::setValue_data() @@ -779,17 +750,17 @@ void tst_QDoubleSpinBox::valueFromTextAndValidate_data() QTest::newRow("data1") << QString() << Intermediate << 0.0 << 100.0 << (int)QLocale::C << QString(); QTest::newRow("data2") << QString("asd") << Invalid << 0.0 << 100.0 << (int)QLocale::C << QString(); QTest::newRow("data3") << QString("2.2") << Acceptable << 0.0 << 100.0 << (int)QLocale::C << QString(); - QTest::newRow("data4") << QString(" ") << Intermediate << 0.0 << 100.0 << (int)QLocale::Norwegian << QString(); + QTest::newRow("data4") << QString(" ") << Intermediate << 0.0 << 100.0 << (int)QLocale::NorwegianBokmal << QString(); QTest::newRow("data5") << QString(" ") << Intermediate << 0.0 << 100.0 << (int)QLocale::C << QString(); - QTest::newRow("data6") << QString(",") << Intermediate << 0.0 << 100.0 << (int)QLocale::Norwegian << QString(); + QTest::newRow("data6") << QString(",") << Intermediate << 0.0 << 100.0 << (int)QLocale::NorwegianBokmal << QString(); QTest::newRow("data7") << QString(",") << Invalid << 0.0 << 100.0 << (int)QLocale::C << QString(); - QTest::newRow("data8") << QString("1 ") << Acceptable << 0.0 << 1000.0 << (int)QLocale::Norwegian << QString("1"); + QTest::newRow("data8") << QString("1 ") << Acceptable << 0.0 << 1000.0 << (int)QLocale::NorwegianBokmal << QString("1"); QTest::newRow("data9") << QString("1 ") << Acceptable << 0.0 << 100.0 << (int)QLocale::C << QString("1"); - QTest::newRow("data10") << QString(" 1") << Acceptable << 0.0 << 100.0 << (int)QLocale::Norwegian << QString("1"); + QTest::newRow("data10") << QString(" 1") << Acceptable << 0.0 << 100.0 << (int)QLocale::NorwegianBokmal << QString("1"); QTest::newRow("data11") << QString(" 1") << Acceptable << 0.0 << 100.0 << (int)QLocale::C << QString("1"); - QTest::newRow("data12") << QString("1,") << Acceptable << 0.0 << 100.0 << (int)QLocale::Norwegian << QString(); + QTest::newRow("data12") << QString("1,") << Acceptable << 0.0 << 100.0 << (int)QLocale::NorwegianBokmal << QString(); QTest::newRow("data13") << QString("1,") << Acceptable << 0.0 << 1000.0 << (int)QLocale::C << QString(); - QTest::newRow("data14") << QString("1, ") << Acceptable << 0.0 << 100.0 << (int)QLocale::Norwegian << QString("1,"); + QTest::newRow("data14") << QString("1, ") << Acceptable << 0.0 << 100.0 << (int)QLocale::NorwegianBokmal << QString("1,"); QTest::newRow("data15") << QString("1, ") << Invalid << 0.0 << 100.0 << (int)QLocale::C << QString(); QTest::newRow("data16") << QString("2") << Intermediate << 100.0 << 102.0 << (int)QLocale::C << QString(); QTest::newRow("data17") << QString("22.0") << Intermediate << 100.0 << 102.0 << (int)QLocale::C << QString(); @@ -828,7 +799,7 @@ void tst_QDoubleSpinBox::valueFromTextAndValidate_data() QTest::newRow("data50") << QString("2.2") << Acceptable << 0.0 << 1000.0 << (int)QLocale::C << QString(); QTest::newRow("data51") << QString("2.2,00") << Invalid << 0.0 << 1000.0 << (int)QLocale::C << QString(); QTest::newRow("data52") << QString("2..2,00") << Invalid << 0.0 << 1000.0 << (int)QLocale::German << QString(); - QTest::newRow("data53") << QString("2.2") << Invalid << 0.0 << 1000.0 << (int)QLocale::Norwegian << QString(); + QTest::newRow("data53") << QString("2.2") << Invalid << 0.0 << 1000.0 << (int)QLocale::NorwegianBokmal << QString(); QTest::newRow("data54") << QString(" 2.2") << Acceptable << 0.0 << 1000.0 << (int)QLocale::C << QString(); QTest::newRow("data55") << QString("2.2 ") << Acceptable << 0.0 << 1000.0 << (int)QLocale::C << QString("2.2"); QTest::newRow("data56") << QString(" 2.2 ") << Acceptable << 0.0 << 1000.0 << (int)QLocale::C << QString("2.2"); @@ -876,17 +847,25 @@ void tst_QDoubleSpinBox::setReadOnly() void tst_QDoubleSpinBox::editingFinished() { - QVBoxLayout *layout = new QVBoxLayout(testFocusWidget); - QDoubleSpinBox *box = new QDoubleSpinBox(testFocusWidget); + QWidget testFocusWidget(nullptr); + QVBoxLayout *layout = new QVBoxLayout(&testFocusWidget); + QDoubleSpinBox *box = new QDoubleSpinBox(&testFocusWidget); layout->addWidget(box); - QDoubleSpinBox *box2 = new QDoubleSpinBox(testFocusWidget); + QDoubleSpinBox *box2 = new QDoubleSpinBox(&testFocusWidget); layout->addWidget(box2); - testFocusWidget->show(); - testFocusWidget->activateWindow(); - QVERIFY(QTest::qWaitForWindowActive(testFocusWidget)); + testFocusWidget.show(); + testFocusWidget.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&testFocusWidget)); + + box->show(); + QVERIFY(QTest::qWaitForWindowExposed(box)); box->setFocus(); - QTRY_VERIFY(box->hasFocus()); + + // Box may fail to acquire focus due to a system popup + // it is fair in that case to skip the test + if (!QTest::qWaitForWindowActive(box)) + QSKIP("Focus acquisition failed."); QSignalSpy editingFinishedSpy1(box, SIGNAL(editingFinished())); QSignalSpy editingFinishedSpy2(box2, SIGNAL(editingFinished())); @@ -895,42 +874,37 @@ void tst_QDoubleSpinBox::editingFinished() QTest::keyClick(box, Qt::Key_Up); - QCOMPARE(editingFinishedSpy1.count(), 0); - QCOMPARE(editingFinishedSpy2.count(), 0); + QCOMPARE(editingFinishedSpy1.size(), 0); + QCOMPARE(editingFinishedSpy2.size(), 0); QTest::keyClick(box2, Qt::Key_Up); QTest::keyClick(box2, Qt::Key_Up); box2->setFocus(); - QCOMPARE(editingFinishedSpy1.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 1); box->setFocus(); - QCOMPARE(editingFinishedSpy1.count(), 1); - QCOMPARE(editingFinishedSpy2.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 1); + QCOMPARE(editingFinishedSpy2.size(), 1); QTest::keyClick(box, Qt::Key_Up); - QCOMPARE(editingFinishedSpy1.count(), 1); - QCOMPARE(editingFinishedSpy2.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 1); + QCOMPARE(editingFinishedSpy2.size(), 1); QTest::keyClick(box, Qt::Key_Enter); - QCOMPARE(editingFinishedSpy1.count(), 2); - QCOMPARE(editingFinishedSpy2.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 2); + QCOMPARE(editingFinishedSpy2.size(), 1); QTest::keyClick(box, Qt::Key_Return); - QCOMPARE(editingFinishedSpy1.count(), 3); - QCOMPARE(editingFinishedSpy2.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 3); + QCOMPARE(editingFinishedSpy2.size(), 1); box2->setFocus(); - QCOMPARE(editingFinishedSpy1.count(), 4); - QCOMPARE(editingFinishedSpy2.count(), 1); + QCOMPARE(editingFinishedSpy1.size(), 4); + QCOMPARE(editingFinishedSpy2.size(), 1); QTest::keyClick(box2, Qt::Key_Enter); - QCOMPARE(editingFinishedSpy1.count(), 4); - QCOMPARE(editingFinishedSpy2.count(), 2); + QCOMPARE(editingFinishedSpy1.size(), 4); + QCOMPARE(editingFinishedSpy2.size(), 2); QTest::keyClick(box2, Qt::Key_Return); - QCOMPARE(editingFinishedSpy1.count(), 4); - QCOMPARE(editingFinishedSpy2.count(), 3); - testFocusWidget->hide(); - QCOMPARE(editingFinishedSpy1.count(), 4); - QCOMPARE(editingFinishedSpy2.count(), 4); - - // On some platforms this is our root window - // we need to show it again otherwise subsequent - // tests will fail - testFocusWidget->show(); + QCOMPARE(editingFinishedSpy1.size(), 4); + QCOMPARE(editingFinishedSpy2.size(), 3); + testFocusWidget.hide(); + QCOMPARE(editingFinishedSpy1.size(), 4); + QCOMPARE(editingFinishedSpy2.size(), 4); } void tst_QDoubleSpinBox::removeAll() @@ -1047,7 +1021,7 @@ void tst_QDoubleSpinBox::undoRedo() QVERIFY(spin.lineEdit()->isUndoAvailable()); //testing CTRL+Z (undo) - int val = QKeySequence(QKeySequence::Undo)[0]; + int val = QKeySequence(QKeySequence::Undo)[0].toCombined(); if (val != 0) { Qt::KeyboardModifiers mods = (Qt::KeyboardModifiers)(val & Qt::KeyboardModifierMask); QTest::keyClick(&spin, val & ~mods, mods); @@ -1055,12 +1029,12 @@ void tst_QDoubleSpinBox::undoRedo() QVERIFY(!spin.lineEdit()->isUndoAvailable()); QVERIFY(spin.lineEdit()->isRedoAvailable()); } else { - QWARN("Undo not tested because no key sequence associated to QKeySequence::Redo"); + qWarning("Undo not tested because no key sequence associated to QKeySequence::Redo"); } //testing CTRL+Y (redo) - val = QKeySequence(QKeySequence::Redo)[0]; + val = QKeySequence(QKeySequence::Redo)[0].toCombined(); if (val != 0) { Qt::KeyboardModifiers mods = (Qt::KeyboardModifiers)(val & Qt::KeyboardModifierMask); QTest::keyClick(&spin, val & ~mods, mods); @@ -1068,7 +1042,7 @@ void tst_QDoubleSpinBox::undoRedo() QVERIFY(!spin.lineEdit()->isRedoAvailable()); QVERIFY(spin.lineEdit()->isUndoAvailable()); } else { - QWARN("Redo not tested because no key sequence associated to QKeySequence::Redo"); + qWarning("Redo not tested because no key sequence associated to QKeySequence::Redo"); } @@ -1081,7 +1055,7 @@ void tst_QDoubleSpinBox::undoRedo() struct task199226_DoubleSpinBox : public QDoubleSpinBox { - task199226_DoubleSpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent) {} + task199226_DoubleSpinBox(QWidget *parent = nullptr) : QDoubleSpinBox(parent) {} QLineEdit *lineEdit() { return QAbstractSpinBox::lineEdit(); } }; @@ -1109,15 +1083,15 @@ public: void tst_QDoubleSpinBox::task224497_fltMax() { - task224497_fltMax_DoubleSpinBox *dspin = new task224497_fltMax_DoubleSpinBox; - dspin->setMinimum(3); - dspin->setMaximum(FLT_MAX); - dspin->show(); - QVERIFY(QTest::qWaitForWindowActive(dspin)); - dspin->lineEdit()->selectAll(); - QTest::keyClick(dspin->lineEdit(), Qt::Key_Delete); - QTest::keyClick(dspin->lineEdit(), Qt::Key_1); - QCOMPARE(dspin->cleanText(), QLatin1String("1")); + task224497_fltMax_DoubleSpinBox dspin; + dspin.setMinimum(3); + dspin.setMaximum(FLT_MAX); + dspin.show(); + QVERIFY(QTest::qWaitForWindowActive(&dspin)); + dspin.lineEdit()->selectAll(); + QTest::keyClick(dspin.lineEdit(), Qt::Key_Delete); + QTest::keyClick(dspin.lineEdit(), Qt::Key_1); + QCOMPARE(dspin.cleanText(), QLatin1String("1")); } void tst_QDoubleSpinBox::task221221() @@ -1172,7 +1146,7 @@ void tst_QDoubleSpinBox::taskQTBUG_5008_textFromValueAndValidate() } //we use the French delimiters here - QString textFromValue (double value) const + QString textFromValue (double value) const override { return locale().toString(value); } @@ -1180,7 +1154,6 @@ void tst_QDoubleSpinBox::taskQTBUG_5008_textFromValueAndValidate() spinbox.show(); spinbox.activateWindow(); spinbox.setFocus(); - QApplication::setActiveWindow(&spinbox); QVERIFY(QTest::qWaitForWindowActive(&spinbox)); QCOMPARE(static_cast<QWidget *>(&spinbox), QApplication::activeWindow()); QTRY_VERIFY(spinbox.hasFocus()); @@ -1218,7 +1191,7 @@ void tst_QDoubleSpinBox::taskQTBUG_6496_fiddlingWithPrecision() void tst_QDoubleSpinBox::setGroupSeparatorShown_data() { QTest::addColumn<QLocale::Language>("lang"); - QTest::addColumn<QLocale::Country>("country"); + QTest::addColumn<QLocale::Territory>("country"); QTest::newRow("data0") << QLocale::English << QLocale::UnitedStates; QTest::newRow("data1") << QLocale::Swedish << QLocale::Sweden; @@ -1230,7 +1203,7 @@ void tst_QDoubleSpinBox::setGroupSeparatorShown_data() void tst_QDoubleSpinBox::setGroupSeparatorShown() { QFETCH(QLocale::Language, lang); - QFETCH(QLocale::Country, country); + QFETCH(QLocale::Territory, country); QLocale loc(lang, country); QLocale::setDefault(loc); @@ -1786,12 +1759,12 @@ void tst_QDoubleSpinBox::stepModifierPressAndHold() QStyle::CC_SpinBox, &spinBoxStyleOption, subControl, &spin); QTest::mousePress(&spin, Qt::LeftButton, modifiers, buttonRect.center()); - QTRY_VERIFY(spy.length() >= 3); + QTRY_VERIFY(spy.size() >= 3); QTest::mouseRelease(&spin, Qt::LeftButton, modifiers, buttonRect.center()); const auto value = spy.last().at(0); - QVERIFY(value.type() == QVariant::Double); - QCOMPARE(value.toDouble(), spy.length() * expectedStepModifier); + QVERIFY(value.userType() == QMetaType::Double); + QCOMPARE(value.toDouble(), spy.size() * expectedStepModifier); } QTEST_MAIN(tst_QDoubleSpinBox) |