Skip to content

Commit 49e8e86

Browse files
committed
add rendering tests for HiDPI (device pixel ratio)
1 parent 3ff8477 commit 49e8e86

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

src/core/qgsrenderchecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ bool QgsRenderChecker::runTest( const QString &testName,
182182
//
183183
mMapSettings.setBackgroundColor( qRgb( 152, 219, 249 ) );
184184
mMapSettings.setFlag( QgsMapSettings::Antialiasing );
185-
mMapSettings.setOutputSize( QSize( myExpectedImage.width(), myExpectedImage.height() ) );
185+
mMapSettings.setOutputSize( QSize( myExpectedImage.width(), myExpectedImage.height() ) / mMapSettings.devicePixelRatio() );
186186

187187
QTime myTime;
188188
myTime.start();
@@ -194,6 +194,7 @@ bool QgsRenderChecker::runTest( const QString &testName,
194194
mElapsedTime = myTime.elapsed();
195195

196196
QImage myImage = job.renderedImage();
197+
Q_ASSERT( myImage.devicePixelRatioF() == mMapSettings.devicePixelRatio() );
197198

198199
//
199200
// Save the pixmap to disk so the user can make a

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ SET(TESTS
147147
testqgslegendrenderer.cpp
148148
testqgscentroidfillsymbol.cpp
149149
testqgslinefillsymbol.cpp
150+
testqgsmapdevicepixelratio.cpp
150151
testqgsmaplayerstylemanager.cpp
151152
testqgsmaplayer.cpp
152153
testqgsmaprendererjob.cpp
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/***************************************************************************
2+
TestQgsMapDevicePixelRatio.cpp
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 by Denis Rouzaud
6+
Email : denis@opengis.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 <QObject>
17+
#include <QString>
18+
#include <QStringList>
19+
#include <QApplication>
20+
#include <QFileInfo>
21+
#include <QDir>
22+
#include <QFile>
23+
#include <QIODevice>
24+
25+
#include "qgstest.h"
26+
#include "qgsvectorlayer.h"
27+
#include "qgsfontutils.h"
28+
#include "qgsmapsettings.h"
29+
30+
//qgis unit test includes
31+
#include <qgsrenderchecker.h>
32+
33+
/**
34+
* \ingroup UnitTests
35+
* This is a unit test for the map rotation feature
36+
*/
37+
class TestQgsMapDevicePixelRatio : public QObject
38+
{
39+
Q_OBJECT
40+
public:
41+
TestQgsMapDevicePixelRatio()
42+
{
43+
mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/';
44+
}
45+
46+
~TestQgsMapDevicePixelRatio() override;
47+
48+
private slots:
49+
void initTestCase();// will be called before the first testfunction is executed.
50+
void cleanupTestCase();// will be called after the last testfunction was executed.
51+
void init() {} // will be called before each testfunction is executed.
52+
void cleanup() {} // will be called after every testfunction.
53+
54+
void pointsLayer();
55+
56+
private:
57+
bool render( const QString &fileName, float dpr );
58+
59+
QString mTestDataDir;
60+
QgsVectorLayer *mPointsLayer = nullptr;
61+
QgsMapSettings *mMapSettings = nullptr;
62+
QString mReport;
63+
};
64+
65+
//runs before all tests
66+
void TestQgsMapDevicePixelRatio::initTestCase()
67+
{
68+
// init QGIS's paths - true means that all path will be inited from prefix
69+
QgsApplication::init();
70+
QgsApplication::initQgis();
71+
72+
mMapSettings = new QgsMapSettings();
73+
74+
//create a point layer that will be used in all tests...
75+
QString myPointsFileName = mTestDataDir + "points.shp";
76+
QFileInfo myPointFileInfo( myPointsFileName );
77+
mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
78+
myPointFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
79+
80+
mReport += QLatin1String( "<h1>Map Device Pixel Ratio Tests</h1>\n" );
81+
82+
QgsFontUtils::loadStandardTestFonts( QStringList() << QStringLiteral( "Bold" ) );
83+
}
84+
85+
TestQgsMapDevicePixelRatio::~TestQgsMapDevicePixelRatio() = default;
86+
87+
//runs after all tests
88+
void TestQgsMapDevicePixelRatio::cleanupTestCase()
89+
{
90+
delete mMapSettings;
91+
delete mPointsLayer;
92+
QgsApplication::exitQgis();
93+
94+
QString myReportFile = QDir::tempPath() + "/qgistest.html";
95+
QFile myFile( myReportFile );
96+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
97+
{
98+
QTextStream myQTextStream( &myFile );
99+
myQTextStream << mReport;
100+
myFile.close();
101+
}
102+
}
103+
104+
void TestQgsMapDevicePixelRatio::pointsLayer()
105+
{
106+
mMapSettings->setLayers( QList<QgsMapLayer *>() << mPointsLayer );
107+
108+
QString qml = mTestDataDir + "points.qml";
109+
bool success = false;
110+
mPointsLayer->loadNamedStyle( qml, success );
111+
QVERIFY( success );
112+
QVERIFY( render( "dpr-normal", 1 ) );
113+
QVERIFY( render( "dpr-2", 2 ) );
114+
QVERIFY( render( "dpr-float", 2.5 ) );
115+
}
116+
117+
118+
bool TestQgsMapDevicePixelRatio::render( const QString &testType, float dpr )
119+
{
120+
mReport += "<h2>" + testType + "</h2>\n";
121+
mMapSettings->setOutputSize( QSize( 256, 256 ) );
122+
mMapSettings->setOutputDpi( 96 );
123+
mMapSettings->setDevicePixelRatio( dpr );
124+
mMapSettings->setExtent( QgsRectangle( -105.5, 37, -97.5, 45 ) );
125+
qDebug() << "scale" << QString::number( mMapSettings->scale(), 'f' );
126+
QgsRenderChecker checker;
127+
checker.setControlPathPrefix( QStringLiteral( "mapdevicepixelratio" ) );
128+
checker.setControlName( "expected_" + testType );
129+
checker.setMapSettings( *mMapSettings );
130+
bool result = checker.runTest( testType );
131+
mReport += "\n\n\n" + checker.report();
132+
return result;
133+
}
134+
135+
QGSTEST_MAIN( TestQgsMapDevicePixelRatio )
136+
#include "testqgsmapdevicepixelratio.moc"
Loading
Loading
Loading

0 commit comments

Comments
 (0)