From 9205110cfe84204c0f8eb2b90f42e9131d27108e Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 9 Jun 2017 09:05:02 +0200 Subject: Refactor tst_QStyleSheetStyle::focus/hoverColors() - Put all widgets in one dialog so that show/setActive occurs only once. - Use the center of the widget geometry for positioning. - Remove BypassWindowManagerHint which likely causes qWaitForWindowActive() to fail. - Move the cursor out of the way and subsequently send mouse events to the QWindow Task-number: QTBUG-51400 Change-Id: I2176d8dbaead72d7a6fa89aa769e4c804eea7a0c Reviewed-by: Qt CI Bot Reviewed-by: Simon Hausmann --- .../qstylesheetstyle/tst_qstylesheetstyle.cpp | 119 ++++++++++++--------- 1 file changed, 69 insertions(+), 50 deletions(-) (limited to 'tests/auto/widgets/styles') diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index d8c70a0cc1..693b0b8aee 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -817,22 +817,45 @@ static bool testForColors(const QImage& image, const QColor& color, bool ensureP return false; } -static const QList sample_widgets() // returning const to avoid detaching when passing to range-for +class TestDialog : public QDialog { +public: + explicit TestDialog(const QString &styleSheet); + + QWidgetList widgets() const { return m_widgets; } + QLineEdit *focusDummy() const { return m_focusDummy; } + +private: + void addWidget(QWidget *w) + { + w->setStyleSheet(m_styleSheet); + m_layout->addWidget(w); + m_widgets.append(w); + } + + const QString m_styleSheet; + QVBoxLayout* m_layout; + QLineEdit *m_focusDummy; + QWidgetList m_widgets; +}; + +TestDialog::TestDialog(const QString &styleSheet) : + m_styleSheet(styleSheet), + m_layout(new QVBoxLayout(this)), + m_focusDummy(new QLineEdit) { - QList widgets; - widgets << new QPushButton("TESTING TESTING"); - widgets << new QLineEdit("TESTING TESTING"); - widgets << new QLabel("TESTING TESTING"); + m_layout->addWidget(m_focusDummy); // Avoids initial focus. + addWidget(new QPushButton("TESTING TESTING")); + addWidget(new QLineEdit("TESTING TESTING")); + addWidget(new QLabel("TESTING TESTING")); QSpinBox *spinbox = new QSpinBox; spinbox->setMaximum(1000000000); spinbox->setValue(123456789); - widgets << spinbox; + addWidget(spinbox); QComboBox *combobox = new QComboBox; combobox->setEditable(true); combobox->addItems(QStringList() << "TESTING TESTING"); - widgets << combobox; - widgets << new QLabel("TESTING TESTING"); - return widgets; + addWidget(spinbox); + addWidget(new QLabel("TESTING TESTING")); } void tst_QStyleSheetStyle::focusColors() @@ -852,28 +875,21 @@ void tst_QStyleSheetStyle::focusColors() "That doesn't mean that the feature doesn't work in practice."); #endif + TestDialog frame(QStringLiteral("*:focus { border:none; background: #e8ff66; color: #ff0084 }")); + frame.setWindowTitle(QTest::currentTestFunction()); - for (QWidget *widget : sample_widgets()) { - QDialog frame; - QLayout* layout = new QGridLayout; - - QLineEdit* dummy = new QLineEdit; // Avoids initial focus. - - widget->setStyleSheet("*:focus { border:none; background: #e8ff66; color: #ff0084 }"); + centerOnScreen(&frame); + frame.show(); - layout->addWidget(dummy); - layout->addWidget(widget); - frame.setLayout(layout); + QApplication::setActiveWindow(&frame); + QVERIFY(QTest::qWaitForWindowActive(&frame)); - centerOnScreen(&frame); - frame.show(); - QApplication::setActiveWindow(&frame); - QVERIFY(QTest::qWaitForWindowActive(&frame)); + for (QWidget *widget : frame.widgets()) { widget->setFocus(); QApplication::processEvents(); - QImage image(frame.width(), frame.height(), QImage::Format_ARGB32); - frame.render(&image); + QImage image(widget->width(), widget->height(), QImage::Format_ARGB32); + widget->render(&image); if (image.depth() < 24) QSKIP("Test doesn't support color depth < 24"); @@ -896,32 +912,35 @@ void tst_QStyleSheetStyle::hoverColors() #ifdef Q_OS_OSX QSKIP("This test is fragile on Mac, most likely due to QTBUG-33959."); #endif + TestDialog frame(QStringLiteral("*:hover { border:none; background: #e8ff66; color: #ff0084 }")); + frame.setWindowTitle(QTest::currentTestFunction()); - for (QWidget *widget : sample_widgets()) { - //without Qt::X11BypassWindowManagerHint the window manager may move the window after we moved the cursor - QDialog frame(0, Qt::X11BypassWindowManagerHint); - QLayout* layout = new QGridLayout; - - QLineEdit* dummy = new QLineEdit; + centerOnScreen(&frame); + // Move the mouse cursor out of the way to suppress spontaneous QEvent::Enter + // events interfering with QApplicationPrivate::dispatchEnterLeave(). + // Mouse events can then be sent directly to the QWindow instead of the + // QWidget, triggering the enter/leave handling within the dialog window, + // speeding up the test. + QCursor::setPos(frame.geometry().topLeft() - QPoint(100, 0)); + frame.show(); - widget->setStyleSheet("*:hover { border:none; background: #e8ff66; color: #ff0084 }"); + QApplication::setActiveWindow(&frame); + QVERIFY(QTest::qWaitForWindowActive(&frame)); - layout->addWidget(dummy); - layout->addWidget(widget); - frame.setLayout(layout); + QWindow *frameWindow = frame.windowHandle(); + QVERIFY(frameWindow); - centerOnScreen(&frame); - frame.show(); + const QPoint dummyPos = frame.focusDummy()->geometry().center(); + QTest::mouseMove(frameWindow, dummyPos); - QApplication::setActiveWindow(&frame); - QVERIFY(QTest::qWaitForWindowActive(&frame)); + for (QWidget *widget : frame.widgets()) { //move the mouse inside the widget, it should be colored - QTest::mouseMove ( widget, QPoint(6,6)); + const QRect widgetGeometry = widget->geometry(); + QTest::mouseMove(frameWindow, widgetGeometry.center()); + QTRY_VERIFY2(widget->testAttribute(Qt::WA_UnderMouse), widget->metaObject()->className()); - QTRY_VERIFY(widget->testAttribute(Qt::WA_UnderMouse)); - - QImage image(frame.width(), frame.height(), QImage::Format_ARGB32); - frame.render(&image); + QImage image(widgetGeometry.size(), QImage::Format_ARGB32); + widget->render(&image); QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)), (QString::fromLatin1(widget->metaObject()->className()) @@ -931,10 +950,10 @@ void tst_QStyleSheetStyle::hoverColors() + " did not contain text color #ff0084").toLocal8Bit().constData()); //move the mouse outside the widget, it should NOT be colored - QTest::mouseMove ( dummy, QPoint(5,5)); - QTest::qWait(60); + QTest::mouseMove(frameWindow, dummyPos); + QTRY_VERIFY2(frame.focusDummy()->testAttribute(Qt::WA_UnderMouse), "FocusDummy"); - frame.render(&image); + widget->render(&image); QVERIFY2(!testForColors(image, QColor(0xe8, 0xff, 0x66)), (QString::fromLatin1(widget->metaObject()->className()) @@ -944,10 +963,10 @@ void tst_QStyleSheetStyle::hoverColors() + " did contain text color #ff0084").toLocal8Bit().constData()); //move the mouse again inside the widget, it should be colored - QTest::mouseMove (widget, QPoint(5,5)); - QTRY_VERIFY(widget->testAttribute(Qt::WA_UnderMouse)); + QTest::mouseMove (frameWindow, widgetGeometry.center()); + QTRY_VERIFY2(widget->testAttribute(Qt::WA_UnderMouse), widget->metaObject()->className()); - frame.render(&image); + widget->render(&image); QVERIFY2(testForColors(image, QColor(0xe8, 0xff, 0x66)), (QString::fromLatin1(widget->metaObject()->className()) -- cgit v1.2.3