Stabilize tst_qtoolbutton for Windows 8.1.

Ensure triggered() and task176137_autoRepeatOfAction()
are using an active main window. Change click timer in
triggered() to repeat until the menu is visible and
add a timer to close the widget and bail out if the
simulated click fails to close the menu.

Task-number: QTBUG-37822
Change-Id: If675660d02335bffd5caa89796914924dad1a909
Reviewed-by: Tony Sarajärvi <tony.sarajarvi@digia.com>
Reviewed-by: Oliver Wolff <oliver.wolff@digia.com>
This commit is contained in:
Friedemann Kleint 2014-03-27 16:44:44 +01:00 committed by The Qt Project
parent 685f8fe550
commit b980df3f3c

View File

@ -48,6 +48,8 @@
#include <qmenu.h> #include <qmenu.h>
#include <qaction.h> #include <qaction.h>
#include <qstyleoption.h> #include <qstyleoption.h>
#include <qscreen.h>
#include <qlabel.h>
class tst_QToolButton : public QObject class tst_QToolButton : public QObject
{ {
@ -68,7 +70,7 @@ private slots:
protected slots: protected slots:
void sendMouseClick(); void sendMouseClick();
private: private:
QWidget *w; QPointer<QWidget> m_menu;
}; };
tst_QToolButton::tst_QToolButton() tst_QToolButton::tst_QToolButton()
@ -121,29 +123,39 @@ void tst_QToolButton::getSetCheck()
void tst_QToolButton::triggered() void tst_QToolButton::triggered()
{ {
qRegisterMetaType<QAction *>("QAction *"); qRegisterMetaType<QAction *>("QAction *");
QToolButton tb; QWidget mainWidget;
tb.show(); mainWidget.setWindowTitle(QStringLiteral("triggered"));
QSignalSpy spy(&tb,SIGNAL(triggered(QAction*))); mainWidget.resize(200, 200);
QMenu *menu = new QMenu("Menu"); mainWidget.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100));
QToolButton *toolButton = new QToolButton(&mainWidget);
QSignalSpy spy(toolButton,SIGNAL(triggered(QAction*)));
QScopedPointer<QMenu> menu(new QMenu(QStringLiteral("Menu")));
QAction *one = menu->addAction("one"); QAction *one = menu->addAction("one");
menu->addAction("two"); menu->addAction("two");
QAction *def = new QAction("def", this); QAction *defaultAction = new QAction(QStringLiteral("def"), this);
tb.setMenu(menu); toolButton->setMenu(menu.data());
tb.setDefaultAction(def); toolButton->setDefaultAction(defaultAction);
mainWidget.show();
QApplication::setActiveWindow(&mainWidget);
QVERIFY(QTest::qWaitForWindowActive(&mainWidget));
def->trigger(); defaultAction->trigger();
QCOMPARE(spy.count(),1); QCOMPARE(spy.count(),1);
QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), def); QCOMPARE(qvariant_cast<QAction *>(spy.at(0).at(0)), defaultAction);
w = menu; m_menu = menu.data();
QTimer::singleShot(30, this, SLOT(sendMouseClick()));
tb.showMenu(); 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); QTest::qWait(20);
QCOMPARE(spy.count(),2); QCOMPARE(spy.count(),2);
QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one); QCOMPARE(qvariant_cast<QAction *>(spy.at(1).at(0)), one);
delete menu;
} }
void tst_QToolButton::collapseTextOnPriority() void tst_QToolButton::collapseTextOnPriority()
@ -195,20 +207,30 @@ void tst_QToolButton::task230994_iconSize()
void tst_QToolButton::task176137_autoRepeatOfAction() void tst_QToolButton::task176137_autoRepeatOfAction()
{ {
QAction action(0); QAction action(0);
QToolButton tb; QWidget mainWidget;
tb.setDefaultAction (&action); mainWidget.setWindowTitle(QStringLiteral("task176137_autoRepeatOfAction"));
tb.show(); 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())); QSignalSpy spy(&action,SIGNAL(triggered()));
QTest::mousePress ( &tb, Qt::LeftButton); QTest::mousePress (toolButton, Qt::LeftButton);
QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 2000); QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 2000);
QCOMPARE(spy.count(),1); QCOMPARE(spy.count(),1);
// try again with auto repeat // try again with auto repeat
tb.setAutoRepeat (true); toolButton->setAutoRepeat (true);
QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy QSignalSpy repeatSpy(&action,SIGNAL(triggered())); // new spy
QTest::mousePress ( &tb, Qt::LeftButton); QTest::mousePress (toolButton, Qt::LeftButton);
QTest::mouseRelease ( &tb, Qt::LeftButton, 0, QPoint (), 3000); QTest::mouseRelease (toolButton, Qt::LeftButton, 0, QPoint (), 3000);
qreal expected = (3000 - tb.autoRepeatDelay()) / tb.autoRepeatInterval() + 1; const qreal expected = (3000 - toolButton->autoRepeatDelay()) / toolButton->autoRepeatInterval() + 1;
//we check that the difference is small (on some systems timers are not super accurate) //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.count()) / expected;
QVERIFY2(qAbs(diff) < 0.2, qPrintable( QVERIFY2(qAbs(diff) < 0.2, qPrintable(
@ -221,7 +243,17 @@ void tst_QToolButton::task176137_autoRepeatOfAction()
void tst_QToolButton::sendMouseClick() 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<QTimer *>(sender())) {
timer->stop();
timer->deleteLater();
}
} }
void tst_QToolButton::qtbug_26956_popupTimerDone() void tst_QToolButton::qtbug_26956_popupTimerDone()