/
testqgsapplayoutvaliditychecks.cpp
223 lines (184 loc) · 7.59 KB
/
testqgsapplayoutvaliditychecks.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
/***************************************************************************
testqgsapplayoutvaliditychecks.cpp
--------------------------------------
Date : January 2019
Copyright : (C) 2019 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 "qgstest.h"
#include <QObject>
#include <QString>
#include <QStringList>
//qgis includes...
#include "qgsdataitem.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgslayout.h"
#include "qgslayoutitemscalebar.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitempicture.h"
#include "qgsabstractvaliditycheck.h"
#include "qgsvaliditycheckcontext.h"
#include "layout/qgslayoutvaliditychecks.h"
#include "qgsfeedback.h"
class TestQgsLayoutValidityChecks : public QObject
{
Q_OBJECT
public:
TestQgsLayoutValidityChecks();
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 testScaleBarValidity();
void testOverviewValidity();
void testPictureValidity();
private:
QString mTestDataDir;
};
TestQgsLayoutValidityChecks::TestQgsLayoutValidityChecks() = default;
void TestQgsLayoutValidityChecks::initTestCase()
{
//
// Runs once before any tests are run
//
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = dataDir + '/';
}
void TestQgsLayoutValidityChecks::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsLayoutValidityChecks::testScaleBarValidity()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutItemScaleBar *scale = new QgsLayoutItemScaleBar( &l );
l.addItem( scale );
QgsLayoutValidityCheckContext context( &l );
QgsFeedback f;
// scalebar not linked to map
QgsLayoutScaleBarValidityCheck check;
QVERIFY( check.prepareCheck( &context, &f ) );
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
// now link a map
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
l.addItem( map );
scale->setLinkedMap( map );
QgsLayoutScaleBarValidityCheck check2;
QVERIFY( check2.prepareCheck( &context, &f ) );
res = check2.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
}
void TestQgsLayoutValidityChecks::testOverviewValidity()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutItemMap *map1 = new QgsLayoutItemMap( &l );
l.addItem( map1 );
QgsLayoutValidityCheckContext context( &l );
QgsFeedback f;
// no overviews
QgsLayoutOverviewValidityCheck check;
QVERIFY( check.prepareCheck( &context, &f ) );
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
// overview not linked to map
map1->overviews()->addOverview( new QgsLayoutItemMapOverview( QStringLiteral( "blah" ), map1 ) );
QgsLayoutOverviewValidityCheck check2;
QVERIFY( check2.prepareCheck( &context, &f ) );
res = check2.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
map1->overview()->setEnabled( false );
QgsLayoutOverviewValidityCheck check3;
QVERIFY( check3.prepareCheck( &context, &f ) );
res = check3.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
// now link a map
QgsLayoutItemMap *map2 = new QgsLayoutItemMap( &l );
l.addItem( map2 );
map1->overview()->setLinkedMap( map2 );
map1->overview()->setEnabled( true );
QgsLayoutOverviewValidityCheck check4;
QVERIFY( check4.prepareCheck( &context, &f ) );
res = check4.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
map1->overviews()->addOverview( new QgsLayoutItemMapOverview( QStringLiteral( "blah2" ), map1 ) );
QgsLayoutOverviewValidityCheck check5;
QVERIFY( check5.prepareCheck( &context, &f ) );
res = check5.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
map1->overviews()->addOverview( new QgsLayoutItemMapOverview( QStringLiteral( "blah3" ), map1 ) );
QgsLayoutOverviewValidityCheck check6;
QVERIFY( check6.prepareCheck( &context, &f ) );
res = check6.runCheck( &context, &f );
QCOMPARE( res.size(), 2 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
QCOMPARE( res.at( 1 ).type, QgsValidityCheckResult::Warning );
}
void TestQgsLayoutValidityChecks::testPictureValidity()
{
QgsProject p;
QgsLayout l( &p );
QgsLayoutItemPicture *picture = new QgsLayoutItemPicture( &l );
l.addItem( picture );
QgsLayoutValidityCheckContext context( &l );
QgsFeedback f;
// invalid picture source
picture->setPicturePath( QStringLiteral( "blaaaaaaaaaaaaaaaaah" ) );
QgsLayoutPictureSourceValidityCheck check;
QVERIFY( check.prepareCheck( &context, &f ) );
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
QgsLayoutPictureSourceValidityCheck check2;
picture->setPicturePath( QString() );
QVERIFY( check2.prepareCheck( &context, &f ) );
res = check2.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
QgsLayoutPictureSourceValidityCheck check3;
picture->setPicturePath( QStringLiteral( TEST_DATA_DIR ) + "/sample_svg.svg" );
QVERIFY( check3.prepareCheck( &context, &f ) );
res = check3.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
QgsLayoutItemPicture *picture2 = new QgsLayoutItemPicture( &l );
l.addItem( picture2 );
picture2->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'d:/bad' || 'robot'" ) ) );
l.refresh();
QgsLayoutPictureSourceValidityCheck check4;
QVERIFY( check4.prepareCheck( &context, &f ) );
res = check4.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
picture2->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "''" ) ) );
l.refresh();
QgsLayoutPictureSourceValidityCheck check5;
QVERIFY( check5.prepareCheck( &context, &f ) );
res = check5.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
picture2->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'%1'" ).arg( QStringLiteral( TEST_DATA_DIR ) + "/sam' || 'ple_svg.svg" ) ) );
l.refresh();
QgsLayoutPictureSourceValidityCheck check6;
QVERIFY( check6.prepareCheck( &context, &f ) );
res = check6.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
}
QGSTEST_MAIN( TestQgsLayoutValidityChecks )
#include "testqgsapplayoutvaliditychecks.moc"