File indexing completed on 2024-05-12 05:17:30

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../src/lib/jsapi/jsonld.h"
0008 
0009 #include <QJSEngine>
0010 #include <QJSValue>
0011 #include <QObject>
0012 #include <QTest>
0013 
0014 #include <cmath>
0015 
0016 using namespace KItinerary;
0017 
0018 #define s(x) QStringLiteral(x)
0019 
0020 class JsApiTest : public QObject
0021 {
0022     Q_OBJECT
0023 private Q_SLOTS:
0024     void initTestCase()
0025     {
0026         // use some exotic locale and timezone to ensure the date/time parsing doesn't just work by luck
0027         QLocale::setDefault(QLocale(QStringLiteral("fr_FR")));
0028         qputenv("TZ", "UTC");
0029     }
0030 
0031     void testToDateTime_data()
0032     {
0033         QTest::addColumn<QString>("input");
0034         QTest::addColumn<QString>("format");
0035         QTest::addColumn<QString>("locale");
0036         QTest::addColumn<QDateTime>("result");
0037 
0038         QTest::newRow("iso") << s("2018-06-22 19:37") << s("yyyy-MM-dd hh:mm") << s("en") << QDateTime({2018, 6, 22}, {19, 37});
0039         QTest::newRow("2 digit year") << s("18-06-22 19:37") << s("yy-MM-dd hh:mm") << s("en") << QDateTime({2018, 6, 22}, {19, 37});
0040         QTest::newRow("short month name en") << s("2018 Mar 22 19:37") << s("yyyy MMM dd hh:mm") << s("en") << QDateTime({2018, 3, 22}, {19, 37});
0041         QTest::newRow("short month name sv") << s("2018 Mar 22 19:37") << s("yyyy MMM dd hh:mm") << s("sv_SE") << QDateTime({2018, 3, 22}, {19, 37});
0042         QTest::newRow("short month name de") << s("2018 Mai 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 5, 22}, {19, 37});
0043         QTest::newRow("short month name nl") << s("30 okt 2023 22:34") << s("d MMM yyyy hh:mm") << s("nl") << QDateTime({2023, 10, 30}, {22, 34});
0044         QTest::newRow("missing year") << s("1 22 19:37") << s("M dd hh:mm") << s("en") << QDateTime({2019, 1, 22}, {19, 37});
0045         QTest::newRow("December short sv") << s("4 dec 2012 07:05") << s("d MMM yyyy hh:mm") << s("sv_SE") << QDateTime({2012, 12, 4}, {7, 5});
0046         QTest::newRow("time only") << s("19:08") << s("hh:mm") << s("en") << QDateTime({1970, 1, 1}, {19, 8});
0047 
0048         // commonly used abbreviations in de differ from QLocale
0049         QTest::newRow("short month de 1") << s("2018 Mär 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 3, 22}, {19, 37});
0050         QTest::newRow("short month de 2") << s("2018 Mrz 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 3, 22}, {19, 37});
0051         QTest::newRow("short month de 3") << s("2018 Jun 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 6, 22}, {19, 37});
0052         QTest::newRow("short month de 4") << s("2018 Jul 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 7, 22}, {19, 37});
0053         QTest::newRow("short month de 5") << s("2018 Okt 22 19:37") << s("yyyy MMM dd hh:mm") << s("de") << QDateTime({2018, 10, 22}, {19, 37});
0054     }
0055 
0056     void testToDateTime()
0057     {
0058         QFETCH(QString, input);
0059         QFETCH(QString, format);
0060         QFETCH(QString, locale);
0061         QFETCH(QDateTime, result);
0062 
0063         JsApi::JsonLd jsonLd(nullptr);
0064         jsonLd.setContextDate({{2018, 4, 1}, {}});
0065         QCOMPARE(jsonLd.toDateTime(input, QJSValue(format), QJSValue(locale)), result);
0066     }
0067 
0068     void testToGeoCoordinates_data()
0069     {
0070         QTest::addColumn<QString>("url");
0071         QTest::addColumn<double>("latitude");
0072         QTest::addColumn<double>("longitude");
0073 
0074         QTest::newRow("empty") << QString() << (double)NAN << (double)NAN;
0075         QTest::newRow("non-map") << s("http://www.kde.org") << (double)NAN << (double)NAN;
0076         QTest::newRow("google maps 1") << s("https://www.google.com/maps/place/48.182849,16.378636") << 48.182849 << 16.378636;
0077         QTest::newRow("google maps 1 no ssl") << s("http://www.google.com/maps/place/48.182849,16.378636") << 48.182849 << 16.378636;
0078         QTest::newRow("google maps 2") << s("http://maps.google.fr/?ll=-48.8471603393555,-2.37761735916138&z=19") << -48.8471603393555 << -2.37761735916138;
0079         QTest::newRow("google maps 3") << s("https://maps.google.com/maps?f=q&hl=en&q=52.434788,-13.544329") << 52.434788 << -13.544329;
0080         QTest::newRow("google maps api 1") << s("http://maps.googleapis.com/maps/api/staticmap?language=en-gb&size=280x120&center=69.54363918781344,31.028996706008911&sensor=false&markers=color:0x0896FF%7C69.54363918781344,31.028996706008911&zoom=14&client=gme-booking&signature=xxxxxxxxxxxxxxxxxxxxxx") << 69.54363918781344 << 31.028996706008911;
0081     }
0082 
0083     void testToGeoCoordinates()
0084     {
0085         QFETCH(QString, url);
0086         QFETCH(double, latitude);
0087         QFETCH(double, longitude);
0088         QCOMPARE(std::isnan(latitude), std::isnan(longitude));
0089 
0090         QJSEngine engine;
0091         JsApi::JsonLd api(&engine);
0092         const auto geo = api.toGeoCoordinates(url);
0093         QVERIFY((geo.isUndefined() && std::isnan(latitude)) || (!geo.isUndefined() && !std::isnan(latitude)));
0094         if (geo.isUndefined()) {
0095             return;
0096         }
0097 
0098         QCOMPARE(geo.property(s("latitude")).toNumber(), latitude);
0099         QCOMPARE(geo.property(s("longitude")).toNumber(), longitude);
0100     }
0101 };
0102 
0103 QTEST_GUILESS_MAIN(JsApiTest)
0104 
0105 #include "jsapitest.moc"