/
testqgslayoutcontext.cpp
277 lines (230 loc) · 9.6 KB
/
testqgslayoutcontext.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
268
269
270
271
272
273
274
275
276
277
/***************************************************************************
testqgslayoutcontext.cpp
------------------------
begin : November 2014
copyright : (C) 2014 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 "qgslayoutrendercontext.h"
#include "qgslayoutreportcontext.h"
#include "qgis.h"
#include "qgsfeature.h"
#include "qgsvectorlayer.h"
#include "qgsgeometry.h"
#include "qgsproject.h"
#include "qgslayout.h"
#include "qgsexpressionutils.h"
#include <QObject>
#include "qgstest.h"
#include <QtTest/QSignalSpy>
class TestQgsLayoutContext: public QObject
{
Q_OBJECT
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 init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void creation(); //test creation of QgsLayout
void flags(); //test QgsLayout flags
void feature();
void layer();
void dpi();
void renderContextFlags();
void textFormat();
void boundingBoxes();
void exportLayer();
void geometry();
void scales();
private:
QString mReport;
};
void TestQgsLayoutContext::initTestCase()
{
mReport = QStringLiteral( "<h1>Layout Context Tests</h1>\n" );
}
void TestQgsLayoutContext::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}
void TestQgsLayoutContext::init()
{
}
void TestQgsLayoutContext::cleanup()
{
}
void TestQgsLayoutContext::creation()
{
QgsLayoutRenderContext *context = new QgsLayoutRenderContext( nullptr );
QVERIFY( context );
delete context;
}
void TestQgsLayoutContext::flags()
{
QgsLayoutRenderContext context( nullptr );
QSignalSpy spyFlagsChanged( &context, &QgsLayoutRenderContext::flagsChanged );
//test getting and setting flags
context.setFlags( QgsLayoutRenderContext::Flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects ) );
// default flags, so should be no signal
QCOMPARE( spyFlagsChanged.count(), 0 );
QVERIFY( context.flags() == ( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects ) );
QVERIFY( context.testFlag( QgsLayoutRenderContext::FlagAntialiasing ) );
QVERIFY( context.testFlag( QgsLayoutRenderContext::FlagUseAdvancedEffects ) );
QVERIFY( ! context.testFlag( QgsLayoutRenderContext::FlagDebug ) );
context.setFlag( QgsLayoutRenderContext::FlagDebug );
QCOMPARE( spyFlagsChanged.count(), 1 );
QVERIFY( context.testFlag( QgsLayoutRenderContext::FlagDebug ) );
context.setFlag( QgsLayoutRenderContext::FlagDebug, false );
QCOMPARE( spyFlagsChanged.count(), 2 );
QVERIFY( ! context.testFlag( QgsLayoutRenderContext::FlagDebug ) );
context.setFlag( QgsLayoutRenderContext::FlagDebug, false ); //no change
QCOMPARE( spyFlagsChanged.count(), 2 );
context.setFlags( QgsLayoutRenderContext::FlagDebug );
QCOMPARE( spyFlagsChanged.count(), 3 );
}
void TestQgsLayoutContext::feature()
{
QgsLayoutReportContext context( nullptr );
//test removing feature
context.setFeature( QgsFeature() );
QVERIFY( !context.feature().isValid() );
//test setting/getting feature
QgsFeature testFeature;
testFeature.initAttributes( 1 );
testFeature.setAttribute( 0, "Test" );
context.setFeature( testFeature );
QCOMPARE( context.feature().attribute( 0 ), testFeature.attribute( 0 ) );
}
void TestQgsLayoutContext::layer()
{
QgsLayoutReportContext context( nullptr );
//test clearing layer
context.setLayer( nullptr );
QVERIFY( !context.layer() );
//test setting/getting layer
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
context.setLayer( layer );
QCOMPARE( context.layer(), layer );
//clear layer
context.setLayer( nullptr );
QVERIFY( !context.layer() );
QgsLayout l( QgsProject::instance() );
l.reportContext().setLayer( layer );
//test that expression context created for layout contains report context layer scope
QgsExpressionContext expContext = l.createExpressionContext();
QCOMPARE( QgsExpressionUtils::getVectorLayer( expContext.variable( "layer" ), nullptr ), layer );
delete layer;
}
void TestQgsLayoutContext::dpi()
{
QgsLayoutRenderContext context( nullptr );
QSignalSpy spyDpiChanged( &context, &QgsLayoutRenderContext::dpiChanged );
context.setDpi( 600 );
QCOMPARE( context.dpi(), 600.0 );
QCOMPARE( context.measurementConverter().dpi(), 600.0 );
QCOMPARE( spyDpiChanged.count(), 1 );
context.setDpi( 600 );
QCOMPARE( spyDpiChanged.count(), 1 );
context.setDpi( 6000 );
QCOMPARE( spyDpiChanged.count(), 2 );
}
void TestQgsLayoutContext::renderContextFlags()
{
QgsLayoutRenderContext context( nullptr );
context.setFlags( nullptr );
QgsRenderContext::Flags flags = context.renderContextFlags();
QVERIFY( !( flags & QgsRenderContext::Antialiasing ) );
QVERIFY( !( flags & QgsRenderContext::UseAdvancedEffects ) );
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
context.setFlag( QgsLayoutRenderContext::FlagAntialiasing );
flags = context.renderContextFlags();
QVERIFY( ( flags & QgsRenderContext::Antialiasing ) );
QVERIFY( !( flags & QgsRenderContext::UseAdvancedEffects ) );
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
context.setFlag( QgsLayoutRenderContext::FlagUseAdvancedEffects );
flags = context.renderContextFlags();
QVERIFY( ( flags & QgsRenderContext::Antialiasing ) );
QVERIFY( ( flags & QgsRenderContext::UseAdvancedEffects ) );
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
}
void TestQgsLayoutContext::textFormat()
{
QgsLayoutRenderContext context( nullptr );
context.setTextRenderFormat( QgsRenderContext::TextFormatAlwaysOutlines );
QCOMPARE( context.textRenderFormat(), QgsRenderContext::TextFormatAlwaysOutlines );
context.setTextRenderFormat( QgsRenderContext::TextFormatAlwaysText );
QCOMPARE( context.textRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
}
void TestQgsLayoutContext::boundingBoxes()
{
QgsLayoutRenderContext context( nullptr );
context.setBoundingBoxesVisible( false );
QVERIFY( !context.boundingBoxesVisible() );
context.setBoundingBoxesVisible( true );
QVERIFY( context.boundingBoxesVisible() );
}
void TestQgsLayoutContext::exportLayer()
{
QgsLayoutRenderContext context( nullptr );
// must default to -1
QCOMPARE( context.currentExportLayer(), -1 );
context.setCurrentExportLayer( 1 );
QCOMPARE( context.currentExportLayer(), 1 );
}
void TestQgsLayoutContext::geometry()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutReportContext context( &l );
// no feature set
QVERIFY( context.currentGeometry().isNull() );
QVERIFY( context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).isNull() );
// no layer set
QgsFeature f;
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString( 144 -38, 145 -39 )" ) ) );
context.setFeature( f );
QCOMPARE( context.currentGeometry().asWkt(), f.geometry().asWkt() );
QVERIFY( context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).isNull() );
//with layer
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?crs=EPSG:4326&field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
context.setLayer( layer );
QCOMPARE( context.currentGeometry().asWkt(), f.geometry().asWkt() );
QVERIFY( !context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).isNull() );
QCOMPARE( context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).asWkt( 0 ), QStringLiteral( "LineString (2412169 2388563, 2500000 2277996)" ) );
// should be cached
QCOMPARE( context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).asWkt( 0 ), QStringLiteral( "LineString (2412169 2388563, 2500000 2277996)" ) );
// layer crs
QCOMPARE( context.currentGeometry( layer->crs() ).asWkt(), f.geometry().asWkt() );
// clear cache
QgsFeature f2;
context.setFeature( f2 );
QVERIFY( context.currentGeometry().isNull() );
QVERIFY( context.currentGeometry( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ).isNull() );
delete layer;
}
void TestQgsLayoutContext::scales()
{
QVector< qreal > scales;
scales << 1 << 15 << 5 << 10;
QgsLayoutReportContext context( nullptr );
context.setPredefinedScales( scales );
// should be sorted
QCOMPARE( context.predefinedScales(), QVector< qreal >() << 1 << 5 << 10 << 15 );
}
QGSTEST_MAIN( TestQgsLayoutContext )
#include "testqgslayoutcontext.moc"