/tests/src/gui/testqgsquickprint.cpp

https://github.com/ricardogsilva/Quantum-GIS · C++ · 178 lines · 112 code · 14 blank · 52 comment · 1 complexity · 12c76d51887ef945040d5095b0f20198 MD5 · raw file

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