Warning, file /frameworks/krunner/autotests/runnermatchmethodstest.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 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "abstractrunner.h" 0008 #include "fakerunner.h" 0009 #include "runnermanager.h" 0010 0011 #include <QAction> 0012 #include <QObject> 0013 #include <QStandardPaths> 0014 #include <QTest> 0015 0016 #include "kpluginmetadata_utils_p.h" 0017 0018 using namespace Plasma; 0019 0020 namespace 0021 { 0022 inline QueryMatch createMatch(const QString &id, AbstractRunner *r = nullptr) 0023 { 0024 QueryMatch m(r); 0025 m.setId(id); 0026 return m; 0027 } 0028 } 0029 0030 class RunnerContextMatchMethodsTest : public QObject 0031 { 0032 Q_OBJECT 0033 public: 0034 RunnerContextMatchMethodsTest(); 0035 ~RunnerContextMatchMethodsTest() override; 0036 0037 std::unique_ptr<RunnerContext> ctx; 0038 FakeRunner *runner1; 0039 FakeRunner *runner2; 0040 private Q_SLOTS: 0041 void init(); 0042 void testAdd(); 0043 void testAddMulti(); 0044 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81) 0045 void testRemoveMatch(); 0046 void testRemoveMatchMulti(); 0047 void testRemoveMatchByRunner(); 0048 #endif 0049 void testDuplicateIds(); 0050 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 79) 0051 void testGetMatchById(); 0052 void testNonExistentMatchIds(); 0053 #endif 0054 }; 0055 0056 RunnerContextMatchMethodsTest::RunnerContextMatchMethodsTest() 0057 : QObject() 0058 , runner1(new FakeRunner()) 0059 , runner2(new FakeRunner()) 0060 { 0061 QStandardPaths::setTestModeEnabled(true); 0062 const QByteArray defaultDataDirs = qEnvironmentVariableIsSet("XDG_DATA_DIRS") ? qgetenv("XDG_DATA_DIRS") : QByteArray("/usr/local:/usr"); 0063 const QByteArray modifiedDataDirs = QFile::encodeName(QCoreApplication::applicationDirPath()) + QByteArrayLiteral("/data:") + defaultDataDirs; 0064 qputenv("XDG_DATA_DIRS", modifiedDataDirs); 0065 KPluginMetaData data1 = parseMetaDataFromDesktopFile(QFINDTESTDATA("metadatafile1.desktop")); 0066 KPluginMetaData data2 = parseMetaDataFromDesktopFile(QFINDTESTDATA("metadatafile2.desktop")); 0067 QVERIFY(data1.isValid()); 0068 QVERIFY(data2.isValid()); 0069 runner1 = new FakeRunner(data1); 0070 runner2 = new FakeRunner(data2); 0071 } 0072 0073 RunnerContextMatchMethodsTest::~RunnerContextMatchMethodsTest() 0074 { 0075 delete runner1; 0076 delete runner2; 0077 } 0078 0079 void RunnerContextMatchMethodsTest::init() 0080 { 0081 ctx.reset(new RunnerContext()); 0082 } 0083 0084 void RunnerContextMatchMethodsTest::testAdd() 0085 { 0086 QVERIFY(ctx->matches().isEmpty()); 0087 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m1")))); 0088 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m2")))); 0089 QCOMPARE(ctx->matches().count(), 2); 0090 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m3")))); 0091 QCOMPARE(ctx->matches().count(), 3); 0092 } 0093 0094 void RunnerContextMatchMethodsTest::testAddMulti() 0095 { 0096 QVERIFY(ctx->matches().isEmpty()); 0097 QVERIFY(ctx->addMatches({createMatch(QStringLiteral("m1")), createMatch(QStringLiteral("m2"))})); 0098 QCOMPARE(ctx->matches().count(), 2); 0099 } 0100 0101 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81) 0102 void RunnerContextMatchMethodsTest::testRemoveMatch() 0103 { 0104 QueryMatch m = createMatch(QStringLiteral("m1")); 0105 ctx->addMatches({m, createMatch(QStringLiteral("m2"))}); 0106 QCOMPARE(ctx->matches().count(), 2); 0107 QVERIFY(ctx->removeMatch(m.id())); 0108 QCOMPARE(ctx->matches().count(), 1); 0109 } 0110 #endif 0111 0112 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81) 0113 void RunnerContextMatchMethodsTest::testRemoveMatchByRunner() 0114 { 0115 QVERIFY(ctx->matches().isEmpty()); 0116 QueryMatch m1 = createMatch(QStringLiteral("m1"), runner1); 0117 QueryMatch m2 = createMatch(QStringLiteral("m2"), runner1); 0118 QueryMatch m3 = createMatch(QStringLiteral("m3"), runner2); 0119 ctx->addMatches({m1, m2, m3}); 0120 QCOMPARE(ctx->matches().count(), 3); 0121 QVERIFY(ctx->removeMatches(runner1)); 0122 QCOMPARE(ctx->matches().count(), 1); 0123 QCOMPARE(ctx->matches().constFirst(), m3); 0124 } 0125 #endif 0126 0127 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 81) 0128 void RunnerContextMatchMethodsTest::testRemoveMatchMulti() 0129 { 0130 QVERIFY(ctx->matches().isEmpty()); 0131 QueryMatch m1 = createMatch(QStringLiteral("m1")); 0132 QueryMatch m2 = createMatch(QStringLiteral("m2")); 0133 QueryMatch m3 = createMatch(QStringLiteral("m3")); 0134 ctx->addMatches({m1, m2, m3}); 0135 QCOMPARE(ctx->matches().count(), 3); 0136 0137 // Nothing should happen in case of an empty list 0138 QVERIFY(!ctx->removeMatches(QStringList())); 0139 QCOMPARE(ctx->matches().count(), 3); 0140 0141 QVERIFY(ctx->removeMatches({m1.id(), m2.id()})); 0142 QCOMPARE(ctx->matches().count(), 1); 0143 QCOMPARE(ctx->matches().constFirst(), m3); 0144 } 0145 #endif 0146 0147 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 79) 0148 void RunnerContextMatchMethodsTest::testGetMatchById() 0149 { 0150 QueryMatch m1 = createMatch(QStringLiteral("m1"), runner1); 0151 QueryMatch m2 = createMatch(QStringLiteral("m2"), runner1); 0152 QueryMatch m3 = createMatch(QStringLiteral("m3"), runner2); 0153 ctx->addMatches({m1, m2, m3}); 0154 QCOMPARE(ctx->matches().count(), 3); 0155 QCOMPARE(ctx->match(m1.id()), m1); 0156 // ID gets internally concatenated with runner id 0157 QCOMPARE(ctx->match(QStringLiteral("m1")), m1); 0158 } 0159 #endif 0160 0161 #if KRUNNER_BUILD_DEPRECATED_SINCE(5, 79) 0162 void RunnerContextMatchMethodsTest::testNonExistentMatchIds() 0163 { 0164 QueryMatch m1 = createMatch(QStringLiteral("m1")); 0165 QueryMatch m2 = createMatch(QStringLiteral("m2")); 0166 ctx->addMatches({m1, m2}); 0167 QCOMPARE(ctx->matches().count(), 2); 0168 QVERIFY(!ctx->removeMatch(QStringLiteral("does_not_exist"))); 0169 QCOMPARE(ctx->matches().count(), 2); 0170 0171 QVERIFY(!ctx->match(QStringLiteral("does_not_exist")).isValid()); 0172 QCOMPARE(ctx->match(QStringLiteral("does_not_exist")).runner(), nullptr); 0173 } 0174 #endif 0175 0176 void RunnerContextMatchMethodsTest::testDuplicateIds() 0177 { 0178 const QueryMatch match1 = createMatch(QStringLiteral("id1"), runner1); 0179 QVERIFY(ctx->addMatch(match1)); 0180 const QueryMatch match2 = createMatch(QStringLiteral("id1"), runner2); 0181 QVERIFY(ctx->addMatch(match2)); 0182 const QueryMatch match3 = createMatch(QStringLiteral("id2"), runner1); 0183 QVERIFY(ctx->addMatch(match3)); 0184 const QueryMatch match4 = createMatch(QStringLiteral("id3"), runner2); 0185 QVERIFY(ctx->addMatch(match4)); 0186 const QueryMatch match5 = createMatch(QStringLiteral("id3"), runner2); 0187 QVERIFY(ctx->addMatch(match5)); 0188 0189 const QList<QueryMatch> matches = ctx->matches(); 0190 QCOMPARE(matches.size(), 3); 0191 // match2 should have replaced match1 0192 QCOMPARE(matches.at(0), match2); 0193 QCOMPARE(matches.at(1), match3); 0194 // match4 should not have been replaced, the runner does not have the weak property set 0195 QCOMPARE(matches.at(2), match4); 0196 } 0197 0198 0199 QTEST_MAIN(RunnerContextMatchMethodsTest) 0200 0201 #include "runnermatchmethodstest.moc"