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 May 10, 2020
1 parent b615c74 commit 87742cd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gui/shellwidget/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function(add_xtest SOURCE_NAME)
add_test(NAME ${SOURCE_NAME} COMMAND ${SOURCE_NAME})
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.
98 changes: 98 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 @@ -12,8 +13,33 @@ class Test: public QObject

private slots:
void clearRegion();
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 = "out";
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 All @@ -22,5 +48,77 @@ void Test::clearRegion()
w->resizeShell(2, 2);
}

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);

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

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.pixelColor(x, y);
auto expectedColor = expected.pixelColor(x, y);

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

auto outpath = outputFilePath("diff_" + name);
qDebug() << "Saving diff" << outpath;
QCOMPARE(diff.save(outpath), true);

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.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));
w.put("italic text", 4, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, true, false, false, false));
w.put("bold text", 5, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, true, false, false));
w.put("underline text", 6, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, false, true, false));
w.put("undercurl text", 7, 3, HighlightAttribute(Qt::white, Qt::black, Qt::red, false, false, false, false, true));

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

QTEST_MAIN(Test)
#include "test_shellwidget.moc"

0 comments on commit 87742cd

Please sign in to comment.