File indexing completed on 2024-02-18 03:56:47

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