From 8579ec54838b7188ed016f7adb4a69bbf2e39712 Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Wed, 10 Oct 2018 14:48:49 +0100 Subject: 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 --- autotests/kconfigtest.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'autotests/kconfigtest.cpp') 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 #include #include @@ -30,6 +32,7 @@ #include #include +#include #ifdef Q_OS_UNIX #include @@ -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(); // 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 a, QList b) { + return a[0].value().name() < b[0].value().name(); + }); + + QCOMPARE(watcherSpy[0][0].value().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value(), QByteArrayList({"entryA"})); + + QCOMPARE(watcherSpy[1][0].value().name(), "aSubGroup"); + QCOMPARE(watcherSpy[1][0].value().parent().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[1][1].value(), 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().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value(), 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().name(), "aSubGroup"); + QCOMPARE(watcherSpy[0][1].value(), 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().name(), "TopLevelGroup"); + QCOMPARE(watcherSpy[0][1].value(), QByteArrayList({"someGlobalEntry"})); + + //watching another file should have only triggered from the kdeglobals change + QCOMPARE(otherWatcherSpy.count(), 1); + QCOMPARE(otherWatcherSpy[0][0].value().name(), "TopLevelGroup"); + QCOMPARE(otherWatcherSpy[0][1].value(), QByteArrayList({"someGlobalEntry"})); +} -- cgit v1.2.1