File indexing completed on 2024-09-22 04:51:21

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "attachmenttemporaryfilesdirstest.h"
0008 #include "../attachmenttemporaryfilesdirs.h"
0009 #include <QDebug>
0010 #include <QTemporaryDir>
0011 #include <QTest>
0012 
0013 using namespace MimeTreeParser;
0014 
0015 AttachmentTemporaryFilesDirsTest::AttachmentTemporaryFilesDirsTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 AttachmentTemporaryFilesDirsTest::~AttachmentTemporaryFilesDirsTest() = default;
0021 
0022 void AttachmentTemporaryFilesDirsTest::shouldHaveDefaultValue()
0023 {
0024     AttachmentTemporaryFilesDirs attachmentDir;
0025     QVERIFY(attachmentDir.temporaryFiles().isEmpty());
0026     QVERIFY(attachmentDir.temporaryDirs().isEmpty());
0027 }
0028 
0029 void AttachmentTemporaryFilesDirsTest::shouldAddTemporaryFiles()
0030 {
0031     AttachmentTemporaryFilesDirs attachmentDir;
0032     attachmentDir.addTempFile(QStringLiteral("foo"));
0033     QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
0034     attachmentDir.addTempFile(QStringLiteral("foo1"));
0035     QCOMPARE(attachmentDir.temporaryFiles().count(), 2);
0036 }
0037 
0038 void AttachmentTemporaryFilesDirsTest::shouldAddTemporaryDirs()
0039 {
0040     AttachmentTemporaryFilesDirs attachmentDir;
0041     attachmentDir.addTempDir(QStringLiteral("foo"));
0042     QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
0043     attachmentDir.addTempDir(QStringLiteral("foo1"));
0044     QCOMPARE(attachmentDir.temporaryDirs().count(), 2);
0045 }
0046 
0047 void AttachmentTemporaryFilesDirsTest::shouldNotAddSameFiles()
0048 {
0049     AttachmentTemporaryFilesDirs attachmentDir;
0050     attachmentDir.addTempFile(QStringLiteral("foo"));
0051     QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
0052     attachmentDir.addTempFile(QStringLiteral("foo"));
0053     QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
0054 }
0055 
0056 void AttachmentTemporaryFilesDirsTest::shouldNotAddSameDirs()
0057 {
0058     AttachmentTemporaryFilesDirs attachmentDir;
0059     attachmentDir.addTempDir(QStringLiteral("foo"));
0060     QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
0061     attachmentDir.addTempDir(QStringLiteral("foo"));
0062     QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
0063 }
0064 
0065 void AttachmentTemporaryFilesDirsTest::shouldForceRemoveTemporaryDirs()
0066 {
0067     AttachmentTemporaryFilesDirs attachmentDir;
0068     attachmentDir.addTempDir(QStringLiteral("foo"));
0069     attachmentDir.addTempDir(QStringLiteral("foo1"));
0070     QCOMPARE(attachmentDir.temporaryDirs().count(), 2);
0071     attachmentDir.forceCleanTempFiles();
0072     QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
0073     QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
0074 }
0075 
0076 void AttachmentTemporaryFilesDirsTest::shouldForceRemoveTemporaryFiles()
0077 {
0078     AttachmentTemporaryFilesDirs attachmentDir;
0079     attachmentDir.addTempFile(QStringLiteral("foo"));
0080     attachmentDir.addTempFile(QStringLiteral("foo2"));
0081     QCOMPARE(attachmentDir.temporaryFiles().count(), 2);
0082     attachmentDir.forceCleanTempFiles();
0083     QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
0084     QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
0085 }
0086 
0087 void AttachmentTemporaryFilesDirsTest::shouldCreateDeleteTemporaryFiles()
0088 {
0089     QTemporaryDir tmpDir;
0090     QVERIFY(tmpDir.isValid());
0091     QFile file(tmpDir.path() + QStringLiteral("/foo"));
0092     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0093         qWarning() << "Can open file: " << file.fileName();
0094         return;
0095     }
0096     tmpDir.setAutoRemove(false);
0097     file.close();
0098     QVERIFY(file.exists());
0099     AttachmentTemporaryFilesDirs attachmentDir;
0100     attachmentDir.addTempDir(tmpDir.path());
0101     attachmentDir.addTempFile(file.fileName());
0102     QVERIFY(!attachmentDir.temporaryFiles().isEmpty());
0103     QCOMPARE(attachmentDir.temporaryFiles().constFirst(), file.fileName());
0104     const QString path = tmpDir.path();
0105     attachmentDir.forceCleanTempFiles();
0106     QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
0107     QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
0108     QVERIFY(!QDir(path).exists());
0109 }
0110 
0111 void AttachmentTemporaryFilesDirsTest::shouldRemoveTemporaryFilesAfterTime()
0112 {
0113     QTemporaryDir tmpDir;
0114     QVERIFY(tmpDir.isValid());
0115     QFile file(tmpDir.path() + QStringLiteral("/foo"));
0116     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0117         qDebug() << "Can open file";
0118         return;
0119     }
0120     tmpDir.setAutoRemove(false);
0121     file.close();
0122     QVERIFY(file.exists());
0123     AttachmentTemporaryFilesDirs attachmentDir;
0124     attachmentDir.addTempDir(tmpDir.path());
0125     attachmentDir.addTempFile(file.fileName());
0126     QVERIFY(!attachmentDir.temporaryFiles().isEmpty());
0127     QCOMPARE(attachmentDir.temporaryFiles().constFirst(), file.fileName());
0128     attachmentDir.setDelayRemoveAllInMs(500);
0129     QTest::qSleep(1000);
0130     attachmentDir.removeTempFiles();
0131     const QString path = tmpDir.path();
0132     attachmentDir.forceCleanTempFiles();
0133     QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
0134     QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
0135     QVERIFY(!QDir(path).exists());
0136 }
0137 
0138 QTEST_GUILESS_MAIN(AttachmentTemporaryFilesDirsTest)
0139 
0140 #include "moc_attachmenttemporaryfilesdirstest.cpp"