File indexing completed on 2024-03-24 05:00:06

0001 /**************************************************************************
0002  *   Copyright (C) 2011 Matthias Fuchs <mat69@gmx.net>                     *
0003  *   Code mostly from email from Will Stephenson <wstephenson@suse.de>     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0019  ***************************************************************************/
0020 
0021 #include "filedeletertest.h"
0022 #include "../core/filedeleter.h"
0023 
0024 #include <QFile>
0025 #include <QSignalSpy>
0026 #include <QTemporaryFile>
0027 #include <QUrl>
0028 #include <QtTest>
0029 
0030 #include <KJob>
0031 
0032 void FileDeleterTest::fileDeleterTest()
0033 {
0034     // nothing started to delete yet
0035     QVERIFY(!FileDeleter::isFileBeingDeleted(QUrl()));
0036     QVERIFY(!FileDeleter::isFileBeingDeleted(QUrl::fromLocalFile("/tmp/aFile")));
0037 
0038     // create file that is going to be deleted
0039     QTemporaryFile file1;
0040     file1.setAutoRemove(false);
0041     QVERIFY(file1.open());
0042     const QString fileUrl1 = file1.fileName();
0043     QVERIFY(QFile::exists(fileUrl1));
0044 
0045     // nothing started to delete yet
0046     QVERIFY(!FileDeleter::isFileBeingDeleted(QUrl::fromLocalFile(fileUrl1)));
0047 
0048     // create two QObjects that will receive the result signal
0049     SignalReceiver receiver1;
0050     QSignalSpy spy1(&receiver1, &SignalReceiver::result);
0051     QVERIFY(spy1.isEmpty());
0052 
0053     SignalReceiver receiver2;
0054     QSignalSpy spy2(&receiver1, &SignalReceiver::result);
0055     QVERIFY(spy2.isEmpty());
0056 
0057     // delete the file
0058     FileDeleter::deleteFile(QUrl::fromLocalFile(fileUrl1), &receiver1, SIGNAL(result()));
0059     QVERIFY(FileDeleter::isFileBeingDeleted(QUrl::fromLocalFile(fileUrl1)));
0060 
0061     // deleting twice with the same receiver, still the method should only be called once
0062     FileDeleter::deleteFile(QUrl::fromLocalFile(fileUrl1), &receiver1, SIGNAL(result()));
0063 
0064     KJob *job = FileDeleter::deleteFile(QUrl::fromLocalFile(fileUrl1));
0065     connect(job, &KJob::result, &receiver2, &SignalReceiver::result);
0066 
0067     // removal should be done by now
0068     QTest::qWait(5000);
0069 
0070     QVERIFY(!FileDeleter::isFileBeingDeleted(QUrl::fromLocalFile(fileUrl1)));
0071     QVERIFY(!QFile::exists(fileUrl1));
0072 
0073     QCOMPARE(spy1.count(), 1);
0074     QCOMPARE(spy2.count(), 1);
0075 }
0076 
0077 QTEST_MAIN(FileDeleterTest)
0078 
0079 #include "moc_filedeletertest.cpp"