File indexing completed on 2024-04-21 03:56:46
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 <KConfig> 0009 #include <KConfigGroup> 0010 #include <KSharedConfig> 0011 #include <QObject> 0012 #include <QProcess> 0013 #include <QSignalSpy> 0014 #include <QStandardPaths> 0015 #include <QTest> 0016 namespace KRunner 0017 { 0018 extern int __changeCountBeforeSaving; 0019 } 0020 using namespace KRunner; 0021 class RunnerManagerHistoryTest : public QObject 0022 { 0023 Q_OBJECT 0024 public: 0025 RunnerManagerHistoryTest() 0026 { 0027 __changeCountBeforeSaving = 1; 0028 QStandardPaths::setTestModeEnabled(true); 0029 stateConfigFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QDir::separator() + "krunnerstaterc"; 0030 } 0031 0032 private: 0033 QString stateConfigFile; 0034 void addToHistory(const QStringList &queries, RunnerManager &manager) 0035 { 0036 QCOMPARE(manager.runners().count(), 1); 0037 for (const QString &query : queries) { 0038 QueryMatch match(manager.runners().constFirst()); 0039 // Make sure internally the term and untrimmedTerm are set 0040 manager.launchQuery(query, "thisrunnerdoesnotexist"); 0041 manager.searchContext()->setQuery(query); 0042 manager.run(match); 0043 } 0044 } 0045 void launchQuery(const QString &query, RunnerManager *manager) 0046 { 0047 QSignalSpy spy(manager, &KRunner::RunnerManager::queryFinished); 0048 manager->launchQuery(query); 0049 QVERIFY2(spy.wait(), "RunnerManager did not emit the queryFinished signal"); 0050 } 0051 0052 private Q_SLOTS: 0053 void init() 0054 { 0055 if (QFileInfo::exists(stateConfigFile)) { 0056 QFile::remove(stateConfigFile); 0057 } 0058 } 0059 void testRunnerHistory(); 0060 void testRunnerHistory_data(); 0061 void testHistorySuggestionsAndRemoving(); 0062 void testRelevanceForOftenLaunched(); 0063 }; 0064 0065 void RunnerManagerHistoryTest::testRunnerHistory() 0066 { 0067 QFETCH(const QStringList, queries); 0068 QFETCH(const QStringList, expectedEntries); 0069 0070 RunnerManager manager; 0071 manager.setAllowedRunners({QStringLiteral("fakerunnerplugin")}); 0072 manager.loadRunner(KPluginMetaData::findPluginById(QStringLiteral("krunnertest"), QStringLiteral("fakerunnerplugin"))); 0073 addToHistory(queries, manager); 0074 QCOMPARE(manager.history(), expectedEntries); 0075 } 0076 0077 void RunnerManagerHistoryTest::testRunnerHistory_data() 0078 { 0079 QTest::addColumn<QStringList>("queries"); 0080 QTest::addColumn<QStringList>("expectedEntries"); 0081 0082 QTest::newRow("should add simple entry to history") << QStringList{"test"} << QStringList{"test"}; 0083 QTest::newRow("should not add entry that starts with space") << QStringList{" test"} << QStringList{}; 0084 QTest::newRow("should not add duplicate entries") << QStringList{"test", "test"} << QStringList{"test"}; 0085 QTest::newRow("should not add duplicate entries but put last run at beginning") << QStringList{"test", "test2", "test"} << QStringList{"test", "test2"}; 0086 } 0087 0088 void RunnerManagerHistoryTest::testHistorySuggestionsAndRemoving() 0089 { 0090 RunnerManager manager; 0091 manager.setAllowedRunners({QStringLiteral("fakerunnerplugin")}); 0092 manager.loadRunner(KPluginMetaData::findPluginById(QStringLiteral("krunnertest"), QStringLiteral("fakerunnerplugin"))); 0093 const QStringList queries = {"test1", "test2", "test3"}; 0094 addToHistory(queries, manager); 0095 QStringList expectedBeforeRemoval = QStringList{"test3", "test2", "test1"}; 0096 QCOMPARE(manager.history(), expectedBeforeRemoval); 0097 QCOMPARE(manager.getHistorySuggestion("t"), "test3"); 0098 QCOMPARE(manager.getHistorySuggestion("doesnotexist"), QString()); 0099 0100 manager.removeFromHistory(42); 0101 QCOMPARE(manager.history(), expectedBeforeRemoval); 0102 manager.removeFromHistory(0); 0103 QStringList expectedAfterRemoval = QStringList{"test2", "test1"}; 0104 QCOMPARE(manager.history(), expectedAfterRemoval); 0105 QCOMPARE(manager.getHistorySuggestion("t"), "test2"); 0106 } 0107 0108 void RunnerManagerHistoryTest::testRelevanceForOftenLaunched() 0109 { 0110 { 0111 KConfig cfg(stateConfigFile); 0112 cfg.group("PlasmaRunnerManager").writeEntry("LaunchCounts", "5 foo"); 0113 cfg.sync(); 0114 } 0115 std::unique_ptr<RunnerManager> manager(new RunnerManager()); 0116 manager->setAllowedRunners({QStringLiteral("fakerunnerplugin")}); 0117 manager->loadRunner(KPluginMetaData::findPluginById(QStringLiteral("krunnertest"), QStringLiteral("fakerunnerplugin"))); 0118 0119 launchQuery(QStringLiteral("foo"), manager.get()); 0120 0121 const auto matches = manager->matches(); 0122 QCOMPARE(matches.size(), 2); 0123 QCOMPARE(matches.at(0).id(), QStringLiteral("foo")); 0124 QCOMPARE(matches.at(1).id(), QStringLiteral("bar")); 0125 QCOMPARE(matches.at(1).relevance(), 0.2); 0126 0127 QVERIFY(matches.at(0).relevance() > matches.at(1).relevance()); 0128 QVERIFY(matches.at(0).relevance() < 0.6); // 0.5 is the max we add as a bonus, 0.1 comes from the runner 0129 { 0130 KConfig cfg(stateConfigFile); 0131 cfg.group("PlasmaRunnerManager").writeEntry("LaunchCounts", QStringList{"5 foo", "5 bar"}); 0132 cfg.sync(); 0133 KSharedConfig::openConfig(QStringLiteral("krunnerstaterc"), KConfig::NoGlobals, QStandardPaths::GenericDataLocation)->reparseConfiguration(); 0134 } 0135 manager.reset(new RunnerManager()); 0136 manager->setAllowedRunners({QStringLiteral("fakerunnerplugin")}); 0137 manager->loadRunner(KPluginMetaData::findPluginById(QStringLiteral("krunnertest"), QStringLiteral("fakerunnerplugin"))); 0138 0139 launchQuery(QStringLiteral("foo"), manager.get()); 0140 const auto newMatches = manager->matches(); 0141 QCOMPARE(newMatches.size(), 2); 0142 QVERIFY(newMatches.at(0).relevance() < newMatches.at(1).relevance()); 0143 } 0144 0145 QTEST_MAIN(RunnerManagerHistoryTest) 0146 0147 #include "runnermanagerhistorytest.moc"