diff --git a/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp b/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp index aaff322c4b..168a17773e 100644 --- a/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp +++ b/tests/auto/widgets/widgets/qtoolbutton/tst_qtoolbutton.cpp @@ -48,6 +48,8 @@ #include #include #include +#include +#include class tst_QToolButton : public QObject { @@ -68,7 +70,7 @@ private slots: protected slots: void sendMouseClick(); private: - QWidget *w; + QPointer m_menu; }; tst_QToolButton::tst_QToolButton() @@ -121,29 +123,39 @@ void tst_QToolButton::getSetCheck() void tst_QToolButton::triggered() { qRegisterMetaType("QAction *"); - QToolButton tb; - tb.show(); - QSignalSpy spy(&tb,SIGNAL(triggered(QAction*))); - QMenu *menu = new QMenu("Menu"); + QWidget mainWidget; + mainWidget.setWindowTitle(QStringLiteral("triggered")); + mainWidget.resize(200, 200); + mainWidget.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100)); + QToolButton *toolButton = new QToolButton(&mainWidget); + QSignalSpy spy(toolButton,SIGNAL(triggered(QAction*))); + QScopedPointer menu(new QMenu(QStringLiteral("Menu"))); QAction *one = menu->addAction("one"); menu->addAction("two"); - QAction *def = new QAction("def", this); + QAction *defaultAction = new QAction(QStringLiteral("def"), this); - tb.setMenu(menu); - tb.setDefaultAction(def); + toolButton->setMenu(menu.data()); + toolButton->setDefaultAction(defaultAction); + mainWidget.show(); + QApplication::setActiveWindow(&mainWidget); + QVERIFY(QTest::qWaitForWindowActive(&mainWidget)); - def->trigger(); + defaultAction->trigger(); QCOMPARE(spy.count(),1); - QCOMPARE(qvariant_cast(spy.at(0).at(0)), def); + QCOMPARE(qvariant_cast(spy.at(0).at(0)), defaultAction); - w = menu; - QTimer::singleShot(30, this, SLOT(sendMouseClick())); - tb.showMenu(); + m_menu = menu.data(); + + QTimer *timer = new QTimer(this); + timer->setInterval(50); + connect(timer, SIGNAL(timeout()), this, SLOT(sendMouseClick())); + timer->start(); + QTimer::singleShot(10000, &mainWidget, SLOT(close())); // Emergency bail-out + toolButton->showMenu(); QTest::qWait(20); QCOMPARE(spy.count(),2); QCOMPARE(qvariant_cast(spy.at(1).at(0)), one); - delete menu; } void tst_QToolButton::collapseTextOnPriority() @@ -195,20 +207,30 @@ void tst_QToolButton::task230994_iconSize() void tst_QToolButton::task176137_autoRepeatOfAction() { QAction action(0); - QToolButton tb; - tb.setDefaultAction (&action); - tb.show(); + QWidget mainWidget; + mainWidget.setWindowTitle(QStringLiteral("task176137_autoRepeatOfAction")); + mainWidget.resize(200, 200); + mainWidget.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100)); + QToolButton *toolButton = new QToolButton(&mainWidget); + toolButton->setDefaultAction (&action); + QLabel *label = new QLabel(QStringLiteral("This test takes a while."), &mainWidget); + label->move(0, 50); + + mainWidget.show(); + QApplication::setActiveWindow(&mainWidget); + QVERIFY(QTest::qWaitForWindowActive(&mainWidget)); + QSignalSpy spy(&action,SIGNAL(triggered())); - QTest::mousePress ( &tb, Qt::LeftButton); - QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 2000); + QTest::mousePress (toolButton, Qt::LeftButton); + QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 2000); QCOMPARE(spy.count(),1); // try again with auto repeat - tb.setAutoRepeat (true); + toolButton->setAutoRepeat (true); QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy - QTest::mousePress ( &tb, Qt::LeftButton); - QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 3000); - qreal expected = (3000 - tb.autoRepeatDelay()) / tb.autoRepeatInterval() + 1; + QTest::mousePress (toolButton, Qt::LeftButton); + QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 3000); + 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; QVERIFY2(qAbs(diff) < 0.2, qPrintable( @@ -221,7 +243,17 @@ void tst_QToolButton::task176137_autoRepeatOfAction() void tst_QToolButton::sendMouseClick() { - QTest::mouseClick(w, Qt::LeftButton, 0, QPoint(7,7)); + if (m_menu.isNull()) { + qWarning("m_menu is NULL"); + return; + } + if (!m_menu->isVisible()) + return; + QTest::mouseClick(m_menu.data(), Qt::LeftButton, 0, QPoint(7, 7)); + if (QTimer *timer = qobject_cast(sender())) { + timer->stop(); + timer->deleteLater(); + } } void tst_QToolButton::qtbug_26956_popupTimerDone()