|
| 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 <QtTest> |
| 16 | +#include <QObject> |
| 17 | +#include <QStringList> |
| 18 | +#include <QObject> |
| 19 | +#include <QApplication> |
| 20 | +#include <QFileInfo> |
| 21 | +#include <QDir> |
| 22 | +#include <QDesktopServices> |
| 23 | + |
| 24 | +#include <iostream> |
| 25 | +//qgis includes... |
| 26 | +#include <qgsmaprender.h> |
| 27 | +#include <qgsmaplayer.h> |
| 28 | +#include <qgsvectorlayer.h> |
| 29 | +#include <qgsapplication.h> |
| 30 | +#include <qgsproviderregistry.h> |
| 31 | +#include <qgsmaplayerregistry.h> |
| 32 | +#include <qgsquickprint.h> |
| 33 | +//qgis test includes |
| 34 | +#include <qgsrenderchecker.h> |
| 35 | + |
| 36 | +/** \ingroup UnitTests |
| 37 | + * This is a unit test for the different renderers for vector layers. |
| 38 | + */ |
| 39 | +class TestQgsQuickPrint: public QObject |
| 40 | +{ |
| 41 | + Q_OBJECT; |
| 42 | + private slots: |
| 43 | + void initTestCase();// will be called before the first testfunction is executed. |
| 44 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 45 | + void init(){};// will be called before each testfunction is executed. |
| 46 | + void cleanup(){};// will be called after every testfunction. |
| 47 | + |
| 48 | + void basicMapTest(); |
| 49 | + QString getQgisPath(); // Gets the path to QGIS installation |
| 50 | + private: |
| 51 | + bool imageCheck(QString theType); //as above |
| 52 | + QgsMapRender * mpMapRenderer; |
| 53 | + QgsMapLayer * mpPointsLayer; |
| 54 | + QgsMapLayer * mpLinesLayer; |
| 55 | + QgsMapLayer * mpPolysLayer; |
| 56 | + QString mTestDataDir; |
| 57 | + QString mReport; |
| 58 | +}; |
| 59 | + |
| 60 | +QString TestQgsQuickPrint::getQgisPath() |
| 61 | +{ |
| 62 | +#ifdef Q_OS_LINUX |
| 63 | + QString qgisPath = QCoreApplication::applicationDirPath () + "/../"; |
| 64 | +#else //mac and win |
| 65 | + QString qgisPath = QCoreApplication::applicationDirPath () ; |
| 66 | +#endif |
| 67 | + return qgisPath; |
| 68 | +} |
| 69 | + |
| 70 | +void TestQgsQuickPrint::initTestCase() |
| 71 | +{ |
| 72 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 73 | + //QString qgisPath = QCoreApplication::applicationDirPath (); |
| 74 | + QgsApplication::setPrefixPath(getQgisPath(), TRUE); |
| 75 | +#ifdef Q_OS_LINUX |
| 76 | +// QgsApplication::setPkgDataPath(qgisPath + "/../share/qgis"); |
| 77 | +// QgsApplication::setPluginPath(qgisPath + "/../lib/qgis"); |
| 78 | +#endif |
| 79 | + // Instantiate the plugin directory so that providers are loaded |
| 80 | + QgsProviderRegistry::instance(QgsApplication::pluginPath()); |
| 81 | + |
| 82 | + //create some objects that will be used in all tests... |
| 83 | + |
| 84 | + std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl; |
| 85 | + std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl; |
| 86 | + std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl; |
| 87 | + std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl; |
| 88 | + |
| 89 | + // |
| 90 | + //create a point layer that will be used in all tests... |
| 91 | + // |
| 92 | + QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt |
| 93 | + mTestDataDir = myDataDir + QDir::separator(); |
| 94 | + QString myPointsFileName = mTestDataDir + "points.shp"; |
| 95 | + QFileInfo myPointFileInfo ( myPointsFileName ); |
| 96 | + mpPointsLayer = new QgsVectorLayer ( myPointFileInfo.filePath(), |
| 97 | + myPointFileInfo.completeBaseName(), "ogr" ); |
| 98 | + // Register the layer with the registry |
| 99 | + QgsMapLayerRegistry::instance()->addMapLayer(mpPointsLayer); |
| 100 | + |
| 101 | + // |
| 102 | + //create a poly layer that will be used in all tests... |
| 103 | + // |
| 104 | + QString myPolysFileName = mTestDataDir + "polys.shp"; |
| 105 | + QFileInfo myPolyFileInfo ( myPolysFileName ); |
| 106 | + mpPolysLayer = new QgsVectorLayer ( myPolyFileInfo.filePath(), |
| 107 | + myPolyFileInfo.completeBaseName(), "ogr" ); |
| 108 | + // Register the layer with the registry |
| 109 | + QgsMapLayerRegistry::instance()->addMapLayer(mpPolysLayer); |
| 110 | + |
| 111 | + // |
| 112 | + // Create a line layer that will be used in all tests... |
| 113 | + // |
| 114 | + QString myLinesFileName = mTestDataDir + "lines.shp"; |
| 115 | + QFileInfo myLineFileInfo ( myLinesFileName ); |
| 116 | + mpLinesLayer = new QgsVectorLayer ( myLineFileInfo.filePath(), |
| 117 | + myLineFileInfo.completeBaseName(), "ogr" ); |
| 118 | + // Register the layer with the registry |
| 119 | + QgsMapLayerRegistry::instance()->addMapLayer(mpLinesLayer); |
| 120 | + // |
| 121 | + // We only need maprender instead of mapcanvas |
| 122 | + // since maprender does not require a qui |
| 123 | + // and is more light weight |
| 124 | + // |
| 125 | + mpMapRenderer = new QgsMapRender(); |
| 126 | + QStringList myLayers; |
| 127 | + myLayers << mpPointsLayer->getLayerID(); |
| 128 | + myLayers << mpPolysLayer->getLayerID(); |
| 129 | + myLayers << mpLinesLayer->getLayerID(); |
| 130 | + mpMapRenderer->setLayerSet(myLayers); |
| 131 | + mpMapRenderer->setExtent(mpPointsLayer->extent()); |
| 132 | + mReport+= "<h1>QuickPrint Tests</h1>\n"; |
| 133 | +} |
| 134 | +void TestQgsQuickPrint::cleanupTestCase() |
| 135 | +{ |
| 136 | + /* |
| 137 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "quickprinttest.html"; |
| 138 | + QFile myFile ( myReportFile); |
| 139 | + if ( myFile.open ( QIODevice::WriteOnly ) ) |
| 140 | + { |
| 141 | + QTextStream myQTextStream ( &myFile ); |
| 142 | + myQTextStream << mReport; |
| 143 | + myFile.close(); |
| 144 | + QDesktopServices::openUrl("file://"+myReportFile); |
| 145 | + } |
| 146 | + */ |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | +void TestQgsQuickPrint::basicMapTest() |
| 151 | +{ |
| 152 | + //make the legends really long so we can test |
| 153 | + //word wrapping |
| 154 | + mpPointsLayer->setLayerName("This is a very very very long name it should word wrap"); |
| 155 | + mpPolysLayer->setLayerName("This is a very very very long name it should also word wrap"); |
| 156 | + mpLinesLayer->setLayerName("This is a very very very very long name it should word wrap"); |
| 157 | + |
| 158 | + //now print the map |
| 159 | + QgsQuickPrint myQuickPrint; |
| 160 | + myQuickPrint.setMapBackgroundColor ( Qt::cyan ); |
| 161 | + myQuickPrint.setOutputPdf (QDir::tempPath() + QDir::separator() + "quickprinttest.pdf"); |
| 162 | + myQuickPrint.setMapRender (mpMapRenderer); |
| 163 | + myQuickPrint.setTitle ("Map Title"); |
| 164 | + myQuickPrint.setName ("Map Name"); |
| 165 | + myQuickPrint.setCopyright ("Copyright Text"); |
| 166 | + //void setNorthArrow(QString theFileName); |
| 167 | + //void setLogo1(QString theFileName); |
| 168 | + //void setLogo2(QString theFileName); |
| 169 | + myQuickPrint.printMap(); |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +// |
| 174 | +// Helper functions below |
| 175 | +// |
| 176 | + |
| 177 | +bool TestQgsQuickPrint::imageCheck(QString theTestType) |
| 178 | +{ |
| 179 | + //use the QgsRenderChecker test utility class to |
| 180 | + //ensure the rendered output matches our control image |
| 181 | + mpMapRenderer->setExtent(mpPointsLayer->extent()); |
| 182 | + QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png"; |
| 183 | + QgsRenderChecker myChecker; |
| 184 | + myChecker.setExpectedImage ( myExpectedImage ); |
| 185 | + myChecker.setMapRenderer ( mpMapRenderer ); |
| 186 | + bool myResultFlag = myChecker.runTest( theTestType ); |
| 187 | + mReport += myChecker.report(); |
| 188 | + return myResultFlag; |
| 189 | +} |
| 190 | + |
| 191 | +QTEST_MAIN(TestQgsQuickPrint) |
| 192 | +#include "moc_testqgsquickprint.cxx" |
| 193 | + |
0 commit comments