Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 9d09fde5 authored by Laurent Montel's avatar Laurent Montel 😁

Add test about remove file after a timeout

parent c0260bd3
......@@ -31,7 +31,8 @@ using namespace PimCommon;
static int DELETEAFTER = 10000;
AttachmentTemporaryFilesDirs::AttachmentTemporaryFilesDirs(QObject *parent)
: QObject(parent)
: QObject(parent),
mDelayRemoveAll(10000)
{
}
......@@ -40,9 +41,14 @@ AttachmentTemporaryFilesDirs::~AttachmentTemporaryFilesDirs()
{
}
void AttachmentTemporaryFilesDirs::setDelayRemoveAllInMs(int ms)
{
mDelayRemoveAll = ( ms < 0) ? 0 : ms;
}
void AttachmentTemporaryFilesDirs::removeTempFiles()
{
QTimer::singleShot(DELETEAFTER, this, SLOT(slotRemoveTempFiles()));
QTimer::singleShot(mDelayRemoveAll, this, SLOT(slotRemoveTempFiles()));
}
void AttachmentTemporaryFilesDirs::forceCleanTempFiles()
......
......@@ -41,12 +41,16 @@ public:
void forceCleanTempFiles();
QStringList temporaryDirs() const;
void setDelayRemoveAllInMs(int ms);
private Q_SLOTS:
void slotRemoveTempFiles();
private:
QStringList mTempFiles;
QStringList mTempDirs;
int mDelayRemoveAll;
};
}
......
......@@ -126,4 +126,31 @@ void AttachmentTemporaryFilesDirsTest::shouldCreateDeleteTemporaryFiles()
QVERIFY(!QDir(path).exists());
}
void AttachmentTemporaryFilesDirsTest::shouldRemoveTemporaryFilesAfterTime()
{
KTempDir tmpDir;
QVERIFY(tmpDir.exists());
QFile file(tmpDir.name() + QLatin1String("/foo"));
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
kDebug()<<"Can open file";
return;
}
tmpDir.setAutoRemove(false);
file.close();
QVERIFY(file.exists());
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(tmpDir.name());
attachmentDir.addTempFile(file.fileName());
QVERIFY(!attachmentDir.temporaryFiles().isEmpty());
QCOMPARE(attachmentDir.temporaryFiles().first(), file.fileName());
attachmentDir.setDelayRemoveAllInMs(500);
QTest::qSleep(1000);
attachmentDir.removeTempFiles();
const QString path = tmpDir.name();
attachmentDir.forceCleanTempFiles();
QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
QVERIFY(!QDir(path).exists());
}
QTEST_KDEMAIN(AttachmentTemporaryFilesDirsTest, NoGUI)
......@@ -38,6 +38,7 @@ private Q_SLOTS:
void shouldForceRemoveTemporaryDirs();
void shouldForceRemoveTemporaryFiles();
void shouldCreateDeleteTemporaryFiles();
void shouldRemoveTemporaryFilesAfterTime();
};
#endif // ATTACHMENTTEMPORARYFILESDIRSTEST_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment