File indexing completed on 2024-05-12 04:19:59

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (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 Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018 
0019 */
0020 
0021 #include "timeutilstest.h"
0022 
0023 // libc
0024 #include <utime.h>
0025 
0026 // Qt
0027 #include <QTemporaryFile>
0028 #include <QTest>
0029 
0030 // KF
0031 #include <KFileItem>
0032 
0033 // Local
0034 #include "../lib/timeutils.h"
0035 
0036 #include "testutils.h"
0037 
0038 QTEST_MAIN(TimeUtilsTest)
0039 
0040 using namespace Gwenview;
0041 
0042 static void touchFile(const QString &path)
0043 {
0044     utime(QFile::encodeName(path).data(), nullptr);
0045 }
0046 
0047 #define NEW_ROW(fileName, dateTime) QTest::newRow(fileName) << fileName << dateTime
0048 void TimeUtilsTest::testBasic_data()
0049 {
0050     QTest::addColumn<QString>("fileName");
0051     QTest::addColumn<QDateTime>("expectedDateTime");
0052 
0053     NEW_ROW("date/exif-datetimeoriginal.jpg", QDateTime::fromString("2003-03-10T17:45:21", Qt::ISODate));
0054     NEW_ROW("date/exif-datetime-only.jpg", QDateTime::fromString("2003-03-25T02:02:21", Qt::ISODate));
0055 
0056     QUrl url = urlForTestFile("test.png");
0057     KFileItem item(url);
0058     NEW_ROW("test.png", item.time(KFileItem::ModificationTime));
0059 }
0060 
0061 void TimeUtilsTest::testBasic()
0062 {
0063     QFETCH(QString, fileName);
0064     QFETCH(QDateTime, expectedDateTime);
0065     QDateTime dateTime;
0066     QUrl url = urlForTestFile(fileName);
0067     KFileItem item(url);
0068 
0069     dateTime = TimeUtils::dateTimeForFileItem(item);
0070     QCOMPARE(dateTime, expectedDateTime);
0071 
0072     dateTime = TimeUtils::dateTimeForFileItem(item, TimeUtils::SkipCache);
0073     QCOMPARE(dateTime, expectedDateTime);
0074 }
0075 
0076 void TimeUtilsTest::testCache()
0077 {
0078     QTemporaryFile tempFile;
0079     QVERIFY(tempFile.open());
0080     QUrl url = QUrl::fromLocalFile(tempFile.fileName());
0081     KFileItem item1(url);
0082     QDateTime dateTime1 = TimeUtils::dateTimeForFileItem(item1);
0083     QCOMPARE(dateTime1, item1.time(KFileItem::ModificationTime));
0084 
0085     QTest::qWait(1200);
0086     touchFile(url.toLocalFile());
0087 
0088     KFileItem item2(url);
0089     QDateTime dateTime2 = TimeUtils::dateTimeForFileItem(item2);
0090 
0091     QVERIFY(dateTime1 != dateTime2);
0092 
0093     QCOMPARE(dateTime2, item2.time(KFileItem::ModificationTime));
0094 }
0095 
0096 #include "moc_timeutilstest.cpp"