|
| 1 | +/*************************************************************************** |
| 2 | + testqgsstylev2.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : Wed Aug 1 12:13:24 BRT 2012 |
| 5 | + Copyright : (C) 2012 Etienne Tourigny and Tim Sutton |
| 6 | + Email : etourigny dot dev 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 <QStringList> |
| 18 | +#include <QObject> |
| 19 | +#include <QApplication> |
| 20 | +#include <QFileInfo> |
| 21 | + |
| 22 | +//qgis includes... |
| 23 | +#include <qgsapplication.h> |
| 24 | +#include "qgsconfig.h" |
| 25 | +#include "qgslogger.h" |
| 26 | +#include "qgsvectorcolorrampv2.h" |
| 27 | + |
| 28 | +#include "qgsstylev2.h" |
| 29 | + |
| 30 | +/** \ingroup UnitTests |
| 31 | + * This is a unit test to verify that styles are working correctly |
| 32 | + */ |
| 33 | +class TestStyleV2: public QObject |
| 34 | +{ |
| 35 | + Q_OBJECT; |
| 36 | + |
| 37 | + private: |
| 38 | + |
| 39 | + QgsStyleV2 *mStyle; |
| 40 | + |
| 41 | + bool testValidColor( QgsVectorColorRampV2 *ramp, double value, QColor expected ); |
| 42 | + |
| 43 | + private slots: |
| 44 | + |
| 45 | + // init / cleanup |
| 46 | + void initTestCase();// will be called before the first testfunction is executed. |
| 47 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 48 | + void init() {};// will be called before each testfunction is executed. |
| 49 | + void cleanup() {};// will be called after every testfunction. |
| 50 | + // void initStyles(); |
| 51 | + |
| 52 | + void testCreateColorRamps(); |
| 53 | + void testLoadColorRamps(); |
| 54 | + void testSaveLoad(); |
| 55 | +}; |
| 56 | + |
| 57 | + |
| 58 | +// slots |
| 59 | +void TestStyleV2::initTestCase() |
| 60 | +{ |
| 61 | + // initialize with test directory so we don't mess with user's stuff |
| 62 | + QgsApplication::init( QDir::homePath() + QString( "/.qgis_test" ) ); |
| 63 | + QgsApplication::initQgis(); |
| 64 | + |
| 65 | + // output test environment |
| 66 | + QgsApplication::showSettings(); |
| 67 | + |
| 68 | + // Set up the QSettings environment |
| 69 | + QCoreApplication::setOrganizationName( "QuantumGIS" ); |
| 70 | + QCoreApplication::setOrganizationDomain( "qgis.org" ); |
| 71 | + QCoreApplication::setApplicationName( "QGIS-TEST" ); |
| 72 | + |
| 73 | + // initialize with a clean style |
| 74 | + QFile styleFile( QgsApplication::userStyleV2Path() ); |
| 75 | + if ( styleFile.exists() ) |
| 76 | + { |
| 77 | + styleFile.remove(); |
| 78 | + QgsDebugMsg( "removed user style file " + styleFile.fileName() ); |
| 79 | + } |
| 80 | + mStyle = QgsStyleV2::defaultStyle(); |
| 81 | + // mStyle->clear(); |
| 82 | +} |
| 83 | + |
| 84 | +void TestStyleV2::cleanupTestCase() |
| 85 | +{ |
| 86 | + // don't save |
| 87 | + // mStyle->save(); |
| 88 | + delete mStyle; |
| 89 | +} |
| 90 | + |
| 91 | +bool TestStyleV2::testValidColor( QgsVectorColorRampV2 *ramp, double value, QColor expected ) |
| 92 | +{ |
| 93 | + QColor result = ramp->color( value ); |
| 94 | + if ( result != expected ) |
| 95 | + { |
| 96 | + QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg( |
| 97 | + result.name() ).arg( expected.name() ).toLocal8Bit().data() ); |
| 98 | + return false; |
| 99 | + } |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +void TestStyleV2::testCreateColorRamps() |
| 104 | +{ |
| 105 | + // gradient ramp |
| 106 | + QgsVectorGradientColorRampV2* gradientRamp = new QgsVectorGradientColorRampV2( QColor( Qt::red ), QColor( Qt::blue ) ); |
| 107 | + QgsVectorGradientColorRampV2::StopsMap stops; |
| 108 | + stops[ 0.5 ] = QColor( Qt::white ); |
| 109 | + gradientRamp->setStops( stops ); |
| 110 | + QVERIFY( mStyle->addColorRamp( "test_gradient", gradientRamp ) == true ); |
| 111 | + |
| 112 | + // random ramp |
| 113 | + QgsVectorRandomColorRampV2* randomRamp = new QgsVectorRandomColorRampV2(); |
| 114 | + QVERIFY( mStyle->addColorRamp( "test_random", randomRamp ) == true ); |
| 115 | + |
| 116 | + // color brewer ramp |
| 117 | + QgsVectorColorBrewerColorRampV2* cb1Ramp = new QgsVectorColorBrewerColorRampV2(); |
| 118 | + QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp ) == true ); |
| 119 | + QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "RdYlGn", 6 ); |
| 120 | + QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp ) == true ); |
| 121 | +} |
| 122 | + |
| 123 | +void TestStyleV2::testLoadColorRamps() |
| 124 | +{ |
| 125 | + QStringList colorRamps = mStyle->colorRampNames(); |
| 126 | + QStringList colorRampsTest = QStringList() << "BrBG" << "Spectral" |
| 127 | + << "test_gradient" << "test_random" << "test_cb1" << "test_cb2" ; |
| 128 | + |
| 129 | + // values for color tests |
| 130 | + QMultiMap< QString, QPair< double, QColor> > colorTests; |
| 131 | + colorTests.insert( "test_gradient", qMakePair ( 0.25, QColor( "#ff7f7f" ) ) ); |
| 132 | + colorTests.insert( "test_gradient", qMakePair ( 0.66, QColor( "#adadff" ) ) ); |
| 133 | + // cannot test random colors! |
| 134 | + colorTests.insert( "test_cb1", qMakePair ( 0.25, QColor( "#fdae61" ) ) ); |
| 135 | + colorTests.insert( "test_cb1", qMakePair ( 0.66, QColor( "#abdda4" ) ) ); |
| 136 | + colorTests.insert( "test_cb2", qMakePair ( 0.25, QColor( "#fc8d59" ) ) ); |
| 137 | + colorTests.insert( "test_cb2", qMakePair ( 0.66, QColor( "#d9ef8b" ) ) ); |
| 138 | + |
| 139 | + foreach( QString name, colorRampsTest ) |
| 140 | + { |
| 141 | + QgsDebugMsg( "colorRamp " + name ); |
| 142 | + QVERIFY( colorRamps.contains( name ) ); |
| 143 | + QgsVectorColorRampV2* ramp = mStyle->colorRamp( name ); |
| 144 | + QVERIFY( ramp != 0 ); |
| 145 | + // test colors |
| 146 | + if ( colorTests.contains( name ) ) |
| 147 | + { |
| 148 | + QList< QPair< double, QColor> > values = colorTests.values( name ); |
| 149 | + for ( int i = 0; i < values.size(); ++i ) |
| 150 | + { |
| 151 | + QVERIFY( testValidColor( ramp, values.at(i).first, values.at(i).second ) ); |
| 152 | + } |
| 153 | + } |
| 154 | + if ( ramp ) |
| 155 | + delete ramp; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void TestStyleV2::testSaveLoad() |
| 160 | +{ |
| 161 | + mStyle->save(); |
| 162 | + mStyle->clear(); |
| 163 | + mStyle->load( QgsApplication::userStyleV2Path() ); |
| 164 | + |
| 165 | + QStringList colorRamps = mStyle->colorRampNames(); |
| 166 | + QStringList colorRampsTest = QStringList() << "test_gradient"; |
| 167 | + |
| 168 | + foreach( QString name, colorRampsTest ) |
| 169 | + { |
| 170 | + QgsDebugMsg( "colorRamp " + name ); |
| 171 | + QVERIFY( colorRamps.contains( name ) ); |
| 172 | + QgsVectorColorRampV2* ramp = mStyle->colorRamp( name ); |
| 173 | + QVERIFY( ramp != 0 ); |
| 174 | + if ( ramp ) |
| 175 | + delete ramp; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +QTEST_MAIN( TestStyleV2 ) |
| 181 | +#include "moc_testqgsstylev2.cxx" |
0 commit comments