File indexing completed on 2024-12-08 10:14:49
0001 /* 0002 SPDX-FileCopyrightText: 2018 Michael Heidelbach <ottwolt@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "filemetadatadatedisplaytest.h" 0008 0009 // This is an implementation detail of how dates are returned 0010 #include <KFormat> 0011 0012 #include <QDateTime> 0013 #include <QLabel> 0014 #include <QRegularExpression> 0015 #include <QScopedPointer> 0016 #include <QSignalSpy> 0017 #include <QStandardPaths> 0018 #include <QTest> 0019 0020 void initLocale() 0021 { 0022 QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); 0023 } 0024 Q_CONSTRUCTOR_FUNCTION(initLocale) 0025 0026 QTEST_MAIN(FileMetadataDateDisplayTest) 0027 0028 static QString filenameYesterday() 0029 { 0030 return QStringLiteral("file_yesterday.txt"); 0031 } 0032 0033 static QString filenameLongAgo() 0034 { 0035 return QStringLiteral("file_longago.txt"); 0036 } 0037 0038 static void setFileTime(const QString &filePath, const QDateTime &fileTime) 0039 { 0040 QFile file(filePath); 0041 QVERIFY2(file.open(QIODevice::ReadOnly), qUtf8Printable(filePath)); 0042 QVERIFY2(file.setFileTime(fileTime, QFileDevice::FileModificationTime), qUtf8Printable(filePath)); 0043 } 0044 0045 static void createFile(const QString &filePath) 0046 { 0047 QFile file(filePath); 0048 QVERIFY2(file.open(QIODevice::ReadWrite), qUtf8Printable(filePath)); 0049 QVERIFY2(file.write("dummy"), qUtf8Printable(filePath)); 0050 } 0051 0052 void FileMetadataDateDisplayTest::initTestCase() 0053 { 0054 qRegisterMetaType<KFileItemList>("KFileItemList"); 0055 QStandardPaths::setTestModeEnabled(true); 0056 0057 QVERIFY(m_testDir.isValid()); 0058 0059 auto now = QDateTime::currentDateTime(); 0060 createFile(m_testDir.filePath(filenameYesterday())); 0061 setFileTime(m_testDir.filePath(filenameYesterday()), now.addDays(-1)); 0062 0063 createFile(m_testDir.filePath(filenameLongAgo())); 0064 setFileTime(m_testDir.filePath(filenameLongAgo()), now.addYears(-10)); 0065 } 0066 0067 void FileMetadataDateDisplayTest::cleanupTestCase() 0068 { 0069 } 0070 0071 static QRegularExpression yesterdayShortRegex() 0072 { 0073 // the last space is a Narrow No-Break Space (NNBSP) caracter 0074 return QRegularExpression(QStringLiteral("Yesterday at ([1-2][0-9]|[1-9]):[0-5][0-9][ ][AP]M")); 0075 } 0076 0077 static QRegularExpression longAgoShortRegex() 0078 { 0079 // the last space group includes is a Narrow No-Break Space (NNBSP) caracter 0080 return QRegularExpression(QStringLiteral("([1-3][0-9]|[1-9]) at ([1-2][0-9]|[1-9]):[0-5][0-9][ ][AP]M")); 0081 } 0082 0083 void FileMetadataDateDisplayTest::validateDateFormats() 0084 { 0085 auto yesterday = QDateTime::currentDateTime().addDays(-1); 0086 auto long_ago = QDateTime::currentDateTime().addYears(-10); 0087 0088 // This tests only the "short form" regular expressions also found in shouldDisplayLongAndShortDates_data() 0089 auto yesterday_re = yesterdayShortRegex(); 0090 auto long_ago_re = longAgoShortRegex(); 0091 0092 KFormat form; 0093 0094 auto yesterday_s = form.formatRelativeDateTime(yesterday, QLocale::ShortFormat); 0095 auto long_ago_s = form.formatRelativeDateTime(long_ago, QLocale::ShortFormat); 0096 0097 QVERIFY2(yesterday_re.match(yesterday_s).hasMatch(), qPrintable(QStringLiteral("\"%1\" did not match \"%2\"").arg(yesterday_s, yesterday_re.pattern()))); 0098 QVERIFY2(long_ago_re.match(long_ago_s).hasMatch(), qPrintable(QStringLiteral("\"%1\" did not match \"%2\"").arg(long_ago_s, long_ago_re.pattern()))); 0099 } 0100 0101 void FileMetadataDateDisplayTest::shouldDisplayLongAndShortDates_data() 0102 { 0103 QTest::addColumn<Baloo::DateFormats>("format"); 0104 QTest::addColumn<QUrl>("file"); 0105 QTest::addColumn<QRegularExpression>("regex"); 0106 0107 auto urlLongAgo = QUrl::fromLocalFile(m_testDir.filePath(filenameLongAgo())); 0108 auto urlYesterday = QUrl::fromLocalFile(m_testDir.filePath(filenameYesterday())); 0109 0110 QTest::addRow("Short date, long ago") << Baloo::DateFormats::ShortFormat << urlLongAgo << longAgoShortRegex(); 0111 0112 QTest::addRow("Short date, yesterday") << Baloo::DateFormats::ShortFormat << urlYesterday << yesterdayShortRegex(); 0113 0114 // the last space group includes is a Narrow No-Break Space (NNBSP) caracter 0115 QTest::addRow("Long date, long ago") << Baloo::DateFormats::LongFormat << urlLongAgo 0116 << QRegularExpression(QStringLiteral( 0117 "[A-Z][a-z]+, [A-Z][a-z]+ ([1-3][0-9]|[1-9]), 20[0-9]{2} at (1[0-2]|[1-9]):[0-5][0-9][ ][AP]M")); 0118 0119 // the last space group includes is a Narrow No-Break Space (NNBSP) caracter 0120 QTest::addRow("Long date, yesterday") << Baloo::DateFormats::LongFormat << urlYesterday 0121 << QRegularExpression(QStringLiteral("Yesterday at (1[0-2]|[1-9]):[0-5][0-9][ ][AP]M")); 0122 } 0123 0124 void FileMetadataDateDisplayTest::shouldDisplayLongAndShortDates() 0125 { 0126 QFETCH(Baloo::DateFormats, format); 0127 QFETCH(QUrl, file); 0128 QFETCH(QRegularExpression, regex); 0129 0130 const auto widget = new Baloo::FileMetaDataWidget(); 0131 widget->setDateFormat(format); 0132 QSignalSpy spy(widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); 0133 widget->setItems({KFileItem{file}}); 0134 QVERIFY(spy.wait()); 0135 0136 auto dateWidget = widget->findChild<QLabel *>(QStringLiteral("kfileitem#modified"), Qt::FindDirectChildrenOnly); 0137 QVERIFY2(dateWidget, "Date widget not found"); 0138 QVERIFY2(regex.match(dateWidget->text()).hasMatch(), qPrintable(QStringLiteral("\"%1\" did not match \"%2\"").arg(dateWidget->text(), regex.pattern()))); 0139 } 0140 0141 #include "moc_filemetadatadatedisplaytest.cpp"