PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/core/testqgsrenderers.cpp

https://github.com/linz/Quantum-GIS
C++ | 239 lines | 166 code | 22 blank | 51 comment | 4 complexity | 7374fa037d0e1b4a10769526509112d8 MD5 | raw file
  1. /***************************************************************************
  2. testqgsrenderers.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 <QString>
  18. #include <QStringList>
  19. #include <QObject>
  20. #include <QApplication>
  21. #include <QFileInfo>
  22. #include <QDir>
  23. #include <QDesktopServices>
  24. #include <iostream>
  25. //qgis includes...
  26. #include <qgsmaprenderer.h>
  27. #include <qgsmaplayer.h>
  28. #include <qgsvectorlayer.h>
  29. #include <qgsapplication.h>
  30. #include <qgsproviderregistry.h>
  31. #include <qgsmaplayerregistry.h>
  32. //qgis test includes
  33. #include "qgsrenderchecker.h"
  34. /** \ingroup UnitTests
  35. * This is a unit test for the different renderers for vector layers.
  36. */
  37. class TestQgsRenderers: public QObject
  38. {
  39. Q_OBJECT;
  40. private slots:
  41. void initTestCase();// will be called before the first testfunction is executed.
  42. void cleanupTestCase();// will be called after the last testfunction was executed.
  43. void init() {};// will be called before each testfunction is executed.
  44. void cleanup() {};// will be called after every testfunction.
  45. void singleSymbol();
  46. void uniqueValue();
  47. void graduatedSymbol();
  48. void continuousSymbol();
  49. void checkClassificationFieldMismatch();
  50. private:
  51. bool mTestHasError;
  52. bool setQml( QString theType ); //uniquevalue / continuous / single /
  53. bool imageCheck( QString theType ); //as above
  54. QgsMapRenderer * mpMapRenderer;
  55. QgsMapLayer * mpPointsLayer;
  56. QgsMapLayer * mpLinesLayer;
  57. QgsMapLayer * mpPolysLayer;
  58. QString mTestDataDir;
  59. QString mReport;
  60. };
  61. void TestQgsRenderers::initTestCase()
  62. {
  63. mTestHasError = false;
  64. // init QGIS's paths - true means that all path will be inited from prefix
  65. QString qgisPath = QCoreApplication::applicationDirPath();
  66. QgsApplication::setPrefixPath( INSTALL_PREFIX, true );
  67. QgsApplication::showSettings();
  68. // Instantiate the plugin directory so that providers are loaded
  69. QgsProviderRegistry::instance( QgsApplication::pluginPath() );
  70. //create some objects that will be used in all tests...
  71. std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
  72. std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
  73. std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
  74. std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;
  75. //
  76. //create a point layer that will be used in all tests...
  77. //
  78. QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
  79. mTestDataDir = myDataDir + QDir::separator();
  80. QString myPointsFileName = mTestDataDir + "points.shp";
  81. QFileInfo myPointFileInfo( myPointsFileName );
  82. mpPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
  83. myPointFileInfo.completeBaseName(), "ogr" );
  84. // Register the layer with the registry
  85. QgsMapLayerRegistry::instance()->addMapLayer( mpPointsLayer );
  86. //
  87. //create a poly layer that will be used in all tests...
  88. //
  89. QString myPolysFileName = mTestDataDir + "polys.shp";
  90. QFileInfo myPolyFileInfo( myPolysFileName );
  91. mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
  92. myPolyFileInfo.completeBaseName(), "ogr" );
  93. // Register the layer with the registry
  94. QgsMapLayerRegistry::instance()->addMapLayer( mpPolysLayer );
  95. //
  96. // Create a line layer that will be used in all tests...
  97. //
  98. QString myLinesFileName = mTestDataDir + "lines.shp";
  99. QFileInfo myLineFileInfo( myLinesFileName );
  100. mpLinesLayer = new QgsVectorLayer( myLineFileInfo.filePath(),
  101. myLineFileInfo.completeBaseName(), "ogr" );
  102. // Register the layer with the registry
  103. QgsMapLayerRegistry::instance()->addMapLayer( mpLinesLayer );
  104. //
  105. // We only need maprender instead of mapcanvas
  106. // since maprender does not require a qui
  107. // and is more light weight
  108. //
  109. mpMapRenderer = new QgsMapRenderer();
  110. QStringList myLayers;
  111. myLayers << mpPointsLayer->id();
  112. myLayers << mpPolysLayer->id();
  113. myLayers << mpLinesLayer->id();
  114. mpMapRenderer->setLayerSet( myLayers );
  115. mReport += "<h1>Vector Renderer Tests</h1>\n";
  116. }
  117. void TestQgsRenderers::cleanupTestCase()
  118. {
  119. QString myReportFile = QDir::tempPath() + QDir::separator() + "renderertest.html";
  120. QFile myFile( myReportFile );
  121. if ( myFile.open( QIODevice::WriteOnly ) )
  122. {
  123. QTextStream myQTextStream( &myFile );
  124. myQTextStream << mReport;
  125. myFile.close();
  126. QDesktopServices::openUrl( "file://" + myReportFile );
  127. }
  128. }
  129. void TestQgsRenderers::singleSymbol()
  130. {
  131. mReport += "<h2>Single symbol renderer test</h2>\n";
  132. QVERIFY( setQml( "single" ) );
  133. QVERIFY( imageCheck( "single" ) );
  134. }
  135. void TestQgsRenderers::uniqueValue()
  136. {
  137. mReport += "<h2>Unique value symbol renderer test</h2>\n";
  138. QVERIFY( setQml( "uniquevalue" ) );
  139. QVERIFY( imageCheck( "uniquevalue" ) );
  140. }
  141. void TestQgsRenderers::graduatedSymbol()
  142. {
  143. mReport += "<h2>Graduated symbol renderer test</h2>\n";
  144. QVERIFY( setQml( "graduated" ) );
  145. QVERIFY( imageCheck( "graduated" ) );
  146. }
  147. void TestQgsRenderers::continuousSymbol()
  148. {
  149. mReport += "<h2>Continuous symbol renderer test</h2>\n";
  150. QVERIFY( setQml( "continuous" ) );
  151. QVERIFY( imageCheck( "continuous" ) );
  152. }
  153. void TestQgsRenderers::checkClassificationFieldMismatch()
  154. {
  155. mReport += "<h2>Classification field mismatch test</h2>\n";
  156. // Here we test to see that a qml created for one layer
  157. // will raise an error properly if the
  158. // We will do this by trying to apply the points qml to the polys shpfile
  159. // it should fail and raise an error
  160. QString myFileName = mTestDataDir + "points_continuous_symbol.qml";
  161. bool myStyleFlag = false;
  162. mpPolysLayer->loadNamedStyle( myFileName, myStyleFlag );
  163. QVERIFY( !myStyleFlag ); //we are expecting this to have raised an error
  164. }
  165. //
  166. // Private helper functions not called directly by CTest
  167. //
  168. bool TestQgsRenderers::setQml( QString theType )
  169. {
  170. //load a qml style and apply to our layer
  171. //the style will correspond to the renderer
  172. //type we are testing
  173. if ( ! mpPointsLayer->isValid() )
  174. {
  175. return false;
  176. }
  177. QString myFileName = mTestDataDir + "points_" + theType + "_symbol.qml";
  178. bool myStyleFlag = false;
  179. QString error = mpPointsLayer->loadNamedStyle( myFileName, myStyleFlag );
  180. if ( !myStyleFlag )
  181. {
  182. qDebug( "%s", error.toLocal8Bit().constData() );
  183. return false;
  184. }
  185. else
  186. {
  187. myStyleFlag = false; //ready for next test
  188. }
  189. myFileName = mTestDataDir + "polys_" + theType + "_symbol.qml";
  190. mpPolysLayer->loadNamedStyle( myFileName, myStyleFlag );
  191. if ( !myStyleFlag )
  192. {
  193. return false;
  194. }
  195. else
  196. {
  197. myStyleFlag = false; //ready for next test
  198. }
  199. myFileName = mTestDataDir + "lines_" + theType + "_symbol.qml";
  200. mpLinesLayer->loadNamedStyle( myFileName, myStyleFlag );
  201. return myStyleFlag;
  202. }
  203. bool TestQgsRenderers::imageCheck( QString theTestType )
  204. {
  205. //use the QgsRenderChecker test utility class to
  206. //ensure the rendered output matches our control image
  207. mpMapRenderer->setExtent( mpPointsLayer->extent() );
  208. QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png";
  209. QgsRenderChecker myChecker;
  210. myChecker.setExpectedImage( myExpectedImage );
  211. myChecker.setMapRenderer( mpMapRenderer );
  212. bool myResultFlag = myChecker.runTest( theTestType );
  213. mReport += myChecker.report();
  214. return myResultFlag;
  215. }
  216. QTEST_MAIN( TestQgsRenderers )
  217. #include "moc_testqgsrenderers.cxx"