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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <qbaselinetest.h>
#include <qwidgetbaselinetest.h>
#include <QtWidgets>
class tst_Text : public QWidgetBaselineTest
{
Q_OBJECT
public:
tst_Text();
void loadTestFiles();
private slots:
void tst_render_data();
void tst_render();
void tst_differentScriptsBackgrounds();
private:
QDir htmlDir;
};
tst_Text::tst_Text()
{
QString baseDir = QFINDTESTDATA("data/empty.html");
htmlDir = QDir(QFileInfo(baseDir).path());
}
void tst_Text::loadTestFiles()
{
QTest::addColumn<QString>("html");
QStringList htmlFiles;
// first add generic test files
for (const auto &qssFile : htmlDir.entryList({QStringLiteral("*.html")}, QDir::Files | QDir::Readable))
htmlFiles << htmlDir.absoluteFilePath(qssFile);
// then test-function specific files
const QString testFunction = QString(QTest::currentTestFunction()).remove("tst_").toLower();
if (htmlDir.cd(testFunction)) {
for (const auto &htmlFile : htmlDir.entryList({QStringLiteral("*.html")}, QDir::Files | QDir::Readable))
htmlFiles << htmlDir.absoluteFilePath(htmlFile);
htmlDir.cdUp();
}
for (const auto &htmlFile : htmlFiles) {
QFileInfo fileInfo(htmlFile);
QFile file(htmlFile);
file.open(QFile::ReadOnly);
QString html = QString::fromUtf8(file.readAll());
QBaselineTest::newRow(fileInfo.baseName().toUtf8()) << html;
}
}
void tst_Text::tst_render_data()
{
loadTestFiles();
}
void tst_Text::tst_render()
{
QFETCH(QString, html);
QTextDocument textDocument;
textDocument.setPageSize(QSizeF(800, 600));
textDocument.setHtml(html);
QImage image(800, 600, QImage::Format_ARGB32);
image.fill(Qt::white);
{
QPainter painter(&image);
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, Qt::black);
textDocument.documentLayout()->draw(&painter, context);
}
QBASELINE_TEST(image);
}
void tst_Text::tst_differentScriptsBackgrounds()
{
QTextDocument textDocument;
textDocument.setPageSize(QSizeF(800, 600));
textDocument.setHtml(QString::fromUtf8("<i><font style=\"font-size:72px\"><font style=\"background:#FFFF00\">イ雨エ</font></font></i>"));
QImage image(800, 600, QImage::Format_ARGB32);
image.fill(Qt::white);
{
QPainter painter(&image);
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, Qt::black);
textDocument.documentLayout()->draw(&painter, context);
}
QBASELINE_CHECK(image, "tst_differentScriptsBackgrounds");
}
#define main _realmain
QTEST_MAIN(tst_Text)
#undef main
int main(int argc, char *argv[])
{
// Avoid rendering variations caused by QHash randomization
QHashSeed::setDeterministicGlobalSeed();
QBaselineTest::handleCmdLineArgs(&argc, &argv);
return _realmain(argc, argv);
}
#include "tst_baseline_text.moc"
|