-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtestqgslayouthtml.cpp
267 lines (224 loc) · 11.1 KB
/
testqgslayouthtml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/***************************************************************************
testqgslayouthtml.cpp
-----------------------
begin : October 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsapplication.h"
#include "qgslayoutitemhtml.h"
#include "qgslayoutframe.h"
#include "qgsfontutils.h"
#include "qgsvectorlayer.h"
#include "qgsrelationmanager.h"
#include "qgsvectordataprovider.h"
#include "qgsproject.h"
#include "qgslayout.h"
#include "qgslayoutreportcontext.h"
#include <QObject>
#include "qgstest.h"
class TestQgsLayoutHtml : public QgsTest
{
Q_OBJECT
public:
TestQgsLayoutHtml() : QgsTest( QStringLiteral( "Layout HTML Tests" ), QStringLiteral( "composer_html" ) ) {}
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void sourceMode(); //test if rendering manual HTML works
void userStylesheets(); //test if user stylesheets work
void evalExpressions(); //test if rendering with expressions works
void evalExpressionsOff(); //test if rendering with expressions disabled works
void table(); //test if rendering a HTML url works
void tableMultiFrame(); //tests multiframe capabilities of composer html
void htmlMultiFrameSmartBreak(); //tests smart page breaks in html multi frame
void javascriptSetFeature(); //test that JavaScript setFeature() function is correctly called
private:
QFont mTestFont;
};
void TestQgsLayoutHtml::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsFontUtils::loadStandardTestFonts( QStringList() << QStringLiteral( "Oblique" ) );
mTestFont = QgsFontUtils::getStandardTestFont( QStringLiteral( "Oblique " ) );
}
void TestQgsLayoutHtml::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsLayoutHtml::sourceMode()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) );
htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
htmlItem->setHtml( QStringLiteral( "<body style=\"margin: 10px;\"><div style=\"width: 100px; height: 50px; background-color: red;\"></div></body>" ) );
htmlItem->loadHtml();
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_manual" ), &l, 0, 100 );
}
void TestQgsLayoutHtml::userStylesheets()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) );
htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
htmlItem->setHtml( QStringLiteral( "<body style=\"margin: 10px;\"><div style=\"width: 100px; height: 50px; background-color: red;\"></div></body>" ) );
//set user stylesheet
htmlItem->setUserStylesheet( QStringLiteral( "div { background-color: green !important; }" ) );
//setting user stylesheet enabled automatically loads html
htmlItem->setUserStylesheetEnabled( true );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_userstylesheet" ), &l, 0, 100 );
}
void TestQgsLayoutHtml::evalExpressions()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) );
htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
htmlItem->setEvaluateExpressions( true );
htmlItem->setHtml( QStringLiteral( "<body style=\"margin: 10px;\"><div style=\"width: [% 10 * 10 %]px; height: [% 30 + 20 %]px; background-color: [% 'yel' || 'low' %];\"></div></body>" ) );
htmlItem->loadHtml();
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_expressions_enabled" ), &l );
}
void TestQgsLayoutHtml::evalExpressionsOff()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) ); htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
htmlItem->setEvaluateExpressions( false );
htmlItem->setHtml( QStringLiteral( "<body style=\"margin: 10px;\"><div style=\"width: [% 10 * 10 %]px; height: [% 30 + 20 %]px; background-color: [% 'yel' || 'low' %];\"></div></body>" ) );
htmlItem->loadHtml();
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_expressions_disabled" ), &l );
}
void TestQgsLayoutHtml::table()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) ); htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setUrl( QUrl( QStringLiteral( "file:///%1/test_html.html" ).arg( TEST_DATA_DIR ) ) );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_table" ), &l );
}
void TestQgsLayoutHtml::tableMultiFrame()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 10, 10, 100, 50 ) );
htmlItem->addFrame( htmlFrame );
htmlItem->setResizeMode( QgsLayoutMultiFrame::RepeatUntilFinished );
htmlItem->setUseSmartBreaks( false );
//page1
htmlItem->setUrl( QUrl( QStringLiteral( "file:///%1/test_html.html" ).arg( TEST_DATA_DIR ) ) );
htmlItem->frame( 0 )->setFrameEnabled( true );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_multiframe1" ), &l );
//page2
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_multiframe2" ), &l, 1 );
}
void TestQgsLayoutHtml::htmlMultiFrameSmartBreak()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 10, 10, 100, 52 ) );
htmlItem->addFrame( htmlFrame );
htmlItem->setResizeMode( QgsLayoutMultiFrame::RepeatUntilFinished );
htmlItem->setUseSmartBreaks( true );
//page1
htmlItem->setUrl( QUrl( QStringLiteral( "file:///%1/test_html.html" ).arg( TEST_DATA_DIR ) ) );
htmlItem->frame( 0 )->setFrameEnabled( true );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_smartbreaks1" ), &l, 0, 200 );
//page2
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_smartbreaks2" ), &l, 1, 200 );
}
void TestQgsLayoutHtml::javascriptSetFeature()
{
//test that JavaScript setFeature() function is correctly called
// first need to setup some layers with a relation
//parent layer
QgsVectorLayer *parentLayer = new QgsVectorLayer( QStringLiteral( "Point?field=fldtxt:string&field=fldint:integer&field=foreignkey:integer" ), QStringLiteral( "parent" ), QStringLiteral( "memory" ) );
QgsVectorDataProvider *pr = parentLayer->dataProvider();
QgsFeature pf1;
pf1.setFields( parentLayer->fields() );
pf1.setAttributes( QgsAttributes() << "test1" << 67 << 123 );
QgsFeature pf2;
pf2.setFields( parentLayer->fields() );
pf2.setAttributes( QgsAttributes() << "test2" << 68 << 124 );
QVERIFY( pr->addFeatures( QgsFeatureList() << pf1 << pf2 ) );
// child layer
QgsVectorLayer *childLayer = new QgsVectorLayer( QStringLiteral( "Point?field=x:string&field=y:integer&field=z:integer" ), QStringLiteral( "referencedlayer" ), QStringLiteral( "memory" ) );
pr = childLayer->dataProvider();
QgsFeature f1;
f1.setFields( childLayer->fields() );
f1.setAttributes( QgsAttributes() << "foo" << 123 << 321 );
QgsFeature f2;
f2.setFields( childLayer->fields() );
f2.setAttributes( QgsAttributes() << "bar" << 123 << 654 );
QgsFeature f3;
f3.setFields( childLayer->fields() );
f3.setAttributes( QgsAttributes() << "foobar" << 124 << 554 );
QVERIFY( pr->addFeatures( QgsFeatureList() << f1 << f2 << f3 ) );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << childLayer << parentLayer );
//atlas
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
l.reportContext().setLayer( parentLayer );
QgsRelation rel;
rel.setId( QStringLiteral( "rel1" ) );
rel.setName( QStringLiteral( "relation one" ) );
rel.setReferencingLayer( childLayer->id() );
rel.setReferencedLayer( parentLayer->id() );
rel.addFieldPair( QStringLiteral( "y" ), QStringLiteral( "foreignkey" ) );
QgsProject::instance()->relationManager()->addRelation( rel );
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( &l );
QgsLayoutFrame *htmlFrame = new QgsLayoutFrame( &l, htmlItem );
htmlFrame->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) );
htmlFrame->setFrameEnabled( true );
htmlItem->addFrame( htmlFrame );
htmlItem->setContentMode( QgsLayoutItemHtml::ManualHtml );
htmlItem->setEvaluateExpressions( true );
// hopefully arial bold 40px is big enough to avoid cross-platform rendering issues
htmlItem->setHtml( QString( "<body style=\"margin: 10px; font-family: Arial; font-weight: bold; font-size: 40px;\">"
"<div id=\"dest\"></div><script>setFeature=function(feature){"
"document.getElementById('dest').innerHTML = feature.properties.foreignkey + ',' +"
" feature.properties['relation one'][0].z + ',' + feature.properties['relation one'][1].z;}"
"</script></body>" ) );
QgsFeature f;
QgsFeatureIterator it = parentLayer->getFeatures();
it.nextFeature( f );
l.reportContext().setFeature( f );
htmlItem->loadHtml();
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerhtml_setfeature" ), &l );
QgsProject::instance()->removeMapLayers( QList<QgsMapLayer *>() << childLayer << parentLayer );
}
QGSTEST_MAIN( TestQgsLayoutHtml )
#include "testqgslayouthtml.moc"