Skip to content
Permalink
Browse files

Add unit test for mbtiles, copyright header, project exception handling

  • Loading branch information
wonder-sk committed Jan 17, 2020
1 parent 4dc26a3 commit 9f7c6d9034b45bdddf098bef2bc890d5b8fd8116
@@ -1,3 +1,18 @@
/***************************************************************************
qgsmbtilesreader.cpp
--------------------------------------
Date : January 2020
Copyright : (C) 2020 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* 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 "qgsmbtilesreader.h"

#include "qgslogger.h"
@@ -1,3 +1,18 @@
/***************************************************************************
qgsmbtilesreader.h
--------------------------------------
Date : January 2020
Copyright : (C) 2020 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSMBTILESREADER_H
#define QGSMBTILESREADER_H

@@ -1363,10 +1363,19 @@ bool QgsWmsProvider::setupMBTilesCapabilities( const QString &uri )
QgsRectangle sourceExtentWgs84 = mbtilesReader.extent();
if ( !sourceExtentWgs84.isNull() )
{
QgsPointXY customTopLeft, customBottomRight;
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsCoordinateReferenceSystem( mSettings.mCrsId ),
transformContext() );
QgsPointXY customTopLeft = ct.transform( QgsPointXY( sourceExtentWgs84.xMinimum(), sourceExtentWgs84.yMaximum() ) );
QgsPointXY customBottomRight = ct.transform( QgsPointXY( sourceExtentWgs84.xMaximum(), sourceExtentWgs84.yMinimum() ) );
try
{
customTopLeft = ct.transform( QgsPointXY( sourceExtentWgs84.xMinimum(), sourceExtentWgs84.yMaximum() ) );
customBottomRight = ct.transform( QgsPointXY( sourceExtentWgs84.xMaximum(), sourceExtentWgs84.yMinimum() ) );
}
catch ( const QgsCsException & )
{
QgsDebugMsg( QStringLiteral( "Failed to reproject extent from MBTiles metadata" ) );
return false;
}
sourceExtent = QgsRectangle( customTopLeft.x(), customBottomRight.y(), customBottomRight.x(), customTopLeft.y() );
}

@@ -17,6 +17,8 @@
#include "qgstest.h"
#include <qgswmsprovider.h>
#include <qgsapplication.h>
#include <qgsmultirenderchecker.h>
#include <qgsrasterlayer.h>

/**
* \ingroup UnitTests
@@ -41,11 +43,22 @@ class TestQgsWmsProvider: public QObject

mCapabilities = new QgsWmsCapabilities();
QVERIFY( mCapabilities->parseResponse( content, config ) );

mReport += QLatin1String( "<h1>WMS Provider Tests</h1>\n" );
}

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

delete mCapabilities;
QgsApplication::exitQgis();
}
@@ -87,8 +100,49 @@ class TestQgsWmsProvider: public QObject
"STYLES=&FORMAT=&TRANSPARENT=TRUE" ) );
}

void testMBTiles()
{
QString dataDir( TEST_DATA_DIR );
QUrlQuery uq;
uq.addQueryItem( "type", "mbtiles" );
uq.addQueryItem( "url", QUrl::fromLocalFile( dataDir + "/isle_of_man_xxx_invalid.mbtiles" ).toString() );

// check first that we do not accept invalid mbtiles paths
QgsRasterLayer layerInvalid( uq.toString(), "invalid", "wms" );
//QgsWmsProvider providerInvalid( uq.toString(), QgsDataProvider::ProviderOptions() );
QVERIFY( !layerInvalid.isValid() );

uq.addQueryItem( "url", QUrl::fromLocalFile( dataDir + "/isle_of_man.mbtiles" ).toString() );
QgsRasterLayer layer( uq.toString(), "isle_of_man", "wms" );
QVERIFY( layer.isValid() );

QVERIFY( imageCheck( "mbtiles_1", &layer, layer.extent() ) );
}


bool imageCheck( const QString &testType, QgsMapLayer *layer, const QgsRectangle &extent )
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
QgsMapSettings mapSettings;
mapSettings.setLayers( QList<QgsMapLayer *>() << layer );
mapSettings.setExtent( extent );
mapSettings.setOutputSize( QSize( 400, 400 ) );
mapSettings.setOutputDpi( 96 );
QgsMultiRenderChecker myChecker;
myChecker.setControlPathPrefix( QStringLiteral( "wmsprovider" ) );
myChecker.setControlName( "expected_" + testType );
myChecker.setMapSettings( mapSettings );
myChecker.setColorTolerance( 20 );
bool myResultFlag = myChecker.runTest( testType, 500 );
mReport += myChecker.report();
return myResultFlag;
}

private:
QgsWmsCapabilities *mCapabilities = nullptr;

QString mReport;
};

QGSTEST_MAIN( TestQgsWmsProvider )
Binary file not shown.
Binary file not shown.

0 comments on commit 9f7c6d9

Please sign in to comment.
You can’t perform that action at this time.