/*************************************************************************** * Copyright (C) 2009 by Pino Toscano * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ #include #include #include namespace QTest { template<> char* toString( const KUrl& url ) { return qstrdup( url.url().toLocal8Bit() ); } } static const KUrl makeUrlFromCwd( const QString& u, const QString& ref = QString() ) { KUrl url( KUrl( QDir::currentPath() + "/" ), u ); if ( !ref.isEmpty() ) url.setRef( ref ); url.cleanPath(); return url; } class ShellTest : public QObject { Q_OBJECT private slots: void initTestCase(); void testUrlArgs_data(); void testUrlArgs(); }; void ShellTest::initTestCase() { qRegisterMetaType(); KCmdLineArgs::setCwd( QDir::currentPath().toLocal8Bit() ); } void ShellTest::testUrlArgs_data() { QTest::addColumn( "arg" ); QTest::addColumn( "exists" ); QTest::addColumn( "resUrl" ); // local files QTest::newRow( "foo.pdf, exist" ) << "foo.pdf" << true << makeUrlFromCwd( "foo.pdf" ); QTest::newRow( "foo.pdf, !exist" ) << "foo.pdf" << false << makeUrlFromCwd( "foo.pdf" ); QTest::newRow( "foo#bar.pdf, !exist" ) << "foo#bar.pdf" << false << makeUrlFromCwd( "foo#bar.pdf" ); QTest::newRow( "foo.pdf#anchor, !exist" ) << "foo.pdf#anchor" << false << makeUrlFromCwd( "foo.pdf", "anchor" ); QTest::newRow( "#207461" ) << "file:///tmp/file%20with%20spaces.pdf" << true << KUrl( "file:///tmp/file%20with%20spaces.pdf" ); // non-local files QTest::newRow( "http://kde.org/foo.pdf" ) << "http://kde.org/foo.pdf" << true << makeUrlFromCwd( "http://kde.org/foo.pdf" ); QTest::newRow( "http://kde.org/foo#bar.pdf" ) << "http://kde.org/foo#bar.pdf" << true << makeUrlFromCwd( "http://kde.org/foo#bar.pdf" ); QTest::newRow( "http://kde.org/foo.pdf#anchor" ) << "http://kde.org/foo.pdf#anchor" << true << makeUrlFromCwd( "http://kde.org/foo.pdf", "anchor" ); QTest::newRow( "#207461" ) << "http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf" << true << KUrl( "http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf" ); } void ShellTest::testUrlArgs() { QFETCH( QString, arg ); QFETCH( bool, exists ); QFETCH( KUrl, resUrl ); // note: below is a snippet taken from the Shell ctor const QString origArg = arg; arg.replace(QRegExp("^file:/{1,3}"), "/"); if (arg != origArg) { arg = QString::fromUtf8(QByteArray::fromPercentEncoding(arg.toUtf8())); } KUrl url = KCmdLineArgs::makeURL(arg.toUtf8()); int sharpPos = -1; if (!url.isLocalFile() || !exists) { sharpPos = arg.lastIndexOf(QLatin1Char('#')); } if (sharpPos != -1) { url = KCmdLineArgs::makeURL(arg.left(sharpPos).toUtf8()); url.setHTMLRef(arg.mid(sharpPos + 1)); } QCOMPARE( url, resUrl ); } QTEST_KDEMAIN_CORE( ShellTest ) #include "shelltest.moc"