diff options
Diffstat (limited to 'tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp')
-rw-r--r-- | tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp | 135 |
1 files changed, 96 insertions, 39 deletions
diff --git a/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp b/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp index 32003f2124..b77a9c0eec 100644 --- a/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp +++ b/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.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 <QTimer> +#include <QSignalSpy> #include <qcoreapplication.h> #include <qdebug.h> @@ -38,6 +15,8 @@ #include <qscreen.h> #include <qlabel.h> +#include <QtWidgets/private/qapplication_p.h> + class tst_QToolButton : public QObject { Q_OBJECT @@ -54,6 +33,8 @@ private slots: void task176137_autoRepeatOfAction(); void qtbug_26956_popupTimerDone(); void qtbug_34759_sizeHintResetWhenSettingMenu(); + void defaultActionSynced(); + void deleteInHandler(); protected slots: void sendMouseClick(); @@ -140,11 +121,11 @@ void tst_QToolButton::triggered() toolButton->setDefaultAction(defaultAction); mainWidget.show(); - QApplication::setActiveWindow(&mainWidget); + QApplicationPrivate::setActiveWindow(&mainWidget); QVERIFY(QTest::qWaitForWindowActive(&mainWidget)); defaultAction->trigger(); - QCOMPARE(spy.count(),1); + QCOMPARE(spy.size(),1); QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), defaultAction); m_menu = menu.data(); @@ -155,7 +136,7 @@ void tst_QToolButton::triggered() timer->start(); QTimer::singleShot(10000, &mainWidget, SLOT(close())); // Emergency bail-out toolButton->showMenu(); - QTRY_COMPARE(spy.count(),2); + QTRY_COMPARE(spy.size(),2); QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one); } @@ -201,26 +182,27 @@ void tst_QToolButton::task176137_autoRepeatOfAction() label->move(0, 50); mainWidget.show(); - QApplication::setActiveWindow(&mainWidget); QVERIFY(QTest::qWaitForWindowActive(&mainWidget)); QSignalSpy spy(&action,SIGNAL(triggered())); QTest::mousePress (toolButton, Qt::LeftButton); - QTest::mouseRelease (toolButton, Qt::LeftButton, {}, QPoint (), 2000); - QCOMPARE(spy.count(),1); + QTest::qWait(2000); + QTest::mouseRelease (toolButton, Qt::LeftButton, {}, {}); + QCOMPARE(spy.size(),1); // try again with auto repeat toolButton->setAutoRepeat (true); QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy QTest::mousePress (toolButton, Qt::LeftButton); - QTest::mouseRelease (toolButton, Qt::LeftButton, {}, QPoint (), 3000); + QTest::qWait(3000); + QTest::mouseRelease (toolButton, Qt::LeftButton, {}, {}); const qreal expected = (3000 - toolButton->autoRepeatDelay()) / toolButton->autoRepeatInterval() + 1; //we check that the difference is small (on some systems timers are not super accurate) - qreal diff = (expected - repeatSpy.count()) / expected; + qreal diff = (expected - repeatSpy.size()) / expected; QVERIFY2(qAbs(diff) < 0.2, qPrintable( QString("expected: %1, actual: %2, diff (fraction): %3") .arg(expected) - .arg(repeatSpy.count()) + .arg(repeatSpy.size()) .arg(diff))); } @@ -276,5 +258,80 @@ void tst_QToolButton::qtbug_34759_sizeHintResetWhenSettingMenu() QTRY_COMPARE(button1.sizeHint(), button2.sizeHint()); } +void tst_QToolButton::defaultActionSynced() +{ + QAction a; + a.setCheckable(true); + + QToolButton tb; + tb.setDefaultAction(&a); + QVERIFY(tb.isCheckable()); + + QSignalSpy tbSpy(&tb, SIGNAL(toggled(bool))); + QSignalSpy aSpy(&a, SIGNAL(toggled(bool))); + + int tbToggledCount = 0; + int aToggledCount = 0; + + tb.setChecked(true); + QVERIFY(a.isChecked()); + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), ++aToggledCount); + tb.setChecked(false); + QVERIFY(!a.isChecked()); + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), ++aToggledCount); + + a.setChecked(true); + QVERIFY(tb.isChecked()); + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), ++aToggledCount); + a.setChecked(false); + QVERIFY(!tb.isChecked()); + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), ++aToggledCount); + + QAction b; + QSignalSpy bSpy(&b, SIGNAL(toggled(bool))); + int bToggledCount = 0; + tb.setDefaultAction(&b); + QVERIFY(!tb.isCheckable()); + b.setCheckable(true); + QVERIFY(tb.isCheckable()); + + tb.setChecked(true); + QVERIFY(!a.isChecked()); + QVERIFY(b.isChecked()); + + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), aToggledCount); + QCOMPARE(bSpy.size(), ++bToggledCount); + + tb.click(); + QVERIFY(!a.isChecked()); + QVERIFY(!tb.isChecked()); + QVERIFY(!b.isChecked()); + QCOMPARE(tbSpy.size(), ++tbToggledCount); + QCOMPARE(aSpy.size(), aToggledCount); + QCOMPARE(bSpy.size(), ++bToggledCount); +} + +void tst_QToolButton::deleteInHandler() +{ + // Tests that if something deletes the button + // while its event handler is still on the callstack, we don't crash + + QPointer<QToolButton> tb = new QToolButton(); + tb->show(); + QVERIFY(QTest::qWaitForWindowActive(tb)); + + connect(tb, &QToolButton::clicked, this, [tb] { + delete tb; + }); + + QTest::mouseClick(tb, Qt::LeftButton); + QVERIFY(!tb); +} + QTEST_MAIN(tst_QToolButton) #include "tst_qtoolbutton.moc" |