File indexing completed on 2025-01-12 12:29:24
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2011 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kiotesthelper.h" // createTestFile etc. 0009 #include <QApplication> 0010 #include <QTest> 0011 #include <kio/copyjob.h> 0012 #include <kio/deletejob.h> 0013 #include <kio/paste.h> 0014 0015 #include <QClipboard> 0016 #include <QMimeData> 0017 0018 static QString otherTmpDir() 0019 { 0020 #ifdef Q_OS_WIN 0021 return QDir::tempPath() + "/jobtest/"; 0022 #else 0023 // This one needs to be on another partition 0024 return QStringLiteral("/tmp/jobtest/"); 0025 #endif 0026 } 0027 0028 class JobGuiTest : public QObject 0029 { 0030 Q_OBJECT 0031 0032 public Q_SLOTS: 0033 void initTestCase() 0034 { 0035 // Start with a clean base dir 0036 cleanupTestCase(); 0037 homeTmpDir(); // create it 0038 if (!QFile::exists(otherTmpDir())) { 0039 bool ok = QDir().mkdir(otherTmpDir()); 0040 if (!ok) { 0041 qFatal("Couldn't create %s", qPrintable(homeTmpDir())); 0042 } 0043 } 0044 } 0045 0046 void cleanupTestCase() 0047 { 0048 delDir(homeTmpDir()); 0049 delDir(otherTmpDir()); 0050 } 0051 0052 void pasteFileToOtherPartition() 0053 { 0054 const QString filePath = homeTmpDir() + "fileFromHome"; 0055 const QString dest = otherTmpDir() + "fileFromHome_copied"; 0056 QFile::remove(dest); 0057 createTestFile(filePath); 0058 0059 QMimeData *mimeData = new QMimeData; 0060 QUrl fileUrl = QUrl::fromLocalFile(filePath); 0061 mimeData->setUrls(QList<QUrl>{fileUrl}); 0062 QApplication::clipboard()->setMimeData(mimeData); 0063 0064 KIO::Job *job = KIO::pasteClipboard(QUrl::fromLocalFile(otherTmpDir()), static_cast<QWidget *>(nullptr)); 0065 job->setUiDelegate(nullptr); 0066 bool ok = job->exec(); 0067 QVERIFY(ok); 0068 0069 QVERIFY(QFile::exists(dest)); 0070 QVERIFY(QFile::exists(filePath)); // still there 0071 } 0072 0073 private: 0074 static void delDir(const QString &pathOrUrl) 0075 { 0076 KIO::Job *job = KIO::del(QUrl::fromLocalFile(pathOrUrl), KIO::HideProgressInfo); 0077 job->setUiDelegate(nullptr); 0078 job->exec(); 0079 } 0080 }; 0081 0082 QTEST_MAIN(JobGuiTest) 0083 0084 #include "jobguitest.moc"