Share common code for baseline-testing widget UIs

Setting up the baseline tests, creating an appearance identifier,
and basic image-grabbing functionality doesn't need to be reinvented
for each test case that wants to use baseline testing of widget UIs.

As a drive-by, remove unneeded Qt 5 meta tags from .pri file.

Change-Id: I1562e1b377946305cac018e0f0f0175c2c07cd31
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Volker Hilsheimer 2021-11-19 12:25:00 +01:00
parent 086a08dcf9
commit 969cd432f5
7 changed files with 270 additions and 251 deletions

View File

@ -6,8 +6,9 @@ SOURCES += \
HEADERS += \
$$PWD/qbaselinetest.h
win32:MKSPEC=$$replace(QMAKESPEC, \\\\, /)
else:MKSPEC=$$QMAKESPEC
DEFINES += QMAKESPEC=\\\"$$MKSPEC\\\"
qtHaveModule(widgets) {
SOURCES += $$PWD/qwidgetbaselinetest.cpp
HEADERS += $$PWD/qwidgetbaselinetest.h
}
include($$PWD/baselineprotocol.pri)

View File

@ -0,0 +1,189 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "qwidgetbaselinetest.h"
#include <qbaselinetest.h>
#include <QApplication>
#include <QStyle>
#include <QScreen>
QT_BEGIN_NAMESPACE
QWidgetBaselineTest::QWidgetBaselineTest()
{
QBaselineTest::addClientProperty("Project", "Widgets");
// Set key platform properties that are relevant for the appearance of widgets
const QString platformName = QGuiApplication::platformName() + "-" + QSysInfo::productType();
QBaselineTest::addClientProperty("PlatformName", platformName);
QBaselineTest::addClientProperty("OSVersion", QSysInfo::productVersion());
// Encode a number of parameters that impact the UI
QPalette palette;
QFont font;
QByteArray appearanceBytes;
{
QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
appearanceStream << palette << font <<
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::style()->metaObject()->className();
#else
QApplication::style()->name();
#endif
const qreal screenDpr = QApplication::primaryScreen()->devicePixelRatio();
if (screenDpr != 1.0)
qWarning() << "DPR is" << screenDpr << "- images will be scaled";
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const quint16 appearanceId = qChecksum(appearanceBytes, appearanceBytes.size());
#else
const quint16 appearanceId = qChecksum(appearanceBytes);
#endif
// Assume that text that's darker than the background means we run in light mode
// This results in a more meaningful appearance ID between different runs than
// just the checksum of the various attributes.
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);
QBaselineTest::addClientProperty("AppearanceID", appearanceIdString);
// let users know where they can find the results
qDebug() << "PlatformName computed to be:" << platformName;
qDebug() << "Appearance ID computed as:" << appearanceIdString;
}
void QWidgetBaselineTest::initTestCase()
{
// Check and setup the environment. Failure to do so skips the test.
QByteArray msg;
if (!QBaselineTest::connectToBaselineServer(&msg))
QSKIP(msg);
}
void QWidgetBaselineTest::init()
{
QVERIFY(!window);
window = new QWidget;
window->setWindowTitle(QTest::currentDataTag());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
window->setScreen(QGuiApplication::primaryScreen());
#endif
window->move(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
doInit();
}
void QWidgetBaselineTest::cleanup()
{
doCleanup();
delete window;
window = nullptr;
}
void QWidgetBaselineTest::makeVisible()
{
Q_ASSERT(window);
window->show();
QApplication::setActiveWindow(window);
QVERIFY(QTest::qWaitForWindowActive(window));
// explicitly unset focus, the test needs to control when focus is shown
if (window->focusWidget())
window->focusWidget()->clearFocus();
}
/*
Always return images scaled to a DPR of 1.0.
This might produce some fuzzy differences, but lets us
compare those.
*/
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);
}
/*!
Sets standard widget properties on the test window and its children,
and uploads snapshots. The widgets are returned in the same state
that they had before.
Call this helper after setting up the test window.
*/
void QWidgetBaselineTest::takeStandardSnapshots()
{
makeVisible();
struct PublicWidget : QWidget {
bool focusNextPrevChild(bool next) override { return QWidget::focusNextPrevChild(next); }
};
QBASELINE_CHECK(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();
}
// this disables all children
window->setEnabled(false);
QBASELINE_CHECK(takeSnapshot(), "disabled");
window->setEnabled(true);
// show and activate another window so that our test window becomes inactive
QWidget otherWindow;
otherWindow.move(window->geometry().bottomRight() + QPoint(10, 10));
otherWindow.resize(50, 50);
otherWindow.setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
otherWindow.show();
otherWindow.windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&otherWindow));
QBASELINE_CHECK(takeSnapshot(), "inactive");
window->windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
if (window->focusWidget())
window->focusWidget()->clearFocus();
}
#include "qwidgetbaselinetest.moc"
QT_END_NAMESPACE

