QGIS API Documentation  2.12.0-Lyon
qgsmultirenderchecker.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmultirenderchecker.cpp
3  --------------------------------------
4  Date : 6.11.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis dot ch
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 
16 #include "qgsmultirenderchecker.h"
17 
18 #include <QDebug>
19 
21  : mColorTolerance( 0 )
22 {
23 }
24 
26 {
27  mControlName = theName;
28 }
29 
31 {
32  mControlPathPrefix = prefix;
33 }
34 
36 {
37  mMapSettings = mapSettings;
38 }
39 
40 bool QgsMultiRenderChecker::runTest( const QString& theTestName, unsigned int theMismatchCount )
41 {
42  bool successful = false;
43 
44  const QString baseDir = controlImagePath();
45 
46  QStringList subDirs = QDir( baseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
47 
48  if ( subDirs.count() == 0 )
49  {
50  subDirs << "";
51  }
52 
53  QVector<QgsDartMeasurement> dartMeasurements;
54 
55  Q_FOREACH ( const QString& suffix, subDirs )
56  {
57  qDebug() << "Checking subdir " << suffix;
58  bool result;
59  QgsRenderChecker checker;
60  checker.enableDashBuffering( true );
61  checker.setColorTolerance( mColorTolerance );
62  checker.setControlPathPrefix( mControlPathPrefix );
63  checker.setControlPathSuffix( suffix );
64  checker.setControlName( mControlName );
65  checker.setMapSettings( mMapSettings );
66 
67  if ( !mRenderedImage.isNull() )
68  {
69  checker.setRenderedImage( mRenderedImage );
70  result = checker.compareImages( theTestName, theMismatchCount, mRenderedImage );
71  }
72  else
73  {
74  result = checker.runTest( theTestName, theMismatchCount );
75  mRenderedImage = checker.renderedImage();
76  }
77 
78  successful |= result;
79 
80  dartMeasurements << checker.dartMeasurements();
81 
82  mReport += checker.report();
83  }
84 
85  if ( !successful )
86  {
87  Q_FOREACH ( const QgsDartMeasurement& measurement, dartMeasurements )
88  measurement.send();
89 
90  QgsDartMeasurement msg( "Image not accepted by test", QgsDartMeasurement::Text, "This may be caused because the test is supposed to fail or rendering inconsistencies."
91  "If this is a rendering inconsistency, please add another control image folder, add an anomaly image or increase the color tolerance." );
92  msg.send();
93  }
94 
95  return successful;
96 }
97 
99 {
100  QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
101  QString myControlImageDir = myDataDir + QDir::separator() + "control_images" +
102  QDir::separator() + mControlPathPrefix + QDir::separator() + mControlName + QDir::separator();
103  return myControlImageDir;
104 }
const QString controlImagePath() const
controlImagePath
void setControlPathSuffix(const QString &theName)
void setColorTolerance(unsigned int theColorTolerance)
Set tolerance for color components used by runTest() and compareImages().
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
void setControlName(const QString &theName)
Base directory name for the control image (with control image path suffixed) the path to the image wi...
bool runTest(const QString &theTestName, unsigned int theMismatchCount=0)
Test using renderer to generate the image to be compared.
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
QChar separator()
void setMapSettings(const QgsMapSettings &mapSettings)
bool isNull() const
The QgsMapSettings class contains configuration for rendering of the map.
void setControlName(const QString &theName)
Base directory name for the control image (with control image path suffixed) the path to the image wi...
int count(const T &value) const
void setRenderedImage(const QString &theImageFileName)
bool compareImages(const QString &theTestName, unsigned int theMismatchCount=0, QString theRenderedImageFile="")
Test using two arbitary images (map renderer will not be used)
void setMapSettings(const QgsMapSettings &mapSettings)
Set the map settings to use to render the image.
void setControlPathPrefix(const QString &prefix)
bool runTest(const QString &theTestName, unsigned int theMismatchCount=0)
Test using renderer to generate the image to be compared.
const QString & renderedImage()
The path of the rendered image can be retrieved through that method.
void setControlPathPrefix(const QString &theName)
Prefix where the control images are kept.
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
const QVector< QgsDartMeasurement > & dartMeasurements() const
Get access to buffered dash messages.