File indexing completed on 2024-12-08 10:17:15

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // First included header is the public header of the class we are testing;
0005 // this forces the header to be self-contained.
0006 #include "settingbase.h"
0007 
0008 #include "settings.h"
0009 #include <qfile.h>
0010 #include <qglobal.h>
0011 #include <qobject.h>
0012 #include <qpointer.h>
0013 #include <qsettings.h>
0014 #include <qstring.h>
0015 #include <qstringliteral.h>
0016 #include <qtest.h>
0017 #include <qtestcase.h>
0018 
0019 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0020 #include <qtmetamacros.h>
0021 #else
0022 #include <qobjectdefs.h>
0023 #include <qstring.h>
0024 #endif
0025 
0026 namespace PerceptualColor
0027 {
0028 class TestSettingBase : public QObject
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033     explicit TestSettingBase(QObject *parent = nullptr)
0034         : QObject(parent)
0035     {
0036     }
0037 
0038 private:
0039     static const inline QString organization = QStringLiteral("kde.org");
0040     // Avoid side-effects on the actual configuration file of the actual
0041     // library: Use a value different application name:
0042     static const inline QString application = QStringLiteral("libperceptualcolortestsettings");
0043 
0044 private Q_SLOTS:
0045     void initTestCase()
0046     {
0047         // Called before the first test function is executed
0048 
0049         const QString fileName = QSettings(QSettings::IniFormat, //
0050                                            QSettings::UserScope, //
0051                                            organization, //
0052                                            application)
0053                                      .fileName();
0054         QFile(fileName).remove();
0055     }
0056 
0057     void cleanupTestCase()
0058     {
0059         // Called after the last test function was executed
0060 
0061         const QString fileName = QSettings(QSettings::IniFormat, //
0062                                            QSettings::UserScope, //
0063                                            organization, //
0064                                            application)
0065                                      .fileName();
0066         QFile(fileName).remove();
0067     }
0068 
0069     void init()
0070     {
0071         // Called before each test function is executed
0072 
0073         const QString fileName = QSettings(QSettings::IniFormat, //
0074                                            QSettings::UserScope, //
0075                                            organization, //
0076                                            application)
0077                                      .fileName();
0078         QFile(fileName).remove();
0079     }
0080 
0081     void cleanup()
0082     {
0083         // Called after every test function
0084 
0085         const QString fileName = QSettings(QSettings::IniFormat, //
0086                                            QSettings::UserScope, //
0087                                            organization, //
0088                                            application)
0089                                      .fileName();
0090         QFile(fileName).remove();
0091     }
0092 
0093     void testConstuctorDestructor()
0094     {
0095         Settings mySettings(QSettings::UserScope, //
0096                             organization, //
0097                             application);
0098         {
0099             SettingBase mySettingBase(QStringLiteral("group/key"), //
0100                                       &mySettings,
0101                                       nullptr);
0102         }
0103     }
0104 
0105     void testParent()
0106     {
0107         Settings mySettings(QSettings::UserScope, //
0108                             organization, //
0109                             application);
0110         QPointer<SettingBase> mySettingBase;
0111         {
0112             QObject myParent;
0113             mySettingBase = new SettingBase( //
0114                 QStringLiteral("group/key"), //
0115                 &mySettings,
0116                 &myParent);
0117             QCOMPARE(mySettingBase.data()->parent(), &myParent);
0118         }
0119         QVERIFY(mySettingBase.isNull());
0120     }
0121 
0122     void testKey()
0123     {
0124         Settings mySettings(QSettings::UserScope, //
0125                             organization, //
0126                             application);
0127         SettingBase mySettingBase(QStringLiteral("group/key"), //
0128                                   &mySettings,
0129                                   nullptr);
0130         QCOMPARE(mySettingBase.m_key, QStringLiteral("group/key"));
0131     }
0132 };
0133 
0134 } // namespace PerceptualColor
0135 
0136 QTEST_MAIN(PerceptualColor::TestSettingBase)
0137 
0138 // The following “include” is necessary because we do not use a header file:
0139 #include "testsettingbase.moc"