File indexing completed on 2024-06-23 10:37:26

0001 /*
0002     This file is part of KDE
0003     SPDX-FileCopyrightText: 2005-2006 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "pastetest.h"
0009 
0010 #include <QDir>
0011 #include <QMimeData>
0012 #include <QSignalSpy>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 #include <QUrl>
0016 
0017 #include <KFileItem>
0018 #include <KUrlMimeData>
0019 #include <kio/paste.h>
0020 #include <kio/pastejob.h>
0021 
0022 QTEST_MAIN(KIOPasteTest)
0023 
0024 void KIOPasteTest::initTestCase()
0025 {
0026     QStandardPaths::setTestModeEnabled(true);
0027 
0028     QVERIFY(m_tempDir.isValid());
0029     m_dir = m_tempDir.path();
0030 }
0031 
0032 void KIOPasteTest::testPopulate()
0033 {
0034     QMimeData *mimeData = new QMimeData;
0035 
0036     // Those URLs don't have to exist.
0037     QUrl mediaURL(QStringLiteral("media:/hda1/tmp/Mat%C3%A9riel"));
0038     QUrl localURL(QStringLiteral("file:///tmp/Mat%C3%A9riel"));
0039     QList<QUrl> kdeURLs;
0040     kdeURLs << mediaURL;
0041     QList<QUrl> mostLocalURLs;
0042     mostLocalURLs << localURL;
0043 
0044     KUrlMimeData::setUrls(kdeURLs, mostLocalURLs, mimeData);
0045 
0046     QVERIFY(mimeData->hasUrls());
0047     const QList<QUrl> lst = KUrlMimeData::urlsFromMimeData(mimeData);
0048     QCOMPARE(lst.count(), 1);
0049     QCOMPARE(lst[0].url(), mediaURL.url());
0050 
0051     const bool isCut = KIO::isClipboardDataCut(mimeData);
0052     QVERIFY(!isCut);
0053 
0054     delete mimeData;
0055 }
0056 
0057 void KIOPasteTest::testCut()
0058 {
0059     QMimeData *mimeData = new QMimeData;
0060 
0061     QUrl localURL1(QStringLiteral("file:///tmp/Mat%C3%A9riel"));
0062     QUrl localURL2(QStringLiteral("file:///tmp"));
0063     QList<QUrl> localURLs;
0064     localURLs << localURL1 << localURL2;
0065 
0066     KUrlMimeData::setUrls(QList<QUrl>(), localURLs, mimeData);
0067     KIO::setClipboardDataCut(mimeData, true);
0068 
0069     QVERIFY(mimeData->hasUrls());
0070     const QList<QUrl> lst = KUrlMimeData::urlsFromMimeData(mimeData);
0071     QCOMPARE(lst.count(), 2);
0072     QCOMPARE(lst[0].url(), localURL1.url());
0073     QCOMPARE(lst[1].url(), localURL2.url());
0074 
0075     const bool isCut = KIO::isClipboardDataCut(mimeData);
0076     QVERIFY(isCut);
0077 
0078     delete mimeData;
0079 }
0080 
0081 void KIOPasteTest::testPasteActionText_data()
0082 {
0083     QTest::addColumn<QList<QUrl>>("urls");
0084     QTest::addColumn<bool>("data");
0085     QTest::addColumn<bool>("expectedEnabled");
0086     QTest::addColumn<QString>("expectedText");
0087 
0088     QList<QUrl> urlDir = QList<QUrl>{QUrl::fromLocalFile(QDir::tempPath())};
0089     QList<QUrl> urlFile = QList<QUrl>{QUrl::fromLocalFile(QCoreApplication::applicationFilePath())};
0090     QList<QUrl> urlRemote = QList<QUrl>{QUrl(QStringLiteral("http://www.kde.org"))};
0091     QList<QUrl> urls = urlDir + urlRemote;
0092     QTest::newRow("nothing") << QList<QUrl>() << false << false << "Paste";
0093     QTest::newRow("one_dir") << urlDir << false << true << "Paste One Folder";
0094     QTest::newRow("one_file") << urlFile << false << true << "Paste One File";
0095     QTest::newRow("one_url") << urlRemote << false << true << "Paste One Item";
0096     QTest::newRow("two_urls") << urls << false << true << "Paste 2 Items";
0097     QTest::newRow("data") << QList<QUrl>() << true << true << "Paste Clipboard Contents...";
0098 }
0099 
0100 void KIOPasteTest::testPasteActionText()
0101 {
0102     QFETCH(QList<QUrl>, urls);
0103     QFETCH(bool, data);
0104     QFETCH(bool, expectedEnabled);
0105     QFETCH(QString, expectedText);
0106 
0107     QMimeData mimeData;
0108     if (!urls.isEmpty()) {
0109         mimeData.setUrls(urls);
0110     }
0111     if (data) {
0112         mimeData.setText(QStringLiteral("foo"));
0113     }
0114     QCOMPARE(KIO::canPasteMimeData(&mimeData), expectedEnabled);
0115     bool canPaste;
0116     KFileItem destItem(QUrl::fromLocalFile(QDir::homePath()));
0117     QCOMPARE(KIO::pasteActionText(&mimeData, &canPaste, destItem), expectedText);
0118     QCOMPARE(canPaste, expectedEnabled);
0119 
0120     KFileItem nonWritableDestItem(QUrl::fromLocalFile(QStringLiteral("/nonwritable")));
0121     QCOMPARE(KIO::pasteActionText(&mimeData, &canPaste, nonWritableDestItem), expectedText);
0122     QCOMPARE(canPaste, false);
0123 
0124     KFileItem emptyUrlDestItem = KFileItem(QUrl());
0125     QCOMPARE(KIO::pasteActionText(&mimeData, &canPaste, emptyUrlDestItem), expectedText);
0126     QCOMPARE(canPaste, false);
0127 
0128     KFileItem nullDestItem;
0129     QCOMPARE(KIO::pasteActionText(&mimeData, &canPaste, nullDestItem), expectedText);
0130     QCOMPARE(canPaste, false);
0131 }
0132 
0133 static void createTestFile(const QString &path)
0134 {
0135     QFile f(path);
0136     if (!f.open(QIODevice::WriteOnly)) {
0137         qFatal("Couldn't create %s", qPrintable(path));
0138     }
0139     QByteArray data("Hello world", 11);
0140     QCOMPARE(data.size(), 11);
0141     f.write(data);
0142 }
0143 
0144 void KIOPasteTest::testPasteJob_data()
0145 {
0146     QTest::addColumn<QList<QUrl>>("urls");
0147     QTest::addColumn<bool>("data");
0148     QTest::addColumn<bool>("cut");
0149     QTest::addColumn<QString>("expectedFileName");
0150 
0151     const QString file = m_dir + "/file";
0152     createTestFile(file);
0153 
0154     QList<QUrl> urlFile = QList<QUrl>{QUrl::fromLocalFile(file)};
0155     QList<QUrl> urlDir = QList<QUrl>{QUrl::fromLocalFile(m_dir)};
0156 
0157     QTest::newRow("nothing") << QList<QUrl>() << false << false << QString();
0158     QTest::newRow("copy_one_file") << urlFile << false << false << file.section('/', -1);
0159     QTest::newRow("copy_one_dir") << urlDir << false << false << m_dir.section('/', -1);
0160     QTest::newRow("cut_one_file") << urlFile << false << true << file.section('/', -1);
0161     QTest::newRow("cut_one_dir") << urlDir << false << true << m_dir.section('/', -1);
0162 
0163     // Shows a dialog!
0164     // QTest::newRow("data") << QList<QUrl>() << true << "output_file";
0165 }
0166 
0167 void KIOPasteTest::testPasteJob()
0168 {
0169     QFETCH(QList<QUrl>, urls);
0170     QFETCH(bool, data);
0171     QFETCH(bool, cut);
0172     QFETCH(QString, expectedFileName);
0173 
0174     QMimeData mimeData;
0175     bool isDir = false;
0176     bool isFile = false;
0177     if (!urls.isEmpty()) {
0178         mimeData.setUrls(urls);
0179         QFileInfo fileInfo(urls.first().toLocalFile());
0180         isDir = fileInfo.isDir();
0181         isFile = fileInfo.isFile();
0182     }
0183     if (data) {
0184         mimeData.setText(QStringLiteral("Hello world"));
0185     }
0186     KIO::setClipboardDataCut(&mimeData, cut);
0187 
0188     QTemporaryDir destTempDir;
0189     QVERIFY(destTempDir.isValid());
0190     const QString destDir = destTempDir.path();
0191     KIO::PasteJob *job = KIO::paste(&mimeData, QUrl::fromLocalFile(destDir), KIO::HideProgressInfo);
0192     QSignalSpy spy(job, &KIO::PasteJob::itemCreated);
0193     QVERIFY(spy.isValid());
0194     job->setUiDelegate(nullptr);
0195     const bool expectedSuccess = !expectedFileName.isEmpty();
0196     QCOMPARE(job->exec(), expectedSuccess);
0197     if (expectedSuccess) {
0198         const QString destFile = destDir + '/' + expectedFileName;
0199         QVERIFY2(QFile::exists(destFile), qPrintable(expectedFileName));
0200         if (isDir) {
0201             QVERIFY(QFileInfo(destFile).isDir());
0202         } else {
0203             QVERIFY(QFileInfo(destFile).isFile());
0204             QFile file(destFile);
0205             QVERIFY(file.open(QIODevice::ReadOnly));
0206             QCOMPARE(QString(file.readAll()), QString("Hello world"));
0207         }
0208         if (cut) {
0209             QVERIFY(!QFile::exists(urls.first().toLocalFile()));
0210         } else {
0211             QVERIFY(QFile::exists(urls.first().toLocalFile()));
0212         }
0213         QCOMPARE(spy.count(), isFile || cut ? 1 : 2);
0214         QCOMPARE(spy.at(0).at(0).value<QUrl>().toLocalFile(), destFile);
0215     }
0216 }
0217 
0218 #include "moc_pastetest.cpp"