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

0001 /*
0002     SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "kstandardshortcutwatcher.h"
0008 #include "kconfiggroup.h"
0009 #include "ksharedconfig.h"
0010 #include "kstandardshortcut_p.h"
0011 
0012 #include <QSignalSpy>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 class KStandardShortcutWatcherTest : public QObject
0017 {
0018     Q_OBJECT
0019 private Q_SLOTS:
0020     void initTestCase();
0021     void init();
0022     void testSignal();
0023     void testDataUpdated();
0024 };
0025 
0026 Q_DECLARE_METATYPE(KStandardShortcut::StandardShortcut)
0027 
0028 const QList<QKeySequence> newShortcut = {Qt::CTRL + Qt::Key_Adiaeresis};
0029 
0030 void KStandardShortcutWatcherTest::initTestCase()
0031 {
0032     QStandardPaths::setTestModeEnabled(true);
0033     qRegisterMetaType<KStandardShortcut::StandardShortcut>();
0034     QVERIFY(KStandardShortcut::hardcodedDefaultShortcut(KStandardShortcut::Open) != newShortcut);
0035 }
0036 
0037 void KStandardShortcutWatcherTest::init()
0038 {
0039     KConfigGroup group(KSharedConfig::openConfig(), "Shortcuts");
0040     group.writeEntry("Open", QKeySequence::listToString(KStandardShortcut::hardcodedDefaultShortcut(KStandardShortcut::Open)), KConfig::Global);
0041     group.sync();
0042     KStandardShortcut::initialize(KStandardShortcut::Open);
0043 }
0044 
0045 void KStandardShortcutWatcherTest::testSignal()
0046 {
0047 #ifdef Q_OS_WIN
0048     QSKIP("KConfig is built without DBus on Windows");
0049 #endif
0050     QSignalSpy signalSpy(KStandardShortcut::shortcutWatcher(), &KStandardShortcut::StandardShortcutWatcher::shortcutChanged);
0051     KStandardShortcut::saveShortcut(KStandardShortcut::Open, newShortcut);
0052     QTRY_COMPARE(signalSpy.count(), 1);
0053     const QList<QVariant> arguments = signalSpy.takeFirst();
0054     QCOMPARE(arguments[0].toInt(), KStandardShortcut::Open);
0055     QCOMPARE(arguments[1].value<QList<QKeySequence>>(), newShortcut);
0056 }
0057 
0058 void KStandardShortcutWatcherTest::testDataUpdated()
0059 {
0060 #ifdef Q_OS_WIN
0061     QSKIP("KConfig is built without DBus on Windows");
0062 #endif
0063     QSignalSpy signalSpy(KStandardShortcut::shortcutWatcher(), &KStandardShortcut::StandardShortcutWatcher::shortcutChanged);
0064     // Writing manually to forego automatic update in saveShortcut()
0065     KConfigGroup group(KSharedConfig::openConfig(), "Shortcuts");
0066     group.writeEntry("Open", QKeySequence::listToString(newShortcut), KConfig::Global | KConfig::Notify);
0067     group.sync();
0068     QTRY_COMPARE(signalSpy.count(), 1);
0069     QCOMPARE(KStandardShortcut::open(), newShortcut);
0070 }
0071 
0072 QTEST_MAIN(KStandardShortcutWatcherTest)
0073 #include "kstandardshortcutwatchertest.moc"