Skip to content

Commit 0e65b3a

Browse files
author
equalsraf
committed
Add test for output rendering
For now this is mostly an example - but it seems useful to have the ability to test rendering output.
1 parent 1f73d63 commit 0e65b3a

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

.circleci/config.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ jobs:
2323
pyenv global system
2424
mkdir build
2525
cd build
26-
cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_GCOV=ON -DPYTHON_EXECUTABLE=/usr/bin/python3 ..
26+
cmake -DCMAKE_BUILD_TYPE=Debug -DUSE_GCOV=ON -DPYTHON_EXECUTABLE=/usr/bin/python3 -DENABLE_TESTS=ON ..
2727
cmake --build . --target bindings -- VERBOSE=1
2828
cmake --build . -- VERBOSE=1
2929
- run:
3030
name: test
3131
command: |
3232
cd build
33-
ctest -VV
33+
xvfb-run -a -e /dev/stdout ctest -VV
3434
- run:
3535
name: Generate coverage report with lcov
3636
command: lcov --directory . --capture --output-file coverage.info
3737
- run:
3838
name: Upload coverage report to codecov
3939
command: bash <(curl -s https://codecov.io/bash) -f coverage.info
40+
- store_test_results:
41+
path: build/test_shellwidget_output/

src/gui/shellwidget/test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ function(add_xtest SOURCE_NAME)
1414
add_test(NAME ${SOURCE_NAME} COMMAND ${SOURCE_NAME} ${CTEST_EXE_ARGS})
1515
endfunction()
1616

17+
file(TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR} TEST_SOURCE_DIR)
18+
add_definitions(-DTEST_SOURCE_DIR="${TEST_SOURCE_DIR}")
19+
1720
add_xtest(test_cell)
1821
add_xtest(test_shellcontents)
1922
add_xtest(test_shellwidget)
6.67 KB
Loading

src/gui/shellwidget/test/test_shellwidget.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <QtTest/QtTest>
2+
#include <QPainter>
23
#include "shellwidget.h"
34

45
#if defined(Q_OS_WIN) && defined(USE_STATIC_QT)
@@ -14,8 +15,33 @@ private slots:
1415
void clearRegion();
1516
void fontDescriptionFromQFont();
1617
void fontDescriptionToQFont();
18+
void render();
19+
private:
20+
QString outputFolderPath();
21+
QString outputFilePath(const QString &name);
22+
QString originalFilePath(const QString &name);
23+
void saveWidgetOutput(ShellWidget &w, QString name);
24+
void diffWidgetOutput(QString name);
1725
};
1826

27+
QString Test::outputFolderPath()
28+
{
29+
auto folderName = "test_shellwidget_output";
30+
QDir().mkpath(folderName);
31+
return folderName;
32+
}
33+
34+
/// Render Output file path to save a file
35+
QString Test::outputFilePath(const QString &name)
36+
{
37+
return QDir(outputFolderPath()).filePath(name);
38+
}
39+
40+
/// Previous output from rendering, from the source test folder
41+
QString Test::originalFilePath(const QString &name)
42+
{
43+
return QDir(QDir(TEST_SOURCE_DIR).filePath("renderoutput")).filePath(name);
44+
}
1945

2046
void Test::clearRegion()
2147
{
@@ -126,5 +152,86 @@ void Test::fontDescriptionToQFont()
126152
QCOMPARE(varInvalidHeight, QVariant{ QString{ "Invalid font height" } });
127153
}
128154

155+
void Test::saveWidgetOutput(ShellWidget &w, QString name)
156+
{
157+
QImage img(w.size(), QImage::Format_ARGB32);
158+
QPainter painter(&img);
159+
w.render(&painter);
160+
QCOMPARE(img.save(outputFilePath(name)), true);
161+
}
162+
163+
void Test::diffWidgetOutput(QString name)
164+
{
165+
auto p1 = outputFilePath(name);
166+
auto output = QImage(p1);
167+
qDebug() << "Loading" << p1 << output;
168+
auto p2 = originalFilePath(name);
169+
auto expected = QImage(p2);
170+
qDebug() << "Loading" << p2 << expected;
171+
172+
QCOMPARE(output.isNull(), false);
173+
QCOMPARE(expected.isNull(), false);
174+
175+
QImage diff(output.size(), QImage::Format_RGB32);
176+
diff.fill(Qt::white);
177+
178+
bool failed = false;
179+
180+
for(int y=0; y<output.height(); y++){
181+
for(int x=0; x<output.width(); x++){
182+
auto outputColor = output.pixel(x, y);
183+
auto expectedColor = expected.pixel(x, y);
184+
185+
if (outputColor != expectedColor) {
186+
//qWarning() << "Pixel color mismatch at position " << x << y;
187+
//qWarning() << " output:" << outputColor << "expected:" << expectedColor;
188+
diff.setPixel(x, y, Qt::red);
189+
failed = true;
190+
}
191+
}
192+
}
193+
194+
auto outpath = outputFilePath("diff_" + name);
195+
qDebug() << "Saving diff" << outpath;
196+
197+
198+
if (QGuiApplication::platformName() != "xcb") {
199+
auto msg = QString("Skipping render test in non X11: %1")
200+
.arg(QGuiApplication::platformName());
201+
QSKIP(qPrintable(msg));
202+
}
203+
204+
QCOMPARE(diff.save(outpath), true);
205+
206+
QCOMPARE(output.width(), expected.width());
207+
QCOMPARE(output.height(), expected.height());
208+
209+
if (failed) {
210+
qWarning() << "Failing diff, check the output diff image" << outpath;
211+
QCOMPARE(failed, false);
212+
}
213+
}
214+
215+
/// This is mostly an example it renders a shell widget and saves it as an image.
216+
/// For more complicated tests we may want to parametrize these.
217+
void Test::render()
218+
{
219+
ShellWidget w;
220+
w.setShellFont(QFont("Monospace", 11, QFont::Weight::Normal, false));
221+
w.resizeShell(20, 20);
222+
w.show();
223+
224+
w.put("hello", 2, 2, HighlightAttribute());
225+
w.put("fffffff", 3, 3, HighlightAttribute(Qt::red, Qt::black, Qt::white, false, false, false, false, false, false));
226+
w.put("italic text", 4, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, true, false, false, false, false));
227+
w.put("bold text", 5, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, true, false, false, false));
228+
w.put("underline text", 6, 3, HighlightAttribute(Qt::white, Qt::black, Qt::white, false, false, false, true, false, false));
229+
w.put("undercurl text", 7, 3, HighlightAttribute(Qt::white, Qt::black, Qt::red, false, false, false, false, true, false));
230+
231+
auto name = "shellwidget_render_works.png";
232+
saveWidgetOutput(w, name);
233+
diffWidgetOutput(name);
234+
}
235+
129236
QTEST_MAIN(Test)
130237
#include "test_shellwidget.moc"

0 commit comments

Comments
 (0)