File indexing completed on 2024-04-21 03:54:20

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KCountry>
0008 #include <KTimeZone>
0009 
0010 #include <QObject>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 #include <cmath>
0015 
0016 void initEnvironment()
0017 {
0018     qputenv("LANG", "fr_CH");
0019     QStandardPaths::setTestModeEnabled(true);
0020 }
0021 
0022 Q_CONSTRUCTOR_FUNCTION(initEnvironment)
0023 
0024 class KTimeZoneTest : public QObject
0025 {
0026     Q_OBJECT
0027 private Q_SLOTS:
0028     void testFromLocation_data()
0029     {
0030         QTest::addColumn<float>("lat");
0031         QTest::addColumn<float>("lon");
0032         QTest::addColumn<QString>("tz");
0033         QTest::addColumn<QString>("tzAlt"); // alternative equivalent answer in border regions
0034 
0035         QTest::newRow("invalid") << NAN << NAN << QString() << QString();
0036         QTest::newRow("out-of-coverage") << -90.0f << 180.0f << QString() << QString();
0037 
0038         // basic checks in all quadrants
0039         QTest::newRow("BR") << -8.0f << -35.0f << QStringLiteral("America/Recife") << QString();
0040         QTest::newRow("CA") << 44.0f << -79.5f << QStringLiteral("America/Toronto") << QString();
0041         QTest::newRow("DE") << 52.4f << 13.1f << QStringLiteral("Europe/Berlin") << QString();
0042         QTest::newRow("NZ") << -36.5f << 175.0f << QStringLiteral("Pacific/Auckland") << QString();
0043 
0044         // important KDE locations
0045         QTest::newRow("Randa") << 46.0998f << 7.781469f << QStringLiteral("Europe/Zurich") << QString();
0046 
0047         // Special case: Northern Vietnam has a Thai timezone
0048         QTest::newRow("VN") << 21.0f << 106.0f << QStringLiteral("Asia/Bangkok") << QString();
0049 
0050         // Eliat Airport (IL), close to JO, and with a minor timezone variation due to different weekends
0051         QTest::newRow("Eliat") << 29.72530f << 35.00598f << QString() << QStringLiteral("Asia/Jerusalem");
0052 
0053         // Cordoba (AR), AR has several sub-zones that are all equivalent
0054         QTest::newRow("AR") << -31.4f << -64.2f << QStringLiteral("America/Argentina/Buenos_Aires") << QStringLiteral("America/Argentina/Cordoba");
0055 
0056         // Maastricht (NL), very close to the BE border
0057         QTest::newRow("Maastricht") << 50.8505f << 5.6881f << QStringLiteral("Europe/Amsterdam") << QStringLiteral("Europe/Brussels");
0058         // Aachen, at the BE/DE/NL corner
0059         QTest::newRow("Aachen") << 50.7717f << 6.04235f << QStringLiteral("Europe/Berlin") << QStringLiteral("Europe/Brussels");
0060         // Geneva (CH), very close to the FR border
0061         QTest::newRow("Geneva") << 46.23213f << 6.10636f << QStringLiteral("Europe/Zurich") << QStringLiteral("Europe/Paris");
0062         // Busingen (DE), enclosed by CH
0063         QTest::newRow("Busingen") << 47.69947f << 8.68833f << QStringLiteral("Europe/Zurich") << QStringLiteral("Europe/Berlin");
0064         // Tijuana (MX), close to US
0065         QTest::newRow("Tijuana") << 32.54274f << -116.97505f << QStringLiteral("America/Tijuana") << QStringLiteral("America/Los_Angeles");
0066 
0067         // Baarle, the ultimate special case, NL/BE differs house by house
0068         QTest::newRow("Baarle") << 51.44344f << 4.93373f << QStringLiteral("Europe/Amsterdam") << QStringLiteral("Europe/Brussels");
0069     }
0070 
0071     void testFromLocation()
0072     {
0073         QFETCH(float, lat);
0074         QFETCH(float, lon);
0075         QFETCH(QString, tz);
0076         QFETCH(QString, tzAlt);
0077 
0078         const auto zoneId = QString::fromUtf8(KTimeZone::fromLocation(lat, lon));
0079         if (tzAlt.isEmpty()) {
0080             QCOMPARE(zoneId, tz);
0081         } else {
0082             qDebug() << zoneId;
0083             QVERIFY(zoneId == tz || zoneId == tzAlt);
0084         }
0085     }
0086 
0087     void testCountry()
0088     {
0089         // invalid
0090         QCOMPARE(KTimeZone::country(nullptr).alpha2(), QString());
0091         QCOMPARE(KTimeZone::country("").alpha2(), QString());
0092         QCOMPARE(KTimeZone::country("Moon/Dark_Side").alpha2(), QString());
0093 
0094         // 1:1 mapping
0095         QCOMPARE(KTimeZone::country("Europe/Brussels").alpha2(), QLatin1String("BE"));
0096         // timezones none of our methods would return
0097         QCOMPARE(KTimeZone::country("America/Argentina/Cordoba").alpha2(), QLatin1String("AR"));
0098         QCOMPARE(KTimeZone::country("Europe/Busingen").alpha2(), QLatin1String("DE"));
0099         // 1:N mapping
0100         QCOMPARE(KTimeZone::country("America/Toronto").alpha2(), QLatin1String("CA"));
0101         QCOMPARE(KTimeZone::country("Atlantic/Canary").alpha2(), QLatin1String("ES"));
0102         // ambiguous (also used in northern Vietnam)
0103         QCOMPARE(KTimeZone::country("Asia/Bangkok").alpha2(), QString());
0104     }
0105 };
0106 
0107 QTEST_GUILESS_MAIN(KTimeZoneTest)
0108 
0109 #include "ktimezonetest.moc"