KRunner 5.109.0
abstractrunnertest.h
1/*
2 SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#ifndef PLASMA_ABSTRACTRUNNERTEST_H
7#define PLASMA_ABSTRACTRUNNERTEST_H
8
9#include <KPluginMetaData>
10#include <KRunner/AbstractRunner>
11#include <KRunner/RunnerManager>
12#include <QStandardPaths>
13
14#include <QSignalSpy>
15#include <QTest>
16#if KRUNNER_DBUS_RUNNER_TESTING
17#include <QDBusConnection>
18#include <QDBusServiceWatcher>
19#endif
20
21namespace
22{
28class AbstractRunnerTest : public QObject
29{
30public:
31 using QObject::QObject;
32 std::unique_ptr<Plasma::RunnerManager> manager = nullptr;
33 Plasma::AbstractRunner *runner = nullptr;
34
38 void initProperties()
39 {
40 qputenv("LC_ALL", "C.utf-8");
41 manager.reset(new Plasma::RunnerManager());
42
43#if KRUNNER_DBUS_RUNNER_TESTING
44 auto md = manager->convertDBusRunnerToJson(QStringLiteral(KRUNNER_TEST_DESKTOP_FILE));
45 QVERIFY(md.isValid());
46 manager->loadRunner(md);
47#else
48 const QString pluginId = QFileInfo(QStringLiteral(KRUNNER_TEST_RUNNER_PLUGIN_NAME)).completeBaseName();
49 auto metaData = KPluginMetaData::findPluginById(QStringLiteral(KRUNNER_TEST_RUNNER_PLUGIN_DIR), pluginId);
50 QVERIFY2(metaData.isValid(), qPrintable("Could not find plugin " + pluginId + " in folder " + KRUNNER_TEST_RUNNER_PLUGIN_DIR));
51
52 // Set internal variables
53 manager->loadRunner(metaData);
54#endif
55 QCOMPARE(manager->runners().count(), 1);
56 runner = manager->runners().constFirst();
57
58 // Just make sure all went well
59 QVERIFY(runner);
60 }
61
67 void launchQuery(const QString &query, const QString &runnerName = QString())
68 {
69 QSignalSpy spy(manager.get(), &Plasma::RunnerManager::queryFinished);
70 manager->launchQuery(query, runnerName);
71 QVERIFY2(spy.wait(), "RunnerManager did not emit the queryFinished signal");
72 }
73#if KRUNNER_DBUS_RUNNER_TESTING
80 QProcess *startDBusRunnerProcess(const QStringList &args = {}, const QString waitForService = QString())
81 {
82 qputenv("LC_ALL", "C.utf-8");
83 QProcess *process = new QProcess();
84 auto md = manager->convertDBusRunnerToJson(QStringLiteral(KRUNNER_TEST_DESKTOP_FILE));
85 QString serviceToWatch = waitForService;
86 if (serviceToWatch.isEmpty()) {
87 serviceToWatch = md.value(QStringLiteral("X-Plasma-DBusRunner-Service"));
88 }
89 QDBusServiceWatcher watcher(serviceToWatch, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration);
90
91 QEventLoop loop;
92 connect(&watcher, &QDBusServiceWatcher::serviceRegistered, &loop, &QEventLoop::quit);
93 process->start(QStringLiteral(KRUNNER_TEST_DBUS_EXECUTABLE), args);
94 loop.exec();
95
96 Q_ASSERT(process->state() == QProcess::ProcessState::Running);
97 m_runningProcesses << process;
98 return process;
99 }
100
104 void killRunningDBusProcesses()
105 {
106 for (auto &process : std::as_const(m_runningProcesses)) {
107 process->kill();
108 QVERIFY(process->waitForFinished());
109 }
110 qDeleteAll(m_runningProcesses);
111 m_runningProcesses.clear();
112 }
113
114private:
115 QList<QProcess *> m_runningProcesses;
116#endif
117};
118}
119
120#endif
An abstract base class for Plasma Runner plugins.
Definition abstractrunner.h:69
The RunnerManager class decides what installed runners are runnable, and their ratings.
Definition runnermanager.h:47
void queryFinished()
Emitted when the launchQuery finish.