KRunner

abstractrunnertest.h
1 /*
2  SPDX-FileCopyrightText: 2020 Alexander Lohnau <[email protected]>
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 
21 namespace
22 {
23 /**
24  * This class provides a basic structure for a runner test.
25  * The compile definitions should be configured using the `add_krunner_test` cmake macro
26  * @since 5.80
27  */
28 class AbstractRunnerTest : public QObject
29 {
30 public:
31  using QObject::QObject;
32  std::unique_ptr<Plasma::RunnerManager> manager = nullptr;
33  Plasma::AbstractRunner *runner = nullptr;
34 
35  /**
36  * Load the runner and set the manager and runner properties.
37  */
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 
62  /**
63  * Launch a query and wait for the RunnerManager to finish
64  * @param query
65  * @param runnerName
66  */
67  void launchQuery(const QString &query, const QString &runnerName = QString())
68  {
70  manager->launchQuery(query, runnerName);
71  QVERIFY2(spy.wait(), "RunnerManager did not emit the queryFinished signal");
72  }
73 #if KRUNNER_DBUS_RUNNER_TESTING
74  /**
75  * Launch the configured DBus executable with the given arguments and wait for the process to be started.
76  * @param args
77  * @param waitForService Wait for this service to be registered, this will default to the service from the metadata
78  * @return Process that was successfully started
79  */
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  }
90 
91  QEventLoop loop;
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 
101  /**
102  * Kill all processes that got started with the startDBusRunnerProcess
103  */
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 
114 private:
115  QList<QProcess *> m_runningProcesses;
116 #endif
117 };
118 }
119 
120 #endif
void start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode)
QProcess::ProcessState state() const const
The RunnerManager class decides what installed runners are runnable, and their ratings....
Definition: runnermanager.h:46
bool waitForFinished(int msecs)
QVERIFY(condition)
An abstract base class for Plasma Runner plugins.
int exec(QEventLoop::ProcessEventsFlags flags)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void queryFinished()
Emitted when the launchQuery finish.
QObject(QObject *parent)
QDBusConnection sessionBus()
QCOMPARE(actual, expected)
void quit()
bool isEmpty() const const
void kill()
QVERIFY2(condition, message)
void serviceRegistered(const QString &serviceName)
QString completeBaseName() const const
static KPluginMetaData findPluginById(const QString &directory, const QString &pluginId)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Dec 2 2023 03:50:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.