File indexing completed on 2025-01-05 04:59:54

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_zanshin.h>
0008 
0009 #include <KConfig>
0010 #include <KConfigGroup>
0011 #include <KSharedConfig>
0012 
0013 #include "akonadi/akonadistoragesettings.h"
0014 
0015 using namespace Akonadi;
0016 
0017 Q_DECLARE_METATYPE(QSet<Akonadi::Collection::Id>)
0018 
0019 class AkonadiStorageSettingsTest : public QObject
0020 {
0021     Q_OBJECT
0022 public:
0023     explicit AkonadiStorageSettingsTest(QObject *parent = nullptr)
0024         : QObject(parent)
0025     {
0026         qRegisterMetaType<Akonadi::Collection>();
0027         qRegisterMetaType<Akonadi::Collection::List>();
0028         qRegisterMetaType<QSet<Akonadi::Collection::Id>>();
0029     }
0030 
0031 private:
0032     KConfigGroup configGroup()
0033     {
0034         return KConfigGroup(KSharedConfig::openConfig(), "General");
0035     }
0036 
0037     QList<Akonadi::Collection::Id> idList(int max)
0038     {
0039         QList<Akonadi::Collection::Id> list;
0040         list.reserve(max);
0041         for (int i = 1; i < max; i++) {
0042             list << i;
0043         }
0044         return list;
0045     }
0046 
0047     Akonadi::Collection::List colList(int max)
0048     {
0049         Akonadi::Collection::List list;
0050         list.reserve(max);
0051         foreach (auto id, idList(max)) {
0052             list << Collection(id);
0053         }
0054         return list;
0055     }
0056 
0057 private slots:
0058     void shouldReadFromConfigurationFile()
0059     {
0060         // GIVEN
0061 
0062         KConfigGroup g = configGroup();
0063 
0064         for (int i = 1; i <= 18; i += 3) {
0065             // WHEN
0066             g.writeEntry("defaultCollection", i);
0067 
0068             // THEN
0069             QCOMPARE(StorageSettings::instance().defaultCollection(), Collection(i));
0070         }
0071     }
0072 
0073     void shouldWriteToConfigurationFile()
0074     {
0075         // GIVEN
0076 
0077         KConfigGroup g = configGroup();
0078 
0079         for (int i = 1; i <= 18; i += 3) {
0080             // WHEN
0081             StorageSettings::instance().setDefaultCollection(Collection(i));
0082 
0083             // THEN
0084             QCOMPARE(g.readEntry("defaultCollection", -1), i);
0085         }
0086     }
0087 
0088     void shouldNotifyTaskCollectionChanges()
0089     {
0090         StorageSettings &settings = StorageSettings::instance();
0091         QSignalSpy spy(&settings, &Akonadi::StorageSettings::defaultCollectionChanged);
0092         settings.setDefaultCollection(Collection(2));
0093         QCOMPARE(spy.count(), 1);
0094         QCOMPARE(spy.first().first().value<Collection>(), Collection(2));
0095     }
0096 
0097     void shouldNotNotifyIdenticalTaskCollectionChanges()
0098     {
0099         StorageSettings &settings = StorageSettings::instance();
0100         settings.setDefaultCollection(Collection(4));
0101         QSignalSpy spy(&settings, &Akonadi::StorageSettings::defaultCollectionChanged);
0102         settings.setDefaultCollection(Collection(4));
0103         QCOMPARE(spy.count(), 0);
0104     }
0105 };
0106 
0107 ZANSHIN_TEST_MAIN(AkonadiStorageSettingsTest)
0108 
0109 #include "akonadistoragesettingstest.moc"