File indexing completed on 2022-11-29 18:25:15

0001 /*
0002  * This file is part of the KDE libraries.
0003  * Copyright 2009  Michael Leupold <lemma@confuego.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) version 3, or any
0009  * later version accepted by the membership of KDE e.V. (or its
0010  * successor approved by the membership of KDE e.V.), which shall
0011  * act as a proxy defined in Section 6 of version 3 of the license.
0012  *
0013  * This library is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  * Lesser General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU Lesser General Public
0019  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020  */
0021 
0022 #include "klocaletimeformattest.h"
0023 
0024 #include <kconfig.h>
0025 #include <kconfiggroup.h>
0026 #include "kdayperiod_p.h"
0027 
0028 #include <QStandardPaths>
0029 #include <QStringList>
0030 #include <QTest>
0031 
0032 static QString loadTimeFormat(const QString &country)
0033 {
0034     const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
0035                          QString::fromLatin1("kf5/locale/countries/%1/country.desktop")
0036                          .arg(country));
0037     if (file.isEmpty()) {
0038         return QString();
0039     }
0040     KConfig entryFile(file);
0041     KConfigGroup entry(&entryFile, "KCM Locale");
0042     return entry.readEntry("TimeFormat", QString());
0043 }
0044 
0045 void KLocaleTimeFormatTest::initTestCase()
0046 {
0047     // get countries with according time formats
0048     // for formatAndReadAllCountriesTest().
0049     QStringList countries(KLocale::global()->allCountriesList());
0050     QString timeFormat;
0051     Q_FOREACH (const QString &c, countries) {
0052         timeFormat = loadTimeFormat(c);
0053         if (!timeFormat.isEmpty()) {
0054             m_allFormats.insert(c, timeFormat);
0055         }
0056     }
0057 
0058     // additional test-data (for (format|read)TimeTest)
0059     m_timeFormats["ampm"].timeFormat = "%I:%M:%S %p";
0060     m_timeFormats["ampm"].exp[KLocale::TimeDefault] = "03:59:59 PM";
0061     m_timeFormats["ampm"].exp[KLocale::TimeWithoutAmPm] = "03:59:59";
0062     m_timeFormats["ampm"].exp[KLocale::TimeDuration] = "15:59:59";
0063     m_timeFormats["ampm"].exp[KLocale::TimeWithoutSeconds] = "03:59 PM";
0064     m_timeFormats["ampm"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "03:59";
0065     m_timeFormats["ampm"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15:59";
0066     m_timeFormats["ampm"].exp[KLocale::TimeFoldHours] = "959:59";
0067     m_timeFormats["ampm"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0068 
0069     m_timeFormats["24h"].timeFormat = "%H:%M:%S";
0070     m_timeFormats["24h"].exp[KLocale::TimeDefault] = "15:59:59";
0071     m_timeFormats["24h"].exp[KLocale::TimeWithoutAmPm] = "15:59:59";
0072     m_timeFormats["24h"].exp[KLocale::TimeDuration] = "15:59:59";
0073     m_timeFormats["24h"].exp[KLocale::TimeWithoutSeconds] = "15:59";
0074     m_timeFormats["24h"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "15:59";
0075     m_timeFormats["24h"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15:59";
0076     m_timeFormats["24h"].exp[KLocale::TimeFoldHours] = "959:59";
0077     m_timeFormats["24h"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0078 
0079     m_timeFormats["ampm2"].timeFormat = "%l:%M:%S %p";
0080     m_timeFormats["ampm2"].exp[KLocale::TimeDefault] = "3:59:59 PM";
0081     m_timeFormats["ampm2"].exp[KLocale::TimeWithoutAmPm] = "3:59:59";
0082     m_timeFormats["ampm2"].exp[KLocale::TimeDuration] = "15:59:59";
0083     m_timeFormats["ampm2"].exp[KLocale::TimeWithoutSeconds] = "3:59 PM";
0084     m_timeFormats["ampm2"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "3:59";
0085     m_timeFormats["ampm2"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15:59";
0086     m_timeFormats["ampm2"].exp[KLocale::TimeFoldHours] = "959:59";
0087     m_timeFormats["ampm2"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0088 
0089     m_timeFormats["ag"].timeFormat = "%I.%M.%S.%p";
0090     m_timeFormats["ag"].exp[KLocale::TimeDefault] = "03.59.59.PM";
0091     m_timeFormats["ag"].exp[KLocale::TimeWithoutAmPm] = "03.59.59";
0092     m_timeFormats["ag"].exp[KLocale::TimeDuration] = "15.59.59";
0093     m_timeFormats["ag"].exp[KLocale::TimeWithoutSeconds] = "03.59.PM";
0094     m_timeFormats["ag"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "03.59";
0095     m_timeFormats["ag"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15.59";
0096     m_timeFormats["ag"].exp[KLocale::TimeFoldHours] = "959.59";
0097     m_timeFormats["ag"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0098 
0099     m_timeFormats["ampre"].timeFormat = "%p %l-%M-%S";
0100     m_timeFormats["ampre"].exp[KLocale::TimeDefault] = "PM 3-59-59";
0101     m_timeFormats["ampre"].exp[KLocale::TimeWithoutAmPm] = "3-59-59";
0102     m_timeFormats["ampre"].exp[KLocale::TimeDuration] = "15-59-59";
0103     m_timeFormats["ampre"].exp[KLocale::TimeWithoutSeconds] = "PM 3-59";
0104     m_timeFormats["ampre"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "3-59";
0105     m_timeFormats["ampre"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15-59";
0106     m_timeFormats["ampre"].exp[KLocale::TimeFoldHours] = "959-59";
0107     m_timeFormats["ampre"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0108 
0109     m_timeFormats["ampresep"].timeFormat = "%p-%l-%M-%S";
0110     m_timeFormats["ampresep"].exp[KLocale::TimeDefault] = "PM-3-59-59";
0111     m_timeFormats["ampresep"].exp[KLocale::TimeWithoutAmPm] = "3-59-59";
0112     m_timeFormats["ampresep"].exp[KLocale::TimeDuration] = "15-59-59";
0113     m_timeFormats["ampresep"].exp[KLocale::TimeWithoutSeconds] = "PM-3-59";
0114     m_timeFormats["ampresep"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = "3-59";
0115     m_timeFormats["ampresep"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeDuration] = "15-59";
0116     m_timeFormats["ampresep"].exp[KLocale::TimeFoldHours] = "959-59";
0117     m_timeFormats["ampresep"].exp[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = "959";
0118 }
0119 
0120 void KLocaleTimeFormatTest::formatTimeTest()
0121 {
0122     KLocale locale(*KLocale::global());
0123     QTime time(15, 59, 59);
0124 
0125     QMap<QString, TimeFormatTestValue>::const_iterator it;
0126     QMap<QString, TimeFormatTestValue>::const_iterator end(m_timeFormats.constEnd());
0127     for (it = m_timeFormats.constBegin(); it != end; ++it) {
0128         locale.setTimeFormat(it.value().timeFormat);
0129 
0130         QMap<KLocale::TimeFormatOptions, QString>::const_iterator it2;
0131         QMap<KLocale::TimeFormatOptions, QString>::const_iterator end2(it.value().exp.constEnd());
0132         for (it2 = it.value().exp.constBegin(); it2 != end2; ++it2) {
0133             QCOMPARE(locale.formatLocaleTime(time, it2.key()), it2.value());
0134         }
0135     }
0136 }
0137 
0138 void KLocaleTimeFormatTest::readTimeTest()
0139 {
0140     KLocale locale(*KLocale::global());
0141     QMap<KLocale::TimeFormatOptions, QTime> timeMap;
0142     timeMap[KLocale::TimeDefault] = QTime(15, 59, 59);
0143     timeMap[KLocale::TimeWithoutSeconds] = QTime(15, 59, 0);
0144     timeMap[KLocale::TimeDuration] = QTime(15, 59, 59);
0145     timeMap[KLocale::TimeDuration | KLocale::TimeWithoutSeconds] = QTime(15, 59, 0);
0146     timeMap[KLocale::TimeWithoutAmPm] = QTime(3, 59, 59);
0147     timeMap[KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm] = QTime(3, 59, 0);
0148     timeMap[KLocale::TimeFoldHours] = QTime(15, 59, 59);
0149     timeMap[KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours] = QTime(15, 59);
0150 
0151     QMap<QString, TimeFormatTestValue>::const_iterator it;
0152     QMap<QString, TimeFormatTestValue>::const_iterator end(m_timeFormats.constEnd());
0153     for (it = m_timeFormats.constBegin(); it != end; ++it) {
0154         locale.setTimeFormat(it.value().timeFormat);
0155 
0156         bool ampm = locale.timeFormat().contains(QLatin1String("%p"));
0157 
0158         QMap<KLocale::TimeFormatOptions, QString>::const_iterator it2;
0159         QMap<KLocale::TimeFormatOptions, QString>::const_iterator end2(it.value().exp.constEnd());
0160         for (it2 = it.value().exp.constBegin(); it2 != end2; ++it2) {
0161             KLocale::TimeFormatOptions options = it2.key();
0162             if ((options & KLocale::TimeDuration) != KLocale::TimeDuration && !ampm) {
0163                 options &= ~KLocale::TimeWithoutAmPm;
0164             }
0165             if (!timeMap.contains(options)) {
0166                 QWARN("Missing entry in timeMap, skipping");
0167                 continue;
0168             }
0169             bool ok;
0170             QCOMPARE(locale.readLocaleTime(it2.value(), &ok, it2.key()), timeMap[options]);
0171             QVERIFY(ok);
0172         }
0173     }
0174 }
0175 
0176 void KLocaleTimeFormatTest::formatAndReadAllCountriesTest()
0177 {
0178     KLocale locale(*KLocale::global());
0179 
0180     // for each of the countries, format with any options, read again
0181     // and check if times match.
0182     QTime timeS(15, 59, 59);
0183     QTime timeNs(15, 59, 0);
0184     QTime timeAm(3, 59, 59);
0185     QTime timeNsAm(3, 59, 0);
0186 
0187     QMap<QString, QString>::const_iterator it(m_allFormats.constBegin());
0188     QMap<QString, QString>::const_iterator end(m_allFormats.constEnd());
0189     for (; it != end; ++it) {
0190         locale.setTimeFormat(it.value());
0191 
0192         bool ampm = locale.timeFormat().contains(QLatin1String("%p"));
0193 
0194         bool ok;
0195         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeDefault), &ok, KLocale::TimeDefault), timeS);
0196         QVERIFY(ok);
0197         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeWithoutAmPm), &ok, KLocale::TimeWithoutAmPm), ampm ? timeAm : timeS);
0198         QVERIFY(ok);
0199         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeDuration), &ok, KLocale::TimeDuration), timeS);
0200         QVERIFY(ok);
0201 
0202         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeWithoutSeconds), &ok, KLocale::TimeWithoutSeconds), timeNs);
0203         QVERIFY(ok);
0204         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm), &ok, KLocale::TimeWithoutSeconds | KLocale::TimeWithoutAmPm), ampm ? timeNsAm : timeNs);
0205         QVERIFY(ok);
0206         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeWithoutSeconds | KLocale::TimeDuration), &ok, KLocale::TimeWithoutSeconds | KLocale::TimeDuration), timeNs);
0207         QVERIFY(ok);
0208         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeFoldHours), &ok, KLocale::TimeFoldHours), timeS);
0209         QVERIFY(ok);
0210         QCOMPARE(locale.readLocaleTime(locale.formatLocaleTime(timeS, KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours), &ok, KLocale::TimeWithoutSeconds | KLocale::TimeFoldHours), timeNs);
0211     }
0212 }
0213 
0214 QTEST_MAIN(KLocaleTimeFormatTest)