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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2022 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "copyjob.h"
0009 #include "kiotesthelper.h"
0010 #include "mockcoredelegateextensions.h"
0011 
0012 #include <kio/deletejob.h>
0013 #include <kio/deleteortrashjob.h>
0014 
0015 #include <KJobUiDelegate>
0016 
0017 #include <QObject>
0018 
0019 class DeleteOrTrashJobTest : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void deleteFileTest();
0024     void moveToTrashTest();
0025     void emptyTrashTest();
0026     void deleteTrashFileTest();
0027 };
0028 
0029 QTEST_MAIN(DeleteOrTrashJobTest)
0030 
0031 using AskIface = KIO::AskUserActionInterface;
0032 
0033 struct Info {
0034     KIO::DeleteOrTrashJob *job = nullptr;
0035     MockAskUserInterface *askUserHandler = nullptr;
0036 };
0037 
0038 static Info createJobWithUrl(AskIface::DeletionType deletionType, const QUrl &url)
0039 {
0040     auto *job = new KIO::DeleteOrTrashJob({url}, //
0041                                           deletionType,
0042                                           AskIface::DefaultConfirmation,
0043                                           nullptr);
0044 
0045     auto uiDelegate = new KJobUiDelegate{};
0046     job->setUiDelegate(uiDelegate);
0047     auto askUserHandler = new MockAskUserInterface(uiDelegate);
0048     askUserHandler->m_deleteResult = true;
0049     return {job, askUserHandler};
0050 }
0051 
0052 static Info createJob(AskIface::DeletionType deletionType)
0053 {
0054     const QString path = homeTmpDir() + "delete_or_trash_job_test_file";
0055     createTestFile(path);
0056     return createJobWithUrl(deletionType, QUrl::fromLocalFile(path));
0057 }
0058 
0059 void DeleteOrTrashJobTest::deleteFileTest()
0060 {
0061     auto [job, askUserHandler] = createJob(AskIface::Delete);
0062     QVERIFY(job->exec());
0063     QCOMPARE(askUserHandler->m_askUserDeleteCalled, 1);
0064     QCOMPARE(askUserHandler->m_delType, KIO::AskUserActionInterface::DeletionType::Delete);
0065 }
0066 
0067 void DeleteOrTrashJobTest::moveToTrashTest()
0068 {
0069     auto [job, askUserHandler] = createJob(AskIface::Trash);
0070     QVERIFY(job->exec());
0071     QCOMPARE(askUserHandler->m_askUserDeleteCalled, 1);
0072     QCOMPARE(askUserHandler->m_delType, KIO::AskUserActionInterface::DeletionType::Trash);
0073 }
0074 
0075 void DeleteOrTrashJobTest::emptyTrashTest()
0076 {
0077     auto [job, askUserHandler] = createJob(AskIface::EmptyTrash);
0078     QVERIFY(job->exec());
0079     QCOMPARE(askUserHandler->m_askUserDeleteCalled, 1);
0080     QCOMPARE(askUserHandler->m_delType, KIO::AskUserActionInterface::DeletionType::EmptyTrash);
0081 }
0082 
0083 void DeleteOrTrashJobTest::deleteTrashFileTest()
0084 {
0085     QStandardPaths::setTestModeEnabled(true);
0086 
0087     const QString path = homeTmpDir() + "delete_or_trash_job_test_file";
0088     createTestFile(path);
0089 
0090     KIO::CopyJob *trashJob = KIO::move(QUrl::fromLocalFile(path), QUrl(QStringLiteral("trash:/")), KIO::HideProgressInfo);
0091     trashJob->setUiDelegate(nullptr);
0092     QVERIFY(trashJob->exec());
0093 
0094     auto [job, askUserHandler] = createJobWithUrl(AskIface::Trash, QUrl("trash:/0-delete_or_trash_job_test_file"));
0095     bool res = job->exec();
0096     QVERIFY(res);
0097     QCOMPARE(askUserHandler->m_delType, KIO::AskUserActionInterface::DeletionType::Delete);
0098     QCOMPARE(askUserHandler->m_askUserDeleteCalled, 1);
0099 }
0100 
0101 #include "deleteortrashjobtest.moc"