File indexing completed on 2024-04-21 15:03:01

0001 /*
0002     SPDX-FileCopyrightText: 2010 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "runnercontexttest.h"
0008 
0009 #include <KProtocolInfo>
0010 
0011 #include <QDir>
0012 
0013 Q_DECLARE_METATYPE(Plasma::RunnerContext::Type)
0014 
0015 static QString getSomeExistingFileInHomeDir()
0016 {
0017     const auto files = QDir::home().entryList(QDir::Files | QDir::Hidden);
0018     return !files.isEmpty() ? files.first() : QString();
0019 }
0020 
0021 void RunnerContextTest::typeDetection_data()
0022 {
0023     QTest::addColumn<QString>("url");
0024     QTest::addColumn<Plasma::RunnerContext::Type>("type");
0025 
0026     if (KProtocolInfo::isKnownProtocol(QStringLiteral("man"))) {
0027         QTest::newRow("man page listing") << "man:/" << Plasma::RunnerContext::NetworkLocation;
0028         QTest::newRow("ls man page listing") << "man://ls" << Plasma::RunnerContext::NetworkLocation;
0029     }
0030     QTest::newRow("http without host") << "http://" << Plasma::RunnerContext::UnknownType;
0031     QTest::newRow("http without host") << "http://" << Plasma::RunnerContext::UnknownType;
0032     QTest::newRow("ftp with host") << "ftp://kde.org" << Plasma::RunnerContext::NetworkLocation;
0033     QTest::newRow("file double slash") << "file:/" + QDir::homePath() << Plasma::RunnerContext::Directory;
0034     QTest::newRow("file triple slash") << "file://" + QDir::homePath() << Plasma::RunnerContext::Directory;
0035     QTest::newRow("file single slash") << "file:" + QDir::homePath() << Plasma::RunnerContext::Directory;
0036     QTest::newRow("file multiple path") << "file://usr/bin" << Plasma::RunnerContext::Directory;
0037     QTest::newRow("invalid file path") << "file://bad/path" << Plasma::RunnerContext::UnknownType;
0038     QTest::newRow("executable") << "ls" << Plasma::RunnerContext::Executable;
0039     QTest::newRow("executable with params") << "ls -R" << Plasma::RunnerContext::ShellCommand;
0040     QTest::newRow("full path executable") << "ls -R" << Plasma::RunnerContext::ShellCommand;
0041     QTest::newRow("full path executable with params") << "/bin/ls -R" << Plasma::RunnerContext::ShellCommand;
0042     QTest::newRow("protocol-less path") << QDir::homePath() << Plasma::RunnerContext::Directory;
0043     QTest::newRow("protocol-less tilded") << "~" << Plasma::RunnerContext::Directory;
0044     const QString file = getSomeExistingFileInHomeDir();
0045     if (!file.isEmpty()) {
0046         QTest::newRow("protocol-less file starting with tilded") << QLatin1String("~/") + file << Plasma::RunnerContext::File;
0047     }
0048     QTest::newRow("invalid protocol-less path") << "/bad/path" << Plasma::RunnerContext::UnknownType;
0049     QTest::newRow("calculation") << "5*4" << Plasma::RunnerContext::UnknownType;
0050     QTest::newRow("calculation (float)") << "5.2*4" << Plasma::RunnerContext::UnknownType;
0051     // These are not real file paths, see BUG 342876
0052     QTest::newRow("Invalid casing dir path") << "/UsR" << Plasma::RunnerContext::UnknownType;
0053     QTest::newRow("Invalid casing file path") << "/bin/TruE" << Plasma::RunnerContext::UnknownType;
0054 }
0055 
0056 void RunnerContextTest::typeDetection()
0057 {
0058     QFETCH(QString, url);
0059     QFETCH(Plasma::RunnerContext::Type, type);
0060 
0061     m_context.setQuery(url);
0062     QCOMPARE(int(m_context.type()), int(type));
0063 }
0064 
0065 QTEST_MAIN(RunnerContextTest)
0066 
0067 #include "moc_runnercontexttest.cpp"