1 /***************************************************************************
2 testqgsquickprint.cpp
3 --------------------------------------
4 Date : 20 Jan 2008
5 Copyright : (C) 2008 by Tim Sutton
6 Email : tim @ linfiniti.com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15 #include "qgstest.h"
16 #include <QObject>
17 #include <QStringList>
18 #include <QApplication>
19 #include <QFileInfo>
20 #include <QDir>
21 #include <QDesktopServices>
22
23 //qgis includes...
24 #include <qgsmaprenderer.h>
25 #include <qgsmaplayer.h>
26 #include <qgsvectorlayer.h>
27 #include <qgsapplication.h>
28 #include <qgsproviderregistry.h>
29 #include <qgsproject.h>
30 #include <qgsquickprint.h>
31 //qgis test includes
32 #include <qgsrenderchecker.h>
33
34 /**
35 * \ingroup UnitTests
36 * This is a unit test for the different renderers for vector layers.
37 */
38 class TestQgsQuickPrint : public QObject
39 {
40 Q_OBJECT
41 public:
TestQgsQuickPrint()42 TestQgsQuickPrint()
43 : mpMapRenderer( 0 )
44 , mpPointsLayer( 0 )
45 , mpLinesLayer( 0 )
46 , mpPolysLayer( 0 )
47 {}
48
49 private slots:
50 void initTestCase();// will be called before the first testfunction is executed.
51 void cleanupTestCase();// will be called after the last testfunction was executed.
init()52 void init() {};// will be called before each testfunction is executed.
cleanup()53 void cleanup() {};// will be called after every testfunction.
54
55 void basicMapTest();
56 private:
57 bool imageCheck( QString type ); //as above
58 QgsMapRenderer *mpMapRenderer = nullptr;
59 QgsMapLayer *mpPointsLayer = nullptr;
60 QgsMapLayer *mpLinesLayer = nullptr;
61 QgsMapLayer *mpPolysLayer = nullptr;
62 QString mTestDataDir;
63 QString mReport;
64 };
65
initTestCase()66 void TestQgsQuickPrint::initTestCase()
67 {
68 //
69 // Runs once before any tests are run
70 //
71 // init QGIS's paths - true means that all path will be inited from prefix
72 QgsApplication.init();
73 QgsApplication.initQgis();
74 QgsApplication::showSettings();
75
76 //
77 //create a point layer that will be used in all tests...
78 //
79 QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
80 mTestDataDir = myDataDir + "/";
81 QString myPointsFileName = mTestDataDir + "points.shp";
82 QFileInfo myPointFileInfo( myPointsFileName );
83 mpPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
84 myPointFileInfo.completeBaseName(), "ogr" );
85 // Register the layer with the registry
86 QgsProject::instance()->addMapLayer( mpPointsLayer );
87
88 //
89 //create a poly layer that will be used in all tests...
90 //
91 QString myPolysFileName = mTestDataDir + "polys.shp";
92 QFileInfo myPolyFileInfo( myPolysFileName );
93 mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
94 myPolyFileInfo.completeBaseName(), "ogr" );
95 // Register the layer with the registry
96 QgsProject::instance()->addMapLayer( mpPolysLayer );
97
98 //
99 // Create a line layer that will be used in all tests...
100 //
101 QString myLinesFileName = mTestDataDir + "lines.shp";
102 QFileInfo myLineFileInfo( myLinesFileName );
103 mpLinesLayer = new QgsVectorLayer( myLineFileInfo.filePath(),
104 myLineFileInfo.completeBaseName(), "ogr" );
105 // Register the layer with the registry
106 QgsProject::instance()->addMapLayer( mpLinesLayer );
107 //
108 // We only need maprender instead of mapcanvas
109 // since maprender does not require a qui
110 // and is more light weight
111 //
112 mpMapRenderer = new QgsMapRenderer();
113 QStringList myLayers;
114 myLayers << mpPointsLayer->getLayerID();
115 myLayers << mpPolysLayer->getLayerID();
116 myLayers << mpLinesLayer->getLayerID();
117 mpMapRenderer->setLayerSet( myLayers );
118 mpMapRenderer->setExtent( mpPointsLayer->extent() );
119 mReport += "<h1>QuickPrint Tests</h1>\n";
120 }
cleanupTestCase()121 void TestQgsQuickPrint::cleanupTestCase()
122 {
123 #if 0
124 QString myReportFile = QDir::tempPath() + "/quickprinttest.html";
125 QFile myFile( myReportFile );
126 if ( myFile.open( QIODevice::WriteOnly ) )
127 {
128 QTextStream myQTextStream( &myFile );
129 myQTextStream << mReport;
130 myFile.close();
131 QDesktopServices::openUrl( "file://" + myReportFile );
132 }
133 #endif
134 }
135
basicMapTest()136 void TestQgsQuickPrint::basicMapTest()
137 {
138 //make the legends really long so we can test
139 //word wrapping
140 mpPointsLayer->setLayerName( "This is a very very very long name it should word wrap" );
141 mpPolysLayer->setLayerName( "This is a very very very long name it should also word wrap" );
142 mpLinesLayer->setLayerName( "This is a very very very very long name it should word wrap" );
143
144 //now print the map
145 QgsQuickPrint myQuickPrint;
146 myQuickPrint.setMapBackgroundColor( Qt::cyan );
147 myQuickPrint.setOutputPdf( QDir::tempPath() + "/quickprinttest.pdf" );
148 myQuickPrint.setMapRenderer( mpMapRenderer );
149 myQuickPrint.setTitle( "Map Title" );
150 myQuickPrint.setName( "Map Name" );
151 myQuickPrint.setCopyright( "Copyright Text" );
152 //void setNorthArrow(QString fileName);
153 //void setLogo1(QString fileName);
154 //void setLogo2(QString fileName);
155 myQuickPrint.printMap();
156 }
157
158
159 //
160 // Helper functions below
161 //
162
imageCheck(QString testType)163 bool TestQgsQuickPrint::imageCheck( QString testType )
164 {
165 //use the QgsRenderChecker test utility class to
166 //ensure the rendered output matches our control image
167 mpMapRenderer->setExtent( mpPointsLayer->extent() );
168 QString myExpectedImage = mTestDataDir + "expected_" + testType + ".png";
169 QgsRenderChecker myChecker;
170 myChecker.setExpectedImage( myExpectedImage );
171 myChecker.setMapRenderer( mpMapRenderer );
172 bool myResultFlag = myChecker.runTest( testType );
173 mReport += myChecker.report();
174 return myResultFlag;
175 }
176
177 QGSTEST_MAIN( TestQgsQuickPrint )
178 #include "testqgsquickprint.moc"
179