23
23
#include < QPicture>
24
24
#include < QPainter>
25
25
#include < QtConcurrent>
26
+ #include < QElapsedTimer>
26
27
#include " qgssvgcache.h"
28
+ #include " qgsmultirenderchecker.h"
29
+ #include " qgsapplication.h"
27
30
28
31
/* *
29
32
* \ingroup UnitTests
@@ -33,8 +36,11 @@ class TestQgsSvgCache : public QObject
33
36
{
34
37
Q_OBJECT
35
38
36
- public:
37
- TestQgsSvgCache () = default ;
39
+ private:
40
+
41
+ QString mReport ;
42
+
43
+ bool imageCheck ( const QString &testName, QImage &image, int mismatchCount );
38
44
39
45
private slots:
40
46
void initTestCase ();// will be called before the first testfunction is executed.
@@ -44,6 +50,7 @@ class TestQgsSvgCache : public QObject
44
50
void fillCache ();
45
51
void threadSafePicture ();
46
52
void threadSafeImage ();
53
+ void changeImage (); // check that cache is updated if svg source file changes
47
54
48
55
};
49
56
@@ -52,10 +59,22 @@ void TestQgsSvgCache::initTestCase()
52
59
{
53
60
QgsApplication::init ();
54
61
QgsApplication::initQgis ();
62
+ mReport += " <h1>QgsSvgCache Tests</h1>\n " ;
55
63
}
56
64
57
65
void TestQgsSvgCache::cleanupTestCase ()
58
66
{
67
+ QgsApplication::exitQgis ();
68
+
69
+ QString myReportFile = QDir::tempPath () + " /qgistest.html" ;
70
+ QFile myFile ( myReportFile );
71
+ if ( myFile.open ( QIODevice::WriteOnly | QIODevice::Append ) )
72
+ {
73
+ QTextStream myQTextStream ( &myFile );
74
+ myQTextStream << mReport ;
75
+ myFile.close ();
76
+ // QDesktopServices::openUrl( "file:///" + myReportFile );
77
+ }
59
78
}
60
79
61
80
void TestQgsSvgCache::fillCache ()
@@ -155,5 +174,86 @@ void TestQgsSvgCache::threadSafeImage()
155
174
QtConcurrent::blockingMap ( list, RenderImageWrapper ( cache, svgPath ) );
156
175
}
157
176
177
+ void TestQgsSvgCache::changeImage ()
178
+ {
179
+ bool inCache;
180
+ QgsSvgCache cache;
181
+ // no minimum time between checks
182
+ cache.mFileModifiedCheckTimeout = 0 ;
183
+
184
+ // copy an image to the temp folder
185
+ QString tempImagePath = QDir::tempPath () + " /svg_cache.svg" ;
186
+
187
+ QString originalImage = TEST_DATA_DIR + QStringLiteral ( " /test_symbol_svg.svg" );
188
+ if ( QFileInfo::exists ( tempImagePath ) )
189
+ QFile::remove ( tempImagePath );
190
+ QFile::copy ( originalImage, tempImagePath );
191
+
192
+ // render it through the cache
193
+ QImage img = cache.svgAsImage ( tempImagePath, 200 , QColor ( 0 , 0 , 0 ), QColor ( 0 , 0 , 0 ), 1.0 ,
194
+ 1.0 , inCache );
195
+ QVERIFY ( imageCheck ( " svgcache_changed_before" , img, 30 ) );
196
+
197
+ // wait a second so that modified time is different
198
+ QElapsedTimer t;
199
+ t.start ();
200
+ while ( !t.hasExpired ( 1000 ) )
201
+ {}
202
+
203
+ // replace the image in the temp folder
204
+ QString newImage = TEST_DATA_DIR + QStringLiteral ( " /test_symbol_svg2.svg" );
205
+ QFile::remove ( tempImagePath );
206
+ QFile::copy ( newImage, tempImagePath );
207
+
208
+ // re-render it
209
+ img = cache.svgAsImage ( tempImagePath, 200 , QColor ( 0 , 0 , 0 ), QColor ( 0 , 0 , 0 ), 1.0 ,
210
+ 1.0 , inCache );
211
+ QVERIFY ( imageCheck ( " svgcache_changed_after" , img, 30 ) );
212
+
213
+ // repeat, with minimum time between checks
214
+ QgsSvgCache cache2;
215
+ QFile::remove ( tempImagePath );
216
+ QFile::copy ( originalImage, tempImagePath );
217
+ img = cache2.svgAsImage ( tempImagePath, 200 , QColor ( 0 , 0 , 0 ), QColor ( 0 , 0 , 0 ), 1.0 ,
218
+ 1.0 , inCache );
219
+ QVERIFY ( imageCheck ( " svgcache_changed_before" , img, 30 ) );
220
+
221
+ // wait a second so that modified time is different
222
+ t.restart ();
223
+ while ( !t.hasExpired ( 1000 ) )
224
+ {}
225
+
226
+ // replace the image in the temp folder
227
+ QFile::remove ( tempImagePath );
228
+ QFile::copy ( newImage, tempImagePath );
229
+
230
+ // re-render it - not enough time has elapsed between checks, so file modification time will NOT be rechecked and
231
+ // existing cached image should be used
232
+ img = cache2.svgAsImage ( tempImagePath, 200 , QColor ( 0 , 0 , 0 ), QColor ( 0 , 0 , 0 ), 1.0 ,
233
+ 1.0 , inCache );
234
+ QVERIFY ( imageCheck ( " svgcache_changed_before" , img, 30 ) );
235
+ }
236
+
237
+ bool TestQgsSvgCache::imageCheck ( const QString &testName, QImage &image, int mismatchCount )
238
+ {
239
+ // draw background
240
+ QImage imageWithBackground ( image.width (), image.height (), QImage::Format_RGB32 );
241
+ QgsRenderChecker::drawBackground ( &imageWithBackground );
242
+ QPainter painter ( &imageWithBackground );
243
+ painter.drawImage ( 0 , 0 , image );
244
+ painter.end ();
245
+
246
+ mReport += " <h2>" + testName + " </h2>\n " ;
247
+ QString tempDir = QDir::tempPath () + ' /' ;
248
+ QString fileName = tempDir + testName + " .png" ;
249
+ imageWithBackground.save ( fileName, " PNG" );
250
+ QgsRenderChecker checker;
251
+ checker.setControlName ( " expected_" + testName );
252
+ checker.setRenderedImage ( fileName );
253
+ checker.setColorTolerance ( 2 );
254
+ bool resultFlag = checker.compareImages ( testName, mismatchCount );
255
+ mReport += checker.report ();
256
+ return resultFlag;
257
+ }
158
258
QGSTEST_MAIN ( TestQgsSvgCache )
159
259
#include " testqgssvgcache.moc"
0 commit comments