1 /***************************************************************************
2                          testqgscolorschemeregistry.cpp
3                          -----------------------
4     begin                : July 2014
5     copyright            : (C) 2014 by Nyall Dawson
6     email                : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "qgscolorschemeregistry.h"
19 #include "qgscolorscheme.h"
20 #include "qgis.h"
21 #include <QObject>
22 #include <memory>
23 #include "qgstest.h"
24 
25 //dummy color scheme for testing
26 class DummyColorScheme : public QgsColorScheme
27 {
28   public:
29 
30     DummyColorScheme() = default;
31 
schemeName() const32     QString schemeName() const override { return QStringLiteral( "Dummy scheme" ); }
33 
fetchColors(const QString & context=QString (),const QColor & baseColor=QColor ())34     QgsNamedColorList fetchColors( const QString &context = QString(),
35                                    const QColor &baseColor = QColor() ) override
36     {
37       QList< QPair< QColor, QString> > colors;
38       if ( context == QLatin1String( "testscheme" ) )
39       {
40         colors << qMakePair( QColor( 255, 255, 0 ), QStringLiteral( "schemetest" ) );
41       }
42       else if ( baseColor.isValid() )
43       {
44         colors << qMakePair( baseColor, QStringLiteral( "base" ) );
45       }
46       else
47       {
48         colors << qMakePair( QColor( 255, 0, 0 ), QStringLiteral( "red" ) ) << qMakePair( QColor( 0, 255, 0 ), QString() );
49       }
50       return colors;
51     }
52 
clone() const53     QgsColorScheme *clone() const override
54     {
55       return new DummyColorScheme();
56     }
57 
58 };
59 
60 class DummyColorScheme2 : public QgsColorScheme
61 {
62   public:
63 
64     DummyColorScheme2() = default;
65 
schemeName() const66     QString schemeName() const override { return QStringLiteral( "Dummy scheme2" ); }
67 
fetchColors(const QString &=QString (),const QColor &=QColor ())68     QgsNamedColorList fetchColors( const QString & = QString(),
69                                    const QColor & = QColor() ) override
70     {
71       QList< QPair< QColor, QString> > colors;
72       colors << qMakePair( QColor( 255, 255, 0 ), QStringLiteral( "schemetest" ) );
73       return colors;
74     }
75 
clone() const76     QgsColorScheme *clone() const override
77     {
78       return new DummyColorScheme2();
79     }
80 
81 };
82 
83 class TestQgsColorSchemeRegistry : public QObject
84 {
85     Q_OBJECT
86 
87   private slots:
88     void initTestCase();// will be called before the first testfunction is executed.
89     void cleanupTestCase();// will be called after the last testfunction was executed.
90     void init();// will be called before each testfunction is executed.
91     void cleanup();// will be called after every testfunction.
92     void createInstance(); // create global instance of QgsColorSchemeRegistry
93     void instanceHasDefaultSchemes(); // check that global instance is populated with default schemes
94     void createEmpty(); // check that creating an empty registry works
95     void addScheme(); // check adding a scheme to an empty registry
96     void addDefaultSchemes(); // check adding a scheme to an empty registry
97     void populateFromInstance(); // check populating an empty scheme from the registry
98     void removeScheme(); // check removing a scheme from a registry
99     void matchingSchemes(); //check fetching schemes of specific type
100     void fetchRandomStyleColor();
101 
102   private:
103 
104 };
105 
initTestCase()106 void TestQgsColorSchemeRegistry::initTestCase()
107 {
108   QgsApplication::init();
109   QgsApplication::initQgis();
110 }
111 
cleanupTestCase()112 void TestQgsColorSchemeRegistry::cleanupTestCase()
113 {
114 
115 }
116 
init()117 void TestQgsColorSchemeRegistry::init()
118 {
119 
120 }
121 
cleanup()122 void TestQgsColorSchemeRegistry::cleanup()
123 {
124 
125 }
126 
createInstance()127 void TestQgsColorSchemeRegistry::createInstance()
128 {
129   QgsColorSchemeRegistry *registry = QgsApplication::colorSchemeRegistry();
130   QVERIFY( registry );
131 }
132 
instanceHasDefaultSchemes()133 void TestQgsColorSchemeRegistry::instanceHasDefaultSchemes()
134 {
135   //check that scheme instance is initially populated with some schemes
136   //(assumes that there is some default schemes)
137   QgsColorSchemeRegistry *registry = QgsApplication::colorSchemeRegistry();
138   QVERIFY( registry->schemes().length() > 0 );
139 }
140 
createEmpty()141 void TestQgsColorSchemeRegistry::createEmpty()
142 {
143   //create an empty registry
144   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
145   QVERIFY( registry->schemes().length() == 0 );
146 }
147 
addScheme()148 void TestQgsColorSchemeRegistry::addScheme()
149 {
150   //create an empty registry
151   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
152   QVERIFY( registry->schemes().length() == 0 );
153   QgsColorScheme *recentScheme = new QgsRecentColorScheme();
154   registry->addColorScheme( recentScheme );
155   QVERIFY( registry->schemes().length() == 1 );
156 }
157 
addDefaultSchemes()158 void TestQgsColorSchemeRegistry::addDefaultSchemes()
159 {
160   //create an empty registry
161   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
162   QVERIFY( registry->schemes().length() == 0 );
163   //add default schemes
164   registry->addDefaultSchemes();
165   QVERIFY( registry->schemes().length() > 0 );
166 }
167 
populateFromInstance()168 void TestQgsColorSchemeRegistry::populateFromInstance()
169 {
170   //create an empty registry
171   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
172   QVERIFY( registry->schemes().length() == 0 );
173   //add schemes from instance
174   registry->populateFromInstance();
175   QCOMPARE( registry->schemes().length(), QgsApplication::colorSchemeRegistry()->schemes().length() );
176 }
177 
removeScheme()178 void TestQgsColorSchemeRegistry::removeScheme()
179 {
180   //create an empty registry
181   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
182   QVERIFY( registry->schemes().length() == 0 );
183   //add a scheme
184   QgsColorScheme *recentScheme = new QgsRecentColorScheme();
185   registry->addColorScheme( recentScheme );
186   QVERIFY( registry->schemes().length() == 1 );
187   //remove the scheme
188   QVERIFY( registry->removeColorScheme( recentScheme ) );
189   QVERIFY( registry->schemes().length() == 0 );
190   //try removing a scheme not in the registry
191   QVERIFY( !registry->removeColorScheme( recentScheme ) );
192   delete recentScheme;
193 }
194 
matchingSchemes()195 void TestQgsColorSchemeRegistry::matchingSchemes()
196 {
197   const std::shared_ptr<QgsColorSchemeRegistry> registry( new QgsColorSchemeRegistry() );
198   //add some schemes
199   QgsColorScheme *recentScheme = new QgsRecentColorScheme();
200   registry->addColorScheme( recentScheme );
201   DummyColorScheme *dummyScheme = new DummyColorScheme();
202   registry->addColorScheme( dummyScheme );
203   QVERIFY( registry->schemes().length() == 2 );
204   QList< QgsRecentColorScheme * > recentSchemes;
205   QList< DummyColorScheme * > dummySchemes;
206   registry->schemes( recentSchemes );
207   QVERIFY( recentSchemes.length() == 1 );
208   QCOMPARE( recentSchemes.at( 0 ), recentScheme );
209   registry->schemes( dummySchemes );
210   QVERIFY( dummySchemes.length() == 1 );
211   QCOMPARE( dummySchemes.at( 0 ), dummyScheme );
212 }
213 
fetchRandomStyleColor()214 void TestQgsColorSchemeRegistry::fetchRandomStyleColor()
215 {
216   std::unique_ptr<QgsColorSchemeRegistry> registry = std::make_unique< QgsColorSchemeRegistry >();
217 
218   // no randomStyleColorScheme set - test lots of colors to make sure their valid
219   for ( int i = 0; i < 10000; ++i )
220   {
221     QVERIFY( registry->fetchRandomStyleColor().isValid() );
222   }
223 
224   // set a randomStyleColorScheme
225   DummyColorScheme2 *dummyScheme = new DummyColorScheme2();
226   registry->addColorScheme( dummyScheme );
227   registry->setRandomStyleColorScheme( dummyScheme );
228 
229   // only one color in scheme
230 
231   for ( int i = 0; i < 10; ++i )
232   {
233     QCOMPARE( registry->fetchRandomStyleColor().name(), QStringLiteral( "#ffff00" ) );
234   }
235 
236   DummyColorScheme *dummyScheme2 = new DummyColorScheme();
237   registry->addColorScheme( dummyScheme2 );
238   registry->setRandomStyleColorScheme( dummyScheme2 );
239   for ( int i = 0; i < 10; ++i )
240   {
241     const QString color = registry->fetchRandomStyleColor().name();
242     QVERIFY( color == QLatin1String( "#ff0000" ) || color == QLatin1String( "#00ff00" ) );
243   }
244 
245   // remove current random style color scheme
246   registry->removeColorScheme( dummyScheme2 );
247   QVERIFY( !registry->randomStyleColorScheme() );
248   // no crash!
249   for ( int i = 0; i < 10; ++i )
250   {
251     QVERIFY( registry->fetchRandomStyleColor().isValid() );
252   }
253 
254   // we expect the default application color scheme registry to have a randomStyleColorScheme set
255   QVERIFY( QgsApplication::colorSchemeRegistry()->randomStyleColorScheme() );
256 }
257 
258 QGSTEST_MAIN( TestQgsColorSchemeRegistry )
259 #include "testqgscolorschemeregistry.moc"
260