File indexing completed on 2024-04-14 03:54:30

0001 /*
0002     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0003     SPDX-License-Identifier: LGPL-2.1-or-later
0004 */
0005 
0006 #include "runnermanager.h"
0007 
0008 #include <KConfigGroup>
0009 #include <KSharedConfig>
0010 #include <QCoreApplication>
0011 #include <QObject>
0012 #include <QProcess>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 #include "abstractrunnertest.h"
0017 #include "kpluginmetadata_utils_p.h"
0018 
0019 using namespace KRunner;
0020 
0021 class RunnerManagerSingleRunnerModeTest : public AbstractRunnerTest
0022 {
0023     Q_OBJECT
0024 private Q_SLOTS:
0025     void loadTwoRunners()
0026     {
0027         startDBusRunnerProcess({QStringLiteral("net.krunnertests.dave")});
0028         startDBusRunnerProcess({QStringLiteral("net.krunnertests.multi.a1")}, QStringLiteral("net.krunnertests.multi.a1"));
0029         qputenv("XDG_DATA_DIRS", QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation).toLocal8Bit());
0030         QCoreApplication::setLibraryPaths(QStringList());
0031         initProperties();
0032         auto md = parseMetaDataFromDesktopFile(QFINDTESTDATA("plugins/dbusrunnertestmulti.desktop"));
0033         QVERIFY(md.isValid());
0034         manager->loadRunner(md);
0035     }
0036 
0037     void cleanup()
0038     {
0039         killRunningDBusProcesses();
0040         const QString configFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, "runnermanagersinglerunnermodetestrcm");
0041         if (!configFile.isEmpty()) {
0042             QFile::remove(configFile);
0043         }
0044     }
0045 
0046     void testAllRunnerResults();
0047     void testSingleRunnerResults();
0048     void testNonExistentRunnerId();
0049     void testLoadingDisabledRunner();
0050 };
0051 
0052 void RunnerManagerSingleRunnerModeTest::testAllRunnerResults()
0053 {
0054     loadTwoRunners();
0055     launchQuery("foo");
0056     const auto matches = manager->matches();
0057     QCOMPARE(matches.count(), 2);
0058     const QStringList ids = {matches.at(0).runner()->id(), matches.at(1).runner()->id()};
0059     // Both runners should have produced one result
0060     QVERIFY(ids.contains("dbusrunnertest"));
0061     QVERIFY(ids.contains("dbusrunnertestmulti"));
0062 }
0063 
0064 void RunnerManagerSingleRunnerModeTest::testSingleRunnerResults()
0065 {
0066     loadTwoRunners();
0067     launchQuery("foo", "dbusrunnertest");
0068     QCOMPARE(manager->matches().count(), 1);
0069     QCOMPARE(manager->matches().constFirst().runner()->id(), "dbusrunnertest");
0070     launchQuery("foo", "dbusrunnertestmulti");
0071     QCOMPARE(manager->matches().count(), 1);
0072     QCOMPARE(manager->matches().constFirst().runner()->id(), "dbusrunnertestmulti");
0073 }
0074 
0075 void RunnerManagerSingleRunnerModeTest::testNonExistentRunnerId()
0076 {
0077     loadTwoRunners();
0078     manager->launchQuery("foo", "bla"); // This internally calls reset, we just wait a bit and make sure there are no matches
0079     QTest::qSleep(250);
0080     QVERIFY(manager->matches().isEmpty());
0081 }
0082 
0083 void RunnerManagerSingleRunnerModeTest::testLoadingDisabledRunner()
0084 {
0085     loadTwoRunners();
0086     // Make sure the runner is disabled in the config
0087     auto config = KSharedConfig::openConfig();
0088     config->deleteGroup("Plugins");
0089     config->group("Plugins").writeEntry("dbusrunnertestEnabled", false);
0090     // reset our manager to start clean
0091     manager.reset(new RunnerManager(config->group("Plugins"), config->group("State"), this));
0092     // Ensure no system runners are picked up
0093     manager->setAllowedRunners(QStringList{"dbusrunnertest", "dbusrunnertestmulti"});
0094     // Copy the service files to the appropriate location and only load runners from there
0095     QString location = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/krunner/dbusplugins/";
0096     QDir().mkpath(location);
0097     QFile::copy(QFINDTESTDATA("plugins/dbusrunnertest.desktop"), location + "dbusrunnertest.desktop");
0098     QFile::copy(QFINDTESTDATA("plugins/dbusrunnertestmulti.desktop"), location + "dbusrunnertestmulti.desktop");
0099 
0100     // Only enabled runner should be loaded and have results
0101     auto matches = launchQuery("foo");
0102     QCOMPARE(manager->runners().count(), 1);
0103     QCOMPARE(manager->runners().constFirst()->id(), "dbusrunnertestmulti");
0104     QCOMPARE(matches.count(), 1);
0105     QCOMPARE(matches.constFirst().runner()->id(), "dbusrunnertestmulti");
0106 
0107     // Only enabled runner should be loaded and have results
0108     matches = launchQuery("foo", "dbusrunnertest");
0109     QCOMPARE(manager->runners().count(), 2);
0110     const QStringList ids{manager->runners().at(0)->id(), manager->runners().at(1)->id()};
0111     QVERIFY(ids.contains("dbusrunnertestmulti"));
0112     QVERIFY(ids.contains("dbusrunnertest"));
0113     QCOMPARE(matches.count(), 1);
0114     QCOMPARE(matches.constFirst().runner()->id(), "dbusrunnertest");
0115 
0116     // Only enabled runner should used for querying
0117     matches = launchQuery("foo");
0118     QCOMPARE(manager->runners().count(), 2);
0119     QCOMPARE(matches.count(), 1);
0120     QCOMPARE(matches.constFirst().runner()->id(), "dbusrunnertestmulti");
0121 }
0122 
0123 QTEST_MAIN(RunnerManagerSingleRunnerModeTest)
0124 
0125 #include "runnermanagersinglerunnermodetest.moc"