File indexing completed on 2024-05-19 16:41:37

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 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     initProperties();
0038     normalHomeFile = KShell::tildeExpand(QStringLiteral("~/.krunner_locationsrunner_testfile"));
0039     executableHomeFile = KShell::tildeExpand(QStringLiteral("~/.krunner_locationsrunner_testexecutablefile"));
0040     QFile normalFile(normalHomeFile);
0041     normalFile.open(QIODevice::WriteOnly);
0042     QFile executableFile(executableHomeFile);
0043     executableFile.open(QIODevice::WriteOnly);
0044     executableFile.setPermissions(executableFile.permissions() | QFile::ExeOwner);
0045     QVERIFY(!normalHomeFile.isEmpty());
0046 }
0047 
0048 void LocationsRunnerTest::shouldNotProduceResult()
0049 {
0050     QFETCH(QString, query);
0051     launchQuery(query);
0052     QVERIFY(manager->matches().isEmpty());
0053 }
0054 
0055 void LocationsRunnerTest::shouldProduceResult()
0056 {
0057     QFETCH(QString, query);
0058     QFETCH(QVariant, data);
0059     launchQuery(query);
0060     const QList<Plasma::QueryMatch> matches = manager->matches();
0061     QCOMPARE(matches.size(), 1);
0062     QCOMPARE(matches.first().data(), data);
0063 }
0064 
0065 void LocationsRunnerTest::shouldNotProduceResult_data()
0066 {
0067     QTest::addColumn<QString>("query");
0068 
0069     QTest::newRow("executable name") << "ls";
0070     QTest::newRow("executable file path") << "/bin/ls";
0071     if (!executableHomeFile.isEmpty()) {
0072         QTest::newRow("executable file in home dir") << executableHomeFile;
0073     }
0074     QTest::newRow("executable path and argument") << "/bin/ls -Al";
0075     QTest::newRow("non existent file") << QDir::homePath() + "_thisfiledoesnotexist.abc";
0076     QTest::newRow("non existent file URL") << QUrl::fromLocalFile(QDir::homePath() + "_thisfiledoesnotexist.abc").toString();
0077     QTest::newRow("nonexistent file with $HOME as env variable") << "$HOME/_thisfiledoesnotexist.abc";
0078     QTest::newRow("nonexistent protocol") << "thisprotocoldoesnotexist:test123";
0079     QTest::newRow("empty string") << "";
0080     QTest::newRow("missing closing double quote") << "\"";
0081     QTest::newRow("missing closing single quote") << "'";
0082 }
0083 
0084 void LocationsRunnerTest::shouldProduceResult_data()
0085 {
0086     QTest::addColumn<QString>("query");
0087     QTest::addColumn<QVariant>("data");
0088 
0089     const QUrl homeURL = QUrl::fromLocalFile(QDir::homePath());
0090     QTest::newRow("folder") << QDir::homePath() << QVariant(homeURL);
0091     QTest::newRow("folder tilde") << KShell::tildeCollapse(QDir::homePath()) << QVariant(homeURL);
0092     QTest::newRow("folder URL") << homeURL.toString() << QVariant(homeURL);
0093 
0094     QTest::newRow("file") << normalHomeFile << QVariant(QUrl::fromLocalFile(normalHomeFile));
0095     QTest::newRow("file tilde") << KShell::tildeCollapse(normalHomeFile) << QVariant(QUrl::fromLocalFile(normalHomeFile));
0096     QTest::newRow("file with $HOME as env variable") << KShell::tildeCollapse(normalHomeFile).replace("~", "$HOME")
0097                                                      << QVariant(QUrl::fromLocalFile(normalHomeFile));
0098     QTest::newRow("file URL") << QUrl::fromLocalFile(normalHomeFile).toString() << QVariant(QUrl::fromLocalFile(normalHomeFile));
0099     QTest::newRow("file URL to executable") << QUrl::fromLocalFile(executableHomeFile).toString() << QVariant(QUrl::fromLocalFile(executableHomeFile));
0100     if (KProtocolInfo::isHelperProtocol("vnc")) {
0101         QTest::newRow("vnc URL") << "vnc:foo" << QVariant("vnc:foo");
0102     }
0103     if (KApplicationTrader::preferredService("x-scheme-handler/rtmp")) {
0104         QTest::newRow("rtmp URL") << "rtmp:foo" << QVariant("rtmp:foo");
0105     }
0106     if (KApplicationTrader::preferredService("x-scheme-handler/mailto")) {
0107         // The mailto protocol is not provided by KIO, but by installed apps. BUG: 416257
0108         QTest::newRow("mailto URL") << "mailto:user.user@user.com" << QVariant("mailto:user.user@user.com");
0109     }
0110 
0111     if (KProtocolInfo::isKnownProtocol(QStringLiteral("smb"))) {
0112         QTest::newRow("ssh URL") << "ssh:localhost" << QVariant("ssh:localhost");
0113         QTest::newRow("help URL") << "help:krunner" << QVariant("help:krunner");
0114         QTest::newRow("smb URL") << "smb:server/path" << QVariant("smb:server/path");
0115         QTest::newRow("smb URL shorthand syntax") << R"(\\server\path)" << QVariant("smb://server/path");
0116     }
0117 }
0118 
0119 void LocationsRunnerTest::testMimeData()
0120 {
0121     launchQuery(QDir::homePath());
0122     QMimeData *data = manager->mimeDataForMatch(manager->matches().constFirst());
0123     QVERIFY(data);
0124     QCOMPARE(data->urls(), QList<QUrl>{QUrl::fromLocalFile(QDir::homePath())});
0125 }
0126 
0127 QTEST_MAIN(LocationsRunnerTest)
0128 
0129 #include "locationsrunnertest.moc"