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

0001 /*
0002    This file is part of the KDE libraries
0003    SPDX-FileCopyrightText: 2012 David Faure <faure@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QTest>
0009 #include <kconfiggroup.h>
0010 #include <ksharedconfig.h>
0011 
0012 class KSharedConfigTest : public QObject
0013 {
0014     Q_OBJECT
0015 private Q_SLOTS:
0016     void initTestCase();
0017     void testUnicity();
0018     void testReadWrite();
0019     void testReadWriteSync();
0020     void testQrcFile();
0021 
0022 private:
0023     QString m_path;
0024 };
0025 
0026 void KSharedConfigTest::initTestCase()
0027 {
0028     QStandardPaths::setTestModeEnabled(true);
0029 
0030     m_path =
0031         QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + QCoreApplication::applicationName() + QStringLiteral("rc");
0032     QFile::remove(m_path);
0033 }
0034 
0035 void KSharedConfigTest::testUnicity()
0036 {
0037     KSharedConfig::Ptr cfg1 = KSharedConfig::openConfig();
0038     KSharedConfig::Ptr cfg2 = KSharedConfig::openConfig();
0039     QCOMPARE(cfg1.data(), cfg2.data());
0040 }
0041 
0042 void KSharedConfigTest::testReadWrite()
0043 {
0044     const int value = 1;
0045     {
0046         KConfigGroup cg(KSharedConfig::openConfig(), "KSharedConfigTest");
0047         cg.writeEntry("NumKey", value);
0048     }
0049     {
0050         KConfigGroup cg(KSharedConfig::openConfig(), "KSharedConfigTest");
0051         QCOMPARE(cg.readEntry("NumKey", 0), 1);
0052     }
0053 }
0054 
0055 void KSharedConfigTest::testReadWriteSync()
0056 {
0057     const int value = 1;
0058     {
0059         KConfigGroup cg(KSharedConfig::openConfig(), "KSharedConfigTest");
0060         cg.writeEntry("NumKey", value);
0061     }
0062     QVERIFY(!QFile::exists(m_path));
0063     QVERIFY(KSharedConfig::openConfig()->sync());
0064     QVERIFY(QFile::exists(m_path));
0065     {
0066         KConfigGroup cg(KSharedConfig::openConfig(), "KSharedConfigTest");
0067         QCOMPARE(cg.readEntry("NumKey", 0), 1);
0068     }
0069 }
0070 
0071 void KSharedConfigTest::testQrcFile()
0072 {
0073     QVERIFY(QFile::exists(QStringLiteral(":/testdata/test.ini")));
0074     KSharedConfig::Ptr sharedConfig = KSharedConfig::openConfig(QStringLiteral(":/testdata/test.ini"), KConfig::NoGlobals);
0075     QVERIFY(sharedConfig);
0076 
0077     KConfigGroup cfg(sharedConfig, QStringLiteral("MainSection"));
0078     QCOMPARE(cfg.readEntry(QStringLiteral("TestEntry"), QStringLiteral("UnexpectedData")), QStringLiteral("ExpectedData"));
0079 }
0080 
0081 QTEST_MAIN(KSharedConfigTest)
0082 
0083 #include "ksharedconfigtest.moc"