File indexing completed on 2024-04-28 15:19:31

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kconfigguitest.h"
0008 #include <QtTestGui>
0009 
0010 #include <QDir>
0011 #include <QFont>
0012 #include <QStandardPaths>
0013 #include <kconfig.h>
0014 #include <kconfiggroup.h>
0015 #include <kconfigskeleton.h>
0016 
0017 QTEST_MAIN(KConfigTest)
0018 
0019 // clazy:excludeall=non-pod-global-static
0020 
0021 static const QColor s_color_entry1(QLatin1String{"steelblue"});
0022 static const QColor s_color_entry2(235, 235, 100, 125);
0023 static const QColor s_color_entry3(234, 234, 127);
0024 
0025 static QFont fontEntry()
0026 {
0027     return QFont{QStringLiteral("Times"), 16, QFont::Normal};
0028 }
0029 
0030 void KConfigTest::initTestCase()
0031 {
0032     QStandardPaths::setTestModeEnabled(true);
0033 
0034     // cheat the linker on windows to link against kconfiggui
0035     KConfigSkeleton foo;
0036     Q_UNUSED(foo);
0037 
0038     KConfig sc(QStringLiteral("kconfigtest"));
0039 
0040     KConfigGroup cg(&sc, "ComplexTypes");
0041     cg.writeEntry("colorEntry1", s_color_entry1);
0042     cg.writeEntry("colorEntry2", s_color_entry2);
0043     cg.writeEntry("colorEntry3", (QList<int>() << 234 << 234 << 127));
0044     cg.writeEntry("colorEntry4", (QList<int>() << 235 << 235 << 100 << 125));
0045     cg.writeEntry("fontEntry", fontEntry());
0046     QVERIFY(sc.sync());
0047 
0048     KConfig sc1(QStringLiteral("kdebugrc"));
0049     KConfigGroup sg0(&sc1, "0");
0050     sg0.writeEntry("AbortFatal", false);
0051     sg0.writeEntry("WarnOutput", 0);
0052     sg0.writeEntry("FatalOutput", 0);
0053     QVERIFY(sc1.sync());
0054 
0055     // Qt 5.8.0 would fail the fromString(toString) roundtrip in QFont
0056     // if the qApp font has a styleName set.
0057     // This is fixed by https://codereview.qt-project.org/181645
0058     // It's not in yet, and it depends on the app font, so rather than
0059     // a version check, let's do a runtime check.
0060     QFont orig(fontEntry());
0061     QFont f;
0062     f.fromString(orig.toString());
0063     m_fontFromStringBug = (f.toString() != orig.toString());
0064     if (m_fontFromStringBug) {
0065         qDebug() << "QFont::fromString serialization bug (Qt 5.8.0), the font test will be skipped" << f.toString() << "!=" << orig.toString();
0066     }
0067 }
0068 
0069 void KConfigTest::cleanupTestCase()
0070 {
0071     QDir dir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
0072     QVERIFY(dir.removeRecursively());
0073 }
0074 
0075 void KConfigTest::testComplex()
0076 {
0077     KConfig sc2(QStringLiteral("kconfigtest"));
0078     KConfigGroup sc3(&sc2, "ComplexTypes");
0079 
0080     QCOMPARE(QVariant(sc3.readEntry("colorEntry1", QColor(Qt::black))).toString(), QVariant(s_color_entry1).toString());
0081     QCOMPARE(sc3.readEntry("colorEntry1", QColor()), s_color_entry1);
0082     QCOMPARE(sc3.readEntry("colorEntry2", QColor()), s_color_entry2);
0083     QCOMPARE(sc3.readEntry("colorEntry3", QColor()), s_color_entry3);
0084     QCOMPARE(sc3.readEntry("colorEntry4", QColor()), s_color_entry2);
0085     QCOMPARE(sc3.readEntry("defaultColorTest", QColor("black")), QColor("black"));
0086     if (m_fontFromStringBug) {
0087         QEXPECT_FAIL("", "QFont fromString bug from Qt 5.8.0", Continue);
0088     }
0089     QCOMPARE(sc3.readEntry("fontEntry", QFont()), fontEntry());
0090 }
0091 
0092 void KConfigTest::testInvalid()
0093 {
0094     KConfig sc(QStringLiteral("kconfigtest"));
0095 
0096     // all of these should print a message to the kdebug.dbg file
0097     KConfigGroup sc3(&sc, "InvalidTypes");
0098 
0099     QList<int> list;
0100 
0101     // 1 element list
0102     list << 1;
0103     sc3.writeEntry(QStringLiteral("badList"), list);
0104     QVERIFY(sc.sync());
0105 
0106     QVERIFY(sc3.readEntry("badList", QColor()) == QColor());
0107 
0108     // 2 element list
0109     list << 2;
0110     sc3.writeEntry("badList", list);
0111     QVERIFY(sc.sync());
0112 
0113     QVERIFY(sc3.readEntry("badList", QColor()) == QColor());
0114 
0115     // 3 element list
0116     list << 303;
0117     sc3.writeEntry("badList", list);
0118     QVERIFY(sc.sync());
0119 
0120     QVERIFY(sc3.readEntry("badList", QColor()) == QColor()); // out of bounds
0121 
0122     // 4 element list
0123     list << 4;
0124     sc3.writeEntry("badList", list);
0125     QVERIFY(sc.sync());
0126 
0127     QVERIFY(sc3.readEntry("badList", QColor()) == QColor()); // out of bounds
0128 
0129     list[2] = -3;
0130     sc3.writeEntry("badList", list);
0131     QVERIFY(sc.sync());
0132     QVERIFY(sc3.readEntry("badList", QColor()) == QColor()); // out of bounds
0133 
0134     // 5 element list
0135     list[2] = 3;
0136     list << 5;
0137     sc3.writeEntry("badList", list);
0138     QVERIFY(sc.sync());
0139 
0140     QVERIFY(sc3.readEntry("badList", QColor()) == QColor());
0141 
0142     // 6 element list
0143     list << 6;
0144     sc3.writeEntry("badList", list);
0145     QVERIFY(sc.sync());
0146 
0147     QVERIFY(sc3.readEntry("badList", QColor()) == QColor());
0148 }
0149 
0150 #include "moc_kconfigguitest.cpp"