File indexing completed on 2024-03-24 03:59:36
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 "plugins/fakerunner.h" 0009 #include "runnermanager.h" 0010 0011 #include <QObject> 0012 #include <QStandardPaths> 0013 #include <QTest> 0014 0015 #include "kpluginmetadata_utils_p.h" 0016 0017 using namespace KRunner; 0018 0019 inline QueryMatch createMatch(const QString &id, AbstractRunner *r = nullptr) 0020 { 0021 QueryMatch m(r); 0022 m.setId(id); 0023 return m; 0024 } 0025 0026 class RunnerContextMatchMethodsTest : public QObject 0027 { 0028 Q_OBJECT 0029 public: 0030 RunnerContextMatchMethodsTest(); 0031 0032 std::unique_ptr<RunnerContext> ctx; 0033 FakeRunner *runner1 = nullptr; 0034 FakeRunner *runner2 = nullptr; 0035 private Q_SLOTS: 0036 void init() 0037 { 0038 ctx.reset(new RunnerContext()); 0039 } 0040 void testAdd(); 0041 void testAddMulti(); 0042 void testDuplicateIds(); 0043 }; 0044 0045 RunnerContextMatchMethodsTest::RunnerContextMatchMethodsTest() 0046 { 0047 QStandardPaths::setTestModeEnabled(true); 0048 const QByteArray defaultDataDirs = qEnvironmentVariableIsSet("XDG_DATA_DIRS") ? qgetenv("XDG_DATA_DIRS") : QByteArray("/usr/local:/usr"); 0049 const QByteArray modifiedDataDirs = QFile::encodeName(QCoreApplication::applicationDirPath()) + QByteArrayLiteral("/data:") + defaultDataDirs; 0050 qputenv("XDG_DATA_DIRS", modifiedDataDirs); 0051 KPluginMetaData data1 = parseMetaDataFromDesktopFile(QFINDTESTDATA("plugins/metadatafile1.desktop")); 0052 KPluginMetaData data2 = parseMetaDataFromDesktopFile(QFINDTESTDATA("plugins/metadatafile2.desktop")); 0053 QVERIFY(data1.isValid()); 0054 QVERIFY(data2.isValid()); 0055 runner1 = new FakeRunner(this, data1); 0056 runner2 = new FakeRunner(this, data2); 0057 } 0058 0059 void RunnerContextMatchMethodsTest::testAdd() 0060 { 0061 QVERIFY(ctx->matches().isEmpty()); 0062 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m1")))); 0063 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m2")))); 0064 QCOMPARE(ctx->matches().count(), 2); 0065 QVERIFY(ctx->addMatch(createMatch(QStringLiteral("m3")))); 0066 QCOMPARE(ctx->matches().count(), 3); 0067 } 0068 0069 void RunnerContextMatchMethodsTest::testAddMulti() 0070 { 0071 QVERIFY(ctx->matches().isEmpty()); 0072 QVERIFY(ctx->addMatches({createMatch(QStringLiteral("m1")), createMatch(QStringLiteral("m2"))})); 0073 QCOMPARE(ctx->matches().count(), 2); 0074 } 0075 0076 void RunnerContextMatchMethodsTest::testDuplicateIds() 0077 { 0078 const QueryMatch match1 = createMatch(QStringLiteral("id1"), runner1); 0079 QVERIFY(ctx->addMatch(match1)); 0080 const QueryMatch match2 = createMatch(QStringLiteral("id1"), runner2); 0081 QVERIFY(ctx->addMatch(match2)); 0082 const QueryMatch match3 = createMatch(QStringLiteral("id2"), runner1); 0083 QVERIFY(ctx->addMatch(match3)); 0084 const QueryMatch match4 = createMatch(QStringLiteral("id3"), runner2); 0085 QVERIFY(ctx->addMatch(match4)); 0086 const QueryMatch match5 = createMatch(QStringLiteral("id3"), runner2); 0087 QVERIFY(ctx->addMatch(match5)); 0088 0089 const QList<QueryMatch> matches = ctx->matches(); 0090 QCOMPARE(matches.size(), 3); 0091 // match2 should have replaced match1 0092 QCOMPARE(matches.at(0), match2); 0093 QCOMPARE(matches.at(1), match3); 0094 // match4 should not have been replaced, the runner does not have the weak property set 0095 QCOMPARE(matches.at(2), match4); 0096 } 0097 0098 QTEST_MAIN(RunnerContextMatchMethodsTest) 0099 0100 #include "runnermatchmethodstest.moc"