File indexing completed on 2024-04-21 14:55:18

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2005 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kurlmimetest.h"
0021 #include <QTest>
0022 #include <QMimeData>
0023 #include <kurl.h>
0024 
0025 QTEST_MAIN(KUrlMimeTest)
0026 
0027 void KUrlMimeTest::testURLList()
0028 {
0029     QMimeData *mimeData = new QMimeData;
0030     QVERIFY(!KUrl::List::canDecode(mimeData));
0031     QVERIFY(!mimeData->hasUrls());
0032 
0033     KUrl::List urls;
0034     urls.append(KUrl("http://www.kde.org"));
0035     urls.append(KUrl("http://wstephenson:secret@example.com/path"));
0036     urls.append(KUrl("file:///home/dfaure/konqtests/Mat%C3%A9riel"));
0037     QMap<QString, QString> metaData;
0038     metaData[QLatin1String("key")] = QLatin1String("value");
0039     metaData[QLatin1String("key2")] = QLatin1String("value2");
0040 
0041     urls.populateMimeData(mimeData, metaData);
0042 
0043     QVERIFY(KUrl::List::canDecode(mimeData));
0044     QVERIFY(mimeData->hasUrls());
0045     QVERIFY(mimeData->hasText());
0046 
0047     QMap<QString, QString> decodedMetaData;
0048     KUrl::List decodedURLs = KUrl::List::fromMimeData(mimeData, KUrl::List::PreferKdeUrls, &decodedMetaData);
0049     QVERIFY(!decodedURLs.isEmpty());
0050     KUrl::List expectedUrls = urls;
0051     expectedUrls[1] = KUrl("http://wstephenson:secret@example.com/path"); // password kept, unlike in KDE4, but that's okay, it's not displayed
0052     const QString space(QLatin1Char(' '));
0053     QCOMPARE(expectedUrls.toStringList().join(space), decodedURLs.toStringList().join(space));
0054 
0055     const QList<QUrl> qurls = mimeData->urls();
0056     QCOMPARE(qurls.count(), urls.count());
0057     for (int i = 0; i < qurls.count(); ++i) {
0058         QCOMPARE(qurls[i], static_cast<QUrl>(decodedURLs[i]));
0059     }
0060 
0061     QVERIFY(!decodedMetaData.isEmpty());
0062     QCOMPARE(decodedMetaData[QLatin1String("key")], QString::fromLatin1("value"));
0063     QCOMPARE(decodedMetaData[QLatin1String("key2")], QString::fromLatin1("value2"));
0064 
0065     delete mimeData;
0066 }
0067 
0068 void KUrlMimeTest::testOneURL()
0069 {
0070     KUrl oneURL("file:///tmp");
0071     QMimeData *mimeData = new QMimeData;
0072 
0073     oneURL.populateMimeData(mimeData);
0074 
0075     QVERIFY(KUrl::List::canDecode(mimeData));
0076     QMap<QString, QString> decodedMetaData;
0077     KUrl::List decodedURLs = KUrl::List::fromMimeData(mimeData, KUrl::List::PreferKdeUrls, &decodedMetaData);
0078     QVERIFY(!decodedURLs.isEmpty());
0079     QCOMPARE(decodedURLs.count(), 1);
0080     QCOMPARE(decodedURLs[0].url(), oneURL.url());
0081     QVERIFY(decodedMetaData.isEmpty());
0082     delete mimeData;
0083 }
0084 
0085 void KUrlMimeTest::testFromQUrl()
0086 {
0087     QList<QUrl> qurls;
0088     qurls.append(QUrl(QLatin1String("http://www.kde.org")));
0089     qurls.append(QUrl(QLatin1String("file:///home/dfaure/konqtests/Mat%C3%A9riel")));
0090     QMimeData *mimeData = new QMimeData;
0091     mimeData->setUrls(qurls);
0092 
0093     QVERIFY(KUrl::List::canDecode(mimeData));
0094     QMap<QString, QString> decodedMetaData;
0095     KUrl::List decodedURLs = KUrl::List::fromMimeData(mimeData, KUrl::List::PreferKdeUrls, &decodedMetaData);
0096     QVERIFY(!decodedURLs.isEmpty());
0097     QCOMPARE(decodedURLs.count(), 2);
0098     QCOMPARE(static_cast<QUrl>(decodedURLs[0]), qurls[0]);
0099     QCOMPARE(static_cast<QUrl>(decodedURLs[1]), qurls[1]);
0100     QVERIFY(decodedMetaData.isEmpty());
0101     delete mimeData;
0102 }
0103 
0104 void KUrlMimeTest::testMostLocalUrlList()
0105 {
0106     QMimeData *mimeData = new QMimeData;
0107     KUrl::List urls;
0108     urls.append(KUrl("desktop:/foo"));
0109     urls.append(KUrl("desktop:/bar"));
0110     KUrl::List localUrls;
0111     localUrls.append(KUrl("file:/home/dfaure/Desktop/foo"));
0112     localUrls.append(KUrl("file:/home/dfaure/Desktop/bar"));
0113 
0114     urls.populateMimeData(localUrls, mimeData);
0115 
0116     QVERIFY(KUrl::List::canDecode(mimeData));
0117     QVERIFY(mimeData->hasUrls());
0118     QVERIFY(mimeData->hasText());
0119     QVERIFY(mimeData->hasFormat(QLatin1String("text/plain")));
0120 
0121     // KUrl decodes the real "kde" urls by default
0122     KUrl::List decodedURLs = KUrl::List::fromMimeData(mimeData);
0123     QVERIFY(!decodedURLs.isEmpty());
0124     const QString space(QLatin1Char(' '));
0125     QCOMPARE(decodedURLs.toStringList().join(space), urls.toStringList().join(space));
0126 
0127     // KUrl can also be told to decode the "most local" urls
0128     decodedURLs = KUrl::List::fromMimeData(mimeData, KUrl::List::PreferLocalUrls);
0129     QVERIFY(!decodedURLs.isEmpty());
0130     QCOMPARE(decodedURLs.toStringList().join(space), localUrls.toStringList().join(space));
0131 
0132     // QMimeData decodes the "most local" urls
0133     const QList<QUrl> qurls = mimeData->urls();
0134     QCOMPARE(qurls.count(), localUrls.count());
0135     for (int i = 0; i < qurls.count(); ++i) {
0136         QCOMPARE(qurls[i], static_cast<QUrl>(localUrls[i]));
0137     }
0138 
0139 }
0140 
0141 #include "moc_kurlmimetest.cpp"