Warning, file /graphics/okular/autotests/shelltest.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: 2009 Pino Toscano <pino@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include <QDir>
0010 #include <QUrl>
0011 
0012 #include "../shell/shellutils.h"
0013 
0014 static const QUrl makeUrlFromCwd(const QString &u, const QString &ref = QString())
0015 {
0016     QUrl url = QUrl::fromLocalFile(QDir::currentPath() + QLatin1Char('/') + u);
0017     if (!ref.isEmpty()) {
0018         url.setFragment(ref);
0019     }
0020     url.setPath(QDir::cleanPath(url.path()));
0021     return url;
0022 }
0023 
0024 static bool fileExist_always_Func(const QString &)
0025 {
0026     return true;
0027 }
0028 
0029 static bool fileExist_never_Func(const QString &)
0030 {
0031     return false;
0032 }
0033 
0034 class ShellTest : public QObject
0035 {
0036     Q_OBJECT
0037 
0038 private Q_SLOTS:
0039     void initTestCase();
0040     void testUrlArgs_data();
0041     void testUrlArgs();
0042 };
0043 
0044 void ShellTest::initTestCase()
0045 {
0046     qRegisterMetaType<QUrl>();
0047 }
0048 
0049 void ShellTest::testUrlArgs_data()
0050 {
0051     QTest::addColumn<QString>("arg");
0052     QTest::addColumn<bool>("exists");
0053     QTest::addColumn<QUrl>("resUrl");
0054 
0055     // local files
0056     QTest::newRow("foo.pdf, exist") << "foo.pdf" << true << makeUrlFromCwd(QStringLiteral("foo.pdf"));
0057     QTest::newRow("foo.pdf, !exist") << "foo.pdf" << false << makeUrlFromCwd(QStringLiteral("foo.pdf"));
0058     QTest::newRow("foo#bar.pdf, exist") << "foo#bar.pdf" << true << makeUrlFromCwd(QStringLiteral("foo#bar.pdf"));
0059     QTest::newRow("foo#bar.pdf, !exist") << "foo#bar.pdf" << false << makeUrlFromCwd(QStringLiteral("foo"), QStringLiteral("bar.pdf"));
0060     QTest::newRow("foo.pdf#anchor, !exist") << "foo.pdf#anchor" << false << makeUrlFromCwd(QStringLiteral("foo.pdf"), QStringLiteral("anchor"));
0061     QTest::newRow("#207461") << "file:///tmp/file%20with%20spaces.pdf" << true << QUrl(QStringLiteral("file:///tmp/file%20with%20spaces.pdf"));
0062 
0063     // non-local files
0064     QTest::newRow("http://kde.org/foo.pdf") << "http://kde.org/foo.pdf" << true << QUrl(QStringLiteral("http://kde.org/foo.pdf"));
0065     QUrl withAnchor(QStringLiteral("http://kde.org/foo.pdf"));
0066     withAnchor.setFragment(QStringLiteral("anchor"));
0067     QTest::newRow("http://kde.org/foo.pdf#anchor") << "http://kde.org/foo.pdf#anchor" << true << withAnchor;
0068     QTest::newRow("#207461") << "http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf" << true << QUrl(QStringLiteral("http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf"));
0069     QUrl openOnPage3 = QUrl(QStringLiteral("http://itzsimpl.info/lectures/CG/L2-transformations.pdf"));
0070     openOnPage3.setFragment(QStringLiteral("3"));
0071     QTest::newRow("RR124738") << "http://itzsimpl.info/lectures/CG/L2-transformations.pdf#3" << true << openOnPage3;
0072 }
0073 
0074 void ShellTest::testUrlArgs()
0075 {
0076     QFETCH(QString, arg);
0077     QFETCH(bool, exists);
0078     QFETCH(QUrl, resUrl);
0079     qDebug() << "Expected url:" << resUrl << "path =" << resUrl.path() << "fragment =" << resUrl.fragment();
0080     QUrl url = ShellUtils::urlFromArg(arg, exists ? fileExist_always_Func : fileExist_never_Func);
0081     QCOMPARE(url, resUrl);
0082 }
0083 
0084 QTEST_GUILESS_MAIN(ShellTest)
0085 #include "shelltest.moc"