24
24
#include " qgsreadwritecontext.h"
25
25
#include " qgslayoutexporter.h"
26
26
#include " qgsmultirenderchecker.h"
27
+ #include " qgssettings.h"
27
28
28
29
29
30
#include " qgslayoutitemlabel.h"
30
31
#include " qgslayoutitemshape.h"
32
+ #include " qgslayoutitempicture.h"
33
+ #include " qgslayoutitempolygon.h"
34
+ #include " qgslayoutitempolyline.h"
35
+ #include " qgslayoutitemmap.h"
36
+
31
37
32
38
class TestQgsCompositionConverter : public QObject
33
39
{
@@ -54,17 +60,49 @@ class TestQgsCompositionConverter: public QObject
54
60
*/
55
61
void importComposerTemplate ();
56
62
63
+ /* *
64
+ * Test import pictures from a composer template
65
+ */
66
+ void importComposerTemplatePicture ();
67
+
68
+ /* *
69
+ * Test import polygon from a composer template
70
+ */
71
+ void importComposerTemplatePolygon ();
72
+
73
+ /* *
74
+ * Test import polyline from a composer template
75
+ */
76
+ void importComposerTemplatePolyline ();
77
+
78
+ /* *
79
+ * Test import arrow from a composer template
80
+ */
81
+ void importComposerTemplateArrow ();
82
+
83
+ /* *
84
+ * Test import map from a composer template
85
+ */
86
+ void importComposerTemplateMap ();
87
+
57
88
private:
58
89
90
+
91
+ void checkRenderedImage ( QgsLayout *layout, const QString testName, const int pageNumber = 0 );
92
+
59
93
void exportLayout ( QgsLayout *layout, const QString testName );
60
94
95
+ QDomElement loadComposition ( const QString name );
96
+
61
97
QString mReport ;
62
98
63
99
};
64
100
65
101
void TestQgsCompositionConverter::initTestCase ()
66
102
{
67
103
mReport = QStringLiteral ( " <h1>Layout Tests</h1>\n " );
104
+ QgsSettings settings;
105
+ settings.setValue ( QStringLiteral ( " svg/searchPathsForSVG" ), QStringLiteral ( TEST_DATA_DIR ) ) ;
68
106
}
69
107
70
108
void TestQgsCompositionConverter::cleanupTestCase ()
@@ -92,23 +130,10 @@ void TestQgsCompositionConverter::cleanup()
92
130
93
131
void TestQgsCompositionConverter::importComposerTemplateLabel ()
94
132
{
95
- QString templatePath ( QStringLiteral ( TEST_DATA_DIR ) + " /layouts/2x_template_label.qpt" );
96
- QDomDocument doc ( " mydocument" );
97
- QFile file ( templatePath );
98
- QVERIFY ( file.open ( QIODevice::ReadOnly ) );
99
- if ( !doc.setContent ( &file ) )
100
- {
101
- file.close ();
102
- return ;
103
- }
104
- file.close ();
105
-
106
- QDomNodeList nodes ( doc.elementsByTagName ( QStringLiteral ( " Composition" ) ) );
107
- QVERIFY ( nodes.length () > 0 );
108
- QDomElement docElem = nodes.at ( 0 ).toElement ();
109
-
133
+ QDomElement docElem ( loadComposition ( " 2x_template_label.qpt" ) );
110
134
QgsReadWriteContext context;
111
- QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, context );
135
+ QgsProject project;
136
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
112
137
113
138
QVERIFY ( layout );
114
139
QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
@@ -117,7 +142,7 @@ void TestQgsCompositionConverter::importComposerTemplateLabel()
117
142
layout->layoutItems <QgsLayoutItemLabel>( items );
118
143
QVERIFY ( items.size () > 0 );
119
144
120
- exportLayout ( layout, QStringLiteral ( " ComposerTemplateLabel " ) );
145
+ // exportLayout( layout, QTest::currentTestFunction( ) );
121
146
122
147
// Check the label
123
148
const QgsLayoutItemLabel *label = items.at ( 0 );
@@ -132,35 +157,17 @@ void TestQgsCompositionConverter::importComposerTemplateLabel()
132
157
QCOMPARE ( label->frameStrokeWidth ().length (), 0.2 );
133
158
QCOMPARE ( ( int )label->rotation (), 4 );
134
159
135
- /*
136
- QgsCompositionChecker checker( QStringLiteral( "ComposerTemplateLabel" ), composition );
137
- checker.setSize( QSize( 774, 641 ) );
138
- checker.setControlPathPrefix( QStringLiteral( "compositionconverter" ) );
139
- QVERIFY( checker.testComposition( mReport ) );
140
- */
160
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
141
161
142
162
qDeleteAll ( items );
143
163
}
144
164
145
165
void TestQgsCompositionConverter::importComposerTemplateShape ()
146
166
{
147
- QString templatePath ( QStringLiteral ( TEST_DATA_DIR ) + " /layouts/2x_template_shape.qpt" );
148
- QDomDocument doc ( " mydocument" );
149
- QFile file ( templatePath );
150
- QVERIFY ( file.open ( QIODevice::ReadOnly ) );
151
- if ( !doc.setContent ( &file ) )
152
- {
153
- file.close ();
154
- return ;
155
- }
156
- file.close ();
157
-
158
- QDomNodeList nodes ( doc.elementsByTagName ( QStringLiteral ( " Composition" ) ) );
159
- QVERIFY ( nodes.length () > 0 );
160
- QDomElement docElem = nodes.at ( 0 ).toElement ();
161
-
167
+ QDomElement docElem ( loadComposition ( " 2x_template_shape.qpt" ) );
162
168
QgsReadWriteContext context;
163
- QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, context );
169
+ QgsProject project;
170
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
164
171
165
172
QVERIFY ( layout );
166
173
QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
@@ -169,7 +176,7 @@ void TestQgsCompositionConverter::importComposerTemplateShape()
169
176
layout->layoutItems <QgsLayoutItemShape>( items );
170
177
QVERIFY ( items.size () > 0 );
171
178
172
- exportLayout ( layout, QString ( " ComposerTemplateShape " ) );
179
+ // exportLayout( layout, QTest::currentTestFunction( ) );
173
180
174
181
// Check the shape
175
182
const QgsLayoutItemShape *shape = items.at ( 0 );
@@ -185,37 +192,162 @@ void TestQgsCompositionConverter::importComposerTemplateShape()
185
192
QCOMPARE ( shape->hasFrame (), false );
186
193
QCOMPARE ( shape->hasBackground (), false );
187
194
195
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
196
+
188
197
qDeleteAll ( items );
189
198
}
190
199
191
- void TestQgsCompositionConverter::importComposerTemplate ()
200
+ void TestQgsCompositionConverter::importComposerTemplatePicture ()
192
201
{
193
- QString templatePath ( QStringLiteral ( TEST_DATA_DIR ) + " /layouts/2x_template.qpt" );
194
- QDomDocument doc ( " mydocument" );
195
- QFile file ( templatePath );
196
- QVERIFY ( file.open ( QIODevice::ReadOnly ) );
197
- if ( !doc.setContent ( &file ) )
198
- {
199
- file.close ();
200
- return ;
201
- }
202
- file.close ();
202
+ QDomElement docElem ( loadComposition ( " 2x_template_pictures.qpt" ) );
203
+ QVERIFY ( !docElem.isNull () );
204
+ QgsProject project;
205
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
206
+ QVERIFY ( layout );
207
+ QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
203
208
204
- QDomNodeList nodes ( doc.elementsByTagName ( QStringLiteral ( " Composition" ) ) );
205
- QVERIFY ( nodes.length () > 0 );
206
- QDomElement docElem = nodes.at ( 0 ).toElement ();
209
+ QList<QgsLayoutItemPicture *> items;
210
+ layout->layoutItems <QgsLayoutItemPicture>( items );
211
+ QCOMPARE ( items.size (), 1 );
212
+ QVERIFY ( QFile ( items.at ( 0 )->picturePath () ).exists () );
207
213
208
- QgsReadWriteContext context;
209
- QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, context );
214
+ QgsLayoutItemPicture *item = items.at ( 0 );
215
+ QCOMPARE ( item->mPictureHeight , 18.1796 );
216
+ QCOMPARE ( item->mPictureWidth , 18.1796 );
217
+ QCOMPARE ( item->sizeWithUnits ().width (), 25.7099 );
218
+ QCOMPARE ( item->sizeWithUnits ().height (), 30.7511 );
219
+ QCOMPARE ( item->pos ().x (), 207.192 );
220
+ QCOMPARE ( item->pos ().y (), 12.6029 );
221
+ QVERIFY ( item->isVisible () );
222
+
223
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
224
+
225
+ qDeleteAll ( items );
226
+
227
+ }
228
+
229
+ void TestQgsCompositionConverter::importComposerTemplatePolygon ()
230
+ {
231
+ QDomElement docElem ( loadComposition ( " 2x_template_polygon.qpt" ) );
232
+ QVERIFY ( !docElem.isNull () );
233
+ QgsProject project;
234
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
235
+ QVERIFY ( layout );
236
+ QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
237
+
238
+ QList<QgsLayoutItemPolygon *> items;
239
+ layout->layoutItems <QgsLayoutItemPolygon>( items );
240
+ QCOMPARE ( items.size (), 1 );
241
+
242
+ QgsLayoutItemPolygon *item = items.at ( 0 );
243
+ QVERIFY ( item->isVisible () );
244
+ QCOMPARE ( item->nodes ().count (), 7 );
245
+
246
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
247
+
248
+ qDeleteAll ( items );
249
+
250
+ }
251
+
252
+ void TestQgsCompositionConverter::importComposerTemplatePolyline ()
253
+ {
254
+ QDomElement docElem ( loadComposition ( " 2x_template_polyline.qpt" ) );
255
+ QVERIFY ( !docElem.isNull () );
256
+ QgsProject project;
257
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
258
+ QVERIFY ( layout );
259
+ QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
260
+
261
+ QList<QgsLayoutItemPolyline *> items;
262
+ layout->layoutItems <QgsLayoutItemPolyline>( items );
263
+ QCOMPARE ( items.size (), 1 );
264
+
265
+ QgsLayoutItemPolyline *item = items.at ( 0 );
266
+ QVERIFY ( item->isVisible () );
267
+ QCOMPARE ( item->nodes ().count (), 4 );
268
+ QCOMPARE ( item->startMarker (), QgsLayoutItemPolyline::MarkerMode::NoMarker );
269
+ QCOMPARE ( item->endMarker (), QgsLayoutItemPolyline::MarkerMode::NoMarker );
270
+ // QCOMPARE( item->nodes().at(0), QPointF( 266.622, 371.215) );
271
+ // QCOMPARE( item->nodes().at(1), QPointF( 261.581, 296.606) );
272
+
273
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
274
+
275
+ qDeleteAll ( items );
276
+
277
+ }
278
+
279
+ void TestQgsCompositionConverter::importComposerTemplateArrow ()
280
+ {
281
+ QDomElement docElem ( loadComposition ( " 2x_template_arrow.qpt" ) );
282
+ QVERIFY ( !docElem.isNull () );
283
+ QgsProject project;
284
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
285
+ QVERIFY ( layout );
286
+ QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
287
+
288
+ QList<QgsLayoutItemPolyline *> items;
289
+ layout->layoutItems <QgsLayoutItemPolyline>( items );
290
+ QCOMPARE ( items.size (), 1 );
291
+
292
+ QgsLayoutItemPolyline *item = items.at ( 0 );
293
+ QVERIFY ( item->isVisible () );
294
+ QCOMPARE ( item->nodes ().count (), 2 );
295
+ QCOMPARE ( item->startMarker (), QgsLayoutItemPolyline::MarkerMode::NoMarker );
296
+ QCOMPARE ( item->endMarker (), QgsLayoutItemPolyline::MarkerMode::ArrowHead );
297
+
298
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
299
+
300
+ qDeleteAll ( items );
301
+
302
+ }
303
+
304
+
305
+ void TestQgsCompositionConverter::importComposerTemplateMap ()
306
+ {
307
+ QDomElement docElem ( loadComposition ( " 2x_template_map_overview.qpt" ) );
308
+ QVERIFY ( !docElem.isNull () );
309
+ QgsProject project;
310
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
311
+ QVERIFY ( layout );
312
+ QCOMPARE ( layout->pageCollection ()->pageCount (), 1 );
313
+
314
+ QList<QgsLayoutItemMap *> items;
315
+ layout->layoutItems <QgsLayoutItemMap>( items );
316
+ QCOMPARE ( items.size (), 2 );
317
+
318
+ QgsLayoutItemMap *item = items.at ( 0 );
319
+ QVERIFY ( item->isVisible () );
320
+
321
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
322
+
323
+ qDeleteAll ( items );
324
+
325
+ }
326
+
327
+ void TestQgsCompositionConverter::importComposerTemplate ()
328
+ {
329
+ QDomElement docElem ( loadComposition ( " 2x_template.qpt" ) );
330
+ QgsProject project;
331
+ QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml ( docElem, &project );
210
332
211
333
QVERIFY ( layout );
212
334
QCOMPARE ( layout->pageCollection ()->pageCount (), 2 );
213
335
336
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 0 );
337
+ checkRenderedImage ( layout, QTest::currentTestFunction (), 1 );
214
338
215
- exportLayout ( layout, QString ( " ComposerTemplate" ) );
216
339
delete layout;
217
340
}
218
341
342
+ void TestQgsCompositionConverter::checkRenderedImage ( QgsLayout *layout, const QString testName, const int pageNumber )
343
+ {
344
+ QgsLayoutChecker checker ( testName + " _" + QString::number ( pageNumber ), layout );
345
+ QSize size ( layout->pageCollection ()->page ( pageNumber )->sizeWithUnits ().width () * 3.77 , layout->pageCollection ()->page ( pageNumber )->sizeWithUnits ().height () * 3.77 );
346
+ checker.setSize ( size );
347
+ checker.setControlPathPrefix ( QStringLiteral ( " compositionconverter" ) );
348
+ QVERIFY ( checker.testLayout ( mReport , pageNumber ) );
349
+ }
350
+
219
351
void TestQgsCompositionConverter::exportLayout ( QgsLayout *layout, const QString testName )
220
352
{
221
353
// Save the template for inspection
@@ -247,6 +379,24 @@ void TestQgsCompositionConverter::exportLayout( QgsLayout *layout, const QString
247
379
}
248
380
}
249
381
382
+ QDomElement TestQgsCompositionConverter::loadComposition ( const QString name )
383
+ {
384
+ QString templatePath ( QStringLiteral ( TEST_DATA_DIR ) + " /layouts/" + name );
385
+ QDomDocument doc ( " mydocument" );
386
+ QFile file ( templatePath );
387
+ file.open ( QIODevice::ReadOnly );
388
+ doc.setContent ( &file );
389
+ file.close ();
390
+ QDomNodeList nodes ( doc.elementsByTagName ( QStringLiteral ( " Composition" ) ) );
391
+ if ( nodes.length () > 0 )
392
+ return nodes.at ( 0 ).toElement ();
393
+ else
394
+ {
395
+ QDomElement elem;
396
+ return elem;
397
+ }
398
+ }
399
+
250
400
251
401
QGSTEST_MAIN ( TestQgsCompositionConverter )
252
402
#include " testqgscompositionconverter.moc"
0 commit comments