summaryrefslogtreecommitdiffstats
path: root/tests/baseline/shared/qwidgetbaselinetest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/baseline/shared/qwidgetbaselinetest.cpp')
-rw-r--r--tests/baseline/shared/qwidgetbaselinetest.cpp137
1 files changed, 70 insertions, 67 deletions
diff --git a/tests/baseline/shared/qwidgetbaselinetest.cpp b/tests/baseline/shared/qwidgetbaselinetest.cpp
index 9aafe152db..72a074e268 100644
--- a/tests/baseline/shared/qwidgetbaselinetest.cpp
+++ b/tests/baseline/shared/qwidgetbaselinetest.cpp
@@ -1,43 +1,21 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 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$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qwidgetbaselinetest.h"
#include <qbaselinetest.h>
#include <QApplication>
#include <QStyle>
+#include <QStyleHints>
#include <QScreen>
+#include <QtWidgets/private/qapplication_p.h>
+
QT_BEGIN_NAMESPACE
QWidgetBaselineTest::QWidgetBaselineTest()
{
- QBaselineTest::addClientProperty("Project", "Widgets");
+ QBaselineTest::setProject("Widgets");
// Set key platform properties that are relevant for the appearance of widgets
const QString platformName = QGuiApplication::platformName() + "-" + QSysInfo::productType();
@@ -47,18 +25,25 @@ QWidgetBaselineTest::QWidgetBaselineTest()
// Encode a number of parameters that impact the UI
QPalette palette;
QFont font;
- QByteArray appearanceBytes;
- {
- QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
- appearanceStream << palette << font <<
+ const QString styleName =
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::style()->metaObject()->className();
#else
QApplication::style()->name();
#endif
+ // turn off animations and make the cursor flash time really long to avoid blinking
+ QApplication::style()->setProperty("_qt_animation_time", QTime());
+ QGuiApplication::styleHints()->setCursorFlashTime(50000);
+
+ QByteArray appearanceBytes;
+ {
+ QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
+ appearanceStream << palette << font;
const qreal screenDpr = QApplication::primaryScreen()->devicePixelRatio();
- if (screenDpr != 1.0)
- qWarning() << "DPR is" << screenDpr << "- images will be scaled";
+ if (screenDpr != 1.0) {
+ qWarning() << "DPR is" << screenDpr << "- images will not be compared to 1.0 baseline!";
+ appearanceStream << screenDpr;
+ }
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const quint16 appearanceId = qChecksum(appearanceBytes, appearanceBytes.size());
@@ -72,8 +57,8 @@ QWidgetBaselineTest::QWidgetBaselineTest()
const QColor windowColor = palette.window().color();
const QColor textColor = palette.text().color();
const QString appearanceIdString = (windowColor.value() > textColor.value()
- ? QString("light-%1") : QString("dark-%1"))
- .arg(appearanceId, 0, 16);
+ ? QString("light-%1-%2") : QString("dark-%1-%2"))
+ .arg(styleName).arg(appearanceId, 0, 16);
QBaselineTest::addClientProperty("AppearanceID", appearanceIdString);
// let users know where they can find the results
@@ -92,11 +77,15 @@ void QWidgetBaselineTest::initTestCase()
void QWidgetBaselineTest::init()
{
QVERIFY(!window);
- window = new QWidget;
+ background = new QWidget(nullptr, Qt::FramelessWindowHint);
+ window = new QWidget(background, Qt::Window);
window->setWindowTitle(QTest::currentDataTag());
+ window->setFocusPolicy(Qt::StrongFocus);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
+ background->setScreen(QGuiApplication::primaryScreen());
window->setScreen(QGuiApplication::primaryScreen());
#endif
+ background->move(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
window->move(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
doInit();
@@ -106,33 +95,46 @@ void QWidgetBaselineTest::cleanup()
{
doCleanup();
- delete window;
+ delete background;
+ background = nullptr;
window = nullptr;
}
void QWidgetBaselineTest::makeVisible()
{
Q_ASSERT(window);
+ background->showMaximized();
window->show();
- QApplication::setActiveWindow(window);
+ QApplicationPrivate::setActiveWindow(window);
QVERIFY(QTest::qWaitForWindowActive(window));
- // explicitly unset focus, the test needs to control when focus is shown
- if (window->focusWidget())
- window->focusWidget()->clearFocus();
+ // explicitly set focus on the window so that the test widget doesn't have it
+ window->setFocus(Qt::OtherFocusReason);
+ QTRY_COMPARE(window->focusWidget(), window);
}
/*
- Always return images scaled to a DPR of 1.0.
-
- This might produce some fuzzy differences, but lets us
- compare those.
+ Grabs the test window and returns the resulting QImage, without
+ compensating for DPR differences.
*/
QImage QWidgetBaselineTest::takeSnapshot()
{
- QGuiApplication::processEvents();
- QPixmap pm = window->grab();
- QTransform scaleTransform = QTransform::fromScale(1.0 / pm.devicePixelRatioF(), 1.0 / pm.devicePixelRatioF());
- return pm.toImage().transformed(scaleTransform, Qt::SmoothTransformation);
+ // make sure all effects are done
+ QTest::qWait(250);
+ return window->grab().toImage();
+}
+
+/*
+ Grabs the test window screen and returns the resulting QImage, without
+ compensating for DPR differences.
+ This can be used for popup windows.
+*/
+QImage QWidgetBaselineTest::takeScreenSnapshot(const QRect& windowRect)
+{
+ // make sure all effects are done - wait longer here because entire
+ // windows might be fading in and out.
+ QTest::qWait(750);
+ return window->screen()->grabWindow(0, windowRect.x(), windowRect.y(),
+ windowRect.width(), windowRect.height()).toImage();
}
/*!
@@ -145,27 +147,30 @@ QImage QWidgetBaselineTest::takeSnapshot()
void QWidgetBaselineTest::takeStandardSnapshots()
{
makeVisible();
- struct PublicWidget : QWidget {
- bool focusNextPrevChild(bool next) override { return QWidget::focusNextPrevChild(next); }
- };
- QBASELINE_CHECK(takeSnapshot(), "default");
+ QWidget *oldFocusWidget = testWindow()->focusWidget();
+ QCOMPARE(oldFocusWidget, testWindow());
+ QBASELINE_CHECK_DEFERRED(takeSnapshot(), "default");
// try hard to set focus
- static_cast<PublicWidget*>(window)->focusNextPrevChild(true);
- if (!window->focusWidget()) {
- QWidget *firstChild = window->findChild<QWidget*>();
- if (firstChild)
- firstChild->setFocus();
- }
- if (testWindow()->focusWidget()) {
- QBASELINE_CHECK(takeSnapshot(), "focused");
- testWindow()->focusWidget()->clearFocus();
+ QWidget *testWidget = window->nextInFocusChain();
+ if (!testWidget)
+ testWidget = window->findChild<QWidget*>();
+ QVERIFY(testWidget);
+ // use TabFocusReason, some widgets handle that specifically to e.g. select
+ testWidget->setFocus(Qt::TabFocusReason);
+
+ if (testWindow()->focusWidget() != oldFocusWidget) {
+ QBASELINE_CHECK_DEFERRED(takeSnapshot(), "focused");
+ // set focus back
+ oldFocusWidget->setFocus(Qt::OtherFocusReason);
+ } else {
+ qWarning() << "Couldn't set focus on tested widget" << testWidget;
}
// this disables all children
window->setEnabled(false);
- QBASELINE_CHECK(takeSnapshot(), "disabled");
+ QBASELINE_CHECK_DEFERRED(takeSnapshot(), "disabled");
window->setEnabled(true);
// show and activate another window so that our test window becomes inactive
@@ -176,7 +181,7 @@ void QWidgetBaselineTest::takeStandardSnapshots()
otherWindow.show();
otherWindow.windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&otherWindow));
- QBASELINE_CHECK(takeSnapshot(), "inactive");
+ QBASELINE_CHECK_DEFERRED(takeSnapshot(), "inactive");
window->windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
@@ -184,6 +189,4 @@ void QWidgetBaselineTest::takeStandardSnapshots()
window->focusWidget()->clearFocus();
}
-#include "qwidgetbaselinetest.moc"
-
QT_END_NAMESPACE