PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/core/testqgssymbol.cpp

http://github.com/qgis/Quantum-GIS
C++ | 457 lines | 347 code | 54 blank | 56 comment | 16 complexity | 7e8bac230474f990bd8a0486ac7040c8 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, GPL-2.0, CC-BY-SA-3.0, MIT, 0BSD, BSD-3-Clause
  1. /***************************************************************************
  2. testqgssymbol.cpp
  3. --------------------------------------
  4. Date : 2015-10-07
  5. Copyright : (C) 2015 Nyall Dawson
  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 "qgstest.h"
  16. #include <QObject>
  17. #include <QStringList>
  18. #include <QApplication>
  19. #include <QFileInfo>
  20. //qgis includes...
  21. #include "qgsmultirenderchecker.h"
  22. #include <qgsapplication.h>
  23. #include "qgsconfig.h"
  24. #include "qgslogger.h"
  25. #include "qgscolorramp.h"
  26. #include "qgscptcityarchive.h"
  27. #include "qgsvectorlayer.h"
  28. #include "qgsproject.h"
  29. #include "qgslinesymbollayer.h"
  30. #include "qgsfillsymbollayer.h"
  31. #include "qgssinglesymbolrenderer.h"
  32. #include "qgsmarkersymbollayer.h"
  33. #include "qgsstyle.h"
  34. /**
  35. * \ingroup UnitTests
  36. * This is a unit test to verify that symbols are working correctly
  37. */
  38. class TestQgsSymbol : public QObject
  39. {
  40. Q_OBJECT
  41. public:
  42. TestQgsSymbol();
  43. private:
  44. QString mReport;
  45. QString mTestDataDir;
  46. QgsVectorLayer *mpPointsLayer = nullptr;
  47. QgsVectorLayer *mpLinesLayer = nullptr;
  48. QgsVectorLayer *mpPolysLayer = nullptr;
  49. bool imageCheck( QgsMapSettings &ms, const QString &testName );
  50. private slots:
  51. // init / cleanup
  52. void initTestCase();// will be called before the first testfunction is executed.
  53. void cleanupTestCase();// will be called after the last testfunction was executed.
  54. void init() {}// will be called before each testfunction is executed.
  55. void cleanup() {}// will be called after every testfunction.
  56. // void initStyles();
  57. void testCanvasClip();
  58. void testParseColor();
  59. void testParseColorList();
  60. void symbolProperties();
  61. };
  62. TestQgsSymbol::TestQgsSymbol() = default;
  63. // slots
  64. void TestQgsSymbol::initTestCase()
  65. {
  66. // initialize with test settings directory so we don't mess with user's stuff
  67. QgsApplication::init( QDir::tempPath() + "/dot-qgis" );
  68. QgsApplication::initQgis();
  69. QgsApplication::createDatabase();
  70. mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
  71. // output test environment
  72. QgsApplication::showSettings();
  73. // Set up the QgsSettings environment
  74. QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
  75. QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
  76. QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
  77. // initialize with a clean style
  78. QFile styleFile( QgsApplication::userStylePath() );
  79. if ( styleFile.exists() )
  80. {
  81. styleFile.remove();
  82. QgsDebugMsg( "removed user style file " + styleFile.fileName() );
  83. }
  84. //
  85. //create a point layer that will be used in all tests...
  86. //
  87. QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
  88. mTestDataDir = myDataDir + '/';
  89. QString myPointsFileName = mTestDataDir + "points.shp";
  90. QFileInfo myPointFileInfo( myPointsFileName );
  91. mpPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
  92. myPointFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
  93. // Register the layer with the registry
  94. QgsProject::instance()->addMapLayers(
  95. QList<QgsMapLayer *>() << mpPointsLayer );
  96. //
  97. //create a poly layer that will be used in all tests...
  98. //
  99. QString myPolysFileName = mTestDataDir + "polys.shp";
  100. QFileInfo myPolyFileInfo( myPolysFileName );
  101. mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
  102. myPolyFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
  103. // Register the layer with the registry
  104. QgsProject::instance()->addMapLayers(
  105. QList<QgsMapLayer *>() << mpPolysLayer );
  106. //
  107. // Create a line layer that will be used in all tests...
  108. //
  109. QString myLinesFileName = mTestDataDir + "lines.shp";
  110. QFileInfo myLineFileInfo( myLinesFileName );
  111. mpLinesLayer = new QgsVectorLayer( myLineFileInfo.filePath(),
  112. myLineFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
  113. // Register the layer with the registry
  114. QgsProject::instance()->addMapLayers(
  115. QList<QgsMapLayer *>() << mpLinesLayer );
  116. mReport += QLatin1String( "<h1>StyleV2 Tests</h1>\n" );
  117. }
  118. void TestQgsSymbol::cleanupTestCase()
  119. {
  120. QgsApplication::exitQgis();
  121. QString myReportFile = QDir::tempPath() + "/qgistest.html";
  122. QFile myFile( myReportFile );
  123. if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
  124. {
  125. QTextStream myQTextStream( &myFile );
  126. myQTextStream << mReport;
  127. myFile.close();
  128. //QDesktopServices::openUrl( "file:///" + myReportFile );
  129. }
  130. }
  131. bool TestQgsSymbol::imageCheck( QgsMapSettings &ms, const QString &testName )
  132. {
  133. QgsMultiRenderChecker checker;
  134. ms.setOutputDpi( 96 );
  135. checker.setControlName( "expected_" + testName );
  136. checker.setMapSettings( ms );
  137. bool result = checker.runTest( testName, 0 );
  138. mReport += checker.report();
  139. return result;
  140. }
  141. void TestQgsSymbol::testCanvasClip()
  142. {
  143. //test rendering with and without clip to canvas enabled
  144. QgsMapSettings ms;
  145. QgsRectangle extent( -110.0, 25.0, -90, 40.0 );
  146. ms.setExtent( extent );
  147. ms.setFlag( QgsMapSettings::ForceVectorOutput );
  148. //line
  149. mReport += QLatin1String( "<h2>Line canvas clip</h2>\n" );
  150. ms.setLayers( QList<QgsMapLayer *>() << mpLinesLayer );
  151. QgsMarkerLineSymbolLayer *markerLine = new QgsMarkerLineSymbolLayer();
  152. markerLine->setPlacement( QgsTemplatedLineSymbolLayerBase::CentralPoint );
  153. static_cast< QgsSimpleMarkerSymbolLayer *>( markerLine->subSymbol()->symbolLayer( 0 ) )->setStrokeColor( Qt::black );
  154. QgsLineSymbol *lineSymbol = new QgsLineSymbol();
  155. lineSymbol->changeSymbolLayer( 0, markerLine );
  156. QgsSingleSymbolRenderer *renderer = new QgsSingleSymbolRenderer( lineSymbol );
  157. mpLinesLayer->setRenderer( renderer );
  158. bool result;
  159. lineSymbol->setClipFeaturesToExtent( true );
  160. result = imageCheck( ms, QStringLiteral( "style_linecanvasclip" ) );
  161. QVERIFY( result );
  162. lineSymbol->setClipFeaturesToExtent( false );
  163. result = imageCheck( ms, QStringLiteral( "style_linecanvasclip_off" ) );
  164. QVERIFY( result );
  165. //poly
  166. mReport += QLatin1String( "<h2>Polygon canvas clip</h2>\n" );
  167. ms.setLayers( QList<QgsMapLayer *>() << mpPolysLayer );
  168. QgsCentroidFillSymbolLayer *centroidFill = new QgsCentroidFillSymbolLayer();
  169. static_cast< QgsSimpleMarkerSymbolLayer * >( centroidFill->subSymbol()->symbolLayer( 0 ) )->setStrokeColor( Qt::black );
  170. QgsFillSymbol *fillSymbol = new QgsFillSymbol();
  171. fillSymbol->changeSymbolLayer( 0, centroidFill );
  172. renderer = new QgsSingleSymbolRenderer( fillSymbol );
  173. mpPolysLayer->setRenderer( renderer );
  174. extent = QgsRectangle( -106.0, 29.0, -94, 36.0 );
  175. ms.setExtent( extent );
  176. fillSymbol->setClipFeaturesToExtent( true );
  177. result = imageCheck( ms, QStringLiteral( "style_polycanvasclip" ) );
  178. QVERIFY( result );
  179. fillSymbol->setClipFeaturesToExtent( false );
  180. result = imageCheck( ms, QStringLiteral( "style_polycanvasclip_off" ) );
  181. QVERIFY( result );
  182. }
  183. void TestQgsSymbol::testParseColor()
  184. {
  185. // values for color tests
  186. QMap< QString, QPair< QColor, bool> > colorTests;
  187. colorTests.insert( QStringLiteral( "bad color" ), qMakePair( QColor(), false ) );
  188. colorTests.insert( QStringLiteral( "red" ), qMakePair( QColor( 255, 0, 0 ), false ) );
  189. colorTests.insert( QStringLiteral( "#ff00ff" ), qMakePair( QColor( 255, 0, 255 ), false ) );
  190. colorTests.insert( QStringLiteral( "#99AA00" ), qMakePair( QColor( 153, 170, 0 ), false ) );
  191. colorTests.insert( QStringLiteral( "#GG0000" ), qMakePair( QColor(), false ) );
  192. colorTests.insert( QStringLiteral( "000000" ), qMakePair( QColor( 0, 0, 0 ), false ) );
  193. colorTests.insert( QStringLiteral( "00ff00" ), qMakePair( QColor( 0, 255, 0 ), false ) );
  194. colorTests.insert( QStringLiteral( "00gg00" ), qMakePair( QColor(), false ) );
  195. colorTests.insert( QStringLiteral( "00ff000" ), qMakePair( QColor(), false ) );
  196. colorTests.insert( QStringLiteral( "fff" ), qMakePair( QColor( 255, 255, 255 ), false ) );
  197. colorTests.insert( QStringLiteral( "fff0" ), qMakePair( QColor(), false ) );
  198. // hex rrggbbaa colors
  199. colorTests.insert( QStringLiteral( "#ff00ffaa" ), qMakePair( QColor( 255, 0, 255, 170 ), true ) );
  200. colorTests.insert( QStringLiteral( "#99AA0099" ), qMakePair( QColor( 153, 170, 0, 153 ), true ) );
  201. colorTests.insert( QStringLiteral( "#GG0000aa" ), qMakePair( QColor(), false ) );
  202. colorTests.insert( QStringLiteral( "00000000" ), qMakePair( QColor( 0, 0, 0, 0 ), true ) );
  203. colorTests.insert( QStringLiteral( "00ff0011" ), qMakePair( QColor( 0, 255, 0, 17 ), true ) );
  204. colorTests.insert( QStringLiteral( "00gg0011" ), qMakePair( QColor(), false ) );
  205. colorTests.insert( QStringLiteral( "00ff00000" ), qMakePair( QColor(), false ) );
  206. colorTests.insert( QStringLiteral( "0,0,0" ), qMakePair( QColor( 0, 0, 0 ), false ) );
  207. colorTests.insert( QStringLiteral( "127,60,0" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  208. colorTests.insert( QStringLiteral( "255,255,255" ), qMakePair( QColor( 255, 255, 255 ), false ) );
  209. colorTests.insert( QStringLiteral( "256,60,0" ), qMakePair( QColor(), false ) );
  210. colorTests.insert( QStringLiteral( "rgb(127,60,0)" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  211. colorTests.insert( QStringLiteral( "rgb(255,255,255)" ), qMakePair( QColor( 255, 255, 255 ), false ) );
  212. colorTests.insert( QStringLiteral( "rgb(256,60,0)" ), qMakePair( QColor(), false ) );
  213. colorTests.insert( QStringLiteral( " rgb( 127, 60 , 0 ) " ), qMakePair( QColor( 127, 60, 0 ), false ) );
  214. colorTests.insert( QStringLiteral( "rgb(127,60,0);" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  215. colorTests.insert( QStringLiteral( "(127,60,0);" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  216. colorTests.insert( QStringLiteral( "(127,60,0)" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  217. colorTests.insert( QStringLiteral( "127,060,000" ), qMakePair( QColor( 127, 60, 0 ), false ) );
  218. colorTests.insert( QStringLiteral( "0,0,0,0" ), qMakePair( QColor( 0, 0, 0, 0 ), true ) );
  219. colorTests.insert( QStringLiteral( "127,60,0,0.5" ), qMakePair( QColor( 127, 60, 0, 128 ), true ) );
  220. colorTests.insert( QStringLiteral( "255,255,255,0.1" ), qMakePair( QColor( 255, 255, 255, 26 ), true ) );
  221. colorTests.insert( QStringLiteral( "rgba(127,60,0,1.0)" ), qMakePair( QColor( 127, 60, 0, 255 ), true ) );
  222. colorTests.insert( QStringLiteral( "rgba(255,255,255,0.0)" ), qMakePair( QColor( 255, 255, 255, 0 ), true ) );
  223. colorTests.insert( QStringLiteral( " rgba( 127, 60 , 0 , 0.2 ) " ), qMakePair( QColor( 127, 60, 0, 51 ), true ) );
  224. colorTests.insert( QStringLiteral( "rgba(127,60,0,0.1);" ), qMakePair( QColor( 127, 60, 0, 26 ), true ) );
  225. colorTests.insert( QStringLiteral( "(127,60,0,1);" ), qMakePair( QColor( 127, 60, 0, 255 ), true ) );
  226. colorTests.insert( QStringLiteral( "(127,60,0,1.0)" ), qMakePair( QColor( 127, 60, 0, 255 ), true ) );
  227. colorTests.insert( QStringLiteral( "127,060,000,1" ), qMakePair( QColor( 127, 60, 0, 255 ), true ) );
  228. colorTests.insert( QStringLiteral( "0%,0%,0%" ), qMakePair( QColor( 0, 0, 0 ), false ) );
  229. colorTests.insert( QStringLiteral( "50 %,60 %,0 %" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  230. colorTests.insert( QStringLiteral( "100%, 100%, 100%" ), qMakePair( QColor( 255, 255, 255 ), false ) );
  231. colorTests.insert( QStringLiteral( "rgb(50%,60%,0%)" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  232. colorTests.insert( QStringLiteral( "rgb(100%, 100%, 100%)" ), qMakePair( QColor( 255, 255, 255 ), false ) );
  233. colorTests.insert( QStringLiteral( " rgb( 50 % , 60 % , 0 % ) " ), qMakePair( QColor( 127, 153, 0 ), false ) );
  234. colorTests.insert( QStringLiteral( "rgb(50%,60%,0%);" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  235. colorTests.insert( QStringLiteral( "(50%,60%,0%);" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  236. colorTests.insert( QStringLiteral( "(50%,60%,0%)" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  237. colorTests.insert( QStringLiteral( "050%,060%,000%" ), qMakePair( QColor( 127, 153, 0 ), false ) );
  238. colorTests.insert( QStringLiteral( "0%,0%,0%,0" ), qMakePair( QColor( 0, 0, 0, 0 ), true ) );
  239. colorTests.insert( QStringLiteral( "50 %,60 %,0 %,0.5" ), qMakePair( QColor( 127, 153, 0, 128 ), true ) );
  240. colorTests.insert( QStringLiteral( "100%, 100%, 100%, 1.0" ), qMakePair( QColor( 255, 255, 255, 255 ), true ) );
  241. colorTests.insert( QStringLiteral( "rgba(50%,60%,0%, 1.0)" ), qMakePair( QColor( 127, 153, 0, 255 ), true ) );
  242. colorTests.insert( QStringLiteral( "rgba(100%, 100%, 100%, 0.0)" ), qMakePair( QColor( 255, 255, 255, 0 ), true ) );
  243. colorTests.insert( QStringLiteral( " rgba( 50 % , 60 % , 0 %, 0.5 ) " ), qMakePair( QColor( 127, 153, 0, 128 ), true ) );
  244. colorTests.insert( QStringLiteral( "rgba(50%,60%,0%,0);" ), qMakePair( QColor( 127, 153, 0, 0 ), true ) );
  245. colorTests.insert( QStringLiteral( "(50%,60%,0%,1);" ), qMakePair( QColor( 127, 153, 0, 255 ), true ) );
  246. colorTests.insert( QStringLiteral( "(50%,60%,0%,1.0)" ), qMakePair( QColor( 127, 153, 0, 255 ), true ) );
  247. colorTests.insert( QStringLiteral( "050%,060%,000%,0" ), qMakePair( QColor( 127, 153, 0, 0 ), true ) );
  248. QMap<QString, QPair< QColor, bool> >::const_iterator i = colorTests.constBegin();
  249. while ( i != colorTests.constEnd() )
  250. {
  251. QgsDebugMsg( "color string: " + i.key() );
  252. bool hasAlpha = false;
  253. QColor result = QgsSymbolLayerUtils::parseColorWithAlpha( i.key(), hasAlpha );
  254. QVERIFY( result == i.value().first );
  255. QVERIFY( hasAlpha == i.value().second );
  256. ++i;
  257. }
  258. }
  259. void TestQgsSymbol::testParseColorList()
  260. {
  261. //ensure that majority of single parseColor tests work for lists
  262. //note that some are not possible, as the colors may be ambiguous when treated as a list
  263. QMap< QString, QColor > colorTests;
  264. colorTests.insert( QStringLiteral( "bad color" ), QColor() );
  265. colorTests.insert( QStringLiteral( "red" ), QColor( 255, 0, 0 ) );
  266. colorTests.insert( QStringLiteral( "#ff00ff" ), QColor( 255, 0, 255 ) );
  267. colorTests.insert( QStringLiteral( "#99AA00" ), QColor( 153, 170, 0 ) );
  268. colorTests.insert( QStringLiteral( "#GG0000" ), QColor() );
  269. //colorTests.insert( "000000", QColor( 0, 0, 0 ) );
  270. //colorTests.insert( "00ff00", QColor( 0, 255, 0 ) );
  271. //colorTests.insert( "00gg00", QColor() );
  272. colorTests.insert( QStringLiteral( "00ff000" ), QColor() );
  273. //colorTests.insert( "fff", QColor( 255, 255, 255 ) );
  274. colorTests.insert( QStringLiteral( "fff0" ), QColor() );
  275. // hex rrggbbaa colors
  276. colorTests.insert( QStringLiteral( "#ff00ffaa" ), QColor( 255, 0, 255, 170 ) );
  277. colorTests.insert( QStringLiteral( "#99AA0099" ), QColor( 153, 170, 0, 153 ) );
  278. colorTests.insert( QStringLiteral( "#GG0000aa" ), QColor() );
  279. colorTests.insert( QStringLiteral( "00000000" ), QColor( 0, 0, 0, 0 ) );
  280. colorTests.insert( QStringLiteral( "00ff0011" ), QColor( 0, 255, 0, 17 ) );
  281. colorTests.insert( QStringLiteral( "00gg0011" ), QColor() );
  282. colorTests.insert( QStringLiteral( "00ff00000" ), QColor() );
  283. colorTests.insert( QStringLiteral( "0,0,0" ), QColor( 0, 0, 0 ) );
  284. colorTests.insert( QStringLiteral( "127,60,0" ), QColor( 127, 60, 0 ) );
  285. colorTests.insert( QStringLiteral( "255,255,255" ), QColor( 255, 255, 255 ) );
  286. //colorTests.insert( "256,60,0", QColor() );
  287. colorTests.insert( QStringLiteral( "rgb(127,60,0)" ), QColor( 127, 60, 0 ) );
  288. colorTests.insert( QStringLiteral( "rgb(255,255,255)" ), QColor( 255, 255, 255 ) );
  289. colorTests.insert( QStringLiteral( "rgb(256,60,0)" ), QColor() );
  290. colorTests.insert( QStringLiteral( " rgb( 127, 60 , 0 ) " ), QColor( 127, 60, 0 ) );
  291. colorTests.insert( QStringLiteral( "rgb(127,60,0);" ), QColor( 127, 60, 0 ) );
  292. colorTests.insert( QStringLiteral( "(127,60,0);" ), QColor( 127, 60, 0 ) );
  293. colorTests.insert( QStringLiteral( "(127,60,0)" ), QColor( 127, 60, 0 ) );
  294. colorTests.insert( QStringLiteral( "127,060,000" ), QColor( 127, 60, 0 ) );
  295. colorTests.insert( QStringLiteral( "0,0,0,0" ), QColor( 0, 0, 0, 0 ) );
  296. colorTests.insert( QStringLiteral( "127,60,0,0.5" ), QColor( 127, 60, 0, 128 ) );
  297. colorTests.insert( QStringLiteral( "255,255,255,0.1" ), QColor( 255, 255, 255, 26 ) );
  298. colorTests.insert( QStringLiteral( "rgba(127,60,0,1.0)" ), QColor( 127, 60, 0, 255 ) );
  299. colorTests.insert( QStringLiteral( "rgba(255,255,255,0.0)" ), QColor( 255, 255, 255, 0 ) );
  300. colorTests.insert( QStringLiteral( " rgba( 127, 60 , 0 , 0.2 ) " ), QColor( 127, 60, 0, 51 ) );
  301. colorTests.insert( QStringLiteral( "rgba(127,60,0,0.1);" ), QColor( 127, 60, 0, 26 ) );
  302. colorTests.insert( QStringLiteral( "(127,60,0,1);" ), QColor( 127, 60, 0, 255 ) );
  303. colorTests.insert( QStringLiteral( "(127,60,0,1.0)" ), QColor( 127, 60, 0, 255 ) );
  304. colorTests.insert( QStringLiteral( "127,060,000,1" ), QColor( 127, 60, 0, 255 ) );
  305. colorTests.insert( QStringLiteral( "0%,0%,0%" ), QColor( 0, 0, 0 ) );
  306. colorTests.insert( QStringLiteral( "50 %,60 %,0 %" ), QColor( 127, 153, 0 ) );
  307. colorTests.insert( QStringLiteral( "100%, 100%, 100%" ), QColor( 255, 255, 255 ) );
  308. colorTests.insert( QStringLiteral( "rgb(50%,60%,0%)" ), QColor( 127, 153, 0 ) );
  309. colorTests.insert( QStringLiteral( "rgb(100%, 100%, 100%)" ), QColor( 255, 255, 255 ) );
  310. colorTests.insert( QStringLiteral( " rgb( 50 % , 60 % , 0 % ) " ), QColor( 127, 153, 0 ) );
  311. colorTests.insert( QStringLiteral( "rgb(50%,60%,0%);" ), QColor( 127, 153, 0 ) );
  312. colorTests.insert( QStringLiteral( "(50%,60%,0%);" ), QColor( 127, 153, 0 ) );
  313. colorTests.insert( QStringLiteral( "(50%,60%,0%)" ), QColor( 127, 153, 0 ) );
  314. colorTests.insert( QStringLiteral( "050%,060%,000%" ), QColor( 127, 153, 0 ) );
  315. colorTests.insert( QStringLiteral( "0%,0%,0%,0" ), QColor( 0, 0, 0, 0 ) );
  316. colorTests.insert( QStringLiteral( "50 %,60 %,0 %,0.5" ), QColor( 127, 153, 0, 128 ) );
  317. colorTests.insert( QStringLiteral( "100%, 100%, 100%, 1.0" ), QColor( 255, 255, 255, 255 ) );
  318. colorTests.insert( QStringLiteral( "rgba(50%,60%,0%, 1.0)" ), QColor( 127, 153, 0, 255 ) );
  319. colorTests.insert( QStringLiteral( "rgba(100%, 100%, 100%, 0.0)" ), QColor( 255, 255, 255, 0 ) );
  320. colorTests.insert( QStringLiteral( " rgba( 50 % , 60 % , 0 %, 0.5 ) " ), QColor( 127, 153, 0, 128 ) );
  321. colorTests.insert( QStringLiteral( "rgba(50%,60%,0%,0);" ), QColor( 127, 153, 0, 0 ) );
  322. colorTests.insert( QStringLiteral( "(50%,60%,0%,1);" ), QColor( 127, 153, 0, 255 ) );
  323. colorTests.insert( QStringLiteral( "(50%,60%,0%,1.0)" ), QColor( 127, 153, 0, 255 ) );
  324. colorTests.insert( QStringLiteral( "050%,060%,000%,0" ), QColor( 127, 153, 0, 0 ) );
  325. QMap<QString, QColor >::const_iterator i = colorTests.constBegin();
  326. while ( i != colorTests.constEnd() )
  327. {
  328. QgsDebugMsg( "color list string: " + i.key() );
  329. QList< QColor > result = QgsSymbolLayerUtils::parseColorList( i.key() );
  330. if ( i.value().isValid() )
  331. {
  332. QCOMPARE( result.length(), 1 );
  333. QVERIFY( result.at( 0 ) == i.value() );
  334. }
  335. else
  336. {
  337. QCOMPARE( result.length(), 0 );
  338. }
  339. ++i;
  340. }
  341. QVector< QPair< QString, QList<QColor> > > colorListTests;
  342. QList<QColor> list1;
  343. list1 << QColor( QStringLiteral( "blue" ) ) << QColor( QStringLiteral( "red" ) ) << QColor( QStringLiteral( "green" ) );
  344. colorListTests.append( qMakePair( QStringLiteral( "blue red green" ), list1 ) );
  345. colorListTests.append( qMakePair( QStringLiteral( "blue,red,green" ), list1 ) );
  346. colorListTests.append( qMakePair( QStringLiteral( "blue\nred\ngreen" ), list1 ) );
  347. colorListTests.append( qMakePair( QStringLiteral( "blue\nred green" ), list1 ) );
  348. colorListTests.append( qMakePair( QStringLiteral( "blue\nred,green" ), list1 ) );
  349. QList<QColor> list2;
  350. list2 << QColor( QStringLiteral( "#ff0000" ) ) << QColor( QStringLiteral( "#00ff00" ) ) << QColor( QStringLiteral( "#0000ff" ) );
  351. colorListTests.append( qMakePair( QStringLiteral( "#ff0000 #00ff00 #0000ff" ), list2 ) );
  352. colorListTests.append( qMakePair( QStringLiteral( "#ff0000,#00ff00,#0000ff" ), list2 ) );
  353. colorListTests.append( qMakePair( QStringLiteral( "#ff0000\n#00ff00\n#0000ff" ), list2 ) );
  354. colorListTests.append( qMakePair( QStringLiteral( "#ff0000\n#00ff00 #0000ff" ), list2 ) );
  355. colorListTests.append( qMakePair( QStringLiteral( "#ff0000\n#00ff00,#0000ff" ), list2 ) );
  356. QList<QColor> list3;
  357. list3 << QColor( QStringLiteral( "#ff0000" ) ) << QColor( QStringLiteral( "#00ff00" ) ) << QColor( QStringLiteral( "#0000ff" ) );
  358. colorListTests.append( qMakePair( QStringLiteral( "rgb(255,0,0) rgb(0,255,0) rgb(0,0,255)" ), list3 ) );
  359. colorListTests.append( qMakePair( QStringLiteral( "rgb(255,0,0)\nrgb(0,255,0)\nrgb(0,0,255)" ), list3 ) );
  360. colorListTests.append( qMakePair( QStringLiteral( "rgb(255,0,0)\nrgb(0,255,0) rgb(0,0,255)" ), list3 ) );
  361. QVector< QPair< QString, QList<QColor> > >::const_iterator it = colorListTests.constBegin();
  362. while ( it != colorListTests.constEnd() )
  363. {
  364. QgsDebugMsg( "color list string: " + ( *it ).first );
  365. QList< QColor > result = QgsSymbolLayerUtils::parseColorList( ( *it ).first );
  366. if ( ( *it ).second.length() > 0 )
  367. {
  368. QCOMPARE( result.length(), ( *it ).second.length() );
  369. int index = 0;
  370. for ( QList<QColor>::const_iterator colorIt = ( *it ).second.constBegin(); colorIt != ( *it ).second.constEnd(); ++colorIt )
  371. {
  372. QVERIFY( result.at( index ) == ( *colorIt ) );
  373. index++;
  374. }
  375. }
  376. else
  377. {
  378. QCOMPARE( result.length(), 0 );
  379. }
  380. ++it;
  381. }
  382. }
  383. void TestQgsSymbol::symbolProperties()
  384. {
  385. //test QgsSymbolLayerUtils::symbolProperties
  386. //make a symbol
  387. QgsSimpleFillSymbolLayer *fill = new QgsSimpleFillSymbolLayer();
  388. fill->setColor( QColor( 25, 125, 225 ) );
  389. QgsFillSymbol *fillSymbol = new QgsFillSymbol();
  390. fillSymbol->changeSymbolLayer( 0, fill );
  391. QgsFillSymbol *fillSymbol2 = static_cast< QgsFillSymbol * >( fillSymbol->clone() );
  392. //test that two different symbol pointers return same properties
  393. QCOMPARE( QgsSymbolLayerUtils::symbolProperties( fillSymbol ),
  394. QgsSymbolLayerUtils::symbolProperties( fillSymbol2 ) );
  395. //modify one of the symbols
  396. fillSymbol2->symbolLayer( 0 )->setColor( QColor( 235, 135, 35 ) );
  397. QVERIFY( QgsSymbolLayerUtils::symbolProperties( fillSymbol ) !=
  398. QgsSymbolLayerUtils::symbolProperties( fillSymbol2 ) );
  399. delete fillSymbol;
  400. delete fillSymbol2;
  401. }
  402. QGSTEST_MAIN( TestQgsSymbol )
  403. #include "testqgssymbol.moc"