File indexing completed on 2025-09-28 05:13:57
0001 /* 0002 SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KApplicationTrader> 0008 #include <KProtocolInfo> 0009 #include <KRunner/AbstractRunnerTest> 0010 #include <KShell> 0011 #include <QMimeData> 0012 #include <QTest> 0013 0014 class LocationsRunnerTest : public KRunner::AbstractRunnerTest 0015 { 0016 Q_OBJECT 0017 private: 0018 QString normalHomeFile; 0019 QString executableHomeFile; 0020 0021 private Q_SLOTS: 0022 void initTestCase(); 0023 void cleanupTestCase() 0024 { 0025 QFile::remove(normalHomeFile); 0026 QFile::remove(executableHomeFile); 0027 } 0028 void shouldNotProduceResult(); 0029 void shouldNotProduceResult_data(); 0030 void shouldProduceResult(); 0031 void shouldProduceResult_data(); 0032 void testMimeData(); 0033 }; 0034 0035 void LocationsRunnerTest::initTestCase() 0036 { 0037 QStandardPaths::setTestModeEnabled(true); 0038 // Set up applications.menu so that kservice works 0039 const QString menusDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String{"/menus"}; 0040 QDir(menusDir).removeRecursively(); 0041 QDir(menusDir).mkpath(QStringLiteral(".")); 0042 QFile::copy(QFINDTESTDATA("../../../menu/desktop/plasma-applications.menu"), menusDir + QLatin1String("/applications.menu")); 0043 0044 initProperties(); 0045 normalHomeFile = KShell::tildeExpand(QStringLiteral("~/.krunner_locationsrunner_testfile")); 0046 executableHomeFile = KShell::tildeExpand(QStringLiteral("~/.krunner_locationsrunner_testexecutablefile")); 0047 QFile normalFile(normalHomeFile); 0048 normalFile.open(QIODevice::WriteOnly); 0049 QFile executableFile(executableHomeFile); 0050 executableFile.open(QIODevice::WriteOnly); 0051 executableFile.setPermissions(executableFile.permissions() | QFile::ExeOwner); 0052 QVERIFY(!normalHomeFile.isEmpty()); 0053 } 0054 0055 void LocationsRunnerTest::shouldNotProduceResult() 0056 { 0057 QFETCH(QString, query); 0058 const auto matches = launchQuery(query); 0059 QVERIFY(matches.isEmpty()); 0060 } 0061 0062 void LocationsRunnerTest::shouldProduceResult() 0063 { 0064 QFETCH(QString, query); 0065 QFETCH(QUrl, data); 0066 const auto matches = launchQuery(query); 0067 QCOMPARE(matches.size(), 1); 0068 QCOMPARE(matches.first().data().toUrl(), data); 0069 } 0070 0071 void LocationsRunnerTest::shouldNotProduceResult_data() 0072 { 0073 QTest::addColumn<QString>("query"); 0074 0075 QTest::newRow("executable name") << "ls"; 0076 QTest::newRow("executable file path") << "/bin/ls"; 0077 if (!executableHomeFile.isEmpty()) { 0078 QTest::newRow("executable file in home dir") << executableHomeFile; 0079 } 0080 QTest::newRow("executable path and argument") << "/bin/ls -Al"; 0081 QTest::newRow("non existent file") << QDir::homePath() + "_thisfiledoesnotexist.abc"; 0082 QTest::newRow("non existent file URL") << QUrl::fromLocalFile(QDir::homePath() + "_thisfiledoesnotexist.abc").toString(); 0083 QTest::newRow("nonexistent file with $HOME as env variable") << "$HOME/_thisfiledoesnotexist.abc"; 0084 QTest::newRow("nonexistent protocol") << "thisprotocoldoesnotexist:test123"; 0085 QTest::newRow("empty string") << ""; 0086 QTest::newRow("missing closing double quote") << "\""; 0087 QTest::newRow("missing closing single quote") << "'"; 0088 } 0089 0090 void LocationsRunnerTest::shouldProduceResult_data() 0091 { 0092 QTest::addColumn<QString>("query"); 0093 QTest::addColumn<QUrl>("data"); 0094 0095 const QUrl homeURL = QUrl::fromLocalFile(QDir::homePath()); 0096 QTest::newRow("folder") << QDir::homePath() << homeURL; 0097 QTest::newRow("folder tilde") << KShell::tildeCollapse(QDir::homePath()) << homeURL; 0098 QTest::newRow("folder URL") << homeURL.toString() << homeURL; 0099 0100 QTest::newRow("file") << normalHomeFile << QUrl::fromLocalFile(normalHomeFile); 0101 QTest::newRow("file tilde") << KShell::tildeCollapse(normalHomeFile) << QUrl::fromLocalFile(normalHomeFile); 0102 QTest::newRow("file with $HOME as env variable") << KShell::tildeCollapse(normalHomeFile).replace("~", "$HOME") << QUrl::fromLocalFile(normalHomeFile); 0103 QTest::newRow("file URL") << QUrl::fromLocalFile(normalHomeFile).toString() << QUrl::fromLocalFile(normalHomeFile); 0104 QTest::newRow("file URL to executable") << QUrl::fromLocalFile(executableHomeFile).toString() << QUrl::fromLocalFile(executableHomeFile); 0105 if (KProtocolInfo::isHelperProtocol("vnc")) { 0106 QTest::newRow("vnc URL") << "vnc:foo" << QUrl("vnc:foo"); 0107 } 0108 if (KApplicationTrader::preferredService("x-scheme-handler/rtmp")) { 0109 QTest::newRow("rtmp URL") << "rtmp:foo" << QUrl("rtmp:foo"); 0110 } 0111 if (KApplicationTrader::preferredService("x-scheme-handler/mailto")) { 0112 // The mailto protocol is not provided by KIO, but by installed apps. BUG: 416257 0113 QTest::newRow("mailto URL") << "mailto:user.user@user.com" << QUrl("mailto:user.user@user.com"); 0114 } 0115 0116 if (KProtocolInfo::isKnownProtocol(QStringLiteral("smb"))) { 0117 QTest::newRow("ssh URL") << "ssh:localhost" << QUrl("ssh:localhost"); 0118 QTest::newRow("help URL") << "help:krunner" << QUrl("help:krunner"); 0119 QTest::newRow("smb URL") << "smb:server/path" << QUrl("smb:server/path"); 0120 QTest::newRow("smb URL shorthand syntax") << R"(\\server\path)" << QUrl("smb://server/path"); 0121 } 0122 } 0123 0124 void LocationsRunnerTest::testMimeData() 0125 { 0126 const auto matches = launchQuery(QDir::homePath()); 0127 QVERIFY(!matches.isEmpty()); 0128 QMimeData *data = manager->mimeDataForMatch(matches.first()); 0129 QVERIFY(data); 0130 QCOMPARE(data->urls(), QList<QUrl>{QUrl::fromLocalFile(QDir::homePath())}); 0131 } 0132 0133 QTEST_MAIN(LocationsRunnerTest) 0134 0135 #include "locationsrunnertest.moc"