1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
// 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 <QPainter>
#include <QProxyStyle>
#include <QStyleOption>
#include <QJsonObject>
#include <QJsonArray>
#include <QtCore/private/qabstractanimation_p.h>
#include <QtWidgets/private/qapplication_p.h>
#include <QtWidgets/private/qstyle_p.h>
#if defined(Q_OS_APPLE)
#include <QtCore/private/qcore_mac_p.h>
#endif
QT_BEGIN_NAMESPACE
class DebugStyle : public QProxyStyle
{
public:
DebugStyle(QStyle *style, QWidgetBaselineTest* baselineTest)
: QProxyStyle(style), baselineTest(baselineTest)
{
setParent(baselineTest);
QStylePrivate::get(this)->name = style->name();
}
void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
{
QProxyStyle::drawPrimitive(element, option, painter, widget);
drawDebugRect("QStyle::drawPrimitive", Qt::magenta, element, option, widget, painter);
}
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const override
{
QProxyStyle::drawControl(element, option, painter, widget);
drawDebugRect("QStyle::drawControl", Qt::magenta, element, option, widget, painter);
}
void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const override
{
QProxyStyle::drawComplexControl(control, option, painter, widget);
drawDebugRect("QStyle::drawComplexControl", Qt::magenta, control, option, widget, painter);
// Report all matching sub-controls, independently of whether the above call queries them
static QMetaEnum complexControlEnum = QMetaEnum::fromType<ComplexControl>();
static QMetaEnum subControlEnum = QMetaEnum::fromType<SubControl>();
const auto prefix = QByteArray(complexControlEnum.valueToKey(control)).replace("CC_", "SC_");
for (int i = 0; i < subControlEnum.keyCount(); ++i) {
const QByteArray key = subControlEnum.key(i);
if (key.startsWith(prefix)) {
auto rect = subControlRect(control, option, SubControl(subControlEnum.value(i)), widget);
baselineTest->reportDebugRect("QStyle::subControlRect", QColorConstants::Svg::orange,
key, rect.translated(option->rect.topLeft()), widget, painter);
}
}
}
private:
template <typename T>
void drawDebugRect(const QString &type, QColor color, T element, const QStyleOption *option, const QWidget *widget, QPainter *painter = nullptr) const
{
QMetaEnum metaEnum = QMetaEnum::fromType<T>();
auto *elementName = metaEnum.valueToKey(element);
baselineTest->reportDebugRect(type, color,
QString::fromLatin1(elementName), option->rect,
widget, painter);
if (widget) {
auto *className = widget->metaObject()->className();
baselineTest->reportDebugRect("QWidget::rect", Qt::green,
QString::fromLatin1(className), widget->rect(),
widget, painter);
baselineTest->reportDebugRect("QWidget::contentsRect", Qt::green,
QString::fromLatin1(className), widget->contentsRect(),
widget, painter);
}
if (painter) {
baselineTest->reportDebugRect("QPainter::clipRegion", Qt::red,
QString::fromLatin1(elementName), painter->clipRegion().boundingRect(),
widget, painter);
}
}
QWidgetBaselineTest *baselineTest = nullptr;
};
QWidgetBaselineTest::QWidgetBaselineTest()
{
// Fail by throwing, since we QVERIFY deep in the helper functions
QTest::setThrowOnFail(true);
qApp->setStyle(new DebugStyle(qApp->style(), this));
QBaselineTest::setProject("Widgets");
// Set key platform properties that are relevant for the appearance of widgets
const QString platformName = QGuiApplication::platformName();
QBaselineTest::addClientProperty("PlatformName", platformName);
QBaselineTest::addClientProperty("OSVersion", QSysInfo::productVersion());
// Encode a number of parameters that impact the UI
QPalette palette;
QFont 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());
QApplication::style()->setProperty("_q_no_animation", true);
QUnifiedTimer::instance()->setSpeedModifier(100000);
QGuiApplication::styleHints()->setCursorFlashTime(50000);
QByteArray appearanceBytes;
{
QDataStream appearanceStream(&appearanceBytes, QIODevice::WriteOnly);
appearanceStream << palette << font;
}
#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-%2") : QString("dark-%2"))
.arg(appearanceId, 0, 16);
QBaselineTest::addClientProperty("AppearanceID", appearanceIdString);
#if defined(Q_OS_APPLE)
QBaselineTest::addClientProperty("LiquidGlass",
qt_apple_runningWithLiquidGlass() ? "enabled" : "disabled");
#endif
QBaselineTest::addClientProperty("DevicePixelRatio",
QString::number(QGuiApplication::primaryScreen()->devicePixelRatio()));
QBaselineTest::addClientProperty("Style", styleName);
QBaselineTest::setProjectImageKeys({
"GitBranch",
"OSName",
"OSVersion",
"PlatformName",
"Style",
"AppearanceID"
});
// 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);
background = new QWidget(nullptr, Qt::FramelessWindowHint);
QPalette pal;
QImage checkerboard(QSize(20, 20), QImage::Format_Grayscale8);
checkerboard.fill(Qt::white);
QPainter painter(&checkerboard);
painter.fillRect(0, 0, 10, 10, Qt::lightGray);
painter.fillRect(10, 10, 10, 10, Qt::lightGray);
painter.end();
pal.setBrush(QPalette::Window, checkerboard);
background->setPalette(pal);
window = new QWidget(background, Qt::Window | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
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());
debugRects = QJsonObject{};
doInit();
}
void QWidgetBaselineTest::cleanup()
{
doCleanup();
delete background;
background = nullptr;
window = nullptr;
}
void QWidgetBaselineTest::cleanupTestCase()
{
QBaselineTest::finalizeAndDisconnect();
}
void QWidgetBaselineTest::makeVisible()
{
Q_ASSERT(window);
// Always open window on primary screen
QScreen *preferredScreen = QGuiApplication::primaryScreen();
const QRect preferredScreenRect = preferredScreen->availableGeometry();
background->setScreen(preferredScreen);
background->move(preferredScreenRect.topLeft());
background->showMaximized();
QVERIFY(QTest::qWaitForWindowExposed(background));
window->setScreen(preferredScreen);
window->move(preferredScreenRect.topLeft());
window->show();
QVERIFY(QTest::qWaitForWindowExposed(window));
QApplicationPrivate::setActiveWindow(window);
QVERIFY(QTest::qWaitForWindowActive(window));
// explicitly set focus on the window so that the test widget doesn't have it
window->setFocus(Qt::OtherFocusReason);
QTRY_COMPARE(window->focusWidget(), window);
}
/*
Grabs the test window and returns the resulting QImage, without
compensating for DPR differences.
*/
QImage QWidgetBaselineTest::takeSnapshot()
{
// Process events for whatever state changes was initiated
// prior to the snapshot.
QCoreApplication::processEvents();
// Render to QImage instead of going via QWidget::grab(),
// as the latter will typically use an RGB32 image, and
// we want to detect issues in the alpha-channel too.
const auto dpr = window->devicePixelRatio();
const auto size = window->size();
QImage image(size * dpr, QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(dpr);
// The widget might claim to be be opaque, but we want to detect if it lies
image.fill(Qt::transparent);
window->render(&image, {}, QRect({}, size),
QWidget::DrawWindowBackground
| QWidget::DrawChildren
| QWidget::IgnoreMask
);
if (!debugRects.isEmpty()) {
QJsonDocument doc(debugRects);
image.setText("DebugRects", doc.toJson(QJsonDocument::Compact));
}
return image;
}
/*
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();
}
/*!
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();
QWidget *oldFocusWidget = testWindow()->focusWidget();
QCOMPARE(oldFocusWidget, testWindow());
QBASELINE_CHECK_DEFERRED(takeSnapshot(), "default");
// try hard to set focus
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_DEFERRED(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 | Qt::NoDropShadowWindowHint);
otherWindow.show();
otherWindow.windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(&otherWindow));
QBASELINE_CHECK_DEFERRED(takeSnapshot(), "inactive");
window->windowHandle()->requestActivate();
QVERIFY(QTest::qWaitForWindowActive(window));
if (window->focusWidget())
window->focusWidget()->clearFocus();
}
void QWidgetBaselineTest::reportDebugRect(const QString &type, const QColor &color,
const QString &label, QRect widgetRect, const QWidget *widget, QPainter *painter)
{
const qreal dpr = widget ? widget->devicePixelRatio()
: painter ? painter->device()->devicePixelRatio()
: 1.0;
QRect windowRect = widget ? widgetRect.translated(widget->mapTo(widget->window(), QPoint())) : widgetRect;
QRect rect(windowRect.topLeft() * dpr, windowRect.size() * dpr);
auto typeObject = debugRects[type].toObject();
if (typeObject.isEmpty()) {
typeObject["color"] = color.name();
typeObject["rects"] = QJsonArray();
}
auto rects = typeObject["rects"].toArray();
rects.append(QJsonObject{
{ "x", rect.x() },
{ "y", rect.y() },
{ "width", rect.width() },
{ "height", rect.height() },
{ "label", label },
});
typeObject["rects"] = rects;
debugRects[type] = typeObject;
}
QT_END_NAMESPACE
|