Skip to content
Permalink
Browse files

Begin test suite for data defined composer settings

  • Loading branch information
nyalldawson committed Jul 8, 2014
1 parent e1385c1 commit 0df2b011fc8d7ff3ecaeab275577c4aad4b1d5ff
Showing with 141 additions and 0 deletions.
  1. +1 −0 tests/src/core/CMakeLists.txt
  2. +140 −0 tests/src/core/testqgscomposerdd.cpp
@@ -112,6 +112,7 @@ ADD_QGIS_TEST(composerpicturetest testqgscomposerpicture.cpp)
ADD_QGIS_TEST(atlascompositiontest testqgsatlascomposition.cpp)
ADD_QGIS_TEST(composerlabeltest testqgscomposerlabel.cpp)
ADD_QGIS_TEST(composertabletest testqgscomposertable.cpp)
ADD_QGIS_TEST(composerddtest testqgscomposerdd.cpp)
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
ADD_QGIS_TEST(rectangletest testqgsrectangle.cpp)
@@ -0,0 +1,140 @@
/***************************************************************************
testqgscomposertable.cpp
----------------------
begin : July 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 "qgsapplication.h"
#include "qgscomposition.h"
#include "qgscomposermap.h"
#include "qgscomposertexttable.h"
#include "qgscomposerattributetable.h"
#include "qgsmaplayerregistry.h"
#include "qgsmapsettings.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsfeature.h"
#include "qgssymbolv2.h"
#include "qgssinglesymbolrendererv2.h"
#include "qgsdatadefined.h"

#include <QObject>
#include <QtTest>

class TestQgsComposerDD: 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 ddEvaluate(); //test setting/evaluating data defined value

private:
QgsComposition* mComposition;
QgsMapSettings mMapSettings;
QgsVectorLayer* mVectorLayer;
QgsComposerMap* mAtlasMap;
QgsAtlasComposition* mAtlas;
QString mReport;
};

void TestQgsComposerDD::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

//create maplayers from testdata and add to layer registry
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "france_parts.shp" );
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
mVectorLayer->setSimplifyMethod( simplifyMethod );

QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mVectorLayer );

//create composition with composer map
mMapSettings.setLayers( QStringList() << mVectorLayer->id() );
mMapSettings.setCrsTransformEnabled( true );
mMapSettings.setMapUnits( QGis::Meters );

// select epsg:2154
QgsCoordinateReferenceSystem crs;
crs.createFromSrid( 2154 );
mMapSettings.setDestinationCrs( crs );
mComposition = new QgsComposition( mMapSettings );
mComposition->setPaperSize( 297, 210 ); //A4 landscape

// fix the renderer, fill with green
QgsStringMap props;
props.insert( "color", "0,127,0" );
QgsFillSymbolV2* fillSymbol = QgsFillSymbolV2::createSimple( props );
QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( fillSymbol );
mVectorLayer->setRendererV2( renderer );

// the atlas map
mAtlasMap = new QgsComposerMap( mComposition, 20, 20, 130, 130 );
mAtlasMap->setFrameEnabled( true );
mComposition->addComposerMap( mAtlasMap );

mAtlas = &mComposition->atlasComposition();
mAtlas->setCoverageLayer( mVectorLayer );
mAtlas->setEnabled( true );
mComposition->setAtlasMode( QgsComposition::ExportAtlas );

mReport = "<h1>Composer Data Defined Tests</h1>\n";

}

void TestQgsComposerDD::cleanupTestCase()
{
delete mComposition;
delete mVectorLayer;

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 TestQgsComposerDD::init()
{
}

void TestQgsComposerDD::cleanup()
{
}

void TestQgsComposerDD::ddEvaluate()
{
//set a data defined property
mAtlasMap->setDataDefinedProperty( QgsComposerItem::PositionY, true, true, QString("20+30"),QString() );
//evaluate property
mAtlasMap->refreshDataDefinedProperty( QgsComposerItem::PositionY );
QCOMPARE( mAtlasMap->pos().y(), 50.0 );
mAtlasMap->setDataDefinedProperty( QgsComposerItem::PositionY, false, false, QString(),QString() );
}

QTEST_MAIN( TestQgsComposerDD )
#include "moc_testqgscomposerdd.cxx"

0 comments on commit 0df2b01

Please sign in to comment.