View File

@ -0,0 +1,65 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#pragma once
#include <QObject>
#include <QImage>
QT_BEGIN_NAMESPACE
class QWidget;
class QWidgetBaselineTest : public QObject
{
Q_OBJECT
public:
QWidgetBaselineTest();
void takeStandardSnapshots();
QWidget *testWindow() const { return window; }
protected:
virtual void doInit() {}
virtual void doCleanup() {}
private slots:
void initTestCase();
void init();
void cleanup();
protected:
void makeVisible();
QImage takeSnapshot();
private:
QWidget *window = nullptr;
};
QT_END_NAMESPACE

View File

@ -7,6 +7,7 @@ qt_internal_add_test(tst_baseline_stylesheet
SOURCES
../shared/baselineprotocol.cpp ../shared/baselineprotocol.h ../shared/lookup3.cpp
../shared/qbaselinetest.cpp ../shared/qbaselinetest.h
../shared/qwidgetbaselinetest.cpp ../shared/qwidgetbaselinetest.h
tst_baseline_stylesheet.cpp
INCLUDE_DIRECTORIES
../shared

View File

@ -27,104 +27,39 @@
****************************************************************************/
#include <qbaselinetest.h>
#include <qwidgetbaselinetest.h>
#include <QtWidgets>
#include <QByteArray>
class tst_Stylesheet : public QObject
class tst_Stylesheet : public QWidgetBaselineTest
{
Q_OBJECT
public:
tst_Stylesheet();
QWidget *testWindow() const { return window; }
void loadTestFiles();
private slots:
void initTestCase();
void init();
void cleanup();
void doInit() override;
private slots:
void tst_QToolButton_data();
void tst_QToolButton();
private:
void makeVisible();
QImage takeSnapshot();
QDir styleSheetDir;
QWidget *window = nullptr;
};
tst_Stylesheet::tst_Stylesheet()
{
QBaselineTest::addClientProperty("Project", "Widgets");
// Set key platform properties that are relevant for the appearance of widgets
const QString platformName = QGuiApplication::platformName() + "-" + QSysInfo::productType();
QBaselineTest::addClientProperty("PlatformName", platformName);
QBaselineTest::addClientProperty("OSVersion", QSysInfo::productVersion());
// Encode a number of parameters that impact the UI
QPalette palette;
QFont font;
QByteArray appearanceBytes;
{
QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
appearanceStream << palette << font <<
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::style()->metaObject()->className();
#else
QApplication::style()->name();
#endif
const qreal screenDpr = QApplication::primaryScreen()->devicePixelRatio();
if (screenDpr != 1.0)
qWarning() << "DPR is" << screenDpr << "- images will be scaled";
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const quint16 appearanceId = qChecksum(appearanceBytes, appearanceBytes.size());
#else
const quint16 appearanceId = qChecksum(appearanceBytes);
#endif
// Assume that text that's darker than the background means we run in light mode
// This results in a more meaningful appearance ID between different runs than
// just the checksum of the various attributes.
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);
QBaselineTest::addClientProperty("AppearanceID", appearanceIdString);
// let users know where they can find the results
qDebug() << "PlatformName computed to be:" << platformName;
qDebug() << "Appearance ID computed as:" << appearanceIdString;
}
void tst_Stylesheet::initTestCase()
{
QString baseDir = QFINDTESTDATA("qss/default.qss");
styleSheetDir = QDir(QFileInfo(baseDir).path());
// Check and setup the environment. Failure to do so skips the test.
QByteArray msg;
if (!QBaselineTest::connectToBaselineServer(&msg))
QSKIP(msg);
}
void tst_Stylesheet::init()
void tst_Stylesheet::doInit()
{
QFETCH(QString, styleSheet);
QVERIFY(!window);
window = new QWidget;
window->setWindowTitle(QTest::currentDataTag());
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
window->setScreen(QGuiApplication::primaryScreen());
#endif
window->move(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
window->setStyleSheet(styleSheet);
testWindow()->setStyleSheet(styleSheet);
}
void tst_Stylesheet::loadTestFiles()
@ -153,36 +88,6 @@ void tst_Stylesheet::loadTestFiles()
}
}
void tst_Stylesheet::makeVisible()
{
window->show();
window->window()->windowHandle()->requestActivate();
// explicitly unset focus, the test needs to control when focus is shown
if (window->focusWidget())
window->focusWidget()->clearFocus();
QVERIFY(QTest::qWaitForWindowActive(window));
}
/*
Always return images scaled to a DPR of 1.0.
This might produce some fuzzy differences, but lets us
compare those.
*/
QImage tst_Stylesheet::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);
}
void tst_Stylesheet::cleanup()
{
delete window;
window = nullptr;
}
void tst_Stylesheet::tst_QToolButton_data()
{
loadTestFiles();

View File

@ -2,6 +2,7 @@ qt_internal_add_test(tst_baseline_widgets
SOURCES
../shared/baselineprotocol.cpp ../shared/baselineprotocol.h ../shared/lookup3.cpp
../shared/qbaselinetest.cpp ../shared/qbaselinetest.h
../shared/qwidgetbaselinetest.cpp ../shared/qwidgetbaselinetest.h
tst_baseline_widgets.cpp
INCLUDE_DIRECTORIES
../shared

View File

@ -27,167 +27,24 @@
****************************************************************************/
#include <qbaselinetest.h>
#include <qwidgetbaselinetest.h>
#include <QtWidgets>
class tst_Widgets : public QObject
class tst_Widgets : public QWidgetBaselineTest
{
Q_OBJECT
public:
tst_Widgets();
void takeStandardSnapshots();
QWidget *testWindow() const { return window; }
tst_Widgets() = default;
private slots:
void initTestCase();
void init();
void cleanup();
void tst_QSlider_data();
void tst_QSlider();
void tst_QPushButton_data();
void tst_QPushButton();
private:
void makeVisible();
QImage takeSnapshot();
QWidget *window = nullptr;
};
tst_Widgets::tst_Widgets()
{
QBaselineTest::addClientProperty("Project", "Widgets");
// Set key platform properties that are relevant for the appearance of widgets
const QString platformName = QGuiApplication::platformName() + "-" + QSysInfo::productType();
QBaselineTest::addClientProperty("PlatformName", platformName);
QBaselineTest::addClientProperty("OSVersion", QSysInfo::productVersion());
// Encode a number of parameters that impact the UI
QPalette palette;
QFont font;
QByteArray appearanceBytes;
{
QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
appearanceStream << palette << font << QApplication::style()->name();
const qreal screenDpr = QApplication::primaryScreen()->devicePixelRatio();
if (screenDpr != 1.0)
qWarning() << "DPR is" << screenDpr << "- images will be scaled";
}
const quint16 appearanceId = qChecksum(appearanceBytes);
// Assume that text that's darker than the background means we run in light mode
// This results in a more meaningful appearance ID between different runs than
// just the checksum of the various attributes.
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);
QBaselineTest::addClientProperty("AppearanceID", appearanceIdString);
// let users know where they can find the results
qDebug() << "PlatformName computed to be:" << platformName;
qDebug() << "Appearance ID computed as:" << appearanceIdString;
}
void tst_Widgets::initTestCase()
{
// Check and setup the environment. Failure to do so skips the test.
QByteArray msg;
if (!QBaselineTest::connectToBaselineServer(&msg))
QSKIP(msg);
}
void tst_Widgets::init()
{
QVERIFY(!window);
window = new QWidget;
window->setWindowTitle(QTest::currentDataTag());
window->setScreen(QGuiApplication::primaryScreen());
window->move(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
}
void tst_Widgets::makeVisible()
{
window->show();
window->window()->windowHandle()->requestActivate();
// explicitly unset focus, the test needs to control when focus is shown
if (window->focusWidget())
window->focusWidget()->clearFocus();
QVERIFY(QTest::qWaitForWindowActive(window));
}
/*
Always return images scaled to a DPR of 1.0.
This might produce some fuzzy differences, but lets us
compare those.
*/
QImage tst_Widgets::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);
}
/*!
Sets standard widget properties on the test window and its children,
and uploads snapshots. The widgets are returned in the same state
that they had before.
Call this helper after setting up the test window.
*/
void tst_Widgets::takeStandardSnapshots()
{
makeVisible();
struct PublicWidget : QWidget { friend tst_Widgets; };
QBASELINE_CHECK(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 (window->focusWidget()) {
QBASELINE_CHECK(takeSnapshot(), "focused");
window->focusWidget()->clearFocus();
}
// this disables all children
window->setEnabled(false);
QBASELINE_CHECK(takeSnapshot(), "disabled");
window->setEnabled(true);
// show and activate another window so that our test window becomes inactive
QWidget otherWindow;
otherWindow.move(window->geometry().bottomRight() + QPoint(10, 10));
otherWindow.resize(50, 50);
otherWindow.setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
otherWindow.show();
otherWindow.windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&otherWindow));
QBASELINE_CHECK(takeSnapshot(), "inactive");
window->windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
if (window->focusWidget())
window->focusWidget()->clearFocus();
}
void tst_Widgets::cleanup()
{
delete window;
window = nullptr;
}
void tst_Widgets::tst_QSlider_data()
{
QTest::addColumn<Qt::Orientation>("orientation");
@ -221,7 +78,7 @@ void tst_Widgets::tst_QSlider()
}
const auto sliders = _sliders;
window->setLayout(box);
testWindow()->setLayout(box);
// we want to see sliders with different values
int value = 0;