File indexing completed on 2024-12-22 04:10:24

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_properties_configuration_test.h"
0008 
0009 
0010 #include <simpletest.h>
0011 #include "kis_properties_configuration.h"
0012 
0013 KisPropertiesConfigurationTest::KisPropertiesConfigurationTest() :
0014         v1(10), v2("hello"), v3(1242.0), v4(true)
0015 {
0016     QList<QPointF> pts; pts.push_back(QPointF(0.2, 0.3)); pts.push_back(QPointF(0.5, 0.7));
0017     v5.setPoints(pts);
0018 }
0019 
0020 void KisPropertiesConfigurationTest::testSerialization()
0021 {
0022     KisPropertiesConfigurationSP config = createConfig();
0023     QString xml = config->toXML();
0024     KisPropertiesConfigurationSP decodedConfig = new KisPropertiesConfiguration();
0025     decodedConfig->fromXML(xml);
0026     testConfig(decodedConfig);
0027 
0028 }
0029 
0030 void KisPropertiesConfigurationTest::testSetGet()
0031 {
0032     KisPropertiesConfigurationSP config = createConfig();
0033     testConfig(config);
0034 }
0035 
0036 void KisPropertiesConfigurationTest::testDefaultValues()
0037 {
0038     KisPropertiesConfigurationSP config = new KisPropertiesConfiguration();
0039     QVERIFY(config->getInt("bouh", v1) == v1);
0040     QVERIFY(config->getString("bouh", v2) == v2);
0041     QVERIFY(config->getDouble("bouh", v3) == v3);
0042     QVERIFY(config->getBool("bouh", v4) == v4);
0043     QVERIFY(config->getCubicCurve("bouh", v5) == v5);
0044 }
0045 
0046 KisPropertiesConfigurationSP KisPropertiesConfigurationTest::createConfig()
0047 {
0048     KisPropertiesConfigurationSP config = new KisPropertiesConfiguration();
0049     config->setProperty("v1", v1);
0050     config->setProperty("v2", v2);
0051     config->setProperty("v3", v3);
0052     config->setProperty("v4", v4);
0053     config->setProperty("v5", QVariant::fromValue(v5));
0054     return config;
0055 }
0056 
0057 void KisPropertiesConfigurationTest::testConfig(KisPropertiesConfigurationSP config)
0058 {
0059     QVERIFY(config->getInt("v1", 0) == v1);
0060     QVERIFY(config->getString("v2", QString()) == v2);
0061     QVERIFY(config->getDouble("v3", 0.0) == v3);
0062     QVERIFY(config->getBool("v4", !v4) == v4);
0063     QVERIFY(config->getCubicCurve("v5") == v5);
0064 }
0065 
0066 void KisPropertiesConfigurationTest::testNotSavedValues()
0067 {
0068     KisPropertiesConfigurationSP config = createConfig();
0069     config->setPropertyNotSaved("v3");
0070     testConfig(config);
0071     QString s = config->toXML();
0072 
0073     config = new KisPropertiesConfiguration();
0074     config->fromXML(s);
0075     QVERIFY(config->getInt("v1", 0) == v1);
0076     QVERIFY(config->getString("v2", QString()) == v2);
0077     QVERIFY(config->hasProperty("v3") == false);
0078     QVERIFY(config->getBool("v4", !v4) == v4);
0079     QVERIFY(config->getCubicCurve("v5") == v5);
0080 
0081 }
0082 
0083 void KisPropertiesConfigurationTest::testCopy()
0084 {
0085     KisPropertiesConfigurationSP p1 = createConfig();
0086     p1->setProperty("v6", "bla");
0087     p1->setPropertyNotSaved("v6");
0088     KisPropertiesConfiguration config = KisPropertiesConfiguration(*p1.data());
0089     QVERIFY(config.getInt("v1", 0) == v1);
0090     QVERIFY(config.getString("v2", QString()) == v2);
0091     QVERIFY(config.getDouble("v3", 0.0) == v3);
0092     QVERIFY(config.getBool("v4", !v4) == v4);
0093     QVERIFY(config.getCubicCurve("v5") == v5);
0094     QVERIFY(config.hasProperty("v6") == true); // copying works!
0095 
0096     p1->setProperty("testBool1", true);
0097     p1->setProperty("testBool2", false);
0098 
0099     QString string = p1->toXML();
0100 
0101     KisPropertiesConfiguration p2;
0102     p2.fromXML(string);
0103     QVERIFY(p2.getInt("v1", 0) == v1);
0104     QVERIFY(p2.getString("v2", QString()) == v2);
0105     QVERIFY(p2.getDouble("v3", 0.0) == v3);
0106     QVERIFY(p2.getBool("v4", !v4) == v4);
0107     QVERIFY(p2.hasProperty("v6") == false); // round-tripping -- no
0108     QCOMPARE(p2.getBool("testBool1", false), true);
0109     QCOMPARE(p2.getBool("testBool2", true), false);
0110 }
0111 
0112 void KisPropertiesConfigurationTest::testGetColor()
0113 {
0114     KisPropertiesConfiguration pc;
0115     KoColor kc = KoColor(QColor(Qt::red), KoColorSpaceRegistry::instance()->rgb8());
0116     QVariant c = QVariant::fromValue<KoColor>(kc);
0117     pc.setProperty("colorAsKoColor", c);
0118     pc.setProperty("colorAsQColor", QColor(Qt::red));
0119     pc.setProperty("colorAsString", "#FF0000");
0120     pc.setProperty("colorAsXML", "<!DOCTYPE color><color><RGB space=\"sRGB-elle-V2-g10.icc\" g=\"0\" b=\"0\" r=\"1\"/></color>");
0121 
0122     kc = pc.getColor("colorAsKoColor");
0123     QVERIFY(kc.toQColor() == QColor(Qt::red));
0124     kc = pc.getColor("colorAsQColor");
0125     QVERIFY(kc.toQColor() == QColor(Qt::red));
0126     kc = pc.getColor("colorAsString");
0127     QVERIFY(kc.toQColor() == QColor(Qt::red));
0128     kc = pc.getColor("colorAsXML");
0129     QVERIFY(kc.toQColor() == QColor(Qt::red));
0130 }
0131 
0132 void roundTripStringList(const QStringList &refList)
0133 {
0134     KisPropertiesConfiguration config1;
0135     config1.setProperty("testProp", refList);
0136 
0137     const QString xmlData = config1.toXML();
0138 
0139     KisPropertiesConfiguration config2;
0140     config2.fromXML(xmlData);
0141 
0142     const QStringList results = config2.getStringList("testProp");
0143 
0144     QCOMPARE(results, refList);
0145 }
0146 
0147 void KisPropertiesConfigurationTest::testLists()
0148 {
0149     const QString str1("str1 str2\\ str3/ str4; str5% str6& str7;; str8\\; str9]]> str10");
0150     const QString str2("str1 str2\\ str3/ str4; str5% str6& str7;; str8\\; str9]]> str10;");
0151 
0152     {
0153         const QStringList refList({str1, str1});
0154         roundTripStringList(refList);
0155     }
0156 
0157     {
0158         const QStringList refList({str2, str2});
0159         roundTripStringList(refList);
0160     }
0161 
0162     {
0163         const QStringList refList({"", str2, str2});
0164         roundTripStringList(refList);
0165     }
0166 
0167     {
0168         const QStringList refList({str2, str2, ""});
0169         roundTripStringList(refList);
0170     }
0171 
0172     {
0173         const QStringList refList({str2, str2, ";"});
0174         roundTripStringList(refList);
0175     }
0176 
0177     {
0178         const QStringList refList({";", str2, str2});
0179         roundTripStringList(refList);
0180     }
0181 }
0182 
0183 SIMPLE_TEST_MAIN(KisPropertiesConfigurationTest)
0184