Skip to content

Commit

Permalink
add rendering tests for HiDPI (device pixel ratio)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Oct 23, 2018
1 parent 3ff8477 commit 49e8e86
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/qgsrenderchecker.cpp
Expand Up @@ -182,7 +182,7 @@ bool QgsRenderChecker::runTest( const QString &testName,
//
mMapSettings.setBackgroundColor( qRgb( 152, 219, 249 ) );
mMapSettings.setFlag( QgsMapSettings::Antialiasing );
mMapSettings.setOutputSize( QSize( myExpectedImage.width(), myExpectedImage.height() ) );
mMapSettings.setOutputSize( QSize( myExpectedImage.width(), myExpectedImage.height() ) / mMapSettings.devicePixelRatio() );

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

QImage myImage = job.renderedImage();
Q_ASSERT( myImage.devicePixelRatioF() == mMapSettings.devicePixelRatio() );

//
// Save the pixmap to disk so the user can make a
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -147,6 +147,7 @@ SET(TESTS
testqgslegendrenderer.cpp
testqgscentroidfillsymbol.cpp
testqgslinefillsymbol.cpp
testqgsmapdevicepixelratio.cpp
testqgsmaplayerstylemanager.cpp
testqgsmaplayer.cpp
testqgsmaprendererjob.cpp
Expand Down
136 changes: 136 additions & 0 deletions tests/src/core/testqgsmapdevicepixelratio.cpp
@@ -0,0 +1,136 @@
/***************************************************************************
TestQgsMapDevicePixelRatio.cpp
--------------------------------------
Date : October 2018
Copyright : (C) 2018 by Denis Rouzaud
Email : denis@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QObject>
#include <QString>
#include <QStringList>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QIODevice>

#include "qgstest.h"
#include "qgsvectorlayer.h"
#include "qgsfontutils.h"
#include "qgsmapsettings.h"

//qgis unit test includes
#include <qgsrenderchecker.h>

/**
* \ingroup UnitTests
* This is a unit test for the map rotation feature
*/
class TestQgsMapDevicePixelRatio : public QObject
{
Q_OBJECT
public:
TestQgsMapDevicePixelRatio()
{
mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/';
}

~TestQgsMapDevicePixelRatio() override;

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void pointsLayer();

private:
bool render( const QString &fileName, float dpr );

QString mTestDataDir;
QgsVectorLayer *mPointsLayer = nullptr;
QgsMapSettings *mMapSettings = nullptr;
QString mReport;
};

//runs before all tests
void TestQgsMapDevicePixelRatio::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();

mMapSettings = new QgsMapSettings();

//create a point layer that will be used in all tests...
QString myPointsFileName = mTestDataDir + "points.shp";
QFileInfo myPointFileInfo( myPointsFileName );
mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
myPointFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );

mReport += QLatin1String( "<h1>Map Device Pixel Ratio Tests</h1>\n" );

QgsFontUtils::loadStandardTestFonts( QStringList() << QStringLiteral( "Bold" ) );
}

TestQgsMapDevicePixelRatio::~TestQgsMapDevicePixelRatio() = default;

//runs after all tests
void TestQgsMapDevicePixelRatio::cleanupTestCase()
{
delete mMapSettings;
delete mPointsLayer;
QgsApplication::exitQgis();

QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsMapDevicePixelRatio::pointsLayer()
{
mMapSettings->setLayers( QList<QgsMapLayer *>() << mPointsLayer );

QString qml = mTestDataDir + "points.qml";
bool success = false;
mPointsLayer->loadNamedStyle( qml, success );
QVERIFY( success );
QVERIFY( render( "dpr-normal", 1 ) );
QVERIFY( render( "dpr-2", 2 ) );
QVERIFY( render( "dpr-float", 2.5 ) );
}


bool TestQgsMapDevicePixelRatio::render( const QString &testType, float dpr )
{
mReport += "<h2>" + testType + "</h2>\n";
mMapSettings->setOutputSize( QSize( 256, 256 ) );
mMapSettings->setOutputDpi( 96 );
mMapSettings->setDevicePixelRatio( dpr );
mMapSettings->setExtent( QgsRectangle( -105.5, 37, -97.5, 45 ) );
qDebug() << "scale" << QString::number( mMapSettings->scale(), 'f' );
QgsRenderChecker checker;
checker.setControlPathPrefix( QStringLiteral( "mapdevicepixelratio" ) );
checker.setControlName( "expected_" + testType );
checker.setMapSettings( *mMapSettings );
bool result = checker.runTest( testType );
mReport += "\n\n\n" + checker.report();
return result;
}

QGSTEST_MAIN( TestQgsMapDevicePixelRatio )
#include "testqgsmapdevicepixelratio.moc"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49e8e86

Please sign in to comment.