From be8792d4fe6b1843068f1a115b96c74c80aefe3a Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 30 Dec 2021 12:15:20 +0100 Subject: [PATCH] Add baseline test for text rendering Data-driven test case that renders the HTML files into an 800x600 image for baseline comparison. Task-number: QTBUG-99148 Pick-to: 6.3 Change-Id: I9ccc0cd21a1e94ff68d23bb82b84e1da46d6335a Reviewed-by: Eirik Aavitsland --- tests/baseline/CMakeLists.txt | 1 + tests/baseline/text/CMakeLists.txt | 16 +++ tests/baseline/text/data/empty.html | 0 .../text/data/list_items_with_code.html | 8 ++ tests/baseline/text/tst_baseline_text.cpp | 122 ++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 tests/baseline/text/CMakeLists.txt create mode 100644 tests/baseline/text/data/empty.html create mode 100644 tests/baseline/text/data/list_items_with_code.html create mode 100644 tests/baseline/text/tst_baseline_text.cpp diff --git a/tests/baseline/CMakeLists.txt b/tests/baseline/CMakeLists.txt index dadfa1ba7d4..df5d35863b2 100644 --- a/tests/baseline/CMakeLists.txt +++ b/tests/baseline/CMakeLists.txt @@ -4,4 +4,5 @@ endif() if(TARGET Qt::Network AND TARGET Qt::Widgets) add_subdirectory(widgets) add_subdirectory(stylesheet) + add_subdirectory(text) endif() diff --git a/tests/baseline/text/CMakeLists.txt b/tests/baseline/text/CMakeLists.txt new file mode 100644 index 00000000000..707b3794b5a --- /dev/null +++ b/tests/baseline/text/CMakeLists.txt @@ -0,0 +1,16 @@ +list(APPEND test_data "./data") + +qt_internal_add_test(tst_baseline_text + SOURCES + ../shared/baselineprotocol.cpp ../shared/baselineprotocol.h ../shared/lookup3.cpp + ../shared/qbaselinetest.cpp ../shared/qbaselinetest.h + ../shared/qwidgetbaselinetest.cpp ../shared/qwidgetbaselinetest.h + tst_baseline_text.cpp + INCLUDE_DIRECTORIES + ../shared + PUBLIC_LIBRARIES + Qt::Gui + Qt::Widgets + Qt::Network + TESTDATA ${test_data} +) diff --git a/tests/baseline/text/data/empty.html b/tests/baseline/text/data/empty.html new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/baseline/text/data/list_items_with_code.html b/tests/baseline/text/data/list_items_with_code.html new file mode 100644 index 00000000000..f2823c8065b --- /dev/null +++ b/tests/baseline/text/data/list_items_with_code.html @@ -0,0 +1,8 @@ + +

Header

+
    +
  1. Something (this is code something else)

  2. +
  3. Maybe this is code or not?

  4. +
  5. this is code and it seems to break

  6. +
  7. This has no code

  8. +
diff --git a/tests/baseline/text/tst_baseline_text.cpp b/tests/baseline/text/tst_baseline_text.cpp new file mode 100644 index 00000000000..363a2ef6cdb --- /dev/null +++ b/tests/baseline/text/tst_baseline_text.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** 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 +#include +#include + +class tst_Text : public QWidgetBaselineTest +{ + Q_OBJECT + +public: + tst_Text(); + + void loadTestFiles(); + +private slots: + void tst_render_data(); + void tst_render(); + +private: + QDir htmlDir; +}; + +tst_Text::tst_Text() +{ + QString baseDir = QFINDTESTDATA("data/empty.html"); + htmlDir = QDir(QFileInfo(baseDir).path()); +} + +void tst_Text::loadTestFiles() +{ + QTest::addColumn("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); +} + + +#define main _realmain +QTEST_MAIN(tst_Text) +#undef main + +int main(int argc, char *argv[]) +{ + qSetGlobalQHashSeed(0); // Avoid rendering variations caused by QHash randomization + + QBaselineTest::handleCmdLineArgs(&argc, &argv); + return _realmain(argc, argv); +} + +#include "tst_baseline_text.moc"