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