File indexing completed on 2024-05-12 16:25:28

0001 /*
0002    SPDX-FileCopyrightText: 2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "previewurlcachemanagertest.h"
0008 #include "previewurlcachemanager.h"
0009 #include <QStandardPaths>
0010 #include <QTemporaryDir>
0011 #include <QTest>
0012 QTEST_GUILESS_MAIN(PreviewUrlCacheManagerTest)
0013 
0014 PreviewUrlCacheManagerTest::PreviewUrlCacheManagerTest(QObject *parent)
0015     : QObject{parent}
0016 {
0017     QStandardPaths::setTestModeEnabled(true);
0018 }
0019 
0020 void PreviewUrlCacheManagerTest::shouldHaveDefaultValues()
0021 {
0022     PreviewUrlCacheManager w(nullptr);
0023     QCOMPARE(w.embedCacheExpirationDays(), -1);
0024     QVERIFY(w.cachePath().isEmpty());
0025 }
0026 
0027 void PreviewUrlCacheManagerTest::shouldTestRemoveOldFiles()
0028 {
0029     // Test 1: need to clean cache 1 file! 1 file to remove
0030     {
0031         QTemporaryDir accountFileTmp;
0032         const QString cachePath{accountFileTmp.path()};
0033         QFile file(cachePath + QStringLiteral("/foo1"));
0034         if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0035             qWarning() << " Impossible to create file";
0036         }
0037         if (!file.setFileTime(QDateTime(QDate(2024, 1, 1), QTime(1, 1, 1)), QFileDevice::FileModificationTime)) {
0038             qWarning() << " Impossible to change modification date";
0039         }
0040         file.close();
0041 
0042         QDir dir(cachePath);
0043         const QFileInfoList infoLists = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0044         // qDebug() << "current infoLists " << infoLists.count() << infoLists;
0045         QCOMPARE(infoLists.count(), 1);
0046 
0047         PreviewUrlCacheManager w(nullptr);
0048         w.setCachePath(cachePath);
0049         w.setEmbedCacheExpirationDays(2);
0050 
0051         QDir dir2(cachePath);
0052         const QFileInfoList infoLists2 = dir2.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0053         // qDebug() << "after clean infoLists2 " << infoLists2.count();
0054         QVERIFY(infoLists2.isEmpty());
0055     }
0056     // Test 2: 10 files not necessary to clean them.
0057     {
0058         QTemporaryDir accountFileTmp;
0059         const QString cachePath{accountFileTmp.path()};
0060         QStringList firstList;
0061         for (int i = 0; i < 10; ++i) {
0062             QFile file(cachePath + QStringLiteral("/foo%1").arg(QString::number(i)));
0063             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0064                 qWarning() << " Impossible to create file";
0065             }
0066             if (!file.setFileTime(QDateTime(QDate(2024, 1, i + 1), QTime(1, 1, 1)), QFileDevice::FileModificationTime)) {
0067                 qWarning() << " Impossible to change modification date" << i;
0068             }
0069             firstList.append(QFileInfo(file).fileName());
0070             file.close();
0071         }
0072 
0073         QDir dir(cachePath);
0074         const QFileInfoList infoLists = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0075         // qDebug() << "current infoLists " << infoLists.count() << infoLists;
0076         QCOMPARE(infoLists.count(), 10);
0077 
0078         PreviewUrlCacheManager w(nullptr);
0079         w.setCachePath(cachePath);
0080         w.setEmbedCacheExpirationDays(5555); // 5555 days. It will not have pb :)
0081 
0082         QDir dir2(cachePath);
0083         const QFileInfoList infoLists2 = dir2.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0084         QStringList lst;
0085         for (const QFileInfo &info : infoLists2) {
0086             lst.append(info.fileName());
0087         }
0088         // qDebug() << "after clean infoLists2 " << infoLists2.count();
0089         QVERIFY(!infoLists2.isEmpty());
0090         QCOMPARE(infoLists2.count(), 10);
0091         QCOMPARE(firstList, lst);
0092     }
0093     // Test 3: 10 files => 5 we need to delete
0094     {
0095         QTemporaryDir accountFileTmp;
0096         const QString cachePath{accountFileTmp.path()};
0097         QStringList firstList;
0098         for (int i = 0; i < 10; ++i) {
0099             QFile file(cachePath + QStringLiteral("/foo%1").arg(QString::number(i)));
0100             if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0101                 qWarning() << " Impossible to create file";
0102             }
0103             if (!file.setFileTime(QDateTime(QDate(2024, 1, i + 1), QTime(1, 1, 1)), QFileDevice::FileModificationTime)) {
0104                 qWarning() << " Impossible to change modification date" << i;
0105             }
0106             firstList.append(QFileInfo(file).fileName());
0107             file.close();
0108         }
0109 
0110         QDir dir(cachePath);
0111         const QFileInfoList infoLists = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0112         // qDebug() << "current infoLists " << infoLists.count() << infoLists;
0113         QCOMPARE(infoLists.count(), 10);
0114 
0115         PreviewUrlCacheManager w(nullptr);
0116         w.setCachePath(cachePath);
0117         w.setCurrentDate(QDate(2024, 1, 11));
0118         w.setEmbedCacheExpirationDays(5); // 5555 days. It will not have pb :)
0119 
0120         QDir dir2(cachePath);
0121         const QFileInfoList infoLists2 = dir2.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0122         QStringList lst;
0123         lst.append(QStringLiteral("foo6"));
0124         lst.append(QStringLiteral("foo7"));
0125         lst.append(QStringLiteral("foo8"));
0126         lst.append(QStringLiteral("foo9"));
0127         lst.append(QStringLiteral("foo10"));
0128         // qDebug() << "after clean infoLists2 " << infoLists2.count();
0129         QVERIFY(!infoLists2.isEmpty());
0130         QCOMPARE(infoLists2.count(), 5);
0131     }
0132 }
0133 
0134 #include "moc_previewurlcachemanagertest.cpp"