Warning, file /plasma/kdeplasma-addons/runners/datetime/autotests/datetimerunnertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2023 Natalie Clarius <natalie_clarius@yahoo.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include <KRunner/AbstractRunnerTest>
0008 
0009 #include <QDateTime>
0010 #include <QTimeZone>
0011 #include <math.h>
0012 
0013 #include <qtestcase.h>
0014 
0015 class DateTimeRunnerTest : public AbstractRunnerTest
0016 {
0017     Q_OBJECT
0018 private Q_SLOTS:
0019     void initTestCase();
0020     void testLocalTimeInfo();
0021     void testRemoteTimeInfo();
0022     void testFindTimezones_data();
0023     void testFindTimezones();
0024 };
0025 
0026 #ifndef Q_OS_WIN
0027 void initEnv()
0028 {
0029     setenv("LC_ALL", "en_US.UTF-8", 1);
0030     setenv("TZ", "GMT", 1);
0031 }
0032 Q_CONSTRUCTOR_FUNCTION(initEnv)
0033 #endif
0034 
0035 void DateTimeRunnerTest::initTestCase()
0036 {
0037     initProperties();
0038 }
0039 
0040 void DateTimeRunnerTest::testLocalTimeInfo()
0041 {
0042     const QTime localTime = QDateTime::currentDateTime().time();
0043     const QString timeStr = QLocale().toString(localTime);
0044 
0045     launchQuery("time");
0046 
0047     QCOMPARE(manager->matches().count(), 1);
0048     QVERIFY(manager->matches().first().text().contains(timeStr));
0049 }
0050 
0051 void DateTimeRunnerTest::testRemoteTimeInfo()
0052 {
0053     const QTime remoteTime = QDateTime::currentDateTime().toTimeZone(QTimeZone("UTC-02:00")).time();
0054     const QString timeStr = QLocale().toString(remoteTime, QLocale::ShortFormat);
0055     const QString timeDiffStr = QString("2 hours earlier");
0056 
0057     launchQuery("time gmt-2");
0058     auto matches = manager->matches();
0059     std::sort(matches.begin(), matches.end(), [](const Plasma::QueryMatch &a, const Plasma::QueryMatch &b) {
0060         return a.relevance() > b.relevance();
0061     });
0062 
0063     QCOMPARE(manager->matches().count(), 1);
0064     QVERIFY(manager->matches().first().text().contains(timeStr));
0065     QVERIFY(manager->matches().first().text().contains(timeDiffStr));
0066 }
0067 
0068 void DateTimeRunnerTest::testFindTimezones()
0069 {
0070     QFETCH(QString, searchTerm);
0071     QFETCH(int, minMatchCount);
0072     QFETCH(QString, expectedTimezone);
0073 
0074     launchQuery("time " + searchTerm);
0075     auto matches = manager->matches();
0076     std::sort(matches.begin(), matches.end(), [](const Plasma::QueryMatch &a, const Plasma::QueryMatch &b) {
0077         return a.relevance() > b.relevance();
0078     });
0079 
0080     QVERIFY(matches.count() >= minMatchCount);
0081     QVERIFY(matches.first().text().contains(expectedTimezone));
0082 }
0083 
0084 void DateTimeRunnerTest::testFindTimezones_data()
0085 {
0086     QTest::addColumn<QString>("searchTerm");
0087     QTest::addColumn<int>("minMatchCount");
0088     QTest::addColumn<QString>("expectedTimezone");
0089 
0090     QTest::newRow("Should find time zones by city name") << "Harare" << 1 << "Harare";
0091     QTest::newRow("Should find time zones by long name") << "Central Africa Time" << 1 << "Central Africa Time";
0092     QTest::newRow("Should find time zones by short name") << "GMT+2" << 1 << "GMT+2";
0093     QTest::newRow("Should find time zones by offset name") << "UTC+02:00" << 1 << "UTC+02:00";
0094     QTest::newRow("Should find time zones by abbreviation, and show all time zones with that abbreviation") << "PST" << 1 << "(PST)";
0095     QTest::newRow("Should find time zones by country name, and show all time zones in that country") << "Brazil" << 2 << "Brazil - ";
0096 }
0097 
0098 QTEST_MAIN(DateTimeRunnerTest)
0099 
0100 #include "datetimerunnertest.moc"