diff options
author | David Edmundson <kde@davidedmundson.co.uk> | 2018-10-10 14:48:49 +0100 |
---|---|---|
committer | David Edmundson <kde@davidedmundson.co.uk> | 2018-10-10 14:51:04 +0100 |
commit | 8579ec54838b7188ed016f7adb4a69bbf2e39712 (patch) | |
tree | b9614b014ba779dae40e96069fc2cadeeecea9de /autotests/kconfigtest.cpp | |
parent | 8e56083463374fa6525b5feff4373b5ab58914bb (diff) | |
download | kconfig-8579ec54838b7188ed016f7adb4a69bbf2e39712.tar.gz kconfig-8579ec54838b7188ed016f7adb4a69bbf2e39712.tar.bz2 |
Add mechanism to notify other clients of config changes over DBus
Summary:
Intention is not to create a registry like system, but to replace
KDElibs4Support::KGlobalSettings and to replace other system settingss
-> some app communication in a more generic way.
writeEntry gains an additional flag Notify which if set, will notify
clients of what has actually changed when we sync.
Rationale to put this into KConfig was so that we could have everything
batched and sychronised to the file sync and to get the fine detailed
exposure of what has actually changed which we don't get with a file
watcher.
Default behaviour remains identical without any broadcast messages.
As it is a new dependency it is purely optional and anything referencing
DBus is not in the public API. Our deployment on platforms without DBus
tend to be standalone applications anyway.
Test Plan: Attached unit test
Reviewers: broulik, dfaure
Reviewed By: broulik, dfaure
Subscribers: dfaure, broulik, zzag, kde-frameworks-devel
Tags: #frameworks
Differential Revision: https://phabricator.kde.org/D13034
Diffstat (limited to 'autotests/kconfigtest.cpp')
-rw-r--r-- | autotests/kconfigtest.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/autotests/kconfigtest.cpp b/autotests/kconfigtest.cpp index 35075d1a..01c08770 100644 --- a/autotests/kconfigtest.cpp +++ b/autotests/kconfigtest.cpp @@ -23,6 +23,8 @@ #include "kconfigtest.h" #include "helper.h" +#include "config-kconfig.h" + #include <QtTest> #include <qtemporarydir.h> #include <QStandardPaths> @@ -30,6 +32,7 @@ #include <ksharedconfig.h> #include <kconfiggroup.h> +#include <kconfigwatcher.h> #ifdef Q_OS_UNIX #include <utime.h> @@ -43,6 +46,8 @@ KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(KConfigTest, Flags) QTEST_MAIN(KConfigTest) +Q_DECLARE_METATYPE(KConfigGroup) + static QString homePath() { #ifdef Q_OS_WIN @@ -101,6 +106,7 @@ void KConfigTest::initTestCase() { // ensure we don't use files in the real config directory QStandardPaths::setTestModeEnabled(true); + qRegisterMetaType<KConfigGroup>(); // to make sure all files from a previous failed run are deleted cleanupTestCase(); @@ -1785,3 +1791,76 @@ void KConfigTest::testThreads() f.waitForFinished(); } } + +void KConfigTest::testNotify() +{ +#if !KCONFIG_USE_DBUS + QSKIP("KConfig notification requires DBus") +#endif + + KConfig config(TEST_SUBDIR "kconfigtest"); + auto myConfigGroup = KConfigGroup(&config, "TopLevelGroup"); + + //mimics a config in another process, which is watching for events + auto remoteConfig = KSharedConfig::openConfig(TEST_SUBDIR "kconfigtest"); + KConfigWatcher::Ptr watcher = KConfigWatcher::create(remoteConfig); + + //some random config that shouldn't be changing when kconfigtest changes, only on kdeglobals + auto otherRemoteConfig = KSharedConfig::openConfig(TEST_SUBDIR "kconfigtest2"); + KConfigWatcher::Ptr otherWatcher = KConfigWatcher::create(otherRemoteConfig); + + QSignalSpy watcherSpy(watcher.data(), &KConfigWatcher::configChanged); + QSignalSpy otherWatcherSpy(otherWatcher.data(), &KConfigWatcher::configChanged); + + //write entries in a group and subgroup + myConfigGroup.writeEntry("entryA", "foo", KConfig::Persistent | KConfig::Notify); + auto subGroup = myConfigGroup.group("aSubGroup"); + subGroup.writeEntry("entry1", "foo", KConfig::Persistent | KConfig::Notify); + subGroup.writeEntry("entry2", "foo", KConfig::Persistent | KConfig::Notify); + config.sync(); + watcherSpy.wait(); + QCOMPARE(watcherSpy.count(), 2); + + std::sort(watcherSpy.begin(), watcherSpy.end(), [] (QList<QVariant> a, QList<QVariant> b) { + return a[0].value<KConfigGroup>().name() < b[0].value<KConfigGroup>().name(); + }); + + QCOMPARE(watcherSpy[0][0].value<KConfigGroup>().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"entryA"})); + + QCOMPARE(watcherSpy[1][0].value<KConfigGroup>().name(), "aSubGroup"); + QCOMPARE(watcherSpy[1][0].value<KConfigGroup>().parent().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[1][1].value<QByteArrayList>(), QByteArrayList({"entry1", "entry2"})); + + //delete an entry + watcherSpy.clear(); + myConfigGroup.deleteEntry("entryA", KConfig::Persistent | KConfig::Notify); + config.sync(); + watcherSpy.wait(); + QCOMPARE(watcherSpy.count(), 1); + QCOMPARE(watcherSpy[0][0].value<KConfigGroup>().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"entryA"})); + + //deleting a group, should notify that every entry in that group has changed + watcherSpy.clear(); + myConfigGroup.deleteGroup("aSubGroup", KConfig::Persistent | KConfig::Notify); + config.sync(); + watcherSpy.wait(); + QCOMPARE(watcherSpy.count(), 1); + QCOMPARE(watcherSpy[0][0].value<KConfigGroup>().name(), "aSubGroup"); + QCOMPARE(watcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"entry1", "entry2"})); + + //global write still triggers our notification + watcherSpy.clear(); + myConfigGroup.writeEntry("someGlobalEntry", "foo", KConfig::Persistent | KConfig::Notify | KConfig::Global); + config.sync(); + watcherSpy.wait(); + QCOMPARE(watcherSpy.count(), 1); + QCOMPARE(watcherSpy[0][0].value<KConfigGroup>().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"someGlobalEntry"})); + + //watching another file should have only triggered from the kdeglobals change + QCOMPARE(otherWatcherSpy.count(), 1); + QCOMPARE(otherWatcherSpy[0][0].value<KConfigGroup>().name(), "TopLevelGroup"); + QCOMPARE(otherWatcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"someGlobalEntry"})); +} |