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
2046void 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+
129236QTEST_MAIN (Test)
130237#include " test_shellwidget.moc"
0 commit comments