|
| 1 | +/*************************************************************************** |
| 2 | + testqgsblendmodes.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : May 2013 |
| 5 | + Copyright : (C) 2013 by Nyall Dawson, Tim Sutton |
| 6 | + Email : nyall dot dawson at gmail.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 <QtTest> |
| 16 | +#include <QObject> |
| 17 | +#include <QString> |
| 18 | +#include <QStringList> |
| 19 | +#include <QApplication> |
| 20 | +#include <QDir> |
| 21 | +#include <QPainter> |
| 22 | + |
| 23 | +//qgis includes... |
| 24 | +#include <qgsmaprenderer.h> |
| 25 | +#include <qgsmaplayer.h> |
| 26 | +#include <qgsvectorlayer.h> |
| 27 | +#include <qgsapplication.h> |
| 28 | +#include <qgsproviderregistry.h> |
| 29 | +#include <qgsmaplayerregistry.h> |
| 30 | +#include <qgsmultibandcolorrenderer.h> |
| 31 | +#include <qgsrasterlayer.h> |
| 32 | +//qgis test includes |
| 33 | +#include "qgsrenderchecker.h" |
| 34 | + |
| 35 | +/** \ingroup UnitTests |
| 36 | + * This is a unit test for layer blend modes |
| 37 | + */ |
| 38 | +class TestQgsBlendModes: public QObject |
| 39 | +{ |
| 40 | + Q_OBJECT; |
| 41 | + private slots: |
| 42 | + void initTestCase();// will be called before the first testfunction is executed. |
| 43 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 44 | + void init() {};// will be called before each testfunction is executed. |
| 45 | + void cleanup() {};// will be called after every testfunction. |
| 46 | + |
| 47 | + void vectorBlending(); |
| 48 | + void rasterBlending(); |
| 49 | + private: |
| 50 | + bool imageCheck( QString theType ); //as above |
| 51 | + QgsMapRenderer * mpMapRenderer; |
| 52 | + QgsMapLayer * mpPointsLayer; |
| 53 | + QgsMapLayer * mpPolysLayer; |
| 54 | + QgsRasterLayer* mRasterLayer1; |
| 55 | + QgsRasterLayer* mRasterLayer2; |
| 56 | + QString mTestDataDir; |
| 57 | +}; |
| 58 | + |
| 59 | + |
| 60 | +void TestQgsBlendModes::initTestCase() |
| 61 | +{ |
| 62 | + |
| 63 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 64 | + QgsApplication::init(); |
| 65 | + QgsApplication::initQgis(); |
| 66 | + QgsApplication::showSettings(); |
| 67 | + |
| 68 | + //create some objects that will be used in tests |
| 69 | + |
| 70 | + //create a point layer |
| 71 | + QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt |
| 72 | + mTestDataDir = myDataDir + QDir::separator(); |
| 73 | + QString myPointsFileName = mTestDataDir + "points.shp"; |
| 74 | + QFileInfo myPointFileInfo( myPointsFileName ); |
| 75 | + mpPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(), |
| 76 | + myPointFileInfo.completeBaseName(), "ogr" ); |
| 77 | + QgsMapLayerRegistry::instance()->addMapLayers( |
| 78 | + QList<QgsMapLayer *>() << mpPointsLayer ); |
| 79 | + |
| 80 | + //create a poly layer that will be used in tests |
| 81 | + QString myPolysFileName = mTestDataDir + "polys.shp"; |
| 82 | + QFileInfo myPolyFileInfo( myPolysFileName ); |
| 83 | + mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(), |
| 84 | + myPolyFileInfo.completeBaseName(), "ogr" ); |
| 85 | + QgsMapLayerRegistry::instance()->addMapLayers( |
| 86 | + QList<QgsMapLayer *>() << mpPolysLayer ); |
| 87 | + |
| 88 | + //create two raster layers |
| 89 | + QFileInfo rasterFileInfo( mTestDataDir + "landsat.tif" ); |
| 90 | + mRasterLayer1 = new QgsRasterLayer( rasterFileInfo.filePath(), |
| 91 | + rasterFileInfo.completeBaseName() ); |
| 92 | + mRasterLayer2 = new QgsRasterLayer( rasterFileInfo.filePath(), |
| 93 | + rasterFileInfo.completeBaseName() ); |
| 94 | + QgsMultiBandColorRenderer* rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer1->dataProvider(), 2, 3, 4 ); |
| 95 | + mRasterLayer1->setRenderer( rasterRenderer ); |
| 96 | + mRasterLayer2->setRenderer( rasterRenderer ); |
| 97 | + QgsMapLayerRegistry::instance()->addMapLayers( |
| 98 | + QList<QgsMapLayer *>() << mRasterLayer1 ); |
| 99 | + QgsMapLayerRegistry::instance()->addMapLayers( |
| 100 | + QList<QgsMapLayer *>() << mRasterLayer2); |
| 101 | + |
| 102 | + mpMapRenderer = new QgsMapRenderer(); |
| 103 | +} |
| 104 | +void TestQgsBlendModes::cleanupTestCase() |
| 105 | +{ |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +void TestQgsBlendModes::vectorBlending() |
| 110 | +{ |
| 111 | + //Add two vector layers |
| 112 | + QStringList myLayers; |
| 113 | + myLayers << mpPointsLayer->id(); |
| 114 | + myLayers << mpPolysLayer->id(); |
| 115 | + mpMapRenderer->setLayerSet( myLayers ); |
| 116 | + |
| 117 | + //Set blending modes for both layers |
| 118 | + mpPointsLayer->setBlendMode( QPainter::CompositionMode_Overlay ); |
| 119 | + mpPolysLayer->setBlendMode( QPainter::CompositionMode_Multiply ); |
| 120 | + mpMapRenderer->setExtent( mpPointsLayer->extent() ); |
| 121 | + QVERIFY( imageCheck( "vector_blendmodes" ) ); |
| 122 | +} |
| 123 | + |
| 124 | +void TestQgsBlendModes::rasterBlending() |
| 125 | +{ |
| 126 | + //Add two raster layers to map renderer |
| 127 | + QStringList myLayers; |
| 128 | + myLayers << mRasterLayer1->id(); |
| 129 | + myLayers << mRasterLayer2->id(); |
| 130 | + mpMapRenderer->setLayerSet( myLayers ); |
| 131 | + mpMapRenderer->setExtent( mRasterLayer1->extent() ); |
| 132 | + |
| 133 | + // set blending mode for top layer |
| 134 | + mRasterLayer1->setBlendMode( QPainter::CompositionMode_Plus ); |
| 135 | + QVERIFY( imageCheck( "raster_blendmodes" ) ); |
| 136 | +} |
| 137 | + |
| 138 | +// |
| 139 | +// Private helper functions not called directly by CTest |
| 140 | +// |
| 141 | + |
| 142 | +bool TestQgsBlendModes::imageCheck( QString theTestType ) |
| 143 | +{ |
| 144 | + //use the QgsRenderChecker test utility class to |
| 145 | + //ensure the rendered output matches our control image |
| 146 | + QgsRenderChecker myChecker; |
| 147 | + myChecker.setControlName( "expected_" + theTestType ); |
| 148 | + myChecker.setMapRenderer( mpMapRenderer ); |
| 149 | + bool myResultFlag = myChecker.runTest( theTestType ); |
| 150 | + return myResultFlag; |
| 151 | +} |
| 152 | + |
| 153 | +QTEST_MAIN( TestQgsBlendModes ) |
| 154 | +#include "moc_testqgsblendmodes.cxx" |
0 commit comments