File indexing completed on 2024-11-24 04:44:10

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "timezonetest.h"
0008 #include "conversion/commonconversion.h"
0009 #include "conversion/timezoneconverter.h"
0010 #include "kolabformat/errorhandler.h"
0011 #include "kolabformat/kolabobject.h"
0012 #include "testutils.h"
0013 
0014 #include <KCalendarCore/Event>
0015 #include <KCalendarCore/ICalFormat>
0016 #include <QTest>
0017 
0018 void TimezoneTest::initTestCase()
0019 {
0020 }
0021 
0022 void TimezoneTest::testFromName()
0023 {
0024     TimezoneConverter converter;
0025     const QString timezone = converter.normalizeTimezone(QStringLiteral("(GMT+01.00) Sarajevo/Warsaw/Zagreb"));
0026     QCOMPARE(timezone, QLatin1StringView("Europe/Sarajevo"));
0027 }
0028 
0029 void TimezoneTest::testFromHardcodedList_data()
0030 {
0031     QTest::addColumn<QString>("timezone");
0032 
0033     QTest::newRow("1") << QStringLiteral("(GMT+01:00) West Central Africa");
0034     QTest::newRow("2") << QStringLiteral("(GMT-04:00) Atlantic Time (Canada)");
0035     QTest::newRow("3") << QStringLiteral("(GMT-06:00) Saskatchewan");
0036     QTest::newRow("4") << QStringLiteral("(GMT-01:00) Cape Verde Islands");
0037     QTest::newRow("5") << QStringLiteral("(GMT-06:00) Central America");
0038     QTest::newRow("6") << QStringLiteral("(GMT-06:00) Central Time (US and Canada)");
0039     //     QTest::newRow( "7" ) << QString::fromLatin1("(GMT-12:00) International Date Line West"); //Not mappable
0040     QTest::newRow("8") << QStringLiteral("(GMT-05:00) Eastern Time (US and Canada)");
0041     //     QTest::newRow( "9" ) << QString::fromLatin1("(GMT-02:00) Mid-Atlantic"); //Not mappable
0042     QTest::newRow("10") << QStringLiteral("(GMT-07:00) Mountain Time (US and Canada)");
0043     QTest::newRow("11") << QStringLiteral("(GMT-03:30) Newfoundland and Labrador");
0044     QTest::newRow("12") << QStringLiteral("(GMT-08:00) Pacific Time (US and Canada); Tijuana");
0045     QTest::newRow("13") << QStringLiteral("(GMT-11:00) Midway Island, Samoa");
0046     QTest::newRow("14") << QStringLiteral("W. Europe Standard Time");
0047     QTest::newRow("15") << QStringLiteral("(GMT+1.00) Sarajevo/Warsaw/Zagreb");
0048     // Lotus notes uses it's own set of specifiers
0049     //     QTest::newRow( "Lotus Notes" ) << QString::fromLatin1("W. Europe");
0050     //     QTest::newRow( "Google UTC offset" ) << QString::fromLatin1("2013-10-23T04:00:00+02:00");
0051 }
0052 
0053 void TimezoneTest::testFromHardcodedList()
0054 {
0055     TimezoneConverter converter;
0056     QFETCH(QString, timezone);
0057     const QString tz = converter.normalizeTimezone(timezone);
0058     qDebug() << tz;
0059     QVERIFY(!tz.isEmpty());
0060     QVERIFY(tz != timezone);
0061 }
0062 
0063 void TimezoneTest::testKolabObjectWriter()
0064 {
0065     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
0066     event->setDtStart(QDateTime(QDate(2012, 11, 11), QTime(1, 1), QTimeZone("Africa/Lagos")));
0067     KMime::Message::Ptr msg = Kolab::KolabObjectWriter::writeEvent(event);
0068     Kolab::KolabObjectReader reader(msg);
0069     KCalendarCore::Event::Ptr result = reader.getEvent();
0070     qDebug() << result->dtStart().timeZone().id();
0071     QCOMPARE(result->dtStart().timeZone().id(), QTimeZone("Africa/Lagos").id());
0072 }
0073 
0074 // void TimezoneTest::testKolabObjectReader()
0075 // {
0076 //     const Kolab::Version version = Kolab::KolabV3;
0077 //     const Kolab::ObjectType type = Kolab::EventObject;
0078 //     QString icalFileName = TESTFILEDIR+QString::fromLatin1("timezone/windowsTimezone.ics"); //To compare
0079 //     QString mimeFileName = TESTFILEDIR+QString::fromLatin1("timezone/windowsTimezoneV3.mime"); //For parsing
0080 //
0081 //     //Parse mime message
0082 //     bool ok = false;
0083 //     const KMime::Message::Ptr &msg = readMimeFile( mimeFileName, ok );
0084 //     QVERIFY(ok);
0085 //     Kolab::KolabObjectReader reader;
0086 //     Kolab::ObjectType t = reader.parseMimeMessage(msg);
0087 //     QCOMPARE(t, type);
0088 //     QCOMPARE(reader.getVersion(), version);
0089 //     QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
0090 //
0091 //     KCalendarCore::Incidence::Ptr convertedIncidence = reader.getIncidence();
0092 //     qDebug() << "read incidence";
0093 //
0094 //     //Parse ICalFile for comparison
0095 //     QFile icalFile( icalFileName );
0096 //     QVERIFY( icalFile.open( QFile::ReadOnly ) );
0097 //     KCalendarCore::ICalFormat format;
0098 //     KCalendarCore::Incidence::Ptr realIncidence( format.fromString( QString::fromUtf8( icalFile.readAll() ) ) );
0099 //
0100 //     // fix up the converted incidence for comparison
0101 //     normalizeIncidence(convertedIncidence);
0102 //     normalizeIncidence(realIncidence);
0103 //
0104 //     // recurrence objects are created on demand, but KCalendarCore::Incidence::operator==() doesn't take that into account
0105 //     // so make sure both incidences have one
0106 //     realIncidence->recurrence();
0107 //     convertedIncidence->recurrence();
0108 //
0109 //     realIncidence->setLastModified(convertedIncidence->lastModified());
0110 //
0111 //     //The following test is just for debugging and not really relevant
0112 //     if ( *(realIncidence.data()) != *(convertedIncidence.data()) ) {
0113 //         showDiff(format.toString( realIncidence ), format.toString( convertedIncidence ));
0114 //     }
0115 //     QVERIFY( *(realIncidence.data()) ==  *(convertedIncidence.data()) );
0116 // }
0117 
0118 void TimezoneTest::testFindLegacyTimezone()
0119 {
0120     const QString normalized = TimezoneConverter::normalizeTimezone(QStringLiteral("US/Pacific"));
0121     qDebug() << normalized;
0122     QEXPECT_FAIL("", "Currently broken", Continue);
0123     QVERIFY(!normalized.isEmpty());
0124 }
0125 
0126 void TimezoneTest::testIgnoreInvalidTimezone()
0127 {
0128     const QString normalized = TimezoneConverter::normalizeTimezone(QStringLiteral("FOOOOBAR"));
0129     qDebug() << normalized;
0130     QVERIFY(normalized.isEmpty());
0131 }
0132 
0133 void TimezoneTest::testUTCOffset()
0134 {
0135     const Kolab::cDateTime expected(2013, 10, 23, 2, 0, 0, true);
0136     const QDateTime input(QDateTime::fromString(QStringLiteral("2013-10-23T04:00:00+02:00"), Qt::ISODate));
0137     const Kolab::cDateTime result = Kolab::Conversion::fromDate(input, false);
0138     QVERIFY(!Kolab::ErrorHandler::instance().errorOccured());
0139     QCOMPARE(result, expected);
0140 }
0141 
0142 void TimezoneTest::localTimezone()
0143 {
0144     {
0145         const Kolab::cDateTime result = Kolab::Conversion::fromDate(QDateTime(QDate(2013, 10, 10), QTime(2, 0, 0), Qt::LocalTime), false);
0146         QVERIFY(!result.timezone().empty());
0147         QVERIFY(!Kolab::ErrorHandler::instance().errorOccured());
0148     }
0149     {
0150         const Kolab::cDateTime result = Kolab::Conversion::fromDate(QDateTime(QDate(2013, 10, 10), QTime(2, 0, 0)), false);
0151         QVERIFY(!Kolab::ErrorHandler::instance().errorOccured());
0152     }
0153     {
0154         const Kolab::cDateTime result = Kolab::Conversion::fromDate(QDateTime(QDate(2013, 10, 10), QTime(2, 0, 0), QTimeZone()), false);
0155         QVERIFY(result.timezone().empty());
0156         QVERIFY(!Kolab::ErrorHandler::instance().errorOccured());
0157     }
0158     {
0159         QDateTime dt(QDate(2013, 10, 10), QTime(2, 0, 0), QTimeZone("/etc/localzone"));
0160         const Kolab::cDateTime result = Kolab::Conversion::fromDate(dt, false);
0161         qDebug() << result.timezone();
0162         QVERIFY(result.timezone().empty());
0163         QVERIFY(!Kolab::ErrorHandler::instance().errorOccured());
0164     }
0165 }
0166 
0167 QTEST_MAIN(TimezoneTest)
0168 
0169 #include "moc_timezonetest.cpp"