Skip to content

Commit 3ca416a

Browse files
committed
[composer] Initial test suite for QgsComposerTableV2 (Sponsored by
City of Uster, Switzerland)
1 parent 407a05c commit 3ca416a

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-0
lines changed

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ADD_QGIS_TEST(composerpicturetest testqgscomposerpicture.cpp)
117117
ADD_QGIS_TEST(atlascompositiontest testqgsatlascomposition.cpp)
118118
ADD_QGIS_TEST(composerlabeltest testqgscomposerlabel.cpp)
119119
ADD_QGIS_TEST(composertabletest testqgscomposertable.cpp)
120+
ADD_QGIS_TEST(composertablev2test testqgscomposertablev2.cpp)
120121
ADD_QGIS_TEST(composerddtest testqgscomposerdd.cpp)
121122
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
122123
ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
/***************************************************************************
2+
testqgscomposertablev2.cpp
3+
----------------------
4+
begin : September 2014
5+
copyright : (C) 2014 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsapplication.h"
19+
#include "qgscomposition.h"
20+
#include "qgscomposermap.h"
21+
#include "qgscomposerattributetablev2.h"
22+
#include "qgscomposerframe.h"
23+
#include "qgsmapsettings.h"
24+
#include "qgsvectorlayer.h"
25+
#include "qgsvectordataprovider.h"
26+
#include "qgsfeature.h"
27+
#include "qgscompositionchecker.h"
28+
#include "qgsfontutils.h"
29+
30+
#include <QObject>
31+
#include <QtTest>
32+
33+
class TestQgsComposerTableV2: public QObject
34+
{
35+
Q_OBJECT;
36+
private slots:
37+
void initTestCase();// will be called before the first testfunction is executed.
38+
void cleanupTestCase();// will be called after the last testfunction was executed.
39+
void init();// will be called before each testfunction is executed.
40+
void cleanup();// will be called after every testfunction.
41+
42+
void attributeTableHeadings(); //test retrieving attribute table headers
43+
void attributeTableRows(); //test retrieving attribute table rows
44+
void attributeTableFilterFeatures(); //test filtering attribute table rows
45+
void attributeTableSetAttributes(); //test subset of attributes in table
46+
void attributeTableVisibleOnly(); //test displaying only visible attributes
47+
void attributeTableRender(); //test rendering attribute table
48+
49+
private:
50+
QgsComposition* mComposition;
51+
QgsComposerMap* mComposerMap;
52+
QgsMapSettings mMapSettings;
53+
QgsVectorLayer* mVectorLayer;
54+
QgsComposerAttributeTableV2* mComposerAttributeTable;
55+
QgsComposerFrame* mFrame1;
56+
QgsComposerFrame* mFrame2;
57+
QString mReport;
58+
59+
//compares rows in mComposerAttributeTable to expected rows
60+
void compareTable( QList<QStringList> &expectedRows );
61+
};
62+
63+
void TestQgsComposerTableV2::initTestCase()
64+
{
65+
QgsApplication::init();
66+
QgsApplication::initQgis();
67+
68+
//create maplayers from testdata and add to layer registry
69+
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "points.shp" );
70+
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(),
71+
vectorFileInfo.completeBaseName(),
72+
"ogr" );
73+
74+
//create composition with composer map
75+
mMapSettings.setLayers( QStringList() << mVectorLayer->id() );
76+
mMapSettings.setCrsTransformEnabled( false );
77+
mComposition = new QgsComposition( mMapSettings );
78+
mComposition->setPaperSize( 297, 210 ); //A4 portrait
79+
80+
mComposerAttributeTable = new QgsComposerAttributeTableV2( mComposition, false );
81+
82+
mFrame1 = new QgsComposerFrame( mComposition, mComposerAttributeTable, 5, 5, 100, 30 );
83+
mFrame2 = new QgsComposerFrame( mComposition, mComposerAttributeTable, 5, 40, 100, 30 );
84+
85+
mFrame1->setFrameEnabled( true );
86+
mFrame2->setFrameEnabled( true );
87+
mComposerAttributeTable->addFrame( mFrame1 );
88+
mComposerAttributeTable->addFrame( mFrame2 );
89+
90+
mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame1 );
91+
mComposition->addComposerTableFrame( mComposerAttributeTable, mFrame2 );
92+
mComposerAttributeTable->setVectorLayer( mVectorLayer );
93+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
94+
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
95+
mComposerAttributeTable->setContentFont( QgsFontUtils::getStandardTestFont() );
96+
mComposerAttributeTable->setHeaderFont( QgsFontUtils::getStandardTestFont() );
97+
98+
mReport = "<h1>Composer TableV2 Tests</h1>\n";
99+
}
100+
101+
void TestQgsComposerTableV2::cleanupTestCase()
102+
{
103+
delete mComposition;
104+
delete mVectorLayer;
105+
106+
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
107+
QFile myFile( myReportFile );
108+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
109+
{
110+
QTextStream myQTextStream( &myFile );
111+
myQTextStream << mReport;
112+
myFile.close();
113+
}
114+
}
115+
116+
void TestQgsComposerTableV2::init()
117+
{
118+
}
119+
120+
void TestQgsComposerTableV2::cleanup()
121+
{
122+
}
123+
124+
void TestQgsComposerTableV2::attributeTableHeadings()
125+
{
126+
//test retrieving attribute table headers
127+
QStringList expectedHeaders;
128+
expectedHeaders << "Class" << "Heading" << "Importance" << "Pilots" << "Cabin Crew" << "Staff";
129+
130+
//get header labels and compare
131+
QMap<int, QString> headerMap = mComposerAttributeTable->headerLabels();
132+
QMap<int, QString>::const_iterator headerIt = headerMap.constBegin();
133+
QString expected;
134+
QString evaluated;
135+
for ( ; headerIt != headerMap.constEnd(); ++headerIt )
136+
{
137+
evaluated = headerIt.value();
138+
expected = expectedHeaders.at( headerIt.key() );
139+
QCOMPARE( evaluated, expected );
140+
}
141+
}
142+
143+
void TestQgsComposerTableV2::compareTable( QList<QStringList> &expectedRows )
144+
{
145+
//retrieve rows and check
146+
QgsComposerTableContents tableContents;
147+
bool result = mComposerAttributeTable->getTableContents( tableContents );
148+
QCOMPARE( result, true );
149+
150+
QgsComposerTableContents::const_iterator resultIt = tableContents.constBegin();
151+
int rowNumber = 0;
152+
int colNumber = 0;
153+
154+
//check that number of rows matches expected
155+
QCOMPARE( tableContents.count(), expectedRows.count() );
156+
157+
for ( ; resultIt != tableContents.constEnd(); ++resultIt )
158+
{
159+
colNumber = 0;
160+
QgsComposerTableRow::const_iterator cellIt = ( *resultIt ).constBegin();
161+
for ( ; cellIt != ( *resultIt ).constEnd(); ++cellIt )
162+
{
163+
QCOMPARE(( *cellIt ).toString(), expectedRows.at( rowNumber ).at( colNumber ) );
164+
colNumber++;
165+
}
166+
//also check that number of columns matches expected
167+
QCOMPARE(( *resultIt ).count(), expectedRows.at( rowNumber ).count() );
168+
169+
rowNumber++;
170+
}
171+
}
172+
173+
void TestQgsComposerTableV2::attributeTableRows()
174+
{
175+
//test retrieving attribute table rows
176+
177+
QList<QStringList> expectedRows;
178+
QStringList row;
179+
row << "Jet" << "90" << "3" << "2" << "0" << "2";
180+
expectedRows.append( row );
181+
row.clear();
182+
row << "Biplane" << "0" << "1" << "3" << "3" << "6";
183+
expectedRows.append( row );
184+
row.clear();
185+
row << "Jet" << "85" << "3" << "1" << "1" << "2";
186+
expectedRows.append( row );
187+
188+
//retrieve rows and check
189+
mComposerAttributeTable->setMaximumNumberOfFeatures( 3 );
190+
compareTable( expectedRows );
191+
}
192+
193+
void TestQgsComposerTableV2::attributeTableFilterFeatures()
194+
{
195+
//test filtering attribute table rows
196+
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
197+
mComposerAttributeTable->setFeatureFilter( QString( "\"Class\"='B52'" ) );
198+
mComposerAttributeTable->setFilterFeatures( true );
199+
200+
QList<QStringList> expectedRows;
201+
QStringList row;
202+
row << "B52" << "0" << "10" << "2" << "1" << "3";
203+
expectedRows.append( row );
204+
row.clear();
205+
row << "B52" << "12" << "10" << "1" << "1" << "2";
206+
expectedRows.append( row );
207+
row.clear();
208+
row << "B52" << "34" << "10" << "2" << "1" << "3";
209+
expectedRows.append( row );
210+
row.clear();
211+
row << "B52" << "80" << "10" << "2" << "1" << "3";
212+
expectedRows.append( row );
213+
214+
//retrieve rows and check
215+
compareTable( expectedRows );
216+
217+
mComposerAttributeTable->setFilterFeatures( false );
218+
}
219+
220+
void TestQgsComposerTableV2::attributeTableSetAttributes()
221+
{
222+
//test subset of attributes in table
223+
QSet<int> attributes;
224+
attributes << 0 << 3 << 4;
225+
mComposerAttributeTable->setDisplayAttributes( attributes );
226+
mComposerAttributeTable->setMaximumNumberOfFeatures( 3 );
227+
228+
//check headers
229+
QStringList expectedHeaders;
230+
expectedHeaders << "Class" << "Pilots" << "Cabin Crew";
231+
232+
//get header labels and compare
233+
QMap<int, QString> headerMap = mComposerAttributeTable->headerLabels();
234+
QMap<int, QString>::const_iterator headerIt = headerMap.constBegin();
235+
QString expected;
236+
QString evaluated;
237+
for ( ; headerIt != headerMap.constEnd(); ++headerIt )
238+
{
239+
evaluated = headerIt.value();
240+
expected = expectedHeaders.at( headerIt.key() );
241+
QCOMPARE( evaluated, expected );
242+
}
243+
244+
QList<QStringList> expectedRows;
245+
QStringList row;
246+
row << "Jet" << "2" << "0";
247+
expectedRows.append( row );
248+
row.clear();
249+
row << "Biplane" << "3" << "3";
250+
expectedRows.append( row );
251+
row.clear();
252+
row << "Jet" << "1" << "1";
253+
expectedRows.append( row );
254+
255+
//retrieve rows and check
256+
compareTable( expectedRows );
257+
258+
attributes.clear();
259+
mComposerAttributeTable->setDisplayAttributes( attributes );
260+
}
261+
262+
void TestQgsComposerTableV2::attributeTableVisibleOnly()
263+
{
264+
//test displaying only visible attributes
265+
266+
mComposerMap = new QgsComposerMap( mComposition, 20, 20, 200, 100 );
267+
mComposerMap->setFrameEnabled( true );
268+
mComposition->addComposerMap( mComposerMap );
269+
mComposerMap->setNewExtent( QgsRectangle( -131.767, 30.558, -110.743, 41.070 ) );
270+
271+
mComposerAttributeTable->setComposerMap( mComposerMap );
272+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( true );
273+
274+
QList<QStringList> expectedRows;
275+
QStringList row;
276+
row << "Jet" << "90" << "3" << "2" << "0" << "2";
277+
expectedRows.append( row );
278+
row.clear();
279+
row << "Biplane" << "240" << "1" << "3" << "2" << "5";
280+
expectedRows.append( row );
281+
row.clear();
282+
row << "Jet" << "180" << "3" << "1" << "0" << "1";
283+
expectedRows.append( row );
284+
285+
//retrieve rows and check
286+
compareTable( expectedRows );
287+
288+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
289+
mComposerAttributeTable->setComposerMap( 0 );
290+
mComposition->removeItem( mComposerMap );
291+
}
292+
293+
void TestQgsComposerTableV2::attributeTableRender()
294+
{
295+
mComposerAttributeTable->setMaximumNumberOfFeatures( 20 );
296+
QgsCompositionChecker checker( "composerattributetable_render", mComposition );
297+
bool result = checker.testComposition( mReport );
298+
QVERIFY( result );
299+
}
300+
301+
QTEST_MAIN( TestQgsComposerTableV2 )
302+
#include "moc_testqgscomposertablev2.cxx"
303+

0 commit comments

Comments
 (0)