File indexing completed on 2024-07-21 04:19:54

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 "settings.h"
0007 
0008 #include "setting.h"
0009 #include "settingbase.h"
0010 #include <qfile.h>
0011 #include <qglobal.h>
0012 #include <qobject.h>
0013 #include <qsettings.h>
0014 #include <qsignalspy.h>
0015 #include <qstring.h>
0016 #include <qstringliteral.h>
0017 #include <qtest.h>
0018 #include <qtestcase.h>
0019 
0020 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0021 #include <qtmetamacros.h>
0022 #else
0023 #include <qobjectdefs.h>
0024 #endif
0025 
0026 namespace PerceptualColor
0027 {
0028 class TestSettings : public QObject
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033     explicit TestSettings(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 testConstructorDestructor()
0094     {
0095         // There should be no crash:
0096         const Settings mySettings(QSettings::UserScope, //
0097                                   organization, //
0098                                   application);
0099         Q_UNUSED(mySettings)
0100     }
0101 
0102 #ifndef MSVC_DLL
0103     // The automatic export of otherwise private symbols on MSVC
0104     // shared libraries via CMake's WINDOWS_EXPORT_ALL_SYMBOLS property
0105     // does not work well for Qt meta objects, resulting in non-functional
0106     // signals. Since the following unit tests require signals, it cannot be
0107     // built for MSVC shared libraries.
0108 
0109     void testIntegration()
0110     {
0111         // Test integration with Setting and SettingBase.
0112         Settings mySettings(QSettings::UserScope, //
0113                             organization, //
0114                             application);
0115 
0116         Setting<QString> tab(QStringLiteral("group/testSetting"), &mySettings);
0117 
0118         const QString newTab1 = QStringLiteral("testTab");
0119         tab.setValue(newTab1);
0120         QCOMPARE(tab.value(), newTab1);
0121 
0122         QSignalSpy spy(&tab, &PerceptualColor::SettingBase::valueChanged);
0123 
0124         const QString newTab2 = QStringLiteral("differentTestTab");
0125         tab.setValue(newTab2);
0126         tab.setValue(newTab2); // Intentional duplicate
0127         QCOMPARE(tab.value(), newTab2);
0128         QVERIFY(spy.isValid());
0129         // The second call to the setter with an identical value
0130         // should not trigger a signal.
0131         QCOMPARE(spy.count(), 1);
0132     }
0133 #endif
0134 
0135     void testInternalQSettings()
0136     {
0137         // Test integration with Setting and SettingBase.
0138         Settings mySettings(QSettings::UserScope, //
0139                             organization, //
0140                             application);
0141 
0142         QCOMPARE(mySettings.m_qSettings.organizationName(), organization);
0143         QCOMPARE(mySettings.m_qSettings.applicationName(), application);
0144         QCOMPARE(mySettings.m_qSettings.scope(), QSettings::UserScope);
0145     }
0146 };
0147 
0148 } // namespace PerceptualColor
0149 
0150 QTEST_MAIN(PerceptualColor::TestSettings)
0151 
0152 // The following “include” is necessary because we do not use a header file:
0153 #include "testsettings.moc"