21
21
#include " qgscomposition.h"
22
22
#include " qgsmultirenderchecker.h"
23
23
#include " qgsfontutils.h"
24
+ #include " qgsvectorlayer.h"
25
+ #include " qgsmaplayerregistry.h"
26
+ #include " qgsvectordataprovider.h"
27
+ #include " qgsproject.h"
24
28
#include < QObject>
25
29
#include < QtTest/QtTest>
26
30
@@ -43,6 +47,8 @@ class TestQgsComposerHtml : public QObject
43
47
void table (); // test if rendering a HTML url works
44
48
void tableMultiFrame (); // tests multiframe capabilities of composer html
45
49
void htmlMultiFrameSmartBreak (); // tests smart page breaks in html multi frame
50
+ void javascriptSetFeature (); // test that JavaScript setFeature() function is correctly called
51
+
46
52
private:
47
53
QgsComposition *mComposition ;
48
54
QgsMapSettings *mMapSettings ;
@@ -243,6 +249,80 @@ void TestQgsComposerHtml::htmlMultiFrameSmartBreak()
243
249
QVERIFY ( result );
244
250
}
245
251
252
+ void TestQgsComposerHtml::javascriptSetFeature ()
253
+ {
254
+ // test that JavaScript setFeature() function is correctly called
255
+
256
+ // first need to setup some layers with a relation
257
+
258
+ // parent layer
259
+ QgsVectorLayer* parentLayer = new QgsVectorLayer ( " Point?field=fldtxt:string&field=fldint:integer&field=foreignkey:integer" , " parent" , " memory" );
260
+ QgsVectorDataProvider* pr = parentLayer->dataProvider ();
261
+ QgsFeature pf1;
262
+ pf1.setFields ( parentLayer->fields () );
263
+ pf1.setAttributes ( QgsAttributes () << " test1" << 67 << 123 );
264
+ QgsFeature pf2;
265
+ pf2.setFields ( parentLayer->fields () );
266
+ pf2.setAttributes ( QgsAttributes () << " test2" << 68 << 124 );
267
+ QVERIFY ( pr->addFeatures ( QgsFeatureList () << pf1 << pf2 ) );
268
+
269
+ // child layer
270
+ QgsVectorLayer* childLayer = new QgsVectorLayer ( " Point?field=x:string&field=y:integer&field=z:integer" , " referencedlayer" , " memory" );
271
+ pr = childLayer->dataProvider ();
272
+ QgsFeature f1;
273
+ f1.setFields ( childLayer->fields () );
274
+ f1.setAttributes ( QgsAttributes () << " foo" << 123 << 321 );
275
+ QgsFeature f2;
276
+ f2.setFields ( childLayer->fields () );
277
+ f2.setAttributes ( QgsAttributes () << " bar" << 123 << 654 );
278
+ QgsFeature f3;
279
+ f3.setFields ( childLayer->fields () );
280
+ f3.setAttributes ( QgsAttributes () << " foobar" << 124 << 554 );
281
+ QVERIFY ( pr->addFeatures ( QgsFeatureList () << f1 << f2 << f3 ) );
282
+
283
+ QgsMapLayerRegistry::instance ()->addMapLayers ( QList<QgsMapLayer *>() << childLayer << parentLayer );
284
+
285
+ // atlas
286
+ mComposition ->atlasComposition ().setCoverageLayer ( parentLayer );
287
+ mComposition ->atlasComposition ().setEnabled ( true );
288
+
289
+ QgsRelation rel;
290
+ rel.setRelationId ( " rel1" );
291
+ rel.setRelationName ( " relation one" );
292
+ rel.setReferencingLayer ( childLayer->id () );
293
+ rel.setReferencedLayer ( parentLayer->id () );
294
+ rel.addFieldPair ( " y" , " foreignkey" );
295
+ QgsProject::instance ()->relationManager ()->addRelation ( rel );
296
+
297
+ QgsComposerHtml* htmlItem = new QgsComposerHtml ( mComposition , false );
298
+ QgsComposerFrame* htmlFrame = new QgsComposerFrame ( mComposition , htmlItem, 0 , 0 , 100 , 200 );
299
+ htmlFrame->setFrameEnabled ( true );
300
+ htmlItem->addFrame ( htmlFrame );
301
+ htmlItem->setContentMode ( QgsComposerHtml::ManualHtml );
302
+ htmlItem->setEvaluateExpressions ( true );
303
+ // hopefully arial bold 40px is big enough to avoid cross-platform rendering issues
304
+ htmlItem->setHtml ( QString ( " <body style=\" margin: 10px; font-family: Arial; font-weight: bold; font-size: 40px;\" >"
305
+ " <div id=\" dest\" ></div><script>setFeature=function(feature){"
306
+ " document.getElementById('dest').innerHTML = feature.properties.foreignkey + ',' +"
307
+ " feature.properties['relation one'][0].z + ',' + feature.properties['relation one'][1].z;}"
308
+ " </script></body>" ) );
309
+
310
+ mComposition ->setAtlasMode ( QgsComposition::ExportAtlas );
311
+ QVERIFY ( mComposition ->atlasComposition ().beginRender () );
312
+ QVERIFY ( mComposition ->atlasComposition ().prepareForFeature ( 0 ) );
313
+
314
+ htmlItem->loadHtml ();
315
+
316
+ QgsCompositionChecker checker ( " composerhtml_setfeature" , mComposition );
317
+ checker.setControlPathPrefix ( " composer_html" );
318
+ bool result = checker.testComposition ( mReport );
319
+ mComposition ->removeMultiFrame ( htmlItem );
320
+ delete htmlItem;
321
+ QVERIFY ( result );
322
+
323
+ QgsMapLayerRegistry::instance ()->removeMapLayers ( QList<QgsMapLayer *>() << childLayer << parentLayer );
324
+ }
325
+
246
326
247
327
QTEST_MAIN ( TestQgsComposerHtml )
248
328
#include " testqgscomposerhtml.moc"
0 commit comments