Skip to content

Commit

Permalink
Add test for output rendering
Browse files Browse the repository at this point in the history
For now this is mostly an example - but it seems useful to have the
ability to test rendering output.
  • Loading branch information
equalsraf committed Aug 1, 2023
1 parent 1f73d63 commit 0e65b3a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ jobs:
pyenv global system
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_GCOV=ON -DPYTHON_EXECUTABLE=/usr/bin/python3 ..
cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_GCOV=ON -DPYTHON_EXECUTABLE=/usr/bin/python3 -DENABLE_TESTS=ON ..
cmake --build . --target bindings -- VERBOSE=1
cmake --build . -- VERBOSE=1
- run:
name: test
command: |
cd build
ctest -VV
xvfb-run -a -e /dev/stdout ctest -VV
- run:
name: Generate coverage report with lcov
command: lcov --directory . --capture --output-file coverage.info
- run:
name: Upload coverage report to codecov
command: bash <(curl -s https://codecov.io/bash) -f coverage.info
- store_test_results:
path: build/test_shellwidget_output/
3 changes: 3 additions & 0 deletions src/gui/shellwidget/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function(add_xtest SOURCE_NAME)
add_test(NAME ${SOURCE_NAME} COMMAND ${SOURCE_NAME} ${CTEST_EXE_ARGS})
endfunction()

file(TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR} TEST_SOURCE_DIR)
add_definitions(-DTEST_SOURCE_DIR="${TEST_SOURCE_DIR}")

add_xtest(test_cell)
add_xtest(test_shellcontents)
add_xtest(test_shellwidget)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions src/gui/shellwidget/test/test_shellwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QtTest/QtTest>
#include <QPainter>
#include "shellwidget.h"

#if defined(Q_OS_WIN) && defined(USE_STATIC_QT)
Expand All @@ -14,8 +15,33 @@ private slots:
void clearRegion();
void fontDescriptionFromQFont();
void fontDescriptionToQFont();
void render();
private:
QString outputFolderPath();
QString outputFilePath(const QString &name);
QString originalFilePath(const QString &name);
void saveWidgetOutput(ShellWidget &w, QString name);
void diffWidgetOutput(QString name);
};

QString Test::outputFolderPath()
{
auto folderName = "test_shellwidget_output";
QDir().mkpath(folderName);
return folderName;
}

/// Render Output file path to save a file
QString Test::outputFilePath(const QString &name)
{
return QDir(outputFolderPath()).filePath(name);
}

/// Previous output from rendering, from the source test folder
QString Test::originalFilePath(const QString &name)
{
return QDir(QDir(TEST_SOURCE_DIR).filePath("renderoutput")).filePath(name);
}

void Test::clearRegion()
{
Expand Down Expand Up @@ -126,5 +152,86 @@ void Test::fontDescriptionToQFont()
QCOMPARE(varInvalidHeight, QVariant{ QString{ "Invalid font height" } });
}

void Test::saveWidgetOutput(ShellWidget &w, QString name)
{
QImage img(w.size(), QImage::Format_ARGB32);
QPainter painter(&img);
w.render(&painter);
QCOMPARE(img.save(outputFilePath(name)), true);
}

void Test::diffWidgetOutput(QString name)
{
auto p1 = outputFilePath(name);
auto output = QImage(p1);
qDebug() << "Loading" << p1 << output;
auto p2 = originalFilePath(name);
auto expected = QImage(p2);
qDebug() << "Loading" << p2 << expected;

QCOMPARE(output.isNull(), false);
QCOMPARE(expected.isNull(), false);

QImage diff(output.size(), QImage::Format_RGB32);
diff.fill(Qt::white);

bool failed = false;

for(int y=0; y<output.height(); y++){
for(int x=0; x<output.width(); x++){
auto outputColor = output.pixel(x, y);
auto expectedColor = expected.pixel(x, y);

if (outputColor != expectedColor) {
//qWarning() << "Pixel color mismatch at position " << x << y;
//qWarning() << " output:" << outputColor << "expected:" << expectedColor;
diff.setPixel(x, y, Qt::red);
failed = true;
}
}
}

auto outpath = outputFilePath("diff_" + name);
qDebug() << "Saving diff" << outpath;


if (QGuiApplication::platformName() != "xcb") {
auto msg = QString("Skipping render test in non X11: %1")
.arg(QGuiApplication::platformName());
QSKIP(qPrintable(msg));
}

QCOMPARE(diff.save(outpath), true);

QCOMPARE(output.width(), expected.width());
QCOMPARE(output.height(), expected.height());

if (failed) {
qWarning() << "Failing diff, check the output diff image" << outpath;
QCOMPARE(failed, false);
}
}

/// This is mostly an example it renders a shell widget and saves it as an image.
/// For more complicated tests we may want to parametrize these.
void Test::render()
{
ShellWidget w;
w.setShellFont(QFont("Monospace", 11, QFont::Weight::Normal, false));
w.resizeShell(20, 20);
w.show();

w.put("hello", 2, 2, HighlightAttribute());
w.put("fffffff", 3, 3, HighlightAttribute(Qt::red, Qt::black, Qt::white, false, false, false, false, false, false));
w.put("italic text", 4, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, true, false, false, false, false));
w.put("bold text", 5, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, true, false, false, false));
w.put("underline text", 6, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, false, true, false, false));
w.put("undercurl text", 7, 3, HighlightAttribute(Qt::white, Qt::black, Qt::red, false, false, false, false, true, false));

auto name = "shellwidget_render_works.png";
saveWidgetOutput(w, name);
diffWidgetOutput(name);
}

QTEST_MAIN(Test)
#include "test_shellwidget.moc"

0 comments on commit 0e65b3a

Please sign in to comment.