diff --git a/autotests/filemetadatadatedisplaytest.h b/autotests/filemetadatadatedisplaytest.h --- a/autotests/filemetadatadatedisplaytest.h +++ b/autotests/filemetadatadatedisplaytest.h @@ -1,20 +1,20 @@ /* * This file is part of the KDE Baloo Project * Copyright 2018 Michael Heidelbach - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -24,14 +24,19 @@ #include +class QString; +class QDateTime; + class FileMetadataDateDisplayTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void shouldDisplayLongAndShortDates(); void shouldDisplayLongAndShortDates_data(); +private: + bool setFileTime(const QString& file, const QDateTime& filetime); }; diff --git a/autotests/filemetadatadatedisplaytest.cpp b/autotests/filemetadatadatedisplaytest.cpp --- a/autotests/filemetadatadatedisplaytest.cpp +++ b/autotests/filemetadatadatedisplaytest.cpp @@ -31,6 +31,13 @@ #include #include #include +#include + +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) +#include +#include +#include +#endif void initLocale() { @@ -40,22 +47,42 @@ QTEST_MAIN(FileMetadataDateDisplayTest) +bool FileMetadataDateDisplayTest::setFileTime(const QString& filePath, const QDateTime& fileTime) +{ + bool ret; +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + struct stat fileStat; + struct utimbuf newTimes; + const QByteArray file = QFile::encodeName(filePath); + stat(file, &fileStat); + + newTimes.actime = fileStat.st_atime; + newTimes.modtime = fileTime.toTime_t(); + ret = (utime(file, &newTimes) == 0); +#else + QScopedPointer file{new QFile(filePath)}; + file->open(QIODevice::ReadOnly); + ret = file->setFileTime(fileTime, QFileDevice::FileModificationTime); + file->close(); +#endif + return ret; +} + void FileMetadataDateDisplayTest::initTestCase() { qRegisterMetaType("KFileItemList"); QStandardPaths::setTestModeEnabled(true); - auto yesterday = QDateTime::currentDateTime().addDays(-1); - QFile* file(new QFile(QFINDTESTDATA("samplefiles/testtagged.m4a"))); - file->open(QIODevice::ReadOnly); - QVERIFY(file->setFileTime(yesterday, QFileDevice::FileModificationTime)); - file->close(); - delete file; - auto ancient = QDateTime::currentDateTime().addYears(-10); - file = new QFile(QFINDTESTDATA("samplefiles/testtagged.mp3")); - file->open(QIODevice::ReadOnly); - QVERIFY(file->setFileTime(ancient, QFileDevice::FileModificationTime)); - file->close(); + QVERIFY( + setFileTime(QFINDTESTDATA("samplefiles/testtagged.m4a"), + QDateTime::currentDateTime().addDays(-1)) + ); + + QVERIFY( + setFileTime(QFINDTESTDATA("samplefiles/testtagged.mp3"), + QDateTime::currentDateTime().addYears(-10)) + ); + } void FileMetadataDateDisplayTest::shouldDisplayLongAndShortDates_data